code
stringlengths
4
1.01M
language
stringclasses
2 values
// Vicfred // https://atcoder.jp/contests/dp/tasks/dp_a // dynamic programming import std.algorithm; import std.array; import std.conv; import std.math; import std.stdio; import std.string; void main() { const long n = readln.chomp.to!long; const long[] h = 0~readln.split.map!(to!long).array; long[] dp = new long[n+1]; dp[1] = 0; dp[2] = abs(h[2] - h[1]); for(int i = 3; i <= n; ++i) dp[i] = min(dp[i-1] + abs(h[i] - h[i - 1]), dp[i-2] + abs(h[i] - h[i - 2])); dp[n].writeln; }
D
import std.stdio; import std.conv; import std.string; void main() { char[] buf; int a; stdin.readln(buf); a = to!(int)(chomp(buf)); a = a * a * a; writeln(a); }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random; import std.typecons, std.functional, std.traits,std.concurrency; import std.algorithm, std.container; import core.bitop, core.time, core.memory; import std.bitmanip; import std.regex; enum INF = long.max/3; enum MOD = 10L^^9+7; void main() { auto A = scanElem; auto B = scanElem; auto C = scanElem; if(A+B>=C)writeln("Yes");else writeln("No"); } T[] scanArray(T = long)() { static char[] scanBuf; readln(scanBuf); return scanBuf.split.to!(T[]); } dchar scanChar() { import core.stdc.stdlib; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } return cast(dchar)c; } T scanElem(T = long)() { import core.stdc.stdlib; static auto scanBuf = appender!(char[])([]); scanBuf.clear; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { scanBuf ~= cast(char) c; c = getchar; } return scanBuf.data.to!T; } dchar[] scanString(){ return scanElem!(dchar[]); }
D
import std.algorithm, std.array, std.conv, std.range, std.stdio, std.string; void main() { auto x = readln.chomp.to!int; while (x.notPrime) x += 1; x.writeln; } bool notPrime(int x) { if (x < 4) return x < 2; int d = 2; while (d*d <= x) { if (!(x % d)) return true; d += 1; } return false; }
D
import std.stdio; import std.string; import std.array; // split import std.conv; // to import std.math; void main() { string[] input = split(readln()); int x = to!int(input[0]); int a = to!int(input[1]); int b = to!int(input[2]); if(abs(x - a) < abs(x - b)){ writeln("A"); } else { writeln("B"); } }
D
//dlang template---{{{ import std.stdio; import std.conv; import std.string; import std.array; import std.algorithm; // MIT-License https://github.com/kurokoji/nephele class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } //}}} void main() { Scanner sc = new Scanner; int X, Y; sc.scan(X, Y); writeln(X + Y / 2); }
D
/+ dub.sdl: name "D" dependency "dunkelheit" version=">=0.9.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; int main() { Scanner sc = new Scanner(stdin); scope(exit) assert(!sc.hasNext); int n, m; sc.read(n, m); int[] perm; sc.read(perm); perm[] -= 1; int[][] g = new int[][n]; foreach (i; 0..m) { int a, b; sc.read(a, b); a--; b--; g[a] ~= b; g[b] ~= a; } int[] v; bool[] vis = new bool[n]; void dfs(int p) { if (vis[p]) return; v ~= p; vis[p] = true; foreach (d; g[p]) { dfs(d); } } int ans = 0; int[] buf = new int[n]; foreach (i; 0..n) { if (vis[i]) continue; v = []; dfs(i); int id = i + 114514; foreach (d; v) { buf[d] = id; } foreach (d; v) { int e = perm[d]; if (buf[e] == id) ans++; } } writeln(ans); return 0; } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */ // module dkh.scanner; // import dkh.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(Args...)(auto ref Args args) { import std.exception; static if (args.length != 0) { enforce(readSingle(args[0])); read(args[1..$]); } } bool hasNext() { return succ(); } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std.stdio; import std.string; import std.format; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.concurrency; import std.traits; import std.uni; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; enum long INF = long.max/5; enum long MOD = 10L^^9+7; void main() { writeln(6 - 2.rep!(() => readln.chomp.to!long).sum); } // ---------------------------------------------- void times(alias fun)(long n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(long n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { // `(x+y-1)/y` will only work for positive numbers ... T t = x / y; if (y > 0 && t * y < x) t++; if (y < 0 && t * y > x) t++; return t; } T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { T t = x / y; if (y > 0 && t * y > x) t--; if (y < 0 && t * y < x) t--; return t; } ref T ch(alias fun, T, S...)(ref T lhs, S rhs) { return lhs = fun(lhs, rhs); } unittest { long x = 1000; x.ch!min(2000); assert(x == 1000); x.ch!min(3, 2, 1); assert(x == 1); x.ch!max(100).ch!min(1000); // clamp assert(x == 100); x.ch!max(0).ch!min(10); // clamp assert(x == 10); } mixin template Constructor() { import std.traits : FieldNameTuple; this(Args...)(Args args) { // static foreach(i, v; args) { foreach(i, v; args) { mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v; } } } void scanln(Args...)(auto ref Args args) { enum sep = " "; enum n = Args.length; enum fmt = n.rep!(()=>"%s").join(sep) ~ "\n"; static if (__VERSION__ >= 2071) { readf!fmt(args); } else { enum argsTemp = n.iota.map!( i => "&args[%d]".format(i) ).join(", "); mixin( "readf(fmt, " ~ argsTemp ~ ");" ); } } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // popcnt with ulongs was added in D 2.071.0 static if (__VERSION__ < 2071) { ulong popcnt(ulong x) { x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L); x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L); x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL); x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL); x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL); x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL); return x; } }
D
import std.stdio, std.algorithm, std.array, std.range, std.string, std.conv, std.uni; void main() { auto s = readln.strip; auto yy = s[0..2].to!int; auto mm = s[2..$].to!int; if (1 <= mm && mm <= 12) { if (1 <= yy && yy <= 12) { writeln("AMBIGUOUS"); } else { writeln("YYMM"); } } else { if (1 <= yy && yy <= 12) { writeln("MMYY"); } else { writeln("NA"); } } }
D
import std.stdio, std.string, std.conv; int main(string[] argv) { auto s = split(readln()); int x = to!int(s[0]), y = to!int(s[1]); writeln(x * y, ' ', (x + y) * 2); return 0; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; import std.math; void main() { auto s = readln.chomp; auto hi = new int[](s.length + 1); foreach (i, c; s) hi[i + 1] = hi[i] + c.predSwitch('\\', -1, '_', 0, '/', 1); auto maxH = hi.reduce!(max); auto fs1 = hi.findSplitBefore([maxH]), hiL = fs1[0], hiMR = fs1[1]; if (!hiL.empty) hiL ~= maxH; hiMR.reverse(); auto fs2 = hiMR.findSplitBefore([maxH]), hiR = fs2[0], hiM = fs2[1]; if (!hiR.empty) hiR ~= maxH; auto aiL = hiL.calcAreas; auto aiM = hiM.calcAreas; aiM.reverse(); auto aiR = hiR.calcAreas; aiR.reverse(); auto ai = aiL ~ aiM ~ aiR; writeln(ai.sum); write(ai.length); ai.each!(a => write(" ", a)); writeln; } int[] calcAreas(int[] hi) { int[] areas; int maxH = int.min, area = 0; foreach (i, h; hi) { if (h < maxH) { area += 2 * (maxH - h) + (hi[i - 1] - h).sgn; } else if (h == maxH) { if (area > 0) { areas ~= area / 2; area = 0; } } else { maxH = h; } } return areas; }
D
import std.stdio, std.string, std.conv; void main() { auto n = readln.chomp; if((n[0] == n[1] && n[1] == n[2])||(n[1] == n[2] && n[2] == n[3])){ writeln("Yes"); } else { writeln("No"); } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; alias Pair = Tuple!(int, "a", int, "b"); void main() { int N; scan(N); auto ans = (N + 110) / 111 * 111; 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; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto s = readln.chomp; int a, z; foreach (i, e; s) { if (e == 'A') { a = i.to!int; break; } } foreach_reverse(i, e; s) { if (e == 'Z') { z = i.to!int; break; } } writeln(z - a + 1); }
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); foreach (i ; 0 .. n) { int yi, mi, di; scan(yi, mi, di); writeln(mill(yi, mi, di)); } } int mill(int y, int m, int d) { int res; if (y % 3 == 0) { res += 10 * 20 - ((m - 1) * 20 + d) + 1; } else { res += 5*19 + 5*20 - ((m - 1) / 2 * 39 + ((m-1)&1)*20 + d) + 1; } foreach (yy ; y + 1 .. 1000) { if (yy % 3 == 0) res += 10*20; else res += 5*39; } return res; } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.range; import std.string; import std.algorithm; import std.conv; void main() { ulong x = readln.chomp.to!ulong; (x/11*2 + (x%11 <= 6 ? 1 : 2) + (x%11 == 0 ? -1 : 0)).writeln; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto q = readln.chomp.to!size_t; foreach (_; q.iota) { auto x = readln.chomp, xl = x.length; auto y = readln.chomp, yl = y.length; auto memo = new int[][](xl + 1, yl + 1); foreach (i; 1..xl+1) foreach (j; 1..yl+1) if (x[i-1] == y[j-1]) memo[i][j] = memo[i-1][j-1] + 1; else memo[i][j] = max(memo[i-1][j], memo[i][j-1]); writeln(memo[xl][yl]); } }
D
import std.algorithm, std.string, std.range, std.stdio, std.conv; bool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else { return false; } } void chChar(ref string base, size_t idx, char c) { char[] tmp = base.to!(char[]); tmp[idx] = c; base = cast(string)tmp; } void main() { string Sd = readln.chomp.to!(char[]); string T = readln.chomp.to!(char[]); string ans; foreach (i, s; Sd) { string tmp = Sd; bool match = true; foreach (j, t; T) { if (i + j >= Sd.length) { match = false; break; } if (Sd[i + j] == T[j] || Sd[i + j] == '?') { chChar(tmp, i + j, T[j]); } else { match = false; break; } } if (!match) { continue; } foreach (j, c; tmp) { if (c == '?') { chChar(tmp, j, 'a'); } } if (ans.empty) { ans = tmp; } else { chmin(ans, tmp); } } if (ans.empty) { writeln("UNRESTORABLE"); } else { writeln(ans); } }
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 cnt; foreach (i ; 0 .. n) { string s = readln.chomp; if (s == "A") cnt++; else { if (cnt <= 0) { writeln("NO"); return; } else { cnt--; } } } writeln(cnt == 0 ? "YES" : "NO"); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { string s = readln.chomp; writeln(s.startsWith("YAKI") ? "Yes" : "No"); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; alias sread = () => readln.chomp(); void main() { auto n = lread(); auto s = sread(); s.uniq.array.length.writeln(); } 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; } }
D
void main(){ int[] input; foreach(i; 0..3)input ~= _scan(); ( (input[0]-input[1])%input[2] ).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 std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void main() { auto T = RD!int; auto ans = new long[](T); foreach (ti; 0..T) { auto n = RD; auto d = RD; long last = long.max, x; foreach (i; 1..d+2) { auto y = d / i + (d % i != 0 ? 1 : 0); if (y >= last) { x = i-1; break; } last = y; } ans[ti] = d / x + (d % x != 0 ? 1 : 0) + x - 1 <= n; } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush; debug readln; }
D
import std.conv, std.stdio; import std.algorithm, std.array, std.range, std.string; void main() { auto buf = readln.chomp.split.to!(ulong[]); buf[0].solve(buf[1]).writeln; } ulong solve(in ulong a, in ulong b) { assert (a <= b); if (a == b) return a; if (a.even && b.odd) return (~(b-a)>>1) & 1; if (a.odd) return a ^ solve(a + 1, b); if (b.even) return solve(a, b - 1) ^ b; assert (false); } bool even(in ulong a) { return !(a & 1); } bool odd(in ulong a) { return a & 1; }
D
import std.stdio; import std.ascii; import std.conv; import std.string; import std.algorithm; import std.range; import std.functional; import std.math; import core.bitop; void main() { auto ab = readln.chomp.split().to!(long[]); long a = ab[0]; long b = ab[1]; writeln(a * b - (a + b - 1)); }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { while (1) { int n; scan(n); if (!n) return; auto ans = cntp(n); writeln(ans); } } int cntp(int n) { auto s = new bool[](2*n + 1); s[] = 1; s[0] = s[1] = 0; for (int p = 2; p*p <= 2*n; p++) { if (s[p]) { for (int q = p*p; q <= 2*n; q += p) { s[q] = 0; } } } int ans; foreach (i ; n + 1 .. 2*n + 1) { ans += s[i]; } return ans; } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.ascii; void main() { auto x = readln.chomp.split; auto a = x[0].to!int; auto b = x[2].to!int; if (x[1] == "+") { writeln(a+b); } else { writeln(a-b); } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { int n = readint; int m = cast(int)sqrt(1.0 * n); writeln(m * m); }
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 N = readln.chomp.to!int; auto S = readln.chomp; auto cnt = new int[](2); foreach (s; S) cnt[s=='8'] += 1; int ans = 0; while (cnt[1]) { cnt[1] -= 1; int x = min(10, cnt[0]); cnt[0] -= x; int y = min(10-x, cnt[1]); cnt[1] -= y; if (x + y < 10) break; ans += 1; } ans.writeln; }
D
import std.stdio; import std.uni; import std.conv; import std.container; import std.functional; import std.algorithm; import std.array; import std.typecons; import std.range; import std.numeric; import std.traits; void main(string[] args) { auto t = next!int; foreach(tc; 0 .. t) { auto a = next!long; auto b = next!long; auto c = next!long; auto maxside = max(a, b, c); writeln(maxside); } } static this() { popChar; } struct Any(T) { static auto read() { static if (is(T == char)) { auto res = frontChar; popChar; return res; } } } struct Unsigned(T) { auto read() { auto res = T(0); while(!frontChar.isDigit) popChar; do { res = res * T(10) + T(frontChar - '0'); popChar; } while(frontChar.isDigit); return res; } } struct Matrix(T, alias rows, alias cols) { T[][] _cell; alias _cell this; // Ring constructor this(int n) { final switch(n) { case 0: _cell = new T[][](rows, cols); break; case 1: debug assert(rows == cols); _cell = new T[][](rows, cols); foreach(i; 0 .. rows) _cell[i][i] = T(1); break; } } // Copy constructor this(Matrix!(T, rows, cols) other) { _cell = new T[][](rows, cols); foreach(i; 0 .. rows) _cell[i][] = other[i][]; } auto opBinary(string op)(Matrix!(T, rows, cols) other) if (op == "+" || op == "-") { auto res = Matrix!(T, rows, cols)(0); foreach(i; 0 .. rows) foreach(j; 0 .. cols) res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]}); return res; } auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other) if (op == "*") { debug assert(cols == otherRows); auto res = Matrix!(T, rows, otherCols)(0); foreach(i; 0 .. rows) foreach(k; 0 .. cols) foreach(j; 0 .. otherCols) res[i][j] += this[i][k] * other[k][j]; return res; } ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other) { static if (op == "*") { debug assert(rows == cols); alias n = rows; auto res = Matrix!(T, n, n)(0); T factor; foreach(i; 0 .. n) foreach(k; 0 .. n) foreach(j; 0 .. n) res[i][j] += this[i][k] * other[k][j]; foreach(i; 0 .. n) this[i][] = res[i][]; } else { foreach(i; 0 .. rows) foreach(j; 0 .. cols) mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];}); } return this; } mixin powMethod; } struct Mod(T, alias mod) { alias This = Mod!(T, mod); T _rep; this(T t) { _rep = t % mod; if (_rep < 0) _rep += mod; } static auto plain(T t) { auto res = Mod!(T, mod)(); res._rep = t; return res; } static auto fromPositive(T t) { pragma(inline, true); return Mod!(T, mod).plain(t % mod); } static auto fromNegative(T t) { pragma(inline, true); return Mod!(T, mod).plain(t % mod + mod); } string toString() { return to!string(_rep); } debug invariant { assert(_rep >= 0 && _rep < mod); } auto opBinary(string op)(This b) { static if (op == "+") { T resrep = _rep + b._rep; if (resrep >= mod) resrep -= mod; return This.plain(resrep); } else static if (op == "-") { T resrep = _rep - b._rep; if (resrep < 0) resrep += mod; return This.plain(resrep); } else static if (op == "*") { return This.fromPositive(_rep * b._rep); } } auto opOpAssign(string op)(This b) { mixin(q{_rep }, text(op, "="), q{b._rep;}); static if (op == "+") { if (_rep >= mod) _rep -= mod; } else static if (op == "-") { if (_rep < 0) _rep += mod; } else static if (op == "*") { _rep %= mod; } return this; } auto opBinary(string op)(long exp) if (op == "^^") { return pow(exp); } mixin powMethod; } mixin template powMethod() { auto pow(this ThisType)(long exp) { auto res = ThisType(1); auto pow = ThisType(this); while(exp) { if (exp & 1) res *= pow; pow *= pow; exp >>= 1; } return res; } } // INPUT char frontChar; void popChar() { import core.stdc.stdio; frontChar = cast(char) getchar; if (feof(stdin)) frontChar = ' '; } auto makeArray(T, S...)(S s) { enum brackets = () { string res; foreach(i; 0 .. s.length) res ~= "[]"; return res; } (); mixin(q{alias Type = T}, brackets, q{;}); return new Type(s); } template isNumberLike(T) { enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}())); } void popWhite() { import std.ascii; while (frontChar.isWhite) popChar; } void print(T...)(T t) { static foreach(ti; t) { static if (is(typeof(ti) == string)) { write(ti, " "); } else static if (isArray!(typeof(ti))) { foreach(e; ti) print(e); } else static if (isTuple!(typeof(ti))) { static foreach(i; ti.length) writesp(ti[i]); } else { write(ti, " "); } } } void println(T...)(T t) { static if (t.length == 0) writeln; static foreach(ti; t) { print(ti); writeln; } } auto next(alias T, S...)(S s) { pragma(inline, true); static if (s.length > 0) { auto res = makeArray!(typeof(next!T()))(s); S t; enum loops = () { string res; foreach(i; 0 .. s.length) { auto ti = text(q{t[}, i, q{]}); res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)}); } return res; } (); enum indexing = () { string res = "res"; foreach(i; 0 .. s.length) res ~= text(q{[t[}, i, q{]]}); return res; } (); mixin(loops, indexing, q{ = next!T;}); return res; } else { static if (isNumberLike!T) { import std.ascii: isDigit; T res; while(frontChar.isWhite) popChar; T multiplier = T(1); if (frontChar == '-') { multiplier = T(-1); popChar; } else if (frontChar == '+') { popChar; } debug assert(frontChar.isDigit); do { res = res * T(10) + T(frontChar - '0'); popChar; } while(frontChar.isDigit); return multiplier * res; } else static if (is(T == string)) { import std.ascii: isWhite; string res; while(frontChar.isWhite) popChar; while(!frontChar.isWhite) { res ~= frontChar; popChar; } return res; } else static if (is(T == char)) { while(frontChar.isWhite) popChar; auto res = frontChar; popChar; return res; } else { return T.read; } } }
D
// Cheese-Cracker: cheese-cracker.github.io void solve(){ long n = scan; for(int i = 2; i < 2 + n; ++i){ write(i, " "); } writeln; } void main(){ long tests = scan; // Toggle! while(tests--) solve; } /************** ***** That's All Folks! ***** **************/ import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math; string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;} T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; } void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } } alias ll = long, tup = Tuple!(long, "x", long, "y");
D
import std.stdio; import std.range; import std.array; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.container; import std.typecons; import std.random; import std.csv; import std.regex; import std.math; import core.time; import std.ascii; import std.digest.sha; import std.outbuffer; void main() { enum N = 14; long[] a = readln.chomp.split.map!(to!long).array; long ans = 0; foreach (i, v; a) { // simulate long[] b = a.dup; b[i] = 0; foreach (j; 0..N) { b[cast(uint)((i + j + 1) % N)] += v / N; } foreach (j; 0 .. v % N) { ++b[cast(uint)((i + j + 1) % N)]; } long sub = 0; foreach (w; b) { if (w % 2 == 0) { sub += w; } } ans = max(ans, sub); } ans.writeln; } void tie(R, Args...)(R arr, ref Args args) if (isRandomAccessRange!R || isArray!R) in { assert (arr.length == args.length); } body { foreach (i, ref v; args) { alias T = typeof(v); v = arr[i].to!T; } } void verbose(Args...)(in ref Args args) { stderr.write("["); foreach (i, ref v; args) { if (i) stderr.write(", "); stderr.write(v); } stderr.writeln("]"); }
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; void main() { auto n = readln.strip.to!int; auto s = readln.strip[0 .. n]; auto psteps = (n - 11) >> 1; auto t = s[0 .. n - 10]; int e; foreach (i; 0 .. t.length) { if (t[i] == '8') { ++e; } } writeln (e > psteps ? "YES" : "NO"); }
D
import std.stdio; import std.ascii; import std.algorithm; import core.stdc.stdio; int main() { int t = readInt!int; while (t--) { int n = readInt!int; auto a = new int[](2 * n); foreach(ref ai; a) ai = readInt!int; sort(a); debug writeln(a); auto low = a[0 .. n]; auto high = a[n .. $]; foreach(i; 0 .. n) { write(low[i], " ", high[i], " "); } writeln; } return 0; } /* INPUT ROUTINES */ int currChar; static this() { currChar = getchar(); } char topChar() { return cast(char) currChar; } void popChar() { currChar = getchar(); } auto readInt(T)() { T num = 0; int sgn = 0; while (topChar.isWhite) popChar; while (topChar == '-' || topChar == '+') { sgn ^= (topChar == '-'); popChar; } while (topChar.isDigit) { num *= 10; num += (topChar - '0'); popChar; } if (sgn) return -num; return num; } string readString() { string res = ""; while (topChar.isWhite) popChar; while (!topChar.isWhite) { res ~= topChar; popChar; } return res; }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}} void main() { int h, w; readV(h, w); string[] c; readC(h, c); foreach (ci; c) { writeln(ci); writeln(ci); } }
D
import std.array; import std.conv; import std.stdio; import std.math; void main() { readln.split[0].to!int.pow(2).writeln; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; auto s=readln.chomp.to!(char[]); if(s.count("AC")) writeln("Yes"); else writeln("No"); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm; import std.math :abs; void main() { int n; scan(n); int x, y, t; foreach (i ; 0 .. n) { int ti, xi, yi; scan(ti, xi, yi); int d = abs(xi - x) + abs(yi - y); int e = ((x + y) % 2) ^ ((xi + yi) % 2); if ((ti - t) < d || (ti - t) % 2 != e) { writeln("No"); return; } x = xi, y = yi, t = ti; } writeln("Yes"); return; } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto M = RD; auto b = new long[](N); foreach (i; 0..M) { auto a = RD-1; b[a] = 1; } auto dp = new long[](N+1); dp[0] = 1; if (b[0] == 0) dp[1] = 1; foreach (i; 1..N) { if (b[i] == 1) continue; moda(dp[i+1], dp[i]); moda(dp[i+1], dp[i-1]); } debug writeln(dp); writeln(dp[N]); stdout.flush(); debug readln(); }
D
import std.stdio, std.string, std.conv; void main() { writeln(3 * readln.chomp.to!int ^^ 2); }
D
import std.stdio, std.algorithm, std.string, std.range, std.array,std.conv; void main() { const tmp = readln.split.map!(to!int).array; writeln(tmp[0] < tmp[1] ? 0:10); }
D
import std.stdio; import std.conv; import std.string; import std.range; void main(){ // input int[] input; input.length = 4; foreach(ref i; input) { i = to!int(readln().chomp()); } immutable int five_hund = input[0]; immutable int hund = input[1]; immutable int fifty = input[2]; immutable int sum_num = input[3]; writeln(get_all_paterns(five_hund, hund, fifty, sum_num)); } int get_all_paterns(int five_hund, int hund, int fifty, int sum_num){ int result; foreach(fh_num; iota(0,five_hund+1)) { foreach(h_num; iota(0, hund+1)) { foreach(f_num; iota(0, fifty+1)) { if(500 * fh_num + 100 * h_num + 50 * f_num == sum_num){ result++; } } } } return result; }
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 xy = reads!string; auto x = xy[0], y = xy[1]; if (x == y) writeln('='); else if (x < y) writeln('<'); else writeln('>'); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto abc = readln.split.to!(int[]); auto A = abc[0]; auto B = abc[1]; auto C = abc[2]; writeln(max(0, C - A + B)); }
D
import std.stdio, std.string, std.conv; void main() { while(1){ auto n = readln.chomp.to!int; int cnt; if(n==0) break; while(n!=1){ if(n%2==0) n/=2; else n = n*3+1; cnt++; } cnt.writeln; } }
D
import std.stdio; import std.string; import std.conv; int main() { int i = 0; string str; while ((str = readln().chomp()) != "0") { i++; writeln("Case ", i, ": ", str); } return 0; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.numeric; import std.stdio; import std.string; void main() { int n = to!int(readln().chomp()); int[] a = readln().split().map!(a => to!int(a)).array; int m = int.min; for (int l = 0; l < a.length; ++l) { for (int r = l + 1; r < a.length; ++r) { m = max(m, abs(a[l] - a[r])); } } writeln(m); }
D
import std; void main(){ long a = readln.chomp.to!long - 1; char[] ans; while(a >= 0){ ans ~= 'a' + (a % 26); a /= 26, a -= 1; } ans.reverse.writeln; }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { const N = readln.chomp.to!long; const S = readln.chomp; auto whiteCount = new long[N+1]; auto blackCount = new long[N+1]; foreach (i; 0..N) { whiteCount[i+1] = S[i] == '.' ? 1 : 0; blackCount[i+1] = S[i] == '#' ? 1 : 0; } foreach (i; 0..N) { whiteCount[i+1] += whiteCount[i]; blackCount[i+1] += blackCount[i]; } // 0..i番目までが全部白、i..n番目までが全部黒にする iota(N+1).map!(i => blackCount[i] + whiteCount[N] - whiteCount[i]).reduce!min.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; scan(n); auto p = iota(n).map!(i => readln.chomp.to!int - 1).array; auto a = new int[](n); foreach (i ; 0 .. n) { if (p[i] == 0) { a[0] = 1; continue; } a[p[i]] = a[p[i] - 1] + 1; } debug { writeln(a); } auto ans = n - a.reduce!max; writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.string; import std.format; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.concurrency; import std.traits; import std.uni; import std.regex; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; enum long INF = long.max/5; void main() { writeln(readln.chomp=="ABC"?"ARC":"ABC"); } // ---------------------------------------------- void times(alias fun)(long n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(long n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { // `(x+y-1)/y` will only work for positive numbers ... T t = x / y; if (y > 0 && t * y < x) t++; if (y < 0 && t * y > x) t++; return t; } T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { T t = x / y; if (y > 0 && t * y > x) t--; if (y < 0 && t * y < x) t--; return t; } ref T ch(alias fun, T, S...)(ref T lhs, S rhs) { return lhs = fun(lhs, rhs); } unittest { long x = 1000; x.ch!min(2000); assert(x == 1000); x.ch!min(3, 2, 1); assert(x == 1); x.ch!max(100).ch!min(1000); // clamp assert(x == 100); x.ch!max(0).ch!min(10); // clamp assert(x == 10); } mixin template Constructor() { import std.traits : FieldNameTuple; this(Args...)(Args args) { // static foreach(i, v; args) { foreach(i, v; args) { mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v; } } } template scanln(Args...) { enum sep = " "; enum n = (){ long n = 0; foreach(Arg; Args) { static if (is(Arg == class) || is(Arg == struct) || is(Arg == union)) { n += Fields!Arg.length; } else { n++; } } return n; }(); enum fmt = n.rep!(()=>"%s").join(sep); enum argsString = (){ string[] xs = []; foreach(i, Arg; Args) { static if (is(Arg == class) || is(Arg == struct) || is(Arg == union)) { foreach(T; FieldNameTuple!Arg) { xs ~= "&args[%d].%s".format(i, T); } } else { xs ~= "&args[%d]".format(i); } } return xs.join(", "); }(); void scanln(auto ref Args args) { string line = readln.chomp; static if (__VERSION__ >= 2074) { mixin( "line.formattedRead!fmt(%s);".format(argsString) ); } else { mixin( "line.formattedRead(fmt, %s);".format(argsString) ); } } } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // popcnt with ulongs was added in D 2.071.0 static if (__VERSION__ < 2071) { ulong popcnt(ulong x) { x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L); x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L); x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL); x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL); x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL); x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL); return x; } }
D
import core.bitop; import std.algorithm; import std.array; import std.conv; import std.range; import std.stdio; import std.string; immutable int mod = 10 ^^ 9 + 7; immutable int limit = 1003; void add (ref int a, int b) { a = (a + b) % mod; } void main () { auto h = new int [limit]; h[1] = 0; foreach (i; 2..limit) { h[i] = h[popcnt (i)] + 1; } string s; while ((s = readln.strip) != "") { auto k = readln.strip.to !(int); if (k == 0) { writeln (1); continue; } auto n = s.length.to !(int); auto f = new int [2] [] [] (n + 1, n + 1); f[0][0][0] = 1; foreach (i; 0..n) { int lim = (s[i] == '1'); foreach (j; 0..i + 1) { foreach (prevLess; 0..2) { foreach (cur; 0..2) { if ((cur > lim) && !prevLess) { continue; } int nextLess = prevLess || (cur < lim); add (f[i + 1][j + cur] [nextLess], f[i][j] [prevLess]); } } } } add (f[n][1][1], mod - 1); int res = 0; foreach (j; 1..n + 1) { if (h[j] == k - 1) { add (res, f[n][j][0]); add (res, f[n][j][1]); } } writeln (res); } }
D
import std.stdio; import std.range; import std.array; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.container; import std.typecons; import std.random; import std.csv; import std.regex; import std.math; import core.time; import std.ascii; import std.digest.sha; import std.outbuffer; import std.numeric; import std.bigint; void main() { int n, m; readln.chomp.split.tie(n, m); char[][] field = new char[][](n); foreach (ref a; field) { a = readln.chomp.dup; } int[] cnt = new int[](m); foreach (a; field) { foreach (i, v; a) { if (v == '0') { continue; } cnt[i]++; } } debug verbose(cnt); bool ok = false; foreach (i; 0..n) { // ignore i-th bool flag = true; foreach (j; 0..m) { if (field[i][j] == '0') { continue; } flag &= cnt[j] > 1; } ok |= flag; } writeln = ok ? "YES" : "NO"; } void tie(R, Args...)(R arr, ref Args args) if (isRandomAccessRange!R || isArray!R) in { assert (arr.length == args.length); } body { foreach (i, ref v; args) { alias T = typeof(v); v = arr[i].to!T; } } void verbose(Args...)(in Args args) { stderr.write("["); foreach (i, ref v; args) { if (i) stderr.write(", "); stderr.write(v); } stderr.writeln("]"); }
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.random; import std.datetime.systime : Clock; class InputReader { private: ubyte[] p; ubyte[] buffer; size_t cur; public: this () { buffer = uninitializedArray!(ubyte[])(16<<20); p = stdin.rawRead (buffer); } final ubyte skipByte (ubyte lo) { while (true) { auto a = p[cur .. $]; auto r = a.find! (c => c >= lo); if (!r.empty) { cur += a.length - r.length; return p[cur++]; } p = stdin.rawRead (buffer); cur = 0; if (p.empty) return 0; } } final ubyte nextByte () { if (cur < p.length) { return p[cur++]; } p = stdin.rawRead (buffer); if (p.empty) return 0; cur = 1; return p[0]; } template next(T) if (isSigned!T) { final T next () { T res; ubyte b = skipByte (45); if (b == 45) { while (true) { b = nextByte (); if (b < 48 || b >= 58) { return res; } res = res * 10 - (b - 48); } } else { res = b - 48; while (true) { b = nextByte (); if (b < 48 || b >= 58) { return res; } res = res * 10 + (b - 48); } } } } template next(T) if (isUnsigned!T) { final T next () { T res = skipByte (48) - 48; while (true) { ubyte b = nextByte (); if (b < 48 || b >= 58) { break; } res = res * 10 + (b - 48); } return res; } } final T[] nextA(T) (int n) { auto a = uninitializedArray!(T[]) (n); foreach (i; 0 .. n) { a[i] = next!T; } return a; } } void main() { auto r = new InputReader; immutable n = r.next!int; immutable k = r.next!int; immutable a = r.nextA!int(k).idup; auto c = new int[n + 1]; auto first = new int[n+1]; first[] = int.max; auto last = new int[n+1]; last[] = int.min; foreach_reverse (i, j; a) { first[j] = i.to!int; ++c[j]; } foreach (i, j; a) { last[j] = i.to!int; } int res; foreach (i; 1 .. n + 1) { foreach (delta; -1 .. 2) { int j = i + delta; if (j >= 1 && j <= n) { if (first[i] > last[j]) ++res; } } } writeln (res); }
D
import std.stdio; import std.conv; import std.string; import std.algorithm; void main() { int n; scanf("%d\n", &n); auto hs = readln.chomp.split.to!(int[]); int m = hs[0]; foreach(h; hs){ if (m - h > 1) { write("No"); return; } m = max(m, h); } write("Yes"); }
D
import std.stdio; import std.conv; import std.string; void main() { string s = readln().chomp; int n = s.to!(int); if (s[$ - 1] == '0' || s[$ - 1] == '2' || s[$ - 1] == '4' || s[$ - 1] == '6' || s[$ - 1] == '8' ) { writeln(s); } else { writeln(n*2); } }
D
import std.stdio, std.conv, std.string, std.math; void main(){ auto ip = readln.split.to!(int[]); if(ip[2] >= ip[0] && ip[2] <= ip[1]){ writeln("Yes"); } else { writeln("No"); } }
D
import std.algorithm; import std.array; import std.conv; import std.stdio; import std.string; import std.numeric; void main(){ foreach(string line; lines(stdin)) writeln(patternN(line.parse!int)); } int patternN(int N = 1)(int n){ static if(N == 4){ if(n >= 0 && n < 10) return 1; else return 0; }else{ if(n < 0) return 0; int sum; foreach(i; 0..10) sum += patternN!(N+1)(n - i); return sum; } }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop; // dfmt on void main() { long A, B, C; scan(A, B, C); foreach (x; 1 .. 1_000_000) { if (A * x % B == C) { writeln("YES"); return; } } writeln("NO"); }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; void times(alias pred)(int n) { foreach(i; 0..n) pred(); } auto rep(alias pred, T = typeof(pred()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = pred(); return res; } void main() { string a = "CODEFESTIVAL2016"; string str = readln.chomp; int ans = 0; foreach(i; 0..a.length) { if (a[i]!=str[i])ans++; } ans.writeln; }
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(); } bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); } void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); } string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ string s = scan, t = scan; int n = s.length.to!int, m = t.length.to!int; int ans = n; foreach(i; 0 .. n){ if(i + m > n) continue; int tmpans; foreach(j; 0 .. m){ if(s[i + j] != t[j]) tmpans += 1; } ans.lowerTo(tmpans); } ans.writeln; }
D
void main(){ if(inelm() >= 30){ writeln("Yes"); }else{ writeln("No"); } } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range; // 1要素のみの入力 T inelm(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] inln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split())ln ~= elm.to!T(); return ln; }
D
import std.stdio; 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 k = readln.chomp.to!int; long sum; foreach (int i; 1..k+1) { foreach (int j; 1..k+1) { auto t = gcd(i, j); foreach (int l; 1..k+1) { sum += gcd(t, l); } } } sum.writeln; }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); for (int i = 0; i <= 100; i++) { for (int j = 0; j <= 100; j++) { if (i * 4 + j * 7 == n) { writeln("Yes"); return; } } } writeln("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; /+ const int LEFT = 0; const int RIGHT = 1; const int UP = 2; const int DOWN = 3; +/ enum { LEFT, RIGHT, UP, DOWN } void main() { string[9] kabe; int x, y; int direction; foreach (i; 0..9) { kabe[i] = readln().chomp; kabe[i].length = 18; } x = 0; y = 0; direction = RIGHT; move(&x, &y, direction); while (x != 0 || y != 0) { if (isKabe(kabe, x, y, getLeft(direction))) direction = getLeft(direction); else if (isKabe(kabe, x, y, direction)) {} else if (isKabe(kabe, x, y, getRight(direction))) direction = getRight(direction); else direction = getRight(getRight(direction)); move(&x, &y, direction); } writeln(); } bool isKabe(string[9] kabe, int x, int y, int dir) { final switch (dir) { case LEFT: if (x <= 0) return false; return (kabe[y*2][x-1] == '1'); break; case RIGHT: if (x >= 4) return false; return (kabe[y*2][x] == '1'); break; case UP: if (y <= 0) return false; return (kabe[y*2-1][x] == '1'); break; case DOWN: if (y >= 4) return false; return (kabe[y*2+1][x] == '1'); break; } return false; } int getLeft(int dir) { final switch (dir) { case LEFT: return DOWN; break; case RIGHT: return UP; break; case UP: return LEFT; break; case DOWN: return RIGHT; break; } return 0; } int getRight(int dir) { final switch (dir) { case LEFT: return UP; break; case RIGHT: return DOWN; break; case UP: return RIGHT; break; case DOWN: return LEFT; break; } return 0; } void move(int* x, int* y, int dir) { final switch (dir) { case LEFT: (*x)--; write("L"); break; case RIGHT: (*x)++; write("R"); break; case UP: (*y)--; write("U"); break; case DOWN: (*y)++; write("D"); break; } }
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; import std.container; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } void main(){ int n = read.to!int; int[] ss; foreach(i; 0 .. n) ss ~= read.to!int; int sum = ss.reduce!"a + b"(); int ans; if(sum % 10 != 0) ans = sum; else{ int m = sum; foreach(s; ss){ if(s < m && (sum - s) % 10 != 0) m = s; } ans = sum - m; } ans.writeln; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { auto ni = readints; int n = ni[0], i = ni[1]; writeln(n + 1 - i); }
D
import std; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { auto N = sread(); writeln((N[0] == '7' || N[1] == '7' || N[2] == '7') ? "Yes" : "No"); }
D
import std.stdio, std.string, std.conv; void main() { auto n = readln.split.to!(int[]), a = n[0], b = n[1]; int m; if((a + b) % 2 == 0){ m = 0; } else { m = 1; } writeln((a + b) / 2 + m); }
D
import std.stdio, std.string, std.algorithm, std.conv; int[] a; void main(){ string[] s; while (1) { s = split(readln()); a = []; foreach (i; s) a ~= to!int(i); if (a[0] == 0) break; writeln(solve(0, 0)); } } int solve(int p, int n){ if (n > 21) return 0; if (p >= a.length) return n; if (a[p] == 1) return max(solve(p+1, n+1), solve(p+1, n+11)); if (a[p] > 10) a[p] = 10; return solve(p+1, n+a[p]); }
D
void main() { dchar[] c = rdDchar; dchar[] d = rdDchar; dchar[] e = c.dup; dchar[] f = d.dup; reverse(e); reverse(f); writeln(c == f && d == e ? "YES" : "NO"); } enum long mod = 10L^^9 + 7; enum long inf = 1L << 60; enum double eps = 1.0e-9; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
import std.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 abx = readints; int a = abx[0], b = abx[1], x = abx[2]; if (a <= x && x <= a + b) writeln("YES"); else writeln("NO"); }
D
void main() { long m = readln.chomp.to!long; long x, y; foreach (i; 0 .. m) { long[] tmp = readln.split.to!(long[]); long d = tmp[0], c = tmp[1]; x += c, y += d * c; } writeln(x - 1 + (y - 1) / 9); } 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
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop; // dfmt on void main() { long X = lread(); long ans; foreach (b; 1 .. 500) foreach (p; 2 .. 500) if (b ^^ p <= X) { ans = ans.max(b ^^ p); } writeln(ans); }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; void main() { auto ip = readln.split.to!(int[]); if(ip[1] == 100) ip[1]++; writeln(100 ^^ ip[0] * ip[1]); } //writeln("滲み出す混濁の紋章 不遜なる狂気の器 湧き上がり 否定し 痺れ 瞬き 眠りを妨げる 爬行する鉄の王女 絶えず自壊する泥の人形 結合せよ 反発せよ 地に満ち己の無力を知れ 破道の九十 黒棺")
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.ascii; void main() { auto b = readln.chomp; if (b == "A") { writeln("T"); } else if (b == "T") { writeln("A"); } else if (b == "C") { writeln("G"); } else { writeln("C"); } }
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; if (N == 0) { writeln(0); return; } char[] r; foreach (i; 0..40) { if (N == 0) break; if (abs(N) & (1<<i)) { r ~= '1'; N -= (-2)^^i; } else { r ~= '0'; } } r.reverse(); writeln(r); }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; void times(alias fun)(int n) { foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } void main() { 2.rep!(() => readln.chomp.to!BigInt).pipe!( a => a[0]>a[1]?"GREATER":a[0]<a[1]?"LESS":"EQUAL" ).writeln; } // ---------------------------------------------- // 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
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop; // dfmt on void main() { long N = lread(); long ans = long.max; foreach (x; 1 .. N) { if (N < x * x) break; if (N % x != 0) continue; ans = ans.min((x - 1) + ((N / x) - 1)); } writeln(ans); }
D
import std.stdio, std.string, std.conv, std.range, std.algorithm; void main() { auto N = readln.chomp.to!int; ((1 + N) * N / 2).writeln; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "l ", long, "r"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { auto n = lread(); auto a = new long[](n); auto b = new long[](n); foreach (i; iota(n)) { long a_, b_; scan(a_, b_); a[i] = a_; b[i] = b_; } long count; foreach_reverse (i; iota(n)) { auto m = ((a[i] + count) % b[i]); if(m) count += b[i] - m; } count.writeln(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.stdio, std.range, std.string, std.conv, std.math, std.algorithm; void main(){ int base = 100000; real p = 1.05; int n = readln.chomp.to!int; n.iota.each!((e){ base *= p; base += (base % 1000) ? 1000 - base % 1000 : 0; }); writeln(base); }
D
void main() { long[] tmp = readln.split.to!(long[]); long a = tmp[0], b = tmp[1]; if (a * b <= 0) { "Zero".writeln; } else if (a < 0 && b < 0 && (b - a) % 2 == 0) { "Negative".writeln; } else { "Positive".writeln; } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto str = readln.chomp.dup; auto q = readln.chomp.to!size_t; foreach (_; 0..q) { auto rd = readln.split, cmd = rd[0]; auto a = rd[1].to!int, b = rd[2].to!int; switch (cmd) { case "print": writeln(str[a..b+1]); break; case "reverse": auto t = str[a..b+1]; t.reverse(); break; case "replace": auto p = rd[3]; str[a..b+1][] = p[]; break; default: assert(0); } } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto ad = readln.split.to!(int[]); auto a = ad[0]; auto b = ad[1]; auto c = ad[2]; auto d = ad[3]; writeln(abs(a-c) <= d || (abs(a-b) <= d && abs(b-c) <= d) ? "Yes" : "No"); }
D
import std.stdio, std.array, std.conv, std.string, std.algorithm, std.math; void main(){ string s; for(;;){ s=readln(); if(stdin.eof()) break; int num = to!int(chomp(s)); int result = 0; int[] nums = new int[num]; for(int i=0;i < num;++i) nums[i] = 1; nums[0] = 0; for(int i=1;i < sqrt(cast(double)num);++i){ if(nums[i] == 1){ for (int j = (i+1); (i+1) * j <= num; j++) nums[(i+1) * j - 1] = 0; } } for(int i = 0;i < num; ++i){ if(nums[i] == 1)++result; } writeln(result); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto N = readln.to!(wchar[]); writeln(N[0] == '9' || N[1] == '9' ? "Yes" : "No"); }
D
import std.stdio; import std.string; bool isK(C)(C[] a) { if (a.length <= 1) return true; size_t l = 0, r = a.length - 1; while (l < r) { if (a[l] != a[r]) { return false; } ++l; --r; } return true; } void main() { auto s = readln.chomp; if (s.isK && s[0.. $ / 2].isK && s[$ / 2 + 1..$].isK) { writeln("Yes"); } else { writeln("No"); } }
D
import std.stdio, std.conv, std.array,std.string,std.algorithm; void main() { auto n=readln.chomp.to!int; long ans=1; for(int i=1;i<=n;i++){ ans*=i; ans%=1000000000+7; } 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; } void main() { auto xs = readints; long q = xs[0]; long h = xs[1]; long s = xs[2]; long d = xs[3]; long one = min(q * 4, h * 2, s); long two = min(one * 2, d); auto n = readint; long ans = (n / 2) * two + (n % 2) * one; writeln(ans); }
D
void main() { int a = readln.chomp.to!int; int b = readln.chomp.to!int; int h = readln.chomp.to!int; writeln((a + b) * h / 2); } 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.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!int).array; int ans = 0; for (int i = 1; i < N; ++i) { if (A[i] == A[i-1]) { ans += 1; i += 1; } } ans.writeln; }
D
import std.stdio, std.conv; import std.array, std.algorithm, std.range; void main() { immutable int[dchar] L = ['I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000]; foreach(w;stdin.byLine()) { int s=0,t=0,c=0; foreach(n;w.map!(a=>L[a])()) { if(n==c) t+=c; if(n>c) s-=t,t=c=n; if(n<c) s+=t,t=c=n; } writeln(s+t); } }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { writeln(2^^cast(int)readln.chomp.to!int.log2); }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { string s; scan(s); writeln(s[0 .. $ - 8]); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.string; import std.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() { int maxN = 10^^6; int[] f, f_odd; for(int n=1; ;n++) { f ~= func(n); if (f.back > maxN) break; if (f.back%2!=0) f_odd~= f.back; } int INF = 1<<30; int[] ary = new int[maxN+1]; ary[] = INF; ary[0] = 0; while(true) { foreach(int i; 0..maxN) { if (ary[i]<INF) { for(int n=0; n<f.length; n++) { if (i+f[n]<=maxN) { ary[i+f[n]] = min(ary[i+f[n]], ary[i]+1); } else { break; } } } } if (ary[maxN] < INF) break; } int[] _ary = new int[maxN+1]; _ary[] = INF; _ary[0] = 0; while(true) { foreach(int i; 0..maxN) { if (_ary[i]<INF) { for(int n=0; n<f_odd.length; n++) { if (i+f_odd[n]<=maxN) { _ary[i+f_odd[n]] = min(_ary[i+f_odd[n]], _ary[i]+1); } else { break; } } } } if (_ary[maxN] < INF) break; } while(true) { int N = readln.chomp.to!int; if (N==0) break; writeln(ary[N], " ", _ary[N]); } } int func(int n) { return n*(n+1)*(n+2)/6; }
D
import std.functional, std.algorithm, std.container, std.typetuple, std.typecons, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv; void main() { auto ip = readln.split.to!(int[]); if(ip[0] < ip[1] && ip[1] < ip[2]) writeln("Yes"); else writeln("No"); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n, m; readV(n, m); writeln((n-1)*(m-1)); }
D
import core.bitop, std.bitmanip; import core.checkedint; import std.algorithm, std.functional; import std.array, std.container; import std.bigint; import std.conv; import std.math, std.numeric; import std.range, std.range.interfaces; import std.stdio, std.string; import std.typecons; void main() { immutable int MD = 10 ^^ 9 + 7; auto s = readln.chomp.to!(dchar[]); s.reverse(); debug { s.writeln; } int[] digs; foreach (i; 0 .. 10) { digs ~= i; } auto dp = new int[][] (s.length + 1, 13); dp[0][0] = 1; int mult = 1; foreach (i, c; s) { int[] toCheck; if (c == '?') { toCheck = digs; } else { int e = c - '0'; toCheck ~= e; } foreach (d; toCheck) { int added = (d.to!long * mult) % 13; foreach (j; 0 .. 13) { dp[i+1][(j + added) % 13] = (dp[i+1][(j + added) % 13] + dp[i][j]) % MD; } } mult = (mult.to!long * 10) % 13; } debug { dp.each!writeln; } dp[s.length][5].writeln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { int n, a; scan(n); scan(a); if (n % 500 <= a) { writeln("Yes"); } else { 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); } } } struct Queue(T) { private { size_t cap, head, tail; T[] data; } this (size_t n) in { assert(n > 0, "The capacity of a queue must be a positive integer."); } body { cap = n + 1; data = new T[](cap); } void clear() { head = tail = 0; } bool empty() { return head == tail; } bool full() { return head == (tail + 1) % cap; } size_t length() { return head <= tail ? tail - head : cap - head + tail; } T front() in { assert(!empty, "The queue is empty."); } body { return data[head]; } void removeFront() in { assert(!empty, "The queue is empty."); } body { (++head) %= cap; } alias popFront = removeFront; void insertBack(T x) in { assert(!full, "The queue is full."); } body { data[tail++] = x; tail %= cap; } alias insert = insertBack; T[] array() { return head <= tail ? data[head .. tail].dup : data[head .. $] ~ data[0 .. tail]; } string toString() { import std.format : format; if (head <= tail) { return format("[%(%s, %)]", data[head .. tail]); } else { return format("[%(%s, %)", data[head .. $]) ~ format(", %(%s, %)]", data[0 .. tail]); } } }
D