code
stringlengths
4
1.01M
language
stringclasses
2 values
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.container; import std.datetime; void main() { auto n = readln.chomp.to!double; int cnt; while (n > 1) { n /= 3; cnt++; } cnt.writeln; }
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; immutable long INF = 1L << 59; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto K = s[2]; auto A = readln.split.map!(to!long).array; auto dp = new long[][](2, N); dp[0] = A.dup; int cur = 0, tar = 1; foreach (i; 1..K) { dp[tar].fill(-INF); auto q = new long[](2*N+10); int x = N/2; int y = N/2; foreach (j; i..N) { int a = j - M - 1; int b = j - 1; if (a >= 0 && x < y && q[x] == a) x++; while (x < y && dp[cur][q[y-1]] < dp[cur][b]) y--; q[y++] = b; dp[tar][j] = dp[cur][q[x]] + A[j] * (i + 1); } swap(cur, tar); } dp[cur].reduce!max.writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; long P = 10^^9+7; long[10^^5+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; } 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) { 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 nk = readln.split.to!(long[]); auto N = nk[0]; auto K = nk[1]; auto NK = N-K; foreach (long i; 1..K+1) { if (i-1 > K-1 || i > NK+1) { writeln(0); continue; } auto b = comb(K-1, i-1); auto r = comb(NK+1, i); writeln((b*r)%P); } }
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, std.datetime; // 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 H, W, N; scan(H, W, N); long SR, SC; scan(SR, SC); auto S = sread(); auto T = sread(); long l = SC; long r = W - SC + 1; long u = SR; long d = H - SR + 1; foreach (i; 0 .. N) { switch (S[i]) { case 'L': l--; break; case 'R': r--; break; case 'U': u--; break; case 'D': d--; break; default: assert(0); } // writefln("%s %s %s %s", l, r, u, d); if (l <= 0 || r <= 0 || u <= 0 || d <= 0) { writeln("NO"); return; } switch (T[i]) { case 'L': if (r < W) r++; break; case 'R': if (l < W) l++; break; case 'U': if (d < H) d++; break; case 'D': if (u < H) u++; break; default: assert(0); } } writeln("YES"); }
D
import std.stdio; import std.conv; import std.string; void main() { int[] input = readln.split.to!(int[]); int a = input[0]; int b = input[1]; if (a + b == 15){ writeln("+"); } else if (a * b == 15){ writeln("*"); } else { writeln("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; 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 ss = reads!string; int a = ss[0].to!int; int b = ss[2].to!int; switch (ss[1]) { case "+": writeln(a + b); break; case "-": writeln(a - b); break; default: break; } }
D
import std.stdio; import std.string; string toSwapCase(string s) { if (s == s.toLower) { return s.toUpper; } if (s == s.toUpper) { return s.toLower; } return s; } void main() { auto str = readln; foreach (ch; str) { write(toSwapCase(ch ~ "")); } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } long calc(int n) { if (n == 1) return 1; long a = 2; long b = 1; for (int i = 2; i <= n; i++) { long c = a + b; a = b; b = c; } return b; } void main() { int n = readint; writeln(calc(n)); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; ulong[10^^5] AS; int[40] BS; bool[40] KS; void main() { auto nk = readln.split.to!(ulong[]); auto N = nk[0]; auto K = nk[1]; ulong o = 1; foreach (i, a; readln.split.to!(ulong[])) { AS[i] = a; foreach (j; 0..40) { BS[j] += ((o<<j) & a) ? 1 : 0; } } foreach (i; 0..40) { KS[i] = !!((o<<i) & K); } ulong ret; foreach (i; 0..40) { if (!KS[i]) continue; ulong x; foreach (j; 0..i) { if (BS[j] <= N / 2) { x |= (o<<j); } } foreach (j; i+1..40) { x |= ((o<<j) & K); } ulong r; foreach (a; AS[0..N]) { r += a ^ x; } ret = max(ret, r); } ulong r; foreach (a; AS[0..N]) { r += a ^ K; } ret = max(ret, r); writeln(ret); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD; if (n == 1) continue; else if (n == 2) ans[ti] = 1; else if (n == 3) ans[ti] = 2; else if (n % 2 == 0) ans[ti] = 2; else ans[ti] = 3; } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
import std.stdio; import std.algorithm; int main() { int t; scanf("%d", &t); while (t --> 0) { int n; scanf("%d", &n); int[] a = new int[n]; int[] cnt = new int [42]; long result = 0; for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); int bit_id = 0; while (a[i] > 0) { ++bit_id; a[i] /= 2; } result += cnt[bit_id]; ++cnt[bit_id]; } printf("%lld\n", result); } return 0; }
D
import std.stdio, std.conv, std.string; import std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } int DEBUG_LEVEL = 0; void print()(){ writeln(""); } void print(T, A ...)(T t, lazy A a){ write(t), print(a); } void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); } void main(string[] args){ if(args.length > 1 && args[1] == "-debug"){ if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int; else DEBUG_LEVEL = 1; } long t = read.to!long; foreach(q; 0 .. t){ long n = read.to!long; string s = readln.chomp; bool f = 0; for(int i = 0; i < n - 10; i ++) if(s[i] == '8') f = 1; if(f) "YES".writeln; else "NO".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; } size_t binarySearch(alias pred, T)(in T[] arr, bool isLower = true) { long ok, ng; if (isLower) { ok = arr.length; ng = -1; } else { ok = 0; ng = arr.length; } while (abs(ok-ng) > 1) { size_t mid = cast(size_t)(ok+ng) / 2; if (unaryFun!pred(arr[mid])) ok = mid; else ng = mid; } return cast(size_t)ok; } void main() { auto n = RD!int; auto s = RD!string; auto cnt = new int[][](s.length+1, 26); foreach (i; 0..s.length) { auto c = cnt[i].dup; ++c[s[i]-'a']; cnt[i+1] = c; } auto m = RD!int; foreach (i; 0..m) { auto t = RD!string; auto cnt2 = new int[](26); foreach (c; t) { ++cnt2[c-'a']; } bool check(in int[] cnt1) { foreach (j; 0..cnt1.length) { if (cnt1[j] < cnt2[j]) return false; } return true; } auto pos = binarySearch!(check)(cnt); writeln(pos); } stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ int n = read.to!int; Node[] nodes; foreach(i; 0 .. n) nodes ~= new Node(i); foreach(i; 0 .. n - 1){ int u = read.to!int - 1; int v = read.to!int - 1; nodes[u].nodes ~= nodes[v]; nodes[v].nodes ~= nodes[u]; } int f = 0; foreach(nd; nodes){ if(nd.nodes.length == 2) f = 1; } if(f == 1) "NO".writeln; else "YES".writeln; } /* A1: YES if and only if there are no vertex with degree 2. */ class Node{ int id; Node[] nodes; this(int id){ this.id = id; } }
D
module main; import core.stdc.stdio; char[9][6] a; char[9][6] ud; void print() { for (int i = 0; i < 6; i++) { printf("%s\n", &a[i]); } } int main(string[] argv) { ud[0] = "334433"; ud[1] = "334433"; ud[2] = "223322"; ud[3] = "223322"; ud[4] = "112211"; ud[5] = "112211"; //for (int i = 0; i < 6; i++) { // printf("%s\n", &ud[i]); //} for (int i = 0; i < 6; i++) { scanf("%s", &a[i]); a[i][8] = '\0'; } for (char i = '4'; i > '0'; i--) { for (int x = 0; x < 6; x++) { for (int b = 0; b < 9; b++) { int b2 = b - (b>1) - (b>4); if (a[x][b] == '.' && ud[x][b2] == i) { a[x][b] = 'P'; print(); return 0; } } } } return 0; }
D
import std.stdio :write, writeln; import std.array; import std.range; import std.typecons; import std.algorithm : max, min; string[] list = [ "", "Washington", "Adams", "Jefferson", "Madison", "Monroe", "Adams", "Jackson", "Van Buren", "Harrison", "Tyler", "Polk", "Taylor", "Fillmore", "Pierce", "Buchanan", "Lincoln", "Johnson", "Grant", "Hayes", "Garfield", "Arthur", "Cleveland", "Harrison", "Cleveland", "McKinley", "Roosevelt", "Taft", "Wilson", "Harding", "Coolidge", "Hoover", "Roosevelt", "Truman", "Eisenhower", "Kennedy", "Johnson", "Nixon", "Ford", "Carter", "Reagan" ]; void main(){ list[ next!int ].writeln; } import std.stdio : readln; import std.conv : to; import std.string : split, chomp; import std.traits; string[] input; string delim = " "; T next(T)() in { assert(hasNext()); } out { input.popFront; } body { return input.front.to!T; } bool hasNext(){ if(input.length > 0){ return true; } string str = readln; if(str.length > 0){ input ~= str.chomp.split(delim); return true; }else{ return false; } } void dbg(T...)(T vs) { import std.stdio : stderr; foreach(v; vs) stderr.write(v.to!string ~ " "); stderr.write("\n"); } T clone(T)(T v){ T v_; static if(isInputRange!(T)){ foreach(ite; v){ v_ ~= ite.clone; } }else{ v_ = v; } return v_; }
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; int n; long a, b, c, d; void main() { scan(n, a, b, c, d); b = b - a; a = 0; n--; foreach (i ; 0 .. n + 1) { if (i * c - (n - i) * d <= b && b <= i * d - (n - i) * c) { writeln("YES"); return; } } writeln("NO"); } 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.algorithm, std.conv, std.array, std.string, std.math; void main() { auto ab = readln.split.join("").to!int; writeln(ab == ab.to!float.sqrt.to!int^^2 ? "Yes" : "No"); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { auto n = lread(); auto a = aryread(); auto q = lread(); long sum_a = sum(a); auto num = new long[](10 ^^ 6); foreach (i; 0 .. n) { num[a[i]] += 1; } // writeln(num); foreach (_; 0 .. q) { long b, c; scan(b, c); // writeln(num); sum_a += (c - b) * num[b]; num[c] += num[b]; num[b] = 0; // writeln("sum_a:", sum_a); writeln(sum_a); } } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.algorithm; import std.array; import std.ascii; import std.bigint; import std.complex; import std.container; import std.conv; import std.functional; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } void readlnTo(T...)(ref T t) { auto s = readln().split(); assert(s.length == t.length); foreach(ref ti; t) { ti = s[0].to!(typeof(ti)); s = s[1..$]; } } const real eps = 1e-10; void main(){ auto s = readln().chomp(); string t; foreach(si; s) { if(t.empty || t.back != si) { t ~= si; } } writeln(t.length - 1); }
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 N = RD!uint; auto s = RD!string; auto actg = "ACTG"; long f(in string str) { long cnt; foreach (i; 0..4) { auto x = str[i] < actg[i] ? actg[i] - str[i] : str[i] - actg[i]; cnt += min(x, 26 - x); } return cnt; } long ans = long.max; foreach (i; 0..N-3) { auto r = f(s[i..i+4]); ans = min(ans, r); } writeln(ans); stdout.flush(); debug readln(); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; // dfmt on void main() { long r = lread(); writeln(3 * r * r); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto a=readln.split.to!(int[]); auto c=new int[](100009); foreach(ref e; a) e+=2; foreach(e; a) c[e]+=1; int mx=0; foreach(i; 2..c.length-2){ mx=max(mx, c[i-1]+c[i]+c[i+1]); } writeln(mx); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
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; // }}} // 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; } } // }}} void main() { auto cin = new Scanner; string s = cin.next!string; if (s == "AAA" || s == "BBB") { writeln("No"); } else { writeln("Yes"); } }
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; long calc(long h, long w) { if (h == 1 || w == 1) return 1; return (h * w + 1) / 2; } void main() { long h, w; scan(h, w); writeln(calc(h, w)); } 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.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; bool calc(string[] ss) { bool[string] d = [ss[0]: true]; for (int i = 1; i < ss.length; i++) { if (ss[i] in d) return false; if (ss[i-1][$-1] != ss[i][0]) return false; d[ss[i]] = true; } return true; } void main() { int n = readint; string[] ss; for (int i = 0; i < n; i++) { ss ~= read!string; } writeln(calc(ss) ? "Yes" : "No"); }
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); int res = 0; for (int a = 3; ; a += 2) { int d = a * a; int b = d / 2; int c = d - b; if (c > n) { break; } res += 1; } writeln (res); } }
D
import std.stdio, std.string,std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { auto S = [readln.chomp, readln.chomp, readln.chomp]; auto current = 0; int[3] count; while (true) { if (count[current] == S[current].length) { writeln(current == 0 ? "A" : current == 1 ? "B" : "C"); return; } auto next = S[current][count[current]] - 'a'; count[current]++; current = next; } }
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; T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void minAssign(T)(ref T dst, T src) { dst = min(dst, src); } alias sread = () => readln.chomp(); void main() { long N = lread(); auto dp = new long[](N + 1); dp[] = long.max; dp[0] = 0; foreach (i; 0 .. N) if (dp[i] != long.max) { dp[i + 1].minAssign(dp[i] + 1); size_t j = 6; while (i + j < dp.length) { dp[i + j].minAssign(dp[i] + 1); j *= 6; } j = 9; while (i + j < dp.length) { dp[i + j].minAssign(dp[i] + 1); j *= 9; } } dp[N].writeln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } 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; long ans, x = 2; ans += (RD - 1); foreach (i; 1..N) { auto A = RD; ans += (A - 1) / x; if (A == x) { x = A+1; } debug writeln("ans:", ans, " x:", x); } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n,a,b; scan(n, a, b); auto ans = min(a*n, b); 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.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 10L^^15; void main() { int n, m; scan(n, m); auto adj = new int[][](n, 0); foreach (i ; 0 .. m) { int xi, yi; scan(xi, yi); xi--, yi--; adj[yi] ~= xi; } auto dp = new int[](n); dp[] = -1; int dfs(int v) { if (dp[v] != -1) { return dp[v]; } dp[v] = 0; foreach (u ; adj[v]) { dp[v] = max(dp[v], dfs(u) + 1); } return dp[v]; } int ans; foreach (i ; 0 .. n) { ans = max(dfs(i), 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); } } }
D
import std.stdio, std.conv, std.string; import std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } int DEBUG_LEVEL = 0; void print()(){ writeln(""); } void print(T, A ...)(T t, lazy A a){ write(t), print(a); } void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); } void main(string[] args){ if(args.length > 1 && args[1] == "-debug"){ if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int; else DEBUG_LEVEL = 1; } // ------ ここまでテンプレ ------ // char[] s = readln.chomp.to!(char[]); long x, y; foreach(c; s) if(c == '0') x += 1; else y += 1; print!1("x:", x, " y:", y); long ans; if(x <= y) ans = x * 2; else ans = y * 2; 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 long mod = 10^^9 + 7; void main() { int n; scan(n); auto s = iota(n + 1).array; long ans = 1; foreach (p ; 2 .. n + 1) { if (s[p] > 1) { int cnt = 1; for (int q = p; q < n + 1; q += p) { while (s[q] % p == 0) { cnt++; s[q] /= p; } } (ans *= cnt) %= mod; } } 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.conv; void main() { int[] integers; int x; int y; int z; integers = readln.split.to!(int[]); x = integers[0]; y = integers[1]; z = integers[2]; writeln(z, ' ', x, ' ', y); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD; ans[ti] = (n-1) / 2 + 1; } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto np = readln.split.to!(int[]); auto N = np[0]; auto P = np[1]; int even, odd; foreach (a; readln.split.to!(int[])) { if (a%2 == 0) { ++even; } else { ++odd; } } long r = 2L^^even; if (odd == 0) { writeln(P == 1 ? 0 : r); } else { writeln(r * 2^^(odd-1)); } }
D
import std; void main() { int t; scanf("%d", &t); getchar(); foreach(_; 0..t) { int n; scanf("%d", &n); getchar(); int[] a = readln.strip.split(" ").to!(int[]); int res = 0; while (a.uniq.array.length != 1) { // writeln(a); // writeln("len = ", a.uniq.array.length); int left_pad; int indx; for(int i = n - 1; i >= 0; i--) { if (a[i] == a[n-1]) left_pad++; else { indx = i; break; } } if (indx - left_pad + 1 > 0) { // writeln("changing from", indx - left_pad + 1, " to ", indx + 1); a[indx - left_pad + 1 .. indx + 1] = a[n - 1]; } else { a[0 .. indx + 1] = a[n - 1]; } res++; } writeln(res); } }
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 s = readln.chomp.to!(char[]); auto q = readln.chomp.to!int; auto r = 1; string a = ""; string b = ""; foreach (_; 0..q) { auto x = readln.chomp.split; if (x[0].to!int == 1) { r *= -1; } else { if (x[1].to!int == 1) { if (r == 1) { a = x[2] ~ a; } else { b = b ~ x[2]; } } else { if (r == -1) { a = x[2] ~ a; } else { b = b ~ x[2]; } } } } s = a ~ s ~ b; if (r == -1) { reverse(s); } s.writeln; }
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; readV(n); writeln(n%2 == 0 ? n : n*2); }
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 N = RD; auto a = RDA; bool[long] set; set[0] = true; foreach (i; 0..N) { bool[long] nSet; foreach (key; set.keys) { if (a[i] == key+1) nSet[key+1] = true; else nSet[key] = true; } set = nSet; } long ans; foreach (key; set.keys) ans.chmax(key); if (ans == 0) writeln(-1); else writeln(N-ans); stdout.flush; debug readln; }
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, k; string s; void main() { scan(n, k); s = readln.chomp; auto t = new int[](n + 10); foreach (char ch ; 'A' .. 'Z' + 1) { foreach (i ; 0 .. n) { if (s[i] == ch) { t[i]++; break; } } foreach_reverse (i ; 0 .. n) { if (s[i] == ch) { t[i + 1]--; break; } } } foreach (i ; 0 .. n + 1) { t[i + 1] += t[i]; } debug { writeln(t); } if (t.reduce!max > k) { writeln("YES"); } else { writeln("NO"); } } 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 "B" 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); int n; int[] a; sc.read(n, a); long all = 1, err = 1; foreach (d; a) { all *= 3; if (d % 2 == 0) { err *= 2; } else { err *= 1; } } writeln(all - err); 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.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(a <= c && c <= b ? "Yes" : "No"); }
D
import std.algorithm; import std.conv; import std.stdio; import std.string; void main() { auto abc = readln.split.map!( to!int ); auto k = readln.strip.to!int; writeln( solve( abc[ 0 ], abc[ 1 ], abc[ 2 ], k ) ); } int solve( in int a, in int b, in int c, in int k ) { auto m = max( a, b, c ); int md = m; foreach( i; 0 .. k ) { md *= 2; } return a + b + c + ( md - m ); } unittest { assert( solve( 5, 3, 11, 1 ) == 30 ); assert( solve( 3, 3, 4, 2 ) == 22 ); }
D
/+ dub.sdl: name "J" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; import std.typecons; int main() { auto sc = new Scanner(stdin); alias Edge = Tuple!(int, "to", int, "dist"); int n; sc.read(n); Edge[][] g = new Edge[][](n); foreach (i; 0..n) { int a, b, c; sc.read(a, b, c); g[a] ~= Edge(b, c); g[b] ~= Edge(a, c); } int ma = -1; void dfs(int p, int b, int nd) { ma = max(ma, nd); foreach (e; g[p]) { if (e.to == b) continue; dfs(e.to, p, nd + e.dist); } } dfs(0, -1, 0); writeln(ma); 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) { 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 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); } enum MOD = (10 ^^ 9) + 7; void main() { long A, B, C; scan(A, B, C); bool[long] b; long n = A; b[A % B] = true; while (true) { if (C in b) { writeln("YES"); return; } n += A; n %= B; if (n in b) { writeln("NO"); return; } b[n] = true; } }
D
import std.stdio, std.string, std.conv, std.array, std.algorithm; import std.uni, std.range, std.math, std.container, std.datetime; import core.bitop, std.typetuple, std.typecons; immutable long MOD = 1_000_000_007; alias tie = TypeTuple; alias triplet = Tuple!(int, int, int); void main(){ int a, b, c; readVars(a, b, c); if (a & 1 || b & 1 || c & 1) { writeln(0); return; } if (a == b && b == c) { writeln(-1); return; } int ans; while(true){ if (a & 1 || b & 1 || c & 1) { writeln(ans); return; } auto tmp1 = (b + c) / 2, tmp2 = (a + c) / 2, tmp3 = (a + b) / 2; a = tmp1, b = tmp2, c = tmp3; ans++; } } void readVars(T...)(auto ref T args){ auto line = readln.split; foreach(ref arg ; args){ arg = line.front.to!(typeof(arg)); line.popFront; } if(!line.empty){ throw new Exception("args num < input num"); } }
D
import std; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long S, W; scan(S, W); writeln((S <= W) ? "unsafe" : "safe"); }
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; immutable long INF = 1L << 50; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto P = s[2].to!long; auto G = new Edge!long[][](N); foreach (_; 0..M) { s = readln.split.map!(to!int); auto u = s[0] - 1; auto v = s[1] - 1; long c = -s[2] + P; G[u] ~= Edge!long(v, c); } auto dist = bellman_ford!(long, INF)(G, 0); if (dist[N-1] == -INF) { writeln(-1); } else { writeln(max(0L, -dist[N-1])); } } struct Edge(T) { int to; T cost; } T[] bellman_ford(T, T inf)(const Edge!(T)[][] graph, int start) { int n = graph.length.to!int; auto dist = new T[](n); dist[] = inf; dist[start] = 0; foreach (i; 0..n) { foreach (u; 0..n) { foreach (e; graph[u]) { if (dist[u] != inf && dist[e.to] > dist[u] + e.cost) { dist[e.to] = dist[u] + e.cost; if (i == n - 1) { dist[e.to] = -inf; } } } } } auto stack = new int[](n); int sp = 0; foreach (i; 0..n) { if (dist[i] == -inf) { stack[sp++] = i; } } while (sp > 0) { int u = stack[--sp]; foreach (e; graph[u]) { if (dist[e.to] == -inf) continue; dist[e.to] = -inf; stack[sp++] = e.to; } } return dist; }
D
void main() { string s = readln.chomp; long k = readln.chomp.to!int; auto g = s.group.array; long ans; if (g.length == 1) { ans = k * g[0][1].to!long / 2; } else { foreach (x; g) { ans += x[1] / 2; } ans *= k; if (s[0] == s[$-1]) { long tmp = (g[0][1] + g[$-1][1]) / 2; tmp -= g[0][1] / 2 + g[$-1][1] / 2; ans += (k - 1) * tmp; } } ans.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.container; import std.typecons;
D
import std.stdio; import std.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() { auto s = sort(readln.chomp.split).array; if (s == ["1", "4", "7", "9"]) { writeln("YES"); } else { writeln("NO"); } }
D
void main() { problem(); } void problem() { auto S = scan; auto T = scan; auto SL = S.length; auto TL = T.length; long solve() { int max_contained; foreach(i; 0..SL - TL + 1) { int contained; foreach(j; 0..TL) { if (T[j] == S[i + j]) contained++; } if (max_contained < contained) max_contained = contained; } return TL - max_contained; } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
import std.stdio, std.string, std.conv; import std.typecons; import std.algorithm, std.array, std.range, std.container; import std.math; void main() { auto data = readln.split.to!(int[]); writeln( max(data[0] * 2 - 1, data[0] + data[1], data[1] * 2 - 1) ); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.bigint; void main() { auto N = readln.chomp.to!int; auto ds = readln.split.to!(int[]); int l, r; foreach (d; ds) { if (d) { ++r; } else { ++l; } } int cnt; foreach (d; ds) { ++cnt; if (d) { --r; } else { --l; } if (!r || !l) { writeln(cnt); return; } } }
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; writeln(X >= 30 ? "Yes" : "No"); stdout.flush; debug readln; }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; import std.string; void main() { int N, Y; auto l = readln.chomp.split.map!(to!int).array; N = l[0], Y = l[1]; for (int i = 0; i <= N; ++i) { for (int j = 0; j <= N - i; ++j) { int k = N - i - j; int sum = 10000 * i + 5000 * j + 1000 * k; if (sum == Y) { writeln(i, " ", j, " ", k); return; } } } writeln(-1, " ", -1, " ", -1); return; }
D
import std.stdio, std.string, std.algorithm, std.array; void bracket (int count) { for (int i = count; i >= 1; i--) { string s = ""; s ~= replicate("(", i); s ~= replicate(")", i); s ~= replicate("(", count - i); s ~= replicate(")", count - i); writeln(s); } } void main() { int t; scanf("%d", &t); getchar(); foreach(_; 0..t){ int n; scanf("%d", &n); bracket(n); } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "x", long, "y", long, "cost"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { auto n = lread(); auto a = aryread(); auto up = new long[](n); foreach (e; a) { up[e - 1]++; } foreach (e; up) e.writeln(); } bool is_exist(string s, char c) { return (s.countUntil(c) >= 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.conv, std.string; void main(){ auto ip = readln.split.to!(int[]), x = ip[0], y = ip[1]; const gr = [0, 1, 2, 1, 3, 1, 3, 1, 1, 3, 1, 3, 1]; if(gr[x] == gr[y]){ writeln("Yes"); } else { writeln("No"); } }
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(){ foreach(_; 0 .. scan!int){ int a = scan!int, b = scan!int; if(a % b == 0) "YES".print; else "NO".print; } }
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 N = RD; writeln(N / 3); 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; const MOD = 10^^9 + 7; long calc(int h, int w, int a, int b) { initFactTable(h + w); // (a, b) -> (c, d) long f(int a, int b, int c, int d) { return nck(c - a + (d - b), d - b); } long ans = 0; for (int i = b; i < w; i++) { long x = f(0, 0, h - 1 - a, i) * f(h - a, i, h - 1, w - 1) % MOD; ans = (ans + x) % MOD; } return ans; } void main() { int h, w, a, b; scan(h, w, a, b); writeln(calc(h, w, a, b)); } 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[] fact; // 階乗 long[] factInv; // 階乗の逆元 void initFactTable(int n) { fact = new long[n + 1]; fact[0] = 1; for (int i = 1; i <= n; i++) fact[i] = i * fact[i - 1] % MOD; factInv = new long[n + 1]; factInv[n] = modPow(fact[n], MOD - 2, MOD); for (int i = n - 1; i >= 0; i--) factInv[i] = factInv[i + 1] * (i + 1) % MOD; } long modPow(long x, long k, long m) { if (k == 0) return 1; if (k % 2 == 0) return modPow(x * x % m, k / 2, m); return x * modPow(x, k - 1, m) % m; } long nck(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return (fact[n] * factInv[k] % MOD) * factInv[n - k] % MOD; }
D
void main() { if(rs.count('7')) writeln("Yes"); else writeln("No"); } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.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 Ns = readln.split.to!(int[]); writeln(Ns[0]+Ns[1]+Ns[2] >= 22 ? "bust" : "win"); }
D
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math; import core.stdc.stdio; // foreach, foreach_reverse, writeln void main() { int n; long c; scanf("%d%ld", &n, &c); struct P { long x; int v;} P[] p = new P[n]; foreach (i; 0..n) { long x; int v; scanf("%ld%d", &x, &v); p[i] = P(x,v); } long ans = 0; foreach (_; 0..2) { p.sort!("a.x<b.x"); long[] l = new long[n+1]; { long sum = 0, best = 0; foreach (i; 0..n) { sum += p[i].v; best = max(best, sum-p[i].x); l[i+1] = best; } } long[] r = new long[n+1]; { long sum = 0, best = 0; foreach_reverse (i; 0..n) { sum += p[i].v; best = max(best, sum-(c-p[i].x)*2); r[i] = best; } } foreach (i; 0..n+1) { ans = max(ans, l[i]+r[i]); } foreach (i; 0..n) { p[i].x = c-p[i].x; } } writeln(ans); }
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 / 2) + (n & 1)); }
D
import std.algorithm; import std.container; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.traits; class UnionFind { private: uint[] rank; uint[] par; uint size; public: this(uint size) { rank = repeat(0u).take(size).array; par = iota(size).array; this.size = size; } uint length() { return size; } uint getRoot(uint n) { return par[n] = (par[n] == n ? n : getRoot(par[n])); } bool unite(uint n1, uint n2) { n1 = getRoot(n1); n2 = getRoot(n2); if (n1 == n2) return false; bool t = rank[n1] < rank[n2]; par[t ? n1 : n2] = t ? n2 : n1; if (rank[n1] == rank[n2]) rank[n1]++; return true; } bool haveSameRoot(uint n1, uint n2) { return getRoot(n1) == getRoot(n2); } uint countRoots() { uint roots = 0; par.each!((i, v){ if (getRoot(v) == i) roots++; }); return roots; } } unittest { auto uf = new UnionFind!5; uf.unite(0, 1); uf.unite(0, 2); uf.unite(3, 4); assert(uf.length == 5); assert(uf.haveSameRoot(1, 2)); assert(uf.haveSameRoot(4, 3)); assert(!uf.haveSameRoot(1, 4)); assert(uf.countRoots == 2); } void main() { auto nm = readln.chomp.split.to!(uint[]); auto n = nm[0]; auto m = nm[1]; auto uf = new UnionFind(n); iota(m).each!((_){ auto xyz = readln.chomp.split.to!(uint[]); uf.unite(xyz[0] - 1, xyz[1] - 1); }); uf.countRoots.writeln; }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { auto S = readln.chomp.map!(c => c.to!(int) - '0'); auto K = readln.chomp.to!long; long pos = 1; foreach(x; S) { if (x == 1 && pos < K) { pos++; } else { writeln(x); return; } } assert(false); }
D
import std.stdio; import std.ascii; import std.conv; import std.string; import std.algorithm; import std.range; import std.functional; import std.math; import core.bitop; import std.numeric; void main() { auto cord = readln.split.to!(long[]); auto x1 = cord[0]; auto y1 = cord[1]; auto x2 = cord[2]; auto y2 = cord[3]; long vx = x2 - x1; long vy = y2 - y1; long ix = -vy; long iy = vx; writeln(x2 + ix, " ", y2 + iy, " ", x1 + ix, " ", y1 + iy); }
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[]); } void solve() { string s = readln; writeln(s.count('+').to!int - s.count('-').to!int); } void main() { solve(); readln; }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void main() { int x, y, z; readV(x, y, z); writeln((x-z)/(y+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; 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(); writeln(0); stdout.flush(); auto s = sread(); if (s == "Vacant") return; long z = (s == "Male") ? 0 : 1; long ok = 0; long ng = N - 1; while (1 < (ng - ok)) { long x = (ok + ng) / 2; writeln(x); stdout.flush(); s = sread(); if (s == "Vacant") return; long g = (s == "Male") ? 0 : 1; if ((x & 1) == (z ^ g)) { ok = x; } else { ng = x; } } writeln(ng); stdout.flush(); s = sread(); if (s == "Vacant") return; }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int a, b; readV(a, b); writeln(a <= 8 && b <= 8 ? "Yay!" : ":("); }
D
import std.stdio; import std.string; import std.range; import std.algorithm; void main() { auto s = readln.chomp.retro.array; auto cands = ["dream", "dreamer", "erase", "eraser"].map!(s => s.retro.array).array; int idx = 0; while(true) { bool succ; if (idx >= s.length-1) break; foreach(cand; cands) { if (idx+cand.length <= s.length && s[idx..idx+cand.length] == cand) { idx += cand.length; succ = true; break; } } if (!succ) { "NO".write; return; } } "YES".write; }
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; void main() { auto s = readln.split; auto N = s[0].to!int; auto H = s[1].to!long; auto A = N.iota.map!(_ => readln.split.map!(to!long).array).array; long ans = 0; long tmp = 0; long h = 0; for (int i = 0, j = 0; i < N; ++i) { if (j <= i) { tmp = A[i][1] - A[i][0]; h = H; j = i; } else { tmp -= A[i][0] - A[i - 1][0]; h += A[i][0] - A[i - 1][1]; } while (j + 1 < N && h - (A[j + 1][0] - A[j][1]) > 0) { tmp += A[j + 1][1] - A[j][1]; h -= A[j + 1][0] - A[j][1]; ++j; } ans = max(ans, tmp + h); } ans.writeln; }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { const tmp = readln.split.to!(long[]); auto D = tmp[0], N = tmp[1]; auto res = 100^^D; if (N < 100) res *= N; else res *= N+1; writeln(res); }
D
import std.stdio, std.range, std.conv, std.string, std.array, std.functional; import std.algorithm.comparison, std.algorithm.iteration, std.algorithm.mutation, std.algorithm.searching, std.algorithm.setops, std.algorithm.sorting; import std.container.binaryheap; void main() { char input; scanf("%c", &input); if(input == 'A') { writeln("T"); } if(input == 'T') { writeln("A"); } if(input == 'G') { writeln("C"); } if(input == 'C') { writeln("G"); } }
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; void main() { auto c = readMatrix!int(3, 3); (check(c) ? "Yes" : "No").writeln; } bool check(int[][] a) { return a[0][0] == a[0][1] + a[1][0] - a[1][1] && a[1][0] == a[1][1] + a[2][0] - a[2][1] && a[0][1] == a[0][2] + a[1][1] - a[1][2] && a[1][1] == a[1][2] + a[2][1] - a[2][2]; } // =================================== 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; }
D
import std.stdio; void main() { long n; scanf("%ld", &n); write((n*(n-1))/2); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new int[](t); foreach (ti; 0..t) { auto a = RD; auto b = RD; string s, s2; while (a != 0) { s ~= cast(char)('0'+(a%2)); a /= 2; } while (b != 0) { s2 ~= cast(char)('0'+(b%2)); b /= 2; } debug writeln(s); debug writeln(s2); ans[ti] = abs(cast(int)s.length - cast(int)s2.length); ans[ti] += 2; ans[ti] /= 3; while (s.length < s2.length) { s = '0' ~ s; } while (s.length > s2.length) { s2 = '0' ~ s2; } debug writeln(s); debug writeln(s2); foreach (i; 0..s.length) { if (s[i] != s2[i]) ans[ti] = -1; } } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
void main() { int[] s = readln.chomp.split("/").to!(int[]); writeln(s[1] <= 4 ? "Heisei" : "TBD"); } 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, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!long; long r = long.max; for (long i = 1; i^^2 <= N; ++i) { if (N%i != 0) continue; r = min(r, i-1 + N/i-1); } writeln(r); }
D
import std.stdio, std.string, std.conv, std.algorithm; void main(){ while(true){ auto input = readln.split.map!(to!int); int h = input[0], n = input[1]; if(n == 0 && h == 0) break; int[int] cube; foreach(i; 0..n){ auto line = readln.split; int a = line[1].to!int - 1, b = line[2].to!int - 1; final switch(line[0]){ case "xy": foreach(z; 0..h){ cube[(a << 18) + (b << 9) + z] = 0; } break; case "xz": foreach(y; 0..h){ cube[(a << 18) + (y << 9) + b] = 0; } break; case "yz": foreach(x; 0..h){ cube[(x << 18) + (a << 9) + b] = 0; } break; } } (h * h * h - cube.length).writeln; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; int[int][2000] MEMO; int N, Z, W; void main() { auto nzw = readln.split.to!(int[]); N = nzw[0]; Z = nzw[1]; W = nzw[2]; auto AS = readln.split.to!(int[]); int solve(int i, int t, int z, int w) { if (i == N) return abs(z - w).to!int; auto key = t > 0 ? w : z * t; if (key in MEMO[i]) return MEMO[i][key]; auto ret = t > 0 ? 0 : int.max; foreach (j; i..N) { if (t > 0) { z = AS[j]; ret = max(ret, solve(j+1, -t, z, w)); } else { w = AS[j]; ret = min(ret, solve(j+1, -t, z, w)); } } return MEMO[i][key] = ret; } writeln(solve(0, 1, Z, W)); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip, std.regex; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!long).array; auto B = new long[][](N, N); foreach (i; 0..N) B[i][i] = A[i]; foreach (len; 2..N+1) { foreach (l; 0..N-len+1) { int r = l + len - 1; B[l][r] = B[l+1][r] ^ B[l][r-1]; } } auto dp = new long[][](N, N); foreach (i; 0..N) dp[i][i] = A[i]; foreach (len; 2..N+1) { foreach (l; 0..N-len+1) { int r = l + len - 1; dp[l][r] = B[l][r]; dp[l][r] = max(dp[l][r], dp[l+1][r]); dp[l][r] = max(dp[l][r], dp[l][r-1]); } } auto Q = readln.chomp.to!int; while (Q--) { auto s = readln.split.map!(to!int); auto a = s[0] - 1; auto b = s[1] - 1; dp[a][b].writeln; } }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { int h, w, n; //scan(h, w, n); scan(h); scan(w); scan(n); h = max(h, w); auto ans = (n + h - 1) / h; writeln(ans); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.stdio; // readln import std.array; // split import std.conv; // to int[] dx = [1,1,1,0,-1,-1,-1,0]; int[] dy = [1,0,-1,-1,-1,0,1,1]; void main(){ string[] s = split(readln()); int h = to!int(s[0]); int w = to!int(s[1]); char[][] m = new char[][](h + 2,w + 2); for(int i = 1; i < m.length - 1; i++){ string str = readln(); for(int j = 1; j < m[0].length - 1; j++){ m[i][j] = str[j - 1]; } } char[][] ans = new char[][](h + 2,w + 2); for(int i = 0; i < m.length; i++){ ans[i][0] = '.'; ans[i][w + 1] = '.'; } for(int i = 0; i < m[0].length; i++){ ans[0][i] = '.'; ans[h + 1][i] = '.'; } for(int i = 1; i < m.length - 1; i++){ for(int j = 1; j < m[0].length - 1; j++){ if(m[i][j] == '#'){ ans[i][j] = '#'; }else{ int cnt = 0; for(int k = 0; k < 8; k++){ if(m[i + dy[k]][j + dx[k]] == '#') cnt++; } ans[i][j] = to!char(cnt + 48); } } } for(int i = 1; i < m.length - 1; i++){ for(int j = 1; j < m[0].length - 1; j++){ write(ans[i][j]); } writeln(""); } }
D
import std.stdio,std.string,std.conv; void main(){ auto debt = 10_0000; auto week = to!int( readln().chomp() ) ; for( ; 0<week ; week-- ){ debt *= 1.05; debt += (debt%1000)?1000-debt%1000:0; } writeln(debt); }
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; // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string : chomp; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.chomp.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 main() { auto cin = new Scanner; string s; cin.scan(s); writeln(s[0] ~ (s.length - 2).to!string ~ s[$ - 1]); }
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 k) { auto m = new long[k]; for (int i = 1; i <= n; i++) m[i % k]++; long ans = 0; for (int i = 1; i <= n; i++) { // a を固定 int a = i % k; int b = (k - a) % k; int c = (k - a) % k; if ((b + c) % k == 0) { ans += m[b] * m[c]; } } return ans; } void main() { int n, k; scan(n, k); writeln(calc(n, k)); }
D
import std.stdio,std.string; void main(){ char[] str2="".dup; auto str = readln().chomp() ; foreach( c ; str ){ str2 = c~str2; } writeln(str2); }
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; immutable long MOD = 10^^9 + 7; void main() { auto s = readln.split.map!(to!long); auto N = s[0]; auto M = s[1]; auto S = readln.chomp; auto T = readln.chomp; auto L = N * M / gcd(N, M); char[long] X; long p = 0; long a = L / N; foreach (i; 0..N) { if (p in X) { if (X[p] != S[i]) { writeln(-1); return; } } X[p] = S[i]; p += a; } p = 0; a = L / M; foreach (i; 0..M) { if (p in X) { if (X[p] != T[i]) { writeln(-1); return; } } X[p] = T[i]; p += a; } writeln(L); }
D
import std.stdio; import std.algorithm; import core.stdc.stdio; static immutable maxM = 5000; struct t{ int idx; int len; } class State{ int[] sizes; t[][] data; this(){ sizes = new int[3]; data = new t[][3]; t[] buf = new t[3*(maxM*2+1)]; for(int i=0;i<3;i++){ data[i] = buf[i*(maxM*2+1)..(i+1)*(maxM*2+1)]; } } void Clear(){ sizes[] = 0; } } void main(){ State now = new State; State next = new State; while(true){ int n,m,p,q,r; scanf("%d",&n); if(n==0) break; scanf("%d%d%d%d",&m,&p,&q,&r); p--; now.Clear; now.data[0][now.sizes[0]++] = t(1,n); for(int _=0;_<m;_++){ next.Clear; int x,y; scanf("%d%d",&x,&y); int len=0; for(int i=0;i<3;i++){ for(int j=0;j<now.sizes[i];j++){ t a = now.data[i][j]; t b; if(len < x){ if(len+a.len > x){ b.len = len+a.len-x; a.len -= b.len; b.idx = a.idx+a.len; now.data[i][j] = b; j--; } next.data[2][next.sizes[2]++] = a; }else if(len < y){ if(len+a.len > y){ b.len = len+a.len-y; a.len -= b.len; b.idx = a.idx+a.len; now.data[i][j] = b; j--; } next.data[1][next.sizes[1]++] = a; }else{ next.data[0][next.sizes[0]++] = a; } len += a.len; } } swap(now,next); } int res=0; int len=0; for(int i=0;i<3;i++){ for(int j=0;j<now.sizes[i];j++){ t a = now.data[i][j]; t b; if(len < p){ if(len+a.len > p){ b.len = len+a.len-p; a.len -= b.len; b.idx = a.idx+a.len; now.data[i][j] = b; j--; } }else if(len < q){ if(len+a.len > q){ a.len = q-len; } res += max(0,min(a.len,r+1-a.idx)); } len += a.len; } } printf("%d\n",res); } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { dchar[] s; scan(s); s.sort(); if (s == "abc") { writeln("Yes"); } else { writeln("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 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; alias Card = Tuple!(int, "x", bool, "h"); void main() { int N; scan(N); auto a = readln.split.to!(int[]); auto b = readln.split.to!(int[]); if (N == 1) { writeln(0); return; } auto ans = inf; foreach (set ; 0 .. 1 << N) { Card[] cs; foreach (i ; 0 .. N) { if (set & 1 << i) { cs ~= Card(a[i], 1); } else { cs ~= Card(b[i], 0); } } chmin(ans, solve(N, cs)); } writeln(ans == inf ? -1 : ans); } int solve(int N, Card[] cs) { int res; foreach (i ; 0 .. N) { foreach_reverse (j ; i + 1 .. N) { if (cs[j - 1].x > cs[j].x) { swap(cs[j - 1], cs[j]); cs[j - 1].h ^= 1; cs[j].h ^= 1; res++; } } } foreach (i ; 0 .. N - 2) { assert(cs[i].x <= cs[i + 1].x); if (!cs[i].h) { if (cs[i].x != cs[i + 1].x) { return inf; } if (!cs[i + 1].h) { res++; cs[i + 1].h ^= 1; } else if (cs[i + 2].x == cs[i].x && cs[i + 2].h) { res += 2; cs[i + 2].h ^= 1; } else { return inf; } } } if (cs[N - 2].h && cs[N - 1].h) { return res; } else if (cs[N - 2].x == cs[N - 1].x && !cs[N - 2].h && !cs[N - 1].h) { res++; } else { return inf; } return res; } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.stdio; 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 x = readln.chomp.split.to!(int[]); if (abs(x[0] - x[2]) <= x[3] || (abs(x[0] - x[1]) <= x[3] && abs(x[1] - x[2]) <= x[3])) { writeln("Yes"); } else { writeln("No"); } }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto a=readln.split.to!(int[]); auto freq=new int[](n+1); int cnt=0; foreach(e; a){ if(e>n) cnt++; else freq[e]++; } for(int i=1; i<=n; i++){ if(i<freq[i]) cnt+=(freq[i]-i); else if(i>freq[i]) cnt+=freq[i]; } writeln(cnt); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } } void wr(T...)(T x){ import std.stdio; foreach(e; x) stderr.write(e, " "); stderr.writeln(); }
D
void main() { string[] tmp = rdRow!string; string s = tmp[0], t = tmp[1]; writeln(t, s); } T rdElem(T = long)() { //import std.stdio : readln; //import std.string : chomp; //import std.conv : to; return readln.chomp.to!T; } alias rdStr = rdElem!string; dchar[] rdDchar() { //import std.conv : to; return rdStr.to!(dchar[]); } T[] rdRow(T = long)() { //import std.stdio : readln; //import std.array : split; //import std.conv : to; return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdRow!T).array; } void wrMat(T = long)(T[][] mat) { //import std.stdio : write, writeln; foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import 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, std.datetime; // 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(); bool g(string s) { foreach (i; 0 .. s.length / 2) if (s[i] != s[$ - i - 1]) return false; // writeln(s); return true; } bool f() { return g(S) && g(S[0 .. $ / 2]) && g(S[($ + 1) / 2 .. $]); } writeln(f() ? "Yes" : "No"); }
D