code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto rd = readln.split.to!(int[]), a = rd[0], b = rd[1]; writeln((a-1)*(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; int[] stk; foreach (_; 0..N) { auto a = readln.chomp.to!int; if (stk.empty || stk[$-1] >= a) { stk ~=a; continue; } int l, r = stk.length.to!int-1; while (l+1 < r) { auto mid = (l+r)/2; if (stk[mid] < a) { r = mid; } else { l = mid; } } if (stk[l] < a) { stk[l] = a; } else { stk[r] = a; } } writeln(stk.length); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto kn = readln.split.to!(int[]); auto K = kn[0]; auto N = kn[1]; auto as = readln.split.to!(int[]); int max_a = K - as[$-1] + as[0]; foreach (i; 0..N-1) max_a = max(max_a, as[i+1] - as[i]); writeln(K - max_a); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto a = readln.chomp.to!int; auto b = readln.chomp.to!int; auto c = readln.chomp.to!int; auto d = readln.chomp.to!int; auto e = readln.chomp.to!int; auto k = readln.chomp.to!int; auto ts = [a, b, c, d, e]; foreach (i; 0..5) { foreach (j; i+1..5) { if (ts[j] - ts[i] > k) { writeln(":("); return; } } } writeln("Yay!"); }
D
import std.stdio; import std.conv; import std.array; void main() { auto reader = readln.split; int N = reader[0].to!int; int D = reader[1].to!int; int range = D * 2 + 1; int ans = N / range; if (N % range > 0){ ans++; } writeln(ans); }
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; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; // dfmt on void main() { long N = lread(); auto S = sread(); long l; long level; foreach (c; S) { if (c == '(') level++; if (c == ')') { if (level) { level--; } else { l++; } } } long r = level; writeln(repeat('(').take(l).array, S, repeat(')').take(r).array); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { auto s = read!string; writeln("2018", s[4..$]); }
D
import std.stdio, std.algorithm; void main() { int A, B, C, D, P; scanf("%d\n", &A); scanf("%d\n", &B); scanf("%d\n", &C); scanf("%d\n", &D); scanf("%d\n", &P); writeln( min(A * P, B + D * max(0, P - C)) ); }
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) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } void main() { readln.chomp.to!int.rep!(() => readln.chomp.to!long).fold!((a, b) { long t1 = min(a.back, b); long t2 = max(0, (b - a.back)/2); return [a.front+t1+t2, b-t1-2*t2]; })([0L, 0L]).front.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
import std.stdio; import std.string, std.conv; import std.array, std.algorithm; import std.math; void main() { int[] weight = [200, 300, 500, 200 * 5, 300 * 4, 500 * 3]; int[] price = [380, 550, 850, 1520, 1870, 2244]; int[5000+1] dp = cast(int)1e9; dp[0] = 0; foreach(i; 0 .. 6) { for(int j = weight[i]; j <= 5000; j++) { dp[j] = min(dp[j], dp[j - weight[i]] + price[i]); } } for(int N; (N = readln.chomp.to!int) != 0; ) { dp[N].writeln(); } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; // dfmt on void main() { long N = lread(); auto S = sread(); auto T = sread(); outer: foreach_reverse (i; 0 .. N + 1) { foreach (j; 0 .. i) { if (S[N - i + j] != T[j]) continue outer; } writeln(2 * N - i); return; } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.container; import std.array; import std.math; import std.range; import std.typecons; import std.ascii; import std.format; void main() { auto l = readln.chomp; if (l.equal(l.retro)) { writeln = "Yes"; } else { writeln = "No"; } }
D
import std.algorithm; import std.array; import std.bitmanip; import std.conv; import std.stdio; import std.string; import std.typecons; T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; } void main() { immutable ip = readln.split.to!(ulong[]); immutable n = ip[0], k = ip[1]; ulong ret = k == 1 ? 0 : n-k; writeln(ret); }
D
import std.stdio; import std.algorithm; import std.range; import std.conv; int f(int n, int m, int k) { if (n < m) return max(0, n-k+1); return f(n%m, m, k) + (n/m)*(m-k); } void main() { string[] input = split(readln()); int n = to!int(input[0]); int k = to!int(input[1]); long ans = 0; foreach (i; k+1 .. n+1) { ans += f(n,i,k); if (k == 0) ans--; } writeln(ans); }
D
import std.stdio, std.string, std.conv; import std.typecons; import std.algorithm, std.array, std.range, std.container; import std.math; void main() { auto abc = readln.split.to!(int[]); if (abc[0] > abc[1]) { auto tmp = abc[0]; abc[0] = abc[1]; abc[1] = tmp; } writeln(abc[0] <= abc[2] && abc[2] <= abc[1] ? "Yes" : "No"); }
D
import std.stdio; void main() { while(true) { uint[][][char] mapping; char[3][3] field; foreach (c; ['b', 'w', '+']) mapping[c] = new uint[][](3, 3); for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++) { scanf(j == 2 ? "%c\n" : "%c", &field[i][j]); if (field[i][j] == '0') goto end; } for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++) { mapping[field[i][j]][0][j]++; mapping[field[i][j]][1][i]++; } for(int i = 0; i < 3; i++) { mapping[field[i][i]][2][0]++; mapping[field[2 - i][i]][2][1]++; } for(int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) foreach (c; ['b', 'w']) if (mapping[c][i][j] == 3) {writeln(c); goto next;} writeln("NA"); next:; } end:; }
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 T = readln.chomp.to!int; while (T--) solve; } void solve() { auto N = readln.chomp.to!int; auto S = readln.chomp; bool ok = true; foreach (i; 0..N/2) { char x = S[i]; char y = S[N-i-1]; if (x != y && abs(x - y) != 2) ok = false; } writeln(ok ? "YES" : "NO"); }
D
import std.stdio; import std.conv; import std.string; import std.math; import std.random; import std.range; import std.algorithm; import std.functional; import core.bitop; import std.bigint; import std.typecons; import std.container; void main() { auto input = readln.split.to!(int[]); auto A = input[0], B = input[1], C = input[2], D = input[3]; ((A + B) > (C + D) ? "Left" : (A + B) < (C + D) ? "Right" : "Balanced").writeln; }
D
void main() { int n = readln.chomp.to!int; int cnt; for (int i = 1; i <= n; i += 2) { int divisor; for (int j = 1; j * j <= i; j += 2) { if (i % j == 0) { ++divisor; if (j * j != i) ++divisor; } } if (divisor == 8) ++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.container; import std.typecons;
D
void main(){ int a = _scan(); string s = readln().chomp(); if(a>=3200)s.writeln(); else writeln("red"); } 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.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { int N; string s; scan(N); scan(s); long r, g, b; foreach (ch ; s) { if (ch == 'R') r++; if (ch == 'G') g++; if (ch == 'B') b++; } long ans = r * g * b; foreach (i ; 0 .. N) { foreach (j ; i + 1 .. N) { if ((i + j) % 2) continue; auto k = (i + j) / 2; if (s[i] != s[k] && s[k] != s[j] && s[i] != s[j]) ans--; } } writeln(ans); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.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 s = readln.chomp.to!int; writeln((s/3600).to!string ~ ":" ~ (s%3600/60).to!string ~ ":" ~ (s%3600%60).to!string); }
D
import std.stdio, std.algorithm; void main(){ foreach(char[] line; lines(stdin)){ foreach(i; 0..26){ foreach(ref c; line){ if('a' <= c && c <= 'z') c = c == 'z' ? 'a' : cast(char)(c + 1); } if(line.countUntil("the") + 1 || line.countUntil("this") + 1 || line.countUntil("that") + 1){ line.write; break; } } } }
D
import std.stdio, std.array, std.conv, std.algorithm; void main() { string s; while((s=readln()).length != 0) { string[] input = split(s); int pos = 1; int carry = 0; int lena = to!int(input[0].length); int lenb = to!int(input[1].length); while(true) { int a = lena<pos ? 0 : to!int(input[0][lena-pos]-'0'); int b = lenb<pos ? 0 : to!int(input[1][lenb-pos]-'0'); int res = a + b + carry; carry = res<10 ? 0 : 1; if(input[0].length <= pos && input[1].length <= pos) { if(carry) pos++; break; } pos++; } writeln(pos); } }
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 x; readV(x); writeln(x == 3 || x == 5 || x == 7 ? "YES" : "NO"); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp.map!(x=>(x-'0').to!int).array; auto op = ['-', ' ', '+']; a:foreach (op1; iota(-1, 2, 2)) { foreach (op2; iota(-1, 2, 2)) { foreach (op3; iota(-1, 2, 2)) { auto sum = n[0] + op1 * n[1] + op2 * n[2] + op3 * n[3]; if (sum == 7) { writeln(n[0], op[op1+1] , n[1], op[op2+1], n[2], op[op3+1], n[3], "=7"); break a; } } } } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; void main() { string input; int num; int[] train; while ((input = readln().chomp()).length != 0) { num = input.to!int(); if (num == 0) { writeln(train[$ - 1]); train.length--; } else { train ~= num; } } }
D
import std.stdio; import std.algorithm; import std.string; import std.container; import std.typecons; void log(A...)(A arg) { stderr.writeln(arg); } void main() { long N, K; scanf("%d %d\n", &N, &K); long f(long n) { return n + 1 + (n / (K - 1)); } long x = 0; for (long n = 1; n < N; n++) { x = f(x); } writeln(x); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { char c; scan(c); writeln((c + 1).to!char); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto a = RD!string; auto b = RD!string; long x = a == "H" ? 1 : 0; long y = b == "H" ? 1 : 0; writeln(x ^ y ? "D" : "H"); 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; } T read(T)(){ return read.to!T; } T[] read(T)(long n){ return n.iota.map!(_ => read!T).array; } T[][] read(T)(long m, long n){ return m.iota.map!(_ => read!T(n)).array; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ int n = read.to!int; int m = read.to!int; long[] xs; foreach(i; 0 .. n) xs ~= read.to!long; int[long] xcnt; foreach(x; xs){ if(x !in xcnt) xcnt[x] = 0; xcnt[x] += 1; } int[] as, bs; // a: 同じ数のペアの個数 b: 全個数 as = new int[](m); bs = new int[](m); long[] ks = xcnt.keys; foreach(x; ks){ int k = x % m; as[k] += xcnt[x] / 2; bs[k] += xcnt[x]; } long ans; foreach(k; 0 .. m){ if(m - k < k) continue; if(k == 0) ans += bs[k] / 2; else if(k + k == m) ans += bs[k] / 2; else{ if(bs[k] >= bs[m - k]){ ans += bs[m - k]; ans += min((bs[k] - bs[m - k]) / 2, as[k]); } else{ ans += bs[k]; ans += min((bs[m - k] - bs[k]) / 2, as[m - k]); } } } ans.writeln; }
D
void main() { problem(); } void problem() { const A = scan!long; const B = scan!long; const N = scan!long; long calc(long x) { return (A*x / B) - A*(x/B); } long solve() { if (B > N) { return calc(N); } const k = N / B; return calc(B * k - 1); } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } 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; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); import std.bigint, std.functional; // -----------------------------------------------
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; import std.math; 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 h, w, d; readV(h, w, d); auto a = new int[][](h, w); foreach (i; 0..h) readA(w, a[i]); auto hw = h*w, x = new int[](hw), y = new int[](hw); foreach (i; 0..h) foreach (j; 0..w) { x[a[i][j]] = i; y[a[i][j]] = j; } auto m = new int[](h*w+1); foreach (i; d+1..hw+1) m[i] = m[i-d] + (x[i]-x[i-d]).abs + (y[i]-y[i-d]).abs; int q; readV(q); foreach (_; 0..q) { int l, r; readV(l, r); writeln(m[r]-m[l]); } }
D
import std.stdio, std.string, std.conv; import std.typecons; import std.algorithm, std.array, std.range, std.container; import std.math; void main() { auto X = readln.split[0].to!long; long money = 100; foreach (i; 1 .. 4000) { money = cast(long) floor(money * 1.01); if (money >= X) { writeln(i); break; } } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } void main() { string s; while ((s = readln.chomp) != null) { auto ab = s.split.map!(to!int).array; int a = ab[0], b = ab[1]; writeln(gcd(a, b)); } }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { string s; scan(s); writeln(s.count!"a == '1'"); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { writeln("ABC" ~ readln.chomp); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; 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 a = new long[](N+1); foreach (i; 0..M) { auto L = RD-1; auto R = RD; ++a[L]; --a[R]; } foreach (i; 0..N) { a[i+1] += a[i]; } long ans; foreach (i; 0..N) { if (a[i] == M) { ++ans; } } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio; import std.algorithm; import std.math; import std.string; import std.conv; import std.range; void main() { while (true) { int sum = readln.chomp.to!int; if (sum == 0) break; foreach (i; 0..9) { sum -= readln.chomp.to!int; } sum.writeln; } }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto rd1 = readln.split.to!(size_t[]), n = rd1[0], m = rd1[1]; auto r = new int[](n); foreach (_; 0..m) { auto rd2 = readln.split.to!(size_t[]), a = rd2[0]-1, b = rd2[1]-1; ++r[a]; ++r[b]; } foreach (ri; r) writeln(ri); }
D
import std.stdio; import std.algorithm; import std.string; import std.functional; import std.conv; void main(){ readln.split.map!(to!int).unaryFun!(a => a[0] < a[1] ? "a < b" : a[0] > a[1] ? "a > b" : "a == b").writeln; }
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 n=scanElem; if(n>=1200)end("ARC");end("ABC"); }
D
import std.stdio, std.string, std.conv, std.algorithm, std.ascii; void main() { foreach(_; 0 .. readln.chomp.to!int) { auto line = readln; auto keys = line.split.filter!(str => str.length == 4); loop:foreach(a; [0, 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25]) { foreach(b; 0 .. 26) { foreach(key; keys) { string decoded = key.decode(a, b); if(decoded == "this" || decoded == "that") { line.decode(a, b).write; break loop; } } } } } } string decode(string encoded, int a, int b) { string decoded; foreach(c; encoded) { if(c.isAlpha) { decoded ~= (a * (c - 'a') + b) % 26 + 'a'; } else { decoded ~= c; } } return decoded; }
D
import std.stdio; import std.conv; import std.string; void main() { int[] input = readln.split.to!(int[]); int sx = input[0]; int sy = input[1]; int tx = input[2]; int ty = input[3]; char[] ans; foreach (i; 0..(ty - sy)) ans ~= 'U'; foreach (i; 0..(tx - sx)) ans ~= 'R'; foreach (i; 0..(ty - sy)) ans ~= 'D'; foreach (i; 0..(tx - sx)) ans ~= 'L'; ans ~= 'L'; foreach (i; 0..(ty - sy + 1)) ans ~= 'U'; foreach (i; 0..(tx - sx + 1)) ans ~= 'R'; ans ~= 'D'; ans ~= 'R'; foreach (i; 0..(ty - sy + 1)) ans ~= 'D'; foreach (i; 0..(tx - sx + 1)) ans ~= 'L'; ans ~= 'U'; ans.writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto N = RD; auto K = RD; auto ans = N % K; ans = min(ans, K - ans); writeln(ans); stdout.flush; debug readln; }
D
import std.stdio; import std.range; import std.conv; import std.algorithm; import std.string; import std.array; import std.conv; void main() { readln(); writeln(readln().strip.split(" ").reverse.join(" ")); }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; import std.string; import std.math; void main() { int n = readln.chomp.to!int; int[] arr = readln.chomp.split.map!(to!int).array; long ans = 1000000000000007; for (int i = 0; i < 2; ++i) { auto a = arr.dup; long tmp; long[] sum_a = new long[](n); // 偶数番目が正の数 if (i == 0) { for (int j = 0; j < n; ++j) { sum_a[j] = a[j] + (j == 0 ? 0 : sum_a[j - 1]); if (j % 2 == 0 && sum_a[j] <= 0) { long dif = 1 - sum_a[j]; a[j] += dif; sum_a[j] += dif; tmp += dif.abs; } else if (j % 2 == 1 && sum_a[j] >= 0) { long dif = -1 - sum_a[j]; a[j] += dif; sum_a[j] += dif; tmp += dif.abs; } } } else { for (int j = 0; j < n; ++j) { sum_a[j] = a[j] + (j == 0 ? 0 : sum_a[j - 1]); if (j % 2 == 1 && sum_a[j] <= 0) { long dif = 1 - sum_a[j]; a[j] += dif; sum_a[j] += dif; tmp += dif.abs; } else if (j % 2 == 0 && sum_a[j] >= 0) { long dif = -1 - sum_a[j]; a[j] += dif; sum_a[j] += dif; tmp += dif.abs; } } } ans = ans.min(tmp); } ans.writeln; return; }
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 S = readln.chomp.to!(char[]); int r; foreach (i; 1..N-1) { int[char] CNT; foreach (c; S[0..i]) { CNT[c] = 1; } foreach (c; S[i..N]) { if (c in CNT) ++CNT[c]; } int rr; foreach (_, v; CNT) if (v > 1) ++rr; r = max(r, rr); } writeln(r); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); writeln((N + 1) / 2 - 1); }
D
import std.stdio, std.conv, std.string; import std.algorithm, std.array, std.container; import std.numeric, std.math; import core.bitop; T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T)(in string str) { return str.split.to!(T[]); } 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; } long mod = pow(10, 9) + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } struct UnionFind { void init(long n) { par = new long[](n); foreach (i; 0..n) par[i] = i; } long root(long i) { return par[i] == i ? i : (par[i] = root(par[i])); } bool same(long i, long j) { return root(i) == root(j); } void unite(long i, long j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; } long[] par; } void main() { auto K = RD!long; auto A = RD!long; auto B = RD!long; auto c = max(B - A, 2); auto d = min(K, A - 1); long ans = d + 1; K -= d; ans += c * (K / 2); ans += K % 2; writeln(ans); stdout.flush(); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { string s; scan(s); int N = s.length.to!int; foreach (l ; 0 .. N) { foreach (r ; l .. N + 1) { auto t = s[0 .. l] ~ s[r .. N]; if (t == "keyence") { writeln("YES"); return; } } } writeln("NO"); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!long; long n = long.max; foreach (_; 0..5) n = min(n, readln.chomp.to!long); writeln((N + n - 1) / n + 4); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto rd = readln.split.to!(int[]), a = rd[0], b = rd[1], c = rd[2], d = rd[3]; writeln(max(a*b, c*d)); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto N = RD; long ans; if (N != 1) { ans = 10; ans.modpow(N); long cnt1 = 9; cnt1.modpow(N); cnt1.modm(2); long cnt2 = 8; cnt2.modpow(N); ans.mods(cnt1); ans.moda(cnt2); } writeln(ans); stdout.flush; debug readln; }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; import std.datetime; import std.numeric; import std.math; import std.string; string my_readln() { return chomp(readln()); } void main() { auto tokens = my_readln().split(); auto N = tokens[0].to!uint; uint[] h; foreach (token; my_readln().split()) { h ~= token.to!uint; } uint ans; while (true) { uint l = N, r = N; foreach (i; 0..N) { if (h[i] >= 1) { l = i; break; } } if (l == N) break; foreach (j; l+1..N) { if (h[j] == 0) { r = j; break; } } foreach (i; l..r) { --h[i]; } ++ans; } writeln(ans); stdout.flush(); }
D
import std.stdio; import std.string; import std.conv; void main(){ (readln.chomp.to!int ^^ 3).writeln; }
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 main() { long N = lread(); auto s = sread(); long[2] a; foreach (c; s) { a[c / 'R']++; } writeln((a[0] < a[1]) ? "Yes" : "No"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nab = readln.split.to!(int[]); auto N = nab[0]; auto A = nab[1]; auto B = nab[2]; auto S = readln.chomp; int b, n; foreach (c; S) { switch (c) { case 'a': if (n < A+B) { writeln("Yes"); ++n; } else { writeln("No"); } break; case 'b': if (n < A+B && b < B) { ++b; ++n; writeln("Yes"); } else { writeln("No"); } break; default: writeln("No"); } } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; import std.range; import std.math; void main(){ auto a=readln.chomp; writeln("A",a[8],"C"); }
D
void main(){ int x, y; scanf("%d %d", &x, &y); (x + y/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.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; immutable int INF = 1 << 29; void main() { auto T = readln.chomp.to!int; while (T--) { auto s = readln.split.map!(to!long); auto N = s[0]; auto K = s[1]; long cnt = 0; while (N > 0) { long m = N % K; if (m == 0) { N /= K; cnt += 1; } else { N -= m; cnt += m; } } cnt.writeln; } }
D
//dlang template---{{{ import std.stdio; import std.conv; import std.string; import std.array; import std.algorithm; import std.typecons; import std.math; import std.range; // MIT-License https://github.com/kurokoji/nephele class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } //Digit count---{{{ int DigitNum(int num) { int digit = 0; while (num != 0) { num /= 10; digit++; } return digit; } //}}} //}}} void main() { Scanner sc = new Scanner; int N, A, B; sc.scan(N, A, B); writeln(min(N * A, B)); }
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 t = RD!int; auto nk = new long[2][](t); foreach (i; 0..t) { nk[i] = [RD, RD]; } foreach (i; 0..t) { auto n = nk[i][0]; auto k = nk[i][1]; long ans; while (n != 0) { auto x = n % k; if (x == 0) { n /= k; ++ans; } else { n -= x; ans += x; } } writeln(ans); } stdout.flush(); debug readln(); }
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() { string s; scan(s); auto n = s.length.to!int; bool check(int x) { if (2*x <= n) { return true; } foreach (i ; n - x .. x) { if (s[i] != s[n - x]) { return false; } } return true; } int ok = 1, ng = n + 1; while (abs(ok - ng) > 1) { int mid = (ok + ng) / 2; (check(mid) ? ok : ng) = mid; } writeln(ok); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; import std.range; void main(){ auto Q=readln.split.to!(int[]),A=Q[0],B=Q[1]; if(A==B)writeln("Draw"); else if(A==1&&A<B)writeln("Alice"); else if(B==1&&A>B)writeln("Bob"); else if(A<B)writeln("Bob"); else if(A>B)writeln("Alice"); }
D
void main() { int[] tmp = readln.split.to!(int[]); int a = tmp[0], b = tmp[1]; writeln((a + b) % 24); } 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.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; alias Edge = Tuple!(int, "to", int, "idx"); void main() { int N; scan(N); auto adj = new Edge[][](N, 0); foreach (i ; 0 .. N - 1) { int ai, bi; scan(ai, bi); ai--, bi--; adj[ai] ~= Edge(bi, i); adj[bi] ~= Edge(ai, i); } auto ans = new int[](N - 1); ans[] = -1; int K; void dfs(int v, int p, int c) { int col = 1; foreach (e ; adj[v]) if (e.to != p) { if (col == c) col++; ans[e.idx] = col; K = max(K, col); dfs(e.to, v, col); col++; } } dfs(0, -1, -1); writeln(K); foreach (i ; 0 .. N - 1) { writeln(ans[i]); } } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
void main(){ int[int] dic; int ans; int[] colors = inln(); foreach(elm; colors){ dic[elm]++; if(dic.get(elm,0)==1)ans++; } ans.writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range; const long mod = 10^^9+7; // 1要素のみの入力 T inelm(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] inln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split())ln ~= elm.to!T(); return ln; }
D
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 A = RD; auto B = RD; writeln(A*B); stdout.flush; debug readln; }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp.to!int; writeln(((n-1)/111+1)*111); }
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 p = readln.chomp.split.to!(int[]); int cnt; foreach (i; 0..n) { if (p[i] != i + 1) { cnt++; } } if (cnt <= 2) { writeln("YES"); } else { writeln("NO"); } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.container; import std.datetime; void main() { auto pat = ['z', '.', '?', '!', ' ']; foreach (line; stdin.byLine) { auto str = line.chomp; if (str.length % 2) { writeln("NA"); continue; } int c; string res = ""; foreach (i, s; str) { if (i % 2) { if (s >= '1' && s <= '5') { if (c == 'z') { c = pat[s - '1']; } else { c += s - '1'; } res ~= c.to!char; } else { res = "NA"; break; } } else { if (s >= '1' && s <= '6') { c = 'a' + (s - '1') * 5; } else { res = "NA"; break; } } } res.writeln; } }
D
import std.algorithm; import std.array; import std.stdio; import std.conv; import std.string; import std.range; void main(){ int[] ary; int[] temp1, temp2; while(true){ auto input = readln.split; int n = input[0].to!int; int r = input[1].to!int; if(n == 0 && r == 0){ break; } ary = iota(1, n + 1).retro.array; temp1 = new int[n]; temp2 = new int[n]; foreach(i; 0..r){ auto input2 = readln.split; int p = input2[0].to!int; int c = input2[1].to!int; temp1[0..(p-1)] = ary[0..(p-1)]; temp2[0..c] = ary[(p-1)..(p-1) + c]; ary[c..c + (p-1)] = temp1[0..(p-1)]; ary[0..c] = temp2[0..c]; } ary[0].writeln; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto Q = readln.chomp.to!int; foreach (_; 0..Q) { auto X = readln.chomp; auto Y = readln.chomp; auto DP = new int[][](X.length, Y.length); foreach_reverse (i; 0..X.length) { foreach_reverse (j; 0..Y.length) { if (X[i] == Y[j]) { DP[i][j] = max( DP[i][j], (i+1 != X.length && j+1 != Y.length ? DP[i+1][j+1] : 0) + 1 ); } else { DP[i][j] = max( i+1 != X.length ? DP[i+1][j] : 0, j+1 != Y.length ? DP[i][j+1] : 0 ); } } } writeln(DP[0][0]); } }
D
import std.stdio, std.conv, std.array,std.string,std.algorithm; void main() { auto input1=readln.chomp,input2=readln.chomp; writeln((2*input2.to!int)-input1.to!int); }
D
void main() { problem(); } void problem() { auto N = scan!int; auto STONES = scan!string.to!(char[]); long solve() { long left = 0; long right = N-1; long ans; while(true) { if (STONES[right] != 'R') right--; if (STONES[left] != 'W') left++; if (right <= left) break; if (STONES[right] == 'R' && STONES[left] == 'W') { right--; left++; ans++; } } return ans; } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } 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; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
import std.stdio; long fact(int N) { if (N == 0) return 1L; return N * fact(N - 1); } void main() { int N; scanf("%d\n", &N); writeln(fact(N)); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto S = RD!string; long ans = 1; foreach (i; 1..N) { if (S[i] != S[i-1]) ++ans; } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio; import std.conv; import std.string; import std.range; import std.algorithm; void main(){ while(true){ int n = readln().chomp().to!int(); if(n == 0){ break; } string str = readln().chomp(); foreach(i; 0..n){ string res; int count = 1; char current = '$'; foreach(c; str){ if(current == '$'){ current = c; }else if(current == c){ ++count; }else{ res ~= text(count, current); current = c; count = 1; } } res ~= text(count, current); str = res; } str.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() { int x = readint; int a = readint; int b = readint; int donut = (x - a) / b; writeln(x - a - b * donut); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; import std.typecons : Tuple; ulong safeMod(long x, long m) @safe pure nothrow @nogc { x %= m; if (x < 0) x += m; return x; } long ctPowMod(long x, long n, int m) @safe pure nothrow @nogc { if (m == 1) return 0; uint _m = cast(uint) m; ulong r = 1; ulong y = safeMod(x, m); while (n) { if (n & 1) r = (r * y) % _m; y = (y * y) % _m; n >>= 1; } return r; } bool ctIsPrime(int n) @safe pure nothrow @nogc { if (n <= 1) return false; if (n == 2 || n == 7 || n == 61) return true; if (n % 2 == 0) return false; long d = n - 1; while (d % 2 == 0) d /= 2; foreach (a; [2, 7, 61]) { long t = d; long y = ctPowMod(a, t, n); while (t != n - 1 && y != 1 && y != n - 1) { y = y * y % n; t <<= 1; } if (y != n - 1 && t % 2 == 0) { return false; } } return true; } enum bool isPrime(int n) = ctIsPrime(n); Tuple!(long, long) invGcd(long a, long b) @safe pure nothrow @nogc { a = safeMod(a, b); if (a == 0) return Tuple!(long, long)(b, 0); long s = b, t = a, m0 = 0, m1 = 1; while (t) { long u = s / t; s -= t * u; m0 -= m1 * u; long tmp = s; s = t; t = tmp; tmp = m0; m0 = m1; m1 = tmp; } if (m0 < 0) m0 += b / s; return Tuple!(long, long)(s, m0); } int ctPrimitiveRoot(int m) @safe pure nothrow @nogc { if (m == 2) return 1; if (m == 167_772_161) return 3; if (m == 469_762_049) return 3; if (m == 754_974_721) return 11; if (m == 998_244_353) return 3; int[20] divs; divs[0] = 2; int cnt = 1; int x = (m - 1) / 2; while (x % 2 == 0) x /= 2; for (int i = 3; (cast(long) i) * i <= x; i += 2) if (x % i == 0) { divs[cnt++] = i; while (x % i == 0) x /= i; } if (x > 1) divs[cnt++] = x; for (int g = 2;; g++) { bool ok = true; foreach (i; 0 .. cnt) if (ctPowMod(g, (m - 1) / divs[i], m) == 1) { ok = false; break; } if (ok) return g; } } enum primitiveRoot(int m) = ctPrimitiveRoot(m); long powMod(long x, long n, long m) @safe pure nothrow @nogc { assert(0 <= n && 1 <= m); if (m == 1) return 0; ulong r = 1, y = safeMod(x, m); while (n) { if (n & 1) r = (r * y) % m; y = (y * y) % m; n >>= 1; } return r; } long invMod(long x, long m) @safe pure nothrow @nogc { assert(1 <= m); auto z = invGcd(x, m); assert(z[0] == 1); return z[1]; } Tuple!(long, long) crt(long[] r, long[] m) @safe pure nothrow @nogc { assert(r.length == m.length); long r0 = 0, m0 = 1; foreach (i; 0 .. r.length) { assert(1 <= m[i]); long r1 = safeMod(r[i], m[i]); long m1 = m[i]; if (m0 < m1) { auto tmp = r0; r0 = r1; r1 = tmp; tmp = m0; m0 = m1; m1 = tmp; } if (m0 % m1 == 0) { if (r0 % m1 != r1) return Tuple!(long, long)(0, 0); continue; } long g, im; { auto tmp = invGcd(m0, m1); g = tmp[0]; im = tmp[1]; } long u1 = m1 / g; if ((r1 - r0) % g) return Tuple!(long, long)(0, 0); long x = (r1 - r0) / g % u1 * im % u1; r0 += x * m0; m0 *= u1; if (r0 < 0) r0 += m0; } return Tuple!(long, long)(r0, m0); } long floorSum(long n, long m, long a, long b) @safe pure nothrow @nogc { long ans; if (m <= a) { ans += (n - 1) * n * (a / m) / 2; a %= m; } if (m <= b) { ans += n * (b / m); b %= m; } long y_max = (a * n + b) / m, x_max = (y_max * m - b); if (y_max == 0) return ans; ans += (n - (x_max + a - 1) / a) * y_max; ans += floorSum(y_max, a, m, (a - x_max % a) % a); return ans; } void main() { auto N = readln.chomp.to!long * 2; long[] ps; for (long p = 1; p^^2 <= N; ++p) if (N%p == 0) { ps ~= p; if (N/p != p) ps ~= N/p; } long res = long.max; foreach (p; ps) { auto ret = crt([0, -1], [p, N/p]); if (ret[0]) res = min(res, ret[0]); } writeln(res == long.max ? N : res); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); alias 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 n, x; scan(n, x); long[] m = new long[n]; foreach (ref e; m) { e = lread(); } foreach (e; m) { x -= e; } long min_cost = m[0]; foreach (e; m) { min_cost = min(e, min_cost); } writeln(n + x / min_cost); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { string s; readV(s); string t; readV(t); auto n = s.length; foreach (_; 0..n) { if (s == t) { writeln("Yes"); return; } s = s[$-1] ~ s[0..$-1]; } 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, core.bitop; enum inf = 1<<30; enum full = (1<<26) - 1; void main() { string s; scan(s); int n = s.length.to!int; long ans; void dfs(int i, long val, long d) { if (i == n) { debug { writeln(val + d); } ans += val + d; return; } dfs(i + 1, val, d*10 + s[i] - '0'); dfs(i + 1, val + d, s[i] - '0'); } dfs(1, 0, s[0] - '0'); 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
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; import std.typecons; import std.bigint; // import dcomp.foundation, dcomp.scanner; // import dcomp.container.deque; int main() { auto sc = new Scanner(stdin); int n, m, k; sc.read(n, m, k); m++; int[] a = new int[n]; a.each!((ref x) => sc.read(x)); long[] dp = a.map!(to!long).array; long[] ndp = new long[n]; int[] deq = new int[n]; foreach (int ph; 2..k+1) { ndp[] = -(10L^^18); int l = 0, r = 0; foreach (int i; 0..n) { if (l < r && deq[l] == i-m) { l++; } if (l < r) { ndp[i] = dp[deq[l]] + 1L * ph * a[i]; } while (l < r && dp[deq[r-1]] <= dp[i]) { r--; } deq[r] = i; r++; } swap(dp, ndp); } writeln(dp.fold!max); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; //fold(for old compiler) static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } unittest { import std.stdio; auto l = [1, 2, 3, 4, 5]; assert(l.fold!"a+b"(10) == 25); } } version (X86) static if (__VERSION__ < 2071) { int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/deque.d */ // module dcomp.container.deque; struct Deque(T) { import core.exception : RangeError; import core.memory : GC; import std.range : ElementType, isInputRange; import std.traits : isImplicitlyConvertible; struct Payload { T *d; size_t st, length, cap; @property bool empty() const { return length == 0; } alias opDollar = length; ref inout(T) opIndex(size_t i) inout { version(assert) if (length <= i) throw new RangeError(); return d[(st+i >= cap) ? (st+i-cap) : st+i]; } private void expand() { import std.algorithm : max; assert(length == cap); auto nc = max(size_t(4), 2*cap); T* nd = cast(T*)GC.malloc(nc * T.sizeof); foreach (i; 0..length) { nd[i] = this[i]; } d = nd; st = 0; cap = nc; } void clear() { st = length = 0; } void insertFront(T v) { if (length == cap) expand(); if (st == 0) st += cap; st--; length++; this[0] = v; } void insertBack(T v) { if (length == cap) expand(); length++; this[length-1] = v; } void removeFront() { assert(!empty, "Deque.removeFront: Deque is empty"); st++; length--; if (st == cap) st = 0; } void removeBack() { assert(!empty, "Deque.removeBack: Deque is empty"); length--; } } struct RangeT(A) { alias T = typeof(*(A.p)); alias E = typeof(A.p.d[0]); T *p; size_t a, b; @property bool empty() const { return b <= a; } @property size_t length() const { return b-a; } @property RangeT save() { return RangeT(p, a, b); } @property RangeT!(const A) save() const { return typeof(return)(p, a, b); } alias opDollar = length; @property ref inout(E) front() inout { return (*p)[a]; } @property ref inout(E) back() inout { return (*p)[b-1]; } void popFront() { version(assert) if (empty) throw new RangeError(); a++; } void popBack() { version(assert) if (empty) throw new RangeError(); b--; } ref inout(E) opIndex(size_t i) inout { return (*p)[i]; } RangeT opSlice() { return this.save; } RangeT opSlice(size_t i, size_t j) { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } RangeT!(const A) opSlice() const { return this.save; } RangeT!(const A) opSlice(size_t i, size_t j) const { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } } alias Range = RangeT!Deque; alias ConstRange = RangeT!(const Deque); alias ImmutableRange = RangeT!(immutable Deque); Payload *p; private void I() { if (!p) p = new Payload(); } private void C() const { version(assert) if (!p) throw new RangeError(); } //some value this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) {I; p = new Payload(); foreach (v; values) { insertBack(v); } } //range this(Range)(Range r) if (isInputRange!Range && isImplicitlyConvertible!(ElementType!Range, T) && !is(Range == T[])) {I; p = new Payload(); foreach (v; r) { insertBack(v); } } @property bool empty() const { return (!p || p.empty); } @property size_t length() const { return (p ? p.length : 0); } alias opDollar = length; ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; } ref inout(T) front() inout {C; return (*p)[0]; } ref inout(T) back() inout {C; return (*p)[$-1]; } void clear() { if (p) p.clear(); } void insertFront(T v) {I; p.insertFront(v); } void insertBack(T v) {I; p.insertBack(v); } void removeFront() {C; p.removeFront(); } void removeBack() {C; p.removeBack(); } Range opSlice() {I; return Range(p, 0, length); } } unittest { import std.algorithm : equal; import std.range.primitives : isRandomAccessRange; import std.container.util : make; auto q = make!(Deque!int); assert(isRandomAccessRange!(typeof(q[]))); //insert,remove assert(equal(q[], new int[](0))); q.insertBack(1); assert(equal(q[], [1])); q.insertBack(2); assert(equal(q[], [1, 2])); q.insertFront(3); assert(equal(q[], [3, 1, 2]) && q.front == 3); q.removeFront; assert(equal(q[], [1, 2]) && q.length == 2); q.insertBack(4); assert(equal(q[], [1, 2, 4]) && q.front == 1 && q.back == 4 && q[$-1] == 4); q.insertFront(5); assert(equal(q[], [5, 1, 2, 4])); //range assert(equal(q[][1..3], [1, 2])); assert(equal(q[][][][], q[])); //const range const auto rng = q[]; assert(rng.front == 5 && rng.back == 4); //reference type auto q2 = q; q2.insertBack(6); q2.insertFront(7); assert(equal(q[], q2[]) && q.length == q2.length); //construct with make auto a = make!(Deque!int)(1, 2, 3); auto b = make!(Deque!int)([1, 2, 3]); assert(equal(a[], b[])); } unittest { import std.algorithm : equal; import std.range.primitives : isRandomAccessRange; import std.container.util : make; auto q = make!(Deque!int); q.clear(); assert(equal(q[], new int[0])); foreach (i; 0..100) { q.insertBack(1); q.insertBack(2); q.insertBack(3); q.insertBack(4); q.insertBack(5); assert(equal(q[], [1,2,3,4,5])); q.clear(); assert(equal(q[], new int[0])); } } unittest { Deque!int a; Deque!int b; a.insertFront(2); assert(b.length == 0); } unittest { import std.algorithm : equal; import std.range : iota; Deque!int a; foreach (i; 0..100) { a.insertBack(i); } assert(equal(a[], iota(100))); } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; if (f.eof) return false; line = lineBuf[]; f.readln(line); } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { //string or char[10] etc //todo optimize auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { //static assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); fout.writeln("1 2 3"); fout.writeln("ab cde"); fout.writeln("1.0 1.0 2.0"); fout.close; Scanner sc = new Scanner(File(fileName, "r")); int a; int[2] b; char[2] c; string d; double e; double[] f; sc.read(a, b, c, d, e, f); assert(a == 1); assert(equal(b[], [2, 3])); assert(equal(c[], "ab")); assert(equal(d, "cde")); assert(e == 1.0); assert(equal(f, [1.0, 2.0])); } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File, writeln; import std.datetime; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); foreach (i; 0..1_000_000) { fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2); } fout.close; writeln("Scanner Speed Test(3*1,000,000 int)"); StopWatch sw; sw.start; Scanner sc = new Scanner(File(fileName, "r")); foreach (i; 0..500_000) { int a, b, c; sc.read(a, b, c); assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 500_000..700_000) { int[3] d; sc.read(d); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 700_000..1_000_000) { int[] d; sc.read(d); assert(d.length == 3); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } writeln(sw.peek.msecs, "ms"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; long solve(long N) { if (N == -1) return 0; if (N%2 == 0) { return N ^ ((N/2)%2 == 0 ? 0 : 1); } else { return (N/2)%2 == 1 ? 0 : 1; } } void main() { auto ab = readln.split.to!(long[]); writeln(solve(ab[0]-1) ^ solve(ab[1])); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto ab = readln.split.to!(int[]); auto A = ab[0]; auto B = ab[1]; writeln(A * B - A - B + 1); }
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; import std.math; void main(string[] args) { long[100][100] mat; auto t = next!int; foreach(tc; 0 .. t) { auto n = next!int; auto m = next!int; foreach(i; 0 .. n) foreach(j; 0 .. m) mat[i][j] = next!int; long cost = 0; foreach(i; 0 .. n) { foreach(j; 0 .. m) { int si = n - 1 - i; int sj = m - 1 - j; long localcost = 0; if (si == i) { if (sj == j) { localcost = 0; } else { localcost = abs(mat[i][j] - mat[i][sj]); mat[i][j] = mat[i][sj]; } } else { if (sj == j) { localcost = abs(mat[i][j] - mat[si][j]); mat[i][j] = mat[si][j]; } else { localcost = int.max; long[] vals = [mat[i][j], mat[i][sj], mat[si][j], mat[si][sj]]; auto svals = sort(vals); long usedval = vals[1]; localcost = abs(mat[i][j] - usedval) + abs(mat[i][sj] - usedval) + abs(mat[si][j] - usedval) + abs(mat[si][sj] - usedval); mat[i][j] = usedval; mat[i][sj] = usedval; mat[si][j] = usedval; mat[si][sj] = usedval; } } cost += localcost; } } cost.writeln; } } 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
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto n = RD!string; auto pat = new long[](n.length+1); foreach (i; 0..n.length) { pat[i+1] = pat[i]; long tmp = 10; tmp.modpow(i); tmp.modm(i+1); pat[i+1].moda(tmp); } long ans; foreach (i; 0..n.length) { { long num = n[i]-'0'; long left = i; left *= (i+1); left /= 2; left %= mod; long digit = 10; digit.modpow(n.length-i-1); num.modm(digit); left.modm(num); ans.moda(left); debug writeln("i:", i, " ans:", ans); } { long num = n[i]-'0'; num.modm(pat[n.length-i-1]); ans.moda(num); debug writeln("i:", i, " ans:", ans); } } writeln(ans); stdout.flush; debug readln; }
D
//prewritten code: https://github.com/antma/algo import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.traits; final class InputReader { private: ubyte[] p, buffer; bool eof; bool rawRead () { if (eof) { return false; } p = stdin.rawRead (buffer); if (p.empty) { eof = true; return false; } return true; } ubyte nextByte(bool check) () { static if (check) { if (p.empty) { if (!rawRead ()) { return 0; } } } auto r = p.front; p.popFront (); return r; } public: this () { buffer = uninitializedArray!(ubyte[])(16<<20); } bool seekByte (in ubyte lo) { while (true) { p = p.find! (c => c >= lo); if (!p.empty) { return false; } if (!rawRead ()) { return true; } } } template next(T) if (isSigned!T) { T next () { if (seekByte (45)) { return 0; } T res; ubyte b = nextByte!false (); if (b == 45) { while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 - (b - 48); } } else { res = b - 48; while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 + (b - 48); } } } } template next(T) if (isUnsigned!T) { T next () { if (seekByte (48)) { return 0; } T res = nextByte!false () - 48; while (true) { ubyte b = nextByte!true (); if (b < 48 || b >= 58) { break; } res = res * 10 + (b - 48); } return res; } } T[] nextA(T) (in int n) { auto a = uninitializedArray!(T[]) (n); foreach (i; 0 .. n) { a[i] = next!T; } return a; } } void main() { auto r = new InputReader (); immutable nt = r.next!uint (); auto t = r.nextA!uint (nt); foreach (n; t) { int x = n; if (x & 1) { write ('7'); x -= 3; } while (x > 0) { write ('1'); x -= 2; } writeln; } }
D
/+ dub.sdl: name "A" 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) sc.read!true; string s; sc.read(s); string err = "aeiou13579"; int ans = 0; foreach (c; err) { ans += s.count(c); } writeln(ans); return 0; } /* IMPORT /Users/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 /Users/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 /Users/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(bool enforceEOF = false, T, Args...)(ref T x, auto ref Args args) { import std.exception; enforce(readSingle(x)); static if (args.length == 0) { enforce(enforceEOF == false || !succ()); } else { read!enforceEOF(args); } } void read(bool enforceEOF = false, Args...)(auto ref Args args) { import std.exception; static if (args.length == 0) { enforce(enforceEOF == false || !succ()); } else { enforce(readSingle(args[0])); read!enforceEOF(args); } } } /* 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
void solve(string[] s){ foreach(elm; s){ writeln(elm); writeln(elm); } } void main(){ int[] hw = inln(); string[] s; foreach(_; 0..hw[0])s ~= readln().chomp(); solve(s); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range; const long mod = 10^^9+7; // 1要素のみの入力 T inelm(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] inln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split())ln ~= elm.to!T(); return ln; }
D
import std.stdio,std.conv,std.string,std.algorithm; void main(){ for( int i;;++i ){ auto arg = readln().chomp().split; auto a = arg[0].to!int , b = arg[1].to!int; if( !a && !b ) break; if( a<b ){ writeln( a," ",b ); }else{ writeln( b," ",a ); } } }
D
void main(){ int r = _scan(); ( r^^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; import std.string; import std.algorithm; import std.conv; void main() { int[3] coins; int x, ans; for (int i = 0; i < 3; i++) { coins[i] = readln.chomp.to!int + 1; } x = readln.chomp.to!int; foreach (i; 0..coins[0]) { foreach (j; 0..coins[1]) { foreach (k; 0..coins[2]) { int sum = 500 * i + 100 * j + 50 * k; if (sum == x) { ans++; } else if (sum > x) { break; } } } } 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; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } string calc(Vec2 p0, Vec2 p1, Vec2 p2) { auto p01 = p1 - p0; auto p02 = p2 - p0; auto d = p01.cross(p02); if (d > 0) return "COUNTER_CLOCKWISE"; if (d < 0) return "CLOCKWISE"; // |p01||p02|cos(pi) ??§ cos(pi) = -1 ????????§?????????????????? if (p01.dot(p02) < 0) return "ONLINE_BACK"; if (p01.magSq() >= p02.magSq()) return "ON_SEGMENT"; return "ONLINE_FRONT"; } void main() { auto xs = readints(); auto p0 = Vec2(xs[0], xs[1]); auto p1 = Vec2(xs[2], xs[3]); int q = readint(); for (int i = 0; i < q; i++) { auto xy = readints(); auto p2 = Vec2(xy[0], xy[1]); auto ans = calc(p0, p1, p2); writeln(ans); } } struct Vec2 { immutable double x; immutable double y; this(double x, double y) { this.x = x; this.y = y; } Vec2 opAdd(Vec2 other) { return Vec2(this.x + other.x, this.y + other.y); } Vec2 opSub(Vec2 other) { return Vec2(this.x - other.x, this.y - other.y); } Vec2 opMul(double d) { return Vec2(this.x * d, this.y * d); } double dot(Vec2 other) { return this.x * other.x + this.y * other.y; } double cross(Vec2 other) { return this.x * other.y - other.x * this.y; } double mag() { return sqrt(magSq()); } double magSq() { return this.x * this.x + this.y * this.y; } Vec2 normalize() { auto m = mag(); if (m != 0 && m != 1) return Vec2(this.x / m, this.y / m); return this; } static double distance(Vec2 a, Vec2 b) { return (a - b).mag(); } }
D
// Vicfred // https://atcoder.jp/contests/abc156/tasks/abc156_c // brute force import std.algorithm; import std.array; import std.conv; import std.stdio; import std.string; void main() { int n = readln.chomp.to!int; int[] x = readln.split.map!(to!int).array; long minima = 1<<30; foreach(p; 1..101) { long dist = 0; foreach(person; x) { dist += (person-p)^^2; } minima = min(minima, dist); } minima.writeln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { long H; scan(H); long[long] memo; long dfs(long h) { if (h == 0) { return 0; } if (h in memo) { return memo[h]; } return 2 * dfs(h / 2) + 1; } long ans = dfs(H); 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.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/3; enum long MOD = 10L^^9+7; void main() { long A, B, C; scanln(A, B, C); writeln(min(C, B/A)); } // ---------------------------------------------- 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 (t * y < x) t++; return t; } T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { T t = x / y; if (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) { import std.meta; template getFormat(T) { static if (isIntegral!T) { enum getFormat = "%d"; } else static if (isFloatingPoint!T) { enum getFormat = "%g"; } else static if (isSomeString!T || isSomeChar!T) { enum getFormat = "%s"; } else { static assert(false); } } enum string fmt = [staticMap!(getFormat, Args)].join(" "); string[] inputs = readln.chomp.split; foreach(i, ref v; args) { v = inputs[i].to!(Args[i]); } } // 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) { private template RebindableOrUnqual(T) { static if (is(T == class) || is(T == interface) || isDynamicArray!T || isAssociativeArray!T) alias RebindableOrUnqual = Rebindable!T; else alias RebindableOrUnqual = Unqual!T; } private auto extremum(alias map, alias selector = "a < b", Range)(Range r) if (isInputRange!Range && !isInfinite!Range && is(typeof(unaryFun!map(ElementType!(Range).init)))) in { assert(!r.empty, "r is an empty range"); } body { alias Element = ElementType!Range; RebindableOrUnqual!Element seed = r.front; r.popFront(); return extremum!(map, selector)(r, seed); } private auto extremum(alias map, alias selector = "a < b", Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seedElement) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void) && is(typeof(unaryFun!map(ElementType!(Range).init)))) { alias mapFun = unaryFun!map; alias selectorFun = binaryFun!selector; alias Element = ElementType!Range; alias CommonElement = CommonType!(Element, RangeElementType); RebindableOrUnqual!CommonElement extremeElement = seedElement; // if we only have one statement in the loop, it can be optimized a lot better static if (__traits(isSame, map, a => a)) { // direct access via a random access range is faster static if (isRandomAccessRange!Range) { foreach (const i; 0 .. r.length) { if (selectorFun(r[i], extremeElement)) { extremeElement = r[i]; } } } else { while (!r.empty) { if (selectorFun(r.front, extremeElement)) { extremeElement = r.front; } r.popFront(); } } } else { alias MapType = Unqual!(typeof(mapFun(CommonElement.init))); MapType extremeElementMapped = mapFun(extremeElement); // direct access via a random access range is faster static if (isRandomAccessRange!Range) { foreach (const i; 0 .. r.length) { MapType mapElement = mapFun(r[i]); if (selectorFun(mapElement, extremeElementMapped)) { extremeElement = r[i]; extremeElementMapped = mapElement; } } } else { while (!r.empty) { MapType mapElement = mapFun(r.front); if (selectorFun(mapElement, extremeElementMapped)) { extremeElement = r.front; extremeElementMapped = mapElement; } r.popFront(); } } } return extremeElement; } private auto extremum(alias selector = "a < b", Range)(Range r) if (isInputRange!Range && !isInfinite!Range && !is(typeof(unaryFun!selector(ElementType!(Range).init)))) { return extremum!(a => a, selector)(r); } // if we only have one statement in the loop it can be optimized a lot better private auto extremum(alias selector = "a < b", Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seedElement) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void) && !is(typeof(unaryFun!selector(ElementType!(Range).init)))) { return extremum!(a => a, selector)(r, seedElement); } auto minElement(alias map = (a => a), Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { return extremum!map(r); } auto minElement(alias map = (a => a), Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void)) { return extremum!map(r, seed); } auto maxElement(alias map = (a => a), Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { return extremum!(map, "a > b")(r); } auto maxElement(alias map = (a => a), Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void)) { return extremum!(map, "a > b")(r, seed); } } // 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.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; long[] as; foreach (_; 0..N) as ~= readln.chomp.to!long; long min_p = 1; long r; foreach (a; as) { if (min_p == a) { min_p += 1; continue; } auto x = a / min_p; if (x == 0) continue; if (a%min_p == 0) { r += x-1; if (min_p == 1) min_p = 2; } else { r += x; } } writeln(r); }
D