code
stringlengths
4
1.01M
language
stringclasses
2 values
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; import std.concurrency; void times(alias fun)(int n) { foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } long rec(int depth, long D, long repunit) { if (repunit == 0) return D==0 ? 1 : 0; long d = D%10; long a = depth==0?1:0; return (10-d.abs-a) * rec(depth+1, (D-repunit*d)/10, repunit/100); } void main() { long D = readln.chomp.to!long; if (D%9 != 0) { 0.writeln; return; } D /= 9; long ans = 0; long repunit = 1; foreach(i; 2..18+1) { long a = i%2==1 ? 10:1; ans += a * rec(0, D, repunit); repunit = repunit*10 + 1; } ans.writeln; } // ---------------------------------------------- // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { auto minElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto minimum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b < minimum) { element = a; minimum = b; } } return element; } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto maximum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b > maximum) { element = a; maximum = b; } } return element; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string; wchar[600][600] BD1, BD2; void main() { auto N = readln.chomp.to!int; foreach (i; 0..N) { foreach (j, c; readln.chomp) { BD1[i][j] = BD1[i][N+j] = BD1[N+i][j] = BD1[N+i][N+j]= c; BD2[j][i] = BD2[j][N+i] = BD2[N+j][i] = BD2[N+j][N+i] = c; } } int cnt; foreach (a; 0..N) { foreach (b; 0..N) { foreach (i; 0..N) { if (BD1[a+i][b..N+b] != BD2[b+i][a..N+a]) goto bad; } ++cnt; bad: } } writeln(cnt); }
D
import std.stdio, std.conv, std.string, std.algorithm.iteration, std.math, std.range, std.algorithm.sorting, std.algorithm.searching, std.algorithm.mutation, std.array, std.typecons; void main() { readln; auto t = readln; int[] list = t.split.to!(int[]); int[] eList; int[] oList; eList.length = 100001; oList.length = 100001; foreach(i, n; list.enumerate()) { if(i % 2 == 0) { eList[n]++; }else{ oList[n]++; } } auto eTop = searchTop2Index(eList); auto oTop = searchTop2Index(oList); int eSameNum = eTop[0]; int oSameNum = oTop[0]; if(eSameNum == oSameNum) { if(eList[eTop[1]] < oList[oTop[1]]) { oSameNum = oTop[1]; }else{ eSameNum = eTop[1]; } } writeln(list.length - eList[eSameNum] - oList[oSameNum]); } auto searchTop2Index(int[] list) { int top1; int top1Count; int top2; int top2Count; foreach(i, n; list.enumerate()) { if(top1Count < n) { top2 = top1; top2Count = top1Count; top1 = i.to!int; top1Count = n; }else if(top2Count < n) { top2 = i.to!int; top2Count = n; } } return Tuple!(int,int)(top1, top2); }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; alias Edge = Tuple!(int, "to", int, "c", int, "rev"); immutable inf = 101101; void main() { int n, m; scan(n, m); auto adj = new Edge[][](n, 0); foreach (i ; 0 .. m) { int ui, vi, ci; scan(ui, vi, ci); adj[ui] ~= Edge(vi, ci, adj[vi].length.to!int); adj[vi] ~= Edge(ui, 0, adj[ui].length.to!int - 1); } int ans = FordFulkerson(n, m, adj, 0, n - 1); writeln(ans); } int FordFulkerson(int n, int m, Edge[][] adj, int s, int t) { auto visited = new bool[](n); int dfs(int u, int f) { if (u == t) {return f;} visited[u] = 1; foreach (ref e ; adj[u]) { if (!visited[e.to] && e.c > 0) { int d = dfs(e.to, min(f, e.c)); if (d > 0) { e.c -= d; adj[e.to][e.rev].c += d; return d; } } } return 0; } int F; while (true) { visited[] = 0; int f = dfs(0, inf); if (!f) break; F += f; } return F; } 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.string; // chomp????????????????????????(?????????????????????) import std.conv; // to???????????????????????? import std.array; // split????????????????????? import std.algorithm; // map????????????????????? void main() { auto input = readln.split.map!(to!int); int a = input[0]; int b = input[1]; int c = input[2]; int cnt = 0; foreach (i; a .. (b + 1)) { if (c % i == 0) { cnt++; } } writeln(cnt); }
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; import std.container; enum MAX = 1_000_100; ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias Pass = Tuple!(long, "x", long, "y"); void main() { auto s = sread(); long l = s.length / 2, cnt; foreach(i; iota(l)) { if(s[i] != s[$ - i - 1]) cnt++; } cnt.writeln(); } 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; } }
D
import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; uint readNumber() { return readln.chomp.to!uint; } void main() { uint[3 * 3] numbers; bool[3 * 3] checked; size_t contain(uint x,out size_t index) { foreach (size_t i, n;numbers) { if (n == x) { index = i; return true; } } return false; } bool isBingo() { auto c = checked; if (c[0] && c[1] && c[2]) return true; if (c[3] && c[4] && c[5]) return true; if (c[6] && c[7] && c[8]) return true; if (c[0] && c[3] && c[6]) return true; if (c[1] && c[4] && c[7]) return true; if (c[2] && c[5] && c[8]) return true; if (c[0] && c[4] && c[8]) return true; if (c[2] && c[4] && c[6]) return true; return false; } foreach (i;0..3) { numbers[i * 3..(i + 1) * 3] = readln.chomp.split(" ").map!(to!uint).array; } auto n = readNumber; auto r = (() => readln.chomp.to!uint).repeat(n).map!"a()"; foreach (x;r) { size_t found; if (contain(x, found)) { checked[found] = true; } } if (isBingo) { writeln("Yes"); } else { writeln("No"); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto S = readln.chomp; writeln(S[2] == S[3] && S[4] == S[5] ? "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; import std.concurrency; void times(alias fun)(int n) { foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } // fold was added in D 2.071.0. template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } void main() { readln.chomp.to!long.pipe!"(a-1)/11*2+((a+10)%11<6?1:2)".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.container; void main() { auto n = readln.chomp.to!int; while (n--) { auto pat = readln.chomp; auto str = readln.chomp; foreach_reverse (e; pat) { if (e == 'C') { str = str[1..$] ~ str[0]; } else if (e == 'J') { str = str[$-1] ~ str[0..$-1]; } else if (e == 'E') { if (str.length % 2) { str = str[str.length/2+1..$] ~ str[str.length/2] ~ str[0..str.length/2]; } else { str = str[str.length/2..$] ~ str[0..str.length/2]; } } else if (e == 'A') { str = str.array.reverse.to!string; } else if (e == 'M') { foreach (i; 0..str.length) { if (str[i] >= '0' && str[i] < '9') { str = str[0..i] ~ (str[i] + 1).to!char ~ str[i+1..$]; } else if (str[i] == '9') { str = str[0..i] ~ '0' ~ str[i+1..$]; } } } else { foreach (i; 0..str.length) { if (str[i] > '0' && str[i] <= '9') { str = str[0..i] ~ (str[i] - 1).to!char ~ str[i+1..$]; } else if (str[i] == '0') { str = str[0..i] ~ '9' ~ str[i+1..$]; } } } } str.writeln; } }
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 q; scan(q); while (q--) { string s, t; scan(s); scan(t); auto ans = solve(s, t); writeln(ans); } } int solve(string s, string t) { auto n = s.length.to!int; auto m = t.length.to!int; auto dp = new int[][](n + 1, m + 1); foreach (i ; 1 .. n + 1) { foreach (j ; 1 .. m + 1) { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); if (s[i - 1] == t[j - 1]) chmax(dp[i][j], dp[i - 1][j - 1] + 1); } } auto res = dp[n][m]; return res; } 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.conv, std.string; import std.algorithm, std.array, std.container; import std.numeric, std.math; import core.bitop; T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; } string RDR()() { return readln.chomp; } void ERR(T)(in T x) { stderr.writeln(x); } long mod = pow(10, 9) + 7; long moda(long x, long y) { return (x + y) % mod; } long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; } long modm(long x, long y) { return (x * y) % mod; } void main() { auto S = RD!string; auto T = RD!string; ulong pos = ulong.max; foreach (i; 0..S.length-T.length+1) { bool ng = false; foreach (j, c; T) { if (S[i+j] != c && S[i+j] != '?') { ng = true; break; } } if (!ng) { pos = i; } } if (pos == ulong.max) writeln("UNRESTORABLE"); else { auto ans = S.dup; foreach (i; 0..T.length) { ans[pos+i] = T[i]; } ans = ans.replace("?", "a"); writeln(ans); } stdout.flush(); }
D
import std.stdio, std.array, std.conv, std.string, std.range, std.algorithm; void main() { long n = readln.strip.to!(long); string s = readln.strip; long k = readln.strip.to!(long); writeln(cast(dchar[])(s.map!(c => (c == s[k-1]) ? c : '*').array)); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto S = readln.chomp; writeln(S.length > 3 && S[0..4] == "YAKI" ? "Yes" : "No"); }
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 s = readln.chomp; if (n % 2 == 0 && s[0..n/2] == s[n/2..n]) { writeln("Yes"); } else { writeln("No"); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math; void main() { auto X = readln.chomp.to!int; auto A = readln.chomp.to!int; auto B = readln.chomp.to!int; writeln((X - A) % B); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { int n, k, x, y; scan(n); scan(k); scan(x); scan(y); writeln(x*(min(k, n)) + y*(max(0, n - k))); } 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; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { string s, t; scan(s); scan(t); auto n = s.length; bool ok; foreach (i ; 0 .. n) { auto sr = s[i .. $] ~ s[0 .. i]; if (sr == t) ok = true; } writeln(ok ? "Yes" : "No"); } 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.math, std.algorithm, std.array, std.string, std.conv, std.container, std.range; void main() { int n = readln.chomp.to!int; iota(1, n+1).filter!(i => i.to!string.length & 1) .array .length .writeln; }
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 readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n; readV(n); int[] a; readA(n, a); auto b = 20; auto c = new int[][](b, n); foreach (i; 0..b) foreach (j; 0..n) c[i][j] = a[j].bitTest(i); foreach (i; 0..b) foreach (j; 1..n) c[i][j] += c[i][j-1]; auto ans = 0L; foreach (l; 0..n) { auto r = n; foreach (i; 0..b) r = min(r, c[i].assumeSorted.lowerBound(c[i][l] + 2 - a[l].bitTest(i)).length); ans += r-l; } writeln(ans); } pragma(inline) { pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; } pure T bitSet(T)(T n, size_t i) { return n | (T(1) << i); } pure T bitReset(T)(T n, size_t i) { return n & ~(T(1) << i); } pure T bitComp(T)(T n, size_t i) { return n ^ (T(1) << i); } pure T bitSet(T)(T n, size_t s, size_t e) { return n | ((T(1) << e) - 1) & ~((T(1) << s) - 1); } pure T bitReset(T)(T n, size_t s, size_t e) { return n & (~((T(1) << e) - 1) | ((T(1) << s) - 1)); } pure T bitComp(T)(T n, size_t s, size_t e) { return n ^ ((T(1) << e) - 1) & ~((T(1) << s) - 1); } import core.bitop; pure int bsf(T)(T n) { return core.bitop.bsf(ulong(n)); } pure int bsr(T)(T n) { return core.bitop.bsr(ulong(n)); } pure int popcnt(T)(T n) { return core.bitop.popcnt(ulong(n)); } }
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]; } int count; for(int i = 0; i < h; i ++){ string s = readln.chomp; foreach(c; s) if(c == '#') count += 1; } string ans; if(count == h + w - 1) ans = "Possible"; else ans = "Impossible"; ans.writeln; }
D
import std.stdio; import std.string; import std.format; 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.concurrency; import std.traits; import std.uni; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; enum long INF = long.max/3; enum long MOD = 10L^^9+7; void main() { long N, M, K; scanln(N, M, K); long[] as = readln.split.to!(long[]); long[][] dp = new long[][](2, N+1); foreach(j; 1..K+1) { dp[j%2][] = -INF; long[] xs = dp[(j-1)%2][0..N]; size_t[] ys = xs.slideMinimum!(long, "a>b")(M); foreach(i; 0..N) { dp[j%2][i+1] = xs[ys[i]] + j*as[i]; } } dp[K%2].reduce!max.writeln; } // スライド最小値/最大値 O(n+k) // fun = "a<b" => ys[i] := argmin(xs[max(0, i+1-k)..min(n, i+1)]) // fun = "a>b" => ys[i] := argmax(xs[max(0, i+1-k)..min(n, i+1)]) size_t[] slideMinimum(T, alias fun = "a<b")(T[] xs, size_t k) if(is(typeof(binaryFun!fun(T.init, T.init)) == bool)) in { assert(k > 0); } body { size_t n = xs.length; size_t[] deq = new size_t[n]; size_t l = 0, r = 0; size_t empty() { return l == r; } size_t front() { return deq[l]; } size_t back() { return deq[r-1]; } void removeFront() { l++; } void removeBack() { r--; } void insertBack(size_t v) { deq[r++] = v; } size_t[] ys = new size_t[n+k-1]; foreach(i; 0..n+k-1) { if (i < n) { while(!empty() && !binaryFun!fun(xs[back()], xs[i])) { removeBack(); } insertBack(i); } ys[i] = front(); if (front()+k == i+1) { removeFront(); } } assert(empty()); return ys; } // ---------------------------------------------- void times(alias fun)(long n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(long n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { // `(x+y-1)/y` will only work for positive numbers ... T t = x / y; if (t * y < x) t++; return t; } T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { T t = x / y; if (t * y > x) t--; return t; } ref T ch(alias fun, T, S...)(ref T lhs, S rhs) { return lhs = fun(lhs, rhs); } unittest { long x = 1000; x.ch!min(2000); assert(x == 1000); x.ch!min(3, 2, 1); assert(x == 1); x.ch!max(100).ch!min(1000); // clamp assert(x == 100); x.ch!max(0).ch!min(10); // clamp assert(x == 10); } mixin template Constructor() { import std.traits : FieldNameTuple; this(Args...)(Args args) { // static foreach(i, v; args) { foreach(i, v; args) { mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v; } } } void scanln(Args...)(auto ref Args args) { import std.meta; template getFormat(T) { static if (isIntegral!T) { enum getFormat = "%d"; } else static if (isFloatingPoint!T) { enum getFormat = "%g"; } else static if (isSomeString!T || isSomeChar!T) { enum getFormat = "%s"; } else { static assert(false); } } enum string fmt = [staticMap!(getFormat, Args)].join(" "); string[] inputs = readln.chomp.split; foreach(i, ref v; args) { v = inputs[i].to!(Args[i]); } } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { private template RebindableOrUnqual(T) { static if (is(T == class) || is(T == interface) || isDynamicArray!T || isAssociativeArray!T) alias RebindableOrUnqual = Rebindable!T; else alias RebindableOrUnqual = Unqual!T; } private auto extremum(alias map, alias selector = "a < b", Range)(Range r) if (isInputRange!Range && !isInfinite!Range && is(typeof(unaryFun!map(ElementType!(Range).init)))) in { assert(!r.empty, "r is an empty range"); } body { alias Element = ElementType!Range; RebindableOrUnqual!Element seed = r.front; r.popFront(); return extremum!(map, selector)(r, seed); } private auto extremum(alias map, alias selector = "a < b", Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seedElement) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void) && is(typeof(unaryFun!map(ElementType!(Range).init)))) { alias mapFun = unaryFun!map; alias selectorFun = binaryFun!selector; alias Element = ElementType!Range; alias CommonElement = CommonType!(Element, RangeElementType); RebindableOrUnqual!CommonElement extremeElement = seedElement; // if we only have one statement in the loop, it can be optimized a lot better static if (__traits(isSame, map, a => a)) { // direct access via a random access range is faster static if (isRandomAccessRange!Range) { foreach (const i; 0 .. r.length) { if (selectorFun(r[i], extremeElement)) { extremeElement = r[i]; } } } else { while (!r.empty) { if (selectorFun(r.front, extremeElement)) { extremeElement = r.front; } r.popFront(); } } } else { alias MapType = Unqual!(typeof(mapFun(CommonElement.init))); MapType extremeElementMapped = mapFun(extremeElement); // direct access via a random access range is faster static if (isRandomAccessRange!Range) { foreach (const i; 0 .. r.length) { MapType mapElement = mapFun(r[i]); if (selectorFun(mapElement, extremeElementMapped)) { extremeElement = r[i]; extremeElementMapped = mapElement; } } } else { while (!r.empty) { MapType mapElement = mapFun(r.front); if (selectorFun(mapElement, extremeElementMapped)) { extremeElement = r.front; extremeElementMapped = mapElement; } r.popFront(); } } } return extremeElement; } private auto extremum(alias selector = "a < b", Range)(Range r) if (isInputRange!Range && !isInfinite!Range && !is(typeof(unaryFun!selector(ElementType!(Range).init)))) { return extremum!(a => a, selector)(r); } // if we only have one statement in the loop it can be optimized a lot better private auto extremum(alias selector = "a < b", Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seedElement) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void) && !is(typeof(unaryFun!selector(ElementType!(Range).init)))) { return extremum!(a => a, selector)(r, seedElement); } auto minElement(alias map = (a => a), Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { return extremum!map(r); } auto minElement(alias map = (a => a), Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void)) { return extremum!map(r, seed); } auto maxElement(alias map = (a => a), Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { return extremum!(map, "a > b")(r); } auto maxElement(alias map = (a => a), Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void)) { return extremum!(map, "a > b")(r, seed); } } // popcnt with ulongs was added in D 2.071.0 static if (__VERSION__ < 2071) { ulong popcnt(ulong x) { x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L); x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L); x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL); x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL); x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL); x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL); return x; } }
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; 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; const MOD = 10^^9 + 7; long[][] pascal(int n) { auto g = new long[][](n + 1, n + 1); g[0][0] = 1; for (int i = 1; i < n; i++) { for (int j = 0; j <= i; j++) { g[i][j] = g[i-1][j]; if (j > 0) { g[i][j] += g[i-1][j-1]; g[i][j] %= MOD; } } } return g; } long[][] C; void calc(int n, int k) { C = pascal(n + k); for (int i = 1; i <= k; i++) { // すき間のどこに入れるか long x = C[n - k + 1][i]; // i 個のすき間にボールを入れる // 最低 1 つは入れる必要があるので、あらかじめ i 個分を引く long y = C[k - i + i - 1][i - 1]; writeln(x * y % MOD); } } void main() { int n, k; scan(n, k); calc(n, k); }
D
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } bool solve(int L, int N, int R, in string S, in string T, char plus, char minus) { int now = R; foreach (i; 0 .. N) { if (S[i] == plus) { ++now; if (now >= L) { return true; } } if (T[i] == minus) { if (now > 0) { --now; } } } return false; } void main() { try { for (; ; ) { const H = readInt(); const W = readInt(); const N = readInt(); const RX = readInt() - 1; const RY = readInt() - 1; const S = readToken(); const T = readToken(); const resXP = solve(H, N, RX, S, T, 'D', 'U'); const resXM = solve(H, N, H - 1 - RX, S, T, 'U', 'D'); const resYP = solve(W, N, RY, S, T, 'R', 'L'); const resYM = solve(W, N, W - 1 - RY, S, T, 'L', 'R'); writeln((resXP || resXM || resYP || resYM) ? "NO" : "YES"); } } catch (EOFException e) { } }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int a, b; rd(a, b); if(a+b==15){ writeln("+"); }else if(a*b==15){ writeln("*"); }else{ writeln("x"); } } 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.algorithm; import std.array; import std.ascii; import std.bigint; import std.complex; import std.container; import std.conv; import std.functional; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } void readlnTo(T...)(ref T t) { auto s = readln().split(); assert(s.length == t.length); foreach(ref ti; t) { ti = s[0].to!(typeof(ti)); s = s[1..$]; } } class UnionFindTree { public: this(size_t n) { parent = iota(n).array(); rank = new size_t[](n); } size_t find(size_t n) { if(n == parent[n]) { return n; } else { parent[n] = find(parent[n]); return parent[n]; } } void unite(size_t n, size_t m) { auto p1 = find(n); auto p2 = find(m); if(n == m) { return; } if(rank[p1] < rank[p2]) { swap(p1, p2); } if(rank[p1] == rank[p2]) { ++rank[p1]; } parent[p2] = p1; } private: size_t[] parent; size_t[] rank; } unittest { auto uft = new UnionFindTree(10); foreach(i; iota(10)) { assert(uft.find(i) == i); } foreach(i; iota(10).filter!(a => a%3 == 0).drop(1)) { uft.unite(i, i-3); } foreach(i; iota(10).filter!(a => a%3 == 0)) { assert(uft.find(0) == uft.find(i)); } foreach(i; iota(10).filter!(a => a%3 != 0)) { assert(uft.find(0) != uft.find(i)); } } const real eps = 1e-10; const long p = 1_000_000_000 + 7; void main(){ long x, y; readlnTo(x, y); writeln(abs(x-y) > 1 ? "Alice": "Brown"); }
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; void main() { int[10] wSum, kSum; foreach (i; 0..10) { wSum[i] = readln.chomp.to!int; } foreach (i; 0..10) { kSum[i] = readln.chomp.to!int; } writeln(wSum.array.sort!("a > b")[0..3].reduce!("a+b"), " ", kSum.array.sort!("a > b")[0..3].reduce!("a+b")); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto xy = readln.split.to!(long[]); auto x = xy[0]; auto y = xy[1]; if (y > x) { if (x < 0 && y > 0) { writeln(min(y - x, abs(y + x) + 1)); } else { writeln(y - x); } } else { if (x == 0) { writeln(-y + 1); } else if (y == 0) { writeln(x + 1); } else if (x > 0 && y > 0) { writeln(min(x - y + 2, x + y + 1)); } else if (x > 0 && y < 0) { writeln(abs(x + y) + 1); } else { writeln(min(-y + x + 2, -x - y + 1)); } } }
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; import std.concurrency; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; void main() { recurrence!"a[n-1]+a[n-2]"(2L, 1L).take(readln.chomp.to!int + 1).array.back.writeln; } // ---------------------------------------------- void scanln(Args...)(ref Args args) { foreach(i, ref v; args) { "%d".readf(&v); (i==args.length-1 ? "\n" : " ").readf; } // ("%d".repeat(args.length).join(" ") ~ "\n").readf(args); } void times(alias fun)(int n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { auto minElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto minimum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b < minimum) { element = a; minimum = b; } } return element; } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto maximum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b > maximum) { element = a; maximum = b; } } return element; } }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.7.4" +/ import std.stdio, std.algorithm, std.range, std.conv; import std.numeric : gcd; // import dcomp.foundation, dcomp.scanner; int main() { Scanner sc = new Scanner(stdin); int n; sc.read(n); long[] a; sc.read(a); int zc = a.count(1).to!int; if (zc) { writeln(n - zc); return 0; } int ans = 10000; foreach (l; 0..n) { long g = 0; foreach (r; l+1..n+1) { g = gcd(g, a[r-1]); if (g == 1) { ans = min(ans, 2*(r-l-1) + l + (n-r)); } } } if (ans == 10000) ans = -1; writeln(ans); 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.algorithm; import std.conv; import std.range; import std.stdio; import std.string; immutable int letters = 26; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto n = readln.strip.to !(int); auto s = readln.strip.map !(q{a - 'a'}).array; auto t = readln.strip.map !(q{a - 'a'}).array; auto where = new int [] [letters]; foreach (i, ref c; s) { where[c] ~= i.to !(int); } auto half = 1; while (half < n) { half <<= 1; } auto total = half << 1; auto r = new int [total]; foreach (i; 0..n) { r[i + half] = i; } void alter (int lo, int hi, int delta) { for (lo += half, hi += half; lo < hi; lo >>= 1, hi >>= 1) { if (lo & 1) { r[lo++] += delta; } if (hi & 1) { r[--hi] += delta; } } } int value (int pos) { int res = 0; for (pos += half; pos > 0; pos >>= 1) { res += r[pos]; } return res; } long res = long.max; long cur = 0; main_loop: foreach (i; 0..n) { foreach (z; 0..t[i]) { if (!where[z].empty) { auto temp = value (where[z].front); res = min (res, cur + temp - i); if (temp == i) { break main_loop; } } } auto z = t[i]; if (where[z].empty) { break; } auto temp = value (where[z].front); cur += temp - i; alter (0, where[z].front, +1); where[z].popFront (); } if (res == long.max) { res = -1; } writeln (res); } }
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 N = readln.chomp.to!int; auto S = readln.chomp; auto ans = new int[](N); int tmp = 0; foreach (i; 0..N) { if (S[i] == '(') { ans[i] = tmp % 2; tmp += 1; } else { tmp -= 1; ans[i] = tmp % 2; } } ans.map!(to!string).join("").writeln; }
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; int sign (int x) { if (!x) return 0; if (x > 0) return 1; return -1; } long test () { auto s = readln.splitter.map!(to!int); immutable n = s.front; s.popFront; immutable x = s.front; auto t = readln[0 .. n]; auto a = new int[n+1]; foreach (i; 0 .. n) { a[i+1] = a[i] + (t[i] == '1' ? -1 : 1); } debug stderr.writeln (a); immutable m = a.back; if (m == 0) { return (minElement (a) <= x && x <= maxElement (a)) ? -1 : 0; } long r = 0; foreach (i; 0 .. n) { int delta = x - a[i]; if (m > 0) { if (delta >= 0 && !(delta % m)) ++r; } else { if (delta <= 0 && !((-delta) % (-m))) ++r; } } return r; } void main() { auto nt = readln.strip.to!int; foreach (tid; 0 .. nt) { writeln (test ()); } }
D
import std.stdio; import std.algorithm; import std.conv; import std.range; import std.string; import std.regex; void main() { int n, x; int ans = 1; int a=0, b=0; int c=0, d=-1; scanf("%d", &n ); for( int i = 1; i <= n; i ++ ) { scanf( "%d", &x ); if( d == -1 ) { d = x; } if( d == x ) { c ++; } else { if( x == 1 ) { if( a == 0 ) a = c; if( a != c ) ans = 0; } else { if( b == 0 ) b = c; if( b != c ) ans = 0; } d = x; c = 1; } } if( d == 0 ) { if( a == 0 ) a = c; if( a != c ) ans = 0; } else { if( b == 0 ) b = c; if( b != c ) ans = 0; } if( a == 0 ) a = b; if( b == 0 ) b = a; if( a != b ) ans = 0; if( ans == 1 ) printf( "YES" ); else printf( "NO" ); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto x=readln.split.to!(int[]); auto y=readln.split.to!(int[]); if(x.reduce!"a+b">=y.reduce!"a+b") writeln("Yes"); else writeln("No"); } 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
//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; final class InputReader { private: ubyte[] p, buffer; bool eof; bool rawRead () { if (eof) { return false; } p = stdin.rawRead (buffer); if (p.empty) { eof = true; return false; } return true; } ubyte nextByte(bool check) () { static if (check) { if (p.empty) { if (!rawRead ()) { return 0; } } } auto r = p.front; p.popFront (); return r; } public: this () { buffer = uninitializedArray!(ubyte[])(16<<20); } bool seekByte (in ubyte lo) { while (true) { p = p.find! (c => c >= lo); if (!p.empty) { return false; } if (!rawRead ()) { return true; } } } template next(T) if (isSigned!T) { T next () { if (seekByte (45)) { return 0; } T res; ubyte b = nextByte!false (); if (b == 45) { while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 - (b - 48); } } else { res = b - 48; while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 + (b - 48); } } } } template next(T) if (isUnsigned!T) { T next () { if (seekByte (48)) { return 0; } T res = nextByte!false () - 48; while (true) { ubyte b = nextByte!true (); if (b < 48 || b >= 58) { break; } res = res * 10 + (b - 48); } return res; } } T[] nextA(T) (in int n) { auto a = uninitializedArray!(T[]) (n); foreach (i; 0 .. n) { a[i] = next!T; } return a; } } void main() { auto r = new InputReader (); immutable nt = r.next!uint (); foreach (tid; 0 .. nt) { const n = r.next!uint; int lp, lc, lf; bool ok = true; foreach (i; 0 .. n) { const p = r.next!int; const c = r.next!int; const f = p - c; if (p < lp || c < lc || f < lf) { ok = false; } lp = p; lc = c; lf = f; } writeln (ok ? "YES" : "NO"); } }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; string solve (int n, int [] a) { foreach_reverse (b; 0..30) { auto z = a.count !(x => (x & (1 << b)) > 0); if (z & 1) { if (z % 2 == n % 2 && z % 4 == 3) { return "LOSE"; } return "WIN"; } } return "DRAW"; } void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto n = readln.strip.to !(int); auto a = readln.splitter.map !(to !(int)).array; writeln (solve (n, a)); } }
D
import std.stdio; import std.conv; import std.algorithm; import std.range; import std.string; import std.math; import std.format; void main() { string[][] q; int n = readln.chomp.to!int; q.length = n; foreach (string line; stdin.lines) { if (line == "quit\n") break; auto s = line.chomp.split; switch (s[0]) { case "push": q[s[1].to!ulong-1] ~= s[2]; break; case "move": q[s[2].to!ulong-1] ~= q[s[1].to!ulong-1].back; q[s[1].to!ulong-1].popBack; break; case "pop": q[s[1].to!ulong-1].back.writeln; q[s[1].to!ulong-1].popBack; break; default: } } }
D
void main(){ long n = _scan!long(); long cnt; foreach(i; 1..n+1){ if(i%3==0 || i%5==0)continue; else cnt += i; } cnt.writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.regex; // 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.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; } 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 A = RD!string; auto B = RD!string; auto ab = (A ~ B).to!long; bool ok; for (int i = 1; i*i <= ab; ++i) { if (i * i == ab) ok = true; } writeln(ok ? "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; long MOD = 10^^9 + 7; void main() { auto N = readln.chomp.to!int; int px = 0, py = 0, pt = 0; foreach (_; 0..N) { auto s = readln.split.map!(to!int); auto t = s[0]; auto x = s[1]; auto y = s[2]; auto dt = t - pt; auto dx = abs(x - px); auto dy = abs(y - py); if (dt < dx + dy) { writeln("No"); return; } if ((dt - dx - dy) % 2 != 0) { writeln("No"); return; } pt = t; px = x; py = y; } writeln("Yes"); }
D
void main() { problem(); } void problem() { const N = scan!int; const K = scan!long; const A = scan!int(N); int solve() { int[int] visited; int[] stack = new int[N]; { int next = A[0]; foreach(i; 0..N) { if (i == K - 1) return next; stack[i] = next; visited[next] = i; next = A[next - 1]; if (next in visited) { stack.length = i + 1; break; } } } const loopBackIndex = visited[A[stack.back - 1]]; const loopSize = stack.length - loopBackIndex; const loopStep = (K - stack.length - 1) % loopSize; return stack[loopBackIndex..$][loopStep]; } solve.writeln(); } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); import std.bigint, std.functional; // -----------------------------------------------
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ int a = read.to!int; int b = read.to!int; max(a + b, a - b, a * b).writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; enum P = 10^^9L+7; long[10^^5+1] SS; bool[10^^5+1] BS; void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; foreach (_; 0..M) { auto a = readln.chomp.to!int; BS[a] = true; } SS[0] = 1; foreach (i; 0..N) { if (BS[i]) continue; SS[i+1] = (SS[i+1] + SS[i]) % P; if (i+2 < N+1) SS[i+2] = (SS[i+2] + SS[i]) % P; } writeln(SS[N]); }
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 r; scan(r); writeln(3*r^^2); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto xa = readln.split.to!(int[]); writeln(xa[0] < xa[1] ? 0 : 10); }
D
// Vicfred // https://atcoder.jp/contests/abc158/tasks/abc158_a // implementation import std.stdio; import std.string; void main() { string s = readln.chomp; if(s.count('A') > 0 && s.count('B') > 0) "Yes".writeln; else "No".writeln; }
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 = lread(); auto T = aryread(); long M = lread(); auto P = new long[](M); auto X = new long[](M); foreach (i; 0 .. M) scan(P[i], X[i]); long s = T.sum(); foreach (i; 0 .. M) { writeln(s - T[P[i] - 1] + X[i]); } }
D
import std.stdio, std.algorithm, std.range, std.conv, std.string; import core.stdc.stdio; // foreach, foreach_reverse, writeln void main() { int n; scanf("%d", &n); int[] a = new int[n]; int[] b = new int[n]; foreach (i; 0..n) scanf("%d", &a[i]); foreach (i; 0..n) scanf("%d", &b[i]); int ans = 0; foreach (k; 0..29) { int mask = (1<<k)-1; int s = 0; if (n&1) { foreach (x; a) s ^= x>>k&1; foreach (x; b) s ^= x>>k&1; } int[] c = new int[n]; c[] = b[]&mask; sort(c); auto sorted = assumeSorted(c); foreach (x; a) { x &= mask; int l = n - to!int(sorted.lowerBound((1<<k)-x).length); s ^= l&1; } ans |= s<<k; } writeln(ans); }
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 a, p; readV(a, p); writeln((a*3+p)/2); }
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); auto a = readln.split.to!(int[]); writeln(a.reduce!max - a.reduce!min); } 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; 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() { auto n = lread(); string ans = ""; // writeln(ans); // writeln(cast(char)('a' + 25)); // writeln(cast(char)('a' + n % 26)); while (n > 0) { n -= 1; ans ~= cast(char)('a' + n % 26); n /= 26; } // writeln(ans); foreach_reverse (e; 0 .. (ans.length)) { write(ans[e]); } writeln(); } 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
void main() { int[] tmp = readln.split.to!(int[]); int a = tmp[0], b = tmp[1]; writeln(max(a+b, a-b, a*b)); } 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, std.string, std.array, std.conv; void main() { foreach (i; 0 .. 50) { int[] tmp = readln.chomp.split.to!(int[]); int m = tmp[0], f = tmp[1], r = tmp[2]; if (m == -1 && f == -1 && r == -1) break; if (m == -1 || f == -1) { "F".writeln; } else if (m + f >= 80) { "A".writeln; } else if (m + f >= 65) { "B".writeln; } else if (m + f >= 50) { "C".writeln; } else if (m + f >= 30) { if (r >= 50) { "C".writeln; } else { "D".writeln; } } else { "F".writeln; } } }
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; 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; } } void main() { auto r = new InputReader; auto a = r.next!int; auto b = r.next!int; auto c = r.next!int; long s; int k = min (a, b); //ab a -= k; s += k; b -= k; s += k; s += 2 * c; if (a || b) s++; writeln (s); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; writeln(N * (N-1) / 2 + M * (M-1) / 2); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto a=new long[](n+1); for(int i=1; i<=n; i++){ int q=i; for(int j=2; j<=i; j++){ while(q>1 && q%j==0){ q/=j; a[j]++; } } } // writeln(a); const long mod=1_000_000_000+7; auto ret=reduce!((r, e)=>(r*(e+1)%mod))(1L, a); writeln(ret); } void rd(Type...)(ref Type 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, std.string, std.range, std.algorithm, std.math, std.typecons, std.conv; void main() { auto inp = readln.split.to!(long[]); auto N = inp[0], T = inp[1]; auto t = readln.split.to!(long[]); long res = 0; foreach (i; 0..N-1) { res += min(T, t[i+1] - t[i]); } res += T; writeln(res); }
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; readV(d); write("Christmas"); foreach (_; 0..25-d) write(" Eve"); writeln; }
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 N = RD; auto d = RDA; long ans; foreach (i; 0..N) { foreach (j; i+1..N) { ans += d[i]*d[j]; } } writeln(ans); stdout.flush(); debug readln(); }
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; import std.format; void main() { auto ab = readln.split.to!(int[]), A = ab[0], B = ab[1]; auto S = readln.chomp; if(count(S, '-') == 1 && S[A] == '-') { writeln("Yes"); } else { writeln("No"); } }
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 h = readln.split.to!(int[]); h[0]--; bool ok = true; foreach (i ; 1 .. N) { if (h[i] < h[i - 1]) { ok = false; break; } if (h[i] > h[i - 1]) { h[i]--; } } writeln(ok ? "Yes" : "No"); } 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
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; import std.range; import std.math; import std.regex; void main(){ auto n=readln.chomp.to!int; if(n%2==0)writeln(n); else writeln(2*n); }
D
import std.stdio; int main() { int n = 0; scanf("%d", &n); writeln(n*n*n); return 0; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto a=readln.split.to!(int[]); int cnt=0; void f(int i=0, int pro=1){ if(i==n){if(pro%2==0) cnt++; return;} for(int d=-1; d<=1; d++) f(i+1, pro*(a[i]+d)); } f(); writeln(cnt); } 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.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto rd = readln.split.to!(int[]), a = rd[0], b = rd[1], c = rd[2]; writeln(c >= a && c <= b ? "Yes" : "No"); }
D
import std.algorithm; import std.conv; import std.stdio; import std.string; void main() { auto abc = readln.split.map!( to!int ); auto k = readln.strip.to!int; writeln( solve( abc[ 0 ], abc[ 1 ], abc[ 2 ], k ) ); } int solve( in int a, in int b, in int c, in int k ) { return a + b + c + max( a, b, c ) * ( 2 ^^ k - 1 ); } unittest { assert( solve( 5, 3, 11, 1 ) == 30 ); assert( solve( 3, 3, 4, 2 ) == 22 ); }
D
import std.stdio, std.array, std.conv, std.string, std.algorithm; void main(){ string s; for(;;){ s=readln(); if(stdin.eof()) break; int num = to!int(chomp(s)); int a,b,c,d,result=0; for(a = 0;a < 10;++a){ for(b = 0;b < 10;++b){ for(c = 0;c < 10;++c){ for(d = 0;d < 10; ++d){ if(a+b+c+d == num) ++result; } } } } writeln(result); } }
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; long cnt, res; foreach (i; 0..s.length) { if (s[i] == 'B') { cnt++; } else { res += cnt; } } res.writeln; }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; // import dcomp.bigint; int main() { auto sc = new Scanner(stdin); long aa, bb, cc; sc.read(aa, bb, cc); alias Uint = uintN!1; auto a = Uint(aa), b = Uint(bb), c = Uint(cc); int co = 0; while (co < 2^^15) { if (a%2 || b%2 || c%2) break; co++; auto na = (b+c)/2; auto nb = (a+c)/2; auto nc = (a+b)/2; a = na; b = nb; c = nc; } if (co == 2^^15) co = -1; writeln(co); return 0; } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { 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); } } } } version (X86) static if (__VERSION__ < 2071) { int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/ldc/inline.d */ // module dcomp.ldc.inline; version(LDC) { pragma(LDC_inline_ir) R inlineIR(string s, R, P...)(P); } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; 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 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; if (f.eof) return false; line = lineBuf[]; f.readln(line); } 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 { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { assert(buf.length == T.length); } x = buf; line.length = 0; } } 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 /Users/yosupo/Program/dcomp/source/dcomp/int128.d */ // module dcomp.int128; version(LDC) { // import dcomp.ldc.inline; } version(LDC) version(X86_64) { version = LDC_IR; } ulong[2] mul128(ulong a, ulong b) { ulong[2] res; version(LDC_IR) { ulong upper, lower; inlineIR!(` %r0 = zext i64 %0 to i128 %r1 = zext i64 %1 to i128 %r2 = mul i128 %r1, %r0 %r3 = trunc i128 %r2 to i64 %r4 = lshr i128 %r2, 64 %r5 = trunc i128 %r4 to i64 store i64 %r3, i64* %2 store i64 %r5, i64* %3`, void)(a, b, &lower, &upper); return [lower, upper]; } else version(D_InlineAsm_X86_64) { ulong upper, lower; asm { mov RAX, a; mul b; mov lower, RAX; mov upper, RDX; } return [lower, upper]; } else { ulong B = 2UL^^32; ulong[2] a2 = [a % B, a / B]; ulong[2] b2 = [b % B, b / B]; ulong[4] c; foreach (i; 0..2) { foreach (j; 0..2) { c[i+j] += a2[i] * b2[j] % B; c[i+j+1] += a2[i] * b2[j] / B; } } foreach (i; 0..3) { c[i+1] += c[i] / B; c[i] %= B; } return [c[0] + c[1] * B, c[2] + c[3] * B]; } } ulong div128(ulong[2] a, ulong b) { version(LDC_IR) { return inlineIR!(` %r0 = zext i64 %0 to i128 %r1 = zext i64 %1 to i128 %r2 = shl i128 %r1, 64 %r3 = add i128 %r0, %r2 %r4 = zext i64 %2 to i128 %r5 = udiv i128 %r3, %r4 %r6 = trunc i128 %r5 to i64 ret i64 %r6`,ulong)(a[0], a[1], b); } else version(D_InlineAsm_X86_64) { ulong upper = a[1], lower = a[0]; ulong res; asm { mov RDX, upper; mov RAX, lower; div b; mov res, RAX; } return res; } else { import std.bigint, std.conv; return (((BigInt(a[1]) << 64) + BigInt(a[0])) / BigInt(b)).to!ulong; } } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/bigint.d */ // module dcomp.bigint; import core.checkedint; // import dcomp.int128, dcomp.foundation; void addMultiWord(in ulong[] l, in ulong[] r, ulong[] res) { auto N = res.length; bool of = false; foreach (i; 0..N) { bool nof; res[i] = addu( (i < l.length) ? l[i] : 0UL, (i < r.length) ? r[i] : 0UL, nof); if (of) { res[i]++; nof |= (res[i] == 0); } of = nof; } } void subMultiWord(in ulong[] l, in ulong[] r, ulong[] res) { auto N = res.length; bool of = false; foreach (i; 0..N) { bool nof; res[i] = subu( (i < l.length) ? l[i] : 0UL, (i < r.length) ? r[i] : 0UL, nof); if (of) { res[i]--; nof |= (res[i] == ulong.max); } of = nof; } } void mulMultiWord(in ulong[] l, in ulong r, ulong[] res) { auto N = res.length; ulong ca; foreach (i; 0..N) { auto u = mul128((i < l.length) ? l[i] : 0UL, r); bool of; res[i] = addu(u[0], ca, of); if (of) u[1]++; ca = u[1]; } } void shiftLeftMultiWord(in ulong[] l, int n, ulong[] res) { size_t N = res.length; int ws = n / 64; int bs = n % 64; import std.stdio; foreach_reverse (ptrdiff_t i; 0..N) { ulong b = (0 <= i-ws && i-ws < l.length) ? l[i-ws] : 0UL; if (bs == 0) res[i] = b; else { ulong a = (0 <= i-ws-1 && i-ws-1 < l.length) ? l[i-ws-1] : 0UL; res[i] = (b << bs) | (a >> (64-bs)); } } } int cmpMultiWord(in ulong[] l, in ulong[] r) { import std.algorithm : max; auto N = max(l.length, r.length); foreach_reverse (i; 0..N) { auto ld = (i < l.length) ? l[i] : 0UL; auto rd = (i < r.length) ? r[i] : 0UL; if (ld < rd) return -1; if (ld > rd) return 1; } return 0; } struct uintN(int N) if (N >= 1) { import core.checkedint; ulong[N] d; this(ulong x) { d[0] = x; } this(string s) { foreach (c; s) { this *= 10; this += uintN(c-'0'); } } string toString() { import std.algorithm : reverse; char[] s; if (!this) return "0"; while (this) { s ~= cast(char)('0' + (this % uintN(10))[0]); this /= uintN(10); } reverse(s); return s.idup; } ref inout(ulong) opIndex(int idx) inout { return d[idx]; } T opCast(T: bool)() { import std.algorithm, std.range; return d[].find!"a!=0".empty == false; } uintN opUnary(string op)() const if (op == "~") { uintN res; foreach (i; 0..N) { res[i] = ~d[i]; } return res; } uintN opBinary(string op)(in uintN r) const if (op == "&" || op == "|" || op == "^") { uintN res; foreach (i; 0..N) { res[i] = mixin("d[i]" ~ op ~ "r.d[i]"); } return res; } uintN opBinary(string op : "<<")(int n) const { if (N * 64 <= n) return uintN(0); uintN res; int ws = n / 64; int bs = n % 64; if (bs == 0) { res.d[ws..N][] = d[0..N-ws][]; return res; } foreach_reverse (i; 1..N-ws) { res[i+ws] = (d[i] << bs) | (d[i-1] >> (64-bs)); } res[ws] = (d[0] << bs); return res; } uintN opBinary(string op : ">>")(int n) const { if (N * 64 <= n) return uintN(0); uintN res; int ws = n / 64; int bs = n % 64; if (bs == 0) { res.d[0..N-ws][] = d[ws..N][]; return res; } foreach_reverse (i; 0..N-ws-1) { res[i] = (d[i+ws+1] >> (64-bs)) | (d[i+ws] << bs); } res[dim-ws-1] = (d[N-1] << bs); return res; } int opCmp(in uintN r) const { return cmpMultiWord(d, r.d); } uintN opUnary(string op)() if (op == "++") { uintN res; bool of = true; foreach (i; 0..N) { if (of) { d[i]++; if (d[i]) of = false; } } return res; } uintN opUnary(string op)() if (op == "--") { return this -= uintN(1); } uintN opUnary(string op)() const if (op=="+" || op=="-") { if (op == "+") return this; if (op == "-") { return ++(~this); } } uintN opBinary(string op : "+")(in uintN r) const { uintN res; addMultiWord(d, r.d, res.d); return res; } uintN opBinary(string op : "-")(in uintN r) const { uintN res; subMultiWord(d, r.d, res.d); return res; } uintN opBinary(string op : "*")(in uintN r) const { uintN res; foreach (s; 0..N) { foreach (i; 0..s+1) { int j = s-i; auto u = mul128(d[i], r[j]); bool of; res[s] = addu(res[s], u[0], of); if (s+1 < N) { if (of) { res[s+1]++; of = (res[s+1] == 0); } res[s+1] = addu(res[s+1], u[1], of); if (s+2 < N && of) res[s+2]++; } } } return res; } uintN opBinary(string op : "*")(in ulong r) const { uintN res; mulMultiWord(d, r, res.d); return res; } uintN opBinary(string op : "/")(in uintN rr) const { import core.bitop; int up = -1, shift; foreach_reverse(i; 0..N) { if (rr[i]) { up = i; shift = 63 - bsr(rr[i]); break; } } assert(up != -1); ulong[N+1] l; l[0..N] = d[0..N]; import std.stdio; shiftLeftMultiWord(l, shift, l); auto r = (rr << shift); uintN res; foreach_reverse (i; 0..N-up) { ulong pred = (r[up] == ulong.max) ? l[i+up+1] : div128([l[i+up], l[i+up+1]], r[up]+1); res[i] = pred; ulong[N+1] buf; mulMultiWord(r.d[], pred, buf); subMultiWord(l[i..i+up+2], buf[], l[i..i+up+2]); int co = 0; while (cmpMultiWord(l[i..i+up+2], r.d[]) != -1) { co++; res[i]++; subMultiWord(l[i..i+up+2], r.d[], l[i..i+up+2]); } assert(co <= 2); } return res; } uintN opBinary(string op : "/")(in ulong r) const { return this / uintN(r); } uintN opBinary(string op : "%", T)(in T r) const { return this - this/r*r; } auto opOpAssign(string op, T)(in T r) { return mixin("this=this" ~ op ~ "r"); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto na = readln.split.to!(int[]); auto N = na[0]; auto A = na[1]; auto xs = readln.split.to!(int[]); auto DP = new long[][][](N, N, N*50+1); foreach (ref a; DP) foreach (ref b; a) foreach (ref c; b) c = -1; long solve(int i, int n, int s) { if (i == N) return n != 0 && s / n == A && s % n == 0 ? 1 : 0; if (DP[i][n][s] == -1) { DP[i][n][s] = solve(i+1, n, s) + solve(i+1, n+1, s + xs[i]); } return DP[i][n][s]; } writeln(solve(0, 0, 0)); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; /// [..) struct SegTree(alias _fun, alias def, T) if (is(typeof(def) : T)) { import std.functional : binaryFun; alias fun = binaryFun!_fun; /// this(size_t n, T[] ts) { this.n = 1; while (this.n < n) this.n *= 2; this.tree.length = this.n * 2 - 1; foreach (ref e; this.tree) e = def; foreach (i, e; ts) this.put(i, e); } /// void put(size_t i, T e) { i += this.n - 1; this.tree[i] = e; while (i > 0) { i = (i-1) / 2; this.tree[i] = fun(this.tree[i*2+1], this.tree[i*2+2]); } } /// T query(size_t a, size_t b) { T impl(size_t i, size_t l, size_t r) { if (r <= a || b <= l) return def; if (a <= l && r <= b) return this.tree[i]; return fun( impl(i*2+1, l, (l+r)/2), impl(i*2+2, (l+r)/2, r) ); } return impl(0, 0, this.n); } private: size_t n; T[] tree; } /// SegTree!(f, def, T) seg_tree(alias f, alias def, T)(size_t n, T[] arr = []) { return SegTree!(f, def, T)(n, arr); } /// SegTree!(f, def, T) seg_tree(alias f, alias def, T)(T[] arr) { return SegTree!(f, def, T)(arr.length, arr); } void main() { auto N = readln.chomp.to!int; auto S = readln.chomp.to!(char[]); SegTree!("a + b", 0, int)[] ss; ss.length = 26; foreach (ref s; ss) s = SegTree!("a + b", 0, int)(N+1, []); foreach (i, c; S) { ss[c-'a'].put(i, 1); } auto Q = readln.chomp.to!int; foreach (_; 0..Q) { auto q = readln.split; if (q[0] == "1") { auto i = q[1].to!int-1; auto c = q[2][0]; ss[S[i]-'a'].put(i, 0); S[i] = c; ss[c-'a'].put(i, 1); } else { auto l = q[1].to!int-1; auto r = q[2].to!int; int ret; foreach (i, ref s; ss) { ret += min(s.query(l, r), 1); } writeln(ret); } } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nk = readln.split.to!(int[]); auto N = nk[0]; auto K = nk[1]; auto S = readln.chomp; int cnt, p; int[] as; foreach (c; S) { auto pp = c == '0' ? -1 : 1; if (p != pp) { if (p != 0) as ~= cnt * p; cnt = 0; p = pp; } ++cnt; } as ~= cnt * p; auto len = as.length; size_t i, j; int max_s; foreach (_; 0..K) { if (j == len) break; if (as[j] > 0) { max_s += as[j++]; } if (j == len) break; max_s += -as[j++]; } if (j <= len-1) max_s += as[j++]; auto s = max_s; while (j < len) { if (as[i] > 0) s -= as[i++]; s += as[i++]; s -= as[j++]; if (j < len) s += as[j++]; max_s = max(max_s, s); } writeln(max_s); }
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; // dfmt on void main() { long N, M, K; scan(N, M, K); auto A = aryread(); auto B = aryread(); auto S = [0L] ~ B ~ [0L]; foreach (i; 0 .. M) { S[i + 1] += S[i]; } long as; long ans; foreach (i; 0 .. N + 1) { long ok = 0; long ng = M + 1; while (1 < (ng - ok)) { long j = (ok + ng) / 2; if (S[j] <= K - as) { ok = j; } else { ng = j; } } dprint(i, ok); if (as + S[ok] <= K) ans = ans.max(i + ok); if (i < A.length) as += A[i]; } writeln(ans); }
D
/* imports all std modules {{{*/ import std.algorithm, std.array, std.ascii, std.base64, std.bigint, std.bitmanip, std.compiler, std.complex, std.concurrency, std.container, std.conv, std.csv, std.datetime, std.demangle, std.encoding, std.exception, std.file, std.format, std.functional, std.getopt, std.json, std.math, std.mathspecial, std.meta, std.mmfile, std.net.curl, std.net.isemail, std.numeric, std.parallelism, std.path, std.process, std.random, std.range, std.regex, std.signals, std.socket, std.stdint, std.stdio, std.string, std.system, std.traits, std.typecons, std.uni, std.uri, std.utf, std.uuid, std.variant, std.zip, std.zlib; /*}}}*/ /+---test issii 2 ---+/ /+---test qq 81 ---+/ /+---test cooooooooonteeeeeeeeeest 999993333 ---+/ void main(string[] args) { auto S = readln.chomp.to!string; const K = readln.chomp.to!long; long ans; auto l = S.countUntil!(x => x != S[0]); auto r = S.retro.countUntil!(x => x != S[$-1]); if (l < 0 || r < 0) { (S.length*K/2).writeln; return; } ans += l/2; ans += r/2; if (S[0] == S[$-1]) { ans += (l+r)/2 * (K-1); } else { ans += (l/2 + r/2) * (K-1); } r = S.length - r; long n = 1; foreach (i; l..r+1) { if (S[i-1] == S[i]) { ++n; } else { ans += n/2 * K; n = 1; } } ans.writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; struct Graph(T) { T L, R; T[][] adj; this(T L, T R) { this.L = L; this.R = R; adj.length = L + R; } void add_edge(T u, T v) { adj[u] ~= v+L; adj[v+L] ~= u; } T maximum_matching() { bool[] visited; T[] meta; visited.length = L; meta.length = L + R; meta[] = -1; bool augment(T u) { if (visited[u]) return false; visited[u] = true; foreach (w; adj[u]) { auto v = meta[w]; if (v < 0 || augment(v)) { meta[u] = w; meta[w] = u; return true; } } return false; } auto match = 0; foreach (u; 0..L) { visited[] = false; if (augment(u)) ++match; } return match; } } void main() { auto xye = readln.split.to!(int[]); auto X = xye[0]; auto Y = xye[1]; auto E = xye[2]; auto G = Graph!int(X, Y); foreach (_; 0..E) { auto xy = readln.split.to!(int[]); G.add_edge(xy[0], xy[1]); } writeln(G.maximum_matching()); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); void main() { auto s = sread(); auto s_len = s.length; // writeln(s_len); foreach (i; 0 .. s_len) { write('x'); } } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.typecons; import std.numeric, std.math; import core.bitop; string FMT_F = "%.10f"; T RD(T = long)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.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; } 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; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; 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 N = RD; auto M = RD; auto edge = new long[2][][](N); auto use = new long[][](N, N); foreach (i; 0..N) fill(use[i], -1); foreach (i; 0..M) { auto a = RD-1; auto b = RD-1; auto c = RD; edge[a] ~= [b, c]; edge[b] ~= [a, c]; use[a][b] = 0; use[b][a] = 0; } long[][] f(long from) { auto path = new long[][](N); auto cost = new long[](N); fill(cost, long.max); long[] open = [from]; path[from] = [from]; cost[from] = 0; while (!open.empty) { auto n = open.front; open.popFront; foreach (e; edge[n]) { auto pos = e[0]; auto c = e[1] + cost[n]; if (c < cost[pos]) { path[pos] = path[n] ~ pos; cost[pos] = c; open ~= pos; } } } return path; } foreach (i; 0..N) { auto r = f(i); foreach (e; r) { foreach (j; 0..e.length-1) { use[e[j]][e[j+1]] = 1; use[e[j+1]][e[j]] = 1; } } } long ans; foreach (e; use) { foreach (ee; e) { if (ee == 0) ++ans; } } writeln(ans / 2); stdout.flush(); }
D
import std.stdio, std.array, std.conv, std.algorithm, std.string, std.numeric, std.range; string swap(string s){ if(s.indexOf("apple")!=-1) return s.replace("apple", "peach"); else if(s.indexOf("peach")!=-1) return s.replace("peach", "apple"); else return s; } void main() { string s; while( (s=readln.strip).length != 0 ){ auto as = s.split(' ').map!(swap).join(" "); writeln(as); } }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto rd = readln.split.to!(int[]), n = rd[0], y = rd[1]/1000; foreach (u; 0..n+1) foreach (v; 0..n+1) { auto w = n-u-v; if (w >= 0 && 10*u+5*v+w == y) { writeln(u, " ", v, " ", w); return; } } writeln("-1 -1 -1"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; enum P = 10^^9+7L; long[10^^5*2+50] F, RF; void init() { F[0] = F[1] = 1; foreach (i, ref x; F[2..$]) x = (F[i+1] * (i+2)) % P; { RF[$-1] = 1; auto x = F[$-1]; auto k = P-2; while (k) { if (k%2 == 1) RF[$-1] = (RF[$-1] * x) % P; x = x^^2 % P; k /= 2; } } foreach_reverse(i, ref x; RF[0..$-1]) x = (RF[i+1] * (i+1)) % P; } long comb(N)(N n, N k) { auto n_b = F[n]; // n! auto nk_b = RF[n-k]; // 1 / (n-k)! auto k_b = RF[k]; // 1 / k! auto nk_b_k_b = (nk_b * k_b) % P; // 1 / (n-k)!k! return (n_b * nk_b_k_b) % P; // n! / (n-k)!k! } long[200001] DA, DB, DC; void main() { init(); auto nabc = readln.split.to!(int[]); auto N = nabc[0]; auto A = nabc[1]; auto B = nabc[2]; auto C = nabc[3]; long RC = 1, rc = 100 - C, X = P-2; while (X) { if (X%2 == 1) RC = (RC * rc) % P; rc = rc^^2 % P; X /= 2; } foreach (n; 0..N*2+1) { if (n == 0) { DA[n] = DB[n] = DC[n] = 1; } else if (n == 1) { DA[n] = A; DB[n] = B; DC[n] = RC; } else { DA[n] = (DA[n-1] * A) % P; DB[n] = (DB[n-1] * B) % P; DC[n] = (DC[n-1] * RC) % P; } } long r; foreach (M; N..N*2) { long t = (DA[N] * DB[M-N]) % P; long s = (DA[M-N] * DB[N]) % P; t = (t + s) % P; t = (t * M) % P; t = (t * DC[M+1]) % P; t = (t * 100) % P; t = (t * comb(M-1, N-1)) % P; r = (t + r) % P; } writeln(r); }
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 a, b; readV(a, b); if (a+b >= 10) writeln("error"); else writeln(a+b); }
D
void main() { int[] xy = readln.split.to!(int[]); int x = xy[0], y = xy[1]; bool ok = true; int[] d30 = [4, 6, 9, 11]; if (xy.any!"a == 2") { ok = false; } else if (d30.canFind(x) != d30.canFind(y)) { ok = false; } 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
import std.stdio; import std.ascii; import std.conv; import std.string; import std.algorithm; import std.range; import std.functional; import std.math; import core.bitop; import std.numeric; void main() { auto hw = readln.split.to!(long[]); long h = hw[0]; long w = hw[1]; char[][] table; foreach (i; 0 .. h) table ~= readln.chomp.to!(char[]); bool[size_t] rows; bool[size_t] cols; foreach (i, r; table) if (r.all!(c => c == '.')) { rows[i] = true; } foreach (i; 0 .. w) { bool flag = true; foreach (j; 0 .. h) flag = flag && table[j][i] == '.'; if (flag) { cols[i] = true; } } foreach (r; 0 .. h) { foreach (c; 0 .. w) if (!(r in rows || c in cols)) { write(table[r][c]); } if (!(r in rows)) { writeln; } } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { int n, m; scan(n, m); auto uf = WeightedUnionFind!(int)(n + 1); bool ok = 1; foreach (i ; 0 .. m) { int l, r, d; scan(l, r, d); if (uf.same(l, r)) { if (uf.diff(l, r) != d) { ok = 0; } } else { uf.merge(l, r, d); } } writeln(ok ? "Yes" : "No"); } struct WeightedUnionFind(T) { private { int[] _parent, _rank; T[] _diff; } this(int N) { _parent = new int[](N); _rank = new int[](N); _diff = new T[](N); foreach (i ; 0 .. N) { _parent[i] = i; } } int findRoot(int x) { if (_parent[x] != x) { int r = findRoot(_parent[x]); _diff[x] += _diff[_parent[x]]; _parent[x] = findRoot(r); } return _parent[x]; } bool same(int x, int y) { return findRoot(x) == findRoot(y); } T weight(int x) { findRoot(x); return _diff[x]; } // x と y が異なるグループに属すならinf(十分大きい値)を返す T diff(int x, int y) { return same(x, y) ? weight(y) - weight(x) : T.max; } import std.algorithm : swap; // weight(y) = weight(x) + w bool merge(int x, int y, T w) { w += weight(x) - weight(y); int u = findRoot(x); int v = findRoot(y); // x と y が既に同じグループであるなら距離の更新はしない if (u == v) return false; if (_rank[u] < _rank[v]) { swap(u, v); w = -w; } if (_rank[u] == _rank[v]) { _rank[u]++; } _parent[v] = u; _diff[v] = w; return true; } } 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.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 s = new int[](n); foreach (i; 0..n) { s[i] = readln.chomp.to!int; } auto sum = s.sum; if (sum % 10) writeln(sum); else { bool flag = true; foreach (e; s.sort()) { if ((sum - e) % 10) { writeln(sum - e); flag = false; break; } } if (flag) writeln("0"); } }
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 = 10^^9 + 7; immutable long INF = 1L << 59; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto S = readln.chomp.map!(a => a == 'B').array; auto G = new int[][](N); auto cnt = new int[][](N, 2); auto deleted = new bool[](N); void dfs(int n) { if (deleted[n]) return; deleted[n] = true; foreach (m; G[n]) { if (deleted[m]) continue; cnt[m][S[n]] -= 1; if (cnt[m][S[n]] == 0) { dfs(m); } } } foreach (i; 0..M) { s = readln.split.map!(to!int); auto u = s[0] - 1; auto v = s[1] - 1; G[u] ~= v; G[v] ~= u; cnt[u][S[v]] += 1; cnt[v][S[u]] += 1; } foreach (i; 0..N) { if (!deleted[i] && (cnt[i][0] == 0 || cnt[i][1] == 0)) { dfs(i); } } writeln(deleted.all ? "No" : "Yes"); }
D
void main() { problem(); } void problem() { auto N = scan; void solve() { writeln(N.canFind('7') ? "Yes" : "No"); } solve(); } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.math; import std.bigint; void main(){ auto qty = readInt(); auto num = readLongs(); ulong negative = num.count!("a < 0"); bool zero = num.find(0).length ? true : false; auto pnum = num.map!(a => abs(a)); auto ret = pnum.sum; if(!zero && negative % 2 == 1) ret -= 2 * pnum.minPos.front; writeln(ret); } int readInt(){ return readln.chomp.to!int; } long[] readLongs(){ return readln.chomp.split.to!(long[]); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = readln.chomp.to!int; auto k = readln.chomp.to!int; auto ans = int.max; foreach (i; 0..1<<n) { auto r = 1; foreach (j; 0..n) if (i.bitTest(j)) r *= 2; else r += k; ans = min(ans, r); } writeln(ans); } pragma(inline) { pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; } pure T bitSet(T)(T n, size_t i) { return n | (T(1) << i); } pure T bitReset(T)(T n, size_t i) { return n & ~(T(1) << i); } pure T bitComp(T)(T n, size_t i) { return n ^ (T(1) << i); } import core.bitop; pure int bsf(T)(T n) { return core.bitop.bsf(ulong(n)); } pure int bsr(T)(T n) { return core.bitop.bsr(ulong(n)); } pure int popcnt(T)(T n) { return core.bitop.popcnt(ulong(n)); } }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; auto s = readln.chomp.to!(char[]); long k; rd(k); int j = -1; for (int i = 0; i < s.length; i++) { if (s[i] > '1') { j = i; break; } } if (j < 0) { writeln(1); } else { if (k <= j) { writeln(1); } else { writeln(s[j]); } } } 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 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 n = 0; auto s = sread(); foreach(i;s) { if(i == '+') { n++; } else { n--; } } n.writeln(); }
D
import std.stdio, std.string, std.conv; void main(){ auto xyz = readln.split.to!(int[]); auto x = xyz[0], y = xyz[1], z = xyz[2]; writeln((x - z) / (y + z)); }
D
void main() { long n, m; rdVals(n, m); long[][] edge = new long[][](n); foreach (i; 0 .. m) { long a, b; rdVals(a, b); --a, --b; edge[a] ~= b, edge[b] ~= a; } long[] dist = new long[n]; long[] que; que ~= 0; while (!que.empty) { long now = que.front; que.popFront; foreach (e; edge[now]) { if (dist[e]) continue; dist[e] = now + 1; que ~= e; } } "Yes".writeln; foreach (i; 1 .. n) { dist[i].writeln; } } enum long mod = 10L^^9 + 7; enum long inf = 1L << 60; enum double eps = 1.0e-9; 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
void main() { long n = rdElem; long l, r = n - 1; string s; string t = "Vacant"; l.writeln; stdout.flush; s = rdStr; if (s == t) return; string f = s; r.writeln; stdout.flush; s = rdStr; if (s == t) return; foreach (i; 0 .. 21) { long m = (l + r) >> 1; m.writeln; stdout.flush; s = rdStr; if (s == t) return; if ((m & 1) == (s == f)) r = m; else l = m; } } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; enum double eps = 1.0e-9; 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, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto ab = readln.split.to!(int[]); auto A = ab[0]; auto B = ab[1]; writeln(A <= 8 && B <= 8 ? "Yay!" : ":("); }
D
// Cheese-Cracker: cheese-cracker.github.io void theCode(){ ll a = scan; ll b = scan; ll diff = abs(a - b); ll rem = 0; if(diff > 0){ rem = min(b % diff, diff - (b % diff)); } writeln(diff, " ", rem); } void main(){ long tests = scan; // Toggle! while(tests--) theCode(); } /********* That's All Folks! *********/ import std.stdio, std.random, std.functional, std.container, std.algorithm; import std.numeric, std.range, std.typecons, std.string, std.math, std.conv; string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;} T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; } void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } } alias ll = long, tup = Tuple!(long, "x", long, "y");
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; auto N = s[0].to!int; auto K = s[1].to!long; auto A = N.iota.map!(_ => readln.split.map!(to!long).array).array; auto B = matpow(A, K); long ans = 0; foreach (i; 0..N) foreach (j; 0..N) (ans += B[i][j]) %= MOD; ans.writeln; } long[][] matmul(long[][] m1, long[][] m2) { int n = m1.length.to!int; long[][] ret = new long[][](n, n); foreach (i; 0..n) foreach (j; 0..n) foreach (k; 0..n) { (ret[i][j] += m1[i][k] * m2[k][j] % MOD) %= MOD; } return ret; } long[][] matpow(long[][] m, long x) { int n = m.length.to!int; long[][] ret = new long[][](n, n); foreach (i; 0..n) ret[i][i] = 1; while (x) { if (x % 2) ret = matmul(ret, m); m = matmul(m, m); x /= 2; } return ret; }
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; } 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 A = RD; auto B = RD; auto Q = RD; auto s = new long[](A); auto t = new long[](B); auto x = new long[](Q); foreach (i; 0..A) { s[i] = RD; } foreach (i; 0..B) { t[i] = RD; } foreach (i; 0..Q) { x[i] = RD; } foreach (i; 0..Q) { auto sl = assumeSorted(s).lowerBound(x[i]); auto tl = assumeSorted(t).lowerBound(x[i]); auto sr = assumeSorted(s).upperBound(x[i]); auto tr = assumeSorted(t).upperBound(x[i]); long l1, l2, r1, r2; if (!sl.empty) l1 = sl.back; if (!tl.empty) l2 = tl.back; if (!sr.empty) r1 = sr.front; if (!tr.empty) r2 = tr.front; long ans1 = long.max, ans2 = long.max, ans3 = long.max, ans4 = long.max; if (l1 != 0 && l2 != 0) ans1 = x[i] - min(l1, l2); if (r1 != 0 && r2 != 0) ans2 = max(r1, r2) - x[i]; if (l1 != 0 && r2 != 0) { auto d1 = x[i] - l1; auto d2 = r2 - x[i]; ans3 = min(d1, d2) * 2 + max(d1, d2); } if (r1 != 0 && l2 != 0) { auto d1 = x[i] - l2; auto d2 = r1 - x[i]; ans4 = min(d1, d2) * 2 + max(d1, d2); } writeln(min(ans1, ans2, ans3, ans4)); } 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, std.bitmanip; immutable int INF = 1 << 29; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto K = s[1]; auto X = (1L << K) - 1; auto A = readln.split.map!(to!long).array; long ans = 0; long tmp = 0; long[long] cnt; cnt[0] = 1; long f(long x) { if (x !in cnt) return 0; else return cnt[x]; } foreach (i; 0..N) { long a = tmp ^ A[i]; long b = a ^ X; if (f(a) <= f(b)) { ans += f(a); cnt[a] += 1; tmp = a; } else { ans += f(b); cnt[b] += 1; tmp = b; } } ans = N.to!long * (N.to!long + 1) / 2 - ans; ans.writeln; }
D