code
stringlengths
4
1.01M
language
stringclasses
2 values
void main(){ import std.stdio, std.string, std.conv, std.algorithm; auto args=readln.split.to!(int[]); if((args[2]-args[1])&1){ writeln("Borys"); }else{ writeln("Alice"); } } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } } void wr(T...)(T x){ import std.stdio; foreach(e; x) write(e, " "); writeln(); }
D
import std.algorithm; import std.conv; import std.numeric; import std.range; import std.stdio; import std.string; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto s = readln.strip; auto t = readln.strip; auto m = s.length; auto n = t.length; auto g = gcd (m, n); auto x = s[0..g]; auto r = x; while (r.length % m != 0 || r.length % n != 0) { r ~= x; } writeln ((r[0..m] == s && r[0..n] == t) ? r : "-1"); } }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n; readV(n); auto r = 0; for (auto i = 105; i <= n; i += 2) { auto f = 0; foreach (j; 1..i+1) if (i%j == 0) ++f; if (f == 8) ++r; } writeln(r); }
D
import std.stdio; import std.string; void main() { int a; scanf("%d\n", &a); auto s = readln.chomp; if (a >= 3200) s.write; else "red".write; }
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); if (b%a == 0) writeln(a+b); else writeln(b-a); }
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; void main() { auto n = readln.chomp.to!int; auto s = readln.chomp; long rc, gc, bc; foreach (i; 0..n) { if (s[i] == 'R') rc++; else if (s[i] == 'G') gc++; else bc++; } auto cnt = rc * gc * bc; foreach (int i; 0..n) { foreach (int j; i+1..n) { if (s[i] == s[j]) continue; auto k = 2*j - i; if (k >= n) continue; if (s[i] == s[k] || s[j] == s[k]) continue; cnt--; } } cnt.writeln; }
D
/+ dub.sdl: name "C" dependency "dcomp" version=">=0.6.0" +/ import std.stdio; // import dcomp.scanner; int main() { auto sc = new Scanner(stdin); long x; sc.read(x); long sm; foreach (i; 1..1000000) { sm += i; if (sm >= x) { writeln(i); break; } } return 0; } /* 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; } string[] buf; private bool succ() { while (!buf.length) { if (f.eof) return false; buf = f.readln.split; } return true; } private bool readSingle(T)(ref T x) { if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { //string or char[10] etc x = buf.front; buf.popFront; } else { static if (isStaticArray!T) { //static assert(buf.length == T.length); } x = buf.map!(to!E).array; buf.length = 0; } } else { x = buf.front.to!T; buf.popFront; } 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); } } } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); fout.writeln("1 2 3"); fout.writeln("ab cde"); fout.writeln("1.0 1.0 2.0"); fout.close; Scanner sc = new Scanner(File(fileName, "r")); int a; int[2] b; char[2] c; string d; double e; double[] f; sc.read(a, b, c, d, e, f); assert(a == 1); assert(equal(b[], [2, 3])); assert(equal(c[], "ab")); assert(equal(d, "cde")); assert(e == 1.0); assert(equal(f, [1.0, 2.0])); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; bool canReach(string s, int fm, int to) { assert(s[fm] == '.' && s[to] == '.'); int p = fm; while (p < to) { if (p + 2 <= to && s[p + 2] == '.') p += 2; else if (s[p + 1] == '.') p++; else return false; } return true; } bool canJumpOver(string s, int lo, int hi) { int n = cast(int)s.length; for (int i = lo; i <= hi; i++) { if (i + 2 >= n) break; if (s[i..i+3] == "...") return true; } return false; } bool calc(string s, int a, int b, int c, int d) { int n = cast(int)s.length; bool reachOk = canReach(s, a, c) && canReach(s, b, d); // a, b の移動は独立している if (c < d) return reachOk; // a は b を追い越す必要がある return reachOk && canJumpOver(s, b-1, d-1); } void main() { int n, a, b, c, d; scan(n, a, b, c, d); string s = read!string; writeln(calc(s, a-1, b-1, c-1, d-1) ? "Yes" : "No"); }
D
import std.stdio, std.string, std.conv, std.array, std.algorithm; void main() { auto S = readln.chomp.to!int; writeln(S/3600, ":", S % 3600 / 60, ":", S % 60); }
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 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; } 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; long[] p; for (long i = 2; i*i <= n; ++i) { if (n % i == 0) { p ~= i; p ~= n/i; } } long ans = n; foreach (e; p) { ans = gcd(ans, e); } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio; import std.string; import std.conv; int main() { while (true) { string[] s1 = readln.chomp.split; int n = s1[0].to!int; int m = s1[1].to!int; int a = s1[2].to!int; if (n == 0 && m == 0 && a == 0) break; int max = -1; int[1001][1001] N; for (int i = 0; i < 1001; i++) for (int j = 0; j < 1001; j++) N[i][j] = i; bool flag = true; for (int i = 0; i < m; i++) { string[] s2 = readln.chomp.split; int h = s2[0].to!int; int p = s2[1].to!int; int q = s2[2].to!int; N[p][h] = q; N[q][h] = p; } max = 1000; while (max) { a = N[a][max]; --max; } writeln(a); } return 0; }
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.to!(char[]); foreach (ref c; S) { auto x = c - 'A'; x = (x + N) % 26; c = ('A' + x).to!char; } writeln(S); }
D
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; int w, h, a, b; void main() { scan(h, w, a, b); auto c = ModCombo(w + h); long ans; foreach (i ; 0 .. h - a) { (ans += c.binom(b + i - 1, i) * c.binom(w + h - b - i - 2, h - i - 1) % c.mod) %= c.mod; } writeln(ans); } struct ModCombo { private { immutable long _mod; long[] _f; long[] _finv; long _powmod(long x, long y, long mod) { return y > 0 ? _powmod(x, y>>1, mod)^^2 % mod * x^^(y&1) % mod : 1L; } } @property { long mod() {return _mod;} long f(int n) {return _f[n];} long finv(int n) {return _finv[n];} } this (int N, long mod = 10^^9 + 7) { _mod = mod; _f = new long[](N + 1); _finv = new long[](N + 1); _f[0] = _finv[0] = 1L; foreach (i ; 1 .. N + 1) { _f[i] = i * _f[i - 1] % _mod; } _finv[N] = _powmod(_f[N], _mod - 2, _mod); foreach_reverse (i ; 1 .. N) { _finv[i] = (i + 1) * _finv[i + 1] % _mod; } } long perm(int n, int k) { return _f[n] * _finv[k] % _mod; } long binom(int n, int k) { return _f[n] * _finv[k] % _mod * _finv[n - k] % _mod; } long H(int n, int k) { if (n == 0 && k == 0) return 1L; return _f[n + k - 1] * _finv[k] % _mod * _finv[n - 1] % _mod; } } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.algorithm; import std.conv; import std.datetime; import std.numeric; import std.math; import std.string; string my_readln() { return chomp(readln()); } void main() {//try{ auto tokens = split(my_readln()); auto X = to!ulong(tokens[0]); if (X == 7 || X == 5 || X == 3) writeln("YES"); else writeln("NO"); stdout.flush(); /*}catch (Throwable e) { writeln(e.toString()); } readln();*/ }
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() { readln; readln.chomp.split.map!(a=>a.to!int - 1).sum.writeln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio; void main() { auto N = readln.chomp; writeln(to!char(N[0] + 1)); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { char a, b; scan(a, b); writeln(a == b ? "H" : "D"); } 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.string,std.algorithm,std.array; void main(){ auto a=readln(); auto b=readln(); auto c=readln(); char A,B,C; A=a[0]; B=b[1]; C=c[2]; write(A); write(B); write(C); 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 A = RD; auto B = RD; auto N = RD; if (N >= B) writeln(A*(B-1)/B); else writeln(A*N/B); stdout.flush; debug readln; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { auto s = sread(); uint x; foreach (i, c; s) x |= 1 << (c - 'a'); // writefln("%b", x); if (x == (1 << 26) - 1) { writeln("None"); return; } foreach (c; 'a' .. 'z' + 1) if ((x & (1 << (c - 'a'))) == 0) { writeln(cast(char) c); return; } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int N; scan(N); auto x = new int[](N); auto y = new int[](N); iota(N).each!(i => scan(x[i], y[i])); x[] -= 1; y[] -= 1; int L = 10^^5; auto adj = new int[][](2 * L, 0); foreach (i ; 0 .. N) { adj[x[i]] ~= y[i] + L; adj[y[i] + L] ~= x[i]; } auto visited = new bool[](2 * L); long ans; long cx, cy; void dfs(int v) { visited[v] = true; (v >= L ? cy : cx)++; foreach (u ; adj[v]) { if (!visited[u]) dfs(u); } } foreach (i ; 0 .. 2 * L) { cx = cy = 0; if (!visited[i]) { dfs(i); } ans += cx * cy; } ans -= N; writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { string s; while ((s = readln.strip) != "") { auto t = readln.split; bool ok = t.any !(r => r[0] == s[0] || r[1] == s[1]); writeln (ok ? "YES" : "NO"); } }
D
import std.stdio; void main() { foreach (i; 0 .. 1000) writeln("Hello World"); }
D
/* imports all std modules {{{*/ import std.algorithm, std.array, std.ascii, std.base64, std.bigint, std.bitmanip, std.compiler, std.complex, std.concurrency, std.container, std.conv, std.csv, std.datetime, std.demangle, std.encoding, std.exception, std.file, std.format, std.functional, std.getopt, std.json, std.math, std.mathspecial, std.meta, std.mmfile, std.net.curl, std.net.isemail, std.numeric, std.parallelism, std.path, std.process, std.random, std.range, std.regex, std.signals, std.socket, std.stdint, std.stdio, std.string, std.system, std.traits, std.typecons, std.uni, std.uri, std.utf, std.uuid, std.variant, std.zip, std.zlib; /*}}}*/ /+:---test 3 3 1 2 2 5 4 3 6 ---+/ /+---test 4 2 3 4 1 13 5 8 24 45 9 15 ---+/ /+---test 2 1 2 50 50 50 ---+/ void main(string[] args) { readln; const A = readln.split.map!(x => x.to!long-1).array; const B = readln.split.map!(to!long).array; const C = readln.split.map!(to!long).array; const N = A.length; long ans = B[A[0]]; foreach (i; 1..N) { ans += B[A[i]]; if (A[i-1]+1 == A[i]) { ans += C[A[i-1]]; } } ans.writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto X = readln.chomp.to!long; long x = 100; foreach (t; 1..10000) { x = (x.to!double * 1.01).to!long; if (x >= X) { writeln(t); return; } } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto A = RD; auto B = RD; auto C = RD; auto K = RD; while (A >= B) { B *= 2; --K; } while (B >= C) { C *= 2; --K; } writeln(K >= 0 ? "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.traits; import std.typecons; struct Word { string s; int last; } void main() { int[char] v = [ 'a' : 0, 'e' : 1, 'i' : 2, 'o' : 3, 'u' : 4 ]; int vowel (char c) { return v.get(c, -1); } immutable n = readln.strip.to!int; auto a = new string[n]; size_t maxlen; foreach (i; 0 .. n) { a[i] = readln.strip; maxlen = max(maxlen, a[i].length); } auto l = new int[][5 * (maxlen+1)]; foreach (i; 0 .. n) { int t, last = -1; foreach (c; a[i]) { int x = vowel (c); if (x >= 0){ ++t; last = x; } } l[5 * t + last] ~= i; } alias T = Tuple!(int, int); T[] b; T[] c; foreach (k; 1 .. maxlen+1) { int[] tail; foreach (i; 0 .. 5) { auto idx = 5 * k + i; int cur = -1; foreach (j; l[idx]) { if (cur >= 0) { b ~= tuple (cur, j); cur = -1; } else { cur = j; } } if (cur >= 0) tail ~= cur; } foreach (i; 0 .. tail.length / 2) { c ~= tuple (tail[2*i], tail[2*i+1]); } } immutable bl = b.length; immutable cl = c.length; auto m = min (bl, (bl + cl) / 2); c ~= b[m .. $]; writeln (m); foreach (i; 0 .. m) { int p12 = b[i][0], p22 = b[i][1]; int p11 = c[i][0], p21 = c[i][1]; writeln (a[p11], ' ', a[p12]); writeln (a[p21], ' ', a[p22]); } }
D
void main() { (ri < 1200 ? "ABC" : "ARC").writeln; } // =================================== import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n, m; readV(n, m); auto c = new int[](n+1); foreach (_; 0..m) { int a, b; readV(a, b); if (a == 1) c[b]++; if (b == n) c[a]++; } writeln(c.canFind(2) ? "POSSIBLE" : "IMPOSSIBLE"); }
D
import std.stdio; import std.typecons; import std.algorithm; void main() { long n, m; scanf("%ld %ld", &n, &m); Tuple!(long, long)[] ss; foreach(i;0..n) { long a, b; scanf("%ld %ld", &a, &b); ss ~= tuple(a, b); } ss.sort!((a, b) => a[0] < b[0]); long p; foreach(s; ss) { if (m <= 0) break; p += s[0] * min(m, s[1]); m -= s[1]; } write(p); }
D
import std.stdio, std.conv, std.string, std.algorithm, std.math, std.array, std.container; void main() { int[32] a = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]; a[readln.chomp.to!int-1].writeln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip, std.regex; void main() { auto N = readln.chomp.to!int; auto A = readln.chomp; foreach (i; 0..N-1) { if (A[i] == '1' && A[i+1] == '1') { writeln("No"); return; } } foreach (i; 0..N) { if (A[i] == '1') continue; bool ok1 = (i == 0) || (i > 0 && A[i-1] == '0'); bool ok2 = (i == N-1) || (i < N-1 && A[i+1] == '0'); if (ok1 && ok2) { writeln("No"); return; } } writeln("Yes"); }
D
void main(){ int p, q, r; scanf("%d %d %d", &p, &q, &r); min(p+q, p+r, q+r).writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio; void main() { int n = 0; int m = 0; scanf("%d %d", &n, &m); printf("%d %d\n", n*m, 2*(n+m)); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { long A, B; scan(A, B); long c = A - 1; c += (A <= B) ? 1 : 0; writeln(c); }
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 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; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; writeln(N / 2 - (N % 2 == 0 ? 1 : 0)); stdout.flush(); debug readln(); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n; scan(n); if (n == 0 || n == 1) { writeln(1); return; } auto f = new long[](n + 1); f[0] = f[1] = 1; foreach (i ; 2 .. n + 1) { f[i] = f[i - 1] + f[i - 2]; } writeln(f[n]); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { string s; scan(s); auto p = "keyence"; int m = p.length.to!int; foreach (i ; 0 .. m + 1) { debug { writeln(s[0..i]); writeln(s[$-(m-i)..$]); } if (s[0 .. i] == p[0 .. i] && s[$-(m-i) .. $] == p[i .. $]) { writeln("YES"); return; } } writeln("NO"); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg ; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg ; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; } long powmod(long x, long y, long mod = 1_000_000_007L) { long res = 1L; while (y > 0) { if (y & 1) { (res *= x) %= mod; } (x *= x) %= mod; y >>= 1; } return res; }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}} void main() { long n; readV(n); long[] a; readC(5, a); auto b = a.reduce!min; writeln((n+b-1)/b+4); }
D
import std.functional, std.algorithm, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv; void main() { ulong[] XY = readln.chomp.split.to!(ulong[]); ulong X = XY[0], Y = XY[1]; ulong n; while (X*2^^n <= Y) { n++; } writeln(n); }
D
import std.stdio,std.conv,std.string; void main(){int x=to!int(readln.chomp);writeln(x*x*x);}
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N, X, Y; scan(N, X, Y); X--, Y--; auto ans = new long[](N); foreach (i; 0 .. N) { foreach (j; i + 1 .. N) { long a = j - i; long b = abs(i - X) + abs(j - Y) + 1; long c = abs(j - X) + abs(i - Y) + 1; ans[min(a, b, c)]++; } } foreach (i; 1 .. N) writeln(ans[i]); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { auto x = lread(); foreach (i; x .. (x + 10 ^^ 6)) { if (func(i)) { writeln(i); return; } } } bool func(long x) { foreach (i; 2 .. x) { if (x % i == 0) { return false; } } return true; } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.stdio; import std.uni; import std.string; import core.stdc.stdio; import std.array; import std.algorithm; int main(string[] argv) { int n,k; scanf("%d", &n); int [] arr = new int[n]; for(int i = 0; i<n; i++){ int h,m; scanf("%d:%d", &h,&m); h*=60; arr[i] = h+m; } arr.sort(); int mx=0; for(int i=0;i<n-1;i++){ int dif = arr[i+1] - arr[i] - 1; if(dif>mx)mx = dif; } int dif = 1440 - arr[n-1] + arr[0] - 1; if(dif>mx)mx=dif; if(n==1){ printf("23:59\n"); } if(n!=1){ if(mx/600==0){ printf("0"); } printf("%d:", mx/60); if((mx-mx/60*60)/10 == 0){ printf("0"); } printf("%d\n",mx-mx/60*60); } return 0; }
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 x, y; readV(x, y); writeln(x+y/2); }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; import std.string; import std.uni; import std.range; void main() { auto a = readln.strip.repeat(10).join(""); if (a.indexOf(readln.strip) != -1) writeln("Yes"); else writeln("No"); }
D
void main(){ string s = readln().chomp(); foreach(i,c; s){ if( (i+1)%2 ){ if(c!='R' && c!='U' && c!='D'){ writeln("No"); return; } }else{ if(c!='L' && c!='U' && c!='D'){ writeln("No"); return; } } } writeln("Yes"); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range; const long mod = 10^^9+7; // 1要素のみの入力 T inelm(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] inln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split())ln ~= elm.to!T(); return ln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto nab = readln.split.to!(int[]); writeln(nab[0] * nab[1] > nab[2] ? nab[2] : nab[0] * nab[1]); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void main() { auto dts = readln.split.to!(int[]); auto D = dts[0]; auto T = dts[1]; auto S = dts[2]; writeln(D <= T * S ? "Yes" : "No"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto hw = readln.split.to!(int[]); auto H = hw[0]; auto W = hw[1]; auto MAP = new char[][](H, W); foreach (i; 0..H) { foreach (j, c; readln.chomp) MAP[i][j] = c; } foreach (i; 0..H) foreach (j; 0..W) if (MAP[i][j] == '#') { bool ok; if (i > 0 && MAP[i-1][j] == '#') ok = true; if (i < H-1 && MAP[i+1][j] == '#') ok = true; if (j > 0 && MAP[i][j-1] == '#') ok = true; if (j < W-1 && MAP[i][j+1] == '#') ok = true; if (!ok) { writeln("No"); return; } } writeln("Yes"); }
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.format; void main() { auto input1 = readln.chomp.to!int; int maximumAmount = 0; int totalPaymentAmount; foreach(i; 0..input1){ auto input2 = readln.chomp.to!int; if(maximumAmount <= input2){ totalPaymentAmount += maximumAmount; maximumAmount = input2; } else { totalPaymentAmount += input2; } } writeln(totalPaymentAmount + (maximumAmount/2)); }
D
import std.algorithm, std.array, std.container, std.range, std.bitmanip; import std.numeric, std.math, std.bigint, std.random, core.bitop; import std.string, std.conv, std.stdio, std.typecons; void main() { auto rd = readln.split.map!(to!int); auto w = rd[0], h = rd[1], x = rd[2], y = rd[3], r = rd[4]; writeln(x >= r && x <= w - r && y >= r && y <= h - r ? "Yes" : "No"); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1L<<60; void main() { long n, s; scan(n); scan(s); if (n == s) { writeln(n + 1); return; } if (n < s) { writeln(-1); return; } for (long b = 2; b*b <= n; b++) { if (digitSum(n, b) == s) { writeln(b); return; } } long ans = inf; for (long p = 1; p*p <= n; p++) { long q = s - p; if ((n - q) % p == 0) { long b = (n - q) / p; if (b > 1 && digitSum(n, b) == s) { ans = min(ans, b); } } } debug { if (ans < inf) { writefln("%d, %d == %d", ans, digitSum(n, ans), s); } } writeln(ans == inf ? -1 : ans); } long digitSum(long n, long b) { return n > 0 ? digitSum(n/b, b) + (n % b) : 0; } 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.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void main() { int n; readV(n); writeln((n%1000)%111 == 0 || (n/10)%111 == 0 ? "Yes" : "No"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { int solve(int n, int x, int p) { if (p == 0) return x == 0 ? 1 : 0; if (n == 0) return 0; int r; if (x >= n) r += solve(n-1, x-n, p-1); r += solve(n-1, x, p); return r; } for (;;) { auto nx = readln.split.to!(int[]); auto n = nx[0]; auto x = nx[1]; if (n == 0 && x == 0) return; writeln(solve(n, x, 3)); } }
D
import std.stdio; import std.string; import std.algorithm; import std.conv; void main() { auto arr = readln.chomp.split.map!(to!int); int result = 0; for (int i = arr[0]; i <= arr[1]; i++) { if (arr[2] % i == 0) result++; } result.writeln; }
D
import std.algorithm; import std.array; import std.ascii; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void log(A...)(A arg) { stderr.writeln(arg); } int size(T)(in T s) { return cast(int)s.length; } void main() { auto s = readln.chomp; int from = s.size, to = -1; foreach (int i, c; s) { if (c == 'A') { from = min(i, from); } else if (c == 'Z') { to = max(i, to); } } writeln(to - from + 1); }
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; bool calc(int[] a) { int x = 0; for (int i = 0; i < a.length; i++) { if (i + 1 != a[i]) { x++; } } return x <= 2; } void main() { readint; auto a = readints; writeln(calc(a) ? "YES" : "NO"); }
D
import std.stdio; void main(){ auto inputs = readln; if(inputs[2] == inputs[3] && inputs[4] == inputs[5]) writeln("Yes"); else writeln("No"); }
D
import std.stdio,std.string,std.conv,std.algorithm,std.range; void main() { auto n = readln.chomp.to!ulong; if(n==0) 1.writeln; else iota(1, n + 1).reduce!"a*b".writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } double[] rotate(double[] vec, double rad) { return [cos(rad) * vec[0] - sin(rad) * vec[1], sin(rad) * vec[0] + cos(rad) * vec[1]]; } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto r = RD; auto b = RD; auto d = RD; auto x = min(b, r); auto y = max(b, r); ans[ti] = y-x <= d * x; } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional; import std.algorithm, std.container; struct Box{ long a, b, c; } enum MOD = 1000000007L; void main() { long N = scanElem; string s1 = readln.strip; string s2 = readln.strip; long p; int i; if(s1[0]==s2[0]) { p = 3; i=1; }else{ p = 6; i=2; } for(;i < N;i++) { if(s1[i]==s2[i]) { if(s1[i-1]==s2[i-1]) { p = p*2%MOD; } }else{ if(s1[i-1]!=s2[i-1]) { p = p*3%MOD; }else{ p = p*2%MOD; } i++; } } writeln(p); } class UnionFind{ UnionFind parent = null; void merge(UnionFind a) { if(same(a)) return; a.root.parent = this.root; } UnionFind root() { if(parent is null)return this; return parent = parent.root; } bool same(UnionFind a) { return this.root == a.root; } } void scanValues(TList...)(ref TList list) { auto lit = readln.splitter; foreach (ref e; list) { e = lit.fornt.to!(typeof(e)); lit.popFront; } } T[] scanArray(T = long)() { return readln.split.to!(long[]); } void scanStructs(T)(ref T[] t, size_t n) { t.length = n; foreach (ref e; t) { auto line = readln.split; foreach (i, ref v; e.tupleof) { v = line[i].to!(typeof(v)); } } } T scanElem(T = long)() { char[] res; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { res ~= cast(char) c; c = getchar; } return res.strip.to!T; } template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } struct Factor { long n; long c; } Factor[] factors(long n) { Factor[] res; for (long i = 2; i ^^ 2 <= n; i++) { if (n % i != 0) continue; int c; while (n % i == 0) { n = n / i; c++; } res ~= Factor(i, c); } if (n != 1) res ~= Factor(n, 1); return res; } long[] primes(long n) { if(n<2)return []; auto table = new long[n+1]; long[] res; for(int i = 2;i<=n;i++) { if(table[i]==-1) continue; for(int a = i;a<table.length;a+=i) { table[a] = -1; } res ~= i; } return res; } bool isPrime(long n) { if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (long i = 3; i ^^ 2 <= n; i += 2) if (n % i == 0) return false; return true; }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; void main(){ auto x = readln.chomp.to!int; auto a = readln.chomp.to!int; auto b = readln.chomp.to!int; writeln((x - a) % b); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void main() { int a, b, c, d, e, f; readV(a, b, c, d, e, f); auto nu = 0, de = a*100; foreach (o1; 0..f/100/a+1) foreach (o2; 0..(f/100-o1*a)/b+1) { auto w = (o1*a+o2*b)*100, s = min(f-w, w/100*e); foreach (o3; 0..s/c+1) { auto o4 = (s-o3*c)/d; auto t = o3*c+o4*d; if (nu*(w+t) < de*t) { nu = t; de = w+t; } } } writeln(de, " ", nu); }
D
void main() { auto S = rs, T = rs; foreach(i; 0..S.length) { string tmp = S[1..$]; tmp ~= S[0]; if(S == T) { writeln("Yes"); return; } S = tmp; } 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
import std.stdio; import std.ascii; import std.numeric; import std.algorithm; import core.stdc.stdio; int main() { int t = readInt!int; foreach(ti; 0 .. t) { int n = readInt!int; int[] a = new int[](n); int[] even; int[] odd; foreach(ref ai; a) { ai = readInt!int; if (ai & 1) odd ~= ai; else even ~= ai; } long count = 0; foreach(i, oi; odd) { foreach(j; i + 1 .. odd.length) { if (gcd(oi, odd[j]) > 1) { count++; } } } count += even.length * (even.length - 1) / 2; count += even.length * odd.length; count.writeln; } return 0; } /* INPUT ROUTINES */ int currChar; static this() { currChar = getchar(); } char topChar() { return cast(char) currChar; } void popChar() { currChar = getchar(); } auto readInt(T)() { T num = 0; int sgn = 0; while (topChar.isWhite) popChar; while (topChar == '-' || topChar == '+') { sgn ^= (topChar == '-'); popChar; } while (topChar.isDigit) { num *= 10; num += (topChar - '0'); popChar; } if (sgn) return -num; return num; } string readString() { string res = ""; while (topChar.isWhite) popChar; while (!topChar.isWhite) { res ~= topChar; popChar; } return res; } /**MODULAR SYSTEM*/ struct Z(immutable long m) { long rep; this(long num) { rep = num; } Z!m opBinary(string operator)(Z!m rhs) { static if (operator == "+") { long result = rhs.rep + this.rep; if (result >= m) result -= m; return Z!m(result); } else static if (operator == "-") { long result = this.rep - rhs.rep; if (result < 0) result += m; return Z!m(result); } else static if (operator == "*") { long result = this.rep * rhs.rep; if (result >= m) result %= m; return Z!m(result); } else static assert(text("Operator ", operator, " not supported")); } Z!m opBinary(string operator)(long exponent) if (operator == "^^") { assert(exponent >= 0); Z!m base = this; Z!m result = 1; while (exponent) { if (exponent & 1) result = result * base; base = base * base; exponent >>= 1; } return result; } invariant { assert(rep >= 0 && rep < m); } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n, k; scan(n, k); auto mc = ModComb(n * 2); foreach (i ; 1 .. k + 1) { auto ans = mc.c(k - 1, k - i) * mc.c(n - k + 1, n - k - (i - 1)) % mod; writeln(ans); } } struct ModComb { int _N; long[] _fact, _factinv; long _mod; this(int n, long mod = 1_000_000_007L) { _N = n; _fact = new long[](_N + 1); _factinv = new long[](_N + 1); _mod = mod; _fact[0] = 1; foreach (i ; 1 .. _N + 1) { _fact[i] = (_fact[i-1] * i) % mod; } _factinv[_N] = _powmod(_fact[_N], mod - 2); foreach_reverse (i ; 0 .. _N) { _factinv[i] = (_factinv[i+1] * (i+1)) % mod; } } long c(int n, int k) { if (k < 0) return 0; return f(n) * finv(n - k) % _mod * finv(k) % _mod; } long p(int n, int r) { return f(n) * finv(n - r) % _mod; } long f(int n) { return _fact[n]; } long finv(int n) { return _factinv[n]; } long _powmod(long x, long y) { return y > 0 ? _powmod(x, y>>1)^^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); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.algorithm, std.string, std.conv, std.array, std.range, std.math; int read() { return readln.chomp.to!int; } int[] reads() { return readln.split.to!(int[]); } void main() { auto a = reads(); if (a[0] + a[1] == 15) writeln("+"); else if (a[0] * a[1] == 15) writeln("*"); else writeln("x"); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.ascii; void main() { auto x = readln.chomp.to!int; auto res = (x / 500) * 1000; res += ((x % 500) / 5) * 5; res.writeln; }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { int x, y; scan(x, y); int ans; if (1L * x * y != 0) { if (0 < x && x < y) ans = y - x; else if (0 < y && y < x) ans = x - y + 2; else if (x < y && y < 0) ans = y - x; else if (y < x && x < 0) ans = x - y + 2; else if (x < 0 && 0 < y && abs(x) < abs(y)) ans = y + x + 1; else if (x < 0 && 0 < y && abs(x) == abs(y)) ans = 1; else if (x < 0 && 0 < y && abs(x) > abs(y)) ans = -y - x + 1; else if (y < 0 && 0 < x && abs(x) < abs(y)) ans = -y - x + 1; else if (y < 0 && 0 < x && abs(x) == abs(y)) ans = 1; else if (y < 0 && 0 < x && abs(x) > abs(y)) ans = y + x + 1; } else { if (x == 0) { ans = (y > 0 ? y : abs(y) + 1); } else { ans = (x > 0 ? x + 1 : abs(x)); } } writeln(ans); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.array; import std.algorithm; import std.conv; import std.numeric; import std.string; void main() { int N = readln.chomp.to!int; long[] L = new long[N + 1]; L[0] = 2; L[1] = 1; for (int i = 2; i <= N; ++i) L[i] = L[i - 1] + L[i - 2]; L[N].writeln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string; immutable long INF = 1L << 59; long even(long n) { return n == 0 ? 2 : n % 2; } long odd(long n) { return 1 - n % 2; } void main() { auto N = readln.chomp.to!int; auto A = N.iota.map!(_ => readln.chomp.to!long).array; auto dp = new long[][](N+1, 5); foreach (i; 0..N+1) dp[i][] = INF; dp[0][0] = 0; //foreach (i; 0..N) { // dp[i+1][0] = dp[i][0] + A[i]; //} foreach (i; 0..N) { long m = dp[i][0]; dp[i+1][0] = min(dp[i+1][0], m + A[i]); m = min(m, dp[i][1]); dp[i+1][1] = min(dp[i+1][1], m + even(A[i])); m = min(m, dp[i][2]); dp[i+1][2] = min(dp[i+1][2], m + odd(A[i])); m = min(m, dp[i][3]); dp[i+1][3] = min(dp[i+1][3], m + even(A[i])); m = min(m, dp[i][4]); dp[i+1][4] = min(dp[i+1][4], m + A[i]); } dp[N].reduce!min.writeln; }
D
import std.stdio; void main() { int n, h, x; int result = 0; scanf("%d%d", &n, &h); for (int i = 0; i < n; i++) { scanf("%d", &x); result += (x > h) ? 2 : 1; } printf("%d\n", result); }
D
import std.stdio; void main() { int n; scanf("%d", &n); char[][] m = new char[][n]; for (int i = 0; i < n; i++) { m[i] = new char[n + 1]; scanf("%s", m[i].ptr); } int res = 0; for (int i = 0; i < n; i++) { int r = 0; int c = 0; for (int j = 0; j < n; j++) { if (m[j][i] == 'C') c++; if (m[i][j] == 'C') r++; } res += (r - 1) * r / 2; res += (c - 1) * c / 2; } printf("%d\n", res); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } 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 = 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 main() { auto q = RD!int; auto ans = new bool[](q); foreach (i; 0..q) { auto s = RD!string; auto t = RD!string; auto f1 = new bool[](26); auto f2 = new bool[](26); foreach (c; s) f1[c-'a'] = true; foreach (c; t) f2[c-'a'] = true; foreach (j; 0..26) { if (f1[j] && f2[j]) { ans[i] = true; break; } } } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush(); debug readln(); }
D
import std.algorithm, std.stdio, std.string; void main () { string s; while ((s = readln.strip) != "") s.count !(x => "aeiou13579".canFind (x)).writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto T = RD; long best = long.max; long ans; foreach (i; 0..N) { auto s = RD; auto d = RD; auto x = s - T; auto y = abs(x / d); if (x < 0) { x += (y+1) * d; x %= d; } if (x < best) { best = x; ans = i + 1; } debug writeln(i, " ", best, " ", ans); } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio; void main() { int n; scanf("%d", &n); printf("%lld\n", (1L << n + 1) - 1 - 1); }
D
void main() { writeln(readln.chomp.to!int ^^ 2); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } bool isPrime(int n) { if (n < 2) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (int i = 3; i * i <= n; i += 2) { if (n % i == 0) return false; } return true; } void main() { string s; while ((s = readln.chomp) != null) { int n = s.to!int; int ans = 0; for (int i = 1; i <= n; i++) { int a = i; int b = n - i + 1; if (isPrime(a) && isPrime(b)) ans++; } writeln(ans); } }
D
void main(){ int r = _scan(); (3*r^^2).writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio; import std.conv; import std.array; import std.string; void main() { string s, str = ""; while ((s = readln()).length) { str ~= s; } for (int i = 0; i < 26; i++) { int cnt; cnt += str.count('a' + i); cnt += str.count('A' + i); writeln(to!char('a' + i), " : ", cnt); } }
D
import std.stdio; import std.array; import std.conv; import std.algorithm; import std.string; import std.typecons; import std.format; int[Tuple!(long, long, long)] patterns; long answer; void solve(long a, long b, long c, long x) { auto key = tuple(a, b, c); if (key in patterns) { return; } patterns[key] = 0; if (x == 0) { answer++; } else if (x < 0) { return; } if (a > 0) { solve(a-1, b, c, x-500); } if (b > 0) { solve(a, b-1, c, x-100); } if (c > 0) { solve(a, b, c-1, x-50); } } void main(string[] argv) { long a = readln.chomp.to!long; long b = readln.chomp.to!long; long c = readln.chomp.to!long; long x = readln.chomp.to!long; answer = 0; solve(a, b, c, x); writeln(answer); }
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 readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n, m; readV(n, m); int[] a; readA(n, a); auto ca = new int[](n+1); foreach (i; 0..n) ca[i+1] = (ca[i]+a[i])%m; int[int] h; auto r = 0L; foreach_reverse (l; 0..n+1) { auto k = ca[l]; if (k in h) r += h[k]; ++h[k]; } writeln(r); }
D
import std.algorithm; import std.math; import std.range; import std.ascii; import std.array; import std.container; import std.conv; import std.numeric; import std.stdio; import std.string; import std.typecons; void log(A...)(A arg) { stderr.writeln(arg); } class UnreliableRollingHash(ulong P) { // fast but unreliable rolling hash // base is P, modulus is 2^64 ulong[] H; this(in string s) { auto N = s.length; this.H = new ulong[](N + 1); foreach (int i, c; s) { H[i + 1] = (H[i] * P + cast(int)(c - 'a' + 1)); } } ulong hash(int l, int r) { // [l, r) return H[r] - H[l] * pow(P, r - l); } } void main() { int N, M; scanf("%d %d\n", &N, &M); auto s = readln.chomp; auto hasher = new UnreliableRollingHash!991(s); int l = 0, r = 1; bool[ulong] appeared; foreach (_; 0 .. M) { auto q = readln.chomp; switch (q) { case "L++": l++; break; case "L--": l--; break; case "R++": r++; break; case "R--": r--; break; default: assert(false); } appeared[hasher.hash(l, r)] = true; } writeln(appeared.keys.length); }
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!int; auto x = n / 2; writeln(x + 1); foreach (i; 0..n) { writeln(i / 2 + 1, " ", (i+1) / 2 + 1); } //writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio; import std.algorithm; import std.conv; import std.string; int f(int n) { return n*(n+1)/2; } void main() { int n, l; scanf("%d %d", &n, &l); if (l <= 0 && l + n > 0) { write(f(l+n-1)-f(-l)); } else if (l > 0) { write(f(l+n-1)-f(l)); } else { write(-f(-l)+f(-l-n+1)); } }
D
import std.stdio; import std.range; import std.array; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.container; import std.typecons; import std.random; import std.csv; import std.regex; import std.math; import core.time; import std.ascii; import std.digest.sha; import std.outbuffer; import std.numeric; import std.bigint; void main() { int n = readln.chomp.to!int; int[] p = new int[](n); foreach (ref v; p) { v = readln.chomp.to!int; } bool[] done = new bool[](n + 1); int[] memo = new int[](n + 1); foreach (v; p) { if (done[v - 1]) { memo[v] = memo[v - 1]; } else { memo[v] = v; } done[v] = true; } int ans = 0; foreach (v; p) { ans = max(ans, v - memo[v]); } ++ans; ans = n - ans; ans.writeln; } void tie(R, Args...)(R arr, ref Args args) if (isRandomAccessRange!R || isArray!R) in { assert (arr.length == args.length); } body { foreach (i, ref v; args) { alias T = typeof(v); v = arr[i].to!T; } } void verbose(Args...)(in Args args) { stderr.write("["); foreach (i, ref v; args) { if (i) stderr.write(", "); stderr.write(v); } stderr.writeln("]"); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void pickV(R,T...)(ref R r,ref T t){foreach(ref v;t)pick(r,v);} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n; readV(n); auto a = new int[](n), op = new string[](n), rd = rdsp; pick(rd, a[0]); op[0] = "+"; foreach (i; 1..n) pickV(rd, op[i], a[i]); auto dp = new long[][](n+1, 3); foreach (ref dpi; dp) dpi[] = -10L^^17; dp[0][0] = 0; foreach (i; 0..n) { if (op[i] == "+") { auto r0 = dp[i][0]+a[i]; auto r1 = dp[i][1]-a[i]; auto r2 = dp[i][2]+a[i]; dp[i+1][0] = max(r0, r1, r2); dp[i+1][1] = max(r1, r2); dp[i+1][2] = r2; } else { auto r1 = max(dp[i][0], dp[i][2])-a[i]; auto r2 = dp[i][1]+a[i]; dp[i+1][0] = max(r1, r2); dp[i+1][1] = max(r1, r2); dp[i+1][2] = r2; } } writeln(dp[$-1][0]); }
D
import std.functional, std.algorithm, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv; bool isNumeric(char c) { return '0' <= c && c <= '9'; } void main() { int[] AB = readln.chomp.split.to!(int[]); int A = AB[0], B = AB[1]; string S = readln.chomp; foreach (i, c; S) { if (i == A) { if (!(c == '-')) { return writeln("No"); } } else { if (!isNumeric(c)) { return writeln("No"); } } } writeln("Yes"); }
D
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } void main() { auto S = cast(char[])readln.chomp; auto N = readln.chomp.to!long; void solve() { bool inverted = false; char[200000] head; char[200000] tail; long num_head; long num_tail; foreach(_; 0..N) { auto query = readln.split; if (query[0] == "2") { if ((inverted && query[1] == "2") || !inverted && query[1] == "1") { tail[num_tail] = query[2][0]; num_tail++; } if ((inverted && query[1] == "1") || !inverted && query[1] == "2") { head[num_head] = query[2][0]; num_head++; } continue; } inverted ^= true; } if (inverted) { auto reversed_head = head[0..num_head]; reversed_head.reverse(); S.reverse(); writeln( reversed_head ~ S ~ tail[0..num_tail]); } else { auto reversed_tail = tail[0..num_tail]; reversed_tail.reverse(); writeln(reversed_tail ~ S ~ head[0..num_head]); } } solve(); }
D
string vowels = ['a', 'e', 'i', 'o', 'u']; void main(){ char c = _scan!char(); if(vowels.find(c).empty)writeln("consonant"); else writeln("vowel"); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1<<30; enum full = (1<<26) - 1; void main() { string s; scan(s); int n = s.length.to!int; auto dp = new int[](n + 1); dp[n] = 1; auto next = new int[][](n + 1, 26); fillAll(next, inf); int b = 0; foreach_reverse (i ; 0 .. n) { b |= (1<<(s[i] - 'a')); if (b == full) { dp[i] = dp[i+1] + 1; b = 0; } else { dp[i] = dp[i+1]; } foreach (j ; 0 .. 26) { if (s[i] - 'a' == j) { next[i][j] = i; } else { next[i][j] = next[i+1][j]; } } } debug { writeln(dp); writeln; writefln("%(%s\n%)", next); } auto ans = new char[](dp[0]); ans[] = '#'; int pos = 0; foreach (i ; 0 .. dp[0]) { foreach (c ; 0 .. 26) { if (next[pos][c] == inf) { ans[i] = ('a' + c).to!char; break; } if (dp[pos] == dp[next[pos][c]+1] + 1) { ans[i] = ('a' + c).to!char; pos = next[pos][c]+1; break; } } } 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
#!/usr/bin/rdmd import std.stdio: writeln, readln; import std.conv: parse; void main() { auto input = readln(); auto num = parse!int(input); writeln((num^^3)); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; long f(long b, long n) { long r; while (n) { r += n%b; n /= b; } return r; } void main() { auto n = readln.chomp.to!long; auto s = readln.chomp.to!long; if (n == s) { writeln(n + 1); } else { long i = 1; for (;;) { ++i; if (i*i > n) break; if (f(i, n) == s) { writeln(i); return; } } i = 0; long min_b = long.max; for (;;) { ++i; if (i*i > n || s - i < 0) break; auto b = (n - s) / i + 1; if (b < 2) continue; if (f(b, n) == s) { min_b = min(min_b, b); } } writeln(min_b == long.max ? -1 : min_b); } }
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 core.bitop : popcnt; alias Generator = std.concurrency.Generator; enum long INF = long.max/3; enum long MOD = 10L^^9+7; void main() { string str = readln.chomp; long ans = 0; long s = 0; foreach(i, c; str) { if (c == 'W') { ans += i - s++; } } ans.writeln; } // ---------------------------------------------- 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 (t * y < x) t++; return t; } T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { T t = x / y; if (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) { import std.meta; template getFormat(T) { static if (isIntegral!T) { enum getFormat = "%d"; } else static if (isFloatingPoint!T) { enum getFormat = "%g"; } else static if (isSomeString!T || isSomeChar!T) { enum getFormat = "%s"; } else { static assert(false); } } enum string fmt = [staticMap!(getFormat, Args)].join(" "); string[] inputs = readln.chomp.split; foreach(i, ref v; args) { v = inputs[i].to!(Args[i]); } } // 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) { private auto extremum(alias map, alias selector = "a < b", Range)(Range r) if (isInputRange!Range && !isInfinite!Range && is(typeof(unaryFun!map(ElementType!(Range).init)))) in { assert(!r.empty, "r is an empty range"); } body { alias Element = ElementType!Range; Unqual!Element seed = r.front; r.popFront(); return extremum!(map, selector)(r, seed); } private auto extremum(alias map, alias selector = "a < b", Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seedElement) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void) && is(typeof(unaryFun!map(ElementType!(Range).init)))) { alias mapFun = unaryFun!map; alias selectorFun = binaryFun!selector; alias Element = ElementType!Range; alias CommonElement = CommonType!(Element, RangeElementType); Unqual!CommonElement extremeElement = seedElement; alias MapType = Unqual!(typeof(mapFun(CommonElement.init))); MapType extremeElementMapped = mapFun(extremeElement); // direct access via a random access range is faster static if (isRandomAccessRange!Range) { foreach (const i; 0 .. r.length) { MapType mapElement = mapFun(r[i]); if (selectorFun(mapElement, extremeElementMapped)) { extremeElement = r[i]; extremeElementMapped = mapElement; } } } else { while (!r.empty) { MapType mapElement = mapFun(r.front); if (selectorFun(mapElement, extremeElementMapped)) { extremeElement = r.front; extremeElementMapped = mapElement; } r.popFront(); } } return extremeElement; } private auto extremum(alias selector = "a < b", Range)(Range r) if (isInputRange!Range && !isInfinite!Range && !is(typeof(unaryFun!selector(ElementType!(Range).init)))) { alias Element = ElementType!Range; Unqual!Element seed = r.front; r.popFront(); return extremum!selector(r, seed); } private auto extremum(alias selector = "a < b", Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seedElement) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void) && !is(typeof(unaryFun!selector(ElementType!(Range).init)))) { alias Element = ElementType!Range; alias CommonElement = CommonType!(Element, RangeElementType); Unqual!CommonElement extremeElement = seedElement; alias selectorFun = binaryFun!selector; // direct access via a random access range is faster static if (isRandomAccessRange!Range) { foreach (const i; 0 .. r.length) { if (selectorFun(r[i], extremeElement)) { extremeElement = r[i]; } } } else { while (!r.empty) { if (selectorFun(r.front, extremeElement)) { extremeElement = r.front; } r.popFront(); } } return extremeElement; } auto minElement(Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { return extremum(r); } auto minElement(alias map, Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void)) { return extremum!map(r, seed); } auto minElement(Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void)) { return extremum(r, seed); } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { return extremum!(map, "a > b")(r); } auto maxElement(Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { return extremum!`a > b`(r); } auto maxElement(alias map, Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void)) { return extremum!(map, "a > b")(r, seed); } auto maxElement(Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void)) { return extremum!`a > b`(r, seed); } } // 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, 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 A = RD; auto B = RD; auto C = RD; bool ans; foreach (i; 1..101) { auto x = A * i - C; if (x % B == 0) ans = true; } writeln(ans ? "YES" : "NO"); stdout.flush(); debug readln(); }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; import std.string; import std.math; void main() { auto abc = readln.chomp.split.map!(to!int).array; long a = abc[0], b = abc[1], c = abc[2]; if (a == b && b == c && a % 2 == 0 && b % 2 == 0 && c % 2 == 0) { (-1).writeln; } else { int ans; while (a % 2 == 0 && b % 2 == 0 && c % 2 == 0) { long tmp = a / 2; a /= 2; a += c / 2; c /= 2; c += b / 2; b /= 2; b += tmp; ans++; } ans.writeln; } return; }
D
void main() { long s, w; rdVals(s, w); writeln(w < s ? "safe" : "unsafe"); } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; enum double eps = 1.0e-9; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
import 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, K; scan(N, K); auto G = new long[][](N); foreach (i; 0 .. N - 1) { long a, b; scan(a, b); a--, b--; G[a] ~= b; G[b] ~= a; } auto visited = new bool[](N); auto C = Combination_mod(K + 1); // writeln(C._fact); long dfs1(long p, long v) { if (visited[v]) return 1; visited[v] = true; long x = (p == -1) ? C(K - 1, G[v].length) : C(K - 2, (cast(long) G[v].length) - 1); // writefln("%s %s %s %s", v, G[v].length, x, C._fact[G[v].length - 1 - min(0, p)]); x *= C._fact[G[v].length - 1 - min(0, p)]; x %= MOD; foreach (w; G[v]) if (w != p) { x *= dfs1(v, w); x %= MOD; } return x; } writeln(K * dfs1(-1, 0) % MOD); } /// Number of k-combinations % m (precalculated) alias Combination_mod = Combination_modImpl!long; struct Combination_modImpl(T) { T _n, _m; T[] _fact, _factinv; this(T maxnum, T mod = 10 ^^ 9 + 7) { _n = maxnum, _m = mod, _fact = new T[](_n + 1), _factinv = new T[](_n + 1), _fact[0] = 1; foreach (i; 1 .. _n + 1) _fact[i] = _fact[i - 1] * i % _m; T powmod(T x, T n, T m) { if (n < 1) return 1; if (n & 1) { return x * powmod(x, n - 1, m) % m; } T tmp = powmod(x, n / 2, m); return tmp * tmp % m; } foreach (i; 0 .. _n + 1) _factinv[i] = powmod(_fact[i], _m - 2, _m); } T opCall(T n, T k, T dummy = 10 ^^ 9 + 7) { return n < k ? 0 : ((_fact[n] * _factinv[n - k] % _m) * _factinv[k] % _m); } }
D