code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio; int main(){ int n; long c1 , c2; scanf("%d %lld %lld" , &n , &c1 , &c2); char arr; int zeroes = 0; int ones = 0; for(int i = 0 ; i < n ; ++i){ scanf(" %c" , &arr); if(arr == '0'){ ++zeroes; } else{ ++ones; } } long ans = 800000000000000000L; for(long i = 1 ; i <= ones ; ++i){ long res = 0L; res += 1L * c1 * i; long small = n / i; long large = small + 1L; long largecnt = n % i; long smallcnt = i - largecnt; res += 1L * c2 * (small - 1L) * (small - 1L) * smallcnt; res += 1L * c2 * (large - 1L) * (large - 1L) * largecnt; if(res < ans){ ans = res; } } printf("%lld" , ans); return 0; }
D
import std.algorithm; import std.array; import std.conv; import std.range; import std.stdio; import std.string; import std.typecons; bool ask (long value) { writeln ("? ", value); stdout.flush (); return readln.strip == "Y"; } void answer (long value) { writeln ("! ", value); stdout.flush (); } void main () { long lo = 1; bool rem = false; while (true) { auto x = ask (lo); auto y = ask (lo * 10 - 1); if (x && !y) { rem = true; } if (x && y && rem) { break; } if (!x && y) { lo /= 10; break; } if (lo >= int.max * 10L) { lo = 1; break; } lo *= 10; } long hi = lo * 10 - 1; while (lo < hi) { long me = (lo + hi) / 2; if (ask (me * 10)) { hi = me; } else { lo = me + 1; } } answer (lo); }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void calc(int[] a) { int n = cast(int)a.length; auto b = new int[n + 1]; for (int i = n; i >= 1; i--) { // i の倍数の箱に入っているボールをカウントする int s = 0; for (int j = i + i; j <= n; j += i) { s += b[j]; } // 偶奇が一致するように i 番目の箱にボールを入れるか判定 b[i] = a[i-1] ^ (s % 2); } int t = b.sum; writeln(t); if (t > 0) { for (int i = 1; i <= n; i++) { if (b[i]) write(i, " "); } writeln(); } } void main() { readint; auto a = readints; calc(a); }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; alias Edge = Tuple!(int, "u", int, "v"); void main() { int N, M; scan(N, M); auto es = new Edge[](M); foreach (i ; 0 .. M) { int ai, bi; scan(ai, bi); ai--, bi--; es[i] = Edge(ai, bi); } int ans; foreach (i ; 0 .. M) { auto uf = UnionFind(N); foreach (j ; 0 .. M) { if (j == i) continue; uf.unite(es[j].u, es[j].v); } bool is_bridge = 0; foreach (j ; 0 .. N) { if (!uf.same(0, j)) is_bridge = 1; } if (is_bridge) ans++; } writeln(ans); } struct UnionFind { private { int N; int[] p; int[] rank; } this (int n) { N = n; p = iota(N).array; rank = new int[](N); } int find_root(int x) { if (p[x] != x) { p[x] = find_root(p[x]); } return p[x]; } bool same(int x, int y) { return find_root(x) == find_root(y); } void unite(int x, int y) { int u = find_root(x), v = find_root(y); if (u == v) return; if (rank[u] < rank[v]) { p[u] = v; } else { p[v] = u; if (rank[u] == rank[v]) { rank[u]++; } } } } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.15f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } struct UnionFind { void init(int n) { par = new int[](n); foreach (i; 0..n) par[i] = i; cnt = new int[](n); fill(cnt, 1); } int root(int i) { return par[i] == i ? i : (par[i] = root(par[i])); } bool same(int i, int j) { return root(i) == root(j); } void unite(int i, int j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; cnt[j] += cnt[i]; } int size(int i) { return cnt[root(i)]; } int[] par, cnt; } void main() { auto N = RD; auto M = RD; bool[long] set; UnionFind uf; uf.init(cast(int)M); foreach (i; 0..N) { auto KL = RDA(-1); auto from = KL[1]; foreach (to; KL[1..$]) { set[to] = true; uf.unite(from, to); } } bool ans = true; auto cnt = set.keys.length; debug writeln(cnt); foreach (key; set.keys) { if (uf.size(cast(int)key) != cnt) { debug writeln(key); ans = false; } } writeln(ans ? "YES" : "NO"); stdout.flush(); debug readln(); }
D
import std.stdio, std.string, std.conv; void main() { const tmp = readln.split.to!(int[]); const N = tmp[0]; const X = tmp[1]; const Ls = readln.split.to!(int[]); int p = 0; int cnt; foreach (i; 0..N+1) { if (p > X) break; p += Ls[i]; cnt++; } writeln(cnt); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { long n, h, w; scan(n, h, w); long[] len = new long[n]; long[] side = new long[n]; long[] ans = new long[n]; foreach (e; 0 .. n) { scan(len[e], side[e]); } foreach (ref e; len) { if (e < h) { e = 0; } } foreach (ref e; side) { if (e < w) { e = 0; } } foreach (i, ref e; ans) { e = len[i] && side[i]; } ans.sum.writeln(); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } enum MOD = (10 ^^ 9) + 7; void main() { long N, X; scan(N, X); auto A = aryread(); long ans; if (X < A[0]) { ans = A[0] - X; A[0] = X; } // writeln(A); foreach (i; 0 .. N - 1) { if (X < A[i] + A[i + 1]) { long s = A[i + 1] + A[i] - X; // writeln(s); A[i + 1] -= s; ans += s; } } writeln(ans); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { auto dp = new long[][](100 + 1, (10 ^^ 5) + 1); long N, W; scan(N, W); auto w = new long[](100 + 1); auto v = new long[](100 + 1); foreach (i; 0 .. N) scan(w[i], v[i]); foreach (n; 0 .. N) foreach (m; 0 .. W + 1) { dp[n + 1][m].maxAssign(dp[n][m]); if (m + w[n] <= W) { dp[n + 1][m + w[n]].maxAssign(dp[n][m] + v[n]); } } dp[N][W].writeln(); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.to!(int[]); } int calc(int[][] xs) { int n = cast(int) xs[0].length; int ans = 0; for (int i = 0; i < n; i++) { int x = 0; for (int j = 0; j < n; j++) { if (j < i) x += xs[0][j]; if (j == i) x += xs[0][j] + xs[1][j]; if (j > i) x += xs[1][j]; } ans = max(ans, x); } return ans; } void main() { readint; int[][] xs; xs ~= readints; xs ~= readints; int ans = calc(xs); writeln(ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto S = readln.chomp; auto len = S.length; int c; foreach (i; 0..len/2) { if (S[i] != S[len-i-1]) ++c; } writeln(c); }
D
void main() { string s = rdStr; writeln(s[2] == s[3] && s[4] == s[5] ? "Yes" : "No"); } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } enum MOD = (10 ^^ 9) + 7; void main() { long N = lread(); if (N == 6) { writeln(1); return; } long x = (N * 2 + 10) / 11; if (5 <= x * 11 / 2 - N) { x--; } writeln(x); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int N; string S; scan(N); scan(S); auto rh = RollingHash(S); int ans; foreach (i ; 0 .. N) { foreach (j ; i .. N) { while (ans < j - i && j + ans < N && rh.slice(i, i + ans + 1) == rh.slice(j, j + ans + 1)) ans++; } } writeln(ans); } struct RollingHash { enum MAX_LENGTH = 500000; enum ulong MASK30 = (1UL << 30) - 1; enum ulong MASK31 = (1UL << 31) - 1; enum ulong MOD = (1UL << 61) - 1; enum ulong POSITIVIZER = MOD * ((1UL << 3) - 1); uint base; ulong[] basePow = new ulong[](MAX_LENGTH + 1); ulong[] hash; this(string s) { auto rnd = Random(unpredictableSeed); base = uniform(129, int.max, rnd).to!uint; basePow[0] = 1; foreach (i ; 1 .. MAX_LENGTH + 1) { basePow[i] = calcMod(mul(basePow[i - 1], base)); } hash = new ulong[](s.length + 1); foreach (i ; 0 .. s.length) { hash[i + 1] = calcMod(mul(hash[i], base) + s[i]); } } // hash(s[begin .. end]) ulong slice(int begin, int end) { return calcMod(hash[end] + POSITIVIZER - mul(hash[begin], basePow[end - begin])); } ulong mul(ulong l, ulong r) { auto lu = l >> 31; auto ld = l & MASK31; auto ru = r >> 31; auto rd = r & MASK31; auto middleBit = ld * ru + lu * rd; return ((lu * ru) << 1) + ld * rd + ((middleBit & MASK30) << 31) + (middleBit >> 30); } ulong mul(ulong l, uint r) { auto lu = l >> 31; auto rd = r & MASK31; auto middleBit = lu * rd; return (l & MASK31) * rd + ((middleBit & MASK30) << 31) + (middleBit >> 30); } ulong calcMod(ulong val) { val = (val & MOD) + (val >> 61); if (val >= MOD) val -= MOD; return val; } } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int[] a; readA(3, a); auto r = int.max; foreach (p; iota(3).permutations) r = min(r, (a[p[1]]-a[p[0]]).abs + (a[p[2]]-a[p[1]]).abs); writeln(r); }
D
import std; void main() { char[] s, t; scan(s); scan(t); int n = s.length.to!int; int m = t.length.to!int; if (n < m) { writeln("UNRESTORABLE"); return; } char[] ans = new char[](n); bool exist; foreach_reverse (i; 0 .. n - m + 1) { if (match(s[i .. i + m], t)) { foreach (j; 0 .. n) { if (i <= j && j < i + m) { ans[j] = t[j - i]; } else { ans[j] = s[j]; } if (ans[j] == '?') { ans[j] = 'a'; } } exist = true; break; } } if (exist) { writeln(ans); } else { writeln("UNRESTORABLE"); } } bool match(char[] s, char[] t) { foreach (i; 0 .. s.length) { if (s[i] == '?') { continue; } if (s[i] != t[i]) { return false; } } return true; } void scan(T...)(ref T args) { auto line = readln.split; // @suppress(dscanner.suspicious.unmodified) foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto S = readln.chomp; long solve(int i, long n, long s) { if (i == S.length) { return n + s; } else { return solve(i+1, S[i]-'0', n+s) + solve(i+1, n*10+S[i]-'0', s); } } writeln(solve(1, S[0]-'0', 0)); }
D
import std.stdio, std.conv, std.string, std.array, std.algorithm, std.math, std.regex; void main(){ auto ip = readln.split.to!(int[]); writeln(ip.count(5) == 2 && ip.count(7) == 1 ? "YES" : "NO"); }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { int n; scan(n); auto x = new long[](n + 1); auto s = new int[](n + 1); foreach (i ; 1 .. n + 1) { scan(x[i], s[i]); } auto p = new long[](n + 1); p[1] = s[1]; foreach (i ; 2 .. n + 1) { p[i] = p[i-1] + s[i] - (x[i] - x[i-1]); } auto pm = new long[](n + 1); pm[n] = p[n]; foreach_reverse (i ; 1 .. n) { pm[i] = max(p[i], pm[i+1]); } long ans = pm[1]; long d; foreach (i ; 2 .. n+1) { d += (x[i]-x[i-1]) - s[i-1]; ans = max(ans, pm[i] + d); } writeln(ans); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
void main() { problem(); } void problem() { auto N = scan!int; auto STONES = scan!string.to!(char[]); long solve() { long left = 0; long right = N-1; long ans; while(left <= right) { if (STONES[right] != 'R') { right--; continue; } if (STONES[left] != 'W') { left++; continue; } STONES.deb; [ans, left, right].deb; STONES[left] = 'R'; STONES[right] = 'W'; ans++; } return ans; } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
import std.stdio; 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() { long X, A, B; scanln(X); scanln(A); scanln(B); writeln(X-A - (X-A)/B*B); } // ---------------------------------------------- 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 std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto rd = readln.split.map!(to!size_t), n = rd[0], m = rd[1]; auto aij = n.iota.map!(_ => readln.split.map!(to!int)).array; auto bi = m.iota.map!(_ => readln.chomp.to!int).array; auto ci = new int[](n); foreach (i; 0..n) ci[i] = iota(m).map!(j => aij[i][j] * bi[j]).sum; ci.each!(writeln); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.typecons; import std.numeric, std.math; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto S = RD!string; long[] num; foreach (i; 0..S.length) { num ~= S[i..i+1].to!long; } long ans; foreach (i; 0..2^^(num.length-1)) { long begin; auto x = num.dup; foreach (j; 0..num.length-1) { auto bit = 1LU << j; if (i & bit) { begin = j+1; } else { foreach (k; begin..j+1) { x[k] *= 10; } } } foreach (e; x) ans += e; } writeln(ans); stdout.flush(); //readln(); }
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(); deq.insertBack(1); deq.removeBack(); auto p = deq.p; foreach (int ph; 2..k+1) { ndp[] = -(10L^^18); p.clear(); foreach (int i; 0..n) { if (deq.length && deq[0] == i-m) { deq.removeFront(); } if (deq.length) { ndp[i] = dp[deq[0]] + 1L * ph * a[i]; } while (deq.length && dp[deq[deq.length-1]] <= dp[i]) { deq.removeBack(); } p.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) { import core.exception : RangeError; import core.memory : GC; import std.range : ElementType, isInputRange; import std.traits : isImplicitlyConvertible; struct Payload { T *d; size_t st, length, cap; @property bool empty() const { return length == 0; } alias opDollar = length; ref inout(T) opIndex(size_t i) inout { version(assert) if (length <= i) throw new RangeError(); return d[(st+i >= cap) ? (st+i-cap) : st+i]; } private void expand() { import std.algorithm : max; assert(length == cap); auto nc = max(size_t(4), 2*cap); T* nd = cast(T*)GC.malloc(nc * T.sizeof); foreach (i; 0..length) { nd[i] = this[i]; } d = nd; st = 0; cap = nc; } void clear() { st = length = 0; } void insertFront(T v) { if (length == cap) expand(); if (st == 0) st += cap; st--; length++; this[0] = v; } void insertBack(T v) { if (length == cap) expand(); length++; this[length-1] = v; } void removeFront() { assert(!empty, "Deque.removeFront: Deque is empty"); st++; length--; if (st == cap) st = 0; } void removeBack() { assert(!empty, "Deque.removeBack: Deque is empty"); length--; } } struct RangeT(A) { alias T = typeof(*(A.p)); alias E = typeof(A.p.d[0]); T *p; size_t a, b; @property bool empty() const { return b <= a; } @property size_t length() const { return b-a; } @property RangeT save() { return RangeT(p, a, b); } @property RangeT!(const A) save() const { return typeof(return)(p, a, b); } alias opDollar = length; @property ref inout(E) front() inout { return (*p)[a]; } @property ref inout(E) back() inout { return (*p)[b-1]; } void popFront() { version(assert) if (empty) throw new RangeError(); a++; } void popBack() { version(assert) if (empty) throw new RangeError(); b--; } ref inout(E) opIndex(size_t i) inout { return (*p)[i]; } RangeT opSlice() { return this.save; } RangeT opSlice(size_t i, size_t j) { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } RangeT!(const A) opSlice() const { return this.save; } RangeT!(const A) opSlice(size_t i, size_t j) const { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } } alias Range = RangeT!Deque; alias ConstRange = RangeT!(const Deque); alias ImmutableRange = RangeT!(immutable Deque); Payload *p; private void I() { if (!p) p = new Payload(); } private void C() const { version(assert) if (!p) throw new RangeError(); } //some value this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) {I; p = new Payload(); foreach (v; values) { insertBack(v); } } //range this(Range)(Range r) if (isInputRange!Range && isImplicitlyConvertible!(ElementType!Range, T) && !is(Range == T[])) {I; p = new Payload(); foreach (v; r) { insertBack(v); } } @property bool empty() const { return (!p || p.empty); } @property size_t length() const { return (p ? p.length : 0); } alias opDollar = length; ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; } ref inout(T) front() inout {C; return (*p)[0]; } ref inout(T) back() inout {C; return (*p)[$-1]; } void clear() { if (p) p.clear(); } void insertFront(T v) {I; p.insertFront(v); } void insertBack(T v) {I; p.insertBack(v); } void removeFront() {C; p.removeFront(); } void removeBack() {C; p.removeBack(); } Range opSlice() {I; return Range(p, 0, length); } } unittest { import std.algorithm : equal; import std.range.primitives : isRandomAccessRange; import std.container.util : make; auto q = make!(Deque!int); assert(isRandomAccessRange!(typeof(q[]))); //insert,remove assert(equal(q[], new int[](0))); q.insertBack(1); assert(equal(q[], [1])); q.insertBack(2); assert(equal(q[], [1, 2])); q.insertFront(3); assert(equal(q[], [3, 1, 2]) && q.front == 3); q.removeFront; assert(equal(q[], [1, 2]) && q.length == 2); q.insertBack(4); assert(equal(q[], [1, 2, 4]) && q.front == 1 && q.back == 4 && q[$-1] == 4); q.insertFront(5); assert(equal(q[], [5, 1, 2, 4])); //range assert(equal(q[][1..3], [1, 2])); assert(equal(q[][][][], q[])); //const range const auto rng = q[]; assert(rng.front == 5 && rng.back == 4); //reference type auto q2 = q; q2.insertBack(6); q2.insertFront(7); assert(equal(q[], q2[]) && q.length == q2.length); //construct with make auto a = make!(Deque!int)(1, 2, 3); auto b = make!(Deque!int)([1, 2, 3]); assert(equal(a[], b[])); } unittest { import std.algorithm : equal; import std.range.primitives : isRandomAccessRange; import std.container.util : make; auto q = make!(Deque!int); q.clear(); assert(equal(q[], new int[0])); foreach (i; 0..100) { q.insertBack(1); q.insertBack(2); q.insertBack(3); q.insertBack(4); q.insertBack(5); assert(equal(q[], [1,2,3,4,5])); q.clear(); assert(equal(q[], new int[0])); } } unittest { Deque!int a; Deque!int b; a.insertFront(2); assert(b.length == 0); } unittest { import std.algorithm : equal; import std.range : iota; Deque!int a; foreach (i; 0..100) { a.insertBack(i); } assert(equal(a[], iota(100))); } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; if (f.eof) return false; line = lineBuf[]; f.readln(line); } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { //string or char[10] etc //todo optimize auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { //static assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); fout.writeln("1 2 3"); fout.writeln("ab cde"); fout.writeln("1.0 1.0 2.0"); fout.close; Scanner sc = new Scanner(File(fileName, "r")); int a; int[2] b; char[2] c; string d; double e; double[] f; sc.read(a, b, c, d, e, f); assert(a == 1); assert(equal(b[], [2, 3])); assert(equal(c[], "ab")); assert(equal(d, "cde")); assert(e == 1.0); assert(equal(f, [1.0, 2.0])); } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File, writeln; import std.datetime; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); foreach (i; 0..1_000_000) { fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2); } fout.close; writeln("Scanner Speed Test(3*1,000,000 int)"); StopWatch sw; sw.start; Scanner sc = new Scanner(File(fileName, "r")); foreach (i; 0..500_000) { int a, b, c; sc.read(a, b, c); assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 500_000..700_000) { int[3] d; sc.read(d); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 700_000..1_000_000) { int[] d; sc.read(d); assert(d.length == 3); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } writeln(sw.peek.msecs, "ms"); }
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } /* mod 6で 3 を偶数個(最大5,000個) 2と4を同数(あわせて最大10,000個) 0 を任意個(最大5,000個) とればよい */ void main(){ int n = read.to!int; if(n == 3){ writeln("2 5 63"); return; } int[] ans; int x3, x2, x4, x6; if(n <= 5002){ x3 = (n - 2) / 2 * 2; x2 = 1; x4 = 1; x6 = n - x3 - x2 - x4; } else if(n <= 15000){ x3 = 5000; x2 = 1 + (n - 5002) / 2 ; x4 = 1 + (n - 5002) / 2 ; x6 = n - x3 - x2 - x4; } else{ x3 = 5000; x2 = 5000; x4 = 5000; x6 = n - x3 - x2 - x4; } for(int i = 0; i < x3; i ++) ans ~= 3 + i * 6; for(int i = 0; i < x2; i ++) ans ~= 2 + i * 6; for(int i = 0; i < x4; i ++) ans ~= 4 + i * 6; for(int i = 0; i < x6; i ++) ans ~= 6 + i * 6; ans.to!(string[]).join(" ").writeln; }
D
void main() { auto X = ri; ulong res; while(X >= 500) { res += 1000; X -= 500; } res += (X / 5) * 5; res.writeln; } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.ascii; void main() { auto n = readln.chomp.to!int; int[string] t; int sum; foreach (i; 0..n) { auto a = readln.chomp.split; sum += a[1].to!int; t[a[0]] = sum; } auto x = readln.chomp; writeln(sum - t[x]); }
D
import std.algorithm; import std.array; import std.ascii; import std.bigint; import std.complex; import std.container; import std.conv; import std.functional; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } void readlnTo(T...)(ref T t) { auto s = readln().split(); assert(s.length == t.length); foreach(ref ti; t) { ti = s[0].to!(typeof(ti)); s = s[1..$]; } } const real eps = 1e-10; void main(){ long x, y; readlnTo(x, y); long ans = long.max; if(x <= y) { ans = min(ans, y-x); } if(-x <= y) { ans = min(ans, y - (-x) + 1); } if(x <= -y) { ans = min(ans, -y - x + 1); } if(-x <= -y) { ans = min(ans, -y - (-x) + 2); } writeln(ans); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto n = RD!int; auto s = RD!(char[]); long ans; foreach (i; 0..n/2) { if (s[i*2] == s[i*2+1]) { ++ans; s[i*2] = s[i*2] == 'a' ? 'b' : 'a'; } } writeln(ans); writeln(s); stdout.flush(); debug readln(); }
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() { long N, K; scanln(N, K); long[] as = readln.split.to!(long[]); long T = 45; long[][] bss = new long[][](T, 2); foreach(i; 0..T) { foreach(a; as) { bss[i][0] += (a&(1L<<i))^(0L<<i); bss[i][1] += (a&(1L<<i))^(1L<<i); } } long ans = 0; long k = 0; foreach_reverse(i; 0..T) { long x = k; long y = k | (1L<<i); if (y > K) { ans += bss[i][0]; } else { if (bss[i][0] >= bss[i][1]) { ans += bss[i][0]; } else { ans += bss[i][1]; k = y; } } } 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 std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 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 = readln.split.to!(int[]); auto mn = inf; int ans; foreach (ai ; a) { if (ai < mn) { ans++; mn = ai; } } writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.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 a = RDA; auto b = RDA; auto c = RDA; auto x11 = inside(a[0], b[0], b[2]+1); auto x12 = inside(a[2], b[0], b[2]+1); auto x21 = inside(a[0], c[0], c[2]+1); auto x22 = inside(a[2], c[0], c[2]+1); auto y11 = inside(a[1], b[1], b[3]+1); auto y12 = inside(a[3], b[1], b[3]+1); auto y21 = inside(a[1], c[1], c[3]+1); auto y22 = inside(a[3], c[1], c[3]+1); debug writeln(x11); debug writeln(x12); debug writeln(x21); debug writeln(x22); debug writeln(y11); debug writeln(y12); debug writeln(y21); debug writeln(y22); bool ans = true; if (x11 && x12 && y11 && y12) ans = false; else if (x21 && x22 && y21 && y22) ans = false; else if (x11 && x12) { if (y11) { if (x21 && x22 && inside(b[3], c[1], c[3]+1) && y22) ans = false; } else if (y12) { if (x21 && x22 && y21 && inside(b[1], c[1], c[3]+1)) ans = false; } } else if (y11 && y12) { if (x11) { if (inside(b[2], c[0], c[2]+1) && x22 && y21 && y22) ans = false; } else if (x12) { if (x21 && inside(b[0], c[0], c[2]+1) && y21 && y22) ans = false; } } writeln(ans ? "YES" : "NO"); stdout.flush(); debug readln(); }
D
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; import std.datetime, std.bigint; int n, k; long[] a; void main() { scan(n, k); a = readln.split.to!(long[]); auto two = new int[](n); auto five = new int[](n); foreach (i ; 0 .. n) { while (a[i] % 2 == 0) { a[i] /= 2; two[i]++; } while (a[i] % 5 == 0) { a[i] /= 5; five[i]++; } } int lim = n * 25; auto dp = new int[][][](2, k + 1, lim + 1); fillAll(dp, -1); dp[0][0][0] = 0; foreach (i ; 0 .. n) { foreach (j ; 0 .. k + 1) { foreach (f ; 0 .. lim + 1) { if (dp[i & 1][j][f] == -1) continue; dp[(i & 1) ^ 1][j][f] = max(dp[(i & 1) ^ 1][j][f], dp[i & 1][j][f]); if (j + 1 <= k) { dp[(i & 1) ^ 1][j + 1][f + five[i]] = max(dp[(i & 1) ^ 1][j + 1][f + five[i]], dp[i & 1][j][f] + two[i]); } } } } int ans; foreach (f ; 0 .. lim + 1) { ans = max(ans, min(f, dp[n & 1][k][f])); } writeln(ans); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto n = RD; auto k = RD; if (n < k^^2) ans[ti] = false; else ans[ti] = n % 2 == k % 2; } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush; debug readln; }
D
/+ dub.sdl: name "C" dependency "dunkelheit" version="1.0.1" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner, dkh.algorithm; immutable L = 19; int main() { Scanner sc = new Scanner(stdin); scope(exit) assert(!sc.hasNext); int n, q; sc.read(n, q); int[] a; sc.read(a); int[L] bk; bk[] = n; int[L][] dp = new int[L][n]; foreach_reverse (i; 0..n) { int d = a[i]; dp[i][] = n; foreach (j; 0..L) { if (!(d & (1 << j))) continue; dp[i][j] = i; int bi = bk[j]; if (bi != n) { foreach (k; 0..L) { dp[i][k] = min(dp[i][k], dp[bi][k]); } } bk[j] = i; } } debug writeln(dp); foreach (ph; 0..q) { int u, v; sc.read(u, v); u--; v--; bool ok = false; foreach (i; 0..L) { if (!(a[v] & (1 << i))) continue; if (dp[u][i] <= v) { ok = true; break; } } if (ok) writeln("Shi"); else writeln("Fou"); } return 0; } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/algorithm.d */ // module dkh.algorithm; import std.traits : isFloatingPoint, isIntegral; T binSearch(alias pred, T)(T l, T r) if (isIntegral!T) { while (r-l > 1) { T md = l + (r-l) / 2; if (!pred(md)) l = md; else r = md; } return r; } T binSearch(alias pred, T)(T l, T r, int cnt = 60) if (isFloatingPoint!T) { foreach (i; 0..cnt) { T md = (l+r)/2; if (!pred(md)) l = md; else r = md; } return r; } import std.range.primitives; E minimum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed) if (isInputRange!Range && !isInfinite!Range) { import std.algorithm, std.functional; return reduce!((a, b) => binaryFun!pred(a, b) ? a : b)(seed, range); } ElementType!Range minimum(alias pred = "a < b", Range)(Range range) { assert(!range.empty, "minimum: range must not empty"); auto e = range.front; range.popFront; return minimum!pred(range, e); } E maximum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed) if (isInputRange!Range && !isInfinite!Range) { import std.algorithm, std.functional; return reduce!((a, b) => binaryFun!pred(a, b) ? b : a)(seed, range); } ElementType!Range maximum(alias pred = "a < b", Range)(Range range) { assert(!range.empty, "maximum: range must not empty"); auto e = range.front; range.popFront; return maximum!pred(range, e); } Rotator!Range rotator(Range)(Range r) { return Rotator!Range(r); } struct Rotator(Range) if (isForwardRange!Range && hasLength!Range) { size_t cnt; Range start, now; this(Range r) { cnt = 0; start = r.save; now = r.save; } this(this) { start = start.save; now = now.save; } @property bool empty() const { return now.empty; } @property auto front() const { assert(!now.empty); import std.range : take, chain; return chain(now, start.take(cnt)); } @property Rotator!Range save() { return this; } void popFront() { cnt++; now.popFront; } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/scanner.d */ // module dkh.scanner; // import dkh.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; private File f; this(File f) { this.f = f; } private char[512] lineBuf; private char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { assert(succW()); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(Args...)(auto ref Args args) { import std.exception : enforce; static if (args.length != 0) { enforce(readSingle(args[0])); read(args[1..$]); } } bool hasNext() { return succ(); } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto s = RD!string; ans[ti] = long.max; foreach (i; 0..26) { long cnt; int l, r = n-1; bool ok = true; while (l < r) { if (s[l] != s[r]) { if (s[l]-'a' == i) { ++l; ++cnt; continue; } else if (s[r]-'a' == i) { --r; ++cnt; continue; } else ok = false; } ++l; --r; } if (ok) ans[ti].chmin(cnt); } } foreach (e; ans) writeln(e == long.max ? -1 : 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 l = RD!int; auto r = RD!int; string ans; foreach (i; l..r+1) { auto str = i.to!string; auto cnt = new long[](10); bool ok = true; foreach (c; str) { if (cnt[c-'0'] == 1) { ok = false; break; } ++cnt[c-'0']; } if (ok) { ans = str; break; } } writeln(ans.length == 0 ? "-1" : ans); stdout.flush(); debug readln(); }
D
// cheese-cracker [2022-02-06] void solve(){ long n = scan; long k = scan; auto s = scan!(dchar[]); auto rs = s.dup; rs.reverse; if(s == rs || k == 0){ writeln(1); }else{ writeln(2); } } void main(){ long tests = scan; // Toggle! while(tests--) solve; } /*_________________________*That's All Folks!*__________________________*/ import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math, std.numeric; string[] tk; alias tup = Tuple!(long, long); T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;} T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; } void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new string[](t); foreach (ti; 0..t) { auto s = RD!string; foreach (i; 0..s.length) { if (s[$-i-1] != 'a') { ans[ti] = s[0..i] ~ 'a' ~ s[i..$]; break; } } } foreach (e; ans) { if (e.empty) writeln("NO"); else { writeln("YES"); 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, std.bitmanip; void main() { auto N = readln.chomp.to!int; auto G = new int[][](N); foreach (_; 0..N-1) { auto s = readln.split.map!(to!int); G[s[0]-1] ~= s[1]-1; G[s[1]-1] ~= s[0]-1; } auto A = readln.split.map!(x => x.to!int-1).array; auto D = new int[](N); auto E = new int[][](N); auto P = new int[](N); auto order = new int[](N); void dfs(int n, int p, int d) { P[n] = p; D[n] = d; E[d] ~= n; foreach (m; G[n]) if (m != p) dfs(m, n, d+1); } dfs(0, -1, 0); foreach (i; 0..N-1) { if (D[A[i]] > D[A[i+1]]) { writeln("No"); return; } } foreach (i; 1..N) { if (D[A[i]] > D[A[i-1]]) { order[A[i]] = 0; } else { order[A[i]] = order[A[i-1]] + 1; } } foreach (i; 0..N-1) { if (D[A[i]] == D[A[i+1]] && order[P[A[i]]] > order[P[A[i+1]]]) { writeln("No"); return; } } writeln("Yes"); }
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 s = RD!string; int[] list; char last = s[0]; int cnt; foreach (i; 0..n) { if (s[i] != last) { list ~= cnt; cnt = 0; } ++cnt; last = s[i]; } if (!list.empty && s[0] == s[$-1]) { list[0] += cnt; } else { list ~= cnt; } if (list.length == 1) { ans[ti] += (list[0] + 2) / 3; } else { foreach (e; list) ans[ti] += e / 3; } } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
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; long[] as = scan!long(n); long[] xs = new long[](600_999); foreach(i, a; as){ int d = n + a.to!int - (i + 1).to!int; xs[d] += a; } long ans = 0; foreach(x; xs) ans.raiseTo(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 n = RD!int; auto p = RD!int; int ans = -1; foreach (i; 1..33) { auto x = n - i * p; int cnt, cnt_max; foreach (j; 0..32) { auto bit = 1L << j; if (x & bit) { ++cnt; cnt_max += 2^^j; } } debug writeln("i:", i, " x:", x, " cnt:", cnt); if (i >= cnt && i <= cnt_max) { ans = i; break; } } writeln(ans); stdout.flush(); debug readln(); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { auto hw = readints; int h = hw[0]; for (int i = 0; i < h; i++) { string s = read!string; writeln(s); writeln(s); } }
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; long sum; foreach (int i; 1..n+1) { if (i % 3 && i % 5) { sum += i; } } sum.writeln; }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std; // dfmt on void main() { long N = lread(); auto A = aryread(); long Q = lread(); auto cnt = new long[](10 ^^ 5 + 1); foreach (a; A) { cnt[a]++; } long S = A.sum(); foreach (_; 0 .. Q) { long B, C; scan(B, C); S -= B * cnt[B]; S += C * cnt[B]; writeln(S); cnt[C] += cnt[B]; cnt[B] = 0; } }
D
import std.algorithm; import std.array; import std.ascii; import std.bigint; import std.complex; import std.container; import std.conv; import std.functional; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } void readlnTo(T...)(ref T t) { auto s = readln().split(); assert(s.length == t.length); foreach(ref ti; t) { ti = s[0].to!(typeof(ti)); s = s[1..$]; } } const real eps = 1e-10; void main(){ long n, t; readlnTo(n, t); auto a = readLongs(); auto point = 0L; auto aMin = long.max; foreach(ai; a) { point = max(point, ai - aMin); aMin = min(ai, aMin); } long[long] b; foreach(i; iota(a.length)) { b[a[i]] = i; } long ans; foreach(i; iota(a.length)) { if(a[i] + point in b && b[a[i] + point] > i) { ++ans; } } writeln(ans); }
D
import std.conv; import std.stdio; import std.string; void main() { auto n = readln.strip.to!int; writeln( solve( n ) ); } auto solve( in int n ) { if( n < 1000 ) return "ABC"; else return "ABD"; } unittest { assert( solve( 999 ) == "ABC" ); assert( solve( 1000 ) == "ABD" ); assert( solve( 1481 ) == "ABD" ); }
D
void main() { auto S = rs; if(S.uniq.array.length == 1) writeln("No"); else writeln("Yes"); } // =================================== 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.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; } string calc(Vec2 a, Vec2 b, Vec2 c) { auto ab = b - a; auto ac = c - a; auto d = ab.cross(ac); if (d > 0) return "COUNTER_CLOCKWISE"; if (d < 0) return "CLOCKWISE"; // |ab||ac|cos(pi) で cos(pi) = -1 なので負なら逆向き if (ab.dot(ac) < 0) return "ONLINE_BACK"; if (ab.magSq() >= ac.magSq()) return "ON_SEGMENT"; return "ONLINE_FRONT"; } void main() { auto xs = readints(); auto a = Vec2(xs[0], xs[1]); auto b = Vec2(xs[2], xs[3]); int q = readint(); for (int i = 0; i < q; i++) { auto xy = readints(); auto c = Vec2(xy[0], xy[1]); auto ans = calc(a, b, c); writeln(ans); } } struct Vec2 { immutable double x; immutable double y; this(double x, double y) { this.x = x; this.y = y; } Vec2 opNeg() { return Vec2(-this.x, -this.y); } Vec2 opAdd(Vec2 other) { return Vec2(this.x + other.x, this.y + other.y); } Vec2 opSub(Vec2 other) { return Vec2(this.x - other.x, this.y - other.y); } Vec2 opMul(double d) { return Vec2(this.x * d, this.y * d); } double dot(Vec2 other) { return this.x * other.x + this.y * other.y; } double cross(Vec2 other) { return this.x * other.y - other.x * this.y; } double mag() { return sqrt(magSq()); } double magSq() { return this.x * this.x + this.y * this.y; } Vec2 normalized() { auto m = mag(); if (m != 0 && m != 1) return Vec2(this.x / m, this.y / m); return this; } static double distance(Vec2 a, Vec2 b) { return (a - b).mag(); } }
D
import std.stdio, std.conv, std.string, std.algorithm, std.math, std.array, std.container; void main() { int n = readln.chomp.to!int; int[] x = readln.chomp.split.to!(int[]); int minimum = int.max; for(int p=1; p<=100; p++) { int cost = 0; foreach(xi;x) { cost += (xi-p)*(xi-p); } minimum = min(cost, minimum); } writeln(minimum); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.container; import std.array; import std.math; import std.range; void main() { int m = readln.chomp.split.back.to!int; string s = readln.chomp; int l = 0, r = 1; // bool[string] set; bool[ulong[2]] set; auto hasher = new RollingHash(s); foreach (i; 0..m) { string v = readln; auto f = function(char c, ref int w) { if (c == '+') { ++w; } else { --w; } }; if (v[0] == 'L') { f(v[1], l); } else { f(v[1], r); } set[hasher.substr(l, r)] = true; // set[s[l..r]] = true; } writeln = set.length; } class RollingHash { enum long MOD1 = 1_000_000_007; enum long MOD2 = 1_000_000_009; enum long B1 = 1_009; enum long B2 = 1_007; ulong[] b1s, b2s; ulong[] array1; ulong[] array2; this(string s) { array1 ~= 0; array2 ~= 0; b1s ~= 1; b2s ~= 1; foreach (c; s) { array1 ~= ((array1.back + ulong(c)) * B1) % MOD1; array2 ~= ((array2.back + ulong(c)) * B2) % MOD2; b1s ~= (b1s.back * B1) % MOD1; b2s ~= (b2s.back * B2) % MOD2; } } ulong[2] substr(uint l, uint r) { ulong h1 = (array1[r] - ((b1s[r - l] * array1[l]) % MOD1) + MOD1) % MOD1; ulong h2 = (array2[r] - ((b2s[r - l] * array2[l]) % MOD2) + MOD2) % MOD2; return [h1, h2]; } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N, A, B; scan(N, A, B); if (B < A) swap(A, B); if ((B - A) % 2 == 0) { ((B - A) / 2).writeln(); return; } long a = N - A; long b = B - 1; long c = ((B - A) - 1) / 2 + (A); // 1で折り返す long d = (N - (A + N - B + 1)) / 2 + (N - B + 1); // Nで折り返す // writeln(a, " ", b, " ", c, " ", d); min(a, b, c, d).writeln(); }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { const tmp = readln.split.to!(long[]); writeln(max(0, tmp[2] - tmp[0] + tmp[1])); }
D
/* imports all std modules {{{*/ import std.algorithm, std.array, std.ascii, std.base64, std.bigint, std.bitmanip, std.compiler, std.complex, std.concurrency, std.container, std.conv, std.csv, std.datetime, std.demangle, std.encoding, std.exception, std.file, std.format, std.functional, std.getopt, std.json, std.math, std.mathspecial, std.meta, std.mmfile, std.net.curl, std.net.isemail, std.numeric, std.parallelism, std.path, std.process, std.random, std.range, std.regex, std.signals, std.socket, std.stdint, std.stdio, std.string, std.system, std.traits, std.typecons, std.uni, std.uri, std.utf, std.uuid, std.variant, std.zip, std.zlib; /*}}}*/ /+---test 2 ---+/ /+---test 4 ---+/ /+---test 1000000000000 ---+/ void main(string[] args) { const H = readln.chomp.to!long; long ans, cnt = 1; for (long h = H; h > 0; h/=2) { ans += cnt; cnt *= 2; } ans.writeln; }
D
// Code By H~$~C import std.stdio; import std.math, std.uni, std.format, std.bigint; import std.array, std.string, std.container, std.range, std.typecons; import std.algorithm, std.conv, std.functional, std.random; immutable Maxn = 200005; int n; int[Maxn] c; int[][Maxn] g; long[Maxn] ans; int[Maxn] sz, num; void dfs(int u, int fa) { int col = c[u]; int bef = num[col]; sz[u] = 1; foreach(v; g[u]) { if (v == fa) continue; int pre = num[col]; dfs(v, u); int vcol = sz[v] - (num[col] - pre); ans[col] -= cast(long)(vcol + 1) * vcol / 2; sz[u] += sz[v]; } num[col] = bef + sz[u]; } void _Main() { read(n); foreach(i; 0 .. n) read(c[i]), c[i]--; foreach(i; 1 .. n) { int u, v; read(u, v); --u, --v; g[u] ~= v; g[v] ~= u; } foreach(i; 0 .. n) ans[i] = cast(long)(n + 1) * n / 2; dfs(0, -1); foreach(i; 0 .. n) { int rest = n - num[i]; (ans[i] -= cast(long)(rest + 1) * rest / 2).writeln; } } class EOFException : Throwable { this() { super("------ EOF Error!"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } void read(T...)(ref T x) { foreach(i, ref y; x) y = readToken.to!(T[i]); } void main(string[] args) { try { int tests = 1; // read(tests); foreach (test; 0 .. tests) _Main(); } catch(EOFException e) { } }
D
//dlang template---{{{ import std.stdio; import std.conv; import std.string; import std.array; import std.algorithm; import std.typecons; import std.math; import std.range; // MIT-License https://github.com/kurokoji/nephele 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; } } //Digit count---{{{ int DigitNum(int num) { int digit = 0; while (num != 0) { num /= 10; digit++; } return digit; } //}}} //}}} void main() { Scanner sc = new Scanner; int N; sc.scan(N); int[] A = new int[N]; foreach (i; 0 .. N) sc.scan(A[i]); writeln(reduce!(max)(A) - reduce!(min)(A)); }
D
void main() { problem(); } void problem() { auto N = scan!int; auto K = scan!int; auto A = scan!long(N); void solve() { foreach(i, a; A[K..N]) { writeln(a > A[i] ? "Yes" : "No"); } } solve(); } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
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 X = RD; auto Y = RD; long ans; if (X == 3) ans += 100000; else if (X == 2) ans += 200000; else if (X == 1) ans += 300000; if (Y == 3) ans += 100000; else if (Y == 2) ans += 200000; else if (Y == 1) ans += 300000; if (X == 1 && Y == 1) ans += 400000; writeln(ans); stdout.flush(); debug readln(); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); long bignum = 1_000_000_007; 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 manhattan(long x1, long y1, long x2, long y2) { return abs(x2 - x1) + abs(y2 - y1); } void main() { long n, l; scan(n, l); long sumf, minf = bignum; foreach (i; 0 .. n) { long f = l + i; minf = min(abs(minf), abs(f)); sumf += f; } if (sumf >= 0) (sumf - minf).writeln(); else (sumf + minf).writeln(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string; int[(10^^5)*2+1] PS; void main() { auto N = readln.chomp.to!int; foreach (_; 0..N) { auto p = readln.chomp.to!int; PS[p] = PS[p-1] + 1; } int max = 0; foreach (p; PS[0..N+1]) if (p > max) max = p; writeln(N - max); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int k, x; scan(k, x); writeln(500*k >= x ? "Yes" : "No"); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop; // dfmt on void main() { long N, Q; scan(N, Q); auto ary = aryread!int(); auto seg = segtree!(int, max, "-1")(ary); foreach (_; 0 .. Q) { int T, A, B; scan(T, A, B); A--; if (T == 1) { seg.set(A, B); } else if (T == 2) { seg.prod(A, B).writeln(); } else if (T == 3) { writeln(seg.max_right(A, (int v) => v < B) + 1); } } } int ceil_pow2(int n) { int x = 0; while ((1u << x) < cast(uint)(n)) x++; return x; } struct segtree(S, alias op, alias e) { import std.functional : binaryFun, unaryFun; import std.traits : isCallable, Parameters; static if (is(typeof(e) : string)) { auto unit() { return mixin(e); } } else { alias unit = e; } this(int n) { this(new S[](n)); } this(S[] v) { _n = cast(int) v.length; log = ceil_pow2(_n); size = 1 << log; d = new S[](2 * size); d[] = unit(); foreach (i; 0 .. _n) d[size + i] = v[i]; foreach_reverse (i; 1 .. size) update(i); } void set(int p, S x) { assert(0 <= p && p < _n); p += size; d[p] = x; foreach (i; 1 .. log + 1) update(p >> i); } S get(int p) { assert(0 <= p && p < _n); return d[p + size]; } S prod(int l, int r) { assert(0 <= l && l <= r && r <= _n); S sml = unit(), smr = unit(); l += size; r += size; while (l < r) { if (l & 1) sml = binaryFun!(op)(sml, d[l++]); if (r & 1) smr = binaryFun!(op)(d[--r], smr); l >>= 1; r >>= 1; } return binaryFun!(op)(sml, smr); } S all_prod() { return d[1]; } int max_right(alias f)(int l) { return max_right(l, unaryFun!(f)); } int max_right(F)(int l, F f) if (isCallable!F && Parameters!(F).length == 1) { assert(0 <= l && l <= _n); assert(f(unit())); if (l == _n) return _n; l += size; S sm = unit(); do { while (l % 2 == 0) l >>= 1; if (!f(binaryFun!(op)(sm, d[l]))) { while (l < size) { l = 2 * l; if (f(binaryFun!(op)(sm, d[l]))) { sm = binaryFun!(op)(sm, d[l]); l++; } } return l - size; } sm = binaryFun!(op)(sm, d[l]); l++; } while ((l & -l) != l); return _n; } int min_left(alias f)(int r) { return min_left(r, unaryFun!(f)); } int min_left(F)(int r, F f) if (isCallable!F && Parameters!(F).length == 1) { assert(0 <= r && r <= _n); assert(f(unit())); if (r == 0) return 0; r += size; S sm = unit(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!f(binaryFun!(op)(d[r], sm))) { while (r < size) { r = 2 * r + 1; if (f(binaryFun!(op)(d[r], sm))) { sm = binaryFun!(op)(d[r], sm); r--; } } return r + 1 - size; } sm = binaryFun!(op)(d[r], sm); } while ((r & -r) != r); return 0; } private: int _n, size, log; S[] d; void update(int k) { d[k] = binaryFun!(op)(d[2 * k], d[2 * k + 1]); } }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; import std.ascii; void main() { int a, b; scan(a, b); string s; scan(s); bool ok = s[0 .. a].isNumeric && s[a] == '-' && s[a + 1 .. $].isNumeric; writeln(ok ? "Yes" : "No"); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.container.dlist; alias DL = DList!char; class P { DL l; void add (int f, char c) { if (!f) { l.insertFront (c); } else { l.insertBack (c); } } } class S { string s; int rev; P prefix; P suffix; this (string _s) { s = _s; prefix = new P (); suffix = new P (); } void reverse () { ++rev; } void add (int f, char c) { if (!(rev & 1)) { if (f == 1) prefix.add (0, c); else suffix.add (1, c); } else { if (f == 2) prefix.add (0, c); else suffix.add (1, c); } } void dump () { if (!(rev & 1)) { foreach (c; prefix.l) write (c); write (s); foreach (c; suffix.l) write (c); } else { foreach_reverse (c; suffix.l) write (c); foreach_reverse (c; s) write (c); foreach_reverse (c; prefix.l) write (c); } writeln; } } void main() { auto s = readln.stripRight; auto z = readln.stripRight; const int nt = z.to!int; auto res = new S (s); foreach (tid; 0 .. nt) { z = readln.stripRight; auto w = z.splitter; const int t = w.front.to!int; w.popFront (); if (t == 1) { res.reverse (); } else { int f = w.front.to!int; w.popFront (); char c = w.front[0]; res.add (f, c); } //res.dump (); } res.dump (); }
D
void main() { char c = readln.chomp.to!char; char[] vowels = ['a', 'e', 'i', 'o', 'u']; writeln(vowels.canFind(c) ? "vowel" : "consonant"); } 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
/+ dub.sdl: name "E" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; int main() { auto sc = new Scanner(stdin); string s; sc.read(s); int n = s.length.to!int; int[26][] nx = new int[26][n+1]; nx[n][] = n+1; foreach_reverse (i, c; s) { nx[i][] = nx[i+1][]; nx[i][c-'a'] = i.to!int+1; } int[] res = new int[n+2]; res[n+1] = 0; foreach_reverse(i; 0..n+1) { res[i] = 10^^9; foreach (d; 0..26) { res[i] = min(res[i], 1+res[nx[i][d]]); } } // writeln(res); string t; int p = 0; while (p < n+1) { foreach (d; 0..26) { if (res[p] - 1 == res[nx[p][d]]) { t ~= (d + 'a').to!char; p = nx[p][d]; break; } } } writeln(t); return 0; } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; if (f.eof) return false; line = lineBuf[]; f.readln(line); } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } version (X86) static if (__VERSION__ < 2071) { import core.bitop : bsf, bsr, popcnt; int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } }
D
import std.stdio, std.string, std.conv; void main() { int x = readln.chomp.to!int; writeln(x ^^ 3); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container; void main() { auto hwk = readln.split.to!(int[]); auto H = hwk[0]; auto W = hwk[1]; auto K = hwk[2]; auto xy = readln.split.to!(int[]); auto sy = xy[0]-1; auto sx = xy[1]-1; auto gy = xy[2]-1; auto gx = xy[3]-1; auto MAP = new char[][H]; foreach (i; 0..H) MAP[i] = readln.chomp.to!(char[]); auto DP = new int[][](H, W); foreach (ref dp; DP) dp[] = int.max; auto ss = [[sx, sy, 0]]; while (!ss.empty) { auto h = ss[0]; ss = ss[1..$]; auto x = h[0]; auto y = h[1]; auto c = h[2]; foreach (i, d; [[0, -1], [-1, 0], [0, 1], [1, 0]]) { foreach (n; 1..K+1) { auto dx = x + n * d[0]; auto dy = y + n * d[1]; if (dx < 0 || dx >= W || dy < 0 || dy >= H || MAP[dy][dx] == '@' || DP[dy][dx] < c) break; if (DP[dy][dx] == c) continue; DP[dy][dx] = c; if (dx == gx && dy == gy) { writeln(c+1); return; } ss ~= [dx, dy, c+1]; } } } writeln(-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; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ int n = read.to!int; long[] as, bs; foreach(i; 0 .. n) as ~= read.to!long; foreach(i; 0 .. n) bs ~= read.to!long; long[] xs, ys; foreach(i; 0 .. n){ if(i == 0) xs ~= as[0], ys ~= bs[0]; else{ xs ~= max(ys[i - 1] + as[i], xs[i - 1]); ys ~= max(xs[i - 1] + bs[i], ys[i - 1]); } } long ans = max(xs[n - 1], ys[n - 1]); ans.writeln; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format, std.datetime; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long H, W; scan(H, W); auto C = new long[][](10); foreach (i; 0 .. 10) C[i] = aryread(); auto A = new long[](H * W); foreach (i; 0 .. H) { auto a = aryread(); foreach (j; 0 .. W) { A[i * W + j] = a[j]; } } foreach (k; 0 .. 10) foreach (i; 0 .. 10) foreach (j; 0 .. 10) if (C[i][k] + C[k][j] < C[i][j]) C[i][j] = C[i][k] + C[k][j]; long ans; foreach (a; A) if (a != -1) { ans += C[a][1]; } writeln(ans); }
D
import std.stdio; import std.conv; import std.algorithm; import std.range; import std.string; import std.typecons; import std.math; import std.random; import std.range; import std.functional; sizediff_t minIndex(alias pred = "a < b", Range)(Range range) if (isForwardRange!Range && !isInfinite!Range && is(typeof(binaryFun!pred(range.front, range.front)))) { if (range.empty) return -1; sizediff_t minPos = 0; static if (isRandomAccessRange!Range && hasLength!Range) { foreach (i; 1 .. range.length) { if (binaryFun!pred(range[i], range[minPos])) { minPos = i; } } } else { sizediff_t curPos = 0; Unqual!(typeof(range.front)) min = range.front; for (range.popFront(); !range.empty; range.popFront()) { ++curPos; if (binaryFun!pred(range.front, min)) { min = range.front; minPos = curPos; } } } return minPos; } void selsort(alias pred = "a < b", T)(T a, out int sw) { sw = 0; for (size_t i = 0; i < a.length; i++) { auto min = a[i..$].minIndex!pred + i; swap(a[i], a[min]); sw++; } } void bblsort(alias pred = "a < b", T)(T a, out int sw) { sw = 0; for (size_t i = 0; i < a.length; i++) { for (size_t j = a.length - 1; j >= i + 1; j--) { if (binaryFun!pred(a[j], a[j - 1])) { swap(a[j], a[j - 1]); } } } } void main() { readln; auto a = readln().strip.split(" ").array; int sw; auto b = a.dup; auto c = a.dup; b.bblsort!((x,y) => x[1..$].to!int < y[1..$].to!int)(sw); writeln(b.join(" ")); writeln("Stable"); c.selsort!((x,y) => x[1..$].to!int < y[1..$].to!int)(sw); writeln(c.join(" ")); if (b.join(" ") != c.join(" ")) { writeln("Not stable"); } else { writeln("Stable"); } }
D
import std.stdio, std.conv, std.algorithm, std.string; alias L = long; L M = 998244353; L[] X, D; L q(L m, L[] U){ foreach(i, u; U) if(i / m & 1 ^ U[i % m] ^ u) return u; return 1; } L t(L[] U){ L f; foreach(u; U) f = (f * 2 + u) % M; return f; } void main(){ L n = readln.chomp.to!L, b = n * 2; foreach(c; readln.chomp) X ~= c - '0'; foreach_reverse(d; 3 .. n + 1) if(d % 2 && !(n % d)) D ~= d; L[L] C; foreach(d; D){ C[d] = t(X[0 .. n / d]) + q(n / d, X); foreach(e; D) if(e > d && !(e % d)) C[d] += M - C[e]; C[d] %= M; } n = (t(X) + 1) * b % M; foreach(d; D) n += M - C[d] * (b - b / d) % M; writeln(n % M); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}} void main() { int n, m; readV(n, m); string[] a; readC(n, a); string[] b; readC(m, b); foreach (y; 0..n-m+1) foreach (x; 0..n-m+1) if (a[y..y+m].map!(ai => ai[x..x+m]).array == b) { writeln("Yes"); return; } writeln("No"); }
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; } struct Queue(T) { private { size_t cap, head, tail; T[] data; } this (size_t n) in { assert(n > 0, "The capacity of a queue must be a positive integer."); } body { cap = n + 1; data = new T[](cap); } void clear() { head = tail = 0; } bool empty() { return head == tail; } bool full() { return head == (tail + 1) % cap; } size_t length() { return head <= tail ? tail - head : cap - head + tail; } T front() in { assert(!empty, "The queue is empty."); } body { return data[head]; } void removeFront() in { assert(!empty, "The queue is empty."); } body { (++head) %= cap; } alias popFront = removeFront; void insertBack(T x) in { assert(!full, "The queue is full."); } body { data[tail++] = x; tail %= cap; } alias insert = insertBack; T[] array() { return head <= tail ? data[head .. tail].dup : data[head .. $] ~ data[0 .. tail]; } string toString() { import std.format : format; if (head <= tail) { return format("[%(%s, %)]", data[head .. tail]); } else { return format("[%(%s, %)", data[head .. $]) ~ format(", %(%s, %)]", data[0 .. tail]); } } } unittest { auto a = Queue!(int)(5); //a.front; a.insert(2); a.insert(5); a.insert(3); //a.insert(8); import std.stdio; a.removeFront; a.removeFront; a.insert(11); a.insert(7); a.insert(123); a.insert(543); a.removeFront; a.removeFront; a.removeFront; a.insert(1212); a.insert(9898); a.insert(3154); a.removeFront; a.removeFront; a.removeFront; a.removeFront; a.removeFront; } unittest { auto q = Queue!(int)(5); assert(q.empty); q.insert(3); q.insert(1); q.insert(5); q.insert(4); q.insert(8); // q = [3,1,5,4,8] auto hoge = q.array; assert(hoge == [3,1,5,4,8]); assert(q.full); q.popFront(); q.popFront(); // q = [5,4,8] hoge = q.array; assert(hoge == [5,4,8]); hoge[0] = 3; hoge[2] = 11; assert(q.front == 5); assert(q.length == 3); q.popFront(); q.popFront(); // q = [8] q.insert(11); q.insert(-8); // q = [8,11,-8] assert(q.front == 8); assert(q.length == 3); q.insert(23); q.insert(31); // q = [8,11,-8,23,31] assert(q.full); assert(q.length == 5); auto a = new int[](0); foreach (e ; q) { a ~= e; } assert(a == [8, 11, -8, 23, 31]); foreach (i ; 0 .. 5) { q.popFront(); } assert(q.length == 0); assert(q.empty); hoge = q.array; assert(hoge == []); } unittest { auto q = Queue!(string)(4); q.insert("Hello"); q.insert(","); q.insert("World"); q.insert("!"); auto hoge = q.array; assert(hoge == ["Hello", ",", "World", "!"]); assert(q.full); assert(q.length == 4); q.popFront(); q.popFront(); assert(q.front == "World"); assert(q.length == 2); q.clear(); assert(q.empty); assert(q.length == 0); q.insert("hoge"); q.insert("fuga"); q.insert("melon"); q.popFront(); q.popFront; q.popFront; q.insert("hayo"); q.insert("hoge"); q.insert("pippi"); hoge = q.array; hoge[0] = "aho"; assert(q.front == "hayo"); q.insert("huni"); q.popFront; q.popFront; q.insert("ueue"); assert(q.front == "pippi"); assert(q.length == 3); } enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { int N, M; scan(N, M); auto dist = new long[][](N, N); fillAll(dist, infl); foreach (i ; 0 .. N) { dist[i][i] = 0; } foreach (i ; 0 .. M) { int ui, vi, ci; scan(ui, vi, ci); dist[ui][vi] = ci; } foreach (k ; 0 .. N) { foreach (i ; 0 .. N) { foreach (j ; 0 .. N) { if (dist[i][k] == infl || dist[k][j] == infl) continue; chmin(dist[i][j], dist[i][k] + dist[k][j]); } } } bool nc = false; foreach (i ; 0 .. N) { if (dist[i][i] < 0) nc = true; } if (nc) { writeln("NEGATIVE CYCLE"); return; } foreach (i ; 0 .. N) { foreach (j ; 0 .. N) { write(dist[i][j] < infl ? dist[i][j].to!string : "INF"); write(j == N - 1 ? '\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; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto t = RD!int; auto ans = new int[](t); foreach (ti; 0..t) { auto a = RD!int; auto b = RD!int; auto c = RD!int; auto r = RD!int; if (a > b) swap(a, b); auto x = c - r; auto y = c + r; auto len = b - a; if (x < a) ans[ti] = max(len - max(0, y - a), 0); else if (y >= b) ans[ti] = max(len - max(0, b - x), 0); else ans[ti] = max(len - (y - x), 0); } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
void main(){ string s = _scan!string(); switch(s){ case "SUN": writeln(7); break; case "MON": writeln(6); break; case "TUE": writeln(5); break; case "WED": writeln(4); break; case "THU": writeln(3); break; case "FRI": writeln(2); break; case "SAT": writeln(1); break; default: break; } } 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 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 .. 10) { disit_num = a / pow(10, disit); sum += disit_num % 10; } return sum; } void main() { long s = lread(); long sum = disit_sum(s); (s % sum?"No":"Yes").writeln(); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n; readV(n); string s; readV(s); auto a = new int[](n); foreach (i; 0..n) a[i] = s[i] == 'E'; auto b = cumulativeSum(a); auto m = n; foreach (i; 0..n) m = min(m, (i-b[0..i])+b[i+1..$]); writeln(m); } class CumulativeSum(T) { size_t n; T[] s; this(T[] a) { n = a.length; s = new T[](n+1); s[0] = T(0); foreach (i; 0..n) s[i+1] = s[i] + a[i]; } T opSlice(size_t l, size_t r) { return s[r]-s[l]; } size_t opDollar() { return n; } } auto cumulativeSum(T)(T[] a) { return new CumulativeSum!T(a); }
D
import std.stdio : readln, writeln, writefln; import std.array : array, split; import std.conv : to; import std.range.primitives; import std.range : enumerate, iota, repeat, retro, assumeSorted; import std.algorithm.searching : all, any, canFind, count, countUntil; import std.algorithm.comparison : max, min, clamp; import std.algorithm.iteration : each, group, filter, map, reduce, permutations, sum, uniq; import std.algorithm.sorting : sort; import std.algorithm.mutation : reverse, swap; void main() { int n, y; scan(n, y); foreach (i ; 0 .. n + 1) { foreach (j ; 0 .. n + 1 - i) { int k = n - i - j; if (10000*i + 5000*j + 1000*k == y) { writeln(i, ' ', j, ' ', k); return; } } } writeln(-1, ' ', -1, ' ', -1); } void scan(T...)(ref T args) { import std.stdio : readln; import std.array : split; import std.conv : to; import std.range.primitives; string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.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 N = readln.chomp.to!int; auto A = readln.split.map!(x => x.to!int-1).array; auto B = new int[](N); foreach (a; A) B[a] += 1; B.each!writeln; }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop; // dfmt on void main() { long N = lread(); auto S = sread(); auto E = new long[](N + 1); auto W = new long[](N + 1); foreach (i; 0 .. N) { E[i + 1] = E[i] + (S[i] == 'E'); W[i + 1] = W[i] + (S[i] == 'W'); } long ans = long.max; foreach (i; 0 .. N) { long cnt = W[i] + E[$ - 1] - E[i + 1]; ans = ans.min(cnt); } writeln(ans); }
D
import std.stdio, std.string, std.array, std.conv, std.range; class Dice6 { int[string] dice6; int[] _dice6 = new int[6]; this(int[] _dice6) { dice6["top"] = _dice6[0]; dice6["front"] = _dice6[1]; dice6["right"] = _dice6[2]; dice6["left"] = _dice6[3]; dice6["back"] = _dice6[4]; dice6["bottom"] = _dice6[5]; } void roll(char c) { int tmp = dice6["top"]; if (c == 'E') { dice6["top"] = dice6["left"]; dice6["left"] = dice6["bottom"]; dice6["bottom"] = dice6["right"]; dice6["right"] = tmp; } else if (c == 'N') { dice6["top"] = dice6["front"]; dice6["front"] = dice6["bottom"]; dice6["bottom"] = dice6["back"]; dice6["back"] = tmp; } else if (c == 'S') { dice6["top"] = dice6["back"]; dice6["back"] = dice6["bottom"]; dice6["bottom"] = dice6["front"]; dice6["front"] = tmp; } else { dice6["top"] = dice6["right"]; dice6["right"] = dice6["bottom"]; dice6["bottom"] = dice6["left"]; dice6["left"] = tmp; } } void clockwise() { int tmp = dice6["front"]; dice6["front"] = dice6["right"]; dice6["right"] = dice6["back"]; dice6["back"] = dice6["left"]; dice6["left"] = tmp; } int surface(string dir) { int result; if (dir == "top") { result = dice6["top"]; } else if (dir == "front") { result = dice6["front"]; } else if (dir == "right") { result = dice6["right"]; } else if (dir == "left") { result = dice6["left"]; } else if (dir == "back") { result = dice6["back"]; } else { result = dice6["bottom"]; } return result; } } bool areIdentical(Dice6 x, Dice6 y) { string[] dirs = ["top", "front", "right", "left", "back", "bottom"]; foreach (dir; dirs) { if (x.surface(dir) != y.surface(dir)) return false; } return true; } void main() { int n = readln.chomp.to!int; Dice6[] dices; foreach (i; 0 .. n) { int[] _dices = readln.chomp.split.to!(int[]); dices ~= new Dice6(_dices); } bool ok = true; foreach (i; 0 .. n-1) { Dice6 dice1 = dices[i]; foreach (j; i+1 .. n) { Dice6 dice2 = dices[j]; foreach (k; 0 .. 6) { foreach (l; 0 .. 4) { if (areIdentical(dice1, dice2)) ok = false; dice2.clockwise; } k % 2 == 0 ? dice2.roll('E') : dice2.roll('N'); } } } writeln(ok ? "Yes" : "No"); }
D
void main() { string[] tmp = readln.split; string a = tmp[0], b = tmp[1], c = tmp[2]; writeln(a[$-1] == b[0] && b[$-1] == c[0] ? "YES" : "NO"); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; import std.math; void main() { int n; scan(n); writeln(n/3); } 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.conv, std.functional, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } void main() { try { for (; ; ) { const N = readInt(); const A = readToken(); const B = readToken(); const C = readToken(); int ans; foreach (i; 0 .. N) { if (A[i] == B[i] && A[i] == C[i]) { ans += 0; } else if (A[i] == B[i] || A[i] == C[i] || B[i] == C[i]) { ans += 1; } else { ans += 2; } } writeln(ans); } } catch (EOFException e) { } }
D
void main() { (N => N.iota.map!(i => ri).any!"a & 1" ? "first" : "second")(ri).writeln; } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); void main() { long w, h, n; scan(w, h, n); // writeln(w, h, n); auto tmp = new long[][](w, h); foreach (i; 0 .. tmp.length) tmp[i][] = 1; // writeln(tmp); //白1黒0 foreach (i; 0 .. n) { long x, y, a; scan(x, y, a); // writeln(x, y, a); if (a == 1) { foreach (j; 0 .. x) { foreach (k; 0 .. h) { tmp[j][k] = 0; } } } if (a == 2) { foreach (j; x .. w) { foreach (k; 0 .. h) { tmp[j][k] = 0; } } } if (a == 3) { foreach (j; 0 .. w) { foreach (k; 0 .. y) { tmp[j][k] = 0; } } } if (a == 4) { foreach (j; 0 .. w) { foreach (k; y .. h) { tmp[j][k] = 0; } } } // writeln(tmp); } long ans; foreach (i; 0 .. tmp.length) { ans += sum(tmp[i]); } writeln(ans); } // long[string] AA; // auto SS = ["AC", "WA", "AC"]; // foreach (S; SS) // { // // if (S in AA) // // { // // AA[S] = AA[S] + 1; // // } // // else // // { // // AA[S] = 1; // // } // AA[S] = AA.get(S, 0) + 1; // } // } // long get(long[string] aa, string key, long defaultValue) // { // if (key in aa) // { // return aa[key]; // } // else // { // return defaultValue; // } // } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n, k; rd(n, k); auto x=new long[](n), y=new long[](n); foreach(i; 0..n) rd(x[i], y[i]); long mn=5_000_000_000_000_000_000; foreach(i1; 0..n)foreach(i2; (i1+1)..n){ foreach(j1; 0..n)foreach(j2; (j1+1)..n){ auto x1=x[i1], x2=x[i2]; auto y1=y[j1], y2=y[j2]; if(x1>x2) swap(x1, x2); if(y1>y2) swap(y1, y2); if((x2-x1)*(y2-y1)>=mn) continue; int cnt=0; foreach(t; 0..n){ if(x1<=x[t] && x[t]<=x2 && y1<=y[t] && y[t]<=y2) cnt++; } if(cnt>=k) mn=min(mn, (x2-x1)*(y2-y1)); } } writeln(mn); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.stdio : readf, readln, writeln; import std.string; import std.array; import std.conv; void main() { int[] v; v = readln.split.to!(int[]); int H = v[0]; int W = v[1]; string[] s; for (int i=0; i<H; i++) s ~= readln.chomp; int[][] dw; int[][] dh; for (int i = 0; i < H; i++) { int[] cw; int count = 0; for (int j = 0; j < W; j++) { if (s[i][j] == '#') { cw ~= 0; } else if (j == 0 || s[i][j - 1] == '#') { count = 0; for (int k = j; k < W; k++) { if (s[i][k] == '.') { count++; } else { break; } } cw ~= count; } else { cw ~= count; } } dw ~= cw; } for (int j = 0; j < W; j++) { int[] ch; int count = 0; for (int i = 0; i < H; i++) { if (s[i][j] == '#') { ch ~= 0; } else if (i == 0 || s[i - 1][j] == '#') { count = 0; for (int k = i; k < H; k++) { if (s[k][j] == '.') { count++; } else { break; } } ch ~= count; } else { ch ~= count; } } dh ~= ch; } int max = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { int sum = dw[i][j] + dh[j][i]; if (max < sum) { max = sum; } } } writeln(max - 1); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { int N; long L; scan(N, L); auto s = new string[](N); iota(N).each!(i => s[i] = readln.chomp); auto ssum = s.map!(si => si.length.to!int).sum(); auto nex = new int[][](ssum * 2, 2); fillAll(nex, -1); int m = 1; foreach (i ; 0 .. N) { int k = s[i].length.to!int; int v = 0; foreach (j ; 0 .. k) { int c = s[i][j] - '0'; if (nex[v][c] == -1) { nex[v][c] = m++; } v = nex[v][c]; } } debug { writeln(nex); } int nim; void dfs(int v, int dep) { if (nex[v][0] == -1 && nex[v][1] == -1) { return; } else if (nex[v][0] == -1 || nex[v][1] == -1) { long h = L - dep; int m = 1; while (!(h & 1)) { h >>= 1; m++; } nim ^= m; if (nex[v][0] == -1) { dfs(nex[v][1], dep + 1); } else { dfs(nex[v][0], dep + 1); } } else { dfs(nex[v][0], dep + 1); dfs(nex[v][1], dep + 1); } } dfs(0, 0); writeln(nim ? "Alice" : "Bob"); } 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; import std.bigint; void main() { auto n = readln.chomp.to!int; int sum; auto tmp = n; while (tmp != 0) { auto x = tmp % 10; sum += x; tmp /= 10; } if (n % sum) { writeln("No"); } else { writeln("Yes"); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto N = RD; auto A = RD; auto B = RD; auto ans = N / (A+B) * A; ans += min(A, N % (A+B)); writeln(ans); stdout.flush; debug readln; }
D
import std.stdio; import std.algorithm; const int BORING = 15; const int MATCH = 90; void main() { int n; int last = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); if ((x - 1) - last >= BORING) { printf("%d\n", last + BORING); return; } last = x; } printf("%d\n", min(last + BORING, MATCH)); }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(int a, int b) { if (b == 1) return 0; int ans = 1; int x = a; while (x < b) { x += a - 1; ans++; } return ans; } void main() { int a, b; scan(a, b); writeln(calc(a, b)); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { string s; scan(s); int ans; foreach (i ; 1 .. s.length) { if (s[i] != s[i - 1]) ans++; } writeln(ans); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto S = RDA; auto imos = new long[](n+1); auto a = new long[](n+1); foreach (i; 0..n) { auto d = S[i] - imos[i] - a[i]; if (d >= 1) ans[ti] += d - 1; else a[i+1] += (-d) + 1; auto l = cast(int)(i + 2); auto r = cast(int)(i + S[i] + 1); if (l <= n) ++imos[l]; if (r <= n) --imos[r]; imos[i+1] = min(imos[i+1] + imos[i], 10^^10); } } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio,std.string,std.conv,std.algorithm; import std.algorithm:rev=reverse; int main(){ auto input=readln.chomp.split; auto a=input[0].to!int; auto b=input[1].to!int; auto at=(a*13).to!int; auto bt=(b*10).to!int; foreach(y;bt..at+1){ if((y*0.08).to!int==a&&(y*0.1).to!int==b){ y.writeln; return 0; } } "-1".writeln; return 0; }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; import std.string; void main() { string str = readln.chomp; string[4] ss = ["dream", "erase", "eraser", "dreamer"]; // array.lengthの返り値の型はulongなので // intに代入しようとすると(dmd64 v2.070.1では)CEになる int p = str.length.to!int; while (p > 0) { bool flag = false; if (str[p - 1] == 'r') { for (int i = 2; i < 4; ++i) { if (p < ss[i].length.to!int) { break; } if (ss[i] == str[(p - ss[i].length.to!int)..p]) { p -= ss[i].length.to!int; flag = true; break; } } } else if (str[p - 1] == 'e' && p >= 5) { if (str[(p - ss[1].length.to!int)..p] == ss[1]) { flag = true; p -= ss[1].length.to!int; } } else if (str[p - 1] == 'm' && p >= 5) { if (str[(p - ss[0].length.to!int)..p] == ss[0]) { flag = true; p -= ss[0].length.to!int; } } if (!flag) { writeln("NO"); return; } } writeln("YES"); return; }
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, a, b; scan(n, a, b); auto mx = min(a, b); auto mn = max(0, a + b - n); writeln(mx, " ", mn); } 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