code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio; import std.string; import std.conv; import std.algorithm; void main() { string s = readln.chomp; if (s[0] != 'A') { writeln("WA"); return; } if (s[2..$-1].count('C') != 1) { writeln("WA"); return; } string po = s.toLower; for (int i = 0; i < s.length; i++) { if ((s[i] != 'A' && s[i] != 'C') && s[i] != po[i]) { writeln("WA"); return; } } writeln("AC"); }
D
import std.stdio; import std.algorithm; import std.string; import std.range; import std.array; import std.conv; import std.complex; import std.math; import std.ascii; import std.bigint; import std.container; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } const real eps = 1e-10; void main(){ while(true) { auto n = readInt(); if(n == 0) return; int ans; for(int i = 1; i*i <= n; ++i) { if(i*i == n) { ++ans; break; } for(int j = i; i*i+j*j <= n; ++j) { if(i*i+j*j == n) { ++ans; break; } for(int k = j; i*i+j*j+k*k <= n; ++k) { if(i*i+j*j+k*k == n) { ++ans; break; } auto m = n-(i*i+j*j+k*k); auto l = 1; auto u = m; while(l < u) { auto x = (l+u)/2; if(x*x == m) { if(x >= k) { ++ans; } break; } if(x*x < m) { l = x+1; } else { u = x; } } } } } writeln(ans); } }
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) { 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; } enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { int w, a, b; scan(w, a, b); auto ans = max(0, b - (a + w), a - (b + w)); writeln(ans); }
D
import std.stdio; import std.algorithm; import std.range; import std.conv; void main() { string[] input = split(readln()); int n = to!int(input[0]); int a = to!int(input[1]); int b = to!int(input[2]); bool check() { if (a+b-1 > n) return false; if (long(a)*b < n) return false; return true; } if (!check()) { writeln(-1); return; } int[] num = new int[a]; int sum = b; num[0] = b; foreach (i; 1..a) { num[i] = 1; sum++; } while (sum < n) { foreach (i; 1..a) { if (sum == n) break; num[i]++; sum++; } } int[] ans = new int[n]; int x = 1; foreach (i; 0..a) { foreach_reverse (j; x..x+num[i]) { ans[j-1] = x++; } } foreach (i; ans) { writeln(i); } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.ascii; void main() { auto x = readln.chomp.to!int; writeln(x < 1200 ? "ABC" : "ARC"); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "x", long, "y", long, "cost"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long n, k; scan(n, k); auto l = n / k; writeln(min(n - l * k, abs(n - (l + 1) * k))); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD!string; long ans; foreach (i; 0..N.length) { auto a = [N[i]].to!long; if (i == N.length - 1) { ans += a; break; } auto b = [N[i+1]].to!long; if (a - 1 + 9 >= a + b) { ans += a - 1; ans += (N.length - 1 - i) * 9; break; } else { ans += a; } } writeln(ans); stdout.flush(); debug readln(); }
D
/+ dub.sdl: name "D" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; // import dcomp.algorithm; import std.typecons; int main() { auto sc = new Scanner(stdin); int n; sc.read(n); alias Edge = Tuple!(int, "to", long, "dist"); Edge[][] g = new Edge[][](n); foreach (i; 0..n-1) { int a, b; long c; sc.read(a, b, c); a--; b--; g[a] ~= Edge(b, c); g[b] ~= Edge(a, c); } long sm, ex = -1; long[] sz = new long[n]; void dfs(int p, int b, long bd) { long ma = 0; sz[p] = 1; foreach (e; g[p]) { if (e.to == b) continue; dfs(e.to, p, e.dist); ma = max(ma, sz[e.to]); sz[p] += sz[e.to]; } long X = sz[p], Y = n-sz[p]; debug writeln(p, " ", b, " ", bd, " ", X, " ", Y); if (ex == -1) { if (X == Y) { ex = bd; } else if (ma <= n/2 && Y <= n/2) { //mid point ex = g[p].map!(e => e.dist).minimum; } } if (X > Y) swap(X, Y); sm += 2*X*bd; } dfs(0, -1, 0); writeln(sm - ex); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } version (X86) static if (__VERSION__ < 2071) { import core.bitop : bsf, bsr, popcnt; int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; if (f.eof) return false; line = lineBuf[]; f.readln(line); } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/algorithm.d */ // module dcomp.algorithm; import std.range.primitives; import std.traits : isFloatingPoint, isIntegral; T binSearch(alias pred, T)(T l, T r) if (isIntegral!T) { while (r-l > 1) { T md = (l+r)/2; if (!pred(md)) l = md; else r = md; } return r; } T binSearch(alias pred, T)(T l, T r, int cnt = 60) if (isFloatingPoint!T) { foreach (i; 0..cnt) { T md = (l+r)/2; if (!pred(md)) l = md; else r = md; } return r; } E minimum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed) if (isInputRange!Range && !isInfinite!Range) { import std.algorithm, std.functional; return reduce!((a, b) => binaryFun!pred(a, b) ? a : b)(seed, range); } ElementType!Range minimum(alias pred = "a < b", Range)(Range range) { assert(!range.empty, "range must not empty"); auto e = range.front; range.popFront; return minimum!pred(range, e); } E maximum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed) if (isInputRange!Range && !isInfinite!Range) { import std.algorithm, std.functional; return reduce!((a, b) => binaryFun!pred(a, b) ? b : a)(seed, range); } ElementType!Range maximum(alias pred = "a < b", Range)(Range range) { assert(!range.empty, "range must not empty"); auto e = range.front; range.popFront; return maximum!pred(range, e); } Rotator!Range rotator(Range)(Range r) { return Rotator!Range(r); } struct Rotator(Range) if (isForwardRange!Range && hasLength!Range) { size_t cnt; Range start, now; this(Range r) { cnt = 0; start = r.save; now = r.save; } this(this) { start = start.save; now = now.save; } @property bool empty() { return now.empty; } @property auto front() { assert(!now.empty); import std.range : take, chain; return chain(now, start.take(cnt)); } @property Rotator!Range save() { return this; } void popFront() { cnt++; now.popFront; } }
D
void main() { dchar[] s = readln.chomp.to!(dchar[]); int cnt; foreach (i; 1 .. s.length) { if (s[i] == s[i-1]) { if (s[i] == '1') s[i] = '0'; else s[i] = '1'; ++cnt; } } cnt.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array, std.functional; void main() { auto input = readln.split.to!(int[]); auto N = input[0], K = input[1], Q = input[2]; auto A = new int[Q]; foreach(i; 0..Q) { A[i] = readln.split[0].to!int - 1; } auto ps = new int[N]; ps[] = K; foreach(a; A) { ps[a]++; } foreach(ref p; ps) { p-=Q; } foreach(p; ps) { if(p > 0) { writeln("Yes"); } else { writeln("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; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; // dfmt on void main() { long N, M; scan(N, M); auto S = new uint[](M); foreach (i; 0 .. M) { foreach (s; aryread()[1 .. $]) { S[i] = S[i] | (1 << (s - 1)); } } auto P = aryread(); long ans; outer: foreach (x; 0 .. (1 << N)) { foreach (i; 0 .. M) { // popcnt(S[i] & x).writeln(); if ((popcnt(S[i] & x) & 1) != P[i]) { // writefln("%d %d %d", i, S[i], x); continue outer; } } ans++; } writeln(ans); }
D
void main() { writeln(180 * (readln.chomp.to!int - 2)); } 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.array; import std.stdio; import std.algorithm; import std.conv; import std.string; void main(){ string[] input; input.length = 10; for(int j=0;j<10;++j){ input[j] = chomp(readln()); } int[] num; num.length = 10; for(int i=0;i<10;++i){ num[i] = to!int(input[i]); } sort(num); writeln(num[9]); writeln(num[8]); writeln(num[7]); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); void main() { long n; scan(n); auto a = aryread(); long cnt; foreach (i; 0 .. n) { if ((i % 2 == 0) && (a[i] % 2 != 0)) cnt += 1; } writeln(cnt); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
D
void main() { int[] tmp = readln.split.to!(int[]); int a = tmp[0], b = tmp[1]; writeln(a <= b ? a : a - 1); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.uni;
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto n = RD!int; auto k = RD!int; string pat = "a"; foreach (i; 1..k) { pat ~= 'a'; foreach (_; 0..2) pat ~= cast(char)(i+'a'); } foreach (i; 1..k) { foreach (j; i+1..k) { pat ~= cast(char)(i+'a'); pat ~= cast(char)(j+'a'); } } string ans; while (ans.length < n) { ans ~= pat; } ans.length = n; writeln(ans); stdout.flush; debug readln; }
D
import std.stdio, std.string, std.conv; R delegate(Args) Z(R, Args...)(R delegate(R delegate(Args), Args) f){ return (Args args) => f(Z(f), args); } void main() { (N => (Fibonacci => Fibonacci(1, 1, N) )(Z((ulong delegate(ulong, ulong, ulong)solver, ulong n1, ulong n2, ulong N) => 0 < N ? solver(n1 + n2, n1, N - 1) : n2) ))(readln.chomp.to!ulong).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 modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void main() { auto n = RD!int; auto m = RD!int; string[] s, t; foreach (i; 0..n) s ~= RD!string; foreach (i; 0..m) t ~= RD!string; auto q = RD!int; auto ans = new string[](q); foreach (qi; 0..q) { auto y = RD!int-1; ans[qi] = s[y%n] ~ t[y%m]; } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}} void main() { long x, y; readV(x, y); auto a = [x]; while (a[$-1]*2 <= y) a ~= a[$-1]*2; writeln(a.length); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.math; void main() { int n, q; scan(n, q); auto sd = SqrtDecomp(n); foreach (_ ; 0 .. q) { auto line = readln.split.to!(int[]); if (line[0] == 0) { sd.add(line[1] - 1, line[2]); } else { writeln(sd.sum(line[1] - 1, line[2])); } } } struct SqrtDecomp { private { int N, K; enum sqrtN = 512; int[] bucketSum; int[] data; } this(int n) { N = n; K = (N + sqrtN - 1) / sqrtN; bucketSum = new int[](K); data = new int[](K * sqrtN); } void add(int i, int x) { int k = i / sqrtN; data[i] += x; bucketSum[k] += x; } int sum(int l, int r) { int res; foreach (k ; 0 .. K) { int bl = k * sqrtN, br = (k + 1) * sqrtN; if (r <= bl || br <= l) continue; if (l <= bl && br <= r) { res += bucketSum[k]; } else { foreach (i ; max(l, bl) .. min(r, br)) { res += data[i]; } } } return res; } } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int N; scan(N); auto h = readln.split.to!(int[]); auto dp = new int[](N); dp[] = inf; dp[0] = 0; dp[1] = abs(h[1] - h[0]); foreach (i ; 2 .. N) { chmin(dp[i], dp[i - 1] + abs(h[i] - h[i - 1])); chmin(dp[i], dp[i - 2] + abs(h[i] - h[i - 2])); } auto ans = dp[N - 1]; writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.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 r, d, x; readV(r, d, x); foreach (i; 0..10) { x = r*x-d; writeln(x); } }
D
import std.stdio; import std.math; import std.conv; import std.string; int solve(int x) { return pow(x,3); } void main() { writeln(solve(to!int(chomp(readln)))); }
D
void main() { long s = readln.chomp.to!long; long x1 = 1L; long x2 = 10L ^^ 9; long y1 = (s + x2 - 1) / x2; long y2 = x2 * y1 - s; writeln(0, " ", 0, " ", x1, " ", y1, " ", x2, " ", y2); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.conv, std.string; 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 N = RD!long; auto A = 0 ~ RDR.split.to!(long[]); A ~= 0; auto dp = new long[](N+2); foreach (i; 0..N+1) { dp[i+1] = dp[i] + abs(A[i+1] - A[i]); } foreach (i; 0..N) { writeln(dp[N+1] - abs(A[i+1] - A[i]) - abs(A[i+2] - A[i+1]) + abs(A[i+2] - A[i])); } stdout.flush(); }
D
module app; import core.bitop; import std.algorithm; import std.array; import std.bigint; import std.conv; import std.math; import std.stdio; import std.string; struct Input { int n; int[] p, q; } void parseInput(T)(out Input input, T file) { with (file) with (input) { n = readln().strip().to!int; p = readln().strip().split().map!(to!int).array; q = readln().strip().split().map!(to!int).array; } } struct Output { } auto main2(Input* input) { return abs(orderOf(input.p) - orderOf(input.q)); } int orderOf(int[] p) { bool[] temp; temp.length = p.length; int order = 0; foreach (i, e; p) { auto value = factorial(p.length - i - 1); for (int j = 0; j < e; j++) { if (temp[j] == false) order += value; } temp[e-1] = true; } return order; } ulong factorial(ulong n) { uint ret = 1; foreach (i; 1..n+1) ret *= i; return ret; } unittest { writeln("begin unittest"); } unittest // example1 { string example = `3 1 3 2 3 1 2`; Input input = void; parseExample(input, example); auto result = main2(&input); writeln(result); assert(result == 3); } unittest // example2 { string example = `8 7 3 5 4 2 1 6 8 3 8 2 5 4 6 7 1`; Input input = void; parseExample(input, example); auto result = main2(&input); writeln(result); assert(result == 17517); } unittest // example3 { string example = `3 1 2 3 1 2 3`; Input input = void; parseExample(input, example); auto result = main2(&input); writeln(result); assert(result == 0); } 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 main() { Input input = void; parseInput(input, stdin); auto result = main2(&input); writeln(result); }
D
import std.stdio; void main(string[] args) { int done = 0; string[6] data; for (int i = 0; i < 6; i++) { data[i] = std.stdio.readln(); // data[i] = std.string.stripRight(data[i]); } int[][] cost = [ [ 3, 3, 0, 4, 4, 0, 3, 3 ], [ 3, 3, 0, 4, 4, 0, 3, 3 ], [ 2, 2, 0, 3, 3, 0, 2, 2 ], [ 2, 2, 0, 3, 3, 0, 2, 2 ], [ 1, 1, 0, 2, 2, 0, 1, 1 ], [ 1, 1, 0, 2, 2, 0, 1, 1 ] ]; int ansi = 0; int ansj = 0; int value = -1; for (int i = 0; i < 6; i++) { for (int j = 0; j < 8; j++) { if (data[i][j] == '.' && value < cost[i][j]) { value = cost[i][j]; ansi = i; ansj = j; } } } for (int i = 0; i < 6; i++) { if (i == ansi) { std.stdio.write(data[i][0..ansj]); std.stdio.write('P'); std.stdio.writeln(data[i][ansj+1..8]); } else { std.stdio.write(data[i]); } } }
D
import std.stdio, std.conv, std.string, std.math, std.regex, std.range, std.ascii, std.algorithm; void main(){ auto ip = readln.split.to!(int[]), A=ip[0], B=ip[1]; if(A<6) writeln(0); else if(A<13) writeln(B/2); else writeln(B); }
D
module main; import core.stdc.stdio ; int main() { int n,l,r,q,k=0,j=0,ans=0,low,mid,i; scanf("%d", &q); int [] a = new int[1000005]; for(i=0;i<q;i++) { scanf("%d:%d",&l,&r); k=l*60+r; a[k]++; a[k+1440]++; } for(i=0;i<2879;i++){ if(a[i]==0) j++; else { if(ans<j) ans=j; j=0; } } if(ans<j) ans=j; int hh=ans/60; int mm=ans%60; if(hh<10) printf("0%d:",hh); else printf("%d:",hh); if(mm<10) printf("0%d",mm); else printf("%d",mm); return 0; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto AS = readln.split.to!(long[]); foreach (a; AS) if (a == 0) { writeln(0); return; } long r = 1; foreach (a; AS) { if (r * a < r || r * a > 10L^^18) { writeln(-1); return; } r *= a; } writeln(r); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { auto xab = readints(); int x = xab[0], a = xab[1], b = xab[2]; if (abs(x - a) < abs(x - b)) writeln("A"); else writeln("B"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto A = readln.chomp.to!int; auto B = readln.chomp.to!int; if (A + B == 5) { writeln(1); } else if (A + B == 4) { writeln(2); } else if (A + B == 3) { writeln(3); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto S = RD!string; auto a = S[0..2].to!long; auto b = S[2..$].to!long; bool am, bm; if (a != 0 && a <= 12) { am = true; } if (b != 0 && b <= 12) { bm = true; } if (am && bm) { writeln("AMBIGUOUS"); } else if (am) { writeln("MMYY"); } else if (bm) { writeln("YYMM"); } else { writeln("NA"); } stdout.flush(); debug readln(); }
D
import std.stdio; import std.algorithm; import std.conv; import std.datetime; import std.numeric; import std.math; import std.string; string my_readln() { return chomp(readln()); } void main() { auto N = to!ulong(my_readln()); auto tokens = split(my_readln()); auto T = to!long(tokens[0]); auto A = to!long(tokens[1]); double d = double.max; ulong pos; foreach (i, token; split(my_readln())) { auto x = to!long(token); auto diff = abs(T - x * 0.006 - A); if (diff < d) { d = diff; pos = i + 1; } } writeln(pos); stdout.flush(); }
D
import std.stdio, std.string, std.conv; import std.algorithm; void main() { for(string t; (t=readln().chomp()).length;) { immutable N = t.length; auto a = new int[26][N+1]; int s=-1, e=-1; void solve() { foreach(int i,c;t) { immutable int n = c-'a'; a[i+1]=a[i]; a[i+1][n]++; foreach(int j;max(0,i-2)..i) if(a[i+1][n]-a[j][n]>(i+1-j)/2) { s=j+1; e=i+1; return; } } } solve(); writeln(s,' ',e); } }
D
void main() { auto arr = [ri,ri,ri,ri,ri]; int res = int.max; do { int current = 0; foreach(v; arr) { if(current % 10 != 0) current += (10 - current % 10); current += v; } res = min(res, current); } while(nextPermutation(arr)); res.writeln; } // =================================== 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
void main() { long[] tmp = rdRow; long n = tmp[0], m = tmp[1]; writeln(n == m ? "Yes" : "No"); } T rdElem(T = long)() { //import std.stdio : readln; //import std.string : chomp; //import std.conv : to; return readln.chomp.to!T; } alias rdStr = rdElem!string; dchar[] rdDchar() { //import std.conv : to; return rdStr.to!(dchar[]); } T[] rdRow(T = long)() { //import std.stdio : readln; //import std.array : split; //import std.conv : to; return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdRow!T).array; } void wrMat(T = long)(T[][] mat) { //import std.stdio : write, writeln; foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.string, std.conv, std.algorithm, std.array; struct P { int x, y; } void main() { int[102][102] m; auto f = readln.split.map!(to!int); int W = f[0], H = f[1]; foreach(i; 0 .. H) { foreach(j, n; readln.split.map!(to!int).array) { m[i + 1][j + 1] = n; } } W += 2; H += 2; P[] getRin(P p) { P[] res; if(p.x > 0) res ~= P(p.x - 1, p.y); if(p.x < W - 1) res ~= P(p.x + 1, p.y); if(p.y % 2 == 0) { if(p.y > 0) { res ~= P(p.x, p.y - 1); if(p.x > 0) res ~= P(p.x - 1, p.y - 1); } if(p.y < H - 1) { res ~= P(p.x, p.y + 1); if(p.x > 0) res ~= P(p.x - 1, p.y + 1); } } else { if(p.y > 0) { res ~= P(p.x, p.y - 1); if(p.x < W - 1) res ~= P(p.x + 1, p.y - 1); } if(p.y < H - 1) { res ~= P(p.x, p.y + 1); if(p.x < W - 1) res ~= P(p.x + 1, p.y + 1); } } return res; } void dfs(P p) { if(m[p.y][p.x] == 0) { m[p.y][p.x] = 2; foreach(rin; getRin(p)) { dfs(rin); } } } dfs(P(0, 0)); int res; foreach(y; 1 .. H - 1) { foreach(x; 1 .. W - 1) { if(m[y][x] == 1) foreach(rin; getRin(P(x, y))) { if(m[rin.y][rin.x] == 2) ++res; } } } res.writeln; }
D
import std.stdio, std.string, std.conv; import std.typecons; import std.algorithm, std.array, std.range, std.container; import std.math; /+ // mod must be a prime number struct Mod(uint mod) if (mod < int.max) { import std.conv : to; uint n; this(int m) { this(long(m)); } this(long m) { this.n = m >= 0L ? cast(uint)(m % mod) : cast(uint)(m % mod + mod); } this(ulong m) { this.n = cast(uint)(m % mod); } static auto normalize_(uint x) { return (x<=mod)? x : x-mod; } static auto make_(uint n_) { Mod!mod m; m.n = n_; return m; } auto opBinary(string op:"+")(Mod b) const { return make_(normalize_(n+b.n)); } auto opBinary(string op:"-")(Mod b) const { return make_(normalize_(n+mod-b.n)); } auto opBinary(string op:"*")(Mod b) const { return make_( ((n*b.n) % mod).to!uint ); } auto opBinary(string op:"/")(Mod b) const { return this*inv(b); } auto opBinary(string op:"^^")(Mod b) const { return make_( ( n ^^ ( b.n % (mod-1) ) % mod ).to!uint ); } auto opUnary(string op: "-")() const { return make_(normalize_( mod-this.n )); } auto opOpAssign(string op)(Mod b) if (op == "+" || op == "-" || op == "*" || op == "/" || op == "^^") { return mixin("this=this"~op~"b"); } static Mod inv(Mod x) { return x^^(mod-2); } auto opAssign(ulong b) { n = cast(uint)(b%mod); return this; } auto opAssign(long b) { return opAssign( b < 0 ? cast(ulong)(b%mod + mod) : cast(ulong)(b) ); } auto opBinary(string op)(long b) const if (op == "+" || op == "-" || op == "*" || op == "/" || op == "^^") { auto m = Mod!mod(b); return mixin("this"~op~"m"); } auto opUnary(string op)(long b) const if (op == "-") { auto m = Mod!mod(b); return mixin(op~"m"); } auto opOpAssign(string op)(long b) if (op == "+" || op == "-" || op == "*" || op == "/" || op == "^^") { auto m = Mod!mod(b); return mixin("this"~op~"=m"); } string toString() const { return n.to!string; } } void main() { //static immutable uint mod = 1_000_000_007; alias Int = Mod!1_000_000_007; auto data = readln.split; auto H = data[0].to!int, W = data[1].to!int, K = data[2].to!int; if (W == 1) { writeln("1"); return; } bool[][] all_pattern(uint n) { if (n == 1) return [[false], [true]]; auto next = all_pattern(n-1); auto result = next.map!"a~false".array; foreach (pat; next) { if (!pat[$-1]) result ~= pat ~ true; } return result; } auto pattern = all_pattern(W-1); Int[10][100] A; A[0][0] = Int(1); //pattern.writeln; foreach (h; 0 .. H) foreach (w; 0 .. W) { Int X, Y, Z; if (w == 0) { Y = pattern.count!((a,b) => a[0]==b)(false), Z = Int(pattern.length) - Y; //writeln("--here"); Y.writeln; Z.writeln; writeln("here--"); A[h+1][w] += Y * A[h][w]; A[h+1][w+1] += Z * A[h][w]; } else if (w == W-1) { Y = pattern.count!((a,b) => a[W-2]==b)(false), X = Int(pattern.length) - Y; A[h+1][w-1] += X * A[h][w]; A[h+1][w] += Y * A[h][w]; //writeln("--here"); X.writeln; Y.writeln; writeln("here--"); } else { X = pattern.count!((a,b) => a[w-1]==b)(true), Z = pattern.count!((a,b) => a[w]==b)(true), Y = Int(pattern.length)-X-Z; A[h+1][w-1] += X * A[h][w]; A[h+1][w] += Y * A[h][w]; A[h+1][w+1] += Z * A[h][w]; //writeln("--here"); X.writeln; Y.writeln; Z.writeln; writeln("here--"); } //writeln(A[h+1][w]); } A[H][K-1].writeln; } +/ void main() { static immutable uint mod = 1_000_000_007; //alias Int = Mod!1_000_000_007; auto data = readln.split; auto H = data[0].to!int, W = data[1].to!int, K = data[2].to!int; if (W == 1) { writeln("1"); return; } bool[][] all_pattern(uint n) { if (n == 1) return [[false], [true]]; auto next = all_pattern(n-1); auto result = next.map!"a~false".array; foreach (pat; next) { if (!pat[$-1]) result ~= pat ~ true; } return result; } auto patterns = all_pattern(W-1); ulong[10][101] A; A[0][0] = 1; //patterns.writeln; foreach (h; 0 .. H) foreach (w; 0 .. W) { foreach (row; patterns) { if (w == 0) { if (row[0]) { A[h+1][w+1] += A[h][w]; A[h+1][w+1] %= mod; } else { A[h+1][w] += A[h][w]; A[h+1][w] %= mod; } } else if (w == W-1) { if (row[w-1]) { A[h+1][w-1] += A[h][w]; A[h+1][w-1] %= mod; } else { A[h+1][w] += A[h][w]; A[h+1][w] %= mod; } } else { if (row[w-1]) { A[h+1][w-1] += A[h][w]; A[h+1][w-1] %= mod; } else if (row[w]) { A[h+1][w+1] += A[h][w]; A[h+1][w+1] %= mod; } else { A[h+1][w] += A[h][w]; A[h+1][w] %= mod; } } } } A[H][K-1].writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math; void main() { auto N = readln.chomp.to!int; wchar[][] MAP; MAP.length = N; foreach (ref line; MAP) { auto cs = readln.chomp.to!(wchar[]); line = cs ~ cs; } int cnt; foreach (o; 0..N) { void check() { foreach (i; 0..N) { foreach (j; i..N) { if (MAP[i][o+j] != MAP[j][o+i]) return; } } ++cnt; } check(); } writeln(cnt * N); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.typecons; import std.numeric, std.math; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto a = RDR.ARR; long[13] cnt; foreach (e; a) { ++cnt[e/400]; } long[2] ans; foreach (i; 0..8) { if (cnt[i] != 0) ++ans[0]; } ans[1] = ans[0]; foreach (i; 8..13) { ans[1] += cnt[i]; } writeln(max(ans[0], 1), " ", ans[1]); stdout.flush(); }
D
import std.conv; import std.stdio; import std.array; import std.range; import std.string; import std.algorithm; void main() { switch(readln().chomp()) { case "1 0 0" : writeln("Close"); break; case "0 1 0" : writeln("Close"); break; case "1 1 0" : writeln("Open"); break; case "0 0 1" : writeln("Open"); break; case "0 0 0" : writeln("Close"); break; default : stderr.writeln("Invalid Input"); break; } }
D
import std.stdio, std.string, std.conv; import std.array, std.algorithm, std.range; void main() { for(;;) { string[] ss; for(string t; (t=readln().chomp())!="" && t!="0 0"; ss~=t){} if(ss.length==0) break; int[] c = new int[3]; foreach(s;ss) { auto m = s.split().map!(to!int); immutable a=m[0], b=m[1]; c.length = max(c.length,max(a,b)+1); ++c[a]; ++c[b]; } writeln(c[1..3].all!"a&1"() && !c[3..$].any!"a&1"()?"OK":"NG"); } }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; bool ask (int x, int y) { writeln ("?", " ", x, " ", y); stdout.flush (); auto s = readln.strip; if (s == "e") { assert (false); } return (s == "x"); } void main () { string s; while ((s = readln.strip) != "") { if (s == "end") { break; } if (s != "start") { assert (false); } immutable int start = 0; int x = start; int y = x; int d = 1; do { x = y; y += d; d *= 2; } while (!ask (x, y)); int lo = x + 1; int hi = y; while (lo != hi) { auto me = (lo + hi) / 2; if (ask (x, me)) { hi = me; } else { lo = me + 1; } } writeln ("!", " ", lo); stdout.flush (); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto S = readln.chomp; int min_l, max_l = N/2+1; while (min_l+1 < max_l) { auto l = (min_l + max_l) / 2; int[string] memo; bool ok; foreach (i; 0..N-l+1) { auto k = S[i..i+l]; if (k in memo) { if (memo[k]+l-1 < i) { ok = true; break; } } else { memo[k] = i; } } if (ok) { min_l = l; } else { max_l = l; } } writeln(min_l); }
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 N = readln.chomp.to!int; auto A = readln.split.map!(to!int).array; auto st = new SegmentTree!(Tuple!(long, long), max, tuple(0L, 0L))(10^^5+1); foreach (i; 0..N) { auto v = st.query(0, A[i]-1); v[0] += 1; v[1] += A[i]; if (v > st.query(A[i], A[i])) { st.assign(A[i], v); } } st.query(0, 10^^5)[1].writeln; } class SegmentTree(T, alias op, T e) { T[] table; int size; int offset; this(int n) { size = 1; while (size <= n) size <<= 1; size <<= 1; table = new T[](size); fill(table, e); offset = size / 2; } void assign(int pos, T val) { pos += offset; table[pos] = val; while (pos > 1) { pos /= 2; table[pos] = op(table[pos*2], table[pos*2+1]); } } T query(int l, int r) { if (r < l) return e; return query(l, r, 1, 0, offset-1); } T query(int l, int r, int i, int a, int b) { if (b < l || r < a) { return e; } else if (l <= a && b <= r) { return table[i]; } else { return op(query(l, r, i*2, a, (a+b)/2), query(l, r, i*2+1, (a+b)/2+1, b)); } } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto S = readln.chomp.to!(char[]); auto T = readln.chomp.to!(char[]); auto N = S.length; auto M = T.length; foreach_reverse (i; 0..N-M+1) { foreach (j; 0..M) { if (S[i+j] != '?' && S[i+j] != T[j]) goto ng; } S[i..i+M] = T[]; goto ok; ng: } writeln("UNRESTORABLE"); return; ok: foreach (ref c; S) if (c == '?') c = 'a'; writeln(S); }
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){ auto temp = readln(); if(stdin.eof()) break; auto s = split(temp).to!(int[]); writeln(s[0]-s[1]); } }
D
import std.stdio, std.conv, std.string; import std.algorithm, std.array, std.container, std.typecons; import std.numeric, std.math; import core.bitop; T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T)(in string str, T fix) { 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 mod = pow(10, 9) + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } struct UnionFind { void init(long n) { par = new long[](n); foreach (i; 0..n) par[i] = i; } long root(long i) { return par[i] == i ? i : (par[i] = root(par[i])); } bool same(long i, long j) { return root(i) == root(j); } void unite(long i, long j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; } long[] par; } long[] dijkstra(long from, long to, long[][] edges) { long[] path; auto cost = new long[](edges.length); fill(cost, long.max); cost[from] = 0; long[][] open = [[from]]; while (!open.empty) { auto n = open.front; open.popFront; auto p = n[0]; foreach (i; 0..edges.length) { if (edges[p][i] == -1) continue; auto c = cost[p] + edges[p][i]; if (c < cost[to] && c < cost[i]) { cost[i] = c; if (i == to) path = i ~ n; else open ~= i ~ n; } } } return cost[to] ~ path; } void main() { auto N = RD!long; auto v = new long[2][][](N+1); foreach (i; 0..N-1) { auto a = RD!long; auto b = RD!long; auto c = RD!long; v[a] ~= [b, c]; v[b] ~= [a, c]; } auto Q = RD!long; auto K = RD!long; auto par = new long[2][](N+1); par[K] = [K, 0]; long[] open = [K]; while (!open.empty) { //stderr.writeln(open); auto n = open.front; open.popFront; foreach (chi; v[n]) { if (par[chi[0]][0] != 0) continue; par[chi[0]] = [K, par[n][1] + chi[1]]; open ~= chi[0]; } } foreach (i; 0..Q) { auto x = RD!long; auto y = RD!long; writeln(par[x][1] + par[y][1]); } stdout.flush(); }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; void times(alias fun)(int n) { foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } // fold was added in D 2.071.0. template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } void main() { writeln(readln.split.to!(int[]).sum==5+7+5 ? "YES":"NO"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { readln; auto S = readln.chomp.to!(char[]); auto K = readln.chomp.to!int; auto c = S[K-1]; foreach (ref s; S) if (s != c) s = '*'; writeln(S); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; void main() { string str = chomp(readln()); long n = to!long(str); writeln(factrial(n)); } long factrial(long n) { if (n == 0) { return 1; } else { return n * factrial(n-1); } }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; void main() { string[] input = readln.split; int N = input[0].to!int; int T = input[1].to!int; int H = input[2].to!int; int L = input[3].to!int; int[] t = new int[N]; int[] h = new int[N]; foreach(int i; 0..N) { input = readln.split; t[i] = input[0].to!int; h[i] = input[1].to!int; } int i=-1; int x = 0; // 残金 while(true) { i = (i+1)%N; if (t[i] > 0) { t[i]--; T++; x+=10; } else if (h[i] > 0) { h[i]--; H++; x+=100; } else { break; } if (T > L) break; if (x >= 90) { bool flg = false; for (int j = T; j>=0; j--) { if (x-90-10*j>=0 && (x-90-10*j)%100==0 && (x-90-10*j)/100<=H) { x=0; T-=j; H-=(x-90-10*j)/100; t[i]+=j; h[i]+=(x-90-10*j)/100; flg = true; break; } } if (!flg) break; } } writeln(i+1); }
D
import std.stdio; import std.string; import std.range; import std.conv; void main() { auto X = readln.chomp.to!int; auto A = readln.chomp.to!int; auto B = readln.chomp.to!int; writeln((X-A)%B); }
D
import std.stdio; import std.algorithm; import std.string; import std.functional; import std.array; import std.conv; import std.math; import std.typecons; import std.regex; import std.range; void main(){ int n = readln().chomp().to!int; for(int i=0;i<n;i++){ string ans = ""; string inp = readln().chomp(); ans ~= inp[0]; for(int j=3;j<inp.length;j++){ if(indexOf(ans,inp[j]) != -1) { j+=2; continue; } if(inp[j-2..j] == "->") ans = ans ~ inp[j]; if(inp[j-2..j] == "<-") ans = inp[j] ~ ans; j += 2; } writeln(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; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { auto nx = readints; int n = nx[0], x = nx[1]; int[] ms; for (int i = 0; i < n; i++) ms ~= readint; int min = ms.reduce!((a, b) => min(a, b)); int ans = n + (x - ms.sum) / min; writeln(ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; auto min_i = 1, max_i = M; auto mid_i = (min_i + max_i) / 2; while (max_i - min_i > 1) { mid_i = (min_i + max_i) / 2; if (M / mid_i < N) { max_i = mid_i; } else { min_i = mid_i; } } mid_i += 1000; for (;;) { if (M % mid_i == 0 && M / mid_i >= N ) { writeln(mid_i); break; } --mid_i; } }
D
void main() { /*auto S = rs, T = rs; foreach(i; 0..S.length) { string tmp = S[1..$]; tmp ~= S[0]; if(S == T) { writeln("Yes"); return; } S = tmp; } writeln("No");*/ auto S = rs, T = rs; S ~= S; if(S.canFind(T)) writeln("Yes"); else writeln("No"); } // =================================== import std.stdio; import std.string; import std.functional; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.typecons, std.range, std.container, std.math, std.algorithm.searching, std.functional,std.mathspecial; void main(){ writeln(totalSum(readln.chomp)); } long totalSum(string str){ long total=0; foreach(i,s;str){ auto num=[s].to!long; auto headPattern=2^^i; auto tl=str.length-i-1; auto tailSum=10L^^tl+2L^^(tl-1)*(5L^^tl-1)/4; total+=num*headPattern*tailSum; } return total; }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; import std.typecons; import std.bigint; // import dcomp.foundation, dcomp.scanner; // import dcomp.container.deque; int main() { auto sc = new Scanner(stdin); long n, m, k; sc.read(n, m, k); m++; long[] a = new long[n]; a.each!((ref x) => sc.read(x)); long[] dp = a.dup; long[] ndp = new long[n]; auto deq = Deque!int(); foreach (ph; 2..k+1) { ndp[] = -(10L^^18); deq.clear(); foreach (i; 0..n) { if (deq.length && deq.front == i-m) { deq.removeFront(); } if (deq.length) { ndp[i] = dp[deq.front] + ph * a[i]; } while (deq.length && dp[deq.back] <= dp[i]) { deq.removeBack(); } deq.insertBack(i.to!int); } swap(dp, ndp); } writeln(dp.fold!max); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; //fold(for old compiler) static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } unittest { import std.stdio; auto l = [1, 2, 3, 4, 5]; assert(l.fold!"a+b"(10) == 25); } } version (X86) static if (__VERSION__ < 2071) { int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/deque.d */ // module dcomp.container.deque; struct Deque(T) { import core.exception : RangeError; import core.memory : GC; import std.range : ElementType, isInputRange; import std.traits : isImplicitlyConvertible; struct Payload { T *d; size_t st, length, cap; @property bool empty() const { return length == 0; } alias opDollar = length; ref inout(T) opIndex(size_t i) inout { version(assert) if (length <= i) throw new RangeError(); return d[(st+i >= cap) ? (st+i-cap) : st+i]; } private void expand() { import std.algorithm : max; assert(length == cap); auto nc = max(size_t(4), 2*cap); T* nd = cast(T*)GC.malloc(nc * T.sizeof); foreach (i; 0..length) { nd[i] = this[i]; } d = nd; st = 0; cap = nc; } void clear() { st = length = 0; } void insertFront(T v) { if (length == cap) expand(); if (st == 0) st += cap; st--; length++; this[0] = v; } void insertBack(T v) { if (length == cap) expand(); length++; this[length-1] = v; } void removeFront() { assert(!empty, "Deque.removeFront: Deque is empty"); st++; length--; if (st == cap) st = 0; } void removeBack() { assert(!empty, "Deque.removeBack: Deque is empty"); length--; } } struct RangeT(A) { alias T = typeof(*(A.p)); alias E = typeof(A.p.d[0]); T *p; size_t a, b; @property bool empty() const { return b <= a; } @property size_t length() const { return b-a; } @property RangeT save() { return RangeT(p, a, b); } @property RangeT!(const A) save() const { return typeof(return)(p, a, b); } alias opDollar = length; @property ref inout(E) front() inout { return (*p)[a]; } @property ref inout(E) back() inout { return (*p)[b-1]; } void popFront() { version(assert) if (empty) throw new RangeError(); a++; } void popBack() { version(assert) if (empty) throw new RangeError(); b--; } ref inout(E) opIndex(size_t i) inout { return (*p)[i]; } RangeT opSlice() { return this.save; } RangeT opSlice(size_t i, size_t j) { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } RangeT!(const A) opSlice() const { return this.save; } RangeT!(const A) opSlice(size_t i, size_t j) const { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } } alias Range = RangeT!Deque; alias ConstRange = RangeT!(const Deque); alias ImmutableRange = RangeT!(immutable Deque); Payload *p; private void I() { if (!p) p = new Payload(); } private void C() const { version(assert) if (!p) throw new RangeError(); } //some value this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) {I; p = new Payload(); foreach (v; values) { insertBack(v); } } //range this(Range)(Range r) if (isInputRange!Range && isImplicitlyConvertible!(ElementType!Range, T) && !is(Range == T[])) {I; p = new Payload(); foreach (v; r) { insertBack(v); } } @property bool empty() const { return (!p || p.empty); } @property size_t length() const { return (p ? p.length : 0); } alias opDollar = length; ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; } ref inout(T) front() inout {C; return (*p)[0]; } ref inout(T) back() inout {C; return (*p)[$-1]; } void clear() { if (p) p.clear(); } void insertFront(T v) {I; p.insertFront(v); } void insertBack(T v) {I; p.insertBack(v); } void removeFront() {C; p.removeFront(); } void removeBack() {C; p.removeBack(); } Range opSlice() {I; return Range(p, 0, length); } } unittest { import std.algorithm : equal; import std.range.primitives : isRandomAccessRange; import std.container.util : make; auto q = make!(Deque!int); assert(isRandomAccessRange!(typeof(q[]))); //insert,remove assert(equal(q[], new int[](0))); q.insertBack(1); assert(equal(q[], [1])); q.insertBack(2); assert(equal(q[], [1, 2])); q.insertFront(3); assert(equal(q[], [3, 1, 2]) && q.front == 3); q.removeFront; assert(equal(q[], [1, 2]) && q.length == 2); q.insertBack(4); assert(equal(q[], [1, 2, 4]) && q.front == 1 && q.back == 4 && q[$-1] == 4); q.insertFront(5); assert(equal(q[], [5, 1, 2, 4])); //range assert(equal(q[][1..3], [1, 2])); assert(equal(q[][][][], q[])); //const range const auto rng = q[]; assert(rng.front == 5 && rng.back == 4); //reference type auto q2 = q; q2.insertBack(6); q2.insertFront(7); assert(equal(q[], q2[]) && q.length == q2.length); //construct with make auto a = make!(Deque!int)(1, 2, 3); auto b = make!(Deque!int)([1, 2, 3]); assert(equal(a[], b[])); } unittest { import std.algorithm : equal; import std.range.primitives : isRandomAccessRange; import std.container.util : make; auto q = make!(Deque!int); q.clear(); assert(equal(q[], new int[0])); foreach (i; 0..100) { q.insertBack(1); q.insertBack(2); q.insertBack(3); q.insertBack(4); q.insertBack(5); assert(equal(q[], [1,2,3,4,5])); q.clear(); assert(equal(q[], new int[0])); } } unittest { Deque!int a; Deque!int b; a.insertFront(2); assert(b.length == 0); } unittest { import std.algorithm : equal; import std.range : iota; Deque!int a; foreach (i; 0..100) { a.insertBack(i); } assert(equal(a[], iota(100))); } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; if (f.eof) return false; line = lineBuf[]; f.readln(line); } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { //string or char[10] etc //todo optimize auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { //static assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); fout.writeln("1 2 3"); fout.writeln("ab cde"); fout.writeln("1.0 1.0 2.0"); fout.close; Scanner sc = new Scanner(File(fileName, "r")); int a; int[2] b; char[2] c; string d; double e; double[] f; sc.read(a, b, c, d, e, f); assert(a == 1); assert(equal(b[], [2, 3])); assert(equal(c[], "ab")); assert(equal(d, "cde")); assert(e == 1.0); assert(equal(f, [1.0, 2.0])); } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File, writeln; import std.datetime; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); foreach (i; 0..1_000_000) { fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2); } fout.close; writeln("Scanner Speed Test(3*1,000,000 int)"); StopWatch sw; sw.start; Scanner sc = new Scanner(File(fileName, "r")); foreach (i; 0..500_000) { int a, b, c; sc.read(a, b, c); assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 500_000..700_000) { int[3] d; sc.read(d); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 700_000..1_000_000) { int[] d; sc.read(d); assert(d.length == 3); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } writeln(sw.peek.msecs, "ms"); }
D
void main() { long a, b; rdVals(a, b); writeln(calc(b) ^ calc(a-1)); } long calc(long x) { long r = x % 4, y = (r >> 1) & 1; if (r & 1) { return !y; } else { return x + y; } } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } void main() { try { for (; ; ) { const numCases = readInt(); foreach (caseId; 0 .. numCases) { const M = readInt(); const N = readInt(); auto ans = new char[][](M, N); foreach (x; 0 .. M) { ans[x][] = '0'; } ans[0][0] = ans[0][N - 1] = ans[M - 1][0] = ans[M - 1][N - 1] = '1'; for (int y = 2; y < N - 2; y += 2) { ans[0][y] = ans[M - 1][y] = '1'; } for (int x = 2; x < M - 2; x += 2) { ans[x][0] = ans[x][N - 1] = '1'; } foreach (x; 0 .. M) { writeln(ans[x]); } writeln; } } } catch (EOFException e) { } }
D
module aizu_online_judge; import std.stdio; int main(){ for(int i = 1; i <= 9; i++) { for(int j = 1; j <= 9; j++) { writeln(i, "x", j, "=", i * j); } } return 0; }
D
void main() { problem(); } void problem() { auto X = scan!long; auto K = scan!long; auto D = scan!long; long solve() { if (X < 0) X = -X; auto times = X / D; if (K < times) { return X - D*K; } auto loopTimes = K - times; if (loopTimes % 2 == 0) { return X - D*times; } else { return (X - D*times - D).abs; } } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
import std.stdio, std.math, std.algorithm, std.array, std.string, std.conv, std.container, std.range; pragma(inline, true) T[] Reads(T)() { return readln.split.to!(T[]); } alias reads = Reads!int; pragma(inline, true) void scan(Args...)(ref Args args) { string[] ss = readln.split; foreach (i, ref arg ; args) arg = ss[i].parse!int; } void main() { int n; scan(n); int[] b = reads(); auto a = new int[n]; a[0] = b[0]; foreach (i; 1..n-1) a[i] = min(b[i-1], b[i]); a[n-1] = b[$-1]; writeln(a.sum); }
D
import std.stdio; import std.string; void main() { string s = chomp(readln()); string t = chomp(readln()); int ans = 0; for (int i=0; i<s.length; i++) { if (s[i] == t[i]) ans++; } writeln(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; void main() { auto n = readln.strip.to!int; Tuple!(string, int)[] a; foreach (i; 0 .. n) { auto s = readln.splitter; auto z = s.front; s.popFront (); a ~= tuple (z, s.front.to!int); } auto t = readln.strip; int res; foreach_reverse (i; 0 .. n) { if (a[i][0] == t) break; res += a[i][1]; } writeln (res); }
D
import std.stdio; void main() { int r,g,b,n; scanf("%d %d %d %d", &r, &g, &b, &n); size_t total; for (int i; i <= n; i += r) { for (int j = i; j <= n; j += g) { if ((n - j) % b == 0) ++total; } } total.write; }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; import std.utf; bool isGood (R) (R s) { int balance = 0; foreach (c; s) { balance += c ? +1 : -1; if (balance < 0) { return false; } } return balance == 0; } void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto s = readln.strip; bool ok = false; foreach (i; 0..8) { int [char] t; t['A'] = (i >> 0) & 1; t['B'] = (i >> 1) & 1; t['C'] = (i >> 2) & 1; ok |= isGood (s.byChar.map !(c => t[c])); } writeln (ok ? "YES" : "NO"); } }
D
import std.algorithm; import std.array; import std.range; import std.stdio; import std.string; string solve (string s, int i, int j, int n) { char [13] [2] res; int loopLength = (j - i + 1) / 2; while (i > 0) { s = s[1..$] ~ s[0]; i -= 1; j -= 1; } debug {writeln (s, " ", i, " ", j);} int mid = loopLength - 1; mid = min (mid, 12); int pos = mid; res[0][pos] = s[i]; while (pos > 0) { pos -= 1; i += 1; res[0][pos] = s[i]; } i += 1; res[1][pos] = s[i]; while (i < j - 1) { pos += 1; i += 1; res[1][pos] = s[i]; } i += 1; assert (i == j); pos = mid; assert (res[0][pos] == s[i]); while (pos < 12) { pos += 1; i += 1; res[0][pos] = s[i]; } i += 1; if (i < n) { res[1][pos] = s[i]; } while (i + 1 < n) { pos -= 1; i += 1; res[1][pos] = s[i]; } return res[].map !(x => x.idup).join ("\n"); } void main () { string s; while ((s = readln.strip) != "") { int n = cast (int) (s.length); foreach (i; 0..n) { foreach (j; i + 1..n) { if (s[i] == s[j]) { if (j - i == 1) { writeln ("Impossible"); } else { writeln (solve (s, i, j, n)); } } } } } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } double euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); } double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; } double norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); } double dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto a = RD!string; auto b = RD!string; long x0, x1, y0, y1; foreach (i; 0..n) { if (a[i] == b[i]) { if (a[i] == '0') ++x0; else ++x1; } else { if (a[i] == '0') ++y0; else ++y1; } } ans[ti] = long.max; if ((x0+x1) % 2 == 1 && (x0+x1+1)/2 == x1) ans[ti].chmin(x0+x1); if ((y0+y1) % 2 == 0 && (y0+y1+1)/2 == y1) ans[ti].chmin(y0+y1); if (ans[ti] == long.max) ans[ti] = -1; } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto s = RD!string; string[] ans; ans ~= "R 2"; ans ~= "L 2"; auto len = s.length*2 - 1; ans ~= "R 2"; len += len - 2; ans ~= "R " ~ (len-1).to!string; writeln(ans.length); foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n, x; rd(n, x); auto a=readln.split.to!(int[]); int[int] bef, aft; foreach(e; a){ if(e in bef) bef[e]++; else bef[e]=1; if ((e&x) in aft) aft[e&x]++; else aft[e&x]=1; } int mn=3; foreach(e; a){ if(bef[e]>1) mn=0; else{ bef[e]--; if(((e&x) in bef) && bef[e&x]>0) mn=min(mn, 1); if(aft[e&x]>1) mn=min(mn, 2); bef[e]++; } } if(mn>2) writeln(-1); else writeln(mn); } 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
//prewritten code: https://github.com/antma/algo import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.traits; final class InputReader { private: ubyte[] p, buffer; bool eof; bool rawRead () { if (eof) { return false; } p = stdin.rawRead (buffer); if (p.empty) { eof = true; return false; } return true; } ubyte nextByte(bool check) () { static if (check) { if (p.empty) { if (!rawRead ()) { return 0; } } } auto r = p.front; p.popFront (); return r; } public: this () { buffer = uninitializedArray!(ubyte[])(16<<20); } bool seekByte (in ubyte lo) { while (true) { p = p.find! (c => c >= lo); if (!p.empty) { return false; } if (!rawRead ()) { return true; } } } template next(T) if (isSigned!T) { T next () { if (seekByte (45)) { return 0; } T res; ubyte b = nextByte!false (); if (b == 45) { while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 - (b - 48); } } else { res = b - 48; while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 + (b - 48); } } } } template next(T) if (isUnsigned!T) { T next () { if (seekByte (48)) { return 0; } T res = nextByte!false () - 48; while (true) { ubyte b = nextByte!true (); if (b < 48 || b >= 58) { break; } res = res * 10 + (b - 48); } return res; } } T[] nextA(T) (in int n) { auto a = uninitializedArray!(T[]) (n); foreach (i; 0 .. n) { a[i] = next!T; } return a; } } void main() { auto r = new InputReader (); immutable nt = r.next!uint (); foreach (tid; 0 .. nt) { const n = r.next!uint; const long x = r.next!uint; auto a = r.nextA!uint(n); sort (a); int res; long s; debug stderr.writeln (a); foreach_reverse (z; a) { s += z; debug stderr.writefln ("s = %d, res = %d, x = %d", s, res, x); if (s < x * (res+1)) break; ++res; } 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.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto n = RD!int; auto cnt = new long[](26); foreach (i; 0..n) { auto s = RD!string; foreach (c; s) { auto num = c - 'a'; ++cnt[num]; } } ans[ti] = true; foreach (i; 0..26) { if (cnt[i] % n) { ans[ti] = false; break; } } } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } struct UnionFind(T) { void init(T n) { par = new T[](n); foreach (i; 0..n) par[i] = i; cnt = new T[](n); fill(cnt, 1); } T root(T i) { return par[i] == i ? i : (par[i] = root(par[i])); } bool same(T i, T j) { return root(i) == root(j); } void unite(T i, T j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; cnt[j] += cnt[i]; } T size(T i) { return cnt[root(i)]; } T[] par, cnt; } void main() { auto t = RD!int; auto ans = new int[](t); foreach (ti; 0..t) { auto n = RD!int; auto A = RD!string; auto B = RD!string; UnionFind!int uf; uf.init(20); bool ok = true; foreach (i; 0..n) { auto a = A[i] - 'a'; auto b = B[i] - 'a'; if (a > b) { ok = false; break; } else if (a == b) continue; uf.unite(a, b); } if (!ok) { ans[ti] = -1; continue; } debug writeln(uf.par); debug writeln(uf.cnt); auto root = new bool[](20); foreach (i; 0..20) { auto r = uf.root(i); root[r] = true; } foreach (i; 0..20) { if (root[i]) { ans[ti] += uf.size(i) - 1; } } } 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; long[1000] XS, IXS, YS, IYS, ZS, IZS; void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; foreach (i; 0..N) { auto xyz = readln.split.to!(long[]); XS[i] = xyz[0]; IXS[i] = -xyz[0]; YS[i] = xyz[1]; IYS[i] = -xyz[1]; ZS[i] = xyz[2]; IZS[i] = -xyz[2]; } long res = long.min; foreach (xs; [XS, IXS]) { foreach (ys; [YS, IYS]) { foreach (zs; [ZS, IZS]) { long[int][1000] memo; long solve(int i, int r) { if (r == 0) return 0; if (i == N) return long.min / 2; if (r in memo[i]) return memo[i][r]; auto r1 = solve(i+1, r); auto r2 = solve(i+1, r-1) + xs[i] + ys[i] + zs[i]; return memo[i][r] = max(r1, r2); } res = max(res, solve(0, M)); } } } writeln(res); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; // dfmt on void main() { long N, M; scan(N, M); bool[long] A; foreach (i; 0 .. M) A[lread()] = true; auto dp = new long[](N + 2); dp[0] = 1; foreach (i; 0 .. N) { if (i + 1 !in A) dp[i + 1] = (dp[i + 1] + dp[i]) % MOD; if (i + 2 !in A) dp[i + 2] = (dp[i + 2] + dp[i]) % MOD; } // writeln(dp); writeln(dp[N]); }
D
import std.stdio, std.algorithm, std.range, std.array, std.conv, std.string, std.math, std.container, std.typecons; void main() { auto a = readln.chomp.to!int; auto b = readln.chomp.to!int; auto c = readln.chomp.to!int; auto x = readln.chomp.to!int; int cnt = 0; foreach (i; 0..min(x/500,a)+1) { foreach (j; 0..min((x-500*i)/100,b)+1) { auto res = x-500*i-100*j; if (res % 50 == 0 && res/50 <= c) { cnt++; } } } writeln(cnt); }
D
void main() { long[] tmp = rdRow; long n = tmp[0], m = tmp[1]; long[] a = rdRow; long rem; long[long] lists; lists[0] = 1; long result; foreach (x; a) { rem = (rem + x) % m; result += lists[rem]++; } 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 wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { char X, Y; scan(X, Y); long a = X - Y; if (a == 0) { writeln("="); return; } if (a < 0) { writeln("<"); } else { writeln(">"); } }
D
import std.stdio, std.string, std.array, std.algorithm, std.conv, std.typecons, std.numeric, std.math; void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; auto CS = readln.split.to!(int[]); auto DP = new int[][](M+1, N+1); foreach (ref dp; DP) dp[] = int.max/3; DP[0][N] = 0; foreach (i; 0..M) { foreach_reverse (j; 0..N+1) { auto c = j-CS[i]; if (c >= 0) { DP[i][c] = min(DP[i][c], DP[i][j] + 1); } DP[i+1][j] = min(DP[i+1][j], DP[i][j]); } } writeln(DP[M][0]); }
D
module app; import core.bitop; import std.algorithm; import std.array; import std.bigint; import std.conv; import std.stdio; import std.string; import std.traits; struct Input { int n; int[] x; } void parseInput(T)(out Input input, T file) { with (file) with (input) { n = readln().strip().to!int; x = readln().strip().split().map!(to!int).array(); } } auto main2(Input* input) { with (input) { int min = int.max; foreach (pos; 1..101) { auto cost = 0; foreach (xi; x) cost += (xi - pos) ^^ 2; if (min > cost) min = cost; } return min; } } alias retType = ReturnType!main2; static if (!is(retType == void)) { unittest { writeln("begin unittest"); } auto _placeholder_ = ReturnType!main2.init; unittest // example1 { string example = `2 1 4`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == 5); } unittest // example2 { string example = `7 14 14 2 13 56 2 37`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == 2354); } unittest // example3 { string example = ``; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == _placeholder_); } 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); alias retType = ReturnType!main2; static if (is(retType == void)) main2(&input); else { auto result = main2(&input); printResult(result); } }
D
import std.algorithm, std.array, std.conv, std.range, std.stdio, std.string; void main() { auto buf = readln.chomp.split.to!(long[]); solve(buf[0], buf[1], buf[2]).writeln; } long solve(in long n, in long a, in long b) { if ((b - a) & 1) return min(a, n - b + 1) + (b - a) / 2; return (b - a) / 2; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto A = RD; auto B = RD; auto C = RD; writeln(max(0, 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; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { auto nmxy = readints; int x = nmxy[2], y = nmxy[3]; auto xs = readints; auto ys = readints; int xmax = (xs ~ [x]).reduce!((a, b) => max(a, b)); int ymin = (ys ~ [y]).reduce!((a, b) => min(a, b)); writeln(xmax < ymin ? "No War" : "War"); }
D
// 10進数から26進数への基数変換と同様に考えれば良い // 但し, 番号は1から始まるので注意 void main(){ long n = _scan!long(); string ans; while(n>0){ int rem = (n%26==0)?26:n%26; ans ~= ( ('a'-1) + rem ); n = (n -1)/26; } ans.retro.array.to!string.writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range, std.array; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(string s) { int[] ww = new int[s.length]; // [0, i] を全て白にするコスト int[] bb = new int[s.length]; // [i, $] を全て黒にするコスト int cost = 0; for (int i = 0; i < s.length; i++) { if (s[i] == '#') cost++; ww[i] = cost; } cost = 0; for (int i = cast(int)s.length-1; i >= 0; i--) { if (s[i] == '.') cost++; bb[i] = cost; } int ans = min(ww[$-1], bb[0]); for (int i = 0; i < s.length; i++) { // [0, i] を白、[i+1, $-1] を黒にするコスト int c = ww[i] + (i + 1 < s.length ? bb[i + 1] : 0); ans = min(ans, c); } return ans; } int calc2(string s) { int n = cast(int)s.length; int[] ws = new int[s.length]; // [0, i] の白の個数 for (int i = 0; i < s.length; i++) { if (s[i] == '.') ws[i]++; if (i > 0) ws[i] += ws[i-1]; } int countW(int l, int r) { return ws[r] - (l > 0 ? ws[l-1] : 0); } int countB(int l, int r) { return (r - l + 1) - countW(l, r); } int ans = countW(0, n-1); // 全ての白を黒にするコスト // [0, i] を白、[i+1, n-1] を黒にするコスト for (int i = 0; i < n; i++) { int b = countB(0, i); int w = i == n-1 ? 0 : countW(i+1, n-1); ans = min(ans, b + w); } return ans; } void main() { readint; string s = read!string; int ans = calc2(s); writeln(ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { int a, b; foreach (c; readln.chomp) { if (c == '0') { ++a; } else { ++b; } } writeln(min(a, b) * 2); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nl = readln.split.to!(int[]); auto N = nl[0]; auto L = nl[1]; int s, e = int.max; foreach (i; 0..N) { s += L+i; if (abs(L+i) < abs(e)) e = L+i; } writeln(s - e); }
D
import std.stdio, std.conv, std.string, std.algorithm; void main() { uint n = readln.chomp.to!uint; uint[] result; foreach (i; 0..n) { uint outNum = 0; bool[3] fieldPlayer; uint point = 0; while (outNum < 3) { auto str = readln.chomp; if (str == "HIT") { if (fieldPlayer[2]) point++; fieldPlayer[2] = fieldPlayer[1]; fieldPlayer[1] = fieldPlayer[0]; fieldPlayer[0] = true; } else if (str == "OUT") { outNum++; } else if (str == "HOMERUN"){ foreach (j; 0..3) { if (fieldPlayer[j]) point++; fieldPlayer[j] = false; } point++; } } result ~= point; } foreach (r; result) r.writeln; }
D
import std; // 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 arywrite(T=long)(T[] ary){ary.map!(text).join(' ').writeln;} void scan(TList...)(ref TList Args){auto line = readln.split; foreach (i,T;TList){T val=line[i].to!(T);Args[i]=val;}} void dprint(TList...)(TList Args){debug{auto s=new string[](TList.length); static foreach(i,a;Args) s[i]=text(a);s.map!(text).join(' ').writeln;}} 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(); writeln(S == "ABC" ? "ARC" : "ABC"); }
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; } } enum MOD = (10 ^^ 9) + 7; void main() { long N, T; scan(N, T); auto A = aryread(); long ans; foreach (i; 1 .. N) { ans += min(A[i] - A[i - 1], T); } writeln(ans + T); }
D
void main() { int d = readln.chomp.to!int; string s = "Christmas"; string t = " Eve"; while (d < 25) { s ~= t; ++d; } s.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.container; import std.typecons;
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ int m = read.to!int; int d = read.to!int; int ans = 0; foreach(x; 1 .. m + 1){ foreach(y; 1 .. d + 1){ int y1 = y / 10; int y2 = y % 10; if(y1 >= 2 && y2 >= 2 && x == y1 * y2){ log(x, y); ans += 1; } } } ans.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; alias Pair = Tuple!(int, "a", int, "b"); void main() { int N; scan(N); auto d = readln.split.to!(int[]); int ans; foreach (i ; 0 .. N){ foreach (j ; i + 1 .. N) { ans += d[i] * d[j]; } } 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
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto a=readln.split.to!(int[]); foreach(ref e; a) e-=1; int c=0; foreach(i; 0..n-1){ if(i==a[i]){ swap(a[i], a[i+1]); c+=1; } } if(a[n-1]==n-1) c+=1; writeln(c); } 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; import std.bigint; void main() { auto n = readln.chomp.to!int; auto as = readln.chomp.split.to!(int[]); auto res = int.max; foreach (a; as) { int cnt; while (!(a & 1)) { a >>= 1; cnt++; } res = min(res, cnt); } res.writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto S = RD!(char[]); auto Q = RD; char[] str1, str2; bool dir = true; foreach (i; 0..Q) { auto T = RD; if (T == 1) { dir = !dir; } else { auto F = RD; auto C = RD!string; if ((dir && F == 1) || (!dir && F == 2)) str1 ~= C; else str2 ~= C; } } if (dir) { str1.reverse(); writeln(str1 ~ S ~ str2); } else { str2.reverse(); S.reverse(); writeln(str2 ~ S ~ str1); } stdout.flush; debug readln; }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { string s; readV(s); writeln("aeiou".canFind(s) ? "vowel" : "consonant"); }
D