code
stringlengths
4
1.01M
language
stringclasses
2 values
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 = 998244353L; void main() { int n; scan(n); auto f = new long[](n + 1); f[0] = f[1] = 1; foreach (i ; 2 .. n + 1) { f[i] = f[i-1] * i % mod; } auto rf = new long[](n + 1); rf[n] = powmod(f[n], mod - 2, mod); foreach_reverse (i ; 1 .. n) { rf[i] = rf[i+1] * (i + 1) % mod; } long comb(int n, int k) { return f[n] * rf[k] % mod * rf[n-k] % mod; } long ans = f[n]; foreach (i ; 2 .. n) { long t = comb(n, i) * (f[i] - 1 + mod) % mod * f[n - i] % mod; ans += t; ans %= mod; } writeln(ans); } long powmod(long x, long y, long mod) { return y > 0 ? powmod(x, y>>1, mod)^^2 % mod * x^^(y&1) % mod : 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); } } }
D
import std.stdio, std.conv, std.math, std.string, std.range, std.array, std.algorithm; bool checkgood(char[][] a, int shift) { size_t n(size_t x) { return x % a.length; } for (size_t i = 0; i < a.length; ++i) { for (size_t j = i+1; j < a[0].length; ++j) { if(a[n(i + shift)][n(j)] != a[n(j + shift)][n(i)]) { return false; } } } return true; } void main(){ auto buf = readln().strip().split().map!(to!int)(); immutable N = buf[0]; char[][] a; a.length = N; int ans; foreach(immutable int i; 0 .. N) { a[i] = readln().strip().map!(to!(char))().array; } for (int i = 0; i < N; ++i) { if (checkgood(a, i)) { ans ++; } } writeln(ans * N); }
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; void main() { auto n = to!int(readln().strip()); while(n > 0){ string[] s; for(int i; i < n; ++i){ s ~= readln().chomp(); } int[][] yoko = new int[][](n+1,n+1); int[][] tate = new int[][](n+1,n+1); int[][] square = new int[][](n+1,n+1); int maxSquare = 0; for(int i = n-1; i >= 0; --i) { for(int j = n-1; j >= 0; --j) { if(s[i][j] == '.') { yoko[i][j] = yoko[i][j+1]+1; tate[i][j] = tate[i+1][j]+1; square[i][j] = min(yoko[i][j],tate[i][j],square[i+1][j+1]+1); maxSquare = max(maxSquare, square[i][j]); }else{ yoko[i][j] = 0; tate[i][j] = 0; square[i][j] = 0; } } } writeln(maxSquare); n = to!int(readln().strip()); } }
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() { readint; auto a = readints; auto d = new int[8]; int redOver = 0; // (>= 3200) foreach (x; a) { if (x < 3200) d[x / 400]++; else redOver++; } int x = cast(int) d.count!(e => e > 0); int minAns = x; if (minAns == 0 && redOver > 0) minAns++; int maxAns = x + redOver; writeln(minAns, " ", maxAns); }
D
// Try Codeforces // author: Leonardone @ NEETSDKASU import std.stdio, std.string, std.conv, std.algorithm; T[] getVals(T)() { return to!(T[])(readln().chomp().split(" ")); } void main() { auto nk = getVals!(int)(); auto n = nk[0], k = nk[1]; auto xs = getVals!(int)(); auto candies = 0, present = 0; foreach (i, v; xs) { candies += v; present = min(candies, 8); candies -= present; k -= present; if (k <= 0) { writeln(i+1); return; } } writeln(-1); }
D
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.range; void main() { auto N = readln.split[0].to!int; auto s = new string[N]; foreach(i; 0..N) { s[i] = readln.split[0]; } auto bucket = new int[26][N]; foreach(i; 0..N) { foreach(c; s[i]) { bucket[i][c - 'a']++; } } int[int[26]] m; ulong ans = 0; foreach(b; bucket) { if(b in m) { ans += m[b]; } m[b]++; } ans.writeln; }
D
import std.stdio; import std.string; void main() { string s = readln.chomp; long a = s.indexOf('A'); long z = s.lastIndexOf('Z'); writeln(z - a + 1); }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { while (true) { int n; scan(n); if (n == 0) return; solve(n); } } void solve(int n) { int l=1, r=1; int v; int cnt; while (r <= n) { if (v < n) { v += r++; } else { if (v == n) cnt++; v -= l++; } } writeln(cnt); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { while (true) { int n, r; scan(n, r); if (n == 0 && r == 0) return; auto a = iota(1, n + 1).retro.array; foreach (i ; 0 .. r) { int p, c; scan(p, c); p--; a = a[p .. p + c] ~ a[0 .. p] ~ a[p+c .. $]; } writeln(a[0]); } } 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, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); } void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); } string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ foreach(_; 0 .. scan!int){ int n = scan!int, m = scan!int; long[] as = scan!long(n); min(m, as.sum).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)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto N = RD; auto K = RD; auto a = new bool[](N); foreach (i; 0..K) { auto d = RD; auto A = RDA(-1); foreach (e; A) { a[e] = true; } } long ans; foreach (e; a) { if (!e) ++ans; } writeln(ans); stdout.flush; debug readln; }
D
void main() { int[] abc = readln.split.to!(int[]); writeln(abc.count(5) == 2 && abc.count(7) == 1 ? "YES" : "NO"); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.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; void main() { string s, t; scan(s); scan(t); auto n = s.length; int ans; foreach (i; 0 .. n) { ans += s[i] != t[i]; } ans.writeln; } void scan(T...)(ref T args) { auto line = readln.split; // @suppress(dscanner.suspicious.unmodified) 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
void main() { auto X = ri, A = ri, B = ri; X -= A; (X % B).writeln; } // =================================== import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nx = readln.split.to!(int[]); auto N = nx[0]; auto X = nx[1]; int min_m = int.max; foreach (_; 0..N) { auto m = readln.chomp.to!int; X -= m; min_m = min(min_m, m); } writeln(N + X/min_m); }
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 N = data[0].to!int, M = data[1].to!int; auto D = M / N; if (D == 1) { writeln(1); return; } foreach_reverse (i; 1 .. D+1) { if (M%i == 0) { writeln(i); return; } } }
D
void main() { auto S = rs, T = rs; S ~= S; if(S.canFind(T)) writeln("Yes"); else writeln("No"); } // =================================== 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
// Vicfred // https://atcoder.jp/contests/abc175/tasks/abc175_a // implementation import std.stdio; import std.string; void main() { string S = readln.strip; if(S.count("RRR") == 1) { 3.writeln; return; } else if(S.count("RR") == 1) { 2.writeln; return; } else if(S.count("R") >= 1) { 1.writeln; return; } else { 0.writeln; } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.typecons; import std.numeric, std.math; import core.bitop; string FMT_F = "%.10f"; T RD(T = long)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; } string RDR()() { return readln.chomp; } 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 A = RD; auto B = RD; auto K = RD; long ans; foreach_reverse (i; 1..min(A, B)+1) { if (A % i == 0 && B % i == 0) { --K; //stderr.writeln(i); } if (K == 0) { ans = i; break; } } writeln(ans); stdout.flush(); }
D
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array, std.functional, std.container, std.typecons; enum MOD = 1000000007; ulong _c(ulong n, ulong k) { if(k == 0 || k == n) return 1; if(k < 0 || n < k) return 0; return (c(n-1, k) + c(n-1, k-1)) % MOD; } alias c = memoize!_c; void main() { auto input = readln.split.to!(int[]); int N = input[0], K = input[1]; ulong calc(int i) { return (c(N-K+1, i) * c(K-1, i-1)) % MOD; } foreach(i; 1..(K+1)) { calc(i).writeln; } }
D
import std.stdio; void main() { foreach(i; 1 .. 10) { foreach(j; 1 .. 10) { printf("%dx%d=%d\n",i,j,i*j); } } }
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-1)*(b-1)); }
D
/+ dub.sdl: name "C" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner, dcomp.algorithm; import std.container.rbtree; int main() { auto sc = new Scanner(stdin); bool ans; scope(exit) { if (ans) writeln("Yes"); else writeln("No"); } int n; int[] a; sc.read(n, a); int f(int x) { if (x % 2) return 0; x /= 2; if (x % 2) return 1; return 2; } int[3] c; a.each!(x => c[f(x)]++); int b = 2; foreach (ph; 0..n) { if (b == 0) { if (c[2]) { c[2]--; b = 2; continue; } return 0; } if (b == 1) { if (c[1]) { c[1]--; b = 1; continue; } if (c[2]) { c[2]--; b = 2; continue; } return 0; } if (b == 2) { if (c[0]) { c[0]--; b = 0; continue; } if (c[1]) { c[1]--; b = 1; continue; } if (c[2]) { c[2]--; b = 2; continue; } return 0; } } ans = true; 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); } } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/algorithm.d */ // module dcomp.algorithm; import std.range.primitives; import std.traits : isFloatingPoint, isIntegral; T binSearch(alias pred, T)(T l, T r) if (isIntegral!T) { while (r-l > 1) { T md = (l+r)/2; if (!pred(md)) l = md; else r = md; } return r; } T binSearch(alias pred, T)(T l, T r, int cnt = 60) if (isFloatingPoint!T) { foreach (i; 0..cnt) { T md = (l+r)/2; if (!pred(md)) l = md; else r = md; } return r; } E minimum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed) if (isInputRange!Range && !isInfinite!Range) { import std.algorithm, std.functional; return reduce!((a, b) => binaryFun!pred(a, b) ? a : b)(seed, range); } ElementType!Range minimum(alias pred = "a < b", Range)(Range range) { assert(!range.empty, "range must not empty"); auto e = range.front; range.popFront; return minimum!pred(range, e); } E maximum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed) if (isInputRange!Range && !isInfinite!Range) { import std.algorithm, std.functional; return reduce!((a, b) => binaryFun!pred(a, b) ? b : a)(seed, range); } ElementType!Range maximum(alias pred = "a < b", Range)(Range range) { assert(!range.empty, "range must not empty"); auto e = range.front; range.popFront; return maximum!pred(range, e); } Rotator!Range rotator(Range)(Range r) { return Rotator!Range(r); } struct Rotator(Range) if (isForwardRange!Range && hasLength!Range) { size_t cnt; Range start, now; this(Range r) { cnt = 0; start = r.save; now = r.save; } this(this) { start = start.save; now = now.save; } @property bool empty() { return now.empty; } @property auto front() { assert(!now.empty); import std.range : take, chain; return chain(now, start.take(cnt)); } @property Rotator!Range save() { return this; } void popFront() { cnt++; now.popFront; } }
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 B = RDA; long ans = B[0] + B[$-1]; foreach (i; 1..N-1) { ans += min(B[i], B[i-1]); } writeln(ans); stdout.flush(); debug readln(); }
D
void main() { (ri.to!real.sqrt.floor ^^ 2).to!int.writeln; } // =================================== 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.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 a = readints; int r = a[0], g = a[1], b = a[2], n = a[3]; int ans = 0; for (int i = 0; i * r <= n; i++) { for (int j = 0; j * g <= n; j++) { int m = n - (i * r + j * g); if (m < 0) break; if (m % b == 0) ans++; } } 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.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 N = RD; auto P = RDA; long ans; long x = long.max; foreach (i; 0..N) { if (P[i] <= x) ++ans; x.chmin(P[i]); } writeln(ans); stdout.flush; debug readln; }
D
// 提出解 void solve(){ foreach(_; 0 .. scan!int){ int n = scan!int, m = scan!int; int rb = scan!int - 1, cb = scan!int - 1; int rd = scan!int - 1, cd = scan!int - 1; int high = n + m; int ans = high; if(rd >= rb) ans.mini(rd - rb); else ans.mini(n - 1 - rb + n - 1 - rd); if(cd >= cb) ans.mini(cd - cb); else ans.mini(m - 1 - cb + m - 1 - cd); ans.print; } } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // // 愚直解 void jury(){ } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // // テストケース void gen(){ } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // // テンプレ import std; bool DEBUG = 0; void main(string[] args){ if(args.canFind("-debug")) DEBUG = 1; if(args.canFind("-gen")) gen; else if(args.canFind("-jury")) jury; else solve; } void log(A ...)(lazy A a){ if(DEBUG) print(a); } void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ stdout.write(t, " "), print(a); } string unsplit(T)(T xs, string d = " "){ return xs.array.to!(string[]).join(d); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } T mini(T)(ref T x, T y){ if(x > y) x = y; return x; } T maxi(T)(ref T x, T y){ if(x < y) x = y; return x; } T mid(T)(T l, T r, bool delegate(T) f){ T m = (l + r) / 2; (f(m)? l: r) = m; return f(l)? f(r - 1)? r: mid(l, r, f): l; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // // ライブラリ(基本) class UnionFind{ this(int n){ foreach(i; 0 .. n) R ~= i, K ~= [i]; } int[] R; int[][] K; void unite(int a, int b){ int ra = R[a], rb = R[b]; if(ra == rb) return; if(K[ra].length < K[rb].length) unite(b, a); else foreach(k; K[rb]) R[k] = ra, K[ra] ~= k; } int find(int a){ return R[a]; } int getSize(int a){ return K[R[a]].length.to!int; } } class Queue(T){ private T[] xs; private uint i, j; this(T[] xs = []){ this.xs = xs; j = xs.length.to!uint; } uint length(){ return j - i; } bool isEmpty(){ return j == i; } void enq(T x){ while(j + 1 >= xs.length) xs.length = xs.length * 2 + 1; xs[j ++] = x; } T deq(){ assert(i < j); return xs[i ++]; } T peek(){ assert(i < j); return xs[i]; } void flush(){ i = j; } alias empty = isEmpty, front = peek, popFront = deq, pop = deq, push = enq, top = peek; Queue opOpAssign(string op)(T x) if(op == "~"){ enq(x); return this; } T opIndex(uint li){ assert(i + li < j); return xs[i + li]; } static Queue!T opCall(T[] xs){ return new Queue!T(xs); } T[] array(){ return xs[i .. j]; } override string toString(){ return array.to!string; } } class Stack(T){ private T[] xs; private uint j; this(T[] xs = []){ this.xs = xs; j = xs.length.to!uint; } uint length(){ return j; } bool isEmpty(){ return j == 0; } void push(T x){ while(j + 1 >= xs.length) xs.length = xs.length * 2 + 1; xs[j ++] = x; } T pop(){ assert(j > 0); return xs[-- j]; } T peek(){ assert(j > 0); return xs[j - 1]; } void clear(){ j = 0; } alias empty = isEmpty, front = peek, popFront = pop, top = peek; Stack opOpAssign(string op)(T x) if(op == "~"){ push(x); return this; } T opIndex(uint li){ assert(j > 0 && j - 1 >= li); return xs[j - 1 - li]; } static Stack!T opCall(T[] xs = []){ return new Stack!T(xs); } T[] array(){ return xs[0 .. j]; } override string toString(){ return array.to!string; } } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // // ライブラリ(追加)
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; immutable long MOD = 10^^9+7; void main() { auto S = readln.chomp; if (S.canFind("ABC") || S.canFind("ACB") || S.canFind("BAC") || S.canFind("BCA") || S.canFind("CAB") || S.canFind("CBA")) { writeln("Yes"); } else { writeln("No"); } }
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 = 998244353; //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 main() { auto t = RD!int; auto ans = new int[](t); foreach (ti; 0..t) { auto s = RD!string; bool start; foreach (c; s) { if (c == '1') start = true; else if (start) ++ans[ti]; } foreach_reverse (c; s) { if (c == '1') start = false; else if (start) --ans[ti]; } } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio; import std.math; import std.algorithm; import std.array; import std.string; import std.conv; import std.range; import std.file; import std.datetime; void main() { int elemanSayısı = stdin.readln.strip.to!int; string harfler = stdin.readln.strip.to!string; int enBüyükIndeksi = 0; char enBüyükHarf = 'a'; for ( int i = 0; i < harfler.length; i++ ) { if ( harfler[i] >= enBüyükHarf ) { enBüyükIndeksi = i; enBüyükHarf = harfler[i]; } else { writeln("YES"); return writeln( enBüyükIndeksi+1, " ", i+1 ); } } writeln("NO"); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto n = RD!int; auto k1 = RD!int; auto k2 = RD!int; auto a = RDA; auto b = RDA; foreach (i; 0..k1) { if (a[i] == n) { ans[ti] = true; break; } } } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush; debug readln; }
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; void main() { auto s = readln.stripRight; int[26] c; long[26][26] d; foreach (ch; s) { int i = ch.to!int - 97; foreach (j; 0 .. 26) { d[j][i] += c[j]; } ++c[i]; } long res; foreach (i; 0 .. 26) { res = max (res, c[i]); foreach (j; 0 .. 26) res = max (res, d[i][j]); } 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.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; auto k = RD; if (n >= k) { auto rem = k - (n % k); if (rem == k) ans[ti] = 1; else ans[ti] = 2; } else { ans[ti] = (k+n-1) / n; } } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio; import std.math; void main() { int n, t; scanf("%d%d", &n, &t); printf("%.10f\n", n * pow(1.000000011, t)); }
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 bool[](t); foreach (ti; 0..t) { auto n = RD!int; auto a = RDA; long tot; foreach (i; 0..n) tot ^= a[i]; if (tot == 0) { ans[ti] = true; continue; } foreach (i; 1..n) { long x, y; long cx, cy; foreach (j; 0..i) { x ^= a[j]; if (x == tot) { ++cx; x = 0; } } foreach (j; i..n) { y ^= a[j]; if (y == tot) { ++cy; y = 0; } } if (x == 0 && y == 0 && (cx != 0 && cy != 0)) { ans[ti] = true; break; } } } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush; debug readln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n; long a, b, c, d; scan(n, a, b, c, d); b = b - a; a = 0; auto s = - (n - 1) * d; auto t = - (n - 1) * c; foreach (i ; 0 .. n) { if (s <= b && b <= t) { writeln("YES"); return; } s += c + d; t += c + d; } writeln("NO"); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nmk = readln.split.to!(int[]); auto N = nmk[0]; auto M = nmk[1]; auto K = nmk[2]; foreach (y; 0..N+1) { auto b = 2*y - N; if (b == 0) { if (N*M == 2*K) { writeln("Yes"); return; } } else { auto s = K + M*y - M*N; if (s % b != 0) continue; auto x = s / b; if (0 <= x && x <= M) { writeln("Yes"); return; } } } writeln("No"); return; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { auto ab = reads!string; auto s = ab[0] ~ ab[1]; int n = s.to!int; int x = cast(int)sqrt(1.0 * n); writeln(n == x * x ? "Yes" : "No"); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = readln.chomp.to!size_t; auto t = 1L, a = 1L; foreach (_; 0..n) { auto rd = readln.split.to!(long[]), ti = rd[0], ai = rd[1]; auto k = max((t+ti-1)/ti, (a+ai-1)/ai); t = k*ti; a = k*ai; } writeln(t+a); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.conv; import std.typecons; void main() { (rs.uniq.array.length - 1).writeln; } // =================================== T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (isBasicType!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } int ri() { return readAs!int; } string rs() { return readln.chomp; }
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() { string s; scan(s); auto n = s.length.to!int; auto dp = new long[][](n + 1, 4); dp[0][0] = 1; foreach (i ; 0 .. n) { foreach (j ; 0 .. 4) { dp[i+1][j] = s[i] == '?' ? dp[i][j]*3 % mod : dp[i][j]; } if (s[i] == 'A' || s[i] == '?') { (dp[i+1][1] += dp[i][0]) %= mod; } if (s[i] == 'B' || s[i] == '?') { (dp[i+1][2] += dp[i][1]) %= mod; } if (s[i] == 'C' || s[i] == '?') { (dp[i+1][3] += dp[i][2]) %= mod; } } debug { writeln(dp); } long ans = dp[n][3]; writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.conv, std.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 a = RDA; auto b = RDA; auto c = new long[](N); foreach (i; 0..N) { c[i] = b[i] - a[i]; } debug writeln(a); debug writeln(b); debug writeln(c); long cnt1, cnt2; foreach (i; 0..N) { if (c[i] < 0) cnt1 += c[i].abs; else cnt2 += c[i] / 2; } writeln(cnt2 >= cnt1 ? "Yes" : "No"); stdout.flush(); debug readln(); }
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 main() { int[] cs = new int[10^^5+10]; readln; readln.split.to!(int[]).each!( a => cs[a..a+3] += 1 ); cs.reduce!max.writeln; } // ---------------------------------------------- void scanln(Args...)(ref Args args) { foreach(i, ref v; args) { "%d".readf(&v); (i==args.length-1 ? "\n" : " ").readf; } // ("%d".repeat(args.length).join(" ") ~ "\n").readf(args); } void times(alias fun)(int n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { auto minElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto minimum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b < minimum) { element = a; minimum = b; } } return element; } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto maximum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b > maximum) { element = a; maximum = b; } } return element; } }
D
import std.stdio; 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() { writeln(readln.chomp.to!(char[]).map!(to!long).array.sort!"a<b".uniq.count > 1 ? "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.stdio; import std.conv; import std.string; import std.algorithm; import std.range; void main() { auto input = readln.split; auto X = input[0]; auto Y = input[1]; (X < Y ? "<" : X > Y ? ">" : "=").writeln; }
D
import std.stdio; import std.conv; import std.string; void main() { auto n = readln.chomp.to!int; string[] words = []; auto ans = "Yes"; foreach (i; 0..n) { auto word = readln.chomp; foreach (w; words) { if (w == word){ "No".writeln; return; } } words ~= word; } foreach (i; 1..n){ if (words[i - 1][$ - 1] != words[i][0]) { ans = "No"; break; } } 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.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 T = RDR.ARR; auto M = RD; auto px = new long[][](M, 2); foreach (i; 0..M) { px[i] = [RD-1, RD]; } auto base = T.sum; foreach (i; 0..M) { writeln(base - T[px[i][0]] + px[i][1]); } stdout.flush(); debug readln(); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N, R; scan(N, R); writeln((10 <= N) ? R : R + 100 * (10 - N)); }
D
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.typecons, std.range, std.container, std.math, std.algorithm.searching, std.functional,std.mathspecial; void main(){ auto as=readln.chomp.array; auto bs=readln.chomp.array; auto cs=readln.chomp.array; auto turn=0; while(true){ final switch(turn){ case 0: if(!as.any){ writeln("A");return; } turn=int(as[0]-'a'); as=as[1..$]; break; case 1: if(!bs.any){ writeln("B");return; } turn=int(bs[0]-'a'); bs=bs[1..$]; break; case 2: if(!cs.any){ writeln("C");return; } turn=int(cs[0]-'a'); cs=cs[1..$]; break; } } }
D
//dlang template---{{{ import std.stdio; import std.conv; import std.string; import std.array; import std.algorithm; import std.typecons; import std.math; import std.range; // MIT-License https://github.com/kurokoji/nephele class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } //}}} void main() { Scanner sc = new Scanner; int A, P; sc.scan(A, P); P += (A * 3); writeln(P / 2); }
D
import std.conv : to; import std.stdio; import std.string : strip; void main() { int n = to!int(strip(stdin.readln())); string sequence = strip(stdin.readln()); char last_symbol = sequence[0]; int length = 1; for (auto i = 1; i < sequence.length; ++i) { if (sequence[i] != last_symbol) { ++length; last_symbol = sequence[i]; } } stdout.write(sequence.length - length); }
D
import std.conv, std.functional, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.range, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } int N; long[] A; void main() { try { for (; ; ) { N = readInt(); A = new long[N]; foreach (i; 0 .. N) { A[i] = readLong(); } long ans; long sum; foreach (i; 0 .. N) { sum += A[i]; const tmp = min(sum / 3, A[i] / 2); ans += tmp; sum -= tmp * 3; } writeln(ans); } } catch (EOFException e) { } }
D
import std.container; import std.range; import std.algorithm; import std.array; import std.string; import std.conv; import std.stdio; import std.container; void main() { auto s = readln.chomp; long m; foreach (e;s) { m += cast(int)(e - '0'); } if (m % 9 == 0) { writeln("Yes"); } else { writeln("No"); } }
D
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } void main() { try { for (; ; ) { const numCases = readInt(); foreach (caseId; 0 .. numCases) { const N = readInt(); auto P = new int[N]; foreach (i; 0 .. N) { P[i] = readInt() - 1; } P.reverse; bool ans = true; for (int i = 0, j; i < N; i = j) { j = P[i] + 1; foreach (k; i .. j) { ans = ans && (P[i] + i == P[k] + k); } if (!ans) { break; } } writeln(ans ? "Yes" : "No"); } } } catch (EOFException e) { } }
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, a-b, a*b].reduce!max); }
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; struct Queue(T) { private { size_t cap, head, tail; T[] data; } this (size_t n) in { assert(n > 0, "The capacity of a queue must be a positive integer."); } body { cap = n + 1; data = new T[](cap); } void clear() { head = tail = 0; } bool empty() { return head == tail; } bool full() { return head == (tail + 1) % cap; } size_t length() { return head <= tail ? tail - head : cap - head + tail; } T front() in { assert(!empty, "The queue is empty."); } body { return data[head]; } void removeFront() in { assert(!empty, "The queue is empty."); } body { (++head) %= cap; } alias popFront = removeFront; void insertBack(T x) in { assert(!full, "The queue is full."); } body { data[tail++] = x; tail %= cap; } alias insert = insertBack; T[] array() { return head <= tail ? data[head .. tail].dup : data[head .. $] ~ data[0 .. tail]; } string toString() { import std.format : format; if (head <= tail) { return format("[%(%s, %)]", data[head .. tail]); } else { return format("[%(%s, %)", data[head .. $]) ~ format(", %(%s, %)]", data[0 .. tail]); } } } unittest { auto a = Queue!(int)(5); //a.front; a.insert(2); a.insert(5); a.insert(3); //a.insert(8); import std.stdio; a.removeFront; a.removeFront; a.insert(11); a.insert(7); a.insert(123); a.insert(543); a.removeFront; a.removeFront; a.removeFront; a.insert(1212); a.insert(9898); a.insert(3154); a.removeFront; a.removeFront; a.removeFront; a.removeFront; a.removeFront; } unittest { auto q = Queue!(int)(5); assert(q.empty); q.insert(3); q.insert(1); q.insert(5); q.insert(4); q.insert(8); // q = [3,1,5,4,8] auto hoge = q.array; assert(hoge == [3,1,5,4,8]); assert(q.full); q.popFront(); q.popFront(); // q = [5,4,8] hoge = q.array; assert(hoge == [5,4,8]); hoge[0] = 3; hoge[2] = 11; assert(q.front == 5); assert(q.length == 3); q.popFront(); q.popFront(); // q = [8] q.insert(11); q.insert(-8); // q = [8,11,-8] assert(q.front == 8); assert(q.length == 3); q.insert(23); q.insert(31); // q = [8,11,-8,23,31] assert(q.full); assert(q.length == 5); auto a = new int[](0); foreach (e ; q) { a ~= e; } assert(a == [8, 11, -8, 23, 31]); foreach (i ; 0 .. 5) { q.popFront(); } assert(q.length == 0); assert(q.empty); hoge = q.array; assert(hoge == []); } unittest { auto q = Queue!(string)(4); q.insert("Hello"); q.insert(","); q.insert("World"); q.insert("!"); auto hoge = q.array; assert(hoge == ["Hello", ",", "World", "!"]); assert(q.full); assert(q.length == 4); q.popFront(); q.popFront(); assert(q.front == "World"); assert(q.length == 2); q.clear(); assert(q.empty); assert(q.length == 0); q.insert("hoge"); q.insert("fuga"); q.insert("melon"); q.popFront(); q.popFront; q.popFront; q.insert("hayo"); q.insert("hoge"); q.insert("pippi"); hoge = q.array; hoge[0] = "aho"; assert(q.front == "hayo"); q.insert("huni"); q.popFront; q.popFront; q.insert("ueue"); assert(q.front == "pippi"); assert(q.length == 3); } void main() { int N, T; scan(N, T); auto a = new int[](N); auto b = new int[](N); foreach (i ; 0 .. N) { scan(a[i], b[i]); } auto dpf = new long[][](N + 1, T); foreach (i ; 1 .. N + 1) { foreach (w ; 0 .. T) { dpf[i][w] = dpf[i - 1][w]; if (w - a[i - 1] >= 0) { chmax(dpf[i][w], dpf[i - 1][w - a[i - 1]] + b[i - 1]); } } } auto dpr = new long[][](N + 1, T); foreach_reverse (i ; 0 .. N) { foreach (w ; 0 .. T) { dpr[i][w] = dpr[i + 1][w]; if (w - a[i] >= 0) { chmax(dpr[i][w], dpr[i + 1][w - a[i]] + b[i]); } } } long ans; foreach (i ; 0 .. N) { foreach (j ; 0 .. T) { chmax(ans, dpf[i][j] + dpr[i + 1][T - 1 - j] + b[i]); } } 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
//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; import std.typecons; class InputReader { private: ubyte[] p; ubyte[] buffer; size_t cur; public: this () { buffer = uninitializedArray!(ubyte[])(16<<20); p = stdin.rawRead (buffer); } final ubyte skipByte (ubyte lo) { while (true) { auto a = p[cur .. $]; auto r = a.find! (c => c >= lo); if (!r.empty) { cur += a.length - r.length; return p[cur++]; } p = stdin.rawRead (buffer); cur = 0; if (p.empty) return 0; } } final ubyte nextByte () { if (cur < p.length) { return p[cur++]; } p = stdin.rawRead (buffer); if (p.empty) return 0; cur = 1; return p[0]; } template next(T) if (isSigned!T) { final T next () { T res; ubyte b = skipByte (45); if (b == 45) { while (true) { b = nextByte (); if (b < 48 || b >= 58) { return res; } res = res * 10 - (b - 48); } } else { res = b - 48; while (true) { b = nextByte (); if (b < 48 || b >= 58) { return res; } res = res * 10 + (b - 48); } } } } template next(T) if (isUnsigned!T) { final T next () { T res = skipByte (48) - 48; while (true) { ubyte b = nextByte (); if (b < 48 || b >= 58) { break; } res = res * 10 + (b - 48); } return res; } } final T[] nextA(T) (int n) { auto a = uninitializedArray!(T[]) (n); foreach (i; 0 .. n) { a[i] = next!T; } return a; } } alias T = Tuple!(int, int); void main() { auto r = new InputReader; immutable n = r.next!int; auto p = r.nextA!int(n); p[] -= 1; auto q = new int[n]; foreach (i; 0 .. n) { q[p[i]] = i; } int m = n / 2; T[] res; int s (int i, int j) { debug stderr.writefln ("swap: %d %d\n", i + 1, j + 1); if (i == j) { return j; } debug stderr.writefln ("%d %d", 2 * abs (i - j), n); assert (2 * abs (i - j) >= n); res ~= tuple (i + 1, j + 1); swap (p[i], p[j]); q[p[i]] = i; q[p[j]] = j; return j; } immutable last = n - 1; debug stderr.writeln ("m = ", m); foreach_reverse (i; 1 .. m) { int pos = m - i; int u = q[pos]; debug stderr.writeln ("p = ", p, ", pos = ", pos, ", u = ", u); if (u != pos) { if (u < m) { u = s (u, last); } else { u = s (u, 0); u = s (0, last); } assert (u == last); s (last, pos); } pos = m + i - 1; u = q[pos]; debug stderr.writeln ("p = ", p, ", pos = ", pos, ", u = ", u); if (u != pos) { if (u >= m) { u = s (u, 0); } else { u = s (u, last); u = s (last, 0); } assert (u == 0); s (0, pos); } } if (p[0] != 0) { s (0, last); } writeln (res.length); foreach (t; res) { writeln (t[0], ' ', t[1]); } debug { stderr.writeln (p); foreach (i; 0 .. n) { assert (p[i] == i); } } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.traits; class InputReader { private: ubyte[] p; ubyte[] buffer; size_t cur; public: this () { buffer = uninitializedArray!(ubyte[])(16<<20); p = stdin.rawRead (buffer); } final ubyte skipByte (ubyte lo) { while (true) { auto a = p[cur .. $]; auto r = a.find! (c => c >= lo); if (!r.empty) { cur += a.length - r.length; return p[cur++]; } p = stdin.rawRead (buffer); cur = 0; if (p.empty) return 0; } } final ubyte nextByte () { if (cur < p.length) { return p[cur++]; } p = stdin.rawRead (buffer); if (p.empty) return 0; cur = 1; return p[0]; } template next(T) if (isUnsigned!T) { final T next () { T res = skipByte (48) - 48; while (true) { ubyte b = nextByte (); if (b < 48 || b >= 58) { break; } res = res * 10 + (b - 48); } return res; } } } void main() { auto r = new InputReader; immutable int n = r.next!uint; immutable int m = r.next!uint; auto a = uninitializedArray!(uint[][]) (n, m); auto b = uninitializedArray!(uint[][]) (n, m); foreach (i; 0 .. n) foreach (j; 0 .. m) a[i][j] = r.next!uint; foreach (i; 0 .. n) foreach (j; 0 .. m) b[i][j] = r.next!uint; bool test () { foreach (d; 0 .. n + m - 1) { uint[] X, Y; foreach (i; 0 .. n) { immutable j = d - i; if (j >= 0 && j < m) { X ~= a[i][j]; Y ~= b[i][j]; } } X.sort (); Y.sort (); debug stderr.writeln ("d = ", d); debug stderr.writeln ("X = ", X); debug stderr.writeln ("Y = ", Y); if (!equal (X, Y)) return false; } return true; } write (test () ? "YES" : "NO"); }
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; } bool isParallel(Vec2 a, Vec2 b, Vec2 c, Vec2 d) { auto ab = b - a; auto cd = d - c; return ab.cross(cd) == 0; } bool isOrthogonal(Vec2 a, Vec2 b, Vec2 c, Vec2 d) { auto ab = b - a; auto cd = d - c; return ab.dot(cd) == 0; } int calc(Vec2 a, Vec2 b, Vec2 c, Vec2 d) { if (isParallel(a, b, c, d)) return 2; if (isOrthogonal(a, b, c, d)) return 1; return 0; } void main() { int n = readint(); for (int i = 0; i < n; i++) { auto ps = readints(); auto a = Vec2(ps[0], ps[1]); auto b = Vec2(ps[2], ps[3]); auto c = Vec2(ps[4], ps[5]); auto d = Vec2(ps[6], ps[7]); writeln(calc(a, b, c, d)); } } struct Vec2 { immutable double x; immutable double y; this(double x, double y) { this.x = x; this.y = y; } Vec2 opAdd(Vec2 other) { return Vec2(this.x + other.x, this.y + other.y); } Vec2 opSub(Vec2 other) { return Vec2(this.x - other.x, this.y - other.y); } Vec2 opMul(double d) { return Vec2(this.x * d, this.y * d); } double dot(Vec2 other) { return this.x * other.x + this.y * other.y; } double cross(Vec2 other) { return this.x * other.y - other.x * this.y; } double mag() { return sqrt(magSq()); } double magSq() { return this.x * this.x + this.y * this.y; } Vec2 normalize() { auto m = mag(); if (m != 0 && m != 1) return Vec2(this.x / m, this.y / m); return this; } static double distance(Vec2 a, Vec2 b) { return (a - b).mag(); } }
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 K = RD; auto X = RD; writeln(K*500 >= X ? "Yes" : "No"); stdout.flush; debug readln; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { for (;;) { auto s = readln.chomp; if (s == "-") break; auto m = readln.chomp.to!size_t; auto hi = m.iota.map!(_ => readln.chomp.to!int); foreach (h; hi) s = s[h..$] ~ s[0..h]; writeln(s); } }
D
void main(){ _scan(); int[] a = _scanln(); int ans_bit; foreach(elm; a)ans_bit = elm|ans_bit; int cnt; while( (ans_bit&1) == 0){ cnt++; ans_bit = ans_bit >> 1; } cnt.writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.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 s = read!string; writeln("aeiou".canFind(s) ? "vowel" : "consonant"); }
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; bool[] seen, finished; int pos = -1; int[] hist; void dfs(int[][] adj, int v) { seen[v] = true; hist ~= v; foreach (nv ; adj[v]) { if (finished[nv]) continue; if (seen[nv] && !finished[nv]) { pos = nv; return; } dfs(adj, nv); if (pos != -1) return; } hist.popBack(); finished[v] = true; } void main() { int N, M; scan(N, M); auto adj = new int[][](N, 0); foreach (i ; 0 .. M) { int ai, bi; scan(ai, bi); ai--, bi--; adj[ai] ~= bi; } seen = new bool[](N); finished = new bool[](N); foreach (v ; 0 .. N) { dfs(adj, v); if (pos != -1) break; } if (pos == -1) { writeln(-1); return; } int[] cycle; while (!hist.empty) { int t = hist.back; cycle ~= t; hist.popBack(); if (t == pos) break; } cycle.reverse(); while (true) { auto ord = new int[](N); ord[] = -1; foreach (i ; 0 .. cycle.length) { ord[cycle[i]] = i.to!int; } int from = -1, to = -1; foreach (i ; 0 .. cycle.length) { foreach (nv ; adj[cycle[i]]) { if (nv != cycle[(i + 1) % cycle.length] && ord[nv] != -1) { from = i.to!int; to = ord[nv]; break; } } if (from != -1) break; } if (from == -1) break; int[] ncycle; ncycle ~= cycle[from]; int i = to; while (i != from) { ncycle ~= cycle[i]; i = (i + 1) % cycle.length.to!int; } cycle = ncycle; } writeln(cycle.length); cycle.each!(v => writeln(v + 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 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, c; scan(a, b, c); bool B = a <= c && c <= b; writeln(B ? "Yes" : "No"); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { long a, b, c; scan(a, b, c); foreach (i; 1 .. 10 ^^ 6) { if ((a * i) % b == c) { writeln("YES"); return; } } writeln("NO"); } 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
void main(){ auto SW = readLine!size_t(); if( SW[0] <= SW[1] ){ writeln("unsafe"); } else { writeln("safe"); } } import std.stdio, std.string, std.conv; import std.math, std.algorithm, std.array; import std.regex; T[] readLine( T = size_t )( string sp = " " ){ T[] ol; foreach( string elm ; readln().chomp().split(sp) ){ ol ~= elm.to!T(); } return ol; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto t = RD!int; auto ans = new string[][](t); foreach (ti; 0..t) { auto n = RD!int; auto m = RD!int; ans[ti].length = n; foreach (y; 0..n-1) { foreach (x; 0..m-1) { ans[ti][y] ~= 'W'; } ans[ti][y] ~= 'B'; } foreach (i; 0..m) { ans[ti][$-1] ~= 'B'; } } foreach (e; ans) { foreach (ee; e) writeln(ee); } stdout.flush; debug readln; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.numeric; import std.stdio; import std.string; void main() { long[] a = readln.chomp.split(" ").to!(long[]); auto n = a[0]; auto k = a[1]; auto m = n % k; if (m != 0) { writeln(1); } else { writeln(0); } }
D
void main() { string s = readln.chomp; int[string] week; week["SAT"] = 1; week["FRI"] = 2; week["THU"] = 3; week["WED"] = 4; week["TUE"] = 5; week["MON"] = 6; week["SUN"] = 7; week[s].writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio; import std.string; import std.conv; import std.algorithm; int s_n(int n) { int sum = 0; while (n != 0) { sum += n % 10; n /= 10; } return sum; } void main() { int n = readln.chomp.to!(int); writeln((n % s_n(n) == 0) ? "Yes" : "No"); }
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!(char[]); int[char] memo; foreach (c; S) { if (c !in memo) memo[c] = 0; ++memo[c]; } foreach (k, v; memo) { if (v != 2) { writeln("No"); return; } } writeln("Yes"); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } alias sread = () => readln.chomp(); void main() { auto np = aryread(); auto n = np[0]; auto p = np[1]; auto f = factorize(p); long result = 1; foreach(k,v;f) { result *= k ^^ (v/n); } result.writeln; } long[long] factorize(long 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; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto K = RD; long ans; foreach (i; 1..K+1) { foreach (j; 1..K+1) { auto tmp = gcd(i, j); foreach (k; 1..K+1) { ans += gcd(tmp, k); } } } writeln(ans); stdout.flush; debug readln; }
D
import std.stdio; import std.string; import std.conv; import std.array; import std.algorithm; import std.range; void main(){ auto A=readln.split.to!(int[]),a=A[0],b=A[1]; if(a+b<10)writeln(a+b); else writeln("error"); }
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 = 998244353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto n = RD!int; auto k = RD; auto a = RDA; auto cnt = new int[](60); foreach (i; 0..n) { int j; while (a[i] != 0) { cnt[j] += a[i] % k; a[i] /= k; ++j; } } bool ok = true; foreach (e; cnt) { if (e > 1) ok = false; } ans[ti] = ok; } foreach (e; ans) writeln(e ? "YES" : "NO"); 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; 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 calc(string[] g) { auto rows = g.length; auto cols = g[0].length; // # が存在する行、列 bool[int] mrow, mcol; for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { if (g[r][c] == '#') { mrow[r] = true; break; } } } for (int c = 0; c < cols; c++) { for (int r = 0; r < rows; r++) { if (g[r][c] == '#') { mcol[c] = true; break; } } } for (int r = 0; r < rows; r++) { if (r !in mrow) continue; for (int c = 0; c < cols; c++) { if (c in mcol) write(g[r][c]); } writeln(); } } void main() { auto hw = readints; int h = hw[0]; string[] g; for (int i = 0; i < h; i++) { g ~= read!string; } calc(g); }
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 graph = new long[long][](N); foreach (_; 0 .. M) { long l, r, d; scan(l, r, d); l--; r--; graph[l][r] = d; graph[r][l] = -d; } auto p = new long[](N); p[] = -long.min; bool ans = true; void solve(long x) { if (p[x] == long.min) p[x] = 0; foreach (k, v; graph[x]) { if (p[k] == long.min) { p[k] = p[x] + v; solve(k); } else { if (p[k] - p[x] != v) { ans = false; return; } } } } foreach (i; 0 .. N) solve(i); writeln(ans ? "Yes" : "No"); }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; 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() { readln.split.repeat(2).enumerate.map!( a => a.value[a.index..$-1+a.index] ).pipe!( a => [a.front, a.back] ).all!( a => a.front.back == a.back.front ).pipe!( a => a ? "YES" : "NO" ).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
void main() { int[] tmp = readln.split.to!(int[]); int n = tmp[0], i = tmp[1]; writeln(n - i + 1); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.container; import std.typecons;
D
import std.stdio; void main() { string s = readln; if (s[0] == '7' || s[1] == '7' || s[2] == '7') { writeln("Yes"); } else { writeln("No"); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string; void main() { auto whn = readln.split.map!(to!int); auto w = whn[0]; auto h = whn[1]; auto n = whn[2]; int t = h, b = 0, l = 0, r = w; foreach (_; 0..n) { auto xya = readln.split.map!(to!int); auto x = xya[0]; auto y = xya[1]; auto a = xya[2]; switch (a) { case 1: l = l < x ? x : l; break; case 2: r = r > x ? x : r; break; case 3: b = b < y ? y : b; break; case 4: t = t > y ? y : t; break; default: } } writeln(r <= l || t <= b ? 0 : (r-l)*(t-b)); }
D
import std.stdio; import std.string; import std.array; // split import std.conv; // to import std.math; import std.algorithm; void main() { string a = chomp(readln()); string b = chomp(readln()); int N = to!int(a); // 第0要素を整数に変換 int K = to!int(b); // 第1要素を整数に変換 int ans = 1; for(int i=0;i<N;i++){ if(ans < K) ans*=2; else ans+=K; } 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; void main() { auto n = readln.chomp.to!int; auto t = readln.chomp.split.map!(to!int); auto a = readln.chomp.split.map!(to!int); auto res1 = new int[](n); res1[0] = t[0]; foreach (i; 1..n) { if (t[i] - t[i-1]) { res1[i] = t[i]; } else { res1[i] = 1; } } auto res2 = new int[](n); res2[$-1] = a[$-1]; foreach_reverse (i; 0..n-1) { if (a[i] - a[i+1]) { res2[i] = a[i]; } else { res2[i] = 1; } } long sum = 1; foreach (i; 0..n) { auto diff = min(t[i], a[i]) - max(res1[i], res2[i]) + 1; if (diff < 0) diff = 0; sum = (sum*diff)% 1_000_000_007; } writeln(sum); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto color=new char[](n*2), a=new int[](n*2); auto pos_w=new int[](n*2), pos_b=new int[](n*2); fill(pos_w, -1); fill(pos_b, -1); foreach(i; 0..(n*2)){ rd(color[i], a[i]); a[i]--; if(color[i]=='W') pos_w[a[i]]=i; else pos_b[a[i]]=i; } auto cnt_w=new int[][](n*2, n*2), // [i, n*2)にあるj以下の数の個数(白色の) cnt_b=new int[][](n*2, n*2); for(int i=n*2-1; i>=0; i--){ for(int h=0; h<=i; h++){ if(color[i]=='W') cnt_w[h][a[i]]++; else cnt_b[h][a[i]]++; } } foreach(i; 0..(n*2)){ for(int j=1; j<(n*2); j++){ cnt_w[i][j]+=cnt_w[i][j-1]; cnt_b[i][j]+=cnt_b[i][j-1]; } } auto dp=new int[][](n+1, n+1); for(int i=0; i<=n; i++) fill(dp[i], 1_000_000_000); dp[0][0]=0; for(int i=0; i<=n; i++)for(int j=0; j<=n; j++){ // 白:0, 1, ..., i-1まで置いた;次置くのはi if(i<n){ int cost_w=0; if(i-1>=0) cost_w+=cnt_w[pos_w[i]][i-1]; if(j-1>=0) cost_w+=cnt_b[pos_w[i]][j-1]; chmin(dp[i+1][j], dp[i][j]+cost_w); } if(j<n){ int cost_b=0; if(j-1>=0) cost_b+=cnt_b[pos_b[j]][j-1]; if(i-1>=0) cost_b+=cnt_w[pos_b[j]][i-1]; chmin(dp[i][j+1], dp[i][j]+cost_b); } } writeln(dp[n][n]); } void chmin(T)(ref T l, T r){if(l>r)l=r;} void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x) e=l[i].to!(typeof(e)); }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; import std.string; import std.math; void main() { int N = readln.chomp.to!int; auto a = readln.chomp.split.map!(to!int).array; int odd; for (int i = 0; i < N; ++i) { if (a[i] % 2 == 1) { odd++; } } if (odd % 2 == 1 && N > 1) { "NO".writeln; } else { "YES".writeln; } return; }
D
import std.stdio, std.string, std.conv; import std.array, std.algorithm, std.range; void main() { foreach(s;stdin.byLine()) { string t; for(int i=0; i<s.length; ++i) if(s[i]!='@') t~=s[i]; else t~=s[i+2].repeat().take(s[i+1]-'0').array(),i+=2; writeln(t); } }
D
void main() { string s = rdStr; long k = rdElem; long idx; foreach (i, x; s) { if (x != '1') { idx = i; break; } } writeln(idx >= k ? s[0] : s[idx]); } enum long mod = 10L^^9 + 7; enum long inf = 1L << 60; enum double eps = 1.0e-9; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
void main() { int[] tmp = readln.split.to!(int[]); int x1 = tmp[0], y1 = tmp[1], x2 = tmp[2], y2 = tmp[3]; int xdiff = x2 - x1, ydiff = y2 - y1; int x3 = x2 - ydiff, y3 = y2 + xdiff; int x4 = x1 - ydiff, y4 = y1 + xdiff; writeln(x3, " ", y3, " ", x4, " ", y4); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.container; import std.typecons;
D
import std.stdio, std.conv, std.string, std.math, std.regex, std.range, std.ascii, std.algorithm; void main(){ auto S = readln.chomp.to!(char[]); int count; foreach(i; 0..4){ if(S[i] == '+') count++; else count--; } writeln(count); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "x", long, "y"); alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less); void main() { auto n = lread(); long ok = -1, ng = n; writeln(0); stdout.flush(); auto first = sread(); if (first == "Vacant") return; long p = (first == "Male" ? 0 : 1); while (abs(ok - ng) > 1) { auto mid = (ok + ng) / 2; mid.writeln(); stdout.flush(); auto s = sread(); bool check(long i, string s) { if ((i + p) % 2) return (s == "Female"); else return (s == "Male"); } if (s == "Vacant") return; if (check(mid, s)) ok = mid; else ng = mid; } } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto x = readln.chomp.split.to!(int[]); if (x[0] > 8 || x[1] > 8) { writeln(":("); } else { writeln("Yay!"); } }
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.numeric; void main() { auto s = readln.chomp; auto p = ["dreamer", "dream", "eraser", "erase"]; int i = s.length.to!int; a:while (i > 0) { foreach (e; p) { if (e.length > i) { continue; } if (s[i-e.length..i] == e) { i -= e.length; continue a; } } break a; } if (i == 0) { writeln("YES"); } else { writeln("NO"); } }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { int n, a, b; scan(n, a, b); // 両方を購読している人の最大数 int x = min(a, b); // 両方を購読している人の最小数 int y = max(0, a + b - n); writeln(x, " ", y); }
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!int; const ls = readln.split.to!(int[]); const lsum = ls.sum; writeln(ls.all!(l => l * 2 < lsum) ? "Yes" : "No"); }
D
void main() { string s = rdStr; string now; string bef; long cnt; foreach (x; s) { now ~= x; if (now != bef) { bef = now; now = ""; ++cnt; } } cnt.writeln; } T rdElem(T = long)() { //import std.stdio : readln; //import std.string : chomp; //import std.conv : to; return readln.chomp.to!T; } alias rdStr = rdElem!string; dchar[] rdDchar() { //import std.conv : to; return rdStr.to!(dchar[]); } T[] rdRow(T = long)() { //import std.stdio : readln; //import std.array : split; //import std.conv : to; return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdRow!T).array; } void wrMat(T = long)(T[][] mat) { //import std.stdio : write, writeln; foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto S = readln.chomp; int r; foreach (i; 0..1000) { auto a = i/100; auto b = (i/10)%10; auto c = i%10; int d; foreach (j; 0..N) { if (d == 0 && S[j] - '0' == a) { ++d; } else if (d == 1 && S[j] - '0' == b) { ++d; } else if (d == 2 && S[j] - '0' == c) { ++r; break; } } } writeln(r); }
D
import std.algorithm, std.array, std.conv, std.functional, std.stdio, std.string; void main() { auto b = readln.chomp.split.to!(int[]); auto arr = readln.chomp.split.to!(int[]); solve(b[1], b[2], arr).writeln; } int[] cards; int _solveX(in int y, in size_t p) { int answer = abd(y, cards[$-1]); foreach (i; 1..p) answer = solveY(cards[$-i-1], i).max(answer); return answer; } int _solveY(in int x, in size_t p) { int answer = abd(x, cards[$-1]); foreach (i; 1..p) answer = solveX(cards[$-i-1], i).min(answer); return answer; } alias solveX = memoize!_solveX; alias solveY = memoize!_solveY; auto solve(in int x, in int y, int[] a) { cards = a; return solveX(y, a.length); } auto abd(in int x, in int y) { return x < y ? y - x : x - y; }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; } enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { string A, B; scan(A); scan(B); if (A == B) { writeln("EQUAL"); return; } bool g; if (A.length > B.length) { g = true; } else if (A.length < B.length) { g = false; } else { g = A > B; } writeln(g ? "GREATER" : "LESS"); }
D