code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio; import std.string; import std.conv; import std.algorithm: canFind; void main() { string[] inputs = split(readln()); int x = to!int(inputs[0]); int y = to!int(inputs[1]); int res_x = group(x); int res_y = group(y); if(res_x == res_y) "Yes".writeln; else "No".writeln; } ubyte group(int a) { switch(a) { case 1,3,5,7,8,10,12: return 0; case 4,6,9,11: return 1; case 2: return 2; default: return 3; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; enum P = 10L^^9+7; long[(10^^6)*2+50] F, RF; long pow(long x, long n) { long y = 1; while (n) { if (n%2 == 1) y = (y * x) % P; x = x^^2 % P; n /= 2; } return y; } long inv(long x) { return pow(x, P-2); } void init() { F[0] = F[1] = 1; foreach (i, ref x; F[2..$]) x = (F[i+1] * (i+2)) % P; { RF[$-1] = 1; auto x = F[$-1]; auto k = P-2; while (k) { if (k%2 == 1) RF[$-1] = (RF[$-1] * x) % P; x = x^^2 % P; k /= 2; } } foreach_reverse(i, ref x; RF[0..$-1]) x = (RF[i+1] * (i+1)) % P; } long comb(N)(N n, N k) { if (k > n) return 0; auto n_b = F[n]; // n! auto nk_b = RF[n-k]; // 1 / (n-k)! auto k_b = RF[k]; // 1 / k! auto nk_b_k_b = (nk_b * k_b) % P; // 1 / (n-k)!k! return (n_b * nk_b_k_b) % P; // n! / (n-k)!k! } void main() { init(); auto K = readln.chomp.to!long; auto S = readln.chomp; auto len = S.length.to!long; long x; foreach (k; 0..K+1) { auto y = comb(len+k-1, len-1) * pow(25, k) % P * pow(26, K-k) % P; (x += y) %= P; } writeln(x); }
D
import std.stdio,std.conv,std.string,std.algorithm,std.array; void main(){ auto sn=readln().split(); int n,ans; n=to!int(sn[0]); ans=n/3; writeln(ans); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // 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; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); auto A = sread(); auto B = sread(); auto C = sread(); long ans; foreach (i; 0 .. N) { if (A[i] == B[i] && A[i] == C[i]) { ans += 0; } else if (A[i] == B[i] || A[i] == C[i] || B[i] == C[i]) { ans += 1; } else { ans += 2; } } writeln(ans); }
D
import std.typecons; import std.stdio; T gcd(T)(T a, T b) { if (a % b == 0) return b; return gcd(b, a % b); } struct Rat(T) { import std.math : abs; T n, d; this(T n_, T d_) { if (d_ == 0) { n = 1; d = 0; } else if (n_ == 0) { d = 1; n = 0; } else { auto c = gcd(abs(n_), abs(d_)); bool neg = (n_ < 0) ^ (d_ < 0); n = abs(n_ / c); d = abs(d_ / c); if (neg) { n = -n; } } } Rat!T opBinary(string op)(T o) if (op == "*"){ if (d == 0) { return this; } return Rat!T(n * o, d); } Rat!T opBinary(string op)(T o) if (op == "/"){ if (d == 0) { return this; } return Rat!T(n, d * o); } Rat!T opBinary(string op)(T o) if (op == "+"){ if (d == 0) { return this; } return Rat!T(n + d * o, d); } bool opEqual(Rat!T o) { return n == o.n && d == o.d; } } int[2][50] points; void main() { bool[Tuple!(Rat!long, Rat!long)] lines; int n; scanf("%d ", &n); foreach(i; 0..n) { scanf("%d %d ", &points[i][0], &points[i][1]); } debug writeln(points); foreach(i; 0..n) { foreach(j; 0..i) { auto t = Rat!long(points[i][1]-points[j][1], points[i][0]-points[j][0]); auto d = t.d ? (t * -points[i][0]) + points[i][1] : Rat!long(points[i][0], 1); debug writeln(t, d, t == d); lines[tuple(t, d)] = true; } } debug writeln(lines); int[Rat!long] tgroup; foreach(t, d; lines.byKey) { tgroup[t]++; } long ans = lines.length * (lines.length - 1) / 2; foreach(t; tgroup.byKey) { ans -= tgroup[t] * (tgroup[t]-1) / 2; } writeln(ans); }
D
import std.stdio; import std.algorithm; import std.range; import std.array; import std.string; import std.conv; void main(){ auto n = readln.chomp.to!long; if(is_harshad(n)){ writeln("Yes"); }else{ writeln("No"); } } bool is_harshad(long n){ auto fx = n.to!string.map!((c) => [c].to!long).reduce!("a+b"); return n % fx == 0; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int a, b; scan(a, b); int t = 1; int ans; while (t < b) { t += a - 1; ans++; } 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; import std.string; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.conv; import std.typecons; void main() { (rs.uniq.array.length - 1).writeln; } // =================================== T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (isBasicType!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } int ri() { return readAs!int; } string rs() { return readln.chomp; }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional; import std.algorithm, std.container; void main() { auto N = scanElem; auto M = scanElem; auto Q = scanElem; long[501][501] table; foreach(i; 0..M) { long l = scanElem; long r = scanElem; table[r][l] += 1; } foreach(x; 1..501) { foreach(y; 1..501) { table[x][y] += table[x-1][y]; table[x][y] += table[x][y-1]; table[x][y] -= table[x-1][y-1]; } } foreach(i; 0..Q) { long p = scanElem; long q = scanElem; writeln(table[q][q] - table[p-1][q] - table[q][p-1] + table[p-1][p-1]); } } void scanValues(TList...)(ref TList list) { auto lit = readln.splitter; foreach (ref e; list) { e = lit.fornt.to!(typeof(e)); lit.popFront; } } T[] scanArray(T = long)() { return readln.split.to!(long[]); } void scanStructs(T)(ref T[] t, size_t n) { t.length = n; foreach (ref e; t) { auto line = readln.split; foreach (i, ref v; e.tupleof) { v = line[i].to!(typeof(v)); } } } T scanElem(T = long)() { string 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); } } } 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; } 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.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto c1 = readln.chomp; auto c2 = readln.chomp; writeln(c1[0] == c2[2] && c1[1] == c2[1] && c1[2] == c2[0] ? "YES" : "NO"); }
D
import std.algorithm; import std.array; import std.ascii; import std.container; import std.conv; import std.format; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; void main() { int n = readln.chomp.to!int; auto a = new long[][](n); foreach (i; 0 .. n) { a[i] = readln.chomp.split.map!(to!long).array; } long sum = 0; bool yes = true; foreach (i; 0 .. n) { foreach (j; i + 1 .. n) { bool ok = true; foreach (k; 0 .. n) { if (k == i || k == j) { continue; } yes &= a[i][j] <= a[i][k] + a[k][j]; ok &= a[i][j] < a[i][k] + a[k][j]; } if (ok) { sum += a[i][j]; } } } if (yes) sum.writeln; else writeln = -1; }
D
// Your code here! import std.stdio,std.conv,std.string,std.algorithm,std.array,std.math; void main(){ auto s=readln().chomp().split().map!(to!int); int a=s[0],b=s[1],x=s[2]; if(a<=x && x<=a+b) writeln("YES"); else writeln("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, c; readV(a, b, c); writeln(max(a*10+b+c, a+b*10+c, a+b+c*10)); }
D
import std.stdio; import std.algorithm; import std.conv; import std.numeric; import std.math; import std.string; void main() { auto n = to!int(chomp(readln())); auto a1 = n / 100; auto a2 = (n / 10) % 10; auto a3 = n % 10; if (a1 == 1) a1 = 9; else a1 = 1; if (a2 == 1) a2 = 9; else a2 = 1; if (a3 == 1) a3 = 9; else a3 = 1; writeln(a1 * 100 + a2 * 10 + a3); stdout.flush(); }
D
import std.stdio, std.conv, std.string, std.range, std.typecons, std.format; import std.algorithm.searching, std.algorithm.sorting, std.algorithm.iteration, std.algorithm.mutation; void main() { readln(); int[] input = readln().split.to!(int[]); auto max = input.reduce!"a<b?b:a"; auto sum = input.sum() - max; writeln(sum>max?"Yes":"No"); }
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, "l ", long, "r"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long d, n; scan(d, n); if(n < 100) writeln(n * 100 ^^ d); else writeln(100 ^^ d + 100 ^^ (d + 1)); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
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() { auto S = sread(); auto T = "ACGT"; auto U = "TGCA"; foreach (i; 0 .. 4) { if (S[0] == T[i]) { writeln(U[i]); return; } } }
D
void main() { long n = rdElem; calc(n).writeln; } long calc(long x) { long result = inf; for (long i = 1; i * i <= x; ++i) { if (x % i == 0) { result = min(result, i+x/i-2); } } 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.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
void main() { auto N = ri; if(N == 1) { writeln("Hello World"); } else { auto A = ri, B = ri; writeln(A + B); } } // =================================== 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 core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; // dfmt on void main() { long A, B, C; scan(A, B, C); if ((A & 1) == 0 || (B & 1) == 0 || (C & 1) == 0) writeln(0); else min(A * B, B * C, C * A).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; long ans; if (X < 600) ans = 8; else if (X < 800) ans = 7; else if (X < 1000) ans = 6; else if (X < 1200) ans = 5; else if (X < 1400) ans = 4; else if (X < 1600) ans = 3; else if (X < 1800) ans = 2; else ans = 1; writeln(ans); stdout.flush; debug readln; }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int H, W; scan(H, W); auto ng = iota(H).map!(i => readln.chomp.map!(ch => ch == '#').array).array; auto dp = new long[][](H, W); dp[0][0] = 1; foreach (i ; 0 .. H) { foreach (j ; 0 .. W) { if (ng[i][j]) continue; if (i > 0) { dp[i][j] += dp[i - 1][j]; dp[i][j] %= mod; } if (j > 0) { dp[i][j] += dp[i][j - 1]; dp[i][j] %= mod; } } } auto ans = dp[H - 1][W - 1]; writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto as = readln.split.to!(long[]); auto cs = new long[](N); auto ds = new long[](N); cs[0] = as[0]; foreach (i; 1..N) cs[i] = gcd(cs[i-1], as[i]); ds[$-1] = as[$-1]; foreach_reverse (i; 0..N-1) ds[i] = gcd(ds[i+1], as[i]); long r = max(ds[1], cs[$-2]); foreach (i; 1..N-1) r = max(r, gcd(cs[i-1], ds[i+1])); writeln(r); }
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; const long p = 1_000_000_000 + 7; void main(){ auto n = readInt(); long[] a, b; foreach(i; iota(n)) { auto ab = readInts(); a ~= ab[0]; b ~= ab[1]; } long ans; foreach_reverse(i; iota(n)){ a[i] += ans; if(a[i] % b[i] != 0) { ans += b[i] - (a[i] % b[i]); } } writeln(ans); }
D
import std.stdio, std.string, std.conv; import std.array, std.algorithm, std.range; void main() { writeln(reduce!((a,_)=>(a*105+99999)/100000*1000)(100000L,iota(readln().chomp().to!long()))); }
D
import std.functional, std.algorithm, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv; void main() { ulong[] NM = readln.chomp.split.to!(ulong[]); ulong n = NM[0], m = NM[1]; ulong s, c; if (n <= m / 2) { writeln(n + (m - 2*n) / 4); } else { writeln(m / 2); } }
D
import std.string; import std.stdio; import std.conv; import std.algorithm; int[] nums = [0,1,2,3,4,5,6,7,8,9]; int n,s; int saiki(int x,int m,int item){ if(m == s && item == n) return 1; if(x == 10) return 0; return saiki(x+1,m+nums[x],item+1) + saiki(x+1,m,item); } void main(){ while(true){ auto ss = split(readln()); n = to!int(ss[0]); s = to!int(ss[1]); if(n == 0 && s == 0) break; writeln(saiki(0,0,0)); } }
D
import std.stdio; import std.algorithm; import std.conv; import std.numeric; import std.math; import std.string; void main() { auto tokens = split(chomp(readln())); auto N = to!ulong(tokens[0]); auto M = to!ulong(tokens[1]); string S = chomp(readln()); string T = chomp(readln()); ulong lcm = (N * M) / gcd(N, M); if (S[0] != T[0]) writeln("-1"); else { uint nr = cast(uint)(lcm / N); uint mr = cast(uint)(lcm / M); bool flag = true; foreach (i; 1..cast(uint)N) { uint p1= i * nr; if (p1 % mr != 0) continue; //foreach (j; 1..M) { uint j = p1 / mr; //uint p2 = j * mr; //if (p1 == p2) { if (S[i] != T[j]) { flag = false; break; } } } } ulong r = nr > mr ? nr * N : mr * M; if (flag) writeln(r); else writeln("-1"); } stdout.flush(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto A = RDA; long ans; foreach (i; 0..N) { ans = gcd(ans, A[i]); } writeln(ans); stdout.flush(); debug readln(); }
D
import std.functional, std.algorithm, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv; void main() { int m = readln.chomp.to!int; writeln(48 - m); }
D
import std.stdio; import std.algorithm; import std.conv; import std.string; void main() { auto k = to!int(chomp(readln())); auto a = k / 2; auto b = k / 2 + k % 2; writeln(a * b); stdout.flush(); }
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; alias A = Tuple!(int, "x", int, "y", int, "h"); void main() { int n = readint; A[] a; for (int i = 0; i < n; i++) { auto xyh = readints; a ~= A(xyh[0], xyh[1], xyh[2]); } // 中心位置を全探索 for (int x = 0; x <= 100; x++) { for (int y = 0; y <= 100; y++) { bool found = true; bool[int] set; // 高さ候補 foreach (e; a) { // 高さ 0 以下はキャップされるので、正しい h が得られない if (e.h == 0) continue; int h = abs(x - e.x) + abs(y - e.y) + e.h; set[h] = true; if (set.length > 1) { // NG(候補が複数ある) found = false; break; } } if (set.length != 1) continue; int h = set.keys.front; // ただ一つの h の候補 // h <= 0 以下のものも含めて答え合わせする foreach (e; a) { if (e.h != max(0, h - abs(x - e.x) - abs(y - e.y))) { // 答え不一致 found = false; break; } } if (found) { writeln(x, " ", y, " ", h); return; } } } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; int abs(int n) { return n < 0 ? -n : n; } void main() { int[] arr = readln.chomp.split.to!(int[]); bool flg = false; writeln( (abs(arr[0] - arr[2]) <= arr[3] || abs(arr[0] - arr[1]) <= arr[3] && abs(arr[1] - arr[2]) <= arr[3]) ? "Yes" : "No" ); }
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 S = RD!string; auto T = RD!string; writeln(T ~ S); stdout.flush; debug readln; }
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 n = readint(); int[] a = 0 ~ readints() ~ 0; auto d = new int[n + 1]; foreach (i; 0..n+1) { d[i] = abs(a[i] - a[i+1]); } long D = d.sum; foreach (i; 0..n) { long x = D - d[i] - d[i+1] + abs(a[i] - a[i+2]); writeln(x); } }
D
import std.stdio; import std.algorithm; import std.string; import std.functional; import std.array; import std.conv; import std.math; import std.typecons; import std.regex; import std.range; char[8][8] s; void bom(int x,int y){ s[y][x] = '0'; for(int dx=-3;dx<=3;dx++){ int nx = x + dx; if(0<=nx&&nx<8) if(s[y][nx]=='1'){ bom(nx,y); } } for(int dy=-3;dy<=3;dy++){ int ny = y + dy; if(0<=ny&&ny<8) if(s[ny][x]=='1'){ bom(x,ny); } } } void main(){ int n = readln().chomp().to!int; for(int i=0;i<n;i++){ readln(); for(int j=0;j<8;j++){ string ss = readln().chomp(); for(int k=0;k<8;k++) s[j][k] = ss[k]; } int x = readln().chomp().to!int; int y = readln().chomp().to!int; bom(x-1,y-1); writeln("Data ",i+1,":"); for(int k=0;k<8;k++){ for(int l=0;l<8;l++){ write(s[k][l]); } writeln(); } } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void solve() { auto N = readln.chomp.to!long; long x; foreach (long i; 0..32) { x += x + (2L^^i)^^2; if (N >= x) { N -= x; } else { writeln(i); return; } } } void main() { auto T = readln.chomp.to!int; foreach (_; 0..T) { solve(); } }
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, c, x, y; readV(a, b, c, x, y); auto ans = 10^^9; foreach (nc; 0..2*max(x,y)+1) { auto na = max(0, x-nc/2), nb = max(0, y-nc/2); ans = min(ans, na*a+nb*b+nc*c); } writeln(ans); }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; void main() { readln.chomp.to!int.rep!readln.map!chomp.map!(to!long).fold!( (a, b) => a/gcd(a, b)*b )(1L).writeln; } // ---------------------------------------------- void times(alias fun)(int n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { auto minElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto minimum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b < minimum) { element = a; minimum = b; } } return element; } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto maximum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b > maximum) { element = a; maximum = b; } } return element; } }
D
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } void main() { auto I = readln.split.to!(long[]); auto N = I[0]; auto K = I[1]; long solve() { long ketasu = 1; for(auto i = K; N >= i; i *= K) { ketasu++; } return ketasu; } solve().writeln; }
D
import std.stdio; import std.algorithm; import std.math; import std.conv; import std.string; T readNum(T)(){ return readStr.to!T; } T[] readNums(T)(){ return readStr.split.to!(T[]); } string readStr(){ return readln.chomp; } void main(){ auto n = readNum!int; auto w = readNums!int; int ans = 10001; foreach(i; 1 .. n){ ans = min(ans, abs(w[0 .. i].sum - w[i .. $].sum)); } writeln(ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math; void main() { auto n = readln.chomp.to!int; auto ps = readln.split.to!(int[]); int cnt; if (ps[0] == 1) { swap(ps[0], ps[1]); cnt++; } foreach (i; 1..n) if (ps[i] == i+1) { swap(ps[i], ps[i+1]); cnt++; } if (ps[$-1] == n) { swap(ps[$-1], ps[$-2]); cnt++; } writeln(cnt); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.math; import std.regex; import std.range; void main() { auto N = readln.chomp.to!int; auto A1 = readln.split.to!(int[]); auto A2 = readln.split.to!(int[]); int res; int tmp; foreach(i; 0..N) { tmp += A1[i]; res = max(res, tmp+A2[i..N].sum); } res.writeln; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; long calc(string[] g) { int rows = cast(int) g.length; int cols = cast(int) g[0].length; int toIndex(int row, int col) { return row * cols + col; } auto uf = new UnionFind(rows * cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (i + 1 < rows) { if (g[i][j] != g[i + 1][j]) uf.unite(toIndex(i, j), toIndex(i+1, j)); } if (j + 1 < cols) { if (g[i][j] != g[i][j + 1]) uf.unite(toIndex(i, j), toIndex(i, j+1)); } } } long ans = 0; bool[int] used; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { int root = uf.root(toIndex(i, j)); if (root in used) continue; used[root] = true; long a = 0; // 黒の個数 long b = 0; // 白の個数 // root に属する全てのノード uf.doEach(root, (node) { int row = node / cols; int col = node % cols; if (g[row][col] == '#') a++; else b++; }); ans += a * b; } } return ans; } void main() { auto hw = readints; int h = hw[0], w = hw[1]; string[] g; for (int i = 0; i < h; i++) { g ~= read!string; } writeln(calc(g)); } class UnionFind { private int[] _data; private int[] _nexts; // 次の要素。なければ -1 private int[] _tails; // 末尾の要素 this(int n) { _data = new int[](n + 1); _nexts = new int[](n + 1); _tails = new int[](n + 1); _data[] = -1; _nexts[] = -1; for (int i = 0; i < _tails.length; i++) _tails[i] = i; } int root(int a) { if (_data[a] < 0) return a; return _data[a] = root(_data[a]); } bool unite(int a, int b) { int ra = root(a); int rb = root(b); if (ra != rb) { if (_data[rb] < _data[ra]) swap(ra, rb); _data[ra] += _data[rb]; // ra に rb を繋げる _data[rb] = ra; // ra の末尾の後ろに rb を繋げる _nexts[_tails[ra]] = rb; // ra の末尾が rb の末尾になる _tails[ra] = _tails[rb]; } return ra != rb; } bool isSame(int a, int b) { return root(a) == root(b); } /// a が属する集合のサイズ int groupSize(int a) { return -_data[root(a)]; } /// a が属する集合の要素全て void doEach(int a, void delegate(int) fn) { auto node = root(a); while (node != -1) { fn(node); node = _nexts[node]; } } }
D
import std; void main() { int t; scanf("%d", &t); getchar(); foreach(_; 0..t) { auto s = readln.strip; auto z = count(s, '0'); auto o = s.length - z; if (z == 0 || o == 0) writeln(0); else if (o == z) if (s.length > 2) writeln(o - 1); else writeln(0); else writeln(min(o, z)); } }
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() { auto S = sread(); long N = S.length; auto dp = new long[][](13, N + 1); dp[0][0] = 1; foreach (i; 0 .. N) { if (S[i] == '?') { foreach (j; 0 .. 10) foreach (k; 0 .. 13) { dp[(k * 10 + j) % 13][i + 1] += dp[k][i]; dp[(k * 10 + j) % 13][i + 1] %= MOD; } continue; } foreach (j; 0 .. 13) { dp[(j * 10 + (S[i] - '0')) % 13][i + 1] += dp[j][i]; dp[(j * 10 + (S[i] - '0')) % 13][i + 1] %= MOD; } } writeln(dp[5][N]); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto T = readln.chomp.to!int; long[63] ns, cs; foreach (i; 0..63) ns[i] = (1L<<(i+1))-1; foreach (_; 0..T) { auto N = readln.chomp.to!int; auto as = readln.split.to!(long[]); cs[] = 0; foreach (i; 0..N-1) { if (as[i+1] >= as[i]) continue; foreach (j, n; ns) if (as[i] - as[i+1] <= n) { ++cs[j]; as[i+1] = as[i]; break; } } foreach_reverse (i, c; cs) if (c > 0) { writeln(i+1); goto ok; } writeln(0); ok: } }
D
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } enum MO = 10L^^9 + 7; void main() { try { for (; ; ) { const N = readInt(); const S = readToken(); long ans = 1; long now = 0; foreach (i; 0 .. 2 * N) { if (S[i] == "WB"[cast(int)(now % 2)]) { // close if (now <= 0) { ans = 0; break; } ans *= now; ans %= MO; --now; } else { // open ++now; } } if (now != 0) { ans = 0; } foreach (k; 1 .. N + 1) { ans *= k; ans %= MO; } writeln(ans); } } catch (EOFException e) { } }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio, std.bitmanip; int N; string S; char[] ans; bool solve() { foreach (i; 1..N-1) { auto prev = ans[(i-1+N)%N]; if (ans[i] == 'S' && S[i] == 'o') { ans[(i+1)%N] = prev; } else if (ans[i] == 'S' && S[i] == 'x') { ans[(i+1)%N] = (prev == 'S') ? 'W' : 'S'; } else if (ans[i] == 'W' && S[i] == 'o') { ans[(i+1)%N] = (prev == 'S') ? 'W' : 'S'; } else { ans[(i+1)%N] = prev; } } foreach (i; 0..N) { auto prev = ans[(i-1+N)%N]; auto next = ans[(i+1)%N]; if (ans[i] == 'S' && S[i] == 'o' && prev != next) return false; else if (ans[i] == 'S' && S[i] == 'x' && prev == next) return false; else if (ans[i] == 'W' && S[i] == 'o' && prev == next) return false; else if (ans[i] == 'W' && S[i] == 'x' && prev != next) return false; } return true; } void main() { N = readln.chomp.to!int; S = readln.chomp; ans = new char[](N); auto cand = ["SS", "SW", "WS", "WW"]; foreach (i; 0..4) { foreach (j; 0..N) ans[j] = '*'; ans[0] = cand[i][0]; ans[1] = cand[i][1]; if (solve) { ans.writeln; return; } } writeln(-1); }
D
import std; void main() { string s = read; string t = read; writeln(t.startsWith(s) ? "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=string)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readints = reads!int;
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 s = sread(); s.length -= 2; while (s.length > 0) { if (s.is_evenstring()) { s.length.writeln(); return; } s.length -= 2; } assert(0); } bool is_evenstring(string s) { return (s[0 .. $ / 2] == s[$ / 2 .. $]); } 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; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; import std.container; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } void main(){ int n = read.to!int; int[] as; foreach(i; 0 .. n) as ~= read.to!int; bool[int] aset; foreach(a; as) aset[a] = 1; if(aset.keys.length > 2){ debug writeln("A"); writeln("No"); return; } if(aset.keys.length == 2){ int m; if(aset.keys[0] + 1 == aset.keys[1]) m = aset.keys[0]; else if(aset.keys[1] + 1 == aset.keys[0]) m = aset.keys[1]; else{ debug writeln("B"); writeln("No"); return; } int k = 0; foreach(a; as) if(a == m) k += 1; if(m < k){ debug writeln("C-1"); writeln("No"); return; } if(k + (n - k) / 2 < m + 1){ debug writeln("C-2"); writeln("No"); return; } debug writeln("D"); writeln("Yes"); return; } else{ int m = aset.keys[0]; if(m == n - 1){ debug writeln("E"); writeln("Yes"); return; } if(n >= m * 2){ debug writeln("F"); writeln("Yes"); return; } debug writeln("G"); writeln("No"); return; } } /* 差は最大で1である。 (たとえば9と10しか存在しないなど) 9と言うのは他の誰ともちがうぼうしをかぶっている人 10と言うのは他の誰かと同じぼうしをかぶっている人 9と言った人の人数+(10と言った人の人数 / 2)に注目。 それが10以上ならOK。 差が0つまり全員同じ数を言った場合は2つのパターンがある。 全員他の人と違う帽子をかぶっているパターン。 これは人数-1を言っているならOK。 全員誰かと同じ帽子をかぶっているパターン。 これは人数がその言った数の2倍以上ならOK。 */
D
import std.stdio; import std.string; void main() { int n, k; scanf("%d %d\n", &n, &k); auto l = readln.chomp(); write(l[0..k-1] ~ l[k-1..k].toLower ~ l[k..$]); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(string s) { int ans = 0; int len = 0; foreach (c; s) { if ("ACGT".canFind(c)) len++; else len = 0; ans = max(ans, len); } return ans; } void main() { string s = read!string; writeln(calc(s)); }
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 N, K; sc.scan(N, K); if ((N - K) >= N / 2) { writeln("YES"); } else { writeln("NO"); } }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; long calc(int n, int[] a, int[] b) { long ans = 0; for (int i = 0; i < n; i++) { int d = min(a[i], b[i]); ans += d; if (a[i] < b[i]) { int d2 = min(a[i+1], b[i] - a[i]); a[i+1] -= d2; ans += d2; } } return ans; } void main() { int n; scan(n); auto a = readints; auto b = readints; writeln(calc(n, a, b)); }
D
// Vicfred // https://atcoder.jp/contests/abc168/tasks/abc168_b // implementation import std.conv; import std.stdio; import std.string; void main() { int K = readln.chomp.to!int; string S = readln.strip; if(S.length > K) { S[0..K].write; "...".writeln; } else { S.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; } T read(T)(){ return read.to!T; } T[] read(T)(long n){ return n.iota.map!(_ => read!T).array; } T[][] read(T)(long m, long n){ return m.iota.map!(_ => read!T(n)).array; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ int n = read.to!int; int[] as; foreach(i; 0 .. n) as ~= read.to!int; // 1: Circle, 2: Triangle, 3: Square long ans; bool f; // was 3 -> 1. foreach(i; 0 .. n - 1){ int a = as[i] * 10 + as[i + 1]; switch(a){ case 11: case 22: case 33: case 23: case 32: writeln("Infinite"); return; case 12: if(f) ans += 2; else ans += 3; f = 0; break; case 13: ans += 4; f = 0; break; case 21: ans += 3; f = 0; break; case 31: ans += 4; f = 1; break; default: assert(0); } } writeln("Finite"); ans.writeln; }
D
void main() { dchar[] n = readln.chomp.to!(dchar[]); dchar[] m = n.dup; reverse(m); writeln(n == m ? "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.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto n = readln.strip.to !(int); auto a = readln.splitter.map !(to !(int)).array; int [] d; int i = 0; while (i < n * 2) { int j = i; do { i += 1; } while (i < n * 2 && a[i] < a[j]); d ~= i - j; } auto f = new int [n * 2 + 1]; f[] = int.max; f[0] = -1; foreach (int k, ref c; d) { for (int p = c; p <= n * 2; p++) { if (f[p] == int.max && f[p - c] < k) { f[p] = k; } } } writeln (f[n] < int.max ? "YES" : "NO"); } }
D
import std.stdio; import std.conv; import std.string; import core.bitop; void main() { auto N = readln.chomp.to!long; immutable march = "MARCH"; long[march.length] list; foreach (_; 0 .. N) { auto name = readln(); foreach (i, c; march) { if (name[0] == c) { list[i]++; } } } long nonzero; byte mask; foreach (i, n; list) { if (n != 0) { mask |= 1 << i; nonzero++; } } long result; foreach (comb; 0b0 .. 0b111111) if ((comb & mask) == comb && popcnt(comb) == 3) { long tmp = 1; foreach (i, n; list) if ((comb & (1 << i)) != 0) { tmp *= n; } result += tmp; } writeln(result); }
D
import std.stdio; import std.string; import std.conv; void main() { string[] inputs = split(readln()); int A = to!int(inputs[0]); int B = to!int(inputs[1]); int C = to!int(inputs[2]); int D = to!int(inputs[3]); if(A + B > C + D) writeln("Left"); else if(A + B == C + D) writeln("Balanced"); else writeln("Right"); }
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; // }}} // 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 n = cin.next!int, y = cin.next!int; writeln(n * y % 2 == 0 ? "Even" : "Odd"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto T = readln.chomp.to!int; foreach (_; 0..T) { readln; auto as = readln.split.to!(int[]); auto x = as[0]%2; foreach (a; as) if (x != a%2) goto ng; writeln("YES"); continue; ng: writeln("NO"); } }
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 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 main() { auto n = RD!int; auto q = RD!int; auto ans = new bool[](q); auto cell = new bool[][](2, n); bool[int] set; foreach (i; 0..q) { auto r = RD!int-1; auto c = RD!int-1; cell[r][c] = !cell[r][c]; debug writeln(cell[0]); debug writeln(cell[1]); if (cell[r][c]) { bool ok = i == 0 ? true : ans[i-1]; foreach (j; max(0, c-1)..min(n, c+2)) { if (cell[r^1][j]) { ok = false; set[r*n+c] = true; } } ans[i] = ok; } else { if (set.get(r*n+c, false)) set.remove(r*n+c); foreach (j; max(0, c-1)..min(n, c+2)) { if (!cell[r^1][j]) continue; if (set.get((r^1)*n+j, false) == false) continue; bool ok = true; foreach (k; max(0, j-1)..min(n, j+2)) { if (cell[r][k]) ok = false; } if (ok) set.remove((r^1)*n+j); } ans[i] = set.length == 0; } } foreach (e; ans) writeln(e ? "Yes" : "No"); 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() { int n, q; readV(n, q); string s; readV(s); auto a = new int[](n); foreach (i; 1..n) a[i] = s[i-1..i+1] == "AC"; auto ac = a.cumulativeSum; foreach (_; 0..q) { int l, r; readV(l, r); writeln(ac[l..r]); } } class CumulativeSum(T) { size_t n; T[] s; this(T[] a) { n = a.length; s = new T[](n+1); static if (T.init != 0) s[0] = T(0); foreach (i; 0..n) s[i+1] = s[i] + a[i]; } pure auto opSlice(size_t l, size_t r) { return s[r]-s[l]; } pure auto opDollar() { return n; } } auto cumulativeSum(T)(T[] a) { return new CumulativeSum!T(a); }
D
import std.stdio, std.string, std.conv, std.algorithm; void main() { auto A = readln.chomp.to!int; auto B = readln.chomp.to!int; auto C = readln.chomp.to!int; auto D = readln.chomp.to!int; writeln(min(A, B) + min(C, D)); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void main() { auto N = readln.chomp.to!long; auto AS = readln.split.to!(long[]); auto BS = readln.split.to!(long[]); long solve(long N, long[] XS, long[] YS) { int[] ps; foreach (i; 0..3) if (XS[i] < YS[i]) ps ~= i; if (ps.length == 1) { auto i = ps[0]; N = N/XS[i]*YS[i] + N%XS[i]; } else if (ps.length == 2) { auto i = ps[0]; auto j = ps[1]; auto n = N; foreach (x; 0..N/XS[i]+1) { auto nn = N - x*XS[i]; n = max(n, x*YS[i] + nn/XS[j]*YS[j] + nn%XS[j]); } N = n; } else if (ps.length == 3) { auto n = N; foreach (x; 0..N/XS[0]+1) { auto nn = N - x*XS[0]; foreach (y; 0..nn/XS[1]+1) { auto nnn = nn - y*XS[1]; auto z = nnn/XS[2]; n = max(n, x*YS[0] + y*YS[1] + z*YS[2] + nnn%XS[2]); } } N = n; } return N; } writeln(solve(solve(N, AS, BS), BS, AS)); }
D
import std; alias Book = Tuple!(int, "cost", int[], "ss"); int calc(int x, Book[] books) { int p = cast(int)books.length; int n = cast(int)books[0].ss.length; int ans = int.max; for (int i = 0; i < (1 << p); i++) { int cost = 0; auto ss = new int[n]; for (int j = 0; j < p; j++) { if (i & (1 << j)) { cost += books[j].cost; foreach (k, s; books[j].ss) { ss[k] += s; } } } if (ss.all!(e => e >= x)) { ans = min(ans, cost); } } return ans == int.max ? -1 : ans; } void main() { int n, m, x; scan(n, m, x); Book[] books; foreach (_; 0..n) { auto t = readints; books ~= Book(t[0], t[1..$]); } writeln(calc(x, books)); } void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T=string)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readints = reads!int;
D
import std.stdio, std.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(){ int n = scan!int; long[] as = scan!long(n); long b = 0; long ans = 0; foreach(a; as){ if(b > a) ans += b - a; b.raiseTo(a); } ans.writeln; }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; immutable inf = 10^^9 + 7; void main() { int n, m; scan(n, m); auto c = readln.split.to!(int[]); auto uf = UnionFind(n); foreach (i ; 0 .. m) { int xi, yi; scan(xi, yi); xi--, yi--; uf.unite(xi, yi); } auto d = new int[](n); d[] = inf; foreach (i ; 0 .. n) { int r = uf.find_root(i); d[r] = min(d[r], c[i]); } long ans; foreach (i ; 0 .. n) { if (d[i] < inf) ans += d[i]; } writeln(ans); } struct UnionFind { private { int N; int[] p; int[] rank; } this (int n) { N = n; p = iota(N).array; rank = new int[](N); } int find_root(int x) { if (p[x] != x) { p[x] = find_root(p[x]); } return p[x]; } bool same(int x, int y) { return find_root(x) == find_root(y); } void unite(int x, int y) { int u = find_root(x), v = find_root(y); if (u == v) return; if (rank[u] < rank[v]) { p[u] = v; } else { p[v] = u; if (rank[u] == rank[v]) { rank[u]++; } } } } 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); } } } struct Queue(T) { private { int N, head, tail; T[] data; } this(int n) { N = n + 1; data = new T[](N); } bool empty() { return head == tail; } bool full() { return (tail + 1) % N == head; } T front() { return data[head]; } void push(T x) { assert(!full); data[tail++] = x; tail %= N; } void pop() { assert(!empty); head = (head + 1) % N; } void clear() { head = tail = 0; } }
D
/+ dub.sdl: name "D" dependency "dcomp" version=">=0.7.3" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; int main() { Scanner sc = new Scanner(stdin); string s; sc.read(s); int n = s.length.to!int; int[] sm = new int[n+1]; foreach (i, c; s) { int d = c - 'a'; sm[i+1] = sm[i]; sm[i+1] ^= (1 << d); } int[] dp = new int[1<<26]; dp[] = 10^^9; dp[sm[n]] = 0; foreach_reverse (i; 1..n) { int f = sm[i]; foreach (j; 0..26) { int g = f ^ (1<<j); dp[f] = min(dp[f], 1+dp[g]); } } int ans = 1+dp[0]; foreach (j; 0..26) { ans = min(ans, 1 + dp[1<<j]); } writeln(ans); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; // import dcomp.array; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { FastAppender!(E[]) buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/array.d */ // module dcomp.array; T[N] fixed(T, size_t N)(T[N] a) {return a;} struct FastAppender(A, size_t MIN = 4) { import std.algorithm : max; import std.conv; import std.range.primitives : ElementEncodingType; import core.stdc.string : memcpy; private alias T = ElementEncodingType!A; private T* _data; private uint len, cap; @property size_t length() const {return len;} bool empty() const { return len == 0; } void reserve(size_t nlen) { import core.memory : GC; if (nlen <= cap) return; void* nx = GC.malloc(nlen * T.sizeof); cap = nlen.to!uint; if (len) memcpy(nx, _data, len * T.sizeof); _data = cast(T*)(nx); } void free() { import core.memory : GC; GC.free(_data); } void opOpAssign(string op : "~")(T item) { if (len == cap) { reserve(max(MIN, cap*2)); } _data[len++] = item; } void insertBack(T item) { this ~= item; } void removeBack() { len--; } void clear() { len = 0; } ref inout(T) back() inout { assert(len); return _data[len-1]; } ref inout(T) opIndex(size_t i) inout { return _data[i]; } T[] data() { return (_data) ? _data[0..len] : null; } } /* This source code generated by dcomp and include dcomp's source code. dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp) dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt) */
D
import std.stdio; import std.string; import std.conv; import std.math; int main() { auto digit = 0; string buf; while ((buf = strip(readln())) != null) { digit = calc_digit(buf); writeln(calc_digit(buf)); } return 0; } int calc_digit(string src) { string[] seed = split(src); int sum = to!(uint)(seed[0]) + to!(uint)(seed[1]); uint digit = 0; while (pow(10, digit) <= sum) { digit += 1; } return digit; }
D
import std.stdio, std.string, std.conv; import std.typecons; import std.algorithm, std.array, std.range, std.container; import std.math; void main() { auto X = readln.split[0].to!(long); foreach (A; 0 .. 10^^3+1) foreach (B; 0 .. A) { if (A^^5 - B^^5 == X) { writeln (A, " ", B); return; } if (A^^5 + B^^5 == X) { writeln (A, " ", -B); return; } } assert(0); }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { int N, K; scan(N); scan(K); auto x = new int[](N); x = readln.split.to!(int[]); int ans; foreach (i ; 0 .. N) { ans += min(2 * x[i], 2 * (K - x[i])); } writeln(ans); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.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; immutable int INF = 1 << 29; bool solve() { auto N = readln.chomp.to!int; auto P = readln.split.map!(to!int).array; auto X = readln.split.map!(to!int).array; auto edges = new int[][](N); foreach (i; 0..N-1) { edges[i+1] ~= P[i]-1; edges[P[i]-1] ~= i+1; } int dfs(int n, int p) { auto dp = new int[][](2, X[n]+1); int cur = 0, tar = 1; dp[cur].fill(INF); dp[cur][0] = 0; foreach (m; edges[n]) { if (m == p) continue; dp[tar].fill(INF); int w = dfs(m, n); int b = X[m]; foreach (i; 0..X[n]+1) { if (dp[cur][i] != INF) { if (i + w <= X[n]) { dp[tar][i+w] = min(dp[tar][i+w], dp[cur][i]+b); } if (i + b <= X[n]) { dp[tar][i+b] = min(dp[tar][i+b], dp[cur][i]+w); } } } cur ^= 1, tar ^= 1; } return dp[cur].reduce!min; } return dfs(0, -1) != INF; } void main() { writeln( solve ? "POSSIBLE" : "IMPOSSIBLE" ); }
D
import std.stdio; import std.algorithm; import std.string; import std.functional; import std.array; import std.conv; import std.math; import std.typecons; import std.regex; import std.range; void main(){ string[6] ans; for(int i=1;i<=6;i++) ans[i-1] = to!char(i + '0') ~ ":"; int n = readln().chomp().to!int; for(int i=0;i<n;i++){ double h = readln().chomp().to!double; if(h < 165.0) ans[0] ~= '*'; else if(h < 170.0) ans[1] ~= '*'; else if(h < 175.0) ans[2] ~= '*'; else if(h < 180.0) ans[3] ~= '*'; else if(h < 185.0) ans[4] ~= '*'; else ans[5] ~= '*'; } foreach(i ; ans) writeln(i); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; int[10^^5] NS; void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; foreach (_; 0..M) { auto ab = readln.split.to!(int[]); auto a = ab[0] - 1; auto b = ab[1] - 1; if (NS[a]) { NS[a] = min(NS[a], b); } else { NS[a] = b; } } int cnt, last = int.max; foreach (i, b; NS[0..N]) { if (i == last) { ++cnt; last = int.max; } if (b) { last = min(b, last); } } writeln(cnt); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto ab = readln.split.to!(int[]); int s, i = 1; while (i <= ab[1] - ab[0]) { s += i; ++i; } writeln(s - ab[1]); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto rd = readln.split, a = rd[0], b = rd[1]; writeln(a == b ? "H" : "D"); }
D
import std.stdio; import std.string; import std.conv; import std.array; import std.range; void main(){ auto a=readln.chomp; auto b=readln.chomp; auto c=readln.chomp; writeln("" ~ a[0] ~ b[1] ~ c[2]); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n, m; rd(n, m); writeln((m*1900+(n-m)*100)*(1<<m)); } 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
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; import std.datetime, std.bigint; int n, a; int[] x; void main() { scan(n, a); x = readln.split.to!(int[]); auto dp = new long[][][](n + 1, n + 1, 2500 + 1); dp[0][0][0] = 1; foreach (i ; 0 .. n) { foreach (j ; 0 .. n + 1) { foreach (k ; 0 .. 2500 + 1) { if (dp[i][j][k] == 0) continue; dp[i + 1][j][k] += dp[i][j][k]; dp[i + 1][j + 1][k + x[i]] += dp[i][j][k]; } } } long ans; foreach (j ; 1 .. n + 1) { ans += dp[n][j][j * a]; } 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
/+ dub.sdl: name "D" dependency "dunkelheit" version=">=0.9.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; long isq(long x) { long l = 0, r = 10L^^10; while (r-l > 1) { long md = (l+r)/2; if (md*(md-1) < x) l = md; else r = md; } return l; } long solve(long a, long b) { if (a > b) swap(a, b); if (a == 1 && b == 1) return 0; long ans = 2*(a-1); long up = min(a*b / (a+1), isq(a*b)); ans += (up-a+1); long lup = (a*b-1) / up; ans += lup - a - 1; return ans; } int main() { Scanner sc = new Scanner(stdin); scope(exit) assert(!sc.hasNext); int q; sc.read(q); foreach (i; 0..q) { long a, b; sc.read(a, b); writeln(solve(a, b)); } return 0; } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */ // module dkh.scanner; // import dkh.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(Args...)(auto ref Args args) { import std.exception; static if (args.length != 0) { enforce(readSingle(args[0])); read(args[1..$]); } } bool hasNext() { return succ(); } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std.algorithm; import std.array; import std.conv; import std.stdio; import std.string; long solve (long [] a) { long a0 = min (a[0], 2); a[0] -= a0; long a3 = min (a[3], 2); a[3] -= a3; long a4 = min (a[4], 2); a[4] -= a4; long res = 0; res += a[1]; a[1] = 0; res += (a[0] / 2) * 2; a[0] %= 2; res += (a[3] / 2) * 2; a[3] %= 2; res += (a[4] / 2) * 2; a[4] %= 2; long add = 0; add = max (add, ((a0 + a[0]) / 2) * 2 + ((a3 + a[3]) / 2) * 2 + ((a4 + a[4]) / 2) * 2); if (a0 > 0 && a3 > 0 && a4 > 0) { add = max (add, 3 + ((a0 + a[0] - 1) / 2) * 2 + ((a3 + a[3] - 1) / 2) * 2 + ((a4 + a[4] - 1) / 2) * 2); } if (a0 > 1 && a3 > 1 && a4 > 1) { add = max (add, 6 + ((a0 + a[0] - 2) / 2) * 2 + ((a3 + a[3] - 2) / 2) * 2 + ((a4 + a[4] - 2) / 2) * 2); } return res + add; } void main () { long [] a; while ((a = readln.split.map !(to !(long)).array).length > 0) { writeln (solve (a)); } }
D
import std.stdio; import std.algorithm; import std.math; import core.stdc.stdio; int[int] compCoord(ref int[] data){ data.sort; int[int] ret; int c=0; foreach(i,d;data){ if((d in ret) is null){ ret[d] = c; data[c++] = d; } } data = data[0..c]; return ret; } void main(){ while(1){ int w,h; scanf("%d%d",&w,&h); if(h==0&&w==0) break; int n; scanf("%d",&n); int size=n*2+2; int[] xs = new int[size]; int[] ys = new int[size]; struct rect{ int x1; int y1; int x2; int y2; } rect[] rs = new rect[n]; for(int i=0;i<n;i++){ scanf("%d%d%d%d",&rs[i].x1,&rs[i].y1,&rs[i].x2,&rs[i].y2); xs[i*2] = rs[i].x1; xs[i*2+1] = rs[i].x2; ys[i*2] = rs[i].y1; ys[i*2+1] = rs[i].y2; } xs[n*2] = 0; xs[n*2+1] = w; ys[n*2] = 0; ys[n*2+1] = h; int[int] xc = xs.compCoord; int[int] yc = ys.compCoord; int[] imos = new int[xs.length*ys.length]; foreach(r;rs){ int cx1 = xc[r.x1]; int cy1 = yc[r.y1]; int cx2 = xc[r.x2]; int cy2 = yc[r.y2]; imos[cy1*xs.length+cx1]++; imos[cy1*xs.length+cx2]--; imos[cy2*xs.length+cx1]--; imos[cy2*xs.length+cx2]++; } for(int i=0;i<ys.length;i++){ int last=0; for(int j=0;j<xs.length;j++){ imos[i*xs.length+j]+=last; last = imos[i*xs.length+j]; } } for(int i=0;i<xs.length;i++){ int last=0; for(int j=0;j<ys.length;j++){ imos[i+j*xs.length]+=last; last = imos[i+j*xs.length]; } } int ans=0; bool[] visited = new bool[xs.length*ys.length]; int[] dx = [0,-1,0,1]; int[] dy = [-1,0,1,0]; int[2][] queue = new int[2][xs.length*ys.length]; int BFS(int y,int x){ if(visited[y*xs.length+x] || imos[y*xs.length+x] != 0) return 0; int qc=0; queue[qc++] = [y,x]; visited[y*xs.length+x]=true; for(int i=0;i<qc;i++){ int py = queue[i][0]; int px = queue[i][1]; for(int k=0;k<4;k++){ int ny = py+dy[k]; int nx = px+dx[k]; if(0<=ny&&ny<ys.length&&ys[ny]<h&&0<=nx&&nx<xs.length&&xs[nx]<w){ if(!visited[ny*xs.length+nx] && imos[ny*xs.length+nx] == 0){ queue[qc++] = [ny,nx]; visited[ny*xs.length+nx]=true; } } } } return 1; } for(int i=0;ys[i]<h;i++){ for(int j=0;xs[j]<w;j++){ ans += BFS(i,j); } } printf("%d\n",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, core.stdc.string; void main() { auto S = readln.chomp; auto T = readln.split.array; foreach (t; T) { if (S[0] == t[0] || S[1] == t[1]) { writeln("YES"); return; } } writeln("NO"); }
D
void main() { long n, m; rdVals(n, m); long[] a = rdRow; max(n-a.sum, -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
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; enum P = 1000000007L; 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; } alias Iw = Tuple!(long, "a", long, "b"); alias Pair = Tuple!(long, "x", long, "y"); void main() { auto N = readln.chomp.to!int; long[Iw] iws; long zz; foreach (_; 0..N) { auto ab = readln.split.to!(long[]); auto A = ab[0]; auto B = ab[1]; if (A < 0) { A = -A; B = -B; } if (A == 0 && B == 0) { ++zz; continue; } else if (A == 0) { B = 1; } else if (B == 0) { A = 1; } else if (A != 0 && B != 0) { auto d = gcd(abs(A), abs(B)); A /= d; B /= d; } auto iw = Iw(A, B); if (iw !in iws) iws[iw] = 0; ++iws[iw]; } Pair[] ps; foreach (k, n; iws) { if (n == 0) continue; auto r = k.b < 0 ? Iw(-k.b, k.a) : Iw(k.b, -k.a); if (r.a == 0) r.b = abs(r.b); if (r in iws && iws[r] > 0) { ps ~= Pair(n, iws[r]); iws[r] = 0; } else { ps ~= Pair(n, 0); } } long r = 1; foreach (p; ps) { if (p.y == 0) { (r *= pow(2, p.x)) %= P; } else { long z = (pow(2, p.x) + pow(2, p.y)) % P; z = (z - 1 + P) % P; (r *= z) %= P; } } writeln((r - 1 + zz + P) % P); }
D
import std.stdio; void main(){ for(int i = 0; i < 1000; i++) writeln("Hello World"); }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random; import std.typecons, std.functional, std.traits,std.concurrency; import std.algorithm, std.container; import core.bitop, core.time, core.memory; import std.bitmanip; import std.regex; enum INF = long.max/3; enum MOD = 10L^^9+7; //辞書順順列はiota(1,N),nextPermituionを使う void end(T)(T v) if(isIntegral!T||isSomeString!T||isSomeChar!T) { import core.stdc.stdlib; writeln(v); exit(0); } T[] scanArray(T = long)() { static char[] scanBuf; readln(scanBuf); return scanBuf.split.to!(T[]); } dchar scanChar() { int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } return cast(dchar)c; } T scanElem(T = long)() { import core.stdc.stdlib; static auto scanBuf = appender!(char[])([]); scanBuf.clear; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { scanBuf ~= cast(char) c; c = getchar; } return scanBuf.data.to!T; } dchar[] scanString(){ return scanElem!(dchar[]); } void main() { if((scanElem*100+scanElem*10+scanElem)%4==0)end("YES");end("NO"); }
D
void main() { int n = readln.chomp.to!int; int[] h = readln.split.to!(int[]); int hs = h[0]; int cnt = 1; foreach (i; 1 .. n) { if (h[i] >= hs) { ++cnt; hs = h[i]; } } cnt.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.container; import std.typecons;
D
import std.stdio; void main() { string input = readln(); int count; foreach(char c; input) { if(c == '1') count++; } writeln(count); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; import std.range; void main(){ auto a=readln.chomp; writeln("ABC",a); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { int n, m; scan(n, m); auto a = new int[n + 2]; for (int i = 0; i < m; i++) { int l, r; scan(l, r); a[l]++; a[r + 1]--; } for (int i = 1; i < a.length; i++) { a[i] += a[i - 1]; } auto ans = a.count!(e => e == m); writeln(ans); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);} void readM(T)(size_t r,size_t c,ref T[][]t){t=new T[][](r);foreach(ref v;t)readA(c,v);} void main() { int n, c; readV(n, c); int[][] d; readM(c, c, d); int[][] a; readM(n, n, a); foreach (i; 0..n) a[i][] -= 1; auto b = new int[][](3, c); foreach (i; 0..n) foreach (j; 0..n) ++b[(i+j)%3][a[i][j]]; auto calc(int[] b, int t) { auto r = 0; foreach (i; 0..c) r += b[i]*d[i][t]; return r; } auto r = 10^^9; foreach (c0; 0..c) foreach (c1; 0..c) foreach (c2; 0..c) { if (c0 == c1 || c0 == c2 || c1 == c2) continue; r = min(r, calc(b[0], c0)+calc(b[1], c1)+calc(b[2], c2)); } writeln(r); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { auto nm = readints; int n = nm[0], m = nm[1]; auto g = new int[n + 1]; for (int i = 0; i < m; i++) { auto ab = readints; int a = ab[0], b = ab[1]; g[a]++; g[b]++; } foreach (i; 1..n+1) { writeln(g[i]); } }
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 N = RD; auto A = RDA; bool ans = true; foreach (e; A) { if (e % 2 == 1) continue; if (e % 3 != 0 && e % 5 != 0) ans = false; } writeln(ans ? "APPROVED" : "DENIED"); stdout.flush; debug readln; }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; T lcm(T)(T a, T b) { return a * (b / gcd(a, b)); } long calc(long a, long b, long c, long d) { // [1, n] で k で割り切れない数の個数 long f(long n, long k) { return n - (n / k); } long x = f(b, c) + f(b, d) - f(b, lcm(c, d)); long y = f(a-1, c) + f(a-1, d) - f(a-1, lcm(c, d)); return x - y; } void main() { long a, b, c, d; scan(a, b, c, d); writeln(calc(a, b, c, d)); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); void main() { auto n = lread(); auto d = aryread(); auto m = lread(); auto t = aryread(); long[long] dd; foreach (i; 0 .. n) { dd[d[i]] = dd.get(d[i], 0) + 1; } // writeln(dd); long[long] tt; foreach (i; 0 .. m) { tt[t[i]] = tt.get(t[i], 0) + 1; } // writeln(tt); foreach (key, value; tt) { if (key in dd) { if (dd[key] < value) { writeln("NO"); return; } } else { writeln("NO"); return; } } writeln("YES"); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
D