code
stringlengths
4
1.01M
language
stringclasses
2 values
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { long A, B; scan(A, B); long c = A - 1; c += (A <= B) ? 1 : 0; writeln(c); }
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; writeln(N / 2 - (N % 2 == 0 ? 1 : 0)); stdout.flush(); debug readln(); }
D
import std; long calc(long a, long b, long c, long k) { long x = min(a, k); long y = min(b, k - x); long z = min(c, k - x - y); return x - z; } void main() { long a, b, c, k; scan(a, b, c, k); writeln(calc(a, b, c, k)); } 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.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n; scan(n); if (n == 0 || n == 1) { writeln(1); return; } auto f = new long[](n + 1); f[0] = f[1] = 1; foreach (i ; 2 .. n + 1) { f[i] = f[i - 1] + f[i - 2]; } writeln(f[n]); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { string s; scan(s); auto p = "keyence"; int m = p.length.to!int; foreach (i ; 0 .. m + 1) { debug { writeln(s[0..i]); writeln(s[$-(m-i)..$]); } if (s[0 .. i] == p[0 .. i] && s[$-(m-i) .. $] == p[i .. $]) { writeln("YES"); return; } } writeln("NO"); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg ; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg ; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; } long powmod(long x, long y, long mod = 1_000_000_007L) { long res = 1L; while (y > 0) { if (y & 1) { (res *= x) %= mod; } (x *= x) %= mod; y >>= 1; } return res; }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}} void main() { long n; readV(n); long[] a; readC(5, a); auto b = a.reduce!min; writeln((n+b-1)/b+4); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nk = readln.split.to!(int[]); auto N = nk[0]; auto K = nk[1]; auto bs = new int[][](K*2, K*2); auto ws = new int[][](K*2, K*2); foreach (_; 0..N) { auto xyc = readln.split; auto x = xyc[0].to!int % (K*2); auto y = xyc[1].to!int % (K*2); if (xyc[2] == "B") { bs[y][x] += 1; } else { ws[y][x] += 1; } } foreach (i; 0..K*2) { foreach (j; 0..K*2) { if (i > 0) { bs[i][j] += bs[i-1][j]; ws[i][j] += ws[i-1][j]; } if (j > 0) { bs[i][j] += bs[i][j-1]; ws[i][j] += ws[i][j-1]; } if (i > 0 && j > 0) { bs[i][j] -= bs[i-1][j-1]; ws[i][j] -= ws[i-1][j-1]; } } } int r; auto m = K*2-1; foreach (i; 0..K) { foreach (j; 0..K) { r = max(r, bs[m][m] - bs[K+i][m] - bs[m][K+j] + bs[K+i][K+j] + bs[K+i][K+j] - bs[i][K+j] - bs[K+i][j] + bs[i][j] + bs[i][m] - bs[i][K+j] + bs[m][j] - bs[K+i][j] + bs[i][j] + ws[K+i][m] - ws[K+i][K+j] - ws[i][m] + ws[i][K+j] + ws[m][K+j] - ws[K+i][K+j] - ws[m][j] + ws[K+i][j] + ws[i][K+j] - ws[i][j] + ws[K+i][j] - ws[i][j] ); r = max(r, ws[m][m] - ws[K+i][m] - ws[m][K+j] + ws[K+i][K+j] + ws[K+i][K+j] - ws[i][K+j] - ws[K+i][j] + ws[i][j] + ws[i][m] - ws[i][K+j] + ws[m][j] - ws[K+i][j] + ws[i][j] + bs[K+i][m] - bs[K+i][K+j] - bs[i][m] + bs[i][K+j] + bs[m][K+j] - bs[K+i][K+j] - bs[m][j] + bs[K+i][j] + bs[i][K+j] - bs[i][j] + bs[K+i][j] - bs[i][j] ); } } writeln(r); }
D
import std.functional, std.algorithm, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv; void main() { ulong[] XY = readln.chomp.split.to!(ulong[]); ulong X = XY[0], Y = XY[1]; ulong n; while (X*2^^n <= Y) { n++; } writeln(n); }
D
import std.stdio,std.conv,std.string; void main(){int x=to!int(readln.chomp);writeln(x*x*x);}
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[] 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 A = aryread(); long ans; foreach (i; 0 .. N) { while (A[i] % 2 == 0) ans++, A[i] /= 2; } writeln(ans); }
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, X, Y; scan(N, X, Y); X--, Y--; auto ans = new long[](N); foreach (i; 0 .. N) { foreach (j; i + 1 .. N) { long a = j - i; long b = abs(i - X) + abs(j - Y) + 1; long c = abs(j - X) + abs(i - Y) + 1; ans[min(a, b, c)]++; } } foreach (i; 1 .. N) writeln(ans[i]); }
D
/+ dub.sdl: name "D" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; int main() { auto sc = new Scanner(stdin); int n; sc.read(n); if (n == 2) { writeln("Second"); return 0; } int[] deg = new int[n]; int[][] g = new int[][](n); foreach (i; 0..n-1) { int a, b; sc.read(a, b); a--; b--; deg[a]++; deg[b]++; g[a] ~= b; g[b] ~= a; } bool[] leaf = deg.map!"a==1".array; if (g.map!(v => v.map!(d => leaf[d]).sum).filter!"a>=2".empty == false) { writeln("First"); return 0; } bool ok = false; int dfs(int p, int b) { int sm = 0; foreach (d; g[p]) { if (d == b) continue; sm += dfs(d, p); } if (sm >= 2) ok = true; if (b == -1 && sm == 0) ok = true; return 1-sm; } int rt = iota(n).filter!(x => !leaf[x]).front; dfs(rt, -1); if (!ok) { writeln("Second"); } else { writeln("First"); } return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { 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) { 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; 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 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; if (f.eof) return false; line = lineBuf[]; f.readln(line); } 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 { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { assert(buf.length == T.length); } x = buf; line.length = 0; } } 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); } } }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { auto x = lread(); foreach (i; x .. (x + 10 ^^ 6)) { if (func(i)) { writeln(i); return; } } } bool func(long x) { foreach (i; 2 .. x) { if (x % i == 0) { return false; } } return true; } 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; import std.uni; import std.string; import core.stdc.stdio; import std.array; import std.algorithm; int main(string[] argv) { int n,k; scanf("%d", &n); int [] arr = new int[n]; for(int i = 0; i<n; i++){ int h,m; scanf("%d:%d", &h,&m); h*=60; arr[i] = h+m; } arr.sort(); int mx=0; for(int i=0;i<n-1;i++){ int dif = arr[i+1] - arr[i] - 1; if(dif>mx)mx = dif; } int dif = 1440 - arr[n-1] + arr[0] - 1; if(dif>mx)mx=dif; if(n==1){ printf("23:59\n"); } if(n!=1){ if(mx/600==0){ printf("0"); } printf("%d:", mx/60); if((mx-mx/60*60)/10 == 0){ printf("0"); } printf("%d\n",mx-mx/60*60); } return 0; }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int x, y; readV(x, y); writeln(x+y/2); }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; import std.string; import std.uni; import std.range; void main() { auto a = readln.strip.repeat(10).join(""); if (a.indexOf(readln.strip) != -1) writeln("Yes"); else writeln("No"); }
D
void main(){ string s = readln().chomp(); foreach(i,c; s){ if( (i+1)%2 ){ if(c!='R' && c!='U' && c!='D'){ writeln("No"); return; } }else{ if(c!='L' && c!='U' && c!='D'){ writeln("No"); return; } } } writeln("Yes"); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range; const long mod = 10^^9+7; // 1要素のみの入力 T inelm(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] inln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split())ln ~= elm.to!T(); return ln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto nab = readln.split.to!(int[]); writeln(nab[0] * nab[1] > nab[2] ? nab[2] : nab[0] * nab[1]); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void main() { auto dts = readln.split.to!(int[]); auto D = dts[0]; auto T = dts[1]; auto S = dts[2]; writeln(D <= T * S ? "Yes" : "No"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto hw = readln.split.to!(int[]); auto H = hw[0]; auto W = hw[1]; auto MAP = new char[][](H, W); foreach (i; 0..H) { foreach (j, c; readln.chomp) MAP[i][j] = c; } foreach (i; 0..H) foreach (j; 0..W) if (MAP[i][j] == '#') { bool ok; if (i > 0 && MAP[i-1][j] == '#') ok = true; if (i < H-1 && MAP[i+1][j] == '#') ok = true; if (j > 0 && MAP[i][j-1] == '#') ok = true; if (j < W-1 && MAP[i][j+1] == '#') ok = true; if (!ok) { writeln("No"); return; } } writeln("Yes"); }
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.format; void main() { auto input1 = readln.chomp.to!int; int maximumAmount = 0; int totalPaymentAmount; foreach(i; 0..input1){ auto input2 = readln.chomp.to!int; if(maximumAmount <= input2){ totalPaymentAmount += maximumAmount; maximumAmount = input2; } else { totalPaymentAmount += input2; } } writeln(totalPaymentAmount + (maximumAmount/2)); }
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 rd = readln.split.map!(to!int); auto w = rd[0], h = rd[1], x = rd[2], y = rd[3], r = rd[4]; writeln(x >= r && x <= w - r && y >= r && y <= h - r ? "Yes" : "No"); }
D
import std.stdio, std.string, std.conv; import std.typecons; import std.algorithm, std.array, std.range, std.container; import std.math; void main() { auto data = readln.split, N = data[0].to!int, X = data[1].to!int; auto x = readln.split.to!(int[]); x = x.map!(a => (a-X).abs).array; auto minx = x.reduce!"min(a,b)"; label: foreach_reverse(i; 1 .. minx+1) { foreach (y; x) { if (y % i != 0) continue label; } writeln(i); return; } }
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(); 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 main() { long n, m, x; scan(n, m, x); long[] a = new long[114 /*514*/ ]; auto admission = aryread(); foreach (e; admission) { a[e] = 1; } auto cost_to_n = sum(a[x .. n]); auto cost_to_zero = sum(a[0 .. x]); min(cost_to_n, cost_to_zero).writeln(); }
D
import std.stdio; import std.algorithm; import std.string; import std.functional; import std.array; import std.conv; import std.math; import std.typecons; import std.regex; import std.range; int n,s; int saiki(int n1,int u,int m){ if(n1 > s) return 0; else if(m==1){ int d = s-n1; if(d <= 9 && (u & (1<<d))==0) return 1; else return 0; }else if(m==2){ if(s-n1>26) return 0; }else if(m==3){ if(s-n1>50) return 0; }else if(m==4){ if(s-n1>80) return 0; }else if(m==5){ if(s-n1>115) return 0; }else if(m==6){ if(s-n1>154) return 0; }else if(m==7){ if(s-n1>196) return 0; } int ans; for(int i=9;i>=0;i--){ if((u & (1<<i))==0){ ans += saiki(n1+m*i,u|(1<<i),m-1); } } return ans; } void main(){ while(true){ auto ss = readln(); if(stdin.eof()) break; int[] s1 = ss.split().to!(int[]); n = s1[0]; s = s1[1]; if(n <= 330) writeln(saiki(0,0,n)); else writeln(0); } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1L<<60; void main() { long n, s; scan(n); scan(s); if (n == s) { writeln(n + 1); return; } if (n < s) { writeln(-1); return; } for (long b = 2; b*b <= n; b++) { if (digitSum(n, b) == s) { writeln(b); return; } } long ans = inf; for (long p = 1; p*p <= n; p++) { long q = s - p; if ((n - q) % p == 0) { long b = (n - q) / p; if (b > 1 && digitSum(n, b) == s) { ans = min(ans, b); } } } debug { if (ans < inf) { writefln("%d, %d == %d", ans, digitSum(n, ans), s); } } writeln(ans == inf ? -1 : ans); } long digitSum(long n, long b) { return n > 0 ? digitSum(n/b, b) + (n % b) : 0; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void main() { int n; readV(n); writeln((n%1000)%111 == 0 || (n/10)%111 == 0 ? "Yes" : "No"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { int solve(int n, int x, int p) { if (p == 0) return x == 0 ? 1 : 0; if (n == 0) return 0; int r; if (x >= n) r += solve(n-1, x-n, p-1); r += solve(n-1, x, p); return r; } for (;;) { auto nx = readln.split.to!(int[]); auto n = nx[0]; auto x = nx[1]; if (n == 0 && x == 0) return; writeln(solve(n, x, 3)); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto A = readln.chomp.to!int; auto B = readln.chomp.to!int; auto C = readln.chomp.to!int; auto D = readln.chomp.to!int; auto E = readln.chomp.to!int; auto ts = [A, B, C, D, E]; sort!"(a+9)%10 < (b+9)%10"(ts); auto r = ts[0]; foreach (t; ts[1..$]) { r += (t+9)/10*10; } writeln(r); }
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(long x, long y) { long p = x; int ans = 1; while (p * 2 <= y) { p *= 2; ans++; } return ans; } void main() { auto xy = reads!long; long x = xy[0], y = xy[1]; writeln(calc(x, y)); }
D
import std.stdio; import std.string; import std.algorithm; import std.conv; void main() { auto arr = readln.chomp.split.map!(to!int); int result = 0; for (int i = arr[0]; i <= arr[1]; i++) { if (arr[2] % i == 0) result++; } result.writeln; }
D
import std.algorithm; import std.array; import std.ascii; 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 log(A...)(A arg) { stderr.writeln(arg); } int size(T)(in T s) { return cast(int)s.length; } void main() { auto s = readln.chomp; int from = s.size, to = -1; foreach (int i, c; s) { if (c == 'A') { from = min(i, from); } else if (c == 'Z') { to = max(i, to); } } writeln(to - from + 1); }
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; bool calc(int[] a) { int x = 0; for (int i = 0; i < a.length; i++) { if (i + 1 != a[i]) { x++; } } return x <= 2; } void main() { readint; auto a = readints; writeln(calc(a) ? "YES" : "NO"); }
D
import std.stdio; void main(){ auto inputs = readln; if(inputs[2] == inputs[3] && inputs[4] == inputs[5]) writeln("Yes"); else writeln("No"); }
D
void main() { long[] tmp = rdRow; long n = tmp[0], k = tmp[1]; long result; if (k) { foreach (i; k+1 .. n+1) { long d = n / i; long cnt = (i - k) * d; long rem = n - i * d; cnt += max(0, rem-k+1); result += cnt; } } else result = n ^^ 2; result.writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void 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
void main() { long n, q; rdVals(n, q); long[][] tree = new long[][](n); foreach (i; 0 .. n-1) { long a, b; rdVals(a, b); --a, --b; tree[a] ~= b, tree[b] ~= a; } long[] lists = new long[n]; foreach (i; 0 .. q) { long p, x; rdVals(p, x); --p; lists[p] += x; } bool[] ok = new bool[n]; long[] que; que ~= 0; while (!que.empty) { long f = que[0]; que.popFront; ok[f] = true; foreach (t; tree[f]) { if (ok[t]) continue; lists[t] += lists[f]; que ~= t; } } foreach (i, l; lists) { l.write; if (i == n - 1) writeln; else " ".write; } } 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
void main() { long n, k; rdVals(n, k); long[] list = new long[n]; foreach (i; 0 .. k) { long d = rdElem; long[] a = rdRow; foreach (x; a) { ++list[x-1]; } } long result; foreach (l; list) { if (l) continue; ++result; } 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.stdio,std.string,std.conv,std.algorithm,std.range; void main() { auto n = readln.chomp.to!ulong; if(n==0) 1.writeln; else iota(1, n + 1).reduce!"a*b".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)); } 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 bool[](t); foreach (ti; 0..t) { auto r = RD; auto b = RD; auto d = RD; auto x = min(b, r); auto y = max(b, r); ans[ti] = y-x <= d * x; } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional; import std.algorithm, std.container; struct Box{ long a, b, c; } enum MOD = 1000000007L; void main() { long N = scanElem; string s1 = readln.strip; string s2 = readln.strip; long p; int i; if(s1[0]==s2[0]) { p = 3; i=1; }else{ p = 6; i=2; } for(;i < N;i++) { if(s1[i]==s2[i]) { if(s1[i-1]==s2[i-1]) { p = p*2%MOD; } }else{ if(s1[i-1]!=s2[i-1]) { p = p*3%MOD; }else{ p = p*2%MOD; } i++; } } writeln(p); } class UnionFind{ UnionFind parent = null; void merge(UnionFind a) { if(same(a)) return; a.root.parent = this.root; } UnionFind root() { if(parent is null)return this; return parent = parent.root; } bool same(UnionFind a) { return this.root == a.root; } } void scanValues(TList...)(ref TList list) { auto lit = readln.splitter; foreach (ref e; list) { e = lit.fornt.to!(typeof(e)); lit.popFront; } } T[] scanArray(T = long)() { return readln.split.to!(long[]); } void scanStructs(T)(ref T[] t, size_t n) { t.length = n; foreach (ref e; t) { auto line = readln.split; foreach (i, ref v; e.tupleof) { v = line[i].to!(typeof(v)); } } } T scanElem(T = long)() { char[] res; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { res ~= cast(char) c; c = getchar; } return res.strip.to!T; } template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } struct Factor { long n; long c; } Factor[] factors(long n) { Factor[] res; for (long i = 2; i ^^ 2 <= n; i++) { if (n % i != 0) continue; int c; while (n % i == 0) { n = n / i; c++; } res ~= Factor(i, c); } if (n != 1) res ~= Factor(n, 1); return res; } long[] primes(long n) { if(n<2)return []; auto table = new long[n+1]; long[] res; for(int i = 2;i<=n;i++) { if(table[i]==-1) continue; for(int a = i;a<table.length;a+=i) { table[a] = -1; } res ~= i; } return res; } bool isPrime(long n) { if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (long i = 3; i ^^ 2 <= n; i += 2) if (n % i == 0) return false; return true; }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; void main(){ auto x = readln.chomp.to!int; auto a = readln.chomp.to!int; auto b = readln.chomp.to!int; writeln((x - a) % b); }
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 long[](t); foreach (ti; 0..t) { auto a = RD; auto b = RD; long bb; if (b == 1) { ++bb; } ans[ti] = long.max; while (true) { long tmp; auto x = a; auto y = b + bb; while (x) { x /= y; ++tmp; } tmp += bb; if (tmp <= ans[ti]) ans[ti] = tmp; else break; ++bb; } } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.math; void main() { int n, x; scan(n, x); auto m = iota(n).map!(i => readln.chomp.to!int).array; writeln(n + (x - m.sum) / m.reduce!min); } 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.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void main() { int a, b, c, d, e, f; readV(a, b, c, d, e, f); auto nu = 0, de = a*100; foreach (o1; 0..f/100/a+1) foreach (o2; 0..(f/100-o1*a)/b+1) { auto w = (o1*a+o2*b)*100, s = min(f-w, w/100*e); foreach (o3; 0..s/c+1) { auto o4 = (s-o3*c)/d; auto t = o3*c+o4*d; if (nu*(w+t) < de*t) { nu = t; de = w+t; } } } writeln(de, " ", nu); }
D
/+ dub.sdl: name "C" dependency "dunkelheit" version=">=0.9.0" +/ 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); long B = 10L^^10; long x, d; sc.read(x, d); long U = 1; long[] res; foreach_reverse (ph; 1..40) { long y = (1L << ph) - 1; long z = x / y; x %= y; foreach (_; 0..z) { U += B; foreach (i; 0..ph) { res ~= U; } } } writeln(res.length); writeln(res.map!(to!string).join(" ")); return 0; } /* IMPORT /home/yosupo/Program/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/Program/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/Program/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; 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 { 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; 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
void main() { auto S = rs, T = rs; foreach(i; 0..S.length) { string tmp = S[1..$]; tmp ~= S[0]; if(S == T) { writeln("Yes"); return; } S = tmp; } writeln("No"); } // =================================== import std.stdio; import std.string; import std.functional; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.string, std.range, std.algorithm, std.math, std.typecons, std.conv; long[] weights, values; long N; long W; long w0; long[][][] memo; void main() { auto inp = readln.split.to!(long[]); N = inp[0]; W = inp[1]; weights = new long[N]; values = new long[N]; foreach (i; 0..N) { inp = readln.split.to!(long[]); auto w = inp[0], v = inp[1]; weights[i] = w; values[i] = v; } w0 = weights[0]; foreach (i; 0..N) { weights[i] -= w0; } memo = new long[][][](N,weights.sum+1,N); func(0, 0, 0).writeln; } long func(int i, long w, int count) { if (i == N) return 0; if (memo[i][w][count] > 0) return memo[i][w][count]; auto cand1 = func(i+1, w, count); if (w + weights[i] > W - (count+1) * w0) return memo[i][w][count] = cand1; auto cand2 = func(i+1, w + weights[i], count+1) + values[i]; return memo[i][w][count] = max(cand1, cand2); }
D
import std.stdio, std.string, std.conv; import std.algorithm, std.array; import std.math; auto solve(string s_) { immutable N = s_.to!int(); auto AS = new int[N]; foreach(ref a;AS) a=readln.chomp().to!int(); int[2][int] c; foreach(i;0..N) { immutable v=AS[i]; if(v !in c) c[v]=[0,0]; c[v][i&1]++; } immutable bs = sort(AS.dup).array(); foreach(i;0..N) { immutable v=bs[i]; if(v !in c) c[v]=[0,0]; c[v][i&1]--; } int p=0; foreach(ref a;c) p+=(abs(a[0])+abs(a[1]))/2; return (p+1)/2; } void main(){ for(string s; (s=readln.chomp()).length;) writeln(solve(s)); }
D
import std.stdio; import std.ascii; import std.numeric; import std.algorithm; import core.stdc.stdio; int main() { int t = readInt!int; foreach(ti; 0 .. t) { int n = readInt!int; int[] a = new int[](n); int[] even; int[] odd; foreach(ref ai; a) { ai = readInt!int; if (ai & 1) odd ~= ai; else even ~= ai; } long count = 0; foreach(i, oi; odd) { foreach(j; i + 1 .. odd.length) { if (gcd(oi, odd[j]) > 1) { count++; } } } count += even.length * (even.length - 1) / 2; count += even.length * odd.length; count.writeln; } return 0; } /* INPUT ROUTINES */ int currChar; static this() { currChar = getchar(); } char topChar() { return cast(char) currChar; } void popChar() { currChar = getchar(); } auto readInt(T)() { T num = 0; int sgn = 0; while (topChar.isWhite) popChar; while (topChar == '-' || topChar == '+') { sgn ^= (topChar == '-'); popChar; } while (topChar.isDigit) { num *= 10; num += (topChar - '0'); popChar; } if (sgn) return -num; return num; } string readString() { string res = ""; while (topChar.isWhite) popChar; while (!topChar.isWhite) { res ~= topChar; popChar; } return res; } /**MODULAR SYSTEM*/ struct Z(immutable long m) { long rep; this(long num) { rep = num; } Z!m opBinary(string operator)(Z!m rhs) { static if (operator == "+") { long result = rhs.rep + this.rep; if (result >= m) result -= m; return Z!m(result); } else static if (operator == "-") { long result = this.rep - rhs.rep; if (result < 0) result += m; return Z!m(result); } else static if (operator == "*") { long result = this.rep * rhs.rep; if (result >= m) result %= m; return Z!m(result); } else static assert(text("Operator ", operator, " not supported")); } Z!m opBinary(string operator)(long exponent) if (operator == "^^") { assert(exponent >= 0); Z!m base = this; Z!m result = 1; while (exponent) { if (exponent & 1) result = result * base; base = base * base; exponent >>= 1; } return result; } invariant { assert(rep >= 0 && rep < m); } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n, k; scan(n, k); auto mc = ModComb(n * 2); foreach (i ; 1 .. k + 1) { auto ans = mc.c(k - 1, k - i) * mc.c(n - k + 1, n - k - (i - 1)) % mod; writeln(ans); } } struct ModComb { int _N; long[] _fact, _factinv; long _mod; this(int n, long mod = 1_000_000_007L) { _N = n; _fact = new long[](_N + 1); _factinv = new long[](_N + 1); _mod = mod; _fact[0] = 1; foreach (i ; 1 .. _N + 1) { _fact[i] = (_fact[i-1] * i) % mod; } _factinv[_N] = _powmod(_fact[_N], mod - 2); foreach_reverse (i ; 0 .. _N) { _factinv[i] = (_factinv[i+1] * (i+1)) % mod; } } long c(int n, int k) { if (k < 0) return 0; return f(n) * finv(n - k) % _mod * finv(k) % _mod; } long p(int n, int r) { return f(n) * finv(n - r) % _mod; } long f(int n) { return _fact[n]; } long finv(int n) { return _factinv[n]; } long _powmod(long x, long y) { return y > 0 ? _powmod(x, y>>1)^^2 % _mod * x^^(y & 1) % _mod : 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); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.algorithm; import std.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 f(long n) { if (n <= 0) return 0; // 0, 1, 2, ..., n の排他的論理和を求める if (n % 2 == 1) { if ((n + 1) / 2 % 2 == 0) return 0; // 偶数奇数のペアが偶数個 return 1; } else { // 0, 1, 2, ..., (n - 1) までの偶数奇数のペアが偶数個 if (n / 2 % 2 == 0) return n; return n ^ 1; } } void main() { auto ab = reads!long; long a = ab[0], b = ab[1]; auto ans = f(a - 1) ^ f(b); writeln(ans); }
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() { auto a = reads(); if (a[0] + a[1] == 15) writeln("+"); else if (a[0] * a[1] == 15) writeln("*"); else writeln("x"); }
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 x = readln.chomp.to!int; auto res = (x / 500) * 1000; res += ((x % 500) / 5) * 5; res.writeln; }
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!(string, "x", long, "y"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { auto n = lread(); auto Music = new Pair[](n); foreach (i; iota(n)) { string title; long time; scan(title, time); Music[i] = Pair(title, time); } auto x = sread(); bool flag; long ans; foreach (e; Music) { if (e.x == x) { flag = true; continue; } if (flag) ans += e.y; } ans.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.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { int x, y; scan(x, y); int ans; if (1L * x * y != 0) { if (0 < x && x < y) ans = y - x; else if (0 < y && y < x) ans = x - y + 2; else if (x < y && y < 0) ans = y - x; else if (y < x && x < 0) ans = x - y + 2; else if (x < 0 && 0 < y && abs(x) < abs(y)) ans = y + x + 1; else if (x < 0 && 0 < y && abs(x) == abs(y)) ans = 1; else if (x < 0 && 0 < y && abs(x) > abs(y)) ans = -y - x + 1; else if (y < 0 && 0 < x && abs(x) < abs(y)) ans = -y - x + 1; else if (y < 0 && 0 < x && abs(x) == abs(y)) ans = 1; else if (y < 0 && 0 < x && abs(x) > abs(y)) ans = y + x + 1; } else { if (x == 0) { ans = (y > 0 ? y : abs(y) + 1); } else { ans = (x > 0 ? x + 1 : abs(x)); } } writeln(ans); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.array; import std.algorithm; import std.conv; import std.numeric; import std.string; void main() { int N = readln.chomp.to!int; long[] L = new long[N + 1]; L[0] = 2; L[1] = 1; for (int i = 2; i <= N; ++i) L[i] = L[i - 1] + L[i - 2]; L[N].writeln; }
D
import std.stdio, std.conv, std.string, std.range, std.typecons, std.format, core.bitop; import std.algorithm.searching, std.algorithm.sorting, std.algorithm.iteration, std.algorithm.mutation; void main() { long[] input = readln().split.to!(long[]); long N = input[0]; long K = input[1]; long[] list = readln().split.to!(long[]); long x = 0; for(long i = 0;i < 41;i++) { int fragCount; foreach(e; list) { if((e&(1L<<i))!=0) { fragCount++; } } x |= (fragCount > list.length-fragCount?0:1L)<<i; } foreach_reverse(long i; 0..41) { if((x & (1L<<i)) < (K & (1L<<i))) { break; } if((x & (1L<<i)) > (K & (1L<<i))) { x &= ~(1L<<i); } } long sum; foreach(e; list) { sum += e^x; } writeln(sum); }
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; immutable long INF = 1L << 59; long even(long n) { return n == 0 ? 2 : n % 2; } long odd(long n) { return 1 - n % 2; } void main() { auto N = readln.chomp.to!int; auto A = N.iota.map!(_ => readln.chomp.to!long).array; auto dp = new long[][](N+1, 5); foreach (i; 0..N+1) dp[i][] = INF; dp[0][0] = 0; //foreach (i; 0..N) { // dp[i+1][0] = dp[i][0] + A[i]; //} foreach (i; 0..N) { long m = dp[i][0]; dp[i+1][0] = min(dp[i+1][0], m + A[i]); m = min(m, dp[i][1]); dp[i+1][1] = min(dp[i+1][1], m + even(A[i])); m = min(m, dp[i][2]); dp[i+1][2] = min(dp[i+1][2], m + odd(A[i])); m = min(m, dp[i][3]); dp[i+1][3] = min(dp[i+1][3], m + even(A[i])); m = min(m, dp[i][4]); dp[i+1][4] = min(dp[i+1][4], m + A[i]); } dp[N].reduce!min.writeln; }
D
import std.stdio; void main() { int n, h, x; int result = 0; scanf("%d%d", &n, &h); for (int i = 0; i < n; i++) { scanf("%d", &x); result += (x > h) ? 2 : 1; } printf("%d\n", result); }
D
import std.stdio; void main() { int n; scanf("%d", &n); char[][] m = new char[][n]; for (int i = 0; i < n; i++) { m[i] = new char[n + 1]; scanf("%s", m[i].ptr); } int res = 0; for (int i = 0; i < n; i++) { int r = 0; int c = 0; for (int j = 0; j < n; j++) { if (m[j][i] == 'C') c++; if (m[i][j] == 'C') r++; } res += (r - 1) * r / 2; res += (c - 1) * c / 2; } printf("%d\n", res); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto q = RD!int; auto ans = new bool[](q); foreach (i; 0..q) { auto s = RD!string; auto t = RD!string; auto f1 = new bool[](26); auto f2 = new bool[](26); foreach (c; s) f1[c-'a'] = true; foreach (c; t) f2[c-'a'] = true; foreach (j; 0..26) { if (f1[j] && f2[j]) { ans[i] = true; break; } } } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush(); debug readln(); }
D
import std.stdio; import std.string; import std.algorithm; import std.conv; import std.range; void main() { int n = readln.chomp.to!int; int[][] diploma; for (int i = 0; i < 3; i++) { diploma ~= readln.chomp.split.map!(to!int).array; } int dip_1 = min(diploma[0][1], n - (diploma[1][0] + diploma[2][0])); n -= dip_1; int dip_2 = min(diploma[1][1], n - diploma[2][0]); int dip_3 = n - dip_2; writeln(dip_1, " ", dip_2, " ", dip_3); }
D
import std.algorithm, std.stdio, std.string; void main () { string s; while ((s = readln.strip) != "") s.count !(x => "aeiou13579".canFind (x)).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 string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto T = RD; long best = long.max; long ans; foreach (i; 0..N) { auto s = RD; auto d = RD; auto x = s - T; auto y = abs(x / d); if (x < 0) { x += (y+1) * d; x %= d; } if (x < best) { best = x; ans = i + 1; } debug writeln(i, " ", best, " ", ans); } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio; void main() { int n; scanf("%d", &n); printf("%lld\n", (1L << n + 1) - 1 - 1); }
D
import std.stdio; import std.range; import std.uni; import std.conv; import std.container; import std.functional; import std.algorithm; import std.array; import std.typecons; void main(string[] args) { inputFile = stdin; debug inputFile = File(args[1]); auto nt = next!int; foreach(t; 0 .. nt) { auto n = next!int; auto x = next!int; auto y = next!int; auto diff = y - x; int bestjump = -1; int bestjumpmax = int.max; foreach(jump; 1 .. diff + 1) { if (diff % jump == 0) { if (diff / jump + 1 > n) continue; int rem = n - (diff / jump + 1); int lessthan = cast(int) iota(1, x).count!(num => num % jump == x % jump); rem -= min(rem, lessthan); int jumpmax = y + rem * jump; if (jumpmax < bestjumpmax) { bestjump = jump; bestjumpmax = jumpmax; } } } foreach(i; 0 .. n) { write(bestjumpmax - bestjump * i, " "); } writeln; } } // INPUT enum InputStyle { byChunk, byLine }; enum inputStyle = InputStyle.byLine; File inputFile; string popWord(); static if (inputStyle == InputStyle.byChunk) { const chunkSize = 1024 * 1024 * 8; const wordSize = 4096; char[chunkSize] chunkBuff; char[] currChunk; void renewChunk() { if (inputFile.eof) currChunk = null; else currChunk = inputFile.rawRead(chunkBuff); } char[wordSize] wordBuff; char[] word; string popWord() { if (currChunk.length == 0) renewChunk; assert(currChunk.length > 0); while (currChunk[0].isWhite) { currChunk = currChunk[1 .. $]; if (currChunk.length == 0) renewChunk; } word = wordBuff[0 .. 0]; while (!currChunk[0].isWhite) { word.length++; word[$ - 1] = currChunk[0]; currChunk = currChunk[1 .. $]; if (currChunk.length == 0) { renewChunk; if (currChunk.length == 0) return cast(string) word; } } return cast(string) word; } } else { DList!string _words; string popWord() { while (_words.empty) { foreach(w; inputFile.readln.split) { _words.insertBack(w); } } auto word = _words.front; _words.removeFront; return word; } } T next(T)() { return to!T(popWord); } auto next(T, S...)(S s) { static if (S.length == 0) { return to!T(popWord); } else { auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]); foreach(ref elem; res) elem = next!T(s[1 .. $]); return res; } }
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 da = RD!int; auto db = RD!int; if (da+1 == db) { writeln(da, "9 ", db, "0"); } else if (da == db) { writeln(da, "0 ", db, "1"); } else if (da == 9 && db == 1) { writeln(da, " ", db, "0"); } else { writeln(-1); } stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } struct UnionFind(T) { void init(T n) { par = new T[](n); foreach (i; 0..n) par[i] = i; cnt = new T[](n); fill(cnt, 1); } T root(T i) { return par[i] == i ? i : (par[i] = root(par[i])); } bool same(T i, T j) { return root(i) == root(j); } void unite(T i, T j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; cnt[j] += cnt[i]; } T size(T i) { return cnt[root(i)]; } T[] par, cnt; } void main() { auto N = RD; auto M = RD; UnionFind!long uf; uf.init(N); foreach (i; 0..M) { auto A = RD-1; auto B = RD-1; uf.unite(A, B); } long ans; foreach (i; 0..N) { ans.chmax(uf.size(i)); } writeln(ans); stdout.flush; debug readln; }
D
void main() { writeln(readln.chomp.to!int ^^ 2); } 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.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } bool isPrime(int n) { if (n < 2) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (int i = 3; i * i <= n; i += 2) { if (n % i == 0) return false; } return true; } void main() { string s; while ((s = readln.chomp) != null) { int n = s.to!int; int ans = 0; for (int i = 1; i <= n; i++) { int a = i; int b = n - i + 1; if (isPrime(a) && isPrime(b)) ans++; } writeln(ans); } }
D
void main(){ int r = _scan(); (3*r^^2).writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio; import std.conv; import std.array; import std.string; void main() { string s, str = ""; while ((s = readln()).length) { str ~= s; } for (int i = 0; i < 26; i++) { int cnt; cnt += str.count('a' + i); cnt += str.count('A' + i); writeln(to!char('a' + i), " : ", cnt); } }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; 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() { int x, a; scan(x, a); writeln(x < a ? 0 : 10); }
D
import std.stdio; import std.array; import std.conv; import std.algorithm; import std.string; import std.typecons; import std.format; int[Tuple!(long, long, long)] patterns; long answer; void solve(long a, long b, long c, long x) { auto key = tuple(a, b, c); if (key in patterns) { return; } patterns[key] = 0; if (x == 0) { answer++; } else if (x < 0) { return; } if (a > 0) { solve(a-1, b, c, x-500); } if (b > 0) { solve(a, b-1, c, x-100); } if (c > 0) { solve(a, b, c-1, x-50); } } void main(string[] argv) { long a = readln.chomp.to!long; long b = readln.chomp.to!long; long c = readln.chomp.to!long; long x = readln.chomp.to!long; answer = 0; solve(a, b, c, x); writeln(answer); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n, m; readV(n, m); int[] a; readA(n, a); auto ca = new int[](n+1); foreach (i; 0..n) ca[i+1] = (ca[i]+a[i])%m; int[int] h; auto r = 0L; foreach_reverse (l; 0..n+1) { auto k = ca[l]; if (k in h) r += h[k]; ++h[k]; } writeln(r); }
D
void main() { auto ip = readln.split, X = ip[0], Y = ip[1]; if(X < Y) writeln("<"); else if(X > Y) writeln(">"); else writeln("="); } // =================================== import std.stdio; import std.string; import std.functional; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto q = RD!int; auto ans = new long[](q); long[] list = [1]; while (true) { auto x = list[$-1] * 3; if (x < list[$-1]) break; list ~= x; } list.reverse; debug writeln("a"); foreach (loop; 0..q) { auto n = RD; long[] x; auto dp = new long[][](list.length+1); dp[0] = [0]; foreach (i; 0..list.length) { foreach (j; 0..dp[i].length) { auto tmp = dp[i][j] + list[i]; if (tmp >= n) { x ~= tmp; dp[i+1] ~= dp[i][j]; } else if (dp[i][j] + list[i]*2 >= n) { dp[i+1] ~= tmp; dp[i+1] ~= dp[i][j]; } } } debug writeln(x); debug writeln(dp); ans[loop] = x[x.MIN_POS]; } foreach (e; ans) writeln(e); stdout.flush(); debug readln(); }
D
import std.algorithm; import std.math; import std.range; import std.ascii; import std.array; import std.container; import std.conv; import std.numeric; import std.stdio; import std.string; import std.typecons; void log(A...)(A arg) { stderr.writeln(arg); } class UnreliableRollingHash(ulong P) { // fast but unreliable rolling hash // base is P, modulus is 2^64 ulong[] H; this(in string s) { auto N = s.length; this.H = new ulong[](N + 1); foreach (int i, c; s) { H[i + 1] = (H[i] * P + cast(int)(c - 'a' + 1)); } } ulong hash(int l, int r) { // [l, r) return H[r] - H[l] * pow(P, r - l); } } void main() { int N, M; scanf("%d %d\n", &N, &M); auto s = readln.chomp; auto hasher = new UnreliableRollingHash!991(s); int l = 0, r = 1; bool[ulong] appeared; foreach (_; 0 .. M) { auto q = readln.chomp; switch (q) { case "L++": l++; break; case "L--": l--; break; case "R++": r++; break; case "R--": r--; break; default: assert(false); } appeared[hasher.hash(l, r)] = true; } writeln(appeared.keys.length); }
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() { int a, b, c; scan(a, b, c); int ans = c - min(a - b, c); writeln(ans); }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int L; rd(L); struct E { int from, to, cost; } E[] edges; int f(int K) { if (K == 2) { edges ~= E(1, 2, 0); edges ~= E(1, 2, 1); return 2; } if (K & 1) { auto last = f(K - 1); edges ~= E(1, last, K - 1); return last; } else { auto last = f(K / 2); foreach (ref e; edges) { e.cost *= 2; } edges ~= E(last, last + 1, 0); edges ~= E(last, last + 1, 1); return last + 1; } } auto n = f(L); writeln(n, " ", edges.length); foreach (e; edges) { writeln(e.from, " ", e.to, " ", e.cost); } } 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.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; bool solve() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto S = s[2]-1; auto G1 = s[3]-1; auto G2 = s[4]-1; if (N == 0) return false; auto D = new int[][](N, N); foreach (i; 0..N) foreach (j; 0..N) D[i][j] = i == j ? 0 : 1 << 29; foreach (i; 0..M) { s = readln.split.map!(to!int); D[s[0]-1][s[1]-1] = s[2]; } foreach (i; 0..N) foreach (j; 0..N) foreach (k; 0..N) D[j][k] = min(D[j][k], D[j][i] + D[i][k]); N.iota.map!(i => D[S][i] + D[i][G1] + D[i][G2]).reduce!min.writeln; return true; } void main() { while (solve()) {} }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; long P = 10^^9+7; long[10^^5*4+50] 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; } 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! } void main() { init(); auto nk = readln.split.to!(long[]); auto n = nk[0]; auto k = nk[1]; long r; foreach (i; 0..min(n, k+1)) { r = (r + (comb(n, i) * comb(n-1, n-i-1)) % P) % P; } writeln(r); }
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!int; auto x = n / 2; writeln(x + 1); foreach (i; 0..n) { writeln(i / 2 + 1, " ", (i+1) / 2 + 1); } //writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio; import std.algorithm; import std.conv; import std.string; int f(int n) { return n*(n+1)/2; } void main() { int n, l; scanf("%d %d", &n, &l); if (l <= 0 && l + n > 0) { write(f(l+n-1)-f(-l)); } else if (l > 0) { write(f(l+n-1)-f(l)); } else { write(-f(-l)+f(-l-n+1)); } }
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; void main() { int n = readln.chomp.to!int; int[] p = new int[](n); foreach (ref v; p) { v = readln.chomp.to!int; } bool[] done = new bool[](n + 1); int[] memo = new int[](n + 1); foreach (v; p) { if (done[v - 1]) { memo[v] = memo[v - 1]; } else { memo[v] = v; } done[v] = true; } int ans = 0; foreach (v; p) { ans = max(ans, v - memo[v]); } ++ans; ans = n - ans; ans.writeln; } 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; import std.conv; import std.string; import std.algorithm; void main(){ int n=to!int(readln.chomp); string[] a; while(n--) a~=readln.chomp; sort(a); writeln(a[0]); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void pickV(R,T...)(ref R r,ref T t){foreach(ref v;t)pick(r,v);} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n; readV(n); auto a = new int[](n), op = new string[](n), rd = rdsp; pick(rd, a[0]); op[0] = "+"; foreach (i; 1..n) pickV(rd, op[i], a[i]); auto dp = new long[][](n+1, 3); foreach (ref dpi; dp) dpi[] = -10L^^17; dp[0][0] = 0; foreach (i; 0..n) { if (op[i] == "+") { auto r0 = dp[i][0]+a[i]; auto r1 = dp[i][1]-a[i]; auto r2 = dp[i][2]+a[i]; dp[i+1][0] = max(r0, r1, r2); dp[i+1][1] = max(r1, r2); dp[i+1][2] = r2; } else { auto r1 = max(dp[i][0], dp[i][2])-a[i]; auto r2 = dp[i][1]+a[i]; dp[i+1][0] = max(r1, r2); dp[i+1][1] = max(r1, r2); dp[i+1][2] = r2; } } writeln(dp[$-1][0]); }
D
import core.stdc.stdio; import std.typecons; import std.algorithm; void main(){ int n,e,t; scanf("%d%d%d",&n,&e,&t); --t; long[] cost = new long[n]; static immutable int inf = 1145141919; bool[] used = new bool[n]; foreach(i;0..n){ int w; scanf("%d",&w); if(w==1){ cost[i]=1; used[i]=true; }else cost[i]=inf; } alias Tuple!(long,"cost",int,"g",int[],"f") cgf; cgf[] rec = new cgf[e]; int[][] h = new int[][n]; void CalcCost(ref cgf r){ sort!((a,b)=>cost[a]<cost[b])(r.f); r.cost=0; foreach(i,v;r.f) r.cost=max(r.cost,r.f.length-i-1+cost[v]); } foreach(i;0..e){ int c; scanf("%d%d",&rec[i].g,&c); rec[i].g--; rec[i].f=new int[c]; foreach(ref v;rec[i].f){ scanf("%d",&v); h[--v]~=i; } CalcCost(rec[i]); } while(cost[t]==inf){ cgf mn = cgf(cast(long)inf,-1,[]); foreach(r;rec) if(!used[r.g]&&r.cost<mn.cost) mn=r; if(mn.g==-1){ printf("-1\n"); return; } used[mn.g]=true; cost[mn.g]=mn.cost; foreach(j;h[mn.g]) CalcCost(rec[j]); } printf("%lld\n",cost[t]); }
D
import std.functional, std.algorithm, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv; bool isNumeric(char c) { return '0' <= c && c <= '9'; } void main() { int[] AB = readln.chomp.split.to!(int[]); int A = AB[0], B = AB[1]; string S = readln.chomp; foreach (i, c; S) { if (i == A) { if (!(c == '-')) { return writeln("No"); } } else { if (!isNumeric(c)) { return writeln("No"); } } } writeln("Yes"); }
D
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } void main() { auto S = cast(char[])readln.chomp; auto N = readln.chomp.to!long; void solve() { bool inverted = false; char[200000] head; char[200000] tail; long num_head; long num_tail; foreach(_; 0..N) { auto query = readln.split; if (query[0] == "2") { if ((inverted && query[1] == "2") || !inverted && query[1] == "1") { tail[num_tail] = query[2][0]; num_tail++; } if ((inverted && query[1] == "1") || !inverted && query[1] == "2") { head[num_head] = query[2][0]; num_head++; } continue; } inverted ^= true; } if (inverted) { auto reversed_head = head[0..num_head]; reversed_head.reverse(); S.reverse(); writeln( reversed_head ~ S ~ tail[0..num_tail]); } else { auto reversed_tail = tail[0..num_tail]; reversed_tail.reverse(); writeln(reversed_tail ~ S ~ head[0..num_head]); } } solve(); }
D
string vowels = ['a', 'e', 'i', 'o', 'u']; void main(){ char c = _scan!char(); if(vowels.find(c).empty)writeln("consonant"); else writeln("vowel"); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1<<30; enum full = (1<<26) - 1; void main() { string s; scan(s); int n = s.length.to!int; auto dp = new int[](n + 1); dp[n] = 1; auto next = new int[][](n + 1, 26); fillAll(next, inf); int b = 0; foreach_reverse (i ; 0 .. n) { b |= (1<<(s[i] - 'a')); if (b == full) { dp[i] = dp[i+1] + 1; b = 0; } else { dp[i] = dp[i+1]; } foreach (j ; 0 .. 26) { if (s[i] - 'a' == j) { next[i][j] = i; } else { next[i][j] = next[i+1][j]; } } } debug { writeln(dp); writeln; writefln("%(%s\n%)", next); } auto ans = new char[](dp[0]); ans[] = '#'; int pos = 0; foreach (i ; 0 .. dp[0]) { foreach (c ; 0 .. 26) { if (next[pos][c] == inf) { ans[i] = ('a' + c).to!char; break; } if (dp[pos] == dp[next[pos][c]+1] + 1) { ans[i] = ('a' + c).to!char; pos = next[pos][c]+1; break; } } } 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
#!/usr/bin/rdmd import std.stdio: writeln, readln; import std.conv: parse; void main() { auto input = readln(); auto num = parse!int(input); writeln((num^^3)); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; long f(long b, long n) { long r; while (n) { r += n%b; n /= b; } return r; } void main() { auto n = readln.chomp.to!long; auto s = readln.chomp.to!long; if (n == s) { writeln(n + 1); } else { long i = 1; for (;;) { ++i; if (i*i > n) break; if (f(i, n) == s) { writeln(i); return; } } i = 0; long min_b = long.max; for (;;) { ++i; if (i*i > n || s - i < 0) break; auto b = (n - s) / i + 1; if (b < 2) continue; if (f(b, n) == s) { min_b = min(min_b, b); } } writeln(min_b == long.max ? -1 : min_b); } }
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/3; enum long MOD = 10L^^9+7; void main() { string str = readln.chomp; long ans = 0; long s = 0; foreach(i, c; str) { if (c == 'W') { ans += i - s++; } } ans.writeln; } // ---------------------------------------------- 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) { 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 fmt = [staticMap!(getFormat, Args)].join(" "); string[] inputs = readln.chomp.split; foreach(i, ref v; args) { v = inputs[i].to!(Args[i]); } } // 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 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; Unqual!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); Unqual!CommonElement extremeElement = seedElement; 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)))) { alias Element = ElementType!Range; Unqual!Element seed = r.front; r.popFront(); return extremum!selector(r, seed); } 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)))) { alias Element = ElementType!Range; alias CommonElement = CommonType!(Element, RangeElementType); Unqual!CommonElement extremeElement = seedElement; alias selectorFun = binaryFun!selector; // 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(); } } return extremeElement; } auto minElement(Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { return extremum(r); } auto minElement(alias map, 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 minElement(Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void)) { return extremum(r, seed); } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { return extremum!(map, "a > b")(r); } auto maxElement(Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { return extremum!`a > b`(r); } auto maxElement(alias map, 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); } auto maxElement(Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void)) { return extremum!`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.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto A = RD; auto B = RD; auto C = RD; bool ans; foreach (i; 1..101) { auto x = A * i - C; if (x % B == 0) ans = true; } writeln(ans ? "YES" : "NO"); stdout.flush(); debug readln(); }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; import std.string; import std.math; void main() { auto abc = readln.chomp.split.map!(to!int).array; long a = abc[0], b = abc[1], c = abc[2]; if (a == b && b == c && a % 2 == 0 && b % 2 == 0 && c % 2 == 0) { (-1).writeln; } else { int ans; while (a % 2 == 0 && b % 2 == 0 && c % 2 == 0) { long tmp = a / 2; a /= 2; a += c / 2; c /= 2; c += b / 2; b /= 2; b += tmp; ans++; } ans.writeln; } return; }
D
void main() { long s, w; rdVals(s, w); writeln(w < s ? "safe" : "unsafe"); } 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.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; alias Edge = Tuple!(int, "from", int, "to", int, "cost"); void main() { int N, M, P; scan(N, M, P); Edge[] es; auto adj = new int[][](N, 0); foreach (i ; 0 .. M) { int ai, bi, ci; scan(ai, bi, ci); ai--, bi--; ci -= P; es ~= Edge(ai, bi, -ci); adj[bi] ~= ai; } auto reachable = new bool[](N); reachable[N - 1] = true; void dfs(int v) { reachable[v] = true; foreach (u ; adj[v]) { if (!reachable[u]) { dfs(u); } } } dfs(N - 1); es = es.filter!(e => reachable[e.to]).array; debug { writeln(es); } auto ans = BellmanFord(N, es); writeln(ans == -inf6 ? -1 : max(-ans, 0)); } auto BellmanFord(int N, Edge[] es) { auto dist = new long[](N); dist[] = inf6; dist[0] = 0; foreach (i ; 0 .. N - 1) { foreach (e ; es) { if (dist[e.from] != inf6 && dist[e.from] + e.cost < dist[e.to]) { dist[e.to] = dist[e.from] + e.cost; } } } foreach (e ; es) { if (dist[e.from] != inf6 && dist[e.from] + e.cost < dist[e.to]) { return -inf6; } } debug { writeln(dist); } return dist[N - 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); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; // dfmt on void main() { long N = lread(); auto A = aryread(); // A_1 - A_N == d long d = A.sum(); A[] *= 2; foreach (i; 0 .. N - 1) d -= A[i] * (i & 1); auto ans = new long[](N); ans[0] = d; // writeln(ans[0]); foreach (i; 1 .. N) ans[i] = A[i - 1] - ans[i - 1]; write(ans[0]); foreach (a; ans[1 .. $]) write(" ", a); writeln(); }
D