code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; int[][] G; G.length = N; foreach (_; 0..M) { auto ab = readln.split.to!(int[]); auto A = ab[0]-1; auto B = ab[1]-1; G[A] ~= B; G[B] ~= A; } auto es = new int[](N); long x, y, z; void walk(int i, int p, int c) { c = c == 1 ? 2 : 1; foreach (j; G[i]) if (j != p) { if (es[j] == 0) { if (c == 1) { ++x; } else { ++y; } es[j] = c; walk(j, i, c); } else if ((es[j] == 1 && c == 2) || es[j] == 2 && c == 1) { if (es[j] == 1) { --x; } else { --y; } ++z; es[j] = 3; walk(j, i, c); } } } es[0] = 1; ++x; walk(0, -1, 1); bool same(int i, int j) { return (es[i] == 1 && es[j] == 2) || (es[i] == 2 && es[j] == 1) || (es[i] == 3 && es[j] == 3); } auto memo = new bool[](N); memo[0] = true; long r; void solve(int i) { long n = (es[i] == 1 ? y : es[i] == 2 ? x : z); if (es[i] == 3) --n; foreach (j; G[i]) if (same(i, j)) --n; r += n; foreach (j; G[i]) if (!memo[j]) { memo[j] = true; solve(j); } } solve(0); writeln(r/2); }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ int n = read.to!int; long[] as; foreach(i; 0 .. n) as ~= read.to!long; int[long] ac; foreach(a; as){ if(a !in ac) ac[a] = 0; ac[a] += 1; } string ans; if(ac.keys.length > 3) ans = "No"; else if(ac.keys.length == 3){ long[] ks = ac.keys; if(ac[ks[0]] == ac[ks[1]] && ac[ks[1]] == ac[ks[2]] && (ks[0] ^ ks[1]) == ks[2]) ans = "Yes"; else ans = "No"; } else if(ac.keys.length == 2){ long[] ks = ac.keys; if(ac[ks[0]] == ac[ks[1]] * 2 && (ks[0] ^ ks[0]) == ks[1]) ans = "Yes"; else if(ac[ks[0]] * 2 == ac[ks[1]] && (ks[0] ^ ks[1]) == ks[1]) ans = "Yes"; else ans = "No"; } else if(ac.keys.length == 1){ long[] ks = ac.keys; if(ks[0] == 0) ans = "Yes"; else ans = "No"; } ans.writeln; }
D
import std.stdio, std.conv, std.string, std.algorithm, std.range, std.array; void main() { auto input = readln.split.to!(int[]); writeln((input[0] % 3 == 0 || input[1] % 3 == 0 || input.sum % 3 == 0) ? "Possible" : "Impossible"); }
D
import std.stdio; import std.string; import std.conv,std.array,std.datetime; void main(){ while(1){ auto cs = readln().chomp().split(); if( cs[0]=="0" ){ break; } final switch( Date( 2004, to!int(cs[0]), to!int(cs[1])).dayOfWeek() ){ case 0:writeln("Sunday");break; case 1:writeln("Monday");break; case 2:writeln("Tuesday");break; case 3:writeln("Wednesday");break; case 4:writeln("Thursday");break; case 5:writeln("Friday");break; case 6:writeln("Saturday");break; } } }
D
import std.stdio, std.conv, std.array, std.string; void main() { int n = to!int(chomp(readln())); while(n--) { string[] l = split(readln()); int a = to!int(l[0]); int b = to!int(l[1]); int c = to!int(l[2]); if(a*a==b*b+c*c ||b*b==c*c+a*a ||c*c==a*a+b*b) { writeln("YES"); } else { writeln("NO"); } } }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; const MOD = 10^^9 + 7; void chmin(T)(ref T a, in T b) { if (a > b) a = b; } void madd(T)(ref T a, in T x) { a += x; a %= MOD; } int calc(string s) { auto dp = new int[][](s.length + 1, 13); // dp[i][j] // i : i 桁目までみた // j : 13 で割った余 dp[0][0] = 1; for (int i = 0; i < s.length; i++) { // i 桁目 for (int j = 0; j < 13; j++) { // 余り foreach (k; s[i] == '?' ? iota(0, 10) : iota(s[i]-'0', s[i]-'0'+1)) { // dp[i + 1][(10 * j + k) % 13] += dp[i][j]; // dp[i + 1][(10 * j + k) % 13] %= MOD; madd(dp[i + 1][(10 * j + k) % 13], dp[i][j]); } } } return dp[s.length][5]; } void main() { string s = read!string; writeln(calc(s)); }
D
// Cheese-Cracker: cheese-cracker.github.io void play(){ int n, m; n = rd!int; m = rd!int; int summ = 0, el; foreach(i; 0..n){ el = rd!int; summ += el; } if(summ == m){ writeln("YES"); }else{ writeln("NO"); } } int main(){ long t = 1; t = rd; // Toggle! while(t--) play(); // Let's play! stdout.flush; return 0; } /**********It's A Me Mario!**********/ import std.stdio, std.conv, std.functional, std.string, std.algorithm; import std.container, std.range, std.typecons, std.numeric, std.math, std.random; static string[] inp; T rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;} T[] rdarr(T = long)(){ auto r = readln.chomp.split.to!(T[]); return r; } void show(A...)(A a) { debug{ foreach(t; a){ write(t, "| "); } writeln; } } alias ll = long; alias rbt = redBlackTree; alias tup = Tuple!(long, "x", long, "y"); T max(T = long)(T a, T b){ return (a > b) ? a : b; } T min(T = long)(T a, T b){ return (a < b) ? a : b; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int l, r, a; rd(l, r, a); if(l>r) swap(l, r); if(r-l>=a){writeln((l+a)*2); return;} a-=(r-l); if(a&1) a--; writeln(r*2+a); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto S = readln.chomp; auto T = readln.chomp; int[26] cs, ds; foreach (i; 0..S.length) { auto s = S[i] - 'a'; auto t = T[i] - 'a'; if (cs[s] == 0 && ds[t] == 0) { cs[s] = t+1; ds[t] = s+1; } else if (cs[s] != t+1 || ds[t] != s+1) { writeln("No"); return; } } writeln("Yes"); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { auto S = sread(); foreach_reverse (i; 1 .. S.length - 1) { if (i % 2 == 0 && S[0 .. i / 2] == S[i / 2 .. i]) { writeln(i); return; } } }
D
import std.conv, std.functional, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } void main() { try { for (; ; ) { const N = readInt(); const K = readInt(); const ans = (N >= 1 + 2 * (K - 1)); writeln(ans ? "YES" : "NO"); } } catch (EOFException e) { } }
D
// import chie template :) {{{ static if (__VERSION__ < 2090) { import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons, std.format, std.bitmanip, std.numeric; } else { import std; } // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan(T...)(ref T args) { foreach (ref arg; args) { arg = next!(typeof(arg)); } } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } // }}} // alias {{{ alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less); alias MinHeap(T) = Heap!(T, "a > b"); // }}} // memo {{{ /* - ある値が見つかるかどうか <https://dlang.org/phobos/std_algorithm_searching.html#canFind> canFind(r, value); -> bool - 条件に一致するやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#filter> // 2で割り切れるやつ filter!"a % 2 == 0"(r); -> Range - 合計 <https://dlang.org/phobos/std_algorithm_iteration.html#sum> sum(r); - 累積和 <https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold> // 今の要素に前の要素を足すタイプの一般的な累積和 // 累積和のrangeが帰ってくる(破壊的変更は行われない) cumulativeFold!"a + b"(r, 0); -> Range - rangeをarrayにしたいとき array(r); -> Array - 各要素に同じ処理をする <https://dlang.org/phobos/std_algorithm_iteration.html#map> // 各要素を2乗 map!"a * a"(r) -> Range - ユニークなやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#uniq> uniq(r) -> Range - 順列を列挙する <https://dlang.org/phobos/std_algorithm_iteration.html#permutations> permutation(r) -> Range - ある値で埋める <https://dlang.org/phobos/std_algorithm_mutation.html#fill> fill(r, val); -> void - バイナリヒープ <https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap> // 昇順にするならこう(デフォは降順) BinaryHeap!(Array!T, "a > b") heap; heap.insert(val); heap.front; heap.removeFront(); - 浮動小数点の少数部の桁数設定 // 12桁 writefln("%.12f", val); - 浮動小数点の誤差を考慮した比較 <https://dlang.org/phobos/std_math.html#.approxEqual> approxEqual(1.0, 1.0099); -> true - 小数点切り上げ <https://dlang.org/phobos/std_math.html#.ceil> ceil(123.4); -> 124 - 小数点切り捨て <https://dlang.org/phobos/std_math.html#.floor> floor(123.4) -> 123 - 小数点四捨五入 <https://dlang.org/phobos/std_math.html#.round> round(4.5) -> 5 round(5.4) -> 5 */ // }}} void main() { auto cin = new Scanner; int x, y; cin.scan(x, y); if (y % 2 == 0 && x * 2 <= y && y <= x * 4) { writeln("Yes"); } else { writeln("No"); } }
D
import std.stdio, std.algorithm, std.array, std.string, std.conv; void main() { auto s = readln.chomp.to!(dchar[]); if (s.length == 3) s.reverse(); writeln(s); }
D
const int m=10000000+10; bool[m] pp; void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); if(n==1){ writeln(0); return; } if(n==2){ writeln(0); return; } mp(); int cnt=0; for(int p=1; p<=n; p++){ if(pp[p] && pp[p+2]){ cnt++; } } writeln(cnt*2); } void mp(){ foreach(i; 0..m) pp[i]=true; pp[0]=pp[1]=false; for(int i=2; i*i<=m; i++){ if(pp[i]){ for(int j=2; i*j<m; j++){ pp[i*j]=false; } } } } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x) e=l[i].to!(typeof(e)); }
D
void main() { while(true) { auto ip = readAs!(int[]), H = ip[0], W = ip[1]; if(H == 0 && W == 0) return; foreach(i; 0..H) { bool flag = i & 1; foreach(j; 0..W) { if(flag) { if(j & 1) write('#'); else write('.'); } else { if(j & 1) write('.'); else write('#'); } } writeln; } writeln; } } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.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 N = readln.chomp.to!(ulong); void solve() { ulong answer; ulong keta = 1; ulong[ulong] ht_set; foreach(i; 11..100) ht_set[i] = 0; foreach(i; 1..N+1) { auto tail = i % 10; if (tail == 0) { keta = i.to!(string).length; continue; } auto head = i / 10.pow(keta - 1); auto htkey = head*10 + tail; if (head == tail) { answer += ht_set[htkey] * 2 + 1; } else { auto inverted_key = tail*10 + head; answer += ht_set[inverted_key] * 2; } ht_set[htkey]++; auto inverted = i % 10.pow(keta - 1); inverted -= inverted % 10; inverted += head + tail * 10.pow(keta - 1); if (inverted <= i) { } } answer.writeln(); // ht_set.writeln(); } solve(); }
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 = readln.chomp; auto l_res = new int[](s.length + 1); auto r_res = new int[](s.length + 1); foreach (i; 0..s.length) { if (s[i] == '<') { l_res[i+1] = l_res[i] + 1; } if (s[$-1-i] == '>') { r_res[$-1-i-1] = r_res[$-1-i] + 1; } } long res; foreach (i; 0..l_res.length) { res += max(l_res[i], r_res[i]); } res.writeln; }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.container; import std.datetime; void main() { while (1) { auto x = readln.chomp.split.map!(to!int).array; if (x[0] == 0) break; x[1]--; int winner, sum; foreach (i; 0..x[0]) { auto a = readln.chomp.to!int; if (i == x[1]) winner = a; sum += a; } if (!winner) writeln(0); else { writeln(sum * (100-x[2]) / winner); } } }
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; void main() { string s, t; scan(s, t); int a, b; scan(a, b); string u; scan(u); if (u == s) a--; else b--; writeln(a, " ", b); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void main() { auto N = readln.chomp.to!int; writeln((N+999)/1000 * 1000 - N); }
D
void main() { long n = readln.chomp.to!long; long[5] cnts; string t = "MARCH"; foreach (i; 0 .. n) { string s = readln.chomp; foreach (j, x; t) { if (x == s[0]) ++cnts[j]; } } long result; foreach (i; 0 .. 3) { foreach (j; i+1 .. 4) { long mul = cnts[i] * cnts[j]; foreach (k; j+1 .. 5) { result += mul * cnts[k]; } } } result.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
void main() { auto s = rs.to!(dchar[]); s[5] = s[13] = ' '; s.writeln; } // =================================== import std.stdio; import std.string; import std.conv; 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 n = readln.chomp.to!int; int h = readln.chomp.to!int; int w = readln.chomp.to!int; writeln((n - h + 1) * (n - w + 1)); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
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 ab = readints; int a = ab[0], b = ab[1]; writeln(a * b % 2 == 0 ? "Even" : "Odd"); }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { iota(1, readln.chomp.to!(int)+1).count!(x => x % 2 == 1 && iota(1,x+1).count!(i => x % i == 0) == 8).writeln; }
D
import std.stdio, std.math, std.algorithm, std.array, std.string, std.conv, std.container, std.range; pragma(inline, true) T[] Reads(T)() { return readln.split.to!(T[]); } alias reads = Reads!int; pragma(inline, true) void scan(Args...)(ref Args args) { string[] ss = readln.split; foreach (i, ref arg ; args) arg = ss[i].parse!int; } void main() { int a; scan(a); string s = readln.chomp; writeln(a < 3200 ? "red" : s); }
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } /* 桁の数字の和 digit sum ---------------------------------------- 「1 以上 j99999... (9がi個) 以下の整数のうち、桁の数字の和を D でわったあまりが r であるものの個数」を xs[i][j][r] とする。 xs[0][j][r] = (j == r? 1: 0) xs[i][0] = xs[i - 1][9] である xs[i][j] は、xs[i][j - 1] に、「xs[i - 1][9] を j だけずらしたもの」を項ごとにたしたもの 具体的には xs[i][j][r] = xs[i][j - 1][r] + xs[i - 1][9][(r + d - j) % d] K = 47185, D = 50 の場合 ・39999 以下 …… xs[4][3][0] ・46999 以下 …… xs[3][6][46] ・47099 以下 …… xs[2][0][39] ・47179 以下 …… xs[1][7][38] ・47184 以下 …… xs[0][4][30] ・47185 に分けて求める ---------------------------------------- */ const mod = 1_000_000_007; void main(){ string kstr = readln.chomp; long d = read.to!long; long[][][] xs; xs ~= [[]]; foreach(j; 0 .. 10){ xs[0] ~= [[]]; foreach(r; 0 .. d){ if(j == 0){ if(r == 0) xs[0][j] ~= 1; else xs[0][j] ~= 0; } else if(j % d == r) xs[0][j] ~= xs[0][j - 1][r] + 1; else xs[0][j] ~= xs[0][j - 1][r]; } } foreach(i; 1 .. kstr.length + 1){ xs ~= [[]]; foreach(j; 0 .. 10){ xs[i] ~= [[]]; foreach(r; 0 .. d){ debug writeln("i:", i, " j:", j, " r:", r, " xs:", xs); if(j == 0) xs[i][j] ~= xs[i - 1][9][r]; else xs[i][j] ~= (xs[i][j - 1][r] + xs[i - 1][9][(r + d * j - j) % d]) % mod; } } } long ans; long offset = 0; foreach(iv, c; kstr){ long i = kstr.length - 1 - iv; debug writeln("iv:", iv, " c:", c, " i:", i, " offset:", offset); if(c >= '1') ans += xs[i][c - '1'][offset], ans %= mod; offset += d - (c - '0'), offset %= d; debug writeln("ans:", ans); } if(offset == 0) ans += 1, ans %= mod; ans += mod - 1, ans %= mod; //「0」が含まれてしまうので ans.writeln; }
D
import std.algorithm; import std.string; import std.stdio; void main() { readln; string s = readln.chomp; if (s.length < 3) { writeln("0"); return; } auto c = count(s, 'R') * count(s, 'G') * count(s, 'B'); foreach (i;1..(s.length + 1) / 2) { for (size_t j = 0;j + i + i < s.length;++j) { if (cast(int)s[j] + s[j + i] + s[j + i + i] == 0x52 + 0x47 + 0x42) { --c; } } } writeln(c); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string; immutable long MOD = 10^^9 + 7; immutable long INF = 1L << 59; alias Node = Tuple!(long, "sum", long, "lmax", long, "rmax", long, "allmax"); Node merge(Node l, Node r) { long lmax = max(l.lmax, l.sum + r.lmax); long rmax = max(r.rmax, r.sum + l.rmax); long allmax = max(l.allmax, r.allmax, l.rmax + r.lmax); return Node(l.sum + r.sum, lmax, rmax, allmax); } class SegmentTree(T, alias op, T e) { T[] table; int size; int offset; this(int n) { size = 1; while (size <= n) size <<= 1; size <<= 1; table = new T[](size); fill(table, e); offset = size / 2; } void assign(int pos, T val) { pos += offset; table[pos] = val; while (pos > 1) { pos /= 2; table[pos] = op(table[pos*2], table[pos*2+1]); } } T query(int l, int r) { if (r < l) return e; return query(l, r, 1, 0, offset-1); } T query(int l, int r, int i, int a, int b) { if (b < l || r < a) { return e; } else if (l <= a && b <= r) { return table[i]; } else { return op(query(l, r, i*2, a, (a+b)/2), query(l, r, i*2+1, (a+b)/2+1, b)); } } } void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto Q = s[1]; auto A = readln.split.map!(to!long).array; auto st = new SegmentTree!(Node, merge, Node(0L, -INF, -INF, -INF))(N); foreach (i; 0..N) st.assign(i, Node(A[i], A[i], A[i], A[i])); writeln(max(0L, st.query(0, N-1).allmax)); while (Q--) { auto q = readln.split; auto i = q[0].to!int - 1; auto x = q[1].to!long; st.assign(i, Node(x, x, x, x)); writeln(max(0L, st.query(0, N-1).allmax)); } }
D
import std.stdio, std.string; void main() { string s = readln.chomp; bool ok = true; foreach (i; 1 .. 4) { if (s[i] == s[i-1]) ok = false; } writeln(ok ? "Good" : "Bad"); }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { struct Path { long begin, end; } Path[] pathList; foreach (i; 0..3) { const tmp = readln.split.to!(long[]); pathList ~= Path(tmp[0], tmp[1]); } foreach (start; 1..5) { auto routeIndexList = [0,1,2]; bool[4] flag; do { long current = start; bool ok = true; foreach (i; 0..3) { const path = pathList[routeIndexList[i]]; if (path.begin == current) { current = path.end; } else if (path.end == current) { current = path.begin; } else { ok = false; break; } } if (ok) { writeln("YES"); return; } } while(routeIndexList.nextPermutation); } writeln("NO"); }
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 times(alias pred)(int n) { foreach(i; 0..n) pred(); } auto rep(alias pred, T = typeof(pred()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = pred(); return res; } void main() { string str = readln.chomp; for(int i=0; i+1<str.length; i++) { if (i>=0 && str[i..i+2]=="ST") { str = str[0..i]~str[i+2..$]; i -= 2; } } str.length.writeln; }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; import std.datetime; import std.numeric; import std.math; import std.string; string my_readln() { return chomp(readln()); } void main() { auto tokens = my_readln().split(); auto N = tokens[0].to!uint; long[] a; foreach (token; my_readln.split) { a ~= token.to!long; } auto dp = new long[][](3005, 3005); foreach_reverse (i; 0..N) { foreach (j; i+1..N+1) { long turn = (N - (j - i)) % 2 == 0 ? 1 : -1; auto r1 = dp[i][j-1] + a[j-1] * turn; auto r2 = dp[i+1][j] + a[i] * turn; auto r = turn == 1 ? max(r1, r2) : min(r1, r2); dp[i][j] = r; } } writeln(dp[0][N]); stdout.flush(); }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ int n = read.to!int; int a = read.to!int - 1; int b = read.to!int - 1; int c = read.to!int - 1; int d = read.to!int - 1; bool[] s = readln.chomp.to!(char[]).map!(x => x == '#').array; string ans = "Yes"; foreach(i; a + 1 .. d){ if(s[i] && s[i + 1]) ans = "No"; } if(c < d){ ans.writeln; return; } bool f2 = 0; foreach(i; b - 1 .. d){ if(! s[i] && ! s[i + 1] && ! s[i + 2]) f2 = 1; } if(! f2) ans = "No"; ans.writeln; return; } /* A<B<C<Dの場合 AからDまでに2連続の岩があれば× それ以外は○ A<B<D<Cの場合 AからDまでに2連続の岩があれば× BからDまでに3連続の土地がなければ× それ以外は○ */
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto x = readln.chomp.to!long; long r = x / 11L * 2; if (x%11L >= 7) { r += 2; } else if (x%11L >= 1) { r += 1; } writeln(r); }
D
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } void main() { try { for (; ; ) { const H1 = readInt(); const M1 = readInt(); const H2 = readInt(); const M2 = readInt(); const K = readInt(); int ans; ans -= H1 * 60 + M1; ans += H2 * 60 + M2; ans -= K; writeln(ans); } } catch (EOFException e) { } }
D
import std.functional, std.algorithm, std.container, std.typetuple, std.typecons, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv; void main() { auto ip = readln.split.to!(int[]); if(ip[0] == ip[1]) writeln("a == b"); else if(ip[0] > ip[1]) writeln("a > b"); else writeln("a < b"); }
D
import std.stdio; import std.conv; import std.string; import std.math; import std.array; void main(){ int N = readln().chomp().to!int(); int[] nums; int[] primes; for(int i = 0; i < N; i++){ nums ~= readln().chomp().to!int(); } foreach(int n; nums){ bool isPrime = true; for(int i = 2; i <= sqrt(n.to!float()); i++){ if(n % i == 0){ isPrime = false; break; } } if(isPrime){ primes ~= n; } } primes.length.writeln(); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; // dfmt on void main() { auto S = sread(); auto L = new long[](S.length); L[0] = 0; foreach (i; 1 .. S.length) L[i] = S[i] == 'L' ? L[i - 1] : i; auto R = new long[](S.length); R[$ - 1] = S.length - 1; foreach_reverse (i; 0 .. S.length - 1) R[i] = S[i] == 'R' ? R[i + 1] : i; auto ans = new long[](S.length); // writeln(R, L); foreach (i; 0 .. S.length) { if (S[i] == 'L') { long d = i - L[i]; // writeln("L",d); ans[L[i] + (d & 1)]++; } else if (S[i] == 'R') { long d = R[i] - i; // writeln("R",d); ans[R[i] - (d & 1)]++; } } write(ans[0]); foreach (v; ans[1 .. $]) write(" ", v); writeln(); }
D
import std.stdio; import std.conv; import std.string; import std.format; import std.algorithm; import std.math; //string lines = q"[ //abcdeffg]"; long INF = pow(10, 9); long[] H = [0L]; long cost(int i, int j) { return abs(H[i]-H[j]).to!long; } void main() { string lines; string buf; while (!stdin.eof) { buf = stdin.readln(); lines ~= buf; } string[] array = splitLines(lines); int N = array[0].split(" ")[0].to!int; int K = array[0].split(" ")[1].to!int; array[1].split(" ").each!(a => H ~= a.to!long); long[] dp = new long[](N+1); foreach (ref long e; dp) { e = INF; } dp[1] = 0L; for (int i = 1; i <= N; i++) { for (int j = i+1; j <= i+K; j++) { if (j>N) continue; dp[j] = min( cost(i, j) + dp[i], dp[j] ); } } writeln(dp[N]); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); void main() { auto n = lread(); auto x = new long[](10 ^^ 6 + 10); foreach (i; 1 .. 101) { foreach (j; 1 .. 101) { foreach (k; 1 .. 101) { long tmp = 0; tmp = i ^^ 2 + j ^^ 2 + k ^^ 2 + i * j + j * k + i * k; if (tmp <= 10 ^^ 4) { x[tmp - 1] += 1; } } } } foreach (i; 0 .. n) { writeln(x[i]); } } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; import std.array; int n; rd(n); auto a = readln.split.map!((s) => (s == "T" ? 1 : 0)).array; int f(int x, int y) { return (x - y <= 0); } // auto t = f(a[0], a[1]); foreach (i; 2 .. n) { t = f(t, a[i]); } if (t) { writeln("T"); } else { writeln("F"); } } void rd(T...)(ref T x) { import std.stdio : readln; import std.string : split; import std.conv : to; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { writeln(readln.chomp.to!int.among(7,5,3) ? "YES" : "NO"); }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; const int INF = int.max/3; void main() { int K = readln.chomp.to!int; Vertex[] vs = K.rep!(()=>new Vertex); foreach(i; 0..K) { vs[i].edges ~= Edge(vs[i], vs[(i+ 1)%K], 1); vs[i].edges ~= Edge(vs[i], vs[(i*10)%K], 0); } vs[1].dist = 0; auto list = DList!Vertex(vs[1]); while(!list.empty) { Vertex v = list.front; list.removeFront(); v.visited = true; foreach(e; v.edges) { if (e.end.visited) continue; e.end.dist = e.start.dist + e.dist; if (e.dist == 1) { list.insertBack(e.end); } else { list.insertFront(e.end); } } } (vs[0].dist+1).writeln; } class Vertex { Edge[] edges; int dist = INF; bool visited = false; } struct Edge { Vertex start, end; int dist; } // ---------------------------------------------- void scanln(Args...)(ref Args args) { foreach(i, ref v; args) { "%d".readf(&v); (i==args.length-1 ? "\n" : " ").readf; } // ("%d".repeat(args.length).join(" ") ~ "\n").readf(args); } void times(alias fun)(int n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { auto minElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto minimum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b < minimum) { element = a; minimum = b; } } return element; } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto maximum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b > maximum) { element = a; maximum = b; } } return element; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto N = readln.chomp.to!int; auto as = readln.split.to!(int[]); writeln(as.sum - N); }
D
import std.stdio, std.conv, std.algorithm, std.string, std.container; void main(){ SList!int list; while(true){ auto line = readln.chomp; if(line.length == 0){ break; } auto input = line.to!int; if(input){ list.insertFront(input); }else{ list.front.writeln; list.removeFront; } } }
D
import std.stdio; import std.algorithm; import std.string; import std.functional; import std.array; import std.conv; import std.math; import std.typecons; import std.regex; import std.range; void main(){ int n = readln().chomp().to!int; for(int i=0;i<n;i++){ auto s = readln().chomp().split(':'); int h = s[0][0] == '0' ? to!int(s[0][1]-'0') : to!int(s[0]); int w = s[1][0] == '0' ? to!int(s[1][1]-'0') : to!int(s[1]); real angleh,anglew; h %= 12; angleh = to!real(h) / 12.0 * 360.0 + to!real(w) / 60.0 * 30.0; anglew = to!real(w) / 60.0 * 360.0; real angle = min(abs(angleh-anglew),360-abs(angleh-anglew)); if(angle < 30.0) writeln("alert"); else if(90 <= angle && angle <= 180) writeln("safe"); else writeln("warning"); } }
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(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { long[] high = new long[1000]; high[0] = 1; foreach (e; 1 .. 1000) { high[e] = high[e - 1] + (e + 1); } long a, b; scan(a, b); long dif = abs(a - b); writeln(high[dif - 1] - b); }
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() { auto C = sread()[0]; writeln(cast(char)(C + 1)); }
D
void main() { string s = readln.chomp; int diff = 1000; foreach (i; 2 .. s.length) { int x; foreach_reverse (j; 0 .. 3) { x += (s[i-j] - '0') * 10 ^^ j; } diff = min(diff, abs(x-753)); } diff.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; long calc(long[] a) { long ans = 0; long[long] d; for (int i = 0; i < a.length; i++) { if (i + 1 >= a[i]) { ans += d.get(i + 1 - a[i], 0); } d[i + 1 + a[i]]++; } return ans; } void main() { readint; auto a = reads!long; writeln(calc(a)); } void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int;
D
void main() { auto c = readMatrix!char(3, 3); foreach(i; 0..3) c[i][i].write; writeln; } // =================================== import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto abn = readln.split.to!(long[]); auto A = abn[0]; auto B = abn[1]; auto N = abn[2]; auto x = min(N, B-1); writeln((A*x)/B - A*(x/B)); }
D
void main() { int[] ab = readln.split.to!(int[]); writeln(ab.all!"a % 2 == 1" ? "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
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() { bool[char] set; foreach (c; readln.chomp) { set[c] = true; } foreach (c; 'a' .. 'z') { if (c !in set) { c.writeln; return; } } if ('z' !in set) "z".writeln; else "None".writeln; }
D
import std.stdio; import std.conv; import std.algorithm; import std.string; import std.array; void main() { int n = readln.chomp.to!int; int[] c = new int[](n), s = new int[](n), f = new int[](n); for (int i = 0; i < n - 1; ++i) { auto l = readln.chomp.split.map!(to!int).array; c[i] = l[0]; s[i] = l[1]; f[i] = l[2]; } for (int i = 0; i < n - 1; ++i) { int ans = s[i]; for (int j = i; j < n - 1; ++j) { ans += c[j]; if (j == n - 2) { break; } int wait; if (ans <= s[j + 1]) { ans += (s[j + 1] - ans); } else { if (ans % f[j + 1] != 0) { wait = ((ans + f[j + 1]) / f[j + 1]) * f[j + 1] - ans; ans += wait; } } } writeln(ans); } writeln(0); return; }
D
import std.functional, std.algorithm, std.container, std.typetuple, std.typecons, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv, std.format, std.math; void main() { while(true){ auto ip = readln.split; auto a = ip[0].to!int, b = ip[2].to!int; if(ip[1] == "?") break; ip[1].predSwitch( "+", a + b, "-", a - b, "*", a * b, "/", a / b).writeln; } }
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; void main() { string s = readln.chomp; string t = "CF"; while (!t.empty) { if (s.empty) { writeln("No"); return; } if (t.front == s.front) { t.popFront(); } s.popFront(); } writeln("Yes"); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; void main(){ int n, m; { int[] tmp = readln.chomp.split.map!(to!int).array; n = tmp[0], m = tmp[1]; } bool[] flag = new bool[n]; flag[0] = 1; int[] count = new int[n]; count.fill(1); foreach(i; 0 .. m){ int[] tmp = readln.chomp.split.map!(to!int).array; int x = tmp[0] - 1, y = tmp[1] - 1; count[x] -= 1, count[y] += 1; if(flag[x]) flag[y] = 1; if(count[x] == 0) flag[x] = 0; } int ans = 0; foreach(i; 0 .. n){ if(flag[i]) ans += 1; } ans.writeln; }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.container; import std.datetime; void main() { while (1) { auto x = readln.chomp.split.map!(to!int); if (!x[0]) break; auto area = new char[][](x[0], x[1]); foreach (i; 0..x[0]) { area[i] = readln.chomp.map!(to!char).array; } int cnt; void solve(int i, int j, char type) { area[i][j] = '.'; foreach (a; iota(-1, 2, 2)) { if (i+a >= 0 && i+a < x[0]) { if (area[i+a][j] == type) solve(i+a, j, type); } if (j+a >= 0 && j+a < x[1]) { if (area[i][j+a] == type) solve(i, j+a, type); } } } foreach (i; 0..x[0]) { foreach (j; 0..x[1]) { if (area[i][j] == '.') continue; cnt++; solve(i, j, area[i][j]); } } cnt.writeln; } }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; import std.math; int n, k; rd(n, k); auto a=new long[n]; auto b=new long[n]; foreach(i; 0..n) rd(a[i], b[i]); long mx=0; foreach(pos; 0..32){ long vsum=0; if(k&(1<<pos))foreach(i; 0..n){ if(a[i]&(1<<pos)) continue; bool ok=true; foreach(j; (pos+1)..32){ ok &= (((k&(1<<j)) | (a[i]&(1<<j))) == (k&(1<<j))); } if(ok) vsum+=b[i]; } mx=max(mx, vsum); } long mx2=0; foreach(i; 0..n){ bool ok=true; foreach(j; 0..32){ ok &= (((k&(1<<j)) | (a[i]&(1<<j))) == (k&(1<<j))); } if(ok) mx2+=b[i]; } writeln(max(mx, mx2)); } 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.algorithm, std.conv, std.range, 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!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}} void main() { int n; readV(n); string[] s; readA(n, s); writeln(s.canFind("Y") ? "Four" : "Three"); }
D
void main(){ ulong x = _scan!ulong(); ulong dep = 100; ulong yy = 0; while( 1 ){ if( dep >= x ){ yy.writeln(); return; } yy++; dep += dep/100; } } 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.conv, std.array,std.string; void main() { auto a=readln; int aa=0; if(a[0]=='1')aa++; if(a[1]=='1')aa++; if(a[2]=='1')aa++; writeln(aa); }
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!string; writeln("ABC" ~ N); stdout.flush(); debug readln(); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void 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); int[] l, r; readC(m, l, r); writeln(max(0, r.reduce!min - l.reduce!max + 1)); }
D
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.typecons, std.range, std.container, std.math, std.algorithm.searching, std.functional,std.mathspecial, std.numeric; void main(){ auto nm=readln.split.map!(to!int).array; int[][] abs; foreach(i;0..nm[1])abs~=readln.split.map!(to!int).array; foreach(n;1..nm[0]+1)writeln(abs.count!(ab=>ab[0]==n||ab[1]==n)); }
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() { readln(); auto N = readln.split.to!(ulong[]); string solve() { return N .filter!(x => x % 2 == 0) .filter!(x => x % 5 != 0 && x % 3 != 0) .array .length > 0 ? "DENIED" : "APPROVED"; } solve().writeln; }
D
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array, std.functional, std.container, std.typecons; ulong gcd(ulong a, ulong b) { if(b == 0) return a; return gcd(b, a % b); } ulong lcm(ulong a, ulong b) { return (a * b) / gcd(a, b); } void main() { auto input = readln.split.to!(ulong[]); ulong A = input[0], B = input[1], C = input[2], D = input[3]; ulong f(ulong n) { return (B/n)-((A-1)/n); } ulong num = f(C) + f(D) - f(lcm(C, D)); (B - A + 1 - num).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; enum mod = 1_000_000_007L; void main() { int N; scan(N); auto x = new int[][](N, 0); auto y = new int[][](N, 0); foreach (i ; 0 .. N) { int Ai; scan(Ai); while (Ai--) { int xi, yi; scan(xi, yi); xi--; if (yi) { x[i] ~= xi; } else { y[i] ~= xi; } } } int ans; foreach (s ; 0 .. (1 << N)) { bool ok = true; foreach (j ; 0 .. N) if (s & (1 << j)) { foreach (xk ; x[j]) { if (!(s & (1 << xk))) { ok = false; } } foreach (yk ; y[j]) { if (s & (1 << yk)) { ok = false; } } } if (ok) ans = max(ans, s.popcnt); } 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.array, std.conv, std.algorithm; int bubbleSort(int n, int[] x) { int cnt = 0; bool flag = true; for (int i = 0; flag; ++i) { flag = false; for (int j = n - 1; j >= i + 1; --j) { if (x[j] < x[j-1]) { swap(x[j], x[j-1]); flag = true; ++cnt; } } } return cnt; } void main() { int n = readln.chomp.to!int; int[] a = readln.chomp.split.to!(int[]); int times = bubbleSort(n, a); foreach (i, x; a) { x.write; if (i != n - 1) " ".write; else writeln; } times.writeln; }
D
import std.stdio,std.string,std.algorithm; void main(){ string[] a = readln().chomp().split(); sort(a); writeln( join(a," " )); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; import std.container; void main() { auto si = readln.split; auto st = SList!int(); int a, b; foreach (s; si) { switch(s) { case "+": st.getOperands(a, b); st.insert(b + a); break; case "-": st.getOperands(a, b); st.insert(b - a); break; case "*": st.getOperands(a, b); st.insert(b * a); break; default: st.insert(s.to!int); } } writeln(st.front); } void getOperands(ref SList!int st, ref int a, ref int b) { a = st.front; st.removeFront; b = st.front; st.removeFront; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { int a, b, c; foreach (s; readln.chomp) { switch (s) { case 'a': ++a; break; case 'b': ++b; break; case 'c': ++c; break; default: } } writeln(abs(a-b) < 2 && abs(b-c) < 2 && abs(c-a) < 2 ? "YES" : "NO"); }
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; int calc(int a) { return (a + a + 1 + a + 2 + a + 7 + a + 8 + a + 9 + a + 14 + a + 15 + a + 16) % 11; } void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto K = s[1]; long ans = 0; if (N < 3) { writeln(-1); return; } if (N < 50) { for (int i = 0, a = 1; i < N - 2; ++i, a = (a + 7) % 11) for (int j = a; j < a + 5; ++j) ans += calc(j) == K; ans.writeln; return; } for (int i = 0, a = 1; i < 11; ++i, a = (a + 7) % 11) for (int j = a; j < a + 5; ++j) ans += calc(j) == K; ans *= (N - 2) / 11L; for (int i = 0, a = 1; i < (N - 2) % 11; ++i, a = (a + 7) % 11) for (int j = a; j < a + 5; ++j) ans += calc(j) == K; ans.writeln; }
D
void main() { (rs.count('o')*100+700).writeln; } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
void main() { auto w = readln.chomp.to!(dchar[]).sort!"a < b".group.assocArray; writeln(w.values.all!(t => t % 2 == 0) ? "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
// 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, m; bool[] a; bool[] b; void main() { scan(n, m); a = new bool[](n + 1); b = new bool[](n + 1); int u, v; foreach (i ; 0 .. m) { scan(u, v); if (u == 1) a[v] = true; if (v == n) b[u] = true; } foreach (i ; 1 .. n) { if (a[i] && b[i]) { writeln("POSSIBLE"); return; } } writeln("IMPOSSIBLE"); } 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.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 t = RD!int; auto x = RDA; foreach (i; 0..t) { if (x[i] < 15) writeln("NO"); else { auto y = x[i] % 14; writeln(y <= 6 && y != 0 ? "YES" : "NO"); } } stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD!int; auto a = RD!string; string ans; ans ~= a[0]; long cnt; char last = a[0]; foreach (i; 1..N) { if ((i-cnt) % 2 == 1) { if (a[i] == last) { ++cnt; continue; } } last = a[i]; ans ~= a[i]; } if (ans.length % 2 == 1) { ans.popBack; ++cnt; } writeln(cnt); writeln(ans); 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 N = readln.chomp.to!int; auto as = "abcdefghij"; char[] ss; ss.length = N; void solve(int i, int j) { if (i == N) { writeln(ss); return; } foreach (k; 0..j+2) { ss[i] = as[k]; solve(i+1, max(j, k)); } } solve(0, -1); }
D
import std.stdio, std.string, std.conv, std.array, std.algorithm, std.range, std.math; void main() { auto n = readln.chomp.to!int; auto as = readln.chomp.split(" ").map!(to!int).array; auto av = as.reduce!"a+b" / n.to!double; auto av1 = ceil(av); auto ret1 = as.dup.map!(i => ((i - av1) ^^ 2).to!long).reduce!"a+b"; av1 = floor(av); auto ret2 = as.dup.map!(i => ((i - av1) ^^ 2).to!long).reduce!"a+b"; writeln(ret1 < ret2 ? ret1 : ret2); }
D
void main() { long n = rdElem; long[] a = n.rdCol; if (a[0]) { writeln(-1); return; } long[] x = new long[n]; x[n-1] = a[n-1]; long cnt = x[n-1]; foreach_reverse (i; 0 .. n-1) { if (x[i+1]) x[i] = x[i+1] - 1; if (i < x[i] || x[i] > a[i]) { writeln(-1); return; } else if (x[i] < a[i]) { cnt += a[i]; x[i] = a[i]; } } cnt.writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.array, std.conv, std.typecons, std.algorithm; immutable INF = 1<<30; long impl(long n, long v, const ref long[] ws, const ref long[] vs, ref long[][] dp) { if(dp[n][v] == INF) { const auto yes = { const auto prev_v = v-vs[n-1]; return prev_v >= 0 ? impl(n-1, prev_v, ws, vs, dp) + ws[n-1] : INF; }(); const auto no = impl(n-1, v, ws, vs, dp); dp[n][v] = min(yes, no, dp[n][v]); } return dp[n][v]; } long solve(long n, long w, const ref long[] ws, const ref long[] vs) { const long avg = (vs.sum + vs.count - 1) / vs.count; auto vmax = avg*n; auto dp = new long[][](n+1, vmax+1); foreach(ref v ; dp[]) { v[] = INF;} // clear all foreach(ref v ; dp[0]) { v = INF-1;} // set dp[0][x] dp[0][0] = 0; for(long i = vmax; i >= 0; i--) { auto ret_w = impl(n, i, ws, vs, dp); if (ret_w <= w) return i; } return -1; // unreachable } void main() { immutable i1 = readln.split.to!(long[]); immutable n = i1[0], w = i1[1]; auto ws = new long[n]; auto vs = new long[n]; for(auto i = 0; i < n; i++) { auto input = readln.split.to!(long[]); ws[i] = input[0]; vs[i] = input[1]; } writeln(solve(n, w, ws, vs)); }
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 p, q, r; scan(p, q, r); (p + q).min(p + r).min(q + r).writeln(); }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto n = readln.strip.to !(int); auto a = readln.strip.map !(q{a - '0'}).array; auto b = readln.strip.map !(q{a - '0'}).array; int mex (int lo, int hi) { bool [3] c; foreach (i; lo..hi) { c[a[i]] = true; c[b[i]] = true; } int res = 0; while (c[res]) { res += 1; } return res; } auto f = new int [n + 1]; f[0] = 0; f[1] = mex (0, 1); foreach (i; 2..n + 1) { foreach (j; 1..3) { f[i] = max (f[i], mex (i - j, i) + f[i - j]); } } writeln (f[n]); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container; void main() { auto N = readln.chomp.to!long; long r; foreach (i; 1..N+1) { auto d = N/i; r += d * (d+1) / 2 * i; } writeln(r); }
D
import std.stdio, std.string, std.algorithm, std.array; void main() { int t; scanf("%d", &t); getchar(); foreach(i; 0..t){ auto str = readln.strip(); string result; while (str.length > 0) { result ~= str[0]; str = str.stripLeft(str[0]); } if (result.length > 3) writeln(2); else if (result == "0") writeln(1); else if (result == "1") writeln(0); else if (result == "01" || result == "10") writeln(1); else if (result == "101") writeln(1); else writeln(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)); } double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto n = RD; auto a = RD; auto b = RD; long x = 1; if (a == 1) { auto rem = n - x; ans[ti] = rem % b == 0; } else { while (x <= n) { if ((n-x) % b == 0) { ans[ti] = true; break; } x *= a; } } } foreach (e; ans) writeln(e ? "Yes" : "No"); stdout.flush; debug readln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { int[] S; int n; char last; foreach (c; readln.chomp) { if (last && last == c) { ++n; } else { if (n) S ~= n; last = c; n = 1; } } S ~= n; if (S.length == 1) { writeln(S[0]); return; } while (S.length > 2) { if (S[0] < S[$-1]) { S[1] += S[0]; S = S[1..$]; } else { S[$-2] += S[$-1]; S = S[0..$-1]; } } writeln(max(S[0], S[1])); }
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; immutable inf = 10^^9 + 7; int c, v0, v1, a, l; void main() { scan(c, v0, v1, a, l); int r; int cnt; while (true) { cnt++; r += min(v0 + a * (cnt - 1), v1); if (r >= c) { writeln(cnt); return; } r -= l; } } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.string, std.conv; 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); int ans = (a - 1) + (a <= 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); } } }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { auto tmp = readln.split.to!(long[]); auto K = tmp[0], A = tmp[1], B = tmp[2]; if (B - A <= 2) { writeln(1 + K); } else { // A-1回最初に叩く const tataki = max(0, A-1); K -= tataki; long pt = 1 + tataki; // 残り回数が1以下になるまで2回ずつ消費し、B-Aだけ稼ぐ const urikai = K/2; K -= urikai * 2; pt += (B-A) * urikai; // 残り回数叩く if (K == 1) pt++; writeln(pt); } }
D
module sigod.codeforces.p298C; import std.algorithm; import std.stdio; import std.string; void main() { string a = stdin.readln().strip(); string b = stdin.readln().strip(); size_t a_count = a.count('1'); if (a_count % 2 == 1) ++a_count; size_t b_count = b.count('1'); stdout.writeln(a_count >= b_count ? "YES" : "NO"); }
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; void main() { while (true) { int N = readln.chomp.to!int; if (N == 0) break; long[] k = readln.split.to!(long[]); if (k.filter!(a=>a>1).empty) { writeln("NA"); } else { writeln(k.filter!(a=>a>0).array.length+1); } } }
D
import std.stdio, std.conv, std.array, std.math; void main() { string[] input = readln.split; int m = input[0].to!int; int k = input[1].to!int; int n = pow(2, m); if(n == 1) { if(k == 0) { writeln(0, " ", 0); } else{ writeln(-1); } return; } if(n == 2) { if(k == 0) { writeln(0, " ", 0, " ", 1, " ", 1); } else { writeln(-1); } return; } if(n <= k) { writeln(-1); return; } if(k == 0) { foreach(i; 0..n) { write(i); write(" "); write(i); if(i != n - 1){ write(" "); } } writeln(""); return; } foreach(i; 0..n) { if(k != i) { write(i, " "); } } write(k, " "); foreach(i; 0..n) { int j = n - i - 1; if(k != j) { write(j, " "); } } writeln(k); }
D
void main() { long n, k; rdVals(n, k); long[] x = rdRow; long result = inf; foreach (i; 0 .. n-k+1) { long time = abs(x[i]-x[i+k-1]) + min(x[i].abs, x[i+k-1].abs); result = min(result, time); } 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 rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
import std.stdio, std.conv, std.string; import std.algorithm, std.array, std.container; import std.numeric, std.math; import core.bitop; T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; } string RDR()() { return readln.chomp; } long mod = pow(10, 9) + 7; long moda(long x, long y) { return (x + y) % mod; } long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; } long modm(long x, long y) { return (x * y) % mod; } void main() { auto X = RD!long; auto Y = RD!long; long ans; while (X <= Y / 2) { X *= 2; ++ans; } writeln(ans + 1); stdout.flush(); }
D
import std.algorithm; import std.concurrency; import std.container; import std.conv; import std.functional; import std.math; import std.meta; import std.random; import std.range; import std.stdio; import std.string; import std.traits; import std.typecons; void main() { auto input = readln.chomp.split.map!(to!long); auto r = input.front; input.popFront; auto D = input.front; input.popFront; auto x = input.front; input.popFront; iota(10).each!((_){ x = r * x - D; writeln(x); }); }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { while (true) { int r0, w0, c, r; scan(r0, w0, c, r); if (!r0) return; int ans; while (r0 < w0*c) { r0 += r; ans++; } 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