code
stringlengths
4
1.01M
language
stringclasses
2 values
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); void main() { auto n = lread(); auto ans = new long[](10 ^^ 5); foreach (i; 1 .. 101) { foreach (j; 1 .. 101) { foreach (k; 1 .. 101) { long tmp; tmp = i ^^ 2 + j ^^ 2 + k ^^ 2 + i * j + j * k + i * k; if (tmp <= 10 ^^ 4) { ans[tmp - 1] += 1; } } } } // writeln(ans); foreach (i; 0 .. n) { writeln(ans[i]); } } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; string m(string a, string b) { if (a == "T" && b == "T") return "T"; if (a == "T" && b == "F") return "F"; if (a == "F" && b == "T") return "T"; return "T"; } void main() { auto N = readln.chomp.to!int; auto ps = readln.split.to!(string[]); auto c = m(ps[0], ps[1]); foreach (x; ps[2..$]) { c = m(c, x); } writeln(c); }
D
import std.stdio,std.conv,std.string; int main(){ int x; x=stdin.readln().chomp.to!int; if(x==3||x==5||x==7){ write("YES"); } else{ write("NO"); } return 0; }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; import std.range; void main(){ auto Q=readln.split,a=Q[0],b=Q[1]; if(a==b)writeln("H"); else writeln("D"); }
D
import std.stdio, std.algorithm, std.string; void main() { readln(); auto s = readln().strip(); int[string] d; foreach (i; 0 .. s.length - 1) { d[s[i .. i+2]]++; } int maxCount = 0; string maxStr = ""; foreach (str, count; d) { if (count > maxCount) { maxCount = count; maxStr = str; } } writeln(maxStr); }
D
import std.stdio, std.conv, std.string; import std.algorithm, std.array, std.container, std.typecons; import std.numeric, std.math; import core.bitop; T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T)(in string str) { return str.split.to!(T[]); } 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; } long mod = pow(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; } struct UnionFind { void init(long n) { par = new long[](n); foreach (i; 0..n) par[i] = i; } long root(long i) { return par[i] == i ? i : (par[i] = root(par[i])); } bool same(long i, long j) { return root(i) == root(j); } void unite(long i, long j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; } long[] par; } void main() { auto H = RD!long; auto W = RD!long; auto D = RD!long; auto A = new long[2][](H*W); foreach (i; 0..H) { foreach (j; 0..W) { auto num = RD!long; A[num] = [i, j]; } } auto Q = RD!long; auto L = new long[](Q); auto R = new long[](Q); foreach (i; 0..Q) { L[i] = RD!long; R[i] = RD!long; } auto dp = new long[](H*W+1); foreach (i; 0..H*W) { auto dist1 = abs(A[i+D][0] - A[i][0]); auto dist2 = abs(A[i+D][1] - A[i][1]); dp[i+D] = dp[i] + dist1 + dist2; } foreach (i; 0..Q) { writeln(dp[R[i]] - dp[L[i]]); } stdout.flush(); }
D
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math; import core.stdc.stdio; // foreach, foreach_reverse, writeln void main() { int q; scanf("%d", &q); foreach (i; 0..q) { int a, b; scanf("%d%d", &a, &b); if (a > b) swap(a,b); int l = 0, r = b; while (l+1 < r) { int x = (l+r)/2; bool judge(long a, long b, long x) { // (x..1) * (a+1..a+x) < a*b if (x*(a+1) >= a*b) return false; if (1*(a+x) >= a*b) return false; long l = (x+a+1)/2; long r = (x+a+1)-l; if (l > x) return true; if (r < a+1) return true; if (l*r >= a*b) return false; return true; } if (judge(a, b, x)) l = x; else r = x; } writeln(l + (a-1)); } }
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() { auto S = sread(); long N = S.length; auto dp = new long[][](2, 2019); // dp[0][0] = 1; long ans; foreach (i; 0 .. N) { dp[(i + 1) & 1][] = 0; dp[(i + 1) & 1][(S[i] - '0')]++; foreach (j; 0 .. 2019) { long x = (j * 10) + (S[i] - '0'); dp[(i + 1) & 1][x % 2019] += dp[i & 1][j]; } // if (dp[i + 1][0] != 0) // { // writeln(" ", i); // } ans += dp[(i + 1) & 1][0]; } writeln(ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; int[] AS; foreach (_; 0..N) AS ~= readln.chomp.to!int; int[] lis; foreach (a; AS) { if (lis.empty || lis[$-1] < a) { lis ~= a; continue; } else if (lis[0] >= a) { lis[0] = a; continue; } size_t l, r = lis.length-1; while (l+1 < r) { auto m = (l+r)/2; if (lis[m] >= a) { r = m; } else { l = m; } } lis[r] = a; } writeln(lis.length); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp.to!int; auto s = readln.chomp.split; int[string] cnt; foreach (e; s) { cnt[e]++; } if (cnt.keys.length == 3) { writeln("Three"); } else { writeln("Four"); } }
D
import std.stdio; import std.conv; import std.string; import std.array; void main() { string[] input1 = split(readln()); string[] input2 = split(readln()); int S; int T; for(int i = 0; i < 4; ++i) S += to!int(input1[i]); for(int i = 0; i < 4; ++i) T += to!int(input2[i]); if (S > T) writeln(S); else writeln(T); }
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 W = RD; auto a = RD; auto b = RD; if (a < b) writeln(max(b-(W+a), 0)); else writeln(max(a-(W+b), 0)); stdout.flush(); debug readln(); }
D
#!/usr/bin/env rdmd import std.stdio, std.string, std.conv; import std.algorithm, std.array; import std.math, std.numeric; import std.range; void main() { for(string S; (S=readln().chomp()).length; ) { auto m = S.split().map!(to!int)(); auto n = m[0], l=m[1], r=m[2], Ql=m[3], Qr=m[4]; auto w = array(readln().split().map!(to!int)()); auto sl = new int[n+1]; auto sr = new int[n+1]; foreach(i;0..n) sl[i+1] = sl[i]+w[i]*l, sr[n-i-1] = sr[n-i]+w[n-i-1]*r; auto s = int.max; foreach(i;0..n+1) s = min(s,sl[i]+max(0,Ql*(i*2-n-1))+sr[i]+max(0,Qr*(n-i*2-1))); writeln(s); } }
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 N = lread(); auto f = factorize(N); long ans; foreach (key, val; f) { // writeln(key, " ", val); long i = 1; while (i * (i + 1) <= val * 2) i++; ans += i - 1; } writeln(ans); } /// 素因数分解 long[long] factorize(long x) { assert(0 < x, "x is negative"); long[long] ps; while ((x & 1) == 0) { x /= 2; ps[2] = (2 in ps) ? ps[2] + 1 : 1; } for (long i = 3; i * i <= x; i += 2) while (x % i == 0) { x /= i; ps[i] = (i in ps) ? ps[i] + 1 : 1; } if (x != 1) ps[x] = (x in ps) ? ps[x] + 1 : 1; return ps; }
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 x = readint; writeln(x < 1200 ? "ABC" : "ARC"); }
D
import std.stdio, std.string, std.conv; void main() { auto S = readln.split.to!(int[]); auto sx = S[0], sy = S[1], tx = S[2], ty = S[3]; void po(string c, int x) { foreach (i; 0..x) { write(c); } } po("R", tx-sx); po("U", ty-sy); po("L", tx-sx); po("D", ty-sy); po("L", 1); po("U", ty-sy+1); po("R", tx-sx+1); po("D", 1); po("R", 1); po("D", ty-sy+1); po("L", tx-sx+1); po("U", 1); 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 inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int N, K; scan(N, K); auto S = readln.chomp; int ans = N - 1; int t; foreach (i ; 1 .. N) { if (S[i] != S[i - 1]) t++; } t = max(0, t - 2 * K); ans -= t; 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; 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 ss = readln.chomp; int res0, res1; foreach (i, s; ss) { if (i % 2) { if (s == '0') { res0++; } else { res1++; } } else { if (s == '0') { res1++; } else { res0++; } } } min(res0, res1).writeln; }
D
import std.stdio; import core.stdc.stdio; import std.array; import std.conv; import std.algorithm; import std.range; void main() { int n; scanf("%d",&n); int[] a; a.length=n; foreach(i; 0..n) scanf("%d", &a[i]); a.sort().reverse(); int x, y; foreach(i, v; a) { if(i & 1) y += v; else x += v; } writeln(x - y); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto s = RD!string; long cnt = s[0] == '0' ? 1 : 0; foreach (i; 0..s.length-1) { if (s[i] == '1' && s[i+1] == '0') ++cnt; } ans[ti] = min(cnt, 2); } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio; import std.conv; import std.string; const int DATA = 10; int main() { int max=-1, mid=-1, min=-1; for(int i=0; i<DATA; i++) { int d = to!(int)(chomp(readln)); if(max < d) { min = mid; mid = max; max = d; } else if(mid < d) { min = mid; mid = d; } else if(min < d) { min = d; } } writeln(max, "\n", mid, "\n", min); return 0; }
D
import std.stdio; import std.string; import std.conv; import std.math; void main() { string[] inputs = split(readln()); int X = to!int(inputs[0]); int A = to!int(inputs[1]); int B = to!int(inputs[2]); if(A - B >= 0) "delicious".writeln; else if(X >= abs(A - B)) "safe".writeln; else "dangerous".writeln; }
D
static immutable MOD = 4; static immutable MOD_OVERAL = 5; static immutable MODS = [ [1, 1, 1, 1], [1, 2, 4, 3], [1, 3, 4, 2], [1, 4, 1, 4] ]; uint solve(const string n) { uint sum = 0; foreach (ch; n) { if (ch < '0' || ch > '9') {break;} sum = ( (sum * 10) + (ch - '0') ) % MOD; } sum %= MOD; uint res = 0; foreach (curMod; MODS) { res += curMod[sum]; } return res % MOD_OVERAL; } unittest { assert(4 == solve("4")); assert(0 == solve("124356983594583453458888889")); assert(4 == solve("0")); assert(0 == solve("5")); assert(0 == solve("6")); assert(0 == solve("7")); assert(4 == solve("8")); } int main(string[] argv) { import std.stdio; writeln(solve(readln)); return 0; }
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, C, D; scan(A, B, C, D); writeln(max(A * B, C * D)); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n; readV(n); int[] a; readA(n, a); writeln(a.map!(ai => ai.bsf).sum); } pragma(inline) { pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; } pure T bitSet(T)(T n, size_t i) { return n | (T(1) << i); } pure T bitReset(T)(T n, size_t i) { return n & ~(T(1) << i); } pure T bitComp(T)(T n, size_t i) { return n ^ (T(1) << i); } pure T bitSet(T)(T n, size_t s, size_t e) { return n | ((T(1) << e) - 1) & ~((T(1) << s) - 1); } pure T bitReset(T)(T n, size_t s, size_t e) { return n & (~((T(1) << e) - 1) | ((T(1) << s) - 1)); } pure T bitComp(T)(T n, size_t s, size_t e) { return n ^ ((T(1) << e) - 1) & ~((T(1) << s) - 1); } import core.bitop; pure int bsf(T)(T n) { return core.bitop.bsf(ulong(n)); } pure int bsr(T)(T n) { return core.bitop.bsr(ulong(n)); } pure int popcnt(T)(T n) { return core.bitop.popcnt(ulong(n)); } }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; void main() { long n; scan(n); if (n == 1) { writeln(0); return; } long ans; for (long i = 1; i * i <= n; i++) { if (n % i != 0) continue; auto m = n / i - 1; if (n / m == n % m) ans += m; } writeln(ans); } 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; import std.array; import std.string; import std.conv; void main() { uint count = 0; auto n = chomp(readln()); auto s = split(readln()); auto q = to!(int)(chomp(readln())); auto t = split(readln()); foreach (i; t) foreach (j; s) if (i == j) {count++; break;} writeln(count); }
D
import std.stdio,std.range,std.algorithm,std.conv,std.array,std.string; void main() { int[1000] field; int[1000] dice; while(true) { auto input=readln.split; int N=input[0].to!int; int M=input[1].to!int; if(N==0&&M==0)break; foreach(i; 0..N) { field[i]=readln.chomp.to!int; } foreach(i; 0..M) { dice[i]=readln.chomp.to!int; } int i, cur; while(true) { cur+=dice[i++]; if(cur >= N - 1) break; cur+=field[cur]; if(cur >= N - 1) break; } i.writeln; } }
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 x = scanElem; if(abs(x-scanElem)<abs(x-scanElem))end("A");end("B"); }
D
//prewritten code: https://github.com/antma/algo import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.traits; class InputReader { private: ubyte[] p; ubyte[] buffer; size_t cur; public: this () { buffer = uninitializedArray!(ubyte[])(16<<20); p = stdin.rawRead (buffer); } final ubyte skipByte (ubyte lo) { while (true) { auto a = p[cur .. $]; auto r = a.find! (c => c >= lo); if (!r.empty) { cur += a.length - r.length; return p[cur++]; } p = stdin.rawRead (buffer); cur = 0; if (p.empty) return 0; } } final ubyte nextByte () { if (cur < p.length) { return p[cur++]; } p = stdin.rawRead (buffer); if (p.empty) return 0; cur = 1; return p[0]; } template next(T) if (isSigned!T) { final T next () { T res; ubyte b = skipByte (45); if (b == 45) { while (true) { b = nextByte (); if (b < 48 || b >= 58) { return res; } res = res * 10 - (b - 48); } } else { res = b - 48; while (true) { b = nextByte (); if (b < 48 || b >= 58) { return res; } res = res * 10 + (b - 48); } } } } template next(T) if (isUnsigned!T) { final T next () { T res = skipByte (48) - 48; while (true) { ubyte b = nextByte (); if (b < 48 || b >= 58) { break; } res = res * 10 + (b - 48); } return res; } } final T[] nextA(T) (int n) { auto a = uninitializedArray!(T[]) (n); foreach (i; 0 .. n) { a[i] = next!T; } return a; } } void main() { auto r = new InputReader; immutable nt = r.next!uint; foreach (tid; 0 .. nt) { immutable n = r.next!uint; immutable k = r.next!uint; auto a = r.nextA!int (n); long res = int.max; int x = int.max; foreach (i; k .. n) { int d = a[i] - a[i - k]; if (res > d) { res = d; x = (a[i] + a[i - k]) / 2; } } writeln (x); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto K = RD; auto A = RD; auto B = RD; bool ans; foreach (i; A..B+1) { if (i % K == 0) { ans = true; } } writeln(ans ? "OK" : "NG"); stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; long f(long x) { if (x < 10) return 9; long ans = 1; ++x; while (x % 10 == 0) { x /= 10; } return ans + f(x); } writeln(f(N)); stdout.flush(); debug readln(); }
D
void main() { auto S = rs; auto A = S[0..2].to!int; auto B = S[2..4].to!int; bool YYMM = 0 <= A && A <= 99 && 0 < B && B <= 12; bool MMYY = 0 <= B && B <= 99 && 0 < A && A <= 12; if(YYMM && MMYY) writeln("AMBIGUOUS"); else if(YYMM) writeln("YYMM"); else if(MMYY) writeln("MMYY"); else writeln("NA"); } // =======================111=1=========== import std.stdio; import std.string; import std.functional; 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.ascii : toLower; 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; }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; void main() { string[] inputs = split(readln()); int a = to!int(inputs[0]); int b = to!int(inputs[1]); int c = to!int(inputs[2]); bool[int] flags; flags[a] = true; flags[b] = true; flags[c] = true; flags.length.writeln; }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void pickV(R,T...)(ref R r,ref T t){foreach(ref v;t)pick(r,v);} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n, m; readV(n, m); auto c = new int[](m+1); foreach (i; 0..n) { auto rd = rdsp; int k; pickV(rd, k); foreach (j; 0..k) { int a; pickV(rd, a); ++c[a]; } } writeln(c.count(n)); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nx = readln.split.to!(int[]); auto N = nx[0]; auto X = nx[1]; auto LS = readln.split.to!(int[]); int d, c; foreach (l; LS) { ++c; d += l; if (d > X) break; } if (d <= X) ++c; writeln(c); }
D
import std.stdio; import std.algorithm; import std.conv; import std.datetime; import std.numeric; import std.math; import std.string; string my_readln() { return chomp(readln()); } void main() {//try{ auto tokens = split(my_readln()); auto N = to!ulong(tokens[0]); auto H = to!ulong(tokens[1]); auto W = to!ulong(tokens[2]); ulong ans; foreach (i; 0..N) { auto tokens2 = split(my_readln()); auto A = to!ulong(tokens2[0]); auto B = to!ulong(tokens2[1]); if (A >= H && B >= W) ++ans; } writeln(ans); stdout.flush(); /*}catch (Throwable e) { writeln(e.toString()); } readln();*/ }
D
void main(){ int[] ab = _scanln(); (ab[0] * ab[1]).writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string; wchar[600][600] BD1, BD2; void main() { auto N = readln.chomp.to!int; foreach (i; 0..N) { foreach (j, c; readln.chomp) { BD1[i][j] = BD1[i][N+j] = BD1[N+i][j] = BD1[N+i][N+j] = c; BD2[j][i] = BD2[j][N+i] = BD2[N+j][i] = BD2[N+j][N+i] = c; } } int cnt; foreach (a; 0..N) { foreach (i; 0..N) { if (BD1[a+i][0..N] != BD2[i][a..N+a]) goto bad; } ++cnt; bad: } writeln(cnt * N); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { auto s = sread(); long n = s.length; long max_x = long.min; long min_x = long.max; foreach (i; 0 .. n) { if (s[i] == 'A') { // writeln(i); min_x = min(min_x, i); } } foreach (i; 0 .. n) { if (s[i] == 'Z') { // writeln(i); max_x = max(max_x, i); } } // writeln(min_x); // writeln(max_x); writeln(max_x - min_x + 1); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.stdio, std.string, std.algorithm, std.array, std.range, std.conv, std.typecons, std.math, std.container, std.format; void main(string[] args) { auto s = readln.strip(); long cnt = 0; foreach (i; 0 .. s.length / 2) { if (s[i] != s[s.length - i - 1]) { cnt++; } } writeln(cnt); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { int n; while ((n = readint) > 0) { int ans = 0; for (int i = 3; i <= n; i += 2) { if (n % i == 0) { ans++; } } writeln(ans); } }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto x = readln.chomp.to!long; auto ans = x / 11 * 2, mod = x % 11; if (1 <= mod && mod <= 6) ans += 1; else if (5 <= mod) ans += 2; writeln(ans); }
D
import std.stdio; import std.string; import std.conv; void main() { string ans = "UNRESTORABLE"; string s = readln.chomp; string t = readln.chomp; if (t.length <= s.length) { char[] t_ans; foreach_reverse (i; 0..s.length - t.length + 1) { bool flag = true; t_ans = s.dup; foreach_reverse (j; 0..t.length) { if (s[i + j] != t[j] && s[i + j] != '?'){ flag = false; break; } else { t_ans[i + j] = t[j]; } } if (flag) { ans = t_ans.to!string.tr("?", "a"); break; } } } writeln(ans); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, 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 main() { int r; readV(r); int g; readV(g); writeln(2*g-r); }
D
import std.stdio; import std.array; import std.string; import std.conv; import std.algorithm; import std.typecons; import std.range; import std.random; import std.math; void main() { int N = readln().chomp.to!int; int Lsum = 0; int Rsum = 0; int[] L = new int[](N); int[] R = new int[](N); foreach (i; iota(N)) { auto input = readln().split.map!(to!int); Lsum += input[0]; Rsum += input[1]; L[i] = input[0]; R[i] = input[1]; } int max_b = abs(Lsum-Rsum); int max_i = 0; foreach (i; iota(N)) { if (max_b < abs((Lsum-L[i]+R[i])-(Rsum-R[i]+L[i]))) { max_b = abs((Lsum-L[i]+R[i])-(Rsum-R[i]+L[i])); max_i = i+1; } } writeln(max_i); }
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 s; scan(s); int ans = 1; bool[long] e; long f(long x) { if (x & 1) { return 3 * x + 1; } else { return x / 2; } } long a = s; e[s] = true; while (true) { ans++; a = f(a); if (a in e) { writeln(ans); return; } e[a] = true; } } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.string, std.conv; void main() { auto n = readln.chomp.to!int; auto a = readln.chomp.to!int; writeln(n * n - a); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { int N, M; scan(N, M); int ans; for (int i = 1; i*i <= M; i++) { if (M % i != 0) continue; if (M / i >= N) chmax(ans, i); if (M / (M / i) >= N) chmax(ans, M / i); } writeln(ans); } 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
//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; } } //Digit count---{{{ int DigitNum(int num) { int digit = 0; while (num != 0) { num /= 10; digit++; } return digit; } //}}} //}}} void main() { Scanner sc = new Scanner; string S, T; sc.scan(S, T); foreach (i; 0 .. S.length) { if (S == T) { writeln("Yes"); return; } S ~= S.front; S.popFront; } writeln("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(){ while(true){ int n = readln().chomp().to!int; if(n==0)break; int m = readln().chomp().to!int; int p = readln().chomp().to!int; int d = readln().chomp().to!int; string[] s; int[11] h; for(int i=0;i<11;i++) h[i] = i; for(int i=0;i<d;i++){ s ~= readln().chomp(); for(int j=0;j<n-1;j++){ if(s[i][j] == '1'){ swap(h[j+1],h[j+2]); } } } bool flg = false; if(h[p] == m) { writeln(0); flg = true; } else{ for(int i=0;i<d;i++){ for(int j=0;j<n-1;j++){ if(j<n-2 && s[i][j+1] == '1' ) continue; if(j>0 && s[i][j-1] == '1' ) continue; int[11] h1; for(int i1=0;i1<11;i1++) h1[i1] = i1; for(int k=0;k<d;k++){ for(int l=0;l<n-1;l++){ if(s[k][l] == '1' ||(k==i&&l==j)){ swap(h1[l+1],h1[l+2]); } } } if(h1[p]==m){ flg = true; writeln(i+1," ",j+1); break; } } if(flg) break; } } if(!flg) writeln(1); } }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; void main(){ iota(1, 10).map!(a => iota(1, 10).map!(b => text(a, "x", b, "=", a * b)).join("\n")).join("\n").writeln; }
D
/* imports all std modules {{{*/ import std.algorithm, std.array, std.ascii, std.base64, std.bigint, std.bitmanip, std.compiler, std.complex, std.concurrency, std.container, std.conv, std.csv, std.datetime, std.demangle, std.encoding, std.exception, std.file, std.format, std.functional, std.getopt, std.json, std.math, std.mathspecial, std.meta, std.mmfile, std.net.curl, std.net.isemail, std.numeric, std.parallelism, std.path, std.process, std.random, std.range, std.regex, std.signals, std.socket, std.stdint, std.stdio, std.string, std.system, std.traits, std.typecons, std.uni, std.uri, std.utf, std.uuid, std.variant, std.zip, std.zlib; /*}}}*/ /+:---test 3 2 5 ---+/ /+:---test 2 3 ---+/ /+---test 6 0 153 10 10 23 ---+/ void main(string[] args) { readln; const B = readln.split.map!(to!long).array; if (B.length == 1) { (B[0]*2).writeln; return; } long ans = B[0] + B[$-1]; foreach (i; 1..B.length) { ans += min(B[i-1], B[i]); } ans.writeln; }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.ascii; void main() { auto s = readln.chomp; auto t = readln.chomp; int cnt; foreach (i; 0..3) { if (s[i] == t[i]) { cnt++; } } cnt.writeln; }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { int n; scan(n); auto luca = new long[](100); luca[0] = 2, luca[1] = 1; foreach (i ; 2 .. 100) { luca[i] = luca[i - 1] + luca[i - 2]; } writeln(luca[n]); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } struct Queue(T) { private { int N, head, tail; T[] data; } this(int n) { N = n + 1; data = new T[](N); } bool empty() { return head == tail; } bool full() { return (tail + 1) % N == head; } T front() { return data[head]; } void push(T x) { assert(!full); data[tail++] = x; tail %= N; } void pop() { assert(!empty); head = (head + 1) % N; } void clear() { head = tail = 0; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; long[10^^5*2] AS; long[10^^5*2][5] MEMO; void main() { auto L = readln.chomp.to!int; foreach (i; 0..L) { AS[i] = readln.chomp.to!long; } foreach (ref m; MEMO) m[] = -1; long solve(int i, int t) { if (i == L) return 0; if (MEMO[t][i] != -1) return MEMO[t][i]; return MEMO[t][i] = min( t > 0 ? long.max : AS[i] + solve(i+1, 0), t > 1 ? long.max : (AS[i] == 0 ? 2 : AS[i] % 2) + solve(i+1, 1), t > 2 ? long.max : (AS[i] == 0 ? 1 : (AS[i]+1) % 2) + solve(i+1, 2), t > 3 ? long.max : (AS[i] == 0 ? 2 : AS[i] % 2) + solve(i+1, 3), AS[i] + solve(i+1, 4) ); } writeln(solve(0, 0)); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.traits; import std.typecons; int cvt (dchar c) { return (c.to!int - 97); } void main() { immutable n = readln.strip.to!int; string s = readln.strip; auto idx = new int[][26]; foreach (i; 0 .. n) { int o = cvt (s[i]); idx[o] ~= i + 1; } immutable m = readln.strip.to!int; debug stderr.writeln (m); foreach (qid; 0 .. m) { string t = readln.strip; int[26] c; foreach (ch; t) { ++c[cvt (ch)]; } int res; foreach (i; 0 .. 26) { if (c[i] > 0) { res = max (res, idx[i][c[i]-1]); } } writeln (res); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void main() { auto t = RD!int; auto ans = new string[](t); foreach (ti; 0..t) { auto n = RD!int; auto digit = n / 2; //if (digit <= 9) { string str; if (n % 2 == 0) str ~= "1"; else str ~= "7"; foreach (i; 1..digit) str ~= "1"; ans[ti] = str; } /*else { auto arr = [9, 9, 8, 2, 4, 4, 3, 5, 3]; auto cnt = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]; auto remain = n - 18; bool isLimit = true; foreach (i; 0..9) { auto lim = isLimit ? arr[i] : 9; long num; foreach_reverse (j; 0..lim+1) { if (remain >= cnt[j] - 2) { num = j; remain -= cnt[j] - 2; break; } } if (num < lim) isLimit = false; ans[ti] += num * 10^^(8-i); } }*/ } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto Q = s[2]; auto S = readln.chomp; auto T = readln.chomp; auto A = new int[](N+1); foreach (i; 0..N-M+1) { bool ok = true; foreach (j; i..i+M) { if (S[j] != T[j-i]) { ok = false; break; } } if (ok) A[i+1] += 1; } foreach (i; 0..N) A[i+1] += A[i]; while (Q--) { s = readln.split.map!(to!int); int l = s[0] - 1; int r = s[1] - M; if (l > r) writeln(0); else writeln(A[r+1] - A[l]); } }
D
import std.stdio; void main() { printf("25\n"); }
D
import std.stdio, std.string, std.conv, std.algorithm; void main(){ int n; rd(n); auto s=readln.chomp.to!(char[]); if(n==1){ writeln("Yes"); return; } bool[char] set; foreach(c; s){ if(c in set){ writeln("Yes"); return; } set[c]=true; } 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.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { int n; string s; scan(n); scan(s); s = "0" ~ s ~ "0"; auto dp = new int[](n + 1); foreach (i ; 3 .. n + 1) { dp[i] = max(dp[i], dp[i - 1]); if (s[i - 2] == '1' && s[i - 1] == '0' && s[i] == '1') { foreach_reverse (j ; 0 .. i - 1) { if (s[j] == '1') { dp[i] = max(dp[i], dp[j - 1] + i - j - 1); } else { break; } } foreach (j ; i .. n + 2) { if (s[j] == '1') { dp[j] = max(dp[j], dp[i - 3] + j - i + 1); } else { break; } } } } debug { writeln(dp); } writeln(dp[n]); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; long[10^^5][4] CS; int P = 10^^9+7; void main() { auto S = readln.chomp.to!(wchar[]); auto len = S.length; foreach_reverse(i, c; S) { if (i == len-1) { switch (c) { case 'C': CS[2][len-1] = 1; CS[3][len-1] = 1; break; case '?': CS[2][len-1] = 1; CS[3][len-1] = 3; break; default: CS[3][len-1] = 1; } } else { foreach (j; 0..4) { CS[j][i] = CS[j][i+1]; } switch (c) { case 'A': CS[0][i] = (CS[0][i+1] + CS[1][i+1]) % P; break; case 'B': CS[1][i] = (CS[1][i+1] + CS[2][i+1]) % P; break; case 'C': CS[2][i] = (CS[2][i+1] + CS[3][i+1]) % P; break; default: CS[0][i] = (CS[0][i+1] * 3 + CS[1][i+1]) % P; CS[1][i] = (CS[1][i+1] * 3 + CS[2][i+1]) % P; CS[2][i] = (CS[2][i+1] * 3 + CS[3][i+1]) % P; CS[3][i] = (CS[3][i] * 3) % P; } } } writeln(CS[0][0]); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!int).array; auto C = new int[](101010); foreach (i; 0..N) C[A[i]] += 1; int ans = 0; foreach (i; 0..100100) { if (i == 0) ans = max(ans, C[0] + C[1]); else ans = max(ans, C[i-1] + C[i] + C[i+1]); } ans.writeln; }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { const N = readln.chomp.to!long; writeln(N < 1000 ? "ABC" : "ABD"); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; void main() { auto s = readln.stripRight[0 .. 3]; writeln ((s.count ('A') < 3 && s.count('B') < 3) ? "Yes" : "No"); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = 3; auto s = new string[](n); foreach (i; 0..n) s[i] = readln.chomp; auto turn = 0; for (;;) { if (s[turn].empty) { writeln(cast(char)('A' + turn)); return; } auto next = cast(int)(s[turn][0] - 'a'); s[turn] = s[turn][1..$]; turn = next; } }
D
import std.stdio; import std.string; import std.algorithm; import std.conv; import std.math; void main() { int A, B, C; auto input = readln.split.to!(int[]); A = input[0]; B = input[1]; C = input[2]; if(A > B){ writeln(0); }else if(B/A >C){ writeln(C); }else{ writeln(B/A); } }
D
import std.stdio,std.string,std.conv; void main(){ foreach(i;1..int.max){ int a = to!int(readln().chomp()); if( a==0 ){ break; } writeln("Case ",i,": ",a); } }
D
import std.stdio,std.conv,std.string,std.algorithm,std.array; void main(){ auto s=readln().chomp().split().map!(to!int); int a=s[0],b=s[1]; writeln(max(a+b,a-b,a*b)); }
D
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math; import core.stdc.stdio; // foreach, foreach_reverse, writeln void main() { int n; scanf("%d\n", &n); int[][] g = new int[][n]; int[] degree = new int[n]; foreach (i; 0..n-1) { int x, y; scanf("%d %d\n", &x, &y); --x; --y; g[x] ~= y; g[y] ~= x; degree[x]++; degree[y]++; } string c = readln().chomp; bool[] live = new bool[n]; live[] = true; int[] queue; int cursor = 0; foreach (i; 0..n) { if (degree[i] == 1) { queue ~= i; } } int L = n; while (cursor < queue.length) { int v = queue[cursor]; cursor++; if (c[v] == 'W') continue; live[v] = false; L--; foreach (u; g[v]) { degree[u]--; if (degree[u] == 1) { queue ~= u; } } } if (L == 0) { writeln(0); return; } int base = L*2; int[] score = new int[n]; foreach (v; 0..n) { if (!live[v]) continue; score[v] = (degree[v] + (c[v]=='B'?0:1)) % 2; base += score[v]; } int best = 0; int dfs(int v, int parent=-1) { int ret = score[v]; foreach (u; g[v]) { if (u == parent) continue; if (!live[u]) continue; int s = dfs(u,v); best = max(best, ret+s); ret = max(ret, s+score[v]); } return ret; } foreach (v; 0..n) { if (!live[v]) continue; dfs(v); break; } writeln(base-best*2-2); }
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 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 main() { auto N = RD; auto p = new long[](N); foreach (i; 0..N) p[i] = RD; auto pos = new long[](N); foreach (i; 0..N) { pos[p[i]-1] = i; } long ans; long streak; long last; foreach (i; 0..N) { if (pos[i] >= last) ++streak; else { ans.chmax(streak); streak = 1; } last = pos[i]; } ans.chmax(streak); writeln(N-ans); stdout.flush; debug readln; }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n, g; rd(n, g); struct P { int p, c; } auto ps = new P[](n); foreach (i; 0 .. n) rd(ps[i].p, ps[i].c); int mn = 1000000000; foreach (bit; 0 .. (1 << n)) { int s = 0, cnt = 0; foreach (i; 0 .. n) { if (bit & (1 << i)) { s += ps[i].p * (i + 1) * 100 + ps[i].c; cnt += ps[i].p; } } if (s >= g) { mn = min(mn, cnt); continue; } foreach_reverse (i; 0 .. n) { if ((bit & (1 << i)) == 0) { if (s + ps[i].p * (i + 1) * 100 >= g) { mn = min(mn, cnt + ((g - s) + (i + 1) * 100 - 1) / ((i + 1) * 100)); } break; } } } writeln(mn); } void rd(T...)(ref T x) { import std.stdio : readln; import std.string : split; import std.conv : to; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
D
import std.stdio, std.conv, std.string, std.range, std.algorithm; void main() { int d = readln.chomp.to!int; ("Christmas " ~ repeat("Eve", 25 - d).join(" ")).writeln; }
D
import std.algorithm; import std.array; import std.ascii; 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 a, int b, string s) { if (s[a] != '-') return false; for (int i = 0; i < s.length; i++) { if (i == a) continue; if (!isDigit(s[i])) return false; } return true; } void main() { auto ab = readints; int a = ab[0], b = ab[1]; auto s = read!string; writeln(calc(a, b, s) ? "Yes" : "No"); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; ans[ti] = n / 10; if (n % 10 == 9) ++ans[ti]; } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; // dfmt on void main() { long N = lread(); auto H = aryread(); foreach_reverse (i; 0 .. N - 1) { if (H[i + 1] < H[i]) { H[i]--; } if (H[i + 1] < H[i]) { writeln("No"); return; } } writeln("Yes"); }
D
import std.stdio, std.algorithm, std.string, std.conv, std.array, std.range, std.math; int read() { return readln.chomp.to!int; } void main() { int n = read(); writeln(n&1 ? 2*n : n); }
D
import std.stdio; import std.algorithm; import std.conv; import std.array; import std.string; void main() { int n = readln.chomp.to!int; auto L = readln.chomp.split.map!(to!int).array; int[] is_alive = new int[n]; for (int i = n - 1; i > 0; i--) { if (L[i] == 0) continue; int idx = max(0, i - L[i]); is_alive[idx] += 1; is_alive[i] -= 1; } int ans = 0; for (int i = 1; i < n; ++i) is_alive[i] += is_alive[i - 1]; for (int i = 0; i < n; ++i) if (is_alive[i] == 0) ans++; ans.writeln; }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; void times(alias fun)(int n) { foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } // fold was added in D 2.071.0. template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } void main() { long n = readln.chomp.to!long; long s = readln.chomp.to!long; if (n == s) { (n+1).writeln; return; } long t = n.to!double.sqrt.to!long+2; foreach(b; 2..t) { if (check(b, n, s)) { b.writeln; return; } } foreach(p; iota(1, t).retro) { long b = (n-s)/p + 1; if (check(b, n, s)) { b.writeln; return; } } (-1).writeln; } bool check(long b, long n, long s) { long f(long b, long n) { return n%b + (n<b ? 0 : f(b, n/b)); } return b>1 && f(b, n) == s; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string; void main() { auto S = readln.chomp; auto N = S.length.to!int; long ans = 0; long w = 0; foreach_reverse (i; 0..N) { if (S[i] == 'W') { w += 1; } else { ans += w; } } ans.writeln; }
D
void main() { int[] tmp = readln.split.to!(int[]); int a = tmp[0], b = tmp[1], c = tmp[2]; writeln(c % gcd(a, b) == 0 ? "YES" : "NO"); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio; import std.algorithm; import std.math; import std.conv; import std.string; T readNum(T)(){ return readStr.to!T; } T[] readNums(T)(){ return readStr.split.to!(T[]); } string readStr(){ return readln.chomp; } void main(){ auto s = readStr; int[char] dic; foreach(c; s){ dic[c]++; } foreach(i; dic.byValue){ if(i != 2){ writeln("No"); return; } } writeln("Yes"); }
D
void main() { string s = readln.chomp; string t = "CODEFESTIVAL2016"; int cnt; foreach (i; 0 .. 16) { if (s[i] != t[i]) ++cnt; } cnt.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.conv, std.string; import std.algorithm, std.array, std.container; import std.numeric, std.math; import core.bitop; T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T)(in string str) { return str.split.to!(T[]); } long mod = pow(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; } struct UnionFind { void init(long n) { par = new long[](n); foreach (i; 0..n) par[i] = i; } long root(long i) { return par[i] == i ? i : (par[i] = root(par[i])); } bool same(long i, long j) { return root(i) == root(j); } void unite(long i, long j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; } long[] par; } void main() { auto N = RD!long; auto C = RD!long; auto x = new long[](N); auto v = new long[](N); foreach (i; 0..N) { x[i] = RD!long; v[i] = RD!long; } auto dp = new long[2][2][](N+1); foreach (i; 0..N) { dp[i+1][0][0] = dp[i][0][0] + v[i]; dp[i+1][0][1] = max(dp[i][0][1], dp[i+1][0][0] - x[i] * 2); } foreach_reverse (i; 0..N) { dp[i][1][0] = dp[i+1][1][0] + v[i]; dp[i][1][1] = max(dp[i+1][1][1], dp[i][1][0] - (C - x[i]) * 2); } long ans; foreach (i; 0..N) { auto cal1 = dp[i+1][0][0] - x[i]; auto cal2 = dp[i+1][1][1]; ans = max(ans, cal1 + cal2); } foreach_reverse (i; 0..N) { auto cal1 = dp[i][1][0] - (C - x[i]); auto cal2 = dp[i][0][1]; ans = max(ans, cal1 + cal2); } writeln(ans); stdout.flush(); }
D
import std.stdio, std.conv, std.string; import std.algorithm, std.array, std.container; import std.numeric, std.math; string my_readln() { return chomp(readln()); } long mod = pow(10, 9) + 7; long moda(long x, long y) { return (x + y) % mod; } long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; } long modm(long x, long y) { return (x * y) % mod; } void main() { //auto tokens = my_readln().split(); //auto N = tokens[0].to!uint; //auto K = tokens[1].to!uint; auto s = my_readln; auto t = my_readln; auto dp = new uint[][](3005, 3005); foreach (i, cs; s) { foreach (j, ct; t) { dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1]); if (cs == ct) { dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j] + 1); } //stderr.writeln(i, ":", j, " ", dp[i+1][j+1]); } } string ans; long i = s.length - 1, j = t.length - 1; while (i >= 0 && j >= 0) { if (s[i] == t[j]) { ans = s[i] ~ ans; --i; --j; } else if (dp[i+1][j] > dp[i][j+1]) --j; else --i; } writeln(ans); stdout.flush(); }
D
import std.stdio; import std.string; import std.algorithm; import std.conv; import std.array; import std.math; import std.range; void main() { foreach(line; stdin.byLine) { auto s = line.chomp; if(s == "0") return; writeln(reduce!"a+b"(0, s.map!"a-'0'")); } }
D
import std.stdio; import std.string; import std.conv; import std.math; void main() { string[] inputs = split(readln()); int A = to!int(inputs[0]); int B = to!int(inputs[1]); int result = A + B; if(result < 10) result.writeln; else "error".writeln; }
D
import std.algorithm, std.array, std.container, std.range, std.bitmanip; import std.numeric, std.math, std.bigint, std.random, core.bitop; import std.string, std.conv, std.stdio, std.typecons; void main() { while (true) { auto rd = readln.split.map!(to!int); auto h = rd[0], w = rd[1]; if (h == 0 && w == 0) break; foreach (_1; 0..h) { foreach (_2; 0..w) write('#'); writeln; } writeln; } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { int n = readint; writeln(n / 3); }
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; ) { long[3] h; foreach(c;S) if(c=='B') ++h[0]; else if(c=='S') ++h[1]; else ++h[2]; auto n = array(readln().split().map!(to!long)()); auto p = array(readln().split().map!(to!long)()); auto r = readln().chomp().to!long(); long lo=0, hi=1000000000000000; while(lo+1<hi) { auto m = (lo+hi)/2; long c = 0; foreach(i;0..3) c += p[i]*max(0,m*h[i]-n[i]); if(c<=r) lo=m; else hi=m; } writeln(lo); } }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; immutable long mod = 10^^9 + 7; void main() { int n; scan(n); auto s = iota(n + 1).array; long ans = 1; foreach (p ; 2 .. n + 1) { if (s[p] > 1) { int cnt = 1; for (int q = p; q < n + 1; q += p) { while (s[q] % p == 0) { cnt++; s[q] /= p; } } (ans *= cnt) %= mod; } } 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, 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 t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto n = RD; bool dfs(long x, bool turn) { if (x == 1) return !turn; if (x == 2 || x % 2) return turn; bool res = !turn; for (long i = 3; i*i <= x; ++i) { if (x % i) continue; if (i % 2) res |= dfs(x/i, !turn) == turn; if ((x / i) % 2) res |= dfs(i, !turn) == turn; } return res; } ans[ti] = dfs(n, true); } foreach (e; ans) { writeln(e ? "Ashishgup" : "FastestFinger"); } 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; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { readint(); auto ss = readints(); bool[int] map; foreach (s; ss) map[s] = true; readint(); auto ts = readints(); auto ans = ts.count!(e => (e in map) != null); writeln(ans); }
D
import std.stdio : writeln; void main() { int a,b; scan(a,b); writeln((a+b+1)/2); } 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.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto q = RD!int; auto ans = new long[](q); foreach (i; 0..q) { auto n = RD!int; auto k = RD!int; auto s = RD!string; auto dp = new long[3][](n+1); auto str = "RGB"; long cnt = k; foreach (j; 0..3) { foreach (l; 0..n) { dp[l+1][j] = dp[l][j]; if (s[l] != str[(j+l)%3]) ++dp[l+1][j]; if (l+1 >= k) { cnt = min(cnt, dp[l+1][j] - dp[l+1-k][j]); } } } ans[i] = cnt; } foreach (e; ans) { writeln(e); } stdout.flush(); debug readln(); }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { int x, y, z; scan(x, y, z); int ans = (x - z) / (y + z); 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, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; writeln(0); stdout.flush(); auto f = readln.chomp; if (f == "Vacant") return; int l = 0, r = N; for (;;) { auto m = (l+r)/2; writeln(m); stdout.flush(); auto x = readln.chomp; if (x == "Vacant") { return; } else if (m%2 == 0 && x == f || m%2 == 1 && x != f) { l = m; } else { r = m; } } }
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 a, int b) { for (int i = 0; i < (1 << 16); i++) { bool[] xs; for (int j = 0; j < 16; j++) { xs ~= (i & (1 << j)) == 0; } xs ~= xs[0]; bool ok = true; int x = 0; int y = 0; for (int j = 0; j < xs.length - 1; j++) { if (xs[j]) x++; else y++; if (xs[j] == xs[j + 1]) { ok = false; break; } } if (ok) { if (x >= a && y >= b) return true; if (x >= b && y >= a) return true; } } return false; } void main() { auto ab = readints; int a = ab[0], b = ab[1]; auto ans = calc(a, b); writeln(ans ? "Yay!" : ":("); }
D
void main() { dchar[] s = readln.chomp.to!(dchar[]); reverse(s); string[] words = ["dream", "dreamer", "erase", "eraser"]; dchar[][] rev = new dchar[][](4); foreach (i, x; words) { rev[i] = x.to!(dchar[]); reverse(rev[i]); } long index; bool ok = true; while (index < s.length) { bool check; foreach (x; rev) { long len = x.length; if (index + len > s.length) continue; if (s[index..index+len] == x) { check = true; index += len; break; } } if (!check) { ok = false; break; } } writeln(ok ? "YES" : "NO"); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D