code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n; readV(n); if (n == 1) { writeln("Hello World"); } else { int a; readV(a); int b; readV(b); writeln(a+b); } }
D
import std.algorithm; import std.array; import std.ascii; import std.bigint; import std.complex; import std.container; import std.conv; import std.functional; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } void readlnTo(T...)(ref T t) { auto s = readln().split(); assert(s.length == t.length); foreach(ref ti; t) { ti = s[0].to!(typeof(ti)); s = s[1..$]; } } const real eps = 1e-10; void main(){ auto n = readLong(); auto a = readLongs(); int ans = 1; int i = 1; long sign = a[1] - a[0]; while(i < a.length) { while(i < a.length) { if(sign == 0) { sign = a[i] - a[i-1]; } if((a[i] - a[i-1]) * sign < 0) { ++ans; sign = 0; } ++i; } } writeln(ans); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); void main() { auto x = lread(); if ((400 <= x) && (x <= 599)) { writeln(8); } else if ((600 <= x) && (x <= 799)) { writeln(7); } else if ((800 <= x) && (x <= 999)) { writeln(6); } else if ((1000 <= x) && (x <= 1199)) { writeln(5); } else if ((1200 <= x) && (x <= 1399)) { writeln(4); } else if ((1400 <= x) && (x <= 1599)) { writeln(3); } else if ((1600 <= x) && (x <= 1799)) { writeln(2); } else if ((1800 <= x) && (x <= 1999)) { writeln(1); } } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto X = readln.chomp.to!int; auto DP = new int[][](6, 10^^5+1); int solve(int i, int x) { if (i == 6) return x == 0 ? 1 : -1; if (DP[i][x] != 0) return DP[i][x]; int r = solve(i+1, x); if (x >= 100 + i) r = max(r, solve(i, x - (100+i))); DP[i][x] = r; return DP[i][x]; } writeln(solve(0, X) == 1 ? 1 : 0); }
D
import std.stdio,std.string,std.conv; int main() { string s; int count_1 = 0,count_2 = 0; while((s = readln.chomp).length != 0) { string[] _s = s.split(","); int len1 = _s[0].to!int; int len2 = _s[1].to!int; int len3 = _s[2].to!int; if(len3*len3 == len1*len1 + len2*len2) count_1++; if(len1 == len2) count_2++; } writeln(count_1); writeln(count_2); return 0; }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void main() { int h, w; readV(h, w); dchar[][] s; readC(h, s); auto isBakudan(int i, int j) { if (i < 0 || i >= h || j < 0 || j >= w) return false; return s[i][j] == '#'; } foreach (i; 0..h) foreach (j; 0..w) { if (s[i][j] == '#') continue; int r = 0; foreach (di; -1..2) foreach (dj; -1..2) if (di != 0 || dj != 0) r += isBakudan(i+di, j+dj); s[i][j] = cast(dchar)('0'+r); } foreach (si; s) writeln(si); }
D
import std.stdio,std.conv,std.string,std.math; void main(){ double money = 100000; while(true){ char[] buf; stdin.readln(buf); double x = buf.chomp().to!double(); if(0 <= x && x <= 100){ for(int i = 0;i < x;i++){ money *= 1.05; if(money - (money/1000).to!int() * 1000 != 0){ money = ((money/1000 + 1).to!int())*1000; } } break; } } writeln(money.to!int()); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; } enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { int N, M; scan(N, M); auto c = new int[](N); foreach (i ; 0 .. M) { int ai, bi; scan(ai, bi); ai--, bi--; c[ai]++; c[bi]++; } auto ok = c.all!(ci => ci % 2 == 0); writeln(ok ? "YES" : "NO"); }
D
void main() { problem(); } void problem() { auto H = scan!int; auto W = scan!int; auto K = scan!int; auto MAP = H.iota.map!(x => scan).array; long solve() { long ans; foreach(wx; 0..2^^(W)) { auto w = (W+1).iota.map!(b => wx >> b & 1).array; foreach(hx; 0..2^^(H)) { auto h = (H+1).iota.map!(b => hx >> b & 1).array; w.deb; h.deb; int count; foreach(x; 0..W) { foreach(y; 0..H) { if (w[x] == 0 && h[y] == 0) { if (MAP[y][x] == '#') count++; } } } if (count == K) ans++; count.deb; } "----".deb; } return ans; } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; int[10^^5] AS; void main() { auto N = readln.chomp.to!int; auto as = readln.split.to!(int[]); auto x = as[0]; foreach (a; as) { x = gcd(x, a); } writeln(x); }
D
import std.stdio, std.string,std.range, std.conv, std.array, std.algorithm, std.math, std.typecons, std.functional, std.bigint, std.random; void main() { auto S = readln.chomp; if (S[0] == S[1]) { writeln("1 2"); return; } foreach (i; 0..S.length-2) { if (S[i] == S[i+1] || S[i+1] == S[i+2] || S[i+2] == S[i]) { writeln(i+1, " ", i+3); return; } } writeln("-1 -1"); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}} void main() { int n; readV(n); int[] a, b; readM(n, a, b); int[] c, d; readM(n, c, d); auto g = GraphW!()(n*2+2); foreach (i; 0..n) foreach (j; 0..n) if (a[i] < c[j] && b[i] < d[j]) g.addEdge(i, n+j, 1); foreach (i; 0..n) { g.addEdge(n*2, i, 1); g.addEdge(n+i, n*2+1, 1); } auto r = FordFulkerson!(typeof(g)).fordFulkerson(g, n*2, n*2+1); writeln(r); } struct GraphW(N = int, W = int, W i = 10^^9) { alias Node = N, Wt = W, inf = i; struct Edge { Node src, dst; Wt wt; alias cap = wt; } Node n; Edge[][] g; alias g this; this(Node n) { this.n = n; g = new Edge[][](n); } void addEdge(Node u, Node v, Wt w) { g[u] ~= Edge(u, v, w); } void addEdgeB(Node u, Node v, Wt w) { g[u] ~= Edge(u, v, w); g[v] ~= Edge(v, u, w); } } template FordFulkerson(Graph) { import std.algorithm, std.container, std.traits; alias Node = TemplateArgsOf!Graph[0], Wt = TemplateArgsOf!Graph[1]; struct EdgeR { Node src, dst; Wt cap, flow; Node rev; } Wt fordFulkerson(ref Graph g, Node s, Node t) { auto n = g.n, adj = withRev(g, n), visited = new bool[](n); Wt augment(Node u, Wt cur) { if (u == t) return cur; visited[u] = true; foreach (ref e; adj[u]) { if (!visited[e.dst] && e.cap > e.flow) { auto f = augment(e.dst, min(e.cap - e.flow, cur)); if (f > 0) { e.flow += f; adj[e.dst][e.rev].flow -= f; return f; } } } return 0; } Wt flow; for (;;) { visited[] = false; auto f = augment(s, g.inf); if (f == 0) break; flow += f; } return flow; } EdgeR[][] withRev(ref Graph g, Node n) { auto r = new EdgeR[][](n); foreach (gi; g) foreach (e; gi) { r[e.src] ~= EdgeR(e.src, e.dst, e.cap, 0, cast(Node)(r[e.dst].length)); r[e.dst] ~= EdgeR(e.dst, e.src, 0, 0, cast(Node)(r[e.src].length) - 1); } return r; } }
D
import std.stdio, std.conv, std.string, std.algorithm, std.range, std.math; void main() { auto po = readln.split.to!(int[]); auto H = po[0], W = po[1]; string[] lines; foreach (i; 0..H) { lines ~= readln.chomp; } foreach (i; 0..W+2) { write("#"); } writeln; foreach (line; lines) { write("#"); write(line); writeln("#"); } foreach (i; 0..W+2) { write("#"); } 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 t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto n = RD!int; auto a = RDA!int; ans[ti] = a.front < a.back; } foreach (e; ans) { writeln(e ? "YES" : "NO"); } stdout.flush; debug readln; }
D
unittest { assert( [ "apple" ].solve == "apples" ); assert( [ "bus" ].solve == "buses" ); assert( [ "box" ].solve == "boxs" ); } import std.conv; import std.range; import std.stdio; void main() { stdin.byLineCopy.solve.writeln; } auto solve( Range )( Range input ) if( isInputRange!Range && is( ElementType!Range == string ) ) { auto s = input.front; return s ~ ( ( s.back == 's' ) ? "es" : "s" ); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int N, tk, ao; scan(N, tk, ao); tk--, ao--; auto adj = new int[][](N, 0); foreach (i ; 0 .. N - 1) { int ai, bi; scan(ai, bi); ai--, bi--; adj[ai] ~= bi; adj[bi] ~= ai; } auto par = new int[](N); par[] = -1; auto height = new int[](N); height[] = -1; auto depth = new int[](N); depth[] = -1; void dfs(int v, int p, int dep) { par[v] = p; depth[v] = dep; foreach (u ; adj[v]) if (u != p) { dfs(u, v, dep + 1); chmax(height[v], height[u]); } height[v] += 1; } dfs(ao, -1, 0); debug { writeln(par); writeln(depth); writeln(height); } int pos = tk; int upc = (depth[tk] + 1) / 2 - 1; int ans = upc; while (upc--) { pos = par[pos]; } ans += height[pos] + (depth[tk] % 2 == 0); writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.string, std.conv; enum N = 110000; enum M = 10000; immutable hurui = { bool[N + 1] table; table[] = true; table[0 .. 2] = false; for(int i = 2; i * i <= N; ++i) { if(table[i]) { for(int j = 2; i * j <= N; j++) { table[i * j] = false; } } } return table; }(); immutable res = { int[M + 1] table; int sum, index; for(int i = 1; i <= N && index < M; ++i) { if(hurui[i]) table[++index] = (sum += i); } return table; }(); void main() { while(true) { int n = readln.chomp.to!int; if(!n) break; res[n].writeln; } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.stdio; import std.string; import std.range; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { const int N = 1_000_000; auto tab = new bool[N]; tab[] = true; for (int i = 2 + 2; i < N; i += 2) tab[i] = false; for (int i = 3; i < N; i += 2) { if (tab[i]) { for (int j = i + i; j < N; j += i) { tab[j] = false; } } } auto sum = new int[N]; for (int i = 2; i < N; i++) { sum[i] = sum[i - 1] + (tab[i] ? 1 : 0); } string s; while ((s = readln.chomp).length > 0) { int n = s.to!int; writeln(sum[n]); } }
D
import std.stdio; import std.string; import std.conv; void main() { auto N = chomp(readln()); writeln((N[0] == '9' || N[1] == '9') ? "Yes" : "No"); }
D
import std.stdio; import std.string; import std.conv; import std.math; int main() { //int n = to!(int)(chomp(readln())); int n = readln.chomp.to!int; foreach(int i;0..n) { string s = chomp(readln()); string[] _s = s.split(" "); double x1 = to!double(_s[0]); double y1 = to!double(_s[1]); double x2 = to!double(_s[2]); double y2 = to!double(_s[3]); double x3 = to!double(_s[4]); double y3 = to!double(_s[5]); double a1 = 2*(x2 - x1); double b1 = 2*(y2 - y1); double c1 = x1*x1 - x2*x2 + y1*y1 - y2*y2; double a2 = 2*(x3 -x1); double b2 = 2*(y3 - y1); double c2 = x1*x1 - x3*x3 + y1*y1 - y3*y3; double x = (b1*c2 - b2*c1) / (a1*b2 - a2*b1); double y = (c1*a2 - c2*a1) / (a1*b2 - a2*b1); double r_2 = (x1 - x)*(x1 - x) + (y1 - y)*(y1 - y); double r = sqrt(r_2); printf("%.3lf %.3lf %.3lf\n",x,y,r); } return 0; }
D
import std.stdio,std.string,std.array,std.algorithm,std.range,std.conv; int main(){ auto s=readln.chomp; auto n=s.length; if(s==s.rev){ auto s1=s.dup[0..(n-1)/2]; if(s1==s1.rev){ auto s2=s.dup[(n+3)/2-1..$]; if(s2==s2.rev){ "Yes".writeln; return 0; } } } "No".writeln; return 0; } auto rev(const char[] s){ auto ss=s.dup; foreach(i;0..ss.length/2){ ss[i]=ss[$-i-1]; } return ss; }
D
import std.stdio, std.algorithm, std.string, std.conv, std.array, std.range, std.math; int read() { return readln.chomp.to!int; } int[] reads() { return readln.split.to!(int[]); } void main() { int n = read(); int[] a = reads(); int ans; foreach (i; 1..n) { if (a[i-1] == a[i]) { a[i] = a[i-1]-10002; ans++; } } writeln(ans); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum mod = 998244353; void main() { long n, a, b, k; scan(n, a, b, k); auto fact = new long[](n + 1); fact[0] = 1; foreach (i ; 1 .. n + 1) { fact[i] = (fact[i-1] * i) % mod; } auto rfact = new long[](n + 1); rfact[n] = powmod(fact[n], mod - 2, mod); foreach_reverse (i ; 0 .. n) { rfact[i] = (rfact[i+1] * (i + 1)) % mod; } long comb(long n, long k) { return fact[n] * rfact[k] % mod * rfact[n-k] % mod; } long ans; foreach (i ; 0 .. n + 1) { if ((k - i*a) % b != 0) continue; long j = (k - i*a) / b; if (j < 0 || j > n) continue; ans += comb(n, i) * comb(n, j) % mod; ans %= mod; } writeln(ans); } long powmod(long x, long y, long m) { return y > 0 ? powmod(x, y>>1, m)^^2 % m * x^^(y&1) % m : 1; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.algorithm; import std.string; import std.conv; import std.math; void main(){ while(true){ auto s = readln(); if(stdin.eof()) break; s = chomp(s); int ans = 0; for(int i=0;i<s.length;i++){ char c = s[i]; int n1,n2; if(c == 'I') n1 = 1; else if (c=='V') n1 = 5; else if (c=='X') n1 = 10; else if (c=='L') n1 = 50; else if (c=='C') n1 = 100; else if (c=='D') n1 = 500; else if (c=='M') n1 = 1000; if(i<to!int(s.length)-1){ c = s[i+1]; if(c == 'I') n2 = 1; else if (c=='V') n2 = 5; else if (c=='X') n2 = 10; else if (c=='L') n2 = 50; else if (c=='C') n2 = 100; else if (c=='D') n2 = 500; else if (c=='M') n2 = 1000; if(n1 < n2){ n1 = n2 - n1; i++; } } ans += n1; } writeln(ans); } }
D
/* imports all std modules {{{*/ import std.algorithm, std.array, std.ascii, std.base64, std.bigint, std.bitmanip, std.compiler, std.complex, std.concurrency, std.container, std.conv, std.csv, std.datetime, std.demangle, std.encoding, std.exception, std.file, std.format, std.functional, std.getopt, std.json, std.math, std.mathspecial, std.meta, std.mmfile, std.net.curl, std.net.isemail, std.numeric, std.parallelism, std.path, std.process, std.random, std.range, std.regex, std.signals, std.socket, std.stdint, std.stdio, std.string, std.system, std.traits, std.typecons, std.uni, std.uri, std.utf, std.uuid, std.variant, std.zip, std.zlib; /*}}}*/ /* libnum {{{*/ void amax(N, Args...)(ref N x, Args y) { x = x.max(y); } void amin(N, Args...)(ref N x, Args y) { x = x.min(y); } auto maxE(R)(R r) if (isInputRange!R) { ElementType!R x = r.front; r.popFront(); foreach (y; r) x.amax(y); return x; } auto minE(R)(R r) if (isInputRange!R) { ElementType!R x = r.front; r.popFront(); foreach (y; r) x.amin(y); return x; } /*}}}*/ /+---test 3 2 4 3 ---+/ /+---test 12 100 104 102 105 103 103 101 105 104 102 104 101 ---+/ void main(string[] args) { const N = readln.chomp.to!long; auto A = readln.split.map!(to!long).array; long L = A.sum; long half = 0, halfi = -1; foreach (i; 0..N) { const x = half + A[i]; if ((half - L/2).abs >= (x - L/2).abs) { half = x; halfi = i; } if (x > L/2) break; } long ans; ans = L - min(L - half, half)*2; ans.writeln; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = readln.chomp.to!int; writeln(0.bitSet(n.bsr)); } pragma(inline) { pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; } pure T bitSet(T)(T n, size_t i) { return n | (T(1) << i); } pure T bitReset(T)(T n, size_t i) { return n & ~(T(1) << i); } pure T bitComp(T)(T n, size_t i) { return n ^ (T(1) << i); } import core.bitop; pure int bsf(T)(T n) { return core.bitop.bsf(ulong(n)); } pure int bsr(T)(T n) { return core.bitop.bsr(ulong(n)); } pure int popcnt(T)(T n) { return core.bitop.popcnt(ulong(n)); } }
D
import std.algorithm; import std.array; import std.ascii; import std.bigint; import std.complex; import std.container; import std.conv; import std.functional; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } void readlnTo(T...)(ref T t) { auto s = readln().split(); assert(s.length == t.length); foreach(ref ti; t) { ti = s[0].to!(typeof(ti)); s = s[1..$]; } } class UnionFindTree { public: this(size_t n) { parent = iota(n).array(); rank = new size_t[](n); } size_t find(size_t n) { if(n == parent[n]) { return n; } else { parent[n] = find(parent[n]); return parent[n]; } } void unite(size_t n, size_t m) { auto p1 = find(n); auto p2 = find(m); if(n == m) { return; } if(rank[p1] < rank[p2]) { swap(p1, p2); } if(rank[p1] == rank[p2]) { ++rank[p1]; } parent[p2] = p1; } private: size_t[] parent; size_t[] rank; } unittest { auto uft = new UnionFindTree(10); foreach(i; iota(10)) { assert(uft.find(i) == i); } foreach(i; iota(10).filter!(a => a%3 == 0).drop(1)) { uft.unite(i, i-3); } foreach(i; iota(10).filter!(a => a%3 == 0)) { assert(uft.find(0) == uft.find(i)); } foreach(i; iota(10).filter!(a => a%3 != 0)) { assert(uft.find(0) != uft.find(i)); } } const real eps = 1e-10; const long p = 1_000_000_000 + 7; void main(){ auto s = readln().chomp(); writeln(s[0..$-8]); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new int[][][](t); foreach (ti; 0..t) { auto n = RD!int; int x = n; foreach_reverse (i; 2..n) { if (i <= (x+i-1) / i) { ans[ti] ~= [n, i]; x = (x+i-1) / i; while (i == 2 && x > 2) { ans[ti] ~= [n, i]; x = (x+i-1) / i; } } ans[ti] ~= [i, n]; } } foreach (e; ans) { writeln(e.length); foreach (ee; e) writeln(ee[0], " ", ee[1]); } stdout.flush; debug readln; }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; void main() { int maxN = 10^^6; int[] f, f_odd; for(int n=1; ;n++) { f ~= func(n); if (f.back > maxN) break; if (f.back%2!=0) f_odd~= f.back; } int INF = 1<<30; int[] ary = new int[maxN+1]; ary[] = INF; ary[0] = 0; foreach(int i; 0..maxN) { if (ary[i]<INF) { for(int n=0; n<f.length; n++) { if (i+f[n]<=maxN) { ary[i+f[n]] = min(ary[i+f[n]], ary[i]+1); } else { break; } } } } int[] _ary = new int[maxN+1]; _ary[] = INF; _ary[0] = 0; foreach(int i; 0..maxN) { if (_ary[i]<INF) { for(int n=0; n<f_odd.length; n++) { if (i+f_odd[n]<=maxN) { _ary[i+f_odd[n]] = min(_ary[i+f_odd[n]], _ary[i]+1); } else { break; } } } } while(true) { int N = readln.chomp.to!int; if (N==0) break; writeln(ary[N], " ", _ary[N]); } } int func(int n) { return n*(n+1)*(n+2)/6; }
D
import std.stdio, std.string, std.algorithm, std.conv; void main(){ auto a = readln.split.map!(to!int); writeln(a[0]<a[1]&&a[1]<a[2] ? "Yes" : "No"); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.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 t = RD!int; auto ans = new long[](t); foreach (i; 0..t) { auto a = RD; auto b = RD; auto n = RD; long x, y; if (n == 0) { x = a; } else if ((n-1) % 3 != 0) { x = a; } if (n % 3 != 0) { y = b; } ans[i] = x^y; } foreach (e; ans) writeln(e); stdout.flush(); debug readln(); }
D
void main() { problem(); } void problem() { const K = scan!int; const N = scan!int; const A = scan!(int)(N); int solve() { auto delta = new int[N]; foreach(i; 0..N-1) { delta[i] = A[i+1] - A[i]; } delta[N-1] = K - A[N-1] + A[0]; int max; foreach(d; delta){ if (max < d) max = d; } return delta.sum - max; } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
import std.stdio, std.string, std.conv, std.algorithm; void main(){ int count = readln().chomp.to!int; string[] lines; foreach(i; 0..count){ lines.length++; lines[i] = readln().chomp; } foreach(line; lines){ auto num = line.split.map!(to!int); int d; foreach(n1; num) foreach(n2; num) foreach(n3; num) if((n1*n1) == (n2*n2) + (n3*n3)) d++; if(d != 0) writeln("YES"); else writeln("NO"); } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { auto s = readln.chomp; int n = s.length.to!int; auto dp = new long[][](n + 1, 13); dp[0][0] = 1; foreach (i ; 0 .. n) { foreach (j ; 0 .. 13) { if (s[i] == '?') { foreach (k ; 0 .. 10) { (dp[i + 1][(10*j + k) % 13] += dp[i][j]) %= mod; } } else { int d = s[i] - '0'; (dp[i + 1][(10*j + d) % 13] += dp[i][j]) %= mod; } } } long ans = dp[n][5]; writeln(ans); } int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new int[][](n, 0); foreach (i; 0 .. m) { int u, v; scan(u, v); if (is1indexed) { u--, v--; } adj[u] ~= v; if (isUndirected) { adj[v] ~= u; } } return adj; } alias Edge = Tuple!(int, "to", int, "cost"); Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new Edge[][](n, 0); foreach (i; 0 .. m) { int u, v, c; scan(u, v, c); if (is1indexed) { u--, v--; } adj[u] ~= Edge(v, c); if (isUndirected) { adj[v] ~= Edge(u, c); } } return adj; } void yes(bool b) { writeln(b ? "Yes" : "No"); } void YES(bool b) { writeln(b ? "YES" : "NO"); } T[] readArr(T)() { return readln.split.to!(T[]); } T[] readArrByLines(T)(int n) { return iota(n).map!(i => readln.chomp.to!T).array; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math; void main() { auto N = readln.chomp.to!int; auto A = readln.chomp.to!int; writeln(N % 500 <= A ? "Yes" : "No"); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto s = readln.chomp; writeln(s.length/2 - s.count('p')); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto S = readln.chomp; auto T = readln.chomp; writeln(T[0..S.length] == S ? "Yes" : "No"); }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; auto s = readln.chomp.to!(char[]); auto t = readln.chomp.to!(char[]); int[char] f1, f2; foreach (c; s) { if (c in f1) f1[c]++; else f1[c] = 1; } foreach (c; t) { if (c in f2) f2[c]++; else f2[c] = 1; } int[] a, b; foreach (cnt; f1) a ~= cnt; foreach (cnt; f2) b ~= cnt; if (a.length != b.length) { writeln("No"); return; } sort(a); sort(b); bool ok = true; foreach (i; 0 .. a.length) { ok &= a[i] == b[i]; } writeln(ok ? "Yes" : "No"); } void rd(T...)(ref T x) { import std.stdio, std.string, std.conv; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); long prev; long ans; foreach (_; 0 .. N) { long A = lread(); if (0 < prev && A != 0) { ans++; A--; } ans += A / 2; prev = A & 1; } writeln(ans); }
D
/+ dub.sdl: name "sol_ysp" dependency "dunkelheit" version="1.0.1" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; int main() { Scanner sc = new Scanner(stdin); scope(exit) assert(!sc.hasNext); string s; sc.read(s); s ~= "A"; long ans, a_cnt; size_t pos = 0; while (pos < s.length) { if (s[pos] == 'A') { a_cnt++; pos++; } else if (s[pos..pos+2] == "BC") { ans += a_cnt; pos += 2; } else { a_cnt = 0; pos++; } } writeln(ans); return 0; } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/scanner.d */ // module dkh.scanner; // import dkh.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; private File f; this(File f) { this.f = f; } private char[512] lineBuf; private char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { assert(succW()); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(Args...)(auto ref Args args) { import std.exception : enforce; static if (args.length != 0) { enforce(readSingle(args[0])); read(args[1..$]); } } bool hasNext() { return succ(); } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std.stdio, 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; string S; string T; int compare(int i) { // -1 : docchimo // 0 : T < S // 1 : S < T if (i >= S.length && i >= T.length) return -1; if (i >= T.length) return 0; if (i >= S.length) return 1; if (S[i] == '?' && T[i] == 'a') return compare(i+1); if (S[i] == '?' && T[i] == 'z') return compare(i+1); if (S[i] == '?' && T[i] > 'a' && T[i] < 'z') return -1; if (S[i] == T[i]) return compare(i+1); if (S[i] > T[i]) return 0; if (S[i] < T[i]) return 1; assert(0); } void main() { auto N = readln.chomp.to!int; string[] SS; foreach (i; 0..N) SS ~= readln.chomp; T = readln.chomp; int saisho = 1; int haba = 0; foreach (i; 0..N) { S = SS[i]; int c = compare(0); if (c == -1) haba += 1; else if (c == 1) saisho += 1; } string[] ans; foreach (i; saisho..saisho+haba+1) ans ~= i.to!string; ans.join(" ").writeln; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long A, B, C, K; scan(A, B, C, K); writeln((K & 1) ? (B - A) : (A - B)); }
D
import std.stdio; import std.conv; import std.algorithm; import std.range; import std.string; import std.typecons; import std.math; import std.random; import std.range; import std.functional; import std.container; class bt(T) { t!T root; void insert(T key) { t!T y = null; auto x = root; auto z = new t!T(key); while (x) { y = x; if (z.key < y.key) { x = x.left; } else { x = x.right; } } z.parent = y; if (!y) { root = z; } else { if (z.key < y.key) { y.left = z; } else { y.right = z; } } } } class t(T) { T key; this(T k) { key = k; } t left, right, parent; } void in_(T)(t!T t) { if (!t) return; in_(t.left); write(" ", t.key); in_(t.right); } void pre(T)(t!T t) { if (!t) return; write(" ", t.key); pre(t.left); pre(t.right); } void main() { auto c = readln.strip.to!int; bt!int b = new bt!int; foreach (_; 0..c) { auto instr = readln.strip.split(" "); if (instr.length == 1) { in_(b.root); writeln(); pre(b.root); writeln(); } else { auto key = instr[1].to!int; b.insert(key); } } }
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; void main() { string s = read!string; if (s == "Sunny") writeln("Cloudy"); else if (s == "Cloudy") writeln("Rainy"); else if (s == "Rainy") writeln("Sunny"); }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std; // dfmt on void main() { long N = lread(); long n = N % 10; auto ans = [ "pon", "pon", "hon", "bon", "hon", "hon", "pon", "hon", "pon", "hon" ]; writeln(ans[n]); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; long x; long pos; foreach (i; 0..N) { x += i+1; pos = i+1; if (x >= N) break; } long y = x - N; foreach (i; 0..pos) { if (i+1 == y) continue; writeln(i+1); } stdout.flush; debug readln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string; void main() { auto line = readln.chomp; auto enil = line.dup; reverse(enil); writeln(line == enil ? "Yes" : "No"); }
D
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.range; void main() { auto input = readln.split.to!(int[]); auto K = input[0]; auto X = input[1]; auto m = max(-100_0000, X - (K - 1)); auto M = min(X + (K - 1), 100_0000); for(int i = m; i <= M; i++) { write(i, " "); } }
D
import std.stdio; import std.conv; import std.algorithm; import std.range; import std.string; import std.typecons; import std.math; import std.random; import std.range; import std.functional; import std.container; class bt(T) { t!T root; bool containsKey(T key) { return root && root.containsKey(key); } void insert(T key) { t!T y = null; auto x = root; auto z = new t!T(key); while (x) { y = x; if (z.key < y.key) { x = x.left; } else { x = x.right; } } z.parent = y; if (!y) { root = z; } else { if (z.key < y.key) { y.left = z; } else { y.right = z; } } } } class t(T) { T key; this(T k) { key = k; } t left, right, parent; bool containsKey(T k) { if (key == k) return true; if (key > k) return left && left.containsKey(k); else return right && right.containsKey(k); } } void in_(T)(t!T t) { if (!t) return; in_(t.left); write(" ", t.key); in_(t.right); } void pre(T)(t!T t) { if (!t) return; write(" ", t.key); pre(t.left); pre(t.right); } void main() { auto c = readln.strip.to!int; bt!int b = new bt!int; foreach (_; 0..c) { auto instr = readln.strip.split(" "); if (instr.length == 1) { in_(b.root); writeln(); pre(b.root); writeln(); } else { auto key = instr[1].to!int; if (instr[0] == "find") { writeln(b.containsKey(key) ? "yes" : "no"); } else { b.insert(key); } } } }
D
import std.stdio, std.string, std.conv, std.algorithm; void main() { while(1){ auto n = readln.chomp.to!int; if(n==0) break; int s=1; int max=50000001; for(int i=2;i<n/2;++i){ if(n%i==0){ if(i<max){ s+=(i+n/i); max = n/i; if(i==max) s -= i; }else break; }else if(i>max) break; } if(s<n || n==1) writeln("deficient number"); else if(s==n) writeln("perfect number"); else writeln("abundant number"); } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; import std.math; void main() { auto cnt = new int[](5); int n; scan(n); foreach (i ; 0 .. n) { string s; scan(s); switch (s[0]) { case 'M': cnt[0]++; break; case 'A': cnt[1]++; break; case 'R': cnt[2]++; break; case 'C': cnt[3]++; break; case 'H': cnt[4]++; break; default: break; } } long ans; foreach (i ; 0 .. 5) { foreach (j ; i + 1 .. 5) { foreach (k ; j + 1 .. 5) { ans += 1L * cnt[i] * cnt[j] * cnt[k]; } } } 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.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; long calc(int a, int b, int c) { long ans = 0; // 毒入り(美味しい) -> 解毒剤(美味しい)の順に食べる int x = min(b, c); ans += x * 2; b -= x; c -= x; // 毒入り(美味しい) -> 解毒剤(美味しくない)の順に食べる x = min(a, c); ans += x; a -= x; c -= x; // 毒入り(美味しい)が余っているなら 1 枚だけ食べる if (c > 0) ans++; // 余りの解毒剤(美味しい)を食べる ans += b; return ans; } long calc2(int a, int b, int c) { int d = min(c, a + b + 1); return b + d; } void main() { auto abc = readints; int a = abc[0], b = abc[1], c = abc[2]; writeln(calc2(a, b, c)); }
D
import std.stdio, std.algorithm, std.array, std.string, std.conv; void main() { int t; scanf("%d", &t); foreach(_; 0..t) { int str_len; scanf("%d", &str_len); getchar(); auto str = readln.strip(); // writeln(str); uint[] arr = new uint[str_len]; for(int i = 0; i < str_len; i++) { arr[i] = to!uint(str[i..i + 1]); } auto zeros = new uint[str_len]; long res = 0; int last_non_null = str_len - 1; while (arr != zeros) { if (arr[last_non_null] == 0){ last_non_null--; } else { if (last_non_null != str_len - 1) res += 1; res += arr[last_non_null]; arr[last_non_null] = 0; last_non_null--; } } writeln(res); } }
D
import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; import std.container; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } void main(){ long n = read.to!long; long m = read.to!long; bool[long] f1; bool[long] f2; foreach(i; 0 .. m){ long a = read.to!long; long b = read.to!long; if(a == 1) f1[b] = 1; if(b == 1) f1[a] = 1; if(a == n) f2[b] = 1; if(b == n) f2[a] = 1; } string answer = "IMPOSSIBLE"; foreach(i; 1 .. m + 1){ if((i in f1) && (i in f2)) answer = "POSSIBLE"; } answer.writeln; }
D
void main() { long n, a, b; rdVals(n, a, b); long result = modPow(2, n) - 1; long fa = 1, fb = 1; foreach (i; 0 .. b) { if(i < a) fa = fa * (n - i) % mod; fb = fb * (n - i) % mod; } long[] finv = new long[b+1]; long[] inv = new long[b+1]; finv[0] = 1, finv[1] = 1; inv[1] = 1; foreach (i; 2 .. b+1) { inv[i] = mod - inv[mod%i] * (mod / i) % mod; finv[i] = finv[i-1] * inv[i] % mod; } long ca = fa * finv[a] % mod; long cb = fb * finv[b] % mod; result = (result + mod - ca) % mod; result = (result + mod - cb) % mod; result.writeln; } long modPow(long x, long y) { long result = 1; while (y > 0) { if (y & 1) result = result * x % mod; x = x * x % mod; y >>= 1; } return result; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; void times(alias fun)(int n) { foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } void main() { readln.chomp.to!long.pipe!( N => iota(0L, N.to!double.sqrt.to!long).map!"a+1".filter!( a => N%a==0 ).map!( a => max(a, N/a).to!string.length ).fold!min ).writeln; } // ---------------------------------------------- // 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.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { auto s = readln.chomp.to!(char[]); long ans; while (!s.empty) { if (s.length == 1) break; if (s.front == s.back) { s.popFront(); s.popBack(); } else if (s.front == 'x') { ans++; s.popFront(); } else if (s.back == 'x') { ans++; s.popBack(); } else { writeln(-1); return; } } 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
module bbb;import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto N = readln.chomp.to!int; auto s = readln.split.map!(to!int); auto Ga = s[0]; auto Sa = s[1]; auto Ba = s[2]; s = readln.split.map!(to!int); auto Gb = s[0]; auto Sb = s[1]; auto Bb = s[2]; long solve(long n, long g1, long g2, long s1, long s2, long b1, long b2) { long[][] X; if (g1 < g2) X ~= [g1, g2]; if (s1 < s2) X ~= [s1, s2]; if (b1 < b2) X ~= [b1, b2]; long ans = n; if (X.length == 0) return n; else if (X.length == 1) return n / X.front[0] * X.front[1] + n % X.front[0]; else if (X.length == 2) { foreach (i; 0..n+1) { long j = n - i; long tmp = i / X[0][0] * X[0][1] + j / X[1][0] * X[1][1] + i % X[0][0] + j % X[1][0]; ans = max(ans, tmp); } } else { foreach (i; 0..n+1) { foreach (j; 0..n-i+1) { long k = n - i - j; if (b1 >= b2) k = 0; //long tmp = i / g1 * g2 + j / s1 * s2 + k / b1 * b2 + i % g1 + j % s1 + k % b1; long tmp = i / X[0][0] * X[0][1] + j / X[1][0] * X[1][1] + k / X[2][0] * X[2][1] + i % X[0][0] + j % X[1][0] + k % X[2][0]; ans = max(ans, tmp); } } } return ans; } auto M = solve(N, Ga, Gb, Sa, Sb, Ba, Bb); auto L = solve(M, Gb, Ga, Sb, Sa, Bb, Ba); L.writeln; }
D
void main() { long[] tmp = readln.split.to!(long[]); long n = tmp[0], p = tmp[1]; long[long] lists; lists[2] = 0; while (p % 2 == 0) { ++lists[2]; p /= 2; } for (long i = 3; i * i <= p; i += 2) { if (p % i == 0) { lists[i] = 0; while(p % i == 0) { ++lists[i]; p /= i; } } } if (p > 1) lists[p] = 1; long g = 1; foreach (key, val; lists) { if (val >= n) g *= key ^^ (val / n); } g.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
void main() { auto N = ri; auto S = rs; ulong res; foreach(i; 0..S.length - 2) { if(S[i..i+3] == "ABC") res++; } res.writeln; } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new char[][][](t); foreach (ti; 0..t) { auto n = RD!int; auto s = new char[][](n); foreach (i; 0..n) s[i] = RD!(char[]); bool f(int num) { int cnt0, cnt1; foreach (int i; 0..n) { foreach (int j; 0..n) { if (s[i][j] == '.') continue; ++cnt1; int p = (n*3 + j - i) % 3; if (p != num) continue; int x; { char c; int cnt; foreach (k; max(0, j - 2)..min(n, j + 3)) { if (s[i][k] != c) { c = s[i][k]; cnt = 1; } else { ++cnt; if (cnt == 3) { ++x; break; } } } } { char c; int cnt; foreach (k; max(0, i - 2)..min(n, i + 3)) { if (s[k][j] != c) { c = s[k][j]; cnt = 1; } else { ++cnt; if (cnt == 3) { ++x; break; } } } } if (x != 0) { s[i][j] = s[i][j] == 'O' ? 'X' : 'O'; ++cnt0; } } } return cnt0 <= (cnt1 / 3); } auto ss = new char[][](n, n); foreach (i; 0..n) ss[i][] = s[i][]; foreach (i; 0..3) { foreach (j; 0..n) s[j][] = ss[j][]; auto r = f(i); debug writeln("i:", i); if (r) break; } ans[ti] = s; } foreach (e; ans) { foreach (ee; e) writeln(ee); } stdout.flush; debug readln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; immutable long INF = 1L << 59; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!long).array; auto source = N; auto sink = N+1; auto ff = new FordFulkerson(N+2, source, sink); foreach (i; 0..N) { if (A[i] >= 0) { ff.add_edge(source, i, 0); ff.add_edge(i, sink, A[i]); } else { ff.add_edge(source, i, -A[i]); ff.add_edge(i, sink, 0); } for (int j = (i+1)*2; j <= N; j += i+1) { ff.add_edge(i, j-1, INF); } } writeln(A.map!(a => max(a, 0)).sum - ff.run); } class FordFulkerson { int N, source, sink; int[][] adj; long[][] flow; bool[] used; this(int n, int s, int t) { N = n; source = s; sink = t; assert (s >= 0 && s < N && t >= 0 && t < N); adj = new int[][](N); flow = new long[][](N, N); used = new bool[](N); } void add_edge(int from, int to, long cap) { adj[from] ~= to; adj[to] ~= from; flow[from][to] = cap; } long dfs(int v, long min_cap) { if (v == sink) return min_cap; if (used[v]) return 0; used[v] = true; foreach (to; adj[v]) { if (!used[to] && flow[v][to] > 0) { auto bottleneck = dfs(to, min(min_cap, flow[v][to])); if (bottleneck == 0) continue; flow[v][to] -= bottleneck; flow[to][v] += bottleneck; return bottleneck; } } return 0; } long run() { long ret = 0; while (true) { foreach (i; 0..N) used[i] = false; long f = dfs(source, long.max); if (f > 0) ret += f; else return ret; } } }
D
import std.stdio; // ??\???????????????????????? import std.string; // chomp????????????????????????(?????????????????????) import std.conv; // to???????????????????????? import std.array; // split????????????????????? import std.algorithm; // map????????????????????? void main() { auto input = readln.split.map!(to!int); auto a = input[0]; auto b = input[1]; if (a > b) { writeln("a > b"); } else if (a < b){ writeln("a < b"); } else { writeln("a == b"); } }
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.container; void main() { foreach (line; stdin.byLine) { auto x = line.chomp.to!int; if (!x) break; int sum; foreach (i; 0..x/4) { sum += readln.chomp.to!int; } sum.writeln; } }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std; // dfmt on void main() { long N = lread(); auto ans = new long[](10_000_000); foreach (x; 1 .. 500) foreach (y; 1 .. 500) foreach (z; 1 .. 500) { long n = x * x + y * y + z * z + x * y + y * z + z * x; // dprint(x, y, z, n); ans[n]++; } foreach (i; 1 .. N + 1) { writeln(ans[i]); } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp.to!int; auto s = readln.chomp.array; foreach (ref e; s) { auto d = (e - 'A' + n) % 26; e = 'A' + d; } s.writeln; }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { auto N = readln.chomp.to!int; auto a = readln.split.to!(int[]); auto y = a.filter!"a%2==0&&a%4".walkLength; auto z = a.filter!"a%4==0".walkLength; auto x = N-y-z; writeln(() { if (x == z + 1) return y == 0; else if (x > z + 1) return false; else return true; }() ? "Yes" : "No"); }
D
import std.stdio; import std.algorithm; import std.math; import std.conv; import std.string; T readNum(T)(){ return readStr.to!T; } T[] readNums(T)(){ return readStr.split.to!(T[]); } string readStr(){ return readln.chomp; } void main(){ auto t = readStr; auto s = t.replace("?", "D"); writeln(s); }
D
void main() { int x = readln.chomp.to!int; writeln(x == 7 || x == 5 || x == 3 ? "YES" : "NO"); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.container; import std.typecons;
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() { while (true) { int n, a, b; scan(n,a,b); if (!n) return; auto p = iota(n).map!(i => readln.chomp.to!int).array; solve(n,a,b,p); } } void solve(int n, int a, int b, int[] p) { int difmax, ans; foreach (i ; a .. b + 1) { if (difmax <= p[i-1] - p[i]) { difmax = p[i-1] - p[i]; ans = i; } } 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
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto s = readln.split .to!(int[]) .reduce!((res, a) => (res + a - 1)); writeln(s - 1); } void rd(T...)(ref T x) { import std.stdio : readln; import std.string : split; import std.conv : to; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional; import std.algorithm, std.container; void main() { int N = scanElem!int; int M = scanElem!int; int[] nList; nList.length = N + 1; foreach(i;0..M) { int s = scanElem!int; int g = scanElem!int; if(nList[s]==0) { nList[s] = g; }else{ nList[s] = min(nList[s], g); } } int count; int end; foreach(s, g; nList) { if(g==0) continue; if(end <= s) { count++; end = g; } end = min(end, g); } writeln(count); } 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; }
D
import std.stdio, std.string, std.conv, std.algorithm, std.range; void main() { auto tmp = readln.split.to!(int[]); auto K = tmp[0], S = tmp[1]; iota(K+1).map!(x => max(0, min(S-x, K) - max(0, S - K - x) + 1)).sum.writeln; }
D
void main() { long[] tmp = readln.split.to!(long[]); long x = tmp[0], y = tmp[1]; long mod = 10 ^^ 9 + 7; long ans; if ((x + y) % 3 == 0) { long n = (2 * y - x) / 3, m = (2 * x - y) / 3; if (n >= 0 && m >= 0) { long l = n+m; long[] fac = new long[l+1]; long[] finv = new long[l+1]; long[] inv = new long[l+1]; fac[0] = 1, fac[1] = 1; finv[0] = 1, finv[1] = 1; inv[1] = 1; foreach (i; 2 .. l+1) { fac[i] = fac[i-1] * i % mod; inv[i] = mod - inv[mod%i] * (mod / i) % mod; finv[i] = finv[i-1] * inv[i] % mod; } long comb(long x, long y) { if (x < y) return 0; if (x < 0 || y < 0) return 0; return fac[x] * (finv[y] * finv[x-y] % mod) % mod; } ans = comb(n+m, n); } } ans.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
void main(){ char[] val = inln!char(); writeln( (val[0]=='H')^(val[1]=='H')?'D':'H'); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range; const long mod = 10^^9+7; // 1要素のみの入力 T inelm(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] inln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split())ln ~= elm.to!T(); return ln; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; // int n=6, m=10; // auto c=new int[][](n, m); // auto dir=[[-1, -1], [-1, 0], [-1, 1], [0, 1], [1, 1], [1, 0], [1, -1], [0, -1]]; // foreach(i; 0..n)foreach(j; 0..m){ // c[i][j]^=1; // foreach(d; dir){ // auto ni=i+d[0], nj=j+d[1]; // if(ni<0 || ni>=n || nj<0 || nj>=m) continue; // c[ni][nj]^=1; // } // } // foreach(i; 0..n) writeln(c[i]); // writeln(313*2+1591*2); long n, m; rd(n, m); if(n==1 && m==1) writeln(1); else if(n==1) writeln(m-2); else if(m==1) writeln(n-2); else writeln(n*m-((n-1)*2+(m-1)*2)); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.stdio; import std.algorithm; import std.string; import std.range; import std.array; import std.conv; import std.complex; import std.math; import std.ascii; import std.bigint; import std.container; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } void readlnTo(T...)(ref T t) { auto s = readln().split(); assert(s.length == t.length); foreach(ref ti; t) { ti = s[0].to!(typeof(ti)); s = s[1..$]; } } const real eps = 1e-10; void main(){ long n, k; readlnTo(n ,k); auto a = readLongs(); auto b = readLongs(); long l = 0, u = 10000000000; while(u-l > 1) { auto m = (u+l)/2; auto t = k; foreach(i; iota(n.to!uint)) { if(m * a[i] > b[i]) { t -= m*a[i] - b[i]; } if(t < 0) { break; } } if(t < 0) { u = m; } else { l = m; } } writeln(l); }
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[string] s, t; int n = readint; for (int i = 0; i < n; i++) { string a = readln.chomp; s[a]++; } int m = readint; for (int i = 0; i < m; i++) { string a = readln.chomp; t[a]++; } int ans = 0; foreach (k, v; s) { ans = max(ans, max(0, v - t.get(k, 0))); } writeln(ans); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto A = RDA; auto cnt = new long[](61); long ans; foreach (i; 0..N) { foreach (j; 0..61) { auto bit = 1L << j; if (A[i] & bit) { long x; x.moda(bit); x.modm(i-cnt[j]); ans.moda(x); ++cnt[j]; } else { long x; x.moda(bit); x.modm(cnt[j]); ans.moda(x); } } debug writeln(ans); } writeln(ans); stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.algorithm, std.range, std.array, std.string, std.uni; void main() { readln.split.map!(a => a.capitalize[0]).array.writeln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; int H, W; auto dy = [0, 0, 1, -1], dx = [1, -1, 0, 0]; char[][] board; void main(){ for(;init;){ writeln(solve); } } bool init(){ auto input = map!(to!int)(readln.chomp.split); H = input[0]; W = input[1]; board = new char[][](H); foreach(ref line; board){ line = to!(char[])(readln.chomp); } return H > 0; } int solve(){ int ans = 0; foreach(y; 0..H){ foreach(x; 0..W){ if(board[y][x] != '.'){ ++ans; dfs(y, x, board[y][x]); } } } return ans; } void dfs(int y, int x, char col){ board[y][x] = '.'; foreach(k; 0..4){ int ny = y + dy[k], nx = x + dx[k]; if(0<=ny && ny<H && 0<=nx && nx<W && board[ny][nx] == col){ dfs(ny, nx, col); } } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.math; void main() { readln; (readln.split.sort().uniq.array.length == 3 ? "Three" : "Four").writeln; }
D
// Vicfred // https://atcoder.jp/contests/abc165/tasks/abc165_b // simulation import std.conv; import std.stdio; import std.string; void main() { long x = readln.chomp.to!long; long current = 100; long answer = 0; while(current < x) { current += current/100; answer += 1; } answer.writeln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { readln.chomp.count!"a == '1'".writeln; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { writeln("ABC" ~ sread()); }
D
import std.stdio, std.conv, std.string, std.math, std.regex, std.range, std.ascii, std.algorithm; void main(){ auto ip = readln.split.to!(int[]), N=ip[0], M=ip[1]; int L = 0; int R = N; foreach(i; 0..M){ auto LR = readln.split.to!(int[]); L = max(L, LR[0]); R = min(R, LR[1]); } if(L<=R) writeln(R-L+1); else writeln(0); }
D
import std.stdio, std.conv, std.string; import std.algorithm, std.array, std.container; import std.numeric, std.math; import core.bitop; T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; } string RDR()() { return readln.chomp; } long mod = pow(10, 9) + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD!long; auto C = RD!long; long[][] D; foreach (i; 0..C) { D ~= RDR.split.to!(long[]); } long[][] c; foreach (i; 0..N) { c ~= RDR.split.to!(long[]); } auto cnt = new long[][](3, C); foreach (i; 0..N) { foreach (j; 0..N) { foreach (k; 0..C) { cnt[(i+j+2)%3][k] += D[c[i][j]-1][k]; } } } long[][] pat = [ [0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]]; long ans = long.max; foreach (arr; pat) { long[] used; long tmpAns; foreach (i; arr) { long best = long.max; long bestPos; foreach (j; 0..C) { if (used.countUntil(j) != -1) continue; if (cnt[i][j] < best) { best = cnt[i][j]; bestPos = j; } } tmpAns += best; used ~= bestPos; } ans = min(ans, tmpAns); } writeln(ans); stdout.flush(); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; 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, M; scan(N, M); long[] cnt = new long[](N); foreach (_; 0 .. M) { long a, b; scan(a, b); cnt[a - 1]++; cnt[b - 1]++; } foreach (c; cnt) writeln(c); }
D
// Vicfred // https://atcoder.jp/contests/abc155/tasks/abc155_b // simulation import std.algorithm; import std.array; import std.conv; import std.stdio; import std.string; void main() { int n = readln.chomp.to!int; int[] a = readln.split.map!(to!int).array; foreach(item; a) { if(item%2 == 0 && item%3 != 0 && item%5 != 0) { writeln("DENIED"); return; } } writeln("APPROVED"); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; } enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { long a, b, c, d; scan(a, b, c, d); auto ans = f(b, c, d) - f(a - 1, c, d); writeln(ans); } long f(long x, long c, long d) { return x - (x / c) - (x / d) + (x / lcm(c, d)); } long lcm(long a, long b) { return a / gcd(a, b) * b; }
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 s = readln.chomp; int[char] cnt; foreach (e; s) { cnt[e]++; } if (s.length == cnt.keys.length) { writeln("yes"); } else { writeln("no"); } }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto S = "0" ~ readln.chomp; auto N = S.length.to!int; auto dp = new int[][](N+1, 2); foreach (i; 0..N+1) dp[i][] = 1 << 29; dp[0][0] = 0; foreach (i; 0..N) { int d = S[N - i - 1] - '0'; dp[i+1][0] = min(dp[i][0] + d, d == 9 ? 1 << 29 : dp[i][1] + d + 1); dp[i+1][1] = min(d == 0 ? 1 << 29 : dp[i][0] + 10 - d, dp[i][1] + 9 - d); } dp[N][0].writeln; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; void main() { auto s = readln.strip.splitter; immutable int n = s.front.to!int;s.popFront; immutable int m = s.front.to!int; int problems, penalty; int[int] c; bool[int] d; foreach (qid; 0 .. m) { s = readln.strip.splitter; immutable int k = s.front.to!int;s.popFront; bool ac = s.front.front == 'A'; if (ac) { auto ptr = k in d; if (ptr is null) { ++problems; int t = c.get (k, 0); penalty += t; d[k] = true; } } else { c[k] = c.get (k, 0) + 1; } } writeln (problems, ' ', penalty); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "begin", long, "end"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long n, k; scan(n, k); auto a = aryread(); auto diff = new long[](n - 1); long x = a[0]; foreach (i; iota(1, n)) x = gcd(a[i], x); long m = a.reduce!(max); // x.writeln(); if (k <= m && k % x == 0) writeln("POSSIBLE"); else writeln("IMPOSSIBLE"); } 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.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 nk = readln.chomp.split.map!(to!int); auto num = 1; foreach (i; 0..nk[0]) { if (i==0) num *= nk[1]; else num *= nk[1]-1; } num.writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto n = RD!int; auto ans = new int[](n); foreach (i; 0..n) { auto c = RD!int; auto sum = RD!int; auto x = sum / c; auto y = sum % c; ans[i] += ((x+1)^^2) * y; ans[i] += (x^^2) * (c - y); } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.algorithm, std.string, std.range, std.stdio, std.conv; void main() { int[char] dic; foreach (c; readln.chomp) { if (c !in dic) { dic[c] = 1; } else { dic[c]++; } } writeln(dic.values.all!(n => n % 2 == 0) ? "Yes" : "No"); }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.range; import std.stdio; import std.string; bool solve(int[] p, int t) { if (p.empty) return t == 0; if (t == 0) return true; if (t < 0) return false; int n = p.front; p.popFront; if (solve(p, t-n)) return true; if (solve(p, t)) return true; return false; } void main() { auto n = readln.chomp.to!int; auto a = readln.chomp.split.map!(to!int).array; auto q = readln.chomp.to!int; auto m = readln.chomp.split.map!(to!int).array; foreach (m_i; m) { if (solve(a, m_i)) { "yes".writeln; } else { "no".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; import std.container; alias sread = () => readln.chomp(); ulong bignum = 1_000_000_007; alias SugarWater = Tuple!(long, "swater", long, "sugar"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { long n, m; scan(n, m); auto lights = new long[][](m, n); foreach (i; iota(m)) { auto k = aryread(); foreach (s; k[1 .. $]) { lights[i][s - 1]++; } } auto p = aryread(); long count; // lights.writeln(); foreach (i; 0 .. (1 << n)) { long[] bits = i.bitary(n); bool check = true; foreach (j; iota(m)) { long on; foreach (k; iota(n)) on += bits[k] & lights[j][k]; if (on % 2 != p[j]) check = false; } if(check) count++; } count.writeln(); } auto bitary(long n, long disit) { auto ary = new long[](disit); long tmp; while (n > 0) { ary[tmp++] = n % 2; n = n >> 1; } ary.reverse(); return ary; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; long calc(int[] xs) { long sum = 0; long[long] d = [0: 1]; foreach (x; xs) { sum += x; d[sum]++; } return d.values.map!(e => e * (e - 1) / 2).sum; } void main() { readint; auto xs = readints; writeln(calc(xs)); }
D