code
stringlengths
4
1.01M
language
stringclasses
2 values
void main() { problem(); } void problem() { auto N = scan!long, M = scan!long; long solve() { long answer; if (N > 0) answer += N*(N-1)/2; if (M > 0) answer += M*(M-1)/2; return answer; } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { auto S = readln.chomp; int x = 0; int cnt = 0; while (x+1 < S.length) { if (S[x] == S[x+1]) { x++; } else { S = S[0..x] ~ S[x+2..$]; cnt += 2; x = max(x-1, 0); } } writeln(cnt); }
D
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.typecons, std.range, std.container, std.math, std.algorithm.searching, std.functional,std.mathspecial, std.numeric; void main(){ auto nt=readln.split.map!(to!long).array; auto ts=readln.split.map!(to!long).array; auto T=nt[1]; long last=0; long sum=0; foreach(t;ts){ sum+=min(T,t-last); last=t; } sum+=T; writeln(sum); }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { writeln("Christmas", " Eve".repeat(25 - readln.chomp.to!int).join); }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; immutable int steps = 10; int solve (string s, int team) { int [2] goals; foreach (step; 0..steps) { auto cur = (s[step] == '0') ? 0 : (s[step] == '1') ? 1 : (step % 2 == team); goals[step % 2] += cur; if (goals[0] + (steps - 1 - step + 0) / 2 < goals[1] || goals[1] + (steps - 1 - step + 1) / 2 < goals[0]) { return step + 1; } } return steps; } void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto s = readln.strip; writeln (min (solve (s, 0), solve (s, 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; import std.numeric; void main() { auto n = readln.chomp.to!int; auto h = readln.chomp.split.to!(int[]); bool f; foreach (i; 1..n) { auto d = h[i] - h[i-1]; if (d == -1) { h[i]++; } else if (d < 0) { f = true; break; } } if (f) { writeln("No"); } else { writeln("Yes"); } }
D
import std.conv; import std.stdio; import std.string; void main() { auto n = readln.chomp.to!int; (n%2==0 ? n : 2*n).writeln; }
D
import std.stdio; import std.string; import std.conv; import std.math; void main() { auto num = readln().chomp().to!int(); writeln(pow(num, 3)); }
D
import std.stdio, std.conv, std.string; void main() { int a,b,c; scanf("%d %d %d", &a, &b, &c); if (a <= c && c <= b) { writeln("Yes"); } else { writeln("No"); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto n = RD!int; auto k = RD!int; auto x = cast(int)sqrt(cast(double)k); long ans; foreach (i; x..n+2) { auto j = cast(long)i; auto j2 = cast(long)(i-1); auto y = j * j2 / 2; if (j * j2 < k) continue; if (y - k == n - j2) { debug writeln(i); ans = n - j2; break; } } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio; import std.string; import std.conv; int count(int n) { int ans = 0; for(int a = 0; a < 10; a++) for(int b = 0; b < 10; b++) for(int c = 0; c < 10; c++) for(int d = 0; d < 10; d++) if(a + b + c + d == n) ans++; return ans; } void main() { int[] data; while(true) { auto input = readln(); if(stdin.eof()) break; data ~= to!int(chomp(input)); } foreach(i ; data) { writeln(count(i)); } }
D
import std.stdio,std.string,std.conv; void main(){ for(;;){ auto rc = readln().chomp(); if( rc == "0" ){ break; } int sum=0; foreach( c ; rc ){ sum += c-'0'; } writeln(sum); } }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; bool solve (int n, int [] a) { n = min (n, 30); a = a[0..n]; remove_loop: while (!a.empty) { foreach_reverse (i; 0..n) { if (a[i] % (i + 2) != 0) { a = a[0..i] ~ a[i + 1..$]; n -= 1; continue remove_loop; } } return false; } return true; } void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto n = readln.strip.to !(int); auto a = readln.splitter.map !(to !(int)).array; writeln (solve (n, a) ? "YES" : "NO"); } }
D
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math, std.container, std.typecons; import core.stdc.stdio; // foreach, foreach_reverse, writeln void main() { const int MOD = 1_000_000_007; int n; scanf("%d", &n); int[] cb = new int[n+1]; cb[0] = 1; foreach (i; 2..n+1) { if (i&1) continue; cb[i] = long(cb[i-2])*(i-1)%MOD; } int[][] e = new int[][](n); foreach (i; 0..n-1) { int x, y; scanf("%d%d", &x, &y); x--; y--; e[x] ~= y; e[y] ~= x; } int[][] dfs(int v, int par=-1) { int[][] dp = new int[][](2,2); dp[0][1] = 1; foreach (u; e[v]) { if (u == par) continue; auto d = dfs(u,v); int nv = dp[0].length.to!int; int nu = d[0].length.to!int; int[][] p = new int[][](2,nv+nu-1); swap(dp,p); foreach (bi; 0..2) foreach (i; 0..nv) { foreach (bj; 0..2) foreach (j; 0..nu) { (dp[bi^bj][i+j] += long(p[bi][i])*d[bj][j]%MOD) %= MOD; } } } foreach (bi; 0..2) foreach (i; 2..dp[0].length) { if (i&1) continue; (dp[bi^1][0] += long(dp[bi][i])*cb[i]%MOD) %= MOD; } return dp; } auto dp = dfs(0); int ans = (dp[1][0]-dp[0][0]+MOD)%MOD; writeln(ans); }
D
//dlang template---{{{ import std.stdio; import std.conv; import std.string; import std.array; import std.algorithm; import std.typecons; import std.math; import std.range; // MIT-License https://github.com/kurokoji/nephele class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } //}}} void main() { Scanner sc = new Scanner; int S, W; sc.scan(S, W); if (S <= W) writeln("unsafe"); else writeln("safe"); }
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; import std.bigint; import core.checkedint; import core.bitop; import std.digest.sha; void main() { int n, k; readln.chomp.split.tie(n, k); writeln = n % k != 0 ? 1 : 0; } 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
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 S = RD!string; long ans; if (S == "SUN") ans = 7; else if (S == "MON") ans = 6; else if (S == "TUE") ans = 5; else if (S == "WED") ans = 4; else if (S == "THU") ans = 3; else if (S == "FRI") ans = 2; else ans = 1; writeln(ans); stdout.flush(); debug readln(); }
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 m = n; int cnt; foreach_reverse(i; 0..10) { auto e = 10 ^^ i; auto d = n / e; n %= e; cnt += d; } writeln(m % cnt ? "No" : "Yes"); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.ascii; void main() { auto s = readln.chomp; int[char] cnt; foreach (e; s) { cnt[e]++; } if (cnt.keys.length == 2 && cnt[cnt.keys[0]] == 2) { writeln("Yes"); } else { writeln("No"); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void main() { auto S = RD!string; string ans; foreach (i; 0..S.length) ans ~= "x"; writeln(ans); stdout.flush; debug readln; }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; bool check(string s) { if (s.length % 2) { return false; } while (!s.empty) { if (s[0 .. 2] != "hi") { return false; } s.popFront(); s.popFront(); } return true; } void main() { string s; scan(s); yes(check(s)); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.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(int n) { for (int i = 0; i < 100; i++) { for (int j = 0; j < 100; j++) { if (n == i * 4 + j * 7) { return true; } } } return false; } void main() { int n = readint; writeln(calc(n) ? "Yes" : "No"); }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional; import std.algorithm, std.container; struct S{ long x=long.min; long[] pair; long[] cost; } S[] sList; bool resolve(S s){ if(s.x==long.min) { s.x=0; } foreach(i; 0..s.pair.length) { if(sList[s.pair[i]].x==long.min) { sList[s.pair[i]].x = s.x + s.cost[i]; resolve(sList[s.pair[i]]); }else{ if(sList[s.pair[i]].x != s.x + s.cost[i]) { return false; } } } return true; } void main() { long N = scanElem; long M = scanElem; sList.length = N+1; foreach(i; 0..M) { auto l = scanElem; auto r = scanElem; auto d = scanElem; sList[l].pair ~= r; sList[l].cost ~= d; sList[r].pair ~= l; sList[r].cost ~= d*-1; } foreach(s; sList) { if(!resolve(s)) { writeln("No"); return; } } writeln("Yes"); } class UnionFind{ UnionFind parent = null; void merge(UnionFind a) { if(same(a)) return; a.root.parent = this.root; } UnionFind root() { if(parent is null)return this; return parent = parent.root; } bool same(UnionFind a) { return this.root == a.root; } } void scanValues(TList...)(ref TList list) { auto lit = readln.splitter; foreach (ref e; list) { e = lit.fornt.to!(typeof(e)); lit.popFront; } } T[] scanArray(T = long)() { return readln.split.to!(long[]); } void scanStructs(T)(ref T[] t, size_t n) { t.length = n; foreach (ref e; t) { auto line = readln.split; foreach (i, ref v; e.tupleof) { v = line[i].to!(typeof(v)); } } } T scanElem(T = long)() { char[] res; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { res ~= cast(char) c; c = getchar; } return res.strip.to!T; } template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } struct Factor { long n; long c; } Factor[] factors(long n) { Factor[] res; for (long i = 2; i ^^ 2 <= n; i++) { if (n % i != 0) continue; int c; while (n % i == 0) { n = n / i; c++; } res ~= Factor(i, c); } if (n != 1) res ~= Factor(n, 1); return res; } long[] primes(long n) { if(n<2)return []; auto table = new long[n+1]; long[] res; for(int i = 2;i<=n;i++) { if(table[i]==-1) continue; for(int a = i;a<table.length;a+=i) { table[a] = -1; } res ~= i; } return res; } bool isPrime(long n) { if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (long i = 3; i ^^ 2 <= n; i += 2) if (n % i == 0) return false; return true; }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; void times(alias fun)(int n) { foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } void main() { int N = readln.chomp.to!int; int M = 10^^6+1; long[] dp = new long[M]; int[] ary = N.rep!(() => readln.chomp.to!int); dp[0] = 1; foreach(i; 0..N) { foreach(j; M.iota.retro) { if (j+ary[i] >= M) continue; dp[j+ary[i]] += dp[j]; } } auto po = dp.enumerate.retro.find!( a => a.value>0 && a.index%10!=0 ); writeln(po.empty ? 0 : po.front.index); } // ---------------------------------------------- // 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
void main() { writeln(readln.chomp.to!int / 3); } 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; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); void main() { long n, m; scan(n, m); auto ab = new long[][](n, 2); auto cd = new long[][](m, 2); // writeln(ab); // writeln(cd); auto ans = new long(100); auto check_i = new long[](n); foreach (i; 0 .. n) { long a, b; scan(a, b); ab[i][0] = a; ab[i][1] = b; } // writeln(ab); foreach (i; 0 .. m) { long c, d; scan(c, d); cd[i][0] = c; cd[i][1] = d; } // writeln(cd); foreach (i; 0 .. n) { long ans_x = 10 ^^ 9; foreach (j; 0 .. m) { long tmp; tmp = abs(ab[i][0] - cd[j][0]) + abs(ab[i][1] - cd[j][1]); if (tmp < ans_x) { check_i[i] = j + 1; ans_x = tmp; } } } foreach (i; 0 .. n) { writeln(check_i[i]); } } //https://rclone.org/ void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std; // dfmt on void main() { long N = lread(); long[long] D; foreach (i; 1 .. N + 1) { auto f = factorize(i); foreach (k; f) D[k] = D.get(k, 0) + 1; } long ans = 1; foreach (k, v; D) ans *= v + 1, ans %= MOD; writeln(ans); } /// 素因数分解 T[] factorize(T = long)(T x) { assert(0 < x, "x is negative"); T[] result; while ((x & 1) == 0) x /= 2, result ~= 2; for (T i = 3; i * i <= x; i += 2) while (x % i == 0) x /= i, result ~= i; if (x != 1) result ~= x; return result; }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; immutable mod = 10^^9 + 7; immutable inf = 2 * 10^^9; void main() { int n, k; scan(n, k); auto x = new int[](n), y = new int[](n); foreach (i ; 0 .. n) {scan(x[i], y[i]);} long ans = long.max; bool[] mask = new bool[](n); void dfs(int xl, int xr, int yb, int yt, int take) { if (take == k || take == 4) { int cnt; foreach (i ; 0 .. n) { if (xl <= x[i] && x[i] <= xr && yb <= y[i] && y[i] <= yt) cnt++; } if (cnt >= k) { ans = min(ans, 1L * (xr - xl) * (yt - yb)); } return; } foreach (i ; 0 .. n) { if (!mask[i]) { mask[i] = 1; dfs(min(xl, x[i]), max(xr, x[i]), min(yb, y[i]), max(yt, y[i]), take + 1); mask[i] = 0; } } } dfs(inf, -inf, inf, -inf, 0); writeln(ans); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; void main(){ auto F=readln.split.to!(int[]),a=F[0],b=F[1]; if((a+b)%2==0)writeln((a+b)/2); else writeln((a+b)/2+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; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; char calc(string s, long k) { int p = -1; for (int i = 0; i < s.length; i++) { if (s[i] == '1') p = i; else break; } if (k <= p + 1) return '1'; return s[p+1]; } void main() { string s = read!string; long k = read!long; writeln(calc(s, k)); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; void main() { string s; scan(s); writeln(s.count('+').to!int - s.count('-').to!int); } 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
void main() { import std.stdio, std.string, std.conv, std.algorithm; int x, y, z; rd(x, y, z); writeln((x - z) / (y + z)); } void rd(T...)(ref T x) { import std.stdio : readln; import std.string : split; import std.conv : to; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; import std.range; import std.math; void main(){ auto z=readln.split.to!(int[]),a=z[0],b=z[1]; if(a<=8&&b<=8)writeln("Yay!"); else writeln(":("); }
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; void main(){ auto S = scanString; writeln((15-S.length)+S.count('o')>=8?"YES":"NO"); } 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 readln.strip; } 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() { string n = readln.chomp; string m; foreach (x; n) { m ~= x == '1' ? '9' : '1'; } m.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; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.ascii; void main() { auto s = readln.chomp; auto dp = new int[](s.length+1); dp[0] = 0; dp[1] = 1; if (dp.length > 2) { if (s[0] == s[1]) { dp[2] = 1; } else { dp[2] = 2; } } foreach (i; 3..s.length+1) { if (s[i-2] != s[i-1]) { dp[i] = dp[i-1] + 1; } else { dp[i] = max(dp[i-1], dp[i-3] + 2); } } //dp.writeln; dp[$-1].writeln; }
D
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } void main() { auto size = readln.chomp.to!int; auto lucky = cast(byte[])readln.chomp; lucky[] -= '0'; void solve() { int count; foreach(x; 0..10) foreach(y; 0..10) foreach(z; 0..10) { if (!lucky.canFind(x)) continue; auto after = lucky.findSplitAfter([x]); if (!after[1].canFind(y)) continue; after = after[1].findSplitAfter([y]); if (!after[1].canFind(z)) continue; count++; } writeln(count); } solve(); }
D
import std.stdio: writeln, readln; import std.string: strip; void main() { string a = readln.strip; string b = readln.strip; if (a == b) writeln("EQUAL"); else if (a.length > b.length) writeln("GREATER"); else if (b.length > a.length) writeln("LESS"); else if (a > b) writeln("GREATER"); else writeln("LESS"); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n; readV(n); writeln(n*(n+1)/2); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto T = readln.chomp.to!int; foreach (_; 0..T) { auto t = readln.chomp; int o, z; foreach (c; t) { if (c == '0') ++z; if (c == '1') ++o; } if (o == 0 || z == 0) { writeln(t); continue; } char[] s; foreach (i; 0..t.length-1) { s ~= t[i]; if (t[i] == t[i+1]) { if (t[i] == '0') { s ~= '1'; } else { s ~= '0'; } } } s ~= t[$-1]; writeln(s); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto A = RD; auto B = RD; auto C = RD; auto D = RD; writeln(max(min(B, D) - max(A, C), 0)); stdout.flush(); debug readln(); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n, k; rd(n, k); long tot=0; for(int a=1; a<=n; a++){ if(a%k==0){ auto y=(n/k).to!(long); tot+=y*y; }else{ auto x=k-a%k; if((x*2)%k==0){ if(n>=x){ auto y=((n-x)/k+1).to!(long); tot+=y*y; } } // writeln(a, " ", tot); } } writeln(tot); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x) e=l[i].to!(typeof(e)); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { long a, b, c; scan(a, b, c); writeln(min(a + b, a + c, b + c)); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
#!/usr/bin/env rdmd import std.stdio, std.string, std.conv; import std.algorithm, std.array; void main() { for(string S; (S=readln().chomp()).length; ) { auto n = S.to!int(); string w = "<3"; foreach(i;0..n) w ~= readln().chomp() ~ "<3"; immutable string t = readln().chomp().idup; const auto M = w.length; const auto N = t.length; if(M>N){ writeln("no"); return; } auto dp = new int[N+1]; auto dpn = new int[N+1]; foreach(j,v;w) { foreach(i;j..j+N-M+1) dpn[i+1]=max(dpn[i],dp[i+(v==t[i]?0:1)]+(v==t[i]?1:0)); dp.swap(dpn); } writeln(w.length==dp[N]?"yes":"no"); } }
D
import std.stdio, std.array, std.conv, std.string; void main(){ string s; for(;;){ s=readln(); if(stdin.eof()) break; for(int i = cast(int)s.length; i > 1; --i ){ write(s[i-2]); } if(s.length > 0) write("\n"); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto ab = readln.split.to!(int[]); auto a = ab[0]; auto b = ab[1]; if (a > 0) { writeln("Positive"); } else if (a <= 0 && b >= 0) { writeln("Zero"); } else { writeln((b-a+1)%2 == 0 ? "Positive" : "Negative"); } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.math; void main() { int input; immutable limitList = 200000; bool[] listNumbers = new bool[](limitList); int[] listPrimeNumbers; listNumbers.fill(true); foreach (i; 2..limitList.to!double.sqrt.to!int) { if (listNumbers[i]) { for (int j = i*2; j < limitList; j += i) listNumbers[j] = false; } } foreach (i; 2..listNumbers.length) { if (listNumbers[i]) { listPrimeNumbers ~= i.to!int; } } while ((input = readln.chomp.to!int) != 0) { ulong sum = 0; foreach (i; 0..listPrimeNumbers.length) { if (i == input) break; sum += listPrimeNumbers[i]; } writeln(sum); } }
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; } long calc(long n, long a, long b) { if (n == 1) { return a == b ? 1 : 0; } if (a > b) { return 0; } return (n - 2) * (b - a) + 1; } void main() { auto nab = readints; int n = nab[0], a = nab[1], b = nab[2]; writeln(calc(n, a, b)); }
D
import std.stdio; import std.array; import std.conv; void main() { while (1) { string[] str = split(readln()); int a = to!(int)(str[0]); int b = to!(int)(str[1]); if (a == 0 && b == 0) { break; } else if (a > b) { writeln(b, " ", a); } else { writeln(a, " ", b); } } }
D
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; int n; int[] a; void main() { scan(n); a = iota(n).map!(i => readln.chomp.to!int - 1).array; auto d = new int[](n); d[] = -1; d[0] = 0; int pos; while (pos != 1) { if (d[a[pos]] == -1) { d[a[pos]] = d[pos] + 1; pos = a[pos]; } else { break; } } writeln(d[1]); } 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); }
D
void main() { long[] tmp = readln.split.to!(long[]); long a = tmp[0], b = tmp[1], c = tmp[2]; long x = tmp[3], y = tmp[4]; if (a + b < 2 * c) { writeln(a * x + b * y); } else { long z = min(x, y); long u = a * (x - z) + b * (y - z); long v = 2 * c * (max(x, y) - z); writeln(2 * c * z + min(u, v)); } } 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.algorithm, std.string, std.traits, std.array, std.range, std.stdio, std.conv; N gcd(N)(N a, N b) if (isNumeric!N || is (N == BigInt)) { return a ? gcd(b % a, a) : b; } N lcm(N)(N a, N b) if (isNumeric!N || is (N == BigInt)) { return a / gcd(a, b) * b; } void main() { long N = readln.chomp.to!long; long[] T; foreach (_; 0..N) { T ~= readln.chomp.to!long; } long ans = T[0]; foreach (long t; T[1..$]) { ans = lcm(ans, t); } writeln(ans); }
D
import std.functional, std.algorithm, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv; void main() { int n = readln.chomp.to!int; int k = n / 15; writeln(n * 800 - k * 200); }
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, "begin", long, "end"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long n, k; scan(n, k); long i; while(n) { n /= k; i++; } writeln(i); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n, m; rd(n, m); auto g=new int[][](n, 0); foreach(_; 0..m){ int a, b; rd(a, b); a--; b--; g[a]~=b; g[b]~=a; } auto c=new int[](n); fill(c, -1); bool dfs(int i, int w, int p=-1){ c[i]=w; bool ret=true; foreach(j; g[i])if(j!=p){ if(c[j]==-1) ret&=dfs(j, w^1, i); if((c[j]^w)==0) ret=false; } return ret; } if(dfs(0, 0)){ long w=0; foreach(e; c)if(e==0) w++; writeln(w*(long(n)-w)-m); }else{ writeln(long(n)*(n-1)/2-m); } } 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.array, std.conv, std.algorithm, std.typecons, std.range, std.container, std.math, std.algorithm.searching, std.functional,std.mathspecial, std.numeric; void main(){ readln.split.map!(to!int).map!"a-1".reduce!"a*b".writeln; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); int a; rd(a); if(n%500<=a) writeln("Yes"); else writeln("No"); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
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[]);} //END OF TEMPLATE void main(){ auto s=scan!string; auto t=scan!string; foreach(i;0..s.length){ if(s[i]!=t[i]){ "No".writeln; return; } } "Yes".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() { int H, W; scan(H, W); int h, w; scan(h, w); auto ans = (H - h) * (W - w); writeln(ans); } int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new int[][](n, 0); foreach (i; 0 .. m) { int u, v; scan(u, v); if (is1indexed) { u--, v--; } adj[u] ~= v; if (isUndirected) { adj[v] ~= u; } } return adj; } alias Edge = Tuple!(int, "to", int, "cost"); Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new Edge[][](n, 0); foreach (i; 0 .. m) { int u, v, c; scan(u, v, c); if (is1indexed) { u--, v--; } adj[u] ~= Edge(v, c); if (isUndirected) { adj[v] ~= Edge(u, c); } } return adj; } void yes(bool b) { writeln(b ? "Yes" : "No"); } void YES(bool b) { writeln(b ? "YES" : "NO"); } T[] readArr(T)() { return readln.split.to!(T[]); } T[] readArrByLines(T)(int n) { return iota(n).map!(i => readln.chomp.to!T).array; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto H = RD!int; auto W = RD!int; auto a = new string[](H); foreach (y; 0..H) a[y] = RD!string; auto cnt = new long[](26); foreach (y; 0..H) { foreach (x; 0..W) { ++cnt[a[y][x]-'a']; } } bool ans = true; auto hh = H/2; auto ww = W/2; foreach (i; 0..hh*ww) { auto pos = cnt.MIN_POS!"a > b"(); if (cnt[pos] < 4) { ans = false; break; } cnt[pos] -= 4; } auto hhh = H%2; auto www = W%2; foreach (i; 0..hhh*ww+www*hh) { auto pos = cnt.MIN_POS!"a > b"(); if (cnt[pos] < 2) { ans = false; break; } cnt[pos] -= 2; } writeln(ans ? "Yes" : "No"); stdout.flush(); debug readln(); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; enum lim = 80 * 80 * 2; void main() { int m1, d1; scan(m1, d1); int m2, d2; scan(m2, d2); writeln(m1 < m2 ? 1 : 0); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { auto w = ["Sunny", "Cloudy", "Rainy"]; string s; scan(s); auto i = w.countUntil(s); writeln(w[(i + 1) % 3]); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
void main() { problem(); } void problem() { auto K = scan!int; auto S = scan; string solve() { if (S.length > K) return S[0..K] ~ "..."; return S; } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
import std.stdio, std.string, std.conv, std.array, std.algorithm, std.math; long calc(long x) { return x * (x + 1) / 2; } void main() { string s = readln.chomp; if (s[0] == '>') s = '<' ~ s; if (s[$-1] == '<') s ~= '>'; auto g = s.group.array; long[] a = new long[g.length]; foreach (i, x; g) { a[i] = x[1]; } long ans; for (auto i = 0; i < a.length; i += 2) { if (a[i] < a[i+1]) { ans += calc(a[i]-1) + calc(a[i+1]); } else { ans += calc(a[i]) + calc(a[i+1]-1); } } ans.writeln; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}} void main() { int n; readV(n); string[] s; readM(n, s); auto t = ['M', 'A', 'R', 'C', 'H']; auto h = new long[](5); foreach (si; s) { auto i = t.countUntil(si[0]); if (i >= 0) h[i]++; } auto calc(int a, int b, int c) { return h[a].to!long * h[b] * h[c]; } auto r = calc(0, 1, 2) + calc(0, 1, 3) + calc(0, 1, 4) + calc(0, 2, 3) + calc(0, 2, 4) + calc(0, 3, 4) + calc(1, 2, 3) + calc(1, 2, 4) + calc(1, 3, 4) + calc(2, 3, 4); writeln(r); }
D
import std.stdio, std.conv, std.string, std.math, std.regex, std.array; void main(){ auto ip = readln.chomp.to!(char[]); replace(ip, ",", " ").writeln; }
D
import std.stdio; import std.string; import std.conv; void main() { int N = readln.chomp.to!int; int H = readln.chomp.to!int; int W = readln.chomp.to!int; int result = (N - H + 1) * (N - W + 1); writeln(result); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto x = readln.chomp.split.to!(int[]); if (x[0] * x[1] % 2) { writeln("Odd"); } else { writeln("Even"); } }
D
import std.stdio; import std.ascii; import std.conv; import std.string; import std.algorithm; import std.range; import std.functional; import std.math; import core.bitop; void main() { auto N = readln.chomp.to!long; long result; foreach (n; 1 .. N + 1) { long cnt; if (n % 2 == 0) { continue; } foreach (k; 1 .. n + 1) if (n % k == 0) { cnt++; } if (cnt == 8) { result++; } } result.writeln; }
D
import std.algorithm; import std.array; import std.bigint; import std.bitmanip; import std.conv; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; // 差の絶対値 @nogc @safe pure T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; } // 切り上げ除算 @nogc @safe pure T divCeil(T)(const ref T a, const ref T b) { return (a+b-1)/b; } T[] readToArray(T)() { return readln.split.to!(T[]); } void readInto(T...)(ref T ts) { auto ss = readln.split; foreach(ref t; ts) { t = ss.front.to!(typeof(t)); ss.popFront; } } // 冪乗をmod取りつつ計算 @nogc @safe pure ulong modPow(ulong a, ulong n, ulong m) { ulong r = 1; while (n > 0) { if(n % 2 != 0) r = r * a % m; a = a * a % m; n /= 2; } return r; } // フェルマーの小定理から乗法逆元を計算 // 定理の要請により法は素数 @nogc @safe pure ulong modInv(ulong a, ulong m) { return modPow(a, m-2, m); } // mod取りつつ順列を計算 @nogc @safe pure ulong modPerm(ulong n, ulong k, ulong m) { if (n < k) return 0; ulong r = 1; for (ulong i = n-k+1; i <= n; i++) { r *= i; r %= m; } return r; } // mod取りつつ順列を計算 @nogc @safe pure ulong modFact(ulong n, ulong m) { return modPerm(n, n, m); } // mod取りつつ組み合わせを計算 // modInvを使っているので法は素数 @nogc @safe pure ulong modComb(ulong n, ulong r, ulong m) { return modPerm(n, r, m)*modInv(modFact(r, m), m) % m; } immutable ulong MOD = 1000000007; void main() { ulong a; string s; readInto(a); readInto(s); if (a < 3200) writeln("red"); else writeln(s); }
D
import std.stdio; import std.algorithm; import std.conv; import std.string; import std.array; import std.math; void main(){ auto input = readln().chomp.split.map!(to!int); if(input[1] % input[0] == 0){ writeln(input[0] + input[1]); }else{ writeln(input[1] - input[0]); } }
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 L, R; scan(L, R); long ans = long.max; foreach (i; L .. min(R + 1, L + 5000)) { foreach (j; i + 1 .. min(R + 1, i + 5000)) { ans = ans.min(i * j % 2019); } } 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.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 S = RD!string; bool good = true; foreach (i; 1..4) { if (S[i] == S[i-1]) good = false; } writeln(good ? "Good" : "Bad"); stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } void main(){ long[] as; foreach(i; 0 .. 6) as ~= read.to!long; // 同じ頂点が3回出ていたら不可 long[long] cnt; foreach(a; as) if(a !in cnt) cnt[a] = 1; else cnt[a] += 1; long max = 0; foreach(c; cnt) if(c > max) max = c; if(max == 3) writeln("NO"); else writeln("YES"); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n, q; rd(n, q); int s=0; while(q--){ int t, k; rd(t, k); if(t){ (s+=k)%=n; }else{ writeln(((k-1)+s)%n+1); } } } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x) e=l[i].to!(typeof(e)); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); auto S = sread(); long ans; foreach (i; 0 .. N - 2) if (S[i .. i + 3] == "ABC") { ans++; } writeln(ans); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; void main() { while (1) { auto n = readln.chomp.to!int; if (!n) break; auto sum = readln.chomp.split.map!(to!int).reduce!("a+b"); writeln(sum / (n-1)); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto S = readln.chomp.to!(char[]); int[] N; N.length = S.length; int x; foreach (i, c; S) { if (c == 'R') { ++x; } else { N[i-1] += (x+1)/2; N[i] += x/2; x = 0; } } x = 0; foreach_reverse (i, c; S) { if (c == 'L') { ++x; } else { N[i+1] += (x+1)/2; N[i] += x/2; x = 0; } } writeln(N.to!(string[]).join(" ")); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto N = RD; auto cnt = new long[](N); foreach (x; 1..N+1) { auto x2 = x^^2; if (x2 > N) break; foreach (y; 1..N+1) { auto y2 = y^^2; if (x2 + y2 + x*y > N) break; foreach (z; 1..N) { auto z2 = z^^2; auto a = x2 + y2 + z2 + x*y + y*z + z*x; if (a > N) break; ++cnt[a-1]; } } } foreach (i; 0..N) { writeln(cnt[i]); } stdout.flush; debug readln; }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.numeric; void main() { auto n = readln.chomp.to!int; //auto s = new string[](n); int ab, a, b; int res; foreach (i; 0..n) { immutable s = readln.chomp; res += s.count("AB"); if (s[0] == 'B' && s[$-1] == 'A') { ab++; } else if (s[0] == 'B') { b++; } else if (s[$-1] == 'A') { a++; } } if (ab > 0) { res += ab - 1; if (b > 0) { res++; b--; } if (a > 0) { res++; a--; } } res += min(a, b); res.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() { int n, m; scan(n, m); auto adj = new int[][](n, 0); auto iri = new int[](n); auto de = new int[](n); foreach (i ; 0 .. n - 1 + m) { int ai, bi; scan(ai, bi); ai--, bi--; adj[ai] ~= bi; de[ai]++; iri[bi]++; } int root = -1; foreach (i ; 0 .. n) { if (iri[i] == 0) { root = i; break; } } debug { writeln("iri", iri); writeln("de", de); } auto p = new int[](n); p[] = -1; void dfs(int v, int par) { if (p[v] == -1) p[v] = par + 1; foreach (u ; adj[v]) { if (u == par) continue; iri[u]--; } foreach (u ; adj[v]) { if (u == par) continue; if (p[u] != -1) continue; debug { writefln("v: %d, u:%d, iri: %d", v, u, iri[u]); } if (iri[u] == 0) { dfs(u, v); } } } dfs(root, -1); foreach (i ; 0 .. n) { writeln(p[i]); } } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.algorithm; import std.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() { int n = readint; if (n == 3 || n == 5 || n == 7) writeln("YES"); else writeln("NO"); }
D
import std.algorithm; import std.array; import std.conv; import std.stdio; import std.string; void main(){ stdin.readln.chomp.filter!(a => a != ',' && a != '.').array.split.filter!(a => a.length > 2 && a.length < 7).reduce!((a, b) => a ~ " " ~ b).writeln; }
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; import std.bigint; import core.checkedint; import core.bitop; void main() { int a, b; readln.chomp.split.tie(a, b); int ite = 0; for (int i = 1; i <= 999; ++i) { if (b - a == i && ite - a > 0) { debug verbose(i, ite); writeln = ite - a; break; } ite += i; } } 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
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { long N; scan(N); if (N % 2) { writeln(0); return; } long d = 10; long ans; while (d <= N) { ans += N / d; d *= 5; } writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import 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() { auto s = sread(); long result = long.max; foreach (i; 0 .. s.length - 2) { long n = s[i .. i + 3].to!long(); // writeln(" ", n); result.minAssign(abs(n - 753)); } writeln(result); }
D
import std.stdio; void main() { auto r1 = readln(); auto r2 = readln(); auto r3 = readln(); write(r1[0], r2[1], r3[2]); }
D
// import chie template :) {{{ import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons; // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string : chomp; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.chomp.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 main() { auto cin = new Scanner; while (true) { int n, p; cin.scan(n, p); if (n == 0 && p == 0) break; int[] ar = new int[n]; int sum; for (int i = 0; i < n; i = (i + 1) % n) { if (p == 0) { p = ar[i]; sum -= ar[i]; ar[i] = 0; } else { ++sum; ++ar[i]; --p; if (p == 0 && sum == ar[i]) { writeln(i); break; } } } } }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int a, b; rd(a, b); for(int c=1; c<=3; c++){ if((a*b*c)&1){ writeln("Yes"); return; } } writeln("No"); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x) e=l[i].to!(typeof(e)); }
D
void main() { string n = readln.chomp; n.count('2').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.string; import std.conv; import std.algorithm; import std.stdio; void main() { int hp = readln.chomp.split(" ")[0].to!int; foreach (e;readln.chomp.split(" ").map!(to!int)) { hp -= e; if (hp <= 0) { writeln("Yes"); return; } } writeln("No"); }
D
import std.stdio; import std.conv; import std.string; void main() { string s = readln.chomp; int w = readln.chomp.to!int; for (int i = 0; i < s.length; i++) { if (!(i % w)) write(s[i]); } }
D
import std.stdio; void main() { for (int i = 0; i < 1000; i++) { writeln("Hello World"); } }
D
import std; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); long[string] d; foreach (i; 0 .. N) { d[sread()] = 0; } writeln(d.keys.length); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; import std.math; void main() { int n; scan(n); auto s = readln.split; foreach (ch ; s) { if (ch == "Y") { writeln("Four"); return; } } writeln("Three"); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.conv, std.string, std.algorithm, std.math, std.array, std.container, std.typecons; long mod = 1000000007; long modinv(long a) { long b=mod, u=1, v=0; while(b>0) { long t=a/b; a -= t*b; swap(a,b); u -= t*v; swap(u,v); } u %= mod; if(u<0) u+=mod; return u; } void main() { long x = readln.chomp.to!long; long n = 100; int count = 0; while(n<x) { n = n + n/100; count++; } writeln(count); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.numeric; 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; } void main() { string s; while ((s = readln.chomp) != null) { auto ab = s.split.map!(to!int).array; int a = ab[0], b = ab[1]; writeln(gcd(a, b)); } }
D
import std.stdio; import std.algorithm; import std.conv; import std.array; import std.string; import std.math; void main(string[] args) { readln.chomp.solve.writeln; } auto solve(string input) { return input.count("1"); } unittest { assert(solve("101") == 2); assert(solve("000") == 0); assert(solve("0001") == 1); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void main() { auto N = readln.chomp.to!int; int c; bool ok; foreach (_; 0..N) { auto ab = readln.split; if (ab[0] == ab[1]) { ++c; if (c >= 3) ok = true; } else { c = 0; } } writeln(ok ? "Yes" : "No"); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; import std.bitmanip; // BitArray void main() { auto n = readln.chomp.to!size_t; auto s = new int[](n); foreach (i; 0..n) s[i] = readln.chomp.to!int; auto t = s.sum; auto dp = BitArray(); dp.length = t+1; dp[0] = true; foreach (i; 0..n+1) { auto dp2 = dp.dup; dp2.lshift(s[i]); dp |= dp2; } foreach_reverse (i; 0..t+1) if (dp[i] && i % 10 != 0) { writeln(i); return; } writeln(0); } auto lshift(ref BitArray ba, size_t n) { if (n % 64 == 0) { if (n > 0) { ba <<= 1; ba <<= n-1; } } else { ba <<= n; } }
D