code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; struct UFTree(T) { struct Node { T parent; T rank = 1; } /// this(T n) { nodes.length = n; sizes.length = n; foreach (i, ref node; nodes) { node = Node(i.to!T); sizes[i] = 1; } } /// bool unite(T a, T b) { a = root(a); b = root(b); if (a == b) return false; if (nodes[a].rank < nodes[b].rank) { sizes[nodes[a].parent] += sizes[nodes[b].parent]; nodes[b].parent = nodes[a].parent; } else { sizes[nodes[b].parent] += sizes[nodes[a].parent]; nodes[a].parent = nodes[b].parent; if (nodes[a].rank == nodes[b].rank) nodes[a].rank += 1; } return true; } /// bool is_same(T a, T b) { return root(a) == root(b); } /// T size(T i) { return sizes[root(i)]; } T size() { bool[T] rs; foreach (i; 0..this.nodes.length) { rs[root(i.to!T)] = true; } T cnt; foreach (_r; rs) ++cnt; return cnt; } private: Node[] nodes; T[] sizes; T root(T i) { if (nodes[i].parent == i) return i; return nodes[i].parent = root(nodes[i].parent); } } /// UFTree!T uftree(T)(T n) { return UFTree!T(n); } void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; auto uft = uftree(N); foreach (_; 0..M) { auto xyz = readln.split.to!(int[]); auto X = xyz[0]-1; auto Y = xyz[1]-1; uft.unite(X, Y); } writeln(uft.size()); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { auto S = sread(); long N = S.to!long; long s = S.map!"a-'0'"().sum(); writeln(N % s == 0 ? "Yes" : "No"); }
D
// import chie template :) {{{ static if (__VERSION__ < 2090) { import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons, std.format, std.bitmanip, std.numeric; } else { import std; } // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan(T...)(ref T args) { foreach (ref arg; args) { arg = next!(typeof(arg)); } } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } // }}} // ModInt {{{ struct ModInt(ulong modulus) { import std.traits : isIntegral, isBoolean; import std.exception : enforce; private { ulong val; } this(const ulong n) { val = n % modulus; } this(const ModInt n) { val = n.value; } @property { ref inout(ulong) value() inout { return val; } } T opCast(T)() { static if (isIntegral!T) { return cast(T)(val); } else if (isBoolean!T) { return val != 0; } else { enforce(false, "cannot cast from " ~ this.stringof ~ " to " ~ T.stringof ~ "."); } } ModInt opAssign(const ulong n) { val = n % modulus; return this; } ModInt opOpAssign(string op)(ModInt rhs) { static if (op == "+") { val += rhs.value; if (val >= modulus) { val -= modulus; } } else if (op == "-") { if (val < rhs.value) { val += modulus; } val -= rhs.value; } else if (op == "*") { val = val * rhs.value % modulus; } else if (op == "/") { this *= rhs.inv; } else if (op == "^^") { ModInt res = 1; ModInt t = this; while (rhs.value > 0) { if (rhs.value % 2 != 0) { res *= t; } t *= t; rhs /= 2; } this = res; } else { enforce(false, op ~ "= is not implemented."); } return this; } ModInt opOpAssign(string op)(ulong rhs) { static if (op == "+") { val += ModInt(rhs).value; if (val >= modulus) { val -= modulus; } } else if (op == "-") { auto r = ModInt(rhs); if (val < r.value) { val += modulus; } val -= r.value; } else if (op == "*") { val = val * ModInt(rhs).value % modulus; } else if (op == "/") { this *= ModInt(rhs).inv; } else if (op == "^^") { ModInt res = 1; ModInt t = this; while (rhs > 0) { if (rhs % 2 != 0) { res *= t; } t *= t; rhs /= 2; } this = res; } else { enforce(false, op ~ "= is not implemented."); } return this; } ModInt opUnary(string op)() { static if (op == "++") { this += 1; } else if (op == "--") { this -= 1; } else { enforce(false, op ~ " is not implemented."); } return this; } ModInt opBinary(string op)(const ulong rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinary(string op)(ref const ModInt rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinary(string op)(const ModInt rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinaryRight(string op)(const ulong lhs) const { mixin("return ModInt(this) " ~ op ~ "= lhs;"); } long opCmp(ref const ModInt rhs) const { return cast(long)value - cast(long)rhs.value; } bool opEquals(const ulong rhs) const { return value == ModInt(rhs).value; } ModInt inv() const { ModInt ret = this; ret ^^= modulus - 2; return ret; } string toString() const { import std.format : format; return format("%s", val); } } // }}} // alias {{{ alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less); alias MinHeap(T) = Heap!(T, "a > b"); // }}} // memo {{{ /* - ある値が見つかるかどうか <https://dlang.org/phobos/std_algorithm_searching.html#canFind> canFind(r, value); -> bool - 条件に一致するやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#filter> // 2で割り切れるやつ filter!"a % 2 == 0"(r); -> Range - 合計 <https://dlang.org/phobos/std_algorithm_iteration.html#sum> sum(r); - 累積和 <https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold> // 今の要素に前の要素を足すタイプの一般的な累積和 // 累積和のrangeが帰ってくる(破壊的変更は行われない) cumulativeFold!"a + b"(r, 0); -> Range - rangeをarrayにしたいとき array(r); -> Array - 各要素に同じ処理をする <https://dlang.org/phobos/std_algorithm_iteration.html#map> // 各要素を2乗 map!"a * a"(r) -> Range - ユニークなやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#uniq> uniq(r) -> Range - 順列を列挙する <https://dlang.org/phobos/std_algorithm_iteration.html#permutations> permutation(r) -> Range - ある値で埋める <https://dlang.org/phobos/std_algorithm_mutation.html#fill> fill(r, val); -> void - バイナリヒープ <https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap> // 昇順にするならこう(デフォは降順) BinaryHeap!(Array!T, "a > b") heap; heap.insert(val); heap.front; heap.removeFront(); - 浮動小数点の少数部の桁数設定 // 12桁 writefln("%.12f", val); - 浮動小数点の誤差を考慮した比較 <https://dlang.org/phobos/std_math.html#.approxEqual> approxEqual(1.0, 1.0099); -> true - 小数点切り上げ <https://dlang.org/phobos/std_math.html#.ceil> ceil(123.4); -> 124 - 小数点切り捨て <https://dlang.org/phobos/std_math.html#.floor> floor(123.4) -> 123 - 小数点四捨五入 <https://dlang.org/phobos/std_math.html#.round> round(4.5) -> 5 round(5.4) -> 5 */ // }}} void main() { auto cin = new Scanner; int a, b; cin.scan(a, b); int cnt = 1; int res; while (cnt < b) { cnt--; cnt += a; res++; } writeln(res); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); alias route = Tuple!(long, "From", long, "To"); long bignum = 1_000_000_007; T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { auto s = sread(); long warea, barea; bool flag = (s[0] == 'W'); foreach (e; s) { if(flag && e == 'W') { warea++; flag = false; } if(!flag && e == 'B') { barea++; flag = true; } } writeln(warea + barea - 1); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nmq = readln.split.to!(int[]); auto N = nmq[0]; auto M = nmq[1]; auto Q = nmq[2]; auto cs = new int[][](N, N); foreach (_; 0..M) { auto lr = readln.split.to!(int[]); auto L = lr[0]-1; auto R = lr[1]-1; cs[L][R] += 1; } foreach (k; 1..N) { foreach (i; 0..N) { auto j = i+k; if (j >= N) break; cs[i][j] += cs[i+1][j] + cs[i][j-1]; if (i+1 < j) cs[i][j] -= cs[i+1][j-1]; } } foreach (_; 0..Q) { auto pq = readln.split.to!(int[]); writeln(cs[pq[0]-1][pq[1]-1]); } }
D
import std.stdio,std.string,std.conv; int main(){ auto input=readln.chomp.split; auto a=input[0].to!int; auto b=input[1].to!int; auto at=(a*13).to!int; auto bt=(b*10).to!int; foreach(y;bt..at+1){ if((y*0.08).to!int==a&&(y*0.1).to!int==b){ y.writeln; return 0; } } "-1".writeln; return 0; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } double euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); } double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; } double norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); } double dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD; auto B = RD; auto x = RD; auto y = RD; long z; foreach (i; 0..n) { if (z + x <= B) z += x; else z -= y; ans[ti] += z; } } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio; void main() { auto N = readln.chomp.to!int; auto edges = new int[][](N); foreach (_; 0..N-1) { auto s = readln.split.map!(to!int); edges[s[0] - 1] ~= s[1] - 1; edges[s[1] - 1] ~= s[0] - 1; } int dfs(int n, int p) { int ret = 0; foreach (m; edges[n]) if (m != p) ret ^= (dfs(m, n) + 1); return ret; } writeln( dfs(0, -1) ? "Alice" : "Bob" ); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; alias Pair = Tuple!(int, "a", int, "b"); void main() { int N, M; scan(N, M); auto g = new bool[][](N, N); foreach (i ; 0 .. M) { int ai, bi; scan(ai, bi); ai--, bi--; g[ai][bi] = g[bi][ai] = 1; } // 補グラフを作る auto adj = new int[][](N, 0); foreach (i ; 0 .. N) { foreach (j ; 0 .. N) if (j != i) { if (!g[i][j]) { adj[i] ~= j; } } } debug { writeln(adj); } auto col = new int[](N); col[] = -1; Pair nibu(int v, int c) { col[v] = c; auto res = c ? Pair(0, 1) : Pair(1, 0); foreach (u ; adj[v]) { if (col[u] == -1) { auto p = nibu(u, c ^ 1); if (p == Pair(-1, -1)) { return Pair(-1, -1); } res = Pair(res.a + p.a, res.b + p.b); } else if (col[u] == col[v]) { return Pair(-1, -1); } } return res; } Pair[] ps; foreach (i ; 0 .. N) { if (col[i] == -1) { auto p = nibu(i, 0); if (p == Pair(-1, -1)) { writeln(-1); return; } ps ~= p; } } debug { writeln(ps); } auto U = ps.length.to!int; auto dp = new bool[][](U + 1, N + 1); dp[0][0] = true; foreach (i ; 0 .. U) { foreach (j ; 0 .. N + 1) { if (dp[i][j]) { dp[i + 1][j + ps[i].a] = true; dp[i + 1][j + ps[i].b] = true; } } } auto k = 0; foreach (i ; 0 .. N + 1) { if (dp[U][i]) { chmax(k, i * (N - i)); } } debug { writefln("%(%s\n%)", dp); } auto L = N * (N - 1) / 2 - M; auto ans = M - (k - L); writeln(ans); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.stdio, std.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; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD!long; auto M = RD!long; if (abs(N - M) > 1) { writeln(0); return; } long cnt1 = 1, cnt2 = 1; foreach (i; 0..N) { modm(cnt1, N - i); } foreach (i; 0..M) { modm(cnt2, M - i); } long ans = cnt1; modm(ans, cnt2); if (N == M) modm(ans, 2); writeln(ans); stdout.flush(); }
D
void main() { writeln(readln.chomp.count('x') < 8 ? "YES" : "NO"); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio; 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 n = readln.chomp.to!int; auto a = readln.chomp.split.to!(int[]); auto ave = a.sum / n.to!double; int res; double minVal = double.max; foreach (i; 0..n) { auto d = abs(ave - a[i]); if (minVal > d) { minVal = d; res = i; } } res.writeln; }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { auto X = readln.chomp.to!(long); long res = 0; foreach (b; 1..X+1) { foreach (p; 2..10) { if (b^^p <= X) { res = max(res, b^^p); } } } writeln(res); }
D
import std.conv; import std.stdio; import std.string; void main() { auto dn = readln.split.to!( int[] ); writeln( solve( dn[ 0 ], dn[ 1 ] ) ); } auto solve( in int d, in int n ) { auto db = 1; foreach( i; 0 .. d ) { db *= 100; } return db * n + ( ( n == 100 ) ? db : 0 ); } unittest { assert( solve( 0, 5 ) == 5 ); assert( solve( 1, 11 ) == 1100 ); assert( solve( 2, 85 ) == 850000 ); assert( solve( 0, 100 ) == 101 ); assert( solve( 1, 100 ) == 10100 ); assert( solve( 2, 100 ) == 1010000 ); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto b = readln.chomp.to!(char); switch (b) { case 'A': writeln("T"); return; case 'T': writeln("A"); return; case 'C': writeln("G"); return; case 'G': writeln("C"); return; default: return; } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(int k, int[] xs) { int ans = 0; // k 個のグループの個数 int p = 0; while (p+1 < xs.length) { p += k - 1; ans++; } return ans; } void main() { auto nk = readints; int k = nk[1]; auto xs = readints; auto ans = calc(k, xs); writeln(ans); }
D
import std.stdio, std.algorithm, std.range, std.conv, std.meta; /* a1+b1, a1+b2, a1+b3 a2+b1, a2+b2, a2+b3 a3+b1, a3+b2, a3+b3 */ void main() { int[3*3] _c; ref c(size_t i, size_t j) { return _c[i * 3 + j]; } ref ct(size_t i, size_t j) { return _c[j * 3 + i]; } foreach (i; 0..3) { foreach (j, n; readln.split.map!(to!int).enumerate) { c(i, j) = n; } } foreach (f; AliasSeq!(c, ct)) { foreach (i; 0..2) { foreach (j; i..3) { auto d0 = f(i, 0) - f(j, 0); // a[i] - a[j] or b[i] - b[j] auto d1 = f(i, 1) - f(j, 1); if (d0 != d1) { writeln("No"); return; } auto d2 = f(i, 2) - f(j, 2); if (d0 != d2) { writeln("No"); return; } } } } writeln("Yes"); }
D
import std.stdio, std.string, std.conv; void main() { long n = readln.chomp.to!long; writeln((n - 1) * n / 2); }
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!(long[]); auto a = abc[0]; auto b = abc[1]; auto c = abc[2]; if (c-a-b > 0 && 4*a*b < (c-a-b)^^2L) { writeln("Yes"); } else { writeln("No"); } }
D
void main(){ int n = inelm(); ( n*(1+n)/2 ).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
void main() { long h = rdElem; long w = rdElem; long n = rdElem; long a = max(h, w); writeln((n + a - 1) / a); } T rdElem(T = long)() { //import std.stdio : readln; //import std.string : chomp; //import std.conv : to; return readln.chomp.to!T; } alias rdStr = rdElem!string; dchar[] rdDchar() { //import std.conv : to; return rdStr.to!(dchar[]); } T[] rdRow(T = long)() { //import std.stdio : readln; //import std.array : split; //import std.conv : to; return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdRow!T).array; } void wrMat(T = long)(T[][] mat) { //import std.stdio : write, writeln; foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { long A, B, C, D; scan(A, B, C, D); // writefln("%d %d %d %d", A, B, C, D); writeln((min(B, D) - max(A, C)).max(0)); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; struct FenwickTree(T) { private { int _size; T[] _data; } this(int N) { _size = N; _data = new T[](_size + 1); } void add(int i, T x) { i++; while (i <= _size) { _data[i] += x; i += i & (-i); } } T sum(int r) { T res = 0; while (r > 0) { res += _data[r]; r -= r & (-r); } return res; } } unittest { auto ft = FenwickTree!(int)(10); ft.add(0, 3); ft.add(2, 4); ft.add(5, 1); assert(ft.sum(0) == 0); assert(ft.sum(1) == 3); assert(ft.sum(2) == 3); assert(ft.sum(3) == 7); assert(ft.sum(5) == 7); assert(ft.sum(6) == 8); } void main() { int N; scan(N); auto a = readln.split.to!(int[]); auto b = readln.split.to!(int[]); foreach (i ; 0 .. N) { if (a[i] != b[i] && b[i] >= (a[i] + 1) / 2) { writeln(-1); return; } } auto d = new long[][](50 + 1, 50 + 1); fillAll(d, infl); foreach (i ; 0 .. 51) d[i][i] = 0; foreach (i ; 1 .. 51) { foreach (j ; 1 .. i + 1) { chmin(d[i][i % j], 1L << j); } } debug { writefln("%(%s\n%)", d); } foreach (i ; 0 .. 51) { foreach (j ; 0 .. 51) { foreach (k ; 0 .. 51) { chmin(d[i][j], d[i][k] + d[k][j]); } } } auto s = new long[](N); foreach (i ; 0 .. N) { s[i] = 1L << a[i]; } auto f = new bool[](N); long ans; foreach (_ ; 0 .. N) { int im; long cm; foreach (i ; 0 .. N) { if (f[i]) continue; long cst = infl; foreach (j ; 0 .. 51) { if (s[i] & 1L << j) { chmin(cst, d[j][b[i]]); } } if (cm < cst) { cm = cst; im = i; } } f[im] = true; ans |= cm; debug { writefln("i: %s, cm: %08b", im, cm); writefln("%(%020b %)", s); } foreach (i ; 0 .. N) { foreach (j ; 0 .. 51) if (s[i] & 1L << j) { foreach (k ; 1 .. 51) if (cm & 1L << k) { s[i] |= 1L << (j % k); } } } } writeln(ans); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto s = readln.chomp; writeln(s[0], s.length-2, s[$-1]); }
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; long calc(int n, int k) { long[int] d; for (int i = 1; i <= n; i++) { d[i % k]++; } long ans = 0; for (int i = 1; i <= n; i++) { int a = i % k; int bc = (k - a) % k; if ((bc + bc) % k != 0) continue; ans += d[bc] ^^ 2; } return ans; } void main() { auto nk = readints; int n = nk[0], k = nk[1]; writeln(calc(n, k)); }
D
import std.stdio,std.array,std.string,std.algorithm,std.conv; immutable STR_LENGTH = 20; int main(string[] argv) { char[] buf; stdin.readln(buf); char[] c; for(int i = 0;i < STR_LENGTH;i++){ if(buf.length > i){ c ~= buf[i]; } } reverse(c); writeln(c); return 0; }
D
unittest { assert( [ "3 2", "acp", "ae" ].parse.expand.solve == 6 ); assert( [ "6 3", "abcdef", "abc" ].parse.expand.solve == -1 ); assert( [ "15 9", "dnsusrayukuaiia", "dujrunuma" ].parse.expand.solve == 45 ); } import std.conv; import std.range; import std.stdio; import std.typecons; void main() { stdin.byLineCopy.parse.expand.solve.writeln; } auto parse( Range )( Range input ) if( isInputRange!Range && is( ElementType!Range == string ) ) { input.popFront; auto s = input.front; input.popFront; auto t = input.front; return tuple( s, t ); } auto solve( string s, string t ) { auto l = lcm( s.length, t.length ); auto si = 0L; auto ni = 0L; auto ti = 0L; auto mi = 0L; while( si < s.length && ti < t.length ) { if( ni == mi && s[ si ] != t[ ti ] ) return -1; if( ni < mi ) { si++; ni += l / s.length; } else { ti++; mi += l / t.length; } } return l; } // 最小公倍数 auto lcm( long x, long y ) { import std.numeric : gcd; return x * y / x.gcd( y ); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "a", long, "b"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long n, k; scan(n, k); long r, s, p; scan(r, s, p); auto t = sread(); long sum; auto check = new bool[](k); foreach (i; iota(k)) { switch (t[i]) { case 'r': sum += p; break; case 's': sum += r; break; case 'p': sum += s; break; default: assert(0); } } foreach (i; iota(k, n)) { switch (t[i]) { case 'r': if (t[i - k] != 'r' || check[i % k]) { sum += p; check[i % k] = false; } else { check[i % k] = true; } break; case 's': if (t[i - k] != 's' || check[i % k]) { sum += r; check[i % k] = false; } else { check[i % k] = true; } break; case 'p': if (t[i - k] != 'p' || check[i % k]) { sum += s; check[i % k] = false; } else { check[i % k] = true; } break; default: assert(0); } } sum.writeln(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; void main(){ long[] tmp = readln.chomp.split.map!(to!long).array; long a = tmp[0], b = tmp[1]; string ans; if(b < 0){ if((a - b) % 2) ans = "Positive"; // eg -4 .. -1 else ans = "Negative"; // eg -3 .. -1 } else if(a <= 0) ans = "Zero"; else ans = "Positive"; ans.writeln; }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.format; void main() { auto ip = readln.chomp.to!int; writeln(24 + (24 - ip)); }
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); auto ok = new string[](0); void dfs(int i, string t) { if (i == 4) { ok ~= t; return; } if (i == 0) { dfs(i + 1, t ~ "AKIH"); dfs(i + 1, t ~ "KIH"); } else if (i == 1) { dfs(i + 1, t ~ "AB"); dfs(i + 1, t ~ "B"); } else if (i == 2) { dfs(i + 1, t ~ "AR"); dfs(i + 1, t ~ "R"); } else { dfs(i + 1, t ~ "A"); dfs(i + 1, t); } } dfs(0, ""); foreach (t ; ok) { 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, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto K = readln.chomp.to!int; writeln(K/2 * ((K+1)/2)); }
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; void main() { immutable int N = 100; auto s = readln.split.map!(to!int); auto A = s[0] - 1; auto B = s[1] - 1; auto C = new bool[][](100, 100); foreach (i; 0..N) C[i][99] = true; // 横線 for (int i = 3; i < N-1 && A > 0; i += 4, A -= 1) { C[i].fill(true); } // 縦線 for (int j = 3; j < N && A > 0; j += 4) { for (int i = 0; i < N && A > 0; i += 4, A-= 1) { foreach (k; i..i+4) { C[k][j] = true; } } } for (int i = 1; i < N && B > 0; i += 4) { for (int j = 1; j < N && B > 0; j += 4, B -= 1) { C[i][j] = true; } } writeln(100, " ", 100); C.each!(c => c.map!(cc => cc ? "#" : ".").join("").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 readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=rdsp;foreach(ref j;v.tupleof)pick(r,j);}} void main() { int n; readV(n); struct P { int x, y, h; } P[] p; readS(n, p); auto pf = p.find!(pi => pi.h > 0); if (pf.empty) { foreach (x; 0..101) foreach (y; 0..101) if (p.all!(pi => pi.x != x && pi.y != y)) { writeln(x, " ", y, " ", 1); return; } } else { auto pt = pf[0]; foreach (x; 0..101) foreach (y; 0..101) { auto h = pt.h + (x-pt.x).abs + (y-pt.y).abs; if (p.all!(pi => max(h - (x-pi.x).abs - (y-pi.y).abs, 0) == pi.h)) { writeln(x, " ", y, " ", h); return; } } } }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { auto tmp = readln.split.to!(long[]); auto N = tmp[0], K = tmp[1]; const as = readln.split.to!(long[]); const i = N.iota.find!(i => as[i] == 1).front; int cnt = 0; if (N > 0) { N -= K; cnt++; } while (N > 0) { N -= K-1; cnt++; } writeln(cnt); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string; void main() { auto nab = readln.split.to!(long[]); auto N = nab[0]; auto A = nab[1]; auto B = nab[2]; writeln(max(0, B*(N-1)+A - (A*(N-1)+B) + 1)); }
D
void main() { auto ip = readAs!(string[]), S = ip[0], T = ip[1]; writeln(T ~ S); } // =================================== 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
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math; void main() { auto N = readln.chomp.to!int; auto AS = readln.split.to!(int[]); int[] DS1, DS2; DS1.length = N + 1; DS2.length = N; int sum_n; foreach (i; 0..N) { if (i == 0) { DS1[i] = abs(AS[i]); DS2[i] = abs(AS[i+1]); } else { DS1[i] = abs(AS[i-1] - AS[i]); DS2[i] = i == N-1 ? abs(AS[i-1]) : abs(AS[i-1] - AS[i+1]); } sum_n += DS1[i]; } DS1[N] = abs(AS[N-1]); sum_n += DS1[N]; foreach (i; 0..N) { if (i != 0) { sum_n -= DS2[i-1]; sum_n += DS1[i-1] + DS1[i]; } sum_n += DS2[i]; sum_n -= (DS1[i] + DS1[i+1]); writeln(sum_n); } }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.range; import std.stdio; import std.string; T binarySearch(T)(T[] haystack, T needle) { auto min = 0UL; auto max = haystack.length - 1; while (min <= max) { auto mid = (min + max) / 2; if (haystack[mid] == needle) { return needle; } else if (haystack[mid] < needle) { min = mid + 1; } else { max = mid - 1; } } return -1; } void main() { auto n = readln.chomp.to!int; auto s = readln.chomp.split.map!(to!int).array; auto q = readln.chomp.to!int; auto t = readln.chomp.split.map!(to!int).array; auto c = 0; foreach (e_t; t) { if (binarySearch(s, e_t) == e_t) { c++; } } c.writeln; }
D
import std.stdio,std.string,std.conv,std.array,std.algorithm; void main(){ foreach(i;1..int.max){ string[] a = readln().chomp().split(); if( a[0]=="0" && a[1]=="0" ){ break; } if( to!int(a[0]) < to!int(a[1]) ){ writeln( a[0]," ",a[1] ); }else{ writeln( a[1]," ",a[0] ); } } }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { auto N = readln.chomp.to!long; auto a = N.iota.map!(_ => readln.chomp.to!long-1).array; long c = 0; foreach (i; 0..N) { c = a[c]; if (c == 1) { writeln(i+1); return; } } writeln(-1); }
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 a = readln.chomp.to!int; auto b = readln.chomp.to!int; auto h = readln.chomp.to!int; writeln((a+b)*h/2); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void main() { auto q = RD!int; auto ans = new bool[](q); foreach (qi; 0..q) { auto n = RD!int; auto m = RD; auto tlh = new long[][](n); foreach (i; 0..n) { tlh[i] = [RD, RD, RD]; } bool ok = true; long t, h = m, l = m; foreach (i; 0..n) { auto d = tlh[i][0] - t; l = max(tlh[i][1], l-d); h = min(tlh[i][2], h+d); debug writeln("i:", i, " l:", l, " h:", h); if (l > tlh[i][2] || h < tlh[i][1]) { ok = false; break; } t = tlh[i][0]; } ans[qi] = ok; } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush; debug readln; }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void calc(int a, int b) { int d = abs(a - b); if (d % 2 == 1) { writeln("IMPOSSIBLE"); return; } writeln(min(a, b) + d / 2); } void main() { int a, b; scan(a, b); calc(a, b); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { long A, B, C, X, Y; scan(A, B, C, X, Y); long ans = long.max; ans = ans.min(A * X + B * Y); ans = ans.min(C * (max(X, Y) * 2)); ans = ans.min(C * (min(X, Y) * 2) + (X < Y ? B : A) * (max(X, Y) - min(X, Y))); writeln(ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.numeric; ulong[100] TS; T lcm(T)(T a, T b) { return a / gcd(a, b) * b; } void main() { auto N = readln.chomp.to!int; foreach (i; 0..N) { TS[i] = readln.chomp.to!ulong; } auto res = TS[0]; foreach (t; TS[1..N]) { res = lcm(res, t); } writeln(res); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; long P = 10^^6+3; long[10^^6+3] F, RF; void init() { F[0] = F[1] = 1; foreach (i, ref x; F[2..$]) x = (F[i+1] * (i+2)) % P; { RF[$-1] = 1; auto x = F[$-1]; auto k = P-2; while (k) { if (k%2 == 1) RF[$-1] = (RF[$-1] * x) % P; x = x^^2 % P; k /= 2; } } foreach_reverse(i, ref x; RF[0..$-1]) x = (RF[i+1] * (i+1)) % P; } long pow(long x, long n) { long y = 1; while (n) { if (n%2 == 1) y = (y * x) % P; x = x^^2 % P; n /= 2; } return y; } void main() { init(); auto Q = readln.chomp.to!int; foreach (_; 0..Q) { auto xdn = readln.split.to!(long[]); auto x = xdn[0]; auto d = xdn[1]; auto n = xdn[2]; if (x == 0) { writeln(0); continue; } if (d == 0) { writeln(pow(x, n)); continue; } x = (x * pow(d, P-2)) % P; if (x == 0 || x+n-1 >= P) { writeln(0); continue; } auto xton = (F[x+n-1] * RF[x-1]) % P; writeln((pow(d, n) * xton) % P); } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N, K; scan(N, K); long ans; while (0 < N) N /= K, ans++; writeln(ans); }
D
/* imports all std modules {{{*/ import std.algorithm, std.array, std.ascii, std.base64, std.bigint, std.bitmanip, std.compiler, std.complex, std.concurrency, std.container, std.conv, std.csv, std.datetime, std.demangle, std.encoding, std.exception, std.file, std.format, std.functional, std.getopt, std.json, std.math, std.mathspecial, std.meta, std.mmfile, std.net.curl, std.net.isemail, std.numeric, std.parallelism, std.path, std.process, std.random, std.range, std.regex, std.signals, std.socket, std.stdint, std.stdio, std.string, std.system, std.traits, std.typecons, std.uni, std.uri, std.utf, std.uuid, std.variant, std.zip, std.zlib; /*}}}*/ /+---test 3 1 2 3 ---+/ /+---test 4 1 3 1 1 ---+/ /+---test 8 27 23 76 2 3 5 62 52 ---+/ void main(string[] args) { const N = readln.chomp.to!long; const W = readln.split.map!(to!long).array; const all = W.sum; long cur; foreach (i, w; W) { cur += w; if (cur > all - cur) { min(abs(2*cur - all), abs(2*(cur-w) - all)).writeln; return; } } }
D
import std; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N, M, Q; scan(N, M, Q); auto a = new long[](Q); auto b = new long[](Q); auto c = new long[](Q); auto d = new long[](Q); foreach (i; 0 .. Q) scan(a[i], b[i], c[i], d[i]); a[] -= 1; b[] -= 1; long dfs1(long[] A) { if (A.length == N) { // writeln(A); long ret; foreach (i; 0 .. Q) { if (A[b[i]] - A[a[i]] == c[i]) ret += d[i]; } return ret; } long m = A.length == 0 ? 1 : A[$ - 1]; long ret = long.min; foreach (x; m .. M + 1) { ret = ret.max(dfs1(A ~ x)); } return ret; } writeln(dfs1([])); }
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 a1 = readln.chomp.split.to!(int[]); auto a2 = readln.chomp.split.to!(int[]); auto cnt = a1.sum + a2[$-1]; auto cm = cnt; foreach (i; 1..n) { cnt += a2[$-i-1] - a1[$-i]; cm = max(cnt, cm); } cm.writeln; }
D
import std.stdio; import std.range; import std.array; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.container; import std.typecons; import std.random; import std.csv; import std.regex; import std.math; import core.time; import std.ascii; import std.digest.sha; void main() { auto aaa = readln.chomp.split.map!(to!int); int n = aaa[0]; int k = aaa[1]; int m = aaa[2]; string[] msg = readln.chomp.split; ulong[string] mapper; foreach (i, s; msg) { mapper[s] = i; } ulong[] cost = readln.chomp.split.map!(to!ulong).array; ulong[] best = new ulong[n]; foreach (i; 0..k) { auto bbb = readln.chomp.split.map!(to!ulong); ulong x = bbb[0]; ulong[] a = bbb[1..$].array; ulong ans = ulong.max; foreach (v; a) { ulong w = v - 1; ans = min(ans, cost[cast(uint)w]); } // stderr.writeln([x, ans]); foreach (v; a) { ulong w = v - 1; best[cast(uint)w] = ans; // stderr.writeln("\tbest : ", msg[w], " :", [w, ans]); } } string[] next = readln.chomp.split; ulong sum = 0; foreach (s; next) { sum += best[cast(uint)mapper[s]]; } sum.writeln; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; long calc(string[] g) { int rows = cast(int) g.length; int cols = cast(int) g[0].length; auto hash = (int row, int col) => row * cols + col; auto uf = new UnionFind(rows * cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (i + 1 < rows && g[i][j] != g[i + 1][j]) uf.unite(hash(i, j), hash(i+1, j)); if (j + 1 < cols && g[i][j] != g[i][j + 1]) uf.unite(hash(i, j), hash(i, j+1)); } } long ans = 0; bool[int] used; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { int root = uf.root(hash(i, j)); if (root in used) continue; used[root] = true; long a = 0; // 黒の個数 long b = 0; // 白の個数 // root に属する全てのノード uf.doEach(root, (node) { int row = node / cols; int col = node % cols; if (g[row][col] == '#') a++; else b++; }); ans += a * b; } } return ans; } void main() { auto hw = readints; int h = hw[0], w = hw[1]; string[] g; for (int i = 0; i < h; i++) { g ~= read!string; } writeln(calc(g)); } 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) { if (_data[rb] < _data[ra]) swap(ra, rb); _data[ra] += _data[rb]; // ra に rb を繋げる _data[rb] = ra; // ra の末尾の後ろに rb を繋げる _nexts[_tails[ra]] = rb; // ra の末尾が rb の末尾になる _tails[ra] = _tails[rb]; } return ra != rb; } 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 chie template :) {{{ import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons; // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string : chomp; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.chomp.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } } // }}} void main() { auto cin = new Scanner; int a, b; cin.scan(a, b); if (a % 3 == 0 || b % 3 == 0 || (a + b) % 3 == 0) { writeln("Possible"); } else { writeln("Impossible"); } }
D
import std.datetime, std.string, std.conv, std.algorithm, std.stdio; enum Week = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", ]; void main() { foreach(line;stdin.byLine) { auto input = line.split.map!(to!int); if(input[0] == 0 && input[1] == 0) break; Week[Date(2004, input[0], input[1]).dayOfWeek].writeln; } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; ans[ti] = n; } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; void main() { while (1) { auto n = readln.chomp.to!int; if (!n) break; auto bmi = double.max; auto id = int.max; foreach (i; 0..n) { auto x = readln.chomp.split.map!(to!int); auto t = abs(22 - x[2] / (x[1]/100.0)^^2); if (t < bmi || (t == bmi && x[0] < id)) { bmi = t; id = x[0]; } } id.writeln; } }
D
import std.stdio : readln, writeln, writefln; import std.array : array, split; import std.conv : to; import std.range.primitives; import std.range : enumerate, iota, repeat, retro, assumeSorted; import std.algorithm.searching : all, any, canFind, count, countUntil; import std.algorithm.comparison : max, min, clamp; import std.algorithm.iteration : each, group, filter, map, reduce, permutations, sum, uniq; import std.algorithm.sorting : sort; import std.algorithm.mutation : reverse, swap; void main() { string s; scan(s); writeln("2018", s[4 .. $]); } void scan(T...)(ref T args) { import std.stdio : readln; import std.array : split; import std.conv : to; import std.range.primitives; 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, std.container, std.range; void main() { auto K = readln.chomp.to!long; auto k = K; int r; while (k) { ++r; foreach (long i; 0..10) { if ((k+K*i)%10 == 7) { k = (k+K*i)/10; goto ok; } } writeln(-1); return; ok: } writeln(r); }
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() { auto S = sread(); long ans = S.length; ans -= 2; while (S[0 .. ans / 2] != S[ans / 2 .. ans]) ans -= 2; writeln(ans); }
D
import std; import core.bitop; 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, "money", long, "stack"); alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less); void main() { long h, w; scan(h, w); auto a = new string[](h); iota(h).each!(x => a[x] = sread()); long[char] cntalf; foreach (e; a) { foreach (f; e) { if (f in cntalf) cntalf[f] += 1; else cntalf[f] = 1; } } long cnt4, cnt2, cnt1; foreach (key, value; cntalf) { cnt4 += value / 4; cnt2 += (value % 4) / 2; cnt1 += value % 2; } bool check = true; long need4 = (h / 2) * (w / 2), need2 = (h % 2) * (w / 2) + (w % 2) * (h / 2), need1 = (h % 2) * (w % 2); if (need4 > cnt4) check = false; cnt2 += (cnt4 - need4) * 2; if (need2 > cnt2) check = false; if (need1 > cnt1) check = false; if (check) writeln("Yes"); else writeln("No"); } 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.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { readln.chomp.map!(ch => ch == 'A' || ch == 'G' || ch == 'C' || ch == 'T') .group .map!(pair => pair[0] ? pair[1] : 0) .reduce!max .writeln; } int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new int[][](n, 0); foreach (i; 0 .. m) { int u, v; scan(u, v); if (is1indexed) { u--, v--; } adj[u] ~= v; if (isUndirected) { adj[v] ~= u; } } return adj; } alias Edge = Tuple!(int, "to", int, "cost"); Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new Edge[][](n, 0); foreach (i; 0 .. m) { int u, v, c; scan(u, v, c); if (is1indexed) { u--, v--; } adj[u] ~= Edge(v, c); if (isUndirected) { adj[v] ~= Edge(u, c); } } return adj; } void yes(bool b) { writeln(b ? "Yes" : "No"); } void YES(bool b) { writeln(b ? "YES" : "NO"); } T[] readArr(T)() { return readln.split.to!(T[]); } T[] readArrByLines(T)(int n) { return iota(n).map!(i => readln.chomp.to!T).array; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } 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() { long t1, t2; rdVals(t1, t2); long a1, a2; rdVals(a1, a2); long b1, b2; rdVals(b1, b2); long diff1 = b1 - a1, diff2 = b2 - a2; long sign1 = diff1 / diff1.abs, sign2 = diff2 / diff2.abs; if (sign1 != sign2) { long len1 = diff1 * t1, len2 = diff2 * t2; long diff = len1 + len2; if (diff == 0) "infinity".writeln; else if (sign1 > 0 && diff > 0) 0.writeln; else if (sign1 < 0 && diff < 0) 0.writeln; else { long d = len1.abs / diff.abs, r = len1.abs % diff.abs; writeln(2 * d + (r != 0)); } } else 0.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 t; rd(t); while (t--) { int l, r; rd(l, r); writeln(l, " ", l * 2); } } 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.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; import std.container : DList; 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() { Node node = new Node(); node.value = int.max; int q = readint; for (int i = 0; i < q; i++) { auto qu = readints; switch (qu[0]) { case 0: // insert { auto x = new Node(); x.value = qu[1]; if (node !is null) { auto prev = node.prev; if (prev !is null) { prev.next = x; x.prev = prev; } node.prev = x; x.next = node; } node = x; break; } case 1: // move { int d = qu[1]; if (d >= 0) { for (int j = 0; j < d; j++) { node = node.next; } } else { for (int j = 0; j < -d; j++) { node = node.prev; } } break; } case 2: // erase { assert(node !is null); auto prev = node.prev; auto next = node.next; if (prev !is null) { prev.next = next; } if (next !is null) { next.prev = prev; } node = next; break; } default: break; } } auto x = node; while (x.prev !is null) x = x.prev; while (x !is null) { if (x.value == int.max) break; // sentinel writeln(x.value); x = x.next; } } class Node { int value; Node next; Node prev; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto abk = readln.split.to!(long[]); auto A = abk[0]; auto B = abk[1]; auto K = abk[2]; if (A >= K) { A -= K; } else { K -= A; A = 0; B = max(0, B - K); } writeln(A, " ", B); }
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, std.datetime; // 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() { auto S = sread(); long N = S.length + 1; auto d = new long[](N); foreach_reverse (i; 0 .. N - 1) { if (S[i] == '>') { d[i] = d[i + 1] + 1; } } foreach (i; 0 .. N - 1) { if (S[i] == '<') { d[i + 1] = max(d[i + 1], d[i] + 1); } } // writeln(d); d.sum().writeln(); }
D
import std.stdio; import std.algorithm; import std.string; import std.range; import std.array; import std.conv; import std.complex; import std.math; import std.ascii; import std.bigint; import std.container; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } const real eps = 1e-10; void main(){ while(true) { auto NMP = readInts(); auto N = NMP[0]; auto M = NMP[1]; auto P = NMP[2]; if(N == 0 && M == 0 && P == 0) { return; } int[] X; for(int i; i < N; ++i) { X ~= readInt(); } auto sum = reduce!("a+b")(0, X); writeln(X[M-1] == 0 ? 0: sum * (100-P) / X[M-1]); } }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); void main() { auto st = aryread!string(); // writeln(st); long a, b; scan(a, b); auto u = sread(); // writeln(u); if (u == st[0]) { writeln(a - 1, ' ', b); } else if (u == st[1]) { writeln(a, ' ', b - 1); } } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
void main(){ int n = _scan(); writeln(n%1000==0? 0: 1000-n%1000); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto x = readln.chomp.split.to!(int[]); if (x[0]+x[1]==x[2]||x[0]+x[2]==x[1]||x[1]+x[2]==x[0]) { writeln("Yes"); } else { writeln("No"); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto np = readln.split.to!(int[]); auto P = np[1]; auto S = readln.chomp; if (P == 2 || P == 5) { long r; foreach (i, c; S) { if ((c-'0') % P == 0) r += i+1; } writeln(r); return; } auto cs = new long[](P); cs[0] = 1; long x, t = 1, r; foreach_reverse (c; S) { x = ((c-'0').to!long * t + x) % P; t = (t * 10) % P; r += cs[x]; ++cs[x]; } writeln(r); }
D
import std.stdio,std.string,std.conv,std.array,std.algorithm; void main(){ for(;;write("\n")){ auto a = readln().chomp().split(); if( a[0]=="0" && a[1]=="0" ){ break; } int h=to!int(a[0]) , w=to!int(a[1]); foreach(y;0..h){ foreach(x;0..w){ if( (x+y)%2==1 ){ write("."); }else{ write("#"); } } write("\n"); } } }
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() { readln.chomp.replace(",", " ").writeln; }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; void main() { long[] ans = new long[31]; ans[0] = 1; foreach(i; 1..31) { ans[i] += ans[i-1]; if (i>1) ans[i] += ans[i-2]; if (i>2) ans[i] += ans[i-3]; } while(true) { int n = readln.chomp.to!int; if (n==0) break; writeln((ans[n]+3650-1)/3650); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto A = RD; auto B = RD; writeln(A * B % 2 == 0 ? "Even" : "Odd"); stdout.flush(); debug readln(); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.ascii; void main() { auto a = readln.chomp.to!int; auto s = readln.chomp; if (a >= 3200) { writeln(s); } else { writeln("red"); } }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; bool isPrime(int N) { if (N <= 1) return false; for (int i = 2; i*i <= N; i++) { if (N % i == 0) return false; } return true; } enum lim = 10^^5 + 1; void main() { int Q; scan(Q); auto b = new bool[](lim); foreach (i ; 1 .. lim) { b[i] = (i % 2) && isPrime(i) && isPrime((i + 1) / 2); } auto c = new int[](lim + 1); foreach (i ; 1 .. lim + 1) { c[i] = c[i - 1] + b[i - 1]; } foreach (_ ; 0 .. Q) { int li, ri; scan(li, ri); auto ans = c[ri + 1] - c[li - 1]; writeln(ans); } }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = readln.chomp.to!size_t; auto ai = readln.split.to!(int[]); auto c = 0L; mergeSort(ai, 0, n, c); writeln(c); } void merge(ref int[] ai, size_t l, size_t m, size_t r, ref long c) { auto li = ai[l..m].dup; auto ri = ai[m..r].dup; li ~= int.max; ri ~= int.max; auto i = 0, j = 0, t = 0L; foreach (k; l..r) { if (li[i] <= ri[j]) { ai[k] = li[i]; ++i; c += t; } else { ai[k] = ri[j]; ++j; ++t; } } } void mergeSort(ref int[] ai, size_t l, size_t r, ref long c) { if (l + 1 < r) { auto m = (l + r) / 2; mergeSort(ai, l, m, c); mergeSort(ai, m, r, c); merge(ai, l, m, r, c); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto lr = readln.split.to!(long[]); auto L = lr[0]; auto R = lr[1]; if (R - L >= 2050) { writeln(0); } else { long min_r = long.max; foreach (i; L..R) foreach (j; i+1..R+1) min_r = min(min_r, i * j % 2019L); writeln(min_r); } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "begin", long, "end"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long k, t; scan(k, t); auto a = aryread(); auto m = a.reduce!(max); long other = k - m; writeln(max(0, m - 1 - other)); } 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.range; void main(){ string s = readln.chomp; foreach(i; 0..3){ if(s[i] == s[i+1]){ writeln("Bad"); return; } } writeln("Good"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; bool eq(char a, char b) { return a == '?' || b == '?' || a == b; } void main() { auto a = readln.chomp.to!(char[]); auto b = readln.chomp.to!(char[]); auto c = readln.chomp.to!(char[]); auto A = a.length.to!int; auto B = b.length.to!int; auto C = c.length.to!int; bool[20000] ab, ac, bc; ab[] = true; ac[] = true; bc[] = true; foreach (i; 0..A) foreach (j; 0..B) if (!eq(a[i], b[j])) ab[i-j + 10000] = false; foreach (i; 0..A) foreach (j; 0..C) if (!eq(a[i], c[j])) ac[i-j + 10000] = false; foreach (i; 0..B) foreach (j; 0..C) if (!eq(b[i], c[j])) bc[i-j + 10000] = false; auto r = int.max; foreach (i; -4000..4001) foreach (j; -4000..4001) { if (ab[i+10000] && ac[j+10000] && bc[j-i+10000]) { r = min(r, max(A, i+B, j+C)-min(0, i, j)); } } writeln(r); }
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; void main() { auto S = scanElem!string; long s,res; foreach(c; S) { if(c=='S')s++; if(c=='T'&&s!=0) { s--; res+=2; } } writeln(S.length-res); } class UnionFind{ UnionFind parent = null; void merge(UnionFind a) { if(same(a)) return; a.root.parent = this.root; } UnionFind root() { if(parent is null)return this; return parent = parent.root; } bool same(UnionFind a) { return this.root == a.root; } } void scanValues(TList...)(ref TList list) { auto lit = readln.splitter; foreach (ref e; list) { e = lit.fornt.to!(typeof(e)); lit.popFront; } } T[] scanArray(T = long)() { return readln.split.to!(long[]); } void scanStructs(T)(ref T[] t, size_t n) { t.length = n; foreach (ref e; t) { auto line = readln.split; foreach (i, ref v; e.tupleof) { v = line[i].to!(typeof(v)); } } } long scanULong(){ long x; while(true){ const c = getchar; if(c<'0'||c>'9'){ break; } x = x*10+c-'0'; } return x; } T scanElem(T = long)() { char[] res; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { res ~= cast(char) c; c = getchar; } return res.strip.to!T; } template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } struct Factor { long n; long c; } Factor[] factors(long n) { Factor[] res; for (long i = 2; i ^^ 2 <= n; i++) { if (n % i != 0) continue; int c; while (n % i == 0) { n = n / i; c++; } res ~= Factor(i, c); } if (n != 1) res ~= Factor(n, 1); return res; } long[] primes(long n) { if(n<2)return []; auto table = new long[n+1]; long[] res; for(int i = 2;i<=n;i++) { if(table[i]==-1) continue; for(int a = i;a<table.length;a+=i) { table[a] = -1; } res ~= i; } return res; } bool isPrime(long n) { if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (long i = 3; i ^^ 2 <= n; i += 2) if (n % i == 0) return false; return true; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nabcd = readln.split.to!(int[]); auto N = nabcd[0]; auto A = nabcd[1]-1; auto B = nabcd[2]-1; auto C = nabcd[3]-1; auto D = nabcd[4]-1; auto S = readln.chomp.to!(char[]); auto len = S.length; if (D < C) { swap(A, B); swap(C, D); } bool moved; while (A != C || B != D) { if (B < D) { if (B+1 < len && S[B+1] != '#' && B+1 != A) { ++B; moved = true; } else if (B+2 < len && S[B+2] != '#' && B+2 != A) { B += 2; moved = true; } } if (A < C && !moved) { if (A+1 < len && S[A+1] != '#' && A+1 != B) { ++A; moved = true; } else if (A+2 < len && S[A+2] != '#' && A+1 != B && A+2 != B) { A += 2; moved = true; } } if (!moved) { writeln("No"); return; } moved = false; } writeln("Yes"); }
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 nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; if (N > M) swap(N, M); writeln(N == 1 || (N == 2 && M == 2) ? "YES" : "NO"); } }
D
void main() { long[] tmp = readln.split.to!(long[]); long x = tmp[0], y = tmp[1]; writeln(x % y != 0 ? x : -1); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void main() { auto X = RD; auto ans = new long[](2); for (long i = 1; i*i <= X; ++i) { if (X % i == 0) { if (lcm(i, X/i) == X) ans = [i, X / i]; } } if (X == 1) writeln("1 1"); else writeln(ans[0], " ", ans[1]); stdout.flush; debug readln; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } long calc(string s) { long ans = 0; auto xs = new int[26]; for (int i = 0; i < s.length; i++) { int p = s[i] - 'a'; for (int j = 0; j < xs.length; j++) { if (j != p) ans += xs[j]; } xs[p]++; } return ans + 1; } void main() { auto s = readln.chomp; writeln(calc(s)); }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; int INF = int.max/3; void main() { int K = 26; int[] cs = readln.chomp.map!"a-'a'".map!(to!int).array; int[] as = new int[cs.length]; foreach(i, c; cs) { as[i] = 1<<c ^ (i==0 ? 0 : as[i-1]); } int[] dp = INF.repeat((1<<K)+1).array; dp[0] = 0; int opt; as.each!( a => dp[a] = min( dp[a], opt = (K+1).iota.map!( i => dp[1<<(K-1)>>i ^ a] ).reduce!min + 1 ) ); opt.writeln; } // ---------------------------------------------- void scanln(Args...)(ref Args args) { foreach(i, ref v; args) { "%d".readf(&v); (i==args.length-1 ? "\n" : " ").readf; } // ("%d".repeat(args.length).join(" ") ~ "\n").readf(args); } void times(alias fun)(int n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { auto minElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto minimum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b < minimum) { element = a; minimum = b; } } return element; } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto maximum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b > maximum) { element = a; maximum = b; } } return element; } }
D
import std.functional, std.algorithm, std.container, std.typetuple, std.typecons, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv; bool judge(int[] A, int[] B, int C, int M) { int sum = C; foreach (i; 0..M) { sum += A[i] * B[i]; } return sum > 0; } void main() { int[] NMC = readln.chomp.split.to!(int[]); int N = NMC[0], M = NMC[1], C = NMC[2]; int[] B = readln.chomp.split.to!(int[]); int[][] As; As.length = N; foreach (i; 0..N) { As[i] = readln.chomp.split.to!(int[]); } writeln(As.count!(A => A.judge(B, C, M))); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int h, w; scan(h, w); long ans = min(solve(h, w), solve(w, h)); writeln(ans); } long solve(int h, int w) { long ans = inf6; foreach (x ; 0 .. w + 1) { long b = 1L * h * x; long a = 1L * (h / 2) * (w - x); long c = 1L * ((h + 1) / 2) * (w - x); auto m = max(abs(a - b), abs(a - c), abs(b - c)); ans = min(ans, m); a = 1L * h * ((w - x) / 2); c = 1L * h * ((w - x + 1) / 2); m = max(abs(a - b), abs(a - c), abs(b - c)); ans = min(ans, m); } return 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.algorithm, std.string, std.range, std.stdio, std.conv; void main() { long[] nab = readln.chomp.split.to!(long[]); long N = nab[0], A = nab[1], B = nab[2]; long[] hs = N.iota.map!(_ => readln.chomp.to!long).array; long b, e = 10^^9; while (e - b > 1) { long t = (b + e) / 2; long sum; foreach (hi; hs) { if (hi - (t * B) > 0) { sum += (hi - (t * B) + (A - B - 1))/(A-B); } if (sum > t) { break; } } if (sum > t) { b = t; } else { e = t; } } writeln(e); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; if (M < N - 1) { writeln("Impossible"); return; } auto X = new int[][](N+1); auto Y = new int[][](N+1); foreach (i; 2..N+1) { if (!X[i].empty) continue; for (int j = i; j <= N; j += i) { X[j] ~= i; Y[i] ~= j; } } int cnt = 0; auto ans = new Tuple!(int, int)[](M); foreach (i; 2..N+1) { ans[cnt] = tuple(1, i); cnt += 1; } for (int i = N - (1 - N%2); i > 2 && cnt < M; i -= 2) { auto tmp = new bool[](i); foreach (j; X[i]) foreach (k; Y[j]) if (k < i) tmp[k] = true; for (int j = 2; j < i && cnt < M; ++j) if (!tmp[j]) ans[cnt] = tuple(j, i), cnt++; } for (int i = N - N%2; i > 1 && cnt < M; i -= 2) { auto tmp = new bool[](i); foreach (j; X[i]) foreach (k; Y[j]) if (k < i) tmp[k] = true; for (int j = 2; j < i && cnt < M; ++j) if (!tmp[j]) ans[cnt] = tuple(j, i), cnt++; } if (cnt == M) { writeln("Possible"); ans.each!(a => writeln(a[0], " ", a[1])); } else { writeln("Impossible"); } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(int a, int b) { int height(int n) { return n * (n + 1) / 2; } for (int i = 1; i < 999; i++) { int x = height(i); int y = height(i + 1); int dx = x - a; int dy = y - b; if (dx == dy && dx > 0) return dx; /* if (a >= x || b >= y) continue; if (x < a || y < b) continue; int s = x + y - (a + b); if (s > 1 && s % 2 == 0) { return s / 2; } */ } assert(false); } void main() { auto ab = readints; int a = ab[0], b = ab[1]; auto ans = calc(a, b); writeln(ans); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void main() { auto C = RD!string; writeln(cast(string)[C[0]+1]); stdout.flush; debug readln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto as = readln.split.to!(int[]); auto cs = new long[](N); foreach (long i; 0..N) { auto a = as[i]; if (i - (a-1) < 0) continue; ++cs[i-(a-1)]; } long r; foreach (ii, a; as) { auto i = ii.to!long; if (i + a + 1 >= N) continue; r += cs[i+a+1]; } writeln(r); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}} void main() { string[] s; readM(3, s); writeln(s[0][0], s[1][1], s[2][2]); }
D
void main() { import std.stdio, std.string, std.array, std.algorithm, std.conv; auto b = readln.chomp.split.map!(to!int); b[0].solve(b[1]).writeln; } int solve(in int nTestCase, in int mTLE) { immutable base = 100 * nTestCase + 1800 * mTLE, pInv = 1 << mTLE; /* S := 1 p + 2 pq + 3 pq^2 + ... qS := 1 pq + 2 pq^2 + 3 pq^3 + ... S = (p + pq + pq^2 + ...) / (1-q) S = 1 + q + q^2 + ... S = 1/(1-q) = 1/p = pInv; */ return base * pInv; } unittest { assert (1.solve(1) == 3800); assert (10.solve(2) == 18400); assert (100.solve(5) == 608000); import std.stdio; stderr.writeln("unittest passed!"); }
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; immutable long MOD = 10^^9 + 7; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto H = s[1]; auto D = s[2]; auto F = new long[](N+1); F[0] = F[1] = 1; foreach (i; 2..N+1) F[i] = F[i-1] * i % MOD; long co = 0; foreach (i; 1..N+1) co = (co + F[i]) % MOD; auto dp = new long[](H+1); dp[0] = F[N]; auto st = new LazySegmentTree!(long, long, (a,b)=>(a+b)%MOD, (a,b)=>(a+b)%MOD, (a,b)=>(a+b)%MOD, (a,b)=>a*b%MOD, 0L, 0L)(H+1); st.update(0, 0, 1); foreach (i; 0..H) { if (i == 0) { st.update(i+1, min(i+D, H), F[N]); } else { long tmp = st.query(i, i); st.update(i+1, min(i+D, H), tmp*co%MOD); } } st.query(H, H).writeln; } class LazySegmentTree(T, L, alias opTT, alias opTL, alias opLL, alias opPrd, T eT, L eL) { T[] table; L[] lazy_; int n; int size; this(int n) { this.n = n; size = 1; while (size <= n) size <<= 1; size <<= 1; table = new T[](size); lazy_ = new L[](size); table[] = eT; lazy_[] = eL; } void push(int i, int a, int b) { if (lazy_[i] == eL) return; table[i] = opTL(table[i], opPrd(lazy_[i], b - a + 1)); if (i * 2 + 1 < size) { lazy_[i*2] = opLL(lazy_[i*2], lazy_[i]); lazy_[i*2+1] = opLL(lazy_[i*2+1], lazy_[i]); } lazy_[i] = eL; } T query(int l, int r) { if (l > r) return eT; return query(l, r, 1, 0, n-1); } T query(int l, int r, int i, int a, int b) { if (b < l || r < a) return eT; push(i, a, b); if (l <= a && b <= r) { return table[i]; } else { return opTT(query(l, r, i*2, a, (a+b)/2), query(l, r, i*2+1, (a+b)/2+1, b)); } } void update(int l, int r, L val) { if (l > r) return; update(l, r, 1, 0, n-1, val); } void update(int l, int r, int i, int a, int b, L val) { if (b < l || r < a) { push(i, a, b); } else if (l <= a && b <= r) { lazy_[i] = opLL(lazy_[i], val); push(i, a, b); } else { push(i, a, b); update(l, r, i*2, a, (a+b)/2, val); update(l, r, i*2+1, (a+b)/2+1, b, val); table[i] = opTT(table[i*2], table[i*2+1]); } } }
D