code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; void main() { auto N = readln.chomp.to!int; auto S = readln.chomp; int i = 0; for (; i < N - 1; ++i) { if (S[i] > S[i + 1]) break; } S = S[0..i] ~ S[i+1..N]; S.writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD!uint; auto s = new char[][](N); foreach (i; 0..N) { s[i] = RD!(char[]); } bool visit(uint y, uint x) { if (s[y+1][x] == '#' || s[y+1][x-1] == '#' || s[y+1][x+1] == '#' || s[y+2][x] == '#') return false; s[y][x] = '#'; s[y+1][x] = '#'; s[y+1][x-1] = '#'; s[y+1][x+1] = '#'; s[y+2][x] = '#'; return true; } bool ans = true; (){ foreach (y; 0..N) { foreach (x; 0..N) { if (s[y][x] == '#') continue; debug writeln(y, ":", x); if (x == 0 || x == N-1) { ans = false; return; } else if (y >= N-2) { ans = false; return; } auto r = visit(y, x); if (!r) { ans = false; return; } } }}(); writeln(ans ? "YES" : "NO"); stdout.flush(); debug readln(); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { long h, w; scan(h, w); auto c = new string[][](h); foreach (i; 0 .. h) { c[i] = aryread!string(); } // writeln(c); foreach (i; 0 .. h) { foreach (_; 0 .. 2) { foreach (j; 0 .. c[i].length) { write(c[i][j]); } writeln(); } } } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); long ans; foreach (i; 1 .. N + 1) { if (i % 3 != 0 && i % 5 != 0) ans += i; } writeln(ans); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto s = readln.split.map!(to!int); auto H = s[0]; auto W = s[1]; auto K = s[2]; foreach (i; 0..H+1) { foreach (j; 0..W+1) { long tmp = i * W + j * H - (i * j) * 2; if (tmp == K) { writeln("Yes"); return; } } } writeln("No"); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n, m; scan(n, m); auto ng = new bool[](n + 1); iota(m).map!(i => readln.chomp.to!int).each!(ai => ng[ai] = 1); auto f = new long[](n + 1); f[0] = 1; if (!ng[1]) f[1] = 1; foreach (i ; 2 .. n + 1) { if (ng[i]) continue; f[i] = (f[i - 1] + f[i - 2]) % mod; } debug { writeln(f); } writeln(f[$-1]); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.string, std.conv; void main(){ for(int i;;++i){ auto a = readln.chomp.to!int; if(!a) break; writeln("Case ",i+1,": ",a); } }
D
import std.stdio; import std.conv; import std.string; import core.bitop; void main() { auto maxCoin500 = readln.chomp.to!uint; auto maxCoin100 = readln.chomp.to!uint; auto maxCoin50 = readln.chomp.to!uint; auto total = readln.chomp.to!uint; auto count = 0U; foreach (c5h;0..maxCoin500 + 1) { if (c5h * 500 > total) break; foreach (c1h;0..maxCoin100 + 1) { if (c5h * 500 + c1h * 100 > total) break; auto requireCoin50 = (total - c5h * 500 - c1h * 100) / 50; if (requireCoin50 <= maxCoin50) { ++count; } } } writeln(count); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; import std.range; import std.math; void main(){ auto n=readln.chomp.to!int; if(n<=999)writeln("ABC"); else writeln("ABD"); }
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 S = readln.chomp; string solve() { return S == "AAA" || S == "BBB" ? "No" : "Yes"; } solve().writeln; }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { long h, w; scan(h, w); if (h == 1 || w == 1) { writeln(1); return; } auto ans = (h*w + 1) / 2; 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.string, std.conv, std.algorithm; void main() { int[] tmp = readln.split.to!(int[]); int a = tmp[0], b = tmp[1], c = tmp[2]; writeln(max(c-a+b, 0)); }
D
import std.array : split; import std.conv : to; import std.stdio; import std.string : strip; void main() { auto temp = split(strip(stdin.readln())); int n = to!int(temp[0]), t = to!int(temp[1]); string start_queue = strip(stdin.readln()); char[] queue = start_queue.dup; foreach (tick; 0 .. t) { int i = 0; while (i < queue.length - 1) { if (queue[i] == 'B' && queue[i + 1] == 'G') { queue[i] = 'G'; queue[i + 1] = 'B'; i += 2; } else { ++i; } } } stdout.write(queue); }
D
import std.stdio; import std.string; import std.algorithm; import std.conv; import std.array; import std.math; import std.range; void main() { while(1) { auto mfr = readln.chomp.split.map!(to!int).array; if(mfr == [-1, -1, -1]) return; if(mfr[0 .. 2].count(-1)) writeln("F"); else{ int smf = reduce!"a+b"(0, mfr[0 .. 2]); if(smf >= 80) writeln("A"); else if(smf >= 65 && smf < 80) writeln("B"); else if(smf >= 50 && smf < 65) writeln("C"); else if(smf >= 30 && smf < 50) writeln(mfr[2] >= 50 ? "C" : "D"); else writeln("F"); } } }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { auto x = lread(); if (x == 1) { writeln(0); } else { writeln(1); } } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import 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() { long n, m; scan(n, m); writeln((n * (n - 1)) / 2 + (m * (m - 1)) / 2); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
// 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() { auto S = sread(); writeln(S[1] == 'B' ? "ARC" : "ABC"); }
D
import std.stdio, std.string, std.algorithm, std.conv, std.range; void main() { int n = readln.chomp.to!int; string minWord = readln.chomp; for (int i = 0; i < n - 1; i++) { string comp = readln.chomp; if (comp < minWord) { minWord = comp; } } minWord.writeln; }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; // import dcomp.modint; int main() { auto sc = new Scanner(stdin); int n, p; int[] a; sc.read(n, p, a); long[2] dp = [1, 0]; foreach (d; a) { d %= 2; if (d == 0) { dp[0] *= 2; dp[1] *= 2; } else { long sm = dp[0]+dp[1]; dp[0] = dp[1] = sm; } } writeln(dp[p]); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/modint.d */ // module dcomp.modint; // import dcomp.numeric.primitive; struct ModInt(uint MD) if (MD < int.max) { import std.conv : to; uint v; this(int v) {this(long(v));} this(long v) {this.v = (v%MD+MD)%MD;} static auto normS(uint x) {return (x<MD)?x:x-MD;} static auto make(uint x) {ModInt m; m.v = x; return m;} auto opBinary(string op:"+")(ModInt r) const {return make(normS(v+r.v));} auto opBinary(string op:"-")(ModInt r) const {return make(normS(v+MD-r.v));} auto opBinary(string op:"*")(ModInt r) const {return make((long(v)*r.v%MD).to!uint);} auto opBinary(string op:"/")(ModInt r) const {return this*inv(r);} auto opOpAssign(string op)(ModInt r) {return mixin ("this=this"~op~"r");} static ModInt inv(ModInt x) {return ModInt(extGcd!int(x.v, MD)[0]);} string toString() {return v.to!string;} } struct DModInt(string name) { import std.conv : to; static uint MD; uint v; this(int v) {this(long(v));} this(long v) {this.v = ((v%MD+MD)%MD).to!uint;} static auto normS(uint x) {return (x<MD)?x:x-MD;} static auto make(uint x) {DModInt m; m.MD = MD; m.v = x; return m;} auto opBinary(string op:"+")(DModInt r) const {return make(normS(v+r.v));} auto opBinary(string op:"-")(DModInt r) const {return make(normS(v+MD-r.v));} auto opBinary(string op:"*")(DModInt r) const {return make((long(v)*r.v%MD).to!uint);} auto opBinary(string op:"/")(DModInt r) const {return this*inv(r);} auto opOpAssign(string op)(DModInt r) {return mixin ("this=this"~op~"r");} static DModInt inv(DModInt x) { return DModInt(extGcd!int(x.v, MD)[0]); } string toString() {return v.to!string;} } template isModInt(T) { const isModInt = is(T : ModInt!MD, uint MD) || is(S : DModInt!S, string s); } T[] factTable(T)(size_t length) if (isModInt!T) { import std.range : take, recurrence; import std.array : array; return T(1).recurrence!((a, n) => a[n-1]*T(n)).take(length).array; } T[] invFactTable(T)(size_t length) if (isModInt!T) { import std.algorithm : map, reduce; import std.range : take, recurrence, iota; import std.array : array; auto res = new T[length]; res[$-1] = T(1) / iota(1, length).map!T.reduce!"a*b"; foreach_reverse (i, v; res[0..$-1]) { res[i] = res[i+1] * T(i+1); } return res; } T[] invTable(T)(size_t length) if (isModInt!T) { auto f = factTable!T(length); auto invf = invFactTable!T(length); auto res = new T[length]; foreach (i; 1..length) { res[i] = invf[i] * f[i-1]; } return res; } /* IMPORT /home/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; } } /* IMPORT /home/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 /home/yosupo/Program/dcomp/source/dcomp/numeric/primitive.d */ // module dcomp.numeric.primitive; import std.traits; import std.bigint; T pow(T, U)(T x, U n) if (!isFloatingPoint!T && (isIntegral!U || is(U == BigInt))) { return pow(x, n, T(1)); } T pow(T, U)(T x, U n, T e) if (isIntegral!U || is(U == BigInt)) { while (n) { if (n & 1) e *= x; x *= x; n /= 2; } return e; } T lcm(T)(in T a, in T b) { import std.numeric : gcd; return a / gcd(a,b) * b; } T[3] extGcd(T)(in T a, in T b) if (!isIntegral!T || isSigned!T) { if (b==0) { return [T(1), T(0), a]; } else { auto e = extGcd(b, a%b); return [e[1], e[0]-a/b*e[1], e[2]]; } }
D
import std.stdio; import std.algorithm; import std.string; import std.range; import std.array; import std.conv; import std.complex; import std.math; import std.ascii; import std.bigint; import std.container; void main() { int n = to!int(readln().strip()); int[][string] d = ["N": [0,1], "E": [1,0], "S": [0,-1], "W": [-1, 0]]; while(n) { int[][] p; foreach(i; 0..n) { p ~= array(map!(to!int)(readln().strip().split())); } int m = to!int(readln().strip()); bool[21][21] q; int x = 10; int y = 10; foreach(i; 0..m) { auto s = readln().strip().split(); auto t = to!int(s[1]); foreach(j; 0..t) { x += d[s[0]][0]; y += d[s[0]][1]; q[x][y] = true; } } bool f = true; foreach(pi; p){ f &= q[pi[0]][pi[1]]; } writeln(f?"Yes":"No"); n = to!int(readln().strip()); } }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { const N = readln.chomp.to!long; auto Hs = readln.split.to!(long[]); foreach_reverse(i; 0..N-1) { if (Hs[i] > Hs[i+1]+1) { writeln("No"); return; } else if (Hs[i] == Hs[i+1]+1) { Hs[i]--; } } writeln("Yes"); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!int).array; int cnt = 1; foreach (a; A) { if (cnt == a) { cnt += 1; } } int ans = N - cnt + 1; writeln(cnt == 1 ? -1 : ans); }
D
import std.stdio, std.string, std.conv, std.math; void main() { readln.chomp.to!int.pow(3).writeln; }
D
import std.stdio; import std.array; import std.conv; void main(){ string[] d=split(readln()); if(to!int(d[0])<=to!int(d[2])&&to!int(d[2])<=to!int(d[1])){ writeln("Yes"); }else{ writeln("No"); } }
D
import std.stdio; import std.string; import std.conv; int main() { string s; while((s = readln()).length != 0) { int count = 0; int number = to!(int)(chomp(s)); foreach(int i;0..10) { foreach(int j;0..10) { foreach(int k;0..10) { foreach(int l;0..10) { if(i+k+j+l == number) count++; } } } } writeln(count); } return 0; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto S = readln.chomp.to!(wchar[]); int i; long r; foreach (j, s; S) { if (s == 'W') { r += j - i; ++i; } } writeln(r); }
D
import std.stdio, std.string, std.range, std.algorithm, std.math, std.typecons, std.conv; void main() { auto a = readln.split.to!(int[]); auto A = a[0], B = a[1], C = a[2]; foreach (i; 0.. B) { if (A * (i+1) % B == C) { writeln("YES"); return; } } writeln("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; import std.container; enum MAX = 1_000_100; ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias INCOME = Tuple!(long, "after", long, "salary"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long n, m; scan(n, m); auto a = new string[](n); auto b = new string[](m); foreach (ref e; a) e = sread(); foreach (ref e; b) e = sread(); bool flag; foreach (i; iota(n - m + 1)) { foreach (j; iota(n - m + 1)) { bool check = true; foreach (col; iota(m)) { foreach (row; iota(m)) { if (a[i + col][j + row] != b[col][row]) check = false; } } flag = flag || check; } } if (flag) writeln("Yes"); else writeln("No"); } 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; } }
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, core.bitop; // dfmt on void main() { auto S = sread(); repeat('x').take(S.length).array.writeln(); }
D
import std.stdio; import std.string; void main() { char[] line = cast(char[]) readln.chomp; for(int i = 0; i < line.length - 4; i++) { char[] str = line[i..(i + 5)]; if (str == "apple") line[i..(i + 5)] = cast(char[])"peach"; else if (str == "peach") line[i..(i + 5)] = cast(char[])"apple"; } writeln(line); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { auto s = readln.chomp; char[] cs; for (int i = 0; i < s.length; i += 2) cs ~= s[i]; writeln(cs); }
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; } } // }}} // ModInt {{{ struct ModInt(ulong modulus) { import std.traits : isIntegral, isBoolean; import std.exception : enforce; private { ulong val; } this(const ulong n) { val = n % modulus; } this(const ModInt n) { val = n.value; } @property { ref inout(ulong) value() inout { return val; } } T opCast(T)() { static if (isIntegral!T) { return cast(T)(val); } else if (isBoolean!T) { return val != 0; } else { enforce(false, "cannot cast from " ~ this.stringof ~ " to " ~ T.stringof ~ "."); } } ModInt opAssign(const ulong n) { val = n % modulus; return this; } ModInt opOpAssign(string op)(ModInt rhs) { static if (op == "+") { val += rhs.value; if (val >= modulus) { val -= modulus; } } else if (op == "-") { if (val < rhs.value) { val += modulus; } val -= rhs.value; } else if (op == "*") { val = val * rhs.value % modulus; } else if (op == "/") { this *= rhs.inv; } else if (op == "^^") { ModInt res = 1; ModInt t = this; while (rhs.value > 0) { if (rhs.value % 2 != 0) { res *= t; } t *= t; rhs /= 2; } this = res; } else { enforce(false, op ~ "= is not implemented."); } return this; } ModInt opOpAssign(string op)(ulong rhs) { static if (op == "+") { val += rhs; if (val >= modulus) { val -= modulus; } } else if (op == "-") { if (val < rhs) { val += modulus; } val -= rhs; } else if (op == "*") { val = val * rhs % modulus; } else if (op == "/") { this *= ModInt(rhs).inv; } else if (op == "^^") { ModInt res = 1; ModInt t = this; while (rhs > 0) { if (rhs % 2 != 0) { res *= t; } t *= t; rhs /= 2; } this = res; } else { enforce(false, op ~ "= is not implemented."); } return this; } ModInt opUnary(string op)() { static if (op == "++") { this += 1; } else if (op == "--") { this -= 1; } else { enforce(false, op ~ " is not implemented."); } return this; } ModInt opBinary(string op)(const ulong rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinary(string op)(ref const ModInt rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinary(string op)(const ModInt rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinaryRight(string op)(const ulong lhs) const { mixin("return ModInt(this) " ~ op ~ "= lhs;"); } long opCmp(ref const ModInt rhs) const { return cast(long)value - cast(long)rhs.value; } bool opEquals(const ulong rhs) const { return value == ModInt(rhs).value; } ModInt inv() const { ModInt ret = this; ret ^^= modulus - 2; return ret; } string toString() const { import std.format : format; return format("%s", val); } } // }}} // 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 n; cin.scan(n); int[] ar = cin.nextArray!int(n - 1); int[] sum = new int[n]; foreach (e; ar) { sum[e - 1]++; } foreach (i; 0 .. n) { writeln(sum[i]); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto a = RD; writeln(a + a^^2 + a^^3); stdout.flush; debug readln; }
D
import std.stdio, std.string, std.conv; void main() { auto input = getStdin!(string[]); foreach (line; input) { auto num = line.split(" "); int w = num[0].to!(int); int h = num[1].to!(int); if (w == 0 && h == 0) { break; } for (int i_w = 0; i_w < w; i_w++) { for (int j_h = 0; j_h < h; j_h++) { "#".write; } "".writeln; } "".writeln; } } T getStdin(T)() { string[] cmd; string line; while ((line = chomp(stdin.readln())) != "") cmd ~= line; return to!(T)(cmd); }
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; int[] dist, prev; void main() { int N; scan(N); foreach (x ; 1 .. 10) { if (N % x == 0 && N / x < 10) { writeln("Yes"); return; } } writeln("No"); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.string, std.conv; import std.algorithm, std.array, std.range; auto solve(string S_) { immutable N=S_.chomp.to!int(); immutable A=readln.chomp(); immutable B=readln.chomp(); immutable C=readln.chomp(); int k=0; foreach(i;0..N) { if(A[i]!=B[i]) { ++k; if(A[i]!=C[i] && B[i]!=C[i]) ++k; } else if(B[i]!=C[i]) { ++k; } } return k; } void main(){ for(string s; (s=readln.chomp()).length;) writeln(solve(s)); }
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[] a, b; readC(n, a, b); int[] c, d; readC(m, c, d); foreach (i; 0..n) { auto x = 0, yi = int.max; foreach (j; 0..m) { auto y = (a[i]-c[j]).abs + (b[i]-d[j]).abs; if (y < yi) { yi = y; x = j; } } writeln(x+1); } }
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(); ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; ulong MIDDLE = 100_000; alias Pair = Tuple!(long, "flag", long, "num"); 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() { auto n = lread(); auto prime = new long[](n); long len; foreach (i; iota(2, n + 1)) { long flag = 1; foreach (j; iota(2, i)) flag = flag && i % j; if (flag) prime[len++] = i; } auto prime_fact = new long[](n + 1); foreach (i; iota(1, n + 1)) { foreach (j; iota(len)) { while (i > 0 && !(i % prime[j])) { i /= prime[j]; prime_fact[prime[j]]++; } } } long ans = 1; foreach (e; prime_fact) { if (e) { ans *= e + 1; ans %= MOD; } } ans.writeln(); }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; immutable long mod = 998_244_353; immutable int limit = 100_005; long powMod (long a, int p) { long res = 1; for ( ; p != 0; p >>= 1) { if (p & 1) { res = (res * 1L * a) % mod; } a = (a * 1L * a) % mod; } return res; } alias invMod = a => powMod (a, mod - 2); auto fact = new long [limit]; long choose (int n, int k) { auto res = fact[n]; res = (res * 1L * invMod (fact[n - k])) % mod; res = (res * 1L * invMod (fact[k])) % mod; return res; } void main () { fact[0] = 1; foreach (i; 1..limit) { fact[i] = (fact[i - 1] * 1L * i) % mod; } auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto n = readln.strip.to !(int); auto a = readln.strip.map !(q{a == '1'}).array; int zeroes = 0; int pairs = 0; for (int i = 0; i < n; i++) { if (!a[i]) { zeroes += 1; } else if (i + 1 < n && a[i + 1]) { pairs += 1; i += 1; } } writeln (choose (zeroes + pairs, pairs)); } }
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; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long A, B, C; scan(A, B, C); writeln((A + B + C < 22) ? "win" : "bust"); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { int a, b; scan(a, b); writeln((a + b + 1) / 2); } 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; import std.ascii; import std.conv; import std.string; import std.algorithm; import std.range; import std.functional; import std.math; import core.bitop; void main() { string s = readln.chomp; long k = readln.chomp.to!long; long firstone = -1; foreach (size_t i, char c; s) if (c != '1') { firstone = i; break; } // s[0..firstone].writeln; if (k <= firstone) { "1".writeln; return; } if (firstone == -1) { s[0].writeln; return; } s[firstone].writeln; }
D
import std.stdio,std.string,std.conv,std.algorithm; import std.algorithm:rev=reverse; void main(){ auto input=readln.chomp.split.to!(ulong[]); auto n=input[0]; auto a=input[1]; auto b=input[2]; auto ans=n/(a+b)*a; ans+=min(a,n%(a+b)); ans.writeln; }
D
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math, std.typecons; import core.stdc.stdio; // foreach, foreach_reverse, writeln alias Tuple!(int, int) Pair; void main() { int n; scanf("%d", &n); int[][] graph = new int[][n]; foreach (i; 0..n-1) { int v, w; scanf("%d%d", &v, &w); v--; w--; graph[v] ~= w; graph[w] ~= v; } Pair dfs(int v, int depth, int parent) { Pair ret = Pair(depth, v); foreach (u; graph[v]) { if (u == parent) continue; ret = max(ret, dfs(u, depth+1, v)); } return ret; } int s = dfs(0,0,-1)[1]; int t = dfs(s,0,-1)[1]; int[] path; int[] used = new int[n]; bool getpath(int v, int dist, int parent) { if (v == dist) { path ~= v; used[v] = true; return true; } foreach (u; graph[v]) { if (u == parent) continue; if (getpath(u, dist, v)) { path ~= v; used[v] = true; return true; } } return false; } getpath(s, t, -1); int[] generate() { int[] ret; foreach (v; path) { int cnt = 0; foreach (u; graph[v]) { if (used[u]) continue; cnt++; } int id = to!int(ret.length)+1; foreach (i; 0..cnt) { ret ~= id+1+i; } ret ~= id; } return ret; } int[] ans = generate(); path.reverse(); int[] ant = generate(); if (ans.length < n) { writeln(-1); } else { ans = min(ans, ant); foreach (i; ans) writeln(i); } }
D
import std.stdio; import std.conv; import std.string; import std.algorithm; import std.range; void main() { auto input = readln.split.to!(int[]); auto X = input[0]; auto Y = input[1]; auto Z = input[2]; auto ans = X / (Y + Z); (ans * (Y + Z) + Z > X ? ans - 1 : ans ).writeln; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } enum MOD = (10 ^^ 9) + 7; void main() { auto S = sread(); long i = S.length; loop_w: while (0 < i) { foreach (t; ["dream", "dreamer", "erase", "eraser"]) { auto s = S[max(0, i - t.length) .. i]; if (s == t) { i -= s.length; continue loop_w; } } writeln("NO"); return; } writeln("YES"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; long[3] DR = [1, 100, 10000]; void main() { auto dn = readln.split.to!(long[]); auto D = dn[0]; auto N = dn[1]; if (N == 100) ++N; writeln(N * DR[D]); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { auto L = readln.chomp.map!(c => c == '1').array; auto N = L.length.to!int; auto dp = new long[][](N + 1, 2); dp[0][0] = 1; foreach (i ; 0 .. N) { if (L[i]) { dp[i + 1][0] = dp[i][0] * 2 % mod; dp[i + 1][1] = (dp[i][0] + dp[i][1] * 3 % mod) % mod; } else { dp[i + 1][0] = dp[i][0]; dp[i + 1][1] = dp[i][1] * 3 % mod; } } auto ans = (dp[N][0] + dp[N][1]) % 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 std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons, std.datetime; void main() { const S = readln.chomp; auto d = Date.fromISOString(S.replace("/", "")); if (d <= Date(2019, 4, 30)) { writeln("Heisei"); } else { writeln("TBD"); } }
D
import std.stdio; import std.conv; import std.array; import std.algorithm; import std.range; import std.string; alias println = writeln; alias print = write; int readint() { return readln.chomp.to!int; } void main() { if (readint() == 1) { println("Hello World"); } else { println(readint() + readint()); } }
D
import std.stdio; import std.conv; import std.algorithm; import std.range; import std.string; void main() { while (true) { auto input = readln.chomp.split.map!(to!int); if (input.array == [0, 0]) break; int h = input[0]; int w = input[1]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (i == 0 || i == h - 1) { write("#"); } else { if (j == 0 || j == w - 1) { write("#"); } else { write("."); } } } writeln; } writeln; } }
D
//prewritten code: https://github.com/antma/algo import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.traits; final class InputReader { private: ubyte[] p, buffer; bool eof; bool rawRead () { if (eof) { return false; } p = stdin.rawRead (buffer); if (p.empty) { eof = true; return false; } return true; } ubyte nextByte(bool check) () { static if (check) { if (p.empty) { if (!rawRead ()) { return 0; } } } auto r = p.front; p.popFront (); return r; } public: this () { buffer = uninitializedArray!(ubyte[])(16<<20); } bool seekByte (in ubyte lo) { while (true) { p = p.find! (c => c >= lo); if (!p.empty) { return false; } if (!rawRead ()) { return true; } } } template next(T) if (isSigned!T) { T next () { if (seekByte (45)) { return 0; } T res; ubyte b = nextByte!false (); if (b == 45) { while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 - (b - 48); } } else { res = b - 48; while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 + (b - 48); } } } } template next(T) if (isUnsigned!T) { T next () { if (seekByte (48)) { return 0; } T res = nextByte!false () - 48; while (true) { ubyte b = nextByte!true (); if (b < 48 || b >= 58) { break; } res = res * 10 + (b - 48); } return res; } } T[] nextA(T) (in int n) { auto a = uninitializedArray!(T[]) (n); foreach (i; 0 .. n) { a[i] = next!T; } return a; } } void main() { auto r = new InputReader (); immutable nt = r.next!uint (); foreach (tid; 0 .. nt) { int x = r.next!int; int y = r.next!int; const long a = r.next!int; const long b = r.next!int; long res = (x + y) * a; if (x > y) swap (x, y); res = min (res, b * x + (y - x) * a); writeln (res); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; writeln((N+1) * N / 2); 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.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void main() { auto H = RD; auto W = RD; auto N = RD; writeln(N / max(H, W) + (N % max(H, W) != 0 ? 1 : 0)); stdout.flush; debug readln; }
D
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; string s; void main() { scan(s); writeln(s[0] ~ (s.length.to!int - 2).to!string ~ s[$-1]); } 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.string; void main() { auto ip = readln.split.to!(int[]); if(ip[0] + ip[1] <= ip[1] + ip[2] && ip[0] + ip[1] <= ip[2] + ip[0]){ writeln(ip[0] + ip[1]); } else if(ip[1] + ip[2] <= ip[0] + ip[1] && ip[1] + ip[2] <= ip[2] + ip[0]){ writeln(ip[1] + ip[2]); } else { writeln(ip[2] + ip[0]); } }
D
void main() { auto N = ri; if(N < 1200) { writeln("ABC"); } else if(N < 2800) { writeln("ARC"); } else writeln("AGC"); } // =================================== import std.stdio; import std.string; import std.functional; 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
import std.stdio; void main(){ string s; int[2] score; while ((s = readln()) != "0\n") { score = 0; foreach (ch; s[1..$-1]) score[ch - 'A']++; int t = score[0] > score[1] ? 0 : 1; score[t]++; writeln(score[0], " ", score[1]); } }
D
import std.stdio; import std.conv; import std.string; import std.algorithm; import std.range; import std.functional; import std.math; import core.bitop; void main() { /// ABC089_D string s = readln.chomp; writeln((s.canFind('a') && s.canFind('b') && s.canFind('c')) ? "Yes" : "No"); }
D
void main(){ int k = _scan(); writeln((k/2)*((k+1)/2)); } 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
// 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, A, B; scan(N, A, B); if (B < A) { writeln(0); return; } if (N == 1 && A != B) { writeln(0); return; } long m = A * (N - 1) + B; long M = A + B * (N - 1); writeln(M - m + 1); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; void main() { auto s = readln.splitter; auto a = s.front; s.popFront(); auto b = s.front; writeln(b, a); }
D
// import chie template :) {{{ import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons; // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string : chomp; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.chomp.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } } // }}} void main() { auto cin = new Scanner; int n; cin.scan(n); int[] ar = new int[n + 2]; foreach (i; 0 .. n) { cin.scan(ar[i + 1]); } int sum; foreach (i; 0 .. n + 1) { sum += abs(ar[i + 1] - ar[i]); } foreach (i; 1 .. n + 1) { writeln(sum - abs(ar[i] - ar[i - 1]) - abs(ar[i + 1] - ar[i]) + abs(ar[i + 1] - ar[i - 1])); } }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; void times(alias fun)(int n) { foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } void main() { int N = readln.chomp.to!int; int[] as = N.rep!(() => readln.chomp.to!int).map!"a-1".array; int i = 0; foreach(k; 0..N) { i = as[i]; if (i == 1) { (k+1).writeln; return; } } (-1).writeln; } // ---------------------------------------------- // 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.string, std.conv, std.range, std.algorithm; void main() { auto a = readln.chomp.to!int; auto b = readln.chomp.to!int; auto h = readln.chomp.to!int; ((a + b) * h / 2).writeln; }
D
import std.stdio, std.range, std.conv, std.string, std.array, std.functional, std.math; import std.algorithm.comparison, std.algorithm.iteration, std.algorithm.mutation, std.algorithm.searching, std.algorithm.setops, std.algorithm.sorting; import std.container.binaryheap; void main() { auto N = readln().strip.to!(long); long[4][4][4][] memo; memo.length = N+1; memo[0][3][3][3] = 1; foreach(i; 0..N) { foreach(c1; 0..4) { foreach(c2; 0..4) { foreach(c3; 0..4) { foreach(a; 0..4) { if(a==2&&c1==1&&c2==0)continue; if(a==2&&c1==0&&c2==1)continue; if(a==1&&c1==2&&c2==0)continue; if(a==2&&c1==1&&c3==0)continue; if(a==2&&c2==1&&c3==0)continue; memo[i+1][a][c1][c2] += memo[i][c1][c2][c3]; memo[i+1][a][c1][c2] %= pow(10, 9) + 7; } } } } } long res; foreach(c1; 0..4) { foreach(c2; 0..4) { foreach(c3; 0..4) { res += memo[N][c1][c2][c3]; res %= pow(10, 9) + 7; } } } writeln(res); }
D
// import chie template :) {{{ import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons; // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string : chomp; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.chomp.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } } // }}} void main() { auto cin = new Scanner; int n; cin.scan(n); auto a = cin.nextArray!int(n); int cnt; foreach (i; 1 .. n) { if (a[i - 1] == a[i]) { a[i] = -1; cnt++; } } writeln(cnt); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto ii = readln.split.to!(long[]); auto A = ii[0]; auto B = ii[1]; auto C = ii[2]; auto X = ii[3]; auto Y = ii[4]; writeln(min( A*X + B*Y, C*X*2 + max(0, (Y-X)*B), C*Y*2 + max(0, (X-Y)*A) )); }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; bool solve (string s) { if (s.length % 2 != 0) { return false; } if (s.front == ')' || s.back == '(') { return false; } return true; } void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto s = readln.strip; writeln (solve (s) ? "Yes" : "No"); } }
D
import std.stdio, std.conv, std.string, std.math; void main(){ auto ip = readln.chomp.to!int; writeln(800 * ip - 200 * (ip / 15)); }
D
// Cheese-Cracker: cheese-cracker.github.io void theCode(){ int k = scan!int; int j = 0; for(int i = 1; i <= 3*k; ++i){ if(i % 3 == 0 || i % 10 == 3) continue; ++j; if(j == k){ writeln(i); } } } void main(){ long tests = scan; // Toggle! while(tests--) theCode(); } /********* That's All Folks! *********/ import std.stdio, std.random, std.functional, std.container, std.algorithm; import std.numeric, std.range, std.typecons, std.string, std.math, std.conv; string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;} T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; } void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } } alias ll = long, tup = Tuple!(long, "x", long, "y");
D
// cheese-cracker [2022-01-31] void solve(){ long n = scan; long ten = (n/10) % 10; long d = n % 7; long u = 7 - d; long down = n - d; long up = n + u; long tend = (down / 10) % 10; long tenu = (up / 10) % 10; if(tend == ten){ writeln(down); }else{ writeln(up); } } void main(){ long tests = scan; // Toggle! while(tests--) solve; } /*_________________________*That's All Folks!*__________________________*/ import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math, std.numeric; string[] tk; alias tup = Tuple!(long, long); T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;} T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; } void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
D
import core.stdc.stdio; import std.algorithm; static immutable int unSet=-114514; struct Data{ int a,l,r,m; } Data Comb(const ref Data a,const ref Data b){ Data r; r.a=a.a+b.a; r.l=max(a.l,a.a+b.l); r.r=max(a.r+b.a,b.r); r.m=max(a.m,b.m,a.r+b.l); return r; } struct Node{ Node* l,r,p; int delay=unSet,size,v; Data d; bool rev; int State(){ if(p !is null){ if(p.l==&this) return -1; if(p.r==&this) return 1; } return 0; } void Propagate(){ if(rev){ if(l !is null) l.rev=!l.rev; if(r !is null) r.rev=!r.rev; swap(l,r); swap(d.l,d.r); rev=false; } if(delay!=unSet){ if(l !is null) l.delay=delay; if(r !is null) r.delay=delay; int s=delay*(delay<0?1:size); d=Data(delay*size,s,s,s); v=delay; delay=unSet; } } void Prepare(){ if(State) p.Prepare; Propagate; } void Update(){ Propagate; d=Data(v,v,v,v); size=1; if(l !is null){ l.Propagate; d=Comb(l.d,d); size+=l.size; } if(r !is null){ r.Propagate; d=Comb(d,r.d); size+=r.size; } } void Rotate(){ Node* par=p,ch; if(p.l==&this){ ch=r; r=p; r.l=ch; }else{ ch=l; l=p; l.r=ch; } if(ch) ch.p=p; p=par.p; par.p=&this; if(p !is null){ if(p.l==par) p.l=&this; if(p.r==par) p.r=&this; } par.Update; Update; } void Splay(){ Prepare; while(State){ int s=State*p.State; if(!s) Rotate; else if(s==1){ p.Rotate; Rotate; }else{ Rotate; Rotate; } } } void Expose(){ Node* x=&this; while(x){ x.Splay; x=x.p; } x=&this; while(x.p){ x.p.r=x; x=x.p; x.Update; } Splay; } void Evert(){ Expose; r=null; rev=!rev; Update; } void Link(Node* t){ Evert; t.Expose; p=t; t.r=&this; t.Update; } void Set(int x){ Expose; r=null; delay=x; Update; } int Get(){ Expose; r=null; Update; return d.m; } } void main(){ int n,q; scanf("%d%d",&n,&q); Node[] ns=new Node[n]; foreach(ref nd;ns){ int w; scanf("%d",&w); nd.v=w; nd.d=Data(w,w,w,w); } foreach(i;0..n-1){ int s,e; scanf("%d%d",&s,&e); ns[--s].Link(&ns[--e]); } foreach(_;0..q){ int t,a,b,c; scanf("%d%d%d%d",&t,&a,&b,&c); if(t==1){ ns[--a].Evert; ns[--b].Set(c); }else{ ns[--a].Evert; printf("%d\n",ns[--b].Get); } } }
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() { long a, b; scan(a, b); auto d = gcd(a, b); long[] divs; for (long i = 2; i * i < d; i++) { if (d % i == 0) { divs ~= i; while (d % i == 0) d /= i; } } if (d > 1) divs ~= d; auto ans = divs.length.to!int + 1; writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio; import std.string; import std.conv; import std.math; int main() { string buf = strip(readln()); while ((buf = strip(readln())) != null) { writeln(is_right_triangle(buf)); } return 0; } string is_right_triangle(string data_set) { string[] sides = split(data_set); int a = to!(uint)(sides[0]); int b = to!(uint)(sides[1]); int c = to!(uint)(sides[2]); int max = 0; int s1, s2; if (a > b) { if (a > c) { max = a; s1 = b; s2 = c; } else { max = c; s1 = a; s2 = b; } } else { if (b > c) { max = b; s1 = a; s2 = c; } else { max = c; s1 = b; s2 = a; } } if (a < b + c && b < a + c && c < a + b) { if (pow(max, 2) == pow(s1, 2) + pow(s2, 2)) { return "YES"; } } return "NO"; }
D
void main() { auto S = readln.to!(char[]); S[3] = '8'; 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
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void main() { auto K = readln.chomp.to!long; long r = 1, k; foreach (_; 0..10^^7+1) { k = (k*10+7) % K; if (k == 0) { writeln(r); return; } ++r; } writeln(-1); }
D
import std.stdio, std.string, std.conv; void main() { auto ip = readln.split, A = ip[0], B = ip[1], C = ip[2]; if(A == B){ writeln(C); } if(B == C){ writeln(A); } if(C == A){ writeln(B); } }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); void main() { string s; scan(s); // writeln(s); foreach (i; 2 .. s.length) { // writeln(s[0 .. $ - i]); auto ss = s[0 .. $ - i]; if (func(ss)) { writeln(ss.length); return; } } // writeln(s[0 .. (s.length / 2)]); // writeln(s[(s.length / 2) .. s.length]); } auto func(string s) { if (s[0 .. (s.length / 2)] == s[(s.length / 2) .. s.length]) { return true; } else { return false; } } 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.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional, std.traits; import std.algorithm, std.container; import core.stdc.stdlib; void main() { auto N = scanElem; auto K = scanElem; char[] S = readln.strip.to!(char[]); for(int i=0;i <N;i++) { if(K-1==i){ if(S[i]=='A') write('a'); if(S[i]=='B') write('b'); if(S[i]=='C') write('c'); }else write(S[i]); } writeln(""); } long gcd(long a, long b) { if(b == 0) return a; return gcd(b, a % b); } class UnionFind{ UnionFind parent = null; void merge(UnionFind a) { if(same(a)) return; a.root.parent = this.root; } UnionFind root() { if(parent is null)return this; return parent = parent.root; } bool same(UnionFind a) { return this.root == a.root; } } void scanValues(TList...)(ref TList list) { auto lit = readln.splitter; foreach (ref e; list) { e = lit.fornt.to!(typeof(e)); lit.popFront; } } T[] scanArray(T = long)() { return readln.split.to!(T[]); } void scanStructs(T)(ref T[] t, size_t n) { t.length = n; foreach (ref e; t) { auto line = readln.split; foreach (i, ref v; e.tupleof) { v = line[i].to!(typeof(v)); } } } long scanULong(){ long x; while(true){ const c = getchar; if(c<'0'||c>'9'){ break; } x = x*10+c-'0'; } return x; } T scanElem(T = long)() { char[] res; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { res ~= cast(char) c; c = getchar; } return res.strip.to!T; } template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } 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); } } struct Factor { long n; long c; } Factor[] factors(long n) { Factor[] res; for (long i = 2; i ^^ 2 <= n; i++) { if (n % i != 0) continue; int c; while (n % i == 0) { n = n / i; c++; } res ~= Factor(i, c); } if (n != 1) res ~= Factor(n, 1); return res; } long[] primes(long n) { if(n<2)return []; auto table = new long[n+1]; long[] res; for(int i = 2;i<=n;i++) { if(table[i]==-1) continue; for(int a = i;a<table.length;a+=i) { table[a] = -1; } res ~= i; } return res; } bool isPrime(long n) { if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (long i = 3; i ^^ 2 <= n; i += 2) if (n % i == 0) return false; return true; }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { const tmp = readln.split.to!(long[]); const N = tmp[0], K = tmp[1]; writeln(2 * K - 1 <= N ? "YES" : "NO"); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.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; auto B = RD; writeln(abs(A-B) % 2 == 0 ? "Alice" : "Borys"); stdout.flush(); debug readln(); }
D
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; string n; void main() { int n; scan(n); if (n / 100 == n % 10) { writeln("Yes"); } else { writeln("No"); } } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
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 K = RD; writeln(K == 1 ? 0 : N - K); stdout.flush(); debug readln(); }
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() { char[] s = sread().to!(char[]); s[5] = s[13] = ' '; s.writeln(); }
D
import std.stdio,std.conv,std.string,std.algorithm; void main(){ auto ab=readln().chomp().split().map!(to!int); int a=ab[0],b=ab[1]; if(a%2==0 || b%2==0) writeln("Even"); else writeln("Odd"); }
D
import std.stdio, std.conv, std.string, std.algorithm, std.range; void main() { auto a = readln.split[0].to!int; auto s = readln.split[0]; if(a >= 3200) s.writeln; else "red".writeln; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; long k, a, b; rd(k, a, b); if(a>=k){writeln(1); return;} auto d=a-b; if(d<=0){writeln(-1); return;} auto tmp=(k-a+d-1)/d; writeln(tmp*2+1); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } } void wr(T...)(T x){ import std.stdio; foreach(e; x) write(e, " "); writeln(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; enum P = 10L^^9+7; void main() { auto K = readln.chomp.to!(char[]); auto D = readln.chomp.to!long; auto DP1 = new long[][](2, D); auto DP2 = new long[][](2, D); foreach (i, c; K) { if (i == 0) { foreach (x; 0..c-'0') { ++DP1[0][x % D]; } ++DP1[1][(c-'0') % D]; } else { foreach (x; 0..D) { foreach (y; 0..10) { DP1[0][(x+y)%D] = (DP1[0][(x+y)%D] + DP2[0][x]) % P; } foreach (y; 0..c-'0') { DP1[0][(x+y)%D] = (DP1[0][(x+y)%D] + DP2[1][x]) % P; } DP1[1][(x+c-'0')%D] = (DP1[1][(x+c-'0')%D] + DP2[1][x]) % P; } } DP2[0][] = DP1[0][].dup; DP2[1][] = DP1[1][].dup; DP1[0][] = 0; DP1[1][] = 0; } writeln((DP2[1][0] + DP2[0][0] - 1 + P) % P); }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { foreach (test; 0..readln.strip.to !(int)) { auto s = readln.strip; auto x = readln.strip.to !(int); auto n = s.length.to !(int); auto w = new char [n]; w[] = '1'; foreach (i; 0..n) { if (s[i] == '0') { if (i - x >= 0) { w[i - x] = '0'; } if (i + x < n) { w[i + x] = '0'; } } } bool ok = true; foreach (i; 0..n) { if (s[i] == '1') { if (!(i - x >= 0 && w[i - x] == '1') && !(i + x < n && w[i + x] == '1')) { ok = false; } } } writeln (ok ? w : "-1"); } }
D
// AtCoder My Practice // author: Leonardone @ NEETSDKASU import std.stdio : readln, writeln; import std.string : chomp; import std.conv : to; void main() { auto n = readln.chomp.to!uint; auto s1 = readln.chomp; auto s2 = readln.chomp; long ans, md = 1_000_000_007; uint i, pat; if (s1[0] == s2[0]) { ans = 3; i = 1; pat = 0; } else { ans = 6; i = 2; pat = 1; } while (i < n) { if (s1[i] == s2[i]) { if (pat == 0) { ans = (ans * 2) % md; } pat = 0; i += 1; } else { if (pat == 1) { ans = (ans * 3) % md; } else { ans = (ans * 2) % md; } pat = 1; i += 2; } } writeln(ans); }
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 a = "AKIHABARA"; int i; auto f = false; foreach (e; s) { if (i < a.length && e == a[i]) { i++; } else if (i < a.length && a[i] == 'A') { i++; if (i < a.length && e == a[i]) { i++; } else { f = true; break; } } else { f = true; break; } } if (i == a.length - 1) { } else if (i != a.length) { f = true; } if (f) { writeln("NO"); } else { writeln("YES"); } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { int n = readint(); bool[string] map; for (int i = 0; i < n; i++) { auto ss = readln.split; switch (ss[0][0]) { case 'i': map[ss[1]] = true; break; case 'f': writeln(ss[1] in map ? "yes" : "no"); break; default: } } }
D
void main() { string s = rdStr; long len = s.length; long[] lists = new long[len]; auto g = s.group.array; long cnt; foreach (x; g) { long n = x[1].to!long; cnt += n; if (x[0] == 'R') { lists[cnt-1] += (n >> 1) + (n & 1); lists[cnt] += (n >> 1); } else { lists[cnt-n-1] += (n >> 1); lists[cnt-n] += (n >> 1) + (n & 1); } } foreach (i, x; lists) { x.write; if (i == len - 1) writeln; else " ".write; } } 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
void main() { auto X = ri; if(X == 7 || X == 5 || X == 3) { writeln("YES"); } else writeln("NO"); } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void get(Args...)(ref Args args) { import std.traits, std.meta, std.typecons; static if (Args.length == 1) { alias Arg = Args[0]; static if (isArray!Arg) { args[0] = readln.split.to!Arg; } else static if (isTuple!Arg) { auto input = readln.split; static foreach (i; 0..Fields!Arg.length) { args[0][i] = input[i].to!(Fields!Arg[i]); } } else { args[0] = readln.chomp.to!Arg; } } else { auto input = readln.split; assert(input.length == Args.length); static foreach (i; 0..Args.length) { args[i] = input[i].to!(Args[i]); } } } void get_lines(Args...)(size_t N, ref Args args) { import std.traits, std.range; static foreach (i; 0..Args.length) { static assert(isArray!(Args[i])); args[i].length = N; } foreach (i; 0..N) { static if (Args.length == 1) { get(args[0][i]); } else { auto input = readln.split; static foreach (j; 0..Args.length) { args[j][i] = input[j].to!(ElementType!(Args[j])); } } } } int celiPow2(int n) @safe pure nothrow @nogc { int x = 0; while ((1u << x) < cast(uint)(n)) x++; return x; } struct Segtree(S, alias op, alias e) { import std.functional : binaryFun, unaryFun; import std.traits : isCallable, Parameters; static if (is(typeof(e) : string)) { auto unit() { return mixin(e); } } else { alias unit = e; } this(int n) { auto buf = new S[](n); buf[] = unit(); this(buf); } this(S[] v) { _n = cast(int) v.length; log = celiPow2(_n); size = 1 << log; d = new S[](2 * size); d[] = unit(); foreach (i; 0 .. _n) d[size + i] = v[i]; foreach_reverse (i; 1 .. size) update(i); } void set(int p, S x) { assert(0 <= p && p < _n); p += size; d[p] = x; foreach (i; 1 .. log + 1) update(p >> i); } S get(int p) { assert(0 <= p && p < _n); return d[p + size]; } S prod(int l, int r) { assert(0 <= l && l <= r && r <= _n); S sml = unit(), smr = unit(); l += size; r += size; while (l < r) { if (l & 1) sml = binaryFun!(op)(sml, d[l++]); if (r & 1) smr = binaryFun!(op)(d[--r], smr); l >>= 1; r >>= 1; } return binaryFun!(op)(sml, smr); } S allProd() { return d[1]; } int maxRight(alias f)(int l) { return maxRight(l, &unaryFun!(f)); } int maxRight(F)(int l, F f) if (isCallable!F && Parameters!(F).length == 1) { assert(0 <= l && l <= _n); assert(f(unit())); if (l == _n) return _n; l += size; S sm = unit(); do { while (l % 2 == 0) l >>= 1; if (!f(binaryFun!(op)(sm, d[l]))) { while (l < size) { l = 2 * l; if (f(binaryFun!(op)(sm, d[l]))) { sm = binaryFun!(op)(sm, d[l]); l++; } } return l - size; } sm = binaryFun!(op)(sm, d[l]); l++; } while ((l & -l) != l); return _n; } int minLeft(alias f)(int r) { return minLeft(r, &unaryFun!(f)); } int minLeft(F)(int r, F f) if (isCallable!F && Parameters!(F).length == 1) { assert(0 <= r && r <= _n); assert(f(unit())); if (r == 0) return 0; r += size; S sm = unit(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!f(binaryFun!(op)(d[r], sm))) { while (r < size) { r = 2 * r + 1; if (f(binaryFun!(op)(d[r], sm))) { sm = binaryFun!(op)(d[r], sm); r--; } } return r + 1 - size; } sm = binaryFun!(op)(d[r], sm); } while ((r & -r) != r); return 0; } private: int _n = 0, size = 1, log = 0; S[] d = [unit(), unit()]; void update(int k) { d[k] = binaryFun!(op)(d[2 * k], d[2 * k + 1]); } } void main() { int N, K; get(N, K); auto segt = Segtree!(int, "a >= b ? a : b", () => 0)(300_001); while (N--) { int A; get(A); auto l = segt.prod(max(0, A-K), min(300_000, A+K)+1); segt.set(A, l + 1); } writeln(segt.allProd()); }
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 ab = readln.chomp.split.to!(int[]); auto d = ab[1] - ab[0]; int sum; foreach (i; 1..d) { sum += i; } writeln(sum - ab[0]); }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { auto S = readln.chomp; iota(S.length-2).map!(i => S[i..i+3]).map!(s => abs(753-s.to!int)).reduce!min.writeln; }
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 ip1 = readln.chomp; auto ip2 = readln.chomp; auto ip3 = readln.chomp; ("" ~ ip1[0] ~ ip2[1] ~ ip3[2]).writeln; }
D
void main() { long[] tmp = readln.split.to!(long[]); long n = tmp[0], k = tmp[1]; long[] a = readln.split.to!(long[]); long mod = 10 ^^ 9 + 7; long[] l = new long[n], r = new long[n]; foreach (i; 0 .. n) { foreach (j; 0 .. n) { if (a[i] > a[j]) { if (i > j) ++l[i]; else ++r[i]; } } } long lsum = k * (k - 1) / 2 % mod; long rsum = k * (k + 1) / 2 % mod; long result; foreach (i; 0 .. n) { long total = (l[i] * lsum) % mod + (r[i] * rsum) % mod; total %= mod; result = (result + total) % mod; } 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