code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio; import std.conv; import std.string; import std.algorithm; import std.range; void main() { auto input = readln.split.to!(int[]); auto N = input[0]; auto M = input[1]; ulong X; ( 2 ^^ M * (M * 1900 + (N - M) * 100) ).writeln; }
D
import std.stdio, std.conv, std.string, std.algorithm, std.math, std.array, std.container, std.typecons; void main() { auto sw = readln.chomp.split.to!(int[]); if(sw[0]<=sw[1]) writeln("unsafe"); else writeln("safe"); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; void main() { int n, k; scan(n, k); writeln(n % k == 0 ? 0 : 1); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; enum R = [ "SUN": 7, "MON": 6, "TUE": 5, "WED": 4, "THU": 3, "FRI": 2, "SAT": 1 ]; void main() { writeln(R[readln.chomp]); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto n = RD!int; auto ans = new char[][](n, n); ans[0][0] = 'W'; ans[0][1] = 'B'; ans[0][2] = 'W'; ans[1][0] = 'B'; ans[2][0] = 'W'; ans[1][1] = 'W'; bool visit(int y, int x, int c) { bool ok; if (inside(y, 0, n) && inside(x, 0, n)) { if (ans[y][x] == char.init) { ans[y][x] = c == 0 ? 'B' : 'W'; ok = true; } } return ok; } int[3][] open = [[0,0,0], [0,1,1], [0,2,0], [1,0,1], [2,0,0], [1,1,0]]; while (!open.empty) { auto node = open.front; open.popFront; auto y = node[0]; auto x = node[1]; auto c = node[2]; if (visit (y-1, x-2, c)) open ~= [y-1, x-2, c^1]; if (visit (y+1, x-2, c)) open ~= [y+1, x-2, c^1]; if (visit (y-1, x+2, c)) open ~= [y-1, x+2, c^1]; if (visit (y+1, x+2, c)) open ~= [y+1, x+2, c^1]; if (visit (y-2, x-1, c)) open ~= [y-2, x-1, c^1]; if (visit (y-2, x+1, c)) open ~= [y-2, x+1, c^1]; if (visit (y+2, x-1, c)) open ~= [y+2, x-1, c^1]; if (visit (y+2, x+1, c)) open ~= [y+2, x+1, c^1]; } foreach (y; 0..n) { writeln(ans[y]); } stdout.flush(); debug readln(); }
D
void main() { string[] n = readln.split; n.sort!"a < b"; writeln(n.join == "1479" ? "YES" : "NO"); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.container; import std.typecons;
D
// 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);}} alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop; // dfmt on void main() { auto input = stdin.byLine.map!split.joiner; string S; S = input.front.to!string; input.popFront; string T; T = input.front.to!string; input.popFront; solve(S, T); } void solve(string S, string T) { iota(S.length - T.length + 1).map!(x => eval(S[x .. x + T.length], T)) .reduce!min().writeln; } long eval(string s, string t) { dprint(s, t); long ret; foreach (i; 0 .. s.length) { ret += s[i] != t[i]; } return ret; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto A = RD; auto B = RD; writeln(max(max(A+B, A * 2 - 1), B * 2 - 1)); stdout.flush(); debug readln(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; enum P = 10L^^9+7; bool[10^^3+1] PS; int[] QS; void prime_init() { PS[] = true; PS[0] = false; PS[1] = false; foreach (i; 2..10^^3+1) { if (PS[i]) { QS ~= i; auto x = i*2; while (x <= 10^^3) { PS[x] = false; x += i; } } } } long pow(long x, long n) { long y = 1; while (n) { if (n%2 == 1) y = (y * x) % P; x = x^^2 % P; n /= 2; } return y; } long inv(long x) { return pow(x, P-2); } void main() { auto N = readln.chomp.to!int; auto as = readln.split.to!(int[]); prime_init(); auto ps = new int[](10^^6+1); foreach (a; as) { int[int] qs; foreach (p; QS) { while (a%p == 0) { a /= p; if (p !in qs) qs[p] = 0; ++qs[p]; } } if (a != 1) qs[a] = 1; foreach (p, n; qs) ps[p] = max(ps[p], n); } long x = 1; foreach (i, n; ps) if (n) { x = x * pow(i.to!long, n) % P; } long[] bs; long r; foreach (a; as) r = (r + x * inv(a) % P) % P; writeln(r); }
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() { long A, B; scan(A, B); long n = A + B; writeln((n < 10) ? n.text : "error"); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.numeric; import std.stdio; import std.string; void main() { long n = readln.chomp.to!long; for (long cake = 0; cake <= 100; ++cake) { for (long donuts = 0; donuts <= 100; ++donuts) { if (cake * 4 + donuts * 7 == n) { writeln("Yes"); return; } } } writeln("No"); }
D
import std.stdio,std.conv,std.string; void main(){ bool[9][9] m; foreach(y;0..8){ foreach(x,c;readln.chomp){ m[y*2][1+x*2]=c=='1'; } foreach(x,c;readln.chomp){ m[y*2+1][x*2]=c=='1'; } } int x,y,dir=6; while(true){ final switch(dir){ case 2: if(x!=8&&m[y][x+1]){ x+=2; dir=6; dir.p; }else if(y!=8&&m[y+1][x]){ y+=2; dir.p; }else{ dir=4; } break; case 4: if(y!=8&&m[y+1][x]){ y+=2; dir=2; dir.p; }else if(x!=0&&m[y][x-1]){ x-=2; dir.p; }else{ dir=8; } break; case 6: if(y!=0&&m[y-1][x]){ y-=2; dir=8; dir.p; }else if(x!=8&&m[y][x+1]){ x+=2; dir.p; }else{ dir=2; } break; case 8: if(x!=0&&m[y][x-1]){ x-=2; dir=4; dir.p; }else if(y!=0&&m[y-1][x]){ y-=2; dir.p; }else{ dir=6; } } if(!x&&!y)break; } writeln; } void p(int dir){ final switch(dir){ case 2:'D'.write;break; case 4:'L'.write;break; case 6:'R'.write;break; case 8:'U'.write;break; } }
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; void main() { int H, W; scan(H, W); auto a = iota(H).map!(i => readln.chomp.to!(char[])).array; a = a.filter!(s => s.any!"a == '#'").array; H = a.length.to!int; auto b = new bool[](W); foreach (j ; 0 .. W) { foreach (i ; 0 .. H) { if (a[i][j] == '#') b[j] = true; } } foreach (i ; 0 .. H) { foreach (j ; 0 .. W) { if (b[j]) write(a[i][j]); } writeln; } } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } 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
module app; import core.bitop; import std.algorithm; import std.array; import std.bigint; import std.conv; import std.stdio; import std.string; struct Input { int n; } void parseInput(T)(out Input input, T file) { with (file) with (input) { auto single = readln().strip(); n = single.to!int; } } struct Output { } auto main2(Input* input) { foreach (a; 1..10) foreach (b; 1..10) if (a * b == input.n) return "Yes"; return "No"; } unittest { writeln("begin unittest"); } unittest // example1 { string example = `10`; Input input = void; parseExample(input, example); auto result = main2(&input); writeln(result); assert(result == "Yes"); } unittest // example2 { string example = `50`; Input input = void; parseExample(input, example); auto result = main2(&input); writeln(result); assert(result == "No"); } unittest // example3 { string example = `81`; Input input = void; parseExample(input, example); auto result = main2(&input); writeln(result); assert(result == "Yes"); } unittest { writeln("end unittest"); } void parseExample(out Input input, string example) { struct Adapter { string[] _lines; this(string input) { _lines = input.splitLines(); } string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; } } parseInput(input, Adapter(example)); } void main() { Input input = void; parseInput(input, stdin); auto result = main2(&input); writeln(result); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm: canFind; void main() { string[] inputs = split(readln()); string A = inputs[0]; string B = inputs[1]; string C = inputs[2]; if(A[$-1] == B[0] && B[$-1] == C[0]) "YES".writeln; else "NO".writeln; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { long a,b; scan(a,b); (a - b + 1).writeln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD!int; auto M = RD!int; writeln(min(N - M, max(1, M))); stdout.flush(); debug readln(); }
D
import std; void main() { string s = read!string; writeln(s.canFind('7') ? "Yes" : "No"); } 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
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; import std.typecons; void main () { foreach (line; stdin.byLineCopy ()) { writeln ("NO"); stdout.flush (); } }
D
import std.stdio, std.conv, std.array,std.string,std.algorithm; void main() { auto n=readln.chomp.to!int,k=readln.chomp.to!int; int i,ans=1; for(i=0;i<n;i++){ ans=min(2*ans,ans+k); } 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() { 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; stdout.flush; 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; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.numeric; void main() { auto s = readln.chomp; auto k = readln.chomp.to!long; int index; foreach (e; s) { if (e != '1') { break; } index++; } if (index.to!long >= k) { writeln("1"); } else { s[index].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; foreach (c; S) { if (c == '+') ++ans; else --ans; } writeln(ans); stdout.flush(); debug readln(); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto a=new int[][](n, 10); foreach(i; 0..n) a[i]=readln.split.to!(int[]); auto c=new int[][](n, 11); foreach(i; 0..n) c[i]=readln.split.to!(int[]); long sum=-10000000000; for(int bit=1; bit<(1<<10); bit++){ auto cnt=new int[](n); foreach(j; 0..10)if(bit&(1<<j)){ foreach(i; 0..n)if(a[i][j]) cnt[i]++; } long s=0; foreach(i; 0..n) s+=c[i][cnt[i]]; sum=max(sum, s); } writeln(sum); } 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.conv; void main() { auto u = readln.chomp, A = u[0], B = u[1], C = u[2]; auto s = readln.chomp, D = s[0], E = s[1], F = s[2]; if(A == F && B == E && C == D){ writeln("YES"); } else { writeln("NO"); } }
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } /* 行列累乗 有向パス 数え上げ ---------------------------------------- a -> b と、b -> c をつなげると a -> c のパスになる 「aからbへの長さiのパスの総数」をx[i][a][b]とすると、x[i + j][a][c] = sum(b)(x[i][a][b] + x[j][b][c]) これを使って、K を、K/2 と K/2 に分けていけば、log K のオーダーでできるはず ---------------------------------------- 上記を整理して、以下のようにすればよい xs[l][i][j] は、i から j への長さ (1<<i) のパスの個数 xs[1] は与えられる a[i][j]に他ならない xs[l][i][j] = sum(t)(xs[l - 1][i][t] * xs[l - 1][t][j]) これを表すために、 (long[][], long[][]) => long[][] の演算 a ** b を (a ** b)[i][j] = sum(t)(a[i][t] * b[t][j]) で定義する xs[1] = a xs[l] = xs[l - 1] ** xs[l - 1] そして求めるものは ans = xs[l1] ** xs[l2] ** xs[l3] ** ... ただし、l1 などは (1<<l) & k > 0 であるような l のすべての項の和 ---------------------------------------- */ const mod = 1_000_000_007; void main(){ int n = read.to!int; long k = read.to!long; long[][] as; foreach(i; 0 .. n){ as ~= [[]]; foreach(j; 0 .. n){ as[i] ~= read.to!long; } } long[][][] xs = [as]; for(int l = 1; (1L<<l) <= k; l ++) xs ~= prod(xs[l - 1], xs[l - 1]); debug writeln("xs: ", xs); long[][] p; foreach(l, q; xs){ if((1L<<l) & k){ if(p.length) p = prod(p, q); else p = q; } } long ans; foreach(i; 0 .. n) foreach(j; 0 .. n) ans += p[i][j], ans %= mod; ans.writeln; } long[][] prod(long[][] a, long[][] b){ long[][] res; foreach(i; 0 .. a.length){ res ~= [[]]; foreach(j; 0 .. a.length){ long x = 0; foreach(t; 0 .. a.length) x += a[i][t] * b[t][j], x %= mod; res[i] ~= x; } } return res; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; long[10^^5] AS, BS, AS2B, BS2A; void main() { auto abq = readln.split.to!(int[]); auto A = abq[0]; auto B = abq[1]; auto Q = abq[2]; foreach (i; 0..A) AS[i] = readln.chomp.to!long; foreach (i; 0..B) BS[i] = readln.chomp.to!long; int i; foreach (j, a; AS[0..A]) { if (i == B-1) { AS2B[j] = BS[i]; } else if (abs(a - BS[i]) < abs(a - BS[i+1])) { AS2B[j] = BS[i]; } else { AS2B[j] = BS[++i]; } } i = 0; foreach (j, b; BS[0..B]) { if (i == A-1) { BS2A[j] = AS[i]; } else if (abs(b - AS[i]) < abs(b - AS[i+1])) { BS2A[j] = AS[i]; } else { BS2A[j] = AS[++i]; } } foreach (_; 0..Q) { auto q = readln.chomp.to!long; int al = -1, bl = -1, ar = A, br = B; while (ar - al != 1) { auto mid = (al + ar) / 2; if (AS[mid] > q) { ar = mid; } else { al = mid; } } while (br - bl != 1) { auto mid = (bl + br) / 2; if (BS[mid] > q) { br = mid; } else { bl = mid; } } auto max_l = long.max / 4; auto ald = al == -1 ? max_l : q - AS[al]; auto bld = bl == -1 ? max_l : q - BS[bl]; auto ard = ar == A ? max_l : AS[ar] - q; auto brd = br == B ? max_l : BS[br] - q; writeln(min( max(ald, bld), max(ard, brd), ald*2 + brd, ard*2 + bld, bld*2 + ard, brd*2 + ald, ald + (al == -1 ? max_l : abs(AS2B[al] - AS[al])), ard + (ar == A ? max_l : abs(AS2B[ar] - AS[ar])), bld + (bl == -1 ? max_l : abs(BS2A[bl] - BS[bl])), brd + (br == B ? max_l : abs(BS2A[br] - BS[br])) )); } }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string; bool solve() { auto s = readln.split.map!(to!int); auto N = s[0]; auto Q = s[1]; if (N == 0) return false; auto G = new int[][](N); auto D = new int[](N); auto P = new int[](N); P[0] = -1; foreach (i; 1..N) { auto n = readln.chomp.to!int; P[i] = n-1; G[i] ~= n-1; G[n-1] ~= i; } void dfs(int n, int p, int d) { D[n] = d; foreach (m; G[n]) if (m != p) dfs(m, n, d+1); } dfs(0, -1, 0); Tuple!(int, int)[] queries; auto marked = new int[](N); foreach (i; 1..Q+1) { auto t = readln.split; auto n = t[1].to!int - 1; if (t[0] == "M" && !marked[n]) { queries ~= tuple(0, n); marked[n] = i; } else if (t[0] == "Q") { queries ~= tuple(1, n); } } auto uf = new UnionFind(N, D); foreach (i; 0..N) if (!marked[i]) uf.unite(i, P[i]); long ans = 0; foreach_reverse (q; queries) { if (q[0] == 0) { uf.unite(q[1], P[q[1]]); } else { ans += uf.find(q[1]) + 1; } } ans.writeln; return true; } void main() { while (solve()) {} } class UnionFind { int N; int[] table; int[] depth; this(int n, int[] d) { N = n; table = new int[](N); fill(table, -1); depth = d.dup; } int find(int x) { return table[x] < 0 ? x : (table[x] = find(table[x])); } void unite(int x, int y) { if (x == -1 || y == -1) return; x = find(x); y = find(y); if (x == y) return; if (depth[x] > depth[y]) swap(x, y); table[x] += table[y]; table[y] = x; } }
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; void main() { string s = read!string; auto n = s.count!(c => c == 'o') + (15 - s.length); writeln(n >= 8 ? "YES" : "NO"); }
D
import std.stdio, std.algorithm, std.string, std.conv, std.array, std.range, std.math; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.to!(int[]); } void main() { int x = readint(); int ans = 1; foreach (b; 2..1000) { for (int p = 2; b^^p <= x; p++) { int y = b^^p; if (y > ans) ans = y; } } writeln(ans); string _ = readln; }
D
import std; const MOD = 10^^9 + 7; int fac(int n) { int res = 1; for (int i = 1; i <= n; i++) { res = (res * i) % MOD; } return res; } 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 calc(int n, int k) { long[long] memo; for (int g = k; g >= 1; g--) { long a = modPow(k / g, n, MOD); long x = a; for (int i = g + g; i <= k; i += g) { x = (x - memo[i]) % MOD; if (x < 0) x += MOD; } long y = x * g % MOD; memo[g] = x; } long ans = 0; foreach (key, val; memo) { auto x = key * val % MOD; ans = (ans + x) % MOD; } return ans; } void main() { int n, k; scan(n, k); writeln(calc(n, k)); } 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
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() { string L = sread(); auto dp = new long[][](L.length + 1, 2); // 確定桁数, a+b<L確定 dp[0][0] = 1; dp[0][1] = 0; foreach (i; 0 .. L.length) { if (L[i] == '1') { dp[i + 1][0] = dp[i][0] * 2 % MOD; dp[i + 1][1] = (dp[i][0] + dp[i][1] * 3) % MOD; } else { dp[i + 1][0] = dp[i][0]; dp[i + 1][1] = dp[i][1] * 3 % MOD; } } writeln(dp[L.length].sum() % MOD); }
D
import std.stdio; import std.algorithm; import std.conv; import std.string; import std.array; int[] read_nums() {return readln.strip.chomp.split(" ").map!(to!(int)).array;} void main() { int times = readln.chomp.to!(int); int index = -1; int max = -1; foreach(t; 0..times) { int[] result = read_nums; if (result[1] > max) { max = result[1]; index = result[0]; } else if (result[1] == max && result[0] < index) index = result[0]; } writeln(index, " ", max); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.numeric; void main() { auto n = readln.chomp.to!long; long res = (10L^^12)+1; foreach (long a; 1..10^^6+1) { if (n % a) continue; auto b = n / a; res = min(res, a - 1 + b - 1); } res.writeln; }
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; for(int k = 0, m = 1; m <= n; k += 1, m *= 2){ if(m == n){ "No".writeln; return; } } "Yes".writeln; int[2][] ans; int[int] memo; // n + i をつなげた親が memo[i] for(int k = 0, m = 1; m <= n; k += 1, m *= 2){ if(m > 1) ans ~= [m / 2, m]; ans ~= [n + m + 1, n + m]; memo[m] = m; for(int i = m + 1; i < m * 2 && i <= n; i ++){ ans ~= [m, i]; if(i - m / 2 < m) ans ~= [i - m / 2, n + i], memo[i] = i - m / 2; else ans ~= [memo[i - m], n + i], memo[i] = memo[i - m]; } } foreach(an; ans){ writeln(an[0], " ", an[1]); } }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { int L; string s, t; scan(L); scan(s); scan(t); if (s > t) { swap(s, t); } if (s == t[0 .. s.length]) { string ss; string tt = t ~ t; while (ss.length < t.length) { ss ~= s; } if (ss > tt) { swap(s, t); } } int n = s.length.to!int; int m = t.length.to!int; int k = 0; int kk = 0; while (n * kk <= L) { if ((L - n*kk) % m == 0) { k = kk; } kk++; } string ans; foreach (i ; 0 .. k) { ans ~= s; } foreach (i ; 0 .. (L - n*k) / m) { ans ~= t; } writeln(ans); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio,std.string,std.conv; int main() { string s; int count = 0; while((s = readln.chomp).length != 0) { if(s.length == 1) { count++; } else { bool flag = true; foreach(i;0..s.length/2) { if(s[i] != s[s.length - i - 1]) { flag = false; } } if(flag) { count++; } } } count.writeln; return 0; }
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 s = sread(); writeln(s.front, s.length - 2, s.back); }
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.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto A = new int[](N+1); foreach (i; 0..M) { s = readln.split.map!(to!int); A[s[0]-1] += 1; A[s[1]-1] -= 1; } foreach (i; 0..N) { A[i+1] += A[i]; } writeln((N-1).iota.map!(i => A[i] % 2 == 0).all ? "YES" : "NO"); }
D
void main(){ int r = _scan(); if( r<1200 )writeln("ABC"); else if( r<2800 )writeln("ARC"); else writeln("AGC"); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio; import std.string; void main(){ string str = chomp(readln()); int len = cast(int)str.length; char[1000] str2; foreach(i; 0 .. len){ str2[i] = str[len - i - 1]; } str2[len] = '\0'; writeln(str2); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int x; readV(x); auto c = 0; foreach (i; 1..10^^5) { c += i; if (c >= x) { writeln(i); return; } } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; struct Graph(T) { T L, R; T[][] adj; this(T L, T R) { this.L = L; this.R = R; adj.length = L + R; } void add_edge(T u, T v) { adj[u] ~= v+L; adj[v+L] ~= u; } T maximum_matching() { bool[] visited; T[] meta; visited.length = L; meta.length = L + R; meta[] = -1; bool augment(T u) { if (visited[u]) return false; visited[u] = true; foreach (w; adj[u]) { auto v = meta[w]; if (v < 0 || augment(v)) { meta[u] = w; meta[w] = u; return true; } } return false; } auto match = 0; foreach (u; 0..L) { visited[] = false; if (augment(u)) ++match; } return match; } } alias P = Tuple!(int, "x", int, "y"); void main() { auto N = readln.chomp.to!int; P[] RS, BS; foreach (_; 0..N) { auto ab = readln.split.to!(int[]); RS ~= P(ab[0], ab[1]); } foreach (_; 0..N) { auto cd = readln.split.to!(int[]); BS ~= P(cd[0], cd[1]); } auto G = Graph!int(N, N); foreach (i, b; BS) { foreach (j, r; RS) { if (r.x < b.x && r.y < b.y) { G.add_edge(i.to!int, j.to!int); } } } writeln(G.maximum_matching()); }
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; immutable long inf = 10L^^15; void main() { int n; scan(n); auto a = readln.split.to!(int[]); auto m = 1<<n; auto dp = new int[][](m, 2); foreach (i ; 0 .. m) { dp[i][0] = i; dp[i][1] = -1; } debug { writeln(dp); } foreach (s ; 0 .. m) { foreach (j ; 0 .. 20) { if ((s >> j) & 1) { int sb = s ^ (1 << j); foreach (r ; 0 .. 2) { if (dp[sb][r] == -1) continue; if (a[dp[sb][r]] > a[dp[s][0]]) { dp[s][1] = dp[s][0]; dp[s][0] = dp[sb][r]; } else if ((dp[s][1] == -1 || a[dp[sb][r]] > a[dp[s][1]]) && dp[s][0] != dp[sb][r]) { dp[s][1] = dp[sb][r]; } } } } } debug { writeln(dp); } auto ans = new long[](m + 1); foreach (i ; 1 .. m) { ans[i] = max(ans[i-1], a[dp[i][0]]+ a[dp[i][1]]); writeln(ans[i]); } } 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.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } void main() { auto Ns = readln.split.to!(ulong[]); writeln(Ns[0] * Ns[1] / gcd(Ns[0], Ns[1])); }
D
import core.thread; import std.conv, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex; // Input class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { tokens = readln.split; if (stdin.eof) throw new EOFException; } auto token = tokens[0]; tokens.popFront; return token; } int readInt() { return to!int(readToken); } long readLong() { return to!long(readToken); } real readReal() { return to!real(readToken); } // chmin/chmax void chmin(T)(ref T t, in T f) { if (t > f) t = f; } void chmax(T)(ref T t, in T f) { if (t < f) t = f; } // Pair struct Pair(S, T) { S x; T y; int opCmp( const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; } int opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; } string toString() const { return "(" ~ to!string(x) ~ ", " ~ to!string(y) ~ ")"; } } auto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); } // Array int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = as.length; for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; } int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a < val); }); } int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); } T[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; } immutable long MO = 1_000_000_007; void main(string[] args) { try { for (; ; ) { long a = readLong; long b = readLong; long ans = 1; (ans *= a) %= MO; (ans *= (-1 + b)) %= MO; (ans *= b) %= MO; (ans *= ((2 + b + a * b) % MO)) %= MO; for (; ans % 4 != 0; ans += MO) {} ans /= 4; ans = (ans % MO + MO) % MO; writeln(ans); } } catch (EOFException) {} }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n, q; rd(n, q); auto tree=new SquareRootDecomposition(n); while(q--){ auto args=readln.split.to!(int[]); if(args[0]==0){ tree.update(args[1], args[2]+1, args[3]); }else{ writeln(tree.rmin(args[1], args[2]+1)); } } } class SquareRootDecomposition{ import std.algorithm; int D=1, nil=-1; int[] val, buc, laz; this(int n){ while(D*D<n) D++; val.length=D*D; buc.length=laz.length=D; fill(val, (1<<31)-1); fill(buc, (1<<31)-1); fill(laz, nil); } void update(int ql, int qr, int x){ foreach(k; 0..D){ int l=k*D, r=(k+1)*D; if(r<=ql || qr<=l){ // }else if(ql<=l && r<=qr){ buc[k]=x; laz[k]=x; }else{ push(k); val[max(l, ql)..min(r, qr)]=x; buc[k]=reduce!(min)(val[l..r]); } } } int rmin(int ql, int qr){ int ret=(1<<31)-1; foreach(k; 0..D){ int l=k*D, r=(k+1)*D; if(r<=ql || qr<=l){ // }else if(ql<=l && r<=qr){ chmin(ret, buc[k]); }else{ push(k); chmin(ret, reduce!(min)(val[max(l, ql)..min(r, qr)])); } } return ret; } void push(int k){ if(laz[k]<0) return; val[(k*D)..((k+1)*D)]=laz[k]; laz[k]=-1; } void chmin(T)(ref T l, T r){ if(l>r) l=r; } } 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.conv, std.algorithm, std.container,std.array,std.range,std.string,std.typecons; const dx = [1,0,-1,0]; const dy = [0,1,0,-1]; void read(T...)(auto ref T args){ auto line = readln().split(); foreach(i,ref arg; args) arg = line[i].to!(typeof(arg)); } int sum(int n){return n * (n + 1)/ 2;} void main(){ int m;m.read; for(;m>0;m--){ string cars;cars.read; bool[string] exist; foreach(i;1..cars.length){ auto f = (string a) =>[a,a.dup.reverse.idup]; foreach(c1;f(cars[0..i])) { foreach(c2;f(cars[i..$])){ exist[c1 ~ c2] = true; exist[c2 ~ c1] = true; } } } exist.length.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, 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; void main() { int n, k; scan(n, k); auto c = new long[](k); foreach (i ; 1 .. n + 1) { c[i % k]++; } long ans; foreach (i ; 0 .. k) { if (2*i % k == 0) { ans += c[i]^^3; } } writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); } void main(string[] args){ args ~= ["", ""]; string cmd = args[1]; if(cmd == "-debug") DEBUG = 1; if(cmd == "-gen") gen; else if(cmd == "-jury") jury; else solve; } void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ std.stdio.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 gen(){ } void jury(){ } void solve(){ long a = scan!int, b = scan!int, c = scan!int, d = scan!int; long ans = max(a * c, a * d, b * c, b * d); ans.print; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto k = RD; auto a = RDA; ans[ti] = long.min; foreach_reverse (i; 0..n) { auto x = k * a[i]; foreach_reverse (j; 0..i) { long y = cast(long)(i+1) * (j+1); if (y - x <= ans[ti]) break; auto z = y - k * (a[i]|a[j]); ans[ti].chmax(z); } } } 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, core.stdc.string; immutable long MOD = 10^^9 + 7; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1].to!long; auto G = new int[][](N); foreach (i; 0..N-1) { s = readln.split.map!(to!int); G[s[0]-1] ~= s[1]-1; G[s[1]-1] ~= s[0]-1; } auto f = new long[](N); auto l = new long[][](N); auto r = new long[][](N); auto g = new long[](N); long dfs1(int n, int p) { long[] a; f[n] = 1; foreach (m; G[n]) { if (m == p) continue; auto v = dfs1(m, n) + 1; f[n] = f[n] * v % M; a ~= v; } l[n] = new long[](a.length+1); r[n] = new long[](a.length+1); l[n][0] = r[n][a.length] = 1; foreach (i; 0..a.length.to!int) { l[n][i+1] = l[n][i] * a[i] % M; } foreach_reverse (i; 0..a.length.to!int) { r[n][i] = r[n][i+1] * a[i] % M; } return f[n]; } void dfs2(int n, int p, long pv) { if (p == -1) { g[n] = f[n]; } else { g[n] = f[n] * pv % M; } int i = 0; foreach (m; G[n]) { if (m == p) continue; dfs2(m, n, l[n][i] * r[n][i+1] % M * pv % M + 1); i += 1; } } dfs1(0, -1); dfs2(0, -1, 1); g.each!writeln; }
D
import std.stdio; import std.algorithm; import std.conv; import std.array; import std.string; import std.math; import std.functional; import std.range; import std.typecons; import std.format; void main(string[] args) { readln; readln.solve.writeln; } auto solve(string line1) { auto hash = line1.chomp.split.map!(to!long).array.counter; long res; foreach(k, v; hash) { if (k < v) { res += v - k; } else if (v < k) { res += v; } } return res; } unittest { assert(solve("3 3 3 3") == 1, "1"); assert(solve("2 4 1 4 2") == 2, "2"); assert(solve("1 2 2 3 3 3") == 0, "3"); assert(solve("1000000000") == 1, "4"); assert(solve("2 7 1 8 2 8 1 8") == 5, "4"); } pure auto counter(N = int, E)(E[] arr) { N[E] hash; foreach(e; arr) { hash[e]++; } return hash; }
D
import std.stdio, std.conv, std.array,std.string,std.algorithm; void main() { auto n=readln.chomp.to!int; int ans=1; for(int i=1;i<n;i++){ if(ans>n){ break; }else{ ans*=2; } } if(n==1){ ans=2; } writeln(ans/=2); }
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, std.format, std.bitmanip; // }}} // nep.scanner {{{ 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; } } // }}} long[] divisor(const long n) { long[] ret; for (long i = 1; i * i <= n; ++i) { if (n % i == 0) { ret ~= i; if (i != n / i) { ret ~= n / i; } } } return ret; } void main() { auto cin = new Scanner; long n; cin.scan(n); auto primes = divisor(n); auto prevPrimes = divisor(n - 1); auto res = prevPrimes.length - 1; foreach (k; primes) { if (k == 1) continue; long nn = n; while (nn >= k) { if (nn % k == 0) { nn /= k; } else { nn %= k; } } if (nn == 1) { res++; } } writeln(res); }
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"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long h, a; scan(h, a); writeln((h + a - 1) / a); } 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 solve(){ } void main(){ int[] val = inary(); ( (val[0]-1)*(val[1]-1) ).writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range; const long mod = 10^^9+7; alias instr = () => readln().chomp(); T inelm(T= int)(){ return to!(T)( readln().chomp() ); } T[] inary(T = int)(){ return readln().chomp().split().to!(T[])(); }
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 N = readln.chomp.to!int; auto A = readln.split.map!(to!int).array; if (N % 3 != 0) { writeln(A.map!(a => a == 0).all ? "Yes" : "No"); } else { int[int] cnt; foreach (a; A) cnt[a] += 1; int[] B; foreach (k, v; cnt) { if (v % (N / 3) != 0) { writeln("No"); return; } while (v > 0) { B ~= k; v -= N / 3; } } if (B.length != 3) { writeln("No"); return; } if (B[0] != (B[1] ^ B[2])) { writeln("No"); return; } writeln("Yes"); } }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto rd = readln.split.to!(int[]), a = rd[0], b = rd[1]; writeln(a % 3 == 0 || b % 3 == 0 || (a+b) % 3 == 0 ? "Possible" : "Impossible"); }
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() { bool b = (lread() % 500) <= lread(); writeln(b ? "Yes" : "No"); }
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 S = sread(); outer: foreach (state; 0 .. 2 ^^ 2) { auto ans = new char[](N); ans[0] = "SW"[state & 1]; ans[1] = "SW"[(state >> 1) & 1]; foreach (i; 1 .. N - 1) { if (ans[i - 1 .. i + 1] == "SS") { ans[i + 1] = (S[i] == 'o') ? 'S' : 'W'; } if (ans[i - 1 .. i + 1] == "SW") { ans[i + 1] = (S[i] != 'o') ? 'S' : 'W'; } if (ans[i - 1 .. i + 1] == "WS") { ans[i + 1] = (S[i] == 'o') ? 'W' : 'S'; } if (ans[i - 1 .. i + 1] == "WW") { ans[i + 1] = (S[i] != 'o') ? 'W' : 'S'; } } foreach (i; 0 .. N) { long l = (N + i - 1) % N; long r = (i + 1) % N; if (ans[i] == 'S' && S[i] == 'o' && ans[l] != ans[r]) continue outer; if (ans[i] == 'S' && S[i] == 'x' && ans[l] == ans[r]) continue outer; if (ans[i] == 'W' && S[i] == 'o' && ans[l] == ans[r]) continue outer; if (ans[i] == 'W' && S[i] == 'x' && ans[l] != ans[r]) continue outer; } writeln(ans); return; } writeln(-1); }
D
void main() { string s = readln.chomp; string t = readln.chomp; bool[26][26] table; foreach (i; 0 .. s.length) { table[s[i]-'a'][t[i]-'a'] = true; } bool ok = true; foreach (i; 0 .. 26) { long row, col; foreach (j; 0 .. 26) { if (table[i][j]) ++row; if (table[j][i]) ++col; } if (row > 1 || col > 1) ok = false; } writeln(ok ? "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.functional, std.algorithm, std.container, std.typetuple, std.typecons, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv; void main() { int[] HW = readln.chomp.split.to!(int[]); int H = HW[0], W = HW[1]; int[] hw = readln.chomp.split.to!(int[]); int h = hw[0], w = hw[1]; writeln((H - h) * (W - w)); }
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 s = sread(); foreach_reverse (i; 0 .. s.length) { auto t = s[0 .. i]; if (t[0 .. $ / 2] == t[$ / 2 .. $]) { writeln(t.length); return; } } }
D
/+ dub.sdl: name "C" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; int main() { Scanner sc = new Scanner(stdin); int h, w; sc.read(h, w); int[26] cnt; int[5] d; foreach (i; 0..h) { string s; sc.read(s); foreach (j; 0..w) { cnt[s[j]-'a']++; if (h-1-i > i) continue; if (w-1-j > j) continue; int x = 4; if (h-1-i == i) x /= 2; if (w-1-j == j) x /= 2; d[x]++; } } // writeln(cnt); // writeln(d); int[][] ans = new int[][](h, w); foreach (i; 0..h) { foreach (j; 0..w) { if (h-1-i <= i) continue; if (w-1-j <= j) continue; int f = -1; foreach (c; 0..26) { if (cnt[c] >= 4) { f = c; break; } } if (f == -1) { writeln("No"); return 0; } cnt[f] -= 4; ans[i][j] = f + 'a'; ans[h-1-i][j] = f + 'a'; ans[i][w-1-j] = f + 'a'; ans[h-1-i][w-1-j] = f + 'a'; } } foreach (i; 0..h) { foreach (j; 0..w) { if (h-1-i < i || w-1-j < j) continue; if (h-1-i > i && w-1-j > j) continue; if (h-1-i == i && w-1-j == j) continue; int f = -1; foreach (c; 0..26) { if (cnt[c] >= 2) { f = c; break; } } if (f == -1) { writeln("No"); return 0; } cnt[f] -= 2; ans[i][j] = f + 'a'; ans[h-1-i][j] = f + 'a'; ans[i][w-1-j] = f + 'a'; ans[h-1-i][w-1-j] = f + 'a'; } } foreach (i; 0..h) { foreach (j; 0..w) { if (h-1-i != i) continue; if (w-1-j != j) continue; int f = -1; foreach (c; 0..26) { if (cnt[c] >= 1) { f = c; break; } } if (f == -1) { writeln("No"); return 0; } cnt[f] -= 1; ans[i][j] = f + 'a'; ans[h-1-i][j] = f + 'a'; ans[i][w-1-j] = f + 'a'; ans[h-1-i][w-1-j] = f + 'a'; } } writeln("Yes"); foreach (i; 0..h) { // writeln(ans[i].map!(x => cast(char)(x)).array); } return 0; } /* 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
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto xy = readln.split.to!(int[]); auto X = xy[0]; auto Y = xy[1]; foreach (i; 0..X+1) { auto j = X-i; if (i*2 + j*4 == Y) { writeln("Yes"); return; } } writeln("No"); }
D
void main(){ switch(_scan()%10){ case 0,1,6,8: writeln("pon"); break; case 3: writeln("bon"); break; default: writeln("hon"); } } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
// import chie template :) {{{ static if (__VERSION__ < 2090) { import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons, std.format, std.bitmanip, std.numeric; } else { import std; } // }}} // nep.scanner {{{ 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(T...)(ref T args) { foreach (ref arg; args) { arg = next!(typeof(arg)); } } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } // }}} // alias {{{ alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less); alias MinHeap(T) = Heap!(T, "a > b"); // }}} // memo {{{ /* - ある値が見つかるかどうか <https://dlang.org/phobos/std_algorithm_searching.html#canFind> canFind(r, value); -> bool - 条件に一致するやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#filter> // 2で割り切れるやつ filter!"a % 2 == 0"(r); -> Range - 合計 <https://dlang.org/phobos/std_algorithm_iteration.html#sum> sum(r); - 累積和 <https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold> // 今の要素に前の要素を足すタイプの一般的な累積和 // 累積和のrangeが帰ってくる(破壊的変更は行われない) cumulativeFold!"a + b"(r, 0); -> Range - rangeをarrayにしたいとき array(r); -> Array - 各要素に同じ処理をする <https://dlang.org/phobos/std_algorithm_iteration.html#map> // 各要素を2乗 map!"a * a"(r) -> Range - ユニークなやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#uniq> uniq(r) -> Range - 順列を列挙する <https://dlang.org/phobos/std_algorithm_iteration.html#permutations> permutation(r) -> Range - ある値で埋める <https://dlang.org/phobos/std_algorithm_mutation.html#fill> fill(r, val); -> void - バイナリヒープ <https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap> // 昇順にするならこう(デフォは降順) BinaryHeap!(Array!T, "a > b") heap; heap.insert(val); heap.front; heap.removeFront(); - 浮動小数点の少数部の桁数設定 // 12桁 writefln("%.12f", val); - 浮動小数点の誤差を考慮した比較 <https://dlang.org/phobos/std_math.html#.approxEqual> approxEqual(1.0, 1.0099); -> true - 小数点切り上げ <https://dlang.org/phobos/std_math.html#.ceil> ceil(123.4); -> 124 - 小数点切り捨て <https://dlang.org/phobos/std_math.html#.floor> floor(123.4) -> 123 - 小数点四捨五入 <https://dlang.org/phobos/std_math.html#.round> round(4.5) -> 5 round(5.4) -> 5 */ // }}} void main() { auto cin = new Scanner; int k; string s; cin.scan(k, s); if (s.length <= k) { writeln(s); } else { writeln(s[0 .. k], "..."); } }
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } const long mod = 1_000_000_007; void main(){ int n = read.to!int; int m = read.to!int; int[] as; foreach(i; 0 .. n) as ~= read.to!int; int[] bs; foreach(j; 0 .. m) bs ~= read.to!int; int[int] aPos, bPos; foreach(i; 0 .. n) aPos[as[i]] = i; foreach(j; 0 .. m) bPos[bs[j]] = j; // xを減らしていくとき、その行・列・全体で入れるマスはいくつあるか int[] aCounts; // その行にすでに入っている個数 int[] bCounts; // その列にすでに入っている個数 int aCount; // 解禁されている行数 int bCount; // 解禁されている列数 foreach(i; 0 .. n) aCounts ~= 0; foreach(j; 0 .. m) bCounts ~= 0; aCount = 0; bCount = 0; long ans = 1; foreach_reverse(x; 1 .. n * m + 1){ if(x in aPos && x in bPos){ // そこの場所できまり ans *= 1; // その行および列は新たに解禁された aCount += 1; bCount += 1; // その行および列はあきが減る aCounts[aPos[x]] += 1; bCounts[bPos[x]] += 1; } else if(x in aPos && x !in bPos){ // その行で、解禁済みの列のうち、あきをさがす ans *= bCount - aCounts[aPos[x]]; ans %= mod; // その行は新たに解禁された aCount += 1; // その行はあきが減る aCounts[aPos[x]] += 1; } else if(x !in aPos && x in bPos){ // その列で、解禁済みの行のうち、あきをさがす ans *= aCount - bCounts[bPos[x]]; ans %= mod; // その列は新たに解禁された bCount += 1; // その列はあきが減る bCounts[bPos[x]] += 1; } else{ // 全体で、解禁済みの行かつ解禁済みの列のうち、あきをさがす ans *= aCount * bCount - (n * m - x); ans %= mod; } } ans.writeln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { string N = readln.chomp; writeln(N[0] == N[2] ? "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); } } }
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() { long n, k; scan(n, k); long cnt; foreach (b; iota(k + 1, n + 1)) { auto p = n / b; cnt += max(p * (b - k), 0); cnt += max(0, n - p * b - k + 1); } if (k == 0) cnt -= n; cnt.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
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto s=new char[][](n); foreach(i; 0..n) s[i]=readln.chomp.to!(char[]); long[char] cnt; cnt['M']=cnt['A']=cnt['R']=cnt['C']=cnt['H']=0L; foreach(i; 0..n){ auto c=s[i][0]; if(c in cnt) cnt[c]++; } long tot=0; tot+=cnt['M']*cnt['A']*cnt['R']; tot+=cnt['M']*cnt['A']*cnt['C']; tot+=cnt['M']*cnt['A']*cnt['H']; tot+=cnt['M']*cnt['R']*cnt['C']; tot+=cnt['M']*cnt['R']*cnt['H']; tot+=cnt['M']*cnt['C']*cnt['H']; tot+=cnt['A']*cnt['R']*cnt['C']; tot+=cnt['A']*cnt['R']*cnt['H']; tot+=cnt['A']*cnt['C']*cnt['H']; tot+=cnt['R']*cnt['C']*cnt['H']; writeln(tot); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { long A, B, C; scan(A, B, C); solve(A, B, C).writeln(); } long solve(long A, long B, long C) { if (C <= B) { return C + B; } long result; result += 2 * B; C -= B; result += min(C, A); C -= A; if (0 < C) { result++; } return result; }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.math; void main() { immutable EPS = 1.0e-10; 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; import std.array; import std.conv; void main(){ string[] input = readln.split; int A = input[0].to!int; int B = input[1].to!int; if(B % A == 0) write(A + B); else write(B - A); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum mod = 10L^^9 + 7; void main() { auto k = readln.chomp; int n = k.length.to!int; int d; scan(d); auto dp = new long[][][](n + 1, 2, d); dp[0][0][0] = 1; foreach (i ; 0 .. n) { foreach (m ; 0 .. 2) { foreach (r ; 0 .. d) { foreach (a ; 0 .. (m ? 10 : k[i] - '0' + 1)) { (dp[i+1][m | (a < k[i] - '0')][(r + a % d) % d] += dp[i][m][r]) %= mod; } } } } auto ans = dp[n][0][0] + dp[n][1][0] - 1 + mod; ans %= mod; writeln(ans); } int LIS(T)(T[] arr) { import std.range, std.algorithm; auto n = arr.length; auto dp = repeat(T.max, n + 1).array.assumeSorted; dp[0] = T.min; int res; foreach (i; 0 .. n) { auto j = dp.lowerBound(arr[i]).length; dp[j] = arr[i]; res = max(res, j); } return res; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.conv, std.string, std.algorithm, std.math, std.array, std.container, std.typecons; void main() { int n = readln.chomp.to!int; string s = readln.chomp; int[] rcs = new int[n+1]; int[] gcs = new int[n+1]; int[] bcs = new int[n+1]; long sum = 0; for(int i=0; i<s.length; i++) { if(s[$-1-i]=='R') rcs[n-1-i]=rcs[n-i]+1; else rcs[n-1-i]=rcs[n-i]; if(s[$-1-i]=='G') gcs[n-1-i]=gcs[n-i]+1; else gcs[n-1-i]=gcs[n-i]; if(s[$-1-i]=='B') bcs[n-1-i]=bcs[n-i]+1; else bcs[n-1-i]=bcs[n-i]; } for(int i=0; i<s.length; i++) { for(int j=i+1; j<s.length; j++) { if(s[i]=='R') { if(s[j]=='G') sum += bcs[j+1]; if(s[j]=='B') sum += gcs[j+1]; } if(s[i]=='G') { if(s[j]=='R') sum += bcs[j+1]; if(s[j]=='B') sum += rcs[j+1]; } if(s[i]=='B') { if(s[j]=='R') sum += gcs[j+1]; if(s[j]=='G') sum += rcs[j+1]; } } } for(int span=1; span<n; span++) { for(int i=0; i+span*2<n; i++) { if(s[i]!=s[i+span] && s[i+span]!=s[i+span*2] && s[i]!=s[i+span*2]) sum--; } } writeln(sum); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons; import std.math, std.numeric; void main() { int k, t; scan(k, t); auto a = readln.split.to!(int[]); auto amax = a.reduce!max; auto ans = max(0, amax - (k - amax) - 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); } } }
D
void main(){ int[] fares; foreach(_;0..4){ fares ~= inelm(); } min( fares[0]+fares[2], fares[0]+fares[3], fares[1]+fares[2], fares[1]+fares[3]) .writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range; const long mod = 10^^9+7; // 1要素のみの入力 T inelm(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] inln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split())ln ~= elm.to!T(); return ln; }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.ascii; void main() { bool f; auto s = readln.chomp; auto prev = 'a'; foreach (e; s) { if (prev == e) { f = true; } prev = e; } if (f) { writeln("Bad"); } else { writeln("Good"); } }
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 N = readln.chomp.to!int; auto AS = readln.split.to!(long[]); auto ts = new int[](N); ts[N-1] = 2; foreach (i; 0..N-1) { if (AS[i] < AS[i+1]) { ts[i] = 1; } else if (AS[i] > AS[i+1]) { ts[i] = 2; } } long m = 1000L, a; foreach (i; 0..N) { if (ts[i] == 1) { auto x = m / AS[i]; m -= x * AS[i]; a += x; } else if (ts[i] == 2) { m += a * AS[i]; a = 0; } } writeln(m); }
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } /* ゲーム 区間DP ------------ 左をi個、右をj個とった状態からの先手(=次の人)の得点 x[i][j] i + j == n のとき x[i][j] = 0 i + j + 1 == n のとき ※実はこれも下に吸収される x[i][j] = a[i] その他のとき x[i][j] = max(a[i] - x[i + 1][j], a[n - j - 1] - x[i][j + 1]) ------------ 上記だと添字が上からになるのでやりづらい。添字をつけ直す 残りがi個で、左からj個とった状態の先手(=次の人)の得点 x[i][j] ※上のn - i - jがこちらのi、上のiがこちらのj i == 0 のとき x[i][j] = 0 i == 1 のとき x[i][j] = a[j] その他のとき x[i][j] = max(a[j] - x[i - 1][j + 1], a[i + j - 1] - x[i - 1][j]) 求めるものは x[n][0] ------------ オンメモリでできる */ void main(){ int n = read.to!int; long[] as = readln.chomp.split.map!(to!long).array; long[] xs; foreach(j; 0 .. n + 1) xs ~= 0; debug writeln("i:0, xs:", xs); foreach(i; 1 .. n + 1){ foreach(j; 0 .. n - i + 1){ if(j + 1 < n + 1) xs[j] = max(as[j] - xs[j + 1], as[i + j - 1] - xs[j]); else xs[j] = as[i + j - 1] - xs[j]; } debug writeln("i:", i, " xs:", xs); } xs[0].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!(string, "s", long, "p"); alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less); void main() { auto x = lread(); writeln(2 * (x / 11) + (x % 11 ? (x % 11 > 6 ? 2 : 1) : 0)); } 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.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!double; foreach (i; 0..50000+1) { if ((i * 1.08).to!int == N) { writeln(i); return; } } 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; 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); } long[9] cost = [2, 5, 5, 4, 5, 6, 3, 7, 6]; bool[9] canuse; void main() { long N, M; scan(N, M); auto A = aryread(); foreach (a; A) canuse[a - 1] = true; foreach (i; 0 .. 9) foreach (j; 0 .. i) if (canuse[i] && cost[i] == cost[j]) canuse[j] = false; auto dp = new long[](N + 6); dp[] = long.min; dp[0] = 0; foreach (i; 0 .. N) foreach (j; 0 .. 9) if (canuse[j]) { dp[i + cost[j]].maxAssign(dp[i] + 1); } string s; long tmp = N; loop: foreach (_; 0 .. dp[N]) foreach_reverse (j; 0 .. 9) if (canuse[j] && cost[j] <= tmp && dp[tmp - cost[j]] + 1 == dp[tmp]) { tmp -= cost[j]; s ~= '1' + j; continue loop; } writeln(s); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto k = RD!int; auto ans = new int[2][][](k); foreach (i; 0..k) { auto n = RD!int; auto s = RD!(char[]); auto t = RD!(char[]); auto cnt = new int[](26); foreach (j; 0..n) { auto c1 = s[j] - 'a'; auto c2 = t[j] - 'a'; ++cnt[c1]; ++cnt[c2]; } bool ok = true; foreach (e; cnt) { if (e % 2 == 1) { ok = false; break; } } if (!ok) continue; foreach (j; 0..n) { if (s[j] != t[j]) { int pos = -1; foreach (l; j+1..n) { if (t[l] == t[j]) { pos = l; break; } } if (pos == -1) { foreach (l; j+1..n) { if (s[l] == t[j]) { pos = l; break; } } ans[i] ~= [pos, pos]; ans[i] ~= [j, pos]; swap(s[pos], t[pos]); swap(s[j], t[pos]); } else { ans[i] ~= [j, pos]; swap(s[j], t[pos]); } } } } foreach (e; ans) { if (e.empty) writeln("NO"); else { writeln("YES"); writeln(e.length); foreach (ee; e) writeln(ee[0]+1, " ", ee[1]+1); } } stdout.flush(); debug readln(); }
D
import std.stdio,std.string,std.conv,std.math; void main(){ while(1){ string[] arr; int[] arr2;arr2.length=2; auto rd = chomp( readln() ); if(rd){ arr = split(rd); arr2[0] = to!int(arr[0]); arr2[1] = to!int(arr[1]); auto sum = to!string(arr2[0] + arr2[1]); writeln( sum.length ); }else{ break; } } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nmc = readln.split.to!(int[]); auto N = nmc[0]; auto M = nmc[1]; auto C = nmc[2]; auto bs = readln.split.to!(int[]); int cnt; foreach (_; 0..N) { int d; foreach (i, a; readln.split.to!(int[])) { auto b = bs[i]; d += a * b; } if (d + C > 0) ++cnt; } writeln(cnt); }
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 main() { auto t = RD!int; auto ans = new long[][](t); foreach (ti; 0..t) { auto n = RD!int; auto a = RDA; long b = long.max, c = long.min; foreach (i; 0..n) { if (a[i] != -1) continue; if (i != 0) { if (a[i-1] != -1) { b.chmin(a[i-1]); c.chmax(a[i-1]); } } if (i != n-1) { if (a[i+1] != -1) { b.chmin(a[i+1]); c.chmax(a[i+1]); } } } auto x = (b+c) / 2; auto y = max(0, x-b, c-x); foreach (i; 1..n) { if (a[i] != -1 && a[i-1] != -1) { y.chmax(abs(a[i] - a[i-1])); } } ans[ti] = [y, x]; } foreach (e; ans) { writeln(e[0], " ", e[1]); } stdout.flush; debug readln; }
D
import std.stdio, std.string, std.conv; import std.typecons; import std.algorithm, std.array, std.range, std.container; void main() { auto X = readln.split[0].to!int; writeln(X == 7 || X == 5 || X == 3 ? "YES" : "NO"); }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; immutable inf = 10^^6; void main() { int k; scan(k); auto d = new int[](k); d[] = inf; d[1] = 1; auto deq = Deque!(int)(3*k); deq.pushBack(1); while (!deq.empty) { auto u = deq.front; deq.popFront(); if (d[(u + 1) % k] > d[u] + 1) { d[(u + 1) % k] = d[u] + 1; deq.pushBack((u + 1) % k); } if (d[(u * 10) % k] > d[u]) { d[(u * 10) % k] = d[u]; deq.pushFront((u * 10) % k); } } writeln(d[0]); } struct Deque(T) { private { int N, head, tail; T[] deq; } this(int n) { N = n + 1; deq = new T[](N + 1); } bool empty() { return head == tail; } bool full() { return head == ((tail + 1) % N); } int length() { return (tail - head + N) % N; } T front() { assert(!empty); return deq[head]; } T back() { assert(!empty); return deq[(tail - 1 + N) % N]; } void pushFront(T x) { assert(!full); head = (head - 1 + N) % N; deq[head] = x; } void pushBack(T x) { assert(!full); deq[tail++] = x; tail %= N; } void popFront() { assert(!empty); head = (head + 1) % N; } void popBack() { assert(!empty); tail = (tail - 1 + N) % N; } } unittest { int n = 5; auto deq = Deque!(int)(n); deq.pushFront(3); deq.pushBack(1); deq.pushBack(4); deq.pushFront(5); assert(deq.front == 5); assert(deq.back == 4); assert(deq.length == 4); deq.pushFront(0); assert(deq.full); assert(deq.front == 0); assert(deq.length == 5); deq.popFront(); deq.popFront(); deq.popBack(); assert(deq.front == 3); assert(deq.back == 1); assert(deq.length == 2); deq.popBack(); deq.popFront(); assert(deq.empty); assert(deq.length == 0); writeln("ok"); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.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() { readln; auto A = readln.chomp.split.to!(long[]); A.map!(a => a - 1).sum().writeln; }
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() { readln.chomp.replace(".", "").replace(",", "").split.filter!(a=> a.length >= 3 && a.length <= 6).join(" ").writeln; }
D
void main() { string[] tmp = readln.split; string a = tmp[0], b = tmp[1]; writeln(a == b ? "H" : "D"); } 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.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp.to!int; int sum; foreach (_; 0..n) { auto l = readln.chomp.split.map!(to!int); sum += l[1] - l[0] + 1; } writeln(sum); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.math; void main() { int[string] a; ubyte N = readln.chomp.to!ubyte; foreach(_; 0..N) { a[readln.chomp]++; } ubyte M = readln.chomp.to!ubyte; foreach(_; 0..M) { a[readln.chomp]--; } max(a.values.reduce!max, 0).writeln; }
D
import std.stdio, std.string, std.algorithm, std.array, std.range, std.conv, std.typecons, std.math, std.container, std.format, std.traits; /** Computes the value of a positive integer `x`, raised to the power `n`, modulo `m`. * * Params: * x = base * n = exponent * m = modulus * * Returns: * `x` to the power `n`, modulo `m`. * The return type is the largest of `x`'s and `m`'s type. * * The function requires that all values have unsigned types. */ Unqual!(Largest!(F, H)) powmod(F, G, H)(F x, G n, H m) if (isUnsigned!F && isUnsigned!G && isUnsigned!H) { import std.meta : AliasSeq; alias T = Unqual!(Largest!(F, H)); static if (T.sizeof <= 4) { alias DoubleT = AliasSeq!(void, ushort, uint, void, ulong)[T.sizeof]; } static T mulmod(T a, T b, T c) { static if (T.sizeof == 8) { static T addmod(T a, T b, T c) { b = c - b; if (a >= b) return a - b; else return c - b + a; } T result = 0, tmp; b %= c; while (a > 0) { if (a & 1) result = addmod(result, b, c); a >>= 1; b = addmod(b, b, c); } return result; } else { DoubleT result = cast(DoubleT)(cast(DoubleT) a * cast(DoubleT) b); return result % c; } } T base = x, result = 1, modulus = m; Unqual!G exponent = n; while (exponent > 0) { if (exponent & 1) result = mulmod(result, base, modulus); base = mulmod(base, base, modulus); exponent >>= 1; } return result; } void main(string[] args) { const MOD = cast(ulong)(1e9 + 7); long n = readln.strip.to!long; auto as = readln.split.to!(long[]); long ans = 0; foreach (i; 0 .. 60) { ulong[] cnt = [0, 0]; foreach (j; 0 .. n) { cnt[(as[j] >> i) & 1]++; } ans = (ans + (cnt[0] * ((cnt[1] * powmod(2uL, cast(ulong) i, MOD)) % MOD)) % MOD) % MOD; } writeln(ans); }
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; alias lread = () => readln.chomp.to!long; alias aryread = () => readln.split.to!(long[]); void main() { auto ab = aryread(); (((ab[0] * ab[1]) % 2 == 0) ? "No" : "Yes").writeln; }
D
import std.stdio; import std.string, std.conv; import std.algorithm, std.array; import std.typecons; void main() { for(;;) { auto s = readln.split.map!(to!int).array; int N = s[0], M = s[1]; if(N == 0 && M == 0) break; if((N * M) % 2 == 0) writeln("yes"); else writeln("no"); } }
D
import std.stdio, std.algorithm, std.string, std.conv, std.array, std.range, std.math; int read() { return readln.chomp.to!int; } int[] reads() { return readln.split.to!(int[]); } string solve(string s) { int w = read(); string r; foreach (i, c; s) if (i % w == 0) r ~= c; return r; } void main() { writeln(readln.chomp.solve); }
D