code
stringlengths
4
1.01M
language
stringclasses
2 values
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N, K; scan(N, K); auto G = new long[][](N); foreach (i; 0 .. N - 1) { long a, b; scan(a, b); a--, b--; G[a] ~= b; G[b] ~= a; } auto visited = new bool[](N); auto C = Combination_mod(K + 1); // writeln(C._fact); long dfs1(long p, long v) { if (visited[v]) return 1; visited[v] = true; long x = (p == -1) ? C(K - 1, G[v].length) : C(K - 2, (cast(long) G[v].length) - 1); // writefln("%s %s %s %s", v, G[v].length, x, C._fact[G[v].length - 1 - min(0, p)]); x *= C._fact[G[v].length - 1 - min(0, p)]; x %= MOD; foreach (w; G[v]) if (w != p) { x *= dfs1(v, w); x %= MOD; } return x; } writeln(K * dfs1(-1, 0) % MOD); } /// Number of k-combinations % m (precalculated) alias Combination_mod = Combination_modImpl!long; struct Combination_modImpl(T) { T _n, _m; T[] _fact, _factinv; this(T maxnum, T mod = 10 ^^ 9 + 7) { _n = maxnum, _m = mod, _fact = new T[](_n + 1), _factinv = new T[](_n + 1), _fact[0] = 1; foreach (i; 1 .. _n + 1) _fact[i] = _fact[i - 1] * i % _m; T powmod(T x, T n, T m) { if (n < 1) return 1; if (n & 1) { return x * powmod(x, n - 1, m) % m; } T tmp = powmod(x, n / 2, m); return tmp * tmp % m; } foreach (i; 0 .. _n + 1) _factinv[i] = powmod(_fact[i], _m - 2, _m); } T opCall(T n, T k, T dummy = 10 ^^ 9 + 7) { return n < k ? 0 : ((_fact[n] * _factinv[n - k] % _m) * _factinv[k] % _m); } }
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; bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); } void main(string[] args){ args ~= ["", ""]; string cmd = args[1]; if(cmd == "-debug") DEBUG = 1; if(cmd == "-gen") gen; else if(cmd == "-jury") jury; else solve; } void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ std.stdio.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 gen(){} void jury(){} void solve(){ int n = scan!int, m = scan!int; long k = scan!long; long[] as = scan!long(n); long[] bs = scan!long(m); long[] asum = [0], bsum = [0]; foreach(a; as) asum ~= asum[$ - 1] + a; foreach(b; bs) bsum ~= bsum[$ - 1] + b; int ans; foreach(int i; 0 .. n + 1){ if(asum[i] > k) break; int tmpans = i + uplimit(0, m, (int j) => asum[i] + bsum[j] <= k); ans.raiseTo(tmpans); } ans.writeln; } // 二分探索 // a 以上 c 以下の範囲で f(x) をみたす最大の x // ただし、a がみたさない場合は a - 1 を返す ※範囲外エラーに注意 // f は単調減少であること T uplimit(T)(T a, T c, bool delegate(T) f){ if(f(c)) return c; if(! f(a)) return a - 1; while(a + 1 < c){ T b = (a + c) / 2; if(f(b)) a = b; else c = b; } return a; } // a 以上 c 以下の範囲で f(x) をみたす最小の x // ただし、c がみたさない場合は c + 1 を返す ※範囲外エラーに注意 // f は単調増加であること T downlimit(T)(T a, T c, bool delegate(T) f){ if(f(a)) return a; if(! f(c)) return c + 1; while(a + 1 < c){ T b = (a + c) / 2; if(f(b)) c = b; else a = b; } return c; }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { auto s = sread(); auto t = sread(); // writeln(s); long ans = long.max; foreach (i; 0 .. ((s.length - t.length) + 1)) { // writeln(s[i .. (i + (t.length))]); ans = min(ans, func(s[i .. (i + (t.length))], t)); } writeln(ans); } long func(string s, string t) { long cnt; foreach (i; 0 .. s.length) { if (s[i] != t[i]) { cnt += 1; } } return cnt; } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.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 d = readln.chomp.split.to!(int[]); int res; foreach (_; 0..2) { if (d[0] > d[1]) { res += d[0]; d[0]--; } else { res += d[1]; d[1]--; } } res.writeln; }
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 main() { long N, Q; scan(N, Q); auto S = sread(); auto T = new char[](Q); auto D = new char[](Q); foreach (i; 0 .. Q) scan(T[i], D[i]); bool isSurviveLeft(long x) { long now = x; foreach (i; 0 .. Q) if (T[i] == S[now]) { now += (D[i] == 'R') ? 1 : -1; if (now == -1) return false; if (now == N) return true; } return true; } bool isSurviveRight(long x) { long now = x; foreach (i; 0 .. Q) if (T[i] == S[now]) { now += (D[i] == 'R') ? 1 : -1; if (now == -1) return true; if (now == N) return false; } return true; } long l = () { long ok = N; long ng = -1; while (1 < abs(ok - ng)) { long m = (ok + ng) / 2; if (isSurviveLeft(m)) { ok = m; } else { ng = m; } } return ok; }(); long r = () { long ok = -1; long ng = N; while (1 < abs(ok - ng)) { long m = (ok + ng) / 2; if (isSurviveRight(m)) { ok = m; } else { ng = m; } } return ok; }(); // iota(N).map!isSurviveLeft().writeln(); // iota(N).map!isSurviveRight().writeln(); // writeln(l); // writeln(r); writeln(r - l + 1); }
D
void main() { long n = rdElem; long[] a = rdRow; a[] -= 1; long[] list = new long[n]; foreach (x; a) { ++list[x]; } foreach (l; list) { l.writeln; } } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; enum double eps = 1.0e-9; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
import std.stdio, std.conv, std.string, std.algorithm, std.range, std.math; void main() { alias f = a => a == 2 ? 0 : (a == 4 || a == 6 || a== 9 || a == 11) ? 1 : 2; auto po = readln.split.to!(int[]).map!f.array; writeln(po[0] == po[1] ? "Yes" : "No"); }
D
import std.stdio; import std.conv; import std.string; import std.algorithm; void main() { string s; while( (s=readln.strip) != "0 0") { auto n = map!(to!int)(s.strip.split); for(int i=0;i<n[0];i++) { for(int j=0;j<n[1];j++) { write("#"); } writeln(); } writeln(); } }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random; import std.typecons, std.functional, std.traits,std.concurrency; import std.algorithm, std.container; import core.bitop, core.time, core.memory; import std.bitmanip; import std.regex; enum INF = long.max/3; enum MOD = 10L^^9+7; T[] scanArray(T = long)() { static char[] scanBuf; readln(scanBuf); return scanBuf.split.to!(T[]); } dchar scanChar() { import core.stdc.stdlib; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } return cast(dchar)c; } T scanElem(T = long)() { import core.stdc.stdlib; static auto scanBuf = appender!(char[])([]); scanBuf.clear; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { scanBuf ~= cast(char) c; c = getchar; } return scanBuf.data.to!T; } dchar[] scanString(){ return scanElem!(dchar[]); } void main() { auto A = scanElem; writeln(A/3); }
D
import std.stdio; import std.ascii; import std.conv; import std.string; import std.algorithm; import std.range; import std.functional; import std.math; import core.bitop; import std.numeric; void main() { auto ab = readln.split.to!(long[]); (ab[0] - ab[1] + 1).writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; long P = 10^^9+7; long[10^^5*2+50] F, RF; long pow(long x, long n) { long y = 1; while (n) { if (n%2 == 1) y = (y * x) % P; x = x^^2 % P; n /= 2; } return y; } long inv(long x) { return pow(x, P-2); } void init() { F[0] = F[1] = 1; foreach (i, ref x; F[2..$]) x = (F[i+1] * (i+2)) % P; { RF[$-1] = 1; auto x = F[$-1]; auto k = P-2; while (k) { if (k%2 == 1) RF[$-1] = (RF[$-1] * x) % P; x = x^^2 % P; k /= 2; } } foreach_reverse(i, ref x; RF[0..$-1]) x = (RF[i+1] * (i+1)) % P; } long comb(N)(N n, N k) { if (k > n) return 0; auto n_b = F[n]; // n! auto nk_b = RF[n-k]; // 1 / (n-k)! auto k_b = RF[k]; // 1 / k! auto nk_b_k_b = (nk_b * k_b) % P; // 1 / (n-k)!k! return (n_b * nk_b_k_b) % P; // n! / (n-k)!k! } void main() { init(); auto hwab = readln.split.to!(long[]); auto H = hwab[0]; auto W = hwab[1]; auto A = hwab[2]; auto B = hwab[3]; long r; foreach (i; 0..H-A) { (r += F[B-1+i] * RF[B-1] % P * RF[i] % P * F[W-B-1 + H-i-1] % P * RF[W-B-1] % P * RF[H-i-1] % P) %= P; } writeln(r); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { string s; scan(s); yes(s.canFind('7')); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; immutable inf = 1<<30; void main() { int n, k; scan(n); scan(k); int ans = inf; void dfs(int i, int val) { if (i == n) { ans = min(ans, val); return; } dfs(i + 1, val + k); dfs(i + 1, val * 2); } dfs(0, 1); 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.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio, std.bitmanip; int N; int[] colors; int[][] adj; void main() { N = readln.chomp.to!int; adj = new int[][](N); foreach (i; 0..N-1) { auto s = readln.split.map!(to!int).array; adj[s[0]-1] ~= s[1]-1; adj[s[1]-1] ~= s[0]-1; } colors = readln.split.map!(to!int).array; int[int] cnt; foreach (c; colors) { if (c in cnt) cnt[c]++; else cnt[c] = 1; } if (cnt.keys.length == 1) { writeln("YES"); writeln(1); } else { Tuple!(int, int)[] borders; foreach (i; 0..N) { foreach (j; adj[i]) { if (colors[i] != colors[j]) { borders ~= tuple(i, j); } } } foreach (a; borders[0]) { bool flag = true; foreach (b; borders) { if (a != b[0] && a != b[1]) { flag = false; break; } } if (flag) { writeln("YES"); writeln(a+1); return; } } writeln("NO"); } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; 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; void main() { long N, M; scan(N, M); auto uf = UnionFind(N); foreach (_; 0 .. M) { long X, Y, Z; scan(X, Y, Z); uf.unite(X - 1, Y - 1); } uf.size.writeln(); } struct UnionFind { private long[] rank; long[] data; long size; this(long N) { this.rank = new long[](N); this.data = iota!long(N).array; this.size = N; } long find(long x) { if (data[x] == x) return x; long r = this.find(data[x]); data[x] = r; return r; } void unite(long x, long y) { long rx = find(x); long ry = find(y); if (rx != ry) { this.size--; if (rank[rx] == rank[ry]) { data[rx] = ry; rank[ry]++; } else if (rank[rx] < rank[ry]) data[rx] = ry; else data[ry] = rx; } } bool isSame(long x, long y) { return this.find(x) == this.find(y); } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Clue = Tuple!(long, "x", long, "y", long, "h"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); long cnt; void main() { long n, a, b; scan(n, a, b); auto t = n / (a + b); auto r = n - t * (a + b); writeln(a * t + min(a, r)); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.stdio; import std.string; import std.array; import std.range; import std.random; import std.algorithm; import std.conv; void main(){ auto input = readln().chomp(); solve(input).writeln(); } int solve(string input){ auto result = 0; foreach(s; input){ if(s == '+'){ result++; }else{ result--; } } return result; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; long calc(int x, int y, int z) { long lo = 0; long hi = x; long ans = 0; while (lo <= hi) { long m = (lo + hi) / 2; long d = m * y + (m + 1) * z; if (d <= x) { ans = max(ans, m); lo = m + 1; } else hi = m - 1; } return ans; } void main() { auto xyz = readints; int x = xyz[0], y = xyz[1], z = xyz[2]; writeln(calc(x, y, z)); }
D
import std; auto input() { return readln().chomp(); } alias sread = () => readln.chomp(); alias aryread(T = long) = () => readln.split.to!(T[]); void main() { long a, b; scan(a, b); foreach (i; 0 .. 1001) { if (((i * 8) / 100 == a) && ((i * 10) / 100 == b)) { writeln(i); return; } } writeln(-1); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
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; } void main(){ int a = read.to!int; int b = read.to!int; string ans; if(a <= 8 && b <= 8) ans = "Yay!"; else ans = ":("; ans.writeln; }
D
unittest { assert( [ "erasedream" ].parse.expand.solve == "YES" ); assert( [ "dreameraser" ].parse.expand.solve == "YES" ); assert( [ "dreamerer" ].parse.expand.solve == "NO" ); } import std.conv; import std.range; import std.stdio; import std.typecons; void main() { stdin.byLineCopy.parse.expand.solve.writeln; } auto parse( Range )( Range input ) if( isInputRange!Range && is( ElementType!Range == string ) ) { auto s = input.front; return tuple( s ); } auto solve( string s ) { LOOP: while( 0 < s.length ) { foreach( w; [ "dream", "dreamer", "erase", "eraser" ] ) { if( w.length <= s.length && s[ $ - w.length .. $ ] == w ) { s.length -= w.length; continue LOOP; } } return "NO"; } return "YES"; }
D
import std.stdio; import std.math; import std.algorithm; import std.conv; long bisect(T)(T[] arr, T p) { long u, t = arr.length-1; long i = (u + t) / 2; while(i != 0 && i != arr.length - 1 && (arr[i] > p || arr[i+1] <= p)) { if (arr[i] > p) { t = i; } else { if (u == i) u = i + 1; else u = i; } i = (t + u) / 2; } return i; } unittest { assert(bisect!int([1, 2, 3, 4], 1) == 0); assert(bisect!int([1, 2, 3, 4], 2) == 1); assert(bisect!int([1, 2, 3, 4], 3) == 2); assert(bisect!int([1, 2, 3, 4], 4) == 3); } void main() { long a, b, q; long[] s, t, x; scanf("%ld %ld %ld", &a, &b, &q); foreach (i;0..a+b+q) { long y; scanf("%ld", &y); if (i < a) { s ~= y; } else if (i < a + b) { t ~= y; } else { x ~= y; } } s.sort!"a<b"; t.sort!"a<b"; s = -100000000000 ~ s ~ 100000000000; t = -100000000000 ~ t ~ 100000000000; foreach(x_;x) { auto sc = s.bisect!long(x_); auto tc = t.bisect!long(x_); auto tsl = s[sc+1] - x_; auto tsr = x_ - s[sc]; auto ttl = t[tc+1] - x_; auto ttr = x_ - t[tc]; writeln([ max(tsr, ttr), max(tsl, ttl), tsr*2+ttl, tsr+ttl*2, tsl*2+ttr, tsl+ttr*2 ].reduce!((a, b) => min(a,b))); } }
D
import std.stdio,std.string,std.conv,std.array; void main(){ for(;;){ auto rcs = readln().chomp().split(); auto n = to!int(rcs[0]); auto x = to!int(rcs[1]); if( n==0 && x==0 ){ break; } int hit=0; for( int a=1;a <= n-2; a++ ){ for( int b=a+1;b <= n-1; b++ ){ for( int c=b+1;c <= n; c++ ){ if( a+b+c == x ){ hit++; } } } } writeln(hit); } }
D
void main() { int n = readln.chomp.to!int; int[] l = readln.split.to!(int[]); writeln(l.sum > 2 * l.reduce!max ? "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.container; import std.typecons;
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.container, std.math, std.typecons; immutable int max = 2 * 10^^5 + 10; int n, k, q; //int[] l, r; void main() { scan(n, k, q); auto adm = new int[](max); int li, ri; foreach (i ; 0 .. n) { scan(li, ri); adm[li]++; adm[ri+1]--; } iota(max - 1).each!(i => adm[i + 1] += adm[i]); debug { stderr.writeln("adm:", adm[0 .. 120]); } adm = adm.map!(a => (a >= k).to!int).array; debug { stderr.writeln("adm:", adm[0 .. 120]); } iota(max - 1).each!(i => adm[i + 1] += adm[i]); debug { stderr.writeln("adm:", adm[0 .. 150]); } int ai, bi, ans; foreach (i ; 0 .. q) { scan(ai, bi); ans = adm[bi] - adm[ai - 1]; ans.writeln; } } 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); }
D
import std.stdio, std.conv, std.array, std.string; import std.algorithm; void main() { string s = readln.chomp; string prev; uint count = 0; auto n = s.length; for(uint i=0; i<n; i++) { if (s[i..i+1] == prev) { if (i+1 == n) break; prev = s[i..i+2]; count++; i++; continue; } prev = s[i..i+1]; count++; } writeln(count); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "x", long, "y"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long a, b, c; scan(a, b, c); long l = 4 * a * b; long r = (c - (a + b)) ^^ 2; if(l < r && (a + b) < c) writeln("Yes"); else writeln("No"); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.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 a = RDA; auto x = a.minElement; foreach (i; 0..n) { if (a[i] > x) ++ans[ti]; } } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
unittest { assert( [ "2019/04/30" ].parse.expand.solve == "Heisei" ); assert( [ "2019/11/01" ].parse.expand.solve == "TBD" ); } import std.conv; import std.range; import std.stdio; import std.typecons; void main() { stdin.byLineCopy.parse.expand.solve.writeln; } auto parse( Range )( Range input ) if( isInputRange!Range && is( ElementType!Range == string ) ) { auto s = input.front; return tuple( s ); } auto solve( string s ) { return ( s <= "2019/04/30" ) ? "Heisei" : "TBD"; }
D
import std.stdio, std.conv, std.array, std.string; import std.algorithm.iteration, std.algorithm.mutation, std.algorithm.searching, std.algorithm.sorting; void main() { int N = readln().strip.to!int; if(N==1) { writeln("Hello World"); }else{ auto r = readln().strip.to!int + readln().strip.to!int; writeln(r); } }
D
import std.stdio; import std.string, std.conv, std.array, std.algorithm; import std.uni, std.math, std.container; import core.bitop, std.datetime; void main(){ auto N = readln.chomp.to!int; int ans = N * (N + 1) / 2; ans.writeln; }
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } void main(){ auto n = read.to!long; auto a = read.to!long; auto b = read.to!long; long[] ps; foreach(i; 0 .. n) ps ~= read.to!long; long c1, c2, c3; foreach(p; ps){ if(p <= a) c1 += 1; else if(p <= b) c2 += 1; else c3 += 1; } long ans = n; if(c1 < ans) ans = c1; if(c2 < ans) ans = c2; if(c3 < ans) ans = c3; if(ans == n) ans = 0; ans.writeln; }
D
void main() { long n = readln.chomp.to!long; long[] a = new long[n], b = new long[n]; foreach (i; 0 .. n) { long[] tmp = readln.split.to!(long[]); a[i] = tmp[0], b[i] = tmp[1]; } long buttons; foreach_reverse (i; 0 .. n) { long r = (a[i] + buttons) % b[i]; if (r) { buttons += b[i] - r; } } buttons.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.string, std.conv, std.bigint, std.typecons, std.algorithm, std.array, std.math, std.range, std.functional; void main() { auto tmp = readln.split.to!(int[]); writeln(max(0, min(tmp[3], tmp[1]) - max(tmp[2], tmp[0]))); }
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 ---+/ void main(string[] args) { const N = readln.chomp.to!long; (N.pow(3)).writeln; }
D
import std.stdio, std.string, std.array, std.conv, std.algorithm; long solve(long n, long m) { if (2*n > m) return m / 2; auto ret = n; m -= n*2; ret += m/4; return ret; } void main() { auto nm = readln.split.to!(long[]); writeln(solve(nm[0], nm[1])); }
D
import std.conv; import std.stdio; import std.string; void main() { auto r = readln.strip.to!int; writeln( solve( r ) ); } auto solve( in int r ) { if( r < 1200 ) return "ABC"; if( r < 2800 ) return "ARC"; return "AGC"; }
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 H, W, K; scan(H, W, K); auto C = new string[](H); foreach (i; 0 .. H) C[i] = sread(); char[][] f(uint h, uint w) { auto ret = new char[][](H, W); foreach (i; 0 .. H) foreach (j; 0 .. W) { if ((h & (1 << i)) || (w & (1 << j))) { ret[i][j] = 'r'; } else { ret[i][j] = C[i][j]; } } return ret; } long eval(char[][] s) { long ret; foreach (i; 0 .. H) foreach (j; 0 .. W) ret += s[i][j] == '#'; return ret; } long ans; foreach (i; 0 .. 1 << H) foreach (j; 0 .. 1 << W) { auto s = f(i, j); if (eval(s) == K) ans++; } writeln(ans); }
D
import std.stdio; void main(){ auto cin = new Cin(); auto S = cin.line!string()[0]; bool succeed = false; foreach( i ; 0..S.length-1 ){ if( S[i]==S[i+1] ){ writeln( i+1," ",i+2 ); return; } } foreach( i ; 0..S.length-2 ){ if( S[i]==S[i+2] ){ writeln( i+1," ",i+3 ); return; } } writeln( "-1 -1" ); return; } auto solve(){ } unittest{ } import std.stdio,std.conv,std.string; import std.algorithm,std.array,std.math; class Cin { T[] line( T = size_t , string token = " " )( size_t m = 1 ){ T[] arr = []; foreach( i ; 0..m ){ arr ~= this.read!T(); } return arr; } T[][] rect( T = size_t , string token = " " )( size_t m = 1 ){ T[][] arr = new T[][](m); foreach( i ; 0..m ){ arr[i] = this.read!T(token); } return arr; } private T[] read( T = size_t )( string token = " " ){ T[] arr; foreach( elm ; readln().chomp().split(token) ){ arr ~= elm.to!T(); } return arr; } } pure T[] span(T)( in T begein , in T end , in T step = 1 ){ assert( begin < end ); assert( 0 < step ); T[] arr; for( T i = begein ; i < end ; i += step ){ arr ~= i; } return arr; } pure T[] map(T)( ref T[] args , T delegate(T) dlg ){ foreach( ref arg ; args ){ arg.dlg(); } return args; } pure T sum(T)( in T[] args ){ T result = 0; foreach( arg ; args ){ result += arg; } debug writeln( args , "\n" , arg ); return result; } pure T ave(T)( in T[] args ){ return args.sum()/args.length; }
D
import std.stdio,std.conv,std.string,std.algorithm,std.array,std.math; void main(){ auto s=readln().chomp().split().map!(to!int); int a,b,c,d; a=s[0]; b=s[1]; c=s[2]; d=s[3]; if(abs(c-a)<=d) writeln("Yes"); else if(abs(b-a)<=d && abs(c-b)<=d) writeln("Yes"); else writeln("No"); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; void main() { auto n = readln.chomp.to!int; foreach (i; 0..n) { auto as = readln.split.map!(to!int); int l ,r; bool f; foreach (a; as) { if (l < a) l = a; else if (r < a) r = a; else { f = true; } } writeln(f?"NO":"YES"); } }
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 s = readln.chomp.split; writeln(s[1] ~ s[0]); }
D
import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; class A{ int a; int b; int c; int p; this(int a, int b, int c){ this.a = a, this.b = b, this.c = c; this.p = ma * b - mb * a; } } int n, ma, mb; // 問題はsum(p) = 0 となるときのsum(c)の最小値 void main(){ { int[] tmp = readln.chomp.split.map!(to!int).array; n = tmp[0], ma = tmp[1], mb = tmp[2]; } A[] as; for(int i = 0; i < n; i ++){ int[] tmp = readln.chomp.split.map!(to!int).array; as ~= new A(tmp[0], tmp[1], tmp[2]); } int[int] dic1, dic2; int n2 = 0; if(n > 6){ n2 = n / 2; { int i = n2 - 1; long m = 2 ^^ i; int sump = 0; int sumc = 0; long k = 0; while(1){ if(sumc > 0){ if(sump in dic1){ if(sumc < dic1[sump]) dic1[sump] = sumc; } else dic1[sump] = sumc; } while(k & m){ k -= m; sump -= as[i].p; sumc -= as[i].c; i -= 1, m /= 2; } if(m > 0){ k += m; sump += as[i].p; sumc += as[i].c; i = n2 - 1, m = 2 ^^ i; } else break; } } } /*anyway*/ { int i = n - n2 - 1; long m = 2 ^^ i; int sump = 0; int sumc = 0; long k = 0; while(1){ if(sumc > 0){ if(sump in dic2){ if(sumc < dic2[sump]) dic2[sump] = sumc; } else dic2[sump] = sumc; } while(k & m){ k -= m; sump -= as[i + n2].p; sumc -= as[i + n2].c; i -= 1, m /= 2; } if(m > 0){ k += m; sump += as[i + n2].p; sumc += as[i + n2].c; i = n - n2 - 1, m = 2 ^^ i; } else break; } } debug dic1.writeln; debug dic2.writeln; int minsumc = int.max; if(0 in dic1) minsumc = min(minsumc, dic1[0]); if(0 in dic2) minsumc = min(minsumc, dic2[0]); foreach(sp; dic1.keys){ if(-sp in dic2){ int s = dic1[sp] + dic2[-sp]; if(s < minsumc && s > 0) minsumc = s; } } int ans; if(minsumc == int.max) ans = -1; else ans = minsumc; ans.writeln; }
D
void main() { string s = rdStr; long n = s.length; bool ok = true; foreach (i; 0 .. n/2) { if (s[i] != s[n-i-1]) ok = false; } long m = (n - 1) / 2; foreach (i; 0 .. m) { if (s[i] != s[m-i-1]) ok = false; if (s[m+i+1] != s[n-i-1]) ok = false; } writeln(ok ? "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 std.stdio; import std.string; import std.conv; import std.algorithm; import std.math; import std.regex; import std.range; import core.exception; void main() { auto N = readln.chomp.to!int; auto a = new int[](N); foreach(i; 0..N) { a[i] = readln.chomp.to!int; } bool[int] b; foreach(i; 0..N+1) b[i] = false; int current = 1, count; while(true) { if(b[current]) { (-1).writeln; return; } if(current == 2) { count.writeln; return; } b[current] = true; current = a[current-1]; count++; } }
D
import std.stdio, std.range, std.random, std.conv, std.string; import std.algorithm.comparison, std.algorithm.iteration, std.algorithm.mutation, std.algorithm.searching, std.algorithm.sorting; void main() { int[] input = readln().strip.split().to!(int[]); int N = input[0]; int T = input[1]; int minCost = int.max; foreach(i; 0..N) { input = readln().strip.split.to!(int[]); if(input[1] <= T) { minCost = min(minCost, input[0]); } } if(minCost == int.max) { writeln("TLE"); return; } writeln(minCost); }
D
import std.stdio, std.conv, std.string, std.math; void main(){ ((a, b, h) => (a + b) * h / 2)(r, r, r).writeln; } int r() { return readln.chomp.to!int; }
D
char t(ulong c) { switch(c) { case 0: return 'A'; case 1: return 'C'; case 2: return 'G'; case 3: return 'T'; default: return 'x'; } } void main() { auto N = ri; ulong MOD = 1000000007; auto dp = new long[][][][](N+1, 4, 4, 4); dp[0][3][3][3] = 1; foreach(n; 0..N) foreach(t1; 0..4) foreach(t2; 0..4) foreach(t3; 0..4) { if(dp[n][t1][t2][t3] == 0) continue; foreach(a; 0..4) { char c1 = t1.t, c2 = t2.t, c3 = t3.t, c4 = a.t; if([c1, c3, c4] == "AGC") continue; if([c1, c2, c4] == "AGC") continue; if([c2, c3, c4] == "AGC") continue; if([c2, c3, c4] == "GAC") continue; if([c2, c3, c4] == "ACG") continue; dp[n+1][t2][t3][a] += dp[n][t1][t2][t3]; dp[n+1][t2][t3][a] %= MOD; } } long res; foreach(t1; 0..4) foreach(t2; 0..4) foreach(t3; 0..4) { res += dp[N][t1][t2][t3]; res %= MOD; } 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, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; immutable long MOD = 998244353; void main() { auto s = readln.split.map!(to!long); auto N = s[0].to!int; auto A = s[1]; auto B = s[2]; auto K = s[3]; auto Comb = new Combination(); long ans = 0; foreach (a; 0..N+1) { long b = (K- a * A) / B; if ((K - a * A) % B != 0) continue; if (b < 0 || b > N) continue; ans += Comb.comb(N, a.to!int) * Comb.comb(N, b.to!int) % MOD; ans %= MOD; } ans.writeln; } class Combination { immutable int MAX = 2*10^^6+1; long[] modinv; long[] f_mod; long[] f_modinv; this() { modinv = new long[](MAX); modinv[0] = modinv[1] = 1; foreach(i; 2..MAX) { modinv[i] = modinv[MOD.to!int % i] * (MOD - MOD / i) % MOD; } f_mod = new long[](MAX); f_modinv = new long[](MAX); f_mod[0] = f_mod[1] = 1; f_modinv[0] = f_modinv[1] = 1; foreach(i; 2..MAX.to!int) { f_mod[i] = (i * f_mod[i-1]) % MOD; f_modinv[i] = (modinv[i] * f_modinv[i-1]) % MOD; } } long comb(int n, int k) { if (n < k) return 0; return f_mod[n] * f_modinv[n-k] % MOD * f_modinv[k] % MOD; } } long powmod(long a, long x, long m) { long ret = 1; while (x) { if (x % 2) ret = ret * a % m; a = a * a % m; x /= 2; } return ret; }
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 T = lreads(N); long ans = T[0]; foreach (t; T[1 .. $]) ans = lcm(ans, t); writeln(ans); } T lcm(T)(T a, T b) { return a / gcd(a, b) * b; }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random; import std.typecons, std.functional, std.traits,std.concurrency; import std.algorithm, std.container; import core.bitop, core.time, core.memory; import std.bitmanip; import std.regex; enum INF = long.max/3; enum MOD = 10L^^9+7; //辞書順順列はiota(1,N),nextPermituionを使う void end(T)(T v) if(isIntegral!T||isSomeString!T||isSomeChar!T) { import core.stdc.stdlib; writeln(v); exit(0); } T[] scanArray(T = long)() { static char[] scanBuf; readln(scanBuf); return scanBuf.split.to!(T[]); } dchar scanChar() { int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } return cast(dchar)c; } T scanElem(T = long)() { import core.stdc.stdlib; static auto scanBuf = appender!(char[])([]); scanBuf.clear; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { scanBuf ~= cast(char) c; c = getchar; } return scanBuf.data.to!T; } dchar[] scanString(){ return scanElem!(dchar[]); } void main() { auto n=scanElem; writeln(n*800-n/15*200); }
D
import std.stdio, std.string, std.conv; void main() { auto input = getStdin!(string[])()[0]; int[] num = input.split(" ").to!(int[]); if (num[0] < num[1]) { if (num[1] < num[2]) { "Yes".writeln; return; } } "No".writeln; } T getStdin(T)() { string[] cmd; string line; while ((line = chomp(stdin.readln())) != "") cmd ~= line; return to!(T)(cmd); }
D
// import chie template :) {{{ import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons; // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string : chomp; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.chomp.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 main() { auto cin = new Scanner; int n, m; cin.scan(n, m); writeln((n - 1) * (m - 1)); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; int[] AS, cs; foreach (_; 0..N) AS ~= readln.chomp.to!int; foreach (a; AS) { if (cs.empty || cs[$-1] >= a) { cs ~= a; } else if (cs[0] < a) { cs[0] = a; } else { int l, r = cs.length.to!int-1; while (l+1 < r) { auto m = (l+r)/2; if (a > cs[m]) { r = m; } else { l = m; } } cs[r] = a; } } writeln(cs.length); }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; import std.string; import std.uni; import std.range; import std.algorithm; void main() { auto n = readln.strip.to!int; auto h = 0, t = 0; while (n--) { auto k = readln.strip.split(" "); if (k[0] == k[1]) { t++; h++; } else if (k[0] > k[1]) { t += 3; } else { h += 3; } } writeln(t, " ", h); }
D
import std.stdio; import core.stdc.stdio; import core.memory; import std.algorithm; import std.typecons; int TopBit(int a){ for(int i=18;i>=0;i--) if((1<<i)&a) return i; return -1; } alias Tuple!(int,"val",int,"idx") vi; struct SparseTable{ vi[][] st; void Initialize(vi[] d){ int h=TopBit(cast(int)d.length)+1; st = new vi[][](h,d.length); for(int i=0;i<d.length;i++) st[0][i] = d[i]; for(int j=1;j<h;j++) for(int i=0;i<d.length-(1<<j)+1;i++) st[j][i] = min(st[j-1][i],st[j-1][i+(1<<(j-1))]); } vi get(int begin,int end){ int h = TopBit(end-begin); return min(st[h][begin],st[h][end-(1<<h)]); } } int n; int[][] tree; int[] subTreeCount; int[] invFirstIdx; struct LCA{ SparseTable st; vi[] eulertour; int[] firstIdx; int ei=0; int sc=0; void calcEulerTour(){ alias Tuple!(int,"idx",int,"depth",int,"par") ev; ev[] evs = new ev[n*2]; int ec = 0; evs[ec++] = ev(0,0,-1); for(int i=0;i<n*2-1;i++){ ec--; with(evs[ec]){ eulertour[i] = vi(depth,idx); if(firstIdx[idx] == -1){ firstIdx[idx] = i; invFirstIdx[i] = idx; foreach(c;tree[idx]){ if(c==par) continue; evs[ec++] = ev(idx,depth,par); evs[ec++] = ev(c,depth+1,idx); } } } } } void calcSubTreeCount(){ int[] stctmp = new int[n]; stctmp[] = 1; foreach_reverse(inv;invFirstIdx){ if(inv!=-1){ subTreeCount[inv] = stctmp[inv]; foreach(c;tree[inv]) stctmp[c] += subTreeCount[inv]; } } } void Initialize(){ eulertour = new vi[n*2]; firstIdx = new int[n]; firstIdx[] = -1; invFirstIdx = new int[n*2-1]; invFirstIdx[] = -1; calcEulerTour; calcSubTreeCount; st.Initialize(eulertour[0..n*2-1]); } int get(int a,int b){ a = firstIdx[a]; b = firstIdx[b]; if(a>b) swap(a,b); return st.get(a,b+1).idx; } } alias Tuple!(int,"l",int,"r",int,"m",int,"a") sums; sums mergeSums(sums a,sums b){ sums ret; ret.l = max(a.l,a.a+b.l); ret.m = max(a.m,b.m,a.r+b.l); ret.r = max(a.r + b.a,b.r); ret.a = a.a+b.a; return ret; } class SegmentTree{ sums[] data; int[] delay; int size; void Initialize(int[] d){ size = 1; while(size<d.length)size*=2; data = new sums[size*2]; delay = new int[size*2]; for(int i=size;i<size+d.length;i++) data[i] = sums(d[i-size],d[i-size],d[i-size],d[i-size]); for(int i=size-1;i>=1;i--) data[i] = mergeSums(data[i*2],data[i*2+1]); for(int i=1;i<size*2;i++) delay[i] = int.min; } void passDelay(int idx,int s){ if(delay[idx]<0){ data[idx].l = delay[idx]; data[idx].r = delay[idx]; data[idx].m = delay[idx]; }else{ data[idx].l = delay[idx]*s; data[idx].r = delay[idx]*s; data[idx].m = delay[idx]*s; } data[idx].a = delay[idx]*s; if(idx<size){ delay[idx*2] = delay[idx]; delay[idx*2+1] = delay[idx]; } delay[idx] = int.min; } int begin; int end; int w; void _update(int idx,int l,int r){ if(end<=l||r<=begin) return; if(begin<=l&&r<=end){ delay[idx] = w; return; } if(delay[idx] != int.min){ passDelay(idx,r-l); } _update(idx*2,l,(l+r)/2); _update(idx*2+1,(l+r)/2,r); if(delay[idx*2] != int.min) passDelay(idx*2,(r-l)/2); if(delay[idx*2+1] != int.min) passDelay(idx*2+1,(r-l)/2); data[idx] = mergeSums(data[idx*2],data[idx*2+1]); } void Update(int _begin,int _end,int _w){ if(_begin>=_end) return; begin = _begin; end = _end; w = _w; _update(1,0,size); } sums get(int idx,int l,int r){ if(begin<=l&&r<=end){ if(delay[idx] != int.min) passDelay(idx,r-l); return data[idx]; } if(delay[idx] != int.min){ passDelay(idx,r-l); } if(begin>=(l+r)/2) return get(idx*2+1,(l+r)/2,r); if(end<=(l+r)/2) return get(idx*2,l,(l+r)/2); return mergeSums(get(idx*2,l,(l+r)/2),get(idx*2+1,(l+r)/2,r)); } sums Get(int _begin,int _end){ begin = _begin; end = _end; return get(1,0,size); } } int[] weights; struct HLdecomposition{ LCA lca; int[] group; int[] invPath; int[] gsize; int[] pathData; int[][] paths; int[] pathParent; SegmentTree[] segs; int gi=0; void calcHeavyLight(){ alias Tuple!(int,"idx",int,"g",int,"p",int,"par") ev; ev[] evs = new ev[n]; evs[0] = ev(0,gi++,0,-1); foreach(inv;invFirstIdx){ if(inv==-1) continue; with(evs[inv]){ group[idx] = g; invPath[idx] = p; int maxsc=0; int midx=-1; foreach(c;tree[idx]){ if(c==par) continue; if(subTreeCount[c] > maxsc){ maxsc = subTreeCount[c]; midx = c; } } foreach(c;tree[idx]){ if(c==par) continue; if(c == midx){ evs[c] = ev(c,g,p+1,idx); } else{ pathParent[gi] = idx; evs[c] = ev(c,gi++,0,idx); } } if(midx == -1) gsize[g] = p+1; } } } void Initialize(){ group = new int[n]; invPath = new int[n]; gsize = new int[n]; pathData = new int[n]; paths = new int[][n]; pathParent = new int[n]; lca.Initialize; pathParent[0] = -1; calcHeavyLight; int pidx=0; for(int i=0;i<gi;i++){ paths[i] = pathData[pidx..pidx+gsize[i]]; pidx += gsize[i]; } for(int i=0;i<n;i++){ paths[group[i]][invPath[i]] = weights[i]; } segs = new SegmentTree[gi]; for(int i=0;i<gi;i++){ segs[i] = new SegmentTree; segs[i].Initialize(paths[i]); } } void _update(int a,int b,int w){ while(group[a] != group[b]){ segs[group[a]].Update(0,invPath[a]+1,w); a = pathParent[group[a]]; } segs[group[a]].Update(invPath[b],invPath[a]+1,w); } void Update(int a,int b,int w){ int lc = lca.get(a,b); _update(a,lc,w); _update(b,lc,w); } sums _get(int a,int b){ sums ret = sums(int.min,int.min,int.min,int.min); sums g; while(group[a] != group[b]){ g = segs[group[a]].Get(0,invPath[a]+1); if(ret.a == int.min) ret = g; else ret = mergeSums(g,ret); a = pathParent[group[a]]; } g = segs[group[a]].Get(invPath[b],invPath[a]+1); if(ret.a == int.min) return g; else return mergeSums(g,ret); } int Get(int a,int b){ int lc = lca.get(a,b); sums as = _get(a,lc); sums bs = _get(b,lc); int ret = max(as.m,bs.m,as.l+bs.l-segs[group[lc]].Get(invPath[lc],invPath[lc]+1).a); return ret; } } HLdecomposition hl; void main(){ GC.disable; int q; scanf("%d%d",&n,&q); tree = new int[][n]; subTreeCount = new int[n]; weights = new int[n]; int[] ans = new int[q]; for(int i=0;i<n;i++) scanf("%d",&weights[i]); for(int i=0;i<n-1;i++){ int a,b; scanf("%d%d",&a,&b); tree[--a] ~= --b; tree[b] ~= a; } hl.Initialize; int qc=0; for(int i=0;i<q;i++){ int t,a,b,c; scanf("%d%d%d%d",&t,&a,&b,&c); if(t == 1){ hl.Update(a-1,b-1,c); }else if(t==2){ ans[qc++] = hl.Get(a-1,b-1); } } for(int i=0;i<qc;i++) printf("%d\n",ans[i]); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; enum P = 10L^^9+7; void main() { auto S = readln.chomp.to!(char[]); auto DP = new long[][](S.length, 13); foreach (i, c; S) { foreach (n; 0..10) { if (c == '?' || n == c-'0') { if (i == 0) { DP[i][n] = 1; } else { foreach (j; 0..13) { auto m = (j*10 + n) % 13; DP[i][m] = (DP[i][m] + DP[i-1][j]) % P; } } } } } writeln(DP[$-1][5]); }
D
import std.stdio; import std.string; import std.conv; void main() { auto n = to!int(readln.chomp()); auto a = to!int(readln.chomp()); writeln((n % 500) <= a ? "Yes" : "No"); }
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 main() { auto a = aryread(); // a.sort(); bool b = a[2] == a[0] && a[0] == a[1]; writeln(b ? "Yes" : "No"); }
D
import std.stdio; import std.string; import std.array; import std.algorithm; import std.conv; import std.range; import std.functional; import std.bigint; import std.typecons; void main(string[] args) { readln; string s = readln.chomp; enum Transit { StoS, StoW, WtoS, WtoW, } alias Reasoner = Tuple!(Transit, "assumption", Transit, "temp"); Reasoner[4] reasoner; string[4] ans_candidate; if (s[0] == 'o') { reasoner[0].assumption = Transit.StoS; ans_candidate[0] = "S"; reasoner[0].temp = Transit.StoS; reasoner[1].assumption = Transit.StoW; ans_candidate[1] = "W"; reasoner[1].temp = Transit.WtoW; reasoner[2].assumption = Transit.WtoS; ans_candidate[2] = "S"; reasoner[2].temp = Transit.StoW; reasoner[3].assumption = Transit.WtoW; ans_candidate[3] = "W"; reasoner[3].temp = Transit.WtoS; } else if (s[0] == 'x') { reasoner[0].assumption = Transit.StoS; ans_candidate[0] = "S"; reasoner[0].temp = Transit.StoW; reasoner[1].assumption = Transit.StoW; ans_candidate[1] = "W"; reasoner[1].temp = Transit.WtoS; reasoner[2].assumption = Transit.WtoS; ans_candidate[2] = "S"; reasoner[2].temp = Transit.StoS; reasoner[3].assumption = Transit.WtoW; ans_candidate[3] = "W"; reasoner[3].temp = Transit.WtoW; } else { throw new Exception("input error"); } auto o_table = [ Transit.StoS : Transit.StoS, Transit.StoW : Transit.WtoW, Transit.WtoS : Transit.StoW, Transit.WtoW : Transit.WtoS, ]; auto x_table = [ Transit.StoS : Transit.StoW, Transit.StoW : Transit.WtoS, Transit.WtoS : Transit.StoS, Transit.WtoW : Transit.WtoW, ]; foreach (c; s[1..$]) { if (c == 'o') { foreach(i; 0..4) { if (reasoner[i].temp.among(Transit.StoS, Transit.WtoS)) ans_candidate[i] ~= "S"; else ans_candidate[i] ~= "W"; reasoner[i].temp = o_table[reasoner[i].temp]; } } else if (c == 'x') { foreach(i; 0..4) { if (reasoner[i].temp.among(Transit.StoS, Transit.WtoS)) ans_candidate[i] ~= "S"; else ans_candidate[i] ~= "W"; reasoner[i].temp = x_table[reasoner[i].temp]; } } else { throw new Exception("input error"); } } foreach (i; 0..5) { if (i == 4){ (-1).writeln; break; } if (reasoner[i].temp == reasoner[i].assumption) { ans_candidate[i].writeln; break; } } }
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 T = readln.chomp; int[27] ss, ts; foreach (i; 0..S.length) { int s = S[i] - 'a' + 1; int t = T[i] - 'a' + 1; if (ts[s] == 0 || ts[s] == t) { ts[s] = t; } else { writeln("No"); return; } if (ss[t] == 0 || ss[t] == s) { ss[t] = s; } else { writeln("No"); return; } } writeln("Yes"); }
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], W = HW[1]; auto hw = readints; int h = hw[0], w = hw[1]; int ans = H * W - h * W - H * w + h * w; writeln(ans); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(string s) { int n = cast(int)s.length; for (int i = 1; i < n; i++) { if ((n - i) % 2 == 1) continue; int m = (n - i) / 2; auto a = s[0..m]; auto b = s[m..m*2]; if (a == b) { return m * 2; } } assert(false); } void main() { string s = read!string; writeln(calc(s)); }
D
import std.stdio, std.range, std.conv, std.string, std.array, std.functional; import std.algorithm.comparison, std.algorithm.iteration, std.algorithm.mutation, std.algorithm.searching, std.algorithm.setops, std.algorithm.sorting; import std.container.binaryheap; void main() { string S; // scanf("%s", &S); S = readln().strip(); int res; foreach(i; 0..S.length) { int count; foreach(c; S[i..$]) { if( c=='A' || c=='C' || c=='G' || c=='T' ) { count++; continue; } break; } res = max(res, count); } writeln(res); }
D
import core.bitop, std.bitmanip; import core.checkedint; import std.algorithm, std.functional; import std.array, std.container; import std.bigint; import std.conv; import std.math, std.numeric; import std.range, std.range.interfaces; import std.stdio, std.string; import std.typecons; void main() { auto s = readln.chomp; auto arr = ["Sunny", "Cloudy", "Rainy"]; int i = 0; while (arr[i] != s) { ++i; } auto ans = arr[(i+1) % 3]; ans.writeln; }
D
void main() { int[] tmp = readln.split.to!(int[]); int a = tmp[0], b = tmp[1], k = tmp[2]; int[] ans; foreach (i; 0 .. k) { if (a + i <= b) { ans ~= a + i; } } foreach_reverse (i; 0 .. k) { if (!ans.canFind(b - i) && b - i >= a) { ans ~= b - i; } } foreach (x; ans) { x.writeln; } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.conv, std.string, std.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; print(["pon", "pon", "hon", "bon", "hon", "hon", "pon", "hon", "pon", "hon"][n % 10]); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; int[] r; int n; foreach (int i; 1..5000) { n += i; r ~= i; if (n >= N) break; } if (n > N) { r[n-N-1] = 0; } foreach (x; r) if (x) { writeln(x); } }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; void times(alias pred)(int n) { foreach(i; 0..n) pred(); } auto rep(alias pred, T = typeof(pred()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = pred(); return res; } int N; long[][] ary; void main() { N = readln.chomp.to!int; ary = N.rep!(() => readln.split.to!(long[])); ary.reverse(); long l = 0; long r = 10L^^18; while(l < r) { long c = (l+r)/2; if (check(c)) { r = c; } else { if (l==c) break; l = c; } } r.writeln; } bool check(long c) { long[] res = ary.front.dup; if (c<res.sum) return false; res[] *= c/res.sum; foreach(e; ary) { if (res.front<e.front || res.back<e.back) return false; long r = min(res.front/e.front, res.back/e.back); res.front = r*e.front; res.back = r*e.back; } return true; }
D
import std.stdio, std.string, std.conv, std.bigint, std.typecons, std.algorithm, std.array, std.math, std.range, std.functional; void main() { auto N = readln.chomp; writeln(N.retro.retro.array == N.retro.array ? "Yes" : "No"); }
D
void main() { int[] tmp = readln.split.to!(int[]); int n = tmp[0], k = tmp[1]; writeln(k == 1 ? 0 : n - k); } 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; void main() { while(true) { int n=readln.chomp.to!int; if(n==0) break; if(n==1) { "deficient number".writeln; continue; } int s=1; for(int i=2; i*i <= n; ++i) { if(n/i*i==n) { if(i*i==n) s+=i; else s+=i+n/i; } } if(s<n) "deficient number".writeln; else if(s>n) "abundant number".writeln; else "perfect number".writeln; } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { int n, k; scan(n, k); auto dp = new long[](2*n + 1); foreach (b ; 1 .. n + 1) { if (b <= k) continue; for (int r = b; r <= 2*n; r += b) { dp[r - (b - k)]++; dp[r]--; } debug { writeln(b, ":", dp); } } foreach (i ; 1 .. n + 1) { dp[i] += dp[i - 1]; } debug { writeln(dp); } long ans; foreach (i ; 1 .. n + 1) { ans += dp[i]; } writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n; scan(n); auto p = readln.split.to!(int[]); int ans; foreach (i ; 1 .. n - 1) { int mx = max(p[i - 1], p[i], p[i + 1]); int mn = min(p[i - 1], p[i], p[i + 1]); ans += mx != p[i] && mn != p[i]; } writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
void main(){ int[] ab = _scanln(); (ab[0]*ab[1]&1)?writeln("Odd"):writeln("Even"); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.container; import std.array; import std.math; import std.range; import std.typecons; import std.ascii; void main() { int n = readln.chomp.to!int; int l = int.max; int r = int.min; foreach (v; readln.chomp.split.map!(to!int)) { l = min(l, v); r = max(r, v); } writeln = r - l; }
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; const long p = 1_000_000_000 + 7; void main(){ long n, m; readlnTo(n, m); auto from1 = new bool[](n); auto fromN = new bool[](n); foreach(i; iota(m)) { long a, b; readlnTo(a, b); --a; --b; if(a == 0) { from1[b] = true; } if(b == n-1) { fromN[a] = true; } } foreach(i; iota(n)) { if(from1[i] && fromN[i]) { writeln("POSSIBLE"); return; } } writeln("IMPOSSIBLE"); }
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 K = RD; char[] inc2(char[] s, long i) { foreach_reverse (j; 0..i) { auto num = s[j+1] - '0'; s[j] = cast(char)(max(num-1, 0)+'0'); } return s; } char[] inc(char[] s, long i) { auto x = s[i] - '0'; if (i+1 < s.length) { auto y = s[i+1] - '0'; if (y - x >= 0 && x+1 != 10) { ++s[i]; return inc2(s, i); } else { return inc(s, i+1); } } else { if (x == 9) { s ~= '1'; return inc2(s, i+1); } else { ++s[i]; return inc2(s, i); } } } char[] ans = ['1']; foreach (i; 0..K-1) { ans = inc(ans, 0); } ans.reverse(); writeln(ans); stdout.flush; debug readln; }
D
/+ dub.sdl: name "B" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.scanner; // import dcomp.graph.dijkstra; struct State { int tm; int col; int opCmp(State r) { if (tm < r.tm) return -1; if (tm > r.tm) return 1; return 0; } } int main() { auto sc = new Scanner(stdin); int n, m; sc.read(n, m); int[][] g = new int[][](n); State[][] dp = new State[][](12, n); dp.each!(v => v[] = State(0, 0)); foreach (i; 0..m) { int a, b; sc.read(a, b); a--; b--; g[a] ~= b; g[b] ~= a; } int q; sc.read(q); foreach (i; 0..q) { int v, d, c; sc.read(v, d, c); v--; dp[d][v] = State(i+1, c); } foreach_reverse (ph; 0..11) { foreach (i; 0..n) { dp[ph][i] = max(dp[ph][i], dp[ph+1][i]); foreach (j; g[i]) { dp[ph][i] = max(dp[ph][i], dp[ph+1][j]); } } } foreach (i; 0..n) { writeln(dp[0][i].col); } return 0; } /* 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"); } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/algorithm.d */ // module dcomp.algorithm; import std.range.primitives; import std.traits : isFloatingPoint, isIntegral; //[0,0,0,...,1,1,1]で、初めて1となる場所を探す。pred(l) == 0, pred(r) == 1と仮定 T binSearch(alias pred, T)(T l, T r) if (isIntegral!T) { while (r-l > 1) { T md = (l+r)/2; if (!pred(md)) l = md; else r = md; } return r; } T binSearch(alias pred, T)(T l, T r, int cnt = 60) if (isFloatingPoint!T) { foreach (i; 0..cnt) { T md = (l+r)/2; if (!pred(md)) l = md; else r = md; } return r; } Rotator!Range rotator(Range)(Range r) { return Rotator!Range(r); } struct Rotator(Range) if (isForwardRange!Range && hasLength!Range) { size_t cnt; Range start, now; this(Range r) { cnt = 0; start = r.save; now = r.save; } this(this) { start = start.save; now = now.save; } @property bool empty() { return now.empty; } @property auto front() { assert(!now.empty); import std.range : take, chain; return chain(now, start.take(cnt)); } @property Rotator!Range save() { return this; } void popFront() { cnt++; now.popFront; } } E minimum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed) if (isInputRange!Range && !isInfinite!Range) { import std.algorithm, std.functional; return reduce!((a, b) => binaryFun!pred(a, b) ? a : b)(seed, range); } ElementType!Range minimum(alias pred = "a < b", Range)(Range range) { assert(!range.empty, "range must not empty"); auto e = range.front; range.popFront; return minimum!pred(range, e); } E maximum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed) if (isInputRange!Range && !isInfinite!Range) { import std.algorithm, std.functional; return reduce!((a, b) => binaryFun!pred(a, b) ? b : a)(seed, range); } ElementType!Range maximum(alias pred = "a < b", Range)(Range range) { assert(!range.empty, "range must not empty"); auto e = range.front; range.popFront; return maximum!pred(range, e); } unittest { assert(minimum([2, 1, 3]) == 1); assert(minimum!"a > b"([2, 1, 3]) == 3); assert(minimum([2, 1, 3], -1) == -1); assert(minimum!"a > b"([2, 1, 3], 100) == 100); assert(maximum([2, 1, 3]) == 3); assert(maximum!"a > b"([2, 1, 3]) == 1); assert(maximum([2, 1, 3], 100) == 100); assert(maximum!"a > b"([2, 1, 3], -1) == -1); } bool[ElementType!Range] toMap(Range)(Range r) { import std.algorithm : each; bool[ElementType!Range] res; r.each!(a => res[a] = true); return res; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/graph/dijkstra.d */ // module dcomp.graph.djikstra; // import dcomp.algorithm; struct Dijkstra(T) { T[] dist; int[] from; this(int n, T inf) { dist = new T[n]; dist[] = inf; from = new int[n]; } } Dijkstra!D dijkstra(D, T)(T g, int s, D inf = D.max) { import std.conv : to; import std.typecons : Tuple; import std.container : make, Array, heapify; int V = g.length.to!int; auto dijk = Dijkstra!D(V, inf); with (dijk) { alias P = Tuple!(int, "to", D, "dist"); auto q = heapify!"a.dist>b.dist"(make!(Array!P)([P(s, D(0))])); dist[s] = D(0); from[s] = -1; while (!q.empty) { P p = q.front; q.popFront(); if (dist[p.to] < p.dist) continue; foreach (e; g[p.to]) { if (p.dist+e.dist < dist[e.to]) { dist[e.to] = p.dist+e.dist; from[e.to] = p.to; q.insert(P(e.to, dist[e.to])); } } } } return dijk; } Dijkstra!D dijkstraDense(D, T)(T g, int s, D inf = D.max) { import std.conv : to; import std.typecons : Tuple; import std.container : make, Array, heapify; import std.range : enumerate; import std.algorithm : filter; int V = g.length.to!int; auto dijk = Dijkstra!D(V, inf); with (dijk) { alias P = Tuple!(int, "to", D, "dist"); bool[] used = new bool[](V); dist[s] = D(0); from[s] = -1; while (true) { //todo can optimize auto rng = dist.enumerate.filter!(a => !used[a.index]); if (rng.empty) break; auto nx = rng.minimum!"a.value < b.value"; used[nx.index] = true; P p = P(nx.index.to!int, nx.value); if (dist[p.to] < p.dist) continue; foreach (e; g[p.to]) { if (p.dist+e.dist < dist[e.to]) { dist[e.to] = p.dist+e.dist; from[e.to] = p.to; } } } } return dijk; } unittest { import std.algorithm, std.conv, std.stdio, std.range; import std.random; import std.typecons; import std.datetime; alias E = Tuple!(int, "to", int, "dist"); writeln("Dijkstra Random100000"); void f(alias pred)() { int n = uniform(1, 100); int m = uniform(1, 1000); E[][] g = new E[][n]; int[][] dist = new int[][](n, n); foreach (i, ref v; dist) { v[] = 10^^9; } iota(n).each!(i => dist[i][i] = 0); foreach (i; 0..m) { int a = uniform(0, n); int b = uniform(0, n); int c = uniform(0, 1000); g[a] ~= E(b, c); dist[a][b] = min(dist[a][b], c); } foreach (k; 0..n) { foreach (i; 0..n) { foreach (j; 0..n) { dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j]); } } } foreach (i; 0..n) { auto dijk = pred!int(g, i, 10^^9); foreach (j; 0..n) { assert(dist[i][j] == dijk.dist[j]); } assert(dijk.from[i] == -1); foreach (j; 0..n) { if (i == j) continue; if (dist[i][j] == 10^^9) continue; int b = dijk.from[j]; assert(dijk.dist[j] == dist[i][b]+dist[b][j]); } } } auto ti = benchmark!(f!dijkstra, f!dijkstraDense)(100); writeln(ti[0].msecs, "ms"); writeln(ti[1].msecs, "ms"); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { long A, B; scan(A, B); if (B % A == 0) { writeln(A + B); } else { writeln(B - A); } }
D
import std; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void 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 = lread(); auto S = sread(); auto sum = new long[][](3, N + 1); foreach (i; 0 .. N) { switch (S[i]) { case 'R': sum[0][i + 1] = 1; break; case 'G': sum[1][i + 1] = 1; break; case 'B': sum[2][i + 1] = 1; break; default: assert(0); } } foreach (i; 0 .. 3) foreach (j; 0 .. N) { sum[i][j + 1] += sum[i][j]; } long[] idx = [0, 1, 2]; long ans; do { foreach (i; 0 .. N) { if (S[i] == "RGB"[idx[0]]) foreach (j; i + 1 .. N) { if (S[j] == "RGB"[idx[1]]) { ans += sum[idx[2]][$ - 1] - sum[idx[2]][j + 1]; // writefln("%s %s %s", i, j, sum[$ - 1] - sum[j + 1]); if (j + (j - i) < N && S[j + (j - i)] == "RGB"[idx[2]]) { // writefln("%s %s %s", i, j, j + (j - i)); ans--; } } } } } while (nextPermutation(idx)); writeln(ans); }
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 kt = readln.chomp.split.map!(to!int); auto a = readln.chomp.split.map!(to!int).array; int res; int prev = -1; foreach_reverse (n; 1..kt[0]+1) { int index = -1; int max_n; foreach (i, e; a) { if (e == n) { index = i.to!int; break; } if (i == prev) { continue; } if (e > max_n) { index = i.to!int; max_n = e; } } a[index]--; if (prev == index) res++; prev = index; } res.writeln; }
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[] xs = new long[4]; foreach(_; 0..3) { long a, b; scanln(a, b); a--; b--; xs[a]++; xs[b]++; } if (xs.any!"a>=3") { "NO".writeln; } else { "YES".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.algorithm, std.array, std.container, std.range, std.bitmanip; import std.numeric, std.math, std.bigint, std.random, core.bitop; import std.string, std.conv, std.stdio, std.typecons; void main() { auto s = readln.chomp.to!int; writeln(s / (60 * 60), ":", s % (60 * 60) / 60, ":", s % 60); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto A = readln.chomp.to!(char[]); long N = A.length; long[char] CNT; foreach (a; A) { if (a !in CNT) CNT[a] = 0; ++CNT[a]; } long r = N * (N-1) / 2 + 1; foreach (_, c; CNT) { r -= (c * (c-1) / 2); } writeln(r); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; alias Side = Tuple!(long, "from", long, "to"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { auto n = lread(); long[long] a; foreach (i; iota(n)) { auto tmp = lread(); if (!(tmp in a)) a[tmp] = 1; else a[tmp] = !a[tmp]; } long ans; foreach (e; a) ans += e; ans.writeln(); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; import std.math; void main() { auto n = readln.chomp.to!size_t; auto r = 0; loop: foreach (_; 0..n) { auto a = readln.chomp.to!int; if (a == 2) { ++r; continue; } if (a % 2 == 0) continue; foreach (i; iota(3, a.to!real.sqrt.to!int + 1)) if (a % i == 0) continue loop; ++r; } writeln(r); }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; int[] A, B; int K; void main() { string[] input = readln.split; A = input[0].to!(char[]).map!(a=>(a-'0').to!int).array; B = input[1].to!(char[]).map!(a=>(a-'0').to!int).array; K = input[2].to!int; while (B.length < A.length) B = 0~B; A.reverse; B.reverse; func(0, 0, new int[A.length], 0).writeln; } long func(int index, int b, int[] C, int k) { if (index == A.length) { return connect(C); } else { long result; if (A[index] - b >= B[index]) { C[index] = A[index]-b-B[index]; result = max(result, func(index+1, 0, C, k)); } else { C[index] = A[index]-b+10-B[index]; result = max(result, func(index+1, 1, C, k)); if (k+1 <= K) { result = max(result, func(index+1, 0, C, k+1)); } } return result; } } long connect(int[] C) { long result = 0; for(int i=0; i<C.length; i++) { result += C[i]*10^^i; } return result; }
D
import std.stdio; import std.string; import std.conv; void main(){ int n; while(true){ int allLunch, allDinner, allMidnight; int lunch, dinner, midnight; n = readln().chomp().to!int(); if(n == 0){ break; } foreach(i; 0..n){ auto input = readln().chomp().split(":"); int h = input[0].to!int(); if(h < 11){ h += 24; } input = input[1].split(); int m = input[0].to!int(); int m2 = input[1].to!int(); int diff = m < m2 ? m2 - m : 60 - m + m2; if(11 <= h && h <= 14){ ++allLunch; if(diff <= 8){ ++lunch; } }else if(18 <= h && h <= 20){ ++allDinner; if(diff <= 8){ ++dinner; } }else if(21 <= h && h <= 25){ ++allMidnight; if(diff <= 8){ ++midnight; } } } if(allLunch){ writeln("lunch ", 100 * lunch / allLunch); }else{ writeln("lunch no guest"); } if(allDinner){ writeln("dinner ", 100 * dinner / allDinner); }else{ writeln("dinner no guest"); } if(allMidnight){ writeln("midnight ", 100 * midnight / allMidnight); }else{ writeln("midnight no guest"); } } }
D
void main() { int[] tmp = readln.split.to!(int[]); int n = tmp[0], m = tmp[1], c = tmp[2]; int[] b = readln.split.to!(int[]); int cnt; foreach (i; 0 .. n) { int[] a = readln.split.to!(int[]); int src = c; foreach (j; 0 .. m) { src += a[j] * b[j]; } if (src > 0) ++cnt; } cnt.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.container; import std.typecons;
D
void main() { int n = readln.chomp.to!int; int k = readln.chomp.to!int; int[] x = readln.split.to!(int[]); int ans; foreach (y; x) { ans += 2 * min(y, k-y); } ans.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto hn = readln.split.to!(int[]); long H = hn[0]; auto N = hn[1]; long[] AS, BS; foreach (_; 0..N) { auto ab = readln.split.to!(long[]); AS ~= ab[0]; BS ~= ab[1]; } auto DP = new long[][](N, H+1); foreach (ref dp; DP) dp[] = -1; long solve(int i, long h) { if (i == N) return h == 0 ? 0 : long.max/3; if (DP[i][h] == -1) { if (h == 0) { DP[i][h] = solve(i+1, h); } else { DP[i][h] = min(solve(i+1, h), solve(i, max(0, h - AS[i])) + BS[i]); } } return DP[i][h]; } writeln(solve(0, H)); }
D
import core.stdc.stdio; import std.algorithm; import std.math; import std.conv; void main(){ int n,a,b; scanf("%d%d%d",&n,&a,&b); int[] cs = new int[n]; int kc; scanf("%d",&kc); for(int i=0;i<n;i++){ scanf("%d",&cs[i]); } cs.sort!"a>b"; double ans = kc.to!double/a; int csum = kc; for(int i=0;i<n;i++){ csum+=cs[i]; ans = max(ans,csum.to!double/(a+b*(i+1))); } printf("%d\n",ans.floor.to!int); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { auto S = sread(); bool n, w, s, e; foreach (c; S) { n = n || c == 'N'; w = w || c == 'W'; s = s || c == 'S'; e = e || c == 'E'; } writeln(((n == s) && (w == e)) ? "Yes" : "No"); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; alias Activity = Tuple!(long, "sea", long, "mount", long, "home"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } auto dp = new long[][](100_100, 3); void main() { auto n = lread(); auto act = new long[][](n, 3); foreach (i; iota(n)) { long a, b, c; scan(a, b, c); act[i] = [a, b, c]; } foreach(i; iota(n)) { foreach(j; iota(act[i].length)) { dp[i + 1][j] = act[i][j] + max(dp[i][(j + 1) % 3], dp[i][(j + 2) % 3]); } } dp[n].reduce!(max).writeln(); } auto max_rm(T)(T[] a, T recent = -1) { long maxnum = -INF; foreach (i; iota(a.length)) if (i != recent) maxnum = max(a[i], maxnum); return maxnum; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void calc(string s) { int toi(char c) { return c - '0'; } int a = toi(s[0]); int b = toi(s[1]); int c = toi(s[2]); int d = toi(s[3]); int reduce(int lhs, int rhs, int op) { return op == 0 ? lhs + rhs : lhs - rhs; } char op(int op) { return op == 0 ? '+' : '-'; } for (int i = 0; i < 2; i++) { int x = reduce(a, b, i); for (int j = 0; j < 2; j++) { int y = reduce(x, c, j); for (int k = 0; k < 2; k++) { int z = reduce(y, d, k); if (z == 7) { writeln(a, op(i), b, op(j), c, op(k), d, "=7"); return; } } } } } void main() { auto s = readln.chomp; calc(s); }
D
import std.stdio,std.string,std.conv; void main(){ int sum; while( 1 ){ auto rd = readln().chomp(); if( !rd ){ break; } sum=0; int d = to!int(rd); for( int i=d; i<600; i+=d ){ sum += i*i*d; } writeln(sum); } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(char, "x", long, "y"); alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less); void main() { long n, a, b; scan(n, a, b); auto x = aryread(); long cost; foreach (i; iota(n - 1)) { cost += min(a * (x[i + 1] - x[i]), b); } cost.writeln(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
void main() { auto N = readAs!ulong; auto k = 1; ulong a = 0; if(N % 2 == 1) { writeln(0); return; } N /= 2; for(ulong i = 5; N / i >= 1; i *= 5) a += N / i; a.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, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nm = readln.split.to!(long[]); auto N = nm[0]; auto M = nm[1]; if (N > M) swap(N, M); if (N == 1) { writeln(M == 1 ? 1 : M-2); } else if (N == 2) { writeln(0); } else { writeln((N-2) * (M-2)); } }
D