code
stringlengths
4
1.01M
language
stringclasses
2 values
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto par=new int[](n); par[0]=-1; foreach(int i; 1..n){ int p; rd(p); par[i]=(p-1); } auto leaf=new bool[](n); foreach(i; 0..n){ auto c=count(par, i); if(c==0) leaf[i]=true; } foreach(i; 0..n)if(leaf[i]==false){ int c=0; foreach(j; 0..n){ if(par[j]==i && leaf[j]) c++; } if(c<3){writeln("No"); return;} } writeln("Yes"); } 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)); } } void wr(T...)(T x){ import std.stdio; foreach(e; x) write(e, " "); writeln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto n = RD!int; auto m = RD!int; auto s1 = RD!(char[]); auto s2 = RD!(char[]); int[] f(in char[] ss, in char[] tt) { auto p = new int[](m); int l; foreach (j; 0..m) { foreach (i; l..n) { if (ss[i] == tt[j]) { p[j] = i; l = i+1; break; } } } return p; } auto p1 = f(s1, s2); s1.reverse; s2.reverse; auto p2 = f(s1, s2); p2.reverse; int ans; foreach (i; 1..m) { auto w = (n - 1 - p2[i]) - p1[i-1]; ans.chmax(w); } writeln(ans); stdout.flush; debug readln; }
D
import std.algorithm; import std.conv; import std.math; import std.range; import std.stdio; import std.string; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto n = readln.strip.to !(int); auto a = readln.splitter.map !(to !(int)).array; int [int] k; foreach (ref c; a) { k[abs (c)] += 1; k[abs (c)] = min (k[abs (c)], 1 + (c != 0)); } k.byValue.sum.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.string; void solve() { auto N = readln.chomp.to!int; auto S = readln.chomp; int pos = -1; foreach (i; 0..N) if (S[i] == '8') { pos = i; break; } if (pos == -1 || N - pos < 11) { writeln("NO"); } else { writeln("YES"); } } void main() { auto T = readln.chomp.to!int; while (T--) solve; }
D
import std.stdio; import std.range; import std.array; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.container; import std.typecons; import std.random; import std.csv; import std.regex; import std.math; import core.time; import std.ascii; import std.digest.sha; import std.outbuffer; import std.numeric; import std.bigint; import core.checkedint; void main() { int x, y; readln.chomp.split.tie(x, y); if (x == y) { writeln = "="; } else { real a = y * log10(cast(real)x); real b = x * log10(cast(real)y); debug verbose(a, b); if (abs(a - b) < 1e-9) { writeln = "="; } else { writeln = a < b ? "<" : ">"; } } } void tie(R, Args...)(R arr, ref Args args) if (isRandomAccessRange!R || isArray!R) in { assert (arr.length == args.length); } body { foreach (i, ref v; args) { alias T = typeof(v); v = arr[i].to!T; } } void verbose(Args...)(in Args args) { stderr.write("["); foreach (i, ref v; args) { if (i) stderr.write(", "); stderr.write(v); } stderr.writeln("]"); }
D
import std.stdio, std.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!long); auto N = s[0]; auto K = s[1]; long x = K % 2 == 1 ? K / 2 : K / 2 - 1; x += 1; long hi = x; long lo = 0; while (hi - lo > 1) { long m1 = (hi + lo) / 2; long m2 = K - m1; if (m1 < m2 && m2 <= N) { hi = m1; } else { lo = m1; } } writeln(x - hi); }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto n = readln.strip.to !(int); auto c = readln.splitter.map !(to !(int)).array; auto a = readln.splitter.map !(to !(int)).array; auto b = readln.splitter.map !(to !(int)).array; long res = 0; long cur = c[n - 1] - 1; foreach_reverse (i; 1..n) { cur += 2; if (a[i] > b[i]) { swap (a[i], b[i]); } res = max (res, cur + b[i] - a[i]); cur = max (cur, b[i] - a[i]); cur += c[i - 1] - b[i]; cur += a[i] - 1; if (a[i] == b[i]) { cur = c[i - 1] - 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)); } 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 string[](t); foreach (ti; 0..t) { auto n = RD!int; auto m = RD!int; string[] a, b; foreach (i; 0..n) a ~= RD!string; foreach (i; 0..n-1) b ~= RD!string; auto cnt = new long[][](m, 26); foreach (i; 0..n) { foreach (j; 0..m) { ++cnt[j][a[i][j]-'a']; } } foreach (i; 0..n-1) { foreach (j; 0..m) { --cnt[j][b[i][j]-'a']; } } foreach (i; 0..n) { bool ok = true; foreach (j; 0..m) { if (cnt[j][a[i][j]-'a'] != 1) { ok = false; break; } } if (ok) { ans[ti] = a[i].idup; break; } } } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; import std.typecons; int n; rd(n); auto y=readln.split.to!(long[]); alias pt=Tuple!(long, "x", long ,"y"); pt[] f(long dy, long dx){ pt[] rt; foreach(x; 1..n){ long dy_=y[x]-y[0]; if(dy*x!=dy_*dx) rt~=pt(x, y[x]); } return rt; } bool g(long _dy, long _dx, pt[] ys){ if(ys.length==0) return false; if(ys.length==1) return true; long dy=ys[1].y-ys[0].y; long dx=ys[1].x-ys[0].x; if(_dy*dx!=dy*_dx) return false; foreach(i; 1..ys.length){ long dy_=ys[i].y-ys[0].y; long dx_=ys[i].x-ys[0].x; if(dy*dx_!=dy_*dx) return false; } return true; } foreach(x; 1..n){ long dy=y[x]-y[0]; // int dx=x; auto rs=f(dy, x); // writeln(x, " ", rs); if(g(dy, x, rs)){ writeln("Yes"); return; } } // l1={1}, l2={2, 3, ..., n}; long dydy=y[2]-y[1]; // int dxdx=2-1; pt[] rsrs; foreach(x; 2..y.length){ long dy_dy=y[x]-y[1]; if(dydy*(x-1)!=dy_dy*1) rsrs~=pt(x, y[x]); } if(rsrs.length==0){ if(y[1]-y[0]!=dydy){ writeln("Yes"); return; } } writeln("No"); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.stdio, std.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 int[](t); foreach (ti; 0..t) { auto xyz1 = RDA; auto xyz2 = RDA; auto x = min(xyz1[2], xyz2[1]); ans[ti] += x * 2; xyz1[0] -= x; xyz2[1] -= x; auto y = xyz2[2] - (xyz1[0]+xyz1[2]); if (y > 0) ans[ti] -= y * 2; } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.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 t = RD!int; auto ans = new string[](t); foreach (ti; 0..t) { auto s = RD!string; if (s.length >= 2) { if (s[$-2..$] == "po") { ans[ti] = "FILIPINO"; continue; } if (s.length >= 4) { if (s[$-4..$] == "desu" || s[$-4..$] == "masu") { ans[ti] = "JAPANESE"; continue; } if (s.length >= 5) { if (s[$-5..$] == "mnida") { ans[ti] = "KOREAN"; continue; } } } } } 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() { string s, t; scan(s); scan(t); int n = s.length.to!int, m = t.length.to!int; auto dp = new int[][](n + 1, m + 1); foreach (i ; 1 .. n + 1) { dp[i][0] = i; } foreach (j ; 1 .. m + 1) { dp[0][j] = j; } foreach (i ; 1 .. n + 1) { foreach (j ; 1 .. m + 1) { dp[i][j] = min(dp[i-1][j] + 1, dp[i][j-1] + 1, dp[i-1][j-1] + (s[i-1] != t[j-1])); } } writeln(dp[n][m]); } 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; void main() { const N = readln().chomp().to!long(); ulong sum = 0; foreach (i; 1 .. N + 1) { if (i % 3 == 0) continue; if (i % 5 == 0) continue; sum += i; } writeln(sum); }
D
void main() { long[] tmp = rdRow; long n = tmp[0], m = tmp[1], k = tmp[2]; foreach (i; 0 .. n+1) { foreach (j; 0 .. m+1) { long s = i * m + j * n - 2 * i * j; if (s == k) { "Yes".writeln; return; } } } "No".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 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 core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } enum MOD = (10 ^^ 9) + 7; void main() { long N = lread(); long X, Y; scan(X, Y); foreach (_; 1 .. N) { long T, A; scan(T, A); long na = (X + T - 1) / T; long nb = (Y + A - 1) / A; long n = max(na, nb); X = n * T; Y = n * A; } writeln(X + Y); }
D
import std; int calc(long k, int[] a) { a = [0] ~ a; auto used = new long[a.length]; used[] = -1; int p = 1; long step = 0; while (true) { if (step == k) { return p; } if (used[p] != -1) { // writeln("loop ", step - used[p]); auto m = (k - used[p]) % (step - used[p]); // writeln("m ", m); m += used[p]; for (int i = 0; i < a.length; i++) { if (used[i] == m) return i; } } used[p] = step++; // writeln(p, " -> ", a[p]); p = a[p]; } return 0; } void main() { long n, k; scan(n, k); auto a = readints; writeln(calc(k, a)); } void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T=string)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readints = reads!int;
D
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 calc(string s) { int ans = 0; for (int i = 1; i < s.length; i++) { if (s[i-1] != s[i]) ans++; } return ans; } void main() { auto s = read!string; writeln(calc(s)); }
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 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)() { string res; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { res ~= cast(char) c; c = getchar; } return res.strip.to!T; } template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } struct Factor { long n; long c; } Factor[] factors(long n) { Factor[] res; for (long i = 2; i ^^ 2 <= n; i++) { if (n % i != 0) continue; int c; while (n % i == 0) { n = n / i; c++; } res ~= Factor(i, c); } if (n != 1) res ~= Factor(n, 1); return res; } 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; } void main() { string N = scanElem!string; long[4][] dp = new long[4][N.length+1]; dp[0][0] = 1; foreach(i, c; N) { dp[i+1][0] = c=='?' ? dp[i][0]*3 : dp[i][0]; dp[i+1][1] = c=='?' ? dp[i][1]*3 : dp[i][1]; dp[i+1][2] = c=='?' ? dp[i][2]*3 : dp[i][2]; dp[i+1][3] = c=='?' ? dp[i][3]*3 : dp[i][3]; if(c=='A' || c=='?')dp[i+1][1] += dp[i][0]; if(c=='B' || c=='?')dp[i+1][2] += dp[i][1]; if(c=='C' || c=='?')dp[i+1][3] += dp[i][2]; dp[i+1][]%=10^^9+7; } writeln(dp[$-1][3]); }
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 x, a; scan(x, a); writeln(x < a ? 0 : 10); } 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
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std; // dfmt on void main() { auto X = aryread(); foreach (i; 0 .. 5) if (X[i] != i + 1) { writeln(i + 1); } }
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 Clue = Tuple!(long, "x", long, "y", long, "h"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); long cnt; void main() { auto s = sread(); if (s == "AAA" || s == "BBB") writeln("No"); else writeln("Yes"); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.stdio; import std.conv; import std.string; import std.algorithm; import std.array; void main() { auto tmp = readln.split.map!(s => s.chomp[0]).array; auto X = tmp[0]; auto Y = tmp[1]; writeln(X == Y ? "=" : X < Y ? "<" : ">"); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp.to!int; int[string] words; auto f = true; auto prev = readln.chomp; words[prev]++; foreach (i; 1..n) { auto w = readln.chomp; if (words.get(w, 0) || prev[$-1] != w[0]) { f = false; } else { words[w]++; } prev = w; } if (f) { writeln("Yes"); } else { writeln("No"); } }
D
void main() { int n = readln.chomp.to!int; int[] t = readln.split.to!(int[]); int m = readln.chomp.to!int; int[] p = new int[m], x = new int[m]; foreach (i; 0 .. m) { int[] tmp = readln.split.to!(int[]); p[i] = tmp[0], x[i] = tmp[1]; } foreach (i; 0 .. m) { writeln(x[i] + t[0..p[i]-1].sum + t[p[i]..$].sum); } } 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, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } /* i番目までの子供にj個の飴を分ける分け方の数 xs[i][j] j <= a[i] のとき xs[i][j] = sum(u; 0 ..= j) xs[i - 1][u] j > a[i] のとき xs[i][j] = sum(u; j - a[i] ..= j) xs[i - 1][u] これをそのまま実装するとTLEになるので、以下のように実装する j = 0 のとき xs[i][j] = 1 j <= a[i] のとき xs[i][j] = xs[i][j - 1] + xs[i - 1][j] j > a[i] のとき xs[i][j] = xs[i][j - 1] + xs[i - 1][j] - xs[i - 1, j - a[i] - 1] */ const long mod = 1_000_000_007; void main(){ int n = read.to!int; int k = read.to!int; int[] as = readln.chomp.split.map!(to!int).array; long[][] xs = [[]]; foreach(j; 0 .. k + 1){ if(j <= as[0]) xs[0] ~= 1; else xs[0] ~= 0; } debug writeln("i:0 xs:", xs); foreach(i; 1 .. n){ xs ~= [[]]; foreach(j; 0 .. k + 1){ if(j == 0) xs[i] ~= 1; else if(j <= as[i]) xs[i] ~= xs[i][j - 1] + xs[i - 1][j]; else xs[i] ~= xs[i][j - 1] + xs[i - 1][j] + mod - xs[i - 1][j - as[i] - 1]; xs[i][j] %= mod; } debug writeln("i:", i, " xs:", xs); } xs[n - 1][k].writeln; }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { auto s = iota(3).map!(i => readln.chomp.map!(ch => ch - 'a').array).array; int p = 0; while (!s[p].empty) { int np = s[p].front; s[p].popFront(); p = np; } writeln((p + 'A').to!char); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.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 dp = new long[](N + 2); dp[] = long.max; dp[0] = 0; foreach (i; 0 .. N) { dp[i + 1] = dp[i + 1].min(dp[i] + 1); { long tmp = 6; while (i + tmp <= N) dp[i + tmp] = dp[i + tmp].min(dp[i] + 1), tmp *= 6; } { long tmp = 9; while (i + tmp <= N) dp[i + tmp] = dp[i + tmp].min(dp[i] + 1), tmp *= 9; } } writeln(dp[N]); }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; import std.numeric; void main() { int n; int[] a; scan(n); a = readln.split.to!(int[]); int am = a.reduce!max; auto cnt = new int[](n); foreach (i ; 0 .. n) { cnt[a[i]]++; } foreach (aa ; 0 .. am + 1) { if (aa < (am + 1) / 2) { if (cnt[aa] > 0) { writeln("Impossible"); return; } } else if (aa == (am + 1) / 2) { if (am & 1) { if (cnt[aa] != 2) { writeln("Impossible"); return; } } else { if (cnt[aa] != 1) { writeln("Impossible"); return; } } } else { if (cnt[aa] < 2) { writeln("Impossible"); return; } } } writeln("Possible"); } struct Stack(T) { private: int N, peek; T[] data; public: this(int size) { N = size; data = new T[](N); } bool empty() @property { return peek == 0; } bool full() @property { return peek == N; } void push(T x) @property { assert(!full); data[peek++] = x; } void pop() @property { assert(!empty); --peek; } T top() @property { return data[peek - 1]; } void clear() @property { peek = 0; } int length() @property { return peek; } } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.algorithm, std.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 a, b, c; readV(a, b, c); writeln(min(b/a, c)); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void main() { auto N = readln.chomp; int s; foreach (c; N) s += (c - '0').to!int; writeln(s%9 == 0 ? "Yes" : "No"); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] 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, K; scan(N, K); auto X = new long[](10 ^^ 5 + 1); foreach (_; 0 .. N) { long a, b; scan(a, b); X[a] += b; } foreach (i; 0 .. X.length) { K -= X[i]; if (0 >= K) { writeln(i); return; } } }
D
void main() { long n, m; rdVals(n, m); long[][] edge = new long[][](n); foreach (i; 0 .. m) { long x, y; rdVals(x, y); --x, --y; edge[x] ~= y; } long[] dp = new long[n]; long dfs(long x) { if (dp[x]) return dp[x]; long result; foreach (e; edge[x]) { result = max(result, dfs(e)+1); } dp[x] = result; return result; } long result; foreach (i; 0 .. n) { if (!dp[i]) result = max(result, dfs(i)); } result.writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; enum double eps = 1.0e-9; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
import std.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; string[] ss; for (int i = 0; i < n; i++) { ss ~= read!string; } char[] ans; foreach (c; 'a'..'z'+1) { int cnt = int.max; foreach (s; ss) { cnt = min(cnt, s.count!(e => e == c)); if (cnt == 0) break; } if (cnt > 0) { ans ~= repeat(cast(char) c, cnt).array; } } writeln(ans); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); void main() { auto stack = DList!long(); auto s = sread(); long cnt; foreach(e; s) { if(stack.empty || stack.back == e) { stack.insertBack(e); } else { cnt += 2; stack.removeBack(); } } cnt.writeln(); }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // const long infty = 1_000_000_000_000_000_100; void solve(){ int n = read.to!int; long[] as; foreach(i; 0 .. n) as ~= read.to!long; long[long[4]] memo; long calc(long x, long y, int l, int r){ if(l + 1 == r) return 0; // else if([x, y, l, r] !in memo){ long res = infty; foreach(i; l + 1 .. r){ long temp = calc(x, x + y, l, i) + (x + y) * as[i] + calc(x + y, y, i, r); log("l:", l, "r:", r, "i:", i, "temp:", temp); res = min(res, temp); // } // memo[[x, y, l, r]] = res; } // else log("Found in memo."); // log("x:", x, "y:", y, "l:", l, "r:", r, "res:", memo[[x, y, l, r]]); // return memo[[x, y, l, r]]; return res; } long ans = as[0] + calc(1, 1, 0, n - 1) + as[n - 1]; ans.writeln; }
D
import std.stdio,std.conv,std.string; void main(){ foreach(_;0..readln.chomp.to!int){ int point,base,_out; while(1){ final switch(readln.chomp){ case "HIT": if(base==3)++point; else ++base; break; case "HOMERUN": point+=base+1; base=0; break; case "OUT": ++_out; break; } if(_out==3)break; } point.writeln; } }
D
void main() { long[] tmp = readln.split.to!(long[]); long n = tmp[0], t = tmp[1]; long[] a = readln.split.to!(long[]); long x = t; foreach (i; 0 .. n-1) { x += min(t, a[i+1]-a[i]); } x.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.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } 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 calc(int m, int d) { int ans = 0; for (int i = 1; i <= m; i++) { for (int j = 1; j <= d; j++) { int d1 = j % 10; int d10 = j / 10; if (d1 >= 2 && d10 >= 2 && i == d1 * d10) { ans++; } } } return ans; } void main() { int m, d; scan(m, d); writeln(calc(m, d)); }
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 S = readln.chomp; auto Q = readln.chomp.to!int; DList!dchar que; foreach (s; S) que.insertBack(s.to!dchar); bool flag = false; while (Q--) { auto s = readln.split; if (s[0] == "1") { flag ^= 1; } else { auto f = s[1].to!int; auto c = s[2].to!dchar; if ((f == 1 && !flag) || (f == 2 && flag)) { que.insertFront(c); } else { que.insertBack(c); } } } dchar[] ans; if (!flag) foreach (s; que) ans ~= s; else foreach_reverse (s; que) ans ~= s; ans.writeln; }
D
unittest { assert( [ "a" ].parse.expand.solve == "vowel" ); assert( [ "z" ].parse.expand.solve == "consonant" ); assert( [ "s" ].parse.expand.solve == "consonant" ); } import std.conv; import std.range; import std.stdio; import std.typecons; void main() { stdin.byLineCopy.parse.expand.solve.writeln; } auto parse( Range )( Range input ) if( isInputRange!Range && is( ElementType!Range == string ) ) { auto c = input.front; return tuple( c ); } auto solve( string c ) { if( c == "a" || c == "e" || c == "i" || c == "o" || c == "u" ) return "vowel"; else return "consonant"; }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto n = readln.strip.to !(int); auto a = readln.splitter.map !(to !(int)).array; int lo = int.max; int hi = 0; bool ok = true; foreach (i; 0..n) { lo = min (lo, a[i] - hi); hi = max (hi, a[i] - lo); ok &= (lo >= 0 && lo + hi == a[i]); debug {writeln (lo, " ", hi, " ", ok);} } writeln (ok ? "YES" : "NO"); } }
D
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; long n, s; void main() { scan(n); scan(s); if (n == s) { writeln(n + 1); return; } for (long b = 2; b*b <= n; b++) { if (f(b, n) == s) { writeln(b); return; } } long ans = n + 1; for (long p = 1; p*p < n; p++) { long b = (n - s) / p + 1; if (f(b, n) == s) { ans = b; } } writeln(ans == n + 1 ? -1 : ans); } long f(long b, long n) { if (b < 2) return 0; return n < b ? n : f(b, n / b) + (n % b); } 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.string, std.conv; import std.algorithm, std.array; auto solve(string s_) { immutable N = s_.to!int(); auto a = new int[][N]; foreach(i;1..N) a[readln.chomp.to!int()-1]~=i; int dfs(int i) { int m=0; foreach(int k,v;a[i].map!dfs().array().sort!"a>b"().array()) m=max(m,k+v+1); return m; } return dfs(0); } void main(){ for(string s; (s=readln.chomp()).length;) writeln(solve(s)); }
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 a, b, c; readV(a, b, c); auto v = new bool[](b); foreach (i; 1..b+1) { auto m = a*i%b; if (m == c) { writeln("YES"); return; } else if (v[m]) { writeln("NO"); return; } v[m] = true; } }
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 M = RD; auto A = new string[](N); auto B = new string[](M); foreach (i; 0..N) A[i] = RD!string; foreach (i; 0..M) B[i] = RD!string; bool ans; (){ foreach (y; 0..N-M+1) { foreach (x; 0..N-M+1) { bool ok = true; foreach (i; 0..M) { foreach (j; 0..M) { if (A[y+i][x+j] != B[i][j]) { ok = false; break; } } } if (ok) { ans = true; return; } } }}(); writeln(ans ? "Yes" : "No"); stdout.flush(); debug readln(); }
D
import std.stdio; import std.string; import std.conv; import std.array; void main() { auto reader = readln.split; int N = reader[0].to!int; int K = reader[1].to!int; string S = readln.chomp; int[] cnt; int beforeI = 0; if (S[0] == '0') cnt ~= 0; foreach (i; 0..N){ if (S[i] != S[beforeI]){ cnt ~= i - beforeI; beforeI = i; } } cnt ~= N - beforeI; if (S[N - 1] == '0') cnt ~= 0; ulong cntLen = cnt.length; int sumRange = K * 2 + 1; int sum = 0; foreach (i; 0..sumRange){ if (i >= cntLen) break; sum += cnt[i]; } int ans = sum; for (uint i = 2; i + sumRange - 1 < cntLen; i += 2){ sum -= cnt[i - 2]; sum -= cnt[i - 1]; sum += cnt[i + sumRange - 2]; sum += cnt[i + sumRange - 1]; if (sum > ans) ans = sum; } writeln(ans); }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto s = readln.chomp.to!(char[]); int sum = 0; foreach (c; s[1 .. $]) { if (c == 'E') sum++; } auto mn = sum; foreach (i; 1 .. n) { if (s[i - 1] == 'W') sum++; if (s[i] == 'E') sum--; mn = min(mn, sum); } 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 core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { long N = lread(); long i = 1; long ans; while (true) { char[4] t = ['0', '3', '5', '7']; string s; long j = i; bool[] b = new bool[](4); while (j != 0) { b[j % 4] = true; s = t[j % 4] ~ s; j /= 4; } if (!b[0] && b[1 .. $].all!(x => x)) { // writefln("%d %s", i, s); if (N < s.to!long) { writeln(ans); return; } ans++; } i++; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.typecons; alias Path = Tuple!(int, "from", int, "to", int, "weight"); auto make_node() { int[100][100] ret; foreach (ref l; ret) foreach (ref e; l) e = int.max / 2; return ret; } auto NS = make_node(); Path[1000] PS; void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; foreach (i; 0..M) { auto abc = readln.split.to!(int[]); auto a = abc[0] - 1; auto b = abc[1] - 1; auto c = abc[2]; PS[i] = Path(a, b, c); NS[a][b] = c; NS[b][a] = c; } foreach (k; 0..N) foreach (i; 0..N) foreach (j; 0..N) if (NS[i][j] > NS[i][k] + NS[k][j]) NS[i][j] = NS[i][k] + NS[k][j]; int cnt; foreach (p; PS[0..M]) if (p.weight != NS[p.from][p.to]) ++cnt; writeln(cnt); }
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 f = true; if (!(s.length % 2)) { foreach (i; 0..s.length/2) { if (s[i*2..i*2+2] != "hi") { f = false; break; } } } else { f = false; } if (f) { writeln("Yes"); } else { writeln("No"); } }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; auto s = readln.chomp.to!(char[]); char[] t; foreach (i; 0 .. (s.length)) { if (i % 2 == 0) { t ~= s[i]; } } writeln(t); } 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; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); void main() { auto n = lread(); auto a = aryread(); auto ans = new long[](2 * (10 ^^ 5) + 10); foreach (i; 0 .. n - 1) { ans[a[i]] += 1; } foreach (i; 1 .. n + 1) { writeln(ans[i]); } } //https://rclone.org/ void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
D
import std.stdio, std.conv, std.string, std.math; void main(){ auto ip = readln.split.to!(int[]); if(ip[0] + ip[1] < 10){ writeln(ip[0] + ip[1]); } else { writeln("error"); } }
D
import std.stdio,std.string,std.array; void main(){ string line,sw; size_t total=0; sw = readln().chomp(); while( 1 ){ line = readln().chomp(); if( line == "END_OF_TEXT" ){ break; } foreach( w ; line.split() ){ if( toLower(w) == toLower(sw) ) total++; } } writeln( total ); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container; long P = 10^^9+7; long[1001] F, RF; long pow(long x, long n) { long y = 1; while (n) { if (n%2 == 1) y = (y * x) % P; x = x^^2 % P; n /= 2; } return y; } long inv(long x) { return pow(x, P-2); } void init() { F[0] = F[1] = 1; foreach (i, ref x; F[2..$]) x = (F[i+1] * (i+2)) % P; { RF[$-1] = 1; auto x = F[$-1]; auto k = P-2; while (k) { if (k%2 == 1) RF[$-1] = (RF[$-1] * x) % P; x = x^^2 % P; k /= 2; } } foreach_reverse(i, ref x; RF[0..$-1]) x = (RF[i+1] * (i+1)) % P; } long comb(N)(N n, N k) { if (k > n) return 0; auto n_b = F[n]; // n! auto nk_b = RF[n-k]; // 1 / (n-k)! auto k_b = RF[k]; // 1 / k! auto nk_b_k_b = (nk_b * k_b) % P; // 1 / (n-k)!k! return (n_b * nk_b_k_b) % P; // n! / (n-k)!k! } long perm(N)(N n, N k) { if (k > n) return 0; auto n_b = F[n]; auto n_k_b = RF[n-k]; return (n_b * n_k_b) % P; } void main() { init(); auto nk = readln.split.to!(long[]); auto N = nk[0]; auto K = nk[1]; long r; foreach (k; 1..K+1) { long rr; foreach (e; 0..k) { (rr += comb(k, e) * pow(k-e, N) % P * (-1)^^e + P) %= P; } (r += rr * RF[k] % P) %= P; } writeln(r); }
D
import std.stdio; import std.string; import std.algorithm; import std.conv; import std.array; import std.math; void main() { foreach(line; stdin.byLine) { auto hw = line.chomp.split.map!(to!int); auto h = hw[0], w = hw[1]; if(h == 0 && w == 0) return; foreach(i; 0 .. h){ foreach(j; 0 .. w) write('#'); writeln(); } writeln(); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(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 T = RD; writeln((N+X-1)/X * T); stdout.flush; debug readln; }
D
import std.stdio; void position(int n, int row, int col, char[6][1000] rows) { rows[row][col] = rows[row][col + 1] = '+'; printf("YES\n"); for (int i = 0; i < n; i++) { printf("%s\n", rows[i].ptr); } } void main() { int n; scanf("%d", &n); char[6][1000] rows; for (int i = 0; i < n; i++) { scanf("%s", rows[i].ptr); } for (int i = 0; i < n; i++) { if (rows[i][0] == 'O' && rows[i][1] == 'O') { position(n, i, 0, rows); return; } if (rows[i][3] == 'O' && rows[i][4] == 'O') { position(n, i, 3, rows); return; } } printf("NO\n"); }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; void main(){ (readln.chomp.to!int / 3).writeln; }
D
import std.stdio, std.conv, std.string; import std.algorithm, std.array, std.container; import std.numeric, std.math; import core.bitop; 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!long; auto A = my_readln; auto B = my_readln; auto C = my_readln; ulong ans; foreach (i; 0..N) { ulong cnt; if (A[i] != B[i]) ++cnt; if (A[i] != C[i]) ++cnt; if (B[i] != C[i]) ++cnt; if (cnt == 2) ++ans; else if (cnt == 3) ans += 2; } writeln(ans); stdout.flush(); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "x", long, "y"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { auto s = sread(); long pcnt; foreach (e; s) pcnt += e == 'p' ? 1 : 0; writeln(s.length / 2 - pcnt); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
/+ dub.sdl: name "D" dependency "dcomp" version=">=0.7.3" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; // import dcomp.algorithm; int main() { Scanner sc = new Scanner(stdin); int n, k; sc.read(n, k); int[2][] p = new int[2][n]; foreach (i; 0..n) { sc.read(p[i][0], p[i][1]); } int calc(int l, int r, int o, int u) { int c; foreach (d; p) { if (l <= d[0] && d[0] <= r && o <= d[1] && d[1] <= u) { c++; } } return c; } long ans = 5 * (10L^^18); foreach (a; 0..n) { foreach (b; a..n) { foreach (c; b..n) { foreach (d; c..n) { auto xv = [p[a][0], p[b][0], p[c][0], p[d][0]]; auto yv = [p[a][1], p[b][1], p[c][1], p[d][1]]; int l = xv.minimum; int r = xv.maximum; int o = yv.minimum; int u = yv.maximum; int C = calc(l, r, o, u); if (C < k) continue; ans = min(ans, long(r-l)*(u-o)); } } } } writeln(ans); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } version (X86) static if (__VERSION__ < 2071) { import core.bitop : bsf, bsr, popcnt; int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; // import dcomp.array; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { FastAppender!(E[]) buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/algorithm.d */ // module dcomp.algorithm; import std.range.primitives; import std.traits : isFloatingPoint, isIntegral; T binSearch(alias pred, T)(T l, T r) if (isIntegral!T) { while (r-l > 1) { T md = (l+r)/2; if (!pred(md)) l = md; else r = md; } return r; } T binSearch(alias pred, T)(T l, T r, int cnt = 60) if (isFloatingPoint!T) { foreach (i; 0..cnt) { T md = (l+r)/2; if (!pred(md)) l = md; else r = md; } return r; } E minimum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed) if (isInputRange!Range && !isInfinite!Range) { import std.algorithm, std.functional; return reduce!((a, b) => binaryFun!pred(a, b) ? a : b)(seed, range); } ElementType!Range minimum(alias pred = "a < b", Range)(Range range) { assert(!range.empty, "range must not empty"); auto e = range.front; range.popFront; return minimum!pred(range, e); } E maximum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed) if (isInputRange!Range && !isInfinite!Range) { import std.algorithm, std.functional; return reduce!((a, b) => binaryFun!pred(a, b) ? b : a)(seed, range); } ElementType!Range maximum(alias pred = "a < b", Range)(Range range) { assert(!range.empty, "range must not empty"); auto e = range.front; range.popFront; return maximum!pred(range, e); } Rotator!Range rotator(Range)(Range r) { return Rotator!Range(r); } struct Rotator(Range) if (isForwardRange!Range && hasLength!Range) { size_t cnt; Range start, now; this(Range r) { cnt = 0; start = r.save; now = r.save; } this(this) { start = start.save; now = now.save; } @property bool empty() { return now.empty; } @property auto front() { assert(!now.empty); import std.range : take, chain; return chain(now, start.take(cnt)); } @property Rotator!Range save() { return this; } void popFront() { cnt++; now.popFront; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/array.d */ // module dcomp.array; T[N] fixed(T, size_t N)(T[N] a) {return a;} struct FastAppender(A, size_t MIN = 4) { import std.algorithm : max; import std.conv; import std.range.primitives : ElementEncodingType; import core.stdc.string : memcpy; private alias T = ElementEncodingType!A; private T* _data; private uint len, cap; @property size_t length() const {return len;} bool empty() const { return len == 0; } void reserve(size_t nlen) { import core.memory : GC; if (nlen <= cap) return; void* nx = GC.malloc(nlen * T.sizeof); cap = nlen.to!uint; if (len) memcpy(nx, _data, len * T.sizeof); _data = cast(T*)(nx); } void free() { import core.memory : GC; GC.free(_data); } void opOpAssign(string op : "~")(T item) { if (len == cap) { reserve(max(MIN, cap*2)); } _data[len++] = item; } void insertBack(T item) { this ~= item; } void removeBack() { len--; } void clear() { len = 0; } ref inout(T) back() inout { assert(len); return _data[len-1]; } ref inout(T) opIndex(size_t i) inout { return _data[i]; } T[] data() { return (_data) ? _data[0..len] : null; } } /* This source code generated by dcomp and include dcomp's source code. dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp) dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt) */
D
// Cheese-Cracker: cheese-cracker.github.io void theCode(){ ll n = scan; ll tim = (n / 2) * 5; if(n % 2 != 0) tim += 5; tim = max(tim, 15); writeln(tim); } void main(){ long tests = scan; // Toggle! while(tests--) theCode(); } /********* That's All Folks! *********/ import std.stdio, std.random, std.functional, std.container, std.algorithm; import std.numeric, std.range, std.typecons, std.string, std.math, std.conv; 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
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.0.2" +/ import std.algorithm, std.range, std.stdio, std.math, std.conv, core.bitop; // import dcomp.scanner; bool solve(bool[] a) { int n = (a.length.to!int + 1) / 2; int l = n-1; while (l) { if (a[l-1] == a[l]) break; l--; } int r = n-1; while (r < 2*n-2) { if (a[r] == a[r+1]) break; r++; } if ((n-1)-l < r-(n-1)) { return a[l]; } else { return a[r]; } } int main() { import std.conv; auto sc = new Scanner(); int n; int[] a; sc.read(n, a); int l = 0, r = 2*n + 10; // x以上 while (r-l > 1) { int md = (l+r)/2; if (solve(a.map!(a => (md <= a)).array)) { l = md; } else { r = md; } } writeln(l); return 0; } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File, readln, stdin; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeString, isDynamicArray; import std.algorithm : map; File f; this(File f = stdin) { this.f = f; } string[] buf; bool succ() { while (!buf.length) { if (f.eof) return false; buf = readln.split; } return true; } int read(Args...)(auto ref Args args) { foreach (i, ref v; args) { if (!succ()) return i; alias VT = typeof(v); static if (!isSomeString!VT && isDynamicArray!VT) { v = buf.map!(to!(ElementType!VT)).array; buf.length = 0; } else { v = buf.front.to!VT; buf.popFront(); } } return args.length; } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.math; void main() { int a, b, c, x, y; scan(a, b, c, x, y); int ans = 1<<30; foreach (i ; 0 .. 2 * 10^^5 + 1) { int xr = max(0, x - i / 2); int yr = max(0, y - i / 2); int tmp = xr * a + yr * b + i * c; ans = min(ans, tmp); } writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.algorithm; import std.container; import std.conv; import std.functional; import std.math; import std.meta; import std.random; import std.range; import std.stdio; import std.string; import std.traits; import std.typecons; void main() { int ans = 0; foreach (c; readln.chomp) if (c == '+') ans++; else ans--; ans.writeln; }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; void main() { auto n = readln.chomp.to!int; int[][][] f = new int[][][](n, 5, 2); int[][] p = new int[][](n, 11); for (int i = 0; i < n; ++i) { auto l = readln.chomp.split.map!(to!int).array; for (int j = 0; j < 10; j += 2) { f[i][j / 2][0] = l[j]; f[i][j / 2][1] = l[j + 1]; } } for (int i = 0; i < n; ++i) { auto l = readln.chomp.split.map!(to!int).array; for (int j = 0; j < 11; ++j) { p[i][j] = l[j]; } } int maxBenefit = -1000000007; for (int i = 1; i < (2 << 9); ++i) { int sum; int[] c = new int[](n); for (int j = 0; j < 10; ++j) { //writeln(j, " = ", (i >> j) & 1); if (((i >> j) & 1) == 1) { for (int k = 0; k < n; ++k) { //writeln(k, " ", j / 2, " " , j % 2, " = ", f[k][j / 2][j % 2]); if (f[k][j / 2][j % 2] == 1) { c[k]++; } } } } for (int j = 0; j < n; ++j) { sum += p[j][c[j]]; } if (maxBenefit < sum) { maxBenefit = sum; } } maxBenefit.writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; int[500][500] MS, CS; void main() { auto nmq = readln.split.to!(int[]); auto N = nmq[0]; auto M = nmq[1]; auto Q = nmq[2]; foreach (_; 0..M) { auto lr = readln.split.to!(int[]); ++MS[lr[0]-1][lr[1]-1]; } foreach_reverse (s; 0..N) { foreach (e; s..N) { CS[s][e] = MS[s][e] + (e ? CS[s][e-1] : 0) + (s+1 < N ? CS[s+1][e] : 0) - (e && s+1 < N && s+1 <= e-1 ? CS[s+1][e-1] : 0); } } foreach (_; 0..Q) { auto pq = readln.split.to!(int[]); writeln(CS[pq[0]-1][pq[1]-1]); } }
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() { auto c = new string[](2); c[0] = readln.chomp; c[1] = readln.chomp; string ans = (equal(c[0], c[1].retro)) ? "YES" : "NO"; 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.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto A = RD; auto B = RD; writeln(A <= 8 && B <= 8 ? "Yay!" : ":("); 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); } enum MOD = (10 ^^ 9) + 7; long[] s, t; void main() { long A, B, Q; scan(A, B, Q); s = new long[](A); t = new long[](B); foreach (i; 0 .. A) scan(s[i]); foreach (i; 0 .. B) scan(t[i]); foreach (_; 0 .. Q) { long x; scan(x); solve(x).writeln(); } } long solve(long x) { if (s.length <= 2 && t.length <= 2) return eval(s, t, x); if (x <= s[0] && x <= t[0]) return eval(s[0 .. 1], t[0 .. 1], x); if (s[$ - 1] <= x && t[$ - 1] <= x) return eval(s[$ - 1 .. $], t[$ - 1 .. $], x); long si = bisect(x, s); long ti = bisect(x, t); return eval(s[si .. si + 2], t[ti .. ti + 2], x); } long bisect(long x, long[] ary) { long yes, no; yes = 0; no = ary.length; while (1 < no - yes) { long mid = (yes + no) / 2; if (ary[mid] < x) { yes = mid; } else { no = mid; } } return yes; } long eval(long[] ss, long[] ts, long x) { long ans = long.max; foreach (s; ss) foreach (t; ts) { ans = ans.min(min(abs(x - s), abs(x - t)) + abs(s - t)); } return ans; }
D
import std.stdio, std.string, std.conv; void main() { while(true) { int num = readln.chomp.to!int, cnt; if(!num)break; while(num) cnt += num /= 5; cnt.writeln; } }
D
import std.stdio; import std.string; import std.format; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.concurrency; import std.traits; import std.uni; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; enum long INF = long.max/5; enum long MOD = 10L^^9+7; void main() { bool[] xs = readln.chomp.map!"a=='o'".array; long n = xs.length; long t = xs.count!"a"; writeln((15 - n) + t>=8 ? "YES" : "NO"); } // ---------------------------------------------- void times(alias fun)(long n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(long n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { // `(x+y-1)/y` will only work for positive numbers ... T t = x / y; if (t * y < x) t++; return t; } T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { T t = x / y; if (t * y > x) t--; return t; } ref T ch(alias fun, T, S...)(ref T lhs, S rhs) { return lhs = fun(lhs, rhs); } unittest { long x = 1000; x.ch!min(2000); assert(x == 1000); x.ch!min(3, 2, 1); assert(x == 1); x.ch!max(100).ch!min(1000); // clamp assert(x == 100); x.ch!max(0).ch!min(10); // clamp assert(x == 10); } mixin template Constructor() { import std.traits : FieldNameTuple; this(Args...)(Args args) { // static foreach(i, v; args) { foreach(i, v; args) { mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v; } } } void scanln(Args...)(auto ref Args args) { enum sep = " "; enum n = Args.length; enum fmt = n.rep!(()=>"%s").join(sep) ~ "\n"; static if (__VERSION__ >= 2071) { readf!fmt(args); } else { enum argsTemp = n.iota.map!( i => "&args[%d]".format(i) ).join(", "); mixin( "readf(fmt, " ~ argsTemp ~ ");" ); } } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { private template RebindableOrUnqual(T) { static if (is(T == class) || is(T == interface) || isDynamicArray!T || isAssociativeArray!T) alias RebindableOrUnqual = Rebindable!T; else alias RebindableOrUnqual = Unqual!T; } private auto extremum(alias map, alias selector = "a < b", Range)(Range r) if (isInputRange!Range && !isInfinite!Range && is(typeof(unaryFun!map(ElementType!(Range).init)))) in { assert(!r.empty, "r is an empty range"); } body { alias Element = ElementType!Range; RebindableOrUnqual!Element seed = r.front; r.popFront(); return extremum!(map, selector)(r, seed); } private auto extremum(alias map, alias selector = "a < b", Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seedElement) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void) && is(typeof(unaryFun!map(ElementType!(Range).init)))) { alias mapFun = unaryFun!map; alias selectorFun = binaryFun!selector; alias Element = ElementType!Range; alias CommonElement = CommonType!(Element, RangeElementType); RebindableOrUnqual!CommonElement extremeElement = seedElement; // if we only have one statement in the loop, it can be optimized a lot better static if (__traits(isSame, map, a => a)) { // direct access via a random access range is faster static if (isRandomAccessRange!Range) { foreach (const i; 0 .. r.length) { if (selectorFun(r[i], extremeElement)) { extremeElement = r[i]; } } } else { while (!r.empty) { if (selectorFun(r.front, extremeElement)) { extremeElement = r.front; } r.popFront(); } } } else { alias MapType = Unqual!(typeof(mapFun(CommonElement.init))); MapType extremeElementMapped = mapFun(extremeElement); // direct access via a random access range is faster static if (isRandomAccessRange!Range) { foreach (const i; 0 .. r.length) { MapType mapElement = mapFun(r[i]); if (selectorFun(mapElement, extremeElementMapped)) { extremeElement = r[i]; extremeElementMapped = mapElement; } } } else { while (!r.empty) { MapType mapElement = mapFun(r.front); if (selectorFun(mapElement, extremeElementMapped)) { extremeElement = r.front; extremeElementMapped = mapElement; } r.popFront(); } } } return extremeElement; } private auto extremum(alias selector = "a < b", Range)(Range r) if (isInputRange!Range && !isInfinite!Range && !is(typeof(unaryFun!selector(ElementType!(Range).init)))) { return extremum!(a => a, selector)(r); } // if we only have one statement in the loop it can be optimized a lot better private auto extremum(alias selector = "a < b", Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seedElement) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void) && !is(typeof(unaryFun!selector(ElementType!(Range).init)))) { return extremum!(a => a, selector)(r, seedElement); } auto minElement(alias map = (a => a), Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { return extremum!map(r); } auto minElement(alias map = (a => a), Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void)) { return extremum!map(r, seed); } auto maxElement(alias map = (a => a), Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { return extremum!(map, "a > b")(r); } auto maxElement(alias map = (a => a), Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void)) { return extremum!(map, "a > b")(r, seed); } } // popcnt with ulongs was added in D 2.071.0 static if (__VERSION__ < 2071) { ulong popcnt(ulong x) { x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L); x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L); x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL); x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL); x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL); x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL); return x; } }
D
import std.stdio, std.string, std.conv; import std.algorithm, std.array, std.range; auto solve(string S_) { auto NM=S_.split.map!(to!int)(); immutable N=NM[0], M=NM[1]; auto A=new int[][N]; foreach(ref a;A) a=readln.split.map!(to!int).array(); auto c=new int[M+1]; foreach(a;A) ++c[a[0]]; int m=N; bool[int] s; foreach(r;0..M) { int k=-1,j=-1; foreach(int i,v;c) if(k<v) k=v,j=i; m=min(m,k); if(r+1>=M) break; s[j]=true; foreach(ref a;A) while(a[0] in s) --c[a[0]], a.popFront, ++c[a[0]]; } return m; } void main(){ for(string s; (s=readln.chomp()).length;) writeln(solve(s)); }
D
import std.stdio; import std.string; import std.conv; void main() { int[] arr = readln.chomp.split.to!(int[]); int d = arr[0], n = arr[1]; if (d == 0) { if (n == 100) writeln(101); else writeln(n); } else if (d == 1) { if (n == 100) writeln(100 * n + 100); else writeln(100 * n); } else if (d == 2) { if (n == 100) writeln(10000 * n + 10000); else writeln(10000 * n); } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc2(int lo, int hi, int k, int[] xs) { int ans = 0; int x = lo; while (x-1 >= 0) { ans++; x -= k - 1; } x = hi; while (x+1 < xs.length) { ans++; x += k - 1; } // writeln(lo, " ", hi, " ans ", ans); return ans; } int calc(int k, int[] xs) { int p = 0; for (int i = 0; i < xs.length; i++) { if (xs[i] == 1) { p = i; break; } } int ans = int.max; int lo = max(0, p - k + 1); int hi = min(xs.length, p + k - 1); for (int i = max(0, p - k + 1); i <= p; i++) { ans = min(ans, 1 + calc2(i, i + k - 1, k, xs)); } return ans; } void main() { auto nk = readints; int k = nk[1]; auto xs = readints; auto ans = calc(k, xs); writeln(ans); }
D
import std.stdio; import std.string; import std.format; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; import std.traits; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; const long INF = long.max/3; const long MOD = 10L^^9+7; void main() { int N = 3; int[][] css = N.rep!(() =>readln.split.to!(int[])); int[] as = new int[N]; int[] bs = new int[N]; as[0] = 0; foreach(j; 0..N) { bs[j] = css[0][j] - as[0]; } foreach(i; 1..N) { as[i] = css[i][0] - bs[0]; } foreach(i; 0..N) foreach(j; 0..N) { if (css[i][j] != as[i] + bs[j]) { "No".writeln; return; } } "Yes".writeln; } // ---------------------------------------------- void scanln(Args...)(auto ref Args args) { import std.meta; template getFormat(T) { static if (isIntegral!T) { enum getFormat = "%d"; } else static if (isFloatingPoint!T) { enum getFormat = "%g"; } else static if (isSomeString!T || isSomeChar!T) { enum getFormat = "%s"; } else { static assert(false); } } enum string str = [staticMap!(getFormat, Args)].join(" ") ~ "\n"; // readf!str(args); mixin("str.readf(" ~ Args.length.iota.map!(i => "&args[%d]".format(i)).join(", ") ~ ");"); } void times(alias fun)(int n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } T ceil(T)(T x, T y) if (__traits(isIntegral, T)) { // `(x+y-1)/y` will only work for positive numbers ... T t = x / y; if (t * y < x) t++; return t; } T floor(T)(T x, T y) if (__traits(isIntegral, T)) { T t = x / y; if (t * y > x) t--; return t; } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { auto minElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto minimum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b < minimum) { element = a; minimum = b; } } return element; } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto maximum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b > maximum) { element = a; maximum = b; } } return element; } }
D
import std.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 n = RD!int; auto k = RD!int; auto s = RD!string; foreach (i; 0..n) { if (i == 0) { if (n == 1) { if (k != 0) write("0"); else write(s[i]); } else { if (k == 0 || s[i] == '1') write(s[i]); else { --k; write("1"); } } } else { if (k == 0 || s[i] == '0') write(s[i]); else { --k; write("0"); } } } writeln(); stdout.flush(); debug readln(); }
D
import std.stdio; import std.ascii; import std.conv; import std.string; import std.algorithm; import std.range; import std.functional; import std.math; import core.bitop; void main() { auto n = readln.chomp.to!long; string result; while (n != 0) { long m = n % -2; // writeln([n,m]); n /= -2; result = ((m == 0) ? "0" : "1") ~ result; if (m == -1) { n++; } } if (result.length == 0) { writeln("0"); } else { writeln(result); } }
D
import std.stdio; import std.conv; import std.algorithm; import std.range; import std.string; import std.typecons; import std.math; import std.range; auto gcd(T)(T x, T y) { if (x < y) x.swap(y); while (y > 0) { auto r = x % y; x = y; y = r; } return x; } void main() { auto a = readln().strip.split(" ").map!(to!int); auto x = a[0]; auto y = a[1]; writeln(gcd(x, y)); }
D
import std.stdio, std.conv, std.array, std.string; import std.algorithm; import std.container; import std.range; import core.stdc.stdlib; import std.math; void main() { auto N = readln.chomp.to!ulong; ulong answer = N-1; for(ulong i = 1; i * i <= N; i++) { if (N % i != 0) continue; auto j = N/i; auto candidate = i-1 + j-1; if (candidate < answer) answer = candidate; } writeln(answer); }
D
void main() { int x = readln.chomp.to!int; int d = x / 100, r = x % 100; writeln(d >= (r + 4) / 5 ? 1 : 0); } 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
void main() { long n = readln.chomp.to!long; long[] a = readln.split.to!(long[]); long[] l = new long[n+1]; foreach (i; 0 .. n) { l[i+1] = gcd(l[i], a[i]); } long[] r = new long[n+1]; foreach_reverse (i; 0 .. n) { r[i] = gcd(r[i+1], a[i]); } long result; foreach (i; 0 .. n) { result = max(result, gcd(l[i], r[i+1])); } result.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.string, std.conv; import std.array, std.algorithm, std.range; void main() { int c=0; foreach(s;stdin.byLine()) { bool f=true; foreach(i;0..s.length/2+1) if(s[i]!=s[$-i-1]) { f=false;break;} if(f) ++c; } writeln(c); }
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 = 100_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, "before", long, "after"); alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less); void main() { long n, k; scan(n, k); long cnt; foreach (i; iota(n).map!(x => x + 1).array()) { if (2 * i % k == 0) { auto a = (n + i) / k; auto b = i / k; cnt += (a - b) ^^ 2; } } cnt.writeln(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import 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() { auto n = readln.chomp.to!int; auto ai = readln.split.map!(to!long); writeln(ai.reduce!(min), " ", ai.reduce!(max), " ", ai.sum); }
D
import std.stdio; import std.string; import std.conv; import std.range; import std.array; import std.algorithm; void main(){ auto S=readln.chomp; if(S[0]=='a' && S[1]=='b' && S[2]=='c')writeln("Yes"); else if(S[0]=='a' && S[1]=='c' && S[2]=='b')writeln("Yes"); else if(S[0]=='b' && S[1]=='a' && S[2]=='c')writeln("Yes"); else if(S[0]=='b' && S[1]=='c' && S[2]=='a')writeln("Yes"); else if(S[0]=='c' && S[1]=='b' && S[2]=='a')writeln("Yes"); else if(S[0]=='c' && S[1]=='a' && S[2]=='b')writeln("Yes"); else writeln("No"); }
D
import std.stdio; import std.string; import std.conv; void main() { while (true) { auto input = readln().split; if (input.length == 0) break; writeln(to!int(input[0]) + to!int(input[1])); } }
D
void main() { writeln(48 - readln.chomp.to!int); } 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
module app; import core.bitop; import std.algorithm; import std.array; import std.bigint; import std.container.rbtree; import std.conv; import std.stdio; import std.string; import std.traits; struct Input { string s; } void parseInput(T)(out Input input, T adapter) { with (input) { s = adapter.readln(); } } struct Output { } auto main2(Input* input) { if (input.s[$-1] == 's') return input.s ~ "es"; else return input.s ~ "s"; } alias retType = ReturnType!main2; static if (!is(retType == void)) { unittest { writeln("begin unittest"); } auto _placeholder_ = ReturnType!main2.init; unittest // example1 { string example = `apple`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == "apples"); } unittest // example2 { string example = `bus`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == "buses"); } unittest // example3 { string example = `box`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == "boxs"); } unittest { writeln("end unittest"); } void parseExample(out Input input, string example) { parseInput(input, new StringAdapter(example)); } } class StdinAdapter { // readf!"%s"は使えない // https://issues.dlang.org/show_bug.cgi?id=19820 uint readf(alias format, A...)(auto ref A args) { return std.stdio.readf!format(args); } string readln() { return std.stdio.readln().strip(); } } class StringAdapter { import std.format; string _s; this(string input) { _s = input; } uint readf(alias format, A...)(auto ref A args) { return _s.formattedRead!format(args); } string readln() { string ret; auto i = _s.countUntil("\n"); if (i == -1) { ret = _s; _s = ""; } else { ret = _s[0..i]; _s = _s[i+1..$]; } return ret; } } void printResult(T)(T result) { static if (isFloatingPoint!T) writefln("%f", result); else writeln(result); } void main() { Input input = void; parseInput(input, new StdinAdapter()); static if (is(retType == void)) main2(&input); else { auto result = main2(&input); printResult(result); } }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons, std.functional; void main() { readln.chomp.to!int.pipe!(K => (K+1)/2 * (K/2)).writeln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { int a, b; scan(a, b); auto map = new char[][](100, 100); fillAll(map, '.'); foreach (i ; 0 .. 50) { foreach (j ; 0 .. 50) { map[i][j] = '#'; } } a--; foreach (i ; 0 .. 25) { foreach (j ; 0 .. 25) { if (a == 0) { goto B1; } a--; map[2*i][2*j] = '.'; } } B1: b--; foreach (i ; 0 .. 25) { foreach (j ; 0 .. 50) { if (b == 0) { goto B2; } b--; map[50 + 2*i + 1][2*j] = '#'; } } B2: writeln("100 100"); foreach (i ; 0 .. 100) { writeln(map[i]); } } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto s = readln.chomp; writeln(s.canFind('9') ? "Yes" : "No"); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto nt = readln.chomp.split.to!(int[]); auto res = int.max; foreach(_; 0..nt[0]) { auto tmp = readln.chomp.split.to!(int[]); if (tmp[1] > nt[1]) { continue; } else if (res > tmp[0]) { res = tmp[0]; } } if (res != int.max) { writeln(res); } else { writeln("TLE"); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string; void main() { auto a = readln.chomp.to!int; auto b = readln.chomp.to!int; auto h = readln.chomp.to!int; writeln((a + b) * h / 2); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; long powMod(long x, long k, long m) { if (k == 0) return 1; if (k % 2 == 0) return powMod(x * x % m, k / 2, m); return x * powMod(x, k - 1, m) % m; } void main() { auto mn = readints; int m = mn[0], n = mn[1]; const int mod = 10^^9 + 7; auto ans = powMod(m, n, mod); writeln(ans); }
D
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math, std.container, std.typecons; import core.stdc.stdio; // foreach, foreach_reverse, writeln alias P = Tuple!(int,int); int k; class UnionFind { int score; int[] data, cnt; this(int n) { data = new int[n]; cnt = new int[n]; data[] = -1; score = 0; } int find(int v) { if (data[v] < 0) return v; return data[v] = find(data[v]); } int calc(int v) { v = find(v); int size = -data[v]; return min(max(0,size-k+1), cnt[v]); } void add(int v, int x) { score -= calc(v); cnt[find(v)] += x; score += calc(v); } bool join(int u, int v) { u = find(u); v = find(v); if (u == v) return false; if (data[u] > data[v]) swap(u,v); score -= calc(u); score -= calc(v); data[u] += data[v]; cnt[u] += cnt[v]; data[v] = u; score += calc(u); return true; } } void main() { int n, q; scanf("%d%d%d", &n, &k, &q); int[] a = new int[n]; foreach (i; 0..n) scanf("%d", &a[i]); int l = -1, r = 1_000_000_000; bool judge(int x) { UnionFind uf = new UnionFind(n); P[] events; foreach (i; 0..n) { events ~= P(a[i],n+i); events ~= P(a[i]-x,i); } events.sort.reverse; bool[] added = new bool[n]; foreach (event; events) { int i = event[1]; if (i < n) { uf.add(i,-1); //writeln("del ",i," ",uf.score); } else { i -= n; uf.add(i,1); if (i && added[i-1]) uf.join(i-1,i); if (i+1 < n && added[i+1]) uf.join(i+1,i); added[i] = true; //writeln("add ",i," ",uf.score); if (uf.score >= q) return true; } } return false; } while (l+1 < r) { int mid = (l+r)/2; if (judge(mid)) r = mid; else l = mid; } writeln(r); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); void main() { long a, b, ab, x, y; scan(a, b, ab, x, y); long cost1, cost2, cost3, cost4; cost1 = a * x + b * y; cost2 = ab * 2 * max(x, y); long tmp; tmp = ab * min(x, y) * 2; // writeln("tmp", tmp); if (x > y) { cost3 = tmp + a * (max(x, y) - min(x, y)); } else { cost4 = tmp + b * (max(x, y) - min(x, y)); } // writeln(cost1); // writeln(cost2); // writeln(cost3); // writeln(cost4); if (cost3 == 0) { writeln(min(cost1, cost2, cost4)); } if (cost4 == 0) { writeln(min(cost1, cost2, cost3)); } } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { long N = lread(); auto T = new long[](N); foreach (i; 0 .. N) T[i] = lread(); // gcd(10, 1000000000000000000).writeln; // writeln(10 * 1000000000000000000); long lcm(long a, long b) { // writefln("%d %d", a, b); return a * (b / gcd(a, b)); } T.reduce!lcm().writeln(); }
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 N = lread(); writeln((N * 800) - ((N/15) * 200)); }
D