code
stringlengths
4
1.01M
language
stringclasses
2 values
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n, r; rd(n, r); auto as = readln.split.to!(int[]); auto bs = new int[](n); foreach (int i, a; as) { if (a) { foreach (int j; max(0, i - r + 1) .. min(n, i + r)) { bs[j]++; } } } if (bs.find(0).length > 0) { writeln(-1); return; } int num = reduce!"a+b"(as); foreach (int i, b; bs) { if (as[i]) { if (b >= 2) { bool ok = true; foreach (int j; max(0, i - r + 1) .. min(n, i + r)) { ok &= bs[j] >= 2; } if (ok) { num--; foreach (int j; max(0, i - r + 1) .. min(n, i + r)) { bs[j]--; } } } } } writeln(num); } 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; import std.algorithm; import std.conv; import std.string; import std.array; import std.math; void main(){ readln(); int[] monster = readln().chomp.split.map!(to!int).array; int min_mon; while(true){ monster = monster.uniq.array; if(monster.length == 1){break;} min_mon = monster.reduce!min; monster = monster.map!(x=>x%min_mon==0?min_mon:x%min_mon).array; } monster[0].writeln; }
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; void main() { auto k = readln.chomp.to!long; writeln((k/2) * (k - (k/2))); }
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() { int[4] type; foreach (line; stdin.byLine) { auto st = line.split(","); if (st[1] == "A") type[0]++; else if (st[1] == "B") type[1]++; else if (st[1] == "O") type[3]++; else type[2]++; } foreach (t; type) { t.writeln; } }
D
void main() { int[] tmp = readln.split.to!(int[]); int n = tmp[0], k = tmp[1]; int[] a = readln.split.to!(int[]); int m = a.countUntil(1).to!int; int l = (m + k - 2) / (k - 1); m = max(m, (k-1)*l); int r = (n - m + k - 3) / (k - 1); writeln(l + r); } 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 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); if (N == 1) { writeln(A == B ? 1 : 0); return; } if (B < A) { writeln(0); return; } writeln((B * (N - 1) + A) - (A * (N - 1) + B) + 1); }
D
void main() { writeln(readln.chomp.count('9') ? "Yes" : "No"); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio; import std.range; import std.array; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.container; import std.typecons; import std.random; import std.math; import core.time; void main() { int n = readln.chomp.to!int; int[] a = 0 ~ readln.chomp.split.map!(to!int).array ~ 0; int sum = 0; int prev = 0; foreach (v; a) { sum += abs = prev - v; prev = v; } foreach (i; iota(1,a.length-1)) { int v = a[i]; int sub = sum; sub -= abs = v - a[i - 1]; sub -= abs = v - a[i + 1]; sub += abs = a[i - 1] - a[i + 1]; sub.writeln; } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 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, "x", long, "y"); alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less); void main() { auto s = sread(); // s[0 .. ($ - 1) / 2].writeln(); // s[($ + 1) / 2 .. $].writeln(); if (s.isreverse() && s[0 .. ($ - 1) / 2].isreverse() && s[($ + 1) / 2 .. $].isreverse()) writeln("Yes"); else writeln("No"); } bool isreverse(string t) { bool ret = true; foreach (i; iota(t.length / 2)) { ret &= (t[i] == t[$ - 1 - i]); } return ret; } 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.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(){ int n = read.to!int; int[] as; foreach(i; 0 .. n) as ~= read.to!int; int f = 0; int last = -10; foreach(a; as){ if(last < a){ if(f == 1){ "NO".writeln; return; } } if(last > a){ if(f == 0) f = 1; } last = a; } "YES".writeln; return; }
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() { // 日本語文字列のテスト int w = readint; int n = readint; auto xs = new int[w + 1]; for (int i = 0; i < xs.length; i++) xs[i] = i; for (int i = 0; i < n; i++) { auto ab = readln.chomp.split(",").to!(int[]); int a = ab[0], b = ab[1]; swap(xs[a], xs[b]); } foreach (x; xs[1 .. $]) writeln(x); }
D
import std.stdio, std.array, std.algorithm, std.string, std.conv, std.math; void main() { int n = readln.chomp.to!int; int[int] chie; auto b = map!(x => x.to!int)(readln.chomp.split); int res = 0; foreach (x; b) { ++chie[x]; } foreach (x, cnt; chie) { if (cnt >= x) { res += min(cnt - x, cnt); } else { res += cnt; } } writeln(res); }
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(){ int n = read.to!int; int k = read.to!int; if(n % 2 == k % 2) ((n + k) / 2).writeln; else "IMPOSSIBLE".writeln; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 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() { auto s = sread(); s[0 .. $ - "FESTIVAL".length].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.array, std.conv, std.algorithm, std.string, std.numeric, std.range; void main() { auto n = readln.strip.to!int; foreach(i; 0..n){ readln.chomp.replace("Hoshino", "Hoshina").writeln(); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nk = readln.split.to!(int[]); auto N = nk[0]; auto K = nk[1]; int r; while (N) { ++r; N /= K; } writeln(r); }
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; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; // dfmt on void main() { long N = lread(); auto W = aryread(); long ans = long.max; foreach (x; 0 .. N) { ans = ans.min(abs(W[0 .. x + 1].sum() - W[x + 1 .. $].sum())); } writeln(ans); }
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; auto p = readln.chomp.split.map!(to!int).array; int cnt; foreach (i; 0..p.length-1) { if (i+1 == p[i]) { swap(p[i], p[i+1]); cnt++; } } if (p.length == p[$-1]) { swap(p[$-2], p[$-1]); cnt++; } cnt.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; ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias t = Tuple!(string, "name", long, "y"); void main() { auto n = lread(); auto a = aryread(); ulong check; foreach(e; a) { check ^= e; } if(check) writeln("No"); else writeln("Yes"); } 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; } }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string; const int[] dr = [0, 0, 1, -1]; const int[] dc = [1, -1, 0, 0]; void main() { auto s = readln.split.map!(to!int); auto H = s[0]; auto W = s[1]; auto A = H.iota.map!(_ => readln.chomp).array; auto uf = new UnionFind(H*W); foreach (r; 0..H) { foreach (c; 0..W) { foreach (k; 0..4) { int nr = r + dr[k]; int nc = c + dc[k]; if (nr < 0 || nr >= H || nc < 0 || nc >= W || A[r][c] == A[nr][nc]) continue; uf.unite(nr*W+nc, r*W+c); } } } long ans = 0; auto cnt = new long[][](H*W, 2); foreach (i; 0..H*W) { int k = uf.find(i); cnt[k][A[i/W][i%W]=='#'] += 1; } foreach (i; 0..H*W) { ans += cnt[i][0] * cnt[i][1]; } ans.writeln; } class UnionFind { int N; int[] table; this(int n) { N = n; table = new int[](N); fill(table, -1); } int find(int x) { return table[x] < 0 ? x : (table[x] = find(table[x])); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (table[x] > table[y]) swap(x, y); table[x] += table[y]; table[y] = x; } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto H = RD; auto W = RD; auto A = new long[][](H, W); auto B = new long[][](H, W); foreach (y; 0..H) { A[y] = RDA; } foreach (y; 0..H) { B[y] = RDA; } auto dp = new bool[][][](H, W, 160*(H+W)); dp[0][0][A[0][0] - B[0][0] + 80*(H+W)] = true; dp[0][0][B[0][0] - A[0][0] + 80*(H+W)] = true; foreach (y; 0..H) { foreach (x; 0..W) { long d1, d2; if (y != H-1) d1 = A[y+1][x] - B[y+1][x]; if (x != W-1) d2 = A[y][x+1] - B[y][x+1]; foreach (key; 0..dp[y][x].length) { if (dp[y][x][key] == false) continue; debug writeln(y, ":", x, " ", cast(long)key - 80*(H+W)); if (y != H-1) { dp[y+1][x][key+d1] = true; dp[y+1][x][key-d1] = true; } if (x != W-1) { dp[y][x+1][key+d2] = true; dp[y][x+1][key-d2] = true; } } } } long ans = long.max; foreach (key; 0..dp[H-1][W-1].length) { if (dp[H-1][W-1][key] == false) continue; ans.chmin(abs(cast(long)key - 80*(H+W))); } writeln(ans); stdout.flush; debug readln; }
D
import std.stdio; import std.string; import std.conv; immutable MAX_INPUT = 1000; immutable MAX_COUNT = 1000; int main(string[] argv) { int Count; string[] Ans; Count = readln().chomp().to!int(); if(Count <= MAX_COUNT){ for(int i = 0;i < Count;i++){ string[] tmp = readln().chomp().split(" "); int[3] rect; rect[0] = tmp[0].to!int(); rect[1] = tmp[1].to!int(); rect[2] = tmp[2].to!int(); foreach(int j;rect){ if (!judge(j)) break; } for(int j = 0;j < 3;j++){ if(rect[0] < rect[j]){ int t = rect[0]; rect[0] = rect[j]; rect[j] = t; } } if(isTriangle(rect[1],rect[2],rect[0])){ Ans ~= "YES"; }else{ Ans ~= "NO"; } } } foreach(string str;Ans){ writeln(str); } return 0; } bool judge(int x){ if(1 <= x && x <= MAX_INPUT){ return true; } return false; } bool isTriangle(int x,int y,int z){ x = x * x; y = y * y; z = z * z; if (x + y == z){ return true; } return false; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; // dfmt on void main() { auto S = sread(); auto dp = new long[][](S.length + 1, 13); dp[0][0] = 1; foreach (i; 0 .. S.length) { if (S[i] == '?') { foreach (j; 0 .. 13) { foreach (k; 0 .. 10) { auto m = (j * 10 + k) % 13; dp[i + 1][m] = (dp[i + 1][m] + dp[i][j]) % MOD; } } } else { foreach (j; 0 .. 13) { auto k = S[i] - '0'; auto m = (j * 10 + k) % 13; dp[i + 1][m] = (dp[i + 1][m] + dp[i][j]) % MOD; } } } writeln(dp[S.length][5]); }
D
void main() { auto N = ri, A = ri; (N % 500 <= A ? "Yes" : "No").writeln; } // =================================== import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(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 S = RD!string; auto T = RD!string; writeln(S == T[0..$-1] && S.length+1 == T.length ? "Yes" : "No"); stdout.flush; debug readln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; alias Pair = Tuple!(int, "a", int, "b"); void main() { string s, t; scan(s); scan(t); auto vs = new int[][](26, 0); auto vt = new int[][](26, 0); foreach (int i, ch ; s) { vs[ch - 'a'] ~= i; } foreach (int i, ch ; t) { vt[ch - 'a'] ~= i; } vs.sort(); vt.sort(); writeln(vs == vt ? "Yes" : "No"); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
//dlang template---{{{ import std.stdio; import std.conv; import std.string; import std.array; import std.algorithm; import std.typecons; import std.math; import std.range; // MIT-License https://github.com/kurokoji/nephele class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } //Digit count---{{{ int DigitNum(int num) { int digit = 0; while (num != 0) { num /= 10; digit++; } return digit; } //}}} //}}} void main() { Scanner sc = new Scanner; int H, W; int h, w; sc.scan(H, W); sc.scan(h, w); writeln((H * W) - ((h * W) + ((H - h) * w))); }
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 S = RD!string; auto len = S.length; long ans; foreach (i; 1..len) { auto len2 = len - i; if (len2 % 2 == 1) continue; bool ok = true; foreach (j; 0..len2/2) { if (S[j] != S[len2/2+j]) { ok = false; break; } } if (ok) { ans = len2; break; } } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto S = RD!string; long ans, cnt; foreach (c; S) { if (c == 'A' || c == 'C' || c == 'G' || c == 'T') { ++cnt; } else { ans = max(ans, cnt); cnt = 0; } } ans = max(ans, cnt); writeln(ans); 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; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ int n = read.to!int; long[] as; foreach(i; 0 .. n + 1) as ~= read.to!long; long[] bs; foreach(i; 0 .. n) bs ~= read.to!long; long ans; foreach(i; 0 .. n){ long k; if(bs[i] <= as[i]) k = bs[i]; else k = as[i]; ans += k, bs[i] -= k, as[i] -= k; if(bs[i] <= as[i + 1]) k = bs[i]; else k = as[i + 1]; ans += k, bs[i] -= k, as[i + 1] -= k; } ans.writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto X = RD; auto Y = RD; bool ans = inside(Y, X*2, X*4+1); ans &= Y % 2 == 0; writeln(ans ? "Yes" : "No"); stdout.flush; debug readln; }
D
import std.algorithm; import std.conv; import std.math; import std.range; import std.stdio; import std.string; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto s = readln.strip; int res = s.length.to !(int); int a = 0, b = 0; foreach (c; s) { if (c == 'A') { a += 1; } else { b += 1; if (a > 0 && b > 0) { a -= 1; b -= 1; res -= 2; } else if (b >= 2) { b -= 2; res -= 2; } } } writeln (res); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //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 K = RD; auto S = RD!string; if (S.length <= K) writeln(S); else { writeln(S[0..K], "..."); } stdout.flush; debug readln; }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { auto N = lread(); long ok = (N + 1); long ng = -1; while (abs(ok - ng) > 1) { long mid = (ok + ng) / 2; if (((mid + 1) * ((mid + 1) + 1)) / 2 >= N) { ok = mid; } else { ng = mid; } } // writeln(ok + 1); foreach (i; 1 .. (ok + 1) + 1) { // writeln('t', (ok + 1) * ((ok + 1) + 1) / 2 - i); if ((ok + 1) * ((ok + 1) + 1) / 2 - i == N) { continue; } else { writeln(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
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std; // dfmt on void main() { long N = lread(); long a = 1, b = 1; foreach (_; 0 .. N) { long T, A; scan(T, A); long ng = 0; long ok = (1000000000000000000 + max(T, A) - 1) / max(T, A); dprint((1000000000000000000 - 1 + max(T, A)), ok, T, A); while (abs(ng - ok) > 1) { long m = (ok + ng) / 2; long x = T * m; long y = A * m; if (x < a || y < b) { ng = m; } else { ok = m; } } a = T * ok; b = A * ok; dprint(ok, a, b); } writeln(a + b); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp; if (n[0] == n[2]) { writeln("Yes"); } else { writeln("No"); } }
D
import std.stdio; import std.conv; import std.array; void main() { auto reader = readln.split; int A = reader[0].to!int; int B = reader[1].to!int; int C = reader[2].to!int; if ((A < C && C < B) || (A > C && C > B)) writeln("Yes"); else writeln("No"); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp.to!int; auto p = readln.chomp.split.to!(int[]); int cnt; foreach (i; 1..p.length-1) { if (p[i-1] < p[i] && p[i] < p[i+1] || p[i-1] > p[i] && p[i] > p[i+1]) { cnt++; } } cnt.writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new string[](t); foreach (ti; 0..t) { auto s1 = RD!string; auto s2 = RD!string; auto len = lcm(s1.length, s2.length); while (ans[ti].length < len) { ans[ti] ~= s1; } string s3; while (s3.length < len) { s3 ~= s2; } foreach (i; 0..s3.length) { if (ans[ti][i] != s3[i]) { ans[ti].length = 0; break; } } } foreach (e; ans) { writeln(e.empty ? "-1" : e); } stdout.flush; debug readln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto edges = new int[][](N); foreach (_; 0..M) { s = readln.split.map!(to!int); auto a = s[0] - 1; auto b = s[1] - 1; edges[a] ~= b; edges[b] ~= a; } auto used = new bool[](N); foreach (i; edges[0]) { foreach (j; edges[i]) { used[j] = true; } } writeln(used[N - 1] ? "POSSIBLE" : "IMPOSSIBLE"); }
D
// import chie template :) {{{ import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint; // }}} void main() { bool[101010] e; e[2 .. 101010] = true; e[0 .. 2] = false; foreach (i; 2 .. 101010) { if (e[i]) { for (int j = i * i; j < e.length; j += i) e[j] = false; } } long[101010] sum; for (int i = 1; i < e.length; i += 2) { if (e[i] && e[(i + 1) / 2]) sum[i] = 1; } foreach (i; 1 .. 101010) { sum[i] += sum[i - 1]; } int q = readln.chomp.to!int; foreach (i; 0 .. q) { int[] ra = readln.chomp.split.to!(int[]); writeln(sum[ra[1]] - sum[ra[0] - 1]); } }
D
void main() { long n, a, b; rdVals(n, a, b); long result = modPow(2, n) - 1; long fa = 1, fb = 1; foreach (i; 0 .. b) { if(i < a) fa = fa * (n - i) % mod; fb = fb * (n - i) % mod; } long[] finv = new long[b+1]; long[] inv = new long[b+1]; finv[0] = 1, finv[1] = 1; inv[1] = 1; foreach (i; 2 .. b+1) { inv[i] = mod - inv[mod%i] * (mod / i) % mod; finv[i] = finv[i-1] * inv[i] % mod; } long ca = fa * finv[a] % mod; long cb = fb * finv[b] % mod; result = (result + mod - ca) % mod; result = (result + mod - cb) % mod; result.writeln; } long modPow(long x, long y) { long result = 1; while (y > 0) { if (y & 1) result = result * x % mod; x = x * x % mod; y >>= 1; } return result; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "x", long, "y", long, "cost"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { auto n = lread(); auto s = sread(), c = "RGB"; long color = 3, count; auto pair = new long[][](n + 1, color); foreach(i; iota(n)) { foreach (j; iota(color)) { pair[i + 1][j] = pair[i][j] + (s[i] == c[j]); } } foreach (i; iota(n - 2)) { foreach (j; iota(i + 1, n - 1)) { if(s[i] == s[j]) continue; auto tmp = color.iota.sum() - c.countUntil(s[i]) - c.countUntil(s[j]); count += pair[n][tmp] - pair[j + 1][tmp]; if(j + (j - i) < n && c[tmp] == s[j + (j - i)]) count--; } } count.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.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto S = readln.chomp; auto N = S.length.to!int; int ans = 0; for (int p = 0, q = N-1; p < q; ) { if (S[p] == S[q]) { p += 1; q -= 1; } else if (S[p] == 'x') { ans += 1; p += 1; } else if (S[q] == 'x') { ans += 1; q -= 1; } else { writeln(-1); return; } } ans.writeln; }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.math : abs; import core.stdc.stdlib : exit; void main() { auto A = to!int(split(readln())[0]); auto B = to!int(split(readln())[0]); auto C = to!int(split(readln())[0]); auto D = to!int(split(readln())[0]); writeln( (A < B ? A : B) + (C < D ? C : D) ); }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto s = new char[][](2, n); foreach (i; 0 .. 2) s[i] = readln.chomp.to!(char[]); const long mod = 10 ^^ 9 + 7; auto dp = new long[][](2, n); if (s[0][0] == s[1][0]) dp[0][0] += 3; else dp[1][1] += 6; foreach (i; 1 .. n) { if (s[0][i] == s[1][i]) { dp[0][i] = (dp[0][i - 1] * 2 + dp[1][i - 1]) % mod; } else if (i >= 2) { dp[1][i] = (dp[0][i - 2] * 2 + dp[1][i - 2] * 3) % mod; } } writeln((dp[0][n - 1] + dp[1][n - 1]) % mod); } 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
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); if (n % 3 < 2) { writeln(1, " ", 1, " ", n - 2); } else { writeln(1, " ", 2, " ", n - 3); } } 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.array, std.conv; void main() { int[][][] bld = new int[][][](4, 3, 10); int n = readln.chomp.to!int; foreach (i; 0 .. n) { int[] tmp = readln.chomp.split.to!(int[]); int b = tmp[0], f = tmp[1], r = tmp[2], v = tmp[3]; bld[b-1][f-1][r-1] += v; } foreach (i; 0 .. 4) { foreach (j; 0 .. 3) { foreach (k; 0 .. 10) { write(" ", bld[i][j][k]); } writeln; } if (i != 3) "####################".writeln; } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } T binarySearch(alias pred, T)(T ok, T ng) { while (abs(ok-ng) > 1) { auto mid = (ok+ng)/2; if (unaryFun!pred(mid)) ok = mid; else ng = mid; } return ok; } void main() { auto t = RD!int; auto ans = new int[](t); foreach (ti; 0..t) { auto n = RD!int; while (true) { auto r = binarySearch!((long a) => a*(a+1) + a*(a-1)/2 <= n)(0L, 10L^^9); if (r == 0) break; ++ans[ti]; n -= r*(r+1) + r*(r-1)/2; } } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!long).array; long hi = 10^^9; long lo = -(10^^9); long calc(long b) { return N.iota.map!(i => abs(A[i] - (b+i+1))).sum; } while (hi - lo > 2) { long mid1 = (2*hi + lo) / 3; long mid2 = (hi + 2*lo) / 3; long v1 = calc(mid1); long v2 = calc(mid2); if (v1 >= v2) hi = mid1; else lo = mid2; } iota(lo, lo+4).map!(b => calc(b)).reduce!min.writeln; }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto a = new int[](n); foreach (i; 0 .. n) rd(a[i]); bool[int] map; foreach (e; a) { if (e in map) map.remove(e); else map[e] = true; } writeln(map.length); } 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; import std.string; import std.conv; import std.algorithm; void main() { auto s = readln.chomp; auto y = new int[s.length]; int begin, i; char mode = 'R'; foreach(c; s) { if (c != mode && mode == 'R') { y[i-1] += (i-begin+1)/2; y[i] += (i-begin+1)/2 + (i-begin+1)%2; begin = i; mode = 'L'; } else if (c != mode) { y[begin-1] += (i-begin-1)/2 + (i-begin-1)%2; y[begin] += (i-begin-1)/2; begin = i; mode = 'R'; } ++i; } y[begin-1] += (i-begin-1)/2 + (i-begin-1)%2; y[begin] += (i-begin-1)/2; y.map!(i => i.to!string).join(' ').write; }
D
void main() { long n, k; rdVals(n, k); long[] h = rdRow; long[] dp = new long[n]; dp[] = inf; dp[0] = 0; foreach (i; 1 .. n) { foreach (j; 1 .. k+1) { if (i - j < 0) break; dp[i] = min(dp[i], dp[i-j]+abs(h[i-j]-h[i])); } } dp[n-1].writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; enum double eps = 1.0e-9; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
// 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 ans = new long[](25_000_000); foreach (x; 1 .. 200) foreach (y; 1 .. 200) foreach (z; 1 .. 200) { ans[x * x + y * y + z * z + x * y + y * z + z * x]++; } foreach (i; 1 .. N + 1) { writeln(ans[i]); } }
D
import std.stdio; import std.string; import std.algorithm; void main() { int n; scanf("%d\n", &n); int incl, lb, ra, half; for(size_t i;i < n; ++i){ auto l = readln.chomp; incl += l.count("AB"); auto b = l[0] == 'B'; auto a = l[$-1] == 'A'; if (a) ++ra; if (b) ++lb; if ((a || b) && (a != b)) ++half; } if (min(ra,lb) > 0 && half == 0) --incl; write(incl + min(lb, ra)); }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto a = readln.split.to!(int[]); int o, t, f; foreach (e; a) { if (e % 4 == 0) f++; else if (e % 2 == 0) t++; else o++; } t %= 2; o += t; if (o <= f + 1) { writeln("Yes"); } else { writeln("No"); } } void rd(T...)(ref T x) { import std.stdio, std.string, std.conv; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { char[] r; foreach (c; readln.chomp) { if (c == '?') { r ~= 'D'; } else { r ~= c; } } writeln(r); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto X = readln.chomp.to!int; writeln(X == 7 || X == 5 || X == 3 ? "YES" : "NO"); }
D
import std.stdio, std.conv, std.string; import std.algorithm, std.array, std.container; import std.numeric, std.math; import core.bitop; T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; } string RDR()() { return readln.chomp; } long mod = pow(10, 9) + 7; 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 ABCD = RD!string; long[4] a; foreach (i, c; ABCD) { a[i] = [c].to!long; } foreach (i; 0..0b1000) { auto r = a[0]; string ans = a[0].to!string; foreach (j; 0..3) { if ((i & (1 << j))) { r += a[j+1]; ans ~= "+"; } else { r -= a[j+1]; ans ~= "-"; } ans ~= a[j+1].to!string; } if (r == 7) { writeln(ans ~ "=7"); break; } } stdout.flush(); }
D
void main() { auto N = readAs!ulong; ulong a = 0; if(N % 2 == 1) { writeln(0); return; } for(ulong i = 10; N >= i; i *= 5) a += N / i; a.writeln; } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.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() { string s; readV(s); auto r = 1000; foreach (i; 0..s.length-2) r = min(r, (s[i..i+3].to!int - 753).abs); writeln(r); }
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 int INF = 1 << 29; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!long).array; auto seppen = new long[](N); foreach (i; 0..N) seppen[i] = A[i] - i; int[long] cnt; foreach (a; seppen) cnt[a] += 1; long ans = 0; foreach (i, a; A) { long b = -2 * i.to!long - seppen[i]; if (b in cnt) ans += cnt[b]; } ans.writeln; }
D
import std.stdio; import std.range; import std.array; import std.algorithm; import std.string; void main(){ auto input_1 = readln.chomp; auto input_2 = readln.chomp; auto input_3 = readln.chomp; auto answer = "" ~ input_1[0] ~ input_2[1] ~ input_3[2]; writeln(answer); }
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 s1 = RD!string; auto s2 = RD!string; auto s3 = RD!string; string ans = [cast(char)(s1[0]-'a'+'A'), cast(char)(s2[0]-'a'+'A'), cast(char)(s3[0]-'a'+'A')]; writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto N = readln.chomp.to!int; auto A = N.iota.map!(_ => readln.split.map!(to!long).array).array; bool ok = true; auto unused = new int[][](N, N); foreach (i; 0..N) foreach (j; 0..N) foreach (k; 0..N) if (A[j][k] > A[j][i] + A[i][k]) ok = false; else if (i != j && i != k && A[j][k] == A[j][i] + A[i][k]) unused[j][k] = true; if (!ok) { writeln(-1); } else { long ans = 0; foreach (i; 0..N) foreach (j; i+1..N) if (!unused[i][j]) ans += A[i][j]; ans.writeln; } }
D
// import chie template :) {{{ import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons; // }}} // tbh.scanner {{{ class Scanner { import std.stdio; import std.conv : to; import std.array : split; import std.string : chomp; private File file; private dchar[][] str; private uint idx; this(File file = stdin) { this.file = file; this.idx = 0; } private dchar[] next() { if (idx < str.length) { return str[idx++]; } dchar[] s; while (s.length == 0) { s = file.readln.chomp.to!(dchar[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(uint len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } } // }}} void main() { auto cin = new Scanner; int h = cin.next!int, w = cin.next!int, d = cin.next!int; auto imos = new long[][](d + 2); int[] y = new int[h * w + 1], x = new int[h * w + 1]; foreach (i; 1 .. h + 1) { foreach (j; 1 .. w + 1) { int c = cin.next!int; y[c] = i; x[c] = j; } } foreach (i; 1 .. d + 1) { imos[i] ~= 0; int dy = y[i], dx = x[i]; int idx = 0; for (int j = i + d; j < h * w + 1; j += d) { imos[i] ~= imos[i][idx++] + abs(dy - y[j]) + abs(dx - x[j]); dy = y[j]; dx = x[j]; } } int q = cin.next!int; foreach (i; 0 .. q) { int l = cin.next!int, r = cin.next!int; int idx = l % d == 0 ? d : l % d; if (idx == d) { writeln(imos[idx][r / d - 1] - imos[idx][l / d - 1]); } else { writeln(imos[idx][r / d] - imos[idx][l / d]); } } }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int q; rd(q); while (q--) { long n, a, b; rd(n, a, b); auto ans = n * a; if (n & 1) { ans = min(ans, n / 2 * b + a); } else { ans = min(ans, n / 2 * b); } writeln(ans); } } 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
void main() { auto S = rs; auto N = S.length; auto arr = new int[](N); arr[0] = 1; foreach(i; 0..N-1) arr[i+1] = (10*arr[i]) % 2019; debug arr.writeln; int[int] m; int tmp = S[$-1] - '0'; m[tmp]++; foreach(i; 0..N) { tmp = tmp + arr[i] * (S[$-i-1] - '0'); debug tmp.writeln; tmp %= 2019; m[tmp]++; } ulong res; debug m.writeln; foreach(v; m.values) res += v * (v-1) / 2; res.writeln; } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std; 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 杯 = new long[](n); 杯[] = 1; // writeln(杯); auto 色 = new bool[](n); //赤色true // writeln(色); 色[0] = true; // writeln(色); foreach (_; 0 .. m) { long x, y; scan(x, y); // writeln(x, y); // writeln("杯", 杯); 杯[x - 1] -= 1; 杯[y - 1] += 1; // writeln(杯); if (色[x - 1]) { 色[y - 1] = true; } if (杯[x - 1] == 0) { 色[x - 1] = false; } // writeln("色", 色); } long cnt; foreach (i; 0 .. n) { if ((杯[i] > 0) && (色[i] == true)) { cnt += 1; } } writeln(cnt); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.stdio; import std.string; void main(){ writeln(toUpper(chomp(readln()))); }
D
import std.stdio; void main() { foreach (i; 0..1000) { writeln("Hello World"); } }
D
import std.stdio; import std.range; import std.array; import std.algorithm; import std.string; import std.conv; void main(){ immutable n = readln.chomp.to!int; auto d_x = readln.chomp.split.map!(to!int); auto days = d_x[0]; auto rest = d_x[1]; int[] as; as.length = n; as.each!((ref a){ a = readln.chomp.to!int; }); auto answer = rest; iota(0,n).each!((p){ auto day = 1; while(day <= days){ answer++; day += as[p]; } }); answer.writeln; }
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.to!(int[]),r=Q[0],g=Q[1],b=Q[2]; if((100*r+10*g+b)%4==0)writeln("YES"); else writeln("NO"); }
D
import std.stdio, std.conv, std.string, std.algorithm, std.range; void main() { auto N = readln.split[0].to!int; auto A = readln.split.to!(int[]); auto B = readln.split.to!(int[]); auto C = readln.split.to!(int[]); int result = 0; int before = -1; foreach(a; A) { result += B[a - 1]; if (before != -1 && a - before == 1) { result += C[before - 1]; } before = a; } result.writeln; }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { auto s = readln.chomp.to!(char[]); int k; scan(k); int N = s.length.to!int; foreach (i ; 0 .. N) { if (s[i] == 'a') continue; int x = 'z' - s[i] + 1; if (x <= k) { k -= x; s[i] = 'a'; } } k %= 26; if (k > 0) { s[$ - 1] = (s[$ - 1] + k).to!char; } writeln(s); } 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; void main() { writeln("ABC" ~ readln()); }
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 S = RD!string; if (S[0] != 'A' || !inside(S[1]-'a', 0, 26) || !inside(S[$-1]-'a', 0, 26)) { debug writeln("A", " ", S[1]-'a'); writeln("WA"); } else { debug writeln("B"); bool ok = true; long cnt; foreach (c; S) { if (c == 'C') ++cnt; else if (!inside(S[1]-'a', 0, 26)) ok = false; } debug writeln(cnt, " ", ok); if (cnt != 1) ok = false; writeln(ok ? "AC" : "WA"); } stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.string, std.algorithm, std.array; void main() { int n, m; scanf("%d %d", &n, &m); int[] map; map.length=n; for (int i = 0; i < m; i++) { int from, to; scanf("%d %d", &from, &to); from--; to--; map[from]++; map[to]++; } foreach(routenum; map) { writeln(routenum); } }
D
import std.stdio; import std.algorithm; import std.math; import std.conv; import std.string; import std.numeric; T readNum(T)(){ return readStr.to!T; } T[] readNums(T)(){ return readStr.split.to!(T[]); } string readStr(){ return readln.chomp; } void main(){ auto input = readNums!long; auto a = input[0], b = input[1], c = input[2], d = input[3]; auto lcm = c * d / gcd(c, d); long first, last, num; foreach(j, k; [c, d, lcm]){ first = a + k - a % k; if(a % k == 0) first -= k; last = b - b % k; num += (j == 2 ? -1 : 1) * ((last - first) / k + 1); } writeln(b - a + 1 - num); }
D
import std.algorithm; import std.array; import std.ascii; import std.bigint; import std.complex; import std.container; import std.conv; import std.functional; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } void readlnTo(T...)(ref T t) { auto s = readln().split(); assert(s.length == t.length); foreach(ref ti; t) { ti = s[0].to!(typeof(ti)); s = s[1..$]; } } const real eps = 1e-10; void main(){ auto n = readLong(); long v; foreach(i; iota(n)) { v += readLong(); } writeln(v/n); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio; void main() { auto S = readln.chomp; auto K = readln.chomp.to!int; auto N = S.length.to!int; auto dp = new long[][][](N+1, 2, K+1); dp[0][0][0] = 1; foreach (i; 0..N) foreach (j; 0..2) foreach (k; 0..K+1) foreach (d; 0..10) { if (d != 0 && k == K) continue; if (j == 0 && d > S[i] - '0') continue; dp[i+1][j||(d<S[i]-'0')][k+(d!=0)] += dp[i][j][k]; } long ans = dp[N][0][K] + dp[N][1][K]; ans.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 S = RD!string; long ans = 700; foreach (c; S) { if (c == 'o') ans += 100; } writeln(ans); stdout.flush(); debug readln(); }
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; bool calc(string s) { int[char] d; foreach (c; s) { d[c]++; } return d.values.all!(e => e % 2 == 0); } void main() { auto s = read!string; writeln(calc(s) ? "Yes" : "No"); }
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, t; readV(a, b, t); writeln(t/a*b); }
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 main() { int k; scan(k); auto a = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]; writeln(a[k-1]); } 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;
D
void main() { long n = rdElem; void dfs(string s, long len, long cnt) { if (len == n) { s.writeln; return; } foreach (i; 0 .. cnt+2) { long num = max(cnt, (s[len-1] - 'a') + 1); if (i <= num) dfs(s~lowercase[i], len+1, num); } } dfs("a", 1, 0); } 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 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 = 998244353; //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 N = RD; auto A = RDA; long[long] cnt; foreach (i; 0..N) { ++cnt[A[i]]; } long ans; foreach (key; cnt.keys) { auto v = cnt[key]; ans += v * (v-1) / 2; } foreach (i; 0..N) { auto v = cnt[A[i]]; writeln(ans - (v - 1)); } stdout.flush; debug readln; }
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() { string s; readV(s); auto c1 = s.length.iota.count!(i => s[i] != (i%2 ? '0' : '1')); auto c2 = s.length.iota.count!(i => s[i] != (i%2 ? '1' : '0')); writeln(min(c1, c2)); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; struct Spl{auto r="".splitter;auto rd(){r=readln.splitter;}auto bd(T)(ref T t){t=r.front.to!T;r.popFront;}} auto _s = Spl(); auto getV(T...)(ref T t){foreach(ref u;t)_s.bd(u);} auto getA(T)(size_t n,ref T t){t=new T(n);foreach(ref u;t)_s.bd(u);} auto readV(T...)(ref T t){_s.rd; getV(t);} void main() { int p, q, r; readV(p, q, r); writeln(min(p+q, q+r, r+p)); }
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(){ long n = scan!long; long ans; foreach(d; 1 .. n + 1){ long x = n / d; ans += d * x * (1 + x) / 2; log("d:", d, "x:", x, "ans:", ans); } ans.writeln; }
D
import std.stdio, std.string, std.algorithm, std.array; void main() { int t; scanf("%d", &t); getchar(); foreach(i; 0..t){ int n, s; scanf("%d %d", &n, &s); auto num_zero = (n - 1) / 2; writeln(s / (n - num_zero)); } }
D
import std.stdio; import std.conv; import std.algorithm; import std.string; int main(string[] args) { uint i; uint[] hills; hills.length = 10; uint t; string buf; uint count = 0; while ((buf = readln()) != null) { hills[count] = to!(uint)(strip(buf)); count++; } sort!("a > b")(hills); for (i = 0; i < 3; i++) { writeln(hills[i]); } return 0; }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; import std.string; import std.uni; import std.range; import std.algorithm; void main() { size_t k = 0; auto c = readln.strip.toLower; while (!stdin.eof) { auto a = readln.strip.toLower; if (a == "END_OF_TEXT") break; k += a.split(' ').count(c); } writeln(k); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); ulong bignum = 1_000_000_007; alias Pair = Tuple!(long, "begin", long, "end"); 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, d; scan(n, d); auto dims = new long[][](n, d); foreach (ref e; dims[]) { e = aryread(); } long count; long[] dist = [1]; long tmp; while (dist[tmp] < (40 ^^ 2) * 10) dist ~= (++tmp + 1) ^^ 2; foreach (e1; dims[]) { foreach (e2; dims[]) { auto s = iota(0, d).map!(x => (e1[x] - e2[x]) ^^ 2).sum; foreach (e3; dist) if (s == e3) count++; } } (count / 2).writeln(); }
D
void main() { long k, a, b; rdVals(k, a, b); long diff = k - a + 1; long c = a + (b - a) * (diff / 2) + diff % 2; max(k+1, c).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.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std; // dfmt on void main() { long L, R, D; scan(L, R, D); long ans; foreach (x; L .. R + 1) { if (x % D == 0) { ans++; // writeln(x); } } writeln(ans); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; import std.math; // math functions void main() { auto n = readln.chomp.to!size_t; auto a = readln.split.to!(long[]); auto c1 = new long[](n), c2 = new long[](n); c1[0] = a[0]; foreach (i; 1..n) c1[i] = a[i] + c1[i-1]; c2[n-1] = a[n-1]; foreach_reverse (i; 0..n-1) c2[i] = c2[i+1] + a[i]; auto ans = long.max; foreach (i; 0..n-1) ans = min(ans, (c1[i] - c2[i+1]).abs); writeln(ans); }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional, std.traits; import std.algorithm, std.container; import core.stdc.stdlib; void main() { auto M = scanElem; auto K = scanElem; if(K >= pow(2, M)){ writeln(-1); return; } if(M==0){ if(K==0){ writeln("0 0"); return; }else{ writeln(-1); return; } } if(M==1){ if(K==0) { writeln("0 0 1 1"); return; } writeln(-1); return; } if(K==0){ foreach(i; 0..pow(2, M)) { write(i); write(" "); write(i); write(" "); } writeln(""); return; } foreach(i; 0..pow(2, M)) { if(K==i)continue; write(i); write(" "); } write(K); write(" "); foreach_reverse(i; 0..pow(2, M)) { if(K==i)continue; write(i); write(" "); } write(K); write(" "); } long gcd(long a, long b) { if(b == 0) return a; return gcd(b, a % b); } class UnionFind{ UnionFind parent = null; void merge(UnionFind a) { if(same(a)) return; a.root.parent = this.root; } UnionFind root() { if(parent is null)return this; return parent = parent.root; } bool same(UnionFind a) { return this.root == a.root; } } void scanValues(TList...)(ref TList list) { auto lit = readln.splitter; foreach (ref e; list) { e = lit.fornt.to!(typeof(e)); lit.popFront; } } T[] scanArray(T = long)() { return readln.split.to!(T[]); } void scanStructs(T)(ref T[] t, size_t n) { t.length = n; foreach (ref e; t) { auto line = readln.split; foreach (i, ref v; e.tupleof) { v = line[i].to!(typeof(v)); } } } long scanULong(){ long x; while(true){ const c = getchar; if(c<'0'||c>'9'){ break; } x = x*10+c-'0'; } return x; } T scanElem(T = long)() { char[] res; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { res ~= cast(char) c; c = getchar; } return res.strip.to!T; } template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } struct Factor { long n; long c; } Factor[] factors(long n) { Factor[] res; for (long i = 2; i ^^ 2 <= n; i++) { if (n % i != 0) continue; int c; while (n % i == 0) { n = n / i; c++; } res ~= Factor(i, c); } if (n != 1) res ~= Factor(n, 1); return res; } long[] primes(long n) { if(n<2)return []; auto table = new long[n+1]; long[] res; for(int i = 2;i<=n;i++) { if(table[i]==-1) continue; for(int a = i;a<table.length;a+=i) { table[a] = -1; } res ~= i; } return res; } bool isPrime(long n) { if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (long i = 3; i ^^ 2 <= n; i += 2) if (n % i == 0) return false; return true; }
D
import std.stdio, std.conv, std.array,std.string,std.algorithm; void main() { auto input=readln.split; writeln(max(input[0].to!int*input[1].to!int,input[2].to!int*input[3].to!int)); }
D
void main() { long[] tmp = readln.split.to!(long[]); long x = tmp[0], y = tmp[1]; writeln(log2(y/x).to!long + 1); } 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