code
stringlengths
4
1.01M
language
stringclasses
2 values
void main() { long x = rdElem; bool[] lists = new bool[x+1]; lists[1] = true; for (long i = 2; i * i <= x; ++i) { for (long j = i * i; j <= x; j *= i) { lists[j] = true; } } foreach_reverse (i, y; lists) { if (y) { i.writeln; return; } } } 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; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto dn = readln.chomp.split.to!(int[]); if (dn[1] == 100) { dn[1]++; } auto a = 100 ^^ dn[0] * dn[1]; writeln(a); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, 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 b = RD!string; if (b == "A") writeln("T"); else if (b == "T") writeln("A"); else if (b == "C") writeln("G"); else writeln("C"); stdout.flush(); debug readln(); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { auto c = new int[][](3, 3); iota(3).each!(i => c[i] = readln.split.to!(int[])); bool ok = 1; auto d = new int[][](3, 2); foreach (i ; 0 .. 3) { foreach (j ; 0 .. 2) { d[i][j] = c[i][j+1] - c[i][j]; } } if (!(d[0] == d[1] && d[1] == d[2])) { writeln("No"); return; } foreach (j ; 0 .. 3) { foreach (i ; 0 .. 2) { d[j][i] = c[i+1][j] - c[i][j]; } } if (d[0] == d[1] && d[1] == d[2]) { writeln("Yes"); } else { writeln("No"); } } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } struct Queue(T) { private { size_t cap, head, tail; T[] data; } this (size_t n) in { assert(n > 0, "The capacity of a queue must be a positive integer."); } body { cap = n + 1; data = new T[](cap); } void clear() { head = tail = 0; } bool empty() { return head == tail; } bool full() { return head == (tail + 1) % cap; } size_t length() { return head <= tail ? tail - head : cap - head + tail; } T front() in { assert(!empty, "The queue is empty."); } body { return data[head]; } void removeFront() in { assert(!empty, "The queue is empty."); } body { (++head) %= cap; } alias popFront = removeFront; void insertBack(T x) in { assert(!full, "The queue is full."); } body { data[tail++] = x; tail %= cap; } alias insert = insertBack; T[] array() { return head <= tail ? data[head .. tail].dup : data[head .. $] ~ data[0 .. tail]; } string toString() { import std.format : format; if (head <= tail) { return format("[%(%s, %)]", data[head .. tail]); } else { return format("[%(%s, %)", data[head .. $]) ~ format(", %(%s, %)]", data[0 .. tail]); } } }
D
import std.stdio, std.conv, std.array, std.string; import std.algorithm; import std.container; void main() { auto N = readln.chomp.to!ulong; ulong sum = 0; for(ulong i=0; i<N; i++) { sum += i; } writeln(sum); }
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; int calc(int h, int w, int k, string[] S) { int ans = int.max; auto g = new int[h]; for (int mask = 0; mask < (1 << (h - 1)); mask++) { int num = 0; foreach (i; 0..h) { g[i] = num; if ((mask >> i) & 1) num++; } num++; // +1 することでグループ総数になる int cut = num - 1; // カット回数 int[10] cnt; // グループごとの 1 の個数 for (int col = 0; col < w; col++) { for (int row = 0; row < h; row++) { if (S[row][col] == '1') cnt[g[row]]++; } foreach (i; 0..num) { if (cnt[i] > k) { // 溢れるので、ここで縦にカット cut++; cnt[] = 0; for (int r = 0; r < h; r++) { if (S[r][col] == '1') cnt[g[r]]++; } break; } } // 1 列のみでも溢れるかチェック foreach (i; 0..num) { if (cnt[i] > k) { cut = int.max/2; break; } } } ans = min(ans, cut); } return ans; } void main() { int h, w, k; scan(h, w, k); string[] S; foreach (_; 0..h) S ~= read!string; writeln(calc(h, w, k, S)); } 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.stdio; import std.conv; import std.algorithm; import std.math; void main() { long n; scanf("%ld", &n); long m = long.max; foreach(i; 1..n.to!double.sqrt.ceil.to!size_t+2) { if (n % i == 0) { m = min(i + n/i-2, m); } } m.write; }
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; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // struct P{ int h, w; } void solve(){ int h = scan!int, w = scan!int, m = scan!int; P[] ps; foreach(i; 0 .. m) ps ~= P(scan!int - 1, scan!int - 1); int[] xs = new int[](h); foreach(p; ps) xs[p.h] += 1; int[] ys = new int[](w); foreach(p; ps) ys[p.w] += 1; // Holizontal and vertical can be done separatedly int[] ansi, ansj; int bestx, besty; foreach(i; 0 .. h){ if(xs[i] > bestx) bestx = xs[i], ansi = []; if(xs[i] >= bestx) ansi ~= i; } foreach(j; 0 .. w){ if(ys[j] > besty) besty = ys[j], ansj = []; if(ys[j] >= besty) ansj ~= j; } // If there is no box at some crossing, ans(h) + ans(w) is the answer; else ans(h) + ans(w) - 1 bool[P] pset; foreach(p; ps) pset[p] = 1; int ansvalue = bestx + besty - 1; A: foreach(i; ansi){ foreach(j; ansj){ // This loop breaks in at most M times if(P(i, j) in pset) continue; else{ ansvalue = bestx + besty; break; } } } ansvalue.writeln; }
D
import std.stdio; import std.conv; import std.string; void main() { while(true) { string [] s = split(readln()); int H = to!int(s[0]); int W = to!int(s[1]); if(H==0 && W==0) break; for(int i=0;i<H;i++) { for(int j=0;j<W;j++) { if(i==0 || i==H-1 || j==0 || j==W-1) { write("#"); } else { write("."); } } writeln(); } writeln(); } }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n, z, w; rd(n, z, w); auto a = readln.split.to!(int[]); import std.math : abs; struct P { int i, t, p; } int[P] memo; int f(int i, int t, int pre) { if (i + 1 == n) { return abs(a[i] - pre); } else { auto key = P(i, t, pre); if (key in memo) { return memo[key]; } int ret = t ? 2 * 10 ^^ 9 : 0; if (t) { for (int j = i; j + 1 < n; j++) { ret = min(ret, f(j + 1, t ^ 1, a[j])); } ret = min(ret, abs(a[n - 1] - pre)); } else { for (int j = i; j + 1 < n; j++) { ret = max(ret, f(j + 1, t ^ 1, a[j])); } ret = max(ret, abs(a[n - 1] - pre)); } return memo[key] = ret; } } writeln(f(0, 0, w)); } void rd(T...)(ref T x) { import std.stdio : readln; import std.string : split; import std.conv : to; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; int[10^^5+1] L, R; void main() { auto N = readln.chomp.to!int; auto AS = readln.split.to!(int[]); auto g = AS[0]; foreach (i, a; AS) { g = gcd(g, a); L[i] = g; } g = AS[$-1]; foreach_reverse (i, a; AS) { g = gcd(g, a); R[i] = g; } int max_g; foreach (i; 0..N) { if (i == 0) { g = R[i+1]; } else if (i == N-1) { g = L[i-1]; } else { g = gcd(L[i-1], R[i+1]); } max_g = max(max_g, g); } writeln(max_g); }
D
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math, std.container, std.typecons; import core.stdc.stdio; // foreach, foreach_reverse, writeln void main() { int n; scanf("%d", &n); int[] p = new int[n]; foreach(i; 0..n) { scanf("%d", &p[i]); p[i]--; } int[] a; bool prev = true; int s = 0; bool judge(int[] a) { //writeln(a); int n = a.length.to!int; if (n == 0) return true; if (n%2 == 0) { n--; a = a[0..n]; } foreach (i; 0..n) { if (i%2 == 0) { if (a[i] < 0 || a[i] >= n) return false; if (a[i]%2 == 1) return false; } else { if (a[i] != i) return false; } } int l = -1, r = -1; foreach (i; 0..n) { if (i%2) continue; if (a[i] < i) { if (a[i] < l) return false; l = a[i]; } else { if (a[i] < r) return false; r = a[i]; } } return true; } foreach (i; 0..n) { if (p[i] == i) { if (prev) { if (!judge(a)) { writeln("No"); return; } a = new int[0]; s = i+1; } else { a ~= p[i]-s; } prev = true; } else { if (!prev) { if (!judge(a)) { writeln("No"); return; } a = new int[0]; s = i; a ~= p[i]-i; } else { a ~= p[i]-s; } prev = false; } } if (!judge(a)) { writeln("No"); } else { writeln("Yes"); } }
D
import std.stdio; import std.string; import std.conv; ulong debt(ulong x, int n) { if(n > 0) { ulong m = x * 105 / 100; if(m % 1000 > 0) m = (m / 1000 + 1) * 1000; return debt(m, n - 1); } else return x; } void main() { int n = to!int(chomp(readln())); writeln(debt(100000, n)); }
D
void main() { string s = readln.chomp; writeln(s[0], s.length - 2, s[$-1]); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto K = RD; auto cnt = N / K; auto ans = cnt^^3; if (K % 2 == 0) { auto cnt2 = (N+K/2) / K; ans += cnt2^^3; } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void main() { auto nxm = readln.split.to!(long[]); auto N = nxm[0]; auto X = nxm[1]; auto M = nxm[2]; long res; long[] DP; long[long] MEMO; foreach (i; 0..N) { if (X == 0) { writeln(res); return; } if (X in MEMO) { auto s = MEMO[X]; DP = DP[s..$]; foreach (j; 1..DP.length) DP[j] += DP[j-1]; auto rr = N - i; auto d = rr / DP.length; res += DP[$-1] * d; if (rr % DP.length != 0) { res += DP[rr % DP.length - 1]; } writeln(res); return; } DP ~= X; MEMO[X] = i; res += X; X = X^^2 % M; } writeln(res); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.typecons; import std.numeric, std.math; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto X = RD; long ans, pos; foreach (i; 1..X+1) { pos += i; if (pos >= X) { ans = i; break; } } writeln(ans); stdout.flush(); }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; void main(){ auto ip = readln.chomp.to!(dchar[]); (ip.sort().array == "abc" ? "Yes" : "No").writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; import core.bitop; alias X = Tuple!(int, "n", int, "f", int, "t"); void main() { auto N = readln.chomp.to!int; auto AS = readln.split.to!(int[]); auto BS = readln.split.to!(int[]); auto DP = new int[][](1<<N, 51); foreach (ref dp; DP) dp[] = -1; int solve(uint s, int p) { auto i = popcnt(s); if (i == N) return 0; if (DP[s][p] == -1) { int r = int.max/3; foreach (j; 0..N) if (!(s&(1<<j))) { auto x = (i+j)%2 == 0 ? AS[j] : BS[j]; if (x < p) continue; int d; foreach (k; j+1..N) if (s&(1<<k)) ++d; r = min(r, solve(s | (1<<j), x) + d); } DP[s][p] = r; } return DP[s][p]; } auto r = solve(0, 0); writeln(r >= int.max/3 ? -1 : r); }
D
void main() { auto M = ri; writeln(48 - M); } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio; import std.string; import std.conv; struct BloodData { int number; string bloodGroup; } void main() { string[] input = new string[](2); BloodData[] list; int[] count = [0, 0, 0, 0]; while ((input = readln.chomp.split(",")).length != 0) { BloodData temp = {number:input[0].to!int, bloodGroup:input[1]}; list ~= temp; } foreach (data; list) { if (data.bloodGroup == "A") { count[0]++; } else if (data.bloodGroup == "B") { count[1]++; } else if (data.bloodGroup == "AB") { count[2]++; } else if (data.bloodGroup == "O") { count[3]++; } } foreach (num; count) { writeln(num); } }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto x = new int[](n), y = new int[](n), h = new long[](n); foreach (i; 0 .. n) rd(x[i], y[i], h[i]); long min_h = 10 ^^ 9, max_h = 0; foreach (i; 0 .. n) { min_h = min(min_h, h[i]); max_h = max(max_h, h[i]); } min_h = max(1, min_h - 200); max_h = max_h + 200; import std.math : abs; for (int cx = 0; cx <= 100; cx++) { for (int cy = 0; cy <= 100; cy++) { for (long height = min_h; height <= max_h; height++) { foreach (i; 0 .. n) { if (max(0, height - abs(x[i] - cx) - abs(y[i] - cy)) != h[i]) { goto hell; } } writeln(cx, " ", cy, " ", height); return; hell:; } } } } void rd(T...)(ref T x) { import std.stdio : readln; import std.string : split; import std.conv : to; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
D
import std.stdio, std.conv, std.string; import std.algorithm, std.array, std.container; import std.numeric, std.math; import core.bitop; string my_readln() { return chomp(readln()); } long mod = pow(10, 9) + 7; long moda(long x, long y) { return (x + y) % mod; } long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; } long modm(long x, long y) { return (x * y) % mod; } void main() { auto tokens = my_readln.split; auto N = tokens[0].to!long; auto K = tokens[1].to!long; auto A = my_readln.split.to!(long[]); long ans = 1; N -= K; while (N > 0) { ++ans; N -= K - 1; } writeln(ans); stdout.flush(); }
D
import std.stdio, std.string, std.conv; import std.algorithm, std.array; auto solve(string s_) { auto NAB = s_.split.map!(to!long)(); immutable N=NAB[0], A=NAB[1], B=NAB[2]; if(A>B || (A<B && N<2)) return 0; return (B-A)*(N-2)+1; } void main(){ for(string s; (s=readln.chomp()).length;) writeln(solve(s)); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.math; import std.range; void main(){ auto N = readln.chomp.to!int; if(N/10==9 || N%10==9) writeln("Yes"); else writeln ("No"); }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; long n, k; rd(n, k); long tot = 0; for (long a = 1; a <= n; a++) { if ((a * 2) % k == 0) { auto x = (a + n) / k - (a + 1 + k - 1) / k + 1; tot += x * x; } } writeln(tot); } void rd(T...)(ref T x) { import std.stdio : readln; import std.string : split; import std.conv : to; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n; readV(n); int[] x; readA(n, x); int l; readV(l); auto m = (n-1).bsr+2; auto y = new int[](n); foreach (i; 0..n) y[n-i-1] = -x[i]; auto build(int[] x) { auto s = new int[][](n, m); foreach (j; 0..m) s[n-1][j] = n-1; foreach_reverse (i; 0..n-1) { s[i][0] = x.assumeSorted.lowerBound(x[i]+l+1).length.to!int-1; foreach (j; 1..m) s[i][j] = s[s[i][j-1]][j-1]; } return s; } auto s1 = build(x), s2 = build(y); auto calc(int[][] s, int a, int b) { auto c = a, r = 0; while (c < b) { auto t = s[c].assumeSorted.lowerBound(b); if (t.empty) { ++r; break; } else { r += (1<<(t.length-1)); c = t.back; } } return r; } int q; readV(q); foreach (_; 0..q) { int a, b; readV(a, b); --a; --b; if (a < b) writeln(calc(s1, a, b)); else writeln(calc(s2, n-a-1, n-b-1)); } } pragma(inline) { pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; } pure T bitSet(T)(T n, size_t i) { return n | (T(1) << i); } pure T bitReset(T)(T n, size_t i) { return n & ~(T(1) << i); } pure T bitComp(T)(T n, size_t i) { return n ^ (T(1) << i); } pure T bitSet(T)(T n, size_t s, size_t e) { return n | ((T(1) << e) - 1) & ~((T(1) << s) - 1); } pure T bitReset(T)(T n, size_t s, size_t e) { return n & (~((T(1) << e) - 1) | ((T(1) << s) - 1)); } pure T bitComp(T)(T n, size_t s, size_t e) { return n ^ ((T(1) << e) - 1) & ~((T(1) << s) - 1); } import core.bitop; pure int bsf(T)(T n) { return core.bitop.bsf(ulong(n)); } pure int bsr(T)(T n) { return core.bitop.bsr(ulong(n)); } pure int popcnt(T)(T n) { return core.bitop.popcnt(ulong(n)); } }
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() { // 日本語文字列のテスト int w = readint; int n = readint; auto xs = new int[w + 1]; for (int i = 0; i < xs.length; i++) xs[i] = i; for (int i = 0; i < n; i++) { auto ab = readln.chomp.split(",").to!(int[]); int a = ab[0], b = ab[1]; swap(xs[a], xs[b]); } foreach (x; xs[1 .. $]) writeln(x); }
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; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; long calc(int[] a) { long ans = 0; int hi = 0; int sum = a[0]; // lo=0 の分 for (int lo = 0; lo < a.length; lo++) { // hi を伸ばせるところまで伸ばす while (hi + 1 < a.length && sum + a[hi + 1] == (sum ^ a[hi + 1])) { sum += a[hi + 1]; hi++; } ans += hi - lo + 1; // lo を右にずらすのでその分だけ引く sum -= a[lo]; // hi = max(hi, lo); } return ans; } void main() { readint; auto a = readints; writeln(calc(a)); }
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; auto ans = max(A, B) - min(A, B); if (ans % 2 == 1) writeln("IMPOSSIBLE"); else writeln(max(A, B) - ans / 2); stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.string; import std.algorithm, std.array, std.container; import std.numeric, std.math; string my_readln() { return chomp(readln()); } long mod = pow(10, 9) + 7; long moda(long x, long y) { return (x + y) % mod; } long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; } long modm(long x, long y) { return (x * y) % mod; } void main() { auto tokens = my_readln().split(); auto N = tokens[0].to!uint; auto K = tokens[1].to!uint; auto a = my_readln.split.to!(long[]); auto dp = new long[](100005); foreach (i; 0..K) { if (dp[i] == 1) continue; foreach (j; 0..N) { dp[i+a[j]] = 1; } } writeln(dp[K] == 1 ? "First" : "Second"); stdout.flush(); }
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(); long[] tmp = [1, 2, 4, 8, 16, 32, 64]; long ans = -1; foreach (i; 0 .. tmp.length) { if (n >= tmp[i]) { // writeln(tmp[i]); ans = max(ans, tmp[i]); } } writeln(ans); } long func(long n) { long cnt; while (n % 2 == 0) { n /= 2; cnt += 1; } return cnt; } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.algorithm; import std.array; import std.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; } long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } long lcm(long a, long b) { return a / gcd(a, b) * b; } long calc(long[] xs) { long ans = xs[0]; for (int i = 1; i < xs.length; i++) { ans = lcm(ans, xs[i]); } return ans; } void main() { int n = readint(); long[] xs; for (int i = 0; i < n; i++) { xs ~= readln.chomp.to!long; } writeln(calc(xs)); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; void main() { auto n = readln.chomp.to!int; foreach (i; 0..n) { readln.replace("Hoshino", "Hoshina").write; } }
D
module abc129.abc129_b; import std.algorithm; import std.container; import std.conv; import std.math; import std.range; import std.stdio; import std.string; void main() { auto n = readln.chomp.to!int; auto w = readln.chomp.split.to!(int[]); int ans = int.max; iota(1, n).each!(t => ans = ans.min(abs(w[0..t].sum - w[t..$].sum))); ans.writeln; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}} void main() { int n; readV(n); auto a = new int[][](2); foreach (i; 0..2) readA(n, a[i]); auto r = 0; foreach (i; 0..n) r = max(r, a[0][0..i+1].sum + a[1][i..$].sum); writeln(r); }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n, m; rd(n, m); int[] a; for (int i = 2; i * i <= m; i++) { if (m % i == 0) { int cnt = 0; while (m % i == 0) { cnt++; m /= i; } a ~= cnt; } } if (m > 1) { a ~= 1; } const long mod = 10 ^^ 9 + 7; const size_t M = 10 ^^ 5 * 2; static long[M] fac, inv; { // init fac[0] = fac[1] = 1; foreach (i; 2 .. M) fac[i] = i * fac[i - 1] % mod; long _pow(long a, long x) { if (x == 0) return 1; else if (x == 1) return a; else if (x & 1) return a * _pow(a, x - 1) % mod; else return _pow(a * a % mod, x / 2); } foreach (i; 0 .. M) inv[i] = _pow(fac[i], mod - 2); } long cmb(long nn, long rr) { if (nn < rr) return 0; return fac[nn] * inv[rr] % mod * inv[nn - rr] % mod; } long tot = 1; foreach (e; a) { (tot *= cmb(e + n - 1, e)) %= mod; } writeln(tot); } 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; void main() { int t; scanf("%d", &t); getchar(); foreach(_; 0..t) { int n; scanf("%d", &n); getchar(); auto d = n % 7; if (d == 0) writeln(n); else { int m = n % 10; if (m >= d) writeln(n - d); else { writeln(n + 7 - (n+7) % 7); } } } }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { const xs = 5.iota.map!(_ => readln.chomp.to!int).array; const k = readln.chomp.to!int; foreach (i; 0..5) { foreach (j; i+1..5) { if (xs[j] - xs[i] > k) { writeln(":("); return; } } } writeln("Yay!"); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.container; import std.array; import std.math; import std.range; import std.typecons; import std.ascii; void main() { readln; int right; int left; auto s = readln.chomp; foreach(c; s) { if (c == '(') { ++right; } else { --right; if (right < 0) { ++left; right = 0; } } } writeln = '('.repeat.take(left).array ~ s ~ ')'.repeat.take(right).array; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto n = RD!int; auto m = RD!int; auto a = RDA; ans[ti] = a.sum == m; } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush; debug readln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto abc = readln.split.to!(int[]); writeln(abc[0] == abc[1] ? abc[2] : abc[0] == abc[2] ? abc[1] : abc[0]); }
D
import std.stdio, std.array, std.conv, std.algorithm, std.string, std.numeric, std.range; void main() { auto a = readln.strip.to!int; auto b = readln.strip.to!int; auto c = readln.strip.to!int; auto d = readln.strip.to!int; auto e = readln.strip.to!int; min(a*e, max(b,b+(e-c)*d)).writeln; }
D
import std.algorithm; import std.container; import std.exception; import std.format; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; immutable int MED_L = 1 << 17; immutable int MAX_L = MED_L << 1; void main () { int n, m; while (scanf (" %d %d", &n, &m) > 0) { auto a = new int [n]; foreach (i; 0..n) { scanf (" %d", &a[i]); } auto b = new int [n]; foreach (i; 0..n) { scanf (" %d", &b[i]); } auto x = new int [m]; auto y = new int [m]; auto z = new int [m]; auto t = new int [MAX_L]; t[] = -1; void mark (int q, int lo, int hi) { lo += MED_L; hi += MED_L; while (lo <= hi) { if (lo & 1) { debug {writefln ("t[%s] = %s", lo, q);} t[lo] = q; lo++; } if (!(hi & 1)) { debug {writefln ("t[%s] = %s", hi, q);} t[hi] = q; hi--; } lo >>= 1; hi >>= 1; } } int value (int p) { int res = b[p]; int q = -1; for (int s = p + MED_L; s > 0; s >>= 1) { q = max (q, t[s]); } if (q != -1) { debug {writefln ("q = %s, p = %s", q, p);} debug {writefln ("index = %s + %s - %s = %s", x[q], p, y[q], x[q] + p - y[q]);} res = a[x[q] + p - y[q]]; } return res; } foreach (j; 0..m) { int k; scanf (" %d", &k); if (k == 1) { scanf (" %d %d %d", &x[j], &y[j], &z[j]); x[j]--; y[j]--; mark (j, y[j], y[j] + z[j] - 1); } else if (k == 2) { int p; scanf (" %d", &p); debug {writefln ("p = %d", p);} p--; int v = value (p); printf ("%d\n", v); } else { assert (false); } } } }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n, q; rd(n, q); auto tree=new SquareRootDecomposition(n); while(q--){ auto args=readln.split.to!(int[]); if(args[0]==0){ tree.add(args[1], args[2]+1, args[3]); }else{ writeln(tree.rmin(args[1], args[2]+1)); } } } class SquareRootDecomposition{ // starry import std.algorithm; int D=1; int[] val, buc, star; this(int n){ while(D*D<n) D++; val.length=D*D; buc.length=star.length=D; } void add(int ql, int qr, int x){ foreach(k; 0..D){ int l=k*D, r=(k+1)*D; if(r<=ql || qr<=l){ // }else if(ql<=l && r<=qr){ star[k]+=x; }else{ val[max(l, ql)..min(r, qr)]+=x; buc[k]=reduce!(min)(val[l..r]); } } } int rmin(int ql, int qr){ int ret=1_000_000_000; foreach(k; 0..D){ int l=k*D, r=(k+1)*D; if(r<=ql || qr<=l){ // }else if(ql<=l && r<=qr){ chmin(ret, buc[k]+star[k]); }else{ chmin(ret, reduce!(min)(val[max(l, ql)..min(r, qr)])+star[k]); } } return ret; } void chmin(T)(ref T l, T r){ if(l>r) l=r; } } 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.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n, k; readV(n, k); --k; dchar[] s; readV(s); s[k] = s[k].toLower; writeln(s); }
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 = lread(); auto a = aryread(); auto b = aryread(); long kill; foreach (i; iota(0, n)) { long left = min(a[i], b[i]); b[i] -= left; a[i] -= left; long right = min(a[i + 1], b[i]); b[i] -= right; a[i + 1] -= right; kill += left + right; } kill.writeln(); }
D
import std.algorithm; import std.conv; import std.math; 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 s = readln.strip; if (s.all !(q{a != '>'}) || s.all !(q{a != '<'})) { writeln (n); continue; } int res = n; foreach (i; 0..n) { if (s[i] != '-' && s[(i + 1) % n] != '-') { res -= 1; } } writeln (res); } }
D
void main() { dchar[] s = readln.chomp.to!(dchar[]); if (s.length == 3) reverse(s); s.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.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { string s; scan(s); int n = s.length.to!int + 1; s = ">" ~ s ~ "<"; auto a = new int[](n); a[] = inf; int rec(int i) { debug { writeln("i: ", i); } if (a[i] != inf) { return a[i]; } a[i] = 0; if (s[i] == '<') { chmax(a[i], rec(i - 1) + 1); } if (s[i + 1] == '>') { chmax(a[i], rec(i + 1) + 1); } return a[i]; } long ans; foreach (i ; 0 .. n) { ans += rec(i); } debug { writeln(a); } writeln(ans); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { while (true) { int n, m, p; scan(n, m, p); if (!n && !m && !p) return; auto x = iota(n).map!(i => readln.chomp.to!int).array; solve(n, m, p, x).writeln; } } int solve(int n, int m, int p, int[] x) { return x[m-1] == 0 ? 0 : x.sum * (100 - p) / x[m-1]; } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new string[](t); foreach (ti; 0..t) { auto n = RD!int; auto m = RD; auto s = RD!string; long x; long[] cnt; foreach (i; 0..n-1) { ++x; if (s[i] != s[i+1]) { cnt ~= x; x = 0; } } cnt ~= x+1; long p = s[0] == '0' ? 0 : 1; if (cnt.length != 1) { foreach (i; 0..cnt.length) { if (i % 2 != p) continue; if (i == 0) { cnt[i+1] += min(cnt[i], m); cnt[i] = max(cnt[i]-m, 0); } else if (i == cnt.length-1) { cnt[i-1] += min(cnt[i], m); cnt[i] = max(cnt[i]-m, 0); } else { cnt[i-1] += min(cnt[i]/2, m); cnt[i+1] += min(cnt[i]/2, m); cnt[i] = max(cnt[i]-m*2, cnt[i]%2); } } } foreach (i; 0..cnt.length) { foreach (e; 0..cnt[i]) ans[ti] ~= cast(char)('0'+p); p ^= 1; } } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.string; import std.algorithm, std.array, std.container; import std.numeric, std.math; import core.bitop; T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; } string RDR()() { return readln.chomp; } long mod = pow(10, 9) + 7; long moda(long x, long y) { return (x + y) % mod; } long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; } long modm(long x, long y) { return (x * y) % mod; } void main() { auto N = RD!long; long[char] cnt; cnt['M'] = 0; cnt['A'] = 0; cnt['R'] = 0; cnt['C'] = 0; cnt['H'] = 0; foreach (i; 0..N) { auto name = RD; auto c = cnt.get(name[0], -1); if (c != -1) ++cnt[name[0]]; } long ans = 0; auto keys = cnt.keys; foreach (i; 0..5) { foreach (j; i+1..5) { foreach (k; j+1..5) { ans += cnt[keys[i]] * cnt[keys[j]] * cnt[keys[k]]; } } } writeln(ans); stdout.flush(); }
D
import std.stdio; import std.string; import std.conv; import std.array; import std.algorithm; import std.range; void main(){ auto A=readln.chomp.to!(char[]); writeln(A.replace(","," ")); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; void main() { auto N = readln.chomp.to!int; auto C = readln.split.map!(to!long).array; auto A = readln.split.map!(x => x.to!int-1).array; auto used = new int[](N); int cnt = 0; long ans = 0; foreach (i; 0..N) { if (used[i]) continue; ++cnt; int[] nodes; int n = i; while (!used[n]) { nodes ~= n; used[n] = cnt; n = A[n]; } if (used[n] != cnt) continue; bool flag = false; long tmp = 1L << 59; foreach (j; nodes) { if (j == n) flag = true; if (flag) tmp = min(tmp, C[j]); } ans += tmp; } ans.writeln; }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; void main() { auto ip = readln.split.to!(int[]), A=ip[0],B=ip[1],C=ip[2],D=ip[3]; if(A+B>C+D) { writeln("Left"); } else if(A+B==C+D){ writeln("Balanced"); } else { writeln("Right"); } }
D
import std.stdio; import std.string; import std.algorithm; import std.conv; void main() { auto l = readln.chomp.split.map!(to!int); ((l[0] * l[1]) % 2 ? "Odd" : "Even").writeln; }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ int a = read.to!int; string s = readln.chomp; if(a < 3200) s = "red"; s.writeln; }
D
import std.stdio, std.conv, std.string, std.array, std.algorithm, std.math; void main() { auto ns = readln.chomp; auto ret = ns.count; auto n = ns.to!ulong; for (ulong a = 2; a^^2 <= n; ++a) { if (n%a) continue; auto b = n/a; int i = 1; for (; b > 9; ++i) b /= 10; if (i < ret) ret = i; } writeln(ret); }
D
/+ dub.sdl: name "C" dependency "dcomp" version=">=0.7.3" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; int main() { Scanner sc = new Scanner(stdin); string s; sc.read(s); int n = s.length.to!int; int ans = 0; int l = 0, r = n-1; while (l < r) { char c = s[l]; char d = s[r]; if (c == d) { l++; r--; continue; } if (c == 'x') { ans++; l++; continue; } if (d == 'x') { ans++; r--; continue; } writeln(-1); return 0; } writeln(ans); 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); } } } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; // import dcomp.array; 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 { FastAppender!(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/array.d */ // module dcomp.array; T[N] fixed(T, size_t N)(T[N] a) {return a;} struct FastAppender(A, size_t MIN = 4) { import std.algorithm : max; import std.conv; import std.range.primitives : ElementEncodingType; import core.stdc.string : memcpy; private alias T = ElementEncodingType!A; private T* _data; private uint len, cap; @property size_t length() const {return len;} bool empty() const { return len == 0; } void reserve(size_t nlen) { import core.memory : GC; if (nlen <= cap) return; void* nx = GC.malloc(nlen * T.sizeof); cap = nlen.to!uint; if (len) memcpy(nx, _data, len * T.sizeof); _data = cast(T*)(nx); } void free() { import core.memory : GC; GC.free(_data); } void opOpAssign(string op : "~")(T item) { if (len == cap) { reserve(max(MIN, cap*2)); } _data[len++] = item; } void insertBack(T item) { this ~= item; } void removeBack() { len--; } void clear() { len = 0; } ref inout(T) back() inout { assert(len); return _data[len-1]; } ref inout(T) opIndex(size_t i) inout { return _data[i]; } T[] data() { return (_data) ? _data[0..len] : null; } } /* 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.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; struct SegTree(alias fun, alias E, T) if (is(typeof(E) : T)) { import std.functional : binaryFun; alias OP = binaryFun!fun; /// this(size_t n, T[] ts) { this.n = 1; while (this.n < n) this.n *= 2; this.tree.length = this.n * 2 - 1; foreach (ref e; this.tree) e = E; foreach (i, e; ts) this.update(i, e); } /// void replace(size_t i, T e) { i += this.n - 1; this.tree[i] = e; while (i > 0) { i = (i-1) / 2; this.tree[i] = OP(this.tree[i*2+1], this.tree[i*2+2]); } } /// void update(size_t i, T e) { replace(i, OP(e, tree[i + this.n - 1])); } /// T query(size_t a, size_t b) { T impl(size_t i, size_t l, size_t r) { if (r <= a || b <= l) return E; if (a <= l && r <= b) return this.tree[i]; return OP( impl(i*2+1, l, (l+r)/2), impl(i*2+2, (l+r)/2, r) ); } return impl(0, 0, this.n); } private: size_t n; T[] tree; } /// auto seg_tree(alias f, alias E, T)(size_t n, T[] arr = []) { return SegTree!(f, E, T)(n, arr); } /// auto seg_tree(alias f, alias E, T)(T[] arr) { return SegTree!(f, E, T)(arr.length, arr); } auto max_seg_tree(T)(size_t n, T[] arr = []) { return seg_tree!("a > b ? a : b", 0, T)(n, arr); } auto max_seg_tree(T)(T[] arr) { return max_seg_tree!T(arr.length, arr); } alias P = Tuple!(int, "i", int, "d"); void main() { auto nq = readln.split.to!(int[]); auto N = nq[0]; auto Q = nq[1]; alias SEGT = typeof(max_seg_tree!int(N)); long r = (N.to!long-2)^^2; void put(int p, ref int min_line, int min_line_opposite, ref int[] lines, SEGT segt) { segt.update(p, p+1); auto above = segt.query(0, p); if (above) { lines[p] = lines[above-1]; } else { min_line = p; lines[p] = min_line_opposite - 1; } r -= lines[p]; } auto lms = new int[](N); int min_w = N-1; auto bms = new int[](N); int min_v = N-1; auto wsegt = max_seg_tree!int(N); auto vsegt = max_seg_tree!int(N); foreach (_; 0..Q) { auto q = readln.split.to!(int[]); auto x = q[1]-1; if (q[0] == 1) { put(x, min_v, min_w, lms, wsegt); } else { put(x, min_w, min_v, bms, vsegt); } } writeln(r); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math; int[][52] SS; dchar[52] CS; long N = 1000000007; void main() { auto n = readln.chomp.to!int; auto s1 = readln.chomp.to!(dchar[]); auto s2 = readln.chomp.to!(dchar[]); auto j = 0; for (auto i = 0; i < n; ++i) { if (s1[i] == s2[i]) { if (i == 0) { SS[j] = []; } else if (s1[i-1] == s2[i-1]) { SS[j] = [j-1]; } else { SS[j] = [j-2, j-1]; } } else { if (i == 0) { SS[j++] = []; SS[j] = [j-1]; } else if (s1[i-1] == s2[i-1]) { SS[j] = [j-1]; ++j; SS[j] = [j-2, j-1]; } else { SS[j] = [j-2]; ++j; SS[j] = [j-2, j-1]; } ++i; } ++j; } long[immutable(dchar)[]][52] memo; long solve(int i) { if (i == n) return 1; immutable(dchar)[] key; foreach (j; (i-2 < 0 ? 0 : i-2)..i) key ~= CS[j]; if (key in memo[i]) return memo[i][key]; auto cls = 0b111; foreach (j; SS[i]) { switch (CS[j]) { case 'R': cls &= 0b011; break; case 'G': cls &= 0b101; break; case 'B': cls &= 0b110; break; default: } } long ret; if (cls & 0b100) { CS[i] = 'R'; ret = (ret + solve(i+1)) % N; } if (cls & 0b010) { CS[i] = 'G'; ret = (ret + solve(i+1)) % N; } if (cls & 0b001) { CS[i] = 'B'; ret = (ret + solve(i+1)) % N; } return memo[i][key] = ret; } writeln(solve(0)); }
D
void main() { problem(); } void problem() { const N = scan!int; const M = scan!int; const X = scan!int; auto C = new int[N]; int[][] A; foreach(i; 0..N) { C[i] = scan!int; A ~= scan!int(M); } void solve() { int answer = int.max; foreach(comb; [true, false].permutationsWithRepetitions(N)) { int price; auto manabi = new int[M]; foreach(i; 0..N) { if (comb[i]) continue; price += C[i]; foreach(a; 0..M) manabi[a] += A[i][a]; } if (manabi.any!(m => m < X)) continue; if (answer > price) answer = price; } writeln(answer == int.max ? -1 : answer); } 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.stdio, std.array, std.concurrency; Generator!(T[]) permutationsWithRepetitions(T)(T[] data, in uint n) in { assert(!data.empty && n > 0); } body { return new typeof(return)({ if (n == 1) { foreach (el; data) yield([el]); } else { foreach (el; data) foreach (perm; permutationsWithRepetitions(data, n - 1)) yield(el ~ perm); } }); } // -----------------------------------------------
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 = "%.15f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto x = RD; if (x > 1 && x < 2*N-1) { writeln("Yes"); auto ans = new long[](2*N-1); auto M = 2*N-1; auto mid = M / 2; if (N == 2) { ans = [1, 2, 3]; } else { foreach (i; mid..M) { ans[i] = (x+i-mid-1)%M+1; } foreach (i; 0..mid) { ans[i] = (x+mid+i)%M+1; } } foreach (e; ans) writeln(e); } else writeln("No"); stdout.flush(); debug readln(); }
D
void main() { problem(); } void problem() { auto N = scan!int; auto A = scan!long(N); long solve() { long ans; long current = A[0]; foreach(a; A) { if (a >= current) { current = a; continue; } ans += current - a; } return ans; } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int N; scan(N); auto lb = (100 * N + 107) / 108; auto ub = (100 * (N + 1) + 107) / 108; foreach (x ; lb .. ub) { if (x * 108 / 100 == N) { writeln(x); return; } } writeln(":("); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.string, std.conv, std.algorithm; void main(){ auto a = readln.split.map!(to!int); if(a[0]>a[1]) writeln("a > b"); else if(a[0]<a[1]) writeln("a < b"); else writeln("a == b"); }
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() { while(true) { int n = readln.chomp.to!int; if (n==0) break; (n/4).iota.map!(_ => readln.chomp.to!int).reduce!"a+b".writeln; } }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto E = M.iota.map!(_ => readln.split.map!(x => x.to!int-1).array).array; s = readln.split.map!(to!int); auto S = s[0]-1; auto T = s[1]-1; auto FF = new FordFulkerson(N, S, T); foreach (e; E) FF.add_edge(e[0], e[1], 1), FF.add_edge(e[1], e[0], 1); FF.run.writeln; int[] ans; foreach (i; 0..M) { int u = E[i][0]; int v = E[i][1]; if (FF.flow[u][v]) ans ~= i + 1; } ans.length.writeln; ans.each!writeln; } class FordFulkerson { int N, source, sink; int[][] adj; int[][] flow; bool[] used; this(int n, int s, int t) { N = n; source = s; sink = t; assert (s >= 0 && s < N && t >= 0 && t < N); adj = new int[][](N); flow = new int[][](N, N); used = new bool[](N); } void add_edge(int from, int to, int cap) { adj[from] ~= to; adj[to] ~= from; flow[from][to] = cap; } int dfs(int v, int min_cap) { if (v == sink) return min_cap; if (used[v]) return 0; used[v] = true; foreach (to; adj[v]) { if (!used[to] && flow[v][to] > 0) { auto bottleneck = dfs(to, min(min_cap, flow[v][to])); if (bottleneck == 0) continue; flow[v][to] -= bottleneck; flow[to][v] += bottleneck; return bottleneck; } } return 0; } int run() { int ret = 0; while (true) { foreach (i; 0..N) used[i] = false; int f = dfs(source, int.max); if (f > 0) ret += f; else return ret; } } }
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 Pair = Tuple!(long, "flag", long, "num"); 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; } } auto dp = new long[100_100]; void main() { long n, k; scan(n, k); auto h = aryread(); dp[] = INF; dp[0] = 0, dp[1] = abs(h[1] - h[0]); foreach (i; iota(2, n)) { foreach (j; iota(1, min(i, k) + 1)) { dp[i] = min(dp[i - j] + abs(h[i] - h[i - j]), dp[i]); } } dp[n - 1].writeln(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; long P = 10^^9+7; long[10^^6*2+10] F, RF; long pow(long x, long n) { long y = 1; while (n) { if (n%2 == 1) y = (y * x) % P; x = x^^2 % P; n /= 2; } return y; } long inv(long x) { return pow(x, P-2); } void init() { F[0] = F[1] = 1; foreach (i, ref x; F[2..$]) x = (F[i+1] * (i+2)) % P; { RF[$-1] = 1; auto x = F[$-1]; auto k = P-2; while (k) { if (k%2 == 1) RF[$-1] = (RF[$-1] * x) % P; x = x^^2 % P; k /= 2; } } foreach_reverse(i, ref x; RF[0..$-1]) x = (RF[i+1] * (i+1)) % P; } void main() { init(); auto rc = readln.split.to!(long[]); auto r1 = rc[0]; auto c1 = rc[1]; auto r2 = rc[2]; auto c2 = rc[3]; long g(long r, long c) { long x; foreach (i; 1..r+2) { x = (x + F[i+c] * RF[i] % P * RF[c] % P) % P; } return x; } writeln( (((g(r2, c2) - g(r1-1, c2) + P) % P - g(r2, c1-1) + P) % P + g(r1-1, c1-1)) % P ); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { long n = lread(); auto s = new string[n]; foreach (ref e; s) { e = sread(); } long count; auto first_b = new long[n]; auto last_a = new long[n]; foreach (i, e; s) { foreach (j; 0 .. e.length - 1) { if (e[j .. j + 2] == "AB") { count++; } } if (e[0] == 'B') { first_b[i]++; } if (e[$ - 1] == 'A') { last_a[i]++; } } long allequal; foreach (i; 0 .. n) { if (first_b[i] == last_a[i]) { allequal++; } } auto mintwo = min(first_b.sum, last_a.sum); if (allequal == n && mintwo != 0) { count--; } writeln(count + mintwo); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdlib; immutable int INF = 1 << 29; void main() { auto X = readln.chomp.to!long; long[] F; for (long i = 1; i * i <= X; ++i) { if (X % i == 0) { F ~= i; if (i * i != X) { F ~= X / i; } } } foreach (f; F) for (long a = f, b = 0; a^^5-b^^5 <= X; ++a, ++b) { if (a^^5 - b^^5 == X) { writeln(a, " ", b); return; } } foreach (f; F) for (long a = f-1, b = -1; a >= 0; --a, --b) { if (a^^5 - b^^5 == X) { writeln(a, " ", b); return; } } }
D
// Cheese-Cracker: cheese-cracker.github.io void solve(){ long n = scan; dchar[][] arr; for(int i = 0; i < n-2; ++i){ arr ~= scan!(dchar[]); } show(arr); dchar[] w; w ~= arr[0]; bool f = 0; for(int i = 1; i < n - 2; ++i){ if(w.back == arr[i][0]){ w ~= arr[i][1]; }else{ f = 1; w ~= arr[i]; } } if(!f){ w ~= 'a'; } writeln(w); } void main(){ long tests = scan; // Toggle! while(tests--) solve; } /************** ***** That's All Folks! ***** **************/ import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math, std.numeric; string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;} T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; } void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } } alias ll = long, tup = Tuple!(long, "x", long, "y");
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = readln.chomp.to!size_t; auto co = 0, c2 = 0, c4 = 0; auto rd = readln.splitter; foreach (_; 0..n) { auto a = rd.front.to!int; rd.popFront(); if (a % 2 != 0) ++co; else if (a % 4 != 0) ++c2; else ++c4; } writeln(c2 > 0 && co <= c4 || c2 == 0 && co <= c4+1 ? "Yes" : "No"); }
D
import std.stdio; import std.string; import std.format; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.concurrency; import std.traits; import std.uni; import std.regex; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; enum long INF = long.max/5; void main() { char[] ts; ts = readln.chomp.to!(char[]); long N = ts.length; foreach(i; 0..N) { if (ts[i] == '?') ts[i] = 'D'; } ts.writeln; } // ---------------------------------------------- void times(alias fun)(long n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(long n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { // `(x+y-1)/y` will only work for positive numbers ... T t = x / y; if (y > 0 && t * y < x) t++; if (y < 0 && t * y > x) t++; return t; } T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { T t = x / y; if (y > 0 && t * y > x) t--; if (y < 0 && t * y < x) t--; return t; } ref T ch(alias fun, T, S...)(ref T lhs, S rhs) { return lhs = fun(lhs, rhs); } unittest { long x = 1000; x.ch!min(2000); assert(x == 1000); x.ch!min(3, 2, 1); assert(x == 1); x.ch!max(100).ch!min(1000); // clamp assert(x == 100); x.ch!max(0).ch!min(10); // clamp assert(x == 10); } mixin template Constructor() { import std.traits : FieldNameTuple; this(Args...)(Args args) { // static foreach(i, v; args) { foreach(i, v; args) { mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v; } } } template scanln(Args...) { enum sep = " "; enum n = (){ long n = 0; foreach(Arg; Args) { static if (is(Arg == class) || is(Arg == struct) || is(Arg == union)) { n += Fields!Arg.length; } else { n++; } } return n; }(); enum fmt = n.rep!(()=>"%s").join(sep); enum argsString = (){ string[] xs = []; foreach(i, Arg; Args) { static if (is(Arg == class) || is(Arg == struct) || is(Arg == union)) { foreach(T; FieldNameTuple!Arg) { xs ~= "&args[%d].%s".format(i, T); } } else { xs ~= "&args[%d]".format(i); } } return xs.join(", "); }(); void scanln(auto ref Args args) { string line = readln.chomp; static if (__VERSION__ >= 2074) { mixin( "line.formattedRead!fmt(%s);".format(argsString) ); } else { mixin( "line.formattedRead(fmt, %s);".format(argsString) ); } } } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // popcnt with ulongs was added in D 2.071.0 static if (__VERSION__ < 2071) { ulong popcnt(ulong x) { x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L); x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L); x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL); x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL); x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL); x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL); return x; } }
D
import 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 a = readints; int ans = a.map!(e => e - 1).sum; writeln(ans); }
D
import std.stdio; import std.string; import std.conv; import std.array; import std.algorithm; void main() { auto input = readln.split.map!(to!int); int K = input[0], S = input[1]; int ans = 0; foreach (x; 0 .. K + 1) { foreach (y; 0 .. K + 1) { int z = S - (x + y); if (z >= 0 && K >= z) { ans++; } } } writeln(ans); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { long a, b; scan(a, b); long d = b - a; long ha = (d - 1) * d / 2; writeln(ha - a); }
D
import std.stdio, std.conv, std.string, std.math, std.algorithm; void main(){ auto ip = readln.chomp; if(ip[0] == 'H'){ if(ip[2] == 'H') 'H'.writeln; else if(ip[2] == 'D') 'D'.writeln; } else if(ip[0] == 'D') { if(ip[2] == 'H') 'D'.writeln; else if(ip[2] == 'D') 'H'.writeln; } }
D
// import chie template :) {{{ static if (__VERSION__ < 2090) { import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons, std.format, std.bitmanip, std.numeric; } else { import std; } // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan(T...)(ref T args) { foreach (ref arg; args) { arg = next!(typeof(arg)); } } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } // }}} // alias {{{ alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less); alias MinHeap(T) = Heap!(T, "a > b"); // }}} // memo {{{ /* - ある値が見つかるかどうか <https://dlang.org/phobos/std_algorithm_searching.html#canFind> canFind(r, value); -> bool - 条件に一致するやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#filter> // 2で割り切れるやつ filter!"a % 2 == 0"(r); -> Range - 合計 <https://dlang.org/phobos/std_algorithm_iteration.html#sum> sum(r); - 累積和 <https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold> // 今の要素に前の要素を足すタイプの一般的な累積和 // 累積和のrangeが帰ってくる(破壊的変更は行われない) cumulativeFold!"a + b"(r, 0); -> Range - rangeをarrayにしたいとき array(r); -> Array - 各要素に同じ処理をする <https://dlang.org/phobos/std_algorithm_iteration.html#map> // 各要素を2乗 map!"a * a"(r) -> Range - ユニークなやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#uniq> uniq(r) -> Range - 順列を列挙する <https://dlang.org/phobos/std_algorithm_iteration.html#permutations> permutation(r) -> Range - ある値で埋める <https://dlang.org/phobos/std_algorithm_mutation.html#fill> fill(r, val); -> void - バイナリヒープ <https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap> // 昇順にするならこう(デフォは降順) BinaryHeap!(Array!T, "a > b") heap; heap.insert(val); heap.front; heap.removeFront(); - 浮動小数点の少数部の桁数設定 // 12桁 writefln("%.12f", val); - 浮動小数点の誤差を考慮した比較 <https://dlang.org/phobos/std_math.html#.approxEqual> approxEqual(1.0, 1.0099); -> true - 小数点切り上げ <https://dlang.org/phobos/std_math.html#.ceil> ceil(123.4); -> 124 - 小数点切り捨て <https://dlang.org/phobos/std_math.html#.floor> floor(123.4) -> 123 - 小数点四捨五入 <https://dlang.org/phobos/std_math.html#.round> round(4.5) -> 5 round(5.4) -> 5 */ // }}} void main() { auto cin = new Scanner; int n; cin.scan(n); long[] a = cin.nextArray!long(n); long[long] f, g; foreach (i; 0 .. n) { f[a[i] + i]++; } foreach (i; 0 .. n) { g[i - a[i]]++; } long cnt; foreach (e; f.byKeyValue) { cnt += e.value * g.get(e.key, 0); } writeln(cnt); }
D
import std.stdio; import std.array; import std.string; import std.conv; import std.algorithm; import std.typecons; import std.range; import std.random; import std.math; import std.container; import std.numeric; import std.bigint; void main() { auto input = readln.split.map!(to!int); auto N = input[0]; auto M = input[1]; auto X = readln.split.map!(to!int).array; auto cnt = new int[][](M); foreach (x; X) cnt[x%M] ~= x; auto amari = new int[](10^^5+1); int ans = 0; foreach (m; 0..M/2+1) { int minm, mm; if (m == 0 || (m == M/2 && M % 2 == 0)) { ans += cnt[m].length/2; } else { minm = min(cnt[m].length, cnt[M-m].length).to!int; mm = cnt[m].length > cnt[M-m].length ? m : M-m; int[int] same; foreach (x; cnt[mm]) { if (x in same) same[x] += 1; else same[x] = 1; } ans += minm + min((cnt[mm].length-minm)/2, same.values.map!(x => x/2).sum); } } writeln(ans); }
D
void main() { long a, b, n; rdVals(a, b, n); long calc(long x) { return (a * x) / b - a * (x / b); } long t = b - 1; if (t <= n) calc(t).writeln; else calc(n).writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; enum double eps = 1.0e-9; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
import std.stdio, std.string, std.conv, std.algorithm; void main() { int N = readln.chomp.to!(int); int[] C, S, F; for (int i = 0; i < N - 1; i++) { int[] buf = readln.chomp.split.to!(int[]); C ~= buf[0]; S ~= buf[1]; F ~= buf[2]; } for (int i = 0; i < N - 1; i++) { int x = S[i] + C[i]; for (int j = i + 1; j < N - 1; j++) { if (x > S[j]) { int t = (x - S[j]) % F[j]; if (t) { x += F[j] - t; } } else { x += S[j] - x; } x += C[j]; } writeln(x); } writeln(0); }
D
import std.algorithm, std.array, std.container, std.range, std.bitmanip; import std.numeric, std.math, std.bigint, std.random, core.bitop; import std.string, std.conv, std.stdio, std.typecons; void main() { while (true) { auto rd = readln.split; auto a = rd[0].to!int, op = rd[1], b = rd[2].to!int; if (op == "?") break; switch (op) { case "+": writeln(a + b); break; case "-": writeln(a - b); break; case "*": writeln(a * b); break; case "/": writeln(a / b); break; default: assert(0); } } }
D
import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; void main(){ string s = readln.chomp; int f = 0; foreach(c; s){ if(f == 0 && c == 'C') f = 1; if(f == 1 && c == 'F') f = 2; } string ans; if(f == 2) ans = "Yes"; else ans = "No"; ans.writeln; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { auto nm = readints; int n = nm[0], m = nm[1]; auto balls = new int[n + 1]; // 各箱に入っているボールの数 balls[] = 1; auto reds = new bool[n + 1]; // 赤いボールが入っている可能性 reds[1] = true; for (int i = 0; i < m; i++) { auto xy = readints; int x = xy[0], y = xy[1]; // x の箱のボールを y に移す balls[x]--; balls[y]++; if (reds[x]) { reds[y] = true; if (balls[x] == 0) reds[x] = false; } } auto ans = reds.count(true); writeln(ans); }
D
import std.stdio; import std.array; import std.conv; import std.string; void main() { char[4] suits = ['S', 'H', 'C', 'D']; bool[13][4] cards = false; auto n = to!(int)(chomp(readln())); foreach (i; 0..n) { auto input = split(readln()); auto idx = to!(int)(input[1]); switch (input[0]) { case "S": cards[0][idx - 1] = true; break; case "H": cards[1][idx - 1] = true; break; case "C": cards[2][idx - 1] = true; break; case "D": cards[3][idx - 1] = true; break; default: writeln("ERROR"); break; } } foreach (i; 0..4) { foreach (j; 0..13) { if (!cards[i][j]) writeln(suits[i], ' ', j+1); } } }
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; int [] levels; levels ~= 1; int cur = 0; int prev = 1; foreach (i; 2..n) { if (a[i] < a[i - 1]) { cur -= 1; if (cur < 0) { levels ~= i - prev; prev = i; cur = levels.back - 1; } } } writeln (levels.length); } }
D
import std.stdio; import std.range; import std.array; import std.algorithm; import std.string; import std.conv; void main(){ readln; string[] inputs = readln.chomp.split; writeln(solve(inputs)); } string solve(in string[] inputs){ foreach(color; inputs) { if(color == "Y"){ return "Four"; } } return "Three"; }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto x = readln.chomp.to!long; long money = 100; long cnt; while (x > money) { money = (money * 1.01).to!long; cnt++; } cnt.writeln; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}} void main() { int a, b; readV(a, b); auto c = 0; foreach (i; a..b+1) { auto s = i.to!string, t = s.dup; t.reverse(); if (s == t) ++c; } writeln(c); }
D
import std.stdio; import std.string; import std.conv; import std.numeric; import std.functional; void main(){ foreach(line; stdin.byLine) line.chomp.split.unaryFun!(a => gcd(a[0].to!int, a[1].to!int)).writeln; }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { writeln("ABC"~readln.chomp); }
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; T lcm(T)(T a, T b) { return a * (b / gcd(a, b)); } // 区間 [a, b] で c でも d でも割り切れない数の個数を求める long calc(long a, long b, long c, long d) { // 区間 [1, n] で x で割り切れる数の個数 long f(long n, long x) { return n / x; } // 区間 [1, n] で x or y で割り切れる数の個数 long g(long n, long x, long y) { return f(n, x) + f(n, y) - f(n, lcm(x, y)); } // 区間 [1, n] で x or y で割り切れない数の個数 long h(long n, long x, long y) { return n - g(n, x, y); } return h(b, c, d) - h(a - 1, c, d); } long calc2(long a, long b, long c, long d) { long f(long n) { return n - n/c - n/d + n/lcm(c, d); } return f(b) - f(a - 1); } void main() { long a, b, c, d; scan(a, b, c, d); writeln(calc2(a, b, c, d)); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); void main() { long n, m; scan(n, m); // writeln(n, m); auto 正解したか = new bool[](n + 1); auto WAの回数 = new long[](n + 1); foreach (_; 0 .. m) { long p; string s; scan(p, s); // writeln(p, s); if ((正解したか[p] == false) && (s == "WA")) { WAの回数[p] += 1; } else if (s == "AC") { 正解したか[p] = true; } } // writeln(正解したか); // writeln(WAの回数); long 正解数, ペナルティ; foreach (i; 0 .. 正解したか.length) { 正解数 += 正解したか[i]; } // writeln(正解数); foreach (i; 0 .. WAの回数.length) { if (正解したか[i] == true) { ペナルティ += WAの回数[i]; } } writeln(正解数, ' ', ペナルティ); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
D
void main() { problem(); } void problem() { auto N = scan!ulong; ulong solve() { auto primes = primeFactoring(N); int[ulong] primeCounts; foreach(p; primes) { primeCounts[p]++; } ulong answer; foreach(p; primeCounts.keys) { const count = primeCounts[p]; int sumCount; foreach(i; 1..count+1) { sumCount += i; if (sumCount > count) break; answer++; } } return answer; } 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"); ulong[] primeFactoring(ulong target) { ulong s = target.to!float.sqrt().floor.to!ulong; ulong num = target; ulong[] primes; for (ulong i = 2; i <= s; i++) { if (num % i != 0) continue; while (num%i == 0) { num /= i; primes ~= i; } } if (num > s) primes ~= num; return primes; } // -----------------------------------------------
D
import std.stdio, std.string, std.algorithm, std.range, std.conv, std.typecons, std.math; void main(){ auto x = readln.chomp.to!int; writeln(x < 1200 ? "ABC" : "ARC"); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { auto A = [ 1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51 ]; long K = lread(); writeln(A[K - 1]); }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop; // dfmt on void main() { long N = lread(); auto A = aryread(); long[long] cnt; foreach (a; A) { cnt[a] = cnt.get(a, 0) + 1; } long s; foreach (key, value; cnt) { s += value * (value - 1) / 2; } // writeln(cnt); foreach (a; A) { writeln(s - (cnt[a] * (cnt[a] - 1) / 2) + ((cnt[a] - 1) * (cnt[a] - 2) / 2)); } }
D