code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.stdio;
import std.string;
import std.conv;
void main(){
string[] input = split(readln());
int x = to!int(input[0]);
int y = to!int(input[1]);
writeln(x * y, ' ', 2 * (x + y));
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
void main() {
int n;
scan(n);
int t, a;
scan(t, a);
t *= 1000;
a *= 1000;
auto h = readln.split.to!(int[]);
int ans = -1;
int dif = 10^^9;
foreach (i ; 0 .. n) {
if (dif > abs(t - 6*h[i] - a)) {
ans = i;
dif = abs(t - 6*h[i] - a);
}
}
writeln(ans + 1);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop;
// dfmt on
void main()
{
long N, L;
scan(N, L);
auto A = aryread();
foreach (i; 0 .. N - 1)
{
if (L <= A[i] + A[i + 1])
{
writeln("Possible");
auto ans = new long[](N - 1);
long ptr;
ans[ptr++] = i;
foreach (j; i + 1 .. N - 1)
{
ans[ptr++] = j;
}
foreach_reverse (j; 0 .. i)
{
ans[ptr++] = j;
}
foreach_reverse (a; ans)
{
writeln(a + 1);
}
return;
}
}
writeln("Impossible");
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
// dfmt on
void main()
{
long N = lread();
auto A = aryread();
long[] ans;
auto B = new long[](N);
foreach_reverse (i; 0 .. N)
{
long tmp;
long j = 1;
while (j * (i + 1) - 1 < N)
{
tmp = tmp ^ B[j * (i + 1) - 1];
j++;
}
if (tmp != A[i])
{
B[i] = 1;
ans ~= i + 1;
}
}
writeln(ans.length);
if (ans.empty)
return;
foreach (i; 0 .. ans.length - 1)
write(ans[i], " ");
writeln(ans[$ - 1]);
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
immutable mod = 10^^9 + 7;
void main() {
int n, m;
scan(n, m);
auto adj = new bool[][](n, n);
foreach (i ; 0 .. m) {
int ai, bi;
scan(ai, bi);
ai--; bi--;
adj[ai][bi] = adj[bi][ai] = 1;
}
int ans;
void dfs(bool[] visited, int node) {
visited[node] = 1;
foreach (i ; 0 .. n) {
if (adj[node][i] && !visited[i]) {
dfs(visited, i);
}
}
}
foreach (i ; 0 .. n) {
foreach (j ; i + 1 .. n) {
if (adj[i][j]) {
adj[i][j] = adj[j][i] = 0;
auto visited = new bool[](n);
dfs(visited, 0);
ans += !all(visited);
adj[i][j] = adj[j][i] = 1;
}
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
long n, x;
scan(n, x);
auto l = aryread();
long sum_x;
long[] ans;
ans ~= 0;
// writeln(ans);
foreach (i; 0 .. n)
{
sum_x += l[i];
ans ~= sum_x;
}
// writeln(ans);
long cnt;
foreach (i; 0 .. ans.length)
{
if (ans[i] <= x)
{
cnt += 1;
}
}
writeln(cnt);
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main(){
auto a = readln.chomp.to!int;
auto b = a % 10;
auto c = a / 10 % 10;
auto d = a / 100 % 10;
auto e = a /1000 % 10;
writeln(b==c&&c==d||c==d&&d==e ? "Yes":"No");
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Clue = Tuple!(long, "x", long, "y", long, "h");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
long cnt;
void main()
{
long n, x;
scan(n, x);
auto a = aryread();
long cnt;
if (a[0] > x)
{
cnt += a[0] - x;
a[0] = x;
}
foreach(i; iota(n - 1))
{
auto dif = a[i] + a[i + 1] - x;
if(dif > 0)
{
cnt += dif;
a[i + 1] -= dif;
}
}
cnt.writeln();
}
void dfs(T)(T g, long current, bool[] visit)
{
// current.writeln();
foreach (e; g[current])
{
if (!visit[e])
{
visit[e] = true;
dfs(g, e, visit);
visit[e] = false;
}
}
if (visit.all_true())
cnt++;
}
bool all_true(bool[] a)
{
bool ret = true;
foreach (e; a)
ret &= e;
return ret;
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}}
void main()
{
int n, t; readV(n, t);
int[] w, v; readC(n, w, v);
auto dp = new long[][](n+1, t+1);
foreach (ref dpi; dp) dpi[] = -1;
dp[0][0] = 0;
foreach (i; 0..n)
foreach (j; 0..t+1) {
dp[i+1][j] = dp[i][j];
if (j >= w[i] && dp[i][j-w[i]] >= 0)
dp[i+1][j] = max(dp[i+1][j], dp[i][j-w[i]] + v[i]);
}
writeln(dp[$-1].reduce!max);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
enum P = 10L^^9+7;
void main()
{
auto hwk = readln.split.to!(int[]);
auto H = hwk[0];
auto W = hwk[1];
auto K = hwk[2];
long[] A, B;
A.length = W;
B.length = W;
A[0] = 1;
foreach (_; 0..H) {
B[] = 0;
foreach (w; 0..1<<(W-1)) {
bool nxt;
foreach (i; 0..W-2) if (!!(w & (1<<i)) && !!(w & (1<<(i+1)))) {
nxt = true;
break;
}
if (nxt) continue;
foreach (i; 0..W) {
if (i > 0 && (w & (1<<(i-1)))) {
B[i-1] = (B[i-1] + A[i]) % P;
} else if (i < W-1 && (w & (1<<i))) {
B[i+1] = (B[i+1] + A[i]) % P;
} else {
B[i] = (B[i] + A[i]) % P;
}
}
}
A[] = B[];
}
writeln(B[K-1]);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.container;
void main()
{
foreach (s; stdin.byLine) {
if (s == "-") break;
int m = readln.chomp.to!int;
foreach (i; 0..m) {
int h = readln.chomp.to!int;
s = s[h..$] ~ s[0..h];
}
s.writeln();
}
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto n = readln.chomp.to!int;
auto a = readln.chomp.split.to!(int[]);
int[9] res;
foreach (i; 0..n) {
foreach (j; 1..9) {
if (a[i] < 400 * j) {
res[j-1]++;
break;
}
}
if (a[i] >= 3200) {
res[8]++;
}
}
int cnt;
foreach (i; 0..8) {
if (res[i]) {
cnt++;
}
}
int r_min, r_max;
r_min = cnt;
if (!cnt && res[8]) {
r_min = 1;
}
r_max = cnt + res[8];
writeln(r_min, " ", r_max);
}
|
D
|
import std.stdio, std.conv, std.string;
void main() {
uint n = readln.strip.to!int;
writeln(n & 1 ? n / 2 + 1 : n / 2);
}
|
D
|
//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.typecons;
class InputReader {
private:
ubyte[] p;
ubyte[] buffer;
size_t cur;
public:
this () {
buffer = uninitializedArray!(ubyte[])(16<<20);
p = stdin.rawRead (buffer);
}
final ubyte skipByte (ubyte lo) {
while (true) {
auto a = p[cur .. $];
auto r = a.find! (c => c >= lo);
if (!r.empty) {
cur += a.length - r.length;
return p[cur++];
}
p = stdin.rawRead (buffer);
cur = 0;
if (p.empty) return 0;
}
}
final ubyte nextByte () {
if (cur < p.length) {
return p[cur++];
}
p = stdin.rawRead (buffer);
if (p.empty) return 0;
cur = 1;
return p[0];
}
template next(T) if (isSigned!T) {
final T next () {
T res;
ubyte b = skipByte (45);
if (b == 45) {
while (true) {
b = nextByte ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 - (b - 48);
}
} else {
res = b - 48;
while (true) {
b = nextByte ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 + (b - 48);
}
}
}
}
template next(T) if (isUnsigned!T) {
final T next () {
T res = skipByte (48) - 48;
while (true) {
ubyte b = nextByte ();
if (b < 48 || b >= 58) {
break;
}
res = res * 10 + (b - 48);
}
return res;
}
}
final T[] nextA(T) (int n) {
auto a = uninitializedArray!(T[]) (n);
foreach (i; 0 .. n) {
a[i] = next!T;
}
return a;
}
}
alias Ans = Tuple!(int, int);
void main() {
auto r = new InputReader;
auto nt = r.next!uint;
foreach (tid; 0 .. nt) {
immutable n = r.next!uint.to!int;
immutable s = r.next!uint;
auto a = r.nextA!ulong(n);
auto b = chain (only (0UL), a.cumulativeFold!((acc, x) => acc + x)).array;
auto ans = Ans (0, 0);
foreach (i; 1 .. n + 1) {
if (b[i] <= s) ans = Ans (i, 0);
}
debug stderr.writeln (ans);
auto e = assumeSorted (b);
foreach (i; 0 .. n) {
ulong x = s + a[i];
auto rng = e.lowerBound (s + a[i] + 1);
auto k = rng.length - 1;
if (k >= (i + 1)) {
auto cur = Ans ((k-1).to!int, (i + 1));
if (ans[0] < cur[0]) ans = cur;
}
debug stderr.writeln (i, ' ', ans);
}
debug stderr.writeln (ans);
writeln (ans[1]);
}
}
|
D
|
import std.string;
import std.stdio;
import std.conv;
import std.algorithm;
import std.typecons;
void main() {
int n, q;
int[][int] map;
scanf("%d %d\n", &n, &q);
int[] cnts, checks;
cnts = new int[n+1];
checks = new int[n+1];
foreach(i;0..n-1) {
int a, b;
scanf("%d %d\n", &a, &b);
map[a] ~= b;
}
foreach(i;0..q) {
int p, x;
scanf("%d %d\n", &p, &x);
checks[p] += x;
}
int[] que = [1];
for(size_t i; i < que.length; ++i) {
auto v = que[i];
cnts[v] += checks[v];
if (v in map) {
foreach(child; map[v]) {
cnts[child] += cnts[v];
que ~= child;
}
}
}
foreach (v; cnts[1..$]) {
write(v, " ");
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.numeric;
import std.stdio;
import std.string;
void main()
{
long[] a = readln.chomp.split(" ").to!(long[]);
long m = long.max;
foreach (job; [[0, 1, 2], [1, 2, 0], [2, 1, 0], [1, 0, 2], [2, 0, 1]])
{
long cost;
for (long i = 0; i < 2; ++i)
{
cost += abs(a[job[i + 1]] - a[job[i]]);
}
m = min(m, cost);
}
writeln(m);
}
|
D
|
void main()
{
dchar[] s = rdDchar;
dchar[] t = rdDchar;
long pos = t.length - 1;
bool ok;
foreach_reverse (long i, x; s)
{
if (x == t[pos] || x == '?')
{
bool check;
if (i - pos >= 0)
{
check = true;
foreach (j; 0 .. pos)
{
if (s[i-pos+j] != t[j] && s[i-pos+j] != '?')
{
check = false;
break;
}
}
}
if (check)
{
ok = true;
foreach (j; 0 .. pos+1)
{
s[i-pos+j] = t[j];
}
foreach (j; 0 .. s.length)
{
if (s[j] == '?') s[j] = 'a';
}
break;
}
}
}
writeln(ok ? s.to!string : "UNRESTORABLE");
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.conv;
void main() {
foreach (i; 0..7) {
auto a = readln.split.map!(to!int);
writeln(a[0] - a[1]);
}
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int n;
rd(n);
struct E {
int to;
long cost;
}
auto g = new E[][](n);
foreach (_; 0 .. n - 1) {
int a, b;
long c;
rd(a, b, c);
g[a - 1] ~= E(b - 1, c);
g[b - 1] ~= E(a - 1, c);
}
int q, k;
rd(q, k);
auto dist = new long[](n);
fill(dist, 10L ^^ 18);
void dfs(int i, long d) {
if (dist[i] < 10L ^^ 18)
return;
dist[i] = d;
foreach (e; g[i]) {
dfs(e.to, d + e.cost);
}
}
dfs(k - 1, 0);
while (q--) {
int x, y;
rd(x, y);
writeln(dist[x - 1] + dist[y - 1]);
}
}
void rd(T...)(ref T x) {
import std.stdio : readln;
import std.string : split;
import std.conv : to;
auto l = readln.split;
assert(l.length == x.length);
foreach (i, ref e; x)
e = l[i].to!(typeof(e));
}
|
D
|
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
void main()
{
int[] t = map!(to!int)(split(readln())).array;
int[] checker = [0,0];
for(int i=0;i < 3;i ++)
{
if(t[i] == 5)
{
checker[0] ++;
}
else
{
checker[1] ++;
}
}
if(checker[0] == 2 && checker[1] == 1)
{
writeln("YES");
}
else
{
writeln("NO");
}
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
// dfmt on
void main()
{
long N = lread();
auto S = sread();
long K = lread();
char C = S[K - 1];
foreach (c; S)
{
write(c == C ? C : '*');
}
writeln();
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string, core.stdc.stdlib;
void main() {
auto N = readln.chomp.to!int;
auto S = readln.chomp.map!(c => c.to!int).array;
S.uniq.array.length.writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.typecons;
void main() {
auto input = map!(to!int)(readln.split);
int N = input[0], T = input[1], H = input[2], L = input[3];
Tuple!(int, int)[] data;
foreach (i; 0..N) {
input = map!(to!int)(readln.split);
auto t = input[0], h = input[1];
data ~= tuple(t, h);
}
int i = 0;
int remain = 0;
while (true) {
if (data[i][0] > 0) {
T++;
data[i][0]--;
remain += 10;
if (T > L){
break;
}
} else if(data[i][1] > 0) {
H++;
data[i][1]--;
remain += 100;
} else {
break;
}
if (remain >= 90) {
remain -= 90;
if (remain <= 10 * T) {
data[i][0] += remain / 10;
T -= remain / 10;
} else if ((remain % 100) <= 10 * T && remain / 100 <= H) {
data[i][1] += remain / 100;
H -= remain / 100;
remain %= 100;
data[i][0] += remain / 10;
T -= remain / 10;
} else {
break;
}
remain = 0;
}
i++;
if (i == N) i = 0;
}
writeln(i+1);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.range;
import std.array;
import std.algorithm;
void main(){
auto a=readln.chomp.to!int;
auto b=readln.chomp.to!int;
auto c=readln.chomp.to!int;
writeln((a-b)%c);
}
|
D
|
void main()
{
long a, b;
rdVals(a, b);
max(0, a-2*b).writeln;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
void main() {
int n;
scan(n);
auto a = readln.split.to!(long[]);
auto ans = min(solve(n, a, 1), solve(n, a, -1));
writeln(ans);
}
int sign(long x) {
return x > 0 ? 1 : x < 0 ? -1 : 0;
}
long solve(int n, long[] a, int pm) {
long as;
long res;
foreach (i; 0 .. n) {
as += a[i];
if (sign(as) != pm) {
res += abs(as - pm);
as = pm;
}
pm *= -1;
}
return res;
}
void scan(T...)(ref T args) {
auto line = readln.split; // @suppress(dscanner.suspicious.unmodified)
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args)
if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args)
if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
static import std.ascii;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N, K;
scan(N, K);
foreach (i; 1 .. K + 1)
{
assert(N - K + 1 <= i);
long a = combination_mod(N - K + 1, i, MOD);
long b = combination_mod(K - 1, i - 1, MOD);
writeln((a * b) % MOD);
}
}
T combination_mod(T = long)(T n, T k, T m)
{
if (n < k)
return 0;
long a = 1;
long b = 1;
long c = 1;
foreach (i; 1 .. n + 1)
{
a = (a * i) % m;
if (i == k)
b = a;
if (i == (n - k))
c = a;
}
T powmod(T = long)(T x, T n, T m)
{
if (n < 1)
return 1;
if (n & 1)
{
return x * powmod(x, n - 1, m) % m;
}
T tmp = powmod(x, n / 2, m);
return tmp * tmp % m;
}
long bc = b * c % m;
long bc_inv = powmod(bc, m - 2, m);
return a * bc_inv % m;
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
const tmp = readln.split.to!(long[]);
const A = tmp[0], B = tmp[1];
// f(A,B) = AからBまでのすべての整数のxor
// 各bitでもとめる
// 1bit目はAからBまでのうち1のbitが奇数個なら1、偶数個なら0を返す
// これは交互に来るから、結局(B-A+1)が奇数個かどうかで決まる
// 2bit目はAからBまでのうち1のbitが奇数個なら1、偶数個なら0を返す
// これは2個ずつ飛ばしで来るから、一般に
// Nの2bit目が1であるとは、N%4が2か3であることを意味する
// 一般にnbit目が1であるとは、N%(2^^n)が2^^(n-1)以上であることを意味する
// 各桁を求めるにあたって、A%(2^^n) = a, B%(2^^n) = bであるとき、
// aからはじめて2^^n個のセットは打ち消し合うので、結局そこ以降のxorをとればいい。
long result;
foreach (n; 1..64) {
// A = (2^^n) * a + x
// B = (2^^n) * b + y
// C = (2^^n) * (a+1)
// D = (2^^n) * b
// [C,D)の区間には(D-C)/2個の1がある。
// [A,C)の区間にはmax(2^^n-x, 2^^(n-1))個の1がある
// [D,B]の区間にはmax(0, y-2^^(n-1)+1)個の1がある
auto a = A / (1L << n);
auto b = B / (1L << n);
auto x = A % (1L << n);
auto y = B % (1L << n);
auto C = (1L << n) * (a+1L);
auto D = (1L << n) * b;
long cnt;
cnt += (D-C)/2;
cnt += min((1L << n)-x, (1L << (n-1L)));
cnt += max(0, y-(1L << (n-1L))+1L);
if (cnt % 2 == 1L) result |= (1L << (n-1L));
}
writeln(result);
}
|
D
|
import std.stdio;
void main(){
for(int i = 1; i < 10; ++i){
for(int j = 1; j < 10; ++j){
writeln(i,"x",j,"=",i*j);
}
}
}
|
D
|
import std.stdio;
void main(){
foreach(i; 1..10)
foreach(j; 1..10)
writeln(i,"x",j,"=",i*j);
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.format;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
long a,b;
scan(a,b);
((a - 1) * (b - 1)).writeln();
}
|
D
|
// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
int n;
int[] a;
void main() {
scan(n);
a = readln.split.to!(int[]);
int b1, b2, b3;
foreach (ai ; a) {
if (ai & 1) {
b1++;
}
else if (ai & 3) {
b2++;
}
else {
b3++;
}
}
writeln(b1 <= b3 + !b2 ? "Yes" : "No");
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio;
import std.algorithm;
import std.math;
import std.conv;
import std.string;
T readNum(T)(){
return readStr.to!T;
}
T[] readNums(T)(){
return readStr.split.to!(T[]);
}
string readStr(){
return readln.chomp;
}
void main(){
auto n = readNum!int;
auto b = readNums!int;
int ret;
foreach(i; 0 .. n-2){
ret += min(b[i], b[i+1]);
}
ret += b[0];
ret += b[n-2];
writeln(ret);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto S = RD!string;
auto T = RD!string;
long ans;
foreach (i; 0..3)
{
if (S[i] == T[i])
++ans;
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto x = RD!int;
auto a = RDA;
long e, o;
foreach (i; 0..n)
{
if (a[i] % 2)
++o;
else
++e;
}
if (o == 0) continue;
auto y = min(o, x);
if (y % 2 == 0)
{
--y;
}
x -= y;
ans[ti] = e >= x;
}
foreach (e; ans)
writeln(e ? "Yes" : "No");
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;
void main() {
auto s = readln.split.map!(to!long);
auto N = s[0].to!int;
auto K = s[1];
auto A = readln.split.map!(to!long).array;
long X = 0;
auto B = K.bsr + 1;
bool less = false;
foreach_reverse (i; 0..B) {
if (!less && !(K & (1L << i))) {
continue;
}
long y = 0;
long z = 0;
foreach (a; A) {
if (a & (1L << i)) {
y += 1;
} else {
z += 1;
}
}
if (y > z) {
if (!less) less = true;
} else {
X += (1L << i);
}
}
long ans = 0;
foreach (a; A) ans += (X ^ a);
ans.writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
auto ps = readln.split.to!(int[]);
int r, min_p = ps[0];
foreach (p; ps) {
min_p = min(min_p, p);
if (p == min_p) ++r;
}
writeln(r);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double[] rotate(double[] vec, double rad) { return [cos(rad) * vec[0] - sin(rad) * vec[1], sin(rad) * vec[0] + cos(rad) * vec[1]]; }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (ti; 0..t)
{
auto n = RD;
if (n % 2) continue;
for (long i = 1; i*i <= n; ++i)
{
if (n % i) continue;
if (i*i*2 == n || i*i*4 == n)
{
ans[ti] = true;
}
}
}
foreach (e; ans)
{
writeln(e ? "YES" : "NO");
}
stdout.flush;
debug readln;
}
|
D
|
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
immutable int infinity = int.max / 2;
immutable int NA = -1;
void main ()
{
string s;
while ((s = readln.strip) != "")
{
auto t = readln.strip;
auto k = s.length.to !(int);
auto n = t.length.to !(int);
auto p = new int [k];
p[] = NA;
int [] stack;
foreach (j; 0..k)
{
if (s[j] == '.')
{
if (!stack.empty)
{
p[j] = stack.back;
stack.popBack ();
stack.assumeSafeAppend ();
}
}
else
{
stack ~= j;
}
}
auto f = new int [] [] (2, k + 1);
int b = 0;
f[b][0] = 0;
foreach (j; 1..k + 1)
{
f[b][j] = f[b][j - 1] + 1;
if (p[j - 1] != NA)
{
f[b][j] = min (f[b][j], f[b][p[j - 1]]);
}
}
foreach (i; 0..n)
{
b ^= 1;
f[b][] = infinity;
foreach (j; i + 1..k + 1)
{
f[b][j] = f[b][j - 1] + 1;
if (t[i] == s[j - 1])
{
f[b][j] = min (f[b][j], f[!b][j - 1]);
}
if (p[j - 1] != NA)
{
f[b][j] = min (f[b][j], f[b][p[j - 1]]);
}
}
}
writeln (f[b][k]);
}
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;
immutable long MOD = 10^^6 + 3;
long powmod(long a, long x, long m) {
long ret = 1;
while (x) {
if (x % 2) ret = ret * a % m;
a = a * a % m;
x /= 2;
}
return ret;
}
int gaussianElimination(ref long[][] org_mat, ref long[] ret) {
int N = org_mat.length.to!int;
auto mat = new long[][](N, N+1);
foreach (i; 0..N)
foreach (j; 0..N+1)
mat[i][j] = org_mat[i][j];
foreach (i; 0..N) {
long maxval = 0;
int maxrow = -1;
foreach (j; i..N)
if (abs(mat[j][i]) > maxval)
maxval = abs(mat[j][i]), maxrow = j;
if (maxval == 0)
return -1;
swap(mat[i], mat[maxrow]);
foreach (j; i+1..N+1)
mat[i][j] = mat[i][j] * powmod(mat[i][i], MOD-2, MOD) % MOD;
mat[i][i] = 1;
foreach (j; 0..N) {
if (j == i) continue;
long mul = mat[j][i];
foreach (k; i..N+1) {
(mat[j][k] -= mul * mat[i][k] % MOD) %= MOD;
mat[j][k] = (mat[j][k] + MOD) % MOD;
}
}
}
foreach (i; 0..N)
ret[i] = mat[i][N];
return 0;
}
long[] B;
long ask(long x) {
writeln("? ", x);
debug {
long res = 0;
foreach (i; 0..11) (res += B[i] * powmod(x, i, MOD) % MOD) %= MOD;
return res;
}
stdout.flush;
return readln.chomp.to!long;
}
void ans(long x) {
writeln("! ", x);
return;
}
void main() {
debug {
B = new long[](11);
B[0] = 1000002;
B[2] = 1;
}
int N = 11;
auto mat = new long[][](N, N+1);
foreach (i; 0..N) {
foreach (j; 0..N) mat[i][j] = powmod(i, j, MOD);
mat[i][N] = ask(i);
}
auto A = new long[](N);
gaussianElimination(mat, A);
foreach (i; 0..MOD) {
long res = 0;
foreach (j; 0..N) (res += A[j] * powmod(i, j, MOD) % MOD) %= MOD;
if (res == 0) {
ans(i);
return;
}
}
ans(-1);
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
import std.array;
int n; rd(n);
auto a=readln.split.to!(int[]);
if(a.length%2==0){writeln("No"); return;}
if(a.front%2==1 && a.back%2==1){
writeln("Yes");
}else{
writeln("No");
}
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
immutable long MOD = 998244353;
void main() {
auto N = readln.chomp.to!int;
auto S = readln.chomp;
if (N == 1) {
writeln("Yes");
return;
}
auto cnt = new int[](26);
foreach (s; S) cnt[s - 'a'] += 1;
bool ok = false;
foreach (i; 0..26) if (cnt[i] >= 2) ok = true;
writeln(ok ? "Yes" : "No");
}
|
D
|
import std.stdio, std.conv, std.string, std.algorithm,
std.math, std.array, std.container, std.typecons;
import std.numeric;
void main() {
int n = readln.chomp.to!int;
long sum = 0;
for(int i=1; i<=n; i++) {
if(!(i%3==0 || i%5==0)) sum += i;
}
writeln(sum);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.range;
import std.algorithm;
import std.math;
import std.regex;
void main() {
auto n = readln.chomp.filter!"a != ' '".array;
(sqrt(n.to!float) % 1 == 0 ? "Yes" : "No").writeln;
}
|
D
|
void main(){
long n = _scan!long();
long[] a = _scanln!long();
long q = _scan!long();
long[long] dic;
long ans;
// 合計値と参照辞書の作成
foreach(elm; a){
ans += elm;
dic[elm]++;
}
// replace 操作
foreach(i; 0..q){
long[] bc = _scanln!long();
ans += bc[1]*dic.get(bc[0], 0) - bc[0]*dic.get(bc[0], 0);
dic[bc[1]] += dic.get(bc[0], 0);
dic[bc[0]] = 0;
ans.writeln();
}
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
|
D
|
import std;
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}
T[] lreads(T=long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}
void arywrite(T=long)(T[] ary){ary.map!(text).join(' ').writeln;}
void scan(TList...)(ref TList Args){auto line = readln.split;
foreach (i,T;TList){T val=line[i].to!(T);Args[i]=val;}}
void dprint(TList...)(TList Args){debug{auto s=new string[](TList.length);
static foreach(i,a;Args) s[i]=text(a);s.map!(text).join(' ').writeln;}}
alias sread=()=>readln.chomp();enum MOD =10^^9+7;
alias PQueue(T,alias less="a<b") = BinaryHeap!(Array!T,less);
// dfmt on
void main()
{
long N, K;
scan(N, K);
auto A = aryread();
A[] -= 1;
long k;
auto loop = new long[][](N);
long p;
while (k < K)
{
dprint(k, p);
if (loop[p].length == 0)
{
loop[p] ~= k;
k++;
p = A[p];
continue;
}
loop[p] ~= k - loop[p][0];
dprint(loop[p]);
long ok = 1;
long ng = loop[p].length;
while (1 < ng - ok)
{
long m = (ok + ng) / 2;
if (loop[p][m] <= (K - k))
{
ok = m;
}
else
{
ng = m;
}
}
if (loop[p][ok] <= (K - k))
{
k += loop[p][ok];
continue;
}
else
{
k++;
p = A[p];
continue;
}
}
writeln(p + 1);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
enum P = 10L^^9+7;
void main()
{
auto S = readln.chomp;
auto DP = new long[][](S.length, 4);
foreach (ref dp; DP) dp[] = -1;
long solve(int i, int s) {
if (i == S.length) return s == 3 ? 1 : 0;
if (DP[i][s] == -1) {
long r;
auto c = S[i];
if (c == 'A' || c == '?') {
r = (r + solve(i+1, s)) % P;
if (s == 0) r = (r + solve(i+1, s+1)) % P;
}
if (c == 'B' || c == '?') {
r = (r + solve(i+1, s)) % P;
if (s == 1) r = (r + solve(i+1, s+1)) % P;
}
if (c == 'C' || c == '?') {
r = (r + solve(i+1, s)) % P;
if (s == 2) r = (r + solve(i+1, s+1)) % P;
}
DP[i][s] = r;
}
return DP[i][s];
}
writeln(solve(0, 0));
}
|
D
|
import std.stdio;
import std.algorithm;
import std.conv;
import std.string;
import std.math;
void main(string[] args)
{
auto n = to!int(strip(readln));
int[1001] a;
auto x = split(strip(readln));
foreach (j; 0 .. n)
{
a[to!int(x[j])]++;
}
int count;
int ze = s(a);
while(ze >= 1)
{
count += ze - 1;
ze = s(a);
}
writeln(count);
}
int s(int[] a)
{
int count;
foreach (i, val ; a)
{
if (val >= 1)
count++;
a[i]--;
}
return count;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
char x, y;
scan(x, y);
if (x < y) {
writeln("<");
}
else if (x > y) {
writeln(">");
}
else {
writeln("=");
}
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
/+ dub.sdl:
name "E"
dependency "dcomp" version=">=0.7.4"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
int main() {
Scanner sc = new Scanner(stdin);
int n, k;
sc.read(k, n);
int[] ans = new int[n];
if (k % 2 == 0) {
ans[0] = k/2;
ans[1..$][] = k;
} else if (k == 1) {
ans[0..(n+1)/2] = 1;
} else {
ans[] = (k+1)/2;
int L = n/2;
foreach (i; 0..L) {
int j = n-1;
while (ans[j] == 0) j--;
ans[j]--;
if (ans[j]) {
foreach (l; j+1..n) ans[l] = k;
}
/* void dfs(int p) {
if (ans[p]) ans[p]--;
else {
dfs(p-1);
if (ans[p-1] == 0) ans[p] = 0;
else ans[p] = k;
}
}
dfs(n-1);*/
}
}
while (ans.back == 0) ans.popBack;
writeln(ans.map!(to!string).join(" "));
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
import core.bitop : popcnt;
static if (!__traits(compiles, popcnt(ulong.max))) {
public import core.bitop : popcnt;
int popcnt(ulong v) {
return popcnt(cast(uint)(v)) + popcnt(cast(uint)(v>>32));
}
}
bool poppar(ulong v) {
v^=v>>1;
v^=v>>2;
v&=0x1111111111111111UL;
v*=0x1111111111111111UL;
return ((v>>60) & 1) != 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
// import dcomp.array;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
FastAppender!(E[]) buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/array.d */
// module dcomp.array;
T[N] fixed(T, size_t N)(T[N] a) {return a;}
struct FastAppender(A, size_t MIN = 4) {
import std.algorithm : max;
import std.conv;
import std.range.primitives : ElementEncodingType;
import core.stdc.string : memcpy;
private alias T = ElementEncodingType!A;
private T* _data;
private uint len, cap;
@property size_t length() const {return len;}
bool empty() const { return len == 0; }
void reserve(size_t nlen) {
import core.memory : GC;
if (nlen <= cap) return;
void* nx = GC.malloc(nlen * T.sizeof);
cap = nlen.to!uint;
if (len) memcpy(nx, _data, len * T.sizeof);
_data = cast(T*)(nx);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void opOpAssign(string op : "~")(T item) {
if (len == cap) {
reserve(max(MIN, cap*2));
}
_data[len++] = item;
}
void insertBack(T item) {
this ~= item;
}
void removeBack() {
len--;
}
void clear() {
len = 0;
}
ref inout(T) back() inout { assert(len); return _data[len-1]; }
ref inout(T) opIndex(size_t i) inout { return _data[i]; }
T[] data() {
return (_data) ? _data[0..len] : null;
}
}
/*
This source code generated by dcomp and include dcomp's source code.
dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp)
dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt)
*/
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n, m;
scan(n, m);
auto c = readln.split.to!(int[]);
auto dp = new int[](n + 1);
dp[] = inf;
dp[0] = 0;
foreach (i ; 1 .. n + 1) {
foreach (j ; 0 .. m) {
if (i - c[j] >= 0) chmin(dp[i], dp[i - c[j]] + 1);
}
}
auto ans = dp[n];
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
long INF = 1L << 59;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto M = s[1];
auto G = new Tuple!(int, long)[][](N);
foreach (i; 0..M) {
s = readln.split.map!(to!int);
G[s[0]-1] ~= tuple(s[1]-1, s[2].to!long);
G[s[1]-1] ~= tuple(s[0]-1, s[2].to!long);
}
auto V = new Tuple!(long, long)[](N);
fill(V, tuple(INF, INF));
void dfs(int n, long a, long b) {
if (V[n][0] != INF) return;
V[n] = tuple(a, b);
foreach (to; G[n]) {
auto m = to[0];
auto S = to[1];
if (V[m][0] != INF) continue;
dfs(m, -1*a, S-b);
}
}
dfs(0, 1, 0);
auto ans = INF;
auto X = INF;
foreach (i; 0..N) {
long a1 = V[i][0];
long b1 = V[i][1];
foreach (e; G[i]) {
int j = e[0];
long S = e[1];
long a2 = V[j][0];
long b2 = V[j][1];
if (a1 != a2) {
if (b1 + b2 != S) {
writeln(0);
return;
}
} else if (a1 == 1) {
long c = S - b1 - b2;
if (c % 2 || c < 0) {
writeln(0);
return;
}
auto x = c / 2;
if (X == INF || X == x) {
X = x, ans = 1;
} else {
writeln(0);
return;
}
} else {
long c = S - b1 - b2;
if (c % 2 || c > 0) {
writeln(0);
return;
}
auto x = -c / 2;
if (X == INF || X == x) {
X = x, ans = 1;
} else {
writeln(0);
return;
}
}
}
}
long lo = 1;
long hi = INF;
foreach (i; 0..N) {
long b = V[i][1];
if (V[i][0] == 1 && b < 0) {
lo = max(lo, -b + 1);
} else if (V[i][0] == -1 && b > 0) {
hi = min(hi, b - 1);
}
}
ans = min(ans, hi - lo + 1);
ans = max(0, ans);
ans.writeln;
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
// dfmt on
void main()
{
long A, B, C;
scan(A, B, C);
writeln(C - min(A - B, C));
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int n, m, x, y;
rd(n, m, x, y);
auto a = readln.split.to!(int[]);
auto b = readln.split.to!(int[]);
for (int z = x + 1; z <= y; z++) {
auto ok = reduce!((r, e) => (r && e < z))(true, a) && reduce!((r, e) => (r && e >= z))(true, b);
if (ok) {
writeln("No War");
return;
}
}
writeln("War");
}
void rd(T...)(ref T x) {
import std.stdio, std.string, std.conv;
auto l = readln.split;
assert(l.length == x.length);
foreach (i, ref e; x)
e = l[i].to!(typeof(e));
}
|
D
|
import std.stdio;
import std.string;
import std.array; // split
import std.conv; // to
import std.math;
import std.algorithm;
void main()
{
string n = chomp(readln());
int N = to!int(n); // 第0要素を整数に変換
string[] input = split(readln());
int a = to!int(input[0]);
int min = a,max = a;
for(int i =1;i<N;i++){
a = to!int(input[i]);
if(min > a){
min = a;
}
if(max < a){
max = a;
}
}
writeln(max - min);
}
|
D
|
import std.stdio;
import std.string;
import std.algorithm;
import std.range;
import std.array;
import std.conv;
void main()
{
while(1){
auto N = readln().chomp().to!uint;
if(N == 0)
break;
auto seq = readln().chomp().split();
uint state;
uint cnt;
uint lastState = 0;
foreach(e; seq){
bool bUpdate;
if(((state & 1) == 0 || (state & 2) == 0) && (e == "lu" || e == "ru")){
bUpdate = true;
state += e == "lu" ? 1 : 2;
}
else if((state & 1) == 1 && e == "ld"){
bUpdate = true;
state -= 1;
}else if((state & 2) == 2 && e == "rd"){
bUpdate = true;
state -= 2;
}
if(bUpdate && (state == 3 || state == 0) && lastState != state){
lastState = state;
++cnt;
}
}
writeln(cnt);
}
}
|
D
|
import std.algorithm, std.array, std.container, std.range, std.bitmanip;
import std.numeric, std.math, std.bigint, std.random, core.bitop;
import std.string, std.regex, std.conv, std.stdio, std.typecons;
void main()
{
for (;;) {
auto rd = readln.split.map!(to!int);
auto m = rd[0], f = rd[1], r = rd[2];
if (m == -1 && f == -1 && r == -1) break;
if (m == -1 || f == -1)
writeln("F");
else if (m + f >= 80)
writeln("A");
else if (m + f >= 65 && m + f < 80)
writeln("B");
else if (m + f >= 50 && m + f < 65)
writeln("C");
else if (m + f >= 30 && m + f < 50) {
if (r >= 50)
writeln("C");
else
writeln("D");
}
else
writeln("F");
}
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int calc(int n, int m) {
int f(int k) {
return k * (k - 1) / 2;
}
int ans = 0;
if (n >= 2) ans += f(n);
if (m >= 2) ans += f(m);
return ans;
}
void main() {
int n, m; scan(n, m);
writeln(calc(n, m));
}
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
string s;
scan(s);
auto n = s.length.to!int;
int r = s.count!"a == '0'".to!int;
int b = s.count!"a == '1'".to!int;
int rem = abs(r - b);
int ans = n - rem;
writeln(ans);
}
int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new int[][](n, 0);
foreach (i ; 0 .. m) {
int u, v;
scan(u, v);
if (is1indexed) {
u--, v--;
}
adj[u] ~= v;
if (isUndirected) {
adj[v] ~= u;
}
}
return adj;
}
alias Edge = Tuple!(int, "to", int, "cost");
Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new Edge[][](n, 0);
foreach (i ; 0 .. m) {
int u, v, c;
scan(u, v, c);
if (is1indexed) {
u--, v--;
}
adj[u] ~= Edge(v, c);
if (isUndirected) {
adj[v] ~= Edge(u, c);
}
}
return adj;
}
void yes(bool b) {writeln(b ? "Yes" : "No");}
void YES(bool b) {writeln(b ? "YES" : "NO");}
T[] readArr(T)() {return readln.split.to!(T[]);}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main(){
int qty = readInt();
int[] value = readInts();
int[] cost = readInts();
int profit;
foreach(i; 0 .. qty){
if(value[i] > cost[i]){
profit += value[i] - cost[i];
}
}
writeln(profit);
}
int readInt(){
return readln.chomp.to!int;
}
int[] readInts(){
return readln.chomp.split.to!(int[]);
}
|
D
|
import std.stdio, std.conv, std.string, std.range, std.algorithm;
void main() {
int d = readln.chomp.to!int;
("Christmas " ~ repeat("Eve", 25 - d).join(" ")).writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
auto as = readln.split.to!(long[]);
auto cs = new long[](N);
foreach (i; 0..N) {
if (i != 0) cs[i] = cs[i-1];
cs[i] += as[i];
}
auto DP = new long[][](N, N);
long solve(int l, int r) {
if (l == r) return 0;
if (DP[l][r] == 0) {
long x = long.max;
foreach (m; l..r) {
x = min(x, solve(l, m) + solve(m+1, r));
}
x += cs[r];
if (l != 0) x -= cs[l-1];
DP[l][r] = x;
}
return DP[l][r];
}
writeln(solve(0, N-1));
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
alias Path = Tuple!(int, "to", int, "w");
alias SS = Tuple!(int, "cur", int, "c");
Path[][10^^5] PS;
int[10^^5] TS;
void main()
{
auto N = readln.chomp.to!int;
foreach (_; 0..N-1) {
auto uvw = readln.split.to!(int[]);
auto u = uvw[0]-1;
auto v = uvw[1]-1;
auto w = uvw[2]%2;
PS[u] ~= Path(v, w);
PS[v] ~= Path(u, w);
}
TS[0] = 1;
SS[] Q = [SS(0, 1)];
while (!Q.empty) {
auto head = Q[0];
Q = Q[1..$];
foreach (next; PS[head.cur]) {
if (TS[next.to]) continue;
int c;
if (next.w == 0) {
c = head.c;
} else if (head.c == 1) {
c = 2;
} else {
c = 1;
}
TS[next.to] = c;
Q ~= SS(next.to, c);
}
}
foreach (i; 0..N) {
writeln(TS[i] - 1);
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
auto ds = readln.split.to!(long[]);
long s;
foreach (i; 0..N) {
foreach (j; i+1..N) {
s += ds[i] * ds[j];
}
}
writeln(s);
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string, core.stdc.stdlib;
void main() {
immutable long INF = 1L << 59;
auto s = readln.split.map!(to!int);
auto N = s[0];
auto M = s[1];
auto L = s[2].to!long;
auto G = new long[][](N, N);
foreach (i; 0..N) foreach (j; 0..N) G[i][j] = i == j ? 0 : INF;
foreach (_; 0..M) {
s = readln.split.map!(to!int);
auto a = s[0] - 1;
auto b = s[1] - 1;
auto c = s[2].to!long;
G[a][b] = c;
G[b][a] = c;
}
auto dist1 = warshall_floyd!(long, INF)(G);
auto dist2 = new long[][](N, N);
foreach (i; 0..N) foreach (j; 0..N) dist2[i][j] = i == j ? 0 : INF;
foreach (i; 0..N) foreach (j; 0..N) if (dist1[i][j] <= L) dist2[i][j] = 1;
auto dist3 = warshall_floyd!(long, INF)(dist2);
auto Q = readln.chomp.to!int;
while (Q--) {
s = readln.split.map!(to!int);
auto a = s[0] - 1;
auto b = s[1] - 1;
auto ans = dist3[a][b];
writeln(ans >= INF ? -1 : ans - 1);
}
}
T[][] warshall_floyd(T, T inf)(const T[][] graph) {
import std.conv : to;
int n = graph.length.to!int;
auto dist = new T[][](n, n);
foreach (i; 0..n) foreach (j; 0..n) dist[i][j] = graph[i][j];
foreach (i; 0..n)
foreach (j; 0..n)
foreach (k; 0..n)
if (dist[j][i] != inf && dist[i][k] != inf)
dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k]);
return dist;
}
|
D
|
import std.stdio, std.math, std.algorithm, std.array, std.string, std.conv, std.container, std.range;
pragma(inline, true) T[] Reads(T)() { return readln.split.to!(T[]); }
alias reads = Reads!int;
pragma(inline, true) void scan(Args...)(ref Args args) {
string[] ss = readln.split;
foreach (i, ref arg ; args) arg = ss[i].parse!int;
}
void main() {
int n; scan(n);
int[] h = reads();
bool can() {
// dp[i][j] := i まで見たときの h の最大値
// -1 は不可
auto dp = new int[][](100010,2);
foreach(i;0..100010) foreach(j;0..2) dp[i][j]=-1;
dp[0][0] = dp[0][1] = 0;
foreach (i; 1..n+1) {
if (dp[i-1][0] >= 0) {
dp[i][0] = max(dp[i-1][0], h[i-1]);
dp[i][1] = max(dp[i-1][0], h[i-1] - 1);
}
if (dp[i-1][1] >= 0) {
dp[i][0] = max(dp[i-1][1], h[i-1]);
dp[i][1] = max(dp[i-1][1], h[i-1] - 1);
}
if (dp[i][0] > h[i-1])
dp[i][0] = -1;
if (dp[i][1] > h[i-1] - 1)
dp[i][1] = -1;
}
return ~dp[n][0] || ~dp[n][1];
}
writeln(can() ? "Yes" : "No");
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
void scan(T...)(ref T args) {
auto line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int H, W;
scan(H, W);
auto c = iota(10).map!(i => readln.split.to!(int[])).array;
foreach (k ; 0 .. 10) {
foreach (i ; 0 .. 10) {
foreach (j ; 0 .. 10) {
chmin(c[i][j], c[i][k] + c[k][j]);
}
}
}
auto a = iota(H).map!(i => readln.split.to!(int[])).array;
int ans;
foreach (i ; 0 .. H) {
foreach (j ; 0 .. W) {
if (a[i][j] != -1) ans += c[a[i][j]][1];
}
}
writeln(ans);
}
|
D
|
import std.stdio, std.string, std.conv, std.array, std.algorithm;
import std.uni, std.range, std.math, std.container, std.datetime;
import core.bitop, std.typetuple, std.typecons;
immutable long MOD = 1_000_000_007;
alias tie = TypeTuple;
alias triplet = Tuple!(int, int, int);
void main(){
int A, B, C;
readVars(A, B, C);
if (C % gcd(A, B) == 0) {
writeln("YES");
} else {
writeln("NO");
}
}
int gcd(int x, int y){
return y ? gcd(y, x % y) : x;
}
void readVars(T...)(auto ref T args){
auto line = readln.split;
foreach(ref arg ; args){
arg = line.front.to!(typeof(arg));
line.popFront;
}
if(!line.empty){
throw new Exception("args num < input num");
}
}
|
D
|
void main() {
int[] tmp = readln.split.to!(int[]);
int n = tmp[0], m = tmp[1];
string[] a = new string[n], b = new string[m];
foreach (i; 0 .. n) {
a[i] = readln.chomp;
}
foreach (i; 0 .. m) {
b[i] = readln.chomp;
}
bool ok;
foreach (i; 0 .. n-m+1) {
foreach (j; 0 .. n-m+1) {
bool check = true;
foreach (k; 0 .. m) {
if (a[j+k][i..i+m] != b[k]) {
check = false;
}
}
if (check) ok = true;
}
}
writeln(ok ? "Yes" : "No");
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n; rd(n);
auto q=readln.split.to!(int[]);
const int m=1_000_000+5;
auto isPrime=new bool[](m);
fill(isPrime, true);
isPrime[1]=false;
for(int i=2; i*i<=m; i++)if(isPrime[i]){
for(int j=2; i*j<m; j++) isPrime[i*j]=false;
}
const long mod=1_000_000_000+7;
auto dp_p=new long[](n), dp_e=new long[](n);
dp_p[0]=(isPrime[q[0]] ? 1L : 0L);
for(int i=1; i<n; i++){
if(isPrime[q[i-1]]) (dp_e[i]+=dp_p[i-1])%=mod;
if(isPrime[q[i]]){
if(isPrime[q[i-1]] && q[i-1]<q[i]) (dp_p[i]+=dp_p[i-1])%=mod;
if(i-2>=0 && isPrime[q[i-2]] && q[i-2]<q[i]) (dp_p[i]+=dp_e[i-1])%=mod;
}
}
writeln((dp_p[n-1]+dp_e[n-1])%mod);
}
/+
qi: 素数
dp_e[i]=dp_p[i-1]
q(i-1):素数 and q(i-1)<qi: dp_p[i]+=dp_p[i-1]
q(i-2):素数 and q(i-2)<qi: dp_p[i]+=dp_p[i-2] ? xx
dp_p[i]+=dp_e[i-1] ? ok
qi: 合成数
dp_e[i]=dp_p[i-1]
+/
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
|
D
|
import std;
void calc(int[] a, int[][] adj) {
auto dp = new int[a.length].assumeSorted;
foreach (i; 0..a.length) dp[i] = int.max;
auto ans = new ulong[a.length];
void dfs(int node, int prev) {
auto x = dp.lowerBound(a[node]);
auto old = dp[x.length];
dp[x.length] = a[node];
ans[node] = dp.lowerBound(int.max).length;
foreach (to; adj[node]) {
if (to == prev) continue;
dfs(to, node);
}
dp[x.length] = old;
}
dfs(0, -1);
foreach (e; ans) writeln(e);
}
void main() {
int n; scan(n);
auto a = readints;
int[][] adj;
foreach (_; 0..n) adj ~= [[]];
foreach (_; 0..n-1) {
int u, v; scan(u, v);
u--; v--;
adj[u] ~= v;
adj[v] ~= u;
}
calc(a, adj);
}
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto S = readln.chomp.to!(char[]);
auto K = readln.chomp.to!long;
if (S.length == 1) {
writeln(K/2);
return;
}
long[] ns;
long cnt;
char last;
foreach (c; S) {
if (cnt && last != c) {
ns ~= cnt;
cnt = 0;
}
++cnt;
last = c;
}
ns ~= cnt;
if (ns.length == 1) {
writeln(S.length * K / 2);
return;
}
long r;
foreach (n; ns[1..$-1]) r += n/2 * K;
if (S[0] == S[$-1]) {
r += ns[0]/2;
r += ns[$-1]/2;
r += (ns[0] + ns[$-1])/2 * (K-1);
} else {
r += ns[0]/2 * K;
r += ns[$-1]/2 * K;
}
writeln(r);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum mod = 10L^^9 + 7;
enum inf = 10^^9;
void main() {
int n, m;
scan(n, m);
auto c = new int[][](n, n);
foreach (i ; 0 .. n) {
foreach (j ; 0 .. n) {
if (i != j) {
c[i][j] = inf;
}
}
}
foreach (i ; 0 .. m) {
int ai, bi, ci;
scan(ai, bi, ci);
ai--, bi--;
c[ai][bi] = c[bi][ai] = ci;
}
auto dist = new int[][](n, n);
foreach (i ; 0 .. n) {
foreach (j ; 0 .. n) {
dist[i][j] = c[i][j];
}
}
foreach (k ; 0 .. n) {
foreach (i ; 0 .. n) {
foreach (j ; 0 .. n) {
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
int ans;
foreach (i ; 0 .. n) {
foreach (j ; i + 1 .. n) {
if (c[i][j] == inf) continue;
if (c[i][j] > dist[i][j]) {
ans++;
}
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
bool test (string s) {
if (s.length % 2) return false;
foreach (i; 0 .. s.length) {
if (i % 2) {
if (s[i] != 'i') return false;
} else {
if (s[i] != 'h') return false;
}
}
return true;
}
void main() {
string s = readln.stripRight;
writeln (test (s) ? "Yes" : "No");
}
|
D
|
import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;
void main(){
int h, w;
{
int[] tmp = readln.chomp.split.map!(to!int).array;
h = tmp[0], w = tmp[1];
}
char[][] css;
for(int i = 0; i < h; i ++){
css ~= readln.chomp.to!(char[]);
}
debug css.writeln;
ulong ans = 0;
for(int k = 0; k < w - 1; k ++){
int[][] friction;
// friction[i][j]はa[0..i]とb[0..j]で右揃えにしたとき一致してる個数
// friction[i][j] = friction[i-1][j-1] + (a[i]==b[i]? 1: 0)
for(int i = 0; i <= h; i ++){
friction ~= [0];
friction[i].length = h + 1;
}
friction[0].fill(0);
for(int i = 1; i <= h; i ++){
friction[i][0] = 0;
for(int j = 1; j <= h; j ++){
friction[i][j] = friction[i - 1][j - 1]
+ ((css[i - 1][k] == css[j - 1][k + 1])? 1: 0);
debug write(css[i - 1][k], " ", css[j - 1][k+1], " ");
debug friction.writeln;
}
}
debug friction.writeln;
int[][] xs;
for(int i = 0; i <= h; i ++){
xs ~= [0];
xs[i].length = h + 1;
}
xs[0].fill(0);
for(int i = 1; i <= h; i ++){
xs[i][0] = 0;
for(int j = 1; j <= h; j ++){
xs[i][j] = min(xs[i][j - 1], xs[i - 1][j]) + friction[i][j];
debug xs.writeln;
}
}
debug xs.writeln;
ans += xs[h][h];
}
ans.writeln;
}
/*
隣接する2列だけで考えればよい
それの総和
*/
|
D
|
//dlang template---{{{
import std.stdio;
import std.conv;
import std.string;
import std.array;
import std.algorithm;
import std.typecons;
import std.math;
import std.range;
// MIT-License https://github.com/kurokoji/nephele
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType))
{
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType))
{
str ~= s.to!(char[]).strip.split;
}
}
//Digit count---{{{
int DigitNum(int num) {
int digit = 0;
while (num != 0) {
num /= 10;
digit++;
}
return digit;
}
//}}}
//}}}
void main() {
Scanner sc = new Scanner;
int N;
sc.scan(N);
foreach (i; 0 .. 100) {
foreach (j; 0 .. 100) {
if ((i * 4) + (j * 7) == N) {
writeln("Yes");
return;
} else if ((j * 4) + (i * 7) == N) {
writeln("Yes");
return;
}
}
}
writeln("No");
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dunkelheit" version=">=0.9.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
int main() {
Scanner sc = new Scanner(stdin);
int n;
string s;
sc.read(n, s);
int ans = 10^^9;
int wc, ec = s.count('E').to!int;
foreach (c; s) {
if (c == 'E') ec--;
ans = min(ans, wc+ec);
if (c == 'W') wc++;
}
writeln(ans);
return 0;
}
/* IMPORT /mnt/c/Users/yosupo/Programs/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /mnt/c/Users/yosupo/Programs/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
void read(bool enforceEOF = false, T, Args...)(ref T x, auto ref Args args) {
import std.exception;
enforce(readSingle(x));
static if (args.length == 0) {
enforce(enforceEOF == false || !succ());
} else {
read!enforceEOF(args);
}
}
void read(bool enforceEOF = false, Args...)(auto ref Args args) {
import std.exception;
static if (args.length == 0) {
enforce(enforceEOF == false || !succ());
} else {
enforce(readSingle(args[0]));
read!enforceEOF(args);
}
}
}
/* IMPORT /mnt/c/Users/yosupo/Programs/dunkelheit/source/dkh/foundation.d */
// module dkh.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
|
D
|
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array,
std.functional, std.container, std.typecons;
void main() {
int B = readln.chomp.to!int;
foreach(i; 1..10) {
foreach(k; 1..10) {
if(B == i*k) {
"Yes".writeln;
return;
}
}
}
"No".writeln;
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.ascii;
void main()
{
auto s = readln.chomp.split;
if (s[0][$-1] == s[1][0] && s[1][$-1] == s[2][0]) {
writeln("YES");
} else {
writeln("NO");
}
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
writeln(readln.chomp.to!int / 3);
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
alias sread = () => readln.chomp();
void main()
{
auto n = lread();
bool alleven = true;
foreach(i;0..n)
{
auto a = lread();
alleven = alleven && (a % 2 == 0);
}
auto ans = (!alleven)?"first":"second";
ans.writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
void times(alias pred)(int n) {
foreach(i; 0..n) pred();
}
auto rep(alias pred, T = typeof(pred()))(int n) {
T[] res = new T[n];
foreach(ref e; res) e = pred();
return res;
}
void main() {
readln.chomp.map!(a => a=='p' ? -1:1).sum.pipe!"a/2".writeln;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
long N = lread();
long K = lread();
long m = long.max;
foreach (i; 0 .. N + 1)
{
long a = (2 ^^ i) + (K * (N - i));
// long b = (1 + K * (N - i)) + (2 ^^ i);
// writeln(a);
m.minAssign(a);
}
writeln(m);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n;
scan(n);
auto t = readln.split.to!(int[]);
auto a = readln.split.to!(int[]);
auto m = new int[](n);
auto h = new int[](n);
h[0] = t[0];
foreach (i ; 1 .. n) {
if (t[i] > t[i - 1]) {
h[i] = t[i];
}
else {
m[i] = t[i];
}
}
long ans = 1;
if (h[n - 1] > 0) {
if (a[n - 1] != h[n - 1]) {
ans = 0;
}
}
else {
if (a[n - 1] > m[n - 1]) {
ans = 0;
}
}
foreach_reverse (i ; 0 .. n - 1) {
if (h[i] > 0) {
if (a[i] < h[i]) {
ans = 0;
break;
}
}
else if (a[i] > a[i + 1]) {
if ((h[i] > 0 && a[i] != h[i]) || (m[i] > 0 && m[i] < a[i])) {
ans = 0;
break;
}
}
else {
m[i] = min(m[i], a[i]);
ans *= m[i];
ans %= mod;
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int N;
scan(N);
auto w = new int[](N + 1);
auto b = new int[](N + 1);
w[] = -1;
b[] = -1;
auto sw = new int[][](2*N + 1, N + 1);
auto sb = new int[][](2*N + 1, N + 1);
foreach (i ; 0 .. 2*N) {
char c; int a;
scan(c, a);
if (c == 'W') {
w[a] = i;
sw[i + 1][a]++;
}
else {
b[a] = i;
sb[i + 1][a]++;
}
}
foreach (j ; 0 .. N + 1) {
foreach (i ; 1 .. 2*N + 1) {
sw[i][j] += sw[i - 1][j];
sb[i][j] += sb[i - 1][j];
}
}
foreach (i ; 0 .. 2*N + 1) {
foreach (j ; 1 .. N + 1) {
sw[i][j] += sw[i][j - 1];
sb[i][j] += sb[i][j - 1];
}
}
// dp(i, j) = (白の1 ~ i、黒の1 ~ jをソート順にするための最小操作回数)
auto dp = new int[][](N + 1, N + 1);
fillAll(dp, inf);
dp[0][0] = 0;
foreach (i ; 0 .. N + 1) {
foreach (j ; 0 .. N + 1) {
if (i > 0) {
auto p = w[i];
p += (i - 1 + j) - sw[p][i - 1] - sb[p][j];
assert(p >= w[i]);
chmin(dp[i][j], dp[i - 1][j] + abs(p - (i + j - 1)));
}
if (j > 0) {
auto p = b[j];
p += (i + j - 1) - sw[p][i] - sb[p][j - 1];
assert(p >= b[j]);
chmin(dp[i][j], dp[i][j - 1] + abs(p - (i + j - 1)));
}
}
}
auto ans = dp[N][N];
writeln(ans);
}
void scan(T...)(ref T args) {
auto line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto hw = readln.split.to!(int[]);
auto H = hw[0];
auto W = hw[1];
char[][] MAP;
MAP.length = H;
foreach (ref line; MAP) {
line = readln.chomp.to!(char[]);
}
int[][] HMAP, WMAP;
HMAP.length = H;
WMAP.length = H;
foreach (i; 0..H) {
HMAP[i].length = W;
WMAP[i].length = W;
}
foreach (i; 0..H) {
int x;
foreach (j; 0..W) {
if (MAP[i][j] == '#') {
x = 0;
} else {
WMAP[i][j] = ++x;
}
}
x = 0;
foreach_reverse (j; 0..W) {
if (MAP[i][j] == '#') {
x = 0;
continue;
}
if (WMAP[i][j] > x) {
x = WMAP[i][j];
} else {
WMAP[i][j] = x;
}
}
}
foreach (j; 0..W) {
int x;
foreach (i; 0..H) {
if (MAP[i][j] == '#') {
x = 0;
} else {
HMAP[i][j] = ++x;
}
}
x = 0;
foreach_reverse (i; 0..H) {
if (MAP[i][j] == '#') {
x = 0;
continue;
}
if (HMAP[i][j] > x) {
x = HMAP[i][j];
} else {
HMAP[i][j] = x;
}
}
}
int r;
foreach (i; 0..H) {
foreach (j; 0..W) {
r = max(r, HMAP[i][j] + WMAP[i][j] - 1);
}
}
writeln(r);
}
|
D
|
void main()
{
string s = readln.chomp;
long k = readln.chomp.to!long;
long n = s.countUntil!(x => x != '1');
if (n == -1 || n >= k)
{
s[0].writeln;
}
else
{
s[n].writeln;
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
void main() {
string s = readln.chomp;
int cnt;
foreach (x; s) {
if (x == '+') ++cnt;
else --cnt;
}
cnt.writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.uni;
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
void main() {
auto n = readln.chomp.to!int;
int[][][] f = new int[][][](n, 5, 2);
int[][] p = new int[][](n, 11);
for (int i = 0; i < n; ++i) {
auto l = readln.chomp.split.map!(to!int).array;
for (int j = 0; j < 10; j += 2) {
f[i][j / 2][0] = l[j];
f[i][j / 2][1] = l[j + 1];
}
}
for (int i = 0; i < n; ++i) {
auto l = readln.chomp.split.map!(to!int).array;
for (int j = 0; j < 11; ++j) {
p[i][j] = l[j];
}
}
int maxBenefit = -1000000007;
for (int i = 1; i < (2 << 9); ++i) {
int sum;
int[] c = new int[](n);
for (int j = 0; j < 10; ++j) {
if (((i >> j) & 1) == 1) {
for (int k = 0; k < n; ++k) {
if (f[k][j / 2][j % 2] == 1) c[k]++;
}
}
}
for (int j = 0; j < n; ++j) sum += p[j][c[j]];
if (maxBenefit < sum) maxBenefit = sum;
}
maxBenefit.writeln;
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;
immutable long MOD = 10^^9 + 7;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto M = s[1];
auto K = s[2];
long C = 1;
foreach (i; 1..N*M-1) {
C = C * i % MOD;
}
foreach (i; 1..K-1) {
C = C * powmod(i, MOD-2, MOD) % MOD;
}
foreach (i; 1..(N*M-2-(K-2))+1) {
C = C * powmod(i, MOD-2, MOD) % MOD;
}
long ans = 0;
long tmp = 0;
foreach (i; 0..N) {
long right = N - i - 1;
long left = i;
tmp = (tmp + right * (right + 1) / 2 * M) % MOD;
tmp = (tmp + left * (left + 1) / 2 * M) % MOD;
}
ans += tmp * C % MOD * M % MOD;
ans %= MOD;
tmp = 0;
foreach (i; 0..M) {
long right = M - i - 1;
long left = i;
tmp = (tmp + right * (right + 1) / 2 * N) % MOD;
tmp = (tmp + left * (left + 1) / 2 * N) % MOD;
}
ans += tmp * C % MOD * N % MOD;
ans %= MOD;
ans = ans * powmod(2, MOD-2, MOD) % MOD;
ans.writeln;
}
long powmod(long a, long x, long m) {
long ret = 1;
while (x) {
if (x % 2) ret = ret * a % m;
a = a * a % m;
x /= 2;
}
return ret;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.format;
import std.math;
import std.random;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void main()
{
string f = readln.chomp;
string s = readln.chomp;
auto n = f.length;
bool flag = true;
foreach (i; 0..n) {
flag &= f[i] == s[$ - 1 - i];
}
writeln = flag ? "YES" : "NO";
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
void main()
{
auto ip = readln.split.to!(int[]);
if(ip[0] <= 8 && ip[1] <= 8) writeln("Yay!");
else writeln(":(");
}
//writeln("滲み出す混濁の紋章 不遜なる狂気の器 湧き上がり 否定し 痺れ 瞬き 眠りを妨げる 爬行する鉄の王女 絶えず自壊する泥の人形 結合せよ 反発せよ 地に満ち己の無力を知れ 破道の九十 黒棺")
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
enum P = 10L^^9+7;
long[][] mmul(long[][] x, long[][] y)
{
auto r = new long[][](x.length, y.length);
foreach (i; 0..x.length) {
foreach (j; 0..y[0].length) {
foreach (k; 0..y.length) {
r[i][j] = (r[i][j] + (x[i][k] * y[k][j]) % P) % P;
}
}
}
return r;
}
long[][] mpow(long[][] m, long k)
{
auto r = new long[][](m.length, m[0].length);
foreach (i; 0..m.length) {
r[i][i] = 1;
}
while (k) {
if (k%2 == 1) r = mmul(r, m);
k /= 2;
m = mmul(m, m);
}
return r;
}
void main()
{
auto nk = readln.split.to!(long[]);
auto N = nk[0];
auto K = nk[1];
auto A = new long[][](N, N);
foreach (i; 0..N) {
foreach (j, b; readln.split) {
if (b == "1") A[i][j] = 1;
}
}
auto m = mpow(A, K);
long r;
foreach (mm; m) foreach (e; mm) r = (r + e) % P;
writeln(r);
}
|
D
|
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math, std.container, std.typecons;
import core.stdc.stdio;
// foreach, foreach_reverse, writeln
void main() {
int n, m;
scanf("%d%d", &n, &m);
bool[][] g = new bool[][](n,n);
foreach (i; 0..n) foreach (j; 0..n) {
if (i != j) g[i][j] = true;
}
foreach (i; 0..m) {
int a, b;
scanf("%d%d", &a, &b);
a--;
b--;
g[a][b] = g[b][a] = false;
}
//if (n*(n-1)/2 == m) {
// int s = n/2, t = n-s;
// m -= s*t;
// writeln(m);
// return;
//}
int[] color = new int[n];
color[] = -1;
int[] cnt;
bool dfs(int v, int c) {
if (color[v] != -1) {
if (color[v] != c) return false;
return true;
}
color[v] = c;
cnt[c]++;
foreach (u; 0..n) {
if (g[v][u]) {
if (!dfs(u,c^1)) return false;
}
}
return true;
}
int l = 0;
int[] a;
foreach (i; 0..n) {
if (color[i] != -1) continue;
cnt = new int[2];
if (!dfs(i,0)) {
writeln(-1);
return;
}
cnt.sort;
l += cnt[0];
if (cnt[0] < cnt[1]) a ~= cnt[1]-cnt[0];
}
bool[] dp = new bool[n+1];
dp[0] = true;
foreach (x; a) {
foreach_reverse (i; 0..n-x+1) {
dp[i+x] |= dp[i];
}
}
int ans = m;
int im = n*(n-1)/2-m;
foreach (i; 0..n+1) {
if (!dp[i]) continue;
int s = l+i, t = n-s;
ans = min(ans, m-(s*t-im));
}
writeln(ans);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
int calc(int n) {
// n! を素因数分解
int[int] factors;
for (int i = 2; i <= n; i++) {
int x = i;
for (int j = 2; j * j <= x; j++) {
while (x % j == 0) {
factors[j]++;
x /= j;
}
}
if (x > 1) factors[x]++;
}
// if (factors.empty) return 0;
// v2.070.1 だと empty でコンパイルエラーになるので、代わりに n = 1 でチェック
if (n == 1) return 0;
int[] t; // p_i^k の k の部分
foreach (k, v; factors) t ~= v;
// do[i][約数の個数]
auto dp = new int[][](t.length, 76);
for (int i = 0; i <= t[0]; i++)
dp[0][i + 1] = 1;
for (int i = 1; i < t.length; i++) {
for (int j = 1; j <= 75; j++) { // i-1 までの約数の個数
for (int k = 0; k <= t[i]; k++) {
int x = j * (k + 1);
if (x <= 75) {
dp[i][x] += dp[i - 1][j];
}
}
}
}
return dp[t.length - 1][75];
}
void main() {
int n = readint;
writeln(calc(n));
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nab = readln.split.to!(int[]);
auto N = nab[0];
auto A = nab[1];
auto B = nab[2];
auto ts = new int[](N);
foreach (i; 0..N) ts[i] = readln.chomp.to!int;
int r;
foreach (t; ts) if (A <= t && t < B) ++r;
writeln(N - r);
}
|
D
|
import std.stdio, std.string, std.algorithm, std.conv, std.range, std.array;
void main() {
readln();
auto inp = readln().chomp.split;
string[] b1, b2;
for(int i; i<inp.length; i++){
auto a = inp[i];
if(i%2==0){b1 ~= a;}
else{b2 ~= a;}
}
if(inp.length%2==0){b2.reverse(); (b2 ~ b1).join(" ").writeln;}
else{b1.reverse(); (b1 ~ b2).join(" ").writeln;}
}
|
D
|
void main() {
int x = readln.chomp.to!int;
bool[] ok = new bool[x+1];
ok[1] = true;
for (int i = 2; i * i <= x; ++i) {
for (int j = 2; i ^^ j <= x; ++j) {
ok[i^^j] = true;
}
}
foreach_reverse (i, y; ok) {
if (y) {
i.writeln;
break;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.uni;
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int d, n; readV(d, n);
writeln((n == 100 ? 101 : n)*100^^d);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
auto d = ['A': 'T', 'T': 'A', 'C': 'G', 'G': 'C'];
char a;
scan(a);
writeln(d[a]);
}
int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new int[][](n, 0);
foreach (i; 0 .. m) {
int u, v;
scan(u, v);
if (is1indexed) {
u--, v--;
}
adj[u] ~= v;
if (isUndirected) {
adj[v] ~= u;
}
}
return adj;
}
alias Edge = Tuple!(int, "to", int, "cost");
Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new Edge[][](n, 0);
foreach (i; 0 .. m) {
int u, v, c;
scan(u, v, c);
if (is1indexed) {
u--, v--;
}
adj[u] ~= Edge(v, c);
if (isUndirected) {
adj[v] ~= Edge(u, c);
}
}
return adj;
}
void yes(bool b) {
writeln(b ? "Yes" : "No");
}
void YES(bool b) {
writeln(b ? "YES" : "NO");
}
T[] readArr(T)() {
return readln.split.to!(T[]);
}
T[] readArrByLines(T)(int n) {
return iota(n).map!(i => readln.chomp.to!T).array;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.