code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math; void main() { auto nk = readln.split.to!(int[]); auto N = nk[0]; auto K = nk[1]; readln; int cnt = 1; N -= K; --K; while (N > 0) { ++cnt; N -= K; } writeln(cnt); }
D
import std.stdio, std.string, std.conv; void main() { int[][] c; for (int i = 0; i < 3; i++) { c ~= to!(int[])(readln.chomp.split); } int[3] a, b; // a[0] = 0 と仮定 foreach (i, e; c[0]) { b[i] = e; } for (int i = 1; i < 3; i++) { a[i] = c[i][i] - b[i]; } bool f = true; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { // writeln(c[i][j], " == ", a[i], " + ", b[j]); f = f & (c[i][j] == a[i] + b[j]); } } writeln(f ? "Yes" : "No"); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; 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 s = n * (n - 1) / 2; s.writeln(); }
D
import core.stdc.stdlib; import std.algorithm; import std.array; import std.conv; import std.math; import std.numeric; import std.stdio; import std.string; // void toBaseMinus2(in long rest, in long digit, in string result) // { // // stderr.writeln(rest, ", ", digit, ", ", result.dup.reverse); // if (rest == 0) // { // if (result == "") // { // writeln("0"); // } // else // { // writeln(result.dup.reverse); // } // exit(0); // } // else // { // long d = pow(-2, digit); // if (abs(d) < abs(n)) // { // toBaseMinus2(rest, digit + 1, result ~ '0'); // toBaseMinus2(rest - d, digit + 1, result ~ '1'); // } // } // } // long n; void main() { // n = readln.chomp.to!long; // toBaseMinus2(n, 0, ""); // string s; // long digit = 0; // while(true) { // pow(-2, digit); // } // for (long k = 30; 0 <= k; --k) // { // auto d = pow(-2, k); // if (d <= n) // { // n -= d; // s ~= '1'; // } // else // { // s ~= '0'; // } // } // writeln(s.dup.reverse); long n = readln.chomp.to!long; long digit = 0; string result; while (n != 0) { if (n % pow(2, digit + 1) == 0) { result ~= '0'; ++digit; } else { n -= pow(-2, digit); result ~= '1'; ++digit; } } if (result == "") { writeln("0"); } else { auto dup = result.dup; reverse(dup); writeln(dup); } }
D
import std.stdio, std.string, std.conv; long solve(long n) { import std.algorithm; long result = long.max; for (long i = 1; i * i <= n; ++i) { if (n % i == 0) result = min(result, i+n/i-2); } return result; } void main() { long n = readln.chomp.to!long; solve(n).writeln; }
D
import std.stdio, std.string; void main(){ int w, h; while(true){ scanf("%d %d", &h, &w); if( w== 0&&h == 0) break; 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
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto T = readln.chomp.to!int; foreach (_; 0..T) { auto xy = readln.split.to!(long[]); auto x = xy[0]; auto y = xy[1]; auto ab = readln.split.to!(long[]); auto a = ab[0]; auto b = ab[1]; writeln(min( x * a + y * a, min(x, y) * b + abs(x - y) * a )); } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; void main() { auto input = readln.chomp.split.to!(int[]); auto H = input[0]; auto W = input[1]; auto S = new string[H]; foreach(ref i ; S) i = readln.chomp.to!string; int[8] dx = [-1, 1, 0, 0, -1, -1, 1, 1]; int[8] dy = [0, 0, -1, 1, -1, 1, -1, 1]; foreach(i ; 0..H){ foreach(int j ; 0..W){ if(S[i][j] == '.'){ int count = 0; foreach(k ; 0..8){ int nx = j + dx[k]; int ny = i + dy[k]; if(0 <= nx && nx < W && 0 <= ny && ny < H && S[ny][nx] == '#'){ count++; } } count.write; } else S[i][j].write; } writeln; } }
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; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } void main(){ int n = read.to!int; int m = read.to!int; bool[] xs; xs.length = n + 1; foreach(i; 1 .. n + 1) xs[i] = 0; foreach(i; 0 .. m){ int a = read.to!int; int b = read.to!int; xs[a] = !xs[a]; xs[b] = !xs[b]; } string ans = "YES"; foreach(x; xs) if(x) ans = "NO"; ans.writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } 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 m = RD; auto k = RD-1; if (m < n-1) continue; auto cnt = n * (n-1) / 2; if (m > cnt) continue; long kk; if (n == 1) kk = 0; else if (m == cnt) kk = 1; else kk = 2; debug writeln("kk:", kk); if (kk < k) ans[ti] = true; } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush; debug readln; }
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(string input) { foreach(c; ['a','b','c']) { if (!input.canFind(c)) return "No"; } return "Yes"; } solve(S).writeln; }
D
void main() { long[] nk = rdRow; long n = nk[0], k = nk[1]; long[] rsp = rdRow; string t = rdStr; string u; long score; foreach (i, x; t) { dchar now = check(x); if (i >= k) { if (u[i-k] == now) { if (i + k >= n) { u ~= check(now); } else { u ~= check(check(t[i+k])); } } else { u ~= now; score += calc(now, rsp); } } else { u ~= now; score += calc(now, rsp); } } score.writeln; } dchar check(dchar x) { if (x == 'r') return 'p'; else if (x == 's') return 'r'; else return 's'; } long calc(dchar x, long[] arr) { if (x == 'r') return arr[0]; else if (x == 's') return arr[1]; else return arr[2]; } T rdElem(T = long)() { //import std.stdio : readln; //import std.string : chomp; //import std.conv : to; return readln.chomp.to!T; } alias rdStr = rdElem!string; dchar[] rdDchar() { //import std.conv : to; return rdStr.to!(dchar[]); } T[] rdRow(T = long)() { //import std.stdio : readln; //import std.array : split; //import std.conv : to; return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdRow!T).array; } void wrMat(T = long)(T[][] mat) { //import std.stdio : write, writeln; foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; int a, b; void main() { scan(a, b); if (!(a > 0 || b < 0)) { writeln("Zero"); } else if (b < 0 && (b - a + 1) & 1) { writeln("Negative"); } else { writeln("Positive"); } } 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
void main() { problem(); } void problem() { auto a = scan; string solve() { return a[$-1] == 's' ? a ~ "es" : a ~ "s"; } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; long a, b, c, d, e, f; rd(a, b, c, d, e, f); /* 100b/(a+b) < 100b'/(a'+b') 100b*(a'+b') < 100b'*(a+b) */ long mx_w=100*a, mx_s; for(long w1=100*a; w1<=f; w1+=100*a){ for(long w2=0; w2<=f; w2+=100*b){ for(long s1=0; s1<=f; s1+=c){ for(long s2=0; s2<=f; s2+=d){ // cap? if(w1+w2+s1+s2 > f) continue; // dissolved? if(e*(w1+w2)/100 < s1+s2) continue; if(mx_s*((w1+w2)+(s1+s2)) < (s1+s2)*(mx_w+mx_s)){ mx_w=w1+w2; mx_s=s1+s2; } } } } } writeln(mx_w+mx_s, " ", mx_s); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.math; void main() { int input; immutable limitList = 500000; bool[] listNumbers = new bool[](limitList); int[] listPrimeNumbers; listNumbers.fill(true); foreach (i; 2..limitList.to!double.sqrt.to!int) { if (listNumbers[i]) { for (int j = i*2; j < limitList; j += i) listNumbers[j] = false; } } foreach (i; 2..listNumbers.length) { if (listNumbers[i]) { listPrimeNumbers ~= i.to!int; } } while ((input = readln.chomp.to!int) != 0) { ulong sum = 0; foreach (i; 0..listPrimeNumbers.length) { if (i == input) break; sum += listPrimeNumbers[i]; } writeln(sum); } }
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() { int[] NK = readln.chomp.split.to!(int[]); int N = NK[0], K = NK[1]; int[] A = readln.chomp.split.to!(int[]); size_t p; foreach (i, a; A) { if (a == 1) { p = i; break; } } size_t g; for (;;) { if (g * (K - 1) <= p && p <= (g + 1) * (K - 1)) { break; } g++; } size_t ans; foreach_reverse (i; 0..g+1) { ans++; foreach (j; 0..K) { A[i * (K - 1) + j] = 1; } } import std.math; foreach (i; g+1..ceil((N.to!real-1f)/(K.to!real-1f) - 1f)+1) { ans++; foreach (j; 0..K) { A[N - K + j] = 1; } } writeln(ans); }
D
import std.algorithm; import std.array; import std.ascii; import std.bigint; import std.complex; import std.container; import std.conv; import std.functional; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } void readlnTo(T...)(ref T t) { auto s = readln().split(); assert(s.length == t.length); foreach(ref ti; t) { ti = s[0].to!(typeof(ti)); s = s[1..$]; } } const real eps = 1e-10; const long p = 1_000_000_000 + 7; void main(){ long n, a, b; readlnTo(n, a, b); if(a > b) { writeln(0); return; } if(n == 1) { if(a != b) { writeln(0); } else { writeln(1); } return; } writeln((b * (n-1) + a) - (b + a * (n-1)) + 1); }
D
void main() { (rs.canFind("9") ? "Yes" : "No").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; import std.string; import std.format; 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.concurrency; import std.traits; import std.uni; import std.regex; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; enum long INF = long.max/5; enum long MOD = 10L^^9+7; void main() { string s = readln.chomp; bool ok = true; long N = s.length; foreach(i; 0..N) { ok &= s[i] == s[N-i-1]; } foreach(i; 0..(N-1)/2) { ok &= s[i] == s[(N-1)/2-1-i]; } foreach(i; (N+3)/2..N) { ok &= s[i] == s[N-i-1]; } writeln(ok ? "Yes" : "No"); } // ---------------------------------------------- void times(alias fun)(long n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(long n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { // `(x+y-1)/y` will only work for positive numbers ... T t = x / y; if (y > 0 && t * y < x) t++; if (y < 0 && t * y > x) t++; return t; } T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { T t = x / y; if (y > 0 && t * y > x) t--; if (y < 0 && t * y < x) t--; return t; } ref T ch(alias fun, T, S...)(ref T lhs, S rhs) { return lhs = fun(lhs, rhs); } unittest { long x = 1000; x.ch!min(2000); assert(x == 1000); x.ch!min(3, 2, 1); assert(x == 1); x.ch!max(100).ch!min(1000); // clamp assert(x == 100); x.ch!max(0).ch!min(10); // clamp assert(x == 10); } mixin template Constructor() { import std.traits : FieldNameTuple; this(Args...)(Args args) { // static foreach(i, v; args) { foreach(i, v; args) { mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v; } } } void scanln(Args...)(auto ref Args args) { enum sep = " "; enum n = Args.length; enum fmt = n.rep!(()=>"%s").join(sep); string line = readln.chomp; static if (__VERSION__ >= 2074) { line.formattedRead!fmt(args); } else { enum argsTemp = n.iota.map!( i => "&args[%d]".format(i) ).join(", "); mixin( "line.formattedRead(fmt, " ~ argsTemp ~ ");" ); } } // 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); } } } } // popcnt with ulongs was added in D 2.071.0 static if (__VERSION__ < 2071) { ulong popcnt(ulong x) { x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L); x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L); x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL); x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL); x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL); x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL); return x; } }
D
import std.functional, std.algorithm, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv; ulong M = 10UL^^9 + 7; ulong fact(ulong n) { if (n <= 1) { return 1; } else { return n * fact(n - 1) % M; } } void main() { ulong n = readln.chomp.to!ulong; writeln(fact(n)); }
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; int main() { auto sc = new Scanner(stdin); long q, h, s, d; sc.read(q, h, s, d); h = min(h, 2*q); s = min(s, 2*h); d = min(d, 2*s); long n; sc.read(n); n *= 4; long sm = 0; sm += n / 8 * d; n %= 8; sm += n / 4 * s; n %= 4; sm += n / 2 * h; n %= 2; sm += n / 1 * q; n %= 1; writeln(sm); return 0; } /* 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); } } }
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 = "%.15f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto s = RD!string; char inv(char c) { return c == 'S' ? 'W' : 'S'; } bool same(char ox, char sw) { return !((ox == 'o') ^ (sw == 'S')); } char[] ans; (){ foreach (i; 0..2) { foreach (j; 0..2) { auto t = new char[](N); t[0] = i == 0 ? 'S' : 'W'; t[1] = j == 0 ? 'S' : 'W'; foreach (k, c; s[0..$-1]) { if (k == 0) continue; auto l = (N + k - 1) % N; auto r = (k + 1) % N; t[r] = same(c, t[k]) ? t[l] : inv(t[l]); } bool ok; if (same(s[$-1], t[$-1])) ok = t[$-2] == t[0]; else ok = t[$-2] != t[0]; if (ok) { if (same(s[0], t[0])) ok = t[$-1] == t[1]; else ok = t[$-1] != t[1]; if (ok) { ans = t; return; } } } }}(); if (ans.empty) writeln(-1); else 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 a = readln.chomp.to!int; auto b = readln.chomp.to!int; auto h = readln.chomp.to!int; writeln((a + b) * h / 2); }
D
import std.stdio, std.string, std.range, std.conv, std.algorithm, std.typecons; void main(){ alias Tuple!(int, "x", int, "y") Point; int W, H, N; auto input = readln.split.map!(to!int); W = input[0], H = input[1], N = input[2]; int total; Point before; foreach (i; 0..N) { input = readln.split.map!(to!int); Point p; p.x = input[0], p.y = input[1]; if (i > 0) { int dx = p.x - before.x, dy = p.y - before.y; if (dx * dy > 0) { dx = dx > 0 ? dx : -dx; dy = dy > 0 ? dy : -dy; total += max(dx, dy); } else { dx = dx > 0 ? dx : -dx; dy = dy > 0 ? dy : -dy; total += dx + dy; } } before = p; } total.writeln; }
D
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math, std.container, std.typecons; import core.stdc.stdio; // foreach, foreach_reverse, writeln void main() { int n; scanf("%d", &n); int[] a = new int[n]; foreach (i; 0..n) scanf("%d", &a[i]); int[int] t; t[0] = 0; long ans = 0; foreach (i; 0..n) { int[int] prev; swap(prev, t); prev[0]++; foreach (x; prev.byKey()) { if ((x&a[i]) == 0) { ans += prev[x]; int y = x|a[i]; if (y in t) { t[y] += prev[x]; } else { t[y] = prev[x]; } } } if (!(0 in t)) { t[0] = 0; } } writeln(ans); }
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 = RDR.ARR; long ans; long last, streak; foreach (i; 0..N) { if (a[i] != last) { ans += streak / 2; streak = 1; } else ++streak; last = a[i]; } ans += streak / 2; writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio,std.string,std.conv; int main() { int[char] roman = ['I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000]; string s; while((s = readln.chomp).length != 0) { int ans = 0; for(int i=0;i<s.length-1;i++) { if(roman[s[i]] >= roman[s[i+1]]) ans += roman[s[i]]; else ans -= roman[s[i]]; } ans += roman[s[s.length-1]]; writeln(ans); } return 0; }
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 n = readln.chomp.to!int; auto a = readln.chomp.split.to!(long[]); auto l = 0; auto r = n - 1; long ls, rs; while (l <= r) { if (ls <= rs) { ls += a[l]; l++; } else if (rs < ls) { rs += a[r]; r--; } } abs(ls - rs).to!long.writeln; }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n; readV(n); auto m = 0, a = 1; foreach (i; 1..n+1) { auto r = i.bsf; if (r > m) { a = i; m = r; } } writeln(a); } pragma(inline) { pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; } pure T bitSet(T)(T n, size_t i) { return n | (T(1) << i); } pure T bitReset(T)(T n, size_t i) { return n & ~(T(1) << i); } pure T bitComp(T)(T n, size_t i) { return n ^ (T(1) << i); } pure T bitSet(T)(T n, size_t s, size_t e) { return n | ((T(1) << e) - 1) & ~((T(1) << s) - 1); } pure T bitReset(T)(T n, size_t s, size_t e) { return n & (~((T(1) << e) - 1) | ((T(1) << s) - 1)); } pure T bitComp(T)(T n, size_t s, size_t e) { return n ^ ((T(1) << e) - 1) & ~((T(1) << s) - 1); } import core.bitop; pure int bsf(T)(T n) { return core.bitop.bsf(ulong(n)); } pure int bsr(T)(T n) { return core.bitop.bsr(ulong(n)); } pure int popcnt(T)(T n) { return core.bitop.popcnt(ulong(n)); } }
D
import std.stdio,std.string,std.conv,std.algorithm,std.range,std.array; void main(){ foreach(_;0..readln.chomp.to!int){ readln.replace("Hoshino","Hoshina").write; } }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto s = readln.split.map!(to!int).array; auto N = s[0]; auto A = s[1].to!long; auto B = s[2].to!long; auto H = N.iota.map!(_ => readln.chomp.to!long).array; long hi = 10L ^^ 9 + 1; long lo = 0; while (hi - lo > 1) { auto mid = (hi + lo) / 2; long tmp = 0; foreach (i; 0..N) { if (H[i] - mid * B > 0) tmp += max(0, (H[i] - mid * B - 1) / (A - B) + 1); } if (tmp <= mid) hi = mid; else lo = mid; } hi.writeln; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { long a, b; scan(a, b); writeln((a - 1) * (b - 1)); }
D
// Cheese-Cracker: cheese-cracker.github.io dchar[] stringify(ll num){ dchar[] res; while(num > 0){ res ~= to!dchar('0' + (num % 10)); num /= 10; } res.reverse; return res; } dchar[][] pow2; void preProcess(){ ll num = 1; for(int i = 0; i <= 62; ++i){ pow2 ~= stringify(num); num *= 2; } } ll common(dchar[] w, dchar[] p2){ int ptr = 0; foreach(c; w){ if(p2[ptr] == c){ ++ptr; } if(ptr == p2.length.to!long){ return ptr.to!long; } } return ptr.to!long; } void theCode(){ auto w = scan!(dchar[]); ll mindist = 32; for(int i = 0; i < 62; ++i){ ll com = common(w, pow2[i]); ll dist = w.length.to!long + pow2[i].length.to!long - 2 * com; mindist = min(dist, mindist); } writeln(mindist); } void main(){ preProcess(); 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
// 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() { long N, M; scan(N, M); auto f = factorize(M); long ans = 1; auto C = Combination_mod(500000); foreach (key, val; f) { ans *= C(val + N - 1, val); ans %= MOD; } writeln(ans); } T[U] factorize(T = long, U = long)(T x) { assert(0 < x, "x is negative"); long[long] ps; while ((x & 1) == 0) x /= 2, ps[2] = (2 in ps) ? ps[2] + 1 : 1; for (long i = 3; i * i <= x; i += 2) while (x % i == 0) x /= i, ps[i] = (i in ps) ? ps[i] + 1 : 1; if (x != 1) ps[x] = (x in ps) ? ps[x] + 1 : 1; return ps; } /// Number of k-combinations % m (precalculated) alias Combination_mod = Combination_modImpl!long; struct Combination_modImpl(T) { T _n, _m; T[] _fact, _factinv; this(T maxnum, T mod = 10 ^^ 9 + 7) { _n = maxnum, _m = mod, _fact = new T[](_n + 1), _factinv = new T[](_n + 1), _fact[0] = 1; foreach (i; 1 .. _n + 1) _fact[i] = _fact[i - 1] * i % _m; T powmod(T x, T n, T m) { if (n < 1) return 1; if (n & 1) { return x * powmod(x, n - 1, m) % m; } T tmp = powmod(x, n / 2, m); return tmp * tmp % m; } foreach (i; 0 .. _n + 1) _factinv[i] = powmod(_fact[i], _m - 2, _m); } T opCall(T n, T k, T dummy = 10 ^^ 9 + 7) { return n < k ? 0 : ((_fact[n] * _factinv[n - k] % _m) * _factinv[k] % _m); } }
D
import std.stdio; import std.conv; import std.algorithm; import std.string; import std.file; int main() { int[] hs; string l; readln(); while((l = readln()).length >= 2){ int a, b, c; a = to!int(l.split()[0]), b = to!int(l.split()[1]), c = to!int(l.split()[2]); if(a * a + b * b == c * c || b * b + c * c == a * a || c * c + a * a == b * b) printf("YES\n"); else printf("NO\n"); } return 0; }
D
import std.stdio; import core.stdc.stdio; import std.array; import std.conv; import std.algorithm; import std.range; void main() { int n,a; scanf("%d%d",&n,&a); int x = n % 500; writeln(x <= a ? "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 S = RD!string; writeln("2018" ~ S[4..$]); stdout.flush(); debug readln(); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; const long MOD = 10^^9 + 7; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto G = new DirectedGraph(N); foreach (_; 0..M) { s = readln.split.map!(to!int); G.add_edge(s[0], s[1]); } auto scc = new StronglyConnectedComponent(G); auto Q = readln.chomp.to!int; while (Q--) { s = readln.split.map!(to!int); auto u = s[0]; auto v = s[1]; writeln( scc.index[u] == scc.index[v] ? 1 : 0 ); } } class DirectedGraph { int n; int[][] adj; int[][] inv; this(int n) { this.n = n; adj = new int[][](n); inv = new int[][](n); } void add_edge(int u, int v) { adj[u] ~= v; inv[v] ~= u; } } class StronglyConnectedComponent : DirectedGraph { int[] index; int[][] scc; this(DirectedGraph g) { int cnt = 0; auto ord = new int[](g.n); auto used = new bool[](g.n); index = new int[](g.n); void dfs1(int n) { if (used[n]) return; used[n] = true; foreach (m; g.adj[n]) if (!used[m]) dfs1(m); ord[cnt++] = n; } void dfs2(int n) { if (used[n]) return; used[n] = true; index[n] = cnt; scc.back ~= n; foreach (m; g.inv[n]) if (!used[m]) dfs2(m); } foreach (i; 0..g.n) if (!used[i]) dfs1(i); cnt = 0; fill(used, false); foreach_reverse (i; 0..g.n) { if (used[ord[i]]) continue; scc.length += 1; dfs2(ord[i]); cnt += 1; } super(cnt); auto edge = new bool[int][](cnt); foreach (i; 0..g.n) foreach (j; g.adj[i]) if (index[i] != index[j]) edge[index[i]][index[j]] = true; foreach (i; 0..cnt) foreach (j; edge[i].keys) add_edge(i, j); } }
D
void main() { auto S = rs; auto T = rs; if(S.length + 1 == T.length && T[0..$-1] == S) { 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; } long rl() { return readAs!long; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio; import std.algorithm; import std.range; import std.conv; import std.format; import std.array; import std.math; import std.string; import std.container; void main() { string S, T; readlnTo(S); readlnTo(T); int[] a = new int[26]; int[] b = new int[26]; a.fill(-1); b.fill(-1); bool ok = true; foreach(i; 0..S.length) { auto s = S[i] - 'a'; auto t = T[i] - 'a'; if (a[s] >= 0) { ok = ok && (a[s] == t); } if (b[t] >= 0) { ok = ok && (b[t] == s); } if (a[s] < 0) a[s] = t; if (b[t] < 0) b[t] = s; } writeln(ok ? "Yes" : "No"); } // helpers void readlnTo(T...)(ref T t) { auto s = readln.split; assert(s.length == t.length); foreach(ref ti; t) { ti = s[0].to!(typeof(ti)); s = s[1..$]; } }
D
import std.stdio, std.string, std.conv; import std.typecons; import std.algorithm, std.array, std.range, std.container; import std.math; void main() { auto data = readln.split; auto H = data[0].to!int, W = data[1].to!int; data = readln.split; auto h = data[0].to!int, w = data[1].to!int; writeln(H*W - h*W - w*H + h*w); }
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; alias Mame = Tuple!(long, "point", long, "number"); 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 s = sread(); if (s == "Sunny") writeln("Cloudy"); if (s == "Cloudy") writeln("Rainy"); if (s == "Rainy") writeln("Sunny"); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new int[](t); foreach (ti; 0..t) { auto s = RD!string; int cnt, cnt_b; foreach_reverse (i; 0..s.length) { if (s[i] == 'B') ++cnt_b; else if (cnt_b >= 1) { --cnt_b; cnt += 2; } } cnt += cnt_b - (cnt_b % 2); ans[ti] = cast(int)s.length - cnt; } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
void main() { auto n = ri; n %= 10; if (n == 0) writeln("pon"); if (n == 1) writeln("pon"); if (n == 2) writeln("hon"); if (n == 3) writeln("bon"); if (n == 4) writeln("hon"); if (n == 5) writeln("hon"); if (n == 6) writeln("pon"); if (n == 7) writeln("hon"); if (n == 8) writeln("pon"); if (n == 9) writeln("hon"); } // =================================== 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; } long rl() { return readAs!long; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
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; } int INF = int.max; void main() { int N = readln.chomp.to!int; int[] ary = new int[N+1]; ary[0..$] = INF; int l = 0; int r = 1; foreach(i; 0..N+1) { if (!(l<r)) break; foreach(j; l..r) { if (j>N) break; ary[j] = min(ary[j], i); } l = r; r = r+i+1; } int k = N; while(k > 0) { ary[k].writeln; k -= ary[k]; } }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { auto n = lread(); auto s = sread(); auto t = sread(); if (s == t) { writeln(s.length); return; } long ans = -1; foreach (i; 0 .. n) { if (func(s, t, i)) { ans = max(ans, i); } } // writeln(ans); writeln((s.length + t.length) - ans); } bool func(string s, string t, long x) { if (s[($ - x) .. $] == t[0 .. x]) { return true; } return false; } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = readln.chomp; writeln(n[0] == n[2] ? "Yes" : "No"); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; // dfmt on void main() { long K, X; scan(K, X); write(X - K + 1); foreach (i; 1 .. K * 2 - 1) { write(" ", X - K + i + 1); } writeln(); }
D
import std.stdio; import std.array; import std.conv; void main(){ string[] input = readln.split; int A = input[0].to!int; int B = input[1].to!int; int C = input[2].to!int; int cnt; while(C > 0){ C--; cnt++; if(B > 0){ B--; cnt++; } else if(A > 0){ A--; } else{ break; } } if(B > 0) cnt += B; write(cnt); }
D
import std.stdio; import std.algorithm; import std.math; import std.conv; import std.string; T readNum(T)(){ return readStr.to!T; } T[] readNums(T)(){ return readStr.split.to!(T[]); } string readStr(){ return readln.chomp; } void main(){ auto n = readNum!int; auto p = readNums!int; int ret; foreach(i; 1 .. n-1){ auto s = p[i-1 .. i+2]; if((s[0] < s[1] && s[1] <= s[2]) || (s[0] >= s[1] && s[1] > s[2])){ ret++; } } writeln(ret); }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); int tot = 0; for (int i = 1; i <= n; i += 2) { int k = 2; for (int j = 2; j * j <= i; j++) { if (i % j == 0) k += 2; } if (k == 8) { tot++; } } writeln(tot); } 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.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { long k, a, b; scan(k, a, b); long ans; if (k <= a) { ans = 1; } else if (a <= b) { ans = -1; } else { ans = 2L * ((k - b - 1) / (a - b)) + 1; } writeln(ans); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; const MOD = 10^^9 + 7; void add(T)(ref T a, T b) { a = (a + b) % MOD; } long calc(string s, int d) { int n = cast(int) s.length; auto dp = new int[][][](n + 1, 2, d); // dp[i][j][k] // i: 上から i 桁目まで // j: n 未満か // k: mod d dp[0][0][0] = 1; foreach (i; 0..n) { foreach (j; 0..2) { foreach (k; 0..d) { int lim = j == 1 ? 9 : s[i] - '0'; foreach (m; 0..lim+1) { add(dp[i + 1][j || m < lim][(k + m) % d], dp[i][j][k]); } } } } // -1 は 0 の分 return (-1L + MOD + dp[n][0][0] + dp[n][1][0]) % MOD; } void main() { string s = read!string; int d = readint; writeln(calc(s, d)); }
D
import std.stdio; import std.conv; import std.algorithm; import std.range; import std.string; import std.math; import std.format; void main() { foreach (string line; stdin.lines) { auto a = line.chomp.split.map!(to!int).array; auto b = readln.chomp.split.map!(to!int).array; int hit, blow; foreach (i, ai; a) { foreach (j, bj; b) { if (ai == bj) { if (i == j) { hit++; } else { blow++; } } } } writeln(hit, " ", blow); } }
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() { int[] vx = [-1, 0, 1, 0]; int[] vy = [0, -1, 0, 1]; while(true) { int N = readln.chomp.to!int; if (N == 0) break; int[2][int] aa; aa[0] = [0, 0]; int[] maxPos = [0, 0]; int[] minPos = [0, 0]; foreach(int i; 1..N) { int[] input = readln.split.to!(int[]); int n = input[0]; int d = input[1]; aa[i] = [aa[n][0]+vx[d], aa[n][1]+vy[d]]; maxPos[0] = max(maxPos[0], aa[i][0]); maxPos[1] = max(maxPos[1], aa[i][1]); minPos[0] = min(minPos[0], aa[i][0]); minPos[1] = min(minPos[1], aa[i][1]); } writeln(maxPos[0]-minPos[0]+1, " ", maxPos[1]-minPos[1]+1); } }
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); } struct SegTree(T, E) { alias T delegate(T, E) G; alias T delegate(E, E) H; int n; G g; H h; T d1; E d0; T[] dat; E[] laz; this(int _n, G _g, H _h, T _d1, E _d0, T[] v = []) { g = _g; h = _h; d1 = _d1; d0 = _d0; init(_n); if (_n == cast(int)v.length) build(_n, v); } void init(int _n) { n = 1; while (n < _n) n *= 2; dat.length = n; dat[] = d1; laz.length = 2*n-1; laz[] = d0; } void build(int _n, T[] v) { foreach (i; 0.._n) dat[i] = v[i]; } void update(int a, int b, E x, int k, int l, int r) { if (r <= a || b <= l) return; if (a <= l && r <= b) { laz[k] = h(laz[k], x); return; } update(a, b, x, k*2+1, l, (l+r)/2); update(a, b, x, k*2+2, (l+r)/2, r); } void update(int a, int b, E x) { update(a, b, x, 0, 0, n); } T query(int k) { T c = dat[k]; k += n-1; E x = laz[k]; while (k > 0) { k = (k-1)/2; x = h(x,laz[k]); } return g(c,x); } } void main() { auto N = RD!int; auto Q = RD; long f(long a, long b) { return max(a, b); } auto st1 = new SegTree!(long, long)(N, (long a, long b){return min(a, b);}, (long a, long b){return min(a, b);}, N-1, N-1); auto st2 = new SegTree!(long, long)(N, (long a, long b){return min(a, b);}, (long a, long b){return min(a, b);}, N-1, N-1); st1.update(0, N-1, N-1); st2.update(0, N-1, N-1); long cnt; foreach (i; 0..Q) { auto num = RD; auto x = RD!int-1; if (num == 1) { auto y = st1.query(x); debug writeln("i:", " y:", y); cnt += y-1; st2.update(0, cast(int)y, x); } else { auto y = st2.query(x); debug writeln("i:", " y:", y); cnt += y-1; st1.update(0, cast(int)y, x); } } debug writeln("cnt:", cnt); long ans = (cast(long)(N-2))^^2 - cnt; writeln(ans); stdout.flush; debug readln; }
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; } int[] group(string s, string t) { int[] gs; auto n = s.length; int p = 0; while (p < s.length) { if (s[p] == t[p]) { gs ~= 1; p += 1; } else { gs ~= 0; p += 2; } } return gs; } void main() { readln; auto s = readln.chomp; auto t = readln.chomp; const MOD = 1000000007L; auto gs = group(s, t); long ans = 1; // writeln(gs); if (gs[0] == 0) ans = 6; else ans = 3; for (int i = 1; i < gs.length; i++) { int prev = gs[i - 1]; int next = gs[i]; if (prev == 0) { // 横 if (next == 0) { ans = (ans * 3) % MOD; } } else { if (next == 0) { ans = (ans * 2) % MOD; // 上下で色かえ } else if (next == 1) { ans = (ans * 2) % MOD; } } } writeln(ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto X = readln.chomp; int s, r; foreach (c; X) { if (c == 'S') { ++s; } else if (s > 0) { --s; } else { ++r; } } writeln(s + r); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { auto nq = readints(); int n = nq[0], q = nq[1]; for (int i = 0; i < q; i++) { auto xs = readints(); if (xs[0] == 0) { int s = xs[1]; int t = xs[2]; int x = xs[3]; add(s, t + 1, x, 0, 0, N / 2); } else { int s = xs[1]; int v = sum(s, s + 1, 0, 0, N / 2); writeln(v); } } } /////////////////////////////////////// const int N = 1 << 18; // ???????????° int[N * 2] _data; int[N * 2] _datb; /// [a, b) ??? x ??????????????? /// k ????????????????????§????????? [l, r) ??????????????? void add(int a, int b, int x, int k, int l, int r) { if (a <= l && r <= b) { _data[k] += x; } else if (l < b && a < r) { _datb[k] += (min(b, r) - max(a, l)) * x; add(a, b, x, k * 2 + 1, l, (l + r) / 2); add(a, b, x, k * 2 + 2, (l + r) / 2, r); } } /// [a, b) ??????????±??????? /// k ????????????????????§????????? [l, r) ???????????????] int sum(int a, int b, int k, int l, int r) { if (b <= l || r <= a) return 0; if (a <= l && r <= b) return _data[k] * (r - l) + _datb[k]; int ret = (min(b, r) - max(a, l)) * _data[k]; ret += sum(a, b, k * 2 + 1, l, (l + r) / 2); ret += sum(a, b, k * 2 + 2, (l + r) / 2, r); return ret; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto K = s[1]; auto A = readln.split.map!(to!long).array; long x, y; int cnt = 0; int first = N; int zero = -1; foreach (i; 0..N) { if (A[i] == 0) { zero = i; break; } } if (zero != -1) { A.remove(zero); N -= 1; K -= 1; } if (K == 0) { writeln(0); return; } foreach (i; 0..N) { if (A[i] >= 0) { first = i; break; } } if (first == N) { writeln(-A[N-K]); return; } int bk = max(first - K, 0); long ans = 1L << 59; if (first - K >= 0) ans = min(ans, -A[bk]); if (first + K - 1 < N) ans = min(ans, A[first + K - 1]); foreach (i; first..N) { int len = i - bk + 1; if (len < K) continue; if (len > K) bk += 1; if (A[bk] > 0) continue; long x1 = A[i] * 2 - A[bk]; long x2 = - A[bk] * 2 + A[i]; ans = min(ans, x1, x2); } ans.writeln; }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { auto S = readln.chomp; long[] Ls; long[] Rs; foreach (i; 0..S.length) { if (S[i] == 'L') Ls ~= i; else Rs ~= i; } auto Lss = assumeSorted(Ls); auto Rss = assumeSorted(Rs); auto result = new long[S.length]; foreach (i; 0..S.length) { // i番目の子供が10^100回後にどこにいるか if (S[i] == 'L') { // Rsの中でi未満のもの auto j = Rss.lowerBound(i).back; // このRまでにi-j回移動している if ((i-j) % 2 == 0) { // 残り移動回数が偶数回なら、このマスで終わる result[j]++; } else { // 奇数回なら反対側で終わる result[j+1]++; } } else { // Lsの中でiを超えるもの auto j = Lss.upperBound(i).front; // このLまでにj-i回移動している if ((j-i) % 2 == 0) { // 残り移動回数が偶数回なら、このマスで終わる result[j]++; } else { // 奇数回なら反対側で終わる result[j-1]++; } } } result.map!(to!string).join(" ").writeln; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { auto nk = readints; int n = nk[0], k = nk[1]; auto a = readints; auto dp = new int[a.length]; for (int i = 1; i < a.length; i++) { dp[i] = int.max; for (int j = max(0, i - k); j < i; j++) { dp[i] = min(dp[i], dp[j] + abs(a[j] - a[i])); } } writeln(dp[$ - 1]); }
D
void main() { problem(); } void problem() { auto N = scan!int; int countF(int n) { int c; foreach(x; 1..101) { auto tx = x^^2; if (tx > n) break; foreach(y; x..101) { auto ty = tx + y^^2 + x*y; if (ty > n) break; foreach(z; y..101) { auto t = ty + z^^2 + y*z + z*x; if (t > n) break; if (t == n) { if (x == y && y == z) { c++; continue; } if (x != y && y != z) { c += 6; continue; } c += 3; } } } } return c; } void solve() { foreach(i; 1..N+1) { countF(i).writeln; } } solve(); } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nk = readln.split.to!(int[]); auto N = nk[0]; auto K = nk[1]; auto as = readln.split.to!(long[]); long[] cs, ss; cs.length = N; ss.length = N; long s, sx; foreach (i, a; as) { s += a; if (i < K-1) continue; if (i >= K) s -= as[i-K]; cs[i-K+1] = s; } foreach (a; as) if (a > 0) sx += a; s = 0; foreach (i, a; as) { if (a > 0) s += a; if (i < K-1) continue; if (i >= K && as[i-K] > 0) s -= as[i-K]; ss[i-K+1] = sx - s; } long r; foreach (i; 0..N-K+1) r = max(r, ss[i], ss[i] + cs[i]); writeln(r); }
D
import std.stdio; import std.conv; import std.array; void main(){ string x = readln.split[0]; if (x == "3" || x == "5" || x == "7"){ writeln("YES"); }else{ writeln("NO"); } }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto n = readln.strip.to !(int); auto a = readln.splitter.map !(to !(int)).array; bool [int] b; b[a[0]] = true; foreach (i; 1..n) { if (a[i] <= a[i - 1]) { a[i] += 1; } b[a[i]] = true; } writeln (b.length); } }
D
import std.stdio; import std.string; import std.conv; int main() { string str; while((str = readln()).length != 0) { int d = str.chomp.to!int; int value = 0,s = 0; while(value != 600-d) { value += d; int line = value*value; s += line * d; } writeln(s); } return 0; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto x = RDA; bool[long] set; foreach (i; 0..n) { foreach (j; i+1..n) { set[x[j]-x[i]] = true; } } ans[ti] = set.keys.length; } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!long; if (N%2 == 1 || N <= 10) { writeln(0); } else { long r; N /= 10; while (N) { r += N; N /= 5; } writeln(r); } }
D
import std.stdio; import std.algorithm; import std.conv; import std.datetime; import std.numeric; import std.math; import std.string; string my_readln() { return chomp(readln()); } void main() {//try{ auto tokens = split(my_readln()); auto S = to!string(tokens[0]); ulong best = 999; foreach (i; 0..S.length-2) { auto num = abs(to!long(S[i..i+3]) - 753); if (num < best) best = num; } writeln(best); stdout.flush(); /*}catch (Throwable e) { writeln(e.toString()); } readln();*/ }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); auto A = aryread(); long ans; foreach (i; 0 .. 61) { long[2] cnt; foreach (a; A) cnt[(a & (1L << i)) != 0]++; long x = cnt[0] * cnt[1] % MOD; foreach (_; 0 .. i) x = (x << 1) % MOD; ans = (ans + x) % MOD; } writeln(ans); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { char[] s; scan(s); int ans = 100000; foreach (ch ; 'a' .. 'z' + 1) { ans = min(ans, solve(s, ch.to!char)); } writeln(ans); } int solve(char[] s, char ch) { int n = s.length.to!int; char[] cur = s.dup; foreach (i ; 0 .. n - 1) { bool ok = true; foreach (j ; 0 .. cur.length) { if (cur[j] != cur[0]) { ok = false; break; } } if (ok) { return i; } auto nex = new char[](n - 1 - i); foreach (j ; 0 .. n - 1 - i) { if (cur[j] == ch || cur[j + 1] == ch) { nex[j] = ch; } else { nex[j] = cur[j]; } } cur = nex; debug { writeln(cur); } } return n - 1; } int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new int[][](n, 0); foreach (i; 0 .. m) { int u, v; scan(u, v); if (is1indexed) { u--, v--; } adj[u] ~= v; if (isUndirected) { adj[v] ~= u; } } return adj; } alias Edge = Tuple!(int, "to", int, "cost"); Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new Edge[][](n, 0); foreach (i; 0 .. m) { int u, v, c; scan(u, v, c); if (is1indexed) { u--, v--; } adj[u] ~= Edge(v, c); if (isUndirected) { adj[v] ~= Edge(u, c); } } return adj; } void yes(bool b) { writeln(b ? "Yes" : "No"); } void YES(bool b) { writeln(b ? "YES" : "NO"); } T[] readArr(T)() { return readln.split.to!(T[]); } T[] readArrByLines(T)(int n) { return iota(n).map!(i => readln.chomp.to!T).array; } 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.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int a, b; readV(a, b); writeln(a*b%2 == 0 ? "No" : "Yes"); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.math; import std.range; void main() { auto S = readln.chomp.to!(char[]); auto ni = (iota('a', 'z').chain('z'.only)).find!(c => !S.canFind(c)); ni.empty ? writeln("None") : writeln(ni.front); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); auto C = new long[](N - 1); auto S = new long[](N - 1); auto F = new long[](N - 1); foreach (i; 0 .. N - 1) { scan(C[i], S[i], F[i]); } foreach (i; 0 .. N - 1) { long t; foreach (j; i .. N - 1) { long s = max(S[j], ((t + F[j] - 1) / F[j]) * F[j]); t = s + C[j]; } writeln(t); } writeln(0); }
D
import std.stdio; import std.algorithm; import std.math; import std.conv; import std.string; T readNum(T)(){ return readStr.to!T; } T[] readNums(T)(){ return readStr.split.to!(T[]); } string readStr(){ return readln.chomp; } void main(){ auto nk = readNums!ulong; auto a = readNums!ulong; a ~= a; ulong inv; foreach(i; 0 .. nk[0]){ foreach(j; i+1 .. i+nk[0]){ if(a[i] > a[j]){ if(j < nk[0]){ inv += nk[1] * (nk[1]+1) / 2; inv %= 10^^9 + 7; } else { inv += nk[1] * (nk[1]-1) / 2; inv %= 10^^9 + 7; } } } } writeln(inv); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; int[][10^^5+1] X, Y; bool[10^^5+1] XF, YF; void main() { auto N = readln.chomp.to!int; foreach (_; 0..N) { auto xy = readln.split.to!(int[]); X[xy[0]] ~= xy[1]; Y[xy[1]] ~= xy[0]; } long r; foreach (i; 1..10^^5+1) { if (XF[i]) continue; XF[i] = true; auto ss = [[1, i]]; long xp = 1, yp, e; while (!ss.empty) { auto h = ss[0]; auto x = h[0]; auto n = h[1]; ss = ss[1..$]; foreach (nxt; (x ? X[n] : Y[n])) { ++e; if (x) { if (YF[nxt]) continue; YF[nxt] = true; ss ~= [0, nxt]; ++yp; } else { if (XF[nxt]) continue; XF[nxt] = true; ss ~= [1, nxt]; ++xp; } } } r += xp * yp - e/2; } writeln(r); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto S = readln.chomp; auto T = S.to!(dchar[]); T.reverse(); S = T.to!string; auto N = S.length.to!int; auto A = new long[](2020); A[0] = 1; int tmp = 0; auto tmps = new int[](N+1); auto ten = 1; foreach (i, s; S) { tmp += ten * (s - '0'); tmp %= 2019; ten = ten * 10 % 2019; A[tmp] += 1; tmps[i+1] = tmp; } long ans = 0; foreach_reverse (i, s; S) { A[tmp] -= 1; ans += A[tmp]; tmp = tmps[i]; } ans.writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; bool[10^^5] PS; int[10^^5] BS; void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; PS[0] = true; foreach (i; 0..N) BS[i] = 1; foreach (_; 0..M) { auto xy = readln.split.to!(int[]); auto x = xy[0]-1; auto y = xy[1]-1; --BS[x]; ++BS[y]; if (PS[x]) PS[y] = true; if (BS[x] == 0) PS[x] = false; } int r; foreach (i; 0..N) if (BS[i] > 0 && PS[i]) ++r; writeln(r); }
D
import std.stdio, std.string, std.conv, std.range, std.array, std.algorithm; import std.uni, std.math, std.container, std.typecons, std.typetuple; import core.bitop, std.datetime; immutable long mod = 10^^9 + 7; immutable int inf = mod; void main(){ int n = readln.chomp.to!int; auto a = new int[](n + 1); foreach(i ; 1 .. n + 1){ readVars(a[i]); } auto ma = new int[](n + 1); ma[1 .. $] = inf; ma[0] = -1; auto mb = assumeSorted(ma); int ans; foreach(i ; 1 .. n + 1){ int j = mb.lowerBound(a[i]).length.to!int; ans = max(ans, j); mb[j] = a[i]; } writeln(ans); } void readVars(T...)(auto ref T args){ auto line = readln.split; foreach(ref arg ; args){ arg = line.front.to!(typeof(arg)); line.popFront; } if(!line.empty){ throw new Exception("args num < input num"); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto hw = readln.split.to!(int[]); auto H = hw[0]; auto W = hw[1]; char[][] ss; foreach (_; 0..H) ss ~= readln.chomp.to!(char[]); auto DP = new int[][][](2, H, W); foreach (ref dp1; DP) foreach (ref dp2; dp1) dp2[] = -1; int solve(int c, int i, int j) { auto x = c == 0 && ss[i][j] == '#' ? 1 : 0; if (i == H-1 && j == W-1) return x; if (DP[c][i][j] == -1) { auto cc = ss[i][j] == '.' ? 0 : 1; DP[c][i][j] = min( i == H-1 ? 10001 : solve(cc, i+1, j) + x, j == W-1 ? 10001 : solve(cc, i, j+1) + x ); } return DP[c][i][j]; } writeln(solve(0, 0, 0)); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string; void main() { writeln(readln.split.join.to!int % 4 == 0 ? "YES" : "NO"); }
D
// Cheese-Cracker: cheese-cracker.github.io void solve(){ auto n = scan; auto m = scan; long a = n / 3; long b = m / 3; long lefa = n % 3; long lefb = m % 3; long res = lefb * a + lefa * b + min(lefa, lefb) + (a * b * 3); writeln(res); } 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; 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
import std.stdio, std.string, std.conv; void main() { string s = readln.chomp; int K = readln.chomp.to!(int); // s.to!(char[]); char[] ans; foreach (char c; s[0..(s.length - 1)]) { if (c == 'a') { ans ~= c; } else if ('z' - c + 1 <= K) { ans ~= 'a'; K -= 'z' - c + 1; } else { ans ~= c; } } char c = s[s.length - 1]; for (int i = 0; i < K % 26; i++) { if (c == 'z') { c = 'a'; } else { c = (c + 1).to!(char); } } writeln(ans ~ c); }
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 n = readln.chomp.to!int; auto hs = readln.chomp.split.to!(int[]); int res; int thr; foreach (h; hs) { if (h >= thr) { res++; thr = h; } } res.writeln; }
D
import std; void main() { int n, m; scan(n, m); auto hs = readints; auto s = new bool[n]; foreach (_; 0..m) { int a, b; scan(a, b); a--; b--; if (hs[a] <= hs[b]) s[a] = true; if (hs[a] >= hs[b]) s[b] = true; } auto ans = s.count!"!a"; writeln(ans); } void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int;
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto S = readln.chomp.to!(wchar[]); if (S[0] != 'A') { writeln("WA"); return; } auto len = S.length; int C; foreach (i, c; S) { if (!i) continue; if (i >= 2 && i <= len - 2 && c == 'C') { ++C; } else if (c >= 'A' && c <= 'Z') { writeln("WA"); return; } } if (C != 1) { writeln("WA"); return; } writeln("AC"); }
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 mod = 10L^^9 + 7; enum inf = 10^^9; void main() { int n; scan(n); string s; scan(s); int q; scan(q); auto k = readln.split.to!(int[]); foreach (ki ; k) { auto ans = solve(n, s, ki); writeln(ans); } } long solve(int n, string s, int k) { auto mcs = new long[](n + 1); long cntm, cntc; long mc; foreach (i ; 0 .. k - 1) { if (s[i] == 'M') { cntm++; } else if (s[i] == 'C') { cntc++; mc += cntm; } } mcs[0] = mc; foreach (i ; k - 1 .. n) { if (s[i - (k - 1)] == 'M') { mc -= cntc; cntm--; } else if (s[i - (k - 1)] == 'C') { cntc--; } if (s[i] == 'M') { cntm++; } else if (s[i] == 'C') { mc += cntm; cntc++; } debug { //writeln(cntm, " ", cntc); } mcs[i - (k - 1) + 1] = mc; } debug { writeln(mcs); } foreach (i ; n - (k - 1) + 1 .. n + 1) { if (s[i-1] == 'M') { mc -= cntc; cntm--; } else if (s[i-1] == 'C') { cntc--; } mcs[i] = mc; } debug { writeln(mcs); } long ans; foreach (i ; 0 .. n) { if (s[i] == 'D') { ans += mcs[i + 1]; } } return ans; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto A = new long[][][](N); foreach (i; 0..N) { auto M = RD; foreach (j; 0..M) { auto x = RD-1; auto y = RD; A[i] ~= [x, y]; } } long ans; foreach (i; 0..2^^N) { bool ok = true; long cnt; (){ foreach (j; 0..N) { auto bit = 1L << j; if ((i & bit) == 0) continue; ++cnt; foreach (e; A[j]) { auto x = e[0]; auto y = e[1]; auto state = (i >> x) & 1; if (state != y) { ok = false; return; } } }}(); if (ok) { debug writeln("i:", i); ans.chmax(cnt); } } writeln(ans); stdout.flush; debug readln; }
D
void main() { int[] tmp = readln.split.to!(int[]); int w = tmp[0], a = tmp[1], b = tmp[2]; writeln(abs(b-a) > w ? abs(b-a) - w : 0); } 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.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() { auto N = readln.chomp.map!(ch => (ch - '0').to!int).retro.array; N ~= 0; auto L = N.length.to!int; auto dp = new int[][](L + 1, 2); fillAll(dp, inf); dp[0][0] = 0; foreach (i ; 0 .. L) { foreach (j ; 0 .. 2) { foreach (d ; 0 .. 10) { int x = N[i] + j; if (x <= d) { // 繰り下がりが発生しない chmin(dp[i + 1][0], dp[i][j] + d + d - x); } else { // x > d, 繰り下がりが発生する chmin(dp[i + 1][1], dp[i][j] + d + 10 + d - x); } } } } auto ans = dp[L][0]; 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
void main() { int n = readln.chomp.to!int; int[] s = new int[n]; foreach (i; 0 .. n) { s[i] = readln.chomp.to!int; } bool[] dp = new bool[10001]; dp[0] = true; foreach (p; s) { foreach_reverse (i, d; dp) { if (d) { dp[i+p] = true; } } } foreach_reverse (i, d; dp) { if (d && (i % 10 != 0 || i == 0)) { i.writeln; break; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // 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 S = sread(); long[3] cnt; foreach (c; S) cnt[c - 'a']++; long m = max(abs(cnt[0] - cnt[1]), abs(cnt[1] - cnt[2]), abs(cnt[0] - cnt[2])); writeln((m < 2) ? "YES" : "NO"); }
D
import std.stdio, std.array, std.conv, std.string, std.algorithm, std.math; void main() { while (true) { auto input = readln.chomp.split.map!(to!int); auto h = input[0]; auto w = input[1]; if (h == 0 && w == 0) { break; } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { write("#"); } writeln; } 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 ip = readln.chomp; writeln(ip.count("o")*100+700); }
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 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; } // fold was added in D 2.071.0. 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); } } } void main() { readln.chomp.to!(dchar[]).sort().group.all!"a[1]%2==0".pipe!(a => a?"Yes":"No").writeln; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(int a, int b, int t) { int x = t / a; return x * b; } void main() { auto xs = readints; int a = xs[0]; int b = xs[1]; int t = xs[2]; writeln(calc(a, b, t)); }
D
import std.stdio; import std.algorithm; import std.math; import std.conv; import std.string; T readNum(T)(){ return readStr.to!T; } T[] readNums(T)(){ return readStr.split.to!(T[]); } string readStr(){ return readln.chomp; } void main(){ auto nk = readNums!int; auto s = readStr; int len = 1, ret; foreach(i; 0 .. nk[0]-1){ if(s[i] == s[i+1]) ret++; else len++; } foreach(i; 0 .. nk[1]){ if(len >= 3){ len -= 2; ret += 2; } else if(len == 2){ len -= 1; ret += 1; } else { break; } } writeln(ret); }
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 K = readln.chomp.to!int; auto numbers = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]; int solve() { return numbers[K-1]; } solve().writeln; }
D