code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio, std.conv, std.string, std.range, std.typecons, std.format; import std.algorithm.searching, std.algorithm.sorting, std.algorithm.iteration, std.algorithm.mutation; void main() { string input = readln().strip(); foreach(c; input) { write(c=='1'?'9':'1'); } writeln(""); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n; long a, b; readV(n, a, b); long[] x; readA(n, x); auto ans = 0L; foreach (i; 1..n) ans += min((x[i]-x[i-1])*a, b); writeln(ans); }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; import std.typecons; int solve (string s) { auto n = s.length.to !(int); s ~= ".?!"; auto f = new int [3] [n + 4]; f[1][1] = 1; f[2][2] = 1; foreach (i; 0..n) { foreach (j; 1..3) { if (f[i][j]) { f[i + 3 - j][3 - j] = max (f[i + 3 - j][3 - j], f[i][j] + 1); if (s[i - j..i] != s[i..i + j]) { f[i + j][j] = max (f[i + j][j], f[i][j] + 1); } } } } return max (f[n][1], f[n][2]); } void main () { string s; while ((s = readln.strip) != "") { writeln (solve (s)); } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.bitmanip; void main() { immutable n = readln.strip.to!int; auto s = readln.strip; BitArray m1, m2, m3; m1.length = 10; m2.length = 100; m3.length = 1000; debug stderr.writeln (s); foreach (i; 0 .. n) { int x = s[i].to!int - 48; foreach (k; m2.bitsSet ()) { m3[10 * k + x] = true; } foreach (k; m1.bitsSet ()) { m2[10 * k + x] = true; } m1[x] = true; } int c; foreach (k; m3.bitsSet()) ++c; writeln (c); }
D
import std.algorithm; import std.array; import std.stdio; import std.numeric; import std.conv; void main(){ auto input = readln.split.map!(to!int); gcd(input[0], input[1]).writeln; }
D
import std.stdio; import std.algorithm; import std.conv; import std.numeric; import std.math; import std.string; void main() { auto tokens = split(chomp(readln())); auto N = to!ulong(tokens[0]); if (N == 1) writeln("Hello World"); else { auto a = to!int(chomp(readln())); auto b = to!int(chomp(readln())); writeln(a + b); } stdout.flush(); }
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 t = Tuple!(string, "name", long, "y"); void main() { auto x = lread(); auto rest = x % 100; foreach (a; iota(100 / 1)) { foreach (b; iota(100 / 2)) { foreach (c; iota(100 / 3)) { foreach (d; iota(100 / 4)) { foreach (e; iota(100 / 5)) { if((a + 2 * b + 3 * c + 4 * d + 5 * e) == rest) { auto s = a + b + c + d + e; if(s * 100 <= x - rest) { writeln(1); return; } } } } } } } writeln(0); } T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.stdio, 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; const long MOD = 10^^9 + 7; const long INF = 1L << 59; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto Q = new Tuple!(int, int, long)[](M); auto G = new Tuple!(int, long)[][](N); auto uf = new UnionFind(N); foreach (i; 0..M) { s = readln.split.map!(to!int); auto L = s[0] - 1; auto R = s[1] - 1; auto D = s[2].to!long; uf.unite(L, R); G[L] ~= tuple(R, -D); G[R] ~= tuple(L, D); } auto ans = new long[](N); ans.fill(INF); foreach (i; 0..N) { if (uf.table[i] < 0) { ans[i] = 0; } } auto used = new bool[](N); bool dfs(int n, long d) { if (ans[n] != INF && d != ans[n]) return false; if (used[n]) return true; used[n] = true; ans[n] = d; foreach (m; G[n]) { if (!dfs(m[0], d+m[1])) return false; } return true; } foreach (i; 0..N) { if (uf.table[i] >= 0) continue; if (!dfs(i, 0)) { writeln("No"); return; } } "Yes".writeln; } class UnionFind { int N; int[] table; this(int n) { N = n; table = new int[](N); fill(table, -1); } int find(int x) { return table[x] < 0 ? x : (table[x] = find(table[x])); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (table[x] > table[y]) swap(x, y); table[x] += table[y]; table[y] = x; } }
D
import std.stdio, std.string, std.conv; import std.algorithm, std.array; auto solve(string s_) { immutable N = s_.to!int(); auto ab = new long[2][N]; foreach(ref v;ab) v = readln.split.map!(to!long).array(); long c=0; foreach_reverse(ref v;ab) { immutable a=v[0], b=v[1]; c+=(b-(a+c)%b)%b; } return c; } void main(){ for(string s; (s=readln.chomp()).length;) writeln(solve(s)); }
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; int ans = 100_000; for (int i = 0; i < n; i++) { ans = ans * 105 / 100; if (ans % 1000 > 0) { ans += 1000 - (ans % 1000); } } 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; } 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 M = RD; auto cnt = new long[](N); foreach (i; 0..M) { auto a = RD-1; auto b = RD-1; ++cnt[a]; ++cnt[b]; } bool ans = true; foreach (i; 0..N) { if (cnt[i] % 2 == 1) { ans = false; break; } } writeln(ans ? "YES" : "NO"); stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 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 X = RD; auto M = RD; auto pos = new long[](M); pos[] = -1; auto a = X; pos[a] = 0; auto cnt = new long[](M); cnt[0] = a; long ans = a; foreach (i; 1..min(N, M+1)) { auto last = cnt[a]; a = (a*a) % M; if (pos[a] == -1) { pos[a] = i; cnt[a] = last + a; ans += a; } else { auto len = i - pos[a]; auto loopCnt = last - cnt[a] + a; auto rem = N - i; auto loop = rem / len; auto loopRem = rem % len; ans += loop * loopCnt; foreach (j; 0..loopRem) { ans += a; a = (a*a) % M; } break; } } writeln(ans); stdout.flush; debug readln; }
D
import std.string, std.stdio, std.algorithm; void main() { char[] s = readln.chomp.dup.reverse; writeln(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; import std.numeric; void main() { auto x = readln.chomp.to!int; int sum; auto i = 1; while (sum < x) { sum += i; i++; } (i-1).writeln; }
D
void main(){ string s = readln().chomp(); if(s.count('a') == 1 && s.count('b') == 1 && s.count('c') == 1){ writeln("Yes"); }else{ writeln("No"); } } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range; // 1要素のみの入力 T inelm(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] inln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split())ln ~= elm.to!T(); return ln; }
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() { writeln(48 - lread()); }
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() { long a, b; scan(a, b); writeln(a / gcd(a, b) * b); } 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.algorithm; import std.conv; import std.stdio; import std.string; void main() { string[] b_type = [ "A", "B", "AB", "O" ]; int[string] b_counter; foreach (b; b_type) { b_counter[b] = 0; } foreach (string line; stdin.lines) { string b = line.chomp.split(",")[1]; b_counter[b]++; } foreach (b; b_type) { b_counter[b].writeln; } }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; import std.bitmanip; // BitArray void main() { auto rd = readln.split.to!(int[]), n = rd[0], k = rd[1]; auto a = readln.split.to!(int[]); auto dp1 = new BitArray[](n+1), dp2 = new BitArray[](n+1); dp1[0].length = dp2[0].length = k; dp1[0][0] = dp2[0][0] = true; foreach (i; 0..n) { dp1[i+1] = dp1[i].dup; dp1[i+1].lshift(a[i]); dp1[i+1] |= dp1[i]; dp2[i+1] = dp2[i].dup; dp2[i+1].lshift(a[$-i-1]); dp2[i+1] |= dp2[i]; } auto check(int ai, BitArray dpi1, BitArray dpi2) { if (ai >= k) return true; auto mi = k-ai, cs = new int[](k); cs[0] = dpi2[0]; foreach (j; 1..k) cs[j] = cs[j-1] + dpi2[j]; foreach (j; 0..k) { auto mj = max(0, mi-j), c = cs[k-j-1] - cs[mj] + dpi2[mj]; if (dpi1[j] & (c > 0)) return true; } return false; } auto ans = 0; foreach (i; 0..n) { auto dpi1 = dp1[i], dpi2 = dp2[n-i-1]; ans += check(a[i], dpi1, dpi2); } writeln(n-ans); } auto lshift(ref BitArray ba, size_t n) { if (n % 64 == 0) { if (n > 0) { ba <<= 1; ba <<= n-1; } } else { ba <<= n; } }
D
import std.stdio, std.string, std.conv, std.bigint, std.typecons, std.algorithm, std.array, std.math, std.range; void main() { auto N = readln.chomp.to!int; writeln(N / 10 == 9 || N % 10 == 9 ? "Yes" : "No"); }
D
import std.stdio,std.string,std.array,std.algorithm,std.range,std.conv; int main(){ auto s=readln.chomp; auto n=s.length; if(s.equal(s.retro)){ auto s1=s[0..(n-1)/2]; if(s1.equal(s1.retro)){ auto s2=s[(n+3)/2-1..$]; if(s2.equal(s2.retro)){ "Yes".writeln; return 0; } } } "No".writeln; return 0; }
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);} const mod = 10^^9+7; void main() { int n; readV(n); auto ans = 1L; foreach (i; 1..n+1) (ans *= i) %= mod; writeln(ans); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { // 日本語文字列のテスト int w = readint; int n = readint; auto xs = new int[w + 1]; for (int i = 0; i < xs.length; i++) xs[i] = i; for (int i = 0; i < n; i++) { auto ab = readln.chomp.split(",").to!(int[]); int a = ab[0], b = ab[1]; swap(xs[a], xs[b]); } foreach (x; xs[1 .. $]) writeln(x); }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional; import std.algorithm, std.container; void main() { readln(); long[] list = scanArray; long sum; long res; long l; foreach(r; 0..list.length) { while((sum&list[r])!=0) { sum ^= list[l]; l++; } sum |= list[r]; res+=r-l+1; } writeln(res); } 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, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto T = readln.chomp.to!int; foreach (_t; 0..T) { auto N = readln.chomp.to!long; long a = 2L^^N, b; foreach (i; 1..N/2) { a += 2L^^i; } foreach (i; N/2..N) { b += 2L^^i; } writeln(a - b); } }
D
import std.functional, std.algorithm, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv; void main() { ulong N = readln.chomp.to!ulong; ulong[] a = readln.chomp.split.to!(ulong[]); ulong[ulong] h; foreach (e; a) { if (e in h) { h[e]++; } else { h[e] = 1; } } ulong ans; foreach (k, v; h) { if (k < v) { ans += v - k; } else if (k > v) { ans += v; } } writeln(ans); }
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_007; void main() { int a, b; scan(a, b); if ((a + b) % 2) { writeln("IMPOSSIBLE"); return; } int ans = (a + b) / 2; 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.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) { static if (isSomeChar!(ElementType!Arg)) { args[0] = readln.chomp.to!Arg; } else { 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])); } } } } void main() { int T; get(T); while (T--) { int n, k; get(n, k); int[] hs; get(hs); int l = hs[0]; foreach (i, h; hs[1..$-1]) { if (l + k <= h || l - k + 1 > h + k - 1) goto ng; if (hs[i+2] >= h) { l = min(l + k - 1, h + k - 1); } else { l = max(l - k + 1, h); } } if (abs(hs[$-1] - l) >= k) goto ng; writeln("YES"); continue; ng: writeln("NO"); } }
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; import std.typecons, std.regex; void main() { auto s = read.to!string; writeln(s[0..$-8]); } string read() { static string[] ss; while (!ss.length) ss = readln.chomp.split; auto res = ss[0]; ss.popFront; return res; }
D
import std.stdio; import std.string; import std.conv; void main() { int times = readln.chomp.to!(int); foreach(t; 0..times) { dchar[] line = readln.chomp.dup.to!(dchar[]); if ( line.length >= 6) { for(uint i = 0; i < line.length - 6; i++) { if (line[i..(i + 7)] == "Hoshino") { line[i..(i + 7)] = "Hoshina"; } } } writeln(line); } }
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() { int H, A; scan(H, A); int ans = (H + A - 1) / A; writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = readln.chomp.to!size_t; auto p = readln.split.to!(int[]); auto r = 0; foreach (i; 0..n-1) { if (i+1 == p[i]) { swap(p[i], p[i+1]); ++r; } } if (p[n-1] == n) ++r; writeln(r); }
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 A, B; scan(A, B); bool b = (A % 3) == 0 || (B % 3) == 0 || ((A + B) % 3) == 0; writeln(b ? "Possible" : "Impossible"); }
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; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { long[] arr; arr ~= RD; arr ~= RD; arr ~= RD; arr ~= RD; arr ~= RD; auto K = RD; bool ans = true; foreach (i; 0..5) { foreach (j; i+1..5) { if (abs(arr[i] - arr[j]) > K) ans = false; } } writeln(ans ? "Yay!" : ":("); stdout.flush(); debug readln(); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; import std.range; void main(){ auto N=readln.chomp.to!int; auto A=readln.chomp.to!int; if(N%500<=A)writeln("Yes"); else if(N%500>A)writeln("No"); }
D
import std.stdio, std.string, std.conv, std.array, std.algorithm; void main() { auto q = readln.chomp.to!int; for(int i=0;i<q;++i){ auto b = readln.split.map!(to!int); int c=b[0],a=b[1],n=b[2]; int ans=0; if(a<n){ ans+=min(c,a); c=max(0,c-ans); a=max(0,a-ans); n=max(0,n-ans); }else{ ans+=min(c,n); c=max(0,c-ans); a=max(0,a-ans); n=max(0,n-ans); } if(a>0){ int x = min(c/2,a); c=max(0,c-2*x); a=max(0,a-x); ans+=x; } ans+=c/3; ans.writeln(); } }
D
void main(){ string s = readln().chomp(); string t = readln().chomp(); if( s==t[0..s.length] && t[s.length..$].length==1 ){ writeln("Yes"); }else{ writeln("No"); } } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { auto S = readln.chomp; auto T = readln.chomp; char[char] c0, c1; foreach (i; 0..S.length) { const s = S[i]; const t = T[i]; if (s !in c0 && t !in c1) { c0[s] = t; c1[t] = s; } if (s in c0 && c0[s] != t) { writeln("No"); return; } if (t in c1 && c1[t] != s) { writeln("No"); return; } } writeln("Yes"); }
D
import std.stdio, std.range, std.conv, std.string; import std.algorithm.comparison, std.algorithm.iteration, std.algorithm.mutation, std.algorithm.searching, std.algorithm.setops, std.algorithm.sorting; void main() { int[] input = readln().split.to!(int[]); int H = input[0]; int W = input[1]; input = readln().split.to!(int[]); int h = input[0]; int w = input[1]; writeln((H-h)*(W-w)); }
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() { /*foreach (n; 2..10) { auto a = new int[][](21, 21); long[][] q = [[9, 10, 0, n-1], [11, 10, 0, n-1], [10, 9, 1, n-1], [10, 11, 1, n-1]]; while (!q.empty) { auto nd = q.front; q.popFront; auto y = nd[0]; auto x = nd[1]; auto dir = nd[2]; auto rem = nd[3]; if (rem == 0) { a[y][x] = 1; continue; } if (dir == 0) { q ~= [y, x-1, 1, rem-1]; q ~= [y, x+1, 1, rem-1]; } else { q ~= [y-1, x, 0, rem-1]; q ~= [y+1, x, 0, rem-1]; } } long cnt; foreach (y; 0..21) { writeln(a[y]); foreach (x; 0..21) { if (a[y][x]) ++cnt; } } writeln("cnt:", cnt); }*/ auto t = 1;//RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto m = (n+1) / 2 * 2 + 1; if (n % 2) { ans[ti] = (m^^2) / 2; } else { ans[ti] = ((m+1) / 2)^^2; } } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
import core.stdc.stdio; import std.algorithm; void main(){ int s,t; scanf("%d%d",&s,&t); char[] x = new char[s+1],y=new char[t+1]; scanf("%s%s",x.ptr,y.ptr); int[][][] dp = new int[][][](2,s+1,t+1); foreach(d;dp[0]) d[]=-1145141919; foreach(i;0..s+1) foreach(j;0..t+1){ if(i<s) if(x[i]=='I') dp[0][i+1][j]=max(dp[0][i+1][j],dp[1][i][j]+1); else dp[1][i+1][j]=max(dp[1][i+1][j],dp[0][i][j]+1); if(j<t) if(y[j]=='I') dp[0][i][j+1]=max(dp[0][i][j+1],dp[1][i][j]+1); else dp[1][i][j+1]=max(dp[1][i][j+1],dp[0][i][j]+1); } int ans=0; foreach(ds;dp[0]) foreach(d;ds) ans=max(ans,d); printf("%d\n",ans); }
D
/+ dub.sdl: name "D" dependency "dunkelheit" version="1.0.1" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner, dkh.numeric.prime; int main() { Scanner sc = new Scanner(stdin); scope(exit) assert(!sc.hasNext); debug { foreach (n; 3..1001) { bool ok = false; foreach (m; n..(n + n / 4 * 2) + 1) { if (isPrime(m)) { ok = true; writefln("find %d : %d", n, m); stdout.flush; break; } } if (!ok) { writeln("ERROR ", n); return 0; } } } int n; sc.read(n); foreach (m; n..(n + n / 4 * 2) + 1) { if (isPrime(m)) { int[2][] edges; foreach (i; 0..n) { edges ~= [i, (i + 1) % n]; } foreach (i; 0..n) { if (i % 4 >= 2) continue; if (edges.length.to!int >= m) break; edges ~= [i, (i + 2) % n]; } writeln(m); foreach (d; edges) { writeln(d[0] + 1, " ", d[1] + 1); } return 0; } } return 0; } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/scanner.d */ // module dkh.scanner; // import dkh.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; private File f; this(File f) { this.f = f; } private char[512] lineBuf; private 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) { assert(succW()); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(Args...)(auto ref Args args) { import std.exception : enforce; static if (args.length != 0) { enforce(readSingle(args[0])); read(args[1..$]); } } bool hasNext() { return succ(); } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/numeric/prime.d */ // module dkh.numeric.prime; import std.traits; // import dkh.int128; ulong ulongPowMod(U)(ulong x, U n, ulong md) if (isIntegral!U || is(U == BigInt)) { x %= md; ulong r = 1; while (n) { if (n & 1) { r = mul128(r, x).mod128(md); } x = mul128(x, x).mod128(md); n >>= 1; } return r % md; } T[] divisorList(T)(T x) { import std.algorithm : sort; T[] res; for (T i = 1; i*i <= x; i++) { if (x%i == 0) { res ~= i; if (i*i != x) res ~= x/i; } } sort(res); return res; } T[] factorList(T)(T x) { T[] res; for (T i = 2; i*i <= x; i++) { while (x % i == 0) { res ~= i; x /= i; } } if (x > 1) res ~= x; return res; } // import dkh.numeric.primitive; bool isPrime(ulong n) { // import dkh.int128; if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; ulong d = n-1; while (d % 2 == 0) d /= 2; ulong[] alist = [2,3,5,7,11,13,17,19,23,29,31,37]; foreach (a; alist) { if (n <= a) break; ulong y = ulongPowMod(a, d, n); ulong t = d; while (t != n-1 && y != 1 && y != n-1) { y = mul128(y, y).mod128(n); t <<= 1; } if (y != n-1 && t % 2 == 0) { return false; } } return true; } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/numeric/primitive.d */ // module dkh.numeric.primitive; import std.traits, std.bigint; T lcm(T)(in T a, in T b) { import std.numeric : gcd; return a / gcd(a,b) * b; } Unqual!T pow(T, U)(T x, U n) if (!isFloatingPoint!T && (isIntegral!U || is(U == BigInt))) { return pow(x, n, T(1)); } Unqual!T pow(T, U, V)(T x, U n, V e) if ((isIntegral!U || is(U == BigInt)) && is(Unqual!T == Unqual!V)) { Unqual!T b = x, v = e; Unqual!U m = n; while (m) { if (m & 1) v *= b; b *= b; m /= 2; } return v; } T powMod(T, U, V)(T x, U n, V md) if (isIntegral!U || is(U == BigInt)) { T r = T(1); Unqual!U m = n; while (m) { if (m & 1) r = (r*x)%md; x = (x*x)%md; m >>= 1; } return r % md; } T[3] extGcd(T)(in T a, in T b) if (!isIntegral!T || isSigned!T) { if (b==0) { return [T(1), T(0), a]; } else { auto e = extGcd(b, a%b); return [e[1], e[0]-a/b*e[1], e[2]]; } } T invMod(T)(T x, T md) { auto r = extGcd!T(x, md); assert(r[2] == 1); auto z = r[0]; return (z % md + md) % md; } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/ldc/inline.d */ // module dkh.ldc.inline; version(LDC) { pragma(LDC_inline_ir) R inlineIR(string s, R, P...)(P); } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/int128.d */ // module dkh.int128; version(LDC) { // import dkh.ldc.inline; } version(LDC) version(X86_64) { version = LDC_IR; } ulong[2] mul128(ulong a, ulong b) { ulong[2] res; version(LDC_IR) { ulong upper, lower; inlineIR!(` %r0 = zext i64 %0 to i128 %r1 = zext i64 %1 to i128 %r2 = mul i128 %r1, %r0 %r3 = trunc i128 %r2 to i64 %r4 = lshr i128 %r2, 64 %r5 = trunc i128 %r4 to i64 store i64 %r3, i64* %2 store i64 %r5, i64* %3`, void)(a, b, &lower, &upper); return [lower, upper]; } else version(D_InlineAsm_X86_64) { ulong upper, lower; asm { mov RAX, a; mul b; mov lower, RAX; mov upper, RDX; } return [lower, upper]; } else { ulong B = 2UL^^32; ulong[2] a2 = [a % B, a / B]; ulong[2] b2 = [b % B, b / B]; ulong[4] c; foreach (i; 0..2) { foreach (j; 0..2) { c[i+j] += a2[i] * b2[j] % B; c[i+j+1] += a2[i] * b2[j] / B; } } foreach (i; 0..3) { c[i+1] += c[i] / B; c[i] %= B; } return [c[0] + c[1] * B, c[2] + c[3] * B]; } } ulong div128(ulong[2] a, ulong b) { version(LDC_IR) { return inlineIR!(` %r0 = zext i64 %0 to i128 %r1 = zext i64 %1 to i128 %r2 = shl i128 %r1, 64 %r3 = add i128 %r0, %r2 %r4 = zext i64 %2 to i128 %r5 = udiv i128 %r3, %r4 %r6 = trunc i128 %r5 to i64 ret i64 %r6`,ulong)(a[0], a[1], b); } else version(D_InlineAsm_X86_64) { ulong upper = a[1], lower = a[0]; ulong res; asm { mov RDX, upper; mov RAX, lower; div b; mov res, RAX; } return res; } else { if (b == 1) return a[0]; while (!(b & (1UL << 63))) { a[1] <<= 1; if (a[0] & (1UL << 63)) a[1] |= 1; a[0] <<= 1; b <<= 1; } ulong ans = 0; foreach (i; 0..64) { bool up = (a[1] & (1UL << 63)) != 0; a[1] <<= 1; if (a[0] & (1UL << 63)) a[1] |= 1; a[0] <<= 1; ans <<= 1; if (up || b <= a[1]) { a[1] -= b; ans++; } } return ans; } } ulong mod128(ulong[2] a, ulong b) { version(D_InlineAsm_X86_64) { ulong upper = a[1], lower = a[0]; ulong res; asm { mov RDX, upper; mov RAX, lower; div b; mov res, RDX; } return res; } else { return a[0] - div128(a, b) * b; } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std.stdio; void main() { string[] weather = ['S' : "Cloudy", 'C' : "Rainy", 'R' : "Sunny"]; weather[readln[0]].writeln; }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto s = readln.chomp.to!(char[]); auto bf = new int[](26), af = new int[](26); int kind = 0; foreach (c; s) { af[c - 'a']++; if (af[c - 'a'] == 1) { kind++; } } int ans = 0, mod = 998244353; for (int i = 0, j = 0; i < n; i++) { // [i, j) remove while (j < n && kind >= 2) { if ((--af[s[j++] - 'a']) == 0) { kind--; } } if (kind == 1) { ans += (n - j + 1); ans %= mod; } if ((++af[s[i] - 'a']) == 1) { kind++; } } writeln(ans); } 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.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; enum P = 10L^^9+7; void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; auto ab = new bool[][](2, N*M); foreach (a; readln.split.to!(int[])) { if (ab[0][a-1]) { writeln(0); return; } ab[0][a-1] = true; } foreach (b; readln.split.to!(int[])) { if (ab[1][b-1]) { writeln(0); return; } ab[1][b-1] = true; } long n, m, b, r = 1; foreach_reverse (i; 0..N*M) { if (ab[0][i] && ab[1][i]) { n += 1; m += 1; } else if (ab[0][i]) { n += 1; (r *= m) %= P; } else if (ab[1][i]) { m += 1; (r *= n) %= P; } else { (r *= (m*n-b)) %= P; } b += 1; } writeln(r); }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random; import std.typecons, std.functional, std.traits,std.concurrency; import std.algorithm, std.container; import core.bitop, core.time, core.memory; import std.bitmanip; import std.regex; enum INF = long.max/3; enum MOD = 10L^^9+7; //辞書順順列はiota(1,N),nextPermituionを使う void end(T)(T v) if(isIntegral!T||isSomeString!T||isSomeChar!T) { import core.stdc.stdlib; writeln(v); exit(0); } T[] scanArray(T = long)() { static char[] scanBuf; readln(scanBuf); return scanBuf.split.to!(T[]); } dchar scanChar() { int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } return cast(dchar)c; } T scanElem(T = long)() { import core.stdc.stdlib; static auto scanBuf = appender!(char[])([]); scanBuf.clear; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { scanBuf ~= cast(char) c; c = getchar; } return scanBuf.data.to!T; } dchar[] scanString(){ return scanElem!(dchar[]); } void main() { auto s = scanString; if(s[0]==s[2])end("Yes");end("No"); }
D
void main() { long A, B, m; rdVals(A, B, m); long[] a = rdRow; long[] b = rdRow; long result = a.reduce!min + b.reduce!min; foreach (i; 0 .. m) { long x, y, c; rdVals(x, y, c); --x, --y; result = min(result, a[x]+b[y]-c); } result.writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio; 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]; } const real eps = 1e-10; void main(){ auto l = "qwertasdfgzxcvb"d; while(true) { auto s = readln().chomp(); if(s == "#") { return; } int ans; s.map!(c => !l.find(c).empty).reduce!((a, b) {if(a != b) ++ans; return b;}); writeln(ans); } }
D
import std.stdio, std.algorithm; void main() { enum cs = "MARCH"; size_t[char] d; foreach (c; cs) { d[c] = 0; } foreach (s; stdin.byLine) { if (s[0] in d) { ++d[s[0]]; } } size_t ret = 0; foreach (i, c0; cs[0..$-2]) { if (d[c0] == 0) continue; foreach (j, c1; cs[i+1..$-1]) { if (d[c1] == 0) continue; foreach (k, c2; cs[i+1+j+1..$]) { if (d[c2] == 0) continue; ret += d[c0] * d[c1] * d[c2]; } } } writeln(ret); }
D
void main() { long[] tmp = readln.split.to!(long[]); long a = tmp[0], b = tmp[1], c = tmp[2]; writeln(a + b >= c - 1 ? b + c : 2 * b + a + 1); } 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 : writeln; void main() { int a,b,c,d; scan(a,b,c,d); if (a+b > c+d) { writeln("Left"); } else if (a+b < c+d) { writeln("Right"); } else { writeln("Balanced"); } } void scan(T...)(ref T args) { import std.stdio : readln; import std.array : split; import std.conv : to; import std.range.primitives; string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.conv, std.string, std.algorithm; void main() { writeln(readln.split.to!(uint[]).reduce!"a*b" % 2 == 0 ? "Even" : "Odd"); }
D
unittest { assert( [ "xyz", "abc" ].parse.expand.solve == "xaybzc" ); assert( [ "atcoderbeginnercontest", "atcoderregularcontest" ].parse.expand.solve == "aattccooddeerrbreeggiunlnaerrccoonntteesstt" ); } 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 o = input.front; input.popFront; auto e = input.front; return tuple( o, e ); } auto solve( string o, string e ) { auto ans = new char[ o.length + e.length ]; foreach( i, ref c; ans ) { if( i % 2 == 0 ) c = o[ i / 2 ]; else c = e[ i / 2 ]; } return ans; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int[] sieve(int n) { assert(n > 1); auto t = new bool[n + 1]; int[] primes = [2]; for (int i = 3; i < t.length; i += 2) { if (!t[i]) { primes ~= i; for (int j = i + i; j < t.length; j += i) t[j] = true; } } return primes; } void main() { int n = 10 ^^ 5; auto pmap = new bool[n + 1]; foreach (p; sieve(n)) pmap[p] = true; bool isLike(int n) { if (n % 2 == 0) return false; if (pmap[n] && pmap[(n + 1) / 2]) return true; return false; } auto t = new int[n + 1]; for (int i = 3; i <= n; i++) { t[i] = t[i - 1] + (isLike(i) ? 1 : 0); } int q = readint; for (int i = 0; i < q; i++) { auto lr = readints; int l = lr[0]; int r = lr[1]; writeln(t[r] - t[l - 1]); } }
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; } enum MOD = 2019; void main() { int l, r; scan(l, r); if (r - l > 2019) { writeln(0); return ; } long ans = long.max; foreach(long i; l..r+1) foreach(long j; i+1..r+1) { ans = min(ans, (i%MOD*j%MOD) % MOD); } writeln(ans); }
D
void main() { string s = rdStr; long len = s.length; long l, r = len - 1; long cnt; while (l < r) { if (s[l] == s[r]) { ++l, --r; } else { if (s[l] == 'x') { ++cnt; ++l; } else if (s[r] == 'x') { ++cnt; --r; } else { writeln(-1); return; } } } cnt.writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.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() { long ans = min(RD, RD); ans += min(RD, RD); writeln(ans); stdout.flush(); debug readln(); }
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[5] ary; foreach (i; 0 .. 3) { auto ab = aryread(); ary[ab[0]]++; ary[ab[1]]++; } long[2] x; foreach (i; 1 .. 5) { x[ary[i] & 1]++; } bool b = x[0] == 2 && x[1] == 2; writeln(b ? "YES" : "NO"); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001L; void main() { int n; scan(n); auto a = readln.split.to!(long[]); a = [inf] ~ a ~ [-inf]; long mon = 1000; long kb = 0; foreach (i; 1 .. n + 1) { if (a[i - 1] >= a[i] && a[i] < a[i + 1]) { long t = (mon / a[i]); mon = mon - t * a[i]; kb += t; } else if (a[i - 1] <= a[i] && a[i] > a[i + 1]) { mon += kb * a[i]; kb = 0; } } writeln(mon); } void scan(T...)(ref T args) { auto line = readln.split; // @suppress(dscanner.suspicious.unmodified) foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int N; scan(N); auto a = readln.split.to!(long[]); auto dp = new long[][](N + 1, N + 1); fillAll(dp, infl); long rec(int l, int r) { if (l == r) { return 0; } if (dp[l][r] != infl) { return dp[l][r]; } dp[l][r] = max(a[l] - rec(l + 1, r), a[r - 1] - rec(l, r - 1)); return dp[l][r]; } auto ans = rec(0, N); writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.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 = RD-1; auto B = RD-1; auto C = RD-1; auto D = RD-1; auto S = RD!string; bool ans; if (C > D) { foreach (i; B..D+1) { if (i >= N-1) break; if (S[i] == '.' && S[i-1] == '.' && S[i+1] == '.') { ans = true; break; } } } else { ans = true; } foreach (i; A..C) { if (S[i] == '#' && S[i+1] == '#') { ans = false; break; } } foreach (i; B..D) { if (S[i] == '#' && S[i+1] == '#') { ans = false; break; } } writeln(ans ? "Yes" : "No"); stdout.flush(); debug readln(); }
D
import std.stdio; import std.conv; import std.array; import std.string; import std.algorithm; void main() { int n; n = to!int(chomp(readln())); int [] a; for(int i = 0; i < n; i++) { a ~= to!int(chomp(readln())); } sort(a); a ~= -1; int res = 0; int cnt = 1; for(int i = 1; i < a.length; i++) { if(a[i] == a[i-1]) { cnt++; } else { if(cnt % 2 == 1) res++; cnt = 1; } } writeln(res); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n, k; readV(n, k); int[] h; readA(n, h); auto dp = new int[](n); dp[] = 10^^9; dp[0] = 0; foreach (i; 1..n) foreach (j; 1..k+1) if (i-j >= 0) dp[i] = min(dp[i], dp[i-j] + (h[i]-h[i-j]).abs); writeln(dp[$-1]); }
D
long[long] solve(long n){ long[long] dic; for(long x=1; x^^2<=n; x++){ for(long y=1; x^^2+y^^2<=n; y++){ for(long z=1; x^^2+y^^2+z^^2<=n; z++){ dic[x^^2+y^^2+z^^2 + x*y+y*z+z*x]++; } } } return dic; } void main(){ long n = _scan!long(); auto ans = solve(n); foreach(i; 1..n+1){ ans.get(i, 0).writeln(); } } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio, std.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 N = RD; auto S = RD!string; string ans; foreach (i; 0..S.length) { auto num = S[i] - 'A'; ans ~= cast(char)('A'+(num+N)%26); } 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; void main() { auto n = readln.chomp.to!int; auto ans = new int[6]; foreach (_; 0..n) { auto x = readln.chomp.to!double; foreach (i, a; iota(165, 195, 5).array) { if (x < a) { ans[i]++; break; } } } foreach (i, a; ans) { writeln(i+1, ":", a.iota.map!(b=>"*").join); } }
D
void main() { int n = readln.chomp.to!int; writeln(readln.split.to!(int[]).sum - n); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; import std.container; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } void main(){ long n = read.to!long; long[] as = readln.split.map!(to!long).array; long sum = as.reduce!"a + b"(); long ans = long.max; long s; foreach(i, a; as){ if(i == as.length - 1) continue; s += a; long x = abs(s - (sum - s)); if(x < ans) ans = x; } ans.writeln; }
D
// Cheese-Cracker: cheese-cracker.github.io void theCode(){ auto word = scan!(dchar[]); int n = word.length.to!int; auto vis = new bool[n]; int cnt = 0; for(int i = 0; i < n-1; ++i){ if(vis[i]) continue; if(i < n - 2 && word[i] == word[i+1] && word[i] == word[i+2]){ cnt += 2; vis[i+1] = 1; vis[i+2] = 1; }else if(word[i] == word[i+1]){ ++cnt; vis[i+1] = 1; }else if(i < n - 2 && word[i] == word[i+2]){ ++cnt; vis[i+2] = 1; } } writeln(cnt); } void main(){ long tests = 1; tests = scan; // Toggle! while(tests--) theCode(); } /********* That's All Folks! *********/ import std.stdio, std.conv, std.functional, std.string, std.algorithm; import std.container, std.range, std.typecons, std.numeric, std.math, std.random; static string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;} T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; } void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } } alias ll = long, tup = Tuple!(long, "x", long, "y");
D
void main(){ string[] val = inln!string(); bool a = (val[0]=="H")?true:false; bool b = (val[1]=="H")?true:false; bool ans = a^b; writeln(( (a^b)==false)?'H':'D'); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range; const long mod = 10^^9+7; // 1要素のみの入力 T inelm(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] inln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split())ln ~= elm.to!T(); return ln; }
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; } void main() { int n = readint; int ans = 0; for (int i = 0; i < n; i++) { auto ab = readints; int a = ab[0], b = ab[1]; ans += b - a + 1; } writeln(ans); }
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); int m; readV(m); string[] t; readM(m, t); int[string] h; foreach (si; s) h[si]++; foreach (ti; t) h[ti]--; auto r = h.values.reduce!max; writeln(max(0, r)); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { char[] s; scan(s); int ans = 100000; foreach (ch ; 'a' .. 'z' + 1) { ans = min(ans, solve(s, ch.to!char)); } writeln(ans); } int solve(char[] s, char ch) { int n = s.length.to!int; char[] cur = s.dup; foreach (i ; 0 .. n - 1) { if (cur.all!(ch => ch == cur.front)) { return i; } auto nex = new char[](n - 1 - i); foreach (j ; 0 .. n - 1 - i) { if (cur[j] == ch || cur[j + 1] == ch) { nex[j] = ch; } else { nex[j] = cur[j]; } } cur = nex; debug { writeln(cur); } } return n - 1; } 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.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 q; readV(q); foreach (_; 0..q) { long a, b; readV(a, b); if (a > b) swap(a, b); if (a == b) { writeln(a*2-2); } else { auto c = nsqrt(a*b-1); if (c*(c+1) >= a*b) writeln(c*2-2); else writeln(c*2-1); } } } pure T nsqrt(T)(T n) { import std.algorithm, std.conv, std.range, core.bitop; if (n <= 1) return n; T m = T(1) << (n.bsr/2+1); return iota(1, m).map!"a * a".assumeSorted!"a <= b".lowerBound(n).length.to!T; }
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; auto cnt = new int[][](s.length+1); cnt[0] = new int[](26); foreach (i; 0..s.length) { cnt[i+1] = cnt[i].dup; auto num = s[i]-'a'; cnt[i+1][num] += 1; } auto q = RD!int; auto ans = new bool[](q); foreach (qi; 0..q) { auto l = RD!int-1; auto r = RD!int-1; if (l == r || s[l] != s[r]) { ans[qi] = true; continue; } long c; auto tmp = cnt[r+1].dup; tmp[] -= cnt[l][]; foreach (i; 0..26) { if (i == s[l]-'a') continue; if (tmp[i] > 0) ++c; } ans[qi] = c >= 2; } foreach (e; ans) writeln(e ? "Yes" : "No"); stdout.flush; debug readln; }
D
// Try AtCoder /// author: Leonardone @ NEETSDKASU import std.stdio : readln, writeln; import std.string : chomp; import std.array : split; import std.conv : to; auto getVals(T)() { return readln.chomp.split.to!(T[]); } void main() { bool[char] table; foreach (ch; readln.chomp) { table[ch] = true; } for (auto ch = 'a'; ch <= 'z'; ch++) { if (ch !in table) { writeln(ch); return; } } writeln("None"); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; import std.math; void main() { for (;;) { auto rd = readln.split, a = rd[0].to!int, op = rd[1], b = rd[2].to!int; if (op == "?") break; writeln(op.predSwitch("+", a + b, "-", a - b, "*", a * b, "/", a / b)); } }
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() { int H, N; scan(H, N); auto a = readln.split.to!(int[]); writeln(H <= a.sum() ? "Yes" : "No"); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math; void main() { auto S = readln.chomp.to!(wchar[]); auto w = readln.chomp.to!size_t; size_t i; while (i < S.length) { write(S[i]); i += w; } writeln(""); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio; void main() { auto T = readln.chomp.to!int; while (T--) { auto s = readln.split.map!(to!long); auto A = s[0]; auto B = s[1]; auto D = abs(A - B); long tmp = 0; for (long i = 0; ; ++i) { tmp += i; if (tmp >= D && tmp % 2 == D % 2) { i.writeln; break; } } } }
D
import std.stdio, std.conv, std.string, std.algorithm, std.math, std.array, std.container, std.typecons; void main() { int n = readln.chomp.to!int; bool[string] map; for(int i=0; i<n; i++) map[readln.chomp] = true; writeln(map.length); }
D
void main() { problem(); } void problem() { auto X = scan!ulong; ulong solve() { ulong years; real money = 100; while(money < X) { years++; money = cast(ulong)(cast(real)money * 1.01); } return years; } writeln(solve()); } // ---------------------------------------------- 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.math, std.regex; void main() { auto t = readln; writeln("ABC" ~ t); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; int[10^^5+1] MEMO; void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; foreach (_; 0..M) { auto lr = readln.split.to!(int[]); auto L = lr[0]-1; auto R = lr[1]-1; MEMO[L] += 1; MEMO[R+1] -= 1; } int c, r; foreach (i; 0..N) { c += MEMO[i]; if (c == M) ++r; } writeln(r); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; int[30][30] DC; int[30][3] CS; void main() { auto nc = readln.split.to!(int[]); auto N = nc[0]; auto C = nc[1]; foreach (i; 0..C) { foreach (j, d; readln.split) { DC[i][j] = d.to!int; } } foreach (i; 0..N) { foreach (j, c; readln.split) { CS[(i+j+2)%3][c.to!int-1]++; } } auto ret = long.max; foreach (a; 0..C) { foreach (b; 0..C) { if (b == a) continue; foreach (c; 0..C) { if (a == c || b == c) continue; long sum; foreach (x, aa; CS[0][0..C]) sum += DC[x][a] * aa; foreach (x, bb; CS[1][0..C]) sum += DC[x][b] * bb; foreach (x, cc; CS[2][0..C]) sum += DC[x][c] * cc; ret = min(ret, sum); } } } writeln(ret); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.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 D = new bool[](N); foreach (i; 0..N) { auto d1 = RD; auto d2 = RD; D[i] = d1 == d2; } bool ans; foreach (i; 0..N-2) { if (D[i] && D[i+1] && D[i+2]) ans = true; } writeln(ans ? "Yes" : "No"); stdout.flush; debug readln; }
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, "damage", long, "cost"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { auto n = lread(); auto a = aryread(); bool check = true; foreach (e; a) { if (e % 2) continue; else if (e % 3 && e % 5) check = false; } if (check) writeln("APPROVED"); else writeln("DENIED"); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto N = readln.chomp.to!int; auto D = readln.split.map!(to!int).array; auto M = readln.chomp.to!int; auto T = readln.split.map!(to!int).array; int[int] A; foreach (t; T) A[t] += 1; foreach (d; D) A[d] -= 1; writeln((A.values.map!(a => a <= 0).all) ? "YES" : "NO"); }
D
import std.stdio; import std.algorithm; import std.string; import std.functional; import std.array; import std.conv; import std.math; import std.typecons; import std.regex; import std.range; void main(){ int n = readln().chomp().to!int; real ans; ans = 0; for(int i=0;i<n;i++){ ans += readln().chomp().to!real / to!real(n); } writeln(to!int(ans)); }
D
void main() { long n, m; rdVals(n, m); bool[] ac = new bool[n+1]; long[] wa = new long[n+1]; foreach (i; 0 .. m) { long p; string s; rdVals(p, s); if (s == "AC") { if (!ac[p]) ac[p] = true; } else { if (!ac[p]) ++wa[p]; } } long cnt_ac, cnt_wa; foreach (i; 1 .. n+1) { if (ac[i]) { ++cnt_ac; cnt_wa += wa[i]; } } writeln(cnt_ac, " ", cnt_wa); } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp; auto K = readln.chomp.to!int; auto DP = new long[][][](3, K+1, N.length); DP[2][K-1][0] += 1; foreach (x; 1..N[0]-'0') DP[1][K-1][0] += 1; DP[0][K][0] += 1; foreach (i; 1..N.length) { if (N[i] == '0') { foreach (k; 0..K+1) DP[2][k][i] += DP[2][k][i-1]; } else { foreach (k; 1..K+1) DP[2][k-1][i] += DP[2][k][i-1]; foreach (k; 0..K+1) DP[1][k][i] += DP[2][k][i-1]; } foreach (_; 1..N[i]-'0') { foreach (k; 1..K+1) DP[1][k-1][i] += DP[2][k][i-1]; } foreach (k; 0..K+1) DP[1][k][i] += DP[1][k][i-1]; foreach (_; 1..10) foreach (k; 1..K+1) DP[1][k-1][i] += DP[1][k][i-1]; foreach (k; 0..K+1) DP[0][k][i] += DP[0][k][i-1]; foreach (_; 1..10) foreach (k; 1..K+1) DP[1][k-1][i] += DP[0][k][i-1]; } long r; foreach (x; 0..3) r += DP[x][0][N.length-1]; writeln(r); }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; void times(alias fun)(int n) { foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } // fold was added in D 2.071.0. template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } void main() { readln.chomp.to!long.pipe!"a<1200".pipe!(a => a?"ABC":"ARC").writeln; }
D
import std.container; import std.range; import std.algorithm; import std.array; import std.string; import std.conv; import std.stdio; import std.container; void main() { readln; auto a = readln.chomp.split(' ').map!(to!int).array; auto c = new int[10 ^^ 6 + 1]; foreach (e;a) { ++c[e]; } auto pairwise = true; foreach (i;2..c.length) { auto count = 0; for (auto j = i;j < c.length;j += i) { count += c[j]; } if (count > 1) { pairwise = false; break; } } if (pairwise) { writeln("pairwise coprime"); return; } int gcd(int x, int y) { if (y == 0) return x; return gcd(y, x % y); } if (a.reduce!gcd == 1) { writeln("setwise coprime"); return; } writeln("not coprime"); }
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 modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto arr = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]; auto K = RD; writeln(arr[K-1]); stdout.flush; debug readln; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int maxSumSequence(int[] a) { auto dp = new int[a.length]; dp[0] = a[0]; for (int i = 1; i < a.length; i++) { dp[i] = max(dp[i - 1] + a[i], a[i]); } return dp.reduce!max; } void main() { while (true) { int n = readint; if (n == 0) break; int[] a; for (int i = 0; i < n; i++) a ~= readint; writeln(maxSumSequence(a)); } }
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.map!(to!int).array; int y; if (a.sum > 0) { y = ((a.sum.to!double / n) + 0.5).to!int; } else { y = ((a.sum.to!double / n) - 0.5).to!int; } int sum; foreach (e; a) { sum += (e - y)^^2; } sum.writeln; }
D
void main() { auto N = ri; auto A = iota(N).map!(a => ri).array; if(A[0] > 0) { writeln(-1); return; } foreach(i; 0..N-1) { if(A[i+1]-A[i] > 1) { writeln(-1); return; } } ulong ans; foreach(i; 0..N-1) { if(A[i]+1 == A[i+1]) ans++; else ans += A[i+1]; } ans.writeln; } // =================================== import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; } T[] deepcopy(T)(T[] a) { static if(isArray!T) { T[] res; foreach(i; a) { res ~= deepcopy(i); } return res; } else { return a.dup; } } ulong[] generate_prime_list(T)(T N) if(isIntegral!T) { ulong[] prime_list = [2]; bool not_prime = false; foreach(i; 3..N.to!ulong+1) { foreach(j; prime_list) { if(i % j == 0) { not_prime = true; break; } } if(!not_prime) prime_list ~= i; not_prime = false; } return prime_list; } bool isPrime(ulong n) { if(n <= 1) return false; else if(n == 2) return true; else if(n % 2 == 0) return false; foreach(i; iota(3, n.to!double.sqrt.ceil+1, 2)) { if(n % i == 0) return false; } return true; } class UnionFind(T) { T[] arr; this(ulong n) { arr.length = n+1; arr[] = -1; } T root(T x) { return arr[x] < 0 ? x : root(arr[x]); } bool same(T x, T y) { return root(x) == root(y); } bool unite(T x, T y) { x = root(x); y = root(y); if(x == y) return false; if(arr[x] > arr[y]) swap(x, y); arr[x] += arr[y]; arr[y] = x; return true; } T size(T a) { return -arr[root(a)]; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; int[][] ss; ss.length = M; foreach (i; 0..M) { auto ks = readln.split.to!(int[]); foreach (s; ks[1..$]) ss[i] ~= s-1; } auto ps = readln.split.to!(int[]); int r; foreach (x; 0..1<<N) { foreach (i; 0..M) { int c; foreach (s; ss[i]) if (x & (1<<s)) ++c; if (c%2 != ps[i]) goto ng; } ++r; ng: } writeln(r); }
D
import std.stdio, std.string, std.conv, std.math; void main() { auto ip = readln.split.to!(int[]); if(0 <= (ip[1] - ip[2])){ writeln("delicious"); } else if(ip[0] + 1 > abs(ip[1] - ip[2])){ writeln("safe"); } else { writeln("dangerous"); } }
D
import std.stdio, std.conv, std.algorithm, std.range, std.array, std.string, std.uni; void main() { auto inp = readln.split.to!(int[]); if (inp[1] * 2 == inp[0] + inp[2]) { writeln("YES"); } else { writeln("NO"); } }
D