code
stringlengths
4
1.01M
language
stringclasses
2 values
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 S = readln.chomp; auto T = readln.chomp; int s, t, c, max_c, d, max_d; foreach (i; 0..N) { auto s1 = S[i] == '1'; auto t1 = T[i] == '1'; if (s1) ++s; if (t1) ++t; if (s1 && !t1) { ++c; if (d > 0) --d; } else if (!s1 && t1) { ++d; if (c > 0) --c; } max_c = max(max_c, c); max_d = max(max_d, d); } if (s != t) { writeln(-1); } else { writeln(max(max_c, max_d)); } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.conv; import std.typecons; void main() { alias Point = Tuple!(int, "x", int, "y"); auto ip = readAs!(int[]), W = ip[0], H = ip[1], N = ip[2]; // 下はダメコード /*auto b = new bool[][](H, W); b[][] = true; b.writeln; foreach(_; 0..N) { auto ip2 = readAs!(int[]), x = ip2[0], y = ip2[1], a = ip2[2]; switch(a) { case 1: b[][0..x] = false; break; case 2: b[][x+1..$] = false; break; case 3: b[0..y][] = false; break; case 4: b[y+1..$][] = false; break; default: break; } } b.reduce!(a => a.count!true).sum.writeln;*/ auto p_s = Point(0, 0), p_g = Point(W, H); foreach(_; 0..N) { auto ip2 = readAs!(int[]), x = ip2[0], y = ip2[1], a = ip2[2]; switch(a) { case 1: p_s.x = max(p_s.x, x); break; case 2: p_g.x = min(p_g.x, x); break; case 3: p_s.y = max(p_s.y, y); break; case 4: p_g.y = min(p_g.y, y); break; default: break; } } //"(%d, %d), (%d, %d)".writefln(p_s.x, p_s.y, p_g.x, p_g.y); //max((p_g.x - p_s.x) * (p_g.y - p_s.y), 0).writeln; auto width = max(p_g.x - p_s.x, 0); auto height = max(p_g.y - p_s.y, 0); (width * height).writeln; } // =================================== 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 (isBasicType!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } int ri() { return readAs!int; } string rs() { return readln.chomp; }
D
void main() { auto N = ri; auto K = ri; ulong res = 1; foreach(i; 0..N) res = (res * 2 < res + K ? res * 2 : res + K); writeln(res); } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip, std.regex; void main() { immutable int INF = 1 << 29; auto N = readln.chomp.to!int; auto A = new int[](2*N); auto C = new bool[](2*N); foreach (i; 0..2*N) { auto s = readln.split; A[i] = s[1].to!int; C[i] = s[0] == "B"; } auto BR = new int[](N+1); auto WR = new int[](N+1); foreach (i; 0..2*N) { if (C[i]) BR[A[i]] = i; else WR[A[i]] = i; } auto dpB = new int[][](N+1, 2*N+1); //座標iまでで、jより大きいBの数 auto dpW = new int[][](N+1, 2*N+1); //座標iまでで、jより大きいWの数 foreach (i; 0..N+1) foreach (j; 0..2*N) dpB[i][j+1] = dpB[i][j] + (C[j] && A[j] > i); foreach (i; 0..N+1) foreach (j; 0..2*N) dpW[i][j+1] = dpW[i][j] + (!C[j] && A[j] > i); auto dp = new int[][](N+1, N+1); foreach (i; 0..N+1) dp[i].fill(INF); dp[0][0] = 0; foreach (n; 1..2*N+1) { foreach (b; 0..n+1) { int w = n - b; if (b > N || w > N) continue; if (b > 0) { dp[b][w] = min(dp[b][w], dp[b-1][w] + dpB[b][BR[b]+1] + dpW[w][BR[b]+1]); } if (w > 0) { dp[b][w] = min(dp[b][w], dp[b][w-1] + dpW[w][WR[w]+1] + dpB[b][WR[w]+1]); } } } dp[N][N].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; } 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 H = RD; auto W = RD; auto S = new string[](H); foreach (i; 0..H) { S[i] = RD!string; } long ans; auto dp = new long[2][][](H, W); foreach (y; 0..H) { foreach (x; 0..W) { if (S[y][x] == '#') continue; if (dp[y][x][0] == 0) { long cnt; foreach (z; x..W) { if (S[y][z] == '#') break; ++cnt; } foreach (z; x..x+cnt) { dp[y][z][0] = cnt; } } if (dp[y][x][1] == 0) { long cnt; foreach (z; y..H) { if (S[z][x] == '#') break; ++cnt; } foreach (z; y..y+cnt) { dp[z][x][1] = cnt; } } ans = max(ans, dp[y][x][0] + dp[y][x][1] - 1); } } writeln(ans); stdout.flush(); debug readln(); }
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, M; scan(N, M); auto X = new long[](M); auto Y = new long[](M); auto Z = new long[](M); foreach (i; 0 .. M) scan(X[i], Y[i], Z[i]); auto uf = UnionFind(N); foreach (i; 0 .. M) uf.unite(X[i] - 1, Y[i] - 1); uf.length.writeln(); } struct UnionFind { alias T = long; T[] rank, parent, sizes; long length; this(T n) { rank = new T[](n); sizes = new T[](n); sizes[] = 1; parent = iota!T(n).array(); length = n; } T find(T x) { return (this.parent[x] == x) ? x : (this.parent[x] = this.find(this.parent[x])); } void unite(T x, T y) { T rx = this.find(x), ry = this.find(y); if (rx == ry) return; length--; this.sizes[rx] = this.sizes[ry] = this.sizes[rx] + this.sizes[ry]; if (this.rank[rx] == this.rank[ry]) this.parent[rx] = ry, this.rank[ry]++; else if (this.rank[rx] < this.rank[ry]) this.parent[rx] = ry; else this.parent[ry] = rx; } T size(T x) { return this.sizes[this.find(x)]; } }
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 S = readln.split[0]; auto K = readln.split[0].to!long; foreach (c; S) { if (K == 1 || c != '1') { writeln(c); return; } else K--; } writeln(1); }
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 s = readln.chomp; int plus = cast(int)s.count('+'); writeln(plus - (4 - plus)); }
D
import std.stdio,std.array,std.conv; void main(){ string[] s=split(readln()); // splitで文字列を文字列の配列に切り分け int x=to!int(s[0]); // 第0要素を整数に変換 int y=to!int(s[1]); int z=to!int(s[2]); int ans=to!int((x-z)/(y+z)); writeln(ans); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { long n, m; scan(n, m); auto g = new long[][](n); // writeln(g); foreach (_; 0 .. m) { long a, b; scan(a, b); g[a - 1] ~= b - 1; g[b - 1] ~= a - 1; } // writeln(g); auto dist = new long[](n); dist[] = -1; // writeln(dist); DList!(long) que; dist[0] = 0; que.insertBack(0); // writeln(que); // foreach (x; que) // { // writeln("que:", x); // } // foreach (nv; g[1]) // { // writeln("nv:", nv); // writeln(dist[nv]); // } auto room_num = new long[](n); room_num[] = -1; while (!que.empty()) { long v = que.front(); que.removeFront(); foreach (nv; g[v]) { if (dist[nv] != -1) { continue; } dist[nv] = dist[v] + 1; room_num[nv] = v; que.insertBack(nv); } } if (func(room_num)) { writeln("No"); return; } else { writeln("Yes"); foreach (v; 1 .. n) { writeln(room_num[v] + 1); // writeln(dist[v]); } } } auto func(long[] x) { foreach (i; 1 .. (x.length)) { if (x[i] != -1) { continue; } else { return true; } } return false; } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.stdio, std.string, std.algorithm, std.range, std.conv; void main() { auto ip = readln.split.to!(int[]), A = ip[0], B = ip[1]; if(A > 8 || B > 8) { writeln(":("); } else { writeln("Yay!"); } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "p", long, "dist"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { auto s = sread(); long iter = s.length; auto dream = "dream", dreamer = "dreamer"; auto erase = "erase", eraser = "eraser"; while (iter > 0) { if(s[max(0, iter - cast(long) dream.length) .. iter] == dream) iter -= dream.length; else if(s[max(0, iter - cast(long) dreamer.length) .. iter] == dreamer) iter -= dreamer.length; else if(s[max(0, iter - cast(long) erase.length) .. iter] == erase) iter -= erase.length; else if(s[max(0, iter - cast(long) eraser.length) .. iter] == eraser) iter -= eraser.length; else break; } if(iter) writeln("NO"); else writeln("YES"); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
void main() { int[] tmp = readln.split.to!(int[]); int n = tmp[0], a = tmp[1], b = tmp[2]; writeln(min(a, b), " ", max(0, a+b-n)); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.container; import std.typecons;
D
import std.conv, std.stdio, std.algorithm, std.string, std.range; void main() { const input = readln.split.map!(to!int).array; if (1 <= min(input[0], input[1]) && max(input[0], input[1]) <= 9) { (input[0] * input[1]).writeln; } else { (-1).writeln; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto S = readln.chomp.to!(char[]); auto K = S.length; int w; foreach (s; S) { if (s == 'o') ++w; } writeln(w+(15-K) >= 8 ? "YES" : "NO"); }
D
import std.stdio, std.string, std.ascii, std.algorithm; void main() { string str, tmp; while ((tmp = readln.chomp.toLower) != "") { str ~= tmp; } foreach (ab; lowercase) { writeln(ab, " : ", str.count(ab)); } }
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; immutable long MOD = 10^^9 + 7; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto K = s[1]; auto dp = new long[](K + 1); dp[K] = 1; foreach_reverse (i; 1..K) { dp[i] = powmod(K / i, N, MOD); for (int j = i + i; j <= K; j += i) { dp[i] -= dp[j]; dp[i] = (dp[i] % MOD + MOD) % MOD; } } long ans = 0; foreach (i; 1..K+1) (ans += dp[i] * i % MOD) %= MOD; ans.writeln; } long powmod(long a, long x, long m) { long ret = 1; while (x) { if (x % 2) ret = ret * a % m; a = a * a % m; x /= 2; } return ret; }
D
import std.stdio, std.string, std.conv; import std.array, std.algorithm, std.range; void main() { bool[3] b;b[0]=true; foreach(s;stdin.byLine()) swap(b[s[0]-'A'],b[s[2]-'A']); foreach(i,v;b) if(v) writeln(cast(char)('A'+i)); }
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 nk = readln.split.to!(long[]); auto N = nk[0]; auto K = nk[1]; long[] AS; long max_a; foreach (a; readln.split.to!(long[])) { max_a = max(max_a, a); AS ~= a; } long l, r = max_a; while (l+1 < r) { auto m = (l+r)/2; long k; foreach (a; AS) { k += a/m; if (a%m == 0) --k; } if (k <= K) { r = m; } else { l = m; } } writeln(r); }
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; import core.bitop; int mybsr(long x) { if (x == 0) return -1; return bsr(x); } long f(long a, long b) { if (a == 0 && b == 0) return 1; long u = 1L<<mybsr(b); if (mybsr(a) == mybsr(b)) { return f(a-u, b-u); } long sm = f(a, u-1); long x = u-a; long y = 1L<<(mybsr(b-u)+1); return sm + min(u, x + y); } int main() { auto sc = new Scanner(stdin); long a, b; sc.read(a, b); writeln(f(a, b)); 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) { import core.bitop : bsf, bsr, popcnt; int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; 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.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new int[](t); foreach (ti; 0..t) { auto n = RD!int; auto s = RDA!int; ans[ti] = -1; foreach (i; 1..2^^10) { auto cnt = new int[](2^^10); foreach (j; 0..n) { ++cnt[s[j]]; } foreach (j; 0..n) { auto num = s[j] ^ i; --cnt[num]; } bool ok = true; foreach (j; 0..2^^10) { if (cnt[j] != 0) { ok = false; break; } } if (ok) { ans[ti] = i; break; } } } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
/+ dub.sdl: name "F" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; // import dcomp.container.deque; int main() { Scanner sc = new Scanner(stdin); int n; sc.read(n); long[] a; sc.read(a); int sm = 0; int cnt = 0; foreach (s; 0..n) { int zc, oc; while (a[s] > 1) { if (a[s] % 2) { zc++; oc++; } else { zc++; } a[s] /= 2; } oc = min(1, oc); if (cnt <= zc) { sm += oc+zc - cnt; cnt = oc+zc; } else if (cnt <= zc+oc) { sm += oc+zc - cnt; cnt = zc+oc-(cnt-zc); } else { cnt = zc; } // writeln(zc, " ", oc, " ", sm, " ", cnt); /* while (a[s] != 1) { int t = s+1; while (t < n) { if (a[t] == 1) break; t++; if (a[t-1] % 2) break; } sm++; foreach (i; s..t) { import core.bitop; if (s < i && i < t-1) { a[i] /= 2; continue; } int u = bsr(a[i]); if ((1<<u) == a[i]) a[i] /= 2; else a[i] = 1<<u; } // writeln(s, " ", t, " ", a); }*/ } writeln(sm); return 0; } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/container/deque.d */ // module dcomp.container.deque; struct Deque(T, bool mayNull = true) { import core.exception : RangeError; import core.memory : GC; import std.range : ElementType, isInputRange; import std.traits : isImplicitlyConvertible; struct Payload { T *d; size_t st, length, cap; @property bool empty() const { return length == 0; } alias opDollar = length; ref inout(T) opIndex(size_t i) inout { version(assert) if (length <= i) throw new RangeError(); return d[(st+i >= cap) ? (st+i-cap) : st+i]; } private void expand() { import std.algorithm : max; assert(length == cap); auto nc = max(size_t(4), 2*cap); T* nd = cast(T*)GC.malloc(nc * T.sizeof); foreach (i; 0..length) { nd[i] = this[i]; } d = nd; st = 0; cap = nc; } void clear() { st = length = 0; } void insertFront(T v) { if (length == cap) expand(); if (st == 0) st += cap; st--; length++; this[0] = v; } void insertBack(T v) { if (length == cap) expand(); length++; this[length-1] = v; } void removeFront() { assert(!empty, "Deque.removeFront: Deque is empty"); st++; length--; if (st == cap) st = 0; } void removeBack() { assert(!empty, "Deque.removeBack: Deque is empty"); length--; } } struct RangeT(A) { alias T = typeof(*(A.p)); alias E = typeof(A.p.d[0]); T *p; size_t a, b; @property bool empty() const { return b <= a; } @property size_t length() const { return b-a; } @property RangeT save() { return RangeT(p, a, b); } @property RangeT!(const A) save() const { return typeof(return)(p, a, b); } alias opDollar = length; @property ref inout(E) front() inout { return (*p)[a]; } @property ref inout(E) back() inout { return (*p)[b-1]; } void popFront() { version(assert) if (empty) throw new RangeError(); a++; } void popBack() { version(assert) if (empty) throw new RangeError(); b--; } ref inout(E) opIndex(size_t i) inout { return (*p)[i]; } RangeT opSlice() { return this.save; } RangeT opSlice(size_t i, size_t j) { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } RangeT!(const A) opSlice() const { return this.save; } RangeT!(const A) opSlice(size_t i, size_t j) const { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } } alias Range = RangeT!Deque; alias ConstRange = RangeT!(const Deque); alias ImmutableRange = RangeT!(immutable Deque); Payload* p; private void I() { if (mayNull && !p) p = new Payload(); } private void C() const { version(assert) if (mayNull && !p) throw new RangeError(); } static if (!mayNull) { @disable this(); } private this(Payload* p) { this.p = p; } this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) { p = new Payload(); foreach (v; values) { insertBack(v); } } this(Range)(Range r) if (isInputRange!Range && isImplicitlyConvertible!(ElementType!Range, T) && !is(Range == T[])) { p = new Payload(); foreach (v; r) { insertBack(v); } } static Deque make() { return Deque(new Payload()); } @property bool havePayload() const { return (!mayNull || p); } @property bool empty() const { return (!havePayload || p.empty); } @property size_t length() const { return (havePayload ? p.length : 0); } alias opDollar = length; ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; } ref inout(T) front() inout {C; return (*p)[0]; } ref inout(T) back() inout {C; return (*p)[$-1]; } void clear() { if (p) p.clear(); } void insertFront(T v) {I; p.insertFront(v); } void insertBack(T v) {I; p.insertBack(v); } alias stableInsertBack = insertBack; void removeFront() {C; p.removeFront(); } void removeBack() {C; p.removeBack(); } Range opSlice() {I; return Range(p, 0, length); } } /* IMPORT /Users/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); } } } /* IMPORT /Users/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) { import core.bitop : bsf, bsr, popcnt; int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } }
D
void main() { long n, z, w; rdVals(n, z, w); long[] a = rdRow; long result = abs(a[$-1]-w); if (n > 1) result = max(result, abs(a[$-2]-a[$-1])); 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 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 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() { auto dp = new long[][](1000 + 2, 1000 + 2); long H, W; scan(H, W); // if (H != 3) // return; dp[0][] = 0; foreach (row; dp) row[0] = 0; dp[1][1] = 1; foreach (i; 1 .. H + 1) { auto s = sread(); foreach (j; 1 .. W + 1) if (s[j - 1] == '.') { // writefln("(%d %d): %d + %d", i, j, dp[i - 1][j], dp[i][j - 1]); dp[i][j].maxAssign((dp[i - 1][j] + dp[i][j - 1]) % (10 ^^ 9 + 7)); } else { dp[i][j] = 0; } } dp[H][W].writeln(); }
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; } } //右側と左側からのgcdをやると計算量が減るらしい. void main() { auto n = lread(); auto a = aryread(); long[] b = new long[n + 1]; long[] c = new long[n + 1]; foreach (i, ref e; b) { if (i == 0) { continue; } e = gcd(b[i - 1], a[i - 1]); } foreach_reverse (i, ref e; c) { if (i == n) { continue; } e = gcd(c[i + 1], a[i]); } // writeln(a); // writeln(b); // writeln(c); long max_gcd = 0; foreach (i; 0 .. n) { max_gcd = max(gcd(b[i], c[i + 1]), max_gcd); } writeln(max_gcd); }
D
import std.stdio; import std.algorithm; import std.array; import std.string; import std.math; import std.conv; // 昇順に並んだリスト int solve(int[] list) { if ((pow(list[0], 2) + pow(list[1], 2)) == pow(list[2], 2)) return 0; else if (list[0] == list[1]) return 1; return -1; } void main() { string line; int[] table = new int[](2); while((line = readln.chomp) != null) { int n = line.split(",").map!(to!(int)).array.solve; if (n >= 0) table[n]++; } foreach(t; table) writeln(t); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void calc(string[] ss) { int rows = cast(int) ss.length; int cols = cast(int) ss[0].length; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (ss[i][j] == '#') { write('#'); continue; } int bomb = 0; for (int dr = -1; dr <= 1; dr++) { for (int dc = -1; dc <= 1; dc++) { if (dr == 0 && dc == 0) continue; int r = i + dr; int c = j + dc; if (0 <= r && r < rows && 0 <= c && c < cols) { if (ss[r][c] == '#') { bomb++; } } } } write(bomb); } writeln(); } } void main() { auto hw = readints; int rows = hw[0]; string[] ss; for (int i = 0; i < rows; i++) { ss ~= readln.chomp; } calc(ss); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { int N; scan(N); auto a = readln.split.to!(int[]); auto b = readln.split.to!(int[]); foreach (i ; 0 .. N) { if (a[i] != b[i] && b[i] >= (a[i] + 1) / 2) { writeln(-1); return; } } // dp(x, y, i) = 1 ~ i を使って x を y にできるか auto dp = new bool[][][](51, 51, 51); foreach (i ; 0 .. 51) { dp[i][i][0] = true; } foreach (i ; 1 .. 51) { foreach (x ; 0 .. 51) { foreach (y ; 0 .. 51) { dp[x][y][i] = dp[x][y][i - 1] || dp[x % i][y][i - 1]; } } } auto s = new long[](N); foreach (i ; 0 .. N) { s[i] = 1L << a[i]; } long ans; foreach_reverse (k ; 1 .. 51) { bool use; foreach (i ; 0 .. N) { bool ok; foreach (j ; 0 .. 51) if (s[i] & 1L << j) { ok |= dp[j][b[i]][k - 1]; } if (!ok) { use = true; break; } } if (!use) continue; ans += 1L << k; foreach (i ; 0 .. N) { foreach (j ; 0 .. 51) if (s[i] & 1L << j) { s[i] |= 1L << (j % k); } } } debug { writefln("%b", ans); } 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.array, std.conv, std.algorithm, std.typecons, std.range, std.container, std.math, std.algorithm.searching, std.functional,std.mathspecial, std.numeric; void main(){ auto l=readln.chomp; writeln(l[0],l.length-2,l[$-1]); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math; void main() { auto nk = readln.split.to!(int[]); auto N = nk[0]; auto K = nk[1]; long r; int k = K; while (k <= N) { ++r; k += K; } r ^^= 3; if (K % 2 == 0) { long s; k = K / 2; while (k <= N) { ++s; k += K; } r += s^^3; } writeln(r); }
D
void main() { int r = readln.chomp.to!int; if (r < 1200) { "ABC".writeln; } else if (r < 2800) { "ARC".writeln; } else { "AGC".writeln; } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.container; import std.typecons;
D
import std.stdio; import std.string; import std.conv; import std.algorithm; void main(){ string s; while(!(s=readln).length==0){ s=chomp(s); for(int i=cast(int)s.length-1; i>=0; i--) write(s[i]); writeln(); } }
D
void main() { auto X = ri; ulong res; if(X == 1) res = 1; foreach(i; 1..X) { debug iota(1, i+1).sum.writeln; // アホ // if(iota(i).sum < X) res++; if((i-1)*i/2 < X) res++; else break; } if(X == 2) res = 2; res.writeln; } // =================================== import std.stdio; import std.string; 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; } T[] deepcopy(T)(T[] a) { static if(isArray!T) { T[] res; foreach(i; a) { res ~= deepcopy(i); } return res; } else { return a.dup; } } ulong[] generate_prime_list(T)(T N) if(isIntegral!T) { ulong[] prime_list = [2]; bool not_prime = false; foreach(i; 3..N.to!ulong+1) { foreach(j; prime_list) { if(i % j == 0) { not_prime = true; break; } } if(!not_prime) prime_list ~= i; not_prime = false; } return prime_list; } bool isPrime(ulong n) { if(n <= 1) return false; else if(n == 2) return true; else if(n % 2 == 0) return false; foreach(i; iota(3, n.to!double.sqrt.ceil+1, 2)) { if(n % i == 0) return false; } return true; } class UnionFind(T) { T[] arr; this(ulong n) { arr.length = n+1; arr[] = -1; } T root(T x) { return arr[x] < 0 ? x : root(arr[x]); } bool same(T x, T y) { return root(x) == root(y); } bool unite(T x, T y) { x = root(x); y = root(y); if(x == y) return false; if(arr[x] > arr[y]) swap(x, y); arr[x] += arr[y]; arr[y] = x; return true; } T size(T a) { return -arr[root(a)]; } }
D
import std.stdio, std.algorithm, std.string, std.conv, std.array, std.range, std.math; void main() { string s = readln.chomp; bool can = 1; foreach(c; "abc") can &= (count(s, c) == 1); writeln(can ? "Yes" : "No"); }
D
import std.algorithm; import std.conv; import std.math; import std.range; import std.stdio; import std.string; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto n = readln.strip.to !(int); auto a = readln.splitter.map !(to !(long)).array; writeln (a.front < a.back ? "YES" : "NO"); } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { int m = readint; writeln(48 - m); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { long[] CS, xs; CS.length = 16; xs.length = 20000; auto DP = new long[][](20000, 256); for (;;) { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; if (N == 0 && M == 0) return; foreach (i; 0..M) CS[i] = readln.chomp.to!long; foreach (i; 0..N) xs[i] = readln.chomp.to!long; foreach (ref dp; DP) dp[] = -1; long solve(int i, long n) { if (i == N) return 0; if (DP[i][n] == -1) { auto r = long.max/3; foreach (c; CS[0..M]) { auto m = max(0, min(255, n+c)); r = min(r, solve(i+1, m) + (xs[i] - m)^^2); } DP[i][n] = r; } return DP[i][n]; } writeln(solve(0, 128)); } }
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 n = RD!int; auto a = RDA; long x, tot; foreach (e; a) { x.chmax(e); tot += e; } auto y = x * (n-1); if (tot <= y) ans[ti] = y - tot; else ans[ti] = ((n-1) - (tot % (n-1))) % (n-1); } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
import std.stdio, std.array, std.conv, std.algorithm, std.string, std.numeric, std.range; void main() { int[string] m; m["O"] = 0; m["A"] = 0; m["B"] = 0; m["AB"] = 0; string s; while( (s=readln.strip).length != 0 ){ auto as = s.split(','); m[as[1]] += 1; } writeln(m["A"]); writeln(m["B"]); writeln(m["AB"]); writeln(m["O"]); }
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 calc(int[] a) { long sum = abs(a[0]) + abs(a[$-1]); for (int i = 1; i < a.length; i++) sum += abs(a[i] - a[i-1]); for (int i = 0; i < a.length; i++) { // i を訪れないでスキップした場合の費用 // i のひとつ前 int s = i == 0 ? 0 : a[i-1]; // i のひとつ後 int t = i+1 == a.length ? 0 : a[i+1]; long costOld = abs(s - a[i]) + abs(a[i] - t); // i をスキップした場合のコスト long costNew = abs(s - t); long cost = sum - costOld + costNew; writeln(cost); } } void main() { readint; auto a = readints; calc(a); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { readint(); auto ss = assumeSorted(readints()); readint(); auto ts = readints(); int ans = 0; foreach (t; ts) { if (ss.contains(t)) ans++; } 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 = lread(); auto A = aryread!ulong(); auto S1 = new long[](N + 1); auto S2 = new long[](N + 1); foreach (i; 0 .. N) { S1[i + 1] = S1[i] + A[i]; S2[i + 1] = S2[i] ^ A[i]; } long i; long j; long ans; while (i < N) { while (j < N && (S1[j+1] - S1[i]) == (S2[j+1] ^ S2[i])) j++; ans += j - i; i++; } writeln(ans); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { auto nq = readints(); int n = nq[0], q = nq[1]; for (int i = 0; i < q; i++) { auto xs = readints(); if (xs[0] == 0) { int s = xs[1]; int t = xs[2]; int x = xs[3]; add(s, t + 1, x, 0, 0, N / 2); } else { int s = xs[1]; int t = xs[2]; auto v = sum(s, t + 1, 0, 0, N / 2); writeln(v); } } } /////////////////////////////////////// const int N = 1 << 18; // ???????????° long[N * 2] _data; long[N * 2] _datb; /// [a, b) ??? x ??????????????? /// k ????????????????????§????????? [l, r) ??????????????? void add(int a, int b, int x, int k, int l, int r) { if (a <= l && r <= b) { _data[k] += x; } else if (l < b && a < r) { _datb[k] += (min(b, r) - max(a, l)) * x; add(a, b, x, k * 2 + 1, l, (l + r) / 2); add(a, b, x, k * 2 + 2, (l + r) / 2, r); } } /// [a, b) ??????????±??????? /// k ????????????????????§????????? [l, r) ???????????????] long sum(int a, int b, int k, int l, int r) { if (b <= l || r <= a) return 0; if (a <= l && r <= b) return _data[k] * (r - l) + _datb[k]; auto ret = (min(b, r) - max(a, l)) * _data[k]; ret += sum(a, b, k * 2 + 1, l, (l + r) / 2); ret += sum(a, b, k * 2 + 2, (l + r) / 2, r); return ret; }
D
void main() { auto N = readAs!ulong; ulong res = 1; foreach(i; 0..N) { auto b = readAs!ulong; res = res / gcd(res, b) * b; } res.writeln; } // =================================== import std.stdio; import std.string; 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; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; void times(alias fun)(int n) { foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } void main() { readln.chomp.to!int.pipe!( n => n*800 - n/15*200 ).writeln; } // ---------------------------------------------- // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { auto minElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto minimum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b < minimum) { element = a; minimum = b; } } return element; } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto maximum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b > maximum) { element = a; maximum = b; } } return element; } }
D
// Vicfred // https://atcoder.jp/contests/abc129/tasks/abc129_b // brute force import std.algorithm; import std.array; import std.conv; import std.math; import std.stdio; import std.string; void main() { const long n = readln.chomp.to!long; const long[] ws = readln.split.map!(to!long).array; long[] sum = new long[n]; sum[0] = ws[0]; foreach(i; 1..n) sum[i] += sum[i - 1] + ws[i]; long minima = long.max; for(int i = 1; i < n; i++) { const long left = sum[i - 1]; const long right = sum[n-1] - sum[i - 1]; minima = min(minima, abs(left - right)); } minima.writeln; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; alias Grid = Tuple!(long, "x", long, "y"); 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() { auto n = lread(); auto a = aryread(); auto b = aryread(); auto com_a = new long[](n); auto com_b = new long[](n + 1); com_a[0] = a[0], com_b[1] = b[0]; foreach (i; iota(1, n)) { com_a[i] += com_a[i - 1] + a[i]; com_b[i + 1] += com_b[i] + b[i]; } // com_a.writeln(); // com_b.writeln(); long moves; foreach(i; iota(n)) { auto cost = com_a[i] + com_b[$ - 1] - com_b[i]; moves = max(cost, moves); } moves.writeln(); }
D
void main() { long n, m; rdVals(n, m); Edge[] edge = m.rdCol!Edge; foreach (ref e; edge) --e.from, --e.to; long[] dist = new long[n]; foreach (i; 1 .. n) dist[i] = -inf; void Bellman_Ford() { foreach (i; 0 .. n) { foreach (e; edge) { if (dist[e.from] == -inf) continue; dist[e.to] = max(dist[e.to], dist[e.from]+e.cost); } } } Bellman_Ford(); long result = dist[$-1]; Bellman_Ford(); if (result == dist[$-1]) result.writeln; else "inf".writeln; } struct Edge { long from, to, cost; } 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
// Cheese-Cracker: cheese-cracker.github.io void theCode(){ ll a = scan; ll b = scan; ll c = scan; ll hf = abs(a - b); ll n = 2 * hf; if(n < max(a, b, c)){ writeln(-1); }else{ ll rem = (c + hf) % n; if(rem == 0){ writeln(n); }else{ writeln(rem); } } } void main(){ long tests = scan; // Toggle! while(tests--) theCode(); } /********* That's All Folks! *********/ import std.stdio, std.random, std.functional, std.container, std.algorithm; import std.numeric, std.range, std.typecons, std.string, std.math, std.conv; string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;} T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; } void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } } alias ll = long, tup = Tuple!(long, "x", long, "y");
D
import std; import core.bitop; 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, "B", long, "W"); alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less); class UnionFind { long n; long[] par; this(long init) { n = init; par = iota(n).array(); } long root(long x) { if (par[x] == x) return x; else return par[x] = root(par[x]); } void unit(long x, long y) { if (root(x) != root(y)) par[root(x)] = root(y); } bool same(long x, long y) { return (root(x) == root(y)); } } void main() { long h, w; scan(h, w); auto s = new string[](h); s.each!((ref x) => x = sread()); long convIdx(long x, long y) { return x * w + y; } auto uf = new UnionFind(h * w); foreach (i; iota(h)) { foreach (j; iota(w)) { if (i - 1 >= 0 && s[i - 1][j] != s[i][j]) uf.unit(convIdx(i - 1, j), convIdx(i, j)); if (i + 1 < h && s[i + 1][j] != s[i][j]) uf.unit(convIdx(i + 1, j), convIdx(i, j)); if (j - 1 >= 0 && s[i][j - 1] != s[i][j]) uf.unit(convIdx(i, j - 1), convIdx(i, j)); if (j + 1 < w && s[i][j + 1] != s[i][j]) uf.unit(convIdx(i, j + 1), convIdx(i, j)); } } Pair[long] countBW; foreach (i; iota(h)) { foreach (j; iota(w)) { auto idx = uf.root(convIdx(i, j)); auto p = Pair(s[i][j] == '#', s[i][j] == '.'); if (idx in countBW) countBW[idx] = Pair(countBW[idx].B + p.B, countBW[idx].W + p.W); else countBW[idx] = p; } } long ans; foreach (key, value; countBW) ans += value.B * value.W; 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.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int a, b; readV(a, b); writeln(a%3 == 0 || b%3 == 0 || (a+b)%3 == 0 ? "Possible" : "Impossible"); }
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 a, b, c, d, e, k; scan(a);scan(b);scan(c);scan(d);scan(e);scan(k); bool ok = e - a <= k; writeln(ok ? "Yay!" : ":("); } 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
void main() { import std.stdio, std.string; string s = readln.chomp; writeln("2018", s[4 .. $]); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.container; import std.array; import std.math; import std.range; import std.typecons; import std.ascii; import std.format; void main() { auto a = readln.chomp.split.map!(to!int); writeln = a[0] ^ a[1] ^ a[2]; }
D
import std.stdio; import std.string, std.conv, std.array, std.algorithm; import std.uni, std.math, std.container, std.typecons; import core.bitop, std.datetime, std.range; void main(){ auto rd = readln.split.to!(int[]); auto n = rd[0], q = rd[1]; auto ds = new int[][](n, 1); auto n2s = iota(n).array; foreach(i ; 0 .. n){ ds[i][0] = i; } foreach(lp ; 0 .. q){ rd = readln.split.to!(int[]); auto c = rd[0], x = rd[1], y = rd[2]; if(c == 0){ merge(ds, n2s, x, y); } else{ writeln(n2s[x] == n2s[y] ? 1 : 0); } } //writefln("%(%s\n%)", ds); } void merge(int[][] ds, int[] n2s, int x, int y){ auto u = n2s[x]; auto v = n2s[y]; if(u == v) return; if(u > v) swap(u, v); ds[v].length += ds[u].length; ds[v][$ - ds[u].length .. $] = ds[u]; foreach(z ; ds[u]){ n2s[z] = v; } delete ds[u]; }
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; 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; long calc(string s) { long ans = 0; // int だと overflow int a = 0; // a の個数 int p = 0; while (p < s.length) { if (s[p] == 'A') { a++; p++; } else if (s[p] == 'B') { if (a > 0 && p + 1 < s.length && s[p + 1] == 'C') { ans += a; p += 2; } else { a = 0; p++; } } else { a = 0; p++; } } return ans; } void main() { string s = read!string; writeln(calc(s)); }
D
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } enum long MO = 998244353; void main() { try { for (; ; ) { const M = readInt(); const N = readInt(); long ans = 1; foreach (i; 0 .. M + N) { ans *= 2; ans %= MO; } writeln(ans); } } catch (EOFException e) { } }
D
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array, std.functional, std.container, std.typecons; void main() { auto N = readln.split[0].to!int; auto A = readln.split.to!(int[]); auto B = readln.split.to!(int[]); ulong ans = 0; foreach(i; 0..N) { int b = B[i]; int x; x = min(A[i], b); ans += x; A[i] -= x; b-= x; x = min(A[i+1], b); ans += x; A[i+1] -= x; b -= x; } ans.writeln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n, a, b, c; scan(n, a, b, c); auto x = iota(n).map!(i => readln.chomp.to!int).array; long ans = inf6; foreach (s ; 0 .. 4^^n) { int la, lb, lc; int ca, cb, cc; foreach (i ; 0 .. n) { if (s % 4 == 0) { } else if (s % 4 == 1) { la += x[i]; ca++; } else if (s % 4 == 2) { lb += x[i]; cb++; } else if (s % 4 == 3) { lc += x[i]; cc++; } s /= 4; } if (la == 0 || lb == 0 || lc == 0) continue; ans = min(ans, abs(la - a) + abs(lb - b) + abs(lc - c) + 10*(ca + cb + cc - 3)); } writeln(ans); } int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new int[][](n, 0); foreach (i ; 0 .. m) { int u, v; scan(u, v); if (is1indexed) { u--, v--; } adj[u] ~= v; if (isUndirected) { adj[v] ~= u; } } return adj; } alias Edge = Tuple!(int, "to", int, "cost"); Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new Edge[][](n, 0); foreach (i ; 0 .. m) { int u, v, c; scan(u, v, c); if (is1indexed) { u--, v--; } adj[u] ~= Edge(v, c); if (isUndirected) { adj[v] ~= Edge(u, c); } } return adj; } void yes(bool b) {writeln(b ? "Yes" : "No");} void YES(bool b) {writeln(b ? "YES" : "NO");} T[] readArr(T)() {return readln.split.to!(T[]);} 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.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new int[](t); foreach (ti; 0..t) { auto x1 = RD!int; auto y1 = RD!int; auto x2 = RD!int; auto y2 = RD!int; ans[ti] = abs(x1-x2) + abs(y1-y2); if (x1 != x2 && y1 != y2) ans[ti] += 2; } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio; import std.algorithm; import std.string; import std.functional; import std.array; import std.conv; import std.math; import std.typecons; import std.regex; import std.range; void main(){ while(true){ int n = readln().chomp().to!int; if(n==0) break; int ans = 100000000; for(int i=0;i<=25;i++){ for(int j=0;j<=17;j++){ for(int k=0;k<=10;k++){ if((200*i+300*j+500*k)==n){ double p1,p2,p3; p1 = 380 * 5 * (i/5) * 0.8 + 380 * (i % 5); p2 = 550 * 4 * (j/4) * 0.85 + 550 * (j % 4); p3 = 850 * 3 * (k/3) * 0.88 + 850 * (k % 3); ans = min(ans , to!int(p1+p2+p3)); } } } } writeln(ans); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto a = RD!int; auto b = RD!int; auto c = RD!int; auto d = RD!int; if ((a-b)*n > c+d) continue; if ((a+b)*n < c-d) continue; ans[ti] = true; } foreach (e; ans) { writeln(e ? "Yes" : "No"); } stdout.flush; debug readln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto S = readln.chomp; if (S.length == 2) { if (S[0] == '0' || S[1] == '1') { writeln(-1); } else { writeln("1 2"); } return; } if (S[0] == '0' || S[$-2] == '0' || S[$-1] == '1') { writeln(-1); return; } int[] as, bs; int p = 1; auto N = S.length.to!int; int i, j = N-2; while (i <= j) { if (S[i] != S[j]) { writeln(-1); return; } as ~= p; bs ~= i+2; if (S[i] == '1') p = i+2; ++i; --j; } for (; i < N-1; ++i) { as ~= p; bs ~= i+2; } foreach (k; 0..N-1) writeln(as[k], " ", bs[k]); }
D
void main() { long n = readln.chomp.to!long; dchar[] s = readln.chomp.to!(dchar[]); dchar[] t = readln.chomp.to!(dchar[]); long len = 2 * n; foreach_reverse (i; 0 .. n) { if (s[i..$] == t[0..$-i]) { len = i + n; } } len.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; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; void main() { readln.chomp.pipe!(a => a.retro.equal(a)).pipe!(a => a?"Yes":"No").writeln; } // ---------------------------------------------- void times(alias fun)(int n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { auto minElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto minimum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b < minimum) { element = a; minimum = b; } } return element; } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto maximum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b > maximum) { element = a; maximum = b; } } return element; } }
D
import std.stdio, std.string, std.conv, std.algorithm, std.array; void main(){ while(1){ auto ffffffffff = readln.chomp.to!(int); if(ffffffffff==0) break; int a = 1000 - ffffffffff; int ans; if(a>=500){ ans++; a-=500; } if(a>=100){ ans += a/100; a -= a/100*100; } if(a>=50){ ans ++; a -= 50; } if(a>=10){ ans += a/10; a -= a/10*10; } if(a>=5){ ans++; a -= 5; } ans += a; writeln(ans); } }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip, std.regex; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto D = s[1].to!long; auto A = readln.split.map!(to!long).array; auto B = new long[](N+1); B[0] = D; foreach (i; 0..N) B[i+1] = min(B[i], abs(B[i] - A[i])); auto ans = new bool[](N); long x = 1; foreach_reverse(i; 0..N) { ans[i] = B[i] >= x; if (x > A[i] / 2) x += A[i]; } auto Q = readln.chomp.to!int; readln.split.map!(i => (ans[i.to!int-1] ? "YES" : "NO")).each!writeln; }
D
void main() { string[] s = readln.split(','); s.join(" ").writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.conv, std.string, std.range, std.array, std.algorithm; import std.bigint; int[32] dp; void main() { while (true){ int n = readln().chomp().to!int; if (n == 0) break; dp[0 .. 32] = -1; int pat = shinobu(n); pat /= 10; if (pat % 10 != 0){ pat++; } writeln(pat / 365 + 1); } } int shinobu(int n) { if (dp[n] != -1){ return (dp[n]); } if (n == 1) return (1); if (n == 2) return (2); if (n == 3) return (4); return (dp[n] = shinobu(n - 1) + shinobu(n - 2) + shinobu(n - 3)); }
D
import std.algorithm; import std.array; import std.bigint; import std.bitmanip; import std.conv; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; } T[] readToArray(T)() { return readln.split.to!(T[]); } void readInto(T...)(ref T ts) { auto ss = readln.split; foreach(ref t; ts) { t = ss.front.to!(typeof(t)); ss.popFront; } } void main() { ulong n; readInto(n); ulong[] p = readToArray!(ulong); ulong cnt = 0; for(ulong i = 1; i < p.length-1; i++) { //writefln("%d %d %d", p[i-1], p[i], p[i+1]); if(p[i-1] < p[i] && p[i] < p[i+1]) cnt++; else if (p[i+1] < p[i] && p[i] < p[i-1]) cnt++; } writeln(cnt); }
D
void main() { int[] ab = readln.split.to!(int[]); writeln(ab.all!"a & 1" ? "Odd" : "Even"); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.math; void main() { immutable EPS = 1.0e-12; string[] input = new string[](8); while ((input = readln.chomp.split(" ")).length != 0) { auto points = input.map!(to!double); auto innerProduct = fabs((points[2] - points[0])*(points[6] - points[4]) + (points[3] - points[1])*(points[7] - points[5])); if (innerProduct < EPS) { writeln("YES"); } else { writeln("NO"); } } }
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); int ans; foreach (i ; 1 .. N + 1) { if (i % 2 && countDiv(i) == 8) ans++; } writeln(ans); } int countDiv(int x) { int[] divs; for (int i = 1; i <= x; i++) { if (x % i == 0) divs ~= i; } return divs.length.to!int; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop; // dfmt on void main() { long a = lread(); auto s = sread(); writeln(3200 <= a ? s : "red"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; alias VDC = Tuple!(int, "v", int, "d", int, "c"); int[10^^5] MAX_D, COLS, MEMO; int[][10^^5] G; VDC[10^^5] QS; void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; foreach (_; 0..M) { auto ab = readln.split.to!(int[]); auto a = ab[0] - 1; auto b = ab[1] - 1; G[a] ~= b; G[b] ~= a; } auto Q = readln.chomp.to!int; foreach_reverse (i; 0..Q) { auto vdc = readln.split.to!(int[]); QS[i] = VDC(vdc[0]-1, vdc[1]+1, vdc[2]); } void paint(int v, int d, int c) { if (MAX_D[v] >= d) return; MAX_D[v] = d; if (!COLS[v]) COLS[v] = c; foreach (w; G[v]) if (MAX_D[w] < d-1) paint(w, d-1, c); } foreach (q; QS) paint(q.v, q.d, q.c); foreach (i; 0..N) writeln(COLS[i]); }
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 = 10^^9 + 7; long modPow(long x, long k, long m) { if (k == 0) return 1; if (k % 2 == 0) return modPow(x * x % m, k / 2, m); return x * modPow(x, k - 1, m) % m; } long nck(long n, long k, long mod) { long x = 1; for (int i = 0; i < k; i++) { x = (x * (n - i)) % mod; } long kk = 1; for (int i = 1; i <= k; i++) { kk = (kk * i) % mod; } return (x * modPow(kk, mod - 2, mod)) % mod; } long calc(long n, long a, long b) { long ans = modPow(2, n, mod); ans = (ans - nck(n, a, mod) + mod) % mod; ans = (ans - nck(n, b, mod) + mod) % mod; ans--; return ans; } void main() { int n, a, b; scan(n, a, b); writeln(calc(n, a, b)); }
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; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ long l = read.to!long; long r = read.to!long; long ans; if(r / 2019 > l / 2019) ans = 0; else{ ans = 2018; foreach(i; l .. r + 1) foreach(j; l .. i) ans = min(ans, i * j % 2019); } ans.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; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } int calc(string s) { char[] cs; for (int i = 0; i < s.length; i++) { if (s[i] != 'x') cs ~= s[i]; } for (int i = 0; i < cs.length / 2; i++) { if (cs[i] != cs[$ - i - 1]) { return -1; } } int ans = 0; int lo = 0; int hi = cast(int) s.length - 1; while (lo < hi) { if (s[lo] == s[hi]) { lo++; hi--; } else if (s[lo] == 'x') { lo++; ans++; } else if (s[hi] == 'x') { hi--; ans++; } else { writeln("err"); return -1; } } return ans; } void main() { auto s = readln.chomp; writeln(calc(s)); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.math; import std.conv; void main() { (min(r, r) + min(r, r)).writeln; } int r() { return readln.chomp.to!int; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { int x = readint; int sum = 0; for (int t = 1; ; t++) { sum += t; if (sum >= x) { writeln(t); break; } } }
D
import std.stdio; import std.array; import std.string; import std.conv; import std.algorithm; import std.typecons; import std.range; import std.random; import std.math; import std.container; import std.numeric; import std.bigint; import core.bitop; void main() { auto input = readln.split.map!(to!int); auto N = input[0].to!int; auto K = input[1].to!int; auto P = iota(N).map!(_ => readln[0]).array; auto cnt = new int[](26); foreach (p; P) cnt[p-'A']++; if (N < K) {writeln(0); return;} foreach (i; 1..N/K+1) if (cnt.map!(c => min(c, i)).sum < i*K) { writeln(i-1); return; } writeln(N/K); }
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 x, y; scan(x, y); if (x % y) { writeln(x * (y - 1)); } else { writeln(-1); } } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
void main() { bool[int] a; auto N = ri; foreach(_; 0..N) { a[ri] ^= true; } a.values.count!(a => a).writeln; } // =================================== import std.stdio; import std.string; 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.algorithm; import std.array; import std.stdio; import std.conv; import std.string; void main(){ int count; foreach(Unused; 0..readln.chomp.to!int){ if(readln.chomp.to!int.isPrime){ ++count; } } count.writeln; } bool isPrime(int n){ if(n <= 1){ 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; }
D
import std.stdio, std.string, std.conv; void main() { string s = readln.chomp; int[] num = new int[s.length]; int cnt; foreach (i, x; s) { if (x == 'R') { ++cnt; } else { num[i-1] += cnt / 2 + cnt % 2; num[i] += cnt / 2; cnt = 0; } } foreach_reverse (i, x; s) { if (x == 'L') { ++cnt; } else { num[i] += cnt / 2; num[i+1] += cnt / 2 + cnt % 2; cnt = 0; } } num.to!(string[]).join(" ").writeln; }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int N, K; scan(N, K); auto h = readln.split.to!(int[]); auto dp = new int[](N); dp[] = inf; dp[0] = 0; foreach (i ; 1 .. N) { foreach (j ; max(0, i - K) .. i) { chmin(dp[i], dp[j] + abs(h[i] - h[j])); } } auto ans = dp[N - 1]; writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); void main() { auto n = lread(); auto ans = new long[](10 ^^ 5); foreach (i; 1 .. 101) { foreach (j; 1 .. 101) { foreach (k; 1 .. 101) { long tmp; tmp = i ^^ 2 + j ^^ 2 + k ^^ 2 + i * j + j * k + i * k; if (tmp <= 10 ^^ 4) { ans[tmp - 1] += 1; } } } } // writeln(ans); foreach (i; 0 .. n) { writeln(ans[i]); } } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; string m(string a, string b) { if (a == "T" && b == "T") return "T"; if (a == "T" && b == "F") return "F"; if (a == "F" && b == "T") return "T"; return "T"; } void main() { auto N = readln.chomp.to!int; auto ps = readln.split.to!(string[]); auto c = m(ps[0], ps[1]); foreach (x; ps[2..$]) { c = m(c, x); } writeln(c); }
D
import std.stdio,std.conv,std.string; int main(){ int x; x=stdin.readln().chomp.to!int; if(x==3||x==5||x==7){ write("YES"); } else{ write("NO"); } return 0; }
D
import std.stdio; import std.range; import std.array; import std.algorithm; import std.string; import std.conv; import std.typecons; import std.math; import std.numeric; void main(){ int n = readln().chomp().to!int; int[] inputs = readln().chomp().split().map!(to!int).array(); solve(n, inputs).writeln(); } int solve(int n, int[] inputs){ return inputs.sum - n; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; enum P = 10L^^9+7; void main() { auto F = new long[](10^^6*2+10); auto RF = new long[](10^^6*2+10); 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; } 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; auto xy = readln.split.to!(long[]); auto X = xy[0]; auto Y = xy[1]; auto a = 2*X - Y; auto b = 2*Y - X; if (a < 0 || b < 0 || a%3 != 0 || b%3 != 0) { writeln(0); return; } a /= 3; b /= 3; if (a == 0 || b == 0) { writeln(1); return; } writeln(((F[a+b] * RF[a]) % P * RF[b]) % P); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; import std.range; void main(){ auto Q=readln.split,a=Q[0],b=Q[1]; if(a==b)writeln("H"); else writeln("D"); }
D
import std.stdio; import std.algorithm; import std.string; import std.range; import std.array; import std.conv; import std.complex; import std.math; import std.ascii; import std.bigint; import std.container; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } void readlnTo(T...)(ref T t) { auto s = readln().split(); assert(s.length == t.length); foreach(ref ti; t) { ti = s[0].to!(typeof(ti)); s = s[1..$]; } } const real eps = 1e-10; void main(){ auto n = readInt(); writeln(n/7 * 2 + max(0, n%7-5), " ", n/7*2+min(2, n%7)); }
D
import std.stdio, std.algorithm, std.string; void main() { readln(); auto s = readln().strip(); int[string] d; foreach (i; 0 .. s.length - 1) { d[s[i .. i+2]]++; } int maxCount = 0; string maxStr = ""; foreach (str, count; d) { if (count > maxCount) { maxCount = count; maxStr = str; } } writeln(maxStr); }
D
import std.stdio, std.conv, std.string; import std.algorithm, std.array, std.container, std.typecons; import std.numeric, std.math; import core.bitop; T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T)(in string str) { return str.split.to!(T[]); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } long mod = pow(10, 9) + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } struct UnionFind { void init(long n) { par = new long[](n); foreach (i; 0..n) par[i] = i; } long root(long i) { return par[i] == i ? i : (par[i] = root(par[i])); } bool same(long i, long j) { return root(i) == root(j); } void unite(long i, long j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; } long[] par; } void main() { auto H = RD!long; auto W = RD!long; auto D = RD!long; auto A = new long[2][](H*W); foreach (i; 0..H) { foreach (j; 0..W) { auto num = RD!long; A[num] = [i, j]; } } auto Q = RD!long; auto L = new long[](Q); auto R = new long[](Q); foreach (i; 0..Q) { L[i] = RD!long; R[i] = RD!long; } auto dp = new long[](H*W+1); foreach (i; 0..H*W) { auto dist1 = abs(A[i+D][0] - A[i][0]); auto dist2 = abs(A[i+D][1] - A[i][1]); dp[i+D] = dp[i] + dist1 + dist2; } foreach (i; 0..Q) { writeln(dp[R[i]] - dp[L[i]]); } stdout.flush(); }
D
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math; import core.stdc.stdio; // foreach, foreach_reverse, writeln void main() { int q; scanf("%d", &q); foreach (i; 0..q) { int a, b; scanf("%d%d", &a, &b); if (a > b) swap(a,b); int l = 0, r = b; while (l+1 < r) { int x = (l+r)/2; bool judge(long a, long b, long x) { // (x..1) * (a+1..a+x) < a*b if (x*(a+1) >= a*b) return false; if (1*(a+x) >= a*b) return false; long l = (x+a+1)/2; long r = (x+a+1)-l; if (l > x) return true; if (r < a+1) return true; if (l*r >= a*b) return false; return true; } if (judge(a, b, x)) l = x; else r = x; } writeln(l + (a-1)); } }
D
import std; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { auto S = sread(); long N = S.length; auto dp = new long[][](2, 2019); // dp[0][0] = 1; long ans; foreach (i; 0 .. N) { dp[(i + 1) & 1][] = 0; dp[(i + 1) & 1][(S[i] - '0')]++; foreach (j; 0 .. 2019) { long x = (j * 10) + (S[i] - '0'); dp[(i + 1) & 1][x % 2019] += dp[i & 1][j]; } // if (dp[i + 1][0] != 0) // { // writeln(" ", i); // } ans += dp[(i + 1) & 1][0]; } writeln(ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; int[] AS; foreach (_; 0..N) AS ~= readln.chomp.to!int; int[] lis; foreach (a; AS) { if (lis.empty || lis[$-1] < a) { lis ~= a; continue; } else if (lis[0] >= a) { lis[0] = a; continue; } size_t l, r = lis.length-1; while (l+1 < r) { auto m = (l+r)/2; if (lis[m] >= a) { r = m; } else { l = m; } } lis[r] = a; } writeln(lis.length); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp.to!int; auto s = readln.chomp.split; int[string] cnt; foreach (e; s) { cnt[e]++; } if (cnt.keys.length == 3) { writeln("Three"); } else { writeln("Four"); } }
D
import std.stdio, std.string, std.conv; import std.array, std.algorithm, std.range; void main() { foreach(s;stdin.byLine()) { auto m = s.split().map!(to!int); auto a=m[0], b=m[1], n=m[2]; int t=0; foreach(_;0..n) a=a%b*10,t+=a/b; writeln(t); } }
D