code
stringlengths
4
1.01M
language
stringclasses
2 values
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(string, "x", long, "y"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { auto c = lread!char(); writeln((c + 1).to!char); } 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.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 s = readln.chomp; auto res = int.max; foreach (i; 0..s.length-2) { res = min(res, abs(s[i..i+3].to!int - 753)); } res.writeln; }
D
import std.stdio; import std.algorithm; import std.string; import std.range; import std.array; import std.conv; import std.complex; import std.math; import std.ascii; import std.bigint; import std.container; 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..$]; } } const real eps = 1e-10; void main(){ long n, k; readlnTo(n, k); auto id = readLongs(); long m; while((m+1)*(m+2)/2 < k) { ++m; } auto l = k - m*(m+1)/2; writeln(id[(l-1).to!uint]); }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { while (true) { int x, y, s; scan(x, y, s); if (!x && !y && !s) return; solve(x, y, s); } } void solve(int x, int y, int s) { int ans; foreach (p ; 1 .. s) { int q = s - p; int pp = calckmax(p, x); int qq = calckmax(q, x); if (!pp || !qq) continue; int pz = calczei(pp, y); int qz = calczei(qq, y); ans = max(ans, pz + qz); } writeln(ans); } int calczei(int p, int x) { return p*(100+x)/100; } int calckmax(int p, int x) { int btm = 0, top = 2*p; while (top - btm > 1) { int mid = (btm + top) / 2; if (mid*(100+x)/100 <= p) { btm = mid; } else { top = mid; } } if (btm*(100+x)/100 == p) { return btm; } else { return 0; } } 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 chie template :) {{{ import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint; // }}} void main() { int n = readln.chomp.to!int; int[] c, s, f; foreach (i; 0 .. n - 1) { int[] t = readln.chomp.split.to!(int[]); c ~= t[0]; s ~= t[1]; f ~= t[2]; } foreach (i; 0 .. n - 1) { int res = c[i] + s[i]; foreach (j; (i + 1) .. n - 1) { res = max(res, s[j]) + (max(res, s[j]) % f[j] == 0 ? 0 : (f[j] - max(res, s[j]) % f[j])) + c[j]; } writeln(res); } writeln(0); }
D
import std.stdio, std.string, std.array, std.conv; void main() { while (true) { string[] tmp = readln.chomp.split; int a = tmp[0].to!int, b = tmp[2].to!int; string op = tmp[1]; if (op == "+") { writeln(a + b); } else if (op == "-") { writeln(a - b); } else if (op == "*") { writeln(a * b); } else if (op == "/") { writeln(a / b); } else { break; } } }
D
import std.stdio, std.ascii, std.string; void main() { readln.chomp.toUpper.writeln; }
D
import std.stdio; import std.algorithm; void main(){ string str = "Hello World"; for(int i=0;i<1000;i++){ writeln(str); } }
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; void main() { auto N = readln.chomp.to!int; bool[string] s; foreach (_; 0..N) { auto t = readln.chomp; s[t] = true; } s.keys.length.writeln; }
D
import std.stdio, std.conv, std.string; void main() { int N = to!(int)(readln().chomp()); string [] S = readln().chomp().split(); bool flg = false; foreach(i; S) { if(i=="Y") flg = true; } if(flg) writeln("Four"); else writeln("Three"); }
D
// import chie template :) {{{ static if (__VERSION__ < 2090) { import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons, std.format, std.bitmanip, std.numeric; } else { import std; } // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan(T...)(ref T args) { foreach (ref arg; args) { arg = next!(typeof(arg)); } } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } // }}} // alias {{{ alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less); alias MinHeap(T) = Heap!(T, "a > b"); // }}} // memo {{{ /* - ある値が見つかるかどうか <https://dlang.org/phobos/std_algorithm_searching.html#canFind> canFind(r, value); -> bool - 条件に一致するやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#filter> // 2で割り切れるやつ filter!"a % 2 == 0"(r); -> Range - 合計 <https://dlang.org/phobos/std_algorithm_iteration.html#sum> sum(r); - 累積和 <https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold> // 今の要素に前の要素を足すタイプの一般的な累積和 // 累積和のrangeが帰ってくる(破壊的変更は行われない) cumulativeFold!"a + b"(r, 0); -> Range - rangeをarrayにしたいとき array(r); -> Array - 各要素に同じ処理をする <https://dlang.org/phobos/std_algorithm_iteration.html#map> // 各要素を2乗 map!"a * a"(r) -> Range - ユニークなやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#uniq> uniq(r) -> Range - 順列を列挙する <https://dlang.org/phobos/std_algorithm_iteration.html#permutations> permutation(r) -> Range - ある値で埋める <https://dlang.org/phobos/std_algorithm_mutation.html#fill> fill(r, val); -> void - バイナリヒープ <https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap> // 昇順にするならこう(デフォは降順) BinaryHeap!(Array!T, "a > b") heap; heap.insert(val); heap.front; heap.removeFront(); - 浮動小数点の少数部の桁数設定 // 12桁 writefln("%.12f", val); - 浮動小数点の誤差を考慮した比較 <https://dlang.org/phobos/std_math.html#.approxEqual> approxEqual(1.0, 1.0099); -> true - 小数点切り上げ <https://dlang.org/phobos/std_math.html#.ceil> ceil(123.4); -> 124 - 小数点切り捨て <https://dlang.org/phobos/std_math.html#.floor> floor(123.4) -> 123 - 小数点四捨五入 <https://dlang.org/phobos/std_math.html#.round> round(4.5) -> 5 round(5.4) -> 5 */ // }}} void main() { auto cin = new Scanner; long x; cin.scan(x); long t = 100; int cnt; while (t < x) { t = cast(long)(cast(real)t * 1.01); cnt++; } writeln(cnt); }
D
import std.algorithm, std.string, std.array, std.range, std.stdio, std.conv; void main() { string n = readln.chomp; writeln("ABC", n); }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; auto s = readln.chomp.to!(char[]); if (s[0] != 'A') { writeln("WA"); return; } if (s.count('A') != 1 || s.count('C') != 1) { writeln("WA"); return; } foreach (c; s) { if (c != 'A' && c != 'C') { if ('A' <= c && c <= 'Z') { writeln("WA"); return; } } } if (s[2 .. ($ - 1)].count('C') != 1) { writeln("WA"); return; } writeln("AC"); } void rd(T...)(ref T x) { import std.stdio : readln; import std.string : split; import std.conv : to; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
D
import std.stdio, 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; void main() { auto N = readln.chomp.to!int; auto S = readln.chomp; auto Q = readln.chomp.to!int; auto K = readln.split.map!(to!int).array; foreach (k; K) { long d = 0; long m = 0; long dm = 0; long ans = 0; foreach (i; 0..k) { if (S[i] == 'D') { d += 1; } else if (S[i] == 'M') { m += 1; dm += d; } else if (S[i] == 'C') { ans += dm; } } foreach (i; k..N) { if (S[i-k] == 'D') { d -= 1; dm -= m; } else if (S[i-k] == 'M') { m -= 1; } if (S[i] == 'D') { d += 1; } else if (S[i] == 'M') { m += 1; dm += d; } else if (S[i] == 'C') { ans += dm; } } ans.writeln; } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; } double norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); } double dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[][](t); foreach (ti; 0..t) { auto x = RD; auto y = RD; if ((x+y) % 2) ans[ti] = [-1, -1]; else { auto a = min(x, (x+y)/2); ans[ti] = [a, (x+y)/2 - a]; } } foreach (e; ans) writeln(e[0], " ", e[1]); stdout.flush; debug readln; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); auto honest = new ulong[](N); auto unkind = new ulong[](N); foreach (i; 0 .. N) { long A = lread(); foreach (_; 0 .. A) { long x, y; scan(x, y); if (y) { honest[i] |= 1 << (x - 1); } else { unkind[i] |= 1 << (x - 1); } } } bool isValid(ulong mh) { foreach (i; 0 .. N) if ((mh & (1 << i)) != 0) { if ((unkind[i] & mh) != 0) return false; if ((honest[i] & (~mh)) != 0) return false; } return true; } long ans; foreach (mh; 0 .. (1 << N)) if (isValid(mh)) { ans = ans.max(popcnt(mh)); } writeln(ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp; auto K = readln.chomp.to!int; auto DP = new long[][][](2, K+1, N.length); DP[1][K-1][0] += 1; foreach (x; 1..N[0]-'0') DP[0][K-1][0] += 1; foreach (i; 1..N.length) { if (N[i] == '0') { foreach (k; 0..K+1) DP[1][k][i] += DP[1][k][i-1]; } else { foreach (k; 1..K+1) DP[1][k-1][i] += DP[1][k][i-1]; foreach (k; 0..K+1) DP[0][k][i] += DP[1][k][i-1]; } foreach (k; 1..K+1) DP[0][k-1][i] += DP[1][k][i-1] * max(0, (N[i]-'0').to!long-1); foreach (k; 1..K+1) DP[0][k-1][i] += DP[0][k][i-1] * 9; foreach (k; 0..K+1) DP[0][k][i] += DP[0][k][i-1]; DP[0][K-1][i] += 9; } long r; foreach (x; 0..2) r += DP[x][0][N.length-1]; writeln(r); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; Tuple!(N, N)[] prime_division(N)(N n) { Tuple!(N, N)[] res; foreach (N i; 2..10^^6+1) { if (n%i == 0) { N cnt; while (n%i == 0) { ++cnt; n /= i; } res ~= tuple(i, cnt); } } if (n != cast(N)1) res ~= tuple(n, cast(N)1); return res; } void main() { auto N = readln.chomp.to!long; auto ps = prime_division(N); int r; foreach (p; ps) { auto k = p[1]; foreach (i; 1..40) { if (k < i) break; ++r; k -= i; } } writeln(r); }
D
void main() { string s = readln.chomp; int price = 700; foreach (x; s) { if (x == 'o') price += 100; } price.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
// This file is a "Hello, world!" in D language by DMD for wandbox. import std.algorithm, std.conv, std.stdio, std.array; int main() { const I = readln.split.map!(to!long).array; ((I[2]/I[0])*I[1]).writeln; return 0; } // DMD reference: // https://dlang.org/dmd-linux.html // D language references: // https://dlang.org // http://www.kmonos.net/alang/d/ ( Japanese )
D
void main() { problem(); } void problem() { const X = scan!int; const N = scan!int; const P = scan!int(N); int solve() { int absolute = 101; int answer = 101; auto NP = iota(0, 102, 1).filter!(a => !P.canFind(a)); foreach(p; NP) { const a = X > p ? X - p : p - X; if (a == absolute && answer > p) { answer = p; continue; } if (a < absolute) { answer = p; absolute = a; continue; } } return answer; } 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"); // -----------------------------------------------
D
import std.stdio, std.string, std.algorithm, std.conv; const long INF = long.max/3; void main() { while(solve()){} } bool solve() { int N = readln.chomp.to!int; if (N == 0) return false; long[] as = new long[N]; foreach(i; 0..N) { as[i] = readln.chomp.to!long; } long res = -INF; long s = 0; foreach(i; 0..N) { s = max(as[i], s + as[i]); res = max(res, s); } res.writeln; return true; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string; immutable long MOD = 10^^9 + 7; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto W = new long[](N); auto V = new int[](N); foreach (i; 0..N) { s = readln.split.map!(to!int); W[i] = s[0]; V[i] = s[1]; } const int MAX = 10^^5 + 1; auto dp = new long[][](N+1, MAX); foreach (i; 0..N+1) fill(dp[i], (1L<<59)); dp[0][0] = 0; foreach (i; 0..N) { dp[i+1] = dp[i].dup; foreach (j; 0..MAX) { if (j + V[i] >= MAX) break; dp[i+1][j+V[i]] = min(dp[i+1][j+V[i]], dp[i][j] + W[i]); } } int ans = 0; foreach (j; 0..MAX) if (dp[N][j] <= M) ans = j; ans.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() { int N, Q; scan(N, Q); auto A = aryread(); auto ft = fenwick_tree!long(N); foreach (int i, a; A) { ft.add(i, a); } foreach (_; 0 .. Q) { int t, u, v; scan(t, u, v); if (t == 0) { ft.add(u, v); } else { writeln(ft.sum(u, v)); } } } struct fenwick_tree(T) { import std.traits : isSigned, Unsigned; static if (isSigned!T) { alias U = Unsigned!T; } else { alias U = T; } public: this(int n) { _n = n; data = new U[](n); } void add(int p, T x) { assert(0 <= p && p < _n); p++; while (p <= _n) { data[p - 1] += cast(U) x; p += p & -p; } } T sum(int l, int r) { assert(0 <= l && l <= r && r <= _n); return sum(r) - sum(l); } private: int _n; U[] data; U sum(int r) { U s = 0; while (r > 0) { s += data[r - 1]; r -= r & -r; } return s; } }
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 as = readln.split.to!(long[]); long[] ss; foreach (i; 0..N) { long s; foreach (j; i..N) { s += as[j]; ss ~= s; } } long r; foreach_reverse (i; 0..42) { auto x = 1L<<i; long[] nss; foreach (s; ss) if (s & x) { nss ~= s; } if (nss.length >= K) { ss = nss; r |= x; } } writeln(r); }
D
import std.stdio, std.string, std.conv, std.algorithm, std.range; void main() { int n = readln.chomp.to!int; int[] a = new int[n]; foreach (i; 0 .. n) { a[i] = readln.chomp.to!int; } int[] b = a.dup.sort!("a > b").array; foreach (x; a) { if (x != b[0]) b[0].writeln; else b[1].writeln; } }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { auto a = readln.split.to!(int[]); if (a[2] > a[0] + a[1]) { writeln("dangerous"); } else if (a[2] > a[0]) { writeln("safe"); } else { writeln("delicious"); } }
D
import std.stdio, std.string, std.conv; import std.array, std.algorithm, std.range; void main() { foreach(_;0..readln().chomp().to!int()) { auto m = readln().split().map!(to!real); immutable x1=m[0], y1=m[1], x2=m[2], y2=m[3], x3=m[4], y3=m[5], x4=m[6], y4=m[7]; writeln((x1-x2)*(y3-y4)==(x3-x4)*(y1-y2)?"YES":"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, d; scan(n, d); auto x = new int[][](n, d); foreach (i ; 0 .. n) { x[i] = readln.split.to!(int[]); } int ans; foreach (i ; 0 .. n) { foreach (j ; i + 1 .. n) { ans += isIntDist(d, x[i], x[j]); } } writeln(ans); } bool isIntDist(int d, int[] x, int[] y) { int dist; foreach (j ; 0 .. d) { dist += (x[j] - y[j])^^2; } auto q = sqrt(dist.to!real).to!int; return q^^2 == dist; } 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
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; import std.datetime, std.bigint; immutable inf = 2L * 10L^^16; int n; long[] a; void main() { scan(n); a = readln.split.to!(long[]); bool check(long k) { auto b = a.dup; b[] += k; long cnt; foreach (i ; 0 .. n) { if (b[i] - (n - 1) <= 0) continue; cnt += (b[i] - (n - 1) + n) / (n + 1); if (cnt > k) { return false; } } return true; } auto ans = new long[](n); long btm, top; foreach (i ; 0 .. n) { btm = -1; top = inf; while (top - btm > 1) { long mid = btm + (top - btm) / 2; if (check(n * mid + i)) { top = mid; } else { btm = mid; } } ans[i] = n * top + i; } debug { writeln(ans); } writeln(ans.reduce!min); } 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, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } /* (B - A) < 2 の場合は、換金しないでひたすら叩くだけ ans = 1 + K そうでないの場合は、換金できるなら換金した方がいい 最初にA枚まで貯める それに(A-1)ターンかかる Aターン目から換金できる。残りは K - A + 1 ターン 換金→購入で2ターン使うので ((K - A + 1) / 2)(B-A) 個になる。 K - A + 1 が奇数の場合は最初に1ターン余るのでそこで1枚 */ void main(){ long k = read.to!long; long a = read.to!long; long b = read.to!long; long ans; if(b - a < 2){ ans = 1 + k; } else{ ans = a + ((k - a + 1) / 2 ) * (b - a); if((k - a + 1) % 2 == 1) ans += 1; } ans.writeln; }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto s = readln.chomp; auto d = s.length - 7; auto f = false; foreach (i; 0..s.length - d) { if (s[0..i] ~ s[i+d..$] == "keyence") { f = true; break; } } if (f) { writeln("YES"); } else { writeln("NO"); } }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { long N; scan(N); auto a = iota(5).map!(i => readln.chomp.to!long).array; auto am = a.reduce!min; auto ans = (N + am - 1) / am + 4; writeln(ans); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
void main() { long n, c; rdVals(n, c); long lim = 2 * 10L^^5 + 1; long[][] list = new long[][](c, lim); foreach (i; 0 .. n) { long s, t, d; rdVals(s, t, d); --d; ++list[d][(s<<1)-1], --list[d][t<<1]; } long[] cnt = new long[lim]; foreach (i; 0 .. c) { foreach (j; 1 .. lim) { list[i][j] += list[i][j-1]; if (list[i][j]) ++cnt[j]; } } cnt.reduce!max.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
import std.stdio, std.string, std.array, std.conv, std.algorithm.iteration, std.functional; void main() { auto abcd = readln.split.to!(long[]); auto ab = abcd[0] * abcd[1]; auto cd = abcd[2] * abcd[3]; writeln( ab > cd ? ab : cd ); }
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 nk = readln.split.to!(long[]); long n = nk[0]; long k = nk[1]; long[] X = readln.split.to!(long[]); long result = long.max; foreach (i; 0 .. n - k + 1) { long a = (X[i + k - 1] - X[i]); long b = min(abs(X[i + k - 1]), abs(X[i])); result = result.min(a + b); } result.writeln; }
D
import std.stdio, std.range, std.conv, std.string; import std.algorithm.comparison, std.algorithm.iteration, std.algorithm.mutation, std.algorithm.searching, std.algorithm.setops, std.algorithm.sorting; void main() { readln(); const auto targets = readln().strip.split.to!(long[]); long[] list; list.length = targets.length; int count; foreach(i; 0..targets.length) { while(true) { auto range = list[i..$].commonPrefix!"a!=b"(targets[i..$]); if(range == []) break; range[] += 1; count++; } } writeln(count); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; import std.math; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}} void main() { int n; readV(n); int[] t, x, y; readM(n, t, x, y); if ((x[0]+y[0])%2 != t[0]%2 || x[0]+y[0] > t[0]) { writeln("No"); return; } foreach (i; 1..n) if ((x[i]+y[i])%2 != t[i]%2 || (x[i]-x[i-1]).abs+(y[i]-y[i-1]).abs > t[i]-t[i-1]) { writeln("No"); return; } writeln("Yes"); }
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[]); int ans; foreach (ai ; a) { while (ai % 2 == 0) { ans++; ai /= 2; } } writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.string, std.conv; import std.typecons; import std.algorithm, std.array, std.range, std.container; import std.math; void main() { readln; auto S = readln.split[0]; writeln( S.count('R') > S.count('B') ? "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; void main() { auto ans = new int[](4001); auto tmp = new int[](2001); foreach (i; 0..1001) { foreach (j; 0..1001) { tmp[i+j]++; } } foreach (i; 0..2001) { foreach (j; 0..2001) { ans[i+j] += tmp[i] * tmp[j]; } } foreach (line; stdin.byLine) { ans[line.chomp.to!int].writeln; } }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { int x, t; scan(x, t); writeln(max(x - t, 0)); } 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.algorithm, std.conv, std.range, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}} void main() { int a, b, c; readV(a, b, c); writeln(a+b >= c ? "Yes" : "No"); }
D
void main(){ long n = _scan!long(); long[] a = _scanln!long(); // 要素に0がある場合 if( a.count(0) != 0 ){ writeln(0); return; } // 桁あふれが起きないかチェックしならが積をとる long mula = 1; foreach(elm; a){ if( mula <= mula*elm && mula <= 10L^^18/elm ) mula *= elm; else { writeln(-1); return; } } mula.writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math; void main() { auto xab = readln.split.to!(int[]); writeln(abs(xab[0] - xab[1]) > abs(xab[0] - xab[2]) ? "B" : "A"); }
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; } T read(T)(){ return read.to!T; } T[] read(T)(long n){ return n.iota.map!(_ => read!T).array; } T[][] read(T)(long m, long n){ return m.iota.map!(_ => read!T(n)).array; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ int n = read.to!int; char[] a = readln.chomp.to!(char[]); char[char] y; foreach(c; "123456789") y[c] = '0', y[c] += read.to!int.to!char; int f = 0; foreach(i, c; a){ if(f == 0){ if(y[c] <= c) continue; else{ f = 1; a[i] = y[c]; } } else if(f == 1){ if(y[c] < c){ f = 2; break; } else{ a[i] = y[c]; } } } a.to!string.writeln; }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.ascii; void main() { auto a = readln.chomp.to!int; auto b = readln.chomp.to!int; if ((a == 1 && b == 2) || (a == 2 && b == 1)) { writeln(3); } else if ((a == 1 && b == 3) || (a == 3 && b == 1)) { writeln(2); } else if ((a == 2 && b == 3) || (a == 3 && b == 2)) { writeln(1); } }
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 s = readln.chomp.to!int; auto u2 = s / 100; auto d2 = s % 100; bool uf, df; if (u2 >= 1 && u2 <= 12) { uf = true; } if (d2 >= 1 && d2 <= 12) { df = true; } if (uf && df) { writeln("AMBIGUOUS"); } else if (uf) { writeln("MMYY"); } else if (df) { writeln("YYMM"); } else { writeln("NA"); } }
D
import std.stdio; import std.array; import std.conv; void main() { string[] input = split(readln()); int a = to!(int)(input[0]); int b = to!(int)(input[1]); writeln(a*b, ' ', a * 2 + b * 2); }
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!(long[]); auto N = nm[0]; auto M = nm[1]; writeln(N == 1 && M == 1 ? 1 : N == 1 ? M-2 : M == 1 ? N-2 : (N-2) * (M-2)); }
D
import std.functional, std.algorithm, std.container, std.typetuple, std.typecons, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv; void main() { auto ip = readln.split.to!(int[]), w=ip[0], h=ip[1], x=ip[2], y=ip[3], r=ip[4]; if(x >= r && x <= w - r && y >= r && y <= h - r){ writeln("Yes"); } else { writeln("No"); } }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; import std.numeric; 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, sx; readV(n, sx); int[] x; readA(n, x); auto y = x.map!(xi => (xi-sx).abs); auto g = y[0]; foreach (i; 1..n) g = gcd(g, y[i]); writeln(g); }
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 N = RD!string; writeln(N[1] == N[2] && (N[0] == N[1] || N[2] == N[3]) ? "Yes" : "No"); stdout.flush(); debug readln(); }
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() { while (true) { int n, x; scan(n, x); if (n == 0 && x == 0) return; auto ans = solve(n, x); writeln(ans); } } int solve(int n, int x) { int ans; foreach (i ; 1 .. n + 1) { foreach (j ; i + 1 .. n + 1) { foreach (k ; j + 1 .. n + 1) { if (i + j + k == x) ans++; } } } return ans; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum long mod = 10^^9 + 7; void main() { int h, w, k; scan(h, w, k); k--; if (w == 1) { writeln(1); return; } auto m = new long[][](w, w); foreach (s ; 0 .. 1<<(w-1)) { auto a = iota(w).array; if (isCont(s)) { continue; } foreach (i ; 0 .. w - 1) { if (s & (1 << i)) { swap(a[i], a[i+1]); } } debug { writeln(a); } foreach (i ; 0 .. w) { foreach (j ; 0 .. w) { if (a[j] == i) { m[i][j]++; } } } } debug { writefln("%(%(%s %)\n%)", m); } auto dp = new long[][](h + 1, w); dp[0][0] = 1; foreach (i ; 0 .. h) { foreach (j ; 0 .. w) { foreach (l ; 0 .. w) { dp[i+1][l] += m[j][l] * dp[i][j] % mod; dp[i+1][l] %= mod; } } } debug { writeln("dp"); writefln("%(%(%s %)\n%)", dp); } long ans = dp[h][k]; writeln(ans); } bool isCont(int x) { int p = 0; while (x > 0) { if (p == 0) { p = x & 1; x >>= 1; } else { if (x & 1) { return true; } p = 0; x >>= 1; } } return false; } unittest { //writeln(0b101); assert(isCont(0b101) == false); assert(isCont(0b0) == false); assert(isCont(0b011) == 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.container; import std.random; void main() { auto x = readln.chomp.split.map!(to!int); int cnt; foreach (i; x[0]..x[1]+1) if (x[2] % i == 0) cnt++; cnt.writeln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio; void solve() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1].to!long; auto A = readln.split.map!(to!long).array; long mx = -1; int mxi = -1; long sm = 0; bool erased = false; int skipped = -1; foreach (i, a; A) { if (sm + a <= M) { sm += a; if (a >= mx) { mx = a; mxi = i.to!int; } } else if (!erased) { if (a >= mx) { mx = a; mxi = i.to!int; } if (sm + a - mx <= M) { sm += a - mx; erased = true; skipped = mxi; } else { break; } } else { break; } } writeln(skipped + 1); } void main() { auto T = readln.chomp.to!int; while (T--) solve; }
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 n = read.to!int; int[] ps; foreach(i; 0 .. n) ps ~= read.to!int; int cnt; foreach(i, p; ps) if(i + 1 != p) cnt += 1; if(cnt <= 2) "YES".writeln; else "NO".writeln; }
D
void main() { problem(); } void problem() { auto S = scan; string solve() { return S[2] == S[3] && S[4] == S[5] ? "Yes" : "No"; } solve().writeln; } // ---------------------------------------------- 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, 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"; 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; } 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 x = RD; long ans = x / 11 * 2; if (x % 11 != 0) ans += x % 11 <= 6 ? 1 : 2; writeln(ans); stdout.flush(); //readln(); }
D
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array, std.functional; void main() { auto N = readln.split[0].to!int; auto S = readln.chomp; auto dp = new int[][N]; foreach(ref d; dp) { d = new int[N]; } for(int i = N - 2; i >= 0; i--) { for(int j = N - 1; j > i; j--) { if(S[i] != S[j]) { dp[i][j] = 0; } else if(j == N - 1) { dp[i][j] = 1; } else { dp[i][j] = 1 + dp[i+1][j+1]; } } } int m = 0; foreach(i; 0..N) { foreach(j; (1+i)..N) { m = max(m, min(j-i, dp[i][j])); } } m.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; // dfmt on void main() { auto S = sread(); auto T = sread(); bool eq(string s, string t) { foreach (i; 0 .. s.length) { if (s[i] == '?' || t[i] == '?' || s[i] == t[i]) continue; return false; } return true; } foreach_reverse (i; 0 .. S.length - T.length + 1) { dprint(S[i .. i + T.length], T); if (eq(S[i .. i + T.length], T)) { char[] U = new char[](S.length); foreach (j; 0 .. S.length) { if (i <= j && j < i + T.length) { U[j] = T[j - i]; continue; } if (S[j] == '?') { U[j] = 'a'; continue; } U[j] = S[j]; } writeln(U); return; } } writeln("UNRESTORABLE"); }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; void times(alias pred)(int n) { foreach(i; 0..n) pred(); } auto rep(alias pred, T = typeof(pred()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = pred(); return res; } void main() { string str = readln.chomp; long ans = 0; foreach(i; 0..2^^(str.length-1)) { size_t k = 0; foreach(j; 0..str.length-1) { if (i>>j&1) { ans += str[k..j+1].to!long; k = j+1; } } ans += str[k..$].to!long; } ans.writeln; }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional; import std.algorithm, std.container; struct Node{ long index; long[] to; long[] cost; } void main() { auto N = scanElem; Node[] nodes; nodes.length = N+1; foreach(i;0..N-1) { long a = scanElem; long b = scanElem; long c = scanElem; nodes[a].index = a; nodes[a].to ~= b; nodes[a].cost ~= c; nodes[b].index = b; nodes[b].to ~= a; nodes[b].cost ~= c; } long[] costs; costs.length = N+1; costs[] = -1; auto Q = scanElem; auto K = scanElem; long[] queue = [K]; costs[K] = 0; while(!queue.empty) { auto node = nodes[queue[0]]; queue = queue[1..$]; foreach(i; 0..node.to.length) { if(costs[node.to[i]]!=-1)continue; queue ~= node.to[i]; costs[node.to[i]] = costs[node.index] + node.cost[i]; } } foreach(i;0..Q) { long x = scanElem; long y = scanElem; writeln(costs[x]+costs[y]); } } class UnionFind{ UnionFind parent = null; void merge(UnionFind a) { if(same(a)) return; a.root.parent = this.root; } UnionFind root() { if(parent is null)return this; return parent = parent.root; } bool same(UnionFind a) { return this.root == a.root; } } void scanValues(TList...)(ref TList list) { auto lit = readln.splitter; foreach (ref e; list) { e = lit.fornt.to!(typeof(e)); lit.popFront; } } T[] scanArray(T = long)() { return readln.split.to!(long[]); } void scanStructs(T)(ref T[] t, size_t n) { t.length = n; foreach (ref e; t) { auto line = readln.split; foreach (i, ref v; e.tupleof) { v = line[i].to!(typeof(v)); } } } T scanElem(T = long)() { char[] res; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { res ~= cast(char) c; c = getchar; } return res.strip.to!T; } 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 { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } 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); } } struct Factor { long n; long c; } Factor[] factors(long n) { Factor[] res; for (long i = 2; i ^^ 2 <= n; i++) { if (n % i != 0) continue; int c; while (n % i == 0) { n = n / i; c++; } res ~= Factor(i, c); } if (n != 1) res ~= Factor(n, 1); return res; } long[] primes(long n) { if(n<2)return []; auto table = new long[n+1]; long[] res; for(int i = 2;i<=n;i++) { if(table[i]==-1) continue; for(int a = i;a<table.length;a+=i) { table[a] = -1; } res ~= i; } return res; } bool isPrime(long n) { if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (long i = 3; i ^^ 2 <= n; i += 2) if (n % i == 0) return false; return true; }
D
void main() { int n = readln.chomp.to!int; string s = readln.chomp; int k = readln.chomp.to!int - 1; foreach (x; s) { if (x == s[k]) x.write; else '*'.write; } writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio; import std.conv; import std.string; void main(){ long n=to!int(readln.chomp),a=1; while(n--)a*=n+1; writeln(a); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}} void main() { int x; readV(x); int a; readV(a); int b; readV(b); writeln((x-a)%b); }
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 n; readV(n); int k; readV(k); int x; readV(x); int y; readV(y); writeln(n <= k ? n*x : k*x+(n-k)*y); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum mod = 10L^^9 + 7; void main() { int n, a, b, c, d; scan(n, a, b, c, d); auto fact = new long[](n + 1); auto rfact = new long[](n + 1); fact[0] = 1; foreach (i ; 1 .. n + 1) { fact[i] = (fact[i-1] * i) % mod; } rfact[n] = inv(fact[n]); foreach_reverse (i ; 0 .. n) { rfact[i] = (rfact[i+1] * (i + 1)) % mod; } long perm(int n, int k) { return fact[n] * rfact[n-k] % mod; } debug { //writeln(fact); //writeln(rfact); } auto dp = new long[][](n + 1, n + 1); foreach (i ; 0 .. n/a + 1) { if (i != 0 && (i < c || i > d)) { continue; } dp[a][i*a] = perm(n, i*a) * powmod(rfact[a], i) % mod * rfact[i] % mod; } foreach (i ; a + 1 .. b + 1) { foreach (j ; 0 .. n + 1) { foreach (k ; 0 .. min(d, j/i) + 1) { if (k != 0 && (k < c || k > d)) { continue; } (dp[i][j] += dp[i-1][j - i*k] * perm(n - j + i*k, i*k) % mod * powmod(rfact[i], k) % mod * rfact[k] % mod) %= mod; } } } debug { writefln("%(%(%s %)\n%)", dp); } long ans = dp[b][n]; writeln(ans); } long powmod(long x, long y) { return y > 0 ? powmod(x, y>>1)^^2 % mod * x^^(y&1) % mod : 1; } long inv(long x) { return powmod(x, mod - 2); } 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.algorithm, std.conv, std.array, std.string, std.math; long check(long op, long sum, long[] as) { foreach (a; as) { if (sum < 0) { if ((sum + a) <= 0) { op += (1 - (sum + a)); sum = 1; } else { sum += a; } } else { if ((sum + a) >= 0) { op += sum + a + 1; sum = -1; } else { sum += a; } } } return op; } void main() { readln; auto as = readln.chomp.split(" ").map!(to!long).array; auto op1 = check(0, as[0], as[1..$]); auto op2 = check((as[0] - 1).abs, 1, as[1..$]); auto op3 = check((as[0] + 1).abs, -1, as[1..$]); if (as[0] == 0) op1 = long.max; writeln(min(op1, op2, op3)); }
D
import std.stdio, std.range, std.conv, std.string, std.array, std.functional, std.math; import std.algorithm.comparison, std.algorithm.iteration, std.algorithm.mutation, std.algorithm.searching, std.algorithm.setops, std.algorithm.sorting; import std.container.binaryheap; import std.typecons; struct S{ long all; long patti; } S[51] memo; void main() { const input = readln().strip.split.to!(long[]); const N = input[0]; const X = input[1]; memo[0] = S(1, 1); foreach(i; 1..51) { memo[i] = S(memo[i-1].all*2+3, memo[i-1].patti*2+1); } writeln(func(N, X)); } long func(long n, long x) { if(n==0) return x<=0?0:1; else if(x <= 1+memo[n-1].all) return func(n-1, x-1); else return memo[n-1].patti+1+func(n-1, x-2-memo[n-1].all); }
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 s = sread(); if (s[0] == 'R' && s[1] == 'R' && s[2] == 'R') { writeln(3); return; } if ((s[0] == 'R' && s[1] == 'R') || (s[1] == 'R' && s[2] == 'R')) { writeln(2); return; } else if ((s[0] == 'R') || (s[1] == 'R') || (s[2] == 'R')) { writeln(1); return; } writeln(0); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.algorithm; import std.conv; import std.numeric; import std.range; import std.stdio; import std.string; 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; auto lo = a.filter !(x => (x & 1) == 0).array; auto hi = a.filter !(x => (x & 1) == 1).array; auto b = lo ~ hi; int res = 0; foreach (i; 0..n) { foreach (j; i + 1..n) { if (gcd (b[i], b[j] * 2) > 1) { res += 1; } } } writeln (res); } }
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 A, B, K; scanln(A, B, K); long cnt = 0; foreach_reverse(i; 1..min(A, B)+1) { if (A%i==0 && B%i==0) { cnt++; if (cnt == K) { i.writeln; return; } } } } // ---------------------------------------------- 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.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } void main(){ long a = read.to!long; long b = read.to!long; if(a + b == 15) writeln("+"); else if(a * b == 15) writeln("*"); else writeln("x"); }
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 a, b; scan(a, b); writeln((a-1)*(b-1)); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto X = RD; auto ans = X / 500 * 1000; X %= 500; ans += X / 5 * 5; writeln(ans); stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto D = RD; auto c = RDA; auto s = new long[][](D); foreach (i; 0..D) s[i] = RDA; foreach (i; 0..D) { writeln(s[i].MIN_POS!"a > b" + 1); } /*auto last = new int[](26); long ans; foreach (i; 0..D) { long dec; foreach (j; 0..26) { dec += c[j] * (i - last[j]); } }*/ stdout.flush; debug readln; }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; void main() { string[] inputs = split(readln()); int A = to!int(inputs[0]); char op = inputs[1][0]; int B = to!int(inputs[2]); if(op == '+') (A + B).writeln; else (A - B).writeln; }
D
import std.stdio, std.string, std.conv, std.range, std.algorithm, std.uni; void main(){ string i = readln.chomp; string s; foreach(c;i){ if (isLower(c)) s ~= toUpper(c); else s ~= toLower(c); } writeln(s); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto n = RD!int; auto a = new long[](n); auto b = new long[](n); auto c = new long[](n); foreach (i; 0..n) { a[i] = RD; b[i] = RD; c[i] = RD; } auto edges = new int[][](n); foreach (i; 0..n-1) { auto u = RD!int-1; auto v = RD!int-1; edges[u] ~= v; edges[v] ~= u; } auto d = new long[](n); auto cnt = new long[](n); auto par = new int[](n); long[] dfs(int pos, int last) { par[pos] = last; d[pos] = c[pos] - b[pos]; cnt[pos] = b[pos] == c[pos] ? 0 : 1; foreach (to; edges[pos]) { if (to == last) continue; auto r = dfs(to, pos); d[pos] += r[0]; cnt[pos] += r[1]; } return [d[pos], cnt[pos]]; } dfs(0, -1); if (d[0] != 0) writeln(-1); else { long ans = a[0] * cnt[0]; void dfs2(int pos, int last, long cost) { if (a[pos] < cost) { auto x = cnt[pos] - abs(d[pos]); ans -= cost * x; ans += a[pos] * x; cost = a[pos]; } foreach (to; edges[pos]) { if (to == last) continue; dfs2(to, pos, cost); } } dfs2(0, -1, a[0]); writeln(ans); } stdout.flush; debug readln; }
D
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; import std.datetime, std.bigint; void main() { readln(); auto s = readln.split(); int ans = 0; int vol(string w) { int res; debug { writeln(w); } foreach (ch ; w) { if (ch <= 'Z') res++; } return res; } foreach (w ; s) { ans = max(ans, vol(w)); } writeln(ans); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.range; import std.array; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.container; import std.typecons; import std.random; import std.csv; import std.regex; import std.math; import core.time; import std.ascii; import std.digest.sha; import std.outbuffer; void main() { auto args = readln.chomp.split.map!(to!int).array; int n = args[0]; int A = args[1]; int B = args[2]; int C = args[3]; int T = args[4]; int[] t = readln.chomp.split.map!(to!int).array; int[][][] dp = new int[][][](T + 2, T + 2, 2); int[int] dic; for (int i = 1; i <= T; ++i) { dic[i] = 0; } foreach (v; t) { ++dic[v]; } debug stderr.writeln(dic); for (int i = 1; i <= T; ++i) { for (int j = 1; j <= i; ++j) { debug stderr.writeln(tuple(i, j, 1)); { dp[i + 1][j][1] = dp[i][j][1] + dic[j] * C; debug stderr.writefln("\tdp[%d][%d][1] = %d (%d + %d * %d)", i + 1, j, dp[i + 1][j][0], dp[i][j][1], dic[j], C); } { int pena = A - (i - j) * B; dp[i + 1][j][0] = max(dp[i][j][0], dp[i][j][1] + dic[j] * pena); debug stderr.writefln("\tdp[%d][%d][0] = %d (max(%d, %d + %d * %d)", i + 1, j, dp[i + 1][j][0], dp[i][j][0], dp[i][j][1], dic[j], pena); } } } int sum = 0; for (int i = 1; i <= T; ++i) { debug stderr.writefln("\t(%d) : %d", i, dp[T + 1][i][0]); sum += dp[T + 1][i][0]; } sum.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 = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto t = RD!int; auto ans = new bool[](t); foreach (i; 0..t) { auto n = RD!int; auto m = RD; auto k = RD; auto h = RDA; bool tmp = true; foreach (j; 1..n) { auto d = k - (h[j] - h[j-1]); m += min(d, h[j-1]); if (m < 0) { tmp = false; break; } } ans[i] = tmp; } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void main() { auto T = RD!int; auto ans = new string[](T); foreach (ti; 0..T) { auto s = RD!string; auto edges = new bool[][](26, 26); if (s.length == 2) { auto l = s[0]-'a'; auto r = s[1]-'a'; edges[l][r] = true; edges[r][l] = true; } foreach (i; 1..s.length-1) { auto m = s[i]-'a'; auto l = s[i-1]-'a'; auto r = s[i+1]-'a'; edges[m][l] = true; edges[m][r] = true; edges[l][m] = true; edges[r][m] = true; } bool ok = true; int[] list; foreach (i, e; edges) { int cnt; foreach (ee; e) { if (ee) ++cnt; } if (cnt >= 3) { ok = false; break; } else if (cnt == 1) { list ~= cast(int)i; } else if (cnt == 0) { ans[ti] ~= cast(char)(i+'a'); } } debug writeln(ans[ti]); if (!ok) { ans[ti] = []; continue; } auto used = new int[](26); void dfs(int i, int par) { used[i] = true; ans[ti] ~= cast(char)(i+'a'); foreach (j; 0..26) { if (j == par) continue; if (edges[i][j]) { dfs(j, i); } } } foreach (e; list) { if (used[e] == false) dfs(e, -1); } debug writeln(ans[ti]); if (ans[ti].length != 26) ans[ti] = []; } foreach (e; ans) { if (e.empty) writeln("NO"); else { writeln("YES"); writeln(e); } } stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.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 x = RD!int; auto a0 = x%4; auto a1 = (x+1)%4; auto a2 = (x+2)%4; long ans; if (a0 == 1 || a1 == 1 || a2 == 1) writeln(a0 == 1 ? "0" : a1 == 1 ? "1" : "2", " A"); else if (a0 == 3 || a1 == 3 || a2 == 3) writeln(a0 == 3 ? "0" : a1 == 3 ? "1" : "2", " B"); else if (a0 == 2 || a1 == 2 || a2 == 2) writeln(a0 == 2 ? "0" : a1 == 2 ? "1" : "2", " C"); else writeln(a0 == 0 ? "0" : a1 == 0 ? "1" : "2", " D"); stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto x = RD; auto a = RDA; bool isOne; long y; foreach (i; 0..n) { if (a[i] == x) isOne = true; y.chmax(a[i]); } if (isOne) ans[ti] = 1; else ans[ti] = max(2, (x+y-1) / y); } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio; void main(string[ ] args) { int n; scanf("%d", &n); int n_0 = 0; int n_1 = 0; for (int i = 0; i < n; ++i) { int a; scanf("%d", &a); if (a % 2 == 0) { ++n_0; } else { ++n_1; } } //writeln(n_0); //writeln(n_1); int ans; if (n_0 >= n_1) { writeln(n_1); } else { writeln(n_0 + (n_1 - n_0) / 3); } }
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; void main() { auto N = readln.chomp.to!int; auto A = readln.chomp; auto B = readln.chomp; long ans = 0; for (int i = 0; i < N; ++i) { if (A[i] == B[i]) continue; if (i == N - 1) { ans += 1; } else if (A[i+1] != B[i+1] && A[i] != A[i+1]) { ans += 1; i += 1; } else { ans += 1; } } ans.writeln; }
D
import std.algorithm; import std.ascii; import std.stdio; import std.string; void main () { string s; while ((s = readln.strip) != "") { int res = 0; foreach (c; s) { if (c.isLower) { res -= c - 'a' + 1; } else if (c.isUpper) { res += c - 'A' + 1; } } writeln (res); } }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; bool solve (int n, int [] a) { auto m = 1 << n; for (int s = 0; s < m; s++) { int r = m - 1 - s; int t = r; do { int cur = 0; foreach (i; 0..n) { if (s & (1 << i)) { cur += a[i]; } if (t & (1 << i)) { cur -= a[i]; } } if (cur == 0 && !(s == 0 && t == 0)) { return true; } t = (t - 1) & r; } while (t != r); } return false; } 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) ? "YES" : "NO"); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto a = new long[](n); auto b = new long[](n); foreach (i; 0..n) { a[i] = RD; b[i] = RD; } auto tm = RDA; long time; foreach (i; 0..n) { long d; if (i == 0) d = a[i] + tm[i]; else d = a[i] - b[i-1] + tm[i]; time += d; debug writeln("time:", time); if (i == n-1) break; auto w = (b[i]-a[i]+1) / 2; time = max(time+w, b[i]); } ans[ti] = time; } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string; immutable long MOD = 10^^9 + 7; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto dp = new long[][][](2, N, N); dp[0][0][0] = 1; int cur = 0; int tar = 1; foreach (i; 0..M) { foreach (j; 0..N) { foreach (k; 0..N) { dp[tar][j][k] = 0; } } foreach (j; 0..N) { foreach (k; 0..N) { if (dp[cur][j][k] == 0) { continue; } if (j < N - 1) { (dp[tar][j+1][k] += dp[cur][j][k]) %= MOD; } (dp[tar][j][k] += dp[cur][j][k] * (j - k) % MOD) %= MOD; (dp[tar][j][j] += dp[cur][j][k] * (k + 1) % MOD) %= MOD; } } swap(cur, tar); } long ans = dp[cur][N-1][N-1]; foreach (i; 1..N) ans = ans * i % MOD; ans.writeln; }
D
import std.stdio, std.conv, std.array, std.string; import std.algorithm; import std.container; import std.range; import core.stdc.stdlib; import std.math; void main() { auto r = readln.chomp.to!int; writeln(r*r); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } bool calc(string s) { for (int i = 0; i < s.length - 1; i++) { if (s[i] == 'A' && s[i + 1] == 'C') { return true; } } return false; } void main() { auto s = readln.chomp; writeln(calc(s) ? "Yes" : "No"); }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional; import std.algorithm, std.container; void main() { long N = scanElem; primes(55555).filter!"a%5==1".take(N).each!writeln; } class UnionFind{ UnionFind parent = null; void merge(UnionFind a) { if(same(a)) return; a.root.parent = this.root; } UnionFind root() { if(parent is null)return this; return parent = parent.root; } bool same(UnionFind a) { return this.root == a.root; } } void scanValues(TList...)(ref TList list) { auto lit = readln.splitter; foreach (ref e; list) { e = lit.fornt.to!(typeof(e)); lit.popFront; } } T[] scanArray(T = long)() { return readln.split.to!(long[]); } void scanStructs(T)(ref T[] t, size_t n) { t.length = n; foreach (ref e; t) { auto line = readln.split; foreach (i, ref v; e.tupleof) { v = line[i].to!(typeof(v)); } } } T scanElem(T = long)() { char[] res; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { res ~= cast(char) c; c = getchar; } return res.strip.to!T; } 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 { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } struct Factor { long n; long c; } Factor[] factors(long n) { Factor[] res; for (long i = 2; i ^^ 2 <= n; i++) { if (n % i != 0) continue; int c; while (n % i == 0) { n = n / i; c++; } res ~= Factor(i, c); } if (n != 1) res ~= Factor(n, 1); return res; } long[] primes(long n) { if(n<2)return []; auto table = new long[n+1]; long[] res; for(int i = 2;i<=n;i++) { if(table[i]==-1) continue; for(int a = i;a<table.length;a+=i) { table[a] = -1; } res ~= i; } return res; } bool isPrime(long n) { if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (long i = 3; i ^^ 2 <= n; i += 2) if (n % i == 0) return false; return true; }
D
import std.stdio, std.conv, std.array; import std.algorithm; void main() { auto input = readln.split.to!(int[]); auto a = input[0]; auto b = input[1]; auto max = max(a+b, a-b, a*b); writeln(max); }
D
import std.stdio, std.conv, std.string; void main() { auto r = readln.chomp.to!int; writeln(3 * r * r); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math; void main() { auto A = readln.chomp.to!int; auto B = readln.chomp.to!int; auto C = readln.chomp.to!int; auto X = readln.chomp.to!int; int result; foreach (a; 0..A+1) { foreach (b; 0..B+1) { auto rest = X - 500*a - 100*b; if (rest >= 0 && rest % 50 == 0 && rest / 50 <= C) ++result; } } writeln(result); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto M = readln.split[1].to!long; long[long] HS = [0: 0]; long c; foreach (a; readln.split.to!(long[])) { c = (c + a) % M; if (c in HS) { ++HS[c]; } else { HS[c] = 0; } } long r; foreach (_, n; HS) { r += n * (n + 1) / 2; } writeln(r); }
D