code
stringlengths
4
1.01M
language
stringclasses
2 values
void main() { int n = readln.chomp.to!int; int[] a = readln.split.to!(int[]); int m = round(a.sum / n.to!double).to!int; int cost; foreach (x; a) { cost += (x - m) ^^ 2; } cost.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio; import std.string; void main() { string S = readln.chomp; ulong len = S.length; ulong ans = 0; foreach (i; 0..len){ if (S[i] != '0' + (i % 2)) ans++; } if ((len - ans) < ans) ans = len - ans; writeln(ans); }
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 xab = readints; int x = xab[0], a = xab[1], b = xab[2]; if (b - a <= 0) writeln("delicious"); else if (b - a <= x) writeln("safe"); else writeln("dangerous"); }
D
import std.stdio; import std.string; import std.conv; import std.array; import std.algorithm; import std.range; void main(){ auto Q=readln.split.to!(int[]),a=Q[0],b=Q[1],c=Q[2]; if(b-a==c-b)writeln("YES"); else writeln("NO"); }
D
import std.stdio; import std.string; import std.format; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; import std.traits; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; const long INF = long.max/3; const long MOD = 10L^^9+7; void main() { int[] as = readln.chomp.map!"a=='0'?0:1".array; int N = as.length.to!int; int l = 1, r = N+1; bool isOk(int c) { int[] bs = as.dup; int n = 0; foreach(i; 0..c) { bs[i] += n; if (bs[i]%2 == 0) continue; if (i > N-c) continue; bs[i]++; n++; } foreach(i; 0..c) { if (bs[i]%2 != 0) return false; } return true; } while(r-l > 1) { int c = (l + r)/2; if (isOk(c)) { l = c; } else { r = c; } } l.writeln; } // ---------------------------------------------- void scanln(Args...)(auto ref Args args) { import std.meta; template getFormat(T) { static if (isIntegral!T) { enum getFormat = "%d"; } else static if (isFloatingPoint!T) { enum getFormat = "%g"; } else static if (isSomeString!T || isSomeChar!T) { enum getFormat = "%s"; } else { static assert(false); } } enum string str = [staticMap!(getFormat, Args)].join(" ") ~ "\n"; // readf!str(args); mixin("str.readf(" ~ Args.length.iota.map!(i => "&args[%d]".format(i)).join(", ") ~ ");"); } 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; } T ceil(T)(T x, T y) if (__traits(isIntegral, T)) { // `(x+y-1)/y` will only work for positive numbers ... T t = x / y; if (t * y < x) t++; return t; } T floor(T)(T x, T y) if (__traits(isIntegral, T)) { T t = x / y; if (t * y > x) t--; return t; } // 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; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } struct UnionFind(T) { void init(T n) { par = new T[](n); foreach (i; 0..n) par[i] = i; cnt = new T[](n); fill(cnt, 1); } T root(T i) { return par[i] == i ? i : (par[i] = root(par[i])); } bool same(T i, T j) { return root(i) == root(j); } void unite(T i, T j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; cnt[j] += cnt[i]; } T size(T i) { return cnt[root(i)]; } T[] par, cnt; } void main() { auto n = RD!int; auto m1 = RD!int; auto m2 = RD!int; UnionFind!int uf1, uf2; uf1.init(n); uf2.init(n); foreach (i; 0..m1) { auto u = RD!int-1; auto v = RD!int-1; uf1.unite(u, v); } foreach (i; 0..m2) { auto u = RD!int-1; auto v = RD!int-1; uf2.unite(u, v); } int[][] ans; foreach (i; 0..n) { foreach (j; i+1..n) { if (uf1.same(i, j) || uf2.same(i, j)) continue; uf1.unite(i, j); uf2.unite(i, j); ans ~= [i+1, j+1]; } } writeln(ans.length); foreach (e; ans) { writeln(e[0], " ", e[1]); } 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.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto D = RD; auto X = new long[][](N); foreach (i; 0..N) { X[i] = RDR.ARR; } long ans; foreach (i; 0..N) { foreach (j; i+1..N) { double d = 0.0; foreach (k; 0..D) { d += (X[i][k] - X[j][k])^^2; } d = sqrt(d); if (d == cast(long)d) ++ans; } } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.math, std.string, std.range, std.array, std.algorithm, std.typecons; void main(){ auto buf = readln().strip().split().map!(to!int)(); immutable K = buf[0]; immutable A = buf[1]; immutable B = buf[2]; if(B - A <= 2 || K <= A - 1) { writeln(K + 1); return; } long t = cast(long)K - A + 1; if(t % 2 == 0) { writeln(A + (B-A) * (t/2)); } else { writeln(A + (B-A) * (t/2) + 1); } }
D
import std; auto input() { return readln().chomp(); } alias sread = () => readln.chomp(); alias aryread(T = long) = () => readln.split.to!(T[]); void main() { string s; s = input(); // writeln(s); string key; key = "keyence"; foreach (i; 0 .. key.length + 1) { string tmp = s[0 .. i] ~ s[$ - (7 - i) .. $]; // writeln(tmp); if (tmp == key) { writeln("YES"); return; } } writeln("NO"); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container; long P = 10^^9+7; long[2001] 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; } long inv(long x) { return pow(x, P-2); } 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! } long perm(N)(N n, N k) { if (k > n) return 0; auto n_b = F[n]; auto n_k_b = RF[n-k]; return (n_b * n_k_b) % P; } void main() { init(); auto nk = readln.split.to!(long[]); auto N = nk[0]; auto K = nk[1]; N -= K; if (N < 0) { writeln(0); } else { writeln(comb(N+K-1, K-1)); } }
D
void main() { long n = rdElem; long[] a = rdRow; long q = rdElem; foreach (i; 0 .. q) { long p = rdElem; long r = a.binSrch!"a < b"(p); writeln(r + 1); } } long binSrch(alias pred = "a < b")(long[] arr, long val) { alias predFun = binaryFun!pred; long l = -1, r = arr.length; if (!predFun(l, r)) swap(l, r); while (abs(r - l) > 1) { long m = (l + r) / 2; if (predFun(arr[m], val)) l = m; else r = m; } return l; } 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; import core.bitop;
D
void main() { int[] tmp = readln.split.to!(int[]); int r = tmp[0], d = tmp[1], x = tmp[2]; foreach (i; 0 .. 10) { x = r * x - d; x.writeln; } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.typecons;
D
import std.algorithm; import std.array; import std.conv; import std.range; import std.stdio; import std.string; import std.typecons; void main () { string s; while ((s = readln.strip) != "") { s = readln.strip; writeln (s.count ('R') > s.count ('B') ? "Yes" : "No"); } }
D
// import chie template :) {{{ static if (__VERSION__ < 2090) { import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons, std.format, std.bitmanip, std.numeric; } else { import std; } // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan(T...)(ref T args) { foreach (ref arg; args) { arg = next!(typeof(arg)); } } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } // }}} // ModInt {{{ struct ModInt(ulong modulus) { import std.traits : isIntegral, isBoolean; import std.exception : enforce; private { ulong val; } this(const ulong n) { val = n % modulus; } this(const ModInt n) { val = n.value; } @property { ref inout(ulong) value() inout { return val; } } T opCast(T)() { static if (isIntegral!T) { return cast(T)(val); } else if (isBoolean!T) { return val != 0; } else { enforce(false, "cannot cast from " ~ this.stringof ~ " to " ~ T.stringof ~ "."); } } ModInt opAssign(const ulong n) { val = n % modulus; return this; } ModInt opOpAssign(string op)(ModInt rhs) { static if (op == "+") { val += rhs.value; if (val >= modulus) { val -= modulus; } } else if (op == "-") { if (val < rhs.value) { val += modulus; } val -= rhs.value; } else if (op == "*") { val = val * rhs.value % modulus; } else if (op == "/") { this *= rhs.inv; } else if (op == "^^") { ModInt res = 1; ModInt t = this; while (rhs.value > 0) { if (rhs.value % 2 != 0) { res *= t; } t *= t; rhs /= 2; } this = res; } else { enforce(false, op ~ "= is not implemented."); } return this; } ModInt opOpAssign(string op)(ulong rhs) { static if (op == "+") { val += ModInt(rhs).value; if (val >= modulus) { val -= modulus; } } else if (op == "-") { auto r = ModInt(rhs); if (val < r.value) { val += modulus; } val -= r.value; } else if (op == "*") { val = val * ModInt(rhs).value % modulus; } else if (op == "/") { this *= ModInt(rhs).inv; } else if (op == "^^") { ModInt res = 1; ModInt t = this; while (rhs > 0) { if (rhs % 2 != 0) { res *= t; } t *= t; rhs /= 2; } this = res; } else { enforce(false, op ~ "= is not implemented."); } return this; } ModInt opUnary(string op)() { static if (op == "++") { this += 1; } else if (op == "--") { this -= 1; } else { enforce(false, op ~ " is not implemented."); } return this; } ModInt opBinary(string op)(const ulong rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinary(string op)(ref const ModInt rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinary(string op)(const ModInt rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinaryRight(string op)(const ulong lhs) const { mixin("return ModInt(this) " ~ op ~ "= lhs;"); } long opCmp(ref const ModInt rhs) const { return cast(long)value - cast(long)rhs.value; } bool opEquals(const ulong rhs) const { return value == ModInt(rhs).value; } ModInt inv() const { ModInt ret = this; ret ^^= modulus - 2; return ret; } string toString() const { import std.format : format; return format("%s", val); } } // }}} // alias {{{ alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less); alias MinHeap(T) = Heap!(T, "a > b"); // }}} // memo {{{ /* - ある値が見つかるかどうか <https://dlang.org/phobos/std_algorithm_searching.html#canFind> canFind(r, value); -> bool - 条件に一致するやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#filter> // 2で割り切れるやつ filter!"a % 2 == 0"(r); -> Range - 合計 <https://dlang.org/phobos/std_algorithm_iteration.html#sum> sum(r); - 累積和 <https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold> // 今の要素に前の要素を足すタイプの一般的な累積和 // 累積和のrangeが帰ってくる(破壊的変更は行われない) cumulativeFold!"a + b"(r, 0); -> Range - rangeをarrayにしたいとき array(r); -> Array - 各要素に同じ処理をする <https://dlang.org/phobos/std_algorithm_iteration.html#map> // 各要素を2乗 map!"a * a"(r) -> Range - ユニークなやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#uniq> uniq(r) -> Range - 順列を列挙する <https://dlang.org/phobos/std_algorithm_iteration.html#permutations> permutation(r) -> Range - ある値で埋める <https://dlang.org/phobos/std_algorithm_mutation.html#fill> fill(r, val); -> void - バイナリヒープ <https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap> // 昇順にするならこう(デフォは降順) BinaryHeap!(Array!T, "a > b") heap; heap.insert(val); heap.front; heap.removeFront(); - 浮動小数点の少数部の桁数設定 // 12桁 writefln("%.12f", val); - 浮動小数点の誤差を考慮した比較 <https://dlang.org/phobos/std_math.html#.approxEqual> approxEqual(1.0, 1.0099); -> true - 小数点切り上げ <https://dlang.org/phobos/std_math.html#.ceil> ceil(123.4); -> 124 - 小数点切り捨て <https://dlang.org/phobos/std_math.html#.floor> floor(123.4) -> 123 - 小数点四捨五入 <https://dlang.org/phobos/std_math.html#.round> round(4.5) -> 5 round(5.4) -> 5 */ // }}} void main() { auto cin = new Scanner; int n, a, b; string s; cin.scan(n, a, b, s); int pass, fpass; foreach (c; s) { if (c == 'a' && pass < a + b) { pass++; writeln("Yes"); } else if (c == 'b' && pass < a + b && fpass < b) { pass++; fpass++; writeln("Yes"); } else { writeln("No"); } } }
D
import std.stdio, std.conv, std.string, std.algorithm, std.math, std.array, std.container, std.typecons; int[] p, q; int a, b, n, count=0; void solve(bool[] used, int[] array, int depth=0) { if(depth==n) { count++; if(array==p) a = count; if(array==q) b = count; return; } for(int i=0; i<n; i++) { if(!used[i]) { auto dup = array.dup; dup[depth] = i+1; auto du = used.dup; du[i] = true; solve(du, dup, depth+1); } } } void main() { n = readln.chomp.to!int; p = readln.chomp.split.to!(int[]); q = readln.chomp.split.to!(int[]); auto used = new bool[n]; auto array = new int[n]; solve(used, array); writeln(abs(a-b)); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto x = readln.chomp.split.to!(int[]); if (x[0]+x[1]<x[2]) { writeln("No"); } else { writeln("Yes"); } }
D
import std.stdio; import std.string; import std.format; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.concurrency; import std.traits; import std.uni; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; enum long INF = long.max/3; enum long MOD = 10L^^9+7; void main() { long N, M; scanln(N, M); long[] as = new long[M]; long[] bs = new long[M]; foreach(i; 0..M) { scanln(as[i], bs[i]); as[i]--; bs[i]--; } long[] xs = new long[M]; long x = (N*N - N)/2; auto uf = UnionFind(N); foreach_reverse(i; 0..M) { xs[i] = x; if (!uf.same(as[i], bs[i])) { x -= uf.size(as[i]) * uf.size(bs[i]); } uf.unite(as[i], bs[i]); } xs.each!writeln; } struct UnionFind { private: Vertex[] _vertices; size_t[] _sizes; public: this(size_t n) { init(n); } void init(size_t n) { _vertices.length = n; _sizes.length = n; foreach(i, ref v; _vertices) { v.index = i; v.parent = i; _sizes[i] = 1; } } void unite(size_t x, size_t y) { link(findSet(x), findSet(y)); } void link(size_t x, size_t y) { if (x==y) return; if (_vertices[x].rank > _vertices[y].rank) { _sizes[x] += _sizes[y]; _vertices[y].parent = x; } else { _sizes[y] += _sizes[x]; _vertices[x].parent = y; if (_vertices[x].rank == _vertices[y].rank) { _vertices[y].rank++; } } } bool same(size_t x, size_t y) { return findSet(x) == findSet(y); } size_t findSet(size_t index) { if (_vertices[index].parent == index) { return index; } else { return _vertices[index].parent = findSet(_vertices[index].parent); } } bool isRoot(size_t index) { return _vertices[index].parent == index; } size_t size(size_t index) { return _sizes[findSet(index)]; } private: struct Vertex { size_t index; size_t parent; size_t rank = 1; } } // ---------------------------------------------- void times(alias fun)(long n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(long n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { // `(x+y-1)/y` will only work for positive numbers ... T t = x / y; if (t * y < x) t++; return t; } T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { T t = x / y; if (t * y > x) t--; return t; } ref T ch(alias fun, T, S...)(ref T lhs, S rhs) { return lhs = fun(lhs, rhs); } unittest { long x = 1000; x.ch!min(2000); assert(x == 1000); x.ch!min(3, 2, 1); assert(x == 1); x.ch!max(100).ch!min(1000); // clamp assert(x == 100); x.ch!max(0).ch!min(10); // clamp assert(x == 10); } mixin template Constructor() { import std.traits : FieldNameTuple; this(Args...)(Args args) { // static foreach(i, v; args) { foreach(i, v; args) { mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v; } } } void scanln(Args...)(auto ref Args args) { import std.meta; template getFormat(T) { static if (isIntegral!T) { enum getFormat = "%d"; } else static if (isFloatingPoint!T) { enum getFormat = "%g"; } else static if (isSomeString!T || isSomeChar!T) { enum getFormat = "%s"; } else { static assert(false); } } enum string fmt = [staticMap!(getFormat, Args)].join(" "); string[] inputs = readln.chomp.split; foreach(i, ref v; args) { v = inputs[i].to!(Args[i]); } } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { private template RebindableOrUnqual(T) { static if (is(T == class) || is(T == interface) || isDynamicArray!T || isAssociativeArray!T) alias RebindableOrUnqual = Rebindable!T; else alias RebindableOrUnqual = Unqual!T; } private auto extremum(alias map, alias selector = "a < b", Range)(Range r) if (isInputRange!Range && !isInfinite!Range && is(typeof(unaryFun!map(ElementType!(Range).init)))) in { assert(!r.empty, "r is an empty range"); } body { alias Element = ElementType!Range; RebindableOrUnqual!Element seed = r.front; r.popFront(); return extremum!(map, selector)(r, seed); } private auto extremum(alias map, alias selector = "a < b", Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seedElement) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void) && is(typeof(unaryFun!map(ElementType!(Range).init)))) { alias mapFun = unaryFun!map; alias selectorFun = binaryFun!selector; alias Element = ElementType!Range; alias CommonElement = CommonType!(Element, RangeElementType); RebindableOrUnqual!CommonElement extremeElement = seedElement; // if we only have one statement in the loop, it can be optimized a lot better static if (__traits(isSame, map, a => a)) { // direct access via a random access range is faster static if (isRandomAccessRange!Range) { foreach (const i; 0 .. r.length) { if (selectorFun(r[i], extremeElement)) { extremeElement = r[i]; } } } else { while (!r.empty) { if (selectorFun(r.front, extremeElement)) { extremeElement = r.front; } r.popFront(); } } } else { alias MapType = Unqual!(typeof(mapFun(CommonElement.init))); MapType extremeElementMapped = mapFun(extremeElement); // direct access via a random access range is faster static if (isRandomAccessRange!Range) { foreach (const i; 0 .. r.length) { MapType mapElement = mapFun(r[i]); if (selectorFun(mapElement, extremeElementMapped)) { extremeElement = r[i]; extremeElementMapped = mapElement; } } } else { while (!r.empty) { MapType mapElement = mapFun(r.front); if (selectorFun(mapElement, extremeElementMapped)) { extremeElement = r.front; extremeElementMapped = mapElement; } r.popFront(); } } } return extremeElement; } private auto extremum(alias selector = "a < b", Range)(Range r) if (isInputRange!Range && !isInfinite!Range && !is(typeof(unaryFun!selector(ElementType!(Range).init)))) { return extremum!(a => a, selector)(r); } // if we only have one statement in the loop it can be optimized a lot better private auto extremum(alias selector = "a < b", Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seedElement) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void) && !is(typeof(unaryFun!selector(ElementType!(Range).init)))) { return extremum!(a => a, selector)(r, seedElement); } auto minElement(alias map = (a => a), Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { return extremum!map(r); } auto minElement(alias map = (a => a), Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void)) { return extremum!map(r, seed); } auto maxElement(alias map = (a => a), Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { return extremum!(map, "a > b")(r); } auto maxElement(alias map = (a => a), Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void)) { return extremum!(map, "a > b")(r, seed); } } // popcnt with ulongs was added in D 2.071.0 static if (__VERSION__ < 2071) { ulong popcnt(ulong x) { x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L); x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L); x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL); x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL); x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL); x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL); return x; } }
D
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; import std.datetime, std.bigint; int n; int[][] adj; void main() { scan(n); adj = new int[][](n, 0); int ai, bi; foreach (i ; 0 .. n - 1) { scan(ai, bi); ai--; bi--; adj[ai] ~= bi; adj[bi] ~= ai; } auto d = new int[](n); auto p = new int[](n); void dfs(int u, int par, int dep) { d[u] = dep; p[u] = par; foreach (v ; adj[u]) { if (v != par) { dfs(v, u, dep + 1); } } } dfs(0, 0, 0); int x = (d[n - 1] - 1) / 2; int pos = n - 1; while (x--) { pos = p[pos]; } int dfs2(int u) { int res = 1; foreach (v ; adj[u]) { if (v != p[u]) { res += dfs2(v); } } return res; } int cnt = dfs2(pos); if (cnt*2 >= n) { writeln("Snuke"); } else { writeln("Fennec"); } } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
void main() { problem(); } void problem() { const MAX = 10L.pow(18); auto N = scan!int; auto A = scan!ulong(N); long solve() { ulong multi = 1; real floatMulti = 1; foreach(a; A) { floatMulti *= cast(real)a; multi *= a; } return floatMulti > MAX ? -1 : multi; } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
import core.bitop; import std.algorithm; import std.array; import std.ascii; import std.container; import std.conv; import std.format; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; void main() { auto v = readln.chomp.split.map!(to!int); int x = v[0]; int a = v[1]; int b = v[2]; if (abs(x - a) < abs(x - b)) { "A".writeln; } else { "B".writeln; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto hw = readln.split.to!(int[]); auto H = hw[0]; auto W = hw[1]; auto MAP = new char[][](H, W); int bc; foreach (i; 0..H) foreach (j, c; readln.chomp.to!(char[])) { if (c == '#') ++bc; MAP[i][j] = c; } auto FS = new bool[][](H, W); FS[0][0] = true; auto ss = [[0, 0, 0]]; int cnt; bool ok; while (!ss.empty) { auto h = ss[0]; ss = ss[1..$]; auto x = h[0]; auto y = h[1]; auto c = h[2]; if (x == W-1 && y == H-1) { cnt = c-1; ok = true; break; } foreach (d; [[0,1], [1,0], [-1,0], [0,-1]]) { auto xd = x + d[0]; auto yd = y + d[1]; if (xd < 0 || xd >= W || yd < 0 || yd >= H || MAP[yd][xd] == '#' || FS[yd][xd]) continue; FS[yd][xd] = true; ss ~= [xd, yd, c+1]; } } if (!ok) { writeln(-1); } else { writeln(H * W - cnt - bc - 2); } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { auto nq = readints(); int n = nq[0], q = nq[1]; auto segTree = new SegmentTree(n); for (int i = 0; i < q; i++) { auto xs = readints(); int x = xs[1], y = xs[2]; switch (xs[0]) { case 0: // change a[i] to x segTree.update(x, y); break; case 1: // find minimum int val = segTree.query(x, y + 1); writeln(val); break; default: break; } } } class SegmentTree { private int[] _data; this(int n) { int len = 1; while (len < n) len *= 2; _data = new int[len * 2]; _data[] = int.max; } /// k ?????????????´????(0-indexed)??? a ????????´ void update(int k, int a) { // ????????\??? k += (_data.length / 2) - 1; _data[k] = a; // ?????????????????´??° while (k > 0) { k = (k - 1) / 2; _data[k] = min(_data[k * 2 + 1], _data[k * 2 + 2]); } } /// [a, b) ???????°????????±??????? int query(int a, int b, int k, int l, int r) { // [a, b) ??¨ [l, r) ?????????????????? if (r <= a || b <= l) return int.max; // [a, b) ??? [l, r) ????????¨???????????§????????°???????????\????????? if (a <= l && r <= b) { return _data[k]; } else { // ????????§???????????°???2 ????????????????°???? int vl = query(a, b, k * 2 + 1, l, (l + r) / 2); int vr = query(a, b, k * 2 + 2, (l + r) / 2, r); return min(vl, vr); } } int query(int a, int b) { return query(a, b, 0, 0, cast(int)(_data.length / 2)); } }
D
import std; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long K = lread(); long A, B; scan(A, B); writeln((A % K == 0 || (((A + K) / K) * K <= B)) ? "OK" : "NG"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math; struct UFTree(T) { struct Node { T parent; T rank = 1; } this(T n) { nodes.length = n; foreach (i, ref node; nodes) node = Node(i.to!T); } void unite(T a, T b) { a = root(a); b = root(b); if (a == b) return; if (nodes[a].rank < nodes[b].rank) { nodes[b].parent = nodes[a].parent; } else { nodes[a].parent = nodes[b].parent; if (nodes[a].rank == nodes[b].rank) nodes[a].rank += 1; } } bool is_same(T a, T b) { return root(a) == root(b); } private: Node[] nodes; T root(T i) { if (nodes[i].parent == i) return i; return nodes[i].parent = root(nodes[i].parent); } } void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; auto PS = readln.split.to!(int[]); auto uf = UFTree!int(N); foreach (_; 0..M) { auto xy = readln.split.to!(int[]); uf.unite(xy[0]-1, xy[1]-1); } int cnt; foreach (i, p; PS) { if (uf.is_same(i.to!int, p-1)) ++cnt; } writeln(cnt); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto A = RD; auto B = RD; bool[long] set; set[A] = true; set[B] = true; foreach (i; 1..4) { if (set.get(i, false) == false) writeln(i); } stdout.flush; debug readln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto S = readln.chomp.to!(char[]); auto front = S[0..2]; auto rear = S[2..$]; auto front_ok = front != "00" && front.to!int <= 12; auto rear_ok = rear != "00" && rear.to!int <= 12; if (front_ok && rear_ok) { writeln("AMBIGUOUS"); } else if (front_ok) { writeln("MMYY"); } else if (rear_ok) { writeln("YYMM"); } else { writeln("NA"); } }
D
import std.stdio,std.conv, std.algorithm, std.container,std.array,std.range,std.string,std.typecons; const dx = [1,0,-1,0], dy = [0,1,0,-1]; const readMixin = q{ auto line = readln().split(); if (line.length < args.length) return; foreach(i,ref arg; args) arg = line[i].to!(typeof(arg)); }; void read(T...) (auto ref T args){ mixin(readMixin);} void readArray(T)(auto ref T args){ mixin(readMixin);} //------------------------------ const coin = [10,50,100,500]; void main(){ int total; read(total); while(total){ auto have = new int[4]; readArray(have); int howMany = int.max; auto uses = new int[4]; //(0,0,0,0) ~ (n0,n1,n2,n3) ?????§??? n0 * n1 * n2 * n3 ?????¶???????????£?????¨??¢?´¢ foreach(i0;iota(have[0]+1)){ foreach(i1;iota(have[1]+1)){ foreach(i2;iota(have[2]+1)){ foreach(i3;iota(have[3]+1)){ auto tmpuses = [i0,i1,i2,i3]; auto pay = i0 * coin[0] + i1 * coin[1] + i2 * coin[2] + i3 * coin[3]; if (pay < total) continue; int tmpHowMany = have.sum + howManyCoins(pay - total) - tmpuses.sum; if (tmpHowMany < howMany ){ howMany = tmpHowMany; uses = tmpuses; } } } } } foreach(i,u;uses){if(u >0)writeln(coin[i]," ",u);} read(total); if (total) writeln(); } } int howManyCoins(int pay){ int res; foreach_reverse(i,c;coin){ while(pay >= c){ pay -= c;res ++;} } return res; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; auto DS = [[1,0], [-1,0], [0,1], [0,-1]]; wchar[50][50] MAP; void main() { auto hw = readln.split.to!(int[]); auto H = hw[0]; auto W = hw[1]; foreach (i; 0..H) { foreach (j, c; readln.chomp.to!(wchar[])) { MAP[i][j] = c; } } foreach (y, line; MAP[0..H]) { foreach (x, c; line[0..W]) { if (c == '#') { bool valid; foreach (d; DS) { auto xd = x + d[0]; auto yd = y + d[1]; if (xd < 0 || xd >= W || yd < 0 || yd >= H) continue; if (MAP[yd][xd] == '#') { valid = true; break; } } if (!valid) { writeln("No"); return; } } } } writeln("Yes"); }
D
import std.stdio,std.conv,std.string,std.array; void main(){ auto a=readln.chomp.split; int x = to!int(a[0]), y = to!int(a[1]); writeln( x*y , " " , (x+y)*2); }
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } void main(){ long l = read.to!long; long n = read.to!long; long[] xs; foreach(i; 0 .. n) xs ~= read.to!long; // 右から始め、右にi本、左にj本焼いて右で終わるとする // このとき折り返しの回数は min(i - 1, j) を f とすると右も左も f // 折り返すのは i - f - 1 番から i - 2 番までと、i 番から i + f - 1 番まで // 折り返さないのは i - 1 番 // sum xs[i - f - 1 .. i - 1] * 2 + xs[i - 1] + l * f * 2 - sum xs[i .. i + f] * 2 // 左から始め、右にi本、左にj本焼いて左で終わるとする // このとき折り返しの回数は min(i, j - 1) を f とすると右も左も f // 折り返すのは i - f 番から i - 1 番までと、i + 1 番から i + f 番まで // 折り返さないのは i 番 // sum xs[i - f .. i] * 2 + (l - xs[i]) + l * f * 2 - sum xs[i + 1 .. i + f + 1] * 2 // 左から始め、右にi本、左にj本焼いて右で終わるとする // このとき折り返しの回数は min(i, j) を f とすると右は f - 1 で左は f // 折り返すのは i - f 番から i - 2 番までと、i 番から i + f - 1 番まで // 折り返さないのは i - 1 番 // sum xs[i - f .. i - 1] * 2 + xs[i - 1] + l * f * 2 - sum xs[i .. i + f] * 2 // 右から始め、右にi本、左にj本焼いて左で終わるとする // このとき折り返しの回数は min(i, j) を f とすると右は f で左は f - 1 // 折り返すのは i - f 番から i - 1 番までと、i + 1 番から i + f - 1番まで // 折り返さないのは i 番 // sum xs[i - f .. i] * 2 + (l - xs[i]) + l * f * 2 - sum xs[i + 1 .. i + f] * 2 long[] sum = [0]; // sum[i] は0からiの前までの和 foreach(i; 0 .. n) sum ~= sum[$ - 1] + xs[i]; long ans = -1; foreach(i; 1 .. n + 1){ long j = n - i; long f = min(i - 1, j); long a = (sum[i - 1] - sum[i - f - 1]) * 2 + xs[i - 1] + l * f * 2 - (sum[i + f] - sum[i]) * 2; if(a > ans) ans = a; debug writeln("i: ", i); debug writeln("a: ", a); debug writeln("ans: ", ans); } foreach(i; 0 .. n){ long j = n - i; long f = min(i, j - 1); long a = (sum[i] - sum[i - f]) * 2 + (l - xs[i]) + l * f * 2 - (sum[i + f + 1] - sum[i + 1]) * 2; if(a > ans) ans = a; debug writeln("i: ", i); debug writeln("a: ", a); debug writeln("ans: ", ans); } foreach(i; 1 .. n){ long j = n - i; long f = min(i, j); long a = (sum[i - 1] - sum[i - f]) * 2 + xs[i - 1] + l * f * 2 - (sum[i + f] - sum[i]) * 2; if(a > ans) ans = a; debug writeln("i: ", i); debug writeln("a: ", a); debug writeln("ans: ", ans); } foreach(i; 1 .. n){ long j = n - i; long f = min(i, j); long a = (sum[i] - sum[i - f]) * 2 + (l - xs[i]) + l * (f - 1) * 2 - (sum[i + f] - sum[i + 1]) * 2; if(a > ans) ans = a; debug writeln("i: ", i); debug writeln("a: ", a); debug writeln("ans: ", ans); } ans.writeln; }
D
import std.stdio, std.string, std.conv; import std.algorithm, std.array; void main() { for(string s_; (s_=readln.chomp()).length;) { auto NL = s_.split.map!(to!int); immutable N=NL[0], L=NL[1]; immutable a = readln.split.map!(to!int).array(); int k=0,m=-1; foreach(i;1..N) if(m<a[i-1]+a[i]) k=i,m=a[i-1]+a[i]; if(m<L) writeln("Impossible"); else { writeln("Possible"); foreach(i;1..k) writeln(i); foreach_reverse(i;k+1..N) writeln(i); writeln(k); } } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } int calc(int n, bool[][] adj) { auto refs = new int[](n + 1); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (adj[i][j]) refs[j]++; } } int ans = 0; bool update = true; while (update) { update = false; for (int i = 1; i <= n; i++) { if (refs[i] == 1) { ans++; update = true; refs[i] = 0; for (int j = 1; j <= n; j++) { if (adj[i][j]) { refs[j]--; } } } } } return ans; } void main() { auto nm = readints; int n = nm[0], m = nm[1]; auto adj = new bool[][](n + 1, n + 1); for (int i = 0; i < m; i++) { auto ab = readints; int a = ab[0], b = ab[1]; adj[a][b] = adj[b][a] = true; } writeln(calc(n, adj)); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; struct UFTree(T) { struct Node { T parent; T rank = 1; } /// this(T n) { nodes.length = n; sizes.length = n; foreach (i, ref node; nodes) { node = Node(i.to!T); sizes[i] = 1; } } /// bool unite(T a, T b) { a = root(a); b = root(b); if (a == b) return false; if (nodes[a].rank < nodes[b].rank) { sizes[nodes[a].parent] += sizes[nodes[b].parent]; nodes[b].parent = nodes[a].parent; } else { sizes[nodes[b].parent] += sizes[nodes[a].parent]; nodes[a].parent = nodes[b].parent; if (nodes[a].rank == nodes[b].rank) nodes[a].rank += 1; } return true; } /// bool is_same(T a, T b) { return root(a) == root(b); } /// T size(T i) { return sizes[root(i)]; } private: Node[] nodes; T[] sizes; T root(T i) { if (nodes[i].parent == i) return i; return nodes[i].parent = root(nodes[i].parent); } } /// UFTree!T uftree(T)(T n) { return UFTree!T(n); } void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; auto uft = uftree(N+M); foreach (int n; 0..N) { foreach (l; readln.split.to!(int[])[1..$]) { uft.unite(n, N+l-1); } } foreach (int i; 0..N-1) { if (!uft.is_same(i, i+1)) { writeln("NO"); return; } } writeln("YES"); }
D
import std.stdio; import std.algorithm; import std.string; import std.conv; void main() { int n, x; scanf("%d %d\n", &n, &x); auto ls = readln.chomp.split.to!(int[]); int d, cnt; foreach(l; ls) { d += l; ++cnt; if (d > x) { cnt.write; return; } } write(n+1); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int N, W; scan(N, W); auto w = new int[](N); auto v = new int[](N); iota(N).each!(i => scan(w[i], v[i])); auto dp = new long[](W + 1); foreach (i ; 0 .. N) { foreach_reverse (j ; w[i] .. W + 1) { chmax(dp[j], dp[j - w[i]] + v[i]); } } auto ans = dp[W]; writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import 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 x, y, count; scan(x, y); while(x <= y) { x *= 2; count++; } count.writeln(); }
D
import std.stdio, std.range, std.conv, std.string; import std.algorithm.comparison, std.algorithm.iteration, std.algorithm.mutation, std.algorithm.searching, std.algorithm.setops, std.algorithm.sorting; void main() { long[] input = readln().split.to!(long[]); long A = input[0]; long B = input[1]; writeln(A*B/2); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { auto n = lread(); auto a = aryread(); // writeln(a); auto s = new long[](n + 1); s[1] = a[0]; foreach (i; 0 .. n) { s[i + 1] = s[i] + a[i]; } // writeln(s); long ans; long mod = (10 ^^ 9) + 7; long aj; foreach (i; 0 .. n) { // writeln(a[i], " ", s[n] - s[i + 1]); a[i] %= mod; aj = s[n] - s[i + 1]; aj %= mod; ans += a[i] * aj; ans %= mod; } writeln(ans % mod); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range; import std.bigint; void main(){ long[] ins = readln.chomp.split.map!(to!long).array; long n = ins[0], x = ins[1]; long y = n - x; long a, b; if(x > y) a = x, b = y; else a = y, b = x; long ans = a + b; while(b > 0){ long r = a % b; ans += b * 2 * (a / b); a = b; b = r; } ans -= a; ans.writeln; }
D
import std.stdio, std.string, std.conv, std.array, std.algorithm; void main() { auto a = readln.split.map!(to!int); int count = 0; foreach (i; a[0]..a[1]+1) if (a[2] % i == 0) count++; writeln(count); }
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 solve() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto A = readln.split.map!(x => x.to!int-1).array; auto B = readln.split.map!(x => x.to!int-1).array; auto C = new int[](N); foreach (i; 0..N) C[A[i]] = i; long ans = 0; int mx = -1; long cnt = 0; foreach (b; B) { if (C[b] < mx) { ans += 1; } else { mx = C[b]; ans += (C[b] - cnt) * 2 + 1; } cnt += 1; } ans.writeln; } void main() { auto T = readln.chomp.to!int; while (T--) solve; }
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, std.bitmanip; // }}} // 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; string s = cin.next!string; if (s[2] == s[3] && s[4] == s[5]) { writeln("Yes"); } else { writeln("No"); } }
D
//prewritten code: https://github.com/antma/algo import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.traits; final class InputReader { private: ubyte[] p, buffer; bool eof; bool rawRead () { if (eof) { return false; } p = stdin.rawRead (buffer); if (p.empty) { eof = true; return false; } return true; } ubyte nextByte(bool check) () { static if (check) { if (p.empty) { if (!rawRead ()) { return 0; } } } auto r = p.front; p.popFront (); return r; } public: this () { buffer = uninitializedArray!(ubyte[])(16<<20); } bool seekByte (in ubyte lo) { while (true) { p = p.find! (c => c >= lo); if (!p.empty) { return false; } if (!rawRead ()) { return true; } } } template next(T) if (isSigned!T) { T next () { if (seekByte (45)) { return 0; } T res; ubyte b = nextByte!false (); if (b == 45) { while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 - (b - 48); } } else { res = b - 48; while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 + (b - 48); } } } } template next(T) if (isUnsigned!T) { T next () { if (seekByte (48)) { return 0; } T res = nextByte!false () - 48; while (true) { ubyte b = nextByte!true (); if (b < 48 || b >= 58) { break; } res = res * 10 + (b - 48); } return res; } } T[] nextA(T) (in int n) { auto a = uninitializedArray!(T[]) (n); foreach (i; 0 .. n) { a[i] = next!T; } return a; } } void main() { auto r = new InputReader (); immutable nt = r.next!uint (); foreach (tid; 0 .. nt) { const n = r.next!uint (); const m = r.next!uint (); auto a = r.nextA!uint (n); const s = sum (a[1 .. $]); int delta = min (m - a[0], s); writeln (a[0] + delta); } }
D
void main() { dchar[] s = rdDchar; dchar[] t = rdDchar; long pos = t.length - 1; bool ok; foreach_reverse (long i, x; s) { if (x == t[pos] || x == '?') { bool check; if (i - pos >= 0) { check = true; foreach (j; 0 .. pos) { if (s[i-pos+j] != t[j] && s[i-pos+j] != '?') { check = false; break; } } } if (check) { ok = true; foreach (j; 0 .. pos+1) { s[i-pos+j] = t[j]; } foreach (j; 0 .. s.length) { if (s[j] == '?') s[j] = 'a'; } break; } } } writeln(ok ? s.to!string : "UNRESTORABLE"); } /* void main() { dchar[] s = rdDchar; dchar[] t = rdDchar; long last = t.length - 1; bool ok; foreach_reverse (i, x; s) { if (x == t[last] || x == '?') { bool check = true; if (i.to!long - last >= 0) { foreach (j; 0 .. last) { if (s[i-last+j] != t[j] && s[i-last+j] != '?') { check = false; break; } } } else check = false; if (check) { ok = true; foreach (j; 0 .. last+1) { s[i-last+j] = t[j]; } foreach (j; 0 .. s.length) { if (s[j] == '?') s[j] = 'a'; } break; } } } if (ok) s.writeln; else "UNRESTORABLE".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.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; void main() { foreach(int i; 0..7) { int[] input = readln.split.to!(int[]); writeln(input[0]-input[1]); } }
D
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; import std.numeric; alias Pair = Tuple!(int, "to", int, "cost"); int n, q, k; Pair[][] adj; void main() { scan(n); adj = new Pair[][](n, 0); int ai, bi, ci; foreach (i ; 0 .. n - 1) { scan(ai, bi, ci); ai--; bi--; adj[ai] ~= Pair(bi, ci); adj[bi] ~= Pair(ai, ci); } scan(q, k); k--; auto d = new long[](n); void dfs(int u, int p, long c) { d[u] = c; foreach (v ; adj[u]) { if (v.to == p) continue; dfs(v.to, u, c + v.cost); } } dfs(k, -1, 0); int xi, yi; while (q--) { scan(xi, yi); xi--; yi--; long ans = d[xi] + d[yi]; writeln(ans); } } long lcm(long a, long b) { return a / gcd(a, b) * b; } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.array; import std.conv; void main() { while (1) { auto input = split(readln()); switch (input[1]) { case "+": writeln(to!(int)(input[0]) + to!(int)(input[2])); break; case "-": writeln(to!(int)(input[0]) - to!(int)(input[2])); break; case "*": writeln(to!(int)(input[0]) * to!(int)(input[2])); break; case "/": writeln(to!(int)(input[0]) / to!(int)(input[2])); break; case "?": return; default: writeln("ERROR"); break; } } }
D
import std.stdio, std.algorithm, std.conv, std.string, std.array, std.math; void main() { readln; const s = readln.chomp; if (s.length%2 == 0 && s[0..$/2] == s[$/2..$]) { "Yes".writeln; return; } "No".writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; alias P = Tuple!(size_t, "x", size_t, "y"); void main() { auto hw = readln.split.to!(int[]); auto H = hw[0]; auto W = hw[1]; char[][] AS; AS.length = H; foreach (i; 0..H) { AS[i] = readln.chomp.to!(char[]); } int[][] BS; BS.length = H; foreach (ref bs; BS) { bs.length = W; bs[] = -1; } P[] ps; foreach (y, as; AS) { foreach (x, c; as) { if (c == '#') { ps ~= P(x, y); } } } int t; while (!ps.empty) { P[] nps; foreach (p; ps) { if (BS[p.y][p.x] != -1) continue; BS[p.y][p.x] = t; foreach (xy; [[1, 0], [0, 1], [-1, 0], [0, -1]]) { auto x = p.x + xy[0]; auto y = p.y + xy[1]; if (x < 0 || x >= W || y < 0 || y >= H) continue; nps ~= P(x, y); } } ps = nps; ++t; } auto r = int.min; foreach (bs; BS) foreach (b; bs) r = max(r, b); writeln(r); }
D
/+ dub.sdl: name "hello" +/ import std.algorithm; import std.conv; import std.stdio; import std.string; void main() { auto N = readln.chomp.to!int; auto A = readln.chomp.to!int; writeln(N * N - A); }
D
import std.stdio; import std.algorithm; import std.range; import std.conv; import std.format; import std.array; import std.math; import std.string; import std.container; import std.numeric; void main() { int N, M; readlnTo(N, M); long ans = 1; for (long i = 1; i * i <= M; i++) { if (M % i != 0) continue; if (M >= i * N) ans = max(ans, i); if (M >= (M / i) * N) ans = max(ans, M / i); } ans.writeln; } // helpers void readlnTo(T...)(ref T t) { auto s = readln.split; assert(s.length == t.length); foreach(ref ti; t) { ti = s[0].to!(typeof(ti)); s = s[1..$]; } }
D
import std.stdio; 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 s = readln.chomp; auto t = readln.chomp; auto res = false; foreach (i; 0..s.length) { auto f = true; foreach (j; 0..s.length) { if (s[j] != t[(j+i)%s.length]) { f = false; } } if (f) { res = true; } } if (res) { writeln("Yes"); } else { writeln("No"); } }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}} void main() { int n, wt; readV(n, wt); int[] w, v; readC(n, w, v); auto u = w.dup; u[] -= w[0]; auto dp = new int[][][](n+1, n+1, n*3+1); foreach (i; 0..n) foreach (j; 0..n+1) dp[i][j][] = -1; dp[0][0][0] = 0; foreach (i; 0..n) foreach (j; 0..n+1) foreach (k; 0..n*3+1) { dp[i+1][j][k] = dp[i][j][k]; if (j >= 1 && k >= u[i] && dp[i][j-1][k-u[i]] >= 0) dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j-1][k-u[i]]+v[i]); } auto ans = 0; foreach (j; 0..n+1) foreach (k; 0..n*3+1) if (j.to!long*w[0]+k <= wt) ans = max(ans, dp[n][j][k]); writeln(ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto av = readln.split.to!(long[]); auto A = av[0]; auto V = av[1]; auto bw = readln.split.to!(long[]); auto B = bw[0]; auto W = bw[1]; auto T = readln.chomp.to!long; if (A == B) { writeln("YES"); return; } writeln(V * T - W * T >= abs(A - B) ? "YES" : "NO"); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; import std.range; void main() { auto data = readln().split(); auto A = data[0].to!int(), B = data[1].to!int(), K = data[2].to!int(); iota(min(A, B), 0, -1).filter!(x => A%x == 0 && B%x == 0).array[K-1].writeln(); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); ulong bignum = 1_000_000_007; alias SugarWater = Tuple!(long, "swater", long, "sugar"); 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 a, b; scan(a, b); if (a % 2) calcans(a, b, a); else { if (b - a < 3) { long ans = a; foreach (e; a .. b) ans ^= e + 1; ans.writeln(); } else calcans(a + 3, b, 0); } } void calcans(long a, long b, long c) { long skip = (b - a) / 4; skip *= 4; long start = a + skip, ans = c; foreach (e; start .. b) ans ^= e + 1; ans.writeln(); }
D
import std.stdio; void main(){ foreach(i; 1 .. 10){ foreach(j; 1 .. 10){ writeln(i, "x", j, "=", i * j); } } }
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 xkd = readln.split.to!(long[]); auto X = xkd[0]; auto K = xkd[1]; auto D = xkd[2]; X = abs(X); if (X/D >= K) { writeln(X - K*D); return; } auto k = K - X/D; if (k%2 == 0) { writeln(X - D*(X/D)); } else { writeln(abs(X - D*(X/D+1))); } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp.to!int; auto a = readln.chomp.split.to!(int[]); int odd, even, even4; foreach (i; 0..n) { if (a[i] % 4 == 0) { even4++; } else if (a[i] % 2 == 0) { even++; } else { odd++; } } if (even4 >= n / 2) { writeln("Yes"); } else if (even4 - odd >= 0) { writeln("Yes"); } else { writeln("No"); } }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; 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 main() { long r, g, b, n; scan(r, g, b, n); long count; foreach (i; 0 .. n + 1) { foreach (j; 0 .. n + 1) { long difference = n - r * i - g * j; if(difference < 0) { break; } if(difference % b == 0) { count++; } } } writeln(count); }
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.container; import std.datetime; int s; long number (string str) { auto l = s; s++; while (str[s] >= '0' && str[s] <= '9') s++; return str[l..s].to!long; } long factor (string str) { long res; if (str[s] == '(') { s++; res = expression(str); s++; } else { res = number(str); } return res; } long term (string str) { auto res = factor(str); while (1) { if (str[s] == '*') { s++; res *= factor(str); } else if (str[s] == '/') { s++; res /= factor(str); } else { break; } } return res; } long expression (string str) { auto res = term(str); while (1) { if (str[s] == '+') { s++; res += term(str); } else if (str[s] == '-') { s++; res -= term(str); } else { break; } } return res; } void main() { auto n = readln.chomp.to!int; foreach (i; 0..n) { s = 0; auto eqn = readln.chomp.to!string; eqn.expression.writeln; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto ps = readln.split.to!(int[]); auto qs = new bool[](N+1); int r, last = 1; foreach_reverse (p; ps) { qs[p] = true; if (p == last) ++r; while (last <= N && qs[last]) ++last; } writeln(r); }
D
import std.stdio; import std.typecons; void main() { int r, c, n, k; scanf("%d%d%d%d", &r, &c, &n, &k); Tuple!(int, int)[] alts = new Tuple!(int, int)[n]; for (int i = 0; i < n; i++) scanf("%d%d", &alts[i][0], &alts[i][1]); int res = 0; for (int i = 1; i <= r; i++) for (int j = 1; j <= c; j++) for (int p = i; p <= r; p++) for (int q = j; q <= c; q++) { int count = 0; foreach (Tuple!(int, int) alt; alts) if (alt[0] >= i && alt[0] <= p && alt[1] >= j && alt[1] <= q) count++; if (count >= k) res++; } printf("%d\n", res); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.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 long[](t); foreach (ti; 0..t) { auto n = RD!int; auto a = RDA; auto cnt = new long[](63); foreach (i; 0..n) { foreach_reverse (j; 0..63) { auto bit = 1L << j; if (a[i] & bit) { ++cnt[j]; break; } } } foreach (i; 0..63) { ans[ti] += cnt[i]*(cnt[i]-1)/2; } } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto T = readln.chomp.to!int; auto ps = new long[](10^^6+1); foreach (i; 2..10^^6+1) if (ps[i] == 0) { ps[i] = i; auto x = i; while (x <= 10^^6) { if (ps[x] == 0) ps[x] = i; x += i; } } foreach (_t; 0..T) { auto nk = readln.split.to!(long[]); auto N = nk[0]; auto K = nk[1]; writeln(N + ps[N.to!size_t] + 2*(K-1)); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new int[](t); foreach (ti; 0..t) { auto a = RD; auto b = RD; auto n = RD; while (max(a, b) <= n) { if (a < b) a += b; else b += a; ++ans[ti]; } } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { int k, p; scan(k, p); if (p == 1) { writeln(0); return; } long ans; foreach (i ; 1 .. k + 1) { long zcy = 1L * i * 10L^^(digit(i)) + revnum(i); (ans += zcy) %= p; } writeln(ans); } int digit(int num) { return num > 0 ? digit(num / 10) + 1 : 0; } long revnum(long n) { long res; while (n > 0) { res *= 10; res += (n % 10); n /= 10; } return res; } unittest { assert(revnum(0) == 0); assert(revnum(8) == 8); assert(revnum(10) == 1); assert(revnum(123) == 321); assert(revnum(10100) == 101); writeln("ok"); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.conv, std.string; import std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } int DEBUG_LEVEL = 0; void print()(){ writeln(""); } void print(T, A ...)(T t, lazy A a){ write(t), print(a); } void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); } const long mod = 1_000_000_007; void main(string[] args){ if(args.length > 1 && args[1] == "-debug"){ if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int; else DEBUG_LEVEL = 1; } int n = read.to!int; long t = read.to!long; long[] ss, ds; foreach(i; 0 .. n){ ss ~= read.to!long; ds ~= read.to!long; } long bestx = 10_000_000, ans = -1; foreach(i; 0 .. n){ long s = ss[i], d = ds[i]; long x; if(s >= t) x = s; else x = s + (t - s + d - 1) / d * d; if(x < bestx){ bestx = x; ans = i + 1; } } ans.writeln; }
D
import std.stdio :write, writeln; import std.array; import std.range; import std.typecons; import std.algorithm : max, min; string[] src = [ "111111101010101111100101001111111", "100000100000000001010110001000001", "101110100110110000011010001011101", "101110101011001001111101001011101", "101110101100011000111100101011101", "100000101010101011010000101000001", "111111101010101010101010101111111", "000000001111101111100111100000000", "100010111100100001011110111111001", "110111001111111100100001000101100", "011100111010000101000111010001010", "011110000110001111110101100000011", "111111111111111000111001001011000", "111000010111010011010011010100100", "101010100010110010110101010000010", "101100000101010001111101000000000", "000010100011001101000111101011010", "101001001111101111000101010001110", "101101111111000100100001110001000", "000010011000100110000011010000010", "001101101001101110010010011011000", "011101011010001000111101010100110", "111010100110011101001101000001110", "110001010010101111000101111111000", "001000111011100001010110111110000", "000000001110010110100010100010110", "111111101000101111000110101011010", "100000100111010101111100100011011", "101110101001010000101000111111000", "101110100011010010010111111011010", "101110100100011011110110101110000", "100000100110011001111100111100000", "111111101101000101001101110010001" ]; void main(){ string v = src[ next!int ]; v[ next!int ].writeln; } import std.stdio : readln; import std.conv : to; import std.string : split, chomp; import std.traits; string[] input; string delim = " "; T next(T)() in { assert(hasNext()); } out { input.popFront; } body { return input.front.to!T; } bool hasNext(){ if(input.length > 0){ return true; } string str = readln; if(str.length > 0){ input ~= str.chomp.split(delim); return true; }else{ return false; } } void dbg(T...)(T vs) { import std.stdio : stderr; foreach(v; vs) stderr.write(v.to!string ~ " "); stderr.write("\n"); } T clone(T)(T v){ T v_; static if(isInputRange!(T)){ foreach(ite; v){ v_ ~= ite.clone; } }else{ v_ = v; } return v_; }
D
module main; import core.stdc.stdio; int main(string[] argv) { int n = 0; scanf("%d", &n); if((n & 1) == 1) { n -= 3; putchar('7'); } for(int i = 0; i < n; i += 2) putchar('1'); return 0; }
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, std.bitmanip; // }}} // 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; int n; long res; cin.scan(n); foreach (i; 1 .. n + 1) { if (!(i % 3 == 0 || i % 5 == 0)) { res += i; } } writeln(res); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; long P = 10^^9+7; void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; auto SS = readln.split.to!(int[]); auto TS = readln.split.to!(int[]); auto DP = new long[][](N+1, M+1); foreach (i; 1..N+1) { foreach (j; 1..M+1) { DP[i][j] = (DP[i][j-1] + DP[i-1][j] - DP[i-1][j-1] + P) % P; if (SS[i-1] == TS[j-1]) { (DP[i][j] += DP[i-1][j-1] + 1) %= P; } } } writeln((DP[N][M] + 1) % P); }
D
void main() { problem(); } void problem() { auto N = scan!int; auto A = scan!long(N); auto Q = scan!int; auto Queues = Q.iota.map!(x => Queue(scan!long, scan!long)).array; void solve() { long[long] nums; long summary; foreach(a; A) { nums[a]++; summary += a; } foreach(q; Queues) { if (q.from in nums) { nums[q.to] += nums[q.from]; summary += (q.to - q.from) * nums[q.from]; nums.remove(q.from); } writeln(summary); } return; } solve(); } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Queue = Tuple!(long, "from", long, "to"); // -----------------------------------------------
D
import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; void main(){ int n, t; { int[] tmp = readln.chomp.split.map!(to!int).array; n = tmp[0], t = tmp[1]; } int[] as = readln.chomp.split.map!(to!int).array; int[] bs; bs.length = as.length; int b = bs[$ - 1]; for(int i = as.length.to!int - 1; i >= 0; i --){ if(b < as[i]) b = as[i]; bs[i] = b; } int[] cs; cs.length = as.length; for(int i = 0; i < cs.length; i ++) cs[i] = bs[i] - as[i]; int ans; int max = -1; foreach(c; cs){ if(c > max) max = c, ans = 1; else if(c == max) ans += 1; } ans.writeln; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { int a = readint; int b = readint; int c = readint; int x = readint; int ans = 0; for (int i = 0; i <= a; i++) { // 500 for (int j = 0; j <= b; j++) { // 100 for (int k = 0; k <= c; k++) { // 50 int t = 500 * i + 100 * j + 50 * k; if (t == x) { ans++; } } } } writeln(ans); }
D
void main() { auto N = ri; if(N <= 999) { "ABC".writeln; } else { "ABD".writeln; } } // =================================== import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.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 H = RD; auto W = RD; writeln(min(H, W) == 1 ? 1 : (H*W+1) / 2); stdout.flush; debug readln; }
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(){ auto k = readLong(); long n = 50; auto x = k % n; writeln(n); foreach(i; iota(n)) { if(i < x) { write(n - i + k/n, " \n"[i == n-1 ? 1: 0]); } else { write(n - i - 1 + k/n, " \n"[i == n-1 ? 1: 0]); } } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N, R; scan(N, R); if (10 <= N) { writeln(R); } else { writeln(R + 100 * (10 - N)); } }
D
import std.stdio; import std.regex; void main(){ //auto io = new IO(); char[][char] card; card['a'] = str();//io.rect!char()[0]; card['b'] = str(); card['c'] = str(); char[char] ptr; ptr['a']=ptr['b']=ptr['c']=0; //= ["a":0,"b":0,"c":0]; char target = 'a'; foreach( i ; 0..100*100*100+10 ){ auto now = ptr[target]; if ( now >= card[target].length ){ target.toUpper().writeln(); return; } ptr[target]++; target = card[target][now]; } } char[] str(){ char[] ret; foreach( elm ; readln().chomp().split() ){ //ret ~= elm.to!char(); ret ~= elm; } return ret; } import std.stdio,std.conv,std.string,std.uni; import std.algorithm,std.array,std.math; import std.bigint;
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 A, P; scan(A, P); long x = A * 3 + P; writeln(x / 2); }
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 a, b, c; scan(a, b, c); auto ans = min(b / a, c); writeln(ans); } int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new int[][](n, 0); foreach (i ; 0 .. m) { int u, v; scan(u, v); if (is1indexed) { u--, v--; } adj[u] ~= v; if (isUndirected) { adj[v] ~= u; } } return adj; } alias Edge = Tuple!(int, "to", int, "cost"); Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new Edge[][](n, 0); foreach (i ; 0 .. m) { int u, v, c; scan(u, v, c); if (is1indexed) { u--, v--; } adj[u] ~= Edge(v, c); if (isUndirected) { adj[v] ~= Edge(u, c); } } return adj; } void yes(bool b) {writeln(b ? "Yes" : "No");} void YES(bool b) {writeln(b ? "YES" : "NO");} T[] readArr(T)() {return readln.split.to!(T[]);} T[] readArrByLines(T)(int n) {return iota(n).map!(i => readln.chomp.to!T).array;} void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
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);}} alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop; // dfmt on immutable string YES = "Yes"; immutable string NO = "No"; void main() { auto input = stdin.byLine.map!split.joiner; string N; N = input.front.to!string; input.popFront; solve(N); } void solve(string N) { long x; foreach (c; N) { x += c - '0'; x %= 9; } writeln(x == 0 ? YES : NO); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "l ", long, "r"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long n, k; scan(n, k); auto ary = new long[](MAX); foreach (_; iota(n)) { long a, b; scan(a, b); ary[a] += b; } long ans, s; foreach (i; iota(MAX)) { s += ary[i]; if(s >= k) { ans = i; break; } } writeln(ans); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import 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, "begin", long, "end"); 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, a, b; scan(n, a, b); (min(a * n, b)).writeln(); }
D
unittest { assert( [ "0011" ].parse.expand.solve == 4 ); assert( [ "11011010001011" ].parse.expand.solve == 12 ); assert( [ "0" ].parse.expand.solve == 0 ); } import std.conv; import std.range; import std.stdio; import std.typecons; void main() { stdin.byLineCopy.parse.expand.solve.writeln; } auto parse( Range )( Range input ) if( isInputRange!Range && is( ElementType!Range == string ) ) { auto s = input.front; return tuple( s ); } auto solve( string s ) { // Sを連続する同じ色のキューブの数の配列に変換 auto p = '9'; long[] ns; foreach( c; s ) { if( p != c ) { ns ~= 1L; p = c; } else { ns[ $ - 1 ]++; } } // 取り除けるキューブの数を計算 auto ans = 0L; auto r1 = ns[ 0 ]; auto r2 = 0L; foreach( n; ns[ 1 .. $ ] ) { if( n < r1 ) { ans += n * 2; r2 = r1 - n; r1 = 0L; } else { ans += r1 * 2; r1 = r2 + n - r1; r2 = 0L; } } return ans; }
D
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.typecons, std.range, std.container, std.math, std.algorithm.searching, std.functional,std.mathspecial; void main(){ auto N=readln.chomp.to!int; if(N==1){ writeln(1); return; } long[int] divs; auto primes=get_primes(N); foreach(p;primes){ divs[p]=0; } foreach(n;2..N+1){ foreach(p;primes){ while(n%p==0){ n/=p; divs[p]++; } if(n==1)break; } } auto res=1L; foreach(d;divs.values){ res=res*(d+1)%1000000007; } writeln(res); } int[] get_primes(int max){ int[] ps; foreach(n;2..max+1){ if(isPrime(n,ps))ps~=n; } return ps; } bool isPrime(int n, int[] prims){ foreach(p;prims){ if(n%p==0)return false; } return true; }
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; int n = cin.next!int; int[] v = cin.nextArray!int(n); int[] c = cin.nextArray!int(n); int res = int.min; foreach (i; 0 .. 1 << n) { int vs, cs; foreach (j; 0 .. n) { if (i & (1 << j)) { vs += v[j]; cs += c[j]; } } res = max(res, vs - cs); } res.writeln; }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { auto tmp = readln.split.to!(long[]); const D = tmp[0], G = tmp[1]; struct Po { long p, c; } auto po = new Po[D]; auto completePointList = new long[D]; foreach (i; 0..D) { tmp = readln.split.to!(long[]); const p = tmp[0], c = tmp[1]; po[i] = Po(p,c); completePointList[i] = 100 * (i+1) * p + c; } long calc(bool[] s) { long problem = 0; long point = 0; foreach (i, b; s) { if (!b) continue; point += 100 * (i+1) * po[i].p + po[i].c; problem += po[i].p; } foreach_reverse (i; 0..D) { if (s[i]) continue; if (point >= G) return problem; const cnt = cast(long)min(po[i].p-1, ceil((G - point) / (100.0 * (i+1)))); problem += cnt; point += 100 * (i+1) * cnt; } return point < G ? 114514 : problem; } long func(long i, bool[] s) { if (i == D) { return calc(s); } return min(func(i+1, s.dup ~ false), func(i+1, s.dup ~ true)); } writeln(func(0,[])); }
D
void main() { long x = rdElem; bool op(long len, long cnt) { if (len <= 2) return cnt <= x; return op((len-1)>>1, cnt+1); } long ok, ng = inf; while (ng - ok > 1) { long m = (ok + ng) >> 1; if (op(m, 0)) ok = m; else ng = m; } ok.writeln; } enum long mod = 10L^^9 + 7; enum long inf = 1L << 60; enum double eps = 1.0e-9; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
import std.stdio, std.conv, std.string, std.range, std.algorithm; void main() { int d = readln.chomp.to!int; ("Christmas " ~ repeat("Eve", 25 - d).join(" ")).writeln; }
D
import std.stdio; import std.string; import std.conv; void main() { int n; scanf("%d\n", &n); auto ds = readln.chomp.split.to!(long[]); long total = 0; foreach(i; 0..ds.length) { foreach(j; i+1..ds.length) { total += ds[i] * ds[j]; } } total.write; }
D
import std.stdio; import std.string; import std.algorithm; import std.conv; void main() { int N=to!int(chomp(readln())); string[] input=split(readln()); int ans=1<<30; foreach(item;input) { int A=to!int(item); int cnt=0; while(A%2==0) A/=2,cnt++; ans=min(ans,cnt); } writeln(ans); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { auto n = lread(); auto h = aryread(); foreach (i; 0 .. (n - 1)) { if (h[i] > h[i + 1]) { h[i + 1] += 1; } } // writeln(h); foreach (i; 0 .. (n - 1)) { if (h[i] <= h[i + 1]) { continue; } else { writeln("No"); return; } } writeln("Yes"); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n; scan(n); auto a = readln.split.to!(int[]); int p = 1; int ans; foreach (ai ; a) { if (ai == p) { p++; } else { ans++; } } writeln(ans == n ? -1 : ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio; import std.conv; import std.string; void main() { string s = chomp(readln()); // ?????????????????? long l = to!long(s); writeln(l*l*l); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto AS = readln.split.to!(int[]); int all = 1, odd = 1; foreach (a; AS) { int x, y; foreach (i; a-1..a+2) { ++x; if (i&1) ++y; } all *= x; odd *= y; } writeln(all - odd); }
D
void main() { int[] tmp = readln.split.to!(int[]); int a = tmp[0], b = tmp[1], c = tmp[2]; writeln(a <= c && c <= b ? "Yes" : "No"); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { long A, B, C; scan(A, B, C); long K = lread(); long m = A.max(B).max(C); writeln(A + B + C + (m * ((2 ^^ K) - 1))); }
D
import std.algorithm; import std.conv; import std.stdio; import std.string; void main() { for (;;) { string line = readln.strip; if (line == "0") { break; } int sum = line.split("").map!(to!int).sum(); writeln(sum); } }
D