code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.algorithm; import std.array; import std.bigint; import std.bitmanip; import std.conv; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; // 差の絶対値 @nogc @safe pure T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; } // 切り上げ除算 @nogc @safe pure T divCeil(T)(const ref T a, const ref T b) { return (a+b-1)/b; } T[] readToArray(T)() { return readln.split.to!(T[]); } void readInto(T...)(ref T ts) { auto ss = readln.split; foreach(ref t; ts) { t = ss.front.to!(typeof(t)); ss.popFront; } } // 冪乗をmod取りつつ計算 @nogc @safe pure ulong modPow(ulong a, ulong n, ulong m) { ulong r = 1; while (n > 0) { if(n % 2 != 0) r = r * a % m; a = a * a % m; n /= 2; } return r; } // フェルマーの小定理から乗法逆元を計算 // 定理の要請により法は素数 @nogc @safe pure ulong modInv(ulong a, ulong m) { return modPow(a, m-2, m); } // mod取りつつ順列を計算 @nogc @safe pure ulong modPerm(ulong n, ulong k, ulong m) { if (n < k) return 0; ulong r = 1; for (ulong i = n-k+1; i <= n; i++) { r *= i; r %= m; } return r; } // mod取りつつ順列を計算 @nogc @safe pure ulong modFact(ulong n, ulong m) { return modPerm(n, n, m); } // mod取りつつ組み合わせを計算 // modInvを使っているので法は素数 @nogc @safe pure ulong modComb(ulong n, ulong r, ulong m) { return modPerm(n, r, m)*modInv(modFact(r, m), m) % m; } immutable ulong MOD = 1000000007; void main() { ulong a, b; readInto(a, b); ulong ret; if ( (a + b) % 2 == 0) { writeln((a+b)/2); } else if (diff(a,b) % 2 == 0) { writeln(diff(a,b)%2); } else { writeln("IMPOSSIBLE"); } }
D
import std.stdio; import std.conv; import std.string; import core.bitop; void main() { auto n = readln.chomp.to!uint; if (n == 0) return; writeln(1U << (n.bsr)); }
D
import std; auto input() { return readln().chomp(); } alias sread = () => readln.chomp(); void main() { string s = input(); long s_len = s.length; //writeln(s); //writeln(s_len); writeln(s[0 .. ((s.length) - 8)]); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
D
import std.stdio, std.string, std.conv; void main() { auto n = readln.chomp.to!int; string[] result; foreach (i; 0..n) { result ~= readln.chomp.replace("Hoshino", "Hoshina"); } foreach (r; result) r.writeln; }
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 readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!T;r.popFront;}} void main() { int n; readV(n); int[] p; readA(n, p); auto r = 0; foreach (i; 0..n-1) if (p[i] == i+1) { ++r; swap(p[i], p[i+1]); } if (p[n-1] == n) ++r; writeln(r); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio; void main() { auto s = readln.split.map!(to!int); auto K = s[0]; auto N = s[1]; auto A = readln.split.map!(to!int).array; int ans = 1 << 29; foreach (i; 0..N) { auto a = A[i]; auto b = A[(i+1)%N]; auto c = (b > a) ? b - a : b + K - a; ans = min(ans, K - c); } ans.writeln; }
D
import std.stdio; import std.conv; import std.array; void main() { int a = readln.split[0].to!int; int b = readln.split[0].to!int; int c = readln.split[0].to!int; int d = readln.split[0].to!int; int e = readln.split[0].to!int; int k = readln.split[0].to!int; if (e - a <= k){ writeln("Yay!"); } else { writeln(":("); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto A = RD; writeln(N % 500 <= A ? "Yes" : "No"); stdout.flush(); debug readln(); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; enum L = 2001; void main() { int n; scan(n); auto s = readln.chomp; if (s[0] != 'B' || s[2*n - 1] != 'B') { writeln(0); return; } auto b = new int[](2*n); foreach (i ; 0 .. 2*n) { if (s[i] == 'B') b[i] = 1; else b[i] = 0; } auto a = new int[](2*n); long ans = 1; long cnt; foreach (i ; 0 .. 2*n) { if ((cnt & 1) ^ b[i]) { cnt++; } else { ans *= cnt; ans %= mod; cnt--; } } if (cnt > 0) { writeln(0); return; } foreach (i ; 1 .. n + 1) { ans *= i; ans %= mod; } 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 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; string s = cin.next!string; writeln("2018" ~ s[4 .. $]); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; alias Pair = Tuple!(int, "a", int, "b"); void main() { string s, t; scan(s); scan(t); int N = s.length.to!int; auto rs = new int[](26); auto rt = new int[](26); rs[] = -1; rt[] = -1; bool ok = true; foreach (i, ch ; s) { if (rs[ch - 'a'] == -1) { rs[ch - 'a'] = t[i] - 'a'; if (rt[t[i] - 'a'] != -1 && rt[t[i] - 'a'] != ch - 'a') ok = false; else rt[t[i] - 'a'] = ch - 'a'; } else { if (rs[ch - 'a'] != t[i] - 'a') ok = false; } } writeln(ok ? "Yes" : "No"); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto hw = readln.split.to!(int[]); auto H = hw[0]; auto W = hw[1]; hw = readln.split.to!(int[]); auto h = hw[0]; auto w = hw[1]; writeln(H*W - h*W - w*H + h*w); }
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 S = readln.chomp; S.check.writeln; } int check(string s) { if(s.length & 1) s.popBack(); else { s.popBack; s.popBack; } while(true) { if(s[0..$ / 2] == s[$ / 2..$]) return s.length.to!int; else { s.popBack; s.popBack; } } } // =================================== 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, std.string, std.conv; import std.range, std.algorithm, std.array, std.math; void main() { int h, w; scan(h, w); auto ban = new char[][](h, w); iota(h).each!(i => ban[i] = readln.chomp.to!(char[])); auto cnt = new int[](26); foreach (i ; 0 .. h) { foreach (j ; 0 .. w) { cnt[ban[i][j] - 'a']++; } } if (h & 1) swap(h, w); auto odd = cnt.count!"a % 2 == 1".to!int; auto even = cnt.count!"a % 2 == 0 && a % 4 != 0".to!int; auto m4 = cnt.count!"a % 4 == 0".to!int; bool ok; if ((h & 1) && (w & 1)) { if (odd == 1 && even <= (h / 2 + w / 2)) { ok = 1; } } else if (w & 1) { if (odd == 0 && even <= (h / 2)) { ok = 1; } } else { if (odd == 0 && even == 0) { ok = 1; } } writeln(ok ? "Yes" : "No"); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std; import core.bitop; // dfmt off 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[]); void aryWrite(T = long)(T[] ary){ ary.map!(x => x.text()).join(' ').writeln(); } alias Pair = Tuple!(long, "H", long, "W", long, "cost"); alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less); // dfmt on void main() { auto s = lread(); auto dp = new long[](s + 1); foreach (i; iota(3, s + 1)) { dp[i] += 1; foreach (j; iota(i)) { if (i - j >= 3) { dp[i] += dp[j]; dp[i] %= MOD; } } dp[i] %= MOD; } // dp.writeln(); dp[s].writeln(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std; // dfmt on void main() { long X, Y; scan(X, Y); if (Y & 1) { writeln("No"); return; } if (X * 2 <= Y && Y <= X * 4) { writeln("Yes"); return; } writeln("No"); }
D
void main() { auto s = rs; if(s.length == 2) writeln(s); else writeln(s[2], s[1], s[0]); } // =================================== 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
void main(){ int k = _scan(); string s = readln().chomp(); if( s.length <= k ){ s.writeln(); return; }else{ ( s[0..k] ~ "..." ).writeln(); } } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio, std.string; void main() { readln.replace(",", " ").writeln; }
D
import std.stdio, std.string, std.conv; void main(){ auto ip = readln.split.to!(int[]); if(ip[0] + ip[1] > ip[2] + ip[3]){ writeln("Left"); }else if(ip[0] + ip[1] < ip[2] + ip[3]){ writeln("Right"); }else{ writeln("Balanced"); } }
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; void main(){ auto ip = readln.split.to!(int[]), a = ip[0], b = ip[1]; ((a * b) % 2 == 0 ? "Even" : "Odd").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; 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; } enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { string o, e; scan(o); scan(e); string ans; int i; while (!o.empty || !e.empty) { if (i % 2 == 0) { ans ~= o.front; o.popFront(); } else { ans ~= e.front; e.popFront(); } i++; } writeln(ans); }
D
void main() { long n = rdElem; string s = rdStr; long[] r = new long[n+1], g = new long[n+1], b = new long[n+1]; foreach (i, x; s) { r[i+1] += r[i]; g[i+1] += g[i]; b[i+1] += b[i]; if (x == 'R') ++r[i+1]; if (x == 'G') ++g[i+1]; if (x == 'B') ++b[i+1]; } long cnt; foreach (i; 0 .. n-2) { foreach (j; i+1 .. n-1) { long dist = j - i; if (s[i] == 'R') { if (s[j] == 'G') { cnt += b[n] - b[j]; if (j + dist < n && s[j+dist] == 'B') --cnt; } if (s[j] == 'B') { cnt += g[n] - g[j]; if (j + dist < n && s[j+dist] == 'G') --cnt; } } if (s[i] == 'G') { if (s[j] == 'R') { cnt += b[n] - b[j]; if (j + dist < n && s[j+dist] == 'B') --cnt; } if (s[j] == 'B') { cnt += r[n] - r[j]; if (j + dist < n && s[j+dist] == 'R') --cnt; } } if (s[i] == 'B') { if (s[j] == 'R') { cnt += g[n] - g[j]; if (j + dist < n && s[j+dist] == 'G') --cnt; } if (s[j] == 'G') { cnt += r[n] - r[j]; if (j + dist < n && s[j+dist] == 'R') --cnt; } } } } cnt.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.algorithm; import std.ascii; import std.array; import std.container; import std.conv; import std.numeric; import std.stdio; import std.string; import std.typecons; void log(A...)(A arg) { stderr.writeln(arg); } int size(T)(in T s) { return cast(int)s.length; } const int INF = 10000; int op(char c, int a, int b) { if (c == '+') return a + b; if (c == '-') return a - b; assert(false); } int next_op_index(in string s, int i) { for (int j = i; j < s.size; j++) { if (s[j] == '+' || s[j] == '-') return j; } throw new Exception(""); } int prev_op_index(in string s, int i) { for (int j = i; j >= 0; j--) { if (s[j] == '+' || s[j] == '-') return j; } throw new Exception(""); } void main() { string s = readln.chomp; auto t_index = new int[s.size]; t_index[] = -1; int cur = 0; foreach (i, c; s) { if (c == '(' || c == ')') continue; t_index[i] = cur++; } auto buf = new char[cur]; foreach (i, c; s) { if (c == '(' || c == ')') continue; buf[t_index[i]] = c; } string t = buf.idup; int N = t.size; auto open_banned = new bool[N]; auto close_banned = new bool[N]; foreach (int i, c; s) { try { if (c == '(') close_banned[t_index[next_op_index(s, i)]] = true; else if (c == ')') open_banned[t_index[prev_op_index(s, i)]] = true; } catch (Exception e) {} } auto dp_min = new int[][](N + 1, N + 1); auto dp_max = new int[][](N + 1, N + 1); foreach (ref a; dp_max) a[] = -INF; foreach (ref a; dp_min) a[] = INF; foreach (i, c; t) { if (c.isDigit) dp_max[i][i + 1] = dp_min[i][i + 1] = cast(int)(c - '0'); } for (int k = 2; k <= N; k++) { for (int i = 0; i + k <= N; i++) { for (int j = i + 1; j < i + k; j++) { if (! (t[j] == '+' || t[j] == '-')) continue; int lcount = cast(int)t[i .. j].count!isDigit; int rcount = cast(int)t[j .. i + k].count!isDigit; if (lcount >= 2 && close_banned[j]) continue; if (rcount >= 2 && open_banned[j]) continue; switch (t[j]) { case '+': dp_max[i][i + k] = max(dp_max[i][i + k], dp_max[i][j] + dp_max[j + 1][i + k]); dp_min[i][i + k] = min(dp_min[i][i + k], dp_min[i][j] + dp_min[j+ 1][i + k]); break; case '-': dp_max[i][i + k] = max(dp_max[i][i + k], dp_max[i][j] - dp_min[j + 1][i + k]); dp_min[i][i + k] = min(dp_min[i][i + k], dp_min[i][j] - dp_max[j + 1][i + k]); break; case '(': default: break; } } } } /* foreach (L; dp_max) log(L); log(); foreach (L; dp_min) log(L); */ writeln(dp_max[0][t.size]); }
D
import std.stdio; void main() { char b = 'a'; foreach(c; readln) { if (b == c) { write("Bad"); return; } b = c; } write("Good"); }
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() { int[4] s; foreach (i; 0..3) { auto d = readln.chomp.split.to!(int[]); s[d[0]-1]++; s[d[1]-1]++; } int[4] cnt; foreach (i; 0..4) { cnt[s[i]]++; } if (cnt[1] == 2 && cnt[2] == 2) { writeln("YES"); } else { writeln("NO"); } }
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; void main(string[] args) { string s = readln.chomp; char[] st; foreach(c; s){ if(c == 'T' && !st.empty && st.back == 'S'){ st.popBack(); }else{ st ~= c; } } writeln(st.length); }
D
import std; auto input() { return readln().chomp(); } void main() { long x, y; scan(x, y); if (x % y == 0) { writeln(-1); } else { writeln(x); } } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
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; real[12][1<<12] dp; int[] cakes; int n; real saiki(int u,int v){ if(dp[u][v] > 0) return dp[u][v]; if( u == ((1<<n) - 1)) return cakes[v]; real res = 10000000; for(int i=0;i<n;i++){ if(( (1<<i) & u ) == 0){ res = min(res,saiki(u | (1<<i) ,i) + 2.0 * sqrt(to!real(cakes[i]*cakes[v]) )); } } return dp[u][v] = res; } void main(){ while(true){ string s = readln(); if(stdin.eof()) break; dp = new real[12][1<<12]; auto s1 = s.split().to!(int[]); n = to!int(s1.length) - 1; cakes = (s1[1..$]).dup(); cakes ~= 0; real ans = 1e8; for(int i=0;i<n;i++){ ans = min(saiki(1<<i,i) + cakes[i],ans); } writeln(ans <= s1[0] ? "OK" : "NA"); } }
D
import core.bitop, std.bitmanip; import core.checkedint; import std.algorithm, std.functional; import std.array, std.container; import std.bigint; import std.conv; import std.math, std.numeric; import std.range, std.range.interfaces; import std.stdio, std.string; import std.typecons; void main() { auto s = readln.chomp; int[] sinks; sinks ~= 0; foreach (i; 0 .. s.length - 1) { if (s[i] == 'R' && s[i+1] == 'L') { sinks ~= i.to!int; } } sinks ~= s.length.to!int; debug { sinks.writeln; } auto ans = new int[] (s.length); int j = 0; foreach (i; 0 .. s.length) { if (i > sinks[j+1]) { ++j; } int goToSink = s[i] == 'L' ? sinks[j] : sinks[j+1]; int dist = i.to!int - goToSink; int finalField = dist % 2 == 0 ? goToSink : goToSink + 1; ans[finalField] += 1; debug { writeln(i, ' ', j, ' ', finalField); } } foreach (i; 0 .. s.length) { if (i > 0) { write(" "); } write(ans[i]); } writeln(); }
D
import std.stdio; import std.functional; alias memoize!(c) cm; ulong c(ulong n, ulong k){ if(k==0 || n==k) return 1; return cm(n-1, k) + cm(n-1, k-1); } int main(){ auto s = readln; ulong[char] maji; foreach(i; 0..s.length-1){ maji[s[i]]++; } bool shinu = false; foreach(i; maji){ if(i%2){ if(shinu){ 0.writeln; return 0; } shinu = true; } } ulong[char] anman; ulong total, kazoeruyatu = 1; foreach(i,j; maji) total += (anman[i] = j%2 ? (j-1)/2 : j/2); foreach(i; anman){ kazoeruyatu *= cm(total, i); total -= i; } kazoeruyatu.writeln; return 0; }
D
import std.conv, std.stdio; import std.algorithm, std.array, std.range, std.string; import std.numeric; void main() { auto n = readln.chomp.to!ulong; if (n & 1) return 0.writeln; n /= 2; ulong ret; while (n) { n /= 5; ret += n; } ret.writeln; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); long bignum = 1_000_000_007; T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { long n, m; scan(n, m); abs((n - 2) * (m - 2)).writeln(); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.math; import std.regex; void main() { auto N = readln.chomp.to!int; int count; foreach(_; 0..N) { auto ip = readln.split.to!(int[]); count += ip[1] - ip[0] + 1; } writeln(count); }
D
void main() { string s = rdStr; string f(string x, dchar y) { string t; long len = x.length.to!long - 1; foreach (i; 0 .. len) { if (x[i] == y || x[i+1] == y) t ~= y; else t ~= s[i]; } return t; } long result = inf; foreach (c; lowercase) { string u = s; long cnt; while (u.any!(x => x != c)) { u = f(u, c); ++cnt; } result = min(result, cnt); } result.writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.conv, std.string; void main(){ auto ip = readln.split.to!(int[]); if(ip[0] == 2 || ip[1] == 2){ writeln("No"); } else { writeln("Yes"); } }
D
import std.stdio, std.algorithm, std.array, std.string, std.conv; void main() { int t; scanf("%d", &t); getchar(); foreach(_; 0..t) { int n; scanf("%d", &n); getchar(); auto str1 = readln.strip(); auto str2 = readln.strip(); bool flag = true; for (int i = 0; i < n; i++) { if ((str1[i] == str2[i]) && (str1[i] == '1')) { writeln("NO"); flag = false; break; } } if (flag) writeln("YES"); } }
D
import std.algorithm; import std.array; import std.ascii; 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 log(A...)(A arg) { stderr.writeln(arg); } int size(T)(in T s) { return cast(int)s.length; } void main() { auto s = readln.chomp; int x = 0; foreach (c; s) { if (c == 'C') { if (x == 0) x++; } else if (c == 'F') { if (x == 1) x++; } } writeln(x == 2 ? "Yes" : "No"); }
D
import std.stdio: writeln; void main() { foreach(int i; 0 .. 1000) { writeln("Hello World"); } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { string s; while ((s = readln.chomp) != null) { auto ab = s.split.map!(to!int).array; int a = ab[0], b = ab[1]; writeln(gcd(a, b)); } }
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; auto s = cin.next!(dchar[]); int res; foreach (c; s) { if (c == '1') ++res; } writeln(res); }
D
import std.stdio; import std.string; import std.array; // split import std.conv; // to void main() { string s = chomp(readln()); write("ABC"); writeln(s); }
D
import std.stdio; void main() { int n, m; scanf("%d %d\n", &n, &m); auto add = new int[n]; auto sub = new int[n]; foreach (i; 0..m) { int l, r; scanf("%d %d\n", &l, &r); ++add[l-1]; ++sub[r-1]; } int y, cnt; foreach(i; 0..n) { y += add[i]; if (y == m) ++cnt; y -= sub[i]; } cnt.write; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int N, M; scan(N, M); auto deg = new int[](N); foreach (i ; 0 .. M) { int ai, bi; scan(ai, bi); ai--, bi--; deg[ai]++; deg[bi]++; } deg.each!(d => d.writeln); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
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; const long MOD = 998244353; const long INF = 1L << 59; void main() { auto N = readln.chomp.to!int; auto G = new int[][](N); foreach (_; 0..N) { auto s = readln.split.map!(to!int); G[s[0] - 1] ~= s[1] - 1; G[s[1] - 1] ~= s[0] - 1; } auto loop = new bool[](N); auto used = new bool[](N); auto prev = new int[](N); fill(prev, -1); bool dfs(int n, int p) { prev[n] = p; used[n] = true; foreach (m; G[n]) { if (used[m] && m != p) { for (int x = n; ; x = prev[x]) { loop[x] = true; if (x == m) break; } return true; } if (m != p && dfs(m, n)) { return true; } } return false; } dfs(0, -1); auto Q = readln.chomp.to!int; while (Q--) { auto s = readln.split.map!(to!int); writeln( (loop[s[0] - 1] && loop[s[1] - 1]) ? 2 : 1 ); } }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; import std.array; int n, m; rd(n, m); auto g=new int[][](n, n); foreach(_; 0..m){ int a, b; rd(a, b); g[a-1][b-1]=g[b-1][a-1]=1; } auto v=new int[](0); v~=0; auto usd=new bool[](n); usd[0]=true; int f(int k){ if(k==n){ bool ok=true; foreach(i; 1..n){ ok&=g[v[i-1]][v[i]]==1; } return ok ? 1 : 0; }else{ int ret=0; foreach(j; 1..n)if(!usd[j]){ v~=j; usd[j]=true; ret+=f(k+1); v.popBack; usd[j]=false; } return ret; } } writeln(f(1)); } 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 std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int N; scan(N); auto row = new int[](N); auto col = new int[](N); iota(N).each!(i => scan(row[i], col[i])); auto dp = new int[][](N + 1, N + 1); fillAll(dp, -1); int rec(int l, int r) { if (r - l == 1) { return 0; } if (dp[l][r] != -1) { return dp[l][r]; } dp[l][r] = inf; foreach (i ; l + 1 .. r) { chmin(dp[l][r], rec(l, i) + rec(i, r) + row[l] * row[i] * col[r - 1]); } return dp[l][r]; } auto ans = rec(0, N); 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; auto input() { return readln().chomp(); } alias sread = () => readln.chomp(); alias aryread(T = long) = () => readln.split.to!(T[]); void main() { long n, m; scan(n, m); auto ACしたか = new bool[](n + 1); // ACしたか[n] = false; // writeln(ACしたか); auto 何回WAしたか = new long[](n + 1); long ans1 = 0; long ans2 = 0; foreach (_; 0 .. m) { long p; string s; scan(p, s); // writeln(p, s); if ((ACしたか[p] == false) && (s == "WA")) { 何回WAしたか[p] += 1; } if (s == "AC") { ACしたか[p] = true; } } foreach (i; 0 .. n + 1) { if (ACしたか[i] == true) ans1 += 何回WAしたか[i]; } foreach (i; 0 .. n + 1) { if (ACしたか[i] == true) { ans2 += 1; } } writeln(ans2, ' ', ans1); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
D
import std.stdio; import std.conv; import std.string; void main(){ auto A =readln.chomp.to!int; if(A<1200){ "ABC".writeln; }else{ "ARC".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, core.stdc.stdio; void main() { auto T = readln.chomp.to!int; auto cnt = new int[](10); while (T--) { cnt[] = 0; auto S = readln.chomp; auto N = S.length.to!int; int zero = 0; int even = 0; int div3 = 0; foreach (i; 0..N) { int d = S[i]-'0'; if (d == 0) zero += 1; if (d % 2 == 0) even += 1; div3 = (div3 + d) % 3; } if (zero >= 1 && even >= 2 && div3 == 0) { writeln("red"); } else { writeln("cyan"); } } }
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(b-a == c-b ? "YES" : "NO"); }
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 N = lread(); auto s = sread(); long result; foreach (i; 0 .. N) { uint a, b; foreach (c; s[0 .. i]) a |= 1 << (c - 'a'); foreach (c; s[i .. $]) b |= 1 << (c - 'a'); result.maxAssign((a & b).popcnt); } writeln(result); }
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() { long k, a, b; scan(k, a, b); if (b - a <= 2) { writeln(k + 1); return; } if (k < a - 1) { writeln(k + 1); return; } long ans = a; k -= a - 1; ans += (b - a) * (k / 2); if (k & 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); } } }
D
void main() { string s = readln.chomp; string t = "keyence"; bool ok; foreach (i; 0 .. 8) { if (s[0..i] == t[0..i] && s[$-7+i..$] == t[i..$]) { ok = true; } } writeln(ok ? "YES" : "NO"); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std; // dfmt on void main() { long N = lread(); auto T = new long[](N); auto X = new long[](N); auto Y = new long[](N); foreach (i; 0 .. N) scan(T[i], X[i], Y[i]); long t; long x, y; foreach (i; 0 .. N) { long d = abs(x - X[i]) + abs(y - Y[i]); if ((T[i] - t) < d) { writeln("No"); return; } if (((T[i] - t) - d) & 1) { writeln("No"); return; } t = T[i]; x = X[i]; y = Y[i]; } writeln("Yes"); }
D
void main() { int n = readln.chomp.to!int; int[] a = readln.split.to!(int[]); int cnt; foreach (x; a) { while (x % 2 == 0) { x /= 2; ++cnt; } } 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.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
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 x, t; scan(x, t); writeln((x - t).max(0)); }
D
/* imports all std modules {{{*/ import std.algorithm, std.array, std.ascii, std.base64, std.bigint, std.bitmanip, std.compiler, std.complex, std.concurrency, std.container, std.conv, std.csv, std.datetime, std.demangle, std.encoding, std.exception, std.file, std.format, std.functional, std.getopt, std.json, std.math, std.mathspecial, std.meta, std.mmfile, std.net.curl, std.net.isemail, std.numeric, std.parallelism, std.path, std.process, std.random, std.range, std.regex, std.signals, std.socket, std.stdint, std.stdio, std.string, std.system, std.traits, std.typecons, std.uni, std.uri, std.utf, std.uuid, std.variant, std.zip, std.zlib; /*}}}*/ /+:---test RUDLUDR ---+/ /+---test DULL ---+/ void main(string[] args) { const S = readln.chomp; foreach (i, c; S) { ++i; if ((i%2 == 1 && c == 'L') || (i%2 == 0 && c == 'R')) { "No".writeln; return; } } "Yes".writeln; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto rd = readln.split.to!(int[]), n = rd[0], a = rd[1], b = rd[2]; writeln(min(a*n, b)); }
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]); if(A == B) "Draw".writeln; else if(A == 1 && B > 1) "Alice".writeln; else if(B == 1 && A > 1) "Bob".writeln; else if(A > B) "Alice".writeln; else "Bob".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; immutable long MOD = 10^^9 + 7; void main() { auto N = readln.chomp.to!long; if (N <= 2) { writeln("No"); return; } long S = N * (N - 1) / 2; foreach_reverse (i; 2..N+1) { if (gcd(S-i, i) > 1) { writeln("Yes"); writeln(1, " ", i); write(N-1); foreach (j; 1..N+1) if (j != i) write(" ", j); writeln; return; } } writeln("No"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.range, std.string, std.math, std.typecons; void main() { auto nm = readln.split.to!(long[]); auto N = nm[0]; auto M = nm[1]; long ret; if (N == 1 && M == 1) { ret = 1; } else if (N == 1) { ret = M - 2; } else if (M == 1) { ret = N - 2; } else if (N == 2 || M == 2) { ret = 0; } else { ret = N * M - N * 2 - M * 2 + 4; } writeln(ret); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); void main() { long a, b, c; scan(a, b, c); long max_x = max(a, b, c); long sum_x = a + b + c; long x, y; if (((max_x % 2 == 0) && (sum_x % 2 == 0)) || (max_x % 2 != 0) && (sum_x % 2 != 0)) { x = (max_x * 3 - sum_x) / 2; writeln(x); return; } y = ((max_x * 3 + 3) - sum_x) / 2; writeln(y); } //https://rclone.org/ void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
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(); if (s[0 .. $ - 1].all_eq() || s[1 .. $].all_eq()) writeln("Yes"); else writeln("No"); } bool all_eq(string s) { auto t = s[0]; foreach (e; s) { if (e != t) return false; } return true; } 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; import core.bitop; ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "x", long, "c"); alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less); void main() { auto s = sread(); auto t = sread(); auto nexts = new long[][](s.length, 26); foreach (ref e; nexts) e[] = INF; foreach_reverse (i; iota(2 * s.length)) { foreach (j; iota(26)) { if (s[(i + 1) % s.length] == 'a' + j) nexts[i % s.length][j] = 1; else nexts[i % s.length][j] = nexts[(i + 1) % s.length][j] + 1; } } long idx = s.countUntil(t[0]); foreach (e; t[1 .. $]) { if (idx < 0) break; if (nexts[idx % s.length][e - 'a'] <= s.length) idx += nexts[idx % s.length][e - 'a']; else idx = -1; } writeln(idx >= 0 ? idx + 1 : idx); } 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; import std.array; void main() { int[] T = new int[5]; foreach (i; 0..5){ T[i] = readln.split[0].to!int; } int ans = 0; int minMod = 0; foreach (t; T){ if (t % 10 == 0){ ans += t; } else { if (minMod == 0 || t % 10 < minMod){ minMod = t % 10; } ans += (t / 10 + 1) * 10; } } if (minMod != 0){ ans -= (10 - minMod); } writeln(ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto ab = readln.split.to!(int[]); writeln(ab[0] * ab[1]); }
D
void main(){ int[] hen = _scanln(); (hen[0]*hen[1]/2).writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio; import std.string; import std.conv; void main() { // input int n = to!int(readln.chomp); int k = to!int(readln.chomp); int[] x, y; x.length = y.length = k; for(int i=0; i<k; ++i) { auto temp = readln.split; x[i] = to!int(temp[0]); y[i] = to!int(temp[1]); } // output for(int i=0; i<k; ++i) { if(x[i] > n / 2) x[i] = n - x[i] + 1; if(y[i] > n / 2) y[i] = n - y[i] + 1; int min = x[i] < y[i] ? x[i] : y[i]; writeln((min + 2) % 3 + 1); } }
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 N = readln.split[0].to!int; foreach (n; 1 .. 10) { if (N <= n*111) { writeln(n*111); return; } } }
D
import std.stdio; import std.conv; import std.string; import std.format; void main() { string s = chomp(readln()); int min = to!int(s.split(" ")[0]); int max = to!int(s.split(" ")[1]); int val = to!int(s.split(" ")[2]); int result = 0; for (int i = min; i <= max; i++ ) { if (val % i == 0) { result++; } } writeln(result); }
D
import std.stdio: writeln, writefln, readln; import std.array: array, split; import std.algorithm: map; import std.string: chomp; import std.conv: to; void main() { int[3][3] bingo; foreach(byte i; 0..3) { int[] input = readln.split.map!(to!int).array; foreach(int j; 0..3) bingo[i][j] = input[j]; } int n = readln.chomp.to!int; bool[int] won; foreach(_; 0..n) { int x = readln.chomp.to!int; won[x] = true; } // Check rows foreach(int[3] i; bingo) { if (won.get(i[0], false) && won.get(i[1], false) && won.get(i[2], false)) { writeln("Yes"); return; } } // Check columns foreach(byte i; 0..3) { if (won.get(bingo[0][i], false) && won.get(bingo[1][i], false) && won.get(bingo[2][i], false)) { writeln("Yes"); return; } } bool diagonal_a = true; // Check a diagonal foreach(byte i; 0..3) diagonal_a = diagonal_a && won.get(bingo[i][i], false); // The other diagonal bool diagonal_b = true; foreach(byte i; 0..3) diagonal_b = diagonal_b && won.get(bingo[i][2-i], false); if (diagonal_a || diagonal_b) writeln("Yes"); else writeln("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 Clue = Tuple!(long, "x", long, "y", long, "h"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); long cnt; void main() { auto s = sread(); if(s[2] == s[3] && s[4] == s[5]) writeln("Yes"); else writeln("No"); } 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.string, std.range, std.conv, std.algorithm; void main(){ int s; foreach (i; 0..5) { s += max(readln.chomp.to!int, 40); } writeln(s / 5); }
D
import core.bitop; 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() { string s = readln.chomp; string t = readln.chomp; string ans = '~'.repeat.take(s.length).array; foreach (i; 0 .. s.length - t.length + 1) { bool ok = true; foreach (j, c; t) { ok &= s[i + j] == '?' || s[i + j] == c; } if (ok) { char[] sub = s.map!(a => cast(char)(a == '?' ? 'a' : a)).array; foreach (j, c; t) { sub[i + j] = c; } ans = min(ans, sub.idup); } } if (ans.front == '~') { "UNRESTORABLE".writeln; return; } ans.writeln; }
D
import std.stdio; import std.string; import std.conv; void main() { int R = to!int(chomp(readln())); int G = to!int(chomp(readln())); // (R + P)/2 = G // R + P = 2G // P = 2G - R writeln(2*G - R); }
D
void main(){ int[] abc = _scanln(); writeln(abc.count(5)==2 && abc.count(7)==1? "YES": "NO"); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv, std.string; // import dcomp.foundation, dcomp.scanner; int main() { Scanner sc = new Scanner(stdin); string s; sc.read(s); if (s.startsWith("YAKI")) { writeln("Yes"); } else { writeln("No"); } return 0; } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; if (f.eof) return false; line = lineBuf[]; f.readln(line); } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } version (X86) static if (__VERSION__ < 2071) { import core.bitop : bsf, bsr, popcnt; int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } }
D
import std.stdio, std.range, std.conv, std.string; import std.algorithm.comparison, std.algorithm.iteration, std.algorithm.mutation, std.algorithm.searching, std.algorithm.setops, std.algorithm.sorting; void main() { long s = readln().strip.to!(long); long[] list; list ~= s; while(1) { auto p = list[$-1]; auto n = p % 2 == 0 ? p/2 : p*3+1; foreach(e; list) { if(e==n){ writeln(1+list.length); return; } } list ~= n; } }
D
import std.stdio, std.string, std.conv, std.range, std.algorithm; void main() { auto N = readln.chomp.to!int; auto K = readln.chomp.to!int; auto X = readln.chomp.to!int; auto Y = readln.chomp.to!int; if (N < K) { writeln(N * X); } else { writeln(K * X + (N - K) * Y); } }
D
void main() { string s = readln.chomp; string t = readln.chomp; bool ok; foreach (i; 0 .. s.length) { if (s == t) ok = true; s = s[$-1] ~ s[0..$-1]; } writeln(ok ? "Yes" : "No"); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.container; import std.typecons;
D
// Vicfred // https://atcoder.jp/contests/abc045/tasks/arc061_a // brute force, bitmask import std.algorithm; import std.array; import std.conv; import std.stdio; import std.string; void main() { string s = readln.strip; const long n = s.length; long ans = 0; for(int bit = 0; bit < (1 << n - 1); bit++) { string tmp; for(int idx = 0; idx < n; idx++) { tmp ~= s[idx]; if(!(bit & (1<<idx))) { ans += tmp.to!long; tmp = ""; } } } ans.writeln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; void main() { int a, b; scan(a, b); if (a + b == 15) { writeln("+"); } else if (a * b == 15) { writeln("*"); } else { writeln("x"); } } 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; void main() { long x; scan(x); auto ans = (x / 500) * 1000 + ((x % 500) / 5) * 5; writeln(ans); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string; void main() { auto line = readln.split; auto a = line[0].to!long; auto b = line[2].to!long; writeln(line[1] == "+" ? a + b : a - b); }
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.split("")) { write(toSwapCase(ch)); } }
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 n; readV(n); auto l = new long[](n+1); l[0] = 2; l[1] = 1; foreach (i; 2..n+1) l[i] = l[i-1]+l[i-2]; writeln(l[n]); }
D
import std.stdio; import std.uni; import std.conv; import std.container; import std.functional; import std.algorithm; import std.array; import std.typecons; import std.range; import std.numeric; import std.traits; long pmod(long a, long m) { if (a < 0) return (a % m) + m; return a % m; } void main(string[] args) { enum p = 1_000_000_000 + 7; auto n = next!string; auto tenpow = new long[](n.length); tenpow[n.length - 1] = 1; foreach_reverse(i; 0 .. n.length - 1) tenpow[i] = (tenpow[i + 1] * 10) % p; long rightNum = 0; foreach(d; n) rightNum = ((rightNum * 10) % p + long(d - '0')) % p; long leftSum = 0; long leftNum = 0; long res = 0; foreach(i; 0 .. n.length) { long li = cast(long)i; long dig = n[i] - '0'; rightNum = pmod(rightNum - (dig * tenpow[i]) % p, p); res = (res%p + (rightNum * (li + 1))%p + (leftSum * tenpow[i])%p)%p; leftNum = ((leftNum * 10) % p + dig)%p; leftSum = (leftSum + leftNum) % p; } res.writeln; } static this() { popChar; } struct Any(T) { static auto read() { static if (is(T == char)) { auto res = frontChar; popChar; return res; } } } struct Unsigned(T) { auto read() { auto res = T(0); while(!frontChar.isDigit) popChar; do { res = res * T(10) + T(frontChar - '0'); popChar; } while(frontChar.isDigit); return res; } } struct Matrix(T, alias rows, alias cols) { T[][] _cell; alias _cell this; // Ring constructor this(int n) { final switch(n) { case 0: _cell = new T[][](rows, cols); break; case 1: debug assert(rows == cols); _cell = new T[][](rows, cols); foreach(i; 0 .. rows) _cell[i][i] = T(1); break; } } // Copy constructor this(Matrix!(T, rows, cols) other) { _cell = new T[][](rows, cols); foreach(i; 0 .. rows) _cell[i][] = other[i][]; } auto opBinary(string op)(Matrix!(T, rows, cols) other) if (op == "+" || op == "-") { auto res = Matrix!(T, rows, cols)(0); foreach(i; 0 .. rows) foreach(j; 0 .. cols) res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]}); return res; } auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other) if (op == "*") { debug assert(cols == otherRows); auto res = Matrix!(T, rows, otherCols)(0); foreach(i; 0 .. rows) foreach(k; 0 .. cols) foreach(j; 0 .. otherCols) res[i][j] += this[i][k] * other[k][j]; return res; } ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other) { static if (op == "*") { debug assert(rows == cols); alias n = rows; auto res = Matrix!(T, n, n)(0); T factor; foreach(i; 0 .. n) foreach(k; 0 .. n) foreach(j; 0 .. n) res[i][j] += this[i][k] * other[k][j]; foreach(i; 0 .. n) this[i][] = res[i][]; } else { foreach(i; 0 .. rows) foreach(j; 0 .. cols) mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];}); } return this; } mixin powMethod; } struct Mod(T, alias mod) { alias This = Mod!(T, mod); T _rep; this(T t) { _rep = t % mod; if (_rep < 0) _rep += mod; } static auto plain(T t) { auto res = Mod!(T, mod)(); res._rep = t; return res; } static auto fromPositive(T t) { pragma(inline, true); return Mod!(T, mod).plain(t % mod); } static auto fromNegative(T t) { pragma(inline, true); return Mod!(T, mod).plain(t % mod + mod); } string toString() { return to!string(_rep); } debug invariant { assert(_rep >= 0 && _rep < mod); } auto opBinary(string op)(This b) { static if (op == "+") { T resrep = _rep + b._rep; if (resrep >= mod) resrep -= mod; return This.plain(resrep); } else static if (op == "-") { T resrep = _rep - b._rep; if (resrep < 0) resrep += mod; return This.plain(resrep); } else static if (op == "*") { return This.fromPositive(_rep * b._rep); } } auto opOpAssign(string op)(This b) { mixin(q{_rep }, text(op, "="), q{b._rep;}); static if (op == "+") { if (_rep >= mod) _rep -= mod; } else static if (op == "-") { if (_rep < 0) _rep += mod; } else static if (op == "*") { _rep %= mod; } return this; } auto opBinary(string op)(long exp) if (op == "^^") { return pow(exp); } mixin powMethod; } mixin template powMethod() { auto pow(this ThisType)(long exp) { auto res = ThisType(1); auto pow = ThisType(this); while(exp) { if (exp & 1) res *= pow; pow *= pow; exp >>= 1; } return res; } } // INPUT char frontChar; void popChar() { import core.stdc.stdio; frontChar = cast(char) getchar; if (feof(stdin)) frontChar = ' '; } auto makeArray(T, S...)(S s) { enum brackets = () { string res; foreach(i; 0 .. s.length) res ~= "[]"; return res; } (); mixin(q{alias Type = T}, brackets, q{;}); return new Type(s); } template isNumberLike(T) { enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}())); } void popWhite() { import std.ascii; while (frontChar.isWhite) popChar; } void print(T...)(T t) { static foreach(ti; t) { static if (is(typeof(ti) == string)) { write(ti, " "); } else static if (isArray!(typeof(ti))) { foreach(e; ti) print(e); } else static if (isTuple!(typeof(ti))) { static foreach(i; ti.length) writesp(ti[i]); } else { write(ti, " "); } } } void println(T...)(T t) { static if (t.length == 0) writeln; static foreach(ti; t) { print(ti); writeln; } } auto next(alias T, S...)(S s) { pragma(inline, true); static if (s.length > 0) { auto res = makeArray!(typeof(next!T()))(s); S t; enum loops = () { string res; foreach(i; 0 .. s.length) { auto ti = text(q{t[}, i, q{]}); res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)}); } return res; } (); enum indexing = () { string res = "res"; foreach(i; 0 .. s.length) res ~= text(q{[t[}, i, q{]]}); return res; } (); mixin(loops, indexing, q{ = next!T;}); return res; } else { static if (isNumberLike!T) { import std.ascii: isDigit; T res; while(frontChar.isWhite) popChar; T multiplier = T(1); if (frontChar == '-') { multiplier = T(-1); popChar; } else if (frontChar == '+') { popChar; } debug assert(frontChar.isDigit); do { res = res * T(10) + T(frontChar - '0'); popChar; } while(frontChar.isDigit); return multiplier * res; } else static if (is(T == string)) { import std.ascii: isWhite; string res; while(frontChar.isWhite) popChar; while(!frontChar.isWhite) { res ~= frontChar; popChar; } return res; } else static if (is(T == char)) { while(frontChar.isWhite) popChar; auto res = frontChar; popChar; return res; } else { return T.read; } } }
D
/+ dub.sdl: name "A" dependency "dunkelheit" version=">=0.9.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; int main() { Scanner sc = new Scanner(stdin); scope(exit) assert(!sc.hasNext); int h, w; sc.read(h, w); bool[][] g = new bool[][](h, w); foreach (i; 0..h) { string s; sc.read(s); foreach (j; 0..w) { g[i][j] = (s[j] == '#'); } } bool[][] g2 = new bool[][](h, w); foreach (i; 0..h) { foreach (j; 0..w) { if (!g[i][j]) continue; int[] hv, wv; foreach (i2; 0..h) { if (g[i2][j]) hv ~= i2; } foreach (j2; 0..w) { if (g[i][j2]) wv ~= j2; } foreach (i2; hv) { foreach (j2; wv) { g2[i2][j2] = true; } } } } foreach (i; 0..h) { foreach (j; 0..w) { if (g[i][j] != g2[i][j]) { writeln("No"); return 0; } } } writeln("Yes"); // foreach (v; g2) { // writeln(v.map!(x => x ? '#' : '.')); // } 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.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto n = RD!int; long lp, lc; ans[ti] = true; foreach (i; 0..n) { auto p = RD; auto c = RD; if (p < c || p < lp || c < lc) { ans[ti] = false; } auto d1 = p - lp; auto d2 = c - lc; if (d1 < d2) ans[ti] = false; lp = p; lc = c; } } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush; debug readln; }
D
void main() { writeln(canFind(rs, "AC") ? "Yes" : "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.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 readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}} void main() { int n, m; readV(n, m); long[] x, y, z; readC(n, x, y, z); auto r = -10L^^18; foreach (p; 0..1<<3) { auto sx = p.bitTest(0) ? 1 : -1; auto sy = p.bitTest(1) ? 1 : -1; auto sz = p.bitTest(2) ? 1 : -1; auto dp = new long[][](n+1, m+1); foreach (dpi; dp) dpi[] = -10L^^18; dp[0][0] = 0; foreach (i; 0..n) foreach (j; 0..m+1) { dp[i+1][j] = dp[i][j]; if (j > 0) dp[i+1][j] = max(dp[i+1][j], dp[i][j-1]+x[i]*sx+y[i]*sy+z[i]*sz); } r = max(r, dp[n][m]); } writeln(r); } pragma(inline) { pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; } pure T bitSet(T)(T n, size_t i) { return n | (T(1) << i); } pure T bitReset(T)(T n, size_t i) { return n & ~(T(1) << i); } pure T bitComp(T)(T n, size_t i) { return n ^ (T(1) << i); } pure T bitSet(T)(T n, size_t s, size_t e) { return n | ((T(1) << e) - 1) & ~((T(1) << s) - 1); } pure T bitReset(T)(T n, size_t s, size_t e) { return n & (~((T(1) << e) - 1) | ((T(1) << s) - 1)); } pure T bitComp(T)(T n, size_t s, size_t e) { return n ^ ((T(1) << e) - 1) & ~((T(1) << s) - 1); } import core.bitop; pure int bsf(T)(T n) { return core.bitop.bsf(ulong(n)); } pure int bsr(T)(T n) { return core.bitop.bsr(ulong(n)); } pure int popcnt(T)(T n) { return core.bitop.popcnt(ulong(n)); } }
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; 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; } enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { int N; scan(N); auto cnt = new int[](26); cnt[] = inf; foreach (i ; 0 .. N) { string s; scan(s); auto c = new int[](26); foreach (ch ; s) { c[ch - 'a']++; } foreach (j ; 0 .. 26) { chmin(cnt[j], c[j]); } } string ans; foreach (i ; 0 .. 26) { foreach (j ; 0 .. cnt[i]) { ans ~= (i + 'a').to!char; } } writeln(ans); }
D
import std.stdio, std.string, std.conv; import std.algorithm, std.array; auto solve(string s_) { auto NT = s_.split.map!(to!int)(); immutable N=NT[0], T=NT[1]; auto A = readln.split.map!(to!int).array(); int m=0,d=0,nl=0, nr=0, n=0, nm=0; foreach_reverse(v;A) { if(v>m) m=v,++n,nm=1; else if(v==m) ++n,++nm; else if(m-v>d) d=m-v,nl=1,nr=nm,n=nm; else if(m-v==d) ++nl,nr=n; } return min(nl,nr); } void main(){ for(string s; (s=readln.chomp()).length;) writeln(solve(s)); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; void main() { int n; scan(n); if (n <= 999) { writeln("ABC"); } else { writeln("ABD"); } } 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; void main() { string s; scan(s); auto ok = iota(3).all!(i => s[i] == s[0]); yes(!ok); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.stdio, std.string, std.conv; import std.array, std.algorithm, std.range; void main() { string s; do{ char[][] m; while((s=readln()).chomp().length) m~=s.chomp().to!(char[]); void update(int x, int y) { if(x<0 || m.length<=x || y<0 || m[x].length<=y) return; if(m[x][y]=='0') return; m[x][y]='0'; foreach(a;[[1,0],[-1,0],[0,1],[0,-1]]) update(x+a[0],y+a[1]); } int c=0; foreach(x;0..cast(int)(m.length)) foreach(y;0..cast(int)(m[x].length)) if(m[x][y]=='1') ++c,update(x,y); writeln(c); }while(s.length); }
D
/+ dub.sdl: name "C" dependency "dunkelheit" version=">=0.9.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; int main() { Scanner sc = new Scanner(stdin); scope(exit) assert(!sc.hasNext); int n, m; sc.read(n, m); int a, b; //a : <>, b : ^v sc.read(a, b); char[][] g = new char[][](n, m); foreach (i; 0..n) foreach (j; 0..m) g[i][j] = '.'; if (n % 2) { foreach (i; 0..m/2) { if (!a) break; a--; g[n-1][2*i] = '<'; g[n-1][2*i+1] = '>'; } } if (m % 2) { foreach (i; 0..n/2) { if (!b) break; b--; g[2*i + (n%2)][m-1] = '^'; g[2*i+1 + (n%2)][m-1] = 'v'; } } if (a % 2 && b % 2 && n % 2 && m % 2 && n != 1 && m != 1) { a--; b--; g[0][m-3] = '^'; g[1][m-3] = 'v'; g[0][m-2] = '<'; g[0][m-1] = '>'; } foreach (i; 0..n/2) { foreach (j; 0..m/2) { if (g[2*i][2*j] != '.') continue; if (a) { a--; g[2*i][2*j] = '<'; g[2*i][2*j+1] = '>'; if (a) { a--; g[2*i+1][2*j] = '<'; g[2*i+1][2*j+1] = '>'; } } else if (b) { b--; g[2*i][2*j] = '^'; g[2*i+1][2*j] = 'v'; if (b) { b--; g[2*i][2*j+1] = '^'; g[2*i+1][2*j+1] = 'v'; } } } } if (a || b) { writeln("NO"); return 0; } writeln("YES"); foreach (s; g) { writeln(s); } return 0; } /* IMPORT /Users/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 /Users/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 /Users/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 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, "begin", long, "end"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long n, r; scan(n, r); if(n < 10) writeln(r + 100 * (10 - n)); else writeln(r); } 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; 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 = new string[](3); foreach (i; 0..3) { s[i] = readln.chomp; } auto idx = new int[](3); int i; while (idx[i] < s[i].length) { idx[i]++; i = s[i][idx[i]-1]-'a'; } writeln(('A'+i).to!char); }
D