code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio; import std.conv; import std.algorithm; import std.string; import std.array; void main() { int n = readln.chomp.to!int; int[] a = 0~readln.split.map!(to!int).array; int ans = 0; for(int i = 1; i <= a.length; i++) if(i%2 == 1 && a[i]%2 == 1) ans += 1; ans.writeln; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { auto ab = readints; int a = ab[0], b = ab[1]; int ans = 0; for (int i = 1; i <= 12; i++) { if (i > a) break; if (i == a && i > b) break; ans++; } writeln(ans); }
D
import std; long calc(long a, long b, long c, long k) { long ans = 0; long x = min(a, k); ans += x; k -= x; k -= min(b, k); long y = min(c, k); ans += -1 * y; return ans; } void main() { long a, b, c, k; scan(a, b, c, k); writeln(calc(a, b, c, k)); } void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T=string)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readints = reads!int;
D
unittest { assert( [ "keyofscience" ].parse.expand.solve == "YES" ); assert( [ "mpyszsbznf" ].parse.expand.solve == "NO" ); assert( [ "ashlfyha" ].parse.expand.solve == "NO" ); assert( [ "keyence" ].parse.expand.solve == "YES" ); assert( [ "keyencex" ].parse.expand.solve == "YES" ); // 追加ケース assert( [ "xkeyencex" ].parse.expand.solve == "NO" ); // 追加ケース assert( [ "keyxxxenxxxence" ].parse.expand.solve == "YES" ); // 追加ケース } import std.conv; import std.range; import std.stdio; import std.string; import std.typecons; void main() { stdin.byLineCopy.parse.expand.solve.writeln; } auto parse( Range )( Range input ) if( isInputRange!Range && is( ElementType!Range == string ) ) { auto s = input.front; return tuple( s ); } auto solve( string s ) { return match( s, "keyence", 0, false ) ? "YES" : "NO"; } bool match( string s, string k, long dc, bool ma ) { // 削除対象の部分文字列が2個以上になったら失敗 if( 1 < dc ) return false; // マッチングが完了 if( s.length <= 0 && k.length <= 0 ) return true; // マッチングの途中で入力文字列が足りなくなったら失敗 if( s.length < k.length ) return false; // マッチングの途中で入力文字列が余ったら、余った分は削除対象扱い if( k.length <= 0 ) return ( dc <= 0 ); // 先頭の文字をマッチング if( s[ 0 ] == k[ 0 ] ) { auto m1 = match( s[ 1 .. $ ], k[ 1 .. $ ], dc, false ); // マッチしたパターン auto m2 = match( s[ 1 .. $ ], k[ 0 .. $ ], dc + !ma, true ); // マッチを無視するパターン return ( m1 || m2 ); } else { auto m2 = match( s[ 1 .. $ ], k[ 0 .. $ ], dc + !ma, true ); // マッチしなかったパターン return m2; } }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } enum MOD = 10 ^^ 9 + 7; void main() { long N = lread(); long[] inp; foreach (_; 0 .. 5) inp ~= lread(); long tmp = inp.reduce!min(); writeln(4 + ((N + tmp - 1) / tmp)); }
D
void main() { long n = readln.chomp.to!long; string[] tmp = readln.split; string s = tmp[0], t = tmp[1]; foreach (i; 0 .. n) { write(s[i], t[i]); } 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.algorithm; import std.math; import std.conv; import std.string; int readInt(){ return readln.chomp.to!int; } int[] readInts(){ return readln.chomp.split.to!(int[]); } void main(){ int[] nk = readInts(); int ret = nk[0] - nk[1] + 1; writeln(ret); }
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; scan(n); auto h = readln.split.to!(int[]); auto dp = new long[](n + 1); dp[1] = abs(h[1] - h[0]); foreach (i ; 2 .. n) { dp[i] = min(abs(h[i] - h[i-1]) + dp[i-1], abs(h[i] - h[i-2]) + dp[i-2]); } writeln(dp[n-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 core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { long a, b, x; scan(a, b, x); writeln(f(b, x) - f(a - 1, x)); } long f(long n, long x) { if (n < 0) return 0; return n / x + 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 n = readln.chomp.to!int; auto a = readln.chomp.split.to!(int[]); int cnt; foreach (e; a) { while (e % 2 == 0) { e /= 2; cnt++; } } writeln(cnt); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; long calc(long n) { // n/m = n%m = k とおく // 商が k で、余りが k ということなので // n = mk + k // = k(m + 1) // と表せる。 // m は、n の約数から 1 を引いた数、というのが分かる。 // n の約数は sqrt(n) で列挙できる。 long ans = 0; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { // m の候補 foreach (m; [i - 1, n / i - 1]) { if (m > 0 && n / m == n % m) { ans += m; } } } } return ans; } void main() { long n = read!long; writeln(calc(n)); }
D
import std.stdio; import std.array; import std.algorithm; import std.string; import std.conv; void main(){ int[] pasta; int[] juice; for(int i = 0; i < 3; i++){ pasta ~= readln().chomp().to!int(); } for(int i = 0; i < 2; i++){ juice ~= readln().chomp().to!int(); } writeln(pasta.minPos()[0] + juice.minPos()[0] - 50); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; void main() { string[] inputs = split(readln()); int X = to!int(inputs[0]); int t = to!int(inputs[1]); max(0, X-t).writeln; }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; void times(alias 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() { writeln("A", readln.split[1].front, "C"); }
D
import std.stdio; import std.string; import std.conv; import std.array; import std.range; import std.algorithm; void main() { string input; while ((input = readln.chomp).length != 0) { int[] tehai = new int[](9); int[] yama = new int[](9); int[] ans; fill(yama, 4); foreach (i; 0..13) { int index = (input[i] - '0').to!int; tehai[index-1]++; yama[index-1]--; } ans = agari(tehai, yama); if (ans.length == 0) { writeln(0); } else { writeln(ans.map!(to!string).join(" ")); } } } /* * 何を自摸れば、上がれるかを判定する。 * 上がることのできる牌を見つけたら、それをすべて配列に格納して返す。 * 上がることができなければ、空の配列が返される。 */ int[] agari(int[] tehai, int[] yama) { int[] ans; foreach (i; 0..tehai.length) { if (yama[i] == 0) continue; tehai[i]++; foreach (j; 0..tehai.length) { if (tehai[j] < 2) continue; tehai[j] -= 2; if (yaku(tehai, 0)) ans ~= (i + 1).to!int; tehai[j] += 2; } tehai[i]--; } return ans; } /* * 手持ちの牌で役が作れるかを判定する。 * 順子、刻子の順番で試行し、組み合わせが4個できた時点で true を返し、組み合わせが4個できる前に順子、刻子ともに見つからなかったときは false を返す。 */ bool yaku(int[] haipai, int num_yaku) { if (num_yaku >= 4) return true; int[] tehai; foreach (i; 0..haipai.length) { /* 順子が成立するかの判定 */ if (i >= 2 && haipai[i - 2] > 0 && haipai[i - 1] > 0 && haipai[i] > 0) { tehai = haipai.dup; tehai[i - 2]--; tehai[i - 1]--; tehai[i]--; return yaku(tehai, ++num_yaku); } /* 刻子が成立するかの判定 */ if (haipai[i] >= 3) { tehai = haipai.dup; tehai[i] -= 3; return yaku(tehai, ++num_yaku); } } return false; }
D
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array, std.functional; void main() { auto S = readln.chomp; auto res = true; foreach(i, c; S) { auto odd = i % 2; if(!odd && !c.among('R', 'U', 'D')) res = false; if(odd && !c.among('L', 'U', 'D')) res = false; } writeln(res ? "Yes" : "No"); }
D
// Vicfred // https://atcoder.jp/contests/abc126/tasks/abc126_b // implementation import std.algorithm; import std.conv; import std.stdio; import std.string; void main() { const int a = readln.chomp.to!int; const int yy = a / 100; const int mm = a % 100; if(yy >= 1 && yy <= 12) { if(mm >= 1 && mm <= 12) "AMBIGUOUS".writeln; else "MMYY".writeln; } else { if(mm >= 1 && mm <= 12) "YYMM".writeln; else "NA".writeln; } }
D
import std.stdio, std.string, std.conv; void main() { auto AB = readln.split.to!(int[]); auto A = AB[0], B = AB[1]; A = (A + 11) % 13; B = (B + 11) % 13; if (A < B) writeln("Bob"); else if (A > B) writeln("Alice"); else writeln("Draw"); }
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; int sum, m; foreach (i; 0..n) { auto p = readln.chomp.to!int; sum += p; m = max(m, p); } writeln(sum-m/2); }
D
void main() { long n = rdElem; long[] a = rdRow; long mod = 10 ^^ 9 + 7; long[] hat = new long[n]; long result = 1; foreach (x; a) { if (x == 0) { result = result * (3 - hat[0]) % mod; ++hat[0]; } else { result = result * (hat[x-1] - hat[x]) % mod; ++hat[x]; } } result.writeln; } T rdElem(T = long)() { //import std.stdio : readln; //import std.string : chomp; //import std.conv : to; return readln.chomp.to!T; } alias rdStr = rdElem!string; dchar[] rdDchar() { //import std.conv : to; return rdStr.to!(dchar[]); } T[] rdRow(T = long)() { //import std.stdio : readln; //import std.array : split; //import std.conv : to; return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdElem).array; } T[][] rdMat(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdRow).array; } void wrMat(T = long)(T[][] mat) { //import std.stdio : write, writeln; 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.container; import std.typecons; import std.ascii; import std.uni;
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; alias Tuple!(int, "l", int, "r", int, "x") Condition; immutable int MOD = 10^^9 + 7; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto C = new Condition[][](N+1); foreach (i; 0..M) { s = readln.split.map!(to!int); C[s[1]] ~= Condition(s[0], s[1], s[2]); } auto dp = new int[][][](N+1, N+1, N+1); dp[0][0][0] = 1; foreach (i; 0..N) { foreach (j; 0..N) { foreach (k; 0..N) { int mx = max(i, max(j, k)); (dp[mx+1][j][k] += dp[i][j][k]) %= MOD; (dp[i][mx+1][k] += dp[i][j][k]) %= MOD; (dp[i][j][mx+1] += dp[i][j][k]) %= MOD; foreach (c; C[mx+1]) { if (c.x == 1) { if (j >= c.l || k >= c.l) dp[mx+1][j][k] = 0; if (k >= c.l || i >= c.l) dp[i][mx+1][k] = 0; if (i >= c.l || j >= c.l) dp[i][j][mx+1] = 0; } else if (c.x == 2) { if (j < c.l && k < c.l) dp[mx+1][j][k] = 0; if (j >= c.l && k >= c.l) dp[mx+1][j][k] = 0; if (k < c.l && i < c.l) dp[i][mx+1][k] = 0; if (k >= c.l && i >= c.l) dp[i][mx+1][k] = 0; if (i < c.l && j < c.l) dp[i][j][mx+1] = 0; if (i >= c.l && j >= c.l) dp[i][j][mx+1] = 0; } else { if (j < c.l || k < c.l) dp[mx+1][j][k] = 0; if (k < c.l || i < c.l) dp[i][mx+1][k] = 0; if (i < c.l || j < c.l) dp[i][j][mx+1] = 0; } } } } } int ans = 0; foreach (i; 0..N+1) { foreach (j; 0..N+1) { if (dp[i][j][N] != -1) (ans += dp[i][j][N]) %= MOD; if (dp[i][N][j] != -1) (ans += dp[i][N][j]) %= MOD; if (dp[N][i][j] != -1) (ans += dp[N][i][j]) %= MOD; } } ans.writeln; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { auto nx = readints; int x = nx[1]; auto xs = readints; auto ans = xs.map!(e => abs(x - e)).reduce!(gcd); writeln(ans); } T gcd(T)(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }
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; 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() { auto numbers = aryread(); auto max_num = reduce!max(numbers); long count; foreach (ref e; numbers) { e = max_num - e; while(e > 1) { e -= 2; count++; } } auto ary_sum = numbers.sum(); if(ary_sum > 0) { count += 3 - ary_sum; } writeln(count); }
D
import std.stdio; void main(){ auto n=readln(); if(n[0]==n[1]&&n[1]==n[2]) writeln("Yes"); else if(n[1]==n[2]&&n[2]==n[3]) writeln("Yes"); else writeln("No"); }
D
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array, std.functional; void main() { auto s = readln.chomp; auto t = readln.chomp; auto idx = new ulong[][256]; foreach(i, c; s) { idx[c] ~= 1 + i; } long f() { ulong l,j; foreach(c; t) { if(idx[c].length == 0) return -1; auto a = assumeSorted(idx[c]).upperBound(j); if(!a.empty) { j = a.front; } else { l++; j = idx[c].front; } } return l * s.length + j; } f().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; int calc(int time, int[] n) { if(n.length==0) { return time; } while(time%10!=0) { time++; } int res =int.max; for(int i =0;i < n.length;i++) { int[] cn = n.dup; auto e = cn[i]; cn = cn.remove(i); auto m = calc(time + e, cn); res = min(res, m); } return res; } void main() { int[] list; for (int i = 0; i < 5; ++i) { list~=readln().strip.to!int; } writeln(calc(0, list)); }
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()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } // }}} // 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!(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, y; cin.scan(x, y); long cnt; while (x <= y) { x *= 2; cnt++; } writeln(cnt); }
D
import std.stdio; import std.algorithm; import std.conv; import std.numeric; import std.math; import std.string; void main() { auto n = to!int(chomp(readln())); int result; while (true) { auto a1 = n / 100; auto a2 = (n / 10) % 10; auto a3 = n % 10; if (a1 == a2 && a1 == a3) { result = n; break; } ++n; } writeln(result); stdout.flush(); }
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; long calc(long a, long b) { if(a > b) { swap(a, b); } if(b%a == 0) { return 2*b-a; } else { return 2*(b/a)*a + calc(a, b%a); } } void main(){ long n, x; readlnTo(n, x); long ans = n + calc(x, n-x); writeln(ans); }
D
import std.stdio,std.conv,std.string; void main(){ auto s=readln.chomp; if(s[2]==s[3]&&s[4]==s[5]) "Yes".writeln; else "No".writeln; }
D
void main() { int[] a = readln.split.to!(int[]); writeln(a.reduce!max - a.reduce!min); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.container; import std.typecons;
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.stdlib; immutable int INF = 1 << 29; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto K = s[1]; auto cnt = new int[](N); foreach (_; 0..K) { auto D = readln.chomp.to!int; auto A = readln.split.map!(to!int).array; foreach (a; A) cnt[a-1] += 1; } cnt.map!(a => a == 0).sum.writeln; }
D
import core.bitop; import std.algorithm; import std.array; import std.ascii; import std.container; import std.conv; import std.format; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; void main() { int r = readln.chomp.to!int; int g = readln.chomp.to!int; writeln = (g - r) * 2 + r; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void get(Args...)(ref Args args) { import std.traits, std.meta, std.typecons; static if (Args.length == 1) { alias Arg = Args[0]; static if (isArray!Arg) { args[0] = readln.split.to!Arg; } else static if (isTuple!Arg) { auto input = readln.split; static foreach (i; 0..Fields!Arg.length) { args[0][i] = input[i].to!(Fields!Arg[i]); } } else { args[0] = readln.chomp.to!Arg; } } else { auto input = readln.split; assert(input.length == Args.length); static foreach (i; 0..Args.length) { args[i] = input[i].to!(Args[i]); } } } void get_lines(Args...)(size_t N, ref Args args) { import std.traits, std.range; static foreach (i; 0..Args.length) { static assert(isArray!(Args[i])); args[i].length = N; } foreach (i; 0..N) { static if (Args.length == 1) { get(args[0][i]); } else { auto input = readln.split; static foreach (j; 0..Args.length) { args[j][i] = input[j].to!(ElementType!(Args[j])); } } } } struct Dsu { public: this(int n) @safe nothrow { _n = n, parent_or_size = new int[](n); parent_or_size[] = -1; } int merge(int a, int b) @safe nothrow @nogc { assert(0 <= a && a < _n); assert(0 <= b && b < _n); int x = leader(a), y = leader(b); if (x == y) return x; if (-parent_or_size[x] < -parent_or_size[y]) { auto tmp = x; x = y; y = tmp; } parent_or_size[x] += parent_or_size[y]; parent_or_size[y] = x; return x; } bool same(int a, int b) @safe nothrow @nogc { assert(0 <= a && a < _n); assert(0 <= b && b < _n); return leader(a) == leader(b); } int leader(int a) @safe nothrow @nogc { assert(0 <= a && a < _n); if (parent_or_size[a] < 0) return a; return parent_or_size[a] = leader(parent_or_size[a]); } int size(int a) @safe nothrow @nogc { assert(0 <= a && a < _n); return -parent_or_size[leader(a)]; } int[][] groups() @safe nothrow { auto leader_buf = new int[](_n), group_size = new int[](_n); foreach (i; 0 .. _n) { leader_buf[i] = leader(i); group_size[leader_buf[i]]++; } auto result = new int[][](_n); foreach (i; 0 .. _n) result[i].reserve(group_size[i]); foreach (i; 0 .. _n) result[leader_buf[i]] ~= i; int[][] filtered; foreach (r; result) if (r.length != 0) filtered ~= r; return filtered; } private: int _n; int[] parent_or_size; } void main() { int N, M; get(N, M); auto dsu = Dsu(N); while (M--) { int A, B; get(A, B); dsu.merge(A-1, B-1); } writeln(dsu.groups().length - 1); }
D
import std.stdio, std.string, std.algorithm, std.array, std.range, std.conv, std.typecons, std.math, std.container, std.format, std.numeric; void main(string[] args) { string s = readln.strip; string t = readln.strip; long cnt = 0; foreach (i; 0 .. s.length) { if (s[i] != t[i]) { 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; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { long s = lread(); long[] a = new long[](2000000); a[0] = s; foreach (i; 1 .. a.length) { if ((a[i - 1] & 1) == 0) { a[i] = a[i - 1] / 2; } else { a[i] = a[i - 1] * 3 + 1; } } bool[long] d; foreach (i, v; a) { if (v in d) { writeln(i + 1); return; } else { d[v] = true; } } }
D
import std.stdio; import std.conv; import std.string; void main() { int[] a;// = to!( int[] )( readln.chomp() ); for(;a.length<4;)a~= to!( int )( readln.chomp() ); writeln(a[0] > a[1] ? a[1] * a[2] + (a[0] - a[1]) * a[3]:a[0] * a[2]); }
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() { readln.chomp.to!int.pipe!"a*a".pipe!( a => a-readln.chomp.to!int ).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
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; bool calc(string s, string t) { auto n = s.length; s = s ~ s; for (int i = 0; i < n; i++) { if (s[i..i+n] == t) return true; } return false; } void main() { auto s = readln.chomp; auto t = readln.chomp; writeln(calc(s, t) ? "Yes" : "No"); }
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; } } enum MOD = (10 ^^ 9) + 7; void main() { auto S = sread(); solve(S[1 .. $], 0, S[0] - '0').writeln(); } long solve(string s, long sum, long tmp) { if (s.length == 0) { return sum + tmp; } long a = solve(s[1 .. $], sum, tmp * 10 + (s[0] - '0')); long b = solve(s[1 .. $], sum + tmp, s[0] - '0'); return a + b; }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; import std.typecons; import std.bigint; // import dcomp.foundation, dcomp.scanner; // import dcomp.container.deque; int main() { auto sc = new Scanner(stdin); int n, m, k; sc.read(n, m, k); m++; int[] a = new int[n]; a.each!((ref x) => sc.read(x)); long[] dp = a.map!(to!long).array; long[] ndp = new long[n]; auto deq = Deque!int(); deq.insertBack(1); deq.removeBack(); auto p = deq.p; foreach (int ph; 2..k+1) { ndp[] = -(10L^^18); p.clear(); foreach (int i; 0..n) { if (deq.length && deq[0] == i-m) { deq.removeFront(); } if (deq.length) { ndp[i] = dp[deq[0]] + 1L * ph * a[i]; } while (deq.length && dp[deq[deq.length-1]] <= dp[i]) { deq.removeBack(); } deq.insertBack(i); } swap(dp, ndp); } writeln(dp.fold!max); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; //fold(for old compiler) 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); } } } unittest { import std.stdio; auto l = [1, 2, 3, 4, 5]; assert(l.fold!"a+b"(10) == 25); } } 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 /home/yosupo/Program/dcomp/source/dcomp/container/deque.d */ // module dcomp.container.deque; struct Deque(T) { import core.exception : RangeError; import core.memory : GC; import std.range : ElementType, isInputRange; import std.traits : isImplicitlyConvertible; struct Payload { T *d; size_t st, length, cap; @property bool empty() const { return length == 0; } alias opDollar = length; ref inout(T) opIndex(size_t i) inout { version(assert) if (length <= i) throw new RangeError(); return d[(st+i >= cap) ? (st+i-cap) : st+i]; } private void expand() { import std.algorithm : max; assert(length == cap); auto nc = max(size_t(4), 2*cap); T* nd = cast(T*)GC.malloc(nc * T.sizeof); foreach (i; 0..length) { nd[i] = this[i]; } d = nd; st = 0; cap = nc; } void clear() { st = length = 0; } void insertFront(T v) { if (length == cap) expand(); if (st == 0) st += cap; st--; length++; this[0] = v; } void insertBack(T v) { if (length == cap) expand(); length++; this[length-1] = v; } void removeFront() { assert(!empty, "Deque.removeFront: Deque is empty"); st++; length--; if (st == cap) st = 0; } void removeBack() { assert(!empty, "Deque.removeBack: Deque is empty"); length--; } } struct RangeT(A) { alias T = typeof(*(A.p)); alias E = typeof(A.p.d[0]); T *p; size_t a, b; @property bool empty() const { return b <= a; } @property size_t length() const { return b-a; } @property RangeT save() { return RangeT(p, a, b); } @property RangeT!(const A) save() const { return typeof(return)(p, a, b); } alias opDollar = length; @property ref inout(E) front() inout { return (*p)[a]; } @property ref inout(E) back() inout { return (*p)[b-1]; } void popFront() { version(assert) if (empty) throw new RangeError(); a++; } void popBack() { version(assert) if (empty) throw new RangeError(); b--; } ref inout(E) opIndex(size_t i) inout { return (*p)[i]; } RangeT opSlice() { return this.save; } RangeT opSlice(size_t i, size_t j) { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } RangeT!(const A) opSlice() const { return this.save; } RangeT!(const A) opSlice(size_t i, size_t j) const { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } } alias Range = RangeT!Deque; alias ConstRange = RangeT!(const Deque); alias ImmutableRange = RangeT!(immutable Deque); Payload *p; private void I() { if (!p) p = new Payload(); } private void C() const { version(assert) if (!p) throw new RangeError(); } //some value this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) {I; p = new Payload(); foreach (v; values) { insertBack(v); } } //range this(Range)(Range r) if (isInputRange!Range && isImplicitlyConvertible!(ElementType!Range, T) && !is(Range == T[])) {I; p = new Payload(); foreach (v; r) { insertBack(v); } } @property bool empty() const { return (!p || p.empty); } @property size_t length() const { return (p ? p.length : 0); } alias opDollar = length; ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; } ref inout(T) front() inout {C; return (*p)[0]; } ref inout(T) back() inout {C; return (*p)[$-1]; } void clear() { if (p) p.clear(); } void insertFront(T v) {I; p.insertFront(v); } void insertBack(T v) {I; p.insertBack(v); } void removeFront() {C; p.removeFront(); } void removeBack() {C; p.removeBack(); } Range opSlice() {I; return Range(p, 0, length); } } unittest { import std.algorithm : equal; import std.range.primitives : isRandomAccessRange; import std.container.util : make; auto q = make!(Deque!int); assert(isRandomAccessRange!(typeof(q[]))); //insert,remove assert(equal(q[], new int[](0))); q.insertBack(1); assert(equal(q[], [1])); q.insertBack(2); assert(equal(q[], [1, 2])); q.insertFront(3); assert(equal(q[], [3, 1, 2]) && q.front == 3); q.removeFront; assert(equal(q[], [1, 2]) && q.length == 2); q.insertBack(4); assert(equal(q[], [1, 2, 4]) && q.front == 1 && q.back == 4 && q[$-1] == 4); q.insertFront(5); assert(equal(q[], [5, 1, 2, 4])); //range assert(equal(q[][1..3], [1, 2])); assert(equal(q[][][][], q[])); //const range const auto rng = q[]; assert(rng.front == 5 && rng.back == 4); //reference type auto q2 = q; q2.insertBack(6); q2.insertFront(7); assert(equal(q[], q2[]) && q.length == q2.length); //construct with make auto a = make!(Deque!int)(1, 2, 3); auto b = make!(Deque!int)([1, 2, 3]); assert(equal(a[], b[])); } unittest { import std.algorithm : equal; import std.range.primitives : isRandomAccessRange; import std.container.util : make; auto q = make!(Deque!int); q.clear(); assert(equal(q[], new int[0])); foreach (i; 0..100) { q.insertBack(1); q.insertBack(2); q.insertBack(3); q.insertBack(4); q.insertBack(5); assert(equal(q[], [1,2,3,4,5])); q.clear(); assert(equal(q[], new int[0])); } } unittest { Deque!int a; Deque!int b; a.insertFront(2); assert(b.length == 0); } unittest { import std.algorithm : equal; import std.range : iota; Deque!int a; foreach (i; 0..100) { a.insertBack(i); } assert(equal(a[], iota(100))); } /* IMPORT /home/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) { //string or char[10] etc //todo optimize 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) { //static 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); } } } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); fout.writeln("1 2 3"); fout.writeln("ab cde"); fout.writeln("1.0 1.0 2.0"); fout.close; Scanner sc = new Scanner(File(fileName, "r")); int a; int[2] b; char[2] c; string d; double e; double[] f; sc.read(a, b, c, d, e, f); assert(a == 1); assert(equal(b[], [2, 3])); assert(equal(c[], "ab")); assert(equal(d, "cde")); assert(e == 1.0); assert(equal(f, [1.0, 2.0])); } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File, writeln; import std.datetime; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); foreach (i; 0..1_000_000) { fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2); } fout.close; writeln("Scanner Speed Test(3*1,000,000 int)"); StopWatch sw; sw.start; Scanner sc = new Scanner(File(fileName, "r")); foreach (i; 0..500_000) { int a, b, c; sc.read(a, b, c); assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 500_000..700_000) { int[3] d; sc.read(d); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 700_000..1_000_000) { int[] d; sc.read(d); assert(d.length == 3); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } writeln(sw.peek.msecs, "ms"); }
D
import std.stdio; void main() { foreach(i;1..10) foreach(j;1..10) writeln(i,"x",j,"=",i*j); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto ss = new string[](N); auto ts = new int[](N); foreach (i; 0..N) { auto st = readln.split; ss[i] = st[0]; ts[i] = st[1].to!int; } auto X = readln.chomp; int r; foreach (i; 0..N) { r += ts[i]; if (ss[i] == X) r = 0; } writeln(r); }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons, std.functional; void main() { const tmp = readln.split.to!(long[]); const N = tmp[0], K = tmp[1]; const as = readln.split.to!(long[]); ulong X = 0; foreach_reverse(i; 0..64) { const cnt = as .map!(a => a >> i) .map!(a => a & 1) .sum; if (cnt * 2 >= N) continue; const po = (1L << i); if ((X | po) > K) continue; X |= po; } writeln(as.map!(a => a^X).sum); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { int N; scan(N); auto a = iota(N).map!(i => readln.chomp.to!int).array; auto dp = new long[][](N + 1, 5); fillAll(dp, infl); dp[0][0] = 0; foreach (i ; 1 .. N + 1) { foreach (j ; 0 .. 5) { foreach (k ; 0 .. j + 1) { if (j == 0 || j == 4) { chmin(dp[i][j], dp[i - 1][k] + a[i - 1]); } else if (j == 1 || j == 3) { int x; if (a[i - 1] == 0) { x = 2; } else { x = a[i - 1] % 2; } chmin(dp[i][j], dp[i - 1][k] + x); } else if (j == 2) { chmin(dp[i][j], dp[i - 1][k] + (a[i - 1] + 1) % 2); } } } } auto ans = dp[N].reduce!min; 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
// Try Codeforces // author: Leonardone @ NEETSDKASU import std.stdio, std.string, std.array, std.algorithm, std.conv; void main() { auto nab = to!(int[])(split(chomp(readln()))); int n = nab[0], a = nab[1], b = nab[2]; int ans = 0; int[101] u, w; for (int i = 0; i < n; i++) { auto xy = to!(int[])(split(chomp(readln()))); int x = xy[0], y = xy[1]; int s = x * y; if (x <= a && y <= b) { for (int j = 1; j < 101; j++) { if (u[j] > 0 && j + x <= a) { ans = max(ans, u[j] + s); } if (w[j] > 0 && j + y <= b) { ans = max(ans, w[j] + s); } } } if (y <= a && x <= b) { for (int j = 1; j < 101; j++) { if (u[j] > 0 && j + y <= a) { ans = max(ans, u[j] + s); } if (w[j] > 0 && j + x <= b) { ans = max(ans, w[j] + s); } } } if (x <= a && y <= b) { u[x] = max(u[x], s); w[y] = max(w[y], s); } if (y <= a && x <= b) { u[y] = max(u[y], s); w[x] = max(w[x], s); } } writeln(ans); }
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 bool[][](t); foreach (ti; 0..t) { auto n = RD!int; auto q = RD!int; auto s = RD!string; ans[ti].length = q; foreach (i; 0..q) { auto l = RD!int-1; auto r = RD!int-1; bool ok; foreach (j; 0..l) { if (s[j] == s[l]) { ok = true; break; } } foreach (j; r+1..n) { if (s[j] == s[r]) { ok = true; break; } } ans[ti][i] = ok; } } foreach (e; ans) { foreach (ee; e) writeln(ee ? "YES" : "NO"); } stdout.flush; debug readln; }
D
/+ dub.sdl: name "B" dependency "dcomp" version=">=0.9.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; import std.typecons; int main() { Scanner sc = new Scanner(stdin); int n; sc.read(n); int[] a = [1]; int[] b = []; foreach (_; 0..n) { swap(a, b); auto c = [0] ~ b.dup; foreach (i; 0..a.length) { c[i] += a[i]; c[i] %= 2; } a = c; } writeln(a.length - 1); writeln(a.map!(to!string).join(" ")); writeln(b.length - 1); writeln(b.map!(to!string).join(" ")); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; // import dcomp.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int 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/container/stackpayload.d */ // module dcomp.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* This source code generated by dcomp and include dcomp's source code. dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp) dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt) */
D
import std.stdio, std.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(); } bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); } void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); } string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); } 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; } T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ foreach(_; 0 .. scan!int){ long n = scan!long, k = scan!long; if(n % 2 != k % 2) "NO".writeln; else if(n < k * k) "NO".writeln; else "YES".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 s = RD!string; long ans = 1; int cnt; char last = '?'; auto fib = new long[](10^^5+1); fib[0] = 1; fib[1] = 1; foreach (i; 2..fib.length) { fib[i] = fib[i-1]; fib[i].moda(fib[i-2]); } foreach (c; s) { if (c == 'w' || c == 'm') { ans = 0; break; } if (c == last) ++cnt; else { ans.modm(fib[cnt]); last = '?'; cnt = 0; if (c == 'u' || c == 'n') { last = c; ++cnt; } } } if (cnt != 0) { ans.modm(fib[cnt]); } writeln(ans); stdout.flush(); debug readln(); }
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; import std.numeric; void main() { string s = readln.chomp; int p; int l; p = l = 0; foreach (c; s) { final switch (c) { case 'o': ++p; break; case '-': ++l; break; } } if (p == 0 || l % p == 0) { "YES".writeln; } else { "NO".writeln; } } void tie(R, Args...)(R arr, ref Args args) if (isRandomAccessRange!R || isArray!R) in { assert (arr.length == args.length); } body { foreach (i, ref v; args) { alias T = typeof(v); v = arr[i].to!T; } } void verbose(Args...)(in Args args) { stderr.write("["); foreach (i, ref v; args) { if (i) stderr.write(", "); stderr.write(v); } stderr.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 f (int x) { return (x * (x - 1) ) / 2; } void main() { int n = readln.strip.to!int; int[26] c; foreach (i; 0 .. n) { auto s = readln; ++c[s[0].to!int - 97]; } int res; foreach (i; 0 .. 26) { int m = c[i] >> 1; res += f (m) + f (c[i] - m); } writeln (res); }
D
// cheese-cracker [2022-02-06] void solve(){ long n = scan; long x = scan; long y = scan; auto arr = scanArray; long summ = arr.sum + x; if(summ % 2 == y % 2){ writeln("Alice"); }else{ writeln("Bob"); } } void main(){ long tests = scan; // Toggle! while(tests--) solve; } /*_________________________*That's All Folks!*__________________________*/ import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math, std.numeric; string[] tk; alias tup = Tuple!(long, long); 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; } }
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 bool[](t); foreach (ti; 0..t) { auto n = RD!int; auto a = RD!string; auto b = RD!string; auto cnt = new long[](n+1); foreach (i; 0..n) { cnt[i+1] = cnt[i]; if (a[i] == '1') ++cnt[i+1]; else --cnt[i+1]; } bool ok = true; long inv; foreach_reverse (i; 0..n) { auto x = a[i]-'0'; auto y = b[i]-'0'; if ((x^inv) == y) continue; if (cnt[i+1] != 0) { debug writeln("i:", i); ok = false; break; } inv ^= 1; } ans[ti] = ok; } foreach (e; ans) { writeln(e ? "YES" : "NO"); } stdout.flush; debug readln; }
D
module main; import core.stdc.stdio; int main() { int n; scanf("%d", &n); int [] a = new int[n]; int [] b = new int [n]; int k = 1, id = 0; a[0]=-1; for(int i = 1; i <= n; i ++) { scanf("%d", &a[i]); if(a[i] == a[i - 1]) k ++; else { if(i != 1) { id ++; b[id] = k; k = 1; } } } id ++; b[id] = k; k = 1; int t = 0; for(int i = 2; i <= id; i ++) { if(b[i] != b[i - 1]) t = 1; } if(t == 1) printf("NO"); else printf("YES"); return 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; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto K = s[2]; auto G = new bool[int][](N); auto D = new int[](N); auto Q = new Tuple!(int, int)[](M); foreach (i; 0..M) { s = readln.split.map!(to!int); G[s[0]-1][s[1]-1] = true; G[s[1]-1][s[0]-1] = true; D[s[0]-1] += 1; D[s[1]-1] += 1; Q[i] = tuple(s[0]-1, s[1]-1); } int sm = N; auto valid = new bool[](N); fill(valid, true); void dfs(int n) { if (!valid[n]) return; valid[n] = false; sm -= 1; foreach (m; G[n].keys) { if (!G[n][m]) continue; D[m] -= 1; G[n][m] = false; G[m][n] = false; if (valid[m] && D[m] < K) { dfs(m); } } } foreach (i; 0..N) { if (D[i] < K) dfs(i); } int[] ans; foreach_reverse (q; Q) { ans ~= sm; int u = q[0]; int v = q[1]; if (!valid[u] || !valid[v]) continue; D[u] -= 1; D[v] -= 1; G[u][v] = false; G[v][u] = false; if (valid[u] && D[u] < K) dfs(u); if (valid[v] && D[v] < K) dfs(v); } ans.reverse(); ans.each!writeln; }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { auto n = readln.strip.to!int; int k = 0; int [] [] a; n.iota.each !(_ => a ~= readln.split.map!(to!int).array); n.iota.each !(k => n.iota.each !(i => n.iota.each !(j => a[i][j] = min (a[i][j], a[i][k] + a[k][j]) ) ) ); a.map !(b => b.minPos!q{a > b}.front).minPos!q{a > b}.front.writeln; }
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; immutable long mod = 10^^9 + 7; void main() { long n, m, k; scan(n, m, k); if (n > m) swap(n, m); long ans = powmod(2, n - 1, mod); ans = powmod(ans, m - 1, mod); if (k == 1) { writeln(ans); } else { if ((n & 1) != (m & 1)) { writeln(0); } else { writeln(ans); } } } long powmod(long x, long y, long mod) { return y > 0 ? powmod(x, y>>1, mod)^^2 % mod * x^^(y & 1) % mod : 1L; } 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); } } } struct Queue(T) { private { int N, head, tail; T[] data; } this(int n) { N = n + 1; data = new T[](N); } bool empty() { return head == tail; } bool full() { return (tail + 1) % N == head; } T front() { return data[head]; } void push(T x) { assert(!full); data[tail++] = x; tail %= N; } void pop() { assert(!empty); head = (head + 1) % N; } void clear() { head = tail = 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, core.stdc.string; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto Q = s[1]; auto st = new LazySegmentTree!(long, long, (a,b)=>a+b, (a,b)=>b, (a,b)=>b, (a,b)=>a*b, 0L, 1L<<59)(N+1); while (Q--) { s = readln.split.map!(to!int); int q = s[0]; int l = s[1]; int r = s[2]; if (q == 0) { long v = s[3]; st.update(l, r, v); } else { st.query(l, r).writeln; } } } class LazySegmentTree(T, L, alias opTT, alias opTL, alias opLL, alias opPrd, T eT, L eL) { T[] table; L[] lazy_; int n; int size; this(int n) { this.n = n; size = 1; while (size <= n) size <<= 1; size <<= 1; table = new T[](size); lazy_ = new T[](size); table[] = eT; lazy_[] = eL; } void push(int i, int a, int b) { if (lazy_[i] == eL) return; table[i] = opTL(table[i], opPrd(lazy_[i], b - a + 1)); if (i * 2 + 1 < size) { lazy_[i*2] = opLL(lazy_[i*2], lazy_[i]); lazy_[i*2+1] = opLL(lazy_[i*2+1], lazy_[i]); } lazy_[i] = eL; } T query(int l, int r) { if (l > r) return eT; return query(l, r, 1, 0, n-1); } T query(int l, int r, int i, int a, int b) { if (b < l || r < a) return eT; push(i, a, b); if (l <= a && b <= r) { return table[i]; } else { return opTT(query(l, r, i*2, a, (a+b)/2), query(l, r, i*2+1, (a+b)/2+1, b)); } } void update(int l, int r, L val) { if (l > r) return; update(l, r, 1, 0, n-1, val); } void update(int l, int r, int i, int a, int b, L val) { if (b < l || r < a) { push(i, a, b); } else if (l <= a && b <= r) { lazy_[i] = opLL(lazy_[i], val); push(i, a, b); } else { push(i, a, b); update(l, r, i*2, a, (a+b)/2, val); update(l, r, i*2+1, (a+b)/2+1, b, val); table[i] = opTT(table[i*2], table[i*2+1]); } } }
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 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 r = RD; writeln(r*r); stdout.flush(); debug readln(); }
D
import std.algorithm; import std.conv; 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; while (n >= 2 && a.front == a.back) { n -= 2; a = a[1..$ - 1]; } bool ok = false; ok |= a.equal (a.retro); if (!a.empty) { auto u = a.filter !(c => c != a.front).array; ok |= u.equal (u.retro); auto v = a.filter !(c => c != a.back).array; ok |= v.equal (v.retro); } writeln (ok ? "YES" : "NO"); } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; // dfmt on void main() { long A, B; scan(A, B); 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*2+1] SS; bool[10^^5*2+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; SS[i+2] = (SS[i+2] + SS[i]) % P; } writeln(SS[N]); }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; void main() { int[char] aa; string alphabet = "abcdefghijklmnopqrstuvwxyz"; alphabet.each!(c => aa[c]=0); while(true) { char[] str = readln.chomp.to!(char[]); if (stdin.eof) break; str.each!(c => aa[std.ascii.toLower(c)]++); } alphabet.each!(c => writeln(c~" : "~aa[c].to!string)); }
D
import std.stdio, std.string, std.conv; import std.algorithm, std.array; auto solve(string s_) { auto Nx = s_.split.map!(to!long)(); immutable N=Nx[0], x=Nx[1]; auto as = readln.split.map!(to!long).array(); long m=sum(as); foreach(k;1..N) { auto dp = new long[N]; foreach(i;0..N) dp[i]=min(as[i],as[(i-1+N)%N]); m=min(m,sum(dp)+x*k); swap(dp,as); } return m; } void main(){ for(string s; (s=readln.chomp()).length;) writeln(solve(s)); }
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 x = readln.chomp.to!int; auto c = ('C' + (x / 1000)).to!char; writeln("AB" ~ c); }
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[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} 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 H, W; scan(H, W); if (H == 1 || W == 1) { writeln(1); return; } long a = (W + 1) / 2; long b = W / 2; long c = (H + 1) / 2; long d = H / 2; writeln(a * c + b * d); }
D
import std.stdio, std.string, std.range, std.algorithm, std.math, std.conv; void main() { auto N = readln.chomp.to!int; auto T = readln.split.to!(int[]); auto M = readln.chomp.to!int; auto s = T.sum; foreach (i; 0..M) { auto inp = readln.split.to!(int[]); auto P = inp[0]-1, X = inp[1]; writeln(s - T[P] + X); } }
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 n, a, b; cin.scan(n, a, b); if (abs(a - b) % 2 == 0) { writeln(abs(a - b) / 2); } else { long c = min(a, b); long mini = c + abs(max(a, b) - c) / 2; c = n - max(a, b) + 1; mini = min(mini, c + abs(n - (min(a, b) + c)) / 2); writeln(mini); } }
D
pragma(inline, true) void chmin(T)(ref T a, T b) { if (a > b) a = b; } int[] init() { enum INF = 1 << 28; int[] dp = new int[100010]; foreach (i; 1..100001) { dp[i] = INF; int p6 = 1, p9 = 1; while (p6 <= i || p9 <= i) { if (i - p6 >= 0) chmin(dp[i], dp[i - p6] + 1); if (i - p9 >= 0) chmin(dp[i], dp[i - p9] + 1); p6 *= 6; p9 *= 9; } } return dp; } void main() { import std.conv : to; import std.stdio : readln, writeln; import std.string : chomp; int[] dp = init(); writeln(dp[readln.chomp.to!int]); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "x", long, "y"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { auto h = lread(); ulong i; while (2 ^^ i <= h) i++; writeln(2 ^^ i - 1); } 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 core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { long A, B, C; scan(A, B, C); (B/A).min(C).writeln(); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { string s; scan(s); int x, y; scan(x, y); auto t = s.dup; int xs; while (!t.empty && t.front == 'F') { xs++; t.popFront(); } debug { writeln("xs:", xs); writeln("t:", t); } auto xa = new int[][](2, 0); int mode; foreach (i ; 0 .. t.length) { if (t[i] == 'T') { mode ^= 1; xa[mode] ~= 0; } else { xa[mode].back += 1; } } debug { writeln(xa[0]); writeln(xa[1]); } xa[0] = xa[0].filter!"a > 0".array; xa[1] = xa[1].filter!"a > 0".array; auto ans = solve(xa[0], x, xs) && solve(xa[1], y, 0); writeln(ans ? "Yes" : "No"); } bool solve(int[] a, int x, int s) { auto m = a.length.to!int; int zero = 8000; auto dp = new bool[][](m + 1, 2*zero + 1); dp[0][zero + s] = true; foreach (i ; 0 .. m) { foreach (j ; 0 .. 2*zero + 1) { if (j - a[i] >= 0) { dp[i + 1][j - a[i]] |= dp[i][j]; } if (j + a[i] <= 2*zero) { dp[i + 1][j + a[i]] |= dp[i][j]; } } debug { writefln("%(%b%)", dp[i + 1][zero .. zero + 20]); } } return dp[m][zero + x]; } 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.range, std.conv, std.array, std.algorithm, std.math, std.typecons, std.random; void main() { const tmp = readln.split.to!(long[]); auto N = tmp[0], M = tmp[1]; auto ps = readln.split.to!(long[]); auto pairs = M.iota.map!(_ => readln.split.to!(long[])).array; auto nodeFromIndex = new Node[N]; auto nodeFromValue = new Node[N]; foreach (i; 0..N) { auto n = new Node(i); nodeFromIndex[i] = n; nodeFromValue[ps[i]-1] = n; } foreach (i; 0..M) { nodeFromIndex[pairs[i][0]-1].unite(nodeFromIndex[pairs[i][1]-1]); } long cnt; foreach (i; 0..N) { if (nodeFromIndex[i].find(nodeFromValue[i])) cnt++; } writeln(cnt); } class Node { long value; Node parent; this(long value) { this.value = value; } void unite(Node n) { if (this.find(n)) return; n.root.parent = this.root; } bool find(Node n) { return this.root.value == n.root.value; } Node root() { if (parent is null) return this; auto res = parent.root; this.parent = res; return res; } }
D
import std.stdio, std.math, std.algorithm, std.array, std.string, std.conv, std.container, std.range; pragma(inline, true) T[] Reads(T)() { return readln.split.to!(T[]); } alias reads = Reads!int; pragma(inline, true) void scan(Args...)(ref Args args) { string[] ss = readln.split; foreach (i, ref arg ; args) arg = ss[i].parse!int; } void main() { int n,a,b; scan(n,a,b); writeln(min(a*n, b)); }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; import std.math : abs; int x, y, z; rd(x, y, z); bool plus = false, minus = false, zero = false; if (x + z > y) { plus = true; } if (x < y + z) { minus = true; } if (z >= abs(x - y)) { if ((z - abs(x - y)) % 2 == 0) { zero = true; } } if (plus && !minus && !zero) { writeln("+"); } else if (!plus && minus && !zero) { writeln("-"); } else if (!plus && !minus && zero) { writeln("0"); } else { writeln("?"); } } 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.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 l = read.to!int; int x; if(l >= 0) x = l; else if(l + n - 1 <= 0) x = l + n - 1; else x = 0; int ans; foreach(i; 0 .. n) ans += l + i; ans -= x; ans.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; 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; } enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { int N; long T; scan(N, T); auto t = readln.split.to!(long[]); long ans; foreach (i ; 0 .. N - 1) { ans += min(t[i + 1] - t[i], T); } ans += T; writeln(ans); }
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 t = RD!int; auto ans = new int[](t); foreach (ti; 0..t) { auto n = RD!int; auto m = RD!int; if (n % 2 == 0) ans[ti] = (n/2) * m; else if (m % 2 == 0) ans[ti] = (m/2) * n; else { ans[ti] = (n/2) * m; ans[ti] += (m+1)/2; } } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
import std.conv, std.stdio, std.algorithm, std.string, std.range, std.math; void main() { readln; const tako = readln.split.map!(to!int).array; const N = tako.length; int z; foreach (x; 0..N) foreach (y; x+1..N) { z += tako[x]*tako[y]; } z.writeln; }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; import std.string; import std.uni; import std.range; import std.algorithm; void main() { while (!stdin.eof) { auto a = readln.strip; if (a == "-") break; auto m = readln.strip.to!int; foreach (x; iota(m).map!(x => readln.strip.to!int)) { a = a[x..$] ~ a[0..x]; } writeln(a); } }
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; auto A = RDR.ARR; long ans = long.max; foreach (i; 0..N) { auto x = A[i]; long cnt; while (x % 2 == 0) { x /= 2; ++cnt; } ans = min(ans, cnt); } writeln(ans); stdout.flush(); debug readln(); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto d=new int[](n); foreach(i; 0..n) rd(d[i]); bool ok=true; void f(){ int r=0; foreach(int i, int e; d){ ok&=i*10<=r; r=max(r, i*10+e); } } f(); reverse(d); f(); if(ok) writeln("yes"); else writeln("no"); } void chmax(T)(ref T x, T y){ if(x<y) x=y; } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { const N = readln.chomp.to!long; writeln(N*(1+(N&1))); }
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; } int calc(int[] xs) { int rec(int p, int[] buf) { if (p == buf.length) { bool ok = false; for (int i = 0; i < buf.length; i++) { if (buf[i] % 2 == 0) { ok = true; break; } } return ok ? 1 : 0; } int ans = 0; for (int i = -1; i <= 1; i++) { buf[p] = xs[p] + i; ans += rec(p + 1, buf); } return ans; } auto buf = new int[xs.length]; return rec(0, buf); } void main() { readint; auto xs = readints; writeln(calc(xs)); }
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 x = readln.chomp.split.to!(int[]); auto k = readln.chomp.to!int; int index; int m; foreach (i, e; x) { if (e > m) { m = e; index = i.to!int; } } x[index] *= 2 ^^ k; x.sum.writeln; }
D
import std.stdio,std.string,std.conv; immutable int MAX = 50; immutable int COUNT = 9; void main(){ char[] buf; int[] dataset; while(stdin.readln(buf)){ if(buf.chomp().to!string().isNumeric){ dataset ~= buf.chomp().to!int(); } if(dataset.length > 50) break; } int[] ans; foreach(int num;dataset){ ans ~= searchNum(num); } foreach(int num;ans){ writeln(num); } } int searchNum(int x){ int result = 0; for(int i = 0;i <= COUNT;i++){ for(int j = 0;j <= COUNT;j++){ for(int m = 0;m <= COUNT;m++){ for(int k = 0;k <= COUNT;k++){ if(x == i + j + k + m){ result++; } } } } } return result; }
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; void main() { while(true) { string str = readln.chomp; if (str == "0") break; reduce!((a, b) => a+(b-'0'))(0, str).writeln; } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} 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 A, B, C; scan(A, B, C); if (A == 1) { writeln("YES"); return; } foreach (i; 1 .. 10000000) { long x = A * i % B; // writeln(x); if (x == C) { writeln("YES"); return; } } writeln("NO"); } /// x^^n % m T powmod(T = long)(T x, T n, T m = 10 ^^ 9 + 7) { if (n < 1) return 1; if (n & 1) { return x * powmod(x, n - 1, m) % m; } T tmp = powmod(x, n / 2, m); return tmp * tmp % m; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; // dfmt on void main() { long A, B, C; scan(A, B, C); if (A == B && B == C) { writeln((A & 1) ? 0 : -1); return; } long cnt; while (!((A & 1) | (B & 1) | (C & 1))) { cnt++; long a = (B + C) / 2; long b = (A + C) / 2; long c = (A + B) / 2; A = a; B = b; C = c; } writeln(cnt); }
D
import std; enum inf(T)()if(__traits(isArithmetic,T)){return T.max/4;} T scan(T=long)(){return readln.chomp.to!T;} void scan(T...)(ref T args){auto input=readln.chomp.split;foreach(i,t;T)args[i]=input[i].to!t;} T[] scanarr(T=long)(){return readln.chomp.split.to!(T[]);} alias Queue=DList;auto enq(T)(ref Queue!T q,T e){q.insertBack(e);}T deq(T)(ref Queue!T q){T e=q.front;q.removeFront;return e;} alias Stack=SList;auto push(T)(ref Stack!T s,T e){s.insert(e);}T pop(T)(ref Stack!T s){T e=s.front;s.removeFront;return e;} struct UnionFind(T){T[T]u;ulong[T] rank;@property{bool inc(T e){return e in u;}auto size(){return u.keys.length;}auto dup(){T[] child=u.keys;T[] parent=u.values;auto res=UnionFind!T(child);child.each!(e=>res.add(e));size.iota.each!(i=>res.unite(child[i],parent[i]));return res;}}this(T e){e.add;}this(T[] es){es.each!(e=>e.add);}auto add(T a,T b=a){assert(b.inc);u[a]=a;rank[a];if(a!=b)unite(a,b);}auto find(T e){if(u[e]==e)return e;return u[e]=find(u[e]);}auto same(T a,T b){return a.find==b.find;}auto unite(T a,T b){a=a.find;b=b.find;if(a==b)return;if(rank[a]<rank[b])u[a]=b;else{u[b]=a;if(rank[a]==rank[b])rank[a]++;}}} struct PriorityQueue(T,alias less="a<b"){BinaryHeap!(T[],less) heap;@property{bool empty(){return heap.empty;}auto length(){return heap.length;}auto dup(){return PriorityQueue!(T,less)(array);}T[] array(){T[] res;auto tp=heap.dup;foreach(i;0..length){res~=tp.front;tp.removeFront;}return res;}void push(T e){heap.insert(e);}void push(T[] es){es.each!(e=>heap.insert(e));}}T look(){return heap.front;}T pop(){T tp=look;heap.removeFront;return tp;}this(T e){ heap=heapify!(less,T[])([e]);} this(T[] e){ heap=heapify!(less,T[])(e);}} //END OF TEMPLATE void main(){ long s,w; scan(s,w); (w<s?"safe":"unsafe").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; void main() { int N, M; scan(N, M); auto a = readln.split.to!(int[]); auto t = a.sum(); int cnt; foreach (ai ; a) { if (4*ai*M >= t) { cnt++; } } yes(cnt >= M); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import 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!(long, "a", long, "b"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { auto n = lread(); auto a = aryread(); long t; foreach (i; iota(n)) t = a[i] - t; t /= 2; foreach (i; iota(n)) { write(2 * t, " "); t = a[i] - t; } writeln(); } 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 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; ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias TOWN = Tuple!(long, "x", long, "y"); void main() { auto week = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"]; auto s = sread(); foreach(i; iota(week.length)) { if(s == week[i]) { (7 - i).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; } } long product_MOD(long n) { long p_MOD = 1; while (n > 0) { p_MOD *= n--; p_MOD %= MOD; } return p_MOD; }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional, std.traits; import std.algorithm, std.container; import core.stdc.stdlib, core.bitop; enum MOD = pow(10,9)+7; void main() { auto S = scanString; if(S[0]==S[1]&&S[2]==S[3]&&S[0]!=S[2]) { writeln("Yes"); return; } if(S[0]==S[2]&&S[1]==S[3]&&S[0]!=S[3]) { writeln("Yes"); return; } if(S[0]==S[3]&&S[1]==S[2]&&S[0]!=S[1]) { writeln("Yes"); return; } writeln("No"); } struct Vec2{ long x,y; alias a=x, b=y; } struct Vec3{ long x,y,z; alias a=x, b=y, c=z; } long gcd(long a, long b) { if(b == 0) return a; return gcd(b, a % b); } 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; } } string scanString() { return scanElem!string; } 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!(T[]); } 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)); } } } long scanULong(){ long x; while(true){ const c = getchar; if(c<'0'||c>'9'){ break; } x = x*10+c-'0'; } return x; } 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[] divisors(long n) { long[] list; void func(Factor[] fs, long n) { if(fs.empty){ list ~= n; return; } foreach(c; 0..fs[0].c+1) { func(fs[1..$], n * (fs[0].n ^^ c)); } } func(factors(n), 1); sort(list); return list; } //nまでの素数のリスト 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() { long n, c; ipElems(n, c); Sushi[] sushi = n.rdCol!Sushi; long[] l = new long[n+1]; long[] lrev = new long[n+1], lrevmax = new long[n+1]; long pos; foreach (i, s; sushi) { l[i+1] = l[i] + s.v - s.x + pos; lrev[i+1] = l[i+1] - s.x; lrevmax[i+1] = max(lrevmax[i], lrev[i+1]); pos = s.x; } long[] r = new long[n+1]; long[] rrev = new long[n+1], rrevmax = new long[n+1]; pos = c; foreach_reverse (i, s; sushi) { r[i] = r[i+1] + s.v - pos + s.x; rrev[i] = r[i] - c + s.x; rrevmax[i] = max(rrevmax[i+1], rrev[i]); pos = s.x; } long result; foreach (i; 0 .. n+1) { result = max(result, l[i]+rrevmax[i], lrevmax[i]+r[i]); } result.writeln; } struct Sushi { long x, v; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void ipElems(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
void main() { int[] tmp = readln.split.to!(int[]); int a = tmp[0], b = tmp[1]; int coin; foreach (i; 0 .. 2) { if (a > b) { coin += a; --a; } else { coin += b; --b; } } coin.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.container; import std.typecons;
D
import std.stdio, std.string, std.array; void main() { readln.replace("apple", "##").replace("peach", "apple").replace("##", "peach").write; }
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 N = RD; auto A = RDA(-1); auto cnt = new long[](N); foreach (i; 0..N-1) { ++cnt[A[i]]; } foreach (e; cnt) writeln(e); stdout.flush; debug readln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container; void main() { auto a = readln.chomp.to!int; writeln(a + a^^2 + a^^3); }
D