code
stringlengths
4
1.01M
language
stringclasses
2 values
module main; import core.stdc.stdio; import std.algorithm; int gcd(int a, int b){ while (a > 0 && b > 0){ if (a > b){ a = a % b; } else { b = b % a; } } return a + b; } int main(string[] argv) { int n; scanf("%d", &n); int [] arr = new int[n]; for(int i = 0; i<n; i++) scanf("%d", &arr[i]); arr.sort!(); int cur = arr[1] - arr[0]; for (int i = 1; i < n; i++){ cur = gcd(cur, arr[i] - arr[i - 1]); } int ans = 0; for (int i = 1; i < n; i++){ int len = arr[i] - arr[i - 1]; ans += len / cur - 1; } printf("%d", ans); return 0; }
D
import std.stdio, std.range, std.conv, std.algorithm, std.array, std.string, std.ascii, std.math; void main() { readln; auto a = readln.strip, b = readln.strip; uint sum; foreach (i, e; a) { auto tmpA = e.to!int, tmpB = b[i].to!int; auto minA = min(tmpA, tmpB), maxB = max(tmpA, tmpB); auto cl = maxB - minA, tl = 10 - maxB + minA; if (cl < tl) { sum += cl; } else { sum += tl; } } writeln(sum); }
D
module sigod.codeforces.p290D; import std.conv; import std.stdio; import std.string; void main() { string str = stdin.readln().strip(); int value = stdin.readln().strip().to!int(); stdout.write(solve(str, value)); } string solve(string str, int value) { string result; foreach (ref ch; str.toLower()) { if (ch < value + 97) { result ~= (ch ~ "").toUpper(); } else { result ~= ch; } } return result.idup; } unittest { assert(solve("AprilFool", 14) == "AprILFooL"); }
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; long sd(long n) { long res = 0; while(n) { res += n % 10; n /= 10; } return res; } long pmod(long a, long m) { return (a%m + m)%m; } void main(string[] args) { inputFile = stdin; debug inputFile = File(args[1]); auto nt = next!int; foreach(tc; 0 .. nt) { auto n = next!long; auto s = next!long; long tp = 1; foreach(i; 0 .. 19) { long rounded = n + pmod(-n, tp); if (rounded.sd <= s) { writeln(pmod(-n, tp)); break; } tp *= 10; } } } // INPUT enum InputStyle { byChunk, byLine }; enum inputStyle = InputStyle.byLine; File inputFile; string popWord(); static if (inputStyle == InputStyle.byChunk) { const chunkSize = 1024 * 1024 * 8; const wordSize = 4096; char[chunkSize] chunkBuff; char[] currChunk; void renewChunk() { if (inputFile.eof) currChunk = null; else currChunk = inputFile.rawRead(chunkBuff); } char[wordSize] wordBuff; char[] word; string popWord() { if (currChunk.length == 0) renewChunk; assert(currChunk.length > 0); while (currChunk[0].isWhite) { currChunk = currChunk[1 .. $]; if (currChunk.length == 0) renewChunk; } word = wordBuff[0 .. 0]; while (!currChunk[0].isWhite) { word.length++; word[$ - 1] = currChunk[0]; currChunk = currChunk[1 .. $]; if (currChunk.length == 0) { renewChunk; if (currChunk.length == 0) return cast(string) word; } } return cast(string) word; } } else { DList!string _words; string popWord() { while (_words.empty) { foreach(w; inputFile.readln.split) { _words.insertBack(w); } } auto word = _words.front; _words.removeFront; return word; } } T next(T)() { return to!T(popWord); } auto next(T, S...)(S s) { static if (S.length == 0) { return to!T(popWord); } else { auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]); foreach(ref elem; res) elem = next!T(s[1 .. $]); return res; } }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { string s; while ((s = readln.strip) != "") { auto n = s.length.to !(int); s ~= s; int res = 1; int cur = 1; foreach (i; 1..n * 2) { if (s[i - 1] != s[i]) { cur += 1; res = max (res, cur); } else { cur = 1; } } res = min (res, n); writeln (res); } }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { long H, W; scan(H, W); auto C = new string[](H); foreach (i; 0 .. H) scan(C[i]); foreach (i; 0 .. H * 2) { writeln(C[(i / 2)]); } }
D
import std.stdio, std.string, std.array, std.conv, std.algorithm; void main() { foreach (i; 0 .. 3000) { int[] x = readln.chomp.split.to!(int[]); if (x[0] == 0 && x[1] == 0) break; writeln(min(x[0], x[1]), " ", max(x[0], x[1])); } }
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.stdlib; void main() { auto N = readln.chomp.to!int; long ans = 0; foreach (i; 1..N+1) if (i % 3 != 0 && i % 5 != 0) ans += i; ans.writeln; }
D
import std.stdio, std.string, std.conv; void main(){ for(uint i;;++i){ auto a = readln.chomp.to!int; if(!a) break; writeln("Case ",i+1,": ",a); } }
D
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; int n, x; int[] a; void main() { scan(n, x); a = readln.split.to!(int[]); auto b = a.dup(); long ans = 10L^^15; foreach (k ; 0 .. n) { long anst = 1L * k * x; foreach (i ; 0 .. n) { b[i] = min(b[i], a[(i - k + n) % n]); } anst += sum(b, 0L); ans = min(ans, anst); } 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
//dlang template---{{{ import std.stdio; import std.conv; import std.string; import std.array; import std.algorithm; // MIT-License https://github.com/kurokoji/nephele class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } //Digit count---{{{ int DigitNum(int num) { int digit = 0; while (num != 0) { num /= 10; digit++; } return digit; } //}}} //}}} void main() { Scanner sc = new Scanner; int A, B, C, X; sc.scan(A, B, C, X); int res = 0; foreach (i; 0 .. A + 1) { foreach (j; 0 .. B + 1) { foreach (k; 0 .. C + 1) { if ((i * 500) + (j * 100) + (k * 50) == X) res++; } } } writeln(res); }
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; 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.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto X = RD!char; auto Y = RD!char; writeln(X-'A' == Y-'A' ? "=" : X-'A' < Y-'A' ? "<" : ">"); stdout.flush(); debug readln(); }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto n = readln.strip.to !(int); auto a = readln.splitter.map !(to !(int)).array; writeln (n - a.count (a.minElement)); } }
D
// import chie template :) {{{ static if (__VERSION__ < 2090) { import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons, std.format, std.bitmanip, std.numeric; } else { import std; } // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan(T...)(ref T args) { foreach (ref arg; args) { arg = next!(typeof(arg)); } } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } // }}} // ModInt {{{ struct ModInt(ulong modulus) { import std.traits : isIntegral, isBoolean; import std.exception : enforce; private { ulong val; } this(const ulong n) { val = n % modulus; } this(const ModInt n) { val = n.value; } @property { ref inout(ulong) value() inout { return val; } } T opCast(T)() { static if (isIntegral!T) { return cast(T)(val); } else if (isBoolean!T) { return val != 0; } else { enforce(false, "cannot cast from " ~ this.stringof ~ " to " ~ T.stringof ~ "."); } } ModInt opAssign(const ulong n) { val = n % modulus; return this; } ModInt opOpAssign(string op)(ModInt rhs) { static if (op == "+") { val += rhs.value; if (val >= modulus) { val -= modulus; } } else if (op == "-") { if (val < rhs.value) { val += modulus; } val -= rhs.value; } else if (op == "*") { val = val * rhs.value % modulus; } else if (op == "/") { this *= rhs.inv; } else if (op == "^^") { ModInt res = 1; ModInt t = this; while (rhs.value > 0) { if (rhs.value % 2 != 0) { res *= t; } t *= t; rhs /= 2; } this = res; } else { enforce(false, op ~ "= is not implemented."); } return this; } ModInt opOpAssign(string op)(ulong rhs) { static if (op == "+") { val += ModInt(rhs).value; if (val >= modulus) { val -= modulus; } } else if (op == "-") { auto r = ModInt(rhs); if (val < r.value) { val += modulus; } val -= r.value; } else if (op == "*") { val = val * ModInt(rhs).value % modulus; } else if (op == "/") { this *= ModInt(rhs).inv; } else if (op == "^^") { ModInt res = 1; ModInt t = this; while (rhs > 0) { if (rhs % 2 != 0) { res *= t; } t *= t; rhs /= 2; } this = res; } else { enforce(false, op ~ "= is not implemented."); } return this; } ModInt opUnary(string op)() { static if (op == "++") { this += 1; } else if (op == "--") { this -= 1; } else { enforce(false, op ~ " is not implemented."); } return this; } ModInt opBinary(string op)(const ulong rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinary(string op)(ref const ModInt rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinary(string op)(const ModInt rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinaryRight(string op)(const ulong lhs) const { mixin("return ModInt(this) " ~ op ~ "= lhs;"); } long opCmp(ref const ModInt rhs) const { return cast(long)value - cast(long)rhs.value; } bool opEquals(const ulong rhs) const { return value == ModInt(rhs).value; } ModInt inv() const { ModInt ret = this; ret ^^= modulus - 2; return ret; } string toString() const { import std.format : format; return format("%s", val); } } // }}} // alias {{{ alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less); alias MinHeap(T) = Heap!(T, "a > b"); // }}} // memo {{{ /* - ある値が見つかるかどうか <https://dlang.org/phobos/std_algorithm_searching.html#canFind> canFind(r, value); -> bool - 条件に一致するやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#filter> // 2で割り切れるやつ filter!"a % 2 == 0"(r); -> Range - 合計 <https://dlang.org/phobos/std_algorithm_iteration.html#sum> sum(r); - 累積和 <https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold> // 今の要素に前の要素を足すタイプの一般的な累積和 // 累積和のrangeが帰ってくる(破壊的変更は行われない) cumulativeFold!"a + b"(r, 0); -> Range - rangeをarrayにしたいとき array(r); -> Array - 各要素に同じ処理をする <https://dlang.org/phobos/std_algorithm_iteration.html#map> // 各要素を2乗 map!"a * a"(r) -> Range - ユニークなやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#uniq> uniq(r) -> Range - 順列を列挙する <https://dlang.org/phobos/std_algorithm_iteration.html#permutations> permutation(r) -> Range - ある値で埋める <https://dlang.org/phobos/std_algorithm_mutation.html#fill> fill(r, val); -> void - バイナリヒープ <https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap> // 昇順にするならこう(デフォは降順) BinaryHeap!(Array!T, "a > b") heap; heap.insert(val); heap.front; heap.removeFront(); - 浮動小数点の少数部の桁数設定 // 12桁 writefln("%.12f", val); - 浮動小数点の誤差を考慮した比較 <https://dlang.org/phobos/std_math.html#.approxEqual> approxEqual(1.0, 1.0099); -> true - 小数点切り上げ <https://dlang.org/phobos/std_math.html#.ceil> ceil(123.4); -> 124 - 小数点切り捨て <https://dlang.org/phobos/std_math.html#.floor> floor(123.4) -> 123 - 小数点四捨五入 <https://dlang.org/phobos/std_math.html#.round> round(4.5) -> 5 round(5.4) -> 5 */ // }}} void main() { auto cin = new Scanner; int n; cin.scan(n); int[] x = cin.nextArray!int(n); int mini = int.max; foreach (p; 1 .. 101) { int sum; foreach (e; x) { sum += (p - e) ^^ 2; } mini = min(mini, sum); } writeln(mini); }
D
// problem: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_3_A // require: graph/articulation_points.d import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio; //LRDU bool solve2(int a, int b, int x1, int x2) { if (x1 == 0 && x2 == 0) { return a == 0 && b == 0; } else if (a == b) { return true; } else if (a > b) { return abs(x1) >= a - b; } else { return abs(x2) >= b - a; } } void solve(int T) { while (T--) { auto s = readln.split.map!(to!int); auto A = s[0]; auto B = s[1]; auto C = s[2]; auto D = s[3]; s = readln.split.map!(to!int); auto X = s[0]; auto Y = s[1]; auto x1 = s[2] - X; auto y1 = s[3] - Y; auto x2 = s[4] - X; auto y2 = s[5] - Y; auto AB = A - B; auto ans = solve2(A, B, x1, x2) && solve2(C, D, y1, y2); writeln(ans ? "Yes" : "No"); } } void main() { auto T = readln.chomp.to!int; solve(T); }
D
import std.stdio, std.string, std.conv; import std.algorithm, std.array, std.range; import std.bigint, std.math; auto solve(string s_) { immutable N = s_.to!int(); auto AS = new long[N]; foreach(ref v;AS) v=readln.chomp().to!long(); immutable M=AS.reduce!max(); immutable M2=sqrt(M.to!real()).to!long()+1; immutable M3=cbrt(M.to!real()).to!long()+1; auto pbs = new bool[M2]; long[] ps; size_t q=M2; foreach(i;2..M2) if(!pbs[i]) { if(q==M2 && i>=M3) q=ps.length; ps~=i; for(auto j=i*2; j<M2; j+=i) pbs[j]=true; } if(q==M2) q=ps.length; auto RS = new long[N]; int[long] b; foreach(k;0..N) { BigInt r=1; long a=1,v=AS[k]; foreach(p;ps[0..q]) { int c=0; while(v%p==0) v/=p, ++c; c%=3; if(c!=0) { foreach(_;0..c) a*=p; foreach(_;0..3-c) r*=p; } } if(v>1) { a*=v; long lo=0, hi=ps.length-1; void rev() { while(lo<=hi) { auto m=(lo+hi)/2; immutable p=ps[m], pp=p*p; if(pp==v){ r*=p; return; } if(pp<v) lo=m+1; else hi=m-1; } r*=v; r*=v; } rev(); } AS[k]=a; immutable t=r>M? -1: r.toLong(); RS[k]=t; if(a !in b) b[a]=1; else ++b[a]; if(t !in b) b[t]=0; } if(1 in b) b[1]=1; bool[long] u; int m=0; foreach(k;0..N) { immutable a=AS[k]; if(a in u) continue; immutable r=RS[k]; m+=max(b[a],b[r]); u[a]=true,u[r]=true; } return m; } void main(){ for(string s; (s=readln.chomp()).length;) writeln(solve(s)); }
D
pragma(inline, true) void chmin(T)(ref T a, T b) { if (a > b) a = b; } int[] init() { enum INF = 1 << 28; int[] dp = new int[100010]; foreach (i; 1..100001) { dp[i] = INF; int p6 = 1, p9 = 1; while (p6 <= i || p9 <= i) { if (i - p6 >= 0) chmin(dp[i], dp[i - p6] + 1); if (i - p9 >= 0) chmin(dp[i], dp[i - p9] + 1); p6 *= 6; p9 *= 9; } } return dp; } void main() { import std.conv : to; import std.stdio : readln, writeln; import std.string : chomp; int[] dp = init(); writeln(dp[readln.chomp.to!int]); }
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, "damage", long, "cost"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { auto h = lread(); h.kill.writeln(); } long kill(long h) { if (h == 1) return 1; auto tmp = kill(h / 2); return 1 + 2 * tmp; } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
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; int A, B, C; sc.scan(A, B, C); if (B / A < C) { writeln(B / A); } else { writeln(C); } }
D
void main() { long[] tmp = readln.split.to!(long[]); long n = tmp[0], m = tmp[1]; long[] p = readln.split.to!(long[]); p[] -= 1; auto tree = UnionFind(n); foreach (i; 0 .. m) { tmp = readln.split.to!(long[]); long x = tmp[0] - 1, y = tmp[1] - 1; tree.unite(p[x], p[y]); } long cnt; foreach (i; 0 .. n) { if (tree.same(i, p[i])) ++cnt; } cnt.writeln; } struct UnionFind { long[] par; this(long n) { par = new long[n]; foreach (i; 0 .. n) { par[i] = i; } } long root(long x) { if (par[x] == x) return x; else return par[x] = root(par[x]); } bool same(long x, long y) { return root(x) == root(y); } void unite(long x, long y) { x = root(x), y = root(y); if (x == y) return; par[x] = y; } } 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.typecons, std.algorithm; alias Tuple!(int, "top", int, "bottom") Bundle; const int INF = (1 << 24); void main() { uint times; scanf("%d", &times); uint[][] dp = new uint[][](times, times); Bundle[] cards; foreach(t; 0..times) { uint top, bottom; scanf("%d %d", &top, &bottom); cards ~= Bundle(top, bottom); } foreach(ref d; dp) foreach(ref x; d) x = INF; for(int i = 0; i < times - 1; i++) dp[i][i] = 0; dp[times - 1][times - 1] = 0; // dp[i][j] -> i 番目 から j 番目を計算する時の最適コスト for(uint len = 1; len < times; len++) { for(uint s = 0; s + len < times; s++) { int f = s + len; for(uint i = s; i < f; i++) { uint cost = (cards[s].top * cards[i].bottom) * (cards[i + 1].top * cards[f].bottom); cost += dp[s][i] + dp[i + 1][f]; if (cost < dp[s][f]) dp[s][f] = cost; } } } writeln(dp[0][times - 1]); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); void main() { string s; scan(s); // writeln(s[0]); long x; long y; foreach (i; 0 .. s.length) { if (s[i] == '0') { x += 1; } else if (s[i] == '1') { y += 1; } } writeln(2 * (min(x, y))); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { int N, L; scan(N, L); int taste(int i) { return L + i - 1; } int sum = 0; for (int i = 1; i <= N; i++) { sum += taste(i); } int ans = 0; int best = int.max; for (int i = 1; i <= N; i++) { int s = 0; for (int j = 1; j <= N; j++) { if (i == j) continue; s += taste(j); } if (abs(sum - s) < best) { best = abs(sum - s); ans = s; } } writeln(ans); }
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; void main(){ int n = readln().chomp().to!int; for(int i=0;i<n;i++){ int point,o,r; while(true){ string s = readln().chomp(); if(s == "HIT"){ r = 2 * r + 1; if(r & (1<<3)){ r ^= 1 << 3; point++; } }else if(s == "OUT"){ o++; if(o == 3) break; }else if(s == "HOMERUN"){ point += 1 + (r & 1) + (r & 2)/2 + (r & 4)/4; r = 0; } } writeln(point); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto S = readln.chomp; writeln(S[1] == 'B' ? "ARC" : "ABC"); }
D
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } void main() { try { for (; ; ) { const M = readInt(); const D = readInt(); int ans; foreach (m; 1 .. M + 1) { foreach (d; 1 .. D + 1) { if (10 <= d && d <= 99) { const d1 = d % 10; const d10 = d / 10; if (d1 >= 2 && d10 >= 2 && d1 * d10 == m) { ++ans; } } } } writeln(ans); } } catch (EOFException e) { } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; int celiPow2(int n) { int x = 0; while ((1u << x) < cast(uint)(n)) x++; return x; } struct Segtree(S, alias op, alias e) { import std.functional : binaryFun, unaryFun; import std.traits : isCallable, Parameters; static if (is(typeof(e) : string)) { auto unit() { return mixin(e); } } else { alias unit = e; } this(int n) { this(new S[](n)); } this(S[] v) { _n = cast(int) v.length; log = celiPow2(_n); size = 1 << log; d = new S[](2 * size); d[] = unit(); foreach (i; 0 .. _n) d[size + i] = v[i]; foreach_reverse (i; 1 .. size) update(i); } void set(int p, S x) { assert(0 <= p && p < _n); p += size; d[p] = x; foreach (i; 1 .. log + 1) update(p >> i); } S get(int p) { assert(0 <= p && p < _n); return d[p + size]; } S prod(int l, int r) { assert(0 <= l && l <= r && r <= _n); S sml = unit(), smr = unit(); l += size; r += size; while (l < r) { if (l & 1) sml = binaryFun!(op)(sml, d[l++]); if (r & 1) smr = binaryFun!(op)(d[--r], smr); l >>= 1; r >>= 1; } return binaryFun!(op)(sml, smr); } S allProd() { return d[1]; } int maxRight(alias f)(int l) { return maxRight(l, unaryFun!(f)); } int maxRight(F)(int l, F f) if (isCallable!F && Parameters!(F).length == 1) { assert(0 <= l && l <= _n); assert(f(unit())); if (l == _n) return _n; l += size; S sm = unit(); do { while (l % 2 == 0) l >>= 1; if (!f(binaryFun!(op)(sm, d[l]))) { while (l < size) { l = 2 * l; if (f(binaryFun!(op)(sm, d[l]))) { sm = binaryFun!(op)(sm, d[l]); l++; } } return l - size; } sm = binaryFun!(op)(sm, d[l]); l++; } while ((l & -l) != l); return _n; } int minLeft(alias f)(int r) { return minLeft(r, unaryFun!(f)); } int minLeft(F)(int r, F f) if (isCallable!F && Parameters!(F).length == 1) { assert(0 <= r && r <= _n); assert(f(unit())); if (r == 0) return 0; r += size; S sm = unit(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!f(binaryFun!(op)(d[r], sm))) { while (r < size) { r = 2 * r + 1; if (f(binaryFun!(op)(d[r], sm))) { sm = binaryFun!(op)(d[r], sm); r--; } } return r + 1 - size; } sm = binaryFun!(op)(d[r], sm); } while ((r & -r) != r); return 0; } private: int _n, size, log; S[] d; void update(int k) { d[k] = binaryFun!(op)(d[2 * k], d[2 * k + 1]); } } void main() { auto nq = readln.split.to!(int[]); auto N = nq[0]; auto Q = nq[1]; auto AS = readln.split.to!(int[]); auto segt = Segtree!(int, "a > b ? a : b", "0")(AS); while (Q--) { auto q = readln.split.to!(int[]); if (q[0] == 1) { auto x = q[1]; auto v = q[2]; segt.set(x-1, v); } else if (q[0] == 2) { auto l = q[1]-1; auto r = q[2]-1; writeln(segt.prod(l, r+1)); } else { auto x = q[1]-1; auto v = q[2]; if (segt.prod(x, N) < v) { writeln(N+1); } else { writeln(segt.maxRight(x, (int w) => w < v) + 1); } } } }
D
/+ dub.sdl: name "B" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; int main() { auto sc = new Scanner(stdin); int n; int[] a; sc.read(n, a); a[] -= 1; bool[] b = n.iota.map!(i => i == a[i]).array; int c = -1; int d = 0; foreach (i; 0..n+1) { if (i == n || !b[i]) { int di = i - c; d += di / 2; c = i; } } writeln(d); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } version (X86) static if (__VERSION__ < 2071) { import core.bitop : bsf, bsr, popcnt; int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* 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) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } }
D
void main() { int n = readln.chomp.to!int; int[] a = readln.split.to!(int[]); int cnt; while (a.all!(t => t % 2 == 0)) { a[] /= 2; ++cnt; } cnt.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 core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { auto S = sread(); long Q = lread(); DList!char ans; foreach (c; S) ans.insertBack(c); bool reverse; foreach (i; 0 .. Q) { auto input = readln().split(); if (input[0] == "1") { reverse = (reverse == false); continue; } if (input[0] == "2") { bool front = (input[1] == "1" && !reverse) || (input[1] == "2" && reverse); if (front) { ans.insertFront(input[2][0]); } else { ans.insertBack(input[2][0]); } } } if (!reverse) { foreach (c; ans) write(c); } else { foreach_reverse (c; ans) write(c); } writeln(); }
D
void main() { ("aiueo".canFind(rs) ? "vowel" : "consonant").writeln; } // =================================== import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } // 26文字より少ない → まだ使っていない最小の文字を追加する // zyx.....cba → -1 // その他→後ろから見ていって最初に下がった場所をカエる(例 ~~~~~dzfc → ~~~~~fcqz) // void main(){ char[] s = readln.chomp.to!(char[]); if(s.length < 26){ bool[char] xset; foreach(c; s) xset[c] = 1; foreach(char c; 'a' .. 'z' + 1){ if(c !in xset){ writeln(s ~ c); return; } } } for(int i = 24; i >= 0; i --){ if(s[i] < s[i + 1]){ int j1 = i + 1; for(int j = i + 1; j <= 25; j ++){ if(s[i] < s[j]) if(s[j] < s[j1]) j1 = j; } char[] s2 = s[i .. j1] ~ s[j1 + 1 .. $]; writeln(s[0 .. i] ~ s[j1]); return; } } writeln("-1"); return; }
D
import std.stdio,std.math,std.string,std.conv,std.typecons,std.format; import std.algorithm,std.range,std.array,std.numeric; T[] readarr(T=long)(){return readln.chomp.split.to!(T[]);} void scan(T...)(ref T args){auto input=readln.chomp.split;foreach(i,t;T)args[i]=input[i].to!t;} struct Queue(T){T[]e;auto enq(T t){e~=t;}auto enq(T[]ts){e~=ts;}T deq(){T tp=e[0];e=e.length>1?e[1..$]:[];return tp;}} struct Stack(T){T[]e;auto push(T t){e~=t;}auto push(T[]ts){e~=ts;}T pop(){T tp=e[$-1];e=e.length>1?e[0..$-1]:[];return tp;}} //END OF TEMPLATE void main(){ ((readarr[0]+1)/2).writeln; }
D
void main() { long[] tmp = readln.split.to!(long[]); long n = tmp[0], m = tmp[1]; writeln((100 * (n - m) + 1900 * m) * 2 ^^ m); } 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 s, w; scanf("%d %d", &s, &w); if( s<=w )writeln("unsafe"); else writeln("safe"); } 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
module b; import std.conv, std.stdio; import std.algorithm, std.array, std.string, std.range; void main() { auto buf = readln.chomp.split.to!(int[]); auto votes = readln.chomp.split.to!(int[]); auto s = votes.sum; auto popular = votes.count!(a => s <= a * 4 * buf[1]); ((buf[1] <= popular) ? "Yes" : "No").writeln; }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { const tmp = readln.split.to!(int[]); writeln(min(1, tmp[0] % tmp[1])); }
D
import std.stdio, std.conv, std.array, std.string; import std.algorithm; import std.container; import std.range; import core.stdc.stdlib; import std.math; void main() { auto N = readln.chomp; auto days = ["SUN","MON","TUE","WED","THU","FRI","SAT"]; foreach(i; 0..7) { if (N == days[i]) { writeln(7-i); } } }
D
import std.algorithm; import std.stdio; import std.string; void main() { auto n = readln; auto s = readln.strip; auto lts = new int[ s.length ]; auto rts = new int[ s.length ]; auto result = int.max; lts[ 0 ] = 0; rts[ $-1 ] = 0; for( int index = 1; index < s.length; index++ ) { lts[ index ] = lts[ index - 1 ] + ( s[ index - 1 ] == 'E' ? 0 : 1 ); } for( int index = cast( int ) s.length-2; 0 <= index; index-- ) { rts[ index ] = rts[ index + 1 ] + ( s[ index + 1 ] == 'W' ? 0 : 1 ); } for( int index = 0; index < s.length; index++ ) { result = min( result, lts[ index ] + rts[ index ] ); } writeln( result ); }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto n = readln.strip.to !(int); auto a = readln.splitter.map !(to !(int)).array; auto x = new int [] [] (n + 1, n + 1); foreach (i; 0..n) { foreach (j; i + 1..n + 1) { bool [int] s; foreach (k; i..j) { s[a[k]] = true; } x[i][j] = 0; while (x[i][j] in s) { x[i][j] += 1; } x[i][j] += 1; } } auto f = new int [] [] (n + 1, n + 1); foreach (i; 0..n) { foreach (j; i + 1..n + 1) { f[i][j] = x[i][j]; foreach (k; i..j) { f[i][j] = max (f[i][j], f[i][k] + x[k][j]); } } } int res = 0; foreach (i; 0..n) { foreach (j; i + 1..n + 1) { res += f[i][j]; } } writeln (res); } }
D
void main(){ int[] ab = _scanln(); if( ab[0]>ab[1] )writeln(ab[0]+ab[0]-1); else if( ab[0]<ab[1] )writeln(ab[1]+ab[1]-1); else writeln(ab[0]*2); } 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; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new int[](t); foreach (ti; 0..t) { auto n = RD!int; auto a = RDA!int(-1); int mode; foreach (i; 0..n) { if (a[i] == i) { if (mode == 1) mode = 2; } else { if (mode == 0) mode = 1; else if (mode == 2) mode = 3; } } if (mode == 0) continue; else if (mode == 1 || mode == 2) ans[ti] = 1; else ans[ti] = 2; } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 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 t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto n = RD!int; auto m = RD!int; ans[ti] = n % m == 0; } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush; debug readln; }
D
// Try Codeforces // author: Leonardone @ NEETSDKASU import std.stdio : readln, writeln; import std.array : split; import std.string : chomp; import std.conv : to; void main() { auto n = readln.chomp.to!int; auto ts = readln.chomp.split.to!(int[]); auto ord = new int[n + 1]; auto tim = new int[n + 1]; auto ans = n; ord[0] = 1; auto c = 1; foreach (i ; 1 .. n+1) { auto ti = ts[i-1]; auto nm = ord[ti]; if (ti == tim[nm]) { tim[nm] = i; ord[i] = nm; } else { c++; tim[c] = i; ord[i] = c; } } ans = c; writeln(ans); }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random; import std.typecons, std.functional, std.traits,std.concurrency; import std.algorithm, std.container; import core.bitop, core.time, core.memory; import std.bitmanip; import std.regex; enum INF = long.max/3; enum MOD = 10L^^9+7; //辞書順順列はiota(1,N),nextPermituionを使う void end(T)(T v) if(isIntegral!T||isSomeString!T||isSomeChar!T) { import core.stdc.stdlib; writeln(v); exit(0); } T[] scanArray(T = long)() { static char[] scanBuf; readln(scanBuf); return scanBuf.split.to!(T[]); } dchar scanChar() { int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } return cast(dchar)c; } T scanElem(T = long)() { import core.stdc.stdlib; static auto scanBuf = appender!(char[])([]); scanBuf.clear; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { scanBuf ~= cast(char) c; c = getchar; } return scanBuf.data.to!T; } dchar[] scanString(){ return scanElem!(dchar[]); } void main() { auto a=scanString; auto b=scanString; auto c=scanString; if(a[$-1]==b[0]&&b[$-1]==c[0])end("YES");end("NO"); }
D
import std.stdio; import std.conv; import std.string; void main() { auto N = readln.chomp.to!long; writeln(N / 3); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto as = new int[](N); foreach (i; 0..N) as[i] = readln.chomp.to!int; sort!"a > b"(as); uint x; foreach (i; 1..N) if ((as[i-1] - as[i]) % 2) { x ^= i; } if (as[$-1] % 2) x ^= N; writeln(x ? "first" : "second"); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp.to!int; auto k = readln.chomp.to!int; auto x = 1; foreach (i; 0..n) { if (x < k) { x *= 2; } else { x += k; } } x.writeln; }
D
import std.stdio; import std.algorithm; import std.conv; import std.array; import std.string; import std.math; import std.functional; import std.range; import std.typecons; import std.format; void main(string[] args) { readln.solve.writeln; } auto solve(string line1) { auto _ = line1.split.map!(to!real); real a = _[0]; real b = _[1]; return ((a + b)/2).ceil.to!int; } unittest { assert(solve("1 3") == 2, "1"); assert(solve("7 4") == 6, "2"); assert(solve("5 5") == 5, "3"); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}} void main() { int n; readV(n); writeln(n%calc(n) == 0 ? "Yes" : "No"); } auto calc(int n) { int r = 0; for (; n > 0; n /= 10) r += n%10; return r; }
D
void main() { long n, a, b; rdVals(n, a, b); long c = a + b; long d = n / c, r = n % c; writeln(a * d + min(a, r)); } 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
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { auto n = lread(); auto shop = new long[][](n, 10); auto p = new long[][](n, 11); foreach (ref e; shop) { e = aryread(); } foreach (ref e; p) { e = aryread(); } long max_bene = -INF; foreach (i; iota(1, 1 << 10)) { long bene; auto bitary = i.makebitary(); foreach (j; iota(n)) { long cnt; foreach (k; iota(10)) cnt += shop[j][k] & bitary[k]; bene += p[j][cnt]; } max_bene = max(bene, max_bene); } max_bene.writeln(); } auto makebitary(long a, long disit = 10) { auto bits = new long[](disit); long i; while (a > 0) { bits[i++] = a % 2; a = a >> 1; } return bits; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.typecons; import std.numeric, std.math; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto S = RD!string; long ans; char last; foreach (c; S) { if (c != last) ++ans; last = c; } writeln(ans-1); stdout.flush(); //readln(); }
D
void main() { long[] nmq = rdRow; long n = nmq[0], m = nmq[1], q = nmq[2]; long[][] lists = new long[][](n+1, n+1); foreach (i; 0 .. m) { long[] tmp = rdRow; long l = tmp[0], r = tmp[1]; ++lists[l][r]; } foreach (i; 1 .. n+1) { foreach (j; 1 .. n+1) { lists[i][j] += lists[i-1][j]; } } foreach (i; 1 .. n+1) { foreach (j; 1 .. n+1) { lists[i][j] += lists[i][j-1]; } } foreach (i; 0 .. q) { long[] tmp = rdRow; long a = tmp[0], b = tmp[1]; writeln(lists[b][b] - lists[b][a-1] - lists[a-1][b] + lists[a-1][a-1]); } } T rdElem(T = long)() { //import std.stdio : readln; //import std.string : chomp; //import std.conv : to; return readln.chomp.to!T; } alias rdStr = rdElem!string; dchar[] rdDchar() { //import std.conv : to; return rdStr.to!(dchar[]); } T[] rdRow(T = long)() { //import std.stdio : readln; //import std.array : split; //import std.conv : to; return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdRow!T).array; } void wrMat(T = long)(T[][] mat) { //import std.stdio : write, writeln; foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.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 = 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 main() { auto t = RD!int; auto ans = new long[2][](t); foreach (i; 0..t) { auto a = RD; auto b = RD; auto c = RD; auto d = RD; auto k = RD; auto x = (a+c-1)/c; auto y = (b+d-1)/d; if (x+y <= k) ans[i] = [x, y]; else ans[i] = [-1, -1]; } foreach (e; ans) { if (e[0] == -1) writeln(-1); else writeln(e[0], " ", e[1]); } stdout.flush(); debug readln(); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.ascii; void main() { auto c1 = readln.chomp; auto c2 = readln.chomp; if (c1[0] == c2[2] && c1[1] == c2[1] && c1[2] == c2[0]) { writeln("YES"); } else { writeln("NO"); } }
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 readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!T;r.popFront;}} void readM(T)(size_t r,size_t c,ref T[][]t){t=new T[][](r);foreach(ref v;t)readA(c,v);} void main() { int n; readV(n); int[][] a; readM(n, n, a); auto b = new bool[][](n, n); foreach (i; 0..n) b[i][i] = true; auto r = 0L; foreach (i; 0..n-1) foreach (j; i+1..n) r += a[i][j]; foreach (i; 0..n) foreach (j; 0..n) foreach (k; 0..n) { if (a[i][j] > a[i][k]+a[k][j]) { writeln(-1); return; } else if (a[i][j] == a[i][k]+a[k][j] && i != k && j != k && !b[i][j]) { r -= a[i][j]; b[i][j] = b[j][i] = true; } } writeln(r); }
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; import core.checkedint; void main() { for (;;) { int n = readln.chomp.to!int; if (n == 0) { break; } int[] r = [0, 0]; foreach (i; 0..n) { int[] v = readln.chomp.split.map!(to!int).array; if (v[0] > v[1]) { r[0] += v.sum; } else if (v[0] < v[1]) { r[1] += v.sum; } else { r[] += v[]; } } writeln(r[0], " ", r[1]); } } 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
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto abx = readln.split.to!(int[]); auto c = abx[2] - abx[0]; writeln(c >= 0 && c <= abx[1] ? "YES" : "NO"); }
D
import std.stdio, std.string, std.conv; void main() { string t = readln.chomp; string p = readln.chomp; int i = 0; while (true) { i = indexOf(t, p, i).to!int; if (i == - 1) break; i.writeln; ++i; } }
D
import std.stdio, std.string, std.conv; void main() { int[] tmp = readln.split.to!(int[]); int a = tmp[0], b = tmp[1]; writeln(0 < a && a < 10 && 0 < b && b < 10 ? a * b : -1); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto cs = new int[](101); foreach (n; 2..N+1) { foreach (d; 2..N+1) { while (n%d == 0) { cs[d] += 1; n /= d; } } } int a, b, c, d, e; foreach (x; cs) { if (x >= 74) ++a; if (x >= 24) ++b; if (x >= 14) ++c; if (x >= 4) ++d; if (x >= 2) ++e; } writeln(a + b*(e-1) + c*(d-1) + d*(d-1)/2*(e-2)); }
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 M; scan(M); auto d = new long[](M); auto c = new long[](M); long cs; long ds; foreach (i ; 0 .. M) { scan(d[i], c[i]); cs += c[i]; ds += d[i] * c[i]; } auto ans = cs - 1 + (max(0, ds - 9) + 8) / 9; writeln(ans); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); alias 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 main() { long d,n; scan(d,n); if(n % 100) { writeln(n * pow(100, d)); } else { writeln((n + 1) * pow(100, d)); } }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; long calc(long n) { return n * (n - 1) / 2; } void main() { int n; scan(n); writeln(calc(n)); }
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); if (n == 1) { writeln("Hello World"); } else { int a; readV(a); int b; readV(b); writeln(a+b); } }
D
import std.stdio; import std.algorithm; import std.conv; import std.typecons; void main() { int n; scanf("%d", &n); auto ipt = new Tuple!(int, int)[n]; foreach(i;0..n) { int a, b; scanf("%d %d", &a, &b); ipt[i] = tuple(a, b); } ipt.sort!((a, b) => a[1] < b[1]); int t; foreach(x; ipt){ t += x[0]; if (t > x[1]) { write("No"); return; } } write("Yes"); }
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 readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!T;r.popFront;}} void main() { int n, z, w; readV(n, z, w); int[] a; readA(n, a); if (n == 1) writeln((a[$-1]-w).abs); else writeln(max((a[$-1]-w).abs, (a[$-2]-a[$-1]).abs)); }
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; bool check(string S, string T) { while (!S.empty && !T.empty) { if (S.length > T.length) { swap(S, T); } if (T.startsWith(S)) { T = T[S.length..$]; } else { return false; } } return true; } void main() { auto N = readln.chomp.to!int; auto S = readln.chomp; auto T = readln.chomp; bool loop = check(S, T); for (int i = 0, j = 0; !loop; i = (i + 1) % S.length.to!int, j = (j + 1) % T.length.to!int) { if (S[i] < T[j]) { break; } else if (S[i] > T[j]) { swap(S, T); break; } } int X = (N - 1) / S.length.to!int + 1; X = X * S.length.to!int; while (X > N || (N - X) % T.length != 0) { X -= S.length.to!int; } string ans = ""; while (ans.length < X) ans ~= S; while (ans.length < N) ans ~= T; ans.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.stdio; void main() { auto X = readln.chomp.to!int; auto dp = new int[](100); dp[] = 1 << 29; dp[0] = 0; foreach (i; 0..100) { foreach (j; 1..6) { if (i + j < 100) { dp[i + j] = min(dp[i + j], dp[i] + 1); } } } if (dp[X % 100] <= X / 100) { writeln(1); } else { writeln(0); } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(int[] xs) { auto left = new int[xs.length]; auto right = new int[xs.length]; left[0] = xs[0]; for (int i = 1; i < xs.length; i++) left[i] = gcd(xs[i], left[i-1]); right[$-1] = xs[$-1]; for (int i = cast(int)xs.length-2; i >= 0; i--) { right[i] = gcd(xs[i], right[i+1]); } int ans = max(left[$-2], // 右端を消す right[1]); // 左端を消す for (int i = 1; i <= xs.length - 2; i++) { // 両端以外を消す int g = gcd(left[i-1], right[i+1]); ans = max(ans, g); } return ans; } void main() { readint; auto xs = readints; writeln(calc(xs)); } T gcd(T)(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }
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 h = readln.chomp.to!int; auto w = readln.chomp.to!int; auto n = readln.chomp.to!int; auto p = max(h, w); writeln(n / p + (n % p > 0)); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int h, w; rd(h, w); auto c=new char[][](h, w); foreach(i; 0..h) c[i]=readln.chomp.to!(char[]); auto dd=[[1, 0], [0, 1], [-1, 0], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1]]; foreach(i; 0..h)foreach(j; 0..w)if(c[i][j]!='#'){ int cnt=0; foreach(d; dd){ auto ni=i+d[0], nj=j+d[1]; if(ni<0 || nj<0 || ni>=h || nj>=w) continue; if(c[ni][nj]=='#') cnt++; } c[i][j]=to!char(cnt+'0'); } foreach(i; 0..h) writeln(c[i]); } 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; import std.conv; import std.algorithm; import std.string; import std.file; import std.math; int main() { string l; l = readln().split[0]; int c = to!int(l); // iteration int d = 100000; // debt for(int i=0; i<c; i++){ d *= 1.05; if(d > d / 1000 * 1000) d += 1000; d = d / 1000 * 1000; } printf("%d\n", d); return 0; }
D
import std.algorithm; import std.array; import std.ascii; import std.bigint; import std.complex; import std.container; import std.conv; import std.functional; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } void readlnTo(T...)(ref T t) { auto s = readln().split(); assert(s.length == t.length); foreach(ref ti; t) { ti = s[0].to!(typeof(ti)); s = s[1..$]; } } const real eps = 1e-10; const long p = 1_000_000_000 + 7; void main(){ long n, m; readlnTo(n, m); auto c = new long[](n); foreach(i; iota(m)) { long a, b; readlnTo(a, b); ++c[a-1]; ++c[b-1]; } writeln(c.all!(a => a%2 == 0) ? "YES": "NO"); }
D
import std; import core.bitop; // dfmt off ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); void aryWrite(T = long)(T[] ary){ ary.map!(x => x.text()).join(' ').writeln(); } alias Pair = Tuple!(long, "X", long, "Y"); alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less); // dfmt on void main() { auto n = lread(); auto p = new Pair[](n); foreach (i; iota(n)) { long x, y; scan(x, y); p[i] = Pair(x, y); } auto skip = new Pair[][](n, 0); bool[Pair] pq; foreach (i; iota(n)) { foreach (j; iota(n)) { if (i != j) { skip[i] ~= Pair(p[j].X - p[i].X, p[j].Y - p[i].Y); pq[Pair(p[j].X - p[i].X, p[j].Y - p[i].Y)] = true; } } } long ans = n; foreach (key; pq.byKey) { long cnt; foreach (i; iota(n)) { if (skip[i].canFind(key)) { cnt++; } } ans = min(n - cnt, ans); } ans.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.string, std.conv; import std.range, std.algorithm, std.array, std.math; long n, m; void main() { scan(n, m); auto cmb = new long[][](n + 1, n + 1); auto dp = new long[][](n + 1, n + 1); foreach (i ; 0 .. n + 1) { cmb[i][0] = cmb[i][i] = 1; dp[i][0] = dp[i][i] = 1; } foreach (i ; 0 .. n + 1) { foreach (j ; 1 .. i) { cmb[i][j] = (cmb[i-1][j] + cmb[i-1][j-1]); if (cmb[i][j] >= m) cmb[i][j] -= m; dp[i][j] = (dp[i-1][j-1] + (j + 1) * dp[i-1][j] % m); if (dp[i][j] >= m) dp[i][j] -= m; } } debug { //writeln(cmb); //writeln(dp); } auto tpm = new long[](n*n + 1); auto tpm1 = new long[](n*n + 1); tpm[0] = tpm1[0] = 1; foreach (i ; 1 .. n*n + 1) { tpm[i] = tpm[i-1] * 2 % m; tpm1[i] = tpm1[i-1] * 2 % (m-1); } long f(int k) { long res; auto t = powmod(2, tpm1[n - k], m); foreach (x ; 0 .. k + 1) { //res += powmod(2, powmod(2, n - k, m - 1), m) * dp[k][x] % m * powmod(2, (n - k) * x, m) % m; res += t * dp[k][x] % m * tpm[(n-k)*x] % m; if (res >= m) res -= m; } return res; } long ans; foreach (int k ; 0 .. n.to!int + 1) { if (k & 1) { ans -= cmb[n][k] * f(k) % m; if (ans < 0) ans += m; } else { ans += cmb[n][k] * f(k) % m; if (ans >= m) ans -= m; } } writeln(ans); } // x^y % mod long powmod(long x, long y, long mod) { return y > 0 ? powmod(x, y>>1, mod)^^2 % mod * x^^(y&1) % mod : 1L; } 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.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 INF = 1L << 58; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto Q = s[1]; auto A = s[2]; auto B = s[3]; auto X = B ~ readln.split.map!(to!int).array; auto st1 = new LazySegmentTree!(long)(N+1); auto st2 = new LazySegmentTree!(long)(N+1); //AM.add(B, B, -INF-B); //AP.add(B, B, -INF+B); foreach (i; 0..N+1) { if (i == A) { st1.add(A, A, -A); st2.add(A, A, A); } else { st1.add(i, i, INF - i); st2.add(i, i, INF + i); } } foreach (i; 1..Q+1) { long m = min(st1.getVal(0, X[i]) + X[i], st2.getVal(X[i], N) - X[i]); st1.add(0, N, abs(X[i] - X[i-1])); st2.add(0, N, abs(X[i] - X[i-1])); long v1 = st1.getVal(X[i-1], X[i-1]); long v2 = st2.getVal(X[i-1], X[i-1]); long mv = min(m, v1 + X[i-1], v2 - X[i-1]); st1.add(X[i-1], X[i-1], -v1 + mv - X[i-1]); st2.add(X[i-1], X[i-1], -v2 + mv + X[i-1]); } long ans = INF; foreach (i; 0..N+1) ans = min(ans, st1.getVal(i, i) + i); foreach (i; 0..N+1) ans = min(ans, st2.getVal(i, i) - i); ans.writeln; } class LazySegmentTree(T) { T[] add_table; T[] min_table; int size; this(int n) { assert(bsr(n) < 29); size = 1 << (bsr(n) + 2); add_table = new T[](size); min_table = new T[](size); } void add(int a, int b, T num) { add(a, b, num, 0, 0, size/2-1); } void add(int a, int b, T num, int i, int l, int r) { if (a > r || b < l) { return; } if (a <= l && r <= b) { add_table[i] += num; min_table[i] += num; } else { add(a, b, num, i*2+1, l, (l+r)/2); add(a, b, num, i*2+2, (l+r)/2+1, r); min_table[i] = add_table[i] + min(min_table[i*2+1], min_table[i*2+2]); } } T getVal(int a, int b) { return getVal(a, b, 0, 0, size/2-1); } T getVal(int a, int b, int i, int l, int r) { if (a > r || b < l) { return INF * 5; } else if (a <= l && r <= b) { return min_table[i]; } return add_table[i] + min(getVal(a, b, i*2+1, l, (l+r)/2), getVal(a, b, i*2+2, (l+r)/2+1, r)); } }
D
import std.stdio; import std.algorithm; import std.string; import std.range; import std.array; import std.conv; import std.complex; import std.math; import std.ascii; import std.bigint; import std.container; import std.typecons; auto readInt() { return readInts()[0]; } auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } bool isPalindrome(string s) { foreach(i; 0..s.length) { if(s[i] != s[$-i-1]){ return false; } } return true; } void main() { auto s = readln().strip(); auto k = readInt(); if(s.length%k != 0) { writeln("NO"); return; } foreach(i; 0..k) { auto t = s[s.length/k*i..s.length/k*(i+1)]; if(!isPalindrome(t)){ writeln("NO"); return; } } writeln("YES"); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "a", long, "b"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { string s, t; scan(s, t); writeln(t ~ s); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; import std.math; void main() { int n; scan(n); auto a = readln.split.to!(int[]); a = [0] ~ a ~ [0]; long s; foreach (i ; 1 .. n + 2) { s += abs(a[i] - a[i-1]); } foreach (i ; 1 .. n + 1) { writeln(s - abs(a[i+1] - a[i]) - abs(a[i] - a[i-1]) + abs(a[i+1] - a[i-1])); } } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.string, std.array, std.conv; bool binarySearch(int n, int key, int[] x) { int l = 0, r = n, m; while (l < r) { m = (l + r) / 2; if (key == x[m]) return true; if (key < x[m]) { r = m; } else { l = m + 1; } } return false; } void main() { int n = readln.chomp.to!int; int[] s = readln.chomp.split.to!(int[]); int q = readln.chomp.to!int; int[] t = readln.chomp.split.to!(int[]); int cnt = 0; foreach (x; t) { if (binarySearch(n, x, s)) ++cnt; } cnt.writeln; }
D
void main() { long n = rdElem; long p = 1; foreach (i; 1 .. n+1) { p = p * i % mod; } p.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
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 X = readln.split.map!(to!int).array; auto L = readln.chomp.to!int; auto dp = new int[][](N, 30); // 頂点iから2^j回の移動で辿りつける最遠頂点 int left = 0; int right = 0; while (right < N) { if (X[right] - X[left] <= L) { right++; continue; } else { dp[left][0] = right-1; left++; } } while (left < N) dp[left][0] = N-1, left++; foreach (j; 0..29) { foreach (i; 0..N) { dp[i][j+1] = dp[dp[i][j]][j]; } } auto Q = readln.chomp.to!int; while (Q--) { auto s = readln.split.map!(to!int); auto a = s[0]-1; auto b = s[1]-1; if (b < a) swap(a, b); auto ans = 0; while (dp[a][0] < b) { auto k = 0; while (dp[a][k+1] < b) k++; ans += 2^^k; a = dp[a][k]; } writeln(ans+1); } }
D
import std.algorithm; import std.conv; import std.stdio; import std.string; void main() { int w = readln.chomp.to!int; int[] amida; amida.length = w + 1; foreach (i, ref n; amida) { n = i.to!int; } int n = readln.chomp.to!int; for (int i = 0; i < n; i++) { auto ab = readln.chomp.split(",").map!(to!int); swap(amida[ab[0]], amida[ab[1]]); } foreach (x; amida[1..$]) { x.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, std.bitmanip, std.datetime; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!long).array; long ans = N; int left = 0; long xxx = 0; foreach (i; 0..N) { while (left < i && (xxx + A[i] != (xxx ^ A[i]))) { xxx ^= A[left]; left += 1; } xxx ^= A[i]; ans += i - left; } ans.writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math; int[int] HS; void main() { auto N = readln.chomp.to!int; auto as = readln.split.to!(int[]); foreach (a; as) { if (a in HS) ++HS[a]; else HS[a] = 1; } int cnt; foreach (i, h; HS) { if (!h) continue; if (h > i) { cnt += h - i; } else if (h < i) { cnt += h; } } writeln(cnt); }
D
void main(){ int n = _scan(); int[] ans_arr = [1, 2, 4, 8, 16, 32, 64]; int ans; foreach(elm; ans_arr){ if(elm>n)break; ans = elm; } ans.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
/+ dub.sdl: name "C" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner, dcomp.algorithm; // import dcomp.numeric.primitive; int main() { auto sc = new Scanner(stdin); int n; sc.read(n); long f = 1; foreach (i; 0..n) { long x; sc.read(x); f = lcm(f, x); } writeln(f); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } version (X86) static if (__VERSION__ < 2071) { import core.bitop : bsf, bsr, popcnt; int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* 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) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/algorithm.d */ // module dcomp.algorithm; import std.range.primitives; import std.traits : isFloatingPoint, isIntegral; T binSearch(alias pred, T)(T l, T r) if (isIntegral!T) { while (r-l > 1) { T md = (l+r)/2; if (!pred(md)) l = md; else r = md; } return r; } T binSearch(alias pred, T)(T l, T r, int cnt = 60) if (isFloatingPoint!T) { foreach (i; 0..cnt) { T md = (l+r)/2; if (!pred(md)) l = md; else r = md; } return r; } E minimum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed) if (isInputRange!Range && !isInfinite!Range) { import std.algorithm, std.functional; return reduce!((a, b) => binaryFun!pred(a, b) ? a : b)(seed, range); } ElementType!Range minimum(alias pred = "a < b", Range)(Range range) { assert(!range.empty, "range must not empty"); auto e = range.front; range.popFront; return minimum!pred(range, e); } E maximum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed) if (isInputRange!Range && !isInfinite!Range) { import std.algorithm, std.functional; return reduce!((a, b) => binaryFun!pred(a, b) ? b : a)(seed, range); } ElementType!Range maximum(alias pred = "a < b", Range)(Range range) { assert(!range.empty, "range must not empty"); auto e = range.front; range.popFront; return maximum!pred(range, e); } Rotator!Range rotator(Range)(Range r) { return Rotator!Range(r); } struct Rotator(Range) if (isForwardRange!Range && hasLength!Range) { size_t cnt; Range start, now; this(Range r) { cnt = 0; start = r.save; now = r.save; } this(this) { start = start.save; now = now.save; } @property bool empty() { return now.empty; } @property auto front() { assert(!now.empty); import std.range : take, chain; return chain(now, start.take(cnt)); } @property Rotator!Range save() { return this; } void popFront() { cnt++; now.popFront; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/numeric/primitive.d */ // module dcomp.numeric.primitive; import std.traits; import std.bigint; T pow(T, U)(T x, U n) if (!isFloatingPoint!T && (isIntegral!U || is(U == BigInt))) { return pow(x, n, T(1)); } T pow(T, U)(T x, U n, T e) if (isIntegral!U || is(U == BigInt)) { while (n) { if (n & 1) e *= x; x *= x; n /= 2; } return e; } T lcm(T)(in T a, in T b) { import std.numeric : gcd; return a / gcd(a,b) * b; } T[3] extGcd(T)(in T a, in T b) if (!isIntegral!T || isSigned!T) { if (b==0) { return [T(1), T(0), a]; } else { auto e = extGcd(b, a%b); return [e[1], e[0]-a/b*e[1], e[2]]; } }
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; void main(){ int n = readln().chomp().to!int; for(int i=0;i<n;i++){ char[] s = readln().chomp().to!(char[]); if(s.length>=6){ for(int j=0;j<s.length-6;j++){ if(s[j]=='H'&&s[j+1]=='o'&&s[j+2]=='s'&&s[j+3]=='h'&&s[j+4]=='i'&&s[j+5]=='n'&&s[j+6]=='o') s[j+6] = 'a'; } } s.writeln(); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto n = RD!int; auto s = RD!string; auto a = new long[](n+1); foreach (i, c; s) { a[i+1] = a[i]; if (c == '1') { ++a[i+1]; } } size_t pos; foreach_reverse (i; 0..n) { if (a[i+1] * 2 != i+1) { pos = i+1; break; } } if (pos == n) { writeln(1); writeln(s); } else { writeln(2); writeln(s[0..pos], " ", s[pos..$]); } stdout.flush(); debug readln(); }
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; long[] as = readln.chomp.split.to!(long[]); long[] sums = [0]; foreach(a; as) sums ~= sums[$ - 1] + a; long best = sums[n]; foreach(s; sums){ long tmp = abs(sums[n] - s - s); best = min(best, tmp); } best.writeln; }
D
import std.stdio, std.string, std.conv, std.algorithm; void main(string[] args) { readln(); auto p = readln().chomp.split.map!(to!int); int a; for(int i; i<p.length; i++){ if(p[i]==i+1){a++; i++;} } a.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; auto as = readln.split.to!(uint[]); int[uint] MEMO; foreach (a; as) { if (a !in MEMO) MEMO[a] = 0; ++MEMO[a]; } uint[] ns; foreach (k, _; MEMO) ns ~= k; if (ns.length == 1) { if (ns[0] == 0) { writeln("Yes"); } else { writeln("No"); } } else if (ns.length == 2) { if (0 in MEMO && MEMO[0] >= N/3 && MEMO[0] <= N/3+1) { writeln("Yes"); } else { writeln("No"); } } else if (ns.length == 3) { auto a = ns[0]; auto b = ns[1]; auto c = ns[2]; if (a^b^c) goto ng; foreach (n; ns) { if (MEMO[n] < N/3 || N/3+1 <= MEMO[n]) goto ng; } writeln("Yes"); return; ng: writeln("No"); } 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); } string[] M; long H, W; bool[][] used; void main() { scan(H, W); used = new bool[][](H, W); foreach (_; 0 .. H) M ~= readln().chomp; long sum; foreach (y; 0 .. H) foreach (x; 0 .. W) { auto t = dfs(y, x); sum += t[0] * t[1]; } writeln(sum); } Tuple!(long, long) dfs(long y, long x) { if (used[y][x]) return Tuple!(long, long)(0, 0); used[y][x] = true; long w, b; if (M[y][x] == '.') w = 1; else b = 1; if (0 <= y - 1 && !used[y - 1][x] && M[y][x] != M[y - 1][x]) { auto tmp = dfs(y - 1, x); w += tmp[0]; b += tmp[1]; } if (y + 1 < H && !used[y + 1][x] && M[y][x] != M[y + 1][x]) { auto tmp = dfs(y + 1, x); w += tmp[0]; b += tmp[1]; } if (0 <= x - 1 && !used[y][x - 1] && M[y][x] != M[y][x - 1]) { auto tmp = dfs(y, x - 1); w += tmp[0]; b += tmp[1]; } if (x + 1 < W && !used[y][x + 1] && M[y][x] != M[y][x + 1]) { auto tmp = dfs(y, x + 1); w += tmp[0]; b += tmp[1]; } return Tuple!(long, long)(w, b); }
D
import std.stdio, std.conv, std.string; void main() { auto ip = readln.split.to!(int[]); if(ip[0] % 3 == 0 || ip[1] % 3 == 0 || (ip[0] + ip[1]) % 3 == 0){ writeln("Possible"); } else { writeln("Impossible"); } }
D
import std.stdio, std.string, std.conv; import std.array, std.algorithm, std.range; void main() { immutable string[7] W = [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]; immutable int[12] D = [0,31,29,31,30,31,30,31,31,30,31,30]; foreach(s;stdin.byLine()) { auto md = s.split().map!(to!int); immutable m=md[0],d=md[1]; if(m==0 && d==0) break; W[(D[0..m].reduce!"a+b"()+d+2)%7].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); writeln(n % 500 <= a ? "Yes" : "No"); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.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 d = readln.chomp.split.to!(int[]); if (d[0] == d[1] && d[1] == d[2]) writeln("Yes"); else writeln("No"); }
D