code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio; void main() { int n; scanf("%d", &n); long[500][500] m; int r, c; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%lld", &m[i][j]); if (m[i][j] == 0) { r = i; c = j; } } } long sum = 0; long s = 1; if (n > 1) { int g = 0; if (r == 0) { g++; } for (int i = 0; i < n; i++) { sum += m[g][i]; } s = sum; for (int i = 0; i < n; i++) { s -= m[r][i]; } } else { sum = 1; } m[r][c] = s; long sumDiagL = 0; long sumDiagR = 0; for (int i = 0; i < n; i++) { long sumRow = 0; long sumCol = 0; for (int j = 0; j < n; j++) { sumRow += m[i][j]; sumCol += m[j][i]; } sumDiagL += m[i][i]; sumDiagR += m[n - 1 - i][i]; if (sumRow != sum || sumCol != sum) { s = -1; } } if (sumDiagL != sum || sumDiagR != sum) { s = -1; } if (s <= 0) { printf("-1\n"); } else { printf("%lld\n", s); } }
D
import std.stdio; import std.string; import std.format; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.concurrency; import std.traits; import std.uni; import std.regex; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; enum long INF = long.max/5; void main() { readln.chomp.any!"a=='7'".pipe!"a?\"Yes\":\"No\"".writeln; } // ---------------------------------------------- void times(alias fun)(long n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(long n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { // `(x+y-1)/y` will only work for positive numbers ... T t = x / y; if (y > 0 && t * y < x) t++; if (y < 0 && t * y > x) t++; return t; } T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { T t = x / y; if (y > 0 && t * y > x) t--; if (y < 0 && t * y < x) t--; return t; } ref T ch(alias fun, T, S...)(ref T lhs, S rhs) { return lhs = fun(lhs, rhs); } unittest { long x = 1000; x.ch!min(2000); assert(x == 1000); x.ch!min(3, 2, 1); assert(x == 1); x.ch!max(100).ch!min(1000); // clamp assert(x == 100); x.ch!max(0).ch!min(10); // clamp assert(x == 10); } mixin template Constructor() { import std.traits : FieldNameTuple; this(Args...)(Args args) { // static foreach(i, v; args) { foreach(i, v; args) { mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v; } } } void scanln(Args...)(auto ref Args args) { enum sep = " "; enum n = Args.length; enum fmt = n.rep!(()=>"%s").join(sep); string line = readln.chomp; static if (__VERSION__ >= 2074) { line.formattedRead!fmt(args); } else { enum argsTemp = n.iota.map!( i => "&args[%d]".format(i) ).join(", "); mixin( "line.formattedRead(fmt, " ~ argsTemp ~ ");" ); } } // 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); } } } } // popcnt with ulongs was added in D 2.071.0 static if (__VERSION__ < 2071) { ulong popcnt(ulong x) { x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L); x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L); x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL); x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL); x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL); x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL); return x; } }
D
import std.stdio; 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 times(alias pred)(int n) { foreach(i; 0..n) pred(); } auto rep(alias pred, T = typeof(pred()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = pred(); return res; } void main() { bool[] top = readln.chomp.map!(a => a=='p').array; int N = top.length.to!int; int[] ary = new int[N+1]; foreach(i, e; top) { ary[i+1] = ary[i] + (e ? -1:1); } writeln(ary.back/2); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int w, h, n; readV(w, h, n); auto x1 = 0, x2 = w, y1 = 0, y2 = h; foreach (_; 0..n) { int x, y, a; readV(x, y, a); switch (a) { case 1: x1 = max(x1, x); break; case 2: x2 = min(x2, x); break; case 3: y1 = max(y1, y); break; case 4: y2 = min(y2, y); break; default: assert(0); } } writeln(x2<x1 || y2<y1 ? 0 : (x2-x1)*(y2-y1)); }
D
// import std.stdio,std.array,std.conv; void main(){ auto sn=readln.split,n=to!int(sn[0]); auto sk=readln.split,k=to!int(sk[0]); int d=1; for(int i=0;i<n;i++){ int a=d*2; int b=d+k; if(a>b) d=b; else d=a; } writeln(d); }
D
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; import std.datetime, std.bigint; immutable long mod = 10^^9 + 7; int n; int[] t, a; void main() { scan(n); t = readln.split.to!(int[]); a = readln.split.to!(int[]); auto L = new int[](n); auto H = new int[](n); L[0] = H[0] = t[0]; foreach (i ; 1 .. n) { if (t[i - 1] < t[i]) { L[i] = H[i] = t[i]; } else { L[i] = 1; H[i] = t[i]; } } if (!(L[n - 1] <= a[n - 1] && a[n - 1] <= H[n - 1])) { writeln(0); return; } L[n - 1] = H[n - 1] = a[n - 1]; foreach_reverse (i ; 0 .. n - 1) { if (a[i] > a[i + 1]) { if (L[i] <= a[i] && a[i] <= H[i]) { L[i] = H[i] = a[i]; } else { writeln(0); return; } } else { if (L[i] == H[i]) { if (H[i] > a[i]) { writeln(0); return; } } else { H[i] = min(H[i], a[i]); } } } long ans = 1; foreach (i ; 0 .. n) { (ans *= (H[i] - L[i] + 1)) %= mod; } writeln(ans); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, 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 c = new char[](2*n + 1); auto a = new int[](2*n + 1); c[0] = '#', a[0] = 0; foreach (i ; 1 .. 2*n + 1) { scan(c[i], a[i]); } debug { writeln(a); } auto revw = new int[](n + 1); auto revb = new int[](n + 1); foreach (i ; 1 .. 2*n + 1) { if (c[i] == 'W') { revw[a[i]] = i; } else { revb[a[i]] = i; } } auto wf = new int[][](2*n + 1, n + 2); auto wb = new int[][](2*n + 1, n + 2); auto bf = new int[][](2*n + 1, n + 2); auto bb = new int[][](2*n + 1, n + 2); foreach (i ; 1 .. 2*n + 1) { foreach (j ; 1 .. i + 1) { if (c[j] == 'W') { wf[i][a[j]]++; } else { bf[i][a[j]]++; } } foreach_reverse (j ; 0 .. n + 1) { wf[i][j] += wf[i][j+1]; bf[i][j] += bf[i][j+1]; } } foreach (i ; 1 .. 2*n + 1) { foreach (j ; i .. 2*n + 1) { if (c[j] == 'W') { wb[i][a[j]]++; } else { bb[i][a[j]]++; } } foreach (j ; 1 .. n + 2) { wb[i][j] += wb[i][j-1]; bb[i][j] += bb[i][j-1]; } } debug { writeln("wf:"); writefln("%(%s\n%)", wf); writeln; writeln("wb:"); writefln("%(%s\n%)", wb); writeln; } auto dp = new int[][](n + 1, n + 1); fillAll(dp, 1<<30); dp[0][0] = 0; foreach (i ; 0 .. n + 1) { foreach (j ; 0 .. n + 1) { if (i + 1 <= n) { dp[i+1][j] = min(dp[i+1][j], dp[i][j] + wf[revw[i+1]][i+2] + bf[revw[i+1]][j+1]); //+ wb[revw[i+1]][i] + bb[revw[i+1]][max(j-1, 0)]); } if (j + 1 <= n) { dp[i][j+1] = min(dp[i][j+1], dp[i][j] + wf[revb[j+1]][i+1] + bf[revb[j+1]][j+2]); //+ wb[revb[j+1]][max(i-1, 0)] + bb[revb[j+1]][j]); } } } debug { writefln("%(%(%s %)\n%)", dp); } writeln(dp[n][n]); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.0.2" +/ import std.algorithm, std.range, std.stdio, std.math, std.conv, core.bitop; // import dcomp.scanner; // import dcomp.algorithm; int main() { import std.conv; auto sc = new Scanner(); int n; int[] ai; sc.read(n, ai); bool solve(int md) { bool[] a = ai.map!(a => (md <= a)).array; int l = n-1; while (l) { if (a[l-1] == a[l]) break; l--; } int r = n-1; while (r < 2*n-2) { if (a[r] == a[r+1]) break; r++; } if ((n-1)-l < r-(n-1)) { return !a[l]; } else { return !a[r]; } } writeln(binSearch!(solve)(0, 2*n+10)-1); return 0; } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File, readln, stdin; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeString, isDynamicArray; import std.algorithm : map; File f; this(File f = stdin) { this.f = f; } string[] buf; bool succ() { while (!buf.length) { if (f.eof) return false; buf = readln.split; } return true; } int read(Args...)(auto ref Args args) { foreach (i, ref v; args) { if (!succ()) return i; alias VT = typeof(v); static if (!isSomeString!VT && isDynamicArray!VT) { v = buf.map!(to!(ElementType!VT)).array; buf.length = 0; } else { v = buf.front.to!VT; buf.popFront(); } } return args.length; } } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/algorithm.d */ // module dcomp.algorithm; template binSearch(alias pred) { T binSearch(T)(T l, T r) { while (r-l > 1) { int md = (l+r)/2; if (!pred(md)) l = md; else r = md; } return r; } }
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.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional; import std.algorithm, std.container; void main() { auto N = scanElem; auto M = scanElem; auto Q = scanElem; long[501][501] table; foreach(i; 0..M) { long[] train = scanArray; long l = train[0]; long r = train[1]; table[r][l] += 1; } foreach(x; 1..501) { foreach(y; 1..501) { table[x][y] += table[x-1][y]; table[x][y] += table[x][y-1]; table[x][y] -= table[x-1][y-1]; } } foreach(i; 0..Q) { long[] query = scanArray; long p = query[0]; long q = query[1]; writeln(table[q][q] - table[p-1][q] - table[q][p-1] + table[p-1][p-1]); } } 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)() { string 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; } 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 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 a = sread(); auto b = sread(); bool z = a[0] == b[2] && a[1] == b[1] && a[2] == b[0]; writeln(z ? "YES" : "NO"); }
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; int[][] A; scan(N); A = new int[][](N, N); iota(N).each!(i => A[i] = readln.split.to!(int[])); long ans; foreach (i ; 0 .. N) { foreach (j ; i + 1 .. N) { bool flag = true; foreach (k ; 0 .. N) { if (k == i || k == j) continue; if (A[i][j] > A[i][k] + A[k][j]) { writeln(-1); return; } if (A[i][j] == A[i][k] + A[k][j]) { flag = false; break; } } if (flag) ans += A[i][j]; } } writeln(ans); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.math; void main() { int a, b, x; scan(a, b, x); writeln(0 <= x - a && x - a <= b ? "YES" : "NO"); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.string; import std.conv; void main() { int n; while ((n = readln.chomp.to!int) != 0) { int count = 0; while ((n /= 5) != 0) { count += n; } writeln(count); } }
D
void main() { auto N = readAs!ulong; // 75 = 3 * 5^2 // (75), (3, 25), (5, 15), (3, 5, 5) // (74), (2, 24), (4, 14), (2, 4, 4) pl = generate_prime_list(100); ulong[ulong] arr; foreach(i; 1..N+1) { foreach(k; pl) { ulong p = 1; while(i % k^^p == 0) { arr[k]++; p++; } } } ulong res; ulong r(ulong m) { return arr.values.count!(i => i >= m - 1); } res += r(75); res += r(25) * (r(3) - 1); res += r(15) * (r(5) - 1); res += r(5) * (r(5) - 1) * (r(3) - 2) / 2; res.writeln; } ulong[] pl; ulong[] generate_prime_list(T)(T N) if(isIntegral!T) { ulong[] prime_list = [2]; bool not_prime = false; foreach(i; 3..N.to!ulong+1) { foreach(j; prime_list) { if(i % j == 0) { not_prime = true; break; } } if(!not_prime) prime_list ~= i; not_prime = false; } return prime_list; } // =================================== 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
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.3.2" lflags "-stack_size" "100000000" +/ import std.algorithm, std.conv, std.range, std.stdio; // import dcomp.scanner; // import dcomp.algorithm; // import dcomp.container.deque; int main(string[] argv) { auto sc = new Scanner(); int n, a, b; sc.read(n, a, b); int[] ti = new int[](n); foreach (i; 0..n) { sc.read(ti[i]); } writeln(n-ti.count!(x => (a <= x && x < b))); return 0; } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/container/deque.d */ // module dcomp.container.deque; struct Deque(T) { import core.exception : RangeError; import core.memory : GC; import std.range : ElementType, isInputRange; import std.traits : isImplicitlyConvertible; struct Payload { T *d; size_t st, length, cap; @property bool empty() const { return length == 0; } alias opDollar = length; ref inout(T) opIndex(size_t i) inout { version(assert) if (length <= i) throw new RangeError(); return d[(st+i >= cap) ? (st+i-cap) : st+i]; } private void expand() { import std.algorithm : max; assert(length == cap); auto nc = max(4L, 2*cap); T* nd = cast(T*)GC.malloc(nc * T.sizeof); foreach (i; 0..length) { nd[i] = this[i]; } d = nd; st = 0; cap = nc; } void insertFront(T v) { if (length == cap) expand(); if (st == 0) st += cap; st--; length++; this[0] = v; } void insertBack(T v) { if (length == cap) expand(); length++; this[length-1] = v; } void removeFront() { assert(!empty, "Deque.removeFront: Deque is empty"); st++; length--; if (st == cap) st = 0; } void removeBack() { assert(!empty, "Deque.removeBack: Deque is empty"); length--; } } struct RangeT(A) { alias T = typeof(*(A.p)); alias E = typeof(A.p.d[0]); T *p; size_t a, b; @property bool empty() const { return b <= a; } @property size_t length() const { return b-a; } @property RangeT save() { return RangeT(p, a, b); } @property RangeT!(const A) save() const { return typeof(return)(p, a, b); } alias opDollar = length; @property ref inout(E) front() inout { return (*p)[a]; } @property ref inout(E) back() inout { return (*p)[b-1]; } void popFront() { version(assert) if (empty) throw new RangeError(); a++; } void popBack() { version(assert) if (empty) throw new RangeError(); b--; } ref inout(E) opIndex(size_t i) inout { return (*p)[i]; } RangeT opSlice() { return this.save; } RangeT opSlice(size_t i, size_t j) { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } RangeT!(const A) opSlice() const { return this.save; } RangeT!(const A) opSlice(size_t i, size_t j) const { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } } alias Range = RangeT!Deque; alias ConstRange = RangeT!(const Deque); alias ImmutableRange = RangeT!(immutable Deque); Payload *p; private void I() { if (!p) p = new Payload(); } private void C() const { assert(p, "this deque is not init"); } //some value this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) {I; p = new Payload(); foreach (v; values) { insertBack(v); } } //range this(Range)(Range r) if (isInputRange!Range && isImplicitlyConvertible!(ElementType!Range, T) && !is(Range == T[])) {I; p = new Payload(); foreach (v; r) { insertBack(v); } } @property bool empty() const { return (!p || p.empty); } @property size_t length() const { return (p ? p.length : 0); } alias opDollar = length; ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; } ref inout(T) front() inout {C; return (*p)[0]; } ref inout(T) back() inout {C; return (*p)[$-1]; } void insertFront(T v) {I; p.insertFront(v); } void insertBack(T v) {I; p.insertBack(v); } void removeFront() {C; p.removeFront(); } void removeBack() {C; p.removeBack(); } Range opSlice() {I; return Range(p, 0, length); } } unittest { import std.algorithm : equal; import std.range.primitives : isRandomAccessRange; import std.container.util : make; auto q = make!(Deque!int); assert(isRandomAccessRange!(typeof(q[]))); //insert,remove assert(equal(q[], new int[](0))); q.insertBack(1); assert(equal(q[], [1])); q.insertBack(2); assert(equal(q[], [1, 2])); q.insertFront(3); assert(equal(q[], [3, 1, 2]) && q.front == 3); q.removeFront; assert(equal(q[], [1, 2]) && q.length == 2); q.insertBack(4); assert(equal(q[], [1, 2, 4]) && q.front == 1 && q.back == 4 && q[$-1] == 4); q.insertFront(5); assert(equal(q[], [5, 1, 2, 4])); //range assert(equal(q[][1..3], [1, 2])); assert(equal(q[][][][], q[])); //const range const auto rng = q[]; assert(rng.front == 5 && rng.back == 4); //reference type auto q2 = q; q2.insertBack(6); q2.insertFront(7); assert(equal(q[], q2[]) && q.length == q2.length); //construct with make auto a = make!(Deque!int)(1, 2, 3); auto b = make!(Deque!int)([1, 2, 3]); assert(equal(a[], b[])); } unittest { Deque!int a; Deque!int b; a.insertFront(2); assert(b.length == 0); } unittest { import std.algorithm : equal; import std.range : iota; Deque!int a; foreach (i; 0..100) { a.insertBack(i); } assert(equal(a[], iota(100))); } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File, stdin; 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 = stdin) { this.f = f; } string[] buf; private bool succ() { while (!buf.length) { if (f.eof) return false; buf = f.readln.split; } return true; } private bool readSingle(T)(ref T x) { if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { //string or char[10] etc x = buf.front; buf.popFront; } else { static if (isStaticArray!T) { //static assert(buf.length == T.length); } x = buf.map!(to!E).array; buf.length = 0; } } else { x = buf.front.to!T; buf.popFront; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); fout.writeln("1 2 3"); fout.writeln("ab cde"); fout.writeln("1.0 1.0 2.0"); fout.close; Scanner sc = new Scanner(File(fileName, "r")); int a; int[2] b; char[2] c; string d; double e; double[] f; sc.read(a, b, c, d, e, f); assert(a == 1); assert(equal(b[], [2, 3])); assert(equal(c[], "ab")); assert(equal(d, "cde")); assert(e == 1.0); assert(equal(f, [1.0, 2.0])); } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/algorithm.d */ // module dcomp.algorithm; //[0,0,0,...,1,1,1]で、初めて1となる場所を探す。pred(l) == 0, pred(r) == 1と仮定 T binSearch(alias pred, T)(T l, T r) { while (r-l > 1) { T md = (l+r)/2; if (!pred(md)) l = md; else r = md; } return r; } import std.range.primitives; Rotator!Range rotator(Range)(Range r) if (isForwardRange!Range && hasLength!Range) { return typeof(return)(r); } struct Rotator(Range) if (isForwardRange!Range && hasLength!Range) { size_t cnt; Range start, now; this(Range r) { cnt = 0; start = r.save; now = r.save; } this(this) { start = start.save; now = now.save; } @property bool empty() { return now.empty; } @property auto front() { assert(!now.empty); import std.range : take, chain; return chain(now, start.take(cnt)); } @property Rotator!Range save() { return this; } void popFront() { cnt++; now.popFront; } } E minimum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, E) == void)) { import std.functional; while (!range.empty) { if (binaryFun!pred(range.front, seed)) { seed = range.front; } range.popFront; } return seed; } ElementType!Range minimum(alias pred = "a < b", Range)(Range range) if (isInputRange!Range && !isInfinite!Range) { assert(!range.empty, "range must not empty"); auto e = range.front; range.popFront; return minimum!pred(range, e); } unittest { assert(minimum([2, 1, 3]) == 1); assert(minimum!"a > b"([2, 1, 3]) == 3); assert(minimum([2, 1, 3], -1) == -1); assert(minimum!"a > b"([2, 1, 3], 100) == 100); } bool[ElementType!Range] toMap(Range)(Range r) { import std.algorithm : each; bool[ElementType!Range] res; r.each!(a => res[a] = true); return res; }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp.to!int; auto a = readln.chomp.split.map!(to!int).array; auto b = new int[](n); auto j = 0; if (n % 2) { foreach_reverse (i; iota(0, n, 2)) { b[j] = a[i]; j++; } foreach (i; iota(1, n, 2)) { b[j] = a[i]; j++; } } else { foreach_reverse (i; iota(1, n, 2)) { b[j] = a[i]; j++; } foreach (i; iota(0, n, 2)) { b[j] = a[i]; j++; } } foreach (i, e; b) { write(e); if (i != b.length-1) write(" "); } writeln(""); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } 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 X = RD; long ans; (){ foreach_reverse (i; 1..X+1) { foreach_reverse (j; 1..i+1) { if (j*j > i) continue; foreach (k; 2..i+2) { if (j^^k == i) { ans = i; return; } if (j^^k > i) break; } } }}(); writeln(ans); 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; 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 d, int n) { if (d == 0) { if (n == 100) return 101; return n; } int x = 100 ^^ d; if (d == 1 && n == 100) return x * n + 100; if (d == 2 && n == 100) return x * n + 10000; return x * n; } void main() { auto dn = readints; int d = dn[0], n = dn[1]; auto ans = calc(d, n); writeln(ans); }
D
void main() { auto b = rs; switch(b) { case "A": writeln("T"); break; case "T": writeln("A"); break; case "G": writeln("C"); break; case "C": writeln("G"); break; default: break; } } // =================================== 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.range, std.array, std.conv, std.string; void main() { int[][] C; foreach (i; 0..3) { C ~= readln.split.to!(int[]); } writeln( C[0][1] - C[0][0] == C[1][1] - C[1][0] && C[0][1] - C[0][0] == C[2][1] - C[2][0] && C[0][2] - C[0][0] == C[1][2] - C[1][0] && C[0][2] - C[0][0] == C[2][2] - C[2][0] ? "Yes" : "No"); }
D
import std.stdio, std.string, std.array, std.conv; int arrayGCD(int[] x) { while (x.length > 1) { int a = x[0] < x[1] ? x[1] : x[0]; int b = x[0] < x[1] ? x[0] : x[1]; int r; while (b > 0) { r = a % b; a = b; b = r; } x[1] = a; x = x[1..$]; } return x[0]; } void main() { int[] a = readln.chomp.split.to!(int[]); arrayGCD(a).writeln; }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { int N, z, w; scan(N, z, w); auto a = readln.split.to!(int[]); if (N == 1) { writeln(abs(a.back - w)); return; } auto ans = max(abs(a.back - w), abs(a.back - a[$ - 2])); 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; 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; T gcd(T)(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } int calc(int[] a) { int n = cast(int)a.length; // lt[i] = A_0, A_1, ..., A_{i-1} の最大公約数 auto lt = new int[n + 1]; // rt[i] = A_i, A_{i+1}, ..., A_{N-1} の最大公約数 auto rt = new int[n + 1]; for (int i = 0; i < n; i++) lt[i + 1] = gcd(lt[i], a[i]); for (int i = n - 1; i >= 0; i--) rt[i] = gcd(rt[i + 1], a[i]); int ans = 0; for (int i = 0; i < n; i++) { ans = max(ans, gcd(lt[i], rt[i + 1])); } return ans; } void main() { readint; auto xs = readints; writeln(calc(xs)); }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; immutable mod = 10^^9 + 7; void main() { int h, w; scan(h, w); auto map = new char[][](h + 2, w + 2); iota(h).each!(i => map[i + 1][1 .. w + 1] = readln.chomp.to!(char[])); auto dx = [1, 0, -1, 0, 1, 1, -1, -1], dy = [0, 1, 0, -1, 1, -1, 1, -1]; foreach (i ; 1 .. h + 1) { foreach (j ; 1 .. w + 1) { if (map[i][j] == '.') { int cnt; foreach (k ; 0 .. 8) { cnt += (map[i + dx[k]][j + dy[k]] == '#'); } map[i][j] = ('0' + cnt).to!char; } } } iota(h).each!(i => writeln(map[i + 1][1 .. w + 1])); } 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 core.bitop; import core.checkedint; import core.simd; import core.stdc.stdlib; import core.stdc.string; import std.algorithm; import std.array; import std.ascii; import std.bigint; import std.bitmanip; import std.complex; import std.container; import std.conv; import std.datetime; import std.format; import std.functional; import std.math; import std.meta; import std.numeric; import std.random; import std.range; import std.regex; import std.stdio; import std.string; import std.typecons; import std.variant; __gshared: auto getAddrTuple() { return tuple(); } auto getAddrTuple(T, U...)(ref T head, ref U tail) { return tuple(&head, getAddrTuple(tail).expand); } bool read(T...)(ref T vars) if (vars.length > 0) { return readf(' ' ~ replicate("%s ", vars.length), getAddrTuple(vars).expand) == vars.length; } T[ ] allocate(T)(size_t n) { return (cast(T*)malloc(n * T.sizeof))[0 .. n]; } auto pairwise(R)(R range) if (isForwardRange!R) { return lockstep(range, dropOne(range)); } char[100_001] _a, _b; bool[100_000] used; void main() { while (true) { version (LocalProject) memset(used.ptr, 0x00, used.sizeof); auto a = _a[ ]; auto b = _b[ ]; readln(a); if (a.empty) break; readln(b); a = a[0 .. $ - 1]; b = b[0 .. $ - 1]; const n = cast(int)a.length; const m = cast(int)b.length; //b[0 .. lb] ~ b[rb .. m] int la = 0, lb = 0, ra = n, rb = m; while (la < n && lb < m) { while (la < n && a[la] != b[lb]) la++; if (la == n) break; used[la] = true; debug writeln("Left-using ", la); la++; lb++; } int lres = lb, rres = m; while (ra && rb) { ra--; rb--; int passed = 0; while (ra >= 0 && a[ra] != b[rb]) if (used[ra--]) passed++; if (ra < 0) break; if (used[ra]) passed++; debug writeln("Right-using ", ra); while (passed-- > 0 || lb > rb) { lb--; if (lb) { while (a[--la] != b[lb]) { } do used[la--] = false; while (a[la] != b[lb - 1]); la++; } else la = 0; } if (lb + m - rb > lres + m - rres) { lres = lb; rres = rb; } } if (!lres && rres == m) writeln('-'); else writeln(b[0 .. lres], b[rres .. m]); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; auto qs = new int[](N); foreach (_; 0..M) { auto ab = readln.split.to!(int[]); ++qs[ab[0]-1]; ++qs[ab[1]-1]; } foreach (q; qs) { if (q%2 == 1) { writeln("NO"); return; } } writeln("YES"); }
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) { 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; } enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { long x; scan(x); for (long i = 1; ; i++) { if (i * (i + 1) / 2 >= x) { writeln(i); return; } } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.array; void main(){ auto s=readln.chomp; if(s=="abc"||s=="acb"||s=="bac"||s=="bca"||s=="cab"||s=="cba")writeln("Yes"); else writeln("No"); }
D
import std.algorithm; import std.conv; import std.stdio; import std.string; void main(){ foreach(string s; stdin.lines) writeln(s.chomp.split.map!(to!int).reduce!"a + b"); }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random; import std.typecons, std.functional, std.traits,std.concurrency; import std.algorithm, std.container; import core.bitop, core.time, core.memory; import std.bitmanip; import std.regex; enum INF = long.max/3; enum MOD = 10L^^9+7; //辞書順順列はiota(1,N),nextPermituionを使う void main() { auto N = scanElem; writeln(24-N+24); } void end(T)(T v) if(isIntegral!T||isSomeString!T||isSomeChar!T) { import core.stdc.stdlib; writeln(v); exit(0); } T[] scanArray(T = long)() { static char[] scanBuf; readln(scanBuf); return scanBuf.split.to!(T[]); } dchar scanChar() { int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } return cast(dchar)c; } T scanElem(T = long)() { import core.stdc.stdlib; static auto scanBuf = appender!(char[])([]); scanBuf.clear; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { scanBuf ~= cast(char) c; c = getchar; } return scanBuf.data.to!T; } dchar[] scanString(){ return scanElem!(dchar[]); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.math; void main() { int input; immutable limitList = 200000; bool[] listNumbers = new bool[](limitList); int[] listPrimeNumbers; listNumbers.fill(true); foreach (i; 2..limitList.to!double.sqrt.to!int) { if (listNumbers[i]) { for (int j = i*2; j < limitList; j += i) listNumbers[j] = false; } } foreach (i; 2..listNumbers.length) { if (listNumbers[i]) { listPrimeNumbers ~= i.to!int; } } while ((input = readln.chomp.to!int) != 0) { ulong sum = 0; foreach (i; 0..listPrimeNumbers.length) { if (i == input) break; sum += listPrimeNumbers[i]; } writeln(sum); } }
D
import std.stdio; import std.string; import std.array; import std.range; import std.random; import std.algorithm; import std.conv; void main(){ auto nk = readln().chomp().split().map!(to!int).array(); auto as = readln().chomp().split().map!(to!int).array(); auto k = nk[1]; auto n = nk[0]; solve(n, k).writeln(); } int solve(int n, int k){ if((n - 1) % (k - 1) == 0){ return (n - 1) / (k - 1); }else{ return (n - 1) / (k - 1) + 1; } }
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 readInt() { return readInts()[0]; } auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } int rScore(int[] r) { int ans; int tmp; foreach(i; 0..r.length) { if(r[i] == 1) { ++tmp; } else { ans = max(ans, tmp); tmp = 0; } } return max(tmp, ans); } void main() { auto l = readInts(); auto n = l[0]; auto m = l[1]; auto q = l[2]; int[][] b; foreach(i; 0..n) { b ~= readInts(); } int[] rs; foreach(i; 0..n) { rs ~= rScore(b[i]); } foreach(k; 0..q) { auto ij = readInts(); auto i = ij[0]-1; auto j = ij[1]-1; b[i][j] ^= 1; rs[i] = rScore(b[i]); writeln(reduce!(max)(rs)); } }
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 oder atc ---+/ void main(string[] args) { const S = readln.chomp.split.array; (S[1] ~ S[0]).writeln; }
D
import std.stdio, std.conv, std.array, std.range, std.algorithm, std.string, std.math; void main(){ auto n = readln.chomp.to!long; auto as = [0L] ~ readln.chomp.split.to!(long[]) ~ [0L]; long[] ans = []; long s = 0; foreach(i; 0 .. n + 1) s += abs(as[i] - as[i + 1]); foreach(i; 1 .. n + 1){ auto d = abs(as[i] - as[i - 1]) + abs(as[i] - as[i + 1]) - abs(as[i - 1] - as[i + 1]); ans ~= s - d; } foreach(a; ans) a.writeln; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = readln.chomp.to!size_t; auto si = readln.split.to!(int[]); auto q = readln.chomp.to!size_t; auto ti = readln.split.to!(int[]); writeln(ti.count!(t => si.assumeSorted.contains(t))); }
D
//dlang template---{{{ import std.stdio; import std.conv; import std.string; import std.array; import std.algorithm; // MIT-License https://github.com/kurokoji/nephele class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } //Digit count---{{{ int DigitNum(int num) { int digit = 0; while (num != 0) { num /= 10; digit++; } return digit; } //}}} //}}} void main() { Scanner sc = new Scanner; int N, T; sc.scan(N, T); int[] c = new int[N]; int[] t = new int[N]; foreach (i; 0 .. N) sc.scan(c[i], t[i]); int minCost = c[0]; int minT = t[0]; foreach (i; 1 .. N) { if (t[i] <= T) { if (minCost > c[i]) { minCost = c[i]; minT = t[i]; } } } if (minT > T) writeln("TLE"); else writeln(minCost); }
D
import std.stdio; import std.conv; import std.algorithm; import std.range; import std.string; import std.typecons; import std.math; import std.random; import std.range; import std.functional; import std.container; struct Puzzle { int[9] panel; size_t toHash() const @safe pure nothrow { size_t hash = 0; foreach (i; panel) { hash *= 10; hash += i; } return hash; } bool opEquals(ref const Puzzle s) const @safe pure nothrow { return equal(panel[0..$], s.panel[0..$]); } int c; } int[] dx = [-1, 0, 1, 0]; int[] dy = [0, -1, 0, 1]; void main() { bool[Puzzle] set; auto queue = DList!Puzzle(); Puzzle initial; foreach (i; 0..9) { scanf("%d", &initial.panel[i]); } set[initial] = true; queue.insertBack(initial); while (!queue.empty) { auto u = queue.front; queue.removeFront; if (u.panel[0..$].equal([1, 2, 3, 4, 5, 6, 7, 8, 0])) { writeln(u.c); break; } auto sp = u.panel[0..$].countUntil(0); auto sx = sp % 3; auto sy = sp / 3; foreach (r; 0..4) { auto tx = sx + dx[r]; auto ty = sy + dy[r]; if (tx < 0 || tx >= 3 || ty < 0 || ty >= 3) continue; auto temp = u; temp.panel[sp].swap(temp.panel[tx + ty * 3]); if (temp !in set) { set[temp] = true; temp.c++; queue.insertBack(temp); } } } }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop; // dfmt on void main() { long N = lread(); auto A = aryread!ulong(); auto add = new ulong[](N + 1); auto xor = new ulong[](N + 1); foreach (i; 0 .. N) { add[i + 1] = add[i] + A[i]; xor[i + 1] = xor[i] ^ A[i]; } long ans; long j; foreach (i; 0 .. N) { while (j + 1 < N && (add[j + 2] - add[i]) == (xor[j + 2] ^ xor[i])) j++; ans += j - i + 1; } writeln(ans); }
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() { int n; readV(n); int[] a; readA(n, a); int[int] h; foreach (ai; a) ++h[ai]; int r = 0; foreach (k, v; h) if (v >= k) r += v-k; else r += v; writeln(r); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int N, K; scan(N, K); auto a = readln.split.to!(int[]); auto dp = new bool[](K + 1); foreach (i ; 1 .. K + 1) { foreach (j ; 0 .. N) { if (i - a[j] >= 0 && !dp[i - a[j]]) { dp[i] = true; } } } writeln(dp[K] ? "First" : "Second"); } 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() { writeln(2 ^^ readln.chomp.to!int.log2.to!int); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio; import std.string; import std.conv; int main() { int n = readln().chomp().to!int(); string s = "Hoshino"; while (n--) { string str = readln().chomp(); char[] ans; for (int i = 0; i < str.length; i++) { ans ~= str[i]; if (str[i] == 'H') { bool flag = true; if (i + 6 > str.length - 1) flag = false; for (int j = 0; j < 7 && flag; j++) { if (str[i + j] != s[j]) flag = false; } if (flag) { i += 6; ans ~= str[i-5..i]; ans ~= 'a'; } } } writeln(ans); } 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.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto n = RD!int; auto edges = new long[][](n); foreach (i; 0..n-1) { auto u = RD!int-1; auto v = RD!int-1; edges[u] ~= v; edges[v] ~= u; } debug writeln(edges); bool ans = true; foreach (i; 0..n) { if (edges[i].length == 2) { ans = false; break; } } writeln(ans ? "YES" : "NO"); stdout.flush(); debug readln(); }
D
import std.stdio, std.string, std.conv, std.array, std.algorithm; ulong[] getArray() { auto ary = appender( new ulong[]( 0 ) ); foreach ( n; 1 .. 1000 ) { immutable ulong val = n * ( n + 1 ) * ( n + 2 ) / 6; if ( val >= 10 ^^ 6 ) { break; } else { ary.put( val ); } } return ary.data; } ulong[] getOddArray() { immutable auto s = getArray().length; auto ary = appender( new ulong[]( 0 ) ); foreach ( n; 1 .. s + 1 ) { immutable auto val = n * ( n + 1 ) * ( n + 2 ) / 6; if ( val % 2 == 1 ) { ary.put( val ); } } return ary.data; } void main() { const ulong[] array = getArray(); const ulong[] oddArray = getOddArray(); ulong[] answer = new ulong[]( 100_0001 ); answer[] = 100_0000; answer[ 0 ] = 0; ulong[] oddAnswer = new ulong[]( 100_0001 ); oddAnswer[] = 100_0000; oddAnswer[ 0 ] = 0; foreach ( i; 0 .. 100_0001 ) { foreach ( v; array ) { if ( i + v <= 100_0000 ) { answer[ i + v ] = min( answer[ i + v ], answer[ i ] + 1 ); } } foreach ( v; oddArray ) { if ( i + v <= 100_0000 ) { oddAnswer[ i + v ] = min( oddAnswer[ i + v ], oddAnswer[ i ] + 1 ); } } } while ( true ) { immutable int n = to!int( strip( readln ) ); if ( n == 0 ) break; writeln( answer[ n ], " ", oddAnswer[ n ] ); } }
D
import std.stdio, std.string, std.conv, std.bigint, std.typecons, std.algorithm, std.array, std.math, std.range, std.functional; void main() { auto N = readln.chomp.to!long; auto x = readln.split.to!(long[]); long cnt = 0; foreach (i; 0..N) { if (x[i] == i+1) { cnt++; swap(x[i], x[i+1]); } } writeln(cnt); }
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;}} T[] readArray(T)(size_t n){auto a=new T[](n),r=readln.splitter;foreach(ref v;a){v=r.front.to!T;r.popFront;}return a;} T[] readArrayM(T)(size_t n){auto a=new T[](n);foreach(ref v;a)v=readln.chomp.to!T;return a;} alias graph = GraphW!(int, long, 10L ^^ 18); void main() { int n, m; readV(n, m); auto g = graph(n); foreach (_; 0..m){ int a, b; long c; readV(a, b, c); --a; --b; g.addEdge(a, b, -c); } auto dist = BellmanFord!(typeof(g)).bellmanFord(g, 0), d = dist[n-1]; if (d <= -graph.inf) writeln("inf"); else writeln(-d); } struct GraphW(N = int, W = int, W i = 10^^9) { import std.typecons; alias Node = N, Wt = W, inf = i; struct Edge { Node src, dst; Wt wt; } Node n; Edge[][] g; mixin Proxy!g; this(Node n) { this.n = n; g = new Edge[][](n); } void addEdge(Node u, Node v, Wt w) { g[u] ~= Edge(u, v, w); } void addEdgeB(Node u, Node v, Wt w) { g[u] ~= Edge(u, v, w); g[v] ~= Edge(v, u, w); } } template BellmanFord(Graph) { import std.traits; alias Node = TemplateArgsOf!Graph[0], Wt = TemplateArgsOf!Graph[1]; void bellmanFord(Graph g, Node s, out Wt[] dist, out Node[] prev) { auto n = g.n, sent = n; dist = new Wt[](n); dist[] = g.inf + g.inf; dist[s] = 0; prev = new Node[](n); prev[] = sent; foreach (k; 0..n) foreach (i; 0..n) foreach (e; g[i]) if (dist[e.dst] > dist[e.src] + e.wt) { dist[e.dst] = dist[e.src] + e.wt; prev[e.dst] = e.src; if (k == n-1) dist[e.dst] = -g.inf; } } Wt[] bellmanFord(Graph g, Node s) { Wt[] dist; Node[] prev; bellmanFord(g, s, dist, prev); return dist; } }
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() { int n = readln.chomp.to!int; ["Mahmoud", "Ehab"][n % 2].writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; long P = 10^^9+7; int[2][] prime_division(int m) { int[2][] r; for (int x = 2; x^^2 <= m; ++x) { int cnt; while (m % x == 0) { ++cnt; m /= x; } if (cnt) r ~= [x, cnt]; } if (m != 1) r ~= [m, 1]; return r; } void main() { auto nm = readln.split.to!(int[]); long N = nm[0]; auto M = nm[1]; long[][] MEMO; MEMO.length = 10^^5+1; foreach_reverse (i, ref memo; MEMO[0..N+1]) { memo.length = 41; foreach (j, ref x; memo) { if (j == 0) { x = 1; } else if (i == N) { x = 0; } else { x = (MEMO[i+1][j] + MEMO[i][j-1]) % P; } } } long r = 1; foreach (p; prime_division(M)) { r = (r * MEMO[0][p[1]]) % P; } writeln(r); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; void main(){ auto z=readln.split.to!(int[]),a=z[0],b=z[1]; if(a%3==0||b%3==0||(a+b)%3==0)writeln("Possible"); else writeln("Impossible"); }
D
import std.stdio; import std.string; import std.math; import std.conv; import std.algorithm; import std.bigint; void main(){ int d,m; int date = 2; auto ds = ["Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday","Monday"]; auto a = [31,29,31,30,31,30,31,31,30,31,30,31]; while(true){ date = 2; auto s = split(readln()); d = to!int(s[0]); m = to!int(s[1]); if(d == 0 && m == 0) break; for(int i=0;i<d-1;i++){ date += a[i]; } date += m-1; date = date % 7; writeln(ds[date]); } }
D
//prewritten code: https://github.com/antma/algo import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.traits; final class InputReader { private: ubyte[] p, buffer; bool eof; bool rawRead () { if (eof) { return false; } p = stdin.rawRead (buffer); if (p.empty) { eof = true; return false; } return true; } ubyte nextByte(bool check) () { static if (check) { if (p.empty) { if (!rawRead ()) { return 0; } } } auto r = p.front; p.popFront (); return r; } public: this () { buffer = uninitializedArray!(ubyte[])(16<<20); } bool seekByte (in ubyte lo) { while (true) { p = p.find! (c => c >= lo); if (!p.empty) { return false; } if (!rawRead ()) { return true; } } } template next(T) if (isSigned!T) { T next () { if (seekByte (45)) { return 0; } T res; ubyte b = nextByte!false (); if (b == 45) { while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 - (b - 48); } } else { res = b - 48; while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 + (b - 48); } } } } template next(T) if (isUnsigned!T) { T next () { if (seekByte (48)) { return 0; } T res = nextByte!false () - 48; while (true) { ubyte b = nextByte!true (); if (b < 48 || b >= 58) { break; } res = res * 10 + (b - 48); } return res; } } T[] nextA(T) (in int n) { auto a = uninitializedArray!(T[]) (n); foreach (i; 0 .. n) { a[i] = next!T; } return a; } } void main() { auto r = new InputReader (); immutable nt = r.next!uint (); foreach (tid; 0 .. nt) { auto n = r.next!uint; writeln (n); } }
D
import std.stdio, std.string, std.conv, std.range, std.array, std.algorithm; import std.uni, std.math, std.container, std.typecons, std.typetuple; import core.bitop, std.datetime; immutable long mod = 10^^9 + 7; void main(){ auto n = readln.chomp.to!int; auto s = readln.chomp; int stack; int lb, rb; foreach(ch ; s){ if (ch == '('){ stack += 1; } else { if (stack == 0){ lb += 1; } else { stack--; } } } rb = stack; auto ans = repeat("(", lb).join ~ s ~ repeat(")", rb).join; writeln(ans); } void readVars(T...)(auto ref T args){ auto line = readln.split; foreach(ref arg ; args){ arg = line.front.to!(typeof(arg)); line.popFront; } if(!line.empty){ throw new Exception("args num < input num"); } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; void main() { auto nte = readln.chomp.split.map!(to!int); auto lo = nte[1] - nte[2]; auto up = nte[1] + nte[2]; auto san = readln.chomp.split.map!(to!int).array; auto res = -1; foreach (i, e; san) { auto tmp = e; while (tmp < lo) tmp += e; if (tmp <= up) { res = i.to!int + 1; break; } } res.writeln; }
D
void main() { int[] tmp = readln.split.to!(int[]); int hw = tmp[0], ww = tmp[1]; tmp = readln.split.to!(int[]); int hb = tmp[0], wb = tmp[1]; writeln((hw - hb) * (ww - wb)); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.container; import std.typecons;
D
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); long[] add = [0, 0, 0, 4, -4, 4]; auto list = new long[](2*(10^^6)); list[2] = 4; list[3] = 4; list[4] = 12; foreach (i; 5..list.length) { auto x = add[(i-5)%6]; list[i] = list[i-1]; list[i].modm(2); list[i].moda(x); } foreach (ti; 0..t) { auto n = RD!int-1; ans[ti] = list[n]; /*int[][] edges; edges.length = 1; foreach (i; 0..n-1) { foreach (u; 0..edges.length) { if (edges[u].length == 3) continue; if (edges[u].length == 0) { ++edges.length; edges[u] ~= cast(int)edges.length - 1; } else if (edges[u].length == 1) { ++edges.length; ++edges.length; edges[u] ~= cast(int)edges.length - 2; edges[u] ~= cast(int)edges.length - 1; } } } int[] dfs(int u, int par) { int res; bool used; foreach (v; edges[u]) { if (v == par) continue; auto r = dfs(v, u); if (r[1]) used = true; res += r[0]; } if (!used && edges[u].length == 3) { ++res; used = true; } else { used = false; } return [res, used ? 1 : 0]; } writeln(dfs(0, -1));*/ } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
void main() { auto S = rs; ulong res; ulong tmp; foreach(i; S) { if(i == 'A' || i == 'T' || i == 'C' || i == 'G') { tmp++; } else { res = max(res, tmp); tmp = 0; } } res = max(res, tmp); 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
import std.stdio; import std.array; import std.conv; import std.string; import std.algorithm; void main() { string[string] cards; auto n = readln.strip.to!int; foreach(i;0..n) { auto s = readln; cards[s] = s; } foreach(i;["S","H","C","D"]) { foreach(j;0..13){ string str = i ~ " " ~ (j+1).to!string ~ "\n"; if(cards.get(str,"")=="") { write(str); } } } }
D
import std; void main() { int k; scan(k); string s; scan(s); if (s.length > k) writeln(s[0..k], "..."); else writeln(s); } void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T=string)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readints = reads!int;
D
void main() { long[] tmp = rdRow; long a = tmp[0], b = tmp[1], k = tmp[2]; long c = min(a, k); a -= c; long d = min(b, k - c); b -= d; writeln(a, " ", b); } 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 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) { 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() { long X, Y; get(X, Y); writeln(abs(X - Y) <= 1 ? "Brown" : "Alice"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; enum P = 10L^^9+7; void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; auto AS = readln.split.to!(int[]); auto BS = readln.split.to!(int[]); auto CS = new int[](N*M+1); foreach (a; AS) { if (CS[a] == 1) { writeln(0); return; } CS[a] = 1; } foreach (b; BS) { if (CS[b] >= 2) { writeln(0); return; } CS[b] += 2; } auto DS = new long[](N*M+1); auto ES = new long[](N*M+1); foreach_reverse (i; 1..N*M+1) { DS[i-1] += DS[i] + (CS[i]&1 ? 1 : 0); ES[i-1] += ES[i] + (CS[i]&2 ? 1 : 0); } long r = 1; foreach_reverse (x; 1..N*M+1) { switch (CS[x]) { case 2: r = r * DS[x] % P; break; case 1: r = r * ES[x] % P; break; case 0: r = r * (DS[x] * ES[x] - (N*M-x)) % P; break; default: } } writeln(max(0, r)); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD!string; writeln(N[0] == N[2] ? "Yes" : "No"); stdout.flush(); debug readln(); }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { while (true) { string s; scan(s); if (s == "#") return; solve(s); } } void solve(string s) { auto h = "qwertasdfgzxcvb"; int res; foreach (int i, ch; s) { if (i == 0) continue; res += h.canFind(ch) ^ h.canFind(s[i-1]); } writeln(res); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.math; void main() { int[3] n; n = numText2IntArray(readln()); if((n[0] + n[1]) == n[2] || (n[0] + n[2]) == n[1] || (n[1] + n[2]) == n[0]) { writeln("Yes"); } else { writeln("No"); } } int[] numText2IntArray(string text) { return map!(to!int)(split(text)).array; }
D
import std.stdio; import std.conv; import std.algorithm; import std.string; import std.array; void main() { int n = readln.chomp.to!int; string[] s = new string[](n); char[5] initial = ['M', 'A', 'R', 'C', 'H']; long[5] num; for (int i = 0; i < n; ++i) { s[i] = readln.chomp; for (int j = 0; j < 5; ++j) { if (s[i][0] == initial[j]) { num[j]++; break; } } } long ans; for (int i = 0; i < 5; ++i) { for (int j = i + 1; j < 5; ++j) { for (int k = j + 1; k < 5; ++k) { ans += (num[i] * num[j] * num[k]); } } } writeln(ans); return; }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; void main() { auto N = readln.chomp.to!int; int res; foreach(i; iota(1,N+1,2)) { int count; foreach(k; 1..i+1) { if(i % k == 0) count++; } if(count == 8) res++; } res.writeln; } //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() { long k,a,b; scan(k, a, b); if (a >= k) { writeln(1); return; } if (a <= b) { writeln(-1); } else { long s = a - b; long ans = (k - a + s - 1) / s; ans = 2*ans + 1; 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
void main() { int[] tmp = readln.split.to!(int[]); int a = tmp[0], b = tmp[1]; writeln(b % a == 0 ? a + b : b - a); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.container; import std.typecons;
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; const MOD = 10^^9 + 7L; void add(T)(ref T a, T b) { a = (a + b) % MOD; } long calc(string s, int d) { int n = cast(int) s.length; auto dp = new int[][][](n + 1, 2, d); // dp[i][j][k] // i: 上から i 桁目まで // j: n 未満か // k: mod d dp[0][0][0] = 1; foreach (i; 0..n) { foreach (j; 0..2) { foreach (k; 0..d) { int lim = j == 1 ? 9 : s[i] - '0'; foreach (m; 0..lim+1) { add(dp[i + 1][j || m < lim][(k + m) % d], dp[i][j][k]); } } } } // -1 は 0 の分 return (-1L + MOD + dp[n][0][0] + dp[n][1][0]) % MOD; } void main() { string s = read!string; int d = readint; writeln(calc(s, d)); }
D
import std.stdio,std.string,std.conv,std.math; int main() { int[4] a,b; int count,count_2; string s,s2; while((s = readln.chomp).length != 0 ) { s2 = readln.chomp; string[] input = split(s); string[] input2 = split(s2); foreach(i;0..4) { a[i] = input[i].to!int; b[i] = input2[i].to!int; } count = 0; count_2 = 0; foreach(i;0..4) { foreach(j;0..4) { if(a[i] == b[j]) { if(i == j) count++; else count_2++; } } } writeln(count," ",count_2); } return 0; }
D
import std; // 累積和による解法 long calc(string s) { int n = cast(int)s.length; auto acc = new int[][](3, n); foreach (i; 0..n) { acc[0][i] = (s[i] == 'R') + (i > 0 ? acc[0][i-1] : 0); acc[1][i] = (s[i] == 'G') + (i > 0 ? acc[1][i-1] : 0); acc[2][i] = (s[i] == 'B') + (i > 0 ? acc[2][i-1] : 0); } // "RGB" の使っていない文字 int get(char a, char b) { foreach (i, c; "RGB") { if (c != a && c != b) return cast(int)i; } assert(false); } long ans = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n - 1; j++) { if (s[i] == s[j]) continue; int p = get(s[i], s[j]); ans += acc[p][$-1] - acc[p][j-1]; int k = 2*j - i; if (k < n && s[k] == "RGB"[p]) ans--; } } return ans; } // 全体から j-i == k-j が一致するものを引く long calc2(string s) { int n = cast(int)s.length; long r = 0, g = 0, b = 0; foreach (c; s) { if (c == 'R') r++; if (c == 'G') g++; if (c == 'B') b++; } int x = 0; foreach (i; 0..n) { foreach (j; i+1..n) { if (s[i] == s[j]) continue; int k = 2*j - i; if (k >= n || s[i] == s[k] || s[j] == s[k]) continue; x++; // j-i == k-j and i, j, k の文字が全て異なる } } return r * g * b - x; } void main() { readint; string s = read!string; writeln(calc2(s)); } 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;
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, "x", long, "y"); alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less); void main() { long k, t; scan(k, t); auto a = aryread(); auto maxA = a.reduce!(max); writeln(max(maxA - (k - maxA + 1), 0)); } 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.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; void main() { while(true) { int n = readln.chomp.to!int; if (n==0) break; int[] h = readln.split.to!(int[]); foreach(i; 0..h.length) { int[] _h = h[0..i] ~ h[i+1..$]; int[] sa = new int[n-1]; foreach(j, ref e; sa) { e = _h[j+1]-_h[j]; } if (sa.count!(a=>a==sa[0]) == sa.length) { writeln(h[i]); break; } } } }
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 3776 ---+/ /+---test 8080 ---+/ void main(string[] args) { const S = readln.chomp; (S.uniq.equal(S)? "Good": "Bad").writeln; }
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 = new string[](3); foreach (i; 0 .. 3) S[i] = sread(); long M = S.map!"a.length"().reduce!(max); auto match = new bool[][][](3, 3, M * 3); foreach (x; 0 .. 3) foreach (y; 0 .. 3) loop_i: foreach (i; 0 .. M * 3) { if (S[x].length <= i) { match[x][y][i] = true; continue; } foreach (j; 0 .. M * 2) if (j + i < S[x].length && j < S[y].length) if (S[x][j + i] != S[y][j] && S[x][j + i] != '?' && S[y][j] != '?') { continue loop_i; } match[x][y][i] = true; } // writeln(match[0][1]); auto p = [0, 1, 2]; bool check(long i, long j) { auto A = p[0], B = p[1], C = p[2]; if (!match[A][B][i]) return false; if (!match[A][C][j]) return false; if (!match[B][C][j - i]) return false; return true; } long ans = long.max; do { foreach (i; 0 .. S[p[0]].length + 1) foreach (j; i .. max(S[p[0]].length, i + S[p[1]].length) + 1) { long len = max(S[p[0]].length, i + S[p[1]].length, j + S[p[2]].length); if (check(i, j) && len < ans) { // writeln(i, " ", j, " ", len); ans = len; } // writeln(i, " ", j, " ", i + S[2].length); } } while (nextPermutation(p)); writeln(ans); }
D
// module dcomp.scanner; class Scanner { import std.stdio : File, readln, stdin; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeString, isDynamicArray; import std.algorithm : map; File f; this(File f = stdin) { this.f = f; } string[] buf; bool succ() { while (!buf.length) { if (f.eof) return false; buf = readln.split; } return true; } int read(Args...)(auto ref Args args) { foreach (i, ref v; args) { if (!succ()) return i; alias VT = typeof(v); static if (!isSomeString!VT && isDynamicArray!VT) { v = buf.map!(to!(ElementType!VT)).array; buf.length = 0; } else { v = buf.front.to!VT; buf.popFront(); } } return args.length; } } /+ dub.sdl: name "A" dependency "dcomp" version="~master" +/ int main() { import std.stdio, std.algorithm; // import dcomp.scanner; auto sc = new Scanner(); string s; sc.read(s); int mi = 0, sm = 0; foreach (c; s) { if (c == 'S') { sm++; } else { sm--; } mi = min(mi, sm); } writeln(mi * -2); 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)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new int[](t); foreach (ti; 0..t) { auto n = RD!int; ans[ti] = n / 2 + 1; } 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; import std.bigint; void main() { readln; int[int] list; foreach (line; stdin.byLine) { auto n = line.chomp.to!int; list[n] = 1 - list.get(n, 0); } int sum; foreach (k; list.keys) { sum += list[k]; } sum.writeln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 10L^^12; void main() { int n, k; scan(n, k); auto x = readln.split.to!(int[]); long ans = inf; foreach (i ; 0 .. n - k + 1) { long l = x[i].to!long; long r = x[i + k - 1].to!long; long tmp; if (r < 0) { tmp = abs(l); } else if (l < 0) { tmp = min(2*abs(l) + r, abs(l) + 2*r); } else { tmp = r; } debug { writefln("%d, %d", l, r); } ans = min(ans, tmp); } 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; import std.algorithm; import std.array; import std.conv; import std.datetime; import std.numeric; import std.math; import std.string; string my_readln() { return chomp(readln()); } void main() {//try{ auto tokens = split(my_readln()); auto N = to!ulong(tokens[0]); auto K = to!ulong(tokens[1]); long[] h; foreach (token; split(my_readln())) { h ~= to!long(token); } ulong[] costs; costs.length = N; fill(costs, ulong.max); costs[0] = 0; foreach (i; 0..N) { foreach (j; 1..K+1) { if (i + j >= N) break; costs[i+j] = min(costs[i+j], costs[i] + abs(h[i] - h[i+j])); } } writeln(costs[N-1]); stdout.flush(); /*}catch (Throwable e) { writeln(e.toString()); } 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!(char[]); foreach (i; 0..T.length) { if (T[i] == '?') T[i] = 'D'; } writeln(T); 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; 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(); foreach (state; 0 .. 1 << 3) { long tmp = S[0] - '0'; foreach (j; 0 .. 3) if (state & (1 << j)) { tmp += S[j + 1] - '0'; } else { tmp -= S[j + 1] - '0'; } if (tmp == 7) { write(S[0]); foreach (j; 0 .. 3) if (state & (1 << j)) { write('+', S[j + 1]); } else { write('-', S[j + 1]); } writeln("=7"); return; } } }
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 N = readln.split[0].to!int; auto A = readln.split.to!(int[]); auto Amin = A.reduce!"min(a,b)"; auto gcd = 1; _gcd: foreach_reverse(d; 1 .. Amin+1) { foreach (a; A) { if (a%d != 0) continue _gcd; } gcd = d; } writeln(A.sum - N*gcd); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto rd = readln.split.to!(int[]), k = rd[0], s = rd[1]; auto ans = 0; foreach (x; 0..k+1) foreach (y; 0..k+1) { auto z = s-x-y; if (0 <= z && z <= k) ++ans; } writeln(ans); }
D
import std.algorithm; import std.array; import std.ascii; import std.bigint; import std.complex; import std.container; import std.conv; import std.functional; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } void readlnTo(T...)(ref T t) { auto s = readln().split(); assert(s.length == t.length); foreach(ref ti; t) { ti = s[0].to!(typeof(ti)); s = s[1..$]; } } const real eps = 1e-10; const long p = 1_000_000_000 + 7; void main(){ auto n = readLong(); auto a = readLongs(); auto x = a[0]; auto y = a.sum - a[0]; auto ans = abs(x-y); foreach(i; iota(1, n-1)) { x += a[i]; y -= a[i]; ans = min(ans, abs(x-y)); } writeln(ans); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n, a, b; scan(n, a, b); auto x = readln.split.to!(int[]); long ans; foreach (i ; 1 .. n) { long d = x[i] - x[i - 1]; if (d * a > b) { ans += b; } else { ans += d * a; } } 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 core.bitop; import std.algorithm; import std.array; import std.ascii; import std.container; import std.conv; import std.format; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; void main() { int n = readln.chomp.to!int; int sum = 0; foreach (i; 0..n) { auto lr = readln.chomp.split.map!(to!int); sum += lr.back - lr.front + 1; } sum.writeln; }
D
void main() { int n = readln.chomp.to!int; int[string] a; foreach (i; 0 .. n) { string s = readln.chomp; if (s in a) ++a[s]; else a[s] = 1; } int m = readln.chomp.to!int; foreach (i; 0 .. m) { string t = readln.chomp; if (t in a) --a[t]; else a[t] = -1; } max(0, a.values.reduce!max).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
/* 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 10 3 1 4 1 5 9 2 6 5 3 ---+/ /+---test 10 3 14 159 2653 58979 323846 2643383 27950288 419716939 9375105820 ---+/ enum MOD = 1000000007; void main(string[] args) { const N = readln.chomp.to!long; const A = readln.split.map!(to!long).array; long ans; foreach (i; 0..60) { long x; foreach (a; A) if ((a >> i) & 1) ++x; long cnt = x * (N-x); foreach (j; 0..i) cnt = cnt*2%MOD; ans = (ans+cnt)%MOD; } ans.writeln; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; // dfmt on void main() { auto S = sread(); long ans = long.max; bool[char] d; foreach (c; S) d[c] = true; if (d.keys.length == 1) { writeln(0); return; } foreach (c; d.keys) { ans = ans.min(solve(S, c)); } writeln(ans); } long solve(string S, char c) { bool flag = true; string newS = ""; foreach (i; 0 .. S.length - 1) { if (S[i] == c || S[i + 1] == c) newS ~= c; else { newS ~= S[i]; flag = false; } } if (flag) { return 1; } else { return 1 + solve(newS, c); } }
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 np = readInts(); auto n = np[0]; auto p = np[1]; if(n == 0 && p == 0) { return; } auto stone = new int[n]; int m = 0; int wan = p; while(true) { if(wan == 1 && stone[m] == p-1) { writeln(m); break; } if(wan > 0) { ++stone[m]; --wan; } else { wan = stone[m]; stone[m] = 0; } m = (m+1)%n; } } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto ab = readln.split.to!(int[]); writeln(ab[0] & 1 && ab[1] & 1 ? "Yes" : "No"); }
D
import std.algorithm; import std.array; import std.ascii; import std.bigint; import std.complex; import std.container; import std.conv; import std.functional; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } void readlnTo(T...)(ref T t) { auto s = readln().split(); assert(s.length == t.length); foreach(ref ti; t) { ti = s[0].to!(typeof(ti)); s = s[1..$]; } } const real eps = 1e-10; void main(){ while(true) { auto n = readInt(); if(n == 0) { return; } int ans; foreach(i; iota(n)) { int x, y, h, w; readlnTo(x, y, h, w); auto l = x+y+h; if(l <= 60 && w <= 2) { ans += 600; continue; } if(l <= 80 && w <= 5) { ans += 800; continue; } if(l <= 100 && w <= 10) { ans += 1000; continue; } if(l <= 120 && w <= 15) { ans += 1200; continue; } if(l <= 140 && w <= 20) { ans += 1400; continue; } if(l <= 160 && w <= 25) { ans += 1600; continue; } } writeln(ans); } }
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, S, F; foreach (i; 0..N-1) { C ~= RD!long; S ~= RD!long; F ~= RD!long; } foreach (i; 0..N-1) { long t; foreach (j; i..N-1) { t = max(t, S[j]); t = (t / F[j] + (t % F[j] != 0 ? 1 : 0)) * F[j]; t += C[j]; } writeln(t); } writeln(0); stdout.flush(); }
D
import std.stdio, std.string, std.conv, std.array, std.algorithm; import std.uni, std.range, std.math, std.container, std.datetime; import core.bitop, std.typetuple, std.typecons; immutable long MOD = 1_000_000_007; alias tie = TypeTuple; void main(){ int a, b; char op; while(true){ readVars(a, op, b); switch(op){ case '+': writeln(a + b); break; case '-': writeln(a - b); break; case '*': writeln(a * b); break; case '/': writeln(a / b); break; default: return; } } } void readVars(T...)(auto ref T args){ auto line = readln.split; foreach(ref arg ; args){ arg = line.front.to!(typeof(arg)); line.popFront; } if(!line.empty){ throw new Exception("args num < input num"); } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long H, N; scan(H, N); auto A = aryread(); writeln(H <= A.sum() ? "Yes" : "No"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto hw = readln.split.to!(int[]); auto H = hw[0]; auto W = hw[1]; char[][] MAP; foreach (_; 0..H) MAP ~= readln.chomp.to!(char[]); auto MEMO = new int[][](H, W); foreach (ref memo; MEMO) memo[] = int.max; auto ss = [[0, 0, MAP[0][0] == '#' ? 1 : 0]]; while (!ss.empty) { auto h = ss[0]; ss = ss[1..$]; auto i = h[0]; auto j = h[1]; foreach (d; [[1,0], [0,1]]) { auto k = i+d[0]; auto l = j+d[1]; auto c = h[2]; if (k >= H || l >= W) continue; if (MAP[i][j] == '.' && MAP[k][l] == '#') ++c; if (MEMO[k][l] <= c) continue; MEMO[k][l] = c; ss ~= [k, l, c]; } } writeln(MEMO[H-1][W-1]); }
D
import std.stdio; import std.string; import std.conv; import std.array; import std.algorithm; void main() { string[] input = new string[](3); while ((input = readln.chomp.split(" ")).length != 0) { auto sum = 0; auto data = input.map!(to!int).array; data[0] %= data[1]; foreach (i; 0..data[2]) { data[0] *= 10; sum += data[0] / data[1]; data[0] %= data[1]; } writeln(sum); } }
D