code
stringlengths
4
1.01M
language
stringclasses
2 values
import std; void main() { int N, M, Q; scanf("%d %d %d\n", &N, &M, &Q); auto as = new int[Q]; auto bs = new int[Q]; auto cs = new int[Q]; auto ds = new int[Q]; foreach(q;0..Q) { int a, b, c, d; scanf("%d %d %d %d\n", &a, &b, &c, &d); as[q] = a-1; bs[q] = b-1; cs[q] = c; ds[q] = d; } auto As = new int[10]; long scoremax = -1; foreach(d0; 0..M){ foreach(d1; d0..M){ foreach(d2; d1..M){ foreach(d3; d2..M){ foreach(d4; d3..M){ foreach(d5; d4..M){ foreach(d6; d5..M){ foreach(d7; d6..M){ foreach(d8; d7..M){ foreach(d9; d8..M){ long score; As[0] = d0; As[1] = d1; As[2] = d2; As[3] = d3; As[4] = d4; As[5] = d5; As[6] = d6; As[7] = d7; As[8] = d8; As[9] = d9; foreach(q; 0..Q){ if (As[bs[q]]-As[as[q]] == cs[q]) score += ds[q]; } scoremax = max(score, scoremax); } } } } } } } } } } scoremax.write; }
D
import std.stdio,std.conv,std.string; void main(){ auto args = readln().chomp().split(); auto a = to!int(args[0]); auto b = to!int(args[1]); auto c = to!int(args[2]); writeln( a<b&&b<c?"Yes":"No" ); }
D
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array, std.functional, std.container, std.typecons; void main() { int N = readln.chomp.to!int; int[] A = new int[N]; foreach(ref a; A) { a = readln.chomp.to!int; } int[] pal = new int[0]; foreach(a; A) { auto lb = assumeSorted!"a>b"(pal).upperBound(a); if(lb.empty) { pal ~= a; } else { lb.front = a; } } pal.length.writeln; }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random; import std.typecons, std.functional, std.traits,std.concurrency; import std.algorithm, std.container; import core.bitop, core.time, core.memory; import std.bitmanip; import std.regex; enum INF = long.max/3; enum MOD = 10L^^9+7; //辞書順順列はiota(1,N),nextPermituionを使う void end(T)(T v) if(isIntegral!T||isSomeString!T||isSomeChar!T) { import core.stdc.stdlib; writeln(v); exit(0); } T[] scanArray(T = long)() { static char[] scanBuf; readln(scanBuf); return scanBuf.split.to!(T[]); } dchar scanChar() { int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } return cast(dchar)c; } T scanElem(T = long)() { import core.stdc.stdlib; static auto scanBuf = appender!(char[])([]); scanBuf.clear; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { scanBuf ~= cast(char) c; c = getchar; } return scanBuf.data.to!T; } dchar[] scanString(){ return scanElem!(dchar[]); } void main() { auto a=scanElem; auto b=scanElem; if(a%3==0||b%3==0||(a+b)%3==0)end("Possible");end("Impossible"); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } 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 = 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; } long[2][] primeFact(long x) { long[2][] ans; for (long i = 2; i*i <= x; ++i) { if (x % i != 0) continue; long r; while (x % i == 0) { ++r; x /= i; } ans ~= [i, r]; } if (x != 1) ans ~= [x, 1]; return ans; } void main() { auto A = RD; auto B = RD; auto aa = primeFact(A); auto bb = primeFact(B); debug writeln(aa); debug writeln(bb); bool[long] set; foreach (e; aa) { set[e[0]] = true; } long ans = 1; foreach (e; bb) { if (set.get(e[0], false) == true) ++ans; } writeln(ans); stdout.flush(); debug readln(); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}} void main() { dchar[] s; readV(s); s[3] = '8'; writeln(s); }
D
import std.functional, std.algorithm, std.container, std.typetuple, std.typecons, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv, std.format, std.math; void main() { auto ip = readln.split.to!(int[]), a=ip[0], b=ip[1], c=ip[2]; if(a==b) writeln(c); else if(b==c) writeln(a); else writeln(b); }
D
import std.stdio; import std.ascii; import std.conv; import std.string; import std.algorithm; import std.range; import std.functional; import std.math; import core.bitop; import std.numeric; T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void main() { auto s = readln.chomp; auto t = readln.chomp; char[char] dict1; char[char] dict2; foreach (i; 0 .. s.length) { if (s[i] in dict1 && dict1[s[i]] != t[i]) { writeln("No"); return; } else dict1[s[i]] = t[i]; if (t[i] in dict2 && dict2[t[i]] != s[i]) { writeln("No"); return; } else dict2[t[i]] = s[i]; } writeln("Yes"); }
D
import std.stdio, std.string, std.conv, std.range, std.array, std.algorithm; import std.uni, std.math, std.container, std.typecons, std.typetuple; import core.bitop, std.datetime; void main(){ int e, rest, ans; while (true) { e = readln.chomp.to!int; if (!e) break; ans = 1<<30; for(int z = 0; z^^3 <= e; ++z) { rest = e - z^^3; for(int y = 0; y^^2 <= rest; ++y) { int x = rest - y^^2; ans = min(ans, x + y + z); } } writeln(ans); } }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { int n,m; char[] s, t; scan(n,m); s = readln.chomp.to!(char[]); t = readln.chomp.to!(char[]); s ~= '#'; t ~= '#'; s.reverse(); t.reverse(); auto dpi = new int[][](n + 1, m + 1); auto dpo = new int[][](n + 1, m + 1); /+ foreach (i ; 1 .. n + 1) { if (s[i] == 'I') { dpi[i][0] = dpo[i-1][0] + 1; } if (s[i] == 'O') { dpo[i][0] = dpi[i-1][0] + 1; } } foreach (j ; 1 .. m + 1) { if (t[j] == 'I') { dpi[0][j] = dpo[0][j-1] + 1; } if (t[j] == 'O') { dpo[0][j] = dpi[0][j-1] + 1; } } +/ foreach (i ; 0 .. n + 1) { foreach (j ; 0 .. m + 1) { if (s[i] == 'I') { dpi[i][j] = max(dpi[i][j], dpo[i-1][j] + 1); } else if(s[i] == 'O') { dpo[i][j] = max(dpo[i][j], dpi[i-1][j] + 1); } if (t[j] == 'I') { dpi[i][j] = max(dpi[i][j], dpo[i][j-1] + 1); } else if (t[j] == 'O') { dpo[i][j] = max(dpo[i][j], dpi[i][j-1] + 1); } } } debug { writefln("%(%(%s %)\n%)", dpi); writeln; writefln("%(%(%s %)\n%)", dpo); } int ans; foreach (i ; 0 .. n + 1) { foreach (j ; 0 .. m + 1) { if (dpi[i][j] & 1) { ans = max(dpi[i][j], ans); } else { ans = max(dpi[i][j]-1, ans); } } } 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 core.bitop; import std.algorithm; import std.array; import std.container; import std.conv; import std.exception; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void main () { auto a = readln.strip.to !(int); auto b = readln.strip.to !(int); auto c = readln.strip.to !(int); auto d = readln.strip.to !(int); writeln (((a ^ b) & (c | d)) ^ ((b & c) | (a ^ d))); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { auto t = reads!long; long a = t[0], b = t[1], c = t[2], k = t[3]; writeln(k % 2 == 0 ? a - b : b - a); }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; import std.exception; int t; rd(t); while (t--) { int a; rd(a); bool ok = false; for (int n = 3; n <= 360; n++) { if (360 % n == 0) { auto x = 180 - 360 / n; if (a * (n - 2) % x == 0) { if (a * (n - 2) / x <= (n - 2)) { writeln(n); ok = true; break; } } } } enforce(ok); } } /* 3 60 60 4 90 45 5 108 36 6 120 30 7 180-360/7 x/5 8 135 135/6 . . . n 180-360/n:=x x/(n-2) */ 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.string, std.conv, std.math, std.regex; void main() { auto n = readln.chomp; if(n[0] == n[2]){ writeln("Yes"); } else { writeln("No"); } }
D
import std.stdio, std.array, std.conv; void main() { int[] tmp = readln.split.to!(int[]); int k = tmp[0], x = tmp[1]; foreach (i; x-k+1 .. x+k) { i.write; if (i != x + k - 1) " ".write; else writeln; } }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = readln.chomp.to!size_t; Node node; foreach (_; n.iota) { auto rd = readln.chomp.splitter(' '), cmd = rd.front; switch (cmd) { case "insert": rd.popFront; node.insert(rd.front.to!int); break; case "find": rd.popFront; writeln(node.find(rd.front.to!int) ? "yes" : "no"); break; case "print": node.inorderWalk; writeln; node.preorderWalk; writeln; break; default: assert(0); } } } void writeArray(T)(T[] ai) { foreach (a; ai) write(" ", a); writeln; } void insert(ref Node node, int val) { Node par, cur = node; while (cur !is null) { par = cur; if (val < cur.key) cur = cur.l; else cur = cur.r; } Node chi = new Node(val); if (par is null) node = chi; else if (val < par.key) par.l = chi; else par.r = chi; } bool find(Node node, int val) { if (node.key == val) { return true; } else if (node.key > val) { if (node.l is null) return false; else return node.l.find(val); } else { if (node.r is null) return false; else return node.r.find(val); } } void inorderWalk(Node node) { if (node.l !is null) inorderWalk(node.l); write(" ", node.key); if (node.r !is null) inorderWalk(node.r); } void preorderWalk(Node node) { write(" ", node.key); if (node.l !is null) preorderWalk(node.l); if (node.r !is null) preorderWalk(node.r); } class Node { int key; Node l, r; this(int key) { this.key = key; } }
D
import std.stdio, std.string, std.conv, std.algorithm; void main() { while(1){ auto n = readln.chomp.to!int; if(n==0) break; int s=1; int max=50000001; for(int i=2;i<n/2;++i){ if(n%i==0){ if(i<max){ s+=(i+n/i); max = n/i; if(i==max) s -= i; }else break; }else if(i>max) break; } if(s<n || n==1) writeln("deficient number"); else if(s==n) writeln("perfect number"); else writeln("abundant number"); } }
D
import std.stdio, std.algorithm, std.string, std.conv, std.array, std.range, std.math, core.stdc.stdio; int[] readints() { return readln.split.to!(int[]); } long calc(long n, long k, long b) { long cnt = max(0, b - k); long p = n / b; long r = n - p * b; long x = max(0, r - k + 1); return p * cnt + x; } void main() { auto input = readints(); long n = input[0], k = input[1]; long ans; foreach (b; k+1..n+1) ans += calc(n, k, b); writeln(!k ? n * n : ans); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { long a,b,c; scan(a,b,c); if((a < c && c < b) || (a > c && c > b)) { writeln("Yes"); } else { writeln("No"); } }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; import std.datetime; import std.numeric; import std.math; import std.string; string my_readln() { return chomp(readln()); } void main() {//try{ auto tokens = split(my_readln()); auto A = to!ulong(tokens[0]); auto B = to!ulong(tokens[1]); auto C = to!ulong(tokens[2]); ulong ans = min(A + B + 1, C) + B; writeln(ans); stdout.flush(); /*}catch (Throwable e) { writeln(e.toString()); } readln();*/ }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto ps = readln.split.to!(int[]); int c; foreach (i; 1..N-1) { auto x = ps[i-1]; auto y = ps[i]; auto z = ps[i+1]; if (x < y && y < z || z < y && y < x) ++c; } writeln(c); }
D
import std.string, std.stdio, std.conv; void main() { int N = readln.chomp.to!int; int[] input = readln.chomp.split.to!(int[]); int max , min = int.max; foreach (e; input) { if (max < e) { max = e; } if (min > e) { min = e; } } writeln(max - min); }
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.stdlib; void main() { auto K = readln.chomp.to!int; long[] ans = iota(1L, 10L).array; int idx = 0; while (idx < K) { auto L = ans.length.to!int; foreach (i; idx..L) { auto ld = ans[i] % 10; if (ld > 0) ans ~= ans[i] * 10 + ld - 1; ans ~= ans[i] * 10 + ld; if (ld < 9) ans ~= ans[i] * 10 + ld + 1; } idx = L; } ans[K-1].writeln; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] 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, A, B; scan(N, A, B); long ans = MOD + powmod(2, N, MOD) - 1; ans %= MOD; auto f = new long[](B + 1); f[0] = 1; foreach (i; 1 .. B + 1) f[i] = (f[i - 1] * i) % MOD; long xa = 1; foreach (i; (N - A + 1) .. N + 1) xa *= i, xa %= MOD; long xb = 1; foreach (i; (N - B + 1) .. N + 1) xb *= i, xb %= MOD; auto ca = xa * invmod(f[A]) % MOD; auto cb = xb * invmod(f[B]) % MOD; writeln((ans + 2 * MOD - ca - cb) % MOD); } T powmod(T = long)(T x, T n, T m) { if (n < 1) return 1; if (n & 1) { return x * powmod(x, n - 1, m) % m; } T tmp = powmod(x, n / 2, m); return tmp * tmp % m; } T invmod(T = long)(T x, T m = 10 ^^ 9 + 7) { return powmod(x, m - 2, m); }
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; const MOD = 2019; long calc(long L, long R) { const mod = 2019; long ans = int.max; int[int] i_used; for (long i = L; i <= R; i++) { if (i_used[i % mod]++) continue; int[int] j_used; for (long j = i + 1; j <= R; j++) { if (j_used[j % mod]++) continue; ans = min(ans, (i % mod) * (j % mod) % mod); } } return ans; } long calc1(long L, long R) { long ans = int.max; for (long i = L; i <= R; i++) { for (long j = i + 1; j <= R; j++) { long m = i * j % MOD; ans = min(ans, m); if (ans == 0) goto END; } } END: return ans; } void main() { int L, R; scan(L, R); writeln(calc1(L, 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 = "%.15f"; 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 = 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 s = RD!string; long l = 0, r = s.length-1; long ans; while (r-l > 0) { if (s[l] == s[r]) { ++l; --r; } else if (s[l] == 'x') { ++ans; ++l; } else if (s[r] == 'x') { ++ans; --r; } else { ans = -1; break; } } writeln(ans); stdout.flush(); debug readln(); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } //累積和でバーーン!!ってやってみる. void main() { long n, q; scan(n, q); auto s = sread(); long[] com_count = new long[n + 1]; foreach (i, ref e; com_count) { if (i < 2) { continue; } if (s[i - 2 .. i] == "AC") { com_count[i] = com_count[i - 1] + 1; } else { com_count[i] = com_count[i - 1]; } } // writeln(com_count); foreach (i; 0 .. q) { long a,b; scan(a,b); writeln(com_count[b] - com_count[a]); } }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} T[] readArray(T)(size_t n){auto a=new T[](n),r=readln.splitter;foreach(ref v;a){v=r.front.to!T;r.popFront;}return a;} T[] readArrayM(T)(size_t n){auto a=new T[](n);foreach(ref v;a)v=readln.chomp.to!T;return a;} void main() { int a; readV(a); int b; readV(b); int c; readV(c); int d; readV(d); writeln(min(a, b) + min(c, d)); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nmx = readln.split.to!(int[]); auto N = nmx[0]; auto M = nmx[1]; auto X = nmx[2]; int[][] cas; foreach (_; 0..N) { cas ~= readln.split.to!(int[]); } auto ms = new int[](M); long solve(int i) { if (i == N) { foreach (m; ms) if (m < X) { return int.max/3; } return 0; } else { auto r1 = solve(i+1); foreach (j, a; cas[i][1..$]) { ms[j] += a; } auto r2 = solve(i+1) + cas[i][0]; foreach (j, a; cas[i][1..$]) { ms[j] -= a; } return min(r1, r2); } } auto r = solve(0); writeln(r >= int.max/3 ? -1 : r); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto N = RD; auto A = RDA; long ans; foreach (i; 0..N-1) { auto d = A[i] - A[i+1]; if (d > 0) { ans += d; A[i+1] += d; } } writeln(ans); stdout.flush; debug readln; }
D
import std.stdio; import std.array; import std.conv; import std.string; import std.algorithm; void main() { int n = readln.strip.to!int; int[10][3][4] h ; foreach(i;0..4) foreach(j;0..3) foreach(k;0..10) h[i][j][k] = 0; foreach(i;0..n) { auto info = map!(to!int)(readln.strip.split); h[info[0]-1][info[1]-1][info[2]-1] += info[3]; } foreach(i;0..4) { foreach(j;0..3) { foreach(k;0..10) { write(" "); write(h[i][j][k]); } writeln(); } if(i<3) writeln("####################"); } }
D
import std.stdio, std.string, std.conv, std.algorithm; void main(){ auto s = readln.chomp.to!int; writeln(s/3600, ":", s%3600/60, ":", s%3600%60); }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { string a; scan(a); int[] cnt = new int[](26); foreach (ai ; a) { cnt[ai - 'a']++; } long ans = 1L * a.length * (a.length + 1) / 2 - a.length + 1; foreach (i ; 0 .. 26) { ans -= 1L * cnt[i] * (cnt[i] - 1) / 2; } writeln(ans); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
void main(){ long n = _scan(); long[] a; foreach(i; 0..n)a ~= _scan!long(); a.sort!("a>b"); long prev=-1, cnt=1, ans; foreach(i, elm; a){ // 数えている途中 if(prev==elm){ cnt++; } if(prev!=elm && i==a.length-1){ if(cnt&1)ans++; // 最後尾の要素は奇数 ans++; }else if(prev!=elm || i==a.length-1){ if(prev==-1){ prev = elm; }else{ if(cnt&1)ans++; cnt = 1; prev = elm; } } } ans.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
// url: http://tenka1-2017-beginner.contest.atcoder.jp/tasks/tenka1_2017_b import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = readln.chomp.to!size_t; auto maxA = 0, maxB = 0; foreach (_; 0..n) { auto rd = readln.split.to!(int[]), a = rd[0], b = rd[1]; if (a > maxA) { maxA = a; maxB = b; } } writeln(maxA + maxB); }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); } void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); } string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ string s = scan; string ans; foreach(c; s){ if(c == '?') ans ~= 'D'; else ans ~= c; } ans.print; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n, m; rd(n, m); auto c=readln.split.to!(int[]); auto a=readln.split.to!(int[]); int cnt=0; for(int i=0, j=0; i<n && j<m; ){ if(c[i]<=a[j]){ cnt++; i++; j++; }else{ i++; } } writeln(cnt); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x) e=l[i].to!(typeof(e)); }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons, std.numeric; void main() { const N = readln.chomp.to!long; const as = readln.split.to!(long[]); as.map!(a => a-1).sum.writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string; void main() { auto ks = readln.split.to!(int[]); auto k = ks[0]; auto s = ks[1]; long cnt; foreach (x; 0..k+1) { foreach (y; x..k+1) { auto z = s - x - y; if (z < y || z > k) continue; if (x == y && y == z) ++cnt; else if (x == y || y == z || z == x) cnt += 3; else cnt += 6; } } writeln(cnt); }
D
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; import std.datetime, std.bigint; immutable inf = 10L^^16; int n; int[] a; void main() { scan(n); a = readln.split.to!(int[]); auto p = new long[](n + 1); auto s = new long[](n + 1); iota(n).each!(i => p[i + 1] = p[i] + a[i]); iota(n).retro().each!(i => s[i] = s[i + 1] + a[i]); debug { writeln(p); writeln(s); } long ans = inf; foreach (i ; 1 .. n) { ans = min(ans, abs(p[i] - s[i])); } writeln(ans); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
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 c = new string[](3); foreach (i; 0..3) { c[i] = RD!string; } writeln([c[0][0], c[1][1], c[2][2]]); stdout.flush(); debug readln(); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp.to!int; int[string] blue; foreach (_; 0..n) { blue[readln.chomp]++; } auto m = readln.chomp.to!int; int[string] red; foreach (_; 0..m) { red[readln.chomp]++; } int res; foreach (k; blue.keys) { res = max(blue[k] - red.get(k, 0), res); } writeln(max(res, 0)); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { readln.chomp.count('2').writeln; }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { int n, m; scan(n, m); auto uf = UnionFind(n); foreach (i ; 0 .. m) { int ai, bi; scan(ai, bi); ai--, bi--; uf.unite(ai, bi); } long a = uf.count_node(0), b = uf.count_node(1); a = min(a, b); long ans = a * (a - 1) / 2 + (n - a) * (n - a - 1) / 2 - m; writeln(ans); } struct UnionFind { private { int N; int[] p; int[] rank; int[] num_v; } this (int n) { N = n; p = iota(N).array; rank = new int[](N); num_v = new int[](N); foreach (i ; 0 .. N) { num_v[i] = 1; } } int find_root(int x) { if (p[x] != x) { p[x] = find_root(p[x]); } return p[x]; } bool same(int x, int y) { return find_root(x) == find_root(y); } void unite(int x, int y) { int u = find_root(x), v = find_root(y); if (u == v) return; if (rank[u] < rank[v]) { p[u] = v; num_v[v] += num_v[u]; num_v[u] = 0; } else { p[v] = u; num_v[u] += num_v[v]; num_v[v] = 0; if (rank[u] == rank[v]) { rank[u]++; } } } int count_node(int x) { return num_v[find_root(x)]; } } 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 core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "a", long, "b"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long n, m; scan(n, m); auto ball = new long[](n); ball[] = 1; auto red = new long[](n); red[0] = 1; foreach (_; iota(m)) { long x, y; scan(x, y); x--, y--; ball[x]--, ball[y]++; if (red[x]) red[y] = 1; if (!ball[x]) red[x] = 0; } red.sum().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.algorithm, std.conv, std.range, std.math; void main() { readln.toUpper.write; }
D
import std.stdio; void main(){ foreach(i;0..1000) writeln("Hello World"); }
D
import std.stdio, std.string, std.conv, std.math; void main() { auto ip = readln.split.to!(int[]); if((ip[1] * 10 + ip[2]) % 4 == 0){ writeln("YES"); } else { writeln("NO"); } }
D
import std.stdio; import std.algorithm; import std.math; import std.conv; import std.string; T readNum(T)(){ return readStr.to!T; } T[] readNums(T)(){ return readStr.split.to!(T[]); } string readStr(){ return readln.chomp; } void main(){ auto n = readNum!int; auto a = readNums!int; auto b = readNums!int; auto c = readNums!int; int prev, ret; foreach(i; 0 .. n){ ret += b[a[i]-1]; if(i != 0 && a[i] == prev+1) ret += c[a[i]-2]; prev = a[i]; } writeln(ret); }
D
import std.algorithm; import std.array; import std.ascii; import std.bigint; import std.complex; import std.container; import std.conv; import std.functional; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } void readlnTo(T...)(ref T t) { auto s = readln().split(); assert(s.length == t.length); foreach(ref ti; t) { ti = s[0].to!(typeof(ti)); s = s[1..$]; } } const real eps = 1e-10; void main(){ auto s = readln().chomp().to!(char[]); auto k = readLong(); int next(int n) { while(n < s.length && s[n] == 'a') { ++n; } return n; } int i = next(0); while(k >= 26) { if(i < s.length) { k -= 'z'-s[i] + 1; s[i] = 'a'; i = next(i); } else { k %= 26; } } while(k > 0) { if(i < s.length) { if(k >= 'z'-s[i]+1) { k -= 'z'-s[i]+1; s[i] = 'a'; } i = next(i+1); } else { s[$-1] += k; k = 0; } } writeln(s.to!string); }
D
void main() { readln.chomp.count('1').writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.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]; char[][] MAP; MAP.length = H; foreach (i; 0..H) { MAP[i] = readln.chomp.to!(char[]); } int r; foreach (h; 0..H) { foreach (w; 0..W) { if (MAP[h][w] == '#') continue; auto memo = new bool[][](H, W); int t; int[2][] ss = [[h, w]]; memo[h][w] = true; for (;;) { int[2][] nss; foreach (s; ss) { foreach (d; [[1,0], [0,1], [-1,0], [0,-1]]) { auto y = s[0] + d[0]; auto x = s[1] + d[1]; if (y < 0 || y >= H || x < 0 || x >= W || memo[y][x] || MAP[y][x] == '#') continue; memo[y][x] = true; nss ~= [y, x]; } } if (nss.empty) break; ++t; ss = nss; } r = max(r, t); } } writeln(r); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; } enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { int N, M; scan(N, M); auto e = new bool[][](N, N); foreach (i ; 0 .. M) { int ai, bi; scan(ai, bi); ai--, bi--; e[ai][bi] = e[bi][ai] = true; } auto p = iota(N).array; int ans; do { ans += f(N, p, e); } while (nextPermutation(p)); writeln(ans); } int f(int N, int[] p, bool[][] e) { if (p.front != 0) { return false; } foreach (i ; 0 .. N - 1) { if (!e[p[i]][p[i + 1]]) { return false; } } return true; }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; void main() { auto ab = readln.split.map!(to!int); while (1) { bool f; foreach (y; ab[0]..ab[1]+1) { if (isLeapYear(y)) { y.writeln; f = 1; } } if (!f) writeln("NA"); ab = readln.split.map!(to!int); if (ab[0] + ab[1] == 0) break; writeln(""); } } bool isLeapYear(int year) { if (year % 4) return 0; if (year % 400 == 0) return 1; if (year % 100 == 0) return 0; return 1; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { const long MAX = 3501; auto N = readln.chomp.to!long; foreach (b; 1..MAX) { foreach (c; 1..MAX) { long bunbo = 4 * b * c - b * N - c * N; long bunsi = b * c * N; if (bunbo <= 0 || bunsi % bunbo != 0) continue; long a = bunsi / bunbo; writeln(a, " ", b, " ", c); return; } } }
D
import std.stdio; import std.string; import std.conv; int main() { string[] str = readln().split(); int a = to!int(str[0]); int b = to!int(str[1]); if (a > b) writeln("a > b"); else if (a < b) writeln("a < b"); else writeln("a == b"); return 0; }
D
import std.stdio; import std.algorithm; import std.string; import std.range; import std.array; import std.conv; import std.complex; import std.math; import std.ascii; import std.bigint; import std.container; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } const real eps = 1e-10; void main(){ while(true) { auto hw = readInts(); auto h = hw[0]; auto w = hw[1]; if(h == 0 && w == 0) { return; } auto hm = 150; auto wm = 150; auto less = (int w1, int h1, int w2, int h2) { return w1*w1 + h1*h1 < w2*w2 + h2*h2 || (w1*w1 + h1*h1 == w2*w2 + h2*h2 && h1 < h2); }; for(int i = 1; i < 150; ++i) { for(int j = i+1; j < 150; ++j) { auto d2 = h*h + w*w; if(less(w, h, j, i) && less(j, i, wm, hm)) { hm = i; wm = j; } } } writeln(hm, " ", wm); } }
D
import std.conv, std.stdio; import std.algorithm, std.array, std.range, std.string; void main() { auto a = readln[0]; if ('a' <= a && a <= 'z') return 'a'.writeln; 'A'.writeln; }
D
string solve(int x){ return (x<1200)? "ABC": "ARC"; } void main(){ int x = inelm(); solve(x).writeln(); } 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; import std.array; import std.conv; import std.range; import std.algorithm; import std.string; import std.numeric; void solve(){ string s = readln().strip(); foreach(a;0..26){ if(gcd(a,26)!=1) continue; foreach(b;0..26){ char[30] m; foreach(i;0..26){ m[(i*a+b)%26] = cast(char)(i+'a'); } auto ns = s.map!(c=>(c!=' ')?m[c-'a']:' ').array(); if(ns.split().any!(w=>w=="that"||w=="this")){ writeln(ns); return; } } } } void main() { int n=to!int(readln().strip()); foreach(_;0..n)solve(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.typecons; import std.numeric, std.math; 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 sx = RD; auto sy = RD; auto tx = RD; auto ty = RD; auto dx = tx - sx; auto dy = ty - sy; foreach (i; 0..dy) { write("U"); } foreach (i; 0..dx) { write("R"); } write("U"); foreach (i; 0..dx+1) { write("L"); } foreach (i; 0..dy+1) { write("D"); } write("R"); foreach (i; 0..dx) { write("R"); } foreach (i; 0..dy) { write("U"); } write("R"); foreach (i; 0..dy+1) { write("D"); } foreach (i; 0..dx+1) { write("L"); } write("U"); writeln(); stdout.flush(); //readln(); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { long N, K; scan(N, K); auto v = abs(N - K) % K; auto ans = min(v, K - v); writeln(ans); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.stdio, std.string, std.conv; void main() { string N = readln.chomp; int a, b; foreach (c; N) { a += c - '0'; } b = N[0] - 1 - '0'; foreach (c; N[1..N.length]) { b += '9' - '0'; } writeln(a > b ? a : b); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp.to!int; auto a = readln.chomp.split.to!(int[]); long[int]cnt; foreach (e; a) { cnt[e]++; } long[int] res; long base; foreach (k; cnt.keys) { if (cnt[k] == 1) { res[k] = 0; } else { res[k] = (cnt[k] * (cnt[k] - 1)) / 2; base += res[k]; } } foreach (i; 0..n) { auto sum = base - res[a[i]]; if (cnt[a[i]] > 2) { sum += ((cnt[a[i]]-1) * (cnt[a[i]] - 2)) / 2; } sum.writeln; } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(string s) { int rec(int head) { int change = 0; for (int i = 0; i < s.length; i += 2) { if (s[i] != (head == 0 ? '0' : '1')) change++; if (i+1 < s.length && s[i+1] != (head == 0 ? '1' : '0')) change++; } return change; } return min(rec(0), rec(1)); } void main() { string s = read!string; writeln(calc(s)); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nkq = readln.split.to!(long[]); auto N = nkq[0]; auto K = nkq[1]; auto Q = nkq[2]; int[] as; as.length = N; foreach (_; 0..Q) { ++as[readln.chomp.to!int - 1]; } foreach (a; as) { writeln(K - (Q - a) > 0 ? "Yes" : "No"); } }
D
void main() { problem(); } void problem() { auto N = scan!ulong + 1; ulong solve() { ulong[] divisers = new ulong[N]; divisers[] = 1; foreach(i; 2..N) { for(ulong x = i; x < N; x += i) divisers[x]++; } ulong ans; foreach(i; 1..N) ans += i * divisers[i]; return ans; } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Queue = Tuple!(long, "from", long, "to"); // -----------------------------------------------
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n; scan(n); auto a = iota(n).map!(i => readln.chomp.to!int).array; int top, sec; foreach (i ; 0 .. n) { if (a[i] > top) { sec = top; top = a[i]; } else if (a[i] > sec) { sec = a[i]; } } foreach (i ; 0 .. n) { if (a[i] < top) { writeln(top); } else { writeln(sec); } } } int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new int[][](n, 0); foreach (i; 0 .. m) { int u, v; scan(u, v); if (is1indexed) { u--, v--; } adj[u] ~= v; if (isUndirected) { adj[v] ~= u; } } return adj; } alias Edge = Tuple!(int, "to", int, "cost"); Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new Edge[][](n, 0); foreach (i; 0 .. m) { int u, v, c; scan(u, v, c); if (is1indexed) { u--, v--; } adj[u] ~= Edge(v, c); if (isUndirected) { adj[v] ~= Edge(u, c); } } return adj; } void yes(bool b) { writeln(b ? "Yes" : "No"); } void YES(bool b) { writeln(b ? "YES" : "NO"); } T[] readArr(T)() { return readln.split.to!(T[]); } T[] readArrByLines(T)(int n) { return iota(n).map!(i => readln.chomp.to!T).array; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp.to!int; int[int] cnt; foreach (i; 0..n) { auto d = readln.chomp.to!int; cnt[d]++; } cnt.keys.length.writeln; }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; import std.range; import std.math; void main(){ auto A=readln.split.to!(int[]),x=A[0],a=A[1],b=A[2]; if(a>=b)writeln("delicious"); else if(a<b&&b-a<=x)writeln("safe"); else if(a<b&&b-a>x)writeln("dangerous"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; enum P = 998244353L; long[(10^^5)*2+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; } long inv(long x) { return pow(x, P-2); } void init() { F[0] = F[1] = 1; foreach (i, ref x; F[2..$]) x = (F[i+1] * (i+2)) % P; { RF[$-1] = 1; auto x = F[$-1]; auto k = P-2; while (k) { if (k%2 == 1) RF[$-1] = (RF[$-1] * x) % P; x = x^^2 % P; k /= 2; } } foreach_reverse(i, ref x; RF[0..$-1]) x = (RF[i+1] * (i+1)) % P; } long comb(N)(N n, N k) { if (k > n) return 0; auto n_b = F[n]; // n! auto nk_b = RF[n-k]; // 1 / (n-k)! auto k_b = RF[k]; // 1 / k! auto nk_b_k_b = (nk_b * k_b) % P; // 1 / (n-k)!k! return (n_b * nk_b_k_b) % P; // n! / (n-k)!k! } void main() { init(); auto nmk = readln.split.to!(long[]); auto N = nmk[0]; auto M = nmk[1]; auto K = nmk[2]; long r; foreach (k; N-1-K..N) { long x = comb(N-1, k); (x *= M) %= P; (x *= pow(M-1, k)) %= P; r = (r + x + P) % P; } writeln(r); }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.7.4" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; // import dcomp.geo.primitive; void solve() { alias P = Point2D!double; P [4] p; foreach (i; 0..4) { double x, y; sc.read(x, y); p[i] = P(x, y); } if (!sgn(cross(p[0]-p[1], p[2]-p[3]))) { writeln("YES"); } else { writeln("NO"); } } int main() { EPS!double = 1e-10; int n; sc.read(n); foreach (i; 0..n) solve(); return 0; } Scanner sc; static this() { sc = new Scanner(stdin); } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; // import dcomp.array; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { FastAppender!(E[]) buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/array.d */ // module dcomp.array; T[N] fixed(T, size_t N)(T[N] a) {return a;} struct FastAppender(A, size_t MIN = 4) { import std.algorithm : max; import std.conv; import std.range.primitives : ElementEncodingType; import core.stdc.string : memcpy; private alias T = ElementEncodingType!A; private T* _data; private uint len, cap; @property size_t length() const {return len;} bool empty() const { return len == 0; } void reserve(size_t nlen) { import core.memory : GC; if (nlen <= cap) return; void* nx = GC.malloc(nlen * T.sizeof); cap = nlen.to!uint; if (len) memcpy(nx, _data, len * T.sizeof); _data = cast(T*)(nx); } void free() { import core.memory : GC; GC.free(_data); } void opOpAssign(string op : "~")(T item) { if (len == cap) { reserve(max(MIN, cap*2)); } _data[len++] = item; } void insertBack(T item) { this ~= item; } void removeBack() { len--; } void clear() { len = 0; } ref inout(T) back() inout { assert(len); return _data[len-1]; } ref inout(T) opIndex(size_t i) inout { return _data[i]; } T[] data() { return (_data) ? _data[0..len] : null; } } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } import core.bitop : popcnt; static if (!__traits(compiles, popcnt(ulong.max))) { public import core.bitop : popcnt; int popcnt(ulong v) { return popcnt(cast(uint)(v)) + popcnt(cast(uint)(v>>32)); } } bool poppar(ulong v) { v^=v>>1; v^=v>>2; v&=0x1111111111111111UL; v*=0x1111111111111111UL; return ((v>>60) & 1) != 0; } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/geo/primitive.d */ // module dcomp.geo.primitive; import std.traits; template EPS(R) { R EPS; } int sgn(R)(R a) { if (a < -EPS!R) return -1; if (a > EPS!R) return 1; return 0; } struct Point2D(T) { T[2] d; this(T x, T y) {this.d = [x, y];} this(T[2] d) {this.d = d;} @property ref inout(T) x() inout {return d[0];} @property ref inout(T) y() inout {return d[1];} ref inout(T) opIndex(size_t i) inout {return d[i];} auto opBinary(string op:"+")(Point2D r) const {return Point2D(x+r.x, y+r.y);} auto opBinary(string op:"-")(Point2D r) const {return Point2D(x-r.x, y-r.y);} static if (isFloatingPoint!T) { T abs() { import std.math : sqrt; return (x*x+y*y).sqrt; } T arg() { import std.math : atan2; return atan2(y, x); } } } bool near(T)(Point2D!T a, Point2D!T b) { return !sgn(abs(a-b)); } T dot(T)(in Point2D!T l, in Point2D!T r) { return l[0]*l[0] + l[1]*r[1]; } T cross(T)(in Point2D!T l, in Point2D!T r) { return l[0]*r[1] - l[1]*r[0]; } int argcmp(T)(Point2D!T l, Point2D!T r) if (isIntegral!T) { int sgn(Point2D!T p) { if (p[1] < 0) return -1; if (p[1] > 0) return 1; if (p[0] < 0) return 2; return 0; } int lsgn = sgn(l); int rsgn = sgn(r); if (lsgn < rsgn) return -1; if (lsgn > rsgn) return 1; T x = cross(l, r); if (x > 0) return -1; if (x < 0) return 1; return 0; } /* This source code generated by dcomp and include dcomp's source code. dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp) dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt) */
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto N = RD; long[string] cnt; cnt["AC"] = 0; cnt["WA"] = 0; cnt["TLE"] = 0; cnt["RE"] = 0; foreach (i; 0..N) { ++cnt[RD!string]; } writeln("AC x ", cnt["AC"]); writeln("WA x ", cnt["WA"]); writeln("TLE x ", cnt["TLE"]); writeln("RE x ", cnt["RE"]); stdout.flush; debug readln; }
D
import std.stdio, std.string, std.conv, std.algorithm; void main() { int n = readln.chomp.to!int; int[] r = new int[n]; foreach (i; 0 .. n) { r[i] = readln.chomp.to!int; } int maxv = int.min; int minv = r[0]; foreach (i; 1 .. n) { maxv = max(maxv, r[i] - minv); minv = min(minv, r[i]); } maxv.writeln; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; auto s=readln.chomp.to!(char[]); auto cnt=new int[](3); foreach(c; s) cnt[c-'a']++; auto mn=reduce!(min)(cnt); auto cnt1=map!(e=>e-mn)(cnt); auto a=new int[](0); foreach(e; cnt1)if(e>0) a~=e; if(a.length==0){writeln("YES"); return;} if(a.length==1){ if(a[0]<2){writeln("YES"); return;} else{writeln("NO"); return;} } if(a[0]<2 && a[1]<2){writeln("YES"); return;} else{writeln("NO"); return;} } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.stdio; import std.algorithm; import std.string; import std.conv; import std.array; void main() { while (true) { auto N = readln.chomp; if (N == "0") break; auto k = readln.split.map!(to!int).array; if (reduce!((a,b)=> a > b ? a : b)(k) < 2UL) { writeln("NA"); continue; } auto count = filter!((a)=> a != 0)(k).array.length; writeln(count + 1); } }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop; // dfmt on void main() { long N = lread(); auto A = aryread(); long s = A.sum(); long t; long ans = long.max; foreach (i; 0 .. N - 1) { t += A[i]; ans = ans.min(abs((s - t) - t)); } writeln(ans); }
D
import std.stdio, std.conv, std.string; import std.algorithm, std.array, std.container; import std.numeric, std.math; import core.bitop; string my_readln() { return chomp(readln()); } long mod = pow(10, 9) + 7; long moda(long x, long y) { return (x + y) % mod; } long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; } long modm(long x, long y) { return (x * y) % mod; } void main() { auto tokens = my_readln.split; auto N = tokens[0].to!uint; auto a = new ulong[][](N); foreach (i; 0..N) { foreach (j, token; my_readln.split) { auto x = token.to!(ulong); if (x == 0) continue; a[i] ~= x << j; } } auto dp = new long[](1<<N); dp[0] = 1; foreach (j; 1..1<<N) { auto i = popcnt(j) - 1; foreach (e; a[i]) { if ((j & e) == 0) continue; auto x = j | e; dp[j] = moda(dp[j], dp[j^e]); } } //stderr.writeln(dp[i+1]); writeln(dp[(1<<N)-1]); stdout.flush(); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; string Sorting_String(string s) { char[] str; for (int i = 0; i < s.length; i++) str ~= s[i]; for (int i = 0; i < str.length; i++) for (int j = i + 1; j < str.length; j++) { if (str[i] > str[j]) { auto tmp = str[i]; str[i] = str[j]; str[j] = tmp; } } return to!string(str); } int Change_To_Int(string s) { int res = 0; for (int i = 1; i < s.length; i++) res += ((to!int(s[i]) - 48) * ((10 ^^ i) / 10)); return res; } int Reverse_To_Int(string s) { char[] str; str ~= " "; for (int i = 1; i < s.length; i++) str ~= s[s.length - i]; int res = Change_To_Int(to!string(str)); return res; } int main() { int N; N = readln().chomp().to!int(); for (int i = 0; i < N; i++) { string s = readln(); auto str = Sorting_String(s); int num1 = Change_To_Int(str); int num2 = Reverse_To_Int(str); int ans = num1 - num2; writeln(ans); } return 0; }
D
import std.stdio, std.string, std.conv, std.array, std.algorithm, std.range; void main() { auto s = readln.chomp; string ret; foreach (c; s) { switch (c) { case 'B': if (!ret.empty) ret = ret[0..$-1]; break; default: ret ~= c; } } writeln(ret); }
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,k; scan(n,k); writeln(n - k + 1); }
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; void main() { int n; scan(n); auto h = readln.split.to!(int[]); int a = 0, b = abs(h[1] - h[0]); foreach (i ; 2 .. n) { int t = b; b = min(b + abs(h[i] - h[i-1]), a + abs(h[i] - h[i-2])); a = t; } writeln(b); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.conv, std.string; import std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } int DEBUG_LEVEL = 0; void print()(){ writeln(""); } void print(T, A ...)(T t, lazy A a){ write(t), print(a); } void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); } void main(string[] args){ if(args.length > 1 && args[1] == "-debug"){ if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int; else DEBUG_LEVEL = 1; } long r = read.to!long; long d = read.to!long; long x = read.to!long; foreach(i; 0 .. 10){ x = r * x - d; x.writeln; } }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } enum mod = 1_000_000_007L; struct Mint { long x; this(long a) { x = a % mod; if (x < 0) x += mod; } this(Mint a) { x = a.x; } ref Mint opAssign(long a) { this = Mint(a); return this; } ref Mint opOpAssign(string op)(Mint rhs) { static if (op == "+") { x += rhs.x; if (x >= mod) x -= mod; } static if (op == "-") { x -= rhs.x; if (x < 0) x += mod; } static if (op == "*") { (x *= rhs.x) %= mod; } static if (op == "/") { this *= rhs.inv(); } return this; } ref Mint opOpAssign(string op)(long rhs) { static if (op == "^^") { this = powmod(this, rhs); return this; } else { return mixin("this " ~ op ~ "= Mint(rhs)"); } } const Mint powmod(Mint a, long b) { Mint res = 1, p = a; while (b > 0) { if (b & 1) res *= p; p *= p; b /= 2; } return res; } const Mint inv() { return powmod(this, mod - 2); } Mint opUnary(string op)() if (s == "-") { return Mint(-x); } Mint opBinary(string op, T)(const T rhs) { return mixin("Mint(this) " ~ op ~ "= rhs"); } Mint opBinaryRight(string op)(const long rhs) { return mixin("Mint(rhs) " ~ op ~ "= this"); } bool opEquals(Mint a, Mint b) { return a.x == b.x; } bool opEquals(long rhs) { long y = rhs % mod; if (y < 0) y += mod; return x == y; } string toString() { import std.conv : to; return x.to!string; } } unittest { long powmod(long a, long b) { return b > 0 ? powmod(a, b / 2)^^2 % mod * a^^(b & 1) % mod : 1L; } auto a = Mint(2), b = Mint(3); assert(a + b == 5); assert(a + 5 == 7); assert(1 + b == 4); assert(a - b == mod - 1); assert(a * b == 6); assert(a / b == 2 * powmod(3, mod - 2)); assert(a^^10 == 1024); Mint z; assert(z == 0); (z += 2) *= 3; assert(z == 6); } enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { long a, b, x; scan(a, b, x); auto ans = f(b, x) - f(a - 1, x); writeln(ans); } long f(long a, long x) { if (a < 0) { return 0; } return a / x + 1; }
D
void main(){ long n = _scan!long(); long[] stat = [0, 0, 0]; bool legitimate_travel = true; foreach(i; 0..n){ long[] tmp_stat = _scanln!long(); long tdif = tmp_stat[0] - stat[0]; // 所要時間 long xdif = abs( tmp_stat[1] - stat[1] ); // x方向移動距離 long ydif = abs( tmp_stat[2] - stat[2] ); // y方向移動距離 // 移動可能判定 if( xdif+ydif <= tdif ){ // tdifで移動できる範囲である if( tdif%2 == (xdif+ydif)%2 ){ // かつ, 余らず移動できるか stat = tmp_stat; // 移動できれば位置を変更 }else legitimate_travel = false; }else legitimate_travel = false; if(legitimate_travel == false)break; } writeln(legitimate_travel?"Yes":"No"); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.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() { long k; readV(k); auto n = 50, a = new long[](n); a[] = n-1+(k/n); foreach (i; 0..k%n) foreach (j; 0..n) a[j] += i == j ? n : -1; writeln(n); foreach (i; 0..n) { write(a[i]); if (i < n-1) write(" "); } writeln; }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { const N = readln.chomp.to!long; const as = readln.split.to!(long[]); long count2(long x) { if (x % 2 == 1) return 0; else return 1 + count2(x/2); } writeln(as.map!(a => count2(a)).sum); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; import std.math; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(i;0..n){t[i]=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t)v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}} void main() { int n; readV(n); int[] a; readA(n, a); a = 0 ~ a ~ 0; auto s = 0; foreach (i; 0..n+1) s += (a[i+1]-a[i]).abs; foreach (i; 0..n) writeln(s-(a[i+1]-a[i]).abs-(a[i+2]-a[i+1]).abs+(a[i+2]-a[i]).abs); }
D
void main() { long n = rdElem; long[] p = rdRow; long[] q = rdRow; long num = 1; foreach (i; 2 .. n) { num *= i; } bool[] used = new bool[n+1]; long a; long anum = num; foreach (i, x; p) { long cnt; foreach (j; 1 .. x) { if (used[j]) ++cnt; } a += anum * (x - cnt); used[x] = true; if (i != n - 1) anum /= (n - i - 1); } used[] = false; long b; long bnum = num; foreach (i, x; q) { long cnt; foreach (j; 1 .. x) { if (used[j]) ++cnt; } b += bnum * (x - cnt); used[x] = true; if (i != n - 1) bnum /= (n - i - 1); } abs(a-b).writeln; } T rdElem(T = long)() { //import std.stdio : readln; //import std.string : chomp; //import std.conv : to; return readln.chomp.to!T; } alias rdStr = rdElem!string; dchar[] rdDchar() { //import std.conv : to; return rdStr.to!(dchar[]); } T[] rdRow(T = long)() { //import std.stdio : readln; //import std.array : split; //import std.conv : to; return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdRow!T).array; } void wrMat(T = long)(T[][] mat) { //import std.stdio : write, writeln; 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.container; import std.typecons; import std.ascii; import std.uni;
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(int[] hs) { hs.reverse(); int ans = 0; int x = 0; for (int i = 1; i < hs.length; i++) { if (hs[i-1] <= hs[i]) { x++; ans = max(ans, x); } else x = 0; } return ans; } void main() { readint; auto hs = readints; writeln(calc(hs)); }
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; auto X = data[0].to!int, Y = data[1].to!int; writeln(X + Y/2); }
D
import std.stdio, std.string, std.array, std.conv; struct Dice6 { int[string] dice6; int[] _dice6 = new int[](6); this(int[] _dice6) { dice6["top"] = _dice6[0]; dice6["front"] = _dice6[1]; dice6["right"] = _dice6[2]; dice6["left"] = _dice6[3]; dice6["back"] = _dice6[4]; dice6["bottom"] = _dice6[5]; } void roll(char c) { int tmp = dice6["top"]; if (c == 'E') { dice6["top"] = dice6["left"]; dice6["left"] = dice6["bottom"]; dice6["bottom"] = dice6["right"]; dice6["right"] = tmp; } else if (c == 'N') { dice6["top"] = dice6["front"]; dice6["front"] = dice6["bottom"]; dice6["bottom"] = dice6["back"]; dice6["back"] = tmp; } else if (c == 'S') { dice6["top"] = dice6["back"]; dice6["back"] = dice6["bottom"]; dice6["bottom"] = dice6["front"]; dice6["front"] = tmp; } else { dice6["top"] = dice6["right"]; dice6["right"] = dice6["bottom"]; dice6["bottom"] = dice6["left"]; dice6["left"] = tmp; } } int surface(string dir) { int result; if (dir == "top") { result = dice6["top"]; } else if (dir == "front") { result = dice6["front"]; } else if (dir == "right") { result = dice6["right"]; } else if (dir == "left") { result = dice6["left"]; } else if (dir == "back") { result = dice6["back"]; } else { result = dice6["bottom"]; } return result; } } void main() { int[] _dice = readln.chomp.split.to!(int[]); Dice6 dice = Dice6(_dice); string str = readln.chomp; foreach (x; str) { dice.roll(x); } dice.surface("top").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 = "%.15f"; 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 = 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 S = RD!string; bool ans = true; foreach (i, c; S) { if (i % 2 == 0) { if (c == 'L') { ans = false; break; } } else { if (c == 'R') { ans = false; break; } } } writeln(ans ? "Yes" : "No"); stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T read(T)(){ return read.to!T; } T[] read(T)(long n){ return n.iota.map!(_ => read!T).array; } T[][] read(T)(long m, long n){ return m.iota.map!(_ => read!T(n)).array; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ long n = read.to!long; bool[long] set; int ans; while(n > 0 && n !in set){ ans += 1; set[n] = 1; n += 1; while(n % 10 == 0 && n > 0) n /= 10; } ans.writeln; }
D
/+ dub.sdl: name "D" 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); string s; sc.read(s); int n = s.length.to!int; int i = 0; int mid = n/2; while (true) { if (n/2 + i >= n) break; char l = s[(n-1)/2 - i]; char r = s[n/2 + i]; if (l != s[mid] || r != s[mid]) break; i++; } writeln(n/2 + i); return 0; } /* IMPORT /Users/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 read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /Users/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 /Users/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); } } } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std.stdio, std.string, std.array, std.conv, std.algorithm.iteration, std.functional; void main() { int tol(string n) { auto r = n.to!int; if (r == 1) r = 14; return r; } auto ab = readln.split; auto a = tol(ab[0]); auto b = tol(ab[1]); writeln( a > b ? "Alice" : a < b ? "Bob" : "Draw" ); }
D
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.typecons, std.range, std.container, std.math, std.algorithm.searching, std.functional,std.mathspecial; void main(){ auto ab=readln.split.map!(to!int).array; writeln((ab[0]+ab[1])%24); }
D
import std.stdio; import std.string; import std.conv; void main() { auto input = split( readln() ); long N = to!long(input[0]), M = to!long(input[1]); if (N >= 2 && M >= 2) { writeln( (N-2) * (M-2) ); } else if ( N == 1 && M >= 2 ) { writeln( M-2 ); } else if ( M == 1 && N >= 2 ) { writeln( N-2 ); } else if ( N == 1 && M == 1 ) { writeln(1); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; bool[101] L; void main() { auto nmx = readln.split.to!(int[]); auto N = nmx[0]; auto M = nmx[1]; auto X = nmx[2]; foreach (a; readln.split.to!(int[])) L[a] = true; int a, b; foreach (i; 0..N+1) { if (L[i]) ++a; if (i == X) { b = a; a = 0; } } writeln(min(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.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() { bool[long] set; set[RD] = true; set[RD] = true; set[RD] = true; writeln(set.keys.length); stdout.flush(); debug readln(); }
D