code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio, std.array, std.conv, std.typecons, std.algorithm; T diff(T)(const T a, const T b) { return a > b ? a - b : b - a; } T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; } T[] readToArray(T)() { return readln.split.to!(T[]); } void main() { const ip = readToArray!(long); const auto n = ip[0], l = ip[1]; long sum = 0; for(long i = 0; i < n; i++) { sum += l + i; } if (l < 0 && l + n - 1 >= 0) { // nothing } else if (l + n - 1 < 0) { sum -= l + n - 1; } else { sum -= l; } writeln(sum); }
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; void main() { int n; scan(n); auto p = iota(n).map!(i => readln.chomp.to!int).array; auto dp = new int[](n + 1); int ans = 1<<30; foreach (i ; 0 .. n) { dp[p[i]] = dp[p[i] - 1] + 1; ans = min(ans, n - p[i] + p[i] - dp[p[i]]); } debug { writeln(dp); } writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std; void main() { auto S = readln.split[0]; if (S == "ABC") writeln("ARC"); else writeln("ABC"); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long K, X; scan(K, X); writeln(500 * K < X ? "No" : "Yes"); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto X = RD; auto Y = RD; auto Z = RD; writeln(Z, " ", X, " ", Y); stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD; auto r = RD; auto x = min(n-1, r); ans[ti] = x * (x+1) / 2; if (r >= n) ++ans[ti]; } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; int n, p; int[] a; void main() { scan(n, p); a = readln.split.to!(int[]); int lim = n * 100; auto dp = new long[](lim + 1); dp[0] = 1L; foreach (i ; 0 .. n) { foreach_reverse (j ; a[i] .. lim + 1) { dp[j] += dp[j - a[i]]; } } long ans; foreach (i ; 0 .. lim + 1) { if ((i & 1) == p) { ans += dp[i]; } } writeln(ans); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
// cheese-cracker [2022-01-27] void solve(){ auto n = scan!int; auto a = scanArray; auto b = scanArray; long mn = min(a[0], b[0]); long mx = max(a[0], b[0]); for(int i = 0; i < n; ++i){ mn = max(min(a[i], b[i]), mn); mx = max(max(a[i], b[i]), mx); } writeln(mn * mx); } void main(){ long tests = scan; // Toggle! while(tests--) solve; } /*_________________________*That's All Folks!*__________________________*/ import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math, std.numeric; string[] tk; alias tup = Tuple!(long, long); T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;} T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; } void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.random; struct ImplicitKeyTreapNode(Value, Extra=void) { enum has_extra = !(is (Extra == void)); ImplicitKeyTreapNode!(Value, Extra)* left, right; Value value; static if (has_extra) Extra extra; int y; int sz; this (Value _value) { value = _value; y = uniform!int; sz = 1; } } final class ImplicitKeyTreap(Value, Extra=void, alias relax_op="", alias push_op="", alias update_op="") { /* enum has_push = isCallable!push_op; enum has_relax = isCallable!relax_op; enum has_update = isCallable!update_op; */ enum has_push = true; enum has_relax = true; enum has_update = true; static assert (has_push == has_update); alias Node = ImplicitKeyTreapNode!(Value, Extra); Node *root; private static int _size (const Node *t) { return t ? t.sz : 0; } private static void _relax_inc (Node *t) { ++t.sz; static if (has_relax) relax_op (t); } private static void _relax_dec (Node *t) { --t.sz; static if (has_relax) relax_op (t); } private static void _relax (Node *t) { t.sz = 1; if (t.left) t.sz += t.left.sz; if (t.right) t.sz += t.right.sz; static if (has_relax) relax_op (t); } private static void _split (Node *t, ref Node *l, ref Node *r, int key) { if (!t) { l = r = null; return; } static if (has_push) push_op (t); int ls; if (t.left) ls = t.left.sz; if (key <= ls) { _split (t.left, l, t.left, key); r = t; } else { _split (t.right, t.right, r, key - ls - 1); l = t; } _relax (t); } private static void _build (ref Node *t, Value[] v) { if (v.length == 1) { t = new Node (v[0]); static if (has_relax) relax_op (t); } else { immutable m = v.length >> 1; Node* tl, tr; _build (tl, v[0 .. m]); _build (tr, v[m .. $]); _merge (t, tl, tr); } } private static void _insert (ref Node *t, Node *p, int pos) { if (!t) { static if (has_relax) relax_op (p); t = p; return; } else if (p.y >= t.y) { _split (t, p.left, p.right, pos); t = p; _relax (t); } else { static if (has_push) if (t) push_op (t); int ls; if (t.left) ls = t.left.sz; if (pos <= ls) { _insert (t.left, p, pos); _relax_inc (t); } else { _insert (t.right, p, pos - ls - 1); _relax_inc (t); } } } private static void _merge (ref Node *t, Node *l, Node *r) { if (!l) { static if (has_push) if (r) push_op (r); t = r; } else if (!r) { static if (has_push) push_op (l); t = l; } else { static if (has_push) { push_op (l); push_op (r); } if (l.y > r.y) { _merge (l.right, l.right, r); _relax (l); t = l; } else { _merge (r.left, l, r.left); _relax (r); t = r; } } } private static _replace (Node *t, int pos, Value value) { static if (has_relax) { Node*[128] path = void; int n; } while (true) { static if (has_push) push_op (t); static if (has_relax) { path[n++] = t; } int ls; if (t.left) ls = t.left.sz; if (pos == ls) { t.value = value; break; } if (pos < ls) { t = t.left; } else { t = t.right; pos -= ls + 1; } } static if (has_relax) { foreach_reverse (i; 0 .. n) { relax_op (path[i]); } } } private static Node* _get (Node* t, int pos) in { assert (pos >= 0); assert (pos < _size (t)); assert (t); } body { static if (has_push) push_op (t); if (t.left) { if (pos < t.left.sz) { return _get (t.left, pos); } pos -= t.left.sz; } if (!pos) { return t; } return _get (t.right, pos - 1); } private static void _remove (ref Node *t, int pos) in { assert (t); assert (pos >= 0); assert (pos < _size (t)); } body { static if (has_push) push_op (t); int ls; if (t.left) ls = t.left.sz; if (pos == ls) { _merge (t, t.left, t.right); } else if (pos < ls) { _remove (t.left, pos); assert (t); _relax_dec (t); } else { _remove (t.right, pos - ls - 1); assert (t); _relax_dec (t); } } void insert (int pos, Value value) { _insert (root, new Node (value), pos); } void remove (int pos) { _remove (root, pos); } void replace (int pos, Value value) { _replace (root, pos, value); } Value get (int pos) in { assert (pos < size ()); } body { return _get (root, pos).value; } int size () const { return _size (root); } Node*[] getNodes () { Node*[] a; a.reserve (_size (root)); void rec (Node *t) { if (t.left) rec (t.left); a ~= t; if (t.right) rec (t.right); } rec (root); return a; } Value[] getValues () { Value[] a; a.reserve (_size (root)); void rec (Node *t) { static if (has_push) push_op (t); if (t.left) rec (t.left); a ~= t.value; if (t.right) rec (t.right); } if (root) rec (root); return a; } static if (has_relax) { private Node[256] nodes; private int cur; private void persistent_split (Node *t, ref Node *l, ref Node *r, int key) { if (!t) { l = r = null; return; } int ls; if (t.left) ls = t.left.sz; Node *q = &nodes[cur++]; q.value = t.value; q.y = t.y; if (key <= ls) { q.right = t.right; persistent_split (t.left, l, q.left, key); r = q; } else { q.left = t.left; persistent_split (t.right, q.right, r, key - ls - 1); l = q; } _relax (q); } Extra reduce (int l, int r) { cur = 0; Node *t1, t2, t3; persistent_split (root, t1, t2, l); persistent_split (t2, t2, t3, r - l); return t2.extra; } } static if (has_update) { void update (int l, int r) { if (!l) { Node *t1, t2; _split (root, t1, t2, r); update_op (t1); _merge (root, t1, t2); } else { Node *t1, t2, t3; _split (root, t1, t2, l); _split (t2, t2, t3, r - l); update_op (t2); _merge (t2, t2, t3); _merge (root, t1, t2); } } } this (Value[] v) { _build (root, v); } } struct E { //ImplicitKeyTreapNode!(char, E) *parent; bool rev; } void relax (ImplicitKeyTreapNode!(char, E) *t) { //if (t.left) t.left.extra.parent = t; //if (t.right) t.right.extra.parent = t; } void push (ImplicitKeyTreapNode!(char, E) *t) { if (t.extra.rev) { t.extra.rev = false; swap (t.left, t.right); if (t.left) t.left.extra.rev = !t.left.extra.rev; if (t.right) t.right.extra.rev = !t.right.extra.rev; } } void update (ImplicitKeyTreapNode!(char, E) *t) { t.extra.rev = !t.extra.rev; } void main() { rndGen.seed (1); auto s = readln.stripRight; auto z = readln.stripRight; const int nt = z.to!int; auto tree = new ImplicitKeyTreap!(char, E, relax, push, update) (s.dup); int n = s.length.to!int; foreach (tid; 0 .. nt) { z = readln.stripRight; auto w = z.splitter; const int t = w.front.to!int; w.popFront (); if (t == 1) { //tree.update (0, n); update (tree.root); } else { int f = w.front.to!int; w.popFront (); char c = w.front[0]; tree.insert (f == 1 ? 0 : n, c); ++n; } debug { auto ans = tree.getValues (); stderr.writeln (ans); } } foreach (i; 0 .. n) { write (tree.get (i)); } writeln; }
D
import std.stdio; import std.array; import std.string; import std.conv; void main() { int x = to!int(chomp(readln())); writeln(x * x * x); }
D
import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } void main(){ long a = read.to!long; long b = read.to!long; long c = read.to!long; int count = 0; while(count < 10000){ if(a % 2 == 0 && b % 2 == 0 && c % 2 == 0){ long a2 = b / 2 + c / 2; long b2 = a / 2 + c / 2; long c2 = a / 2 + b / 2; a = a2, b = b2, c = c2; count += 1; } else break; } int ans; if(count > 1000) ans = -1; else ans = count; ans.writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } 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 bool[](t); foreach (ti; 0..t) { auto a = RDA; int cnt; foreach (e; a[0..3]) { if (e % 2) ++cnt; } int cnt2 = a[3] % 2; if (cnt+cnt2 <= 1) { ans[ti] = true; continue; } if (a[0] == 0 || a[1] == 0 || a[2] == 0) continue; int cnt3 = cnt2 % 2 ? 0 : 1; if (3 - cnt + cnt3 <= 1) ans[ti] = true; } foreach (e; ans) { writeln(e ? "Yes" : "No"); } stdout.flush; debug readln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto na = readln.split.to!(int[]); auto N = na[0]; auto A = na[1]; auto XS = readln.split.to!(int[]); auto MEMO = new long[50*50+1][50+1][50+1]; foreach (ref a; MEMO) { foreach (ref b; a) { foreach (ref c; b) c = -1; } } long solve(int i, int n, int s) { if (i == N) return n && s == n * A ? 1 : 0; if (MEMO[i][n][s] != -1) return MEMO[i][n][s]; return MEMO[i][n][s] = solve(i+1, n+1, s+XS[i]) + solve(i+1, n, s); } writeln(solve(0, 0, 0)); }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop; // dfmt on void main() { long N, M; scan(N, M); auto S = new long[](M); auto C = new long[](M); foreach (i; 0 .. M) { scan(S[i], C[i]); S[i]--; } x: foreach (x; 0 .. 1000) { auto s = x.to!string; if (s.length != N) continue x; foreach (i; 0 .. M) { if (s[S[i]] != C[i] + '0') { continue x; } } writeln(x); return; } writeln(-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 S = RD!string; auto T = RD!string; long ans = long.max; foreach (i; 0..S.length-T.length+1) { long cnt; foreach (j; 0..T.length) { if (S[i+j] != T[j]) ++cnt; } ans.chmin(cnt); } writeln(ans); stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional; import std.algorithm, std.container; void main() { long A = scanElem; long B = scanElem; long cost; for(int i =0 ;i < 2;i++) { if(A<B){ cost += B; B--; }else{ cost += A; A--; } } writeln(cost); } class UnionFind{ UnionFind parent = null; void merge(UnionFind a) { if(same(a)) return; a.root.parent = this.root; } UnionFind root() { if(parent is null)return this; return parent = parent.root; } bool same(UnionFind a) { return this.root == a.root; } } void scanValues(TList...)(ref TList list) { auto lit = readln.splitter; foreach (ref e; list) { e = lit.fornt.to!(typeof(e)); lit.popFront; } } T[] scanArray(T = long)() { return readln.split.to!(long[]); } void scanStructs(T)(ref T[] t, size_t n) { t.length = n; foreach (ref e; t) { auto line = readln.split; foreach (i, ref v; e.tupleof) { v = line[i].to!(typeof(v)); } } } T scanElem(T = long)() { char[] res; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { res ~= cast(char) c; c = getchar; } return res.strip.to!T; } template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } struct Factor { long n; long c; } Factor[] factors(long n) { Factor[] res; for (long i = 2; i ^^ 2 <= n; i++) { if (n % i != 0) continue; int c; while (n % i == 0) { n = n / i; c++; } res ~= Factor(i, c); } if (n != 1) res ~= Factor(n, 1); return res; } long[] primes(long n) { if(n<2)return []; auto table = new long[n+1]; long[] res; for(int i = 2;i<=n;i++) { if(table[i]==-1) continue; for(int a = i;a<table.length;a+=i) { table[a] = -1; } res ~= i; } return res; } bool isPrime(long n) { if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (long i = 3; i ^^ 2 <= n; i += 2) if (n % i == 0) return false; return true; }
D
void main() { long n, m; rdVals(n, m); Edge[] edge = m.rdCol!Edge; long[][] dist = new long[][](n, n); foreach (i; 0 .. n) { foreach (j; 0 .. n) { if (i == j) dist[i][j] = 0; else dist[i][j] = inf; } } foreach (ref e; edge) { --e.a, --e.b; dist[e.a][e.b] = dist[e.b][e.a] = e.c; } foreach (i; 0 .. n) { foreach (j; 0 .. n) { foreach (k; 0 .. n) { dist[j][k] = min(dist[j][k], dist[j][i]+dist[i][k]); } } } long cnt; foreach (e; edge) { bool ok; foreach (i; 0 .. n) { if (dist[e.a][i] + e.c == dist[e.b][i]) ok = true; } if (ok) ++cnt; } writeln(m - cnt); } struct Edge { long a, b, c; } 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, std.string, std.conv; import std.array, std.algorithm, std.range; void main() { auto s = readln().chomp(); string t; for(int i=0; i<s.length; ) { if(startsWith(s[i..$],"apple")) t~="peach", i+=5; else if(startsWith(s[i..$],"peach")) t~="apple", i+=5; else t~=s[i], ++i; } writeln(t); }
D
void main() { auto K = ri; long res; foreach(a; 1..K+1) foreach(b; 1..K+1) foreach(c; 1..K+1) { res += gcd(a,gcd(b, c)); } res.writeln; } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int k; rd(k); int[] digit; while(k){ digit~=k&1; k/=2; } reverse(digit); struct E{int u, v, w;} E[] edges; int n=1; k=1; foreach(d; digit[1..$]){ foreach(ref e; edges) e.w*=2; edges~=E(n, n+1, 0); edges~=E(n, n+1, 1); k*=2; if(d){ edges~=E(1, n+1, k); k++; } n++; } writeln(n, " ", edges.length); foreach(e; edges){ writeln(e.u, " ", e.v, " ", e.w); } } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x) e=l[i].to!(typeof(e)); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto hw = readln.split.to!(int[]); auto H = hw[0]; auto W = hw[1]; string[][] map; map.length = H; foreach (i; 0..H) { map[i] = readln.chomp.split(""); } foreach (y; 0..H) { bool b; foreach (x; 0..W) { if (map[y][x] == "#") { b = true; break; } } if (!b) { foreach (x; 0..W) { map[y][x] = ""; } } } foreach (x; 0..W) { bool b; foreach (y; 0..H) { if (map[y][x] == "#") { b = true; break; } } if (!b) { foreach (y; 0..H) { map[y][x] = ""; } } } foreach (line_; map) { auto line = line_.join; if (!line) continue; writeln(line); } }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { long n, x, t; scan(n, x, t); // writeln(n, x, t); long cnt; long time; foreach (_; 0 .. 10 ^^ 7) { cnt += x; time += t; // writeln(cnt); if (cnt >= n) { writeln(time); return; } } } 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; 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; auto b = readln.chomp; auto c = readln.chomp; int res; foreach (i; 0..n) { int[char] cnt; cnt[a[i]]++; cnt[b[i]]++; cnt[c[i]]++; res += cnt.keys.length.to!int - 1; } res.writeln; }
D
void main() { int[] tmp = readln.split.to!(int[]); int n = tmp[0], m = tmp[1]; int[] a = new int[n], b = new int[n]; foreach (i; 0 .. n) { tmp = readln.split.to!(int[]); a[i] = tmp[0], b[i] = tmp[1]; } int[] c = new int[m], d = new int[m]; foreach (i; 0 .. m) { tmp = readln.split.to!(int[]); c[i] = tmp[0], d[i] = tmp[1]; } foreach (i; 0 .. n) { int point; int distance = int.max; foreach (j; 0 .. m) { int len = abs(a[i] - c[j]) + abs(b[i] - d[j]); if (len < distance) { point = j + 1; distance = len; } } point.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.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() { readint(); auto ss = readints(); bool[int] map; foreach (s; ss) map[s] = true; readint(); auto ts = readints(); int ans = 0; foreach (t; ts) { auto p = t in map; if (p != null) ans++; } writeln(ans); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n, k; rd(n, k); auto x=new long[](n), y=new long[](n); foreach(i; 0..n) rd(x[i], y[i]); long mn=5_000_000_000_000_000_000; foreach(i1; 0..n)foreach(i2; (i1+1)..n){ foreach(j1; 0..n)foreach(j2; (j1+1)..n){ auto x1=x[i1], x2=x[i2]; auto y1=y[j1], y2=y[j2]; if(x1>x2) swap(x1, x2); if(y1>y2) swap(y1, y2); // if((x2-x1)*(y2-y1)>=mn) continue; int cnt=0; foreach(t; 0..n){ if(x1<=x[t] && x[t]<=x2 && y1<=y[t] && y[t]<=y2) cnt++; } if(cnt>=k) mn=min(mn, (x2-x1)*(y2-y1)); } } writeln(mn); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "x", long, "y", bool, "haspair"); alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less); void main() { long h, w; scan(h, w); auto s = new string[](h); foreach (ref e; s) e = sread(); auto cntrow = new long[][](h, w); auto cntclm = new long[][](h, w); foreach (i; iota(h)) { long p, q; while (p < w) { while (q < w && s[i][q] == '.') { q++; } foreach (j; iota(p, q)) { cntrow[i][j] = (q - p); } q++; p = q; } } // cntrow.each!(x => x.writeln()); foreach (j; iota(w)) { long p, q; while (p < h) { while (q < h && s[q][j] == '.') { q++; } foreach (i; iota(p, q)) { cntclm[i][j] = (q - p); } q++; p = q; } } // cntclm.each!(x => x.writeln()); long ans; foreach (i; iota(h)) { foreach (j; iota(w)) { if (s[i][j] == '.') { ans = max(ans, cntrow[i][j] + cntclm[i][j]); } } } writeln(ans - 1); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.math; void main() { int a, b, c, x, y; scan(a, b, c, x, y); int ans = min(x, y) * min(2*c, a + b); if (x > y) { ans += (x - y) * min(2*c, a); } else { ans += (y - x) * min(2*c, b); } writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.string, std.conv, std.range, std.algorithm, std.functional; void main() { auto N = readln.chomp.to!long; auto n = N; auto f = 0; while (n > 0) { f += n % 10; n /= 10; } writeln(N % f == 0 ? "Yes" : "No"); }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(int a, int b) { if (b == 1) return 0; if (a >= b) return 1; int ans = 1; int x = a; while (x < b) { x = x + a - 1; ans++; } return ans; } void main() { int a, b; scan(a, b); writeln(calc(a, b)); }
D
import std.functional, std.algorithm, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv; void main() { string s = readln.chomp; int c; foreach (i; 0..s.length-1) { if (s[i] != s[i + 1]) { c++; } } writeln(c); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto q = RD!int; auto ans = new long[](q); foreach (qi; 0..q) { auto u = RD; auto v = RD; auto uc = popcnt(u); auto vc = popcnt(v); if (v >= u && vc <= uc) { int c1, c2; bool ok = true; foreach (i; 0..32) { auto bit = 1L << i; if (u & bit) ++c1; if (v & bit) ++c2; if (c2 > c1) ok = false; } ans[qi] = ok; } } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush; debug readln; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // 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 A, B; scan(A, B); foreach (x; 1 .. 10_000) { // writeln(x, " ", (x * 108) / 100); if (A == (x * 108) / 100 - x && B == (x * 110) / 100 - x) { writeln(x); return; } } writeln(-1); }
D
import std.stdio; void main(){ string c1=readln(); string c2=readln(); if(c1[0]==c2[2]&&c1[1]==c2[1]&&c1[2]==c2[0]) writeln("YES"); else writeln("NO"); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } struct Cmb { void init(int n) { long powmod(long a, long p) { long ans = 1; long mul = a; while (p > 0) { if ((p & 1) == 1) ans.modm(mul); p >>= 1; mul.modm(mul); } return ans; } ++n; fact = new long[2][](n); fact[0][0] = 1; foreach (i; 1..n) { fact[i][0] = fact[i-1][0]; fact[i][0].modm(i); } fact[n-1][1] = powmod(fact[n-1][0], mod - 2); foreach_reverse (i; 0..n-1) { fact[i][1] = fact[i+1][1]; fact[i][1].modm(i+1); } } long get(int n, int r) { long res = fact[n][0]; res.modm(fact[r][1]); res.modm(fact[n-r][1]); return res; } long[2][] fact; } void main() { auto n = RD!int; long ans = 1; foreach (i; 0..n) { ans.modm(i+1); } Cmb cmb; cmb.init(n); long tmp; foreach (i; 0..n) { tmp.moda(cmb.get(n-1, i)); } ans.mods(tmp); writeln(ans); stdout.flush; debug readln; }
D
import std.stdio,std.string,std.conv; int main() { int n; while((n = readln.chomp.to!int) != 0) { int ans = 0; while((n /= 5) != 0) { ans += n; } ans.writeln; } return 0; }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; enum MOD = 1_000_000_007; struct Number { long x; this(long x) { this.x = (x % MOD + MOD) % MOD; } Number opBinary(string op)(long x) { return Number(mixin("this.x"~op~"x")); } Number opBinary(string op)(Number n) { return Number(mixin("this.x"~op~"n.x")); } } void main() { auto tmp = readln.split.to!(long[]); Number fact(Number n) { if (n.x == 0) return Number(1); return n * fact(n-1); } auto solve() { if (tmp[0] > tmp[1]+1) return Number(0); if (tmp[1] > tmp[0]+1) return Number(0); if (tmp[0] == tmp[1]) return fact(Number(tmp[0])) * fact(Number(tmp[1])) * 2; return fact(Number(tmp[0])) * fact(Number(tmp[1])); } solve().x.writeln; }
D
/+ dub.sdl: name "A" dependency "dunkelheit" version=">=0.9.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; int main() { Scanner sc = new Scanner(stdin); string s; sc.read(s); int n = s.length.to!int; bool check(string t) { return !equal(t, t.retro); } int ans = 0; foreach (i; 0..n) { foreach (j; i+1..n+1) { if (check(s[i..j])) ans = max(ans, j-i); } } writeln(ans); return 0; } /* IMPORT /mnt/c/Users/yosupo/Programs/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /mnt/c/Users/yosupo/Programs/dunkelheit/source/dkh/scanner.d */ // module dkh.scanner; // import dkh.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(Args...)(auto ref Args args) { import std.exception; static if (args.length != 0) { enforce(readSingle(args[0])); read(args[1..$]); } } bool hasNext() { return succ(); } } /* IMPORT /mnt/c/Users/yosupo/Programs/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container; void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; int[][] aa; foreach (i; 0..N) aa ~= readln.split.map!(a => a.to!int-1).array; auto ss = new bool[](M); ss[] = true; auto r = N+1; foreach (_; 0..M) { auto ps = new int[](M); foreach (i; 0..N) { foreach (s; aa[i]) if (ss[s]) { ps[s] += 1; break; } } int max_p; size_t k; foreach (i, p; ps) if (p > max_p) { max_p = p; k = i; } r = min(r, max_p); ss[k] = false; } writeln(r); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto X = readln.chomp.to!int; int r = 1; foreach (i; 1..X+1) { foreach (j; 2..X+1) { if (i^^j <= X) r = max(r, i^^j); } } writeln(r); }
D
import std.stdio, std.string, std.array; import std.algorithm, std.regex; void main(){ "ATA CGC".find(readln[0])[1].writeln; }
D
import std.stdio, std.conv, std.string; import std.algorithm, std.array, std.container; import std.numeric, std.math; import core.bitop; T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; } string RDR()() { return readln.chomp; } long mod = pow(10, 9) + 7; long moda(long x, long y) { return (x + y) % mod; } long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; } long modm(long x, long y) { return (x * y) % mod; } void main() { //auto N = RD!long; long[][] c; foreach (i; 0..3) { c ~= RDR.split.to!(long[]); } bool ans = false; long[3] xs, ys; foreach (x0; 0..101) { xs[0] = x0; ys[0] = c[0][0] - xs[0]; foreach (x1; 0..101) { xs[1] = x1; ys[1] = c[1][0] - xs[0]; foreach (x2; 0..101) { bool ng = false; xs[2] = x2; ys[2] = c[2][0] - xs[0]; foreach (i; 0..3) { foreach (j; 1..3) { if (c[i][j] != xs[j] + ys[i]) { ng = true; } } } ans |= !ng; } } } writeln(ans ? "Yes" : "No"); stdout.flush(); }
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() { long N; scan(N); auto ans = N * (N - 1) / 2; writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
void main() { auto N = ri; string res; if(N == 0) res = "0"; while(N != 0) { if(abs(N)%2 == 1) { N--; res ~= '1'; } else res ~= '0'; N/=-2; } auto arr = res.to!(dchar[]); arr.reverse(); arr.writeln; } // =================================== import std.stdio; import std.string; import std.functional; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
void main() { long a, b, c; rdVals(a, b, c); if (a + b >= c) { "No".writeln; return; } long x = a * b; long y = c - a - b; writeln(4 * x < y * y ? "Yes" : "No"); } 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
void main() { long n = rdElem; long result = inf; for (long i = 1; i * i <= n; ++i) { if (n % i == 0) { result = min(result, i+n/i-2); } } result.writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); if (n == 1) { writeln("Hello World"); } else { int a, b; rd(a); rd(b); writeln(a + b); } } void rd(T...)(ref T x) { import std.stdio : readln; import std.string : split; import std.conv : to; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
D
import std.stdio, std.string, std.conv, std.algorithm; void main() { int N = readln.chomp.to!(int); int[] A = readln.chomp.split.to!(int[]); if (N == 1) { writeln(1); return; } int ans1; for (int i = 1; i < N; ) { i += max(plus(A, i), minus(A, i)) + 1; ans1++; if (N == i) { ans1++; } } int ans2; reverse(A); for (int i = 1; i < N; ) { i += max(plus(A, i), minus(A, i)) + 1; ans2++; if (N == i) { ans2++; } } writeln(min(ans1, ans2)); } int plus(int[] A, int index) { int ret; for (int i = index; i < A.length; i++) { if (A[i - 1] <= A[i]) { ret++; } else { break; } } return ret; } int minus(int[] A, int index) { int ret; for (int i = index; i < A.length; i++) { if (A[i - 1] >= A[i]) { ret++; } else { break; } } return ret; }
D
void main(){ import std.stdio, std.conv, std.string, std.algorithm; int n; rd(n); int f(int x){ int ret=0; while(x>0){ ret+=x%10; x/=10; } return ret; } int mn=1_000_000_000; for(int a=1; a<n; a++){ int b=n-a; mn=min(mn, f(a)+f(b)); } writeln(mn); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x) e=l[i].to!(typeof(e)); }
D
import std.stdio, std.string,std.range, std.conv, std.array, std.algorithm, std.math, std.typecons, std.container, std.datetime; void main() { auto n = readln.chomp.to!int; writeln(n*(n+1)/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(); // writeln(n); long 余り = n % 100; // writeln(余り); long これだけ個買える = (n - 余り) / 100; // writeln(これだけ個買える); if (これだけ個買える * 5 >= 余り) { writeln(1); } else { writeln(0); } } 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
/+ dub.sdl: name "D" dependency "dunkelheit" version=">=0.9.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; int main() { Scanner sc = new Scanner(stdin); scope(exit) sc.read!true; import std.typecons; alias E = Tuple!(int, "to", long, "dist"); int n, m; sc.read(n, m); E[][] g = new E[][n]; foreach (i; 0..m) { int l, r; long d; sc.read(l, r, d); l--; r--; g[l] ~= E(r, d); g[r] ~= E(l, -d); } bool[] vis = new bool[n]; long[] pot = new long[n]; bool ok = true; void dfs(int p) { vis[p] = true; foreach (e; g[p]) { if (vis[e.to]) { ok &= pot[e.to] == pot[p] + e.dist; continue; } pot[e.to] = pot[p] + e.dist; dfs(e.to); } } foreach (i; 0..n) { if (vis[i]) continue; dfs(i); } writeln(ok ? "Yes" : "No"); return 0; } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */ // module dkh.scanner; // import dkh.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(bool enforceEOF = false, T, Args...)(ref T x, auto ref Args args) { import std.exception; enforce(readSingle(x)); static if (args.length == 0) { enforce(enforceEOF == false || !succ()); } else { read!enforceEOF(args); } } void read(bool enforceEOF = false, Args...)(auto ref Args args) { import std.exception; static if (args.length == 0) { enforce(enforceEOF == false || !succ()); } else { enforce(readSingle(args[0])); read!enforceEOF(args); } } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std.stdio, std.string, std.conv, std.math; void main() { int n = readln.chomp.to!int; long debt = 100000; foreach(i;0..n) debt = ((((debt + (debt*0.05)) / 1000.0).ceil) * 1000).to!long; debt.writeln; }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional, std.traits; import std.algorithm, std.container; import core.stdc.stdlib; void main() { auto N = scanElem; auto M = scanElem; long[] list; list.length = N; foreach(i; 0..M) { auto a = scanElem; auto b = scanElem; if(a>b)swap(a,b); list[a-1]++; list[b-1]--; } foreach(i; 1..N) { list[i] += list[i-1]; } list = list[0..$-1]; writeln(list.any!"a%2!=0"?"NO":"YES"); } long gcd(long a, long b) { if(b == 0) return a; return gcd(b, a % b); } class UnionFind{ UnionFind parent = null; void merge(UnionFind a) { if(same(a)) return; a.root.parent = this.root; } UnionFind root() { if(parent is null)return this; return parent = parent.root; } bool same(UnionFind a) { return this.root == a.root; } } string scanString() { return readln.strip; } void scanValues(TList...)(ref TList list) { auto lit = readln.splitter; foreach (ref e; list) { e = lit.fornt.to!(typeof(e)); lit.popFront; } } T[] scanArray(T = long)() { return readln.split.to!(T[]); } void scanStructs(T)(ref T[] t, size_t n) { t.length = n; foreach (ref e; t) { auto line = readln.split; foreach (i, ref v; e.tupleof) { v = line[i].to!(typeof(v)); } } } long scanULong(){ long x; while(true){ const c = getchar; if(c<'0'||c>'9'){ break; } x = x*10+c-'0'; } return x; } T scanElem(T = long)() { char[] res; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { res ~= cast(char) c; c = getchar; } return res.strip.to!T; } template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } struct Factor { long n; long c; } Factor[] factors(long n) { Factor[] res; for (long i = 2; i ^^ 2 <= n; i++) { if (n % i != 0) continue; int c; while (n % i == 0) { n = n / i; c++; } res ~= Factor(i, c); } if (n != 1) res ~= Factor(n, 1); return res; } long[] primes(long n) { if(n<2)return []; auto table = new long[n+1]; long[] res; for(int i = 2;i<=n;i++) { if(table[i]==-1) continue; for(int a = i;a<table.length;a+=i) { table[a] = -1; } res ~= i; } return res; } bool isPrime(long n) { if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (long i = 3; i ^^ 2 <= n; i += 2) if (n % i == 0) return false; return true; }
D
import std.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.to!(int[]); } void main() { int n = readint; int[] a; int[string] db; // name -> int for (int i = 0; i < n; i++) { auto ax = readln.split; string material = ax[0]; int price = ax[1].to!int; db[material] = i; a ~= price; } auto uf = new UnionFind(n); int m = readint; for (int i = 0; i < m; i++) { auto ss = readln.split; // s と t は自由に交換可能 int s = db[ss[0]]; int t = db[ss[1]]; // rs, rt の集合の最安値を更新 int rs = uf.root(s); int rt = uf.root(t); uf.unite(s, t); a[uf.root(s)] = min(a[rs], a[rt]); } int ans = 0; foreach (i; 0..n) { ans += a[uf.root(i)]; } writeln(ans); } class UnionFind { private int[] _data; private int[] _nexts; // 次の要素。なければ -1 private int[] _tails; // 末尾の要素 this(int n) { _data = new int[](n + 1); _nexts = new int[](n + 1); _tails = new int[](n + 1); _data[] = -1; _nexts[] = -1; for (int i = 0; i < _tails.length; i++) _tails[i] = i; } int root(int a) { if (_data[a] < 0) return a; return _data[a] = root(_data[a]); } bool unite(int a, int b) { int ra = root(a); int rb = root(b); if (ra == rb) return false; // ra に rb を繋げる _data[ra] += _data[rb]; _data[rb] = ra; // ra の末尾の後ろに rb を繋げる _nexts[_tails[ra]] = rb; // ra の末尾が rb の末尾になる _tails[ra] = _tails[rb]; return true; } bool isSame(int a, int b) { return root(a) == root(b); } /// a が属する集合のサイズ int groupSize(int a) { return -_data[root(a)]; } /// a が属する集合の要素全て void doEach(int a, void delegate(int) fn) { auto node = root(a); while (node != -1) { fn(node); node = _nexts[node]; } } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.ascii; void main() { auto s = readln.chomp; int[char] k; foreach (e; s) { k[e]++; } if (k.keys.length == 3) { writeln("Yes"); } else { writeln("No"); } }
D
import std.stdio, std.string, std.conv; import std.algorithm; void main() { for(string s_; (s_=readln().chomp()).length;) { auto ab = s_.split.map!(to!int)(); immutable a=ab[0], b=ab[1]; if(0<a) writeln("Positive"); else if(a<=0 && 0<=b) writeln("Zero"); else if(b<0 && (b-a)%2==0) writeln("Negative"); else if(b<0 && (b-a)%2==1) writeln("Positive"); else assert(false); } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; void main() { int n; n = readln.chomp.to!int; while (n) { auto a = new int[9][9], b = new int[9][9], c = new int[9][9], ans = new bool[9][9]; foreach (i; 0..9) { a[i] = readln.split.map!(to!int).array; foreach (j; 0..3) { c[i/3*3+j][(i%3)*3..(i%3)*3+3] = a[i][j*3..(j+1)*3]; } foreach (j; 0..9) { b[j][i] = a[i][j]; } } foreach (i; 0..9) { foreach (j; 0..9) { foreach (k; 0..9) { if (j == k) continue; if (a[i][j] == a[i][k]) ans[i][j] = ans[i][k] = 1; if (b[i][j] == b[i][k]) ans[j][i] = ans[k][i] = 1; if (c[i][j] == c[i][k]) ans[i/3*3 + j/3][(i%3)*3 + (j%3)] = ans[i/3*3 + k/3][(i%3)*3 + (k%3)] = 1; } } } foreach (i; 0..9) { foreach (j; 0..9) { write(ans[i][j] ? "*" : " ", a[i][j]); } writeln(""); } if (--n) writeln(""); } }
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() { string s; scan(s); foreach (cmb ; 0 .. 16) { string t; t ~= (cmb & 1) ? "AKIH" : "KIH"; t ~= (cmb & 2) ? "AB" : "B"; t ~= (cmb & 4) ? "AR" : "R"; t ~= (cmb & 8) ? "A" : ""; if (s == t) { writeln("YES"); return; } } writeln("NO"); } struct UnionFind { private { int N; int[] p; int[] rank; } this (int n) { N = n; p = iota(N).array; rank = new int[](N); } int find_root(int x) { if (p[x] != x) { p[x] = find_root(p[x]); } return p[x]; } bool same(int x, int y) { return find_root(x) == find_root(y); } void unite(int x, int y) { int u = find_root(x), v = find_root(y); if (u == v) return; if (rank[u] < rank[v]) { p[u] = v; } else { p[v] = u; if (rank[u] == rank[v]) { rank[u]++; } } } } 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); } } } struct Queue(T) { private { int N, head, tail; T[] data; } this(int n) { N = n + 1; data = new T[](N); } bool empty() { return head == tail; } bool full() { return (tail + 1) % N == head; } T front() { return data[head]; } void push(T x) { assert(!full); data[tail++] = x; tail %= N; } void pop() { assert(!empty); head = (head + 1) % N; } void clear() { head = tail = 0; } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.ascii; void main() { auto k = readln.chomp.to!int; auto h = k / 2; if (k % 2) { writeln((h+1) * h); } else { writeln(h * h); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; int[] xs, ys; long[] hs; foreach (_; 0..N) { auto xyh = readln.split.to!(int[]); xs ~= xyh[0]; ys ~= xyh[1]; hs ~= xyh[2]; } foreach (y; 0..101) { foreach (x; 0..101) { long h; foreach (i; 0..N) { if (hs[i] != 0) { h = hs[i] + abs(x - xs[i]) + abs(y - ys[i]); break; } } foreach (i; 0..N) { if (hs[i] != max(0, h - abs(x - xs[i]) - abs(y - ys[i]))) goto ng; } writeln(x, " ", y, " ", h); ng: } } }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto D = [s[1], s[2]]; auto G = new int[][][](2, 4*N*N); auto C = new int[][](2, 4*N*N); C[0][] = C[1][] = -1; auto xyd = new int[][](2, 2*N); foreach (i; 0..2) { xyd[i][] = -1; foreach (x; 0..2*N) { foreach (y; 0..2*N) { if (x*x+y*y == D[i]) { xyd[i][x] = y; } } } } foreach (i; 0..2) { foreach (x1; 0..2*N) { foreach (y1; 0..2*N) { foreach (x2; x1..2*N) { if (xyd[i][x2-x1] == -1) continue; foreach (k; [-1, 1]) { int y2 = y1 + k * xyd[i][x2-x1]; if (y2 < 0 || y2 >= 2*N) continue; G[i][x1*2*N+y1] ~= x2*2*N+y2; G[i][x2*2*N+y2] ~= x1*2*N+y1; } } } } } foreach (i; 0..2) { foreach (x1; 0..2*N) { foreach (y1; 0..2*N) { if (C[i][x1*2*N+y1] == -1) { dfs(x1*2*N+y1, 0, G[i], C[i]); } } } } int cnt = 0; foreach (i; 0..2) { foreach (j; 0..2) { if (iota(4*N*N).map!(k => C[i][k] == i && C[j][k] == j).sum < N*N) continue; foreach (x; 0..2*N) { foreach (y; 0..2*N) { if (C[0][x*2*N+y] == i && C[1][x*2*N+y] == j) { writeln(x, " ", y); ++cnt; if (cnt >= N * N) { return; } } } } } } } void dfs(int n, int c, ref int[][] G, ref int[] C) { if (C[n] != -1) return; C[n] = c; foreach (m; G[n]) if (C[m] == -1) dfs(m, c^1, G, C); }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { int n; scan(n); while (n > 0) { if (n % 10 == 9) { writeln("Yes"); return; } n /= 10; } writeln("No"); } 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.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto S = readln.chomp; auto rs = S.dup; rs.reverse(); if (S != rs) { writeln("No"); return; } auto N = S.length; auto s = S[0..(N-1)/2]; rs = s.dup; rs.reverse(); if (s != rs) { writeln("No"); return; } s = S[(N+3)/2-1..$]; rs = s.dup; rs.reverse(); if (s != rs) { writeln("No"); return; } writeln("Yes"); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; alias Pair = Tuple!(int, "a", int, "b"); void main() { int N, T; scan(N, T); auto ans = inf; foreach (i ; 0 .. N) { int ci, ti; scan(ci, ti); if (ti <= T) ans = min(ans, ci); } writeln(ans < inf ? ans.to!string : "TLE"); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.numeric; import std.stdio; import std.string; void main() { string n = readln().chomp; int[] a = readln().chomp.split(" ").to!(int[]); int count = 0; int max = 100; for (int i = 1; i < a.length; ++i) { if (a[i - 1] == a[i]) { ++count; a[i] = max++; } } writeln(count); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void get(Args...)(ref Args args) { import std.traits, std.meta, std.typecons; static if (Args.length == 1) { alias Arg = Args[0]; static if (isArray!Arg) { static if (isSomeChar!(ElementType!Arg)) { args[0] = readln.chomp.to!Arg; } else { args[0] = readln.split.to!Arg; } } else static if (isTuple!Arg) { auto input = readln.split; static foreach (i; 0..Fields!Arg.length) { args[0][i] = input[i].to!(Fields!Arg[i]); } } else { args[0] = readln.chomp.to!Arg; } } else { auto input = readln.split; assert(input.length == Args.length); static foreach (i; 0..Args.length) { args[i] = input[i].to!(Args[i]); } } } void get_lines(Args...)(size_t N, ref Args args) { import std.traits, std.range; static foreach (i; 0..Args.length) { static assert(isArray!(Args[i])); args[i].length = N; } foreach (i; 0..N) { static if (Args.length == 1) { get(args[0][i]); } else { auto input = readln.split; static foreach (j; 0..Args.length) { args[j][i] = input[j].to!(ElementType!(Args[j])); } } } } void main() { int T; get(T); while (T--) { int N; get(N); int[] as, bs; auto d = N; foreach_reverse (i; 2..N) { if (i == 448 || i == 22 || i == 5 || i == 3 || i == 2) { as ~= d; as ~= d; bs ~= i; bs ~= i; d = i; } else { as ~= i; bs ~= d; } } writeln(as.length); foreach (i; 0..as.length) writeln(as[i], " ", bs[i]); } }
D
void main(){ int n = inelm(); ( 800*n - (n/15).to!int()*200).writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range; const long mod = 10^^9+7; // 1要素のみの入力 T inelm(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] inln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split())ln ~= elm.to!T(); return ln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto ha = readln.split.to!(int[]); auto H = ha[0]; auto A = ha[1]; writeln((H+A-1)/A); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n, m; rd(n, m); auto g=new int[][](n, 0); foreach(_; 0..m){ int a, b; rd(a, b); a--; b--; g[a]~=b; g[b]~=a; } auto c=new int[](n); fill(c, -1); bool dfs(int i, int w, int p=-1){ c[i]=w; bool ret=true; foreach(j; g[i])if(j!=p){ if(c[j]==-1) ret&=dfs(j, w^1, i); else if((c[j]^w)==0) ret=false; } return ret; } if(dfs(0, 0)){ long w=0; foreach(e; c)if(e==0) w++; writeln(w*(long(n)-w)-m); }else{ writeln(long(n)*(n-1)/2-m); } } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.stdio, std.string, std.conv, std.math, std.regex; void main() { auto n = readln.split.to!(int[]); writeln((n[0] - 1) * (n[1] - 1)); }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; const MOD = 10^^9 + 7; int[int] fac(int n) { assert(n > 1); int[int] ps; while (n % 2 == 0) { ps[2]++; n /= 2; } for (int i = 3; i * i <= n; i += 2) { while (n % i == 0) { ps[i]++; n /= i; } } if (n > 1) ps[n]++; return ps; } long calc(int n, int m) { if (m == 1) return 1; long ans = 1; foreach (v; fac(m).values) { // v 個の素因数と n - 1 個の仕切りの並べ方 ans = (ans * nck(v + n - 1, n - 1)) % MOD; } return ans; } void main() { initFactTable(210000); int n, m; scan(n, m); writeln(calc(n, m)); } long[] fact; // 階乗 long[] factInv; // 階乗の逆元 void initFactTable(int n) { fact = new long[n + 1]; fact[0] = 1; for (int i = 1; i <= n; i++) fact[i] = i * fact[i - 1] % MOD; factInv = new long[n + 1]; factInv[n] = modPow(fact[n], MOD - 2, MOD); for (int i = n - 1; i >= 0; i--) factInv[i] = factInv[i + 1] * (i + 1) % MOD; } long modPow(long x, long k, long m) { if (k == 0) return 1; if (k % 2 == 0) return modPow(x * x % m, k / 2, m); return x * modPow(x, k - 1, m) % m; } long nck(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return (fact[n] * factInv[k] % MOD) * factInv[n - k] % MOD; }
D
// cheese-cracker [2022-01-31] void solve(){ auto s = scan!(dchar[]); long on = 0; long off = 0; long res = 0; foreach(c; s){ on += (c == '1'); off += (c == '0'); if(max(on, off) > min(on, off) && max(on, off) > 1){ res = max(res, min(on, off)); } } writeln(res); } void main(){ long tests = scan; // Toggle! while(tests--) solve; } /*_________________________*That's All Folks!*__________________________*/ import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math, std.numeric; string[] tk; alias tup = Tuple!(long, long); T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;} T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; } void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
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[] a; readC(5, a); int k; readV(k); writeln(a[4]-a[0] <= k ? "Yay!" : ":("); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto T = readln.chomp.to!int; foreach (_; 0..T) { auto N = readln.chomp.to!long; writeln(N); } }
D
void main() { int n = readln.chomp.to!int; string s = readln.chomp; auto g = s.group.array; string bra, ket; foreach (i; 0 .. n) { bra ~= '('; ket ~= ')'; } int l, r; foreach (x; g) { if (x[0] == '(') { r += x[1].to!int; } else { int tmp = x[1].to!int; if (r - tmp >= 0) { r -= tmp; } else { l += tmp - r; r = 0; } } } writeln(bra[0..l], s, ket[0..r]); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio,std.conv,std.string,std.algorithm; void main(){ auto s=readln(); writeln("2018",s[4..10],); }
D
// Cheese-Cracker: cheese-cracker.github.io const ll PRIME = 10^^9 + 7; void play(){ ll n, x, pos; n = rd; x = rd; pos = rd; ll clo = x-1, chi = n - x; ll res = 1; int lo = 0, hi = n.to!int; while(lo < hi){ int midd = (lo + hi)/2; show(midd, pos, clo, chi); if(midd <= pos){ if(pos != midd){ res *= clo; res %= PRIME; --clo; } lo = midd + 1; }else{ res *= chi; res %= PRIME; --chi; hi = midd; } } assert(lo - 1 == pos); ll left = chi + clo; show(left); foreach(i; 1..left+1){ res *= i; res %= PRIME; } writeln(res); } int main(){ long t = 1; /* t = rd; // Toggle! */ while(t--) play(); // Let's play! stdout.flush; return 0; } /**********It's A Me Mario!**********/ import std.stdio, std.conv, std.functional, std.string, std.algorithm; import std.container, std.range, std.typecons, std.numeric, std.math, std.random; static string[] inp; T rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;} T[] rdarr(T = long)(){ auto r = readln.chomp.split.to!(T[]); return r; } void show(A...)(A a) { debug{ foreach(t; a){ write(t, "| "); } writeln; } } alias ll = long; alias rbt = redBlackTree; alias tup = Tuple!(long, "x", long, "y"); T max(T = long)(T a, T b){ return (a > b) ? a : b; } T min(T = long)(T a, T b){ return (a < b) ? a : b; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto S = readln.to!(dchar[]); auto T = readln.to!(dchar[]); dchar[dchar] smemo, tmemo; foreach (i, s; S) { auto t = T[i]; if (s in smemo && t in tmemo) { if (smemo[s] != t || tmemo[t] != s) { writeln("No"); return; } } else if (s in smemo || t in tmemo) { writeln("No"); return; } else { smemo[s] = t; tmemo[t] = s; } } writeln("Yes"); }
D
// Cheese-Cracker: cheese-cracker.github.io void theCode(){ int n = scan!int; auto arr = scanArray!int; auto ix = new int[2*n+1]; ix[] = -1000_000_0; for(int i = 0; i < n; ++i){ ix[arr[i]] = i+1; } int cnt = 0; for(int k = 2; k <= 2*n; ++k){ for(int i = 1; i * i < k; ++i){ if(k % i == 0 && ix[k/i] + ix[i] == k){ ++cnt; } } } writeln(cnt); } void main(){ long tests = scan; // Toggle! while(tests--) theCode(); } /********* That's All Folks! *********/ import std.stdio, std.random, std.functional, std.container, std.algorithm; import std.numeric, std.range, std.typecons, std.string, std.math, std.conv; string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;} T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; } void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } } alias ll = long, tup = Tuple!(long, "x", long, "y");
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.typecons; import std.numeric, std.math; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto H = RD; auto W = RD; auto h = RD; auto w = RD; writeln(H*W - (h*W+w*H-h*w)); stdout.flush(); }
D
import std.algorithm, std.string, std.range, std.array, std.stdio, std.conv; bool check(string s) { if (s.length % 2) { return false; } string a = s[0..$/2], b = s[$/2..$]; return a == b; } void main() { string s = readln.chomp; size_t del = 1; while (del < s.length - 1) { if (check(s[0..$-del])) { break; } else { del++; } } writeln(s[0..$-del].length); }
D
import std; import core.bitop; // dfmt off ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); void aryWrite(T = long)(T[] ary){ ary.map!(x => x.text()).join(' ').writeln(); } alias Pair = Tuple!(long, "H", long, "W"); alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long a, b, c, k; scan(a, b, c, k); writeln(k % 2 ? b - a : a - b); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = readln.chomp.to!size_t; Node node; foreach (_; n.iota) { auto rd = readln.chomp.splitter(' '), cmd = rd.front; switch (cmd) { case "insert": rd.popFront; node.insert(rd.front.to!int); break; case "print": node.inorderWalk; writeln; node.preorderWalk; writeln; break; default: assert(0); } } } void writeArray(T)(T[] ai) { foreach (a; ai) write(" ", a); writeln; } void insert(ref Node node, int val) { Node par, cur = node; while (cur !is null) { par = cur; if (val < cur.key) cur = cur.l; else cur = cur.r; } Node chi = new Node(val); if (par is null) node = chi; else if (val < par.key) par.l = chi; else par.r = chi; } void inorderWalk(Node node) { if (node.l !is null) inorderWalk(node.l); write(" ", node.key); if (node.r !is null) inorderWalk(node.r); } void preorderWalk(Node node) { write(" ", node.key); if (node.l !is null) preorderWalk(node.l); if (node.r !is null) preorderWalk(node.r); } class Node { int key; Node l, r; this(int key) { this.key = key; } }
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 abk = readints; int a = abk[0], b = abk[1], k = abk[2]; for (int i = a; i <= min(a + k - 1, b); i++) writeln(i); for (int i = max(a + k, b - k + 1); i <= b; i++) writeln(i); }
D
// Vicfred // https://atcoder.jp/contests/abc168/tasks/abc168_a import std.stdio; import std.conv; import std.string; void main() { int n = readln.chomp.to!int; n = n%10; switch(n) { case 0, 1, 6, 8: writeln("pon"); break; case 3: writeln("bon"); break; default: writeln("hon"); break; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto K = readln.chomp.to!int; auto S = readln.chomp; if (S.length <= K) { writeln(S); } else { writeln(S[0..K], "..."); } }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { auto s = sread(); bool b = s[0] == s[2]; writeln(b ? "Yes" : "No"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto abc = readln.split.to!(int[]); auto A = abc[0]; auto B = abc[1]; auto C = abc[2]; writeln((A < C && C < B) || (B < C && C < A) ? "Yes" : "No"); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto s=new char[][](n); foreach(i; 0..n) s[i]=readln.chomp.to!(char[]); long[char] cnt; cnt['M']=cnt['A']=cnt['R']=cnt['C']=cnt['H']=0L; foreach(i; 0..n){ auto c=s[i][0]; if(c in cnt) cnt[c]++; } char[int] p; p[0]='M'; p[1]='A'; p[2]='R'; p[3]='C'; p[4]='H'; long fun(int i, char[] h){ if(h.length==3){ long ret=1; foreach(c; h) ret*=cnt[c]; return ret; }else if(i>=5){ return 0L; }else{ long ret=0; foreach(j; i..5) ret+=fun(j+1, h~p[j]); return ret; } } writeln(fun(0, [])); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.stdio, std.string, std.conv; import std.typecons; import std.algorithm, std.array, std.range, std.container; import std.math; void main() { auto data = readln.split.to!(int[]); auto A = data[0], B = data[1], C = data[2]; if (A+B >= C-1) writeln(B+C); else writeln( B + A+B+1 ); }
D
import std.stdio; import std.algorithm; import std.conv; import std.array; import std.string; import std.math; import std.functional; import std.range; import std.typecons; import std.format; void main(string[] args) { readln.solve.writeln; } auto solve(string line1) { auto _ = line1.chomp.split.map!(to!int).array; int a = _[0]; int b = _[1]; int c = _[2]; int d = _[3]; if (a + b > c + d) { return "Left"; } else if (a + b < c + d) { return "Right"; } else { return "Balanced"; } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { readint; auto a = readints; int min = a.reduce!((a, b) => min(a, b)); int max = a.reduce!((a, b) => max(a, b)); writeln(max - min); }
D
void main() { auto O = rs, E = rs; foreach(i, v; E) { write(O[i]); write(v); } if(O.length - E.length == 1) writeln(O[$-1]); else writeln; } // =================================== import std.stdio; import std.string; import std.functional; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
void main() { problem(); } void problem() { const K = scan!long; long solve() { auto queue = DList!long(1,2,3,4,5,6,7,8,9); long count; while(true) { count++; auto n = queue.front; if (count == K) return n; queue.removeFront(); const tail = n % 10; const next = n * 10 + tail; if (tail != 0) queue.insertBack(next - 1); queue.insertBack(next); if (tail != 9) queue.insertBack(next + 1); } } 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.functional; 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
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std; // dfmt on void main() { long N = lread(); auto V = new long[](N); auto W = new long[](N); foreach (i; 0 .. N) scan(V[i], W[i]); auto memo = new long[][](min(2 ^^ 10 + 1, N + 1), 10 ^^ 5 + 2); foreach (i; 1 .. memo.length) { foreach (j; 0 .. 10 ^^ 5 + 1) { memo[i][j] = memo[i][j].max(memo[i / 2][j]); memo[i][min(j + W[i - 1], 10 ^^ 5 + 1)] = memo[i][min(j + W[i - 1], 10 ^^ 5 + 1)].max( memo[i / 2][j] + V[i - 1]); } } long solve(long v, long l) { long[] I; long i = v; while (memo.length <= i) I ~= i, i /= 2; // writeln(I); long ans; foreach (ulong state; 0UL .. 2UL ^^ I.length + 1) { long value, weight; foreach (j; 0 .. I.length) if ((1 << j) & state) { value += V[I[j] - 1]; weight += W[I[j] - 1]; } if (l < weight) continue; ans = ans.max(memo[i][l - weight] + value); } return ans; } long Q = lread(); foreach (_; 0 .. Q) { long v, l; scan(v, l); solve(v, l).writeln(); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; int idx(char c) { switch (c) { case 'R': return 0; case 'G': return 1; default: return 2; } return -1; } void main() { auto N = readln.chomp.to!int; auto S = readln.chomp; auto MEMO = new int[][](N, 3); foreach (i, c; S) { if (i) foreach (j; 0..3) MEMO[i][j] += MEMO[i-1][j]; MEMO[i][idx(c)] += 1; } long r; foreach (i; 1..N-1) { auto m = S[i]; foreach (j; 0..i) { if (S[j] == m) continue; auto n = S[j]; char l; if (m != 'R' && n != 'R') { l = 'R'; } else if (m != 'G' && n != 'G') { l = 'G'; } else { l = 'B'; } r += MEMO[N-1][idx(l)] - MEMO[i][idx(l)]; if (i + (i-j) < N && S[i + (i-j)] == l) r -= 1; } } writeln(r); }
D
/+ dub.sdl: name "F" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; // import dcomp.functional; int n; int[][] g; int[] gsm; int main() { auto sc = new Scanner(stdin); int m; sc.read(n, m); calc.init([[0, 1<<n], [0, n-1], [0, n-1]]); g = new int[][](n, n); foreach (i; 0..m) { int a, b, c; sc.read(a, b, c); a--; b--; g[a][b] = g[b][a] = c; } gsm = new int[1<<n]; foreach (f; 0..1<<n) { foreach (x; 0..n) { if (!(f & (1<<x))) continue; foreach (y; x+1..n) { if (!(f & (1<<y))) continue; gsm[f] += g[x][y]; } } } writeln(calc((1<<n)-1, 0, n-1)); return 0; } int calcBase(int f, int s, int t) { if (s > t) return calc(f, t, s); assert(f & (1<<s)); assert(f & (1<<t)); int ans = 10^^9; if (g[s][t]) { for (int w = 0; w < f; w = ((w | ~f) + 1) & f) { if (!(w & (1<<s))) continue; if ( (w & (1<<t))) continue; ans = min(ans, gsm[f] - gsm[w] - gsm[f^w] - g[s][t]); } } foreach (z; 0..n) { if (!(f & (1<<z))) continue; if (z == s || z == t) continue; for (int w = 0; w < f; w = ((w | ~f) + 1) & f) { if (!(w & (1<<s))) continue; if ( (w & (1<<z))) continue; if ( (w & (1<<t))) continue; int sm = calc(w|(1<<z), s, z) + calc(f^w, z, t); ans = min(ans, sm + gsm[f] - gsm[w|(1<<z)] - gsm[f^w]); } } return ans; } memoCont!calcBase calc; /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/modint.d */ // module dcomp.modint; // import dcomp.numeric.primitive; struct ModInt(uint MD) if (MD < int.max) { import std.conv : to; uint v; this(int v) {this(long(v));} this(long v) {this.v = (v%MD+MD)%MD;} static auto normS(uint x) {return (x<MD)?x:x-MD;} static auto make(uint x) {ModInt m; m.v = x; return m;} auto opBinary(string op:"+")(ModInt r) const {return make(normS(v+r.v));} auto opBinary(string op:"-")(ModInt r) const {return make(normS(v+MD-r.v));} auto opBinary(string op:"*")(ModInt r) const {return make((long(v)*r.v%MD).to!uint);} auto opBinary(string op:"/")(ModInt r) const {return this*inv(r);} auto opOpAssign(string op)(ModInt r) {return mixin ("this=this"~op~"r");} static ModInt inv(ModInt x) {return ModInt(extGcd!int(x.v, MD)[0]);} string toString() {return v.to!string;} } struct DModInt(string name) { import std.conv : to; static uint MD; uint v; this(int v) {this(long(v));} this(long v) {this.v = ((v%MD+MD)%MD).to!uint;} static auto normS(uint x) {return (x<MD)?x:x-MD;} static auto make(uint x) {DModInt m; m.MD = MD; m.v = x; return m;} auto opBinary(string op:"+")(DModInt r) const {return make(normS(v+r.v));} auto opBinary(string op:"-")(DModInt r) const {return make(normS(v+MD-r.v));} auto opBinary(string op:"*")(DModInt r) const {return make((long(v)*r.v%MD).to!uint);} auto opBinary(string op:"/")(DModInt r) const {return this*inv(r);} auto opOpAssign(string op)(DModInt r) {return mixin ("this=this"~op~"r");} static DModInt inv(DModInt x) { return DModInt(extGcd!int(x.v, MD)[0]); } string toString() {return v.to!string;} } template isModInt(T) { const isModInt = is(T : ModInt!MD, uint MD) || is(S : DModInt!S, string s); } T[] factTable(T)(size_t length) if (isModInt!T) { import std.range : take, recurrence; import std.array : array; return T(1).recurrence!((a, n) => a[n-1]*T(n)).take(length).array; } T[] invFactTable(T)(size_t length) if (isModInt!T) { import std.algorithm : map, reduce; import std.range : take, recurrence, iota; import std.array : array; auto res = new T[length]; res[$-1] = T(1) / iota(1, length).map!T.reduce!"a*b"; foreach_reverse (i, v; res[0..$-1]) { res[i] = res[i+1] * T(i+1); } return res; } T[] invTable(T)(size_t length) if (isModInt!T) { auto f = factTable!T(length); auto invf = invFactTable!T(length); auto res = new T[length]; foreach (i; 1..length) { res[i] = invf[i] * f[i-1]; } return res; } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; if (f.eof) return false; line = lineBuf[]; f.readln(line); } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/numeric/primitive.d */ // module dcomp.numeric.primitive; import std.traits; T pow(T, U)(T x, U n) if (!isFloatingPoint!T && isIntegral!U) { return pow(x, n, T(1)); } T pow(T, U)(T x, U n, T e) if (isIntegral!U) { while (n) { if (n & 1) e *= x; x *= x; n /= 2; } return e; } T lcm(T)(in T a, in T b) { import std.numeric : gcd; return a / gcd(a,b) * b; } T[3] extGcd(T)(in T a, in T b) if (!isIntegral!T || isSigned!T) { if (b==0) { return [1, 0, a]; } else { auto e = extGcd(b, a%b); return [e[1], e[0]-a/b*e[1], e[2]]; } } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/functional.d */ // module dcomp.functional; struct memoCont(alias pred) { import core.exception : RangeError; import std.range, std.algorithm, std.conv; import std.string : join; import std.traits : ReturnType, ParameterTypeTuple, isIntegral; import std.typecons : tuple, Tuple; import std.meta; alias R = ReturnType!pred; alias Args = ParameterTypeTuple!pred; static assert (allSatisfy!(isIntegral, Args)); static immutable N = Args.length; int[2][N] rng; int[N] len; R[] dp; bool[] used; void init(int[2][N] rng) { this.rng = rng; len = rng[].map!(a => a[1]-a[0]+1).array; int sz = len.reduce!"a*b"; dp = new R[sz]; used = new bool[sz]; } R opCall(Args args) { int idx, base = 1; foreach (i, v; args) { version(assert) { if (v < rng[i][0] || rng[i][1] < v) { throw new RangeError; } } assert(rng[i][0] <= v && v <= rng[i][1]); idx += base*(v - rng[i][0]); base *= len[i]; } if (used[idx]) return dp[idx]; used[idx] = true; auto r = pred(args); dp[idx] = r; return r; } } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } version (X86) static if (__VERSION__ < 2071) { import core.bitop : bsf, bsr, popcnt; int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } }
D
import std.stdio,std.conv,std.string,std.algorithm,std.array,std.math; void main(){ auto sa=readln().split(); auto sb=readln().split(); auto sc=readln().split(); auto sd=readln().split(); int a,b,c,d,ans=0; a=to!int(sa[0]); b=to!int(sb[0]); c=to!int(sc[0]); d=to!int(sd[0]); if(a<b) ans+=a; else ans+=b; if(c<d) ans+=c; else ans+=d; writeln(ans); }
D
import std.stdio; import std.algorithm; import std.math; import std.conv; import std.string; T readNum(T)(){ return readStr.to!T; } T[] readNums(T)(){ return readStr.split.to!(T[]); } string readStr(){ return readln.chomp; } void main(){ auto s = readStr; bool dif; foreach(i; 0 .. 3){ if(s[i] == s[i+1]) dif = true; } writeln(dif ? "Bad" : "Good"); }
D