code
stringlengths
4
1.01M
language
stringclasses
2 values
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() { string s; readV(s); string t; readV(t); auto ns = s.length.to!int, nt = t.length.to!int; auto dp = new int[][](ns+1, nt+1); foreach (i; 1..ns+1) foreach (j; 1..nt+1) { dp[i][j] = max(dp[i-1][j], dp[i][j-1]); if (s[i-1] == t[j-1]) dp[i][j] = max(dp[i][j], dp[i-1][j-1]+1); } char[] r; auto i = ns, j = nt; while (i > 0 && j > 0) { if (dp[i-1][j] == dp[i][j]) --i; else if (dp[i][j-1] == dp[i][j]) --j; else { r ~= s[i-1]; --i; --j; } } r.reverse(); writeln(r); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new int[](t); foreach (ti; 0..t) { auto s = RD!string; int n = cast(int)s.length; auto a = new int[](s.length); auto cnt = new int[](10); foreach (i; 0..s.length) { a[i] = s[i] - '0'; ++cnt[a[i]]; } int best; foreach (i; 0..10) best.chmax(cnt[i]); ans[ti] = n - best; foreach (i; 0..10) { foreach (j; i+1..10) { int last = -1; int c; foreach (e; a) { if (last != e && (e == i || e == j)) { ++c; last = e; } } if (c % 2) --c; ans[ti].chmin(n - c); } } } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto rd = readln.split.to!(int[]), a = rd[0], b = rd[1], c = a+b; if (c >= 10) writeln("error"); else writeln(c); }
D
void main(){ int[][] group = [[1,3,5,7,8,10,12],[4,6,9,11],[2]]; int[] xy = inln(); int x, y; foreach(i,elm; group){ if(elm.count(xy[0]) != 0 && elm.count(xy[1]) != 0){ writeln("Yes"); return; } } writeln("No"); } 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.conv, std.string; import std.algorithm, std.array, std.container, std.typecons; import std.numeric, std.math; import core.bitop; T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T)(in string str) { return str.split.to!(T[]); } 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 mod = pow(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; } /*struct UnionFind { void init(long n) { par = new long[](n); foreach (i; 0..n) par[i] = i; } long root(long i) { return par[i] == i ? i : (par[i] = root(par[i])); } bool same(long i, long j) { return root(i) == root(j); } void unite(long i, long j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; } long[] par; }*/ struct UnionFind { void init(long n) { par = new long[](n); foreach (i; 0..n) par[i] = i; dist = new long[](n); } long root(long i) { if (par[i] == i) return i; else { auto d = distance(par[i]); par[i] = root(par[i]); dist[i] += d; return par[i]; } } bool same(long i, long j) { return root(i) == root(j); } long distance(long i) { long d; long pos = i; while (par[pos] != pos) { d += dist[pos]; pos = par[pos]; } return d; } bool unite(long i, long j, long d) { auto root_i = root(i); auto root_j = root(j); auto d_i = distance(i); auto d_j = distance(j); if (root_i == root_j) { return d_i - d_j == d; } else { par[root_i] = j; dist[root_i] = d - d_i; return true; } } long[] par; long[] dist; } void main() { auto N = RD!long; auto M = RD!long; UnionFind uf; uf.init(N); foreach (i; 0..M) { auto L = RD!long - 1; auto R = RD!long - 1; auto D = RD!long; auto r = uf.unite(R, L, D); if (!r) { writeln("No"); return; } } writeln("Yes"); stdout.flush(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto T = readln.chomp.to!int; foreach (_; 0..T) { auto N = readln.chomp.to!int; if ((N/2)%2 == 1) { writeln("NO"); continue; } int[] as, bs; int x; foreach (_i; 0..N/2) { ++x; if (x%2 == 1) { bs ~= x++; as ~= x++; } else { as ~= x++; bs ~= x++; } } writeln("YES"); writeln((as ~= bs).to!(string[]).join(" ")); } }
D
import std.stdio, std.string, std.algorithm, std.conv, std.array, std.math, std.container, std.range; void main(){ readln.split.map!(to!int).reduce!"a-b+1".writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto ad = readln.split.to!(int[]); auto A = ad[0]; auto B = ad[1]; auto C = ad[2]; auto D = ad[3]; for (;;) { C -= B; if (C <= 0) { writeln("Yes"); return; } A -= D; if (A <= 0) { writeln("No"); 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) { x.modm(y.modpow(mod - 2)); } void main() { auto N = RD!string; bool ans; foreach (i; 0..3) { if (N[i] == '7') ans = true; } writeln(ans ? "Yes" : "No"); stdout.flush; debug readln; }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void 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; readV(n); long[] a; readA(n, a); auto b = a.map!"a>0".array; foreach (i; 0..n-1) if (!b[i]) { b[i] = !b[i]; b[i+1] = !b[i+1]; } auto c = a.map!abs.array; if (b[$-1]) writeln(c.sum); else writeln(c.sum-c.reduce!min*2); }
D
void main(){ int[] a = _scanln(); writeln( a.sum()>=22? "bust": "win" ); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random; import std.typecons, std.functional, std.traits,std.concurrency; import std.algorithm, std.container; import core.bitop, core.time, core.memory; import std.bitmanip; import std.regex; enum INF = long.max/3; enum MOD = 10L^^9+7; //辞書順順列はiota(1,N),nextPermituionを使う void end(T)(T v) if(isIntegral!T||isSomeString!T||isSomeChar!T) { import core.stdc.stdlib; writeln(v); exit(0); } T[] scanArray(T = long)() { static char[] scanBuf; readln(scanBuf); return scanBuf.split.to!(T[]); } dchar scanChar() { int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } return cast(dchar)c; } T scanElem(T = long)() { import core.stdc.stdlib; static auto scanBuf = appender!(char[])([]); scanBuf.clear; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { scanBuf ~= cast(char) c; c = getchar; } return scanBuf.data.to!T; } dchar[] scanString(){ return scanElem!(dchar[]); } void main() { auto A = scanElem; auto B = scanElem; writeln((A+B+1)/2); }
D
void main() { long[] tmp = rdRow; long n = tmp[0], m = tmp[1]; auto tree = new UnionFind(n); foreach (i; 0 .. m) { long[] xyz = rdRow; long x = xyz[0] - 1, y = xyz[1] - 1, z = xyz[2]; tree.unite(x, y); } long cnt; foreach (i; 0 .. n) { if (i == tree.par[i]) ++cnt; } cnt.writeln; } struct UnionFind { long[] par; this(long n) { par = new long[n]; foreach (i; 0 .. n) { par[i] = i; } } long root(long x) { if (par[x] == x) return x; else return par[x] = root(par[x]); } bool same(long x, long y) { return root(x) == root(y); } void unite(long x, long y) { x = root(x), y = root(y); if (x == y) return; par[x] = y; } } 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).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).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; import std.string; void main() { char[] input = chomp(readln).dup; int i = 0; foreach(e; input) { if(e == '+') { i += 1; } else { i -= 1; } } writeln(i); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto u = RD; auto v = RD; auto a = RDA; bool ok; foreach (i; 1..n) { auto d = a[i] - a[i-1]; if (d.abs > 1) ok = true; } if (ok) continue; bool[long] used; foreach (i; 0..n) used[a[i]] = true; if (used.length == 1) ans[ti] = min(v * 2, u + v); else ans[ti] = min(u, v); } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; import std.typecons; import std.bigint; // import dcomp.foundation, dcomp.scanner; int main() { auto sc = new Scanner(stdin); long n, m; sc.read(n, m); long[][] a = new long[][](n, m); foreach (i; 0..n) { sc.read(a[i]); if (i && a[i-1][0] > a[i][0]) { writeln(-1); return 0; } } if (m == 1) { foreach (i; 1..n) { if (a[i-1][0] == a[i][0]) { writeln(-1); return 0; } } writeln(0); return 0; } // m >= 2 immutable INF = 10L^^9 + 10L^^7; long[][] C = new long[][](2020, 2020); C[0][0] = 1; foreach (i; 1..2020) { C[i][0] = C[i-1][0]; foreach (j; 1..2020) { C[i][j] = min(INF, C[i-1][j-1] + C[i-1][j]); } } long[] suc(long[] x, long c) { long[] y = x.dup; if (c == 0) return y; foreach (i; 1..m) { foreach (j; 0..i) { long di = i-j; long fr; if (di < 6) { fr = 1; double u = 1; foreach (k; 0..di) { fr *= c-1+di-k; u *= c-1+di-k; fr /= k+1; u /= k+1; } if (u > 1e10) fr = INF; } else { fr = C[c-1+di][di]; } y[i] += x[j] * fr; y[i] = min(y[i], INF); } if (y[i] == INF) break; } return y; } long[] co = new long[n]; foreach (i; 1..n) { if (a[i-1][0] < a[i][0]) { continue; } // a[i-1][0] == a[i][0] long b2 = a[i-1][1] + a[i-1][0] * co[i-1]; if (b2 < a[i][1]) { continue; } long c = b2 - a[i][1]; if (c % a[i][0]) { co[i] = c / a[i][0] + 1; continue; } co[i] = c / a[i][0]; long mi = min(co[i-1], co[i]); long[] x1 = suc(a[i-1], co[i-1]-mi); long[] x2 = suc(a[i], co[i]-mi); if (cmp(x1, x2) != -1) { co[i]++; } } writeln(co.sum); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; //fold(for old compiler) 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); } } } unittest { import std.stdio; auto l = [1, 2, 3, 4, 5]; assert(l.fold!"a+b"(10) == 25); } } version (X86) static if (__VERSION__ < 2071) { 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) { //string or char[10] etc //todo optimize 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) { //static 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); } } } 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])); } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File, writeln; import std.datetime; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); foreach (i; 0..1_000_000) { fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2); } fout.close; writeln("Scanner Speed Test(3*1,000,000 int)"); StopWatch sw; sw.start; Scanner sc = new Scanner(File(fileName, "r")); foreach (i; 0..500_000) { int a, b, c; sc.read(a, b, c); assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 500_000..700_000) { int[3] d; sc.read(d); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 700_000..1_000_000) { int[] d; sc.read(d); assert(d.length == 3); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } writeln(sw.peek.msecs, "ms"); }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional, std.traits; import std.algorithm, std.container; import core.stdc.stdlib; long scanState() { auto s = scanElem!string; if(s=="Male") return 0; if(s=="Female") return 1; exit(0); return 0; } void main() { long N = scanElem; writeln(0); stdout.flush; auto o = scanState; long s = 0; long l = N-1; foreach(_;0..19) { auto i = (s+l)/2; writeln(i); stdout.flush; auto res = scanState; if(((i%2)^o)==res)s=i; if(((i%2)^o)!=res)l=i; if(abs(s-l)==1) { writeln(l);stdout.flush; exit(0); } } } 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)); } } } long scanULong(){ long x; while(true){ const c = getchar; if(c<'0'||c>'9'){ break; } x = x*10+c-'0'; } return x; } T scanElem(T = long)() { char[] res; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { res ~= cast(char) c; c = getchar; } return res.strip.to!T; } template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } struct Factor { long n; long c; } Factor[] factors(long n) { Factor[] res; for (long i = 2; i ^^ 2 <= n; i++) { if (n % i != 0) continue; int c; while (n % i == 0) { n = n / i; c++; } res ~= Factor(i, c); } if (n != 1) res ~= Factor(n, 1); return res; } long[] primes(long n) { if(n<2)return []; auto table = new long[n+1]; long[] res; for(int i = 2;i<=n;i++) { if(table[i]==-1) continue; for(int a = i;a<table.length;a+=i) { table[a] = -1; } res ~= i; } return res; } bool isPrime(long n) { if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (long i = 3; i ^^ 2 <= n; i += 2) if (n % i == 0) return false; return true; }
D
import std.stdio, std.conv, std.string; void main() { int [] a; a = readln().chomp().split().to!(int[]); if(a[0] <= 8 && a[1] <= 8) writeln("Yay!"); else writeln(":("); }
D
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math; import core.stdc.stdio; // foreach, foreach_reverse, writeln void main() { int n; scanf("%d", &n); int[] x = new int[n]; foreach (i; 0..n) scanf("%d", &x[i]); int[] y = x.dup; y.sort; int l = y[n/2-1], r = y[n/2]; foreach (i; 0..n) { if (x[i] <= l) writeln(r); else writeln(l); } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); auto f = new long[](N); foreach (x; 1 .. N + 1) { foreach (y; factorize(x)) f[y]++; } auto dp = new long[][](N, 77); dp[0][1] = 1; foreach (i; 1 .. N) { foreach (j; 0 .. f[i] + 1) { foreach (k; 0 .. 77) { long cnt = k * (j + 1); dp[i][min(cnt, 76)] += dp[i - 1][k]; } } } writeln(dp[$ - 1][75]); } /// 素因数分解 long[] factorize(long x) { assert(0 < x, "x is negative"); long[] result; while ((x & 1) == 0) { x /= 2; result ~= 2; } for (long i = 3; i * i <= x; i += 2) while (x % i == 0) { x /= i; result ~= i; } if (x != 1) result ~= x; return result; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } 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); } enum MOD = (10 ^^ 9) + 7; void main() { long N = lread(); auto A = aryread(); long[] ans; ans.reserve(N); foreach_reverse (i; 0 .. N) if ((i ^ (ulong.max * (N & 1))) & 1) { ans ~= A[i]; } foreach (i; 0 .. N) if (~(i ^ (ulong.max * (N & 1))) & 1) { ans ~= A[i]; } // writeln(ans); write(ans[0]); foreach(a;ans[1..$]) write(" ",a); writeln(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto M = readln.chomp.to!int; long r, n = -1; foreach (_; 0..M) { auto dc = readln.split.to!(long[]); auto d = dc[0]; auto c = dc[1]; if (n == -1) { --c; n = d; } while (c) { if (c % 2 == 1) { ++r; n += d; if (n >= 10) { ++r; n = 1 + n%10; } } c /= 2; r += c; d *= 2; if (d >= 10) { r += c; d = 1 + d%10; } } } writeln(r); }
D
void main() { int[] tmp = readln.split.to!(int[]); int d = tmp[0], n = tmp[1]; writeln(n != 100 ? 100 ^^ d * n : 100 ^^ d * (n + 1)); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.uni;
D
import std.stdio; import std.string; import std.algorithm; void main() { auto s = readln.chomp; size_t begin, end = 1, cnt; string cache = ""; while (end < s.length) { if (cache != s[begin..end]) { ++cnt; cache = s[begin..end]; begin = end; end = end + 1; } else { ++end; } } if (cache == s[begin..end]) write(cnt); else write(cnt+1); }
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 s = readln.split.map!(to!int); auto K = s[0]; auto Q = s[1]; auto D = readln.split.map!(to!long).array; auto E = new long[](K); while (Q--) { s = readln.split.map!(to!int); auto N = s[0]; auto X = s[1].to!long; auto M = s[2].to!long; X %= M; foreach (i; 0..K) E[i] = D[i] % M; auto sm = E.sum; auto hoge = (1L * (N - 1) / K) * (K - E.count(0)) - (X + sm * ((N - 1) / K)) / M; auto fuga = (X + sm * ((N - 1) / K)) % M; auto piyo = (N - 1) % K; foreach (i; 0..piyo) { auto fuga2 = (fuga + E[i]) % M; if (fuga2 > fuga) hoge += 1; fuga = fuga2; } hoge.writeln; } }
D
// import chie template :) {{{ import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons, std.format; // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } 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() { auto cin = new Scanner; long a, b, c; cin.scan(a, b, c); writeln(c - a - b > 0 && 4 * a * b < (c - a - b) ^^ 2 ? "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; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int cmp(string a, string b) { if (a.length == b.length) { if (a == b) return 0; return a < b ? -1 : 1; } else if (a.length < b.length) { return -1; } return 1; } void main() { auto a = read!string; auto b = read!string; auto c = cmp(a, b); if (c < 0) writeln("LESS"); else if (c == 0) writeln("EQUAL"); else writeln("GREATER"); }
D
import std.stdio,std.conv, std.string; void main(){ if(readln.chomp.to!int == 1){ writeln("Hello World");} else{ auto a = readln.chomp.to!int; auto b = readln.chomp.to!int; writeln(a+b);} }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}} void main() { int h, w, n; readV(h, w, n); int[] a, b; readC(n, a, b); a[] -= 1; b[] -= 1; auto key(int a, int b) { return (a.to!long<<32)|b; } bool[long] p, q; foreach (i; 0..n) p[key(a[i], b[i])] = true; auto ans = new int[](10); foreach (i; 0..n) foreach (r; a[i]-2..a[i]+1) foreach (c; b[i]-2..b[i]+1) { if (r < 0 || r > h-3 || c < 0 || c > w-3 || key(r, c) in q) continue; q[key(r, c)] = true; auto ci = 0; foreach (cr; r..r+3) foreach (cc; c..c+3) if (key(cr, cc) in p) ++ci; ++ans[ci]; } auto ans0 = (h-2).to!long*(w-2)-ans.sum; writeln(ans0); foreach (i; 1..10) writeln(ans[i]); }
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.split.map!(to!int); auto H = s[0]; auto W = s[1]; auto A = H.iota.map!(_ => readln.chomp).array; auto dp = new long[][](H, W); dp[0][0] = 1; foreach (i; 0..H) { foreach (j; 0..W) { if (A[i][j] == '#') continue; foreach (k; 0..2) { int ni = i + k; int nj = j + 1 - k; if (ni >= H || nj >= W || A[ni][nj] == '#') continue; (dp[ni][nj] += dp[i][j]) %= MOD; } } } dp[H-1][W-1].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 T = RD!int; auto ans = new string[](T); foreach (ti; 0..T) { auto t = RD!string; bool[char] set; foreach (c; t) { set[c] = true; } if (set.keys.length == 1) { ans[ti] = t; } else { char last; foreach (i; 0..t.length) { if (t[i] == last) { if (t[i] == '0') ans[ti] ~= "10"; else ans[ti] ~= "01"; } else { ans[ti] ~= t[i]; } last = t[i]; } } } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio; import std.string; import std.conv; void main(){ int n = stdin.readln.chomp.to!int; int money = 100_000; while(n--){ money = cast(int)(money * 1.05); if(money % 1_000) money = money - (money % 1_000) + 1_000; } writeln(money); }
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 t = RD!int; auto ans = new size_t[2][][](t); foreach (i; 0..t) { auto n = RD!int; auto K = RD!int; auto s = RD!(char[]); int cnt = n/2 - K; foreach (j; 0..cnt+1) { if (s[j] == '(') continue; foreach (k; j+1..n) { if (s[k] == ')') continue; auto d = k - j; ans[i] ~= [j, k]; foreach (l; j..j+(d+1)/2) { auto r = k-(l-j); swap(s[l], s[r]); } break; } } foreach (j; cnt+1..(cnt+1)*2) { if (s[j] == ')') continue; foreach (k; j+1..n) { if (s[k] == '(') continue; auto d = k - j; ans[i] ~= [j, k]; foreach (l; j..j+(d+1)/2) { auto r = k-(l-j); swap(s[l], s[r]); } break; } } foreach (j; (cnt+1)*2..n) { auto c = j % 2 == 0 ? '(' : ')'; if (s[j] == c) continue; foreach (k; j+1..n) { if (s[k] != c) continue; auto d = k - j; ans[i] ~= [j, k]; foreach (l; j..j+(d+1)/2) { auto r = k-(l-j); swap(s[l], s[r]); } break; } } } foreach (e; ans) { writeln(e.length); foreach (ee; e) writeln(ee[0]+1, " ", ee[1]+1); } 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; scan(n); auto a = readln.split.to!(int[]); auto ans = a[0]; foreach (i ; 1 .. n) { ans = gcd(ans, a[i]); } 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.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 A = new int[N]; foreach (i; 0 .. N) { A[i] = readInt(); } writeln((A[0] < A[N - 1]) ? "YES" : "NO"); } } } catch (EOFException e) { } }
D
import std.algorithm; import std.array; import std.ascii; import std.bigint; import std.complex; import std.container; import std.conv; import std.functional; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } void readlnTo(T...)(ref T t) { auto s = readln().split(); assert(s.length == t.length); foreach(ref ti; t) { ti = s[0].to!(typeof(ti)); s = s[1..$]; } } const real eps = 1e-10; const long p = 1_000_000_000 + 7; void main(){ long N, T; readlnTo(N, T); auto t = readLongs(); long ans = 0; foreach(i; iota(N-1)) { ans += min(t[i+1]-t[i], T); } ans += T; writeln(ans); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto u = s[1] - 1; auto v = s[2] - 1; auto G = new int[][](N); foreach (i; 0..N-1) { s = readln.split.map!(to!int); G[s[0]-1] ~= s[1]-1; G[s[1]-1] ~= s[0]-1; } auto d = new int[](N); auto b = new bool[](N); auto dd = new int[](N); void dfs3(int n, int p, int v) { dd[n] = v; foreach (m; G[n]) if (m != p) { dfs3(m, n, v+1); } } bool dfs2(int n, int p) { if (n == u) return b[n] = true; foreach (m; G[n]) if (p != m) { if (dfs2(m, n)) return b[n] = true; } return false; } int dfs(int n, int p) { foreach (m; G[n]) if (m != p) { if (p == -1 && !b[m] ) continue; d[n] = max(d[n], dfs(m, n) + 1); } return d[n]; } dfs3(v, -1, 0); dfs2(v, -1); dfs(v, -1); int dist = dd[u] / 2 - (1 - dd[u] % 2); int ans = 0; foreach (i; 0..N) if (b[i] && dd[u] - dd[i] <= dist) { int turn = (dd[u] - dd[i]); int hoge = (dd[i] - turn) % 2; int val = d[i] + dd[u] - dd[i]; val = hoge ? val : val + 1; ans = max(ans, val); } ans.writeln; }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { const tmp = readln.split.to!(long[]); const a = tmp[0], b = tmp[1], c = tmp[2], d = tmp[3]; writeln(abs(a-c) <= d || (abs(a-b) <= d && abs(b-c) <= d) ? "Yes" : "No"); }
D
import std.stdio; import std.algorithm; import std.string; import std.range; string[dchar] code1; dchar[string] code2; void main() { code1[' '] = "101"; code1['\''] = "000000"; code1[','] = "000011"; code1['-'] = "10010001"; code1['.'] = "010001"; code1['?'] = "000001"; code1['A'] = "100101"; code1['B'] = "10011010"; code1['C'] = "0101"; code1['D'] = "0001"; code1['E'] = "110"; code1['F'] = "01001"; code1['G'] = "10011011"; code1['H'] = "010000"; code1['I'] = "0111"; code1['J'] = "10011000"; code1['K'] = "0110"; code1['L'] = "00100"; code1['M'] = "10011001"; code1['N'] = "10011110"; code1['O'] = "00101"; code1['P'] = "111"; code1['Q'] = "10011111"; code1['R'] = "1000"; code1['S'] = "00110"; code1['T'] = "00111"; code1['U'] = "10011100"; code1['V'] = "10011101"; code1['W'] = "000010"; code1['X'] = "10010010"; code1['Y'] = "10010011"; code1['Z'] = "10010000"; code2["00000"] = 'A'; code2["00001"] = 'B'; code2["00010"] = 'C'; code2["00011"] = 'D'; code2["00100"] = 'E'; code2["00101"] = 'F'; code2["00110"] = 'G'; code2["00111"] = 'H'; code2["01000"] = 'I'; code2["01001"] = 'J'; code2["01010"] = 'K'; code2["01011"] = 'L'; code2["01100"] = 'M'; code2["01101"] = 'N'; code2["01110"] = 'O'; code2["01111"] = 'P'; code2["10000"] = 'Q'; code2["10001"] = 'R'; code2["10010"] = 'S'; code2["10011"] = 'T'; code2["10100"] = 'U'; code2["10101"] = 'V'; code2["10110"] = 'W'; code2["10111"] = 'X'; code2["11000"] = 'Y'; code2["11001"] = 'Z'; code2["11010"] = ' '; code2["11011"] = '.'; code2["11100"] = ','; code2["11101"] = '-'; code2["11110"] = '\''; code2["11111"] = '?'; while(!stdin.eof()) { auto s1 = readln().chomp(); if(!s1.empty()) { auto s2 = map!(a => code1[a])(s1).join(""); while(!s2.empty()) { while(s2.length < 5) s2 = s2 ~ '0'; write(code2[s2[0..5]]); s2 = s2.drop(5); } writeln(); } } }
D
import std.stdio, std.string, std.conv; import std.array, std.algorithm, std.range; void main() { foreach(_;0..readln().chomp().to!int()) { bool f=true; auto a=int.min, b=a; foreach(v;readln().split().map!(to!int)) if(v>a) a=v; else if(v>b) b=v; else{ f=false; break; } writeln(f?"YES":"NO"); } }
D
void main(){ string[] st = _scanln!string(); writeln(st[1], st[0]); } 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, std.range, std.algorithm; void main() { auto S = readln.split.to!(int[]); auto N = S[0], Ma = S[1], Mb = S[2]; auto data = new int[3][N]; foreach (i; 0..N) { data[i] = readln.split.to!(int[]); } auto dp = new int[401][401][41]; foreach (i; 0..41) { foreach (j; 0..401) { foreach (k; 0..401) { dp[i][j][k] = 114514; } } } dp[0][0][0] = 0; foreach (i; 1..N+1) { auto a = data[i-1][0]; auto b = data[i-1][1]; auto c = data[i-1][2]; foreach (ca; 0..401) { foreach (cb; 0..401) { if (dp[i-1][ca][cb] == 114514) continue; dp[i][ca][cb] = min(dp[i-1][ca][cb], dp[i][ca][cb]); dp[i][ca+a][cb+b] = min(dp[i-1][ca][cb] + c, dp[i][ca+a][cb+b]); } } } auto res = 114514; foreach (ca; 1..401) { foreach (cb; 1..401) { if (ca * Mb == cb * Ma) res = min(res, dp[N][ca][cb]); } } if (res == 114514) res = -1; writeln(res); }
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"; 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 A = RDR.ARR; auto cnt = new long[](N); foreach (e; A) { ++cnt[e]; } auto odds = N % 2; bool isWrong = false; foreach (i, e; cnt) { if (i == 0) { if (odds == 1 && e == 1) continue; else if (odds == 0 && e == 0) continue; isWrong = true; break; } else if (i % 2 == odds && e != 0) { isWrong = true; break; } else if (i % 2 != odds && e != 2) { isWrong = true; break; } } if (isWrong) writeln(0); else { long ans = 1; foreach (i; 0..N/2) modm(ans, 2); writeln(ans); } stdout.flush(); //readln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } 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 S = RD!string; bool ans = true; { long l, r = S.length / 2 - 1; while (l < r) { if (S[l] != S[r]) ans = false; ++l; --r; } } { long l = S.length / 2 + 1, r = S.length - 1; while (l < r) { if (S[l] != S[r]) ans = false; ++l; --r; } } { long l = 0, r = S.length - 1; while (l < r) { if (S[l] != S[r]) ans = false; ++l; --r; } } writeln(ans ? "Yes" : "No"); stdout.flush; debug readln; }
D
import std.algorithm; import std.array; import std.conv; import std.stdio; import std.string; void main () { long [] a; while ((a = readln.splitter.map !(to !(long)).array) != []) { auto n = readln.strip.to !(long); foreach (i, c; a) { if (i > 0) { a[i] = min (a[i], a[i - 1] * 2); } } writeln (a[3] * (n / 2) + a[2] * (n % 2)); } }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = readln.chomp.to!size_t; auto s = readln.chomp; auto isValid(bool[] sw) { foreach (i; 2..n) sw[i] = sw[i-1] ^ (s[i-1] != 'o') ^ sw[i-2]; return ((sw[$-1] ^ (s[$-1] != 'o') ^ sw[$-2]) == sw[0] && (sw[0] ^ (s[0] != 'o') ^ sw[$-1]) == sw[1]); } foreach (sw1; [false, true]) foreach (sw2; [false, true]) { auto sw = new bool[](n); sw[0] = sw1; sw[1] = sw2; if (isValid(sw)) { foreach (swi; sw) write(swi ? 'W' : 'S'); writeln; return; } } writeln(-1); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; void main() { int N = readln.chomp.to!(int); int[] a = readln.chomp.split.to!(int[]); int ans; for (int i = 0; i < a.length - 1; i++) { if (a[i] == a[i + 1]) { ans += 1; i += 1; } } writeln(ans); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static 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; auto A = RDA; auto b = new long[](N+1); foreach (i; 0..N) { b[i+1] = b[i] + A[i]; } long d = long.max; foreach (i; 1..N) { d = min(d, abs(b[N] - b[i] - b[i])); } writeln(d); stdout.flush(); debug readln(); }
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(); auto t = new long[](n); foreach (ref e; t) e = lread(); long lcm = t[0]; foreach (e; t[1 .. $]) { lcm /= gcd(lcm, e); lcm *= e; } lcm.writeln(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop; // dfmt on void main() { long N = lread(); writeln(N * 800 - N / 15 * 200); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!long; if (N == 2) { writeln(1); return; } int res; long d; for (d = 2; d^^2 < N; ++d) { if (N % d == 1) { auto dd = (N-1)/d; res += d == dd ? 1 : 2; } if (N % d == 0) { auto n = N; while (n%d == 0) n /= d; if (n % d == 1) { ++res; } auto dd = N / d; n = N; while (n % dd == 0) n /= dd; if (n % dd == 1) { ++res; } } } if (d^^2 == N) { ++res; } writeln(res+2); }
D
import std.stdio, std.string, std.array, std.conv; void main() { auto a = readln.chomp.split.to!(int[]); writeln(a[0] < a[1] && a[1] < a[2] ? "Yes" : "No"); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n, m; rd(n, m); auto g=new int[][](n, 0); foreach(_; 0..m){ int a, b; rd(a, b); a--; b--; g[a]~=b; g[b]~=a; } auto c=new int[](n); fill(c, -1); bool dfs(int i, int w, int p=-1){ c[i]=w; bool ret=true; foreach(j; g[i])if(j!=p){ if(c[j]==-1) ret&=dfs(j, w^1, i); if((c[j]^w)==0) ret=false; } return ret; } if(dfs(0, 0)){ long w=0; foreach(e; c)if(e==0) w++; auto b=to!long(n)-w; writeln(w*b-m); }else{ auto _n=to!long(n); writeln(_n*(_n-1)/2-m); } } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto rd = readln.split.to!(int[]), a = rd[0], b = rd[1]; writeln((a-1)*(b-1)); }
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; int[] stk; foreach (_; 0..N) { auto a = readln.chomp.to!int; if (stk.empty || stk[$-1] >= a) { stk ~=a; continue; } int l, r = stk.length.to!int-1; while (l+1 < r) { auto mid = (l+r)/2; if (stk[mid] < a) { r = mid; } else { l = mid; } } if (stk[l] < a) { stk[l] = a; } else { stk[r] = a; } } writeln(stk.length); }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; const MOD = 10^^9 + 7; int[int] fac(int n) { assert(n > 1); int[int] ps; while (n % 2 == 0) { ps[2]++; n /= 2; } for (int i = 3; i * i <= n; i += 2) { while (n % i == 0) { ps[i]++; n /= i; } } if (n > 1) ps[n]++; return ps; } long calc(int n, int m) { if (m == 1) return 1; if (n == 1) return 1; initFactTable(n * 3); import std.exception; // enforce(n > 1); long ans = 1; foreach (v; fac(m).values) { // v 個の素因数と n - 1 個の仕切りの並べ方 ans = (ans * nck(v + n - 1, n - 1)) % MOD; } return ans; } void main() { int n, m; scan(n, m); writeln(calc(n, m)); } long[] fact; // 階乗 long[] factInv; // 階乗の逆元 void initFactTable(int n) { fact = new long[n + 1]; fact[0] = 1; for (int i = 1; i <= n; i++) fact[i] = i * fact[i - 1] % MOD; factInv = new long[n + 1]; factInv[n] = modPow(fact[n], MOD - 2, MOD); for (int i = n - 1; i >= 0; i--) factInv[i] = factInv[i + 1] * (i + 1) % MOD; } long modPow(long x, long k, long m) { if (k == 0) return 1; if (k % 2 == 0) return modPow(x * x % m, k / 2, m); return x * modPow(x, k - 1, m) % m; } long nck(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return (fact[n] * factInv[k] % MOD) * factInv[n - k] % MOD; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto kn = readln.split.to!(int[]); auto K = kn[0]; auto N = kn[1]; auto as = readln.split.to!(int[]); int max_a = K - as[$-1] + as[0]; foreach (i; 0..N-1) max_a = max(max_a, as[i+1] - as[i]); writeln(K - max_a); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto a = readln.chomp.to!int; auto b = readln.chomp.to!int; auto c = readln.chomp.to!int; auto d = readln.chomp.to!int; auto e = readln.chomp.to!int; auto k = readln.chomp.to!int; auto ts = [a, b, c, d, e]; foreach (i; 0..5) { foreach (j; i+1..5) { if (ts[j] - ts[i] > k) { writeln(":("); return; } } } writeln("Yay!"); }
D
module app; import core.bitop; import std.algorithm; import std.array; import std.bigint; import std.conv; import std.math; import std.stdio; import std.string; struct Input { ulong a,b; } void parseInput(T)(out Input input, T file) { with (file) with (input) { auto ab = readln().strip().split(); a = ab[0].to!ulong; b = ab[1].to!ulong; } } T gcm(T)(T i1, T i2) { T large = i1 < i2 ? i2 : i1; T small = i1 < i2 ? i1 : i2; while (small != 0) { T q = large % small; large = small; small = q; } return large; } auto main2(Input* input) { ulong[] primes = [2]; auto gcm = gcm(input.a, input.b); if (input.a == input.b) { } bool isPrime(ulong n) { auto r = floor(sqrt(cast(real)n)); foreach (p; primes) { if (p > r) return true; if (n % p == 0) return false; } return true; } uint result = 1; { bool contains = false; while (gcm % 2 == 0) { gcm /= 2; contains = true; } if (contains) result++; } for (ulong n = 3; gcm != 1; n+=2) { if (isPrime(n)) primes ~= n; else { if (n > sqrt(floor(cast(real)gcm))) { result++; break; } continue; } bool contains = false; while (gcm % n == 0) { gcm /= n; contains = true; } if (contains) result++; } return result; } unittest { writeln("begin unittest"); } unittest // example1 { string example = `12 18`; Input input = void; parseExample(input, example); auto result = main2(&input); writeln(result); assert(result == 3); } unittest // example2 { string example = `420 660`; Input input = void; parseExample(input, example); auto result = main2(&input); writeln(result); assert(result == 4); } unittest // example3 { string example = `1 2019`; Input input = void; parseExample(input, example); auto result = main2(&input); writeln(result); assert(result == 1); } unittest { writeln("end unittest"); } void parseExample(out Input input, string example) { struct Adapter { string[] _lines; this(string input) { _lines = input.splitLines(); } string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; } } parseInput(input, Adapter(example)); } void main() { Input input = void; parseInput(input, stdin); auto result = main2(&input); writeln(result); }
D
import std.stdio; import std.conv; import std.array; void main() { auto reader = readln.split; int N = reader[0].to!int; int D = reader[1].to!int; int range = D * 2 + 1; int ans = N / range; if (N % range > 0){ ans++; } writeln(ans); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; // dfmt on void main() { long N = lread(); auto S = sread(); long l; long level; foreach (c; S) { if (c == '(') level++; if (c == ')') { if (level) { level--; } else { l++; } } } long r = level; writeln(repeat('(').take(l).array, S, repeat(')').take(r).array); }
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("2018", s[4..$]); }
D
import std.stdio, std.algorithm; void main() { int A, B, C, D, P; scanf("%d\n", &A); scanf("%d\n", &B); scanf("%d\n", &C); scanf("%d\n", &D); scanf("%d\n", &P); writeln( min(A * P, B + D * max(0, P - C)) ); }
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) { // 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; } void main() { readln.chomp.to!int.rep!(() => readln.chomp.to!long).fold!((a, b) { long t1 = min(a.back, b); long t2 = max(0, (b - a.back)/2); return [a.front+t1+t2, b-t1-2*t2]; })([0L, 0L]).front.writeln; } // ---------------------------------------------- // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { auto minElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto minimum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b < minimum) { element = a; minimum = b; } } return element; } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto maximum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b > maximum) { element = a; maximum = b; } } return element; } }
D
import std.stdio, std.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 t = RD!int; auto ans = new int[](t); foreach (ti; 0..t) { auto k = RD!int; auto s = RD!string; bool hasA; int streak; foreach (i; 0..k) { if (!hasA) { if (s[i] == 'A') hasA = true; } else if (s[i] == 'P') { ++streak; } else { ans[ti].chmax(streak); streak = 0; } } ans[ti].chmax(streak); } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n, q; rd(n, q); auto tree=new SquareRootDecomposition(n); while(q--){ auto args=readln.split.to!(int[]); if(args[0]==0){ tree.add(args[1], args[2]+1, args[3]); }else{ writeln(tree.rmin(args[1], args[2]+1)); } } } class SquareRootDecomposition{ import std.algorithm; int D=1; int[] val, buc, laz; this(int n){ while(D*D<n) D++; val.length=D*D; buc.length=laz.length=D; } void add(int ql, int qr, int x){ foreach(k; 0..D){ int l=k*D, r=(k+1)*D; if(r<=ql || qr<=l){ // }else if(ql<=l && r<=qr){ buc[k]+=x; laz[k]+=x; }else{ push(k); val[max(l, ql)..min(r, qr)]+=x; buc[k]=reduce!(min)(val[l..r]); } } } int rmin(int ql, int qr){ int ret=1_000_000_000; foreach(k; 0..D){ int l=k*D, r=(k+1)*D; if(r<=ql || qr<=l){ // }else if(ql<=l && r<=qr){ chmin(ret, buc[k]); }else{ push(k); chmin(ret, reduce!(min)(val[max(l, ql)..min(r, qr)])); } } return ret; } void push(int k){ val[(k*D)..((k+1)*D)]+=laz[k]; laz[k]=0; } 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, 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; immutable int INF = 1 << 29; void main() { auto s = readln.split.map!(to!int); auto H = s[0]; auto W = s[1]; auto B = H.iota.map!(_ => readln.chomp).array; auto source = H + W; auto sink = H + W + 1; auto FF = new FordFulkerson(H + W + 2, source, sink); int r1, c1, r2, c2; foreach (i; 0..H) { foreach (j; 0..W) { if (B[i][j] == 'S') { FF.add_edge(source, i, INF); FF.add_edge(source, H + j, INF); r1 = i; c1 = j; } else if (B[i][j] == 'T') { FF.add_edge(i, sink, INF); FF.add_edge(H + j, sink, INF); r2 = i; c2 = j; } else if (B[i][j] == 'o') { FF.add_edge(i, H + j, 1); FF.add_edge(H + j, i, 1); } } } writeln( (r1 == r2 || c1 == c2) ? -1 : FF.run ); } class FordFulkerson { int N, source, sink; int[][] adj; int[][] flow; bool[] used; this(int n, int s, int t) { N = n; source = s; sink = t; assert (s >= 0 && s < N && t >= 0 && t < N); adj = new int[][](N); flow = new int[][](N, N); used = new bool[](N); } void add_edge(int from, int to, int cap) { adj[from] ~= to; adj[to] ~= from; flow[from][to] = cap; } int dfs(int v, int min_cap) { if (v == sink) return min_cap; if (used[v]) return 0; used[v] = true; foreach (to; adj[v]) { if (!used[to] && flow[v][to] > 0) { auto bottleneck = dfs(to, min(min_cap, flow[v][to])); if (bottleneck == 0) continue; flow[v][to] -= bottleneck; flow[to][v] += bottleneck; return bottleneck; } } return 0; } int run() { int ret = 0; while (true) { foreach (i; 0..N) used[i] = false; int f = dfs(source, int.max); if (f > 0) ret += f; else return ret; } } }
D
void main() { long n = rdElem; long[] a = rdRow; long[] b = rdRow; long monster; foreach (i; 0 .. n) { if (b[i] >= a[i]) { monster += a[i]; long diff = b[i] - a[i]; long next = min(a[i+1], diff); a[i+1] -= next; monster += next; a[i] = 0; } else { monster += b[i]; a[i] -= b[i]; } } monster.writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio; import std.string, std.conv; import std.array, std.algorithm; import std.math; void main() { int[] weight = [200, 300, 500, 200 * 5, 300 * 4, 500 * 3]; int[] price = [380, 550, 850, 1520, 1870, 2244]; int[5000+1] dp = cast(int)1e9; dp[0] = 0; foreach(i; 0 .. 6) { for(int j = weight[i]; j <= 5000; j++) { dp[j] = min(dp[j], dp[j - weight[i]] + price[i]); } } for(int N; (N = readln.chomp.to!int) != 0; ) { dp[N].writeln(); } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; 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 calc(int n, int[] a) { int[dchar] d; int lo, hi; // 重複文字の位置 for (int i = 0; i < a.length; i++) { if (a[i] in d) { lo = d[a[i]] + 1; hi = i + 1; break; } d[a[i]] = i; } int span = hi - lo; foreach (k; 1..n+2) { long ans = nck(n + 1, k) - nck(lo - 1 + (n + 1) - hi, k - 1); if (ans < 0) ans += MOD; writeln(ans); } } void main() { int n = readint; auto a = readints; initFactTable(n + 1); calc(n, a); } const MOD = 10^^9 + 7; long[] fact; // 階乗 long[] factInv; // 階乗の逆元 void initFactTable(int n) { fact = new long[n + 1]; fact[0] = 1; for (int i = 1; i <= n; i++) fact[i] = i * fact[i - 1] % MOD; factInv = new long[n + 1]; factInv[n] = modPow(fact[n], MOD - 2, MOD); for (int i = n - 1; i >= 0; i--) factInv[i] = factInv[i + 1] * (i + 1) % MOD; } long modPow(long x, long k, long m) { if (k == 0) return 1; if (k % 2 == 0) return modPow(x * x % m, k / 2, m); return x * modPow(x, k - 1, m) % m; } long nck(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return (fact[n] * factInv[k] % MOD) * factInv[n - k] % MOD; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; // dfmt on void main() { long N = lread(); auto S = sread(); auto T = sread(); outer: foreach_reverse (i; 0 .. N + 1) { foreach (j; 0 .. i) { if (S[N - i + j] != T[j]) continue outer; } writeln(2 * N - i); return; } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.container; import std.array; import std.math; import std.range; import std.typecons; import std.ascii; import std.format; void main() { auto l = readln.chomp; if (l.equal(l.retro)) { writeln = "Yes"; } else { writeln = "No"; } }
D
import std.algorithm; import std.array; import std.bitmanip; import std.conv; import std.stdio; import std.string; import std.typecons; T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; } void main() { immutable ip = readln.split.to!(ulong[]); immutable n = ip[0], k = ip[1]; ulong ret = k == 1 ? 0 : n-k; writeln(ret); }
D
import std.stdio; import std.algorithm; import std.range; import std.conv; int f(int n, int m, int k) { if (n < m) return max(0, n-k+1); return f(n%m, m, k) + (n/m)*(m-k); } void main() { string[] input = split(readln()); int n = to!int(input[0]); int k = to!int(input[1]); long ans = 0; foreach (i; k+1 .. n+1) { ans += f(n,i,k); if (k == 0) ans--; } writeln(ans); }
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 abc = readln.split.to!(int[]); if (abc[0] > abc[1]) { auto tmp = abc[0]; abc[0] = abc[1]; abc[1] = tmp; } writeln(abc[0] <= abc[2] && abc[2] <= abc[1] ? "Yes" : "No"); }
D
import std.stdio; void main() { while(true) { uint[][][char] mapping; char[3][3] field; foreach (c; ['b', 'w', '+']) mapping[c] = new uint[][](3, 3); for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++) { scanf(j == 2 ? "%c\n" : "%c", &field[i][j]); if (field[i][j] == '0') goto end; } for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++) { mapping[field[i][j]][0][j]++; mapping[field[i][j]][1][i]++; } for(int i = 0; i < 3; i++) { mapping[field[i][i]][2][0]++; mapping[field[2 - i][i]][2][1]++; } for(int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) foreach (c; ['b', 'w']) if (mapping[c][i][j] == 3) {writeln(c); goto next;} writeln("NA"); next:; } end:; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; void main() { auto T = readln.chomp.to!int; while (T--) solve; } void solve() { auto N = readln.chomp.to!int; auto S = readln.chomp; bool ok = true; foreach (i; 0..N/2) { char x = S[i]; char y = S[N-i-1]; if (x != y && abs(x - y) != 2) ok = false; } writeln(ok ? "YES" : "NO"); }
D
import std.stdio; import std.conv; import std.string; import std.math; import std.random; import std.range; import std.algorithm; import std.functional; import core.bitop; import std.bigint; import std.typecons; import std.container; void main() { auto input = readln.split.to!(int[]); auto A = input[0], B = input[1], C = input[2], D = input[3]; ((A + B) > (C + D) ? "Left" : (A + B) < (C + D) ? "Right" : "Balanced").writeln; }
D
void main() { int n = readln.chomp.to!int; int cnt; for (int i = 1; i <= n; i += 2) { int divisor; for (int j = 1; j * j <= i; j += 2) { if (i % j == 0) { ++divisor; if (j * j != i) ++divisor; } } if (divisor == 8) ++cnt; } cnt.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.container; import std.typecons;
D
void main(){ int a = _scan(); string s = readln().chomp(); if(a>=3200)s.writeln(); else writeln("red"); } 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.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; immutable long MOD = 998244353; void main() { auto N = readln.chomp.to!int; auto A = iota(N).map!(_ => readln.chomp.to!int).array; auto dp = new long[][][](2, 3, N*300+1); dp[0][0][0] = 1; int cur = 0; int tar = 1; foreach (i; 0..N) { foreach (j; 0..3) { dp[tar][j][] = 0; } foreach (j; 0..3) foreach (k; 0..N*300+1) if (dp[cur][j][k]) { (dp[tar][j][k+A[i]] += dp[cur][j][k]) %= MOD; if (j == 0) { (dp[tar][min(2, j+1)][k] += dp[cur][j][k] % MOD) %= MOD; } else if (j == 1) { (dp[tar][j][k] += dp[cur][j][k] % MOD) %= MOD; (dp[tar][min(2, j+1)][k] += dp[cur][j][k] % MOD) %= MOD; } else if (j == 2) { (dp[tar][min(2, j+1)][k] += dp[cur][j][k] * 2 % MOD) %= MOD; } } swap(cur, tar); } long ans = 0; int border = A.sum / 2 + A.sum % 2; foreach (k; border..N*300+1) { (ans += dp[cur][2][k] * 6) %= MOD; } ans = ((powmod(3, N, MOD) - ((powmod(2, N, MOD) - 2) * 3) - 3 - ans) % MOD + MOD) % MOD; ans.writeln; } long powmod(long a, long x, long m) { long ret = 1; while (x) { if (x % 2) ret = ret * a % m; a = a * a % m; x /= 2; } return ret; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; long P = 10^^9+7; long[10^^5*2+50] F, RF; long pow(long x, long n) { long y = 1; while (n) { if (n%2 == 1) y = (y * x) % P; x = x^^2 % P; n /= 2; } return y; } void init() { F[0] = F[1] = 1; foreach (i, ref x; F[2..$]) x = (F[i+1] * (i+2)) % P; { RF[$-1] = 1; auto x = F[$-1]; auto k = P-2; while (k) { if (k%2 == 1) RF[$-1] = (RF[$-1] * x) % P; x = x^^2 % P; k /= 2; } } foreach_reverse(i, ref x; RF[0..$-1]) x = (RF[i+1] * (i+1)) % P; } long comb(N)(N n, N k) { if (k > n) return 0; auto n_b = F[n]; // n! auto nk_b = RF[n-k]; // 1 / (n-k)! auto k_b = RF[k]; // 1 / k! auto nk_b_k_b = (nk_b * k_b) % P; // 1 / (n-k)!k! return (n_b * nk_b_k_b) % P; // n! / (n-k)!k! } void main() { init(); auto nab = readln.split.to!(long[]); auto n = nab[0]; auto a = nab[1]; auto b = nab[2]; auto r = (pow(2, n) - 1 + P) % P; long aa = 1; foreach_reverse (x; n-a+1..n+1) aa = (aa * x) % P; aa = (aa * RF[a]) % P; long bb = 1; foreach_reverse (x; n-b+1..n+1) bb = (bb * x) % P; bb = (bb * RF[b]) % P; r = (r - aa + P) % P; r = (r - bb + P) % P; writeln(r); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { int N; string s; scan(N); scan(s); long r, g, b; foreach (ch ; s) { if (ch == 'R') r++; if (ch == 'G') g++; if (ch == 'B') b++; } long ans = r * g * b; foreach (i ; 0 .. N) { foreach (j ; i + 1 .. N) { if ((i + j) % 2) continue; auto k = (i + j) / 2; if (s[i] != s[k] && s[k] != s[j] && s[i] != s[j]) ans--; } } writeln(ans); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void main() { auto nq = readln.split.to!(int[]); auto N = nq[0]; auto Q = nq[1]; long r = (N.to!long-2)^^2; auto lms = new int[](N+1); int min_w = N; auto bms = new int[](N+1); int min_v = N; foreach (_; 0..Q) { auto q = readln.split.to!(int[]); auto x = q[1]; if (q[0] == 1) { if (x < min_w) { while (min_w > x) { lms[--min_w] = min_v - 2; } } r -= lms[x]; } else { if (x < min_v) { while (min_v > x) { bms[--min_v] = min_w - 2; } } r -= bms[x]; } } writeln(r); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new char[][](t); foreach (ti; 0..t) { auto s = RD!string; auto x = RD!int; auto n = s.length; ans[ti].length = s.length; ans[ti][] = '1'; foreach (i; 0..s.length) { if (s[i] == '0') { if (i >= x) { ans[ti][i-x] = '0'; } if (i < n-x) { ans[ti][i+x] = '0'; } } } foreach (i; 0..s.length) { if (s[i] == '1') { bool ok; if (i >= x) { ok |= ans[ti][i-x] == '1'; } if (i < n-x) { ok |= ans[ti][i+x] == '1'; } if (!ok) { ans[ti].length = 0; break; } } } } foreach (e; ans) { if (e.empty) writeln(-1); else writeln(e); } stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } 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 M = RD; auto X = RD; auto C = new long[](N); auto A = new long[][](N); foreach (i; 0..N) { A[i] = RDA; C[i] = A[i][0]; A[i].popFront; debug writeln(A[i]); } long ans = long.max; foreach (i; 0..2^^N) { auto arr = new long[](M); long cost; foreach (j; 0..N) { auto bit = 1L << j; if (i & bit) { arr[] += A[j][]; cost += C[j]; } } debug writeln(arr); bool ok = true; foreach (j; 0..M) { if (arr[j] < X) ok = false; } if (ok) { ans.chmin(cost); } } if (ans == long.max) writeln(-1); else writeln(ans); stdout.flush; debug readln; }
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(); const K = readLong(); auto X = new long[N]; auto Y = new long[N]; foreach (u; 0 .. N) { X[u] = readLong(); Y[u] = readLong(); } int ans = -1; foreach (u; 0 .. N) { bool ok = true; foreach (v; 0 .. N) { ok = ok && (abs(X[u] - X[v]) + abs(Y[u] - Y[v]) <= K); } if (ok) { ans = 1; } } writeln(ans); } } } catch (EOFException e) { } }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); ulong bignum = 1_000_000_007; alias Pair = Tuple!(long, "number", long, "times"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { long n = lread(); auto b = aryread(); b.reverse(); auto ans = new long[](0); while (n > 0) { long i; while (1) { if (i >= n) { writeln(-1); return; } if (b[i] == n - i) { ans ~= b[i]; b = b[0 .. i] ~ b[i + 1 .. n]; break; } i++; } n--; } foreach_reverse (e; ans) { e.writeln(); } }
D
import std.functional, std.algorithm, std.container, std.typetuple, std.typecons, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv; void main() { auto s = readln.chomp.to!int; writeln((s/3600).to!string ~ ":" ~ (s%3600/60).to!string ~ ":" ~ (s%3600%60).to!string); }
D
import std.stdio, std.algorithm; void main(){ foreach(char[] line; lines(stdin)){ foreach(i; 0..26){ foreach(ref c; line){ if('a' <= c && c <= 'z') c = c == 'z' ? 'a' : cast(char)(c + 1); } if(line.countUntil("the") + 1 || line.countUntil("this") + 1 || line.countUntil("that") + 1){ line.write; break; } } } }
D
import std.stdio, std.array, std.conv, std.algorithm; void main() { string s; while((s=readln()).length != 0) { string[] input = split(s); int pos = 1; int carry = 0; int lena = to!int(input[0].length); int lenb = to!int(input[1].length); while(true) { int a = lena<pos ? 0 : to!int(input[0][lena-pos]-'0'); int b = lenb<pos ? 0 : to!int(input[1][lenb-pos]-'0'); int res = a + b + carry; carry = res<10 ? 0 : 1; if(input[0].length <= pos && input[1].length <= pos) { if(carry) pos++; break; } pos++; } writeln(pos); } }
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; readV(x); writeln(x == 3 || x == 5 || x == 7 ? "YES" : "NO"); }
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.map!(x=>(x-'0').to!int).array; auto op = ['-', ' ', '+']; a:foreach (op1; iota(-1, 2, 2)) { foreach (op2; iota(-1, 2, 2)) { foreach (op3; iota(-1, 2, 2)) { auto sum = n[0] + op1 * n[1] + op2 * n[2] + op3 * n[3]; if (sum == 7) { writeln(n[0], op[op1+1] , n[1], op[op2+1], n[2], op[op3+1], n[3], "=7"); break a; } } } } }
D
import std.functional, std.algorithm, std.container, std.typetuple, std.typecons, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv, std.format, std.math; void main() { auto n = readln.chomp.to!int; auto a = readln.split.to!(int[]); writeln((reduce!("a + b")(0, a)) - n); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; void main() { string input; int num; int[] train; while ((input = readln().chomp()).length != 0) { num = input.to!int(); if (num == 0) { writeln(train[$ - 1]); train.length--; } else { train ~= num; } } }
D
import std.stdio; import std.algorithm; import std.string; import std.functional; import std.array; import std.conv; import std.math; import std.typecons; import std.regex; import std.range; void main(){ while(true){ auto a = readln(); if(stdin.eof()) break; if(a.chomp() == "0") break; int n =a.chomp().to!int; bool flg = true; long[4001] map; for(int i=0;i<n;i++){ auto s = readln().split().to!(long[]); map[s[0]] += s[1] * s[2]; if(map[s[0]] - s[1]*s[2] < 1e6 && map[s[0]] >= 1e6){ flg = false; writeln(s[0]); } } if(flg) writeln("NA"); } }
D
import std.stdio; import std.algorithm; import std.string; import std.container; import std.typecons; void log(A...)(A arg) { stderr.writeln(arg); } void main() { long N, K; scanf("%d %d\n", &N, &K); long f(long n) { return n + 1 + (n / (K - 1)); } long x = 0; for (long n = 1; n < N; n++) { x = f(x); } writeln(x); }
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() { char c; scan(c); writeln((c + 1).to!char); } 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.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!string; auto b = RD!string; long x = a == "H" ? 1 : 0; long y = b == "H" ? 1 : 0; writeln(x ^ y ? "D" : "H"); stdout.flush(); debug readln(); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = readln.chomp.to!size_t; auto ans = 0; foreach (_; 0..n) { auto rd = readln.split.to!(int[]), l = rd[0], r = rd[1]; ans += r - l + 1; } writeln(ans); }
D