code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math; void main() { auto ip = readln.split.to!(int[]); auto A = ip[0]; auto B = ip[1]; auto C = ip[2]; auto X = ip[3]; auto Y = ip[4]; writeln(min( X * A + Y * B, X * 2 * C + max(Y - X, 0) * B, max(X - Y, 0) * A + Y * C * 2, max(X, Y) * C * 2 )); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void main() { int n; readV(n); writeln(n%calc(n) == 0 ? "Yes" : "No"); } auto calc(int n) { int r = 0; for (; n > 0; n /= 10) r += n%10; return r; }
D
import std.stdio, std.string, std.conv; void main(){ auto AB = ri!size_t(); solve( AB[0] , AB[1] ).writeln(); return; } auto solve( size_t A , size_t B ){ if ( (B-1)%(A-1)==0 ){ return (B-1)/(A-1); } else { return (B-1)/(A-1)+1; } } T[] ri( T = size_t )(){ T[] ol; foreach( string elm ; readln().chomp().split() ){ ol ~= elm.to!T(); } return ol; }
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; 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; } enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { int N, M, Q; scan(N, M, Q); auto lr = new int[][](N, N); foreach (i ; 0 .. M) { int li, ri; scan(li, ri); li--, ri--; lr[li][ri]++; } auto cu = new int[][](N + 1, N + 1); foreach (i ; 1 .. N + 1) { foreach (j ; 1 .. N + 1) { cu[i][j] += cu[i-1][j] + cu[i][j-1] - cu[i-1][j-1] + lr[i-1][j-1]; } } foreach (_ ; 0 .. Q) { int pi, qi; scan(pi, qi); auto ans = cu[N][qi] - cu[pi - 1][qi]; writeln(ans); } }
D
module app; import core.bitop; import std.algorithm; import std.array; import std.bigint; import std.container.rbtree; import std.conv; import std.math; import std.stdio; import std.string; import std.traits; struct Input { int a, b; } void parseInput(T)(out Input input, T file) { with (file) with (input) { auto ar = readln().strip().split().map!(to!int).array(); a = ar[0]; b = ar[1]; } } auto main2(Input* input) { with (input) { real p1 = a * 12.5; real p2 = b * 10; auto c = cast(int)round(max(p1, p2)); int a2 = cast(int)(c * 0.08); int b2 = cast(int)(c * 0.1); if (a2 == a && b2 == b) return c; else return -1; } } alias retType = ReturnType!main2; static if (!is(retType == void)) { unittest { writeln("begin unittest"); } auto _placeholder_ = ReturnType!main2.init; unittest // example1 { string example = `2 2`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == 25); } unittest // example2 { string example = `8 10`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == 100); } unittest // example3 { string example = `19 99`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == -1); } unittest { writeln("end unittest"); } void parseExample(out Input input, string example) { struct Adapter { string[] _lines; this(string input) { _lines = input.splitLines(); } string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; } } parseInput(input, Adapter(example)); } } void printResult(T)(T result) { static if (isFloatingPoint!T) writefln("%f", result); else writeln(result); } void main() { Input input = void; parseInput(input, stdin); static if (is(retType == void)) main2(&input); else { auto result = main2(&input); printResult(result); } }
D
void main() { dchar[] c1 = readln.chomp.to!(dchar[]); dchar[] c2 = readln.chomp.to!(dchar[]); reverse(c2); writeln(c1 == c2 ? "YES" : "NO"); } 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; 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(){ while(true){ int n = readln().chomp().to!int; if(n==0) break; int a,b; for(int i=0;i<n;i++){ auto s = readln().split().to!(int[]); if(s[0] > s[1]) a += s[0] + s[1]; else if(s[0] < s[1]) b += s[0] + s[1]; else { a += s[0]; b += s[1]; } } writeln(a," ",b); } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void calc(string s, string t) { ulong start = 0; ptrdiff_t p; while ((p = s.indexOf(t, start)) != -1) { writeln(p); start = p + 1; } } void main() { auto s = readln.chomp; auto t = readln.chomp; calc(s, t); }
D
void main() { long n = rdElem; long[] factor = new long[n+1]; foreach (i; 2 .. n+1) { long num = i; for (long j = 2; j * j <= n; ++j) { while (num % j == 0) { num /= j; ++factor[j]; } } if (num > 1) ++factor[num]; } long[] list = [2, 4, 14, 24, 74]; long[long] cnt; foreach (x; list) cnt[x] = 0; foreach (x; factor) { foreach (y; list) { if (x >= y) ++cnt[y]; } } long result; result += cnt[74]; result += cnt[24] * (cnt[2] - 1); result += cnt[14] * (cnt[4] - 1); result += ((cnt[4] * (cnt[4] - 1)) >> 1) * (cnt[2] - 2); result.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
void main() { import std.stdio, std.string, std.conv, std.algorithm; int a, b, c; rd(a, b, c); writeln(max(a * 10 + b + c, b * 10 + c + a, c * 10 + a + b)); } 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.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; auto a = RDR.split.to!(long[]); write(a[n-1]); for (long i = n-3; i >= 0; i -= 2) { write(" ", a[i]); } for (long i = n % 2; i < n; i += 2) { write(" ", a[i]); } writeln(); stdout.flush(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto M = readln.chomp.to!int; long r, n = -1; foreach (_; 0..M) { auto dc = readln.split.to!(long[]); auto d = dc[0]; auto c = dc[1]; if (n == -1) { --c; n = d; } while (c) { if (c % 2 == 1) { ++r; n += d; if (n >= 10) { ++r; n = 1 + n%10; } } c /= 2; switch (d) { case 0: r += c; break; case 1: r += c; d = 2; break; case 2: r += c; d = 4; break; case 3: r += c; d = 6; break; case 4: r += c; d = 8; break; case 5: r += c*2; d = 1; break; case 6: r += c*2; d = 3; break; case 7: r += c*2; d = 5; break; case 8: r += c*2; d = 7; break; case 9: r += c*2; d = 9; break; default: } } } writeln(r); }
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 D = RD; auto N = RD; writeln(N == 100 ? (N+1) * 100^^D : N * 100^^D); stdout.flush(); debug readln(); }
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() { string s = read!string; string t = s.translate(['A': 'T', 'T': 'A', 'C': 'G', 'G': 'C']); writeln(t); }
D
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array, std.functional; void main() { auto N = readln.split[0].to!ulong; ((N*(N-1))/2).writeln; }
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; bool check(long a, long b, long c) { return (c - b - a) >= 0 && 4*a*b < (c - b - a)^^2; } void main() { long a, b, c; scan(a, b, c); yes(check(a, b, c)); } 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.algorithm, std.conv, std.range, std.stdio, std.string; import std.numeric; void main() { auto rd = readln.split.map!(to!int), x = rd[0], y = rd[1]; writeln(gcd(x, y)); }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; long calc(long n) { long ans = n - 1; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { long x = n / i; ans = min(ans, (i - 1) + (x - 1)); } } return ans; } void main() { long n = read!long; writeln(calc(n)); }
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 solve() { auto N = readln.chomp.to!int; auto S = readln.chomp; if (N == 1) { writeln("NO"); return; } if (N == 2) { if (S[0] >= S[1]) { writeln("NO"); return; } } writeln("YES"); writeln(2); writeln(S[0], " ", S[1..$]); } void main() { auto T = readln.chomp.to!int; while (T--) solve; }
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 X = readln.chomp.to!int; writeln(8 - (X-400)/200); }
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, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } /* グリッド 経路数 剰余 x[i][j] = x[i - 1][j] + x[i][j - 1] % mod */ const long mod = 1_000_000_007; void main(){ int h = read.to!int; int w = read.to!int; char[][] cells; foreach(i; 0 .. h) cells ~= readln.chomp.to!(char[]); long[] xs; foreach(i; 0 .. h){ foreach(j; 0 .. w){ if(i == 0){ if(j == 0) xs ~= 1; else xs ~= 0; } if(cells[i][j] == '#'){ xs[j] = 0; } else if(j > 0){ xs[j] += xs[j - 1]; xs[j] %= mod; } } } xs[w - 1].writeln; }
D
import std.functional, std.algorithm, std.typecons, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv, std.math; N gcd(N)(N a, N b) if (isNumeric!N || is (N == Biglong)) { return a ? gcd(b % a, a) : b; } T read_num(T)() { return readln.chomp.to!T; } T[] read_nums(T)() { return readln.chomp.split.to!(T[]); } bool chmax(T)(ref T a, T b) { if (a < b) { a = b; return true; } else { return false; } } void main() { long N = read_num!long; long[] A = read_nums!long; long ans = -1; long[] L, R; L.length = N; R.length = N; L[0] = A[0]; for (size_t i = 1; i < N; i++) { L[i] = gcd(L[i - 1], A[i]); } R[N - 1] = A[N - 1]; for (size_t i = N - 2; i > 0; i--) { R[i] = gcd(R[i + 1], A[i]); } foreach (i; 0..N) { long l = i > 0 ? L[i - 1] : 0; long r = i < N - 1 ? R[i + 1] : 0; chmax(ans, gcd(l, r)); } writeln(ans); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = readln.chomp.to!size_t; auto r = 100_000; foreach (_; n.iota) r = next(r); writeln(r); } int next(int r) { r = r + r / 20; if (r % 1000 > 0) return (r / 1000 + 1) * 1000; else return r / 1000 * 1000; }
D
import std.stdio, std.string, std.conv; import std.typecons; import std.algorithm; import std.math; void main(){ int n = readln.chomp.to!int; auto ls = readln.chomp.split.map!(to!long); long s = 0; foreach(l; ls){ s += l; } long m = 0; foreach(l; ls){ m = max(m, l); } long res = abs(2*m-s)+1; writeln(res); }
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 S = RD!string; long[3] cnt; foreach (c; S) { ++cnt[c-'a']; } writeln(cnt[0] == 1 && cnt[1] == 1 && cnt[2] == 1 ? "Yes" : "No"); stdout.flush(); debug readln(); }
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() { long N, K; scan(N, K); long R, S, P; scan(R, S, P); auto T = sread(); auto dp = new long[][](3, N); long pts(long x, long y) { if (x == 0) return T[y] == 's' ? R : 0; if (x == 1) return T[y] == 'p' ? S : 0; if (x == 2) return T[y] == 'r' ? P : 0; return long.min; } foreach (i; 0 .. N) { if (i < K) { dp[0][i] = pts(0, i); dp[1][i] = pts(1, i); dp[2][i] = pts(2, i); } else { dp[0][i] = max(dp[1][i - K], dp[2][i - K]) + pts(0, i); dp[1][i] = max(dp[0][i - K], dp[2][i - K]) + pts(1, i); dp[2][i] = max(dp[0][i - K], dp[1][i - K]) + pts(2, i); } } long ans; foreach (i; N - K .. N) { ans += max(dp[0][i], dp[1][i], dp[2][i]); } writeln(ans); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto a = RD; auto b = RD; auto ans = sgn(a) * sgn(b); if (ans == 1 && sgn(a) == -1 && (abs(a - b) + 1) % 2 == 0) a = 1; writeln((ans == 1 && sgn(a) == 1) ? "Positive" : ans == 1 && sgn(a) == -1 ? "Negative" : "Zero"); stdout.flush(); debug readln(); }
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 Q = readln.chomp.to!int; while (Q--) { int N = readln.chomp.to!int; int L = 2; int U = 7; int i = 1; while (U < N) { L += 2; U += 7; i += 1; } writeln(i); } }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; 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 main() { auto a = lread(); writeln(a/2 * (a/2 + a%2)); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nk = readln.split.to!(int[]); readln; auto N = nk[0]; auto K = nk[1]; int c; while (N > 1) { ++c; N -= K-1; } writeln(c); }
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^^5*2+50] 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; } long comb(N)(N n, N k) { if (k > n) return 0; auto n_b = F[n]; // n! auto nk_b = RF[n-k]; // 1 / (n-k)! auto k_b = RF[k]; // 1 / k! auto nk_b_k_b = (nk_b * k_b) % P; // 1 / (n-k)!k! return (n_b * nk_b_k_b) % P; // n! / (n-k)!k! } alias E = Tuple!(int, "i", long, "k", long, "p"); void main() { init(); auto N = readln.chomp.to!int; int[][] T; T.length = N; foreach (_; 0..N-1) { auto ab = readln.split.to!(int[]); auto a = ab[0]-1; auto b = ab[1]-1; T[a] ~= b; T[b] ~= a; } E[][] es; es.length = N; E run1(int i, int p) { long pp = 1, k; foreach (j; T[i]) if (j != p) { auto e = run1(j, i); es[i] ~= e; pp = pp * inv(F[e.k]) % P * e.p % P; k += e.k; } return E(i, k + 1, pp * F[k] % P); } run1(0, -1); void run2(int i, int p, E e) { long pp = 1; if (p != -1) { pp = pp * inv(F[e.k]) % P * e.p % P; } foreach (ee; es[i]) { pp = pp * inv(F[ee.k]) % P * ee.p % P; } foreach (ee; es[i]) { run2(ee.i, i, E(i, N - ee.k, pp * F[N-ee.k-1] % P * F[ee.k] % P * inv(ee.p) % P)); } if (p != -1) es[i] ~= e; } run2(0, -1, E(0, 0, 1)); foreach (i; 0..N) { long r = F[N-1]; foreach (e; es[i]) if (e.i != i) { r = r * inv(F[e.k]) % P * e.p % P; } writeln(r); } }
D
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math; import core.stdc.stdio; // foreach, foreach_reverse, writeln void main() { int n; scanf("%d", &n); int[] a = new int[n+2]; foreach (i; 0..n) { scanf("%d", &a[i+1]); } int ans = 0; foreach (i; 0..n+1) { ans += abs(a[i+1]-a[i]); } foreach (i; 1..n+1) { int diff = 0; diff += abs(a[i]-a[i-1]); diff += abs(a[i+1]-a[i]); diff -= abs(a[i+1]-a[i-1]); writeln(ans-diff); } }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { long N; scan(N); long ans = 1; foreach (i; 0 .. N) { ans *= i + 1; ans %= MOD; } writeln(ans); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, 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() { long Q, H, S, D; scan(Q, H, S, D); long N = lread(); long A = Q * 4; long B = H * 2; long ans; if (min(A * 2, B * 2, S * 2, D) == D) { ans = D * (N / 2); ans += min(A, B, S) * (N % 2); } else { ans = min(A, B, S) * N; } writeln(ans); }
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 n = RD!int; auto a = RDA; long pos = MIN_POS!"a > b"(a); int l = cast(int)pos-1, r = cast(int)pos+1; long top = n; bool ans = true; while (top != 1) { if (l >= 0) { if (a[l] == top-1) { --l; --top; continue; } } if (r < n) { if (a[r] == top-1) { ++r; --top; continue; } } ans = false; break; } writeln(ans ? "YES" : "NO"); stdout.flush(); debug readln(); }
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]-1, args[2], args[3]); }else{ writeln(tree.sum(args[1]-1, args[2])); } } } class SquareRootDecomposition{ // starry import std.algorithm; int D=1, nil=-1; long[] 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, long 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{ int s=max(l, ql), t=min(r, qr); buc[k]+=x*(t-s); val[s..t]+=x; } } } long sum(int ql, int qr){ long ret=0; foreach(k; 0..D){ int l=k*D, r=(k+1)*D; if(r<=ql || qr<=l){ // }else if(ql<=l && r<=qr){ ret+=(buc[k]+star[k]*D); }else{ int s=max(l, ql), t=min(r, qr); ret+=star[k]*(t-s); ret+=reduce!"a+b"(0L, val[s..t]); } } return ret; } } 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 core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { long N = lread(); auto A = aryread(); long[long] D; foreach (a; A) { D[a] = (a in D) ? D[a] + 1 : 1; } long ans; foreach (k, v; D) { if (v < k) { ans += v; } else { ans += v - k; } } writeln(ans); }
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)() { 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 A = RD!ulong; auto B = RD!ulong; auto C = RD!ulong; auto X = RD!ulong; auto Y = RD!ulong; ulong ans; auto xy = min(X, Y); ans += min(C * xy * 2, (A + B) * xy); ulong cost; if (X > Y) cost = A; else cost = B; auto q = max(X, Y) - min(X, Y); ans += min(C * q * 2, cost * q); writeln(ans); stdout.flush(); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_003; ModComb mc; void main() { int Q; scan(Q); mc = ModComb(mod - 1, mod); foreach (_ ; 0 .. Q) { int xi, di, ni; scan(xi, di, ni); writeln(solve(xi, di, ni)); } } long solve(int x, int d, int n) { if (d == 0) { return powmod(x, n, mod); } if (n >= mod) { return 0; } if (x == 0) { return powmod(d, n, mod) * mc.f(n - 1) % mod; } auto xpd = x * powmod(d, mod - 2, mod) % mod; if (xpd + n - 1 >= mod) { return 0; } long ans = mc.f(xpd + n - 1) * mc.finv(xpd - 1) % mod; ans *= powmod(d, n, mod); ans %= mod; return ans; } long powmod(long x, long n, long mod) { return n > 0 ? powmod(x, n>>1, mod)^^2 % mod * x^^(n&1) % mod : 1; } struct ModComb { int _N; long[] _fact, _factinv; long _mod; this(int n, long mod = 1_000_000_007L) { _N = n; _fact = new long[](_N + 1); _factinv = new long[](_N + 1); _mod = mod; _fact[0] = 1; foreach (i ; 1 .. _N + 1) { _fact[i] = (_fact[i-1] * i) % _mod; } _factinv[_N] = _powmod(_fact[_N], _mod - 2); foreach_reverse (i ; 0 .. _N) { _factinv[i] = (_factinv[i+1] * (i+1)) % _mod; } } long c(int n, int k) { return f(n) * finv(n - k) % _mod * finv(k) % _mod; } long p(int n, int r) { return f(n) * finv(n - r) % _mod; } long f(long n) { return _fact[n.to!int]; } long finv(long n) { return _factinv[n.to!int]; } long _powmod(long x, long y) { return y > 0 ? _powmod(x, y>>1)^^2 % _mod * x^^(y & 1) % _mod : 1; } } int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new int[][](n, 0); foreach (i; 0 .. m) { int u, v; scan(u, v); if (is1indexed) { u--, v--; } adj[u] ~= v; if (isUndirected) { adj[v] ~= u; } } return adj; } alias Edge = Tuple!(int, "to", int, "cost"); Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new Edge[][](n, 0); foreach (i; 0 .. m) { int u, v, c; scan(u, v, c); if (is1indexed) { u--, v--; } adj[u] ~= Edge(v, c); if (isUndirected) { adj[v] ~= Edge(u, c); } } return adj; } void yes(bool b) { writeln(b ? "Yes" : "No"); } void YES(bool b) { writeln(b ? "YES" : "NO"); } T[] readArr(T)() { return readln.split.to!(T[]); } T[] readArrByLines(T)(int n) { return iota(n).map!(i => readln.chomp.to!T).array; } 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.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; // n を b 進数で表したとき、何桁になるか int calc(int n, int b) { if (n == 0) return 1; int k = 0; while (n > 0) { n /= b; k++; } return k; } void main() { int n, k; scan(n, k); writeln(calc(n, k)); }
D
import std.stdio, std.string, std.conv, std.algorithm, std.math; void main() { int n = readln.chomp.to!int; int[] w = readln.split.to!(int[]); int l, r = w.sum; int diff = r; foreach (i; 0 .. n) { l += w[i]; r -= w[i]; if (abs(l-r) > diff) break; diff = abs(l-r); } diff.writeln; }
D
import std.stdio,std.conv, std.algorithm, std.container,std.array,std.range,std.string,std.typecons,std.numeric,std.math,std.random,std.concurrency; const dx = [1,0,-1,0], dy = [0,1,0,-1]; const readMixin = q{ auto line = readln().split();foreach(i,ref arg; args){ if (i >= line.length) return;arg = line[i].to!(typeof(arg));}}; void read(T...) (auto ref T args){ mixin(readMixin);} void readArray(T)(auto ref T args){ mixin(readMixin);} int read(){int n;read(n);return n;} void times(int n,void delegate() stmt){foreach(i;iota(n))stmt();} auto dot(R1,R2)(R1 a,R2 b){ElementType!(R1) res=0;for(;!a.empty&&!b.empty;a.popFront(),b.popFront())res+=a.front*b.front;return res;} struct infIota{ int i = 0; enum empty = false; int front() { return i; } void popFront() { i++; } } int calc(int n){ if (n < 10) return 0; int [] ns; while(n){ns ~= n % 10;n /= 10;} int [] res; auto tens = ()=> infIota().map!"10^^a"; foreach(i;1 .. ns.length){ res ~= ns[0..i].dot(tens()) * ns[i..$].dot(tens()); } return 1 + calc(res.reduce!(max)); } void main(){ read().times({ read().calc.writeln; }); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n, m; scan(n, m); int[] d; for (int p = 2; p*p <= m; p++) { int c; while (m % p == 0) { m /= p; c++; } if (c) d ~= c; } if (m > 1) d ~= 1; debug { writeln(d); } long ans = 1; auto mc = ModComb(n + 1000); foreach (di ; d) { ans *= mc.c(n + di - 1, n - 1); ans %= mod; } writeln(ans); } struct ModComb { int _N; long[] _fact, _factinv; long _mod; this(int n, long mod = 1_000_000_007L) { _N = n; _fact = new long[](_N + 1); _factinv = new long[](_N + 1); _mod = mod; _fact[0] = 1; foreach (i ; 1 .. _N + 1) { _fact[i] = (_fact[i-1] * i) % mod; } _factinv[_N] = _powmod(_fact[_N], mod - 2); foreach_reverse (i ; 0 .. _N) { _factinv[i] = (_factinv[i+1] * (i+1)) % mod; } } long c(int n, int k) { return f(n) * finv(n - k) % _mod * finv(k) % _mod; } long p(int n, int r) { return f(n) * finv(n - r) % _mod; } long f(int n) { return _fact[n]; } long finv(int n) { return _factinv[n]; } long _powmod(long x, long y) { return y > 0 ? _powmod(x, y>>1)^^2 % _mod * x^^(y & 1) % _mod : 1; } } int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new int[][](n, 0); foreach (i; 0 .. m) { int u, v; scan(u, v); if (is1indexed) { u--, v--; } adj[u] ~= v; if (isUndirected) { adj[v] ~= u; } } return adj; } alias Edge = Tuple!(int, "to", int, "cost"); Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new Edge[][](n, 0); foreach (i; 0 .. m) { int u, v, c; scan(u, v, c); if (is1indexed) { u--, v--; } adj[u] ~= Edge(v, c); if (isUndirected) { adj[v] ~= Edge(u, c); } } return adj; } void yes(bool b) { writeln(b ? "Yes" : "No"); } void YES(bool b) { writeln(b ? "YES" : "NO"); } T[] readArr(T)() { return readln.split.to!(T[]); } T[] readArrByLines(T)(int n) { return iota(n).map!(i => readln.chomp.to!T).array; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
void main() { auto arr = 5.iota.map!(i => ri).array; auto k = ri; if(arr.back - arr.front > k) { writeln(":("); } else writeln("Yay!"); } // =================================== 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, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto n = RD!int; auto edges = new int[][](n); foreach (i; 0..n-1) { auto a = RD!int-1; auto b = RD!int-1; edges[a] ~= b; edges[b] ~= a; } debug writeln(edges); bool odds; { int[][] open; auto dist = new int[](n); dist[] = -1; foreach (i; 0..n) { if (edges[i].length == 1) { open ~= [i, -1]; dist[i] = 0; } } (){ while (!open.empty) { auto nd = open.front; open.popFront; auto from = nd[0]; auto par = nd[1]; auto d = dist[from]^1; foreach (to; edges[from]) { if (to == par) continue; if (dist[to] == -1) { dist[to] = d; open ~= [to, from]; } else if (dist[to] != d) { odds = true; return; } } }}(); } long cnt; { int[][] open; auto dist = new int[](n); dist[] = -1; foreach (i; 0..n) { if (edges[i].length == 1) { open ~= [i, -1]; dist[i] = 0; } } debug writeln("open:", open); foreach (e; open) { int[][] op = [e]; while (!op.empty) { auto nd = op.front; op.popFront; auto from = nd[0]; auto par = nd[1]; auto d = dist[from]+1; if (d == 3) continue; foreach (to; edges[from]) { if (to == par) continue; if (dist[to] == 0) { ++cnt; debug writeln(from, ":", to); } else if (dist[to] == -1 && d != 2) { dist[to] = d; op ~= [to, from]; } } } } } debug writeln("cnt:", cnt); long ans1 = odds ? 3 : 1; long ans2 = n-1-cnt; writeln(ans1, " ", ans2); stdout.flush; debug readln; }
D
unittest { assert( [ "3", "())" ].parse.expand.solve == "(())" ); assert( [ "6", ")))())" ].parse.expand.solve == "(((()))())" ); assert( [ "8", "))))((((" ].parse.expand.solve == "(((())))(((())))" ); } import std.conv; import std.range; import std.stdio; import std.typecons; void main() { stdin.byLineCopy.parse.expand.solve.writeln; } auto parse( Range )( Range input ) if( isInputRange!Range && is( ElementType!Range == string ) ) { input.popFront; auto s = input.front; return tuple( s ); } auto solve( string s ) { // ペアが揃っている括弧を消す auto d = s.dup; auto i = 0L; while( i + 1 < d.length ) { if( d[ i ] == '(' && d[ i + 1 ] == ')' ) { d = d[ 0 .. i ] ~ d[ i + 2 .. $ ]; if( 0 < i ) i--; } else { i++; } } // 余った')'と'('の数を数える auto l = 0L; auto r = 0L; foreach( c; d ) { if( c == ')' ) l++; else r++; } // 両端に足りない'('と')'を追加 return '('.repeat( l ).array ~ s ~ ')'.repeat( r ).array; }
D
import std.stdio; import std.string; import std.range; import std.conv; void main() { auto S = readln.chomp.to!(char[]); S[3] = '8'; S.writeln; //if(S[4] == '7'); //writeln(S[0],S[1],S[2],8,S[4],S[5],S[6],S[7],S[8],S[9],S[10]); }
D
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.typecons, std.range, std.container, std.math, std.algorithm.searching, std.functional,std.mathspecial; void main(){ auto str=readln().chomp; writeln(str.length/2-str.count!(s=>s=='p')); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto A = RD; auto B = RD; auto C = RD; writeln(A == B ? C : B == C ? A : B); stdout.flush(); debug readln(); }
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(string s) { long ans = 0; // int だと overflow int a = 0; // a の個数 int p = 0; while (p < s.length) { if (s[p] == 'A') { a++; p++; } else if (s[p] == 'B') { if (p + 1 < s.length && s[p + 1] == 'C') { ans += a; p += 2; } else { a = 0; p++; } } else { a = 0; p++; } } return ans; } void main() { string s = read!string; writeln(calc(s)); }
D
import std.stdio, std.string, std.conv, std.algorithm, std.math; struct P { int x,y; } void main() { int n = readln.chomp.to!int; foreach(_; 0 .. n) { P[] rooms; P cur; while(true) { auto inp = readln.split.map!(to!int); if(!inp[0] && !inp[1]) break; cur.x += inp[0]; cur.y += inp[1]; rooms ~= cur; } int dis = 0; P farRoom; foreach(room; rooms) { int d = room.x * room.x + room.y * room.y; if(dis < d || (dis == d && farRoom.x < room.x)) { dis = d; farRoom = room; } } writeln(farRoom.x, " ", farRoom.y); } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.container; import std.datetime; void main() { auto path = new bool[][]('I'+1, 'I'+1); foreach (i; 'A'..'I'+1) { if ((i - 'A') % 3) path[i][i-1] = 1; if ((i - 'A') % 3 != 2) path[i][i+1] = 1; if ((i - 'C') > 0) path[i][i-3] = 1; if ((i - 'G') < 0) path[i][i+3] = 1; } foreach (line; stdin.byLine) { auto str = line.chomp; char pre = str[0]; bool f; foreach (c; str[1..$]) { if (!path[pre][c]) { f = 1; break; } pre = c; } if (!f) str.writeln; } }
D
void main() { long[] tmp = readln.split.to!(long[]); long a = tmp[0], b = tmp[1], c = tmp[2], k = tmp[3]; writeln(k & 1 ? b - a : a - b); } 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; import std.string; import std.conv; import std.algorithm; int main() { while (true) { int n = readln().chomp().to!int(); if (n == 0) break; //writeln(dfs(0,0,0,n)); int p = 9999999; for (int i = 0; i <= 25 && i * 200 <= n; i++) { for (int j = 0; j <= 17 && i * 200 + j * 300 <= n; j++) { for (int k = 0; k <= 10 && i * 200 + j * 300 + k * 500 <= n; k++) { if (i * 200 + j * 300 + k * 500 == n) { int t = i * 380 + j * 550 + k * 850; t -= (i / 5) * 380; t -= (j / 4) * 330; t -= (k / 3) * 306; p = min(p, t); } } } } writeln(p); } return 0; }
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!int; const int a = r.next!uint; const int b = r.next!uint; const int c = r.next!uint; const int d = r.next!uint; const u1 = n * (a - b), v1 = n * (a + b); const u2 = c - d, v2 = c + d; const u = max (u1, u2), v = min (v1, v2); writeln ((u <= v) ? "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.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto t = RD!int; auto ans = new long[][](t); foreach (ti; 0..t) { auto n = RD!int; auto k = RD!int; auto a = RDA; auto p = new bool[](n); foreach (i; 1..n-1) { if (a[i] > a[i-1] && a[i] > a[i+1]) p[i] = true; } auto b = new long[](n-k+1); foreach (i; 1..k-1) { if (p[i]) ++b[0]; } foreach (i; 1..n-k+1) { b[i] = b[i-1]; if (p[i]) --b[i]; if (p[i+k-2]) ++b[i]; } long best = long.min; foreach (i; 0..b.length) { if (b[i] > best) { best = b[i]; ans[ti] = [best+1, i+1]; } } } foreach (e; ans) { writeln(e[0], " ", e[1]); } stdout.flush; debug readln; }
D
void main() { long n = rdElem; long[][] list = new long[][](10, 10); foreach (i; 1 .. n+1) { long f = i.to!string[0] - '0'; long b = i % 10; ++list[f][b]; } long result; foreach (i; 0 .. 10) { foreach (j; 0 .. 10) { result += list[i][j] * list[j][i]; } } result.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
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; int n; string s, t; void main() { scan(n); scan(s); scan(t); int ans = n; while (!s.empty) { if (s == t) { writeln(ans); return; } s.popFront(); t.popBack(); ans++; } 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; int n; short[] a; ulong sumW() { ulong sum; for (size_t i = 0; i < 2 * n; i += 2) sum += a[i]; return sum; } size_t j, k, t; short maxH() { short max; for (size_t i = 1; i < 2 * n; i += 2) if ((a[i] > max) && (i != j)) { max = a[i]; k = i; } if (t < 1) j = k; ++t; return max; } void main() { scanf("%d", &n); short w, h; foreach (i; 0 .. n) { scanf("%hd%hd", &w, &h); a ~= w, a ~= h; } ulong sum = sumW(); short max1 = maxH(); short max2 = maxH(); for (size_t i = 0; i < 2 * n; i += 2) if (i != j - 1) write((sum - a[i]) * max1, ' '); else write((sum - a[i]) * max2, ' '); putchar('\n'); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int a, b, c; readV(a, b, c); writeln(a+b == c || b+c == a || c+a == b ? "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; } 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 s = RD!string; writeln(s.replace(",", " ")); stdout.flush(); debug readln(); }
D
import std.conv, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken().to!int; } long readLong() { return readToken().to!long; } real readReal() { return readToken().to!real; } void chmin(T)(ref T t, in T f) { if (t > f) t = f; } void chmax(T)(ref T t, in T f) { if (t < f) t = f; } int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; } int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); } int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); } int N, H, W; void main() { try { for (; ; ) { N = readInt(); H = readInt(); W = readInt(); const ans = (N + 1 - H) * (N + 1 - W); writeln(ans); } } catch (EOFException e) { } }
D
void main() { int[] tmp = readln.split.to!(int[]); int l = tmp[0] + tmp[1], r = tmp[2] + tmp[3]; if (l > r) { "Left".writeln; } else if (l == r) { "Balanced".writeln; } else { "Right".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; import std.string; import std.range; import std.conv; void main() { auto ip = readln.split.to!(int[]), a = ip[0], b = ip[1]; if(a*b%2 == 1){ writeln("Odd"); }else{ writeln("Even"); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto N = readln.chomp.to!int; int cnt; foreach (n; 1..N+1) { if (n % 2 == 0) continue; int y; foreach (x; 1..n+1) { if (n % x == 0) ++y; } if (y == 8) ++cnt; } writeln(cnt); }
D
import std.stdio, std.conv, std.array, std.string; import std.algorithm; import std.container; void main() { auto a = readln.chomp.to!int; auto s = readln.chomp; writeln(a >= 3200 ? s : "red"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto ps = new bool[](10^^5+1); ps[] = true; ps[0] = ps[1] = false; foreach (i; 3..10^^5+1) if (ps[i]) { if (i%2 == 0) { ps[i] = false; continue; } int j = i*2; while (j <= 10^^5) { ps[j] = false; j += i; } } auto PS = new int[](10^^5+1); foreach (n; 3..10^^5+1) { PS[n] += PS[n-1]; if (n%2 == 0) continue; if (ps[n] && ps[(n+1)/2]) PS[n] += 1; } auto Q = readln.chomp.to!int; foreach (_; 0..Q) { auto lr = readln.split.to!(int[]); writeln(PS[lr[1]] - PS[lr[0]-1]); } }
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; const mod = 10^^9 + 7; long modPow(long x, long k, long m) { if (k == 0) return 1; if (k % 2 == 0) return modPow(x * x % m, k / 2, m); return x * modPow(x, k - 1, m) % m; } long nck(long n, long k, long mod) { long x = 1; long y = 1; for (int i = 0; i < k; i++) { x = x * (n - i) % mod; y = y * (i + 1) % mod; } return (x * modPow(y, mod - 2, mod)) % mod; } long calc(long n, long a, long b) { long ans = modPow(2, n, mod); ans = (ans - nck(n, a, mod) + mod) % mod; ans = (ans - nck(n, b, mod) + mod) % mod; ans--; return ans; } void main() { int n, a, b; scan(n, a, b); writeln(calc(n, a, b)); }
D
void main() { long n = readln.chomp.to!long; long f = long.max; for (long i = 1; i * i <= n; ++i) { if (n % i == 0) { long a = i.log10.to!long + 1; long b = log10(n/i).to!long + 1; f = min(f, max(a, b)); } } f.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.algorithm; 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; // [min, max] に n の倍数がいくつあるか ulong countMuls(ulong min, ulong max, ulong n) { return max/n - (min-1)/n; } ulong mod_2019(ulong l, ulong r) { if (l == 0) { return 0; } const ulong mul_3 = countMuls(l, r, 3); const ulong mul_673 = countMuls(l, r, 673); if (mul_3 != 0 && mul_673 != 0) return 0; if (mul_3 == 0) { // r - l == 1 return (l * r % 2019); } // mul_3 != 0 && mul_673 == 0 return naive(l, r); } ulong naive(ulong l, ulong r) { ulong min_mod = 2019; for(ulong i = l; i < r; i++) { for(ulong j = i + 1; j <= r; j++) { const ulong tmp = (cast(BigInt)i * cast(BigInt)j) % 2019; if (tmp < min_mod) { min_mod = tmp; } } } return min_mod; } void main() { ulong l, r; readInto(l, r); writeln(mod_2019(l, r)); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; import std.math; auto s=readln.chomp.to!(char[]); char[] t; foreach(c; s)if(c!='x') t~=c; bool ok=true; foreach(i; 0..(t.length/2)) ok&=(t[i]==t[$-i-1]); if(!ok){writeln(-1); return;} int cnt=0; int[] y; foreach(c; s){ if(c=='x') cnt++; else y~=cnt, cnt=0; } y~=cnt; int sum=0; foreach(i; 0..(y.length/2)) sum+=(y[i]-y[$-i-1]).abs; writeln(sum); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.stdio, std.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 N = RD; auto Q = RD; auto S = RD!string; auto LR = new long[2][](Q); foreach (i; 0..Q) { LR[i][0] = RD-1; LR[i][1] = RD; } auto dp = new long[](N+1); char last = S[0]; foreach (i; 1..N) { if ([last, S[i]] == "AC") { ++dp[i+1]; } last = S[i]; } foreach (i; 0..N) { dp[i+1] += dp[i]; } foreach (i; 0..Q) { auto l = LR[i][0]; auto r = LR[i][1]; auto ans = dp[r] - dp[l]; if (l != 0) if (S[l] == 'C' && S[l-1] == 'A') --ans; writeln(ans); } debug writeln(dp); stdout.flush(); debug readln(); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { int a, b, c, d; scanf("%d %d %d %d ", &a, &b, &c, &d); writeln(min(a,b) + min(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
void main() { long n = rdElem; string[] s = 2.rdCol!string; dchar[] vh = new dchar[n]; long result = 1L; long pos; while (pos < n) { if (s[0][pos] == s[1][pos]) { vh[pos] = 'v'; if (pos > 0) { if (vh[pos-1] == 'v') result *= 2; } else result *= 3; ++pos; } else { vh[pos] = 'h', vh[pos+1] = 'h'; if (pos > 0) { if (vh[pos-1] == 'v') result *= 2; else result *= 3; } else result *= 6; pos += 2; } result %= mod; } result.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; import core.stdc.stdio; import std.algorithm; int n,m; int[][] map; int ans=0; void DFS(int y,int x,int count){ map[y][x] = 0; int[] dx=[0,-1,0,1]; int[] dy=[-1,0,1,0]; for(int k=0;k<4;k++){ int ny = y+dy[k]; int nx = x+dx[k]; if(0<=ny&&ny<n&&0<=nx&&nx<m&&map[ny][nx] == 1) DFS(ny,nx,count+1); } map[y][x] = 1; ans = max(count,ans); } void main(){ while(1){ ans = 0; scanf("%d%d",&m,&n); if(m==0&&n==0) break; map = new int[][n]; for(int i=0;i<n;i++){ map[i] = new int[m]; for(int j=0;j<m;j++){ scanf("%d",&map[i][j]); } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(map[i][j] == 1) DFS(i,j,1); } } printf("%d\n",ans); } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { _lazy[] = NIL; _maxi[] = int.max; auto nq = readints(); int n = nq[0], q = nq[1]; for (int i = 0; i < q; i++) { auto xs = readints(); if (xs[0] == 0) { int s = xs[1]; int t = xs[2]; int x = xs[3]; fill(s, t + 1, x); } else { int s = xs[1]; int v = maximum(s, s + 1); writeln(v); } } } /////////////////////////////////////// const int N = 1 << 18; // ???????????° const int NIL = int.max; int[N * 2] _maxi; // ?????????k?????????????????????????????§??? int[N * 2] _lazy; // lazy[k]????????????k???????????¨??????lazy[k]??§???????????¶?????¨?????????????????¨??????lazy[k]=NIL?????¨???????????????????????¨?´?????????? void setLazy(int k, int v) { _lazy[k] = v; // ??????????????????????????¨?????? v ??§???????????¶????????????????????????????????¶????????????k????????§??????v??§?????? _maxi[k] = v; } void push(int k) { // ?????¶?????????????????????????????°??????????????? if (_lazy[k] == NIL) { return; } setLazy(k * 2 + 0, _lazy[k]); setLazy(k * 2 + 1, _lazy[k]); // ????????????????????¬??????????????§???????????????????????? _lazy[k] = NIL; } void fix(int k) { // ?????????k?????????????????????????????§?????????????????????????????§????????¨????????????????????§??????????????§??? _maxi[k] = max(_maxi[k * 2], _maxi[k * 2 + 1]); } // ??????[queryL,queryR)???val??§???????????¶??? void fill(int queryL, int queryR, int val, int k = 1, int nodeL = 0, int nodeR = N) { // ?????¨???????????¨????????????????????????????????????????????????????????\????????????????????????????????? if (nodeR <= queryL || queryR <= nodeL) { return; } // ???????????????????????¨?????????????????¨??????????????????????????¶????????????????????????????????£?????¨??°??? if (queryL <= nodeL && nodeR <= queryR) { setLazy(k, val); return; } // ???????????????????????¨??????????????????push?????? push(k); int nodeM = (nodeL + nodeR) / 2; fill(queryL, queryR, val, k * 2 + 0, nodeL, nodeM); fill(queryL, queryR, val, k * 2 + 1, nodeM, nodeR); // ???????????????????????¨??????????????±???fix?????? fix(k); } // ??????[queryL, queryR)????????§???????±??????? int maximum(int queryL, int queryR, int k = 1, int nodeL = 0, int nodeR = N) { // ?????¨???????????¨??????????????????????????????????????? if (nodeR <= queryL || queryR <= nodeL) { return int.min; } // ???????????????????????¨?????????????????¨??????????????? if (queryL <= nodeL && nodeR <= queryR) { return _maxi[k]; } // ???????????????????????¨??????????????????push?????? push(k); int nodeM = (nodeL + nodeR) / 2; int vl = maximum(queryL, queryR, k * 2 + 0, nodeL, nodeM); int vr = maximum(queryL, queryR, k * 2 + 1, nodeM, nodeR); return max(vl, vr); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto n = RD!int; auto m = RD!int; if (n == 1 || m == 1) ans[ti] = true; else if (n == 2 && m == 2) ans[ti] = true; } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush; debug readln; }
D
import std.stdio, std.string, std.array, std.conv; struct Dice6 { int[string] dice6; int[] _dice6 = new int[6]; this(int[] _dice6) { dice6["top"] = _dice6[0]; dice6["front"] = _dice6[1]; dice6["right"] = _dice6[2]; dice6["left"] = _dice6[3]; dice6["back"] = _dice6[4]; dice6["bottom"] = _dice6[5]; } void roll(char c) { int tmp = dice6["top"]; if (c == 'E') { dice6["top"] = dice6["left"]; dice6["left"] = dice6["bottom"]; dice6["bottom"] = dice6["right"]; dice6["right"] = tmp; } else if (c == 'N') { dice6["top"] = dice6["front"]; dice6["front"] = dice6["bottom"]; dice6["bottom"] = dice6["back"]; dice6["back"] = tmp; } else if (c == 'S') { dice6["top"] = dice6["back"]; dice6["back"] = dice6["bottom"]; dice6["bottom"] = dice6["front"]; dice6["front"] = tmp; } else { dice6["top"] = dice6["right"]; dice6["right"] = dice6["bottom"]; dice6["bottom"] = dice6["left"]; dice6["left"] = tmp; } } void clockwise() { int tmp = dice6["front"]; dice6["front"] = dice6["right"]; dice6["right"] = dice6["back"]; dice6["back"] = dice6["left"]; dice6["left"] = tmp; } int surface(string dir) { int result; if (dir == "top") { result = dice6["top"]; } else if (dir == "front") { result = dice6["front"]; } else if (dir == "right") { result = dice6["right"]; } else if (dir == "left") { result = dice6["left"]; } else if (dir == "back") { result = dice6["back"]; } else { result = dice6["bottom"]; } return result; } int right(int[] x) { if (x[0] == dice6["front"]) roll('N'); if (x[0] == dice6["back"]) roll('S'); while (x[0] != dice6["top"]) roll('E'); while (x[1] != dice6["front"]) clockwise; return dice6["right"]; } } void main() { int[] _dice = readln.chomp.split.to!(int[]); int q = readln.chomp.to!int; foreach (i; 0 .. q) { Dice6 dice = Dice6(_dice); int[] tmp = readln.chomp.split.to!(int[]); dice.right(tmp).writeln; } }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto a=new int[](n); foreach(i; 0..n) rd(a[i]); int[int] cnt; foreach(e; a){ if(e in cnt) cnt.remove(e); else cnt[e]=1; } writeln(cnt.length); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.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; long calc(int k, int[] a) { long ans = long.max; for (int i = 0; i < a.length; i++) { if (i + k - 1 >= a.length) break; int lo = i; int hi = i + k - 1; // 0 -> lo -> hi の順に火をつける ans = min(ans, abs(a[lo]) + abs(a[hi] - a[lo])); // 0 -> hi -> lo の順に火をつける ans = min(ans, abs(a[hi]) + abs(a[hi] - a[lo])); } return ans; } void main() { auto nk = readints; int k = nk[1]; auto a = readints; writeln(calc(k, 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 S = RD!string; auto N = S.length; auto T = new long[](N); foreach (i; 1..N) { if (S[i-1] =='R' && S[i] == 'L') { T[i] = 2; T[i-1] = 1; } } auto ans = new long[](N); long cnt, cnt2, lastPos; foreach (i; 0..N) { if (S[i] == 'R') { ++cnt; if (cnt2 != 0) { ans[lastPos] += cnt2 / 2 + (cnt2 % 2 != 0 ? 1 : 0); ans[lastPos+1] += cnt2 / 2; cnt2 = 0; } } else if (cnt != 0) { ans[i] += cnt / 2 + 1; ans[i-1] += cnt / 2 + (cnt % 2 != 0 ? 1 : 0); cnt = 0; lastPos = i - 1; } else { ++cnt2; } } if (cnt2 != 0) { ans[lastPos] += cnt2 / 2 + (cnt2 % 2 != 0 ? 1 : 0); ans[lastPos+1] += cnt2 / 2; cnt2 = 0; } ans.map!(to!string).join(" ").writeln(); stdout.flush(); debug readln(); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto s = readln.split.map!(to!long); auto H = s[0]; auto W = s[1]; if (H % 3 == 0 || W % 3 == 0) { writeln(0); return; } long ans = min(H, W); foreach (i; 1..W) { long area1 = H * i; long area2 = H / 2 * (W - i); long area3 = (H / 2 + H % 2) * (W - i); long tmp = max(max(abs(area1 - area2), abs(area2 - area3)), abs(area3 - area1)); ans = min(ans, tmp); } foreach (i; 1..H) { long area1 = W * i; long area2 = W / 2 * (H - i); long area3 = (W / 2 + W % 2) * (H - i); long tmp = max(max(abs(area1 - area2), abs(area2 - area3)), abs(area3 - area1)); ans = min(ans, tmp); } ans.writeln; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { long N = lread(); auto A = aryread(); long a, b, c; foreach (i; 0 .. N) { long cnt; while (~A[i] & 1) { A[i] >>= 1; cnt++; } a += cnt == 0; b += cnt == 1; c += 2 <= cnt; } bool z = a - 1 <= c; bool x = b % 2 == 0 || a == c; writeln(z && x ? "Yes" : "No"); }
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);}} enum MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std; // dfmt on void main() { auto T = sread(); long N = T.length; auto ans = new char[](N); foreach (i; 0 .. N) { ans[i] = T[i]; if (T[i] == '?') { if (0 < i && ans[i - 1] == 'P') { ans[i] = 'D'; continue; } if (i < N - 1 && T[i + 1] == 'D') { ans[i] = 'P'; continue; } if (i < N - 1 && T[i + 1] == '?') { ans[i] = 'P'; continue; } ans[i] = 'D'; } } writeln(ans); }
D
import std.stdio, std.string, std.conv, std.range, std.array, std.algorithm; import std.uni, std.math, std.container, std.typecons, std.typetuple; import core.bitop, std.datetime; void main(){ int n, m, q; readVars(n, m, q); auto s = new int[][](n, m); foreach(i ; 0 .. n){ auto line = readln.chomp; foreach(j, ch; line){ s[i][j] = ch - '0'; } } //stderr.writefln("%(%(%s %)\n%)", s); auto sxy = new int[][](n, m); foreach(i ; 0 .. n){ sxy[i][] = s[i][]; } //stderr.writefln("%(%(%s %)\n%)", sxy); foreach(i ; 0 .. n){ foreach(j ; 0 .. m - 1){ sxy[i][j + 1] += sxy[i][j]; } } foreach(j ; 0 .. m){ foreach(i ; 0 .. n - 1){ sxy[i + 1][j] += sxy[i][j]; } } //stderr.writefln("%(%(%s %)\n%)", sxy); //stderr.writefln("%(%(%s %)\n%)", s); auto exy = new int[][](2*n, 2*m); foreach(i ; 0 .. 2*n - 1){ if (i % 2 == 0) { foreach(j ; 0 .. 2*m - 1){ exy[i][j + 1] += exy[i][j]; if (j % 2 == 0 && j < 2*m - 2) { exy[i][j + 1] += (s[i/2][j/2] == 1 && s[i/2][j/2 + 1] == 1); } } } else { foreach(j ; 0 .. 2*m - 1){ if (j % 2 == 0){ exy[i][j] += (s[i/2][j/2] == 1 && s[i/2 + 1][j/2] == 1); } exy[i][j + 1] += exy[i][j]; } } } foreach(j ; 0 .. 2*m){ foreach(i ; 0 .. 2*n - 1){ exy[i + 1][j] += exy[i][j]; } } int x1, x2, y1, y2, nv, ne, ans; foreach(qi ; 0 .. q){ readVars(x1, y1, x2, y2); x1--; x2--; y1--; y2--; nv = sxy[x2][y2]; if (x1 - 1 >= 0) nv -= sxy[x1 - 1][y2]; if (y1 - 1 >= 0) nv -= sxy[x2][y1 - 1]; if (x1 - 1 >= 0 && y1 - 1 >= 0) nv += sxy[x1 - 1][y1 - 1]; ne = exy[2*x2][2*y2]; if (2*x1 - 1 >= 0) ne -= exy[2*x1 - 1][2*y2]; if (2*y1 - 1 >= 0) ne -= exy[2*x2][2*y1 - 1]; if (2*x1 - 1 >= 0 && 2*y1 - 1 >= 0) ne += exy[2*x1 - 1][2*y1 - 1]; ans = nv - ne; writeln(ans); } } void readVars(T...)(auto ref T args){ auto line = readln.split; foreach(ref arg ; args){ arg = line.front.to!(typeof(arg)); line.popFront; } if(!line.empty){ throw new Exception("args num < input num"); } }
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 MOD = 10^^9 + 7; void main() { auto N = readln.chomp.to!int; auto A = N.iota.map!(_ => readln.split.map!(to!long).array).array; auto dp = new long[][](N+1, 3); foreach (i; 0..N) { foreach (j; 0..3) { foreach (k; 0..3) { if (j == k) continue; dp[i+1][k] = max(dp[i+1][k], dp[i][j] + A[i][k]); } } } dp[N].reduce!max.writeln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n; scan(n); auto ans = readln.split.to!(int[]).sum() - n; writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
/+ dub.sdl: name "C" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv, std.math; // import dcomp.foundation, dcomp.scanner; int main() { auto sc = new Scanner(stdin); int n; long[] a; sc.read(n, a); auto s = a.sum; long ans = 10L^^18; long u = 0; foreach (i; 1..n) { u += a[i-1]; ans = min(ans, abs(s-2*u)); } writeln(ans); return 0; } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; if (f.eof) return false; line = lineBuf[]; f.readln(line); } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } version (X86) static if (__VERSION__ < 2071) { import core.bitop : bsf, bsr, popcnt; int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } }
D
import std.stdio;void main(){writeln(cast(char)(readln[0]+1));}
D
unittest { assert( [ "H H" ].parse.expand.solve == "H" ); assert( [ "D H" ].parse.expand.solve == "D" ); assert( [ "D D" ].parse.expand.solve == "H" ); } import std.conv; import std.range; import std.stdio; import std.typecons; void main() { stdin.byLineCopy.parse.expand.solve.writeln; } auto parse( Range )( Range input ) if( isInputRange!Range && is( ElementType!Range == string ) ) { auto ab = input.front.split; return tuple( ab[ 0 ], ab[ 1 ] ); } auto solve( string a, string b ) { return ( a == b ) ? "H" : "D"; }
D
import core.stdc.stdio; import std.traits; import std.algorithm; import std.typecons; import std.stdio; int[][] wall; bool[][][] pass; int[][][][] dist; int[][][] buf; int[] dx=[0,-1,0,1]; int[] dy=[-1,0,1,0]; static immutable int inf=114514; int w,h; void Clear(T,U)(ref T t,U u){ static if(isArray!T) foreach(ref d;t) d.Clear(u); else t=u; } void SetPass(){ pass.Clear(false); foreach(i;0..h) foreach(j;0..w){ if(i>0) pass[i][j][0]=wall[i*2-1][j]==0; if(j>0) pass[i][j][1]=wall[i*2][j-1]==0; if(i<h-1) pass[i][j][2]=wall[i*2+1][j]==0; if(j<w-1) pass[i][j][3]=wall[i*2][j]==0; } } bool inside(int x,int y){ return 0<=x&&x<w&&0<=y&&y<h; } int[] qx,qy,qd; void BFS(int[][] dst){ SetPass; dst.Clear(inf); qx[0]=w-1; qy[0]=h-1; qd[0]=0; dst[h-1][w-1]=0; int q=1; int i=0; while(i<q){ int x=qx[i],y=qy[i],d=qd[i]; i++; foreach(k;0..4){ int nx=x+dx[k],ny=y+dy[k]; if(pass[y][x][k]&&inside(nx,ny)&&dst[ny][nx]==inf){ qx[q]=nx; qy[q]=ny; qd[q++]=d+1; dst[ny][nx]=d+1; } } } } bool Solve(){ scanf("%d%d",&h,&w); if(w==0&&h==0) return false; foreach(i;0..2*h-1) foreach(j;0..w-1+(i&1)) scanf("%d",&wall[i][j]); BFS(dist[0][0]); if(dist[0][0][0][0]==inf){ printf("-1\n"); return true; } foreach(i;0..2*h-1) foreach(j;0..w-1+(i&1)) if(wall[i][j]==0){ wall[i][j]=1; BFS(dist[i][j]); wall[i][j]=0; if(dist[i][j][0][0]==inf){ printf("-1\n"); return true; } } SetPass; int[][] now=buf[0]; now.Clear(inf); int[][] next=buf[1]; int ans=inf; now[0][0]=0; foreach(k;0..h*w){ next.Clear(inf); foreach(i;0..h) foreach(j;0..w){ if(pass[i][j][0]) next[i][j]=min(next[i][j],max(now[i-1][j],dist[i*2-1][j][i-1][j]+k)); if(pass[i][j][1]) next[i][j]=min(next[i][j],max(now[i][j-1],dist[i*2][j-1][i][j-1]+k)); if(pass[i][j][2]) next[i][j]=min(next[i][j],max(now[i+1][j],dist[i*2+1][j][i+1][j]+k)); if(pass[i][j][3]) next[i][j]=min(next[i][j],max(now[i][j+1],dist[i*2][j][i][j+1]+k)); } swap(now,next); ans=min(ans,max(k+1,now[h-1][w-1])); } printf("%d\n",ans); return true; } void main(){ wall=new int[][](60,30); pass=new bool[][][](30,30,4); dist=new int[][][][](60,30,30,30); buf=new int[][][](2,30,30); qx=new int[1000]; qy=new int[1000]; qd=new int[1000]; while(Solve){} }
D
import std.stdio, std.algorithm, std.string, std.conv, std.array, std.range, std.math, core.stdc.stdio; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.to!(int[]); } void main() { int[string] a, b; { int n = readint(); foreach (i; 0..n) { a[readln.strip]++; } } { int m = readint(); foreach (i; 0..m) { b[readln.strip]++; } } int x; foreach (k, v; a) { x = max(x, v - b.get(k, 0)); } x.writeln; auto _ = readln(); // dbg }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; import std.range; auto str=readln.chomp.to!(char[]); int[char] cnt; foreach(c; str){ if(!(c in cnt)) cnt[c]=1; } bool g(char[] s){ char tg=s[0]; bool ok=true; foreach(c; s) ok&=(c==tg); return ok; } int f(char c, char[] s){ int ret=0; while(!g(s)){ foreach(i; 0..(s.length-1)){ if(s[i+1]==c) s[i]=c; } /* if(s.back!=c) */ s.popBack; ret++; } return ret; } int mi=100; foreach(c, v; cnt){ mi=min(mi, f(c, str.dup)); } writeln(mi); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.stdio; import std.conv; import std.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() { while (1) { auto x = readln.chomp.split.map!(to!int); if (x[0] == 0) break; int res; foreach (i; 1..x[2]) { foreach (j; i..x[2]) { auto pl = i * (100+x[0]) / 100 + j * (100+x[0]) / 100; if (pl == x[2]) { auto npl = i * (100+x[1]) / 100 + j * (100+x[1]) / 100; res = max(res, npl); } } } res.writeln; } }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { long N = lread(); auto C = new long[](N - 1); auto S = new long[](N - 1); auto F = new long[](N - 1); foreach (i; 0 .. N - 1) scan(C[i], S[i], F[i]); // writeln(C); foreach (i; 0 .. N - 1) { long t = S[i]; t += C[i]; foreach (j; i + 1 .. N - 1) { // write(t, " "); if (t < S[j]) t = S[j]; if (t % F[j] != 0) t += F[j] - (t % F[j]); t += C[j]; } writeln(t); } writeln(0); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { auto S = sread(); long state; foreach (c; S) { if (state == 0 && c == 'C') state = 1; if (state == 1 && c == 'F') state = 2; } writeln(state == 2 ? "Yes" : "No"); }
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, std.datetime; // 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 N, M; scan(N, M); auto cnt = new long[](N); cnt[] = 1; auto red = new bool[](N); red[0] = true; foreach (_; 0 .. M) { long X, Y; scan(X, Y); X--, Y--; red[Y] = red[X] || red[Y]; red[X] = (1 != cnt[X]) && red[X]; cnt[X]--; cnt[Y]++; } red.sum().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; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto a = RD; auto b = RD; auto d = max(a, b) - min(a, b); long x, j; foreach (i; 1..10^^5) { if (x >= d && (x - d) % 2 == 0) break; x = cast(long)i * (i+1) / 2; j = i; } ans[ti] = j; } foreach (e; ans) writeln(e); 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 nk = readln.split.to!(long[]); auto N = nk[0]; ulong K = nk[1]; long[31] rs; foreach (_; 0..N) { auto ab = readln.split.to!(long[]); ulong A = ab[0]; auto B = ab[1]; if ((A&K) == A) rs[30] += B; foreach (i, ref r; rs) { if ( (K&(1L<<i)) && !(A&(1L<<i)) && (((K>>(i+1))&(A>>(i+1))) == (A>>(i+1))) ) { r += B; } } } long r; foreach (ref rr; rs) r = max(r, rr); writeln(r); }
D
void main(){ auto N = readLine!long()[0]; long[string] dict; foreach( i ; 0..N ){ dict[ readln().chomp() ] = 1; } writeln(dict.length); } import std.stdio, std.string, std.conv; import std.math, std.algorithm, std.array; import std.regex; T[] readLine( T = size_t )( string sp = " " ){ T[] ol; foreach( string elm ; readln().chomp().split(sp) ){ ol ~= elm.to!T(); } return ol; }
D