code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum mod = 10L^^9 + 7; enum inf = 10^^9; void main() { int n, k; scan(n, k); auto a = readln.split.to!(long[]); auto ac = new long[](n + 1); foreach (i ; 1 .. n + 1) { ac[i] = ac[i-1] + a[i-1]; } long[] b; foreach (i ; 0 .. n) { foreach (j ; i + 1 .. n + 1) { b ~= ac[j] - ac[i]; } } long ans; for (int r = 63; r >= 0; r--) { long[] bt, bf; foreach (bi ; b) { if (bi & (1L<<r)) { bt ~= bi; } else { bf ~= bi; } } if (bt.length < k) { continue; } ans += (1L<<r); b = bt; } writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; void main() { while (1) { auto n = readln.chomp.to!int; if (n == -1) break; n.to!string(4).writeln; } }
D
import std.stdio,std.algorithm,std.range,std.conv,std.string; void main() { foreach(i;0..readln.chomp.to!int) { auto input=readln.split; real x1=input[0].to!real,y1=input[1].to!real, x2=input[2].to!real,y2=input[3].to!real, x3=input[4].to!real,y3=input[5].to!real, x4=input[6].to!real,y4=input[7].to!real; real a=x1-x2,b=y1-y2,c=x4-x3,d=y4-y3; writeln(a * d - b * c == 0 ? "YES" : "NO"); } }
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 n = read.to!int; int d = read.to!int; long[][] xs = new long[][](n, d); foreach(i; 0 .. n) foreach(k; 0 .. d){ xs[i][k] = read.to!long; } int ans = 0; foreach(i; 0 .. n) foreach(j; 0 .. i){ long u2; foreach(k; 0 .. d) u2 += (xs[i][k] - xs[j][k] ) * (xs[i][k] - xs[j][k]); long u = (u2.to!real + 0.1).sqrt.to!long; if(u * u == u2) ans += 1; } ans.writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container; void main() { auto N = readln.chomp.to!int; auto as = readln.split.to!(long[]); foreach (a; as) if (a >= N) goto solve; writeln(0); return; solve: long l, r = (10L^^16+1000)*N+1; while (l+1 < r) { auto m = (l+r)/2; auto aa = as.dup; auto K = m; auto ds = new long[](N); long d; for (;;) { ds[] = 0; d = 0; foreach (i, ref a; aa) { if (i) ds[i] += ds[i-1]; a += d; if (a >= N) { auto x = a / N; if (K < x) goto ng; K -= x; a %= N; ds[i] += x; d += x; } } bool ok = true; foreach (i, ref a; aa) { a += d - ds[i]; if (a >= N) ok = false; } if (ok) break; } r = m; continue; ng: l = m; } writeln(r); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void main() { auto lrd = readln.split.to!(int[]); auto L = lrd[0]; auto R = lrd[1]; auto d = lrd[2]; int c; foreach (i; L..R+1) if (i%d == 0) ++c; writeln(c); }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto a = readln.split.to!(long[]); foreach (i; 1 .. n) a[i] = a[i] + a[i - 1]; long mn = 10L ^^ 18; import std.math; foreach (i; 0 .. (n - 1)) { // [0, i], (i, n) mn = min(mn, abs(a[i] - (a[n - 1] - a[i]))); } 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
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 abcd = readints; int a = abcd[0], b = abcd[1], c = abcd[2], d = abcd[3]; writeln(max(a * b, c * d)); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n; scan(n); auto a = iota(n).map!(i => readln.chomp.to!int).array; auto as = a.reduce!"a ^ b"(); auto b = new int[](32); foreach (i ; 0 .. n) { b[bsf(a[i])]++; } debug { writeln("as:", as); writeln("b:", b); } int s = 0; int ans; foreach_reverse (i ; 0 .. 32) { int ai = (as >> i) & 1; ai ^= s; debug { writefln("i : %d, ai: %d, s: %d", i, ai, s); } if (ai) { if (b[i]) { ans++; s ^= 1; } else { writeln(-1); return; } } } writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.array, std.conv, std.typecons, std.algorithm; T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; } void main() { immutable i = readln.split.to!(long[]); writeln(i[0]+1-i[1]); }
D
import std.algorithm; import std.array; import std.stdio; import std.conv; import std.string; import std.range; void main(){ size_t w, h, sx, sy; char[][] data = new char[][](20, 20); while(true){ auto input = readln.split; w = input[0].to!int; h = input[1].to!int; if(!(w | h)){ break; } foreach(i; 0..h){ foreach(j, c; readln.chomp){ data[i][j] = c; if(c == '@'){ sx = j; sy = i; data[i][j] = '#'; } } } data.dfs(sx, sy, w, h).writeln; } } auto move = [[1, 0], [0, 1], [-1, 0], [0, -1]]; size_t dfs(char[][] data, size_t sx, size_t sy, size_t w, size_t h){ size_t count = 1; foreach(i; 0..4){ size_t x = sx + move[i][0]; size_t y = sy + move[i][1]; if(0 <= x && x < w && 0 <= y && y < h && data[y][x] != '#'){ data[y][x] = '#'; count += dfs(data, x, y, w, h); } } return count; }
D
import std.stdio; import std.algorithm; import std.math; import std.conv; import std.string; import std.array; int readInt(){ return readln.chomp.to!int; } int[] readInts(){ return readln.chomp.split.to!(int[]); } void main(){ int n = readInt(), i; int[] h = readInts(); for(i = 0; h.count(0) != h.length; i++){ bool f = false; foreach(ref e; h){ if(e != 0 && !f) f = true; if(e == 0 && f) break; if(f) e--; } } 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; void main() { auto n = readln.chomp.to!int; int cx, cy, ct; auto f = false; foreach (_; 0..n) { auto x = readln.chomp.split.to!(int[]); x[0] -= ct; auto d = x[1] - cx + x[2] - cy; if (x[0] - d >= 0 && (x[0] - d) % 2 == 0) { cx = x[1]; cy = x[2]; ct += x[0]; } else { f = true; break; } } if (f) { writeln("No"); } else { writeln("Yes"); } }
D
void main() { auto N = ri; auto s = rs; if(s.count!"a == 'R'" > s.count!"a == 'B'") writeln("Yes"); else writeln("No"); } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.algorithm, std.string, std.conv, std.array, std.range, std.math, core.stdc.stdio; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.to!(int[]); } void main() { int a,b,c; scanf("%d%d%d", &a,&b,&c); writeln(a+b >= c ? "Yes" : "No"); auto _ = readln(); // dbg }
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"; T RD(T = long)() { 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 = 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; } struct UnionFind { void init(long n) { par = new long[](n); foreach (i; 0..n) par[i] = i; cnt = new long[](n); fill(cnt, 1); } 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); } long unite(long i, long j) { i = root(i); j = root(j); if (i == j) return 0; par[j] = i; auto cnt_i = ((cnt[i] ^^ 2) - cnt[i]) / 2; auto cnt_j = ((cnt[j] ^^ 2) - cnt[j]) / 2; cnt[i] += cnt[j]; auto diff = ((cnt[i] ^^ 2) - cnt[i]) / 2 - (cnt_i + cnt_j); //stderr.writeln(i, ":", j, " ", cnt_i, " ", cnt_j, " ", diff); return diff; } long[] par; long[] cnt; } void main() { auto N = RD; auto M = RD; auto ab = new long[2][](M); foreach (i; 0..M) { auto a = RD-1; auto b = RD-1; ab[i] = [a, b]; } auto ans = new long[](M); UnionFind uf; uf.init(N); long cnt; long full = ((N ^^ 2) - N) / 2; foreach_reverse (i, e; ab) { ans[i] = full - cnt; cnt += uf.unite(e[0], e[1]); } foreach (e; ans) writeln(e); stdout.flush(); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; void main() { auto s = readln.split.map!(to!int); auto n = s[0]; auto q = s[1]; auto st = new SegmentTree!(int, min, (1<<31)-1)(n); while (q--) { s = readln.split.map!(to!int); auto t = s[0]; auto x = s[1]; auto y = s[2]; if (t == 0) { st.assign(x, y); } else { st.query(x, y).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]); } } void add(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) { 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
void main() { problem(); } void problem() { auto K = scan!int; auto A = scan!int; auto B = scan!int; bool solve() { foreach(i; A..B+1) { if (i % K == 0) return true; } return false; } writeln(solve() ? "OK" : "NG"); } // ---------------------------------------------- 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
class UnionFind{ int n; int[] root; this(int n){ root.length=n; foreach(i; 0..n) root[i]=i; } int find(int i){ if(root[i]==i) return root[i]; else return root[i]=find(root[i]); } void unite(int x, int y){ x=find(x); y=find(y); if(x==y) return; root[x]=y; } bool same(int x, int y){ return find(x)==find(y); } } void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n, m; rd(n, m); auto p=readln.split.to!(int[]); auto uf=new UnionFind(n); foreach(_; 0..m){ int a, b; rd(a, b); uf.unite(a-1, b-1); } int cnt=0; foreach(i; 0..n){ if(uf.same(i, p[i]-1)) cnt++; } writeln(cnt); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x) e=l[i].to!(typeof(e)); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto A = RD; auto B = RD; A = A == 1 ? 14 : A; B = B == 1 ? 14 : B; writeln(A == B ? "Draw" : A < B ? "Bob" : "Alice"); stdout.flush(); debug readln(); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; immutable long MOD = 10^^9 + 7; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto K = s[1]; auto S = readln.chomp; auto cnt = new int[](26); foreach (i; 0..N) cnt[S[i]-'A'] += 1; int ans = 1 << 29; foreach (i; 0..K) ans = min(ans, cnt[i]); writeln(ans * K); }
D
// import chie template :) {{{ import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons; // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } // }}} void main() { auto cin = new Scanner; int n, h, w; cin.scan(n, h, w); int res; foreach (i; 0 .. n) { int y, x; cin.scan(y, x); res += min(y / h, x / w); } writeln(res); }
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 a = readln.chomp.split.to!(int[]); int dup; bool[int] f; foreach (e; a) { if (!f.get(e, 0)) { f[e] = true; } else { dup++; } } writeln(n-(dup+1)/2*2); }
D
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; // void main(string[] args)でもいい. void main() { auto ab = readln.split.to!(int[]); writeln(ab[0] * ab[1]); }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv, std.typecons, std.math; import std.functional; // import dcomp.foundation, dcomp.scanner; // import dcomp.graph.maxflow; long f(int le, int p, long d) { int q = le-1-p; long up = 0; foreach (p2; p..le) { int q2 = le-1-p2; if (p2 >= q2) break; up += 9*(10L^^q2 - 10L^^p2); } if (abs(d) > up) return 0; if (p == q) { if (d == 0) return 10; return 0; } if (p > q) { if (d == 0) return 1; return 0; } long sm = 0; foreach (i; 0..10) { foreach (j; 0..10) { if (p == 0 && j == 0) continue; sm += memoize!f(le, p+1, d - (i-j)*(10L^^q - 10L^^p)); } } return sm; } int main() { auto sc = new Scanner(stdin); long D; sc.read(D); long sm = 0; foreach (le; 1..19) { sm += f(le, 0, D); /* long d = D; long sm = 0; foreach (i; 0..le) { int j = le-1-i; if (i >= j) break; long f = (10L^^j) - (10L^^i); writeln(d / f); d %= f; } if (d) continue;*/ } writeln(sm); return 0; } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/container/deque.d */ // module dcomp.container.deque; struct Deque(T, bool mayNull = true) { 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 (mayNull && !p) p = new Payload(); } private void C() const { version(assert) if (mayNull && !p) throw new RangeError(); } static if (!mayNull) { @disable this(); } private this(Payload* p) { this.p = p; } this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) { p = new Payload(); foreach (v; values) { insertBack(v); } } this(Range)(Range r) if (isInputRange!Range && isImplicitlyConvertible!(ElementType!Range, T) && !is(Range == T[])) { p = new Payload(); foreach (v; r) { insertBack(v); } } static Deque make() { return Deque(new Payload()); } @property bool havePayload() const { return (!mayNull || p); } @property bool empty() const { return (!havePayload || p.empty); } @property size_t length() const { return (havePayload ? p.length : 0); } alias opDollar = length; ref inout(T) opIndex(size_t i) inout {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); } alias stableInsertBack = insertBack; void removeFront() {C; p.removeFront(); } void removeBack() {C; p.removeBack(); } Range opSlice() {I; return Range(p, 0, length); } } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/graph/maxflow.d */ // module dcomp.graph.maxflow; // import dcomp.container.deque; struct maxFlowInfo(C) { C flow; bool[] dual; } maxFlowInfo!(C) maxFlow(C, C EPS, T)(T g, int s, int t) { assert(s != t); import std.algorithm : map; import std.range : array; import std.conv : to; int n = g.length.to!int; int[] level = new int[n]; int[] iter = new int[n]; void bfs() { level[] = -1; level[s] = 0; auto que = Deque!int(s); while (!que.empty) { int v = que.back; que.removeBack(); foreach (e; g[v]) { if (e.cap <= EPS) continue; if (level[e.to] < 0) { level[e.to] = level[v] + 1; que.insertBack(e.to); } } } } C dfs(int v, int t, C f) { import std.algorithm : min; if (v == t) return f; auto edgeList = g[v][iter[v]..$]; foreach (ref e; edgeList) { if (e.cap <= EPS) continue; if (level[v] < level[e.to]) { C d = dfs(e.to, t, min(f, e.cap)); if (d <= EPS) continue; e.cap -= d; g[e.to][e.rev].cap += d; return d; } iter[v]++; } return 0; } C flow = 0; while (true) { bfs(); if (level[t] < 0) break; iter[] = 0; while (true) { C f = dfs(s, t, C.max); if (!f) break; flow += f; } } auto mfInfo = maxFlowInfo!C(); mfInfo.flow = flow; mfInfo.dual = level.map!"a == -1".array; return mfInfo; } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; if (f.eof) return false; line = lineBuf[]; f.readln(line); } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } version (X86) static if (__VERSION__ < 2071) { import core.bitop : bsf, bsr, popcnt; int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; import std.numeric; void main() { int n; int[] a; scan(n); a = readln.split.to!(int[]); a = [0] ~ a ~ [0]; auto lb = new int[](n + 2), rb = new int[](n + 2); auto st = Stack!(int)(n); st.push(0); foreach (i ; 1 .. n + 1) { while (!st.empty && a[st.top] > a[i]) { st.pop(); } lb[i] = st.top; st.push(i); } st.clear(); st.push(n + 1); foreach_reverse (i ; 1 .. n + 1) { while (!st.empty && a[st.top] >= a[i]) { st.pop(); } rb[i] = st.top; st.push(i); } long ans; foreach (i ; 1 .. n + 1) { ans += 1L * (i - lb[i]) * (rb[i] - i) * a[i]; } writeln(ans); } struct Stack(T) { private: int N, peek; T[] data; public: this(int size) { N = size; data = new T[](N); } bool empty() @property { return peek == 0; } bool full() @property { return peek == N; } void push(T x) @property { assert(!full); data[peek++] = x; } void pop() @property { assert(!empty); --peek; } T top() @property { return data[peek - 1]; } void clear() @property { peek = 0; } int length() @property { return peek; } } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; const RED_OVER_INDEX = 8; int score2index(int score) { foreach (i, s; [400, 800, 1200, 1600, 2000, 2400, 2800, 3200]) { if (score < s) { return cast(int)i; } } return RED_OVER_INDEX; } void main() { readint; auto a = readints; auto d = new int[9]; foreach (x; a) { d[score2index(x)]++; } int x = cast(int) d[0..RED_OVER_INDEX].count!(e => e > 0); int minAns = x; int maxAns = x; int zero = cast(int) d[0..RED_OVER_INDEX].count!(e => e == 0); if (d[RED_OVER_INDEX] > 0) { if (minAns == 0) minAns++; maxAns += d[RED_OVER_INDEX]; } writeln(minAns, " ", maxAns); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; void main() { size_t n; scanf("%ld\n", &n); size_t[ubyte[]] map; foreach(i; 0..n) { auto l = readln.chomp.representation.dup; l.sort!"a<b"; immutable key = l.dup; if (key in map) { ++map[key]; } else { map[key] = 1; } } size_t ans; foreach(key; map.keys) { ans += map[key] * (map[key] - 1) / 2; } ans.write; }
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() { string s; scan(s); auto n = s.length.to!int; int a; foreach (i ; 0 .. n) { if (s[i] == 'A') { a = i; break; } } int z = n; foreach_reverse (i ; 0 .. n) { if (s[i] == 'Z') { z = i; break; } } auto ans = z - a + 1; writeln(ans); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; void main() { auto s = readln.strip; int t; for (size_t i = 0, j = s.length - 1; i < j; i++,j--) { if (s[i] != s[j]) ++t; } writeln (t); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto A = RD!long; auto B = (RD!string).replace(".", "").to!long; auto ans = A * B; writeln(ans/100); stdout.flush; debug readln; }
D
import std.stdio, std.string, std.conv; void main() { while(true) { int n=readln.chomp.to!int; if(!n)break; int num; for(int i=1; i<n; ++i) { int sum; for(int j=i; j<n; ++j) { sum+=j; if(sum == n) ++num; else if(sum > n) break; } } num.writeln; } }
D
import std.algorithm; import std.array; import std.ascii; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void log(A...)(A arg) { stderr.writeln(arg); } int size(T)(in T s) { return cast(int)s.length; } void main() { long x = readln.chomp.to!long; long sum(long n) { // 隨ャ0鬆・°繧臥ャャn-1鬆・∪縺ァ縺ョ蜥・// return (n / 2) * 11L + 6 * (n % 2); } long lb = 0, ub = cast(long)(1e15 + 1); while (lb + 1 < ub) { long mid = (lb + ub) / 2; if (sum(mid) < x) { lb = mid; } else { ub = mid; } } writeln(ub); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void main() { string s; readV(s); string t; readV(t); auto ns = s.length.to!int, nt = t.length.to!int; loop: foreach_reverse (i; 0..ns-nt+1) { auto u = s.dup; foreach (j; 0..nt) { if (u[i+j] == '?') { u[i+j] = t[j]; } else { if (u[i+j] != t[j]) continue loop; } } foreach (ref ui; u) if (ui == '?') ui = 'a'; writeln(u); return; } writeln("UNRESTORABLE"); }
D
// import std.stdio,std.array,std.conv; void main(){ auto sr=readln.split; int r=to!int(sr[0]); auto sg=readln.split; int g=to!int(sg[0]); writeln(g*2-r); }
D
import std.algorithm; import std.bigint; import std.concurrency; import std.container; import std.conv; import std.functional; import std.format; import std.math; import std.meta; import std.numeric; import std.random; import std.range; import std.stdio; import std.string; import std.traits; import std.typecons; auto solve(long N, long M, long[] a, long[] b, long[] k, long g) { auto dp = new long[][](M + 1, 2 ^^ N); foreach (i; 0 .. M + 1) foreach (p; 0 .. 2 ^^ N) dp[i][p] = long.max; dp[0][0] = 0; foreach (i; 0 .. M) { foreach (p; 0 .. 2 ^^ N) { if (dp[i][p] == long.max) continue; dp[i + 1][p] = min(dp[i + 1][p], dp[i][p]); dp[i + 1][p | k[i]] = min(dp[i + 1][p | k[i]], dp[i][p] + a[i]); } } return dp[M][g] == long.max ? -1 : dp[M][g]; } void main() { auto l1 = readln.chomp.split.map!(to!long).array; long N = l1[0]; long M = l1[1]; auto g = goalPattern(N); auto a = new long[](M); auto b = new long[](M); auto k = new long[](M); foreach (i; 0 .. M) { auto l2 = readln.chomp.split.map!(to!long).array; a[i] = l2[0]; b[i] = l2[1]; k[i] = readln.chomp.split.map!(to!long).array.keyPattern; } solve(N, M, a, b, k, g).writeln; } long keyPattern(long[] c) { long key; foreach (ci; c) { key += 2 ^^ (ci - 1); } return key; } long goalPattern(long N) { return 2 ^^ N - 1; }
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; auto T = RD!string; long ans; foreach (i; 0..S.length) { if (S[i] != T[i]) ++ans; } writeln(ans); stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.array, std.string; import std.algorithm; import std.container; import std.range; import core.stdc.stdlib; import std.math; void main() { auto N = readln.chomp.to!int; auto S = readln; int total = 0; foreach(i; 0..N) { if (i == 0) { total++; continue; } if (S[i-1] == S[i]) continue; total++; } writeln(total); }
D
void main() { int x = readln.chomp.to!int; int a = readln.chomp.to!int; int b = readln.chomp.to!int; writeln((x - a) % b); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, 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 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; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto D = RDA; auto cnt = new long[](N); foreach (i; 0..N) { ++cnt[D[i]]; } long ans = cnt[0]; foreach (i; 1..N) { foreach (j; 0..cnt[i]) ans.modm(cnt[i-1]); } writeln(cnt[0] >= 2 || D[0] != 0 ? 0 : ans); stdout.flush(); debug readln(); }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; string a, b; int m; rd(a); rd(b); rd(m); const int MOD = 10 ^^ 4; int solve(string n) { auto memo = new short[][][][][][](n.length + 1, 2, 4, 10, 3, m); afill(memo, (-1).to!(short)); int f(size_t i, bool less, int foo, int pre, int sgn, int modm, int k) { // sgn 1=>up, 2=>down if (i == n.length) { return modm == 0; } else { if (memo[i][less][foo][pre][sgn][modm] >= 0) { return memo[i][less][foo][pre][sgn][modm]; } short ret = 0; auto digit = less ? 9 : n[i] - '0'; for (int d = 0; d <= digit; d++) { auto l = less || (d < digit); auto bar = min(3, foo + (foo == 0 ? (d > 0) : 1)); auto p = d; auto s = d - pre; if (s > 0) { s = 1; } else if (s < 0) { s = 2; } if (bar < 2 || (bar == 2 && s != 0) || ((sgn == 1 && s == 2) || (sgn == 2 && s == 1))) { (ret += f(i + 1, l, bar, p, s, (modm * 10 + d) % m, k * 10 + d)) %= MOD; } } return memo[i][less][foo][pre][sgn][modm] = ret; } } return f(0, 0, 0, 0, 0, 0, 0); } bool zigzag(string n) { if (n.length == 1) { return true; } else { for (int i = 1, sgn = 0; i < n.length; i++) { if (i == 1) { sgn = n[i - 1] - n[i]; } else { auto s = n[i - 1] - n[i]; if (sgn * s >= 0) { return false; } else { sgn = s; } } } } return true; } auto res = (solve(b) - solve(a) + MOD) % MOD; if (zigzag(a)) { res++; } writeln(res % MOD); } void rd(T...)(ref T x) { import std.stdio : readln; import std.string : split; import std.conv : to; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); } void afill(Range, Type)(Range r, Type value) { static if (is(typeof(r) == Type[])) { foreach (ref elem; r) elem = value; } else { foreach (ref arr; r) afill(arr, value); } }
D
import std.stdio; import std.ascii; import std.conv; import std.string; import std.algorithm; import std.range; import std.functional; import std.math; import core.bitop; void main() { auto s = readln.chomp; auto t = readln.chomp; foreach (i; 0 .. s.length) { bool flag = true; foreach (k, c; s) flag &= (t[(k + i) % t.length] == c); if (flag) { "Yes".writeln; return; } } "No".writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; int c; foreach (i; 1..N+1) { if (i < 10 || (100 <= i && i < 1000) || (10000 <= i && i < 100000)) ++c; } writeln(c); }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; import std.typecons; import std.bigint; // import dcomp.foundation, dcomp.scanner; // import dcomp.container.deque; int main() { auto sc = new Scanner(stdin); int n, m, k; sc.read(n, m, k); m++; int[] a = new int[n]; a.each!((ref x) => sc.read(x)); long[] dp = a.map!(to!long).array; long[] ndp = new long[n]; auto deq = Deque!(int, false).make(); foreach (int ph; 2..k+1) { ndp[] = -(10L^^18); deq.clear(); foreach (int i; 0..n) { if (deq.length && deq.front == i-m) { deq.removeFront(); } if (deq.length) { ndp[i] = dp[deq.front] + 1L * ph * a[i]; } while (deq.length && dp[deq[deq.length-1]] <= dp[i]) { deq.removeBack(); } deq.insertBack(i); } swap(dp, ndp); } writeln(dp.fold!max); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; //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, bool hasNull = true) { 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 (hasNull && !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); } } static Deque make() { Deque que; que.p = new Payload(); return que; } @property bool empty() const { return (!p || p.empty); } @property size_t length() const { return (p ? p.length : 0); } alias opDollar = length; ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; } ref inout(T) front() inout {C; return (*p)[0]; } ref inout(T) back() inout {C; return (*p)[$-1]; } void clear() { if (p) p.clear(); } void insertFront(T v) {I; p.insertFront(v); } void insertBack(T v) {I; p.insertBack(v); } void removeFront() {C; p.removeFront(); } void removeBack() {C; p.removeBack(); } Range opSlice() {I; return Range(p, 0, length); } } unittest { import std.algorithm : equal; import std.range.primitives : isRandomAccessRange; import std.container.util : make; auto q = make!(Deque!int); assert(isRandomAccessRange!(typeof(q[]))); //insert,remove assert(equal(q[], new int[](0))); q.insertBack(1); assert(equal(q[], [1])); q.insertBack(2); assert(equal(q[], [1, 2])); q.insertFront(3); assert(equal(q[], [3, 1, 2]) && q.front == 3); q.removeFront; assert(equal(q[], [1, 2]) && q.length == 2); q.insertBack(4); assert(equal(q[], [1, 2, 4]) && q.front == 1 && q.back == 4 && q[$-1] == 4); q.insertFront(5); assert(equal(q[], [5, 1, 2, 4])); //range assert(equal(q[][1..3], [1, 2])); assert(equal(q[][][][], q[])); //const range const auto rng = q[]; assert(rng.front == 5 && rng.back == 4); //reference type auto q2 = q; q2.insertBack(6); q2.insertFront(7); assert(equal(q[], q2[]) && q.length == q2.length); //construct with make auto a = make!(Deque!int)(1, 2, 3); auto b = make!(Deque!int)([1, 2, 3]); assert(equal(a[], b[])); } unittest { import std.algorithm : equal; import std.range.primitives : isRandomAccessRange; import std.container.util : make; auto q = make!(Deque!int); q.clear(); assert(equal(q[], new int[0])); foreach (i; 0..100) { q.insertBack(1); q.insertBack(2); q.insertBack(3); q.insertBack(4); q.insertBack(5); assert(equal(q[], [1,2,3,4,5])); q.clear(); assert(equal(q[], new int[0])); } } unittest { Deque!int a; Deque!int b; a.insertFront(2); assert(b.length == 0); } unittest { import std.algorithm : equal; import std.range : iota; Deque!int a; foreach (i; 0..100) { a.insertBack(i); } assert(equal(a[], iota(100))); } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; if (f.eof) return false; line = lineBuf[]; f.readln(line); } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { //string or char[10] etc //todo optimize auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { //static assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); fout.writeln("1 2 3"); fout.writeln("ab cde"); fout.writeln("1.0 1.0 2.0"); fout.close; Scanner sc = new Scanner(File(fileName, "r")); int a; int[2] b; char[2] c; string d; double e; double[] f; sc.read(a, b, c, d, e, f); assert(a == 1); assert(equal(b[], [2, 3])); assert(equal(c[], "ab")); assert(equal(d, "cde")); assert(e == 1.0); assert(equal(f, [1.0, 2.0])); } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File, writeln; import std.datetime; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); foreach (i; 0..1_000_000) { fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2); } fout.close; writeln("Scanner Speed Test(3*1,000,000 int)"); StopWatch sw; sw.start; Scanner sc = new Scanner(File(fileName, "r")); foreach (i; 0..500_000) { int a, b, c; sc.read(a, b, c); assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 500_000..700_000) { int[3] d; sc.read(d); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 700_000..1_000_000) { int[] d; sc.read(d); assert(d.length == 3); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } writeln(sw.peek.msecs, "ms"); }
D
import std.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 calc(int[] a) { int x = a[0]; int index = 0; for (int i = 0; i < a.length; i++) { if (abs(x) < abs(a[i])) { x = a[i]; index = i; } } int[][] ans; for (int i = 0; i < a.length; i++) { if (i == index) continue; ans ~= [index, i]; } if (a[index] >= 0) { // 左端から累積和をとる for (int i = 1; i < a.length; i++) { ans ~= [i - 1, i]; } } else { // 右端から累積和をとる for (int i = cast(int) a.length - 2; i >= 0; i--) { ans ~= [i + 1, i]; } } writeln(ans.length); foreach (e; ans) { writeln(e[0] + 1, " ", e[1] + 1); a[e[1]] += a[e[0]]; } } void main() { readint; auto a = readints; calc(a); }
D
import std.stdio; void main() { for(int i = 1; i < 10; i++) for(int j = 1; j < 10; j++) writeln(i,"x",j,"=",i*j); }
D
import std.stdio; void main(){ foreach(e1; 1..10){ foreach(e2; 1..10){ writeln(e1, "x", e2, "=", e1*e2); } } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; void main() { auto n = readln().chomp().to!int; int cnt4, cnt2, cnt1; foreach(e; readln().chomp().splitter(' ').map!(to!int)){ if(e % 4 == 0){ ++cnt4; }else if(e % 2 != 0){ ++cnt1; }else ++cnt2; } if(cnt2 != 0) writeln(cnt4 >= cnt1 ? "Yes" : "No"); else writeln(cnt4+1 >= cnt1 ? "Yes" : "No"); }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); } void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); } string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ int n = scan!int; int a = n / 500; int b = (n - 500 * a) / 5; int ans = a * 1000 + b * 5; ans.writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto D = RD; auto c = RDA; auto s = new long[][](D); foreach (i; 0..D) s[i] = RDA; auto last = new long[](26); foreach (i; 0..D) { long dec; foreach (j; 0..26) { dec += c[j] * (i - last[j]); } long best; long pos; foreach (j; 0..26) { auto x = s[i][j] + c[j] * (i - last[j]); if (x > best) { best = x; pos = j; } } last[pos] = i; writeln(pos+1); } stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.string, std.regex, std.math, std.array, std.algorithm; void main(){ auto ip = readln.split; int A = ip[0].to!int; int B = ip[2].to!int; char op = ip[1].to!char; if(op == '+'){ writeln(A + B); } else if(op == '-'){ writeln(A - B); } }
D
import std.stdio,std.array,std.conv,std.algorithm,std.range,std.string,std.math; void main(string[] args) { real a; a = readln.chomp.to!real; a.sqrt.floor.to!int.pow(2).writeln; }
D
import std.stdio,std.conv,std.string; /*int lyca(int n){ if(n==0) return 2; else if(n==1) return 1; else return lyca(n-1)+lyca(n-2); }*/ void main(){ int n=readln().chomp().to!int; ulong k=2UL,l=1UL,ans=1UL; for(int i=1;i<n;i++){ ans=k+l; //ansはl(i+1)に対応 k=l; l=ans; } writeln(ans); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.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 int[][](t); foreach (ti; 0..t) { auto n = RD!int; auto a = RDA!int; if (a[0]+a[1] > a[$-1]) continue; ans[ti] = [1, 2, n]; } foreach (e; ans) { if (e.empty) writeln(-1); else writeln(e[0], " ", e[1], " ", e[2]); } stdout.flush; debug readln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; immutable long MOD = 10^^9+7; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto p = s[1]; auto S = readln.chomp.to!(dchar[]); bool ans = false; for (int i = 0; i + p < N; ++i) { int j = i + p; if (S[i] == '.') { ans = true; if (S[i] == S[j]) { S[i] = '0'; S[j] = '1'; } else { S[i] = S[j] == '0' ? '1' : '0'; } } else if (S[j] == '.') { ans = true; S[j] = S[i] == '0' ? '1' : '0'; } else if (S[i] == '0' && S[j] == '1') { ans = true; } else if (S[i] == '1' && S[j] == '0') { ans = true; } } if (ans) { foreach (i; 0..N) if (S[i] == '.') S[i] = '0'; S.writeln; } else { writeln("No"); } }
D
/+ dub.sdl: name "C" dependency "dunkelheit" version=">=0.9.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; int main() { Scanner sc = new Scanner(stdin); scope(exit) sc.read!true; int n; long l; long[] c; sc.read(n, l, c); foreach_reverse (i; 0..n-1) { c[i] = min(c[i], c[i+1]); } foreach (i; 1..n) { c[i] = min(c[i], 2*c[i-1]); } long ans = 10L^^18; long nw = 0; foreach_reverse (i; 0..n) { long u = 2L ^^ i; if (u <= l) { long x = l / u; l -= u * x; nw += c[i] * x; } ans = min(ans, nw + c[i]); } writeln(min(ans, nw)); return 0; } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */ // module dkh.scanner; // import dkh.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(bool enforceEOF = false, T, Args...)(ref T x, auto ref Args args) { import std.exception; enforce(readSingle(x)); static if (args.length == 0) { enforce(enforceEOF == false || !succ()); } else { read!enforceEOF(args); } } void read(bool enforceEOF = false, Args...)(auto ref Args args) { import std.exception; static if (args.length == 0) { enforce(enforceEOF == false || !succ()); } else { enforce(readSingle(args[0])); read!enforceEOF(args); } } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
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 main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto g = RD!int; auto b = RD!int; long need = (n+1) / 2; long cnt = need / g * (g+b) - b; long r = need % g; if (r != 0) cnt += b + r; ans[ti] = max(n, cnt); } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio : write, writeln; import std.array; import std.range; import std.typecons; import std.bigint; import std.algorithm; void main(){ int n = next!int; int m = next!int; int cnt; for( int i = 1; i <= min(n, m); ++i ){ for( int j = i; j <= max(n, m); ++j ){ if( i!=1 && j>i ) continue; ++cnt; } } writeln = cnt; bool flag = n <= m; for( int i = 1; i <= min(n, m); ++i ){ for( int j = i; j <= max(n, m); ++j ){ if( i!=1 && j>i ) continue; if( flag ) writeln(i, " ", j); else writeln(j, " ", i); } } } import std.stdio : readln; import std.conv : to; import std.string : split, chomp; import std.traits; string[] input; string delim = " "; T next(T)() in { assert(hasNext()); } out { input.popFront; } body { return input.front.to!T; } T next( T : char )() in { assert(hasNext()); } out { input.front.popFront; } body { return input.front.front.to!T; } bool hasNext(){ if(input.length > 0){ if(input.front.length == 0){ input.popFront; return hasNext; } return true; } string str = readln; if(str.length > 0){ input ~= str.chomp.split(delim); return hasNext; }else{ return false; } } void dbg(T...)(T vs) { import std.stdio : stderr; foreach(v; vs) stderr.write(v.to!string ~ " "); stderr.write("\n"); } T clone(T)(T v){ T v_; static if(isInputRange!(T)){ foreach(ite; v){ v_ ~= ite.clone; } }else{ v_ = v; } return v_; }
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.traits; import core.bitop; import std.typecons; class LazyPropagationSegmentTree(T = int, D = int, D init = D.init) { immutable size_t n; immutable int h; T[] t; D[] d; abstract void calc (size_t p, int k); abstract void apply (size_t p, D value, int k); final void build (size_t l, size_t r) { int k = 2; for (l += n, r += n - 1; l > 1; k <<= 1) { l >>= 1; r >>= 1; foreach_reverse (i; l .. r + 1) { calc (i, k); } } } final void push (size_t l, size_t r) { int s = h, k = 1 << (h-1); for (l += n, r += n - 1; s > 0; --s, k >>= 1) { foreach (i; l >> s .. (r >> s) + 1) { immutable delta = d[i]; if (delta != init) { apply (i << 1, delta, k); apply ((i << 1) | 1, delta, k); d[i] = init; } } } } this (const T[] a) { n = a.length; h = bsr (n); t = uninitializedArray!(T[])(n); t ~= a; d = uninitializedArray!(D[])(n); d[] = init; build (0, n); } } class AssignSumLazyPropagationSegmentTree (T = int) : LazyPropagationSegmentTree!(T, T, T.min) { override void calc (size_t p, int k) { if (d[p] == T.min) t[p] = t[p<<1] + t[(p << 1) | 1]; else t[p] = d[p] * k; } override void apply (size_t p, T value, int k) { t[p] = value * k; if (p < n) d[p] = value; } final void assign (size_t l, size_t r, T value) { debug stderr.writefln ("assign (l:%d, r:%d, value:%d)", l, r, value); push (l, l + 1); push (r - 1, r); immutable l0 = l, r0 = r; int k = 1; for (l += n, r += n; l < r; l >>= 1, r >>= 1, k <<= 1) { if (l & 1) { apply (l++, value, k); } if (r & 1) { apply (--r, value, k); } } build (l0, l0 + 1); build (r0 - 1, r0); } final T sum (size_t l, size_t r) { push (l, l + 1); push (r - 1, r); T res; for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l & 1) { res += t[l++]; } if (r & 1) { res += t[--r]; } } return res; } this (T[] a) { super (a); } } 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; } } alias Q = Tuple!(int, int); void main() { auto r = new InputReader (); const nt = r.next!uint(); foreach (tid; 0 .. nt) { const n = r.next!uint(); const nq = r.next!uint(); r.seekByte(48); int[] a, b; foreach (i; 0 .. n) { const c = r.nextByte!false(); a ~= c.to!int - 48; } r.seekByte(48); foreach (i; 0 .. n) { const c = r.nextByte!false(); b ~= c.to!int - 48; } debug stderr.writeln(a); debug stderr.writeln(b); auto st = new AssignSumLazyPropagationSegmentTree!int(b); Q[] q; foreach (i; 0 .. nq) { const u = r.next!int - 1; const v = r.next!int; q ~= Q(u, v); } bool res = true; foreach_reverse (k, const w; q) { const l = w[1] - w[0]; const h = (l - 1) / 2; int t = st.sum(w[0], w[1]); debug stderr.writefln("l = %d, r = %d, sum = %d, h = %d, l = %d", w[0], w[1], t, h, l); if (t <= h) { st.assign(w[0], w[1], 0); } else if (t + h >= l) { st.assign(w[0], w[1], 1); } else { debug stderr.writefln("FAILED: l = %d, r = %d", w[0], w[1]); res = false; break; } } if (res) { foreach (i; 0 .. n) { if (st.sum(i, i + 1) != a[i]) { res = false; break; } } } if (res) write("YES\n"); else write("NO\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)); } 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 main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto d = RD!int; auto a = RDA; ans[ti] = a[0]; foreach (i; 1..n) { auto x = min(d/i, a[i]); ans[ti] += x; d -= i*x; } } 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.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto t = RD!int; auto ans = new long[](t); foreach (i; 0..t) { auto bpf = RDA; auto b = bpf[0]; auto hc = RDA; long cnt1, cnt2; if (hc[0] > hc[1]) { cnt1 = bpf[1]; cnt2 = bpf[2]; } else { cnt1 = bpf[2]; cnt2 = bpf[1]; } auto c1 = min(cnt1, b/2); b -= c1 * 2; ans[i] += c1 * max(hc[0], hc[1]); auto c2 = min(cnt2, b/2); ans[i] += c2 * min(hc[0], hc[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 t = RD!int; auto ans = new int[](t); foreach (ti; 0..t) { auto n = RD!int; auto a = RDA!int; int x = a[0]; bool ok; foreach (i; 0..n) { if (a[i] != x) { ok = true; break; } } ans[ti] = ok ? 1 : n; } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1] + 2; string[] A; bool ok = false; foreach (i; 0..N) { auto t = readln.chomp; if (!ok && t.map!(tt => tt == '0').all) continue; ok = true; A ~= t; } N = A.length.to!int; A.reverse(); int[] L = new int[](N); int[] R = new int[](N); foreach (i; 0..N) { L[i] = M-1, R[i] = 0; foreach (j; 0..M) { if (A[i][j] == '1') { L[i] = min(L[i], j); R[i] = max(R[i], j); } } } if (N == 0) { writeln(0); return; } else if (N == 1) { writeln(R[0]); return; } auto dp = new int[][](N, 2); dp[0][0] = R[0] * 2; dp[0][1] = M - 1; foreach (i; 1..N-1) { dp[i][0] = min(dp[i-1][0] + R[i] * 2 + 1, dp[i-1][1] + M); dp[i][1] = min(dp[i-1][1] + (M - L[i] - 1) * 2 + 1, dp[i-1][0] + M); } int ans = min(dp[N-2][0] + R[N-1] + 1, dp[N-2][1] + (M - L[N-1] - 1) + 1); ans.writeln; }
D
import std.algorithm, std.array, std.container, std.range, std.bitmanip; import std.numeric, std.math, std.bigint, std.random, core.bitop; import std.string, std.conv, std.stdio, std.typecons; void main() { while (true) { auto rd = readln.split.map!(to!int); auto x = rd[0], y = rd[1]; if (x == 0 && y == 0) break; if (x > y) swap(x, y); writeln(x, " ", y); } }
D
import std.stdio; immutable mod = 4294967311L; long[] inv_; void main(){ long x; int n, y, op; scanf("%d", &n); inv_ = new long[1_000_001]; inv_[1] = 1; foreach(i; 2..50) inv_[i]=mul(inv_[mod%i], mod-mod/i)%mod; foreach(_; 0..n){ scanf("%d%d", &op, &y); if(op >= 3 && y < 0) x = -x, y = -y; if(op == 1) x += y; else if(op == 2) x -= y; else if(op == 3) x *= y; else if(op == 4) x = mul(x, inv(y)); x %= mod; } if(x < 0) x += mod; if(x > int.max) x -= mod; writeln(x); } long inv(int x){ return inv_[x] ? inv_[x] : (inv_[x]=mod-(inv(mod%x)*(mod/x))%mod); } long mul(long x, long y){ return ((x*(y>>16)%mod)<<16) + (x*(y&0xffff)); }
D
import std.stdio; import std.algorithm; import std.math; import std.conv; import std.string; T readNum(T)(){ return readStr.to!T; } T[] readNums(T)(){ return readStr.split.to!(T[]); } string readStr(){ return readln.chomp; } void main(){ auto nm = readNums!int; int n = nm[0], m = nm[1]; bool[] broken = new bool[](n); foreach(i; 0 .. m){ broken[readNum!size_t - 1] = true; } ulong[] ans = new ulong[](n); if(!broken[0]) ans[0] = 1; if(!broken[1]) ans[1] = ans[0] + 1; foreach(i; 2 .. n){ if(!broken[i]) ans[i] = (ans[i-1] + ans[i-2]) % 1_000_000_007; } writeln(ans[$-1]); }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T read(T)(){ return read.to!T; } T[] read(T)(long n){ return n.iota.map!(_ => read!T).array; } T[][] read(T)(long m, long n){ return m.iota.map!(_ => read!T(n)).array; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ int n = read!int; string s = read!string; int best = 100_000; foreach(int i; 0 .. n - 3){ string q = s[i .. i + 4]; int x = diff(q[0], 'A') + diff(q[1], 'C') + diff(q[2], 'T') + diff(q[3], 'G'); if(x < best) best = x; } best.writeln; } int diff(char a, char b){ int res; if(a > b) res = min(a - b, b + 26 - a); else res = min(b - a, a + 26 - b); return res; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto X = s[1].to!long; auto A = readln.split.map!(to!long).array; auto mins = new long[][](N, N); foreach (i; 0..N) fill(mins[i], 1L << 59); foreach (i; 0..N) { mins[i][0] = A[i]; foreach (jj; 1..N) { int j = (i - jj + N) % N; mins[i][jj] = min(mins[i][jj-1], A[j]); } } long ans = 1L << 59; foreach (i; 0..N) { long tmp = 0; foreach (j; 0..N) tmp += mins[j][i]; ans = min(ans, tmp + i * X); } ans.writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto b = RD; auto g = RD; auto n = RD; auto x = max(b, g); auto y = min(b, g); writeln(min(x, n) - max(n-y, 0) + 1); stdout.flush(); debug readln(); }
D
import std.stdio; import std.range; import std.array; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.container; import std.typecons; import std.random; import std.csv; import std.regex; import std.math; import core.time; import std.ascii; import std.digest.sha; import std.outbuffer; import std.numeric; import std.bigint; import core.checkedint; import core.bitop; void main() { int n = readln.chomp.to!int; writeln = ["ABC", "ABD"][n / 1000]; } void tie(R, Args...)(R arr, ref Args args) if (isRandomAccessRange!R || isArray!R) in { assert (arr.length == args.length); } body { foreach (i, ref v; args) { alias T = typeof(v); v = arr[i].to!T; } } void verbose(Args...)(in Args args) { stderr.write("["); foreach (i, ref v; args) { if (i) stderr.write(", "); stderr.write(v); } stderr.writeln("]"); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string; immutable long MOD = 998244353; long powmod(long a, long x, long m) { long ret = 1; while (x) { if (x % 2) ret = ret * a % m; a = a * a % m; x /= 2; } return ret; } void solve() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto G = new int[][](N); foreach (_; 0..M) { s = readln.split.map!(to!int); G[s[0]-1] ~= s[1]-1; G[s[1]-1] ~= s[0]-1; } long ans = 1; auto cnt = new long[](2); auto C = new int[](N); fill(C, -1); bool dfs(int n, int c) { if (C[n] != -1) return C[n] == c; C[n] = c; cnt[c] += 1; foreach (m; G[n]) { if (C[m] != -1) { if (C[m] == (c^1)) continue; else return false; } if (!dfs(m, c^1)) return false; } return true; } foreach (i; 0..N) { if (C[i] != -1) continue; cnt[0] = cnt[1] = 0; if (dfs(i, 0)) { long tmp = powmod(2, cnt[0], MOD) + powmod(2, cnt[1], MOD); tmp %= MOD; ans = ans * tmp % MOD; } else { writeln(0); return; } } ans.writeln; } void main() { auto Q = readln.chomp.to!int; while (Q--) solve; }
D
import std.stdio; import std.array; import std.string; import std.conv; import std.algorithm; import std.typecons; import std.range; import std.random; import std.math; import std.container; import std.numeric; import std.bigint; class UnionFind { int N; int[] table; this(int n) { N = n; table = new int[](N); foreach(i; 0..N) table[i] = -1; } int find(int x) { return table[x] < 0 ? x : (table[x] = find(table[x])); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (table[x] < table[y]) swap(x, y); table[x] += table[y]; table[y] = x; } void group(ref int[][] g) { auto group = new int[][](N); foreach (i; 0..N) g[find(i)] ~= i; } } void main() { auto input = readln.split.map!(to!int); auto N = input[0]; auto M = input[1]; auto lang = new int[][](M+1); foreach (i; 0..N) { input = readln.split.map!(to!int); if (input[0] == 0) { writeln("NO"); return;} foreach (j; 1..input[0]+1) lang[input[j]] ~= i; } auto uf = new UnionFind(N); foreach (i; 1..M+1) foreach (j; 1..lang[i].length) uf.unite(lang[i][0], lang[i][j]); writeln(uf.table.map!(x => x < 0).sum == 1 ? "YES" : "NO"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string; void main() { auto sa = readln.chomp; auto sb = readln.chomp; auto sc = readln.chomp; char t = 'a'; for (;;) { switch (t) { case 'a': if (sa.empty) { writeln("A"); return; } t = sa[0]; sa = sa[1..$]; break; case 'b': if (sb.empty) { writeln("B"); return; } t = sb[0]; sb = sb[1..$]; break; case 'c': if (sc.empty) { writeln("C"); return; } t = sc[0]; sc = sc[1..$]; break; default: return; } } }
D
import std.stdio, std.array, std.conv, std.typecons, std.algorithm; T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; } void main() { immutable ip = readln.split.to!(ulong[]); immutable a = ip[0], p = ip[1]; writeln((a*3+p)/2); }
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; T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } alias sread = () => readln.chomp(); void main() { long N = lread(); static immutable dp = () { enum Nmax = 10 ^^ 5; auto dp = new byte[](Nmax + 1); dp[] = byte.max; dp[0] = 0; foreach (i; 0 .. Nmax) if (dp[i] != byte.max) { dp[i + 1].minAssign(cast(ubyte) dp[i] + 1); size_t j = 6; while (i + j < dp.length) { dp[i + j].minAssign(cast(ubyte) dp[i] + 1); j *= 6; } j = 9; while (i + j < dp.length) { dp[i + j].minAssign(cast(ubyte) dp[i] + 1); j *= 9; } } return dp; }(); dp[N].writeln(); }
D
import std.stdio, std.string, std.conv; import std.algorithm, std.array; auto solve(string s_) { immutable P = "Possible"; immutable I = "Impossible"; immutable N = s_.to!int(); immutable a = readln.split.map!(to!int).array(); auto c = new int[N]; foreach(v;a) { if(v<1 || N<=v) return I; ++c[v]; } int n=0, d; foreach(int i,v;c) { if(n==0) { if(v>2) return I; d=i-(v-1); } else { if(v<2) return I; if(--d<0) return I; } n+=v; if(n==N && d==0) return P; } return I; } void main(){ for(string s; (s=readln.chomp()).length;) writeln(solve(s)); }
D
import std.stdio, std.conv, std.string, std.random, std.range, std.typecons, std.math; import std.algorithm.comparison, std.algorithm.iteration; import std.algorithm.mutation, std.algorithm.searching, std.algorithm.sorting; void main() { auto input = readln().strip.split.to!(int[]); auto A = input[0]; auto B = input[1]; auto C = input[2]; writeln(min(B / A, C)); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto h = readln.chomp.to!size_t; auto hi = readln.split.to!(int[]); hi = 0 ~ hi; buildMaxHeap(hi, h); foreach (i; 1..h+1) write(" ", hi[i]); writeln; } void maxHeapify(ref int[] ai, size_t h, size_t i) { auto l = i * 2, r = l + 1; size_t largest; if (l <= h && ai[l] > ai[i]) largest = l; else largest = i; if (r <= h && ai[r] > ai[largest]) largest = r; if (largest != i) { swap(ai[i], ai[largest]); maxHeapify(ai, h, largest); } } void buildMaxHeap(ref int[] ai, size_t h) { foreach_reverse(i; 1..h/2+1) maxHeapify(ai, h, i); }
D
unittest { assert( [ "123456789" ].solve == "Yes" ); assert( [ "0" ].solve == "Yes" ); assert( [ "31415926535897932384626433832795028841971693993751058209749445923078164062862089986280" ].solve == "No" ); } import std.algorithm; import std.conv; import std.range; import std.stdio; void main() { stdin.byLineCopy.solve.writeln; } auto solve( Range )( Range input ) if( isInputRange!Range && is( ElementType!Range == string ) ) { auto n = input.front.map!( ( a ) => ( a - '0' ).to!( long ) ); return ( n.sum % 9 == 0 ) ? "Yes" : "No"; }
D
import std.stdio, std.string, std.conv, std.algorithm; void main() { int[] tmp = readln.split.to!(int[]); int n = tmp[0], a = tmp[1], b = tmp[2]; writeln(min(n*a, b)); }
D
import std.stdio; import std.string; import std.format; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.concurrency; import std.traits; import std.uni; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; enum long INF = long.max/3; enum long MOD = 10L^^9+7; void main() { bool[] as = readln.chomp.map!"a=='1'".array; auto stack = DList!bool(); long ans = 0; foreach(a; as) { if (!stack.empty) { long b = stack.back; if (a^b) { stack.removeBack; ans+=2; continue; } } stack.insertBack(a); } ans.writeln; } // ---------------------------------------------- void times(alias fun)(long n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(long n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { // `(x+y-1)/y` will only work for positive numbers ... T t = x / y; if (t * y < x) t++; return t; } T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { T t = x / y; if (t * y > x) t--; return t; } ref T ch(alias fun, T, S...)(ref T lhs, S rhs) { return lhs = fun(lhs, rhs); } unittest { long x = 1000; x.ch!min(2000); assert(x == 1000); x.ch!min(3, 2, 1); assert(x == 1); x.ch!max(100).ch!min(1000); // clamp assert(x == 100); x.ch!max(0).ch!min(10); // clamp assert(x == 10); } mixin template Constructor() { import std.traits : FieldNameTuple; this(Args...)(Args args) { // static foreach(i, v; args) { foreach(i, v; args) { mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v; } } } void scanln(Args...)(auto ref Args args) { import std.meta; template getFormat(T) { static if (isIntegral!T) { enum getFormat = "%d"; } else static if (isFloatingPoint!T) { enum getFormat = "%g"; } else static if (isSomeString!T || isSomeChar!T) { enum getFormat = "%s"; } else { static assert(false); } } enum string fmt = [staticMap!(getFormat, Args)].join(" "); string[] inputs = readln.chomp.split; foreach(i, ref v; args) { v = inputs[i].to!(Args[i]); } } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { private template RebindableOrUnqual(T) { static if (is(T == class) || is(T == interface) || isDynamicArray!T || isAssociativeArray!T) alias RebindableOrUnqual = Rebindable!T; else alias RebindableOrUnqual = Unqual!T; } private auto extremum(alias map, alias selector = "a < b", Range)(Range r) if (isInputRange!Range && !isInfinite!Range && is(typeof(unaryFun!map(ElementType!(Range).init)))) in { assert(!r.empty, "r is an empty range"); } body { alias Element = ElementType!Range; RebindableOrUnqual!Element seed = r.front; r.popFront(); return extremum!(map, selector)(r, seed); } private auto extremum(alias map, alias selector = "a < b", Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seedElement) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void) && is(typeof(unaryFun!map(ElementType!(Range).init)))) { alias mapFun = unaryFun!map; alias selectorFun = binaryFun!selector; alias Element = ElementType!Range; alias CommonElement = CommonType!(Element, RangeElementType); RebindableOrUnqual!CommonElement extremeElement = seedElement; // if we only have one statement in the loop, it can be optimized a lot better static if (__traits(isSame, map, a => a)) { // direct access via a random access range is faster static if (isRandomAccessRange!Range) { foreach (const i; 0 .. r.length) { if (selectorFun(r[i], extremeElement)) { extremeElement = r[i]; } } } else { while (!r.empty) { if (selectorFun(r.front, extremeElement)) { extremeElement = r.front; } r.popFront(); } } } else { alias MapType = Unqual!(typeof(mapFun(CommonElement.init))); MapType extremeElementMapped = mapFun(extremeElement); // direct access via a random access range is faster static if (isRandomAccessRange!Range) { foreach (const i; 0 .. r.length) { MapType mapElement = mapFun(r[i]); if (selectorFun(mapElement, extremeElementMapped)) { extremeElement = r[i]; extremeElementMapped = mapElement; } } } else { while (!r.empty) { MapType mapElement = mapFun(r.front); if (selectorFun(mapElement, extremeElementMapped)) { extremeElement = r.front; extremeElementMapped = mapElement; } r.popFront(); } } } return extremeElement; } private auto extremum(alias selector = "a < b", Range)(Range r) if (isInputRange!Range && !isInfinite!Range && !is(typeof(unaryFun!selector(ElementType!(Range).init)))) { return extremum!(a => a, selector)(r); } // if we only have one statement in the loop it can be optimized a lot better private auto extremum(alias selector = "a < b", Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seedElement) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void) && !is(typeof(unaryFun!selector(ElementType!(Range).init)))) { return extremum!(a => a, selector)(r, seedElement); } auto minElement(alias map = (a => a), Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { return extremum!map(r); } auto minElement(alias map = (a => a), Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void)) { return extremum!map(r, seed); } auto maxElement(alias map = (a => a), Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { return extremum!(map, "a > b")(r); } auto maxElement(alias map = (a => a), Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void)) { return extremum!(map, "a > b")(r, seed); } } // popcnt with ulongs was added in D 2.071.0 static if (__VERSION__ < 2071) { ulong popcnt(ulong x) { x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L); x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L); x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL); x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL); x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL); x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL); return x; } }
D
import 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; } } long disit_sum(long a) { long sum,disit_num; foreach(disit;0 .. 9) { disit_num = a / pow(10, disit); sum += disit_num % 10; } return sum; } void main() { long n = lread(); auto value = aryread(); auto cost = aryread(); foreach(i; 0 .. n) { value[i] -= cost[i]; } long max_gets; foreach(i; 0 .. n) { if(value[i] > 0) { max_gets += value[i]; } } writeln(max_gets); }
D
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.typecons, std.range; void main(){ auto hwd=readln.split.map!(to!int).array; auto H=hwd[0],W=hwd[1],d=hwd[2]; foreach(w;0..H){ foreach(h;0..W){ auto k=w+h; auto j=w-h; auto x=k.div(d); auto y=j.div(d); draw(x.mod(2)+2*y.mod(2)); } writeln(); } } int mod(int a,int d){ if(a>=0)return a%d; return (a%d+d)%d; } int div(int a,int d){ return (a-a.mod(d))/d; } void draw(int i){ "RYGB"[i].write; }
D
import std.stdio, std.conv, std.string; void main() { auto D = readln().strip.to!int; switch(D) { case 25: writeln("Christmas"); break; case 24: writeln("Christmas Eve"); break; case 23: writeln("Christmas Eve Eve"); break; case 22: writeln("Christmas Eve Eve Eve"); break; default: break; } }
D
import std.stdio, std.string, std.array, std.conv; void main() { while (true) { int[] tmp = readln.chomp.split.to!(int[]); int h = tmp[0], w = tmp[1]; if (h == 0 && w == 0) break; foreach (i; 0 .. h) { foreach (j; 0 .. w) { write(i == 0 || i == h - 1 || j == 0 || j == w - 1 ? "#" : "."); } writeln; } writeln; } }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional, std.traits; import std.algorithm, std.container; import core.stdc.stdlib; //C // void main() // { // auto N = scanElem; // auto K = scanElem; // auto k = K; // long count=1; // real res=0; // for(long i=N;i>=1;i--) // { // while(i<=k){ // count*=2; // k/=2; // } // res+=1f/count; // writeln(count); // } // writefln("%f20", res/N); // } struct Node{ long connect; long cost; } long[] res; Node[][] nodes; bool[] toutatu; void main() { auto N = scanElem; nodes.length = N+1; res.length = N+1; toutatu.length = N+1; foreach(i; 0..N-1) { auto a = scanElem; auto b = scanElem; auto cost = scanElem; nodes[a] ~= Node(b, cost); nodes[b] ~= Node(a, cost); } calc(1, 0); foreach(x; res[1..$]) { writeln(x%2); } } void calc(long index, long cost) { if(toutatu[index])return; res[index]=cost; toutatu[index] = true; foreach(node; nodes[index]){ calc(node.connect, cost+node.cost); } } long gcd(long a, long b) { if(b == 0) return a; return gcd(b, a % b); } class UnionFind{ UnionFind parent = null; void merge(UnionFind a) { if(same(a)) return; a.root.parent = this.root; } UnionFind root() { if(parent is null)return this; return parent = parent.root; } bool same(UnionFind a) { return this.root == a.root; } } void scanValues(TList...)(ref TList list) { auto lit = readln.splitter; foreach (ref e; list) { e = lit.fornt.to!(typeof(e)); lit.popFront; } } T[] scanArray(T = long)() { return readln.split.to!(T[]); } void scanStructs(T)(ref T[] t, size_t n) { t.length = n; foreach (ref e; t) { auto line = readln.split; foreach (i, ref v; e.tupleof) { v = line[i].to!(typeof(v)); } } } long scanULong(){ long x; while(true){ const c = getchar; if(c<'0'||c>'9'){ break; } x = x*10+c-'0'; } return x; } T scanElem(T = long)() { char[] res; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { res ~= cast(char) c; c = getchar; } return res.strip.to!T; } 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 { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } struct Factor { long n; long c; } Factor[] factors(long n) { Factor[] res; for (long i = 2; i ^^ 2 <= n; i++) { if (n % i != 0) continue; int c; while (n % i == 0) { n = n / i; c++; } res ~= Factor(i, c); } if (n != 1) res ~= Factor(n, 1); return res; } long[] primes(long n) { if(n<2)return []; auto table = new long[n+1]; long[] res; for(int i = 2;i<=n;i++) { if(table[i]==-1) continue; for(int a = i;a<table.length;a+=i) { table[a] = -1; } res ~= i; } return res; } bool isPrime(long n) { if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (long i = 3; i ^^ 2 <= n; i += 2) if (n % i == 0) return false; return true; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; long[] as = [0]; foreach (i, a; readln.split.to!(long[])) as ~= a + as[i]; auto pq_max = new long[](N+1); auto pq_min = new long[](N+1); auto rs_max = new long[](N+1); auto rs_min = new long[](N+1); int i = 2, j = 1; while (i <= N) { while (j+1 < i && abs(as[j]*2 - as[i]) > abs(as[j+1]*2 - as[i])) ++j; pq_max[i] = max(as[j], as[i]-as[j]); pq_min[i] = min(as[j], as[i]-as[j]); ++i; } i = N-2, j = N-1; while (i >= 0) { while (j-1 > i && abs(as[j]*2 - as[i] - as[N]) > abs(as[j-1]*2 - as[i] - as[N])) --j; rs_max[i] = max(as[j]-as[i], as[N]-as[j]); rs_min[i] = min(as[j]-as[i], as[N]-as[j]); --i; } long r = long.max; foreach (k; 2..N-1) { r = min(r, max(pq_max[k], rs_max[k]) - min(pq_min[k], rs_min[k])); } writeln(r); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto A = RD; auto B = RD; auto S = RD!string; bool ans = true; foreach (i, c; S) { debug writeln(i == A ? c : c - '0'); if (i == A) { if (c != '-') ans = false; debug writeln("a"); } else if (c - '0' < 0 || c - '0' > 9) { ans = false; debug writeln("b"); } } writeln(ans ? "Yes" : "No"); stdout.flush(); debug readln(); }
D
// Vicfred // https://atcoder.jp/contests/abc158/tasks/abc158_d import std.algorithm; import std.array; import std.stdio; import std.conv; import std.string; import std.container; void main() { string s = readln.chomp; int q = readln.chomp.to!int; int[] t = new int[q]; int[] f = new int[q]; char[] c = new char[q]; foreach(i; 0..q) { string[] line = readln.split.map!(to!string).array; t[i] = to!int(line[0]); if(t[i] == 2) { f[i] = to!int(line[1]); c[i] = to!char(line[2]); } } auto str = DList!char(); foreach(ch; s) str.insertBack(ch); bool reversed = false; for(int i = 0; i < q; ++i) { if(t[i] == 1) { reversed = !reversed; } else if(f[i] == 1) { if(!reversed) str.insertFront(c[i]); else str.insertBack(c[i]); } else { if(!reversed) str.insertBack(c[i]); else str.insertFront(c[i]); } } if(reversed) str[].reverse; foreach(ch; str) ch.write; writeln; }
D
import std.conv; import std.stdio; import std.string; void main() { int n = readln.chomp.to!int; writeln(n % 2 == 0 ? n : 2 * n); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto x = readln.chomp.to!int; writeln(x ^^ 3); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } int calc(int[] xs) { int rec(int p, int[] buf) { if (p == buf.length) { return buf.any!(e => e % 2 == 0) ? 1 : 0; } int ans = 0; for (int i = -1; i <= 1; i++) { buf[p] = xs[p] + i; ans += rec(p + 1, buf); } return ans; } auto buf = new int[xs.length]; return rec(0, buf); } void main() { readint; auto xs = readints; writeln(calc(xs)); }
D
import std.stdio, std.conv, std.string, std.algorithm; void main() { int H, W; int [][10] c; auto t = readln().chomp().split().to!(int[])(); H = t[0]; W = t[1]; foreach(int i; 0..10) { c[i] = readln().chomp().split().to!(int[])(); } foreach(int i; 0..10) { foreach(int j; 0..10) { foreach(int k; 0..10) { c[j][k] = min(c[j][k], c[j][i]+c[i][k]); } } } long res = 0; foreach(int i; 0..H) { t = readln().chomp().split().to!(int[])(); foreach(int j; 0..W) { if(t[j] >= 0) { res += c[t[j]][1]; } } } writeln(res); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void main() { int n, m; readV(n, m); auto t = 1900*m + 100*(n-m); writeln(t*2^^m); }
D
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array, std.functional, std.container, std.typecons; void main() { string S = readln.chomp; switch(S) { case "SUN": 7.writeln; break; case "MON": 6.writeln; break; case "TUE": 5.writeln; break; case "WED": 4.writeln; break; case "THU": 3.writeln; break; case "FRI": 2.writeln; break; case "SAT": 1.writeln; break; default: break; } }
D
void main() { auto N = ri; auto S = rs; ulong res = ulong.max; auto A = new int[](N); A[0] = S[0] == 'W'; foreach(i; 1..N) A[i] = S[i] == 'W' ? A[i-1]+1 : A[i-1]; /*auto B = new int[](N); [0] = S[0] == 'E'; foreach(i; 1..N) B[i] = S[i] == 'E' ? B[i-1]+1 : B[i-1]; */ debug A.writeln; //B.writeln; auto C = iota(1, N+1).map!(a => a - A[a-1]).array; debug C.writeln; foreach(i; 0..N) { ulong tmp; tmp += C[$-1] - C[i]; tmp += i < 1 ? 0 : A[i-1]; debug tmp.writeln; res = min(res, tmp); } res.writeln; } // =================================== import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
void main(){ string s = readln().chomp(); foreach(ch; s){ if( s.count(ch) != 2){ writeln("No"); return; } } writeln("Yes"); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; int[][] MAX, MEMO; void main() { auto s = readln.chomp.to!(wchar[]); auto t = readln.chomp.to!(wchar[]); MEMO.length = s.length; MAX.length = s.length; foreach (i; 0..s.length) { MEMO[i].length = t.length; MAX[i].length = t.length; MAX[i][] = -1; } int solve(int i, int j) { if (i == s.length || j == t.length) return 0; if (MAX[i][j] != -1) return MAX[i][j]; auto r1 = s[i] == t[j] ? solve(i+1, j+1) + 1 : -1; auto r2 = solve(i+1, j); auto r3 = solve(i, j+1); auto max_r = max(r1, r2, r3); if (max_r == r1) { return MAX[i][j] = r1; } else if (max_r == r2) { MEMO[i][j] = 1; return MAX[i][j] = r2; } else { MEMO[i][j] = 2; return MAX[i][j] = r3; } } solve(0, 0); wchar[] answer(int i, int j) { if (i == s.length || j == t.length) return []; switch (MEMO[i][j]) { case 0: return s[i] ~ answer(i+1, j+1); case 1: return answer(i+1, j); case 2: return answer(i, j+1); default: return []; } } writeln(answer(0, 0)); }
D
import std.stdio; import std.string; import std.format; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.concurrency; import std.traits; import std.uni; import std.regex; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; enum long INF = long.max/5; enum long MOD = 10L^^9+7; void main() { string s = readln.chomp; bool ok = s.length%2 == 0; foreach(i; 0..s.length/2) { if (!ok) break; ok &= s[i*2..i*2+2] == "hi"; } writeln(ok ? "Yes" : "No"); } // ---------------------------------------------- void times(alias fun)(long n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(long n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { // `(x+y-1)/y` will only work for positive numbers ... T t = x / y; if (y > 0 && t * y < x) t++; if (y < 0 && t * y > x) t++; return t; } T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { T t = x / y; if (y > 0 && t * y > x) t--; if (y < 0 && t * y < x) t--; return t; } ref T ch(alias fun, T, S...)(ref T lhs, S rhs) { return lhs = fun(lhs, rhs); } unittest { long x = 1000; x.ch!min(2000); assert(x == 1000); x.ch!min(3, 2, 1); assert(x == 1); x.ch!max(100).ch!min(1000); // clamp assert(x == 100); x.ch!max(0).ch!min(10); // clamp assert(x == 10); } mixin template Constructor() { import std.traits : FieldNameTuple; this(Args...)(Args args) { // static foreach(i, v; args) { foreach(i, v; args) { mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v; } } } void scanln(Args...)(auto ref Args args) { enum sep = " "; enum n = Args.length; enum fmt = n.rep!(()=>"%s").join(sep); string line = readln.chomp; static if (__VERSION__ >= 2074) { line.formattedRead!fmt(args); } else { enum argsTemp = n.iota.map!( i => "&args[%d]".format(i) ).join(", "); mixin( "line.formattedRead(fmt, " ~ argsTemp ~ ");" ); } } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // popcnt with ulongs was added in D 2.071.0 static if (__VERSION__ < 2071) { ulong popcnt(ulong x) { x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L); x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L); x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL); x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL); x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL); x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL); return x; } }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; void main() { while(true) { int N = readln.chomp.to!int; if (N == 0) break; long[] t = new long[N]; foreach(int i; 0..N) { t[i] = readln.chomp.to!long; } t.sort; long ans = 0; long sum = 0; foreach(int i; 0..N) { ans += sum; sum += t[i]; } ans.writeln; } }
D