code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; void main() { int n; scan(n); auto b = iota(n).map!(i => readln.chomp).array; int checkLeft(string s) { int h; foreach (ch ; s) { if (ch == '(') { h++; } else { h--; if (h < 0) { return -1; } } } return h; } int checkRight(string s) { int h; foreach_reverse (ch ; s) { if (ch == ')') { h++; } else { h--; if (h < 0) { return -1; } } } return h; } auto cnt = new int[](5 * 10^^5 + 1); int perfect; foreach (s ; b) { int h = checkLeft(s); if (h > 0) { cnt[h]++; } if (h == 0) { perfect++; } } int ans; foreach (s ; b) { int h = checkRight(s); if (h > 0 && cnt[h] > 0) { ans++; cnt[h]--; } } ans += perfect / 2; writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 2*10^^9 + 7; void main() { int n, k; scan(n, k); auto a = new int[](n); auto b = new int[](n); foreach (i ; 0 .. n) { int ai, bi; scan(ai, bi); a[i] = ai, b[i] = bi; } long ans; foreach (j ; 0 .. n) { if ((~k & a[j]) == 0) { ans += b[j]; } } foreach (i ; 0 .. 32) { if (k & (1 << i)) { long tmp; int ks = k ^ (1 << i) | ((1<<i) - 1); debug { writefln("%16b", ks); } foreach (j ; 0 .. n) { if ((~ks & a[j]) == 0) { tmp += b[j]; } } ans = max(ans, tmp); } } writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.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() { readint; auto ss = reads!string; bool[string] d; foreach (s; ss) d[s] = true; writeln(d.keys.length == 3 ? "Three" : "Four"); }
D
import std; long calc(long x) { long s = 100; for (int i = 1; ; i++) { s = s + cast(long)(s * 0.01); if (s >= x) return i; } assert(false); } void main() { long x; scan(x); writeln(calc(x)); } void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int;
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { 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; import std.string; import std.algorithm; import std.conv; void main() { readln.chomp.count('1').writeln; }
D
void main() { writeln("ABC", readln.chomp); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); ulong bignum = 1_000_000_007; alias SugarWater = Tuple!(long, "swater", long, "sugar"); 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, m; scan(n, m); long left, right = n; foreach (i; iota(m)) { long l, r; scan(l, r); left = max(l, left); right = min(r, right); } auto pass = right - left + 1; writeln(pass > 0 ? pass : 0); }
D
import std.stdio; import std.string; void main() { const string m = "kstnhm"; const string ch = "aiueo"; const string dir = "TLURD"; auto str = readln; bool flag = false; foreach (int i, c; str) { if (!(i % 2)) { auto a = c - '0'; if (a == 0) flag = true; else if (2 <= a && a <= 7) { write(m[a-2]); } else if (a == 9) { write('r'); } else if (a == 8) { write('y'); } } else { auto a = dir.indexOf(c); if (flag) { if (a == 0) { write("wa"); } else if (a == 2) { write("nn"); } else { write("wo"); } flag = false; } else { write(ch[a]); } } } writeln; }
D
void main() { int[] tmp = readln.split.to!(int[]); int n = tmp[0], m = tmp[1]; int[] road = new int[n]; foreach (i; 0 .. m) { tmp = readln.split.to!(int[]); int a = tmp[0] - 1, b = tmp[1] - 1; ++road[a], ++road[b]; } foreach (x; road) { x.writeln; } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; immutable long inf = 3L * 10L^^18; void main() { int n, k; scan(n, k); auto w = new long[](n); auto d = new long[](n); foreach (i ; 0 .. n) { scan(w[i], d[i]); } bool check(long x) { long hana; foreach (i ; 0 .. n) { if (x - w[i] >= 0) { hana += (x - w[i]) / d[i] + 1; } } return hana >= k; } long btm = 0, top = inf; while (top - btm > 1) { long mid = btm + (top - btm) / 2; if (check(mid)) { top = mid; } else { btm = mid; } } writeln(top); } void solve(int n, int v, int w) { if (n == 1) { writeln(min(v, w)); return; } auto va = new int[](0); va ~= v; while (v > 1) { if (v % n <= 1) { v /= n; } else { v = (v / n) + 1; } va ~= v; } va.reverse(); auto wa = new int[](0); wa ~= w; while (w > 1) { if (w % n <= 1) { w /= n; } else { w = (w / n) + 1; } wa ~= w; } wa.reverse(); debug { writeln(va); writeln(wa); } int ans = 0; while (!va.empty) { while (!wa.empty) { if (va.front == wa.front) { ans = max(ans, va.front); va.popFront; wa.popFront; break; } else if (va.front < wa.front) { va.popFront; break; } else { wa.popFront; } } } writeln(ans); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std; import core.bitop; ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "x", long, "g"); alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less); void main() { long a, b, c, d; scan(a, b, c, d); writeln((b - a + 1) - (b / c - (a - 1) / c) - (b / d - (a - 1) / d) + (b / (c / gcd(c, d) * d) - (a - 1) / (c / gcd(c, d) * d))); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto rd = readln.split.to!(int[]), w = rd[0], a = rd[1], b = rd[2]; if (a > b) swap(a, b); writeln(b <= a+w ? 0 : b-(a+w)); }
D
import std.stdio; import std.algorithm; import std.math; import std.conv; import std.string; T readNum(T)(){ return readStr.to!T; } T[] readNums(T)(){ return readStr.split.to!(T[]); } string readStr(){ return readln.chomp; } void main(){ auto nm = readNums!int; auto n = new int[](nm[0]); int penalty, cleared; foreach(i; 0 .. nm[1]){ auto ps = readStr.split; if(ps[1] == "WA" && n[ps[0].to!int-1] >= 0){ n[ps[0].to!int-1]++; } else if(ps[1] == "AC" && n[ps[0].to!int-1] >= 0){ penalty += n[ps[0].to!int-1]; cleared++; n[ps[0].to!int-1] = -1; } } writeln(cleared, " ", penalty); }
D
void main() { string n = rdStr; long k = rdElem; long len = n.length; long[][][] dp = new long[][][](len+1, k+2, 2); dp[0][0][0] = 1; foreach (i, x; n) { long d = x - '0'; foreach (j; 0 .. k+1) { foreach (u; 0 .. 2) { foreach (v; 0 .. (u?10:d+1)) { dp[i+1][v?j+1:j][u|(v<d)] += dp[i][j][u]; } } } } dp[len][k].sum.writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; void main() { int n; scan(n); auto a = readln.split.to!(int[]); auto pf = new long[](n + 1); foreach (i ; 1 .. n + 1) { pf[i] = pf[i-1] + a[i-1]; } long ans = 10L^^18; foreach (i ; 0 .. n) { if (i < 2 || (n - i) < 2) { continue; } auto l = nibutan(n, pf, 0, i); auto r = nibutan(n, pf, i, n); long p1 = pf[l] - pf[0]; long p2 = pf[i] - pf[l]; long p3 = pf[r] - pf[i]; long p4 = pf[n] - pf[r]; long mx = max(p1, p2, p3, p4); long mn = min(p1, p2, p3, p4); ans = min(ans, mx - mn); } writeln(ans); } int nibutan(int n, long[] pf, int btm, int top) { long dif(int k) { return abs(pf[top] - pf[k] - (pf[k] - pf[btm])); } bool check(int k) { return dif(k) <= dif(k + 1); } int ok = top, ng = btm; while (abs(ok - ng) > 1) { int mid = (ok + ng) / 2; if (check(mid)) { ok = mid; } else { ng = mid; } } return 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; ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "base", long, "cnt"); alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less); void main() { auto N = lread(); auto p = N.make_primeF_pairs(); long bseach(long t) { long ok = -1, ng = t + 1; while (abs(ok - ng) > 1) { auto mid = (ok + ng) / 2; if (mid * (mid + 1) / 2 <= t) ok = mid; else ng = mid; } // ok.writeln(); return ok; } long ans; p.each!(x => ans += bseach(x.cnt)); ans.writeln(); } auto factors(long N) { long iter = 1; long[] ret; while (true) { if (iter ^^ 2 > N) break; if (!(N % iter)) ret ~= only(iter, N / iter).array(); iter++; } return ret; } auto primeFactors(long N) { auto fs = N.factors(); auto ret = fs.filter!(x => x != 1 && x.factors.length == 2).array(); return ret; } auto make_primeF_pairs(long N) { auto pfps = N.primeFactors(); Pair[] ret; foreach (e; pfps) { long cnt; while (!(N % e)) { cnt++; N /= e; } ret ~= Pair(e, cnt); } return ret; } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.stdio, std.conv, std.string, std.math; void main(){ auto ip = readln.chomp.to!int; if(ip < 1200){ "ABC".writeln; } else { "ARC".writeln; } }
D
import std.stdio; import std.algorithm; import std.string; import std.functional; import std.array; import std.conv; import std.math; import std.typecons; import std.regex; import std.range; void main(){ int[] s = readln().split().to!(int[]); int n,r,l; n = s[0]; r = s[1]; l = s[2]; int[] d,t,x; int[] p,pt; int maxp,maxt,now; for(int i=0;i<n;i++) { p ~= 0; pt ~= 0; } for(int i=0;i<r;i++){ s = readln().split().to!(int[]); d ~= s[0]-1; t ~= s[1]; x ~= s[2]; } d ~= 0; t ~= l; x ~= 0; int pre = 0; for(int i=0;i<=r;i++){ pt[now] += t[i]-pre; maxt = max(maxt,pt[now]); pre = t[i]; p[d[i]] += x[i]; if(x[i] > 0){ if(p[d[i]] == maxp && d[i] < now){ now = d[i]; }else if(p[d[i]] > maxp){ now = d[i]; maxp = p[d[i]]; } }else if(now == d[i]){ now = 0; int temp = maxp; int tempnow = now; maxp = -10000000; for(int j=0;j<n;j++){ if(maxp < p[j]){ maxp = p[j]; if(maxp == temp) break; now = j; } } } } for(int i=0;i<n;i++){ if(pt[i] == maxt){ writeln(i+1); break; } } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.stdio; import std.string; import std.range; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { auto nm = readints(); int n = nm[0], m = nm[1]; auto xs = new bool[](n + 1); // 1 から i へ定期便があるか auto ys = new bool[](n + 1); // n から i へ定期便があるか for (int i = 0; i < m; i++) { auto ab = readints(); int a = ab[0], b = ab[1]; if (a > b) swap(a, b); if (a == 1) xs[b] = true; if (b == n) ys[a] = true; } bool found = false; for (int i = 2; i < n; i++) { if (xs[i] && ys[i]) { found = true; break; } } writeln(found ? "POSSIBLE" : "IMPOSSIBLE"); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; alias Mame = Tuple!(long, "point", long, "number"); 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, k; scan(n, k); auto h = aryread(); long cnt; foreach (e; h) { if (e >= k) cnt++; } cnt.writeln(); }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(int k, string s) { int n = cast(int)s.length; int score = 0; for (int i = 0; i < n - 1; i++) { if (s[i] == s[i+1]) score++; } int ans = min(score + 2 * k, n - 1); return ans; } void main() { int n, k; scan(n, k); string s = read!string; writeln(calc(k, s)); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(string s) { int rec(int head) { int change = 0; for (int i = 0; i < s.length; i++) { if ((i % 2 == 0) ^ ((s[i] - '0') == head)) { change++; } } return change; } return min(rec(0), rec(1)); } void main() { string s = read!string; writeln(calc(s)); }
D
import std.stdio, std.conv, std.string; import std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } int DEBUG_LEVEL = 0; void print()(){ writeln(""); } void print(T, A ...)(T t, lazy A a){ write(t), print(a); } void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); } void main(string[] args){ if(args.length > 1 && args[1] == "-debug"){ if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int; else DEBUG_LEVEL = 1; } int n = read.to!int; int m = read.to!int; bool[][] xs = new bool[][](m, n); // xs[i][j]: 電球iはスイッチjに制御される foreach(i; 0 .. m){ int k = read.to!int; foreach(j; 0 .. k){ int s = read.to!int - 1; xs[i][s] = 1; } } int[] ps; foreach(i; 0 .. m) ps ~= read.to!int; int cnt; foreach(t; 0 .. 1L<<n){ bool flag = 1; foreach(i; 0 .. m){ int q; foreach(j; 0 .. n){ if(xs[i][j] && (1L<<j & t)) q += 1; } if(q % 2 != ps[i]) flag = 0; } if(flag) cnt += 1; } cnt.writeln; }
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } void main(){ long n = read.to!long; long[] as = readln.chomp.split.to!(long[]); long sum = 0; long[long] xs; xs[sum] = 1; foreach(a; as){ sum += a; if(sum in xs) xs[sum] += 1; else xs[sum] = 1; } debug xs.writeln; long ans; foreach(k; xs.keys){ auto v = xs[k]; ans += v * (v - 1) / 2; } ans.writeln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip, std.regex; void main() { auto S = readln.chomp; auto K = readln.chomp.to!int; auto N = S.length.to!int; bool[string] cnt; foreach (len; 1..min(11, N+1)) { foreach (i; 0..N-len+1) { cnt[S[i..i+len]] = true; } } auto x = cnt.keys.array; x.sort(); x[K-1].writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { writeln(180 * (readln.chomp.to!int-2)); }
D
import std.stdio; import std.string; import std.math; import std.conv; import std.algorithm; import std.bigint; void main(){ real[4] x,y; auto n = to!int(chomp(readln())); for(int j=0;j<n;j++){ auto s = split(readln()); for(int i=0;i<4;i++){ x[i] = to!real(s[2*i]); y[i] = to!real(s[2*i+1]); } real[2] AB,CD; bool flg = false; AB[0] = x[1] - x[0]; AB[1] = y[1] - y[0]; CD[0] = x[3] - x[2]; CD[1] = y[3] - y[2]; if(AB[0] == 0 || CD[0] == 0){ if(AB[0] == CD[0]) flg = true; }else if( AB[1]*CD[0] - CD[1]*AB[0] == 0) flg = true; if(flg) writeln("YES"); else writeln("NO"); } }
D
import std.algorithm; import std.array; import std.bigint; import std.bitmanip; import std.conv; import std.math; import std.numeric; import std.range; 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; } T[] readToArray(T)() { return readln.split.to!(T[]); } void readInto(T...)(ref T ts) { auto ss = readln.split; foreach(ref t; ts) { t = ss.front.to!(typeof(t)); ss.popFront; } } // 冪乗をmod取りつつ計算 @nogc @safe pure ulong modPow(ulong a, ulong n, ulong m) { ulong r = 1; while (n > 0) { if(n % 2 != 0) r = r * a % m; a = a * a % m; n /= 2; } return r; } // フェルマーの小定理から乗法逆元を計算 // 定理の要請により法は素数 @nogc @safe pure ulong modInv(ulong a, ulong m) { return modPow(a, m-2, m); } // mod取りつつ順列を計算 @nogc @safe pure ulong modPerm(ulong n, ulong k, ulong m) { if (n < k) return 0; ulong r = 1; for (ulong i = n-k+1; i <= n; i++) { r *= i; r %= m; } return r; } // mod取りつつ順列を計算 @nogc @safe pure ulong modFact(ulong n, ulong m) { return modPerm(n, n, m); } // mod取りつつ組み合わせを計算 // modInvを使っているので法は素数 @nogc @safe pure ulong modComb(ulong n, ulong r, ulong m) { return modPerm(n, r, m)*modInv(modFact(r, m), m) % m; } immutable ulong MOD = 1000000007; void main() { ulong n, d; readInto(n, d); long[][] xs; for (ulong i = 0; i < n; i++) { auto x = readToArray!(long); xs ~= x; } ulong cnt = 0; for(ulong i = 0; i < n - 1; i++) { for(ulong j = i + 1; j < n; j++) { ulong dist_sq = 0; for(ulong k = 0; k < d; k++) { const long dd = xs[i][k] - xs[j][k]; dist_sq += dd*dd; } ulong dist = cast(ulong)sqrt(cast(real)dist_sq); if (dist*dist == dist_sq) cnt++; } } writeln(cnt); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = readln.chomp.to!size_t; auto ri = n.iota.map!(_ => readln.chomp.to!int).array; auto maxR = ri.back; auto maxP = int.min; foreach_reverse (i; 0..n-1) { maxP = max(maxP, maxR - ri[i]); maxR = max(maxR, ri[i]); } writeln(maxP); }
D
void main() { long[] tmp = readln.split.to!(long[]); long k = tmp[0], a = tmp[1], b = tmp[2]; long op = k - a + 1; long cal = a + (b - a) * (op / 2) + op % 2; max(k + 1, cal).writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math; void main() { auto n = readln.chomp.to!int; auto as = readln.split.to!(long[]); auto x = as[0]; auto y = reduce!"a + b"(0L, as[1..$]); auto d = abs(x - y); for (int i = 1; i < n-1; ++i) { x += as[i]; y -= as[i]; auto dd = abs(x - y); if (dd < d) d = dd; } writeln(d); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { int a, b, c, d; scan(a, b, c, d); writeln(max(a*b, c*d)); } 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; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { auto n = lread(); string s; string t; scan(s, t); // writeln(s, t); foreach (i; 0 .. n) { write(s[i], t[i]); } } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.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 long INF = 1L << 59; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto A = readln.split.map!(to!int).array; auto cnt1 = new int[](N+10); auto cnt2 = new int[](M+10); cnt2[0] = N; auto ans = new dchar[](M); int mv = 0; foreach (i; 0..M) { bool ok = false; if (cnt1[A[i]] == mv && cnt2[cnt1[A[i]]] == 1) { ok = true; mv += 1; } cnt2[cnt1[A[i]]] -= 1; cnt1[A[i]] += 1; cnt2[cnt1[A[i]]] += 1; if (ok) { ans[i] = '1'; } else { ans[i] = '0'; } } ans.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; 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 r, d, x; scan(r, d, x); auto tmp = x; foreach (i; 0 .. 10) { tmp = calc_algae(r, tmp, d); tmp.writeln(); } } auto calc_algae(long r, long x, long d) { return r * x - d; }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { auto tests = readln.strip.to !(int); multitest_loop: foreach (test; 0..tests) { auto n = readln.strip.to !(int); auto p = readln.splitter.map !(to !(int)).array; int res = 0; foreach (j; 0..n) { int i = j; do { i -= 1; } while (i >= 0 && p[i] > p[j]); if (i < 0) { continue; } int k = j; do { k += 1; } while (k < n && p[k] > p[j]); if (k >= n) { continue; } writeln ("YES"); writeln (i + 1, " ", j + 1, " ", k + 1); continue multitest_loop; } writeln ("NO"); } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm; void main() { int n,k; scan(n,k); auto w = new int[][][](2, k, k); auto b = new int[][][](2, k, k); foreach (i ; 0 .. n) { char ci; int xi, yi; scan(xi, yi, ci); auto e = ((xi / k % 2) + (yi / k % 2)) % 2; if (ci == 'W') { w[e][xi%k][yi%k]++; } else { b[e][xi%k][yi%k]++; } } foreach (e ; 0 .. 2) { foreach (i ; 0 .. k) { foreach (j ; 1 .. k) { w[e][i][j] += w[e][i][j-1]; b[e][i][j] += b[e][i][j-1]; } } foreach (j ; 0 .. k) { foreach (i ; 1 .. k) { w[e][i][j] += w[e][i-1][j]; b[e][i][j] += b[e][i-1][j]; } } } debug { writefln("%(%(%s %)\n%)", w[0]); writeln; writefln("%(%(%s %)\n%)", w[1]); writeln; writefln("%(%(%s %)\n%)", b[0]); writeln; writefln("%(%(%s %)\n%)", b[1]); writeln; } int ans; foreach (i ; 0 .. k) { foreach (j ; 0 .. k) { int t = w[0][k-1][k-1] - w[0][k-1][j] - w[0][i][k-1] + w[0][i][j]*2; int s = w[1][i][k-1] + w[1][k-1][j] - w[1][i][j]*2; int r = b[1][k-1][k-1] - b[1][k-1][j] - b[1][i][k-1] + b[1][i][j]*2; int u = b[0][i][k-1] + b[0][k-1][j] - b[0][i][j]*2; int d = t + s + r + u; ans = max(ans, d); ans = max(ans, n - d); } } writeln(ans); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.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; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto s = RD!string; long cnt; foreach (i; 0..N) { if (s[i] == 'R') ++cnt; } writeln(cnt > N / 2 ? "Yes" : "No"); stdout.flush(); debug readln(); }
D
import std.stdio; import std.string; import std.conv; import std.range; import std.array; import std.algorithm; void main(){ auto Z=readln.split.to!(int[]),a=Z[0],b=Z[1],c=Z[2]; if(a+b>=c)writeln("Yes"); else writeln("No"); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.math, std.typecons; void main() { int n; scan(n); writeln(euler_phi(n)); } int euler_phi(int n) { int ans = n; for (int p = 2; p*p <= n; p++) { if (n % p == 0) { ans = ans / p * (p - 1); while (n % p == 0) n /= p; } } if (n > 1) ans = ans / n * (n - 1); return ans; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto n = readln.strip.to !(int); auto a = readln.splitter.map !(to !(int)).array; long [] [] s = [[0], [0], [0]]; foreach (i, c; a) { s[0] ~= s[0][$ - 1] + !(i & 1) * c; s[1] ~= s[1][$ - 1] + (i & 1) * c; s[2] ~= s[1][$ - 1] - s[0][$ - 1]; } long res = 0; long [2] lo = [long.max / 2, 0]; foreach (i; 0..n) { lo[i & 1] = min (lo[i & 1], s[2][i + 1]); res = max (res, s[2][i + 1] - lo[i & 1]); } writeln (res + s[0].back); } }
D
void main() { int[] tmp = readln.split.to!(int[]); int n = tmp[0], a = tmp[1], b = tmp[2]; min(a*n, b).writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.conv, std.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; long[] as; foreach(i; 0 .. n) as ~= read.to!long; string ans; int i = 0, j = n - 1; long x = -1; while(i <= j){ if(as[i] < as[j]){ if(x < as[i]) x = as[i], ans ~= "L", i += 1; else if(x < as[j]) x = as[j], ans ~= "R", j -= 1; else break; } else{ if(x < as[j]) x = as[j], ans ~= "R", j -= 1; else if(x < as[i]) x = as[i], ans ~= "L", i += 1; else break; } } ans.length.writeln; ans.writeln; }
D
/* imports all std modules {{{*/ import std.algorithm, std.array, std.ascii, std.base64, std.bigint, std.bitmanip, std.compiler, std.complex, std.concurrency, std.container, std.conv, std.csv, std.datetime, std.demangle, std.encoding, std.exception, std.file, std.format, std.functional, std.getopt, std.json, std.math, std.mathspecial, std.meta, std.mmfile, std.net.curl, std.net.isemail, std.numeric, std.parallelism, std.path, std.process, std.random, std.range, std.regex, std.signals, std.socket, std.stdint, std.stdio, std.string, std.system, std.traits, std.typecons, std.uni, std.uri, std.utf, std.uuid, std.variant, std.zip, std.zlib; /*}}}*/ /+---test 3 1 ---+/ void main(string[] args) { const A = readln.chomp.to!long; const B = readln.chomp.to!long; [1, 2, 3].filter!(x => x != A && x != B).front.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 a=(scanElem+11)%13; auto b=(scanElem+11)%13; if(a<b)end("Bob"); if(a>b)end("Alice"); end("Draw"); }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; void main() { int N = 100; int[] ary = new int[N+1]; while(true) { auto input = readln.chomp; if (stdin.eof) break; ary[input.to!size_t]++; } int maximum = ary.reduce!(max); foreach(size_t i; 0..ary.length) if (ary[i]==maximum) i.writeln; }
D
void main() { long n, x; rdVals(n, x); long[] l = rdRow; long[] d = new long[n+1]; foreach (i; 1 .. n+1) { d[i] = d[i-1] + l[i-1]; } long cnt; foreach (i; 0 .. n+1) { if (d[i] <= x) ++cnt; } cnt.writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { string n; scan(n); bool yn = n[0 .. 3].to!int % 111 == 0 || n[1 .. 4].to!int % 111 == 0; writeln(yn ? "Yes" : "No"); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } struct Queue(T) { private { int N, head, tail; T[] data; } this(int n) { N = n + 1; data = new T[](N); } bool empty() { return head == tail; } bool full() { return (tail + 1) % N == head; } T front() { return data[head]; } void push(T x) { assert(!full); data[tail++] = x; tail %= N; } void pop() { assert(!empty); head = (head + 1) % N; } void clear() { head = tail = 0; } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; void main() { auto data = split(readln()); ( to!int(data[0]) * to!int(data[1]) / 2 ).writeln(); }
D
// Vicfred // https://atcoder.jp/contests/abc177/tasks/abc177_c // math import std.algorithm; import std.array; import std.conv; import std.stdio; import std.string; void main() { const long mod = 10^^9+7; long n = readln.chomp.to!long; long[] a = readln.split.map!(to!long).array; long sum = 0; for(long i = 0; i < n; i++) { sum += a[i]; sum %= mod; } long ans = 0; for(int i = 0; i < n; i++) { sum -= a[i]; if(sum < 0) sum += mod; ans += a[i] * sum; ans %= mod; } ans.writeln; }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; void times(alias pred)(int n) { foreach(i; 0..n) pred(); } auto rep(alias pred, T = typeof(pred()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = pred(); return res; } void main() { string str = readln.chomp; writeln(str[0..4], " ", str[4..$]); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto s = readln.chomp; writeln(s.lastIndexOf("Z") - s.indexOf("A") + 1); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; void main() { auto S = readln.chomp; auto N = S.length.to!int; int v_count = 0; int h_count = 0; foreach (s; S) { if (s == '1') { writeln(1, " ", h_count + 1); h_count += 2; if (h_count == 4) h_count = 0; } else { writeln(3, " ", v_count + 1); v_count += 1; if (v_count == 4) v_count = 0; } } }
D
import std.stdio; import std.algorithm; import std.string; import std.range; import std.array; import std.conv; import std.complex; import std.math; import std.ascii; import std.bigint; import std.container; bool test(int[][] s, int n) { auto rect = new bool[](n+1); rect[n] = false; foreach(_; 0..n) { foreach(i; 0..n) { ulong xmin, ymin, xmax, ymax; xmin = s[0].length; ymin = s.length; xmax = 0; ymax = 0; foreach(j; 0..s.length) { foreach(k; 0..s[0].length) { if(s[j][k] == i) { xmin = min(xmin, k); ymin = min(ymin, j); xmax = max(xmax, k); ymax = max(ymax, j); } } } bool f = true; if(xmax >= xmin && ymax >= ymin) { foreach(j; ymin..ymax+1) { foreach(k; xmin..xmax+1) { f &= s[j][k] == i || rect[s[j][k]]; } } rect[i] |= f; } } } rect[n] = true; return !n || reduce!("a&&b")(rect); } void main() { int n = to!int(readln().strip()); foreach(_; 0..n) { auto hw = map!(to!int)(readln().strip().split()); int h = hw[0]; int w = hw[1]; string[] s; int[char] c; int cn; foreach(i; 0..h) { s ~= readln().strip(); } foreach(i; 0..h) { foreach(j; 0..w) { if(s[i][j] != '.' && s[i][j] !in c) c[s[i][j]] = cn++; } } c['.'] = cn; auto t = new int[][](h,w); foreach(i; 0..h) { foreach(j; 0..w) { t[i][j] = c[s[i][j]]; } } writeln(test(t, cn)?"SAFE":"SUSPICIOUS"); } }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); } void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); } string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ int n = scan!int, m = scan!int; long p = scan!long; long[] as = scan!long(n); long[] bs = scan!long(m); int imin, jmin; foreach(i, a; as) if(a % p != 0){ imin = i.to!int; break; } foreach(j, b; bs) if(b % p != 0){ jmin = j.to!int; break; } print(imin + jmin); }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; immutable inf = 1<<30; string ng = "UNRESTORABLE"; void main() { string s, t; scan(s); scan(t); int n = s.length.to!int, k = t.length.to!int; foreach_reverse (i ; 0 .. n - k + 1) { bool match = true; foreach (j ; i .. i + k) { if (s[j] == '?') continue; if (s[j] != t[j - i]) { match = false; break; } } if (match) { foreach (j ; 0 .. n) { if (i <= j && j < i + k) { write(t[j - i]); } else { write(s[j] == '?' ? 'a' : s[j]); } if (j == n - 1) { writeln(); } } return; } } writeln(ng); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { auto s = readln.chomp; writeln(s.startsWith("YAKI") ? "Yes" : "No"); }
D
import std.stdio, std.string, std.array, std.conv; void main() { int[] tmp = readln.chomp.split.to!(int[]); int a = tmp[0], b = tmp[1]; writeln(a > 2 * b ? a - 2 * b : 0); }
D
import std.algorithm; import std.array; import std.conv; import std.range; import std.stdio; import std.string; void main() { auto nx = readln.split.map!( to!int ); auto ms = nx[ 0 ].iota.map!( a => readln.strip ).map!( to!int ).array; writeln( solve( nx[ 1 ], ms ) ); } int solve( in int x, in int[] ms ) { return ( cast(int) ms.length ) + ( ( x - ms.sum ) / ms.reduce!( min ) ); } unittest { assert( solve( 1000, [ 120, 100, 140 ] ) == 9 ); assert( solve( 360, [ 90, 90, 90, 90 ] ) == 4 ); assert( solve( 3000, [ 150, 130, 150, 130, 110 ] ) == 26 ); }
D
void main() { long n = readln.chomp.to!long; long[] a = readln.split.to!(long[]); long psum, msum; long pcnt, mcnt; foreach (i; 0 .. n) { psum = psum + a[i]; msum = msum + a[i]; if (i & 1) { if (psum >= 0) { pcnt += psum + 1; psum = -1; } if (msum <= 0) { mcnt += msum.abs + 1; msum = 1; } } else { if (psum <= 0) { pcnt += psum.abs + 1; psum = 1; } if (msum >= 0) { mcnt += msum + 1; msum = -1; } } } min(pcnt, mcnt).writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nx = readln.split.to!(long[]); auto N = nx[0]; auto X = nx[1]; long[51] as, ps; as[0] = 1; ps[0] = 1; foreach (i; 1..51) { as[i] = as[i-1] * 2 + 3; ps[i] = ps[i-1] * 2 + 1; } long r; while (X > 0 && N >= 0) { if (N == 0) { r += 1; break; } if (X >= as[N]/2+1) { r += ps[N-1] + 1; X -= as[N-1] + 2; } else { X -= 1; } --N; } writeln(r); }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop; // dfmt on void main() { auto input = stdin.byLine.map!split.joiner; string S; S = input.front.to!string; input.popFront; solve(S); } void solve(string S){ if (S=="RRR") { writeln(3); return; } if (S=="RRS" || S == "SRR") { writeln(2); return; } if (S[0] == 'R' || S[1] == 'R'||S[2] == 'R') { writeln(1); return; } writeln(0); }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; import std.typecons; import std.bigint; // import dcomp.foundation, dcomp.scanner; // import dcomp.container.deque; int main() { auto sc = new Scanner(stdin); int n, m, k; sc.read(n, m, k); m++; int[] a = new int[n]; a.each!((ref x) => sc.read(x)); long[] dp = a.map!(to!long).array; long[] ndp = new long[n]; auto deq = Deque!(int, false).make(); foreach (int ph; 2..k+1) { ndp[] = -(10L^^18); deq.clear(); foreach (int i; 0..n) { if (deq.length && deq[0] == i-m) { deq.removeFront(); } if (deq.length) { ndp[i] = dp[deq[0]] + 1L * ph * a[i]; } while (deq.length && dp[deq.back] <= dp[i]) { deq.removeBack(); } deq.insertBack(i); } swap(dp, ndp); } writeln(dp.fold!max); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.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); } } } } T[N] fixed(T, size_t N)(T[N] a) {return a;} /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/deque.d */ // module dcomp.container.deque; struct DequePayload(T) { import core.exception : RangeError; private T* _data; private uint start, len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); if (start + i < cap) return _data[start + i]; else return _data[start + i - cap]; } 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 std.algorithm : max; import std.conv : to; if (newCap <= cap) return; T* newData = cast(T*)GC.malloc(newCap * T.sizeof); foreach (i; 0..length) { newData[i] = this[i]; } _data = newData; start = 0; cap = newCap.to!uint; } void clear() { start = len = 0; } import std.algorithm : max; void insertFront(T item) { if (len == cap) reserve(max(cap * 2, 4)); if (start == 0) start += cap; start--; len++; this[0] = item; } void insertBack(T item) { if (len == cap) reserve(max(cap * 2, 4)); len++; this[len-1] = item; } void removeFront() { assert(!empty, "Deque.removeFront: Deque is empty"); start++; len--; if (start == cap) start = 0; } void removeBack() { assert(!empty, "Deque.removeBack: Deque is empty"); len--; } } struct Deque(T, bool mayNull = true) { import core.exception : RangeError; import std.range : ElementType, isInputRange; import std.traits : isImplicitlyConvertible; alias Payload = DequePayload!T; Payload* _p; static if (!mayNull) @disable this(); this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) { _p = new Payload(); foreach (v; values) { insertBack(v); } } this(Range)(Range r) if (isInputRange!Range && isImplicitlyConvertible!(ElementType!Range, T) && !is(Range == T[])) { _p = new Payload(); foreach (v; r) { insertBack(v); } } private this(Payload* p) { _p = p; } static Deque make() { return Deque(new Payload()); } private bool havePayload() const { return (!mayNull || _p); } @property bool empty() const { return (!havePayload || _p.empty); } @property size_t length() const { return (havePayload ? _p.length : 0); } alias opDollar = length; ref inout(T) opIndex(size_t i) inout { assert(!empty, "Deque.opIndex: Deque is empty"); return (*_p)[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void clear() { if (_p) _p.clear(); } void insertFront(T item) { if (mayNull && !_p) _p = new Payload(); _p.insertFront(item); } void insertBack(T item) { if (mayNull && !_p) _p = new Payload(); _p.insertBack(item); } alias opOpAssign(string op : "~") = insertBack; alias stableInsertBack = insertBack; void removeFront() { assert(!mayNull || _p, "Deque.removeFront: Deque is empty"); _p.removeFront(); } void removeBack() { assert(!mayNull || _p, "Deque.removeBack: Deque is empty"); _p.removeBack(); } alias stableRemoveBack = removeBack; alias Range = RangeT!(DequePayload!T); alias ConstRange = RangeT!(const DequePayload!T); alias ImmutableRange = RangeT!(immutable DequePayload!T); size_t[2] opSlice(size_t dim : 0)(size_t start, size_t end) const { assert(start <= end && end <= length); return [start, end]; } Range opIndex(size_t[2] rng) { return Range(_p, rng[0], rng[1]); } ConstRange opIndex(size_t[2] rng) const { return ConstRange(_p, rng[0], rng[1]); } ImmutableRange opIndex(size_t[2] rng) immutable { return ImmutableRange(_p, rng[0], rng[1]); } auto opIndex() inout { return this[0..$]; } static struct RangeT(QualifiedPayload) { alias A = QualifiedPayload; import std.traits : CopyTypeQualifiers; alias E = CopyTypeQualifiers!(A, T); A *p; size_t l, r; @property bool empty() const { return r <= l; } @property size_t length() const { return r - l; } alias opDollar = length; @property auto save() { return this; } ref inout(E) opIndex(size_t i) inout { version(assert) if (empty) throw new RangeError(); return (*p)[l+i]; } @property ref inout(E) front() inout { return this[0]; } @property ref inout(E) back() inout { return this[$-1]; } void popFront() { version(assert) if (empty) throw new RangeError(); l++; } void popBack() { version(assert) if (empty) throw new RangeError(); r--; } size_t[2] opSlice(size_t dim : 0)(size_t start, size_t end) const { assert(start <= end && end <= length); return [start, end]; } auto opIndex(size_t[2] rng) { return RangeT(p, l+rng[0], l+rng[1]); } auto opIndex(size_t[2] rng) const { return RangeT!(const A)(p, l+rng[0], l+rng[1]); } auto opIndex(size_t[2] rng) immutable { return RangeT!(immutable A)(p, l+rng[0], l+rng[1]); } auto opIndex() inout { return this[0..$]; } } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; // import dcomp.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 read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/stackpayload.d */ // module dcomp.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--; } } /* This source code generated by dcomp and include dcomp's source code. dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp) dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt) */
D
import std.algorithm; import std.array; import std.bigint; import std.bitmanip; import std.conv; import std.numeric; import std.range; 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; } T[] readToArray(T)() { return readln.split.to!(T[]); } void readInto(T...)(ref T ts) { auto ss = readln.split; foreach(ref t; ts) { t = ss.front.to!(typeof(t)); ss.popFront; } } // 冪乗をmod取りつつ計算 ulong modPow(ulong a, ulong n, ulong m) { ulong r = 1; while (n > 0) { if(n % 2 != 0) r = r * a % m; a = a * a % m; n /= 2; } return r; } // フェルマーの小定理から乗法逆元を計算 // 定理の要請により法は素数 ulong modInv(ulong a, ulong m) { return modPow(a, m-2, m); } // mod取りつつ組み合わせを計算 // modInvを使っているので法は素数 ulong modComb(ulong n, ulong r, ulong m) { if (n == r) return 1; if (r == 0) return 1; if (n < r) return 0; ulong up = 1; ulong down = 1; for(ulong i = n-r+1; i <= n; i++) { up *= i; up %= m; } for(ulong i = 2; i <= r; i++) { down *= i; down %= m; } return up*modInv(down, m) % m; } ulong MOD = 1000000007; void main() { ulong n, k; readInto(n, k); for (ulong i = 1; i <= k; i++) { writeln(modComb(n-k+1, i, MOD)*modComb(k-1, i-1, MOD) % MOD); } }
D
/+ dub.sdl: name "B" dependency "dunkelheit" version="1.0.1" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; int main() { Scanner scn = new Scanner(stdin); scope(exit) assert(!scn.hasNext); int h, w, n, sr, sc; string s, t; scn.read(h, w, n, sr, sc, s, t); sr--; sc--; int le = 0, ri = w, dw = 0, up = h; foreach_reverse (ph; 0..n) { { char c = t[ph]; final switch(c) { case 'U': up = min(up + 1, h); break; case 'D': dw = max(dw - 1, 0); break; case 'L': ri = min(ri + 1, w); break; case 'R': le = max(le - 1, 0); break; } } { char c = s[ph]; final switch(c) { case 'U': dw++; break; case 'D': up--; break; case 'L': le++; break; case 'R': ri--; break; } } if (le >= ri || dw >= up) { writeln("NO"); return 0; } debug writefln("%d %d %d %d", le, ri, dw, up); } if (le <= sc && sc < ri && dw <= sr && sr < up) { writeln("YES"); } else { writeln("NO"); } return 0; } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /home/yosupo/Programs/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; private File f; this(File f) { this.f = f; } private char[512] lineBuf; private 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) { assert(succW()); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(Args...)(auto ref Args args) { import std.exception : enforce; static if (args.length != 0) { enforce(readSingle(args[0])); read(args[1..$]); } } bool hasNext() { return succ(); } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std.stdio, std.algorithm, std.range, std.conv; void main(){ iota(1, 10).map!(a => iota(1, 10).map!(b => text(a, "x", b, "=", a*b)).join("\n")).join("\n").writeln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio, std.bitmanip; bool ok(ref long[] A, ref long[] B, long n) { long s = n * (n+1) / 2; if (A.sum % s != 0) return false; long m = A.sum / s; long x = 0; foreach (i; 0..n) B[i] -= m; foreach (i; 0..n) { if (B[i] % n != 0) return false; long d = abs(B[i] / n); x += d; } return x == m; } void main() { auto N = readln.chomp.to!long; auto A = readln.split.map!(to!long).array; auto B = new long[](N); foreach (i; 0..N) { B[i] = A[(i+1)%N] - A[i]; } writeln(ok(A, B, N) ? "YES" : "NO"); }
D
import std.conv, std.stdio; import std.algorithm, std.array, std.string, std.range; void main() { auto x = readln.chomp.to!int, t = x / 500, s = (x % 500) / 5; (t * 1000 + s * 5).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)); } T binarySearch(alias pred, T)(T ok, T ng) { while (abs(ok-ng) > 1) { auto mid = (ok+ng)/2; if (unaryFun!pred(mid)) ok = mid; else ng = mid; } return ok; } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto h = RD; auto c = RD; auto tt = RD; bool f(long x) { return h*x + c*(x-1) > tt*(x*2-1); } auto r = binarySearch!(f)(0L, int.max); if (r == 0) { if (abs((tt - h)*2) < abs(tt*2 - (h+c))) ans[ti] = 1; else ans[ti] = 2; } else if (r == int.max-1) { ans[ti] = 2; } else { auto d1 = cast(double)(h*r + c*(r-1))/(r*2-1) - tt; auto d2 = cast(double)(h*(r+1) + c*r)/(r*2+1) - tt; debug writeln("r:", r, " h*(r+1):", h*(r+1), " c*r:", c*r, " d1:", d1, " d2:", d2); if (abs(d1) <= abs(d2)) ans[ti] = r*2 - 1; else ans[ti] = r*2 + 1; } } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
void main() { auto ip = readAs!(string[]), A = ip[0].to!int, B = ip[2].to!int, op = ip[1]; if(op == "+") { writeln(A + B); } else { writeln(A - B); } } // =================================== import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; void times(alias fun)(int n) { foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } void main() { readln; int ans = 0; readln.chomp.map!"a=='I'?1:-1".fold!((a, b) { ans = max(ans, a+b); return a+b; })(0); ans.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); } } } } // 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
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.0.2" +/ import std.algorithm, std.range, std.stdio, std.math, std.conv, core.bitop; // import dcomp.scanner; // import dcomp.algorithm; // import dcomp.container.simpleSeg; int main() { import std.conv; auto sc = new Scanner(); int n; long e, T; long[] x; sc.read(n, e, T, x); x ~= e; long[] next = iota(n).map!(i => x[i+1]-x[i]).array; long[] dp = new long[](n+1); auto tr1 = SimpleSeg!(long, min, 10L^^12)(n); auto tr2 = SimpleSeg!(long, min, 10L^^12)(n); dp[n] = 0; foreach_reverse (i; 0..n) { tr1[i] = dp[i+1] + 3*x[i] + next[i]; tr2[i] = dp[i+1] + T + x[i] + next[i]; int div = binSearch!(j => 2*(x[j]-x[i]) >= T)(i, n); //[div, n-1) tr1 //[i, div) tr2 long ans = 10L^^12; ans = min(ans, tr1.sum(div, n) - 3*x[i]); ans = min(ans, tr2.sum(i, div) - x[i]); dp[i] = ans; } writeln(dp[0] + x[0]); return 0; } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File, readln, stdin; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeString, isDynamicArray; import std.algorithm : map; File f; this(File f = stdin) { this.f = f; } string[] buf; bool succ() { while (!buf.length) { if (f.eof) return false; buf = readln.split; } return true; } int read(Args...)(auto ref Args args) { foreach (i, ref v; args) { if (!succ()) return i; alias VT = typeof(v); static if (!isSomeString!VT && isDynamicArray!VT) { v = buf.map!(to!(ElementType!VT)).array; buf.length = 0; } else { v = buf.front.to!VT; buf.popFront(); } } return args.length; } } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/container/simpleSeg.d */ // module dcomp.container.simpleSeg; // simpleなsegtree // a op b op .. op x が求められる、遅延評価なし // (T, op)はモノイドの必要がある、eはモノイド struct SimpleSeg(T, alias op, T e) { size_t n; T[] d; this(size_t n) { import std.algorithm : fill; import core.bitop : bsr; int lg = n.bsr; if ((2^^lg) < n) lg++; this.n = 2^^lg; d = new T[](2*this.n); d.fill(e); } void opIndexAssign(T v, int idx) { import std.stdio : writeln; idx += n; d[idx] = v; while (idx/2 >= 1) { idx /= 2; d[idx] = op(d[2*idx], d[2*idx+1]); } } T sum(size_t a, size_t b, size_t l, size_t r, size_t k) { import std.stdio: writefln; if (b <= l || r <= a) return e; if (a <= l && r <= b) return d[k]; size_t md = (l+r)/2; return op(sum(a, b, l, md, 2*k), sum(a, b, md, r, 2*k+1)); } //[a, b) T sum(size_t a, size_t b) { return sum(a, b, 0, n, 1); } } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/algorithm.d */ // module dcomp.algorithm; //[0,0,0,...,1,1,1]で、初めて1となる場所を探す。pred(l) == 0, pred(r) == 1と仮定 T binSearch(alias pred, T)(T l, T r) { while (r-l > 1) { int md = (l+r)/2; if (!pred(md)) l = md; else r = md; } return r; }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto a = readln.splitter.map !(to !(int)).array; auto n = a[0] - 2; a = a[1..$]; bool ok = false; foreach (i; 0..16) { auto corners = 4.iota.map !(k => (i >> k) & 1).array; auto b = a.dup; foreach (k; 0..4) { b[k] -= corners[k]; b[(k + 1) & 3] -= corners[k]; } ok |= b.all !(v => 0 <= v && v <= n); } writeln (ok ? "YES" : "NO"); } }
D
// Try Codeforces // author: Leonardone @ NEETSDKASU import std.stdio, std.string, std.array, std.algorithm, std.ascii; void main() { readln(); auto xs = split(chomp(readln())); auto ys = xs.map!( (x) => x.count!( isUpper ) ); writeln(ys.maxCount[0]); }
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.datetime; void main() { auto N = readln.chomp.to!int; auto G = new int[][](N); foreach (i; 0..N-1) { auto v = readln.chomp.to!int; G[v-1] ~= i+1; } auto is_leaf = new bool[](N); foreach (i; 0..N) is_leaf[i] = G[i].length == 0; auto ok = new bool[](N); foreach (i; 0..N) { if (is_leaf[i]) { ok[i] = true; continue; } int cnt = 0; foreach (m; G[i]) { cnt += is_leaf[m]; } ok[i] = cnt >= 3; } writeln(ok.all ? "Yes" : "No"); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto h = RD!int; auto w = RD!int; auto r = RDA!int; auto c = RDA!int; auto a = new bool[][](h+1, w+1); foreach (i; 0..h) { a[i][r[i]] = true; } foreach (i; 0..w) { a[c[i]][i] = true; } bool ok = true; foreach (y; 0..h) { foreach (x; 0..r[y]) { if (a[y][x]) ok = false; } } foreach (x; 0..w) { foreach (y; 0..c[x]) { if (a[y][x]) ok = false; } } if (!ok) writeln(0); else { long cnt; foreach (y; 0..h) { foreach (x; 0..w) { if (x <= r[y] || y <= c[x]) ++cnt; } } debug writeln(cnt); cnt = h * w - cnt; debug writeln(cnt); /*auto num = new long[](32); num[0] = 2; foreach (i; 1..num.length) { num[i] = num[i-1]; num[i].modm(2); }*/ long ans = 1; foreach (_; 0..cnt) { ans.modm(2); } 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 (); foreach (tid; 0 .. nt) { const n = r.next!uint (); auto d = r.next!uint (); auto a = r.nextA!uint (n); foreach (i; 1 .. n) { while (d >= i && a[i] > 0) { d -= i; a[0]++; a[i]--; } } writeln (a[0]); } }
D
// Try Codeforces // author: Leonardone @ NEETSDKASU import std.algorithm : sort, sum; import std.array : split, appender; import std.conv : to; import std.stdio : readln, writeln; import std.string : chomp; auto gets() { return readln.chomp; } auto getV(T)() { return gets.to!T; } auto getVals(T)() { return gets.split.to!(T[]); } long c(int n, int k) { long ret = 1; foreach (v; 0..k) { ret *= n - v; ret /= v + 1; } return ret; } void main() { auto s = getVals!int; auto n = s[0], m = s[1]; auto cols = new int[m]; long ans = n * m; foreach (_; 0..n) { auto rows = getVals!int; auto x = 0; foreach (i, v; rows) { if (v == 0) { continue; } cols[i]++; x++; } foreach (i; 1..x) { ans += c(x, i+1); } x = m - x; foreach (i; 1..x) { ans += c(x, i+1); } } foreach (v; cols) { auto x = v; foreach (i; 1..x) { ans += c(x, i+1); } x = n - x; foreach (i; 1..x) { ans += c(x, i+1); } } writeln(ans); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; import std.math; int n; rd(n); auto a=readln.split.to!(int[]); writeln(reduce!((r,e)=>(r+e.abs))(0, a)); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto n = RD!int; auto m = RD!int; auto k = RD!int; writeln(m >= n && k >= n ? "Yes" : "No"); stdout.flush(); debug readln(); }
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; void main(string[] args) { inputFile = stdin; debug inputFile = File(args[1]); auto nt = next!int; foreach(t; 0 .. nt) { auto n = next!int; auto ss = next!string(n); int[char] cnt; foreach(s; ss) foreach(c; s) cnt.require(c, 0)++; if (cnt.byValue.all!(c => c % n == 0)) writeln("YES"); else writeln("NO"); } } // INPUT enum InputStyle { byChunk, byLine }; enum inputStyle = InputStyle.byChunk; File inputFile; string popWord(); static if (inputStyle == InputStyle.byChunk) { const chunkSize = 4096; const wordSize = 4096; char[chunkSize] chunkBuff; char[] currChunk; void renewChunk() { if (inputFile.eof) currChunk = null; else currChunk = inputFile.rawRead(chunkBuff); } char[wordSize] wordBuff; char[] word; string popWord() { if (currChunk.length == 0) renewChunk; assert(currChunk.length > 0); while (currChunk[0].isWhite) { currChunk = currChunk[1 .. $]; if (currChunk.length == 0) renewChunk; } word = wordBuff[0 .. 0]; while (!currChunk[0].isWhite) { word.length++; word[$ - 1] = currChunk[0]; currChunk = currChunk[1 .. $]; if (currChunk.length == 0) { renewChunk; if (currChunk.length == 0) return cast(string) word; } } return cast(string) word; } } else { DList!string _words; string popWord() { while (_words.empty) { foreach(w; inputFile.readln.split) { _words.insertBack(w); } } auto word = _words.front; _words.removeFront; return word; } } T next(T)() { return to!T(popWord); } auto next(T, S...)(S s) { static if (S.length == 0) { return to!T(popWord); } else { auto res = new typeof(next!T(s[1 .. $]))[](s[0]); foreach(ref elem; res) elem = next!T(s[1 .. $]); return res; } }
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(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto a = RDA; auto b = new long[](n); foreach (i; 1..n-1) { if (a[i] > a[i-1] && a[i] > a[i+1]) b[i] = 1; else if (a[i] < a[i-1] && a[i] < a[i+1]) b[i] = 1; } long cnt; foreach (i; 1..n-1) { long c0 = b[i-1..i+2].sum; long f() { long res; foreach (j; i-1..i+2) { if (j == 0 || j == n-1) continue; if (a[j] > a[j-1] && a[j] > a[j+1]) ++res; else if (a[j] < a[j-1] && a[j] < a[j+1]) ++res; } return res; } auto tmp = a[i]; a[i] = a[i-1]; long c1 = f(); a[i] = a[i+1]; long c2 = f(); a[i] = tmp; cnt.chmax(max(c0 - c1, c0 - c2)); } ans[ti] = b.sum - cnt; } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math; import core.stdc.stdio; // foreach, foreach_reverse, writeln void main() { const int MOD = 998244353; string s = chomp(readln()); int n = to!int(s.length); int[] a = new int[n]; foreach (i; 0..n) { a[i] = s[i]-'a'; } bool same() { foreach (i; 1..n) { if (a[i-1] != a[i]) return false; } return true; } if (same()) { writeln(1); return; } if (n == 2) { writeln(2); return; } if (n == 3) { if (a[0] != a[1] && a[1] != a[2] && a[2] != a[0]) { writeln(3); return; } } long ans = 1; foreach (i; 0..n-1) ans = (ans*3)%MOD; bool unstable() { foreach (i; 1..n) { if (a[i-1] == a[i]) return false; } return true; } if (unstable()) ans++; long[][][] dp = new long[][][](n,3,3); foreach (i; 0..3) dp[0][i][i] = 1; foreach (i; 1..n) { foreach (j; 0..3) foreach (k; 0..3) { foreach (x; 0..3) { if (k == x) continue; (dp[i][(j+x)%3][x] += dp[i-1][j][k]) %= MOD; } } } ans -= dp[n-1][a.sum%3].sum; ans = (ans%MOD+MOD)%MOD; writeln(ans); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { auto ab = aryread!string(); // writeln(ab); string ab_str = ab.join(); // writeln(ab_str); long tmp; foreach (i; 0 .. ab_str.length) { // writeln(10 ^^ (((ab_str.length) - 1) - i)); tmp += (ab_str[i] - '0') * 10 ^^ (((ab_str.length) - 1) - i); } // writeln(tmp); foreach (i; 1 .. 10 ^^ 4) { if (i * i == tmp) { writeln("Yes"); return; } } writeln("No"); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
void main() { problem(); } void problem() { const N = scan!int; const K = scan!long; const A = scan!int(N); void solve() { int[int] alreadyVisited; int count; int last; for(int next = 1; !(next in alreadyVisited); next = A[next-1]) { if (count == K) { writeln(next); return; } last = next; alreadyVisited[next] = count++; } const loopback = A[last-1]; const stepBeforeLoop = alreadyVisited[loopback]; const loopSize = count - stepBeforeLoop; [last, loopback, count, stepBeforeLoop, loopSize].deb; const amari = (K - stepBeforeLoop) % loopSize; int point = loopback; foreach(i; 0..amari) { point = A[point - 1]; } writeln(point); } solve(); } // ---------------------------------------------- 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.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 A = RD; auto B = RD; writeln(max(A+B, max(A - B, A * B))); stdout.flush(); debug readln(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto r = readln.chomp.to!int; writeln(3 * r^^2); }
D
import std.stdio; import std.algorithm; import std.math; import std.conv; import std.string; T readNum(T)(){ return readStr.to!T; } T[] readNums(T)(){ return readStr.split.to!(T[]); } string readStr(){ return readln.chomp; } void main(){ auto xa = readNums!int; if(xa[0] < xa[1]){ writeln(0); } else { writeln(10); } }
D
void main(){ int[] x = _scanln!()(); foreach(i, elm; x){ if(elm == 0){ writeln(i+1); return; } } } import std.stdio, std.conv, std.algorithm, std.numeric, std.string; // 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; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; void main() { int n, m; scan(n, m); auto a = readln.split.to!(int[]); int[int] asc; int[] b = new int[](n + 1); asc[0] = 1; b[n] = 0; int val; long sum; foreach_reverse (i ; 0 .. n) { val += a[i]; val %= m; if (val in asc) { asc[val]++; } else { asc[val] = 1; } b[i] = val; sum += a[i]; } debug { writeln(asc); writeln(b); } long ans; foreach (i ; 0 .. n) { if (sum % m in asc) { ans += asc[sum % m]; } asc[b[i]]--; sum -= a[i]; } writeln(ans - n); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.string, std.conv; void main() { auto ip = readln.chomp, X = ip[0], Y = ip[2]; if(X > Y){ writeln(">"); } else if(X < Y){ writeln("<"); } else { writeln("="); } }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { auto N = readln.chomp.to!int; string[] wordList; bool result = true; foreach (i; 0..N) { auto W = readln.chomp; scope(exit) wordList ~= W; if (wordList is null) continue; if (wordList.canFind(W)) result = false; if (wordList.back.back != W.front) result = false; } writeln(result ? "Yes" : "No"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string; void main() { readln.count('1').writeln; }
D
import core.stdc.stdio; import std.algorithm; import std.stdio; int lowerBound(int[] data,int t){ int l=0; int r=cast(int)data.length+1; while(r-l>1){ int m=(l+r)/2; if(data[m-1]<t) l=m; else r=m; } return l; } void main(){ int n,m; scanf("%d%d",&n,&m); int[] u = new int[n]; int[] k = new int[n]; foreach(i;0..m){ scanf("%d",&u[i]); int j; scanf("%d",&j); k[--j]=u[i]; } foreach(i;m..n) scanf("%d",&u[i]); sort(u); int[] d=new int[n*2]; static immutable int inf = 114514; bool Solve(int t){ int c=n-u.lowerBound(t); foreach(i;0..n) if(k[i]) if(k[i]>=t){ d[i]=0; c--; }else d[i]=inf; else d[i]=1; int b=0,e=n; while(b<e-1){ d[e++]=min(inf,d[b]+d[b+1]+d[b+2]-max(d[b],d[b+1],d[b+2])); b+=3; } return d[e-1]<=c; } int l=0,r=n; while(r-l>1){ int s=(r+l)/2; if(Solve(u[s])) l=s; else r=s; } printf("%d\n",u[l]); }
D
import std.algorithm; import std.array; import std.ascii; import std.bigint; import std.complex; import std.container; import std.conv; import std.functional; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } void readlnTo(T...)(ref T t) { auto s = readln().split(); assert(s.length == t.length); foreach(ref ti; t) { ti = s[0].to!(typeof(ti)); s = s[1..$]; } } const real eps = 1e-10; void main(){ int[] l = [0, 0, 6, 13, 18, 23, 43, 58]; auto c = [ [1,2]: 300, [1,3]: 500, [1,4]: 600, [1,5]: 700, [1,6]: 1350, [1,7]: 1650, [2,3]: 350, [2,4]: 450, [2,5]: 600, [2,6]: 1150, [2,7]: 1500, [3,4]: 250, [3,5]: 400, [3,6]: 1000, [3,7]: 1350, [4,5]: 250, [4,6]: 850, [4,7]: 1300, [5,6]: 600, [5,7]: 1150, [6,7]: 500 ]; while(true) { auto d = readInt(); if(d == 0) { return; } int hd, md; readlnTo(hd, md); md += hd*60; auto a = readInt(); int ha, ma; readlnTo(ha, ma); ma += ha*60; if(a < d) { swap(a, d); } if((17*60+30 <= md && md <= 19*60+30) || (17*60+30 <= ma && ma <= 19*60+30)) { if(l[a]-l[d] <= 40) { auto ans = c[[d,a]]/2; if(ans%50 != 0) { ans = ans / 50 * 50 + 50; } writeln(ans); } else { writeln(c[[d,a]]); } } else { writeln(c[[d,a]]); } } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long A, P; scan(A, P); writeln((3 * A + P) / 2); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { auto n = sread(); // writeln(n); long ans; foreach (i; 0 .. (n.length)) { ans += n[i] - '0'; } if (ans % 9 == 0) { writeln("Yes"); } else { writeln("No"); } } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
void main() { problem(); } void problem() { const MAX = 1_000_000_000_000_001; auto N = scan!long - 1; string solve() { char[] ans; foreach(i; 0..1000) { auto mod = N % 26; N /= 26; N -= 1; ans ~= 'a' + cast(char)mod; if (N < 0) { break; } } return cast(string)ans.reverse(); } 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, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; void main() { int a, b; scan(a, b); writeln(max(a + b, a - b, a * b)); } 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