code
stringlengths
4
1.01M
language
stringclasses
2 values
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() { string input = readln.chomp; switch(input) { case "1 0 0": case "0 1 0": case "0 0 0": { writeln("Close"); } break; default: { writeln("Open"); } break; } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.to!(int[]); } int calc(int[][] xs) { int n = cast(int) xs[0].length; auto dp = new int[][](2, n); dp[0][0] = xs[0][0]; for (int i = 1; i < n; i++) { dp[0][i] = xs[0][i] + dp[0][i - 1]; } dp[1][0] = xs[1][0] + dp[0][0]; for (int i = 1; i < n; i++) { dp[1][i] = xs[1][i] + max(dp[0][i], dp[1][i - 1]); } return dp[1][n - 1]; } void main() { readint; int[][] xs; xs ~= readints; xs ~= readints; int ans = calc(xs); writeln(ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto AS = new int[][](3, 3); foreach (i; 0..3) foreach (j, a; readln.split.to!(int[])) AS[i][j] = a; auto N = readln.chomp.to!int; foreach (_; 0..N) { auto b = readln.chomp.to!int; foreach (i; 0..9) if (AS[i/3][i%3] == b) AS[i/3][i%3] = -1; } foreach (i; 0..3) { bool ok = true; foreach (j; 0..3) if (AS[i][j] != -1) ok = false; if (ok) { writeln("Yes"); return; } ok = true; foreach (j; 0..3) if (AS[j][i] != -1) ok = false; if (ok) { writeln("Yes"); return; } } if ( (AS[0][0] == -1 && AS[1][1] == -1 && AS[2][2] == -1) || (AS[0][2] == -1 && AS[1][1] == -1 && AS[2][0] == -1) ) { writeln("Yes"); return; } writeln("No"); }
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(){ auto dp = new int[1001][1001]; for(int i = 1; i <= 1000; ++i) { for(int j = i; j <= 1000; ++j) { dp[i][j] = dp[i][j-1] + j; } } auto l = new int[1000001]; for(int i; i <= 1000; ++i) { for(int j; j <= 1000; ++j) { ++l[dp[i][j]]; } } while(true) { auto N = readInt(); if(N == 0) { return; } writeln(l[N]-1); } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.numeric; void main() { auto x = readln.chomp.to!long; auto t = x % 11; auto res = (x / 11) * 2; if (t > 6) { res += 2; } else if (t > 0) { res += 1; } res.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, std.random, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int N; string S; scan(N); scan(S); int ans; foreach (l ; 0 .. N) { auto z = Zalg(S[l .. $]); debug { writeln(z); } foreach (j ; 0 .. N - l) { ans = max(ans, min(j, z[j])); } } writeln(ans); } int[] Zalg(string S) { int N = S.length.to!int; auto z = new int[](N); z[0] = N; for (int i = 1, j = 0; i < N; ) { while (i + j < N && S[j] == S[i + j]) j++; z[i] = j; if (j == 0) { i++; continue; } int k = 1; while (i + k < N && k + z[k] < j) { z[i + k] = z[k]; k++; } i += k; j -= k; } return z; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { auto S = sread(); auto T = sread(); foreach_reverse (i; 0 .. S.length - T.length + 1) { bool b = true; // writeln(S[i .. i + T.length]); foreach (j, c; S[i .. i + T.length]) { // writeln(c, T[j]); b = b && (c == T[j] || c == '?'); } if (b) { char[] ans = S.dup; ans[i .. i + T.length][] = T; foreach (ref c; ans) if (c == '?') { c = 'a'; } writeln(ans); return; } } writeln("UNRESTORABLE"); }
D
void main() { long[] tmp = readln.split.to!(long[]); long n = tmp[0], m = tmp[1]; long[] dp = new long[](1<<n); dp[1..$] = 1L << 60; foreach (i; 0 .. m) { tmp = readln.split.to!(long[]); long a = tmp[0], b = tmp[1]; long[] c = readln.split.to!(long[]); long pos; foreach (x; c) { pos |= 1 << (x - 1); } foreach (j; 0 .. 1<<n) { dp[j|pos] = min(dp[j|pos], dp[j]+a); } } writeln(dp[$-1] != 1L << 60 ? dp[$-1] : -1); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
void main() { auto S = rs; auto T = rs; ulong res; foreach(i; 0..S.length) { if(S[i] != T[i]) res++; } 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; } long rl() { return readAs!long; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.conv, std.stdio, std.algorithm, std.string, std.range, std.math; void main() { readln; const S = readln.chomp; S.uniq.count.writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; bool[1000001] MEMO; void main() { auto s = readln.chomp.to!int; int i; for (;;) { ++i; if (MEMO[s]) { writeln(i); return; } MEMO[s] = true; s = s % 2 == 0 ? s / 2 : s * 3 + 1; } }
D
import std.stdio, std.string, std.conv, std.bigint, std.typecons, std.algorithm, std.array, std.math, std.range; void main() { auto N = readln.chomp.to!int; auto A = readln.chomp.to!int; writeln(N^^2 - A); }
D
import std.stdio; import std.algorithm; import std.string; import std.functional; import std.array; import std.conv; import std.math; import std.typecons; import std.regex; import std.range; long[] m = [31,28,31,30,31,30,31,31,30,31,30,31]; void main(){ while(true){ auto s = readln().split().to!(long[]); if(s[0] < 0 || s[1] < 0 || s[2] <0 || s[3] < 0 || s[4] < 0 || s[5] < 0){ break; } long ans; bool lp1 = s[0] % 4 == 0 && (s[0] % 100 != 0 || ( s[0] % 100 == 0 && s[0] % 400 == 0)); bool lp2 = s[3] % 4 == 0 && (s[3] % 100 != 0 || ( s[3] % 100 == 0 && s[3] % 400 == 0)); if(s[0] == s[3]){ if(s[1] == s[4]){ ans += s[5] - s[2]; }else{ ans+=m[s[1]-1] - s[2]; for(long i=s[1]+1;i<=s[4]-1;i++){ ans += m[i-1]; } ans+=s[5]; if(s[1] <= 2 && lp1 && 2 < s[4]) ans++; } }else{ ans = (s[3] - s[0] - 1) * 365; for(long i=s[0]+1;i<=s[3]-1;i++){ if(i%4==0 && ( i%100!=0 || (i%100==0 &&i%400==0))) ans++; } /* ans += (s[3]-1) / 4 - (s[3]-1) / 100 + (s[3]-1) / 400; ans -= (s[0]+1) / 4 - (s[0]+1) /100 + (s[0]+1) / 400; if((s[0]+1) % 4 == 0 && ((s[0]+1) % 100 != 0 || ( (s[0]+1) % 100 == 0 && (s[0]+1) % 400 == 0))) ans--; */ for(long i=s[1]+1;i<=12;i++){ if(lp1 && i == 2) ans += 1; ans += m[i-1]; } ans += m[s[1]-1] - s[2]; for(long i=1;i<s[4];i++){ if(lp2 && i == 2) ans += 1; ans += m[i-1]; } ans += s[5]; } ans.writeln(); } }
D
void main() { dchar[] s = rdDchar; dchar[] t = rdDchar; long len = s.length; foreach (i; 0 .. len) { if (s == t) { "Yes".writeln; return; } s = s[$-1] ~ s[0..$-1]; } "No".writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto a = readln.split.to!(int[]); long ans = 0; for (int i = 0, j = 0, s = 0; i < n; s ^= a[i++]) { while (j < n && ((s & a[j]) == 0)) { s ^= a[j++]; ans += (j - i); } } writeln(ans); } void rd(T...)(ref T x) { import std.stdio : readln; import std.string : split; import std.conv : to; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
D
import std.stdio, std.math, std.conv, std.array, std.string, std.algorithm; void main() { uint c = to!uint(readln().chomp); for (uint i = 0; i < c; ++i) { double[] dx = map!(to!double)(readln().chomp.split).array; double[] ap = dx[0..2]; double[] bp = dx[3..5]; double ar = dx[2]; double br = dx[5]; double d = dist(ap, bp); if (d < (ar-br)^^2) { if (br < ar) { writeln(2); } else { writeln(-2); } } else if (d > (ar+br)^^2) { writeln(0); } else { writeln(1); } } } double dist(double[] ax, double[] bx) { return (ax[0]-bx[0])^^2 + (ax[1]-bx[1])^^2; }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; import std.typecons; import std.bigint; // import dcomp.foundation, dcomp.scanner; // import dcomp.container.deque; int main() { auto sc = new Scanner(stdin); int n, m, k; sc.read(n, m, k); m++; int[] a = new int[n]; a.each!((ref x) => sc.read(x)); long[] dp = a.map!(to!long).array; long[] ndp = new long[n]; auto deq = Deque!(int, false).make(); auto p = deq.p; foreach (int ph; 2..k+1) { ndp[] = -(10L^^18); deq.clear(); foreach (int i; 0..n) { if (deq.length && deq[0] == i-m) { p.removeFront(); } if (deq.length) { ndp[i] = dp[deq[0]] + 1L * ph * a[i]; } while (deq.length && dp[deq[p.length-1]] <= dp[i]) { p.removeBack(); } p.insertBack(i); } swap(dp, ndp); } writeln(dp.fold!max); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; //fold(for old compiler) static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } unittest { import std.stdio; auto l = [1, 2, 3, 4, 5]; assert(l.fold!"a+b"(10) == 25); } } version (X86) static if (__VERSION__ < 2071) { int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/deque.d */ // module dcomp.container.deque; struct Deque(T, bool hasNull = true) { 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(size_t(4), 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 clear() { st = length = 0; } 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 (hasNull && !p) p = new Payload(); } private void C() const { version(assert) if (hasNull && !p) throw new RangeError(); } //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); } } static Deque make() { Deque que; que.p = new Payload(); return que; } @property private bool hasPayload() const { return (!hasNull || p); } @property bool empty() const { return (!hasPayload || p.empty); } @property size_t length() const { return (hasPayload ? 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 clear() { if (p) p.clear(); } 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 { import std.algorithm : equal; import std.range.primitives : isRandomAccessRange; import std.container.util : make; auto q = make!(Deque!int); q.clear(); assert(equal(q[], new int[0])); foreach (i; 0..100) { q.insertBack(1); q.insertBack(2); q.insertBack(3); q.insertBack(4); q.insertBack(5); assert(equal(q[], [1,2,3,4,5])); q.clear(); assert(equal(q[], new int[0])); } } 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 /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; if (f.eof) return false; line = lineBuf[]; f.readln(line); } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { //string or char[10] etc //todo optimize auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { //static assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); fout.writeln("1 2 3"); fout.writeln("ab cde"); fout.writeln("1.0 1.0 2.0"); fout.close; Scanner sc = new Scanner(File(fileName, "r")); int a; int[2] b; char[2] c; string d; double e; double[] f; sc.read(a, b, c, d, e, f); assert(a == 1); assert(equal(b[], [2, 3])); assert(equal(c[], "ab")); assert(equal(d, "cde")); assert(e == 1.0); assert(equal(f, [1.0, 2.0])); } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File, writeln; import std.datetime; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); foreach (i; 0..1_000_000) { fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2); } fout.close; writeln("Scanner Speed Test(3*1,000,000 int)"); StopWatch sw; sw.start; Scanner sc = new Scanner(File(fileName, "r")); foreach (i; 0..500_000) { int a, b, c; sc.read(a, b, c); assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 500_000..700_000) { int[3] d; sc.read(d); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 700_000..1_000_000) { int[] d; sc.read(d); assert(d.length == 3); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } writeln(sw.peek.msecs, "ms"); }
D
import std.stdio; void main(){ for(int x=1;x<10;x++){for(int y=1;y<10;y++){ writeln(x,"x",y,"=",x*y); }} }
D
import std.stdio; import std.string; import std.algorithm; void main() { foreach (input; stdin.byLine) { // 勝敗を判定 string result = judge(input.chomp); // 結果を出力 result.writeln; } } string judge(char[] banmen) { // 勝ち筋 auto combs = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]; // それぞれの配置を取得 auto areas = ["o":getArea(banmen, 'o'), "x":getArea(banmen, 'x')]; // 各々の配置が勝ち筋に沿っていれば、その記号を返す foreach(key; areas.keys) { foreach(comb; combs) { if(canFind(areas[key], comb[0]) && canFind(areas[key], comb[1]) && canFind(areas[key], comb[2])) return key; } } // どちらも勝ち筋に沿っていない場合はドローの記号を返す return "d"; } int[] getArea(char[] banmen, char side) { int[] area = []; foreach (i; 0..9) { if(banmen[i] == side) { area ~= i; } } return area; }
D
import std.stdio; import std.algorithm; import std.string; import std.conv; import std.math; void main(){ while(true){ auto s1 = readln(); if(stdin.eof()) break; auto s = split(s1); int a = to!int(s[0]); int b = to!int(s[1]); int oth = to!int(s[2]); int[11] cards; cards = 0; cards[a]--; cards[b]--; cards[oth]--; int r = 20 - a - b; int ans = 0; int ini = min(10,r); for(int i=ini;i>=0;i--){ if(cards[i]==0) ans++; } if(ans>=5) writeln("YES"); else writeln("NO"); } }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; 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() { string A, op, B; scan(A, op, B); long a = A.to!long(); long b = B.to!long(); if (op == "-") { writeln(a - b); } else { writeln(a + b); } }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n; readV(n); string s; readV(s); auto x = 0, m = 0; foreach (si; s) { x += si == 'I' ? +1 : -1; m = max(m, x); } writeln(m); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, 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 main() { int n; readV(n); writeln(n.nsqrt^^2); } pure T nsqrt(T)(T n) { import std.algorithm, std.conv, std.range, core.bitop; if (n <= 1) return n; T m = 1 << (n.bsr/2+1); return iota(1, m).map!"a * a".assumeSorted!"a <= b".lowerBound(n).length.to!T; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void main() { auto N = readln.chomp.to!long; long r; foreach (a; 1..N) { auto b = N/a; r += b - (N%a == 0 ? 1 : 0); } writeln(r); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); auto P = aryread(); long m = long.max; long ans; foreach (i; 0 .. N) { if (P[i] <= m) ans++; m = min(P[i], m); } writeln(ans); }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.math; void main(){ writeln("Odd"); }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ int n = read.to!int; char[] cs = readln.chomp.to!(char[]); char[] ans = "0000000000".to!(char[]); foreach(c; cs){ if(c == 'L'){ foreach(i; 0 .. 10) if(ans[i] == '0'){ ans[i] = '1'; break; } } else if(c == 'R'){ foreach_reverse(i; 0 .. 10) if(ans[i] == '0'){ ans[i] = '1'; break; } } else{ int i = (c - '0').to!int; ans[i] = '0'; } } ans.writeln; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n=14; auto a=readln.split.to!(long[]); long ret=0; foreach(i; 0..n)if(a[i]>0){ auto b=a.dup; long k=b[i]; b[i]=0; foreach(j; 0..n) b[j]+=k/n; for(int j=1; j<=(k%n).to!(int); j++) b[(i+j)%n]+=1; long score=0; foreach(e; b)if(e%2==0) score+=e; ret=max(ret, score); } writeln(ret); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x) e=l[i].to!(typeof(e)); }
D
//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; import std.exception; T gcd(T) (T a, T b) pure nothrow @nogc { if (a < b) { swap (a, b); } while (b) { T c = a % b; a = b; b = c; } return a; } T gcdext(T) (T a, T b, ref T x, ref T y) pure nothrow @nogc { if (b == 0) { x = 1; y = 0; return a; } T res = gcdext (b, a % b, y, x); y -= x * (a / b); return res; } struct IntM { enum q = 1_000_003; int v; this (int m) pure nothrow @nogc { v = m % q; if (v < 0) { v += q; } } IntM opAssign (int m) pure nothrow @nogc { v = m % q; if (v < 0) { v += q; } return this; } IntM opUnary (string op : "-")() const pure nothrow @nogc { return IntM ((q - v) % q); } ref IntM opUnary (string op : "++")() pure nothrow @nogc { if (++v >= q) { v -= q; } return this; } ref IntM opUnary (string op : "--")() pure nothrow @nogc { if (--v < 0) { v += q; } return this; } ref IntM opOpAssign (string op : "+")(in IntM rhs) pure nothrow @nogc { v += rhs.v; v %= q; return this; } ref IntM opOpAssign (string op : "-")(in IntM rhs) pure nothrow @nogc { v -= rhs.v; v %= q; return this; } ref IntM opOpAssign (string op : "*")(in IntM rhs) pure nothrow @nogc { v = ((v.to!(long)) * rhs.v.to!(long)) % q; return this; } IntM opBinary (string op : "+")(in IntM rhs) const pure nothrow @nogc { return IntM ( (v + rhs.v) % q); } IntM opBinary (string op : "-")(in IntM rhs) const pure nothrow @nogc { return IntM ( (v - rhs.v) % q); } IntM opBinary (string op : "*")(in IntM rhs) const pure nothrow @nogc { return IntM (((v.to!(long)) * rhs.v.to!(long)) % q); } IntM opBinary (string op : "^^")(in int rhs) const pure nothrow @nogc { IntM a = 1, b = this; int p = rhs; while (p > 0) { //a * (b ^ p) == x ^ rhs if (p & 1) { a *= b; } b *= b; p >>>= 1; } return a; } IntM opBinary (string op)(in int v) const pure nothrow @nogc if (op == "+" || op == "-" || op == "*") { mixin ("return this " ~ op ~ " IntM(v);"); } int opCast(T : int)() const pure nothrow @nogc { return v; } int opCmp (const IntM rhs) const pure nothrow @nogc { if (v < rhs.v) { return -1; } if (v > rhs.v) { return 1; } return 0; } bool opEquals (const IntM rhs) const pure nothrow @nogc { return v == rhs.v; } string toString() const pure nothrow { return ((v < 0) ? v + q : v).text; } } void main() { IntM[][] a = new IntM[][] (11, 11); IntM[11] b; foreach (i; 0 .. 11) { IntM ii = i; writeln ("? ", i); stdout.flush (); a[i][0] = 1; foreach (j; 1 .. 11) { a[i][j] = a[i][j-1] * ii; } int bi = readln.strip.to!int; b[i] = bi; } immutable IntM z = 0; foreach (i; 0 .. 11) { enforce (a[i][i] != z); foreach (j; i + 1 .. 11) { IntM aji = a[j][i]; foreach (k; i .. 11) { a[j][k] *= a[i][i]; a[j][k] -= a[i][k] * aji; } b[j] *= a[i][i]; b[j] -= b[i] * aji; } } IntM[11] x; foreach_reverse (i; 0 .. 11) { x[i] = b[i]; foreach (j; i + 1 .. 11) { x[i] -= a[i][j] * x[j]; } x[i] *= a[i][i] ^^ (IntM.q - 2); } debug stderr.writeln (a); debug stderr.writeln (b); debug stderr.writeln (x); foreach (i; 0 .. IntM.q) { IntM r; IntM ii = i; foreach_reverse (j; 0 .. 11) { r *= ii; r += x[j]; } if (r == z) { writeln ("! ", i); return; } } writeln ("! ", -1); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new int[](t); foreach (ti; 0..t) { auto a = RD!int; auto b = RD!int; auto x = a & b; ans[ti] = (a ^ x) + (b ^ x); } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
import std.stdio, std.bigint, std.string; void main() { int n; scanf("%d", &n); int[] a = new int[n]; for(int i = 0; i<n; i++) { scanf("%d", &a[i]); } int [] b= new int[n]; for (int i =0; i < n; ++i) { b[i] = -1; } b[0] = a[0]; int c = 1; int i = 1; while (i < n) { c +=1; b[c-1] = a[i]; while (c > 1 && b[c-1] == b[c-2]) { b[c-2] = b[c-2] +1; c-=1; } i += 1; } writeln(c); for (int j = 0; j < c; ++j) { write(b[j]); write(" "); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new int[](t); foreach (ti; 0..t) { auto n = RD!int; auto s = RD!string; long cnt; foreach (c; s) { if (c == '0') ++cnt; } if (cnt % 2) ans[ti] = cnt == 1 ? -1 : 1; else ans[ti] = -1; } foreach (e; ans) { writeln(e == 1 ? "ALICE" : e == -1 ? "BOB" : "DRAW"); } stdout.flush; debug readln; }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { int n; scan(n); int ans; int set = (1<<26) - 1; bool safe; foreach (i ; 0 .. n-1) { char e; string s; scan(e, s); if (safe && e != '.') { ans++; continue; } int t; if (e == '!') { foreach (ch ; s) { t |= (1<<(ch - 'a')); } } else if (e == '.') { t = (1<<26) - 1; foreach (ch ; s) { t &= ~(1<<(ch - 'a')); } } else { t = ~(1<<(s[0] - 'a')); } set &= t; debug { writefln("%b",set); } if (set.popcnt == 1) { safe = 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
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { auto tests = readln.strip.to !(int); multitest_loop: foreach (test; 0..tests) { auto n = readln.strip.to !(int); auto s = readln.strip; auto t = readln.strip; if (n.iota.any !(i => s[i] > t[i])) { writeln (-1); continue multitest_loop; } auto r = s.dup; int res = 0; auto b = new bool [n]; foreach (c; 'a'..'u') { foreach (i; 0..n) { b[i] = (r[i] == c && r[i] < t[i]); } if (any (b)) { auto d = n.iota.filter !(i => b[i]) .map !(i => t[i]).minElement; foreach (i; 0..n) { if (r[i] == c) { r[i] = d; } } res += 1; } } writeln (res); } }
D
import std.stdio, std.conv, std.string; void main() { int n,m,k; scanf("%d %d %d", &n, &m, &k); foreach (i;0..(m+1)) { foreach (j;0..(n+1)) { auto a = n*i + (m-2*i)*j; if (a == k) { writeln("Yes"); return; } } } writeln("No"); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; T gcd(T)(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } T lcm(T)(T a, T b) { return a * (b / gcd(a, b)); } void main() { readint; auto xs = readints; auto ans = xs.reduce!lcm; writeln(ans); }
D
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.typecons, std.range, std.container, std.math, std.algorithm.searching, std.functional,std.mathspecial; void main(){ auto n=readln().chomp.to!int; long[] Ts; long[] As; foreach(i;0..n){ auto ln=readln.split.map!(to!long).array; Ts~=ln[0]; As~=ln[1]; } foreach(i;1..n){ long rate=max(1+(Ts[i-1]-1)/Ts[i],1+(As[i-1]-1)/As[i]); Ts[i]*=rate; As[i]*=rate; } writeln(Ts[n-1]+As[n-1]); }
D
import std.stdio; // ??\???????????????????????? import std.string; // chomp????????????????????????(?????????????????????) import std.conv; // to???????????????????????? import std.array; // split????????????????????? import std.algorithm; // map????????????????????? void main() { uint i = 1; while (true) { int x = readln.chomp.to!int; if (x == 0) { break; } writeln("Case ", i, ": ", x); i++; } }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; immutable long MOD = 10^^9 + 7; void main() { auto S = readln.chomp; auto N = S.length.to!int; auto dp = new long[][](N+1, 4); // Aの数, ABの数, ABCの数 dp[0][0] = 1; foreach (i; 0..N) { foreach (j; 0..4) { dp[i+1][j] = S[i] == '?' ? dp[i][j] * 3 % MOD : dp[i][j]; } if (S[i] == 'A' || S[i] == '?') { (dp[i+1][1] += dp[i][0]) %= MOD; } if (S[i] == 'B' || S[i] == '?') { (dp[i+1][2] += dp[i][1]) %= MOD; } if (S[i] == 'C' || S[i] == '?') { (dp[i+1][3] += dp[i][2]) %= MOD; } } dp[N][3].writeln; }
D
void main(){ int[] atox; int cnt; foreach(i; 0..4)atox ~= _scan(); for(int an; an<=atox[0]; an++){ for(int bn; bn<=atox[1]; bn++){ for(int cn; cn<=atox[2]; cn++){ if(500*an+100*bn+50*cn==atox[3])cnt++; } } } cnt.writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { long n = lread(); writeln((n < 1000) ? "ABC" : "ABD"); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto S = RD!string; writeln(S[0] == S[1] && S[1] == S[2] ? "No" : "Yes"); stdout.flush; debug readln; }
D
import core.stdc.stdio; import std.algorithm; char[][] mapdata; bool[] visited; int[3][] queue; int w,h,n; int len(int[2] p1,int[2] p2){ visited[] = false; queue[0]=[p1[0],p1[1],0]; visited[p1[0]*w+p1[1]] = true; int[] dx = [0,-1,0,1]; int[] dy = [-1,0,1,0]; int qc=1; for(int i=0;i<w*h;i++){ int y=queue[i][0]; int x=queue[i][1]; int s=queue[i][2]; if(y==p2[0]&&x==p2[1]) return s; for(int k=0;k<4;k++){ int ny = y+dy[k]; int nx = x+dx[k]; if(0<=ny&&ny<h&&0<=nx&&nx<w&&mapdata[ny][nx]!='X'&&!visited[ny*w+nx]){ visited[ny*w+nx]=true; queue[qc++] = [ny,nx,s+1]; } } } return 114514; } void main(){ scanf("%d%d%d",&h,&w,&n); int[2][] poses = new int[2][n+1]; mapdata = new char[][h]; visited = new bool[w*h]; queue = new int[3][w*h]; for(int i=0;i<h;i++){ mapdata[i] = new char[w+1]; scanf("%s",mapdata[i].ptr); foreach(int j,c;mapdata[i]){ if(c=='S') poses[0] = [i,j]; else if('1'<=c&&c<='9') poses[c-'0'] = [i,j]; } } int ans=0; for(int i=0;i<n;i++) ans += len(poses[i],poses[i+1]); printf("%d\n",ans); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static 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 S = new string[](3); foreach (i; 0..3) S[i] = RD!string; size_t pos; while (true) { if (S[pos].empty) break; auto c = S[pos].front; S[pos].popFront; if (c == 'a') pos = 0; else if (c == 'b') pos = 1; else pos = 2; } writeln(pos == 0 ? "A" : pos == 1 ? "B" : "C"); stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.string; import std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } int DEBUG_LEVEL = 0; void print()(){ writeln(""); } void print(T, A ...)(T t, lazy A a){ write(t), print(a); } void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); } void main(string[] args){ if(args.length > 1 && args[1] == "-debug"){ if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int; else DEBUG_LEVEL = 1; } long a = read.to!long; long p = read.to!long; p += a * 3; (p / 2).writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; enum cs = [6,36,216,1296,7776,46656,279936,9,81,729,6561,59049,531441]; void main() { auto N = readln.chomp.to!int; auto DP = new int[][](cs.length, N+1); foreach (ref dp; DP) dp[] = -1; int solve(int i, int n) { if (i == cs.length) return n; if (DP[i][n] == -1) { if (n >= cs[i]) { DP[i][n] = min(solve(i+1, n), solve(i, n-cs[i]) + 1); } else { DP[i][n] = solve(i+1, n); } } return DP[i][n]; } writeln(solve(0, N)); }
D
import std.stdio; import std.conv; import std.algorithm; import std.range; import std.string; import std.math; import std.format; import std.datetime; import std.typecons; void main() { foreach (string line; stdin.lines) { int n = line.chomp.to!int; int i = 0; int[] ans; while (n != 0) { if (n % 2 == 1) { ans ~= pow(2, i); } n = n >> 1; i++; } ans.map!(to!string).join(" ").writeln; } }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto G = new UndirectedGraph(N); foreach (i; 0..M) { s = readln.split.map!(to!int); G.add_edge(s[0], s[1]); } G.detect_articulation_points.each!writeln; } int[] detect_articulation_points(const ref UndirectedGraph g) { int root = 0; int root_children = 0; int cnt = 1; auto ord = new int[](g.n); auto low = new int[](g.n); auto is_articulation_point = new bool[](g.n); void dfs(int u) { ord[u] = cnt++; low[u] = ord[u]; foreach (v; g.adj[u]) { if (ord[v] == 0) { dfs(v); low[u] = min(low[u], low[v]); if (u == root) { root_children += 1; if (root_children == 2) { is_articulation_point[u] = true; } } else if (ord[u] <= low[v]) { is_articulation_point[u] = true; } } else { low[u] = min(low[u], ord[v]); } } } dfs(root); return g.n.iota.filter!(u => is_articulation_point[u]).array; } class UndirectedGraph { int n; int[][] adj; this (int n) { this.n = n; adj = new int[][](n); } void add_edge(int u, int v) { adj[u] ~= v; adj[v] ~= u; } }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n, m; rd(n, m); auto a = new int[][](n, m); foreach (i; 0 .. n) { a[i] = readln.split.to!(int[]); } auto b = new int[][](n, m); foreach (i; 0 .. n) { b[i] = readln.split.to!(int[]); } if (n < 2 || m < 2) { bool all = true; foreach (i; 0 .. n) { foreach (j; 0 .. m) { all &= a[i][j] == b[i][j]; } } writeln(all ? "Yes" : "No"); return; } foreach (i; 0 .. (n - 1)) { foreach (j; 0 .. (m - 1)) { if (a[i][j] != b[i][j]) { a[i][j] ^= 1; a[i + 1][j] ^= 1; a[i][j + 1] ^= 1; a[i + 1][j + 1] ^= 1; } } } bool all = true; foreach (i; 0 .. n) { foreach (j; 0 .. m) { all &= a[i][j] == b[i][j]; } } writeln(all ? "Yes" : "No"); } void rd(T...)(ref T x) { import std.stdio : readln; import std.string : split; import std.conv : to; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
D
import std.stdio; import std.string; import std.array; import std.range; import std.algorithm; import std.conv; void main(string[] args) { auto input = readln().chomp.split.map!(to!int); max(input[0]+input[1],input[0]-input[1],input[0]*input[1]).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; enum mod = 1_000_000_007L; void main() { int N, T; scan(N, T); auto a = new int[](N); auto b = new int[](N); foreach (i ; 0 .. N) { scan(a[i], b[i]); } auto dpf = new long[][](N + 1, T); foreach (i ; 1 .. N + 1) { foreach (j ; 0 .. T) { dpf[i][j] = dpf[i - 1][j]; if (j - a[i - 1] >= 0) chmax(dpf[i][j], dpf[i - 1][j - a[i - 1]] + b[i - 1]); } } auto dpb = new long[][](N + 1, T); foreach_reverse (i ; 0 .. N) { foreach (j ; 0 .. T) { dpb[i][j] = dpb[i + 1][j]; if (j - a[i] >= 0) chmax(dpb[i][j], dpb[i + 1][j - a[i]] + b[i]); } } long ans; foreach (i ; 0 .. N) { foreach (j ; 0 .. T) { chmax(ans, dpf[i][j] + dpb[i + 1][T - 1 - j] + b[i]); } } writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio,std.string,std.array,std.algorithm,std.range,std.conv; import std.algorithm:rev=reverse; void main(){ auto input=readln.chomp.split.to!(int[]); auto n=input[0]; auto m=input[1]; auto result=n*(n-1)/2; result+=m*(m-1)/2; result.writeln; }
D
//dlang template---{{{ import std.stdio; import std.conv; import std.string; import std.array; import std.algorithm; import std.typecons; import std.math; import std.range; // 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; string input; sc.scan(input); char[] S = new char[input.length]; long len = input.length; int zeroNum = 0; int oneNum = 0; foreach (i; 0 .. len) { S[i] = to!char(input[0]); input.popFront; if (S[i] == '0') zeroNum++; else if (S[i] == '1') oneNum++; } if (oneNum == zeroNum) { writeln(oneNum + zeroNum); return; } writeln((zeroNum + oneNum) - abs(zeroNum - oneNum)); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); void main() { // 1*2*3*4*5*6 == 2^^4 * 3^^2 * 5 // 1* // 2* // 3* // 2*2* // 5* // 2*3 auto n = lread(); long[long] aa; foreach (i; 1 .. n + 1) { long[long] tmp = factorize(i); // writeln(tmp); foreach (key, value; tmp) { aa[key] = aa.get(key, 0) + value; } } // writeln(aa); long ans = 1; foreach (key, value; aa) { ans = ans % (10 ^^ 9 + 7) * (value + 1) % (10 ^^ 9 + 7); } writeln(ans); } long[long] factorize(long n) { long tmp = n; long[long] aa; foreach (x; 2 .. n * n + 100) { if (tmp < x) { if (tmp != 1) aa[tmp] = aa.get(tmp, 0) + 1; return aa; } while (tmp % x == 0) { aa[x] = aa.get(x, 0) + 1; tmp /= x; } } writeln(tmp); assert(0); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
D
void main() { int n = readln.chomp.to!int; int[] v = readln.split.to!(int[]); int[] c = readln.split.to!(int[]); int ans; foreach (i; 0 .. n) { int diff = v[i] - c[i]; if (diff > 0) ans += diff; } ans.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.container; import std.typecons;
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.format; void main() { auto D = readln.chomp.to!int; switch(D) { case 22: writeln("Christmas Eve Eve Eve"); break; case 23: writeln("Christmas Eve Eve"); break; case 24: writeln("Christmas Eve"); break; case 25: writeln("Christmas"); break; default: } }
D
import std.algorithm, std.array, std.container, std.range, std.bitmanip; import std.numeric, std.math, std.bigint, std.random, core.bitop; import std.string, std.conv, std.stdio, std.typecons; void main() { while (true) { auto rd = readln.split.map!(to!int); auto h = rd[0], w = rd[1]; if (h == 0 && w == 0) break; foreach (i; 0..h) { foreach (j; 0..w) write(i == 0 || i == h - 1 || j == 0 || j == w - 1 ? '#' : '.'); writeln; } writeln; } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; alias E = Tuple!(int, "node", int, "w"); void calc(int n, int[][] a) { E[][] adj; foreach (_; 0..n) adj ~= [[]]; foreach (e; a) { int u = e[0], v = e[1], w = e[2]; adj[u] ~= E(v, w); adj[v] ~= E(u, w); } auto ans = new int[n]; void dfs(int node, int prev, int color) { ans[node] = color; foreach (e; adj[node]) { if (e.node == prev) continue; // 逆戻り if (e.w % 2 == 0) dfs(e.node, node, color); else dfs(e.node, node, color ^ 1); } } dfs(0, -1, 0); foreach (c; ans) writeln(c); } void main() { int n = readint; int[][] a; for (int i = 0; i < n - 1; i++) { auto x = readints; a ~= [x[0]-1, x[1]-1, x[2]]; } calc(n, a); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.math; import std.regex; void main() { auto N = readln.chomp.to!int; auto A = readln.split.to!(uint[]); uint count; loop: while(true) { foreach(ref A_i; A) { if(A_i % 2 != 0) break loop; else A_i /= 2; } count++; } writeln(count); }
D
// Vicfred // https://atcoder.jp/contests/abc136/tasks/abc136_c import std.algorithm; import std.array; import std.conv; import std.stdio; import std.string; void main() { const long n = readln.chomp.to!long; long[] h = readln.split.map!(to!long).array; long maxima = h[0] - 1; foreach(square; h) { if(square > maxima) square -= 1; if(square < maxima) { "No".writeln; return; } maxima = square; } "Yes".writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; writeln(N%2 == 0 ? N : N*2); }
D
import std.stdio; import std.conv; import std.string; void main() { int x = readln.chomp.to!int; writeln(x * x * x); }
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 hw = readln.chomp.split.to!(int[]); auto c = new int[][](10, 10); foreach (i; 0..10) { c[i] = readln.chomp.split.to!(int[]); } int[int] cnt; foreach (i; 0..hw[0]) { auto a = readln.chomp.split.to!(int[]); foreach (j; 0..hw[1]) { cnt[a[j]]++; } } foreach (k; 0..10) { foreach (i; 0..10) { foreach (j; 0..10) { if (i == j) continue; c[i][j] = min(c[i][j], c[i][k] + c[k][j]); } } } int cost; foreach (k; cnt.keys) { if (k == -1 || k == 1) continue; cost += c[k][1] * cnt[k]; } cost.writeln; }
D
module app; import core.bitop; import std.algorithm; import std.array; import std.bigint; import std.conv; import std.stdio; import std.string; import std.traits; struct Input { int n; } void parseInput(T)(out Input input, T file) { with (file) with (input) { n = readln().strip().to!int; } } auto main2(Input* input) { with (input) { return (n + 1) / 2; } } alias retType = ReturnType!main2; static if (!is(retType == void)) { unittest { writeln("begin unittest"); } auto _placeholder_ = ReturnType!main2.init; unittest // example1 { string example = `5`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == 3); } unittest // example2 { string example = `2`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == 1); } unittest // example3 { string example = `100`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == 50); } unittest { writeln("end unittest"); } void parseExample(out Input input, string example) { struct Adapter { string[] _lines; this(string input) { _lines = input.splitLines(); } string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; } } parseInput(input, Adapter(example)); } } void printResult(T)(T result) { static if (isFloatingPoint!T) writefln("%f", result); else writeln(result); } void main() { Input input = void; parseInput(input, stdin); alias retType = ReturnType!main2; static if (is(retType == void)) main2(&input); else { auto result = main2(&input); printResult(result); } }
D
import std.algorithm; import std.array; import std.conv; import std.stdio; void main() { int[] n_and_m; int n; int m; int[] a_tachi; int total_vote_count; n_and_m = readln.split.to!(int[]); n = n_and_m[0]; m = n_and_m[1]; a_tachi = readln.split.to!(int[]); total_vote_count = a_tachi.sum; if (a_tachi.count!(a => a * 4 * m >= total_vote_count) >= m) { writeln("Yes"); } else { writeln("No"); } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; int cnt; void InsertionSort(int[] arr, int n, int g){ for(int i = g; i < n; ++i){ int v = arr[i]; int j = i - g; while (j >= 0 && arr[j] > v){ arr[j+g] = arr[j]; j -= g; ++cnt; } arr[j+g] = v; } } void ShellSort(int[] arr, int n){ cnt = 0; int m; int[] g; for(int i = 1; ; ){ g ~= i; i = 3*i + 1; if(i > n) break; } reverse(g); m = g.length.to!int(); //writeln("m : ",m); writeln(m); for(int i = 0; i < m; ++i){ //write("g[", i ,"] : ", g[i]); write(g[i]); if(i == m-1) writeln(); else write(" "); } for(int i = 0; i < m; ++i){ InsertionSort(arr, n, g[i]); } } int main(string[] argv) { auto n = readln.chomp.to!int(); int[] arr; for(int i = 0; i < n; ++i){ arr ~= readln.chomp.to!int(); } ShellSort(arr, n); //writeln("cnt : ", cnt); writeln(cnt); foreach(e; arr){ writeln(e); } return 0; }
D
module app; import core.bitop; import std.algorithm; import std.array; import std.bigint; import std.conv; import std.stdio; import std.string; import std.traits; struct Input { int n, m; int[] s, c; } void parseInput(T)(out Input input, T file) { with (file) with (input) { auto ar = readln().strip().split().map!(to!int).array(); n = ar[0].to!int; m = ar[1].to!int; foreach (i; 0..m) { ar = readln().strip().split().map!(to!int).array(); s ~= ar[0]; c ~= ar[1]; } } } auto main2(Input* input) { with (input) { int[] result; result.length = n; result[] = -1; foreach (i, ss; s) { if (result[ss-1] != -1 && result[ss-1] != c[i]) return -1; result[ss-1] = c[i]; } if (result[0] == 0) { if (n == 1) return 0; else return -1; } if (n == 1 && result[0] == -1) return 0; foreach (i, ref r; result) { if (i == 0) { if (r == -1) r = 1; } else { if (r == -1) r = 0; } } int intResult = 0; foreach (r; result) { intResult *= 10; intResult += r; } return intResult; } } alias retType = ReturnType!main2; static if (!is(retType == void)) { unittest { writeln("begin unittest"); } auto _placeholder_ = ReturnType!main2.init; unittest // example1 { string example = `3 3 1 7 3 2 1 7`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == 702); } unittest // example2 { string example = `3 2 2 1 2 3`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == -1); } unittest // example3 { string example = `3 1 1 0`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == -1); } unittest { writeln("end unittest"); } void parseExample(out Input input, string example) { struct Adapter { string[] _lines; this(string input) { _lines = input.splitLines(); } string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; } } parseInput(input, Adapter(example)); } } void printResult(T)(T result) { static if (isFloatingPoint!T) writefln("%f", result); else writeln(result); } void main() { Input input = void; parseInput(input, stdin); alias retType = ReturnType!main2; static if (is(retType == void)) main2(&input); else { auto result = main2(&input); printResult(result); } }
D
import std.stdio; import std.range; import std.array; import std.algorithm; import std.string; import std.conv; void main(){ auto n = readln(); auto s = readln().chomp(); solve(s).writeln(); } int solve(string s){ int allE = s.filter!((ch) => ch == 'E').array().length.to!int; int allW = s.filter!((ch) => ch == 'W').array().length.to!int; int checkedE; int checkedW; int[] collection; foreach(dir; s) { if(dir == 'E'){ checkedE++; }else{ checkedW++; } if(dir == 'W'){ collection ~= checkedW + (allE - checkedE) - 1; }else{ collection ~= checkedW + (allE - checkedE); } // writefln("checkedE:%s chekedW:%s allE:%s leader:%s collection:%s", checkedE, checkedW, allE, dir, collection); } return collection.sort!("a < b")[0]; }
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 n; readV(n); int[] s; foreach (d; 3..10) s ~= find753(d); writeln(s.assumeSorted.lowerBound(n+1).length); } auto find753(int d) { int[] s; foreach (i; 0..3^^d) { auto r = conv3(i, d); if (!r.canFind(0) || !r.canFind(1) || !r.canFind(2)) continue; auto j = 0; foreach (k, ri; r) j += ri.predSwitch(0, 3, 1, 5, 2, 7) * 10^^k; s ~= j; } return s; } auto conv3(int i, int d) { auto r = new int[](d); foreach (j; 0..d) { r[j] = i%3; i /= 3; } return r; }
D
import std.stdio; import std.algorithm; import std.string; import std.conv; import std.math; void main(){ auto s = chomp(readln()); char[] ss; for(int i=0;i<s.length;i++){ ss ~= s[i]; } for(int i=0;i<s.length-4;i++){ if(ss[i]=='a'&&ss[i+1]=='p'&&ss[i+2]=='p'&&ss[i+3]=='l'&&ss[i+4]=='e'){ ss[i] = 'p'; ss[i+1] = 'e'; ss[i+2] = 'a'; ss[i+3] = 'c'; ss[i+4] = 'h'; }else if(ss[i]=='p'&&ss[i+1]=='e'&&ss[i+2]=='a'&&ss[i+3]=='c'&&ss[i+4]=='h'){ ss[i] = 'a'; ss[i+1] = 'p'; ss[i+2] = 'p'; ss[i+3] = 'l'; ss[i+4] = 'e'; } } for(int i=0;i<s.length;i++) write(ss[i]); 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 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, C; scan(N, A, B, C); long ans; auto mc = ModComb(2*N); auto p = A * powmod(A + B, mod - 2) % mod; auto q = B * powmod(A + B, mod - 2) % mod; foreach (M ; N .. 2*N) { auto c = mc.c(M - 1, N - 1); auto num = powmod(p, N) * powmod(q, M - N) % mod; num += powmod(q, N) * powmod(p, M - N) % mod; num %= mod; num = c * num % mod * M % mod; (ans += num) %= mod; } ans = (ans * 100) % mod * powmod(100 - C, mod - 2) % mod; writeln(ans); } long powmod(long x, long y) { return y > 0 ? powmod(x, y>>1)^^2 % mod * x^^(y&1) % mod : 1; } struct ModComb { int _N; long[] _fact, _factinv; long _mod; this(int n, long mod = 1_000_000_007L) { _N = n; _fact = new long[](_N + 1); _factinv = new long[](_N + 1); _mod = mod; _fact[0] = 1; foreach (i ; 1 .. _N + 1) { _fact[i] = (_fact[i-1] * i) % mod; } _factinv[_N] = _powmod(_fact[_N], mod - 2); foreach_reverse (i ; 0 .. _N) { _factinv[i] = (_factinv[i+1] * (i+1)) % mod; } } long c(int n, int k) { return f(n) * finv(n - k) % _mod * finv(k) % _mod; } long p(int n, int r) { return f(n) * finv(n - r) % _mod; } long f(int n) { return _fact[n]; } long finv(int n) { return _factinv[n]; } long _powmod(long x, long y) { return y > 0 ? _powmod(x, y>>1)^^2 % _mod * x^^(y & 1) % _mod : 1; } } int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new int[][](n, 0); foreach (i; 0 .. m) { int u, v; scan(u, v); if (is1indexed) { u--, v--; } adj[u] ~= v; if (isUndirected) { adj[v] ~= u; } } return adj; } alias Edge = Tuple!(int, "to", int, "cost"); Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new Edge[][](n, 0); foreach (i; 0 .. m) { int u, v, c; scan(u, v, c); if (is1indexed) { u--, v--; } adj[u] ~= Edge(v, c); if (isUndirected) { adj[v] ~= Edge(u, c); } } return adj; } void yes(bool b) { writeln(b ? "Yes" : "No"); } void YES(bool b) { writeln(b ? "YES" : "NO"); } T[] readArr(T)() { return readln.split.to!(T[]); } T[] readArrByLines(T)(int n) { return iota(n).map!(i => readln.chomp.to!T).array; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { auto S = sread(); foreach (i; 0 .. S.length) if (i % 2 == 0) write(S[i]); 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; void main() { int n; scan(n); foreach (i ; 0 .. 100) { foreach (j ; 0 .. 100) { if (4*i + 7*j == n) { writeln("Yes"); return; } } } writeln("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, 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 H = RD; auto W = RD; auto a = new string[](H); foreach (i; 0..H) a[i] = RD!string; auto ws = new bool[](W); auto hs = new bool[](H); foreach (y; 0..H) { foreach (x; 0..W) { if (a[y][x] == '#') { ws[x] = 1; hs[y] = 1; } } } foreach (y; 0..H) { bool writed; foreach (x; 0..W) { if (ws[x] == 1 && hs[y] == 1) { write(a[y][x]); writed = true; } } if (writed) writeln(); } stdout.flush(); debug readln(); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; void main() { int n = readln.chomp.to!int; string s = readln.chomp; int leftW = 0; int rightE = 0; foreach (c; s) { if (c == 'E') { ++rightE; } } int ans = int.max; foreach (c; s) { if (c == 'E') { --rightE; } ans = min(ans, leftW + rightE); if (c == 'W') { ++leftW; } } ans.writeln; }
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..$]; } } class UnionFindTree { public: this(size_t n) { parent = iota(n).array(); rank = new size_t[](n); } size_t find(size_t n) { if(n == parent[n]) { return n; } else { parent[n] = find(parent[n]); return parent[n]; } } void unite(size_t n, size_t m) { auto p1 = find(n); auto p2 = find(m); if(n == m) { return; } if(rank[p1] < rank[p2]) { swap(p1, p2); } if(rank[p1] == rank[p2]) { ++rank[p1]; } parent[p2] = p1; } private: size_t[] parent; size_t[] rank; } unittest { auto uft = new UnionFindTree(10); foreach(i; iota(10)) { assert(uft.find(i) == i); } foreach(i; iota(10).filter!(a => a%3 == 0).drop(1)) { uft.unite(i, i-3); } foreach(i; iota(10).filter!(a => a%3 == 0)) { assert(uft.find(0) == uft.find(i)); } foreach(i; iota(10).filter!(a => a%3 != 0)) { assert(uft.find(0) != uft.find(i)); } } const real eps = 1e-10; const long p = 1_000_000_000 + 7; void main(){ auto s = readln().chomp(); if(s[0] == s[$-1]) { writeln(s.length % 2 == 0 ? "First": "Second"); } else { writeln(s.length % 2 == 1 ? "First": "Second"); } }
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; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } 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; bool ans; foreach (i; 1..10) { foreach (j; 1..10) { if (i*j == N) ans = true; } } writeln(ans ? "Yes" : "No"); stdout.flush(); debug readln(); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { string a, b, c; readV(a, b, c); writeln(a[$-1] == b[0] && b[$-1] == c[0] ? "YES" : "NO"); }
D
import std.stdio; import std.string; import std.format; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; import std.traits; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; const long INF = long.max/3; const long MOD = 10L^^9+7; void main() { writeln(readln.chomp.to!int.floor(3)); } // ---------------------------------------------- void scanln(Args...)(auto ref Args args) { import std.meta; template getFormat(T) { static if (isIntegral!T) { enum getFormat = "%d"; } else static if (isFloatingPoint!T) { enum getFormat = "%g"; } else static if (isSomeString!T || isSomeChar!T) { enum getFormat = "%s"; } else { static assert(false); } } enum string str = [staticMap!(getFormat, Args)].join(" ") ~ "\n"; // readf!str(args); mixin("str.readf(" ~ Args.length.iota.map!(i => "&args[%d]".format(i)).join(", ") ~ ");"); } void times(alias fun)(int n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } T ceil(T)(T x, T y) if (__traits(isIntegral, T)) { // `(x+y-1)/y` will only work for positive numbers ... T t = x / y; if (t * y < x) t++; return t; } T floor(T)(T x, T y) if (__traits(isIntegral, T)) { T t = x / y; if (t * y > x) t--; return t; } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { auto minElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto minimum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b < minimum) { element = a; minimum = b; } } return element; } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto maximum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b > maximum) { element = a; maximum = b; } } return element; } }
D
void main(){ auto ABCD = readLine!long(); foreach( i ; 0..102 ){ ABCD[2] -= ABCD[1]; if( ABCD[2] <= 0 ){ writeln("Yes");return; } ABCD[0] -= ABCD[3]; if ( ABCD[0] <= 0 ){ writeln("No");return; } } } import std.stdio, std.string, std.conv; import std.math, std.algorithm, std.array; import std.regex; T[] readLine( T = size_t )( string sp = " " ){ T[] ol; foreach( string elm ; readln().chomp().split(sp) ){ ol ~= elm.to!T(); } return ol; }
D
// Vicfred // https://atcoder.jp/contests/abc162/tasks/abc162_a // implementation import std.stdio; void main() { string n = readln; foreach(ch; n) { if(ch == '7') { "Yes".writeln; return; } } "No".writeln; }
D
import std.algorithm; import std.array; import std.ascii; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void log(A...)(A arg) { stderr.writeln(arg); } int size(T)(in T s) { return cast(int)s.length; } void main() { auto s = readln.chomp; long n = s.size; long ans = 0; foreach (i, c; s) { if (i < (n + 1) / 2) { // choose 'g' ans += (c == 'g' ? 0 : -1); } else { ans += (c == 'g' ? 1 : 0); } } writeln(ans); }
D
void main() { int[] tmp = readln.split.to!(int[]); int w = tmp[0], h = tmp[1], n = tmp[2]; int[] x = new int[n], y = new int[n], a = new int[n]; foreach (i; 0 .. n) { tmp = readln.split.to!(int[]); x[i] = tmp[0], y[i] = tmp[1], a[i] = tmp[2]; } int xmin, xmax = w, ymin, ymax = h; foreach (i; 0 .. n) { if (a[i] == 1) { xmin = max(xmin, x[i]); } else if (a[i] == 2) { xmax = min(xmax, x[i]); } else if (a[i] == 3) { ymin = max(ymin, y[i]); } else { ymax = min(ymax, y[i]); } } writeln(max(0, xmax-xmin) * max(0, ymax-ymin)); } 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
void main() { int n = readln.chomp.to!int; int k = readln.chomp.to!int; int m = k.log2.ceil.to!int; int ans = n - m > 0 ? 2 ^^ m + (n - m) * k : 2 ^^ n; ans.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, 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; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } 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 T = RDA; auto A = RDA; long ans = 1; foreach (i; 1..N-1) { if (T[i] != T[i-1]) { if (A[i] < T[i]) { ans = 0; break; } } else if (A[i] != A[i+1]) { if (T[i] < A[i]) { ans = 0; break; } } else ans.modm(min(T[i], A[i])); } if (N >= 2) { if (T[N-1] != T[N-2] && A[N-1] < T[N-1]) ans = 0; else if (A[0] != A[1] && T[0] < A[0]) ans = 0; } else { if (T[0] != A[0]) ans = 0; } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio; import std.algorithm; import std.range; import std.conv; import std.string; int f(int a, int k) { if (a < k) return 0; if (a%k == 0) return a/k; int c = a/k+1; int l = a/k*k; a -= ((a-l-1)/c+1)*c; return f(a, k); } void main() { int n = to!int(chomp(readln())); //int m = 100; //int[] dp = new int[m+1]; //foreach (i; 0..m+1) { // int[] s = new int[m+1]; // foreach (j; 1..i/n+1) { // s[dp[i-j]] = 1; // } // foreach (j; 0..m) { // if (s[j] == 1) continue; // dp[i] = j; // break; // } // writefln("%2d : %2d - %2d", i, dp[i], f(i,n)); //} //return; int x = 0; foreach (i; 0..n) { string[] input = split(readln()); int a = to!int(input[0]); int k = to!int(input[1]); x ^= f(a,k); } writeln(x ? "Takahashi" : "Aoki"); }
D
import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; void main(){ int n, x; { int[] tmp = readln.chomp.split.map!(to!int).array; n = tmp[0], x = tmp[1]; } ulong[] as = readln.chomp.split.map!(to!ulong).array; ulong ans = 0; for(int i = 1; i < as.length; i ++){ if(as[i - 1] + as[i] < x) continue; ulong r = as[i - 1] + as[i] - x; if(r <= as[i]){ ans += r, as[i] -= r; } else{ ans += as[i], as[i] = 0; ans += as[i - 1] - x, as[i - 1] = x; } } debug as.writeln; ans.writeln; }
D
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math; import core.stdc.stdio; // foreach, foreach_reverse, writeln void main() { int a, b, c, x, y; scanf("%d%d%d%d%d", &a, &b, &c, &x, &y); a = min(a,c*2); b = min(b,c*2); int ans = a*x + b*y; ans -= max(0, a+b-c*2) * min(x,y); writeln(ans); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } int[] digits(int n) { if (n == 0) return [0]; int[] ds; while (n > 0) { ds ~= n % 10; n /= 10; } ds.reverse(); return ds; } void main() { int n = readint; auto ds = digits(n); writeln(n % ds.sum == 0 ? "Yes" : "No"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto ab = readln.split.to!(int[]); auto A = ab[0]; auto B = ab[1]; int t = 1, r; while (t < B) { ++r; t = t - 1 + A; } writeln(r); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; void main() { string S = readln.chomp; long n; char prev = '#'; foreach(c;S) { if(c != prev) { prev = c; n++; } } writeln(n-1); }
D
import std.stdio, std.conv, std.string; import std.algorithm, std.array, std.container; import std.numeric, std.math; import core.bitop; T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; } string RDR()() { return readln.chomp; } long mod = pow(10, 9) + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD!long; auto M = RD!long; auto Q = RD!long; auto table = new long[][](N+1, N+1); foreach (i; 0..M) { auto L = RD!long; auto R = RD!long; ++table[L][R]; } auto dp = new long[][](N+2, N+2); foreach_reverse (i; 1..N+1) { foreach (j; i..N+1) { dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1] + table[i][j]; } } string[] ans; foreach (i; 0..Q) { auto p = RD!long; auto q = RD!long; ans ~= to!string(dp[p][q]); } foreach (e; ans) writeln(e); stdout.flush(); }
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 a, b; scan(a, b); foreach (x ; 0 .. 100000) { auto p8 = x * 8 / 100; auto p10 = x * 10 / 100; if (p8 == a && p10 == b) { writeln(x); return; } } writeln(-1); } 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; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { auto s = read!string; auto t = read!string; auto rs = s.dup; rs.reverse(); writeln(rs == t ? "YES" : "NO"); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto x = readln.chomp.split.to!(int[]); if (x[0]+x[1]<x[2] || x[0] > x[2]) { writeln("NO"); } else { writeln("YES"); } }
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; bool isInside(long xp1, long yp1, long xp2, long yp2, long xp3, long yp3, long x, long y) { xp1 -= x; xp2 -= x; xp3 -= x; yp1 -= y; yp2 -= y; yp3 -= y; auto s1 = xp1*yp2 - xp2*yp1; auto s2 = xp2*yp3 - xp3*yp2; auto s3 = xp3*yp1 - xp1*yp3; return s1*s2 > 0 && s2*s3 > 0 && s3*s1 > 0; } void main(){ auto n = readLong(); foreach(i; iota(n)) { auto line = readLongs(); writeln(isInside(line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]) ^ isInside(line[0], line[1], line[2], line[3], line[4], line[5], line[8], line[9]) ? "OK": "NG"); } }
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 N = RD; auto D = RD; auto X = new long[](N); auto Y = new long[](N); foreach (i; 0..N) { X[i] = RD; Y[i] = RD; } long ans; foreach (i; 0..N) { if(X[i]^^2 + Y[i]^^2 <= D^^2) ++ans; } writeln(ans); stdout.flush; debug readln; }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons, std.functional; alias FactorizeResult = ulong[101]; void main() { const N = readln.chomp.to!int; FactorizeResult table; foreach (i; 1..N+1) { table[] += primeFactorize(i)[]; } ulong func(ulong i, int r) { if (r > 75) return 0; if (i == 101) { return r == 75 ? 1 : 0; } ulong result = 0; foreach (x; [0,2,4,14,24,74].filter!(y => y <= table[i])) { result += memoize!func(i+1,r * (x+1)); } return result; } writeln(func(0,1)); } FactorizeResult primeFactorize(int x) { if (x == 1) { FactorizeResult result; return result; } int y = 2; while (true) { if (x % y == 0) { auto po = memoize!primeFactorize(x/y); po[y]++; return po; } else { y++; } } }
D
void main() { long n, a, b; rdVals(n, a, b); long cnt; foreach (i; 0 .. n) { long t = rdElem; if (t < a || b <= t) ++cnt; } cnt.writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; enum double eps = 1.0e-9; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto N = readln.chomp.to!int; auto C = new int[](N); auto D = new long[](N); foreach (i; 0..N) { auto s = readln.split.map!(to!long); C[i] = s[0].to!int; D[i] = s[1]; } long ans = 0; int tmp = 0; int[] amari; foreach (i; 0..N) { if (D[i] <= 1) { continue; } tmp = 0; while (D[i] > 1) { ans += D[i] / 2; if (D[i] % 2) { amari ~= C[i]; } if (D[i] > 1) { if (C[i] >= 5) { ans += D[i] / 2; C[i] = C[i] * 2 - 9; } else { C[i] *= 2; } } if (D[i] > 1) { tmp = C[i]; } D[i] /= 2; } foreach (a; amari) { if (tmp + a >= 10) { ans += 2; tmp = (tmp + a) - 9; } else { ans += 1; tmp += a; } } amari = []; C[i] = tmp; } tmp = C.front; foreach (a; C[1..$]) { if (tmp + a >= 10) { ans += 2; tmp = (tmp + a) - 9; } else { ans += 1; tmp += a; } } ans.writeln; }
D