code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio; import std.algorithm; import std.string; import std.functional; import std.array; import std.conv; import std.math; import std.typecons; import std.regex; import std.range; void main(){ int[] ch = [0,6000,4000,3000,2000]; for(int i=0;i<4;i++){ auto s = split(readln()).to!(int[]); writeln(ch[s[0]]*s[1]); } }
D
import std.stdio; import std.string; import std.conv; import std.range; import std.array; import std.algorithm; import std.typecons; void main(){ auto hw = readln().chomp().split().map!(to!int).array(); string[] grids; foreach(i; 0..(hw[0])){ grids ~= readln().chomp(); } solve(grids).writeln(); } string solve(string[] grids){ bool result = true; foreach(int y; 0..cast(int) grids.length){ foreach(int x; 0..cast(int) grids[y].length){ if(!check(x, y, grids)){ result = false; break; } } if(!result){ break; } } if(result){ return "Yes"; }else{ return "No"; } } bool check(int x, int y, string[] grids){ bool result; if(grids[y][x] == '#'){ if(x - 1 >= 0){ if(grids[y][x-1] == '#'){ result = true; } } if(y - 1 >= 0){ if(grids[y - 1][x] == '#'){ result = true; } } if(x + 1 < grids[y].length){ if(grids[y][x+1] == '#'){ result = true; } } if(y + 1 < grids.length){ if(grids[y+1][x] == '#'){ result = true; } } }else{ result = true; } return result; }
D
void main() { auto N = ri; int m = int.min; int res; foreach(i; 0..N) { auto p = ri; m = max(m, p); res += p; } res -= m / 2; 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.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n; scan(n); auto a = readln.split.to!(int[]); long ans = 1; auto cnt = new int[](3); foreach (i ; 0 .. n) { int t; bool b; foreach (j ; 0 .. 3) { if (cnt[j] == a[i]) { t++; if (!b) { cnt[j]++; b = 1; } } } (ans *= t) %= mod; } writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio; import std.algorithm; import std.math; import std.string; import std.conv; import std.range; void main() { foreach (string line; stdin.lines) { string s = line.chomp; int joi = 0; int ioi = 0; for (int i = 0; i < s.length - 2; i++) { if (s[i..i+3] == "JOI") { joi++; } else if (s[i..i+3] == "IOI") { ioi++; } } joi.writeln; ioi.writeln; } }
D
import std.functional, std.algorithm, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv; void dfs(bool[][] dp, bool[] visited, int v) { visited[v] = true; foreach (int i, e; dp[v]) { if (e) { if (!visited[i]) { dfs(dp, visited, i); } } } } void main() { int[] NM = readln.chomp.split.to!(int[]); int N = NM[0], M = NM[1]; bool[][] dp; dp.length = N; foreach (ref e; dp) { e.length = N; } int[][] vs; int ans; foreach (_; 0..M) { int[] ab = readln.chomp.split.to!(int[]); int a = ab[0], b = ab[1]; a--; b--; vs ~= [a, b]; dp[a][b] = dp[b][a] = true; } foreach (v; vs) { int a = v[0], b = v[1]; bool[] visited; visited.length = N; dp[a][b] = dp[b][a] = false; dfs(dp, visited, 0); if (!visited.all!(e => e)) { ans++; } dp[a][b] = dp[b][a] = true; } writeln(ans); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; auto s=readln.chomp.to!(char[]); foreach(i; 0..10){ char[] t; foreach(_; 0..3) t~=i+'0'; if(s[0..($-1)]==t){writeln("Yes"); return;} if(s[1..$]==t){writeln("Yes"); return;} } writeln("No"); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; template bsearch(alias fun) { import std.functional: unaryFun; alias f = unaryFun!fun; int bsearch(T)(T[] arr) { if (arr.empty) return -1; if (!f(arr[0])) return -1; if (f(arr[$-1])) return arr.length.to!int - 1; int l, r = arr.length.to!int - 1; while (l+1 < r) { auto m = (l+r)/2; if (f(arr[m])) { l = m; } else { r = m; } } return l; } } size_t[][26] D; void main() { auto s = readln.chomp.to!(char[]); auto t = readln.chomp.to!(char[]); foreach (i, c; s) { D[c - 'a'] ~= i; } ulong cnt; size_t p; bool first = true; foreach (c; t) { auto d = D[c - 'a']; if (d.empty) { writeln(-1); return; } if (first) { first = false; if (d[0] == 0) { continue; } } if (d[$-1] <= p) { p = d[0]; ++cnt; continue; } else if (d[0] > p) { p = d[0]; continue; } auto l = bsearch!(i => i <= p)(d); p = d[l+1]; } writeln(s.length * cnt + p + 1); }
D
import std.stdio, std.string, std.conv; import std.typecons; import std.algorithm, std.array, std.range, std.container; import std.math; void main() { auto a = readln.split[0].to!int; auto b = readln.split[0].to!int; auto c = readln.split[0].to!int; auto d = readln.split[0].to!int; auto e = readln.split[0].to!int; int f(int x) { if (x % 10 == 0) return x; else return x + 10 - x%10; } writeln(min( f(a) + f(b) + f(c) + f(d) + e, f(a) + f(b) + f(c) + d + f(e), f(a) + f(b) + c + f(d) + f(e), f(a) + b + f(c) + f(d) + f(e), a + f(b) + f(c) + f(d) + f(e) )); }
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=s[0],b=s[1],c=s[2],ans=0; if(a>b && a>c) ans=b*c/2; else if(b>a && b>c) ans=c*a/2; else ans=a*b/2; writeln(int(ans)); }
D
import std.stdio,std.regex; void main(){ auto next = readln().chomp().to!size_t; auto list = [111,222,333,444,555,666,777,888,999]; foreach( val; list ){ if( next <= val ){ writeln(val); break; } continue; } return; } import std.string,std.conv;
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons; import std.math, std.numeric; void main() { long n, x; scan(n, x); if (x > n - x) { x = n - x; } if (x == n - x) { writeln(3*x); return; } long ans = n; void dfs(long x, long y) { if (x % y == 0) { ans += (x / y) * 2 * y - y; return; } else { ans += (x / y) * 2 * y; dfs(y, x % y); } } dfs(n - x, x); 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.functional, std.algorithm, std.container, std.typetuple, std.typecons, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv, std.format; void main() { auto ip = readln.split.to!(int[]); int count; foreach(i; ip[0]..ip[1]+1){ if(ip[2] % i == 0) count++; } writeln(count); }
D
import std.stdio, std.string, std.array, std.conv, std.algorithm.iteration, std.functional; void main() { auto s = readln.chomp; auto n = s.length; foreach (c; s) { if (c == 'A') break; --n; } foreach_reverse (c; s) { if (c == 'Z') break; --n; } writeln(n); }
D
import std.algorithm; import std.array; import std.bigint; import std.bitmanip; import std.conv; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; // 差の絶対値 @nogc @safe pure T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; } // 切り上げ除算 @nogc @safe pure T divCeil(T)(const ref T a, const ref T b) { return (a+b-1)/b; } T[] readToArray(T)() { return readln.split.to!(T[]); } void readInto(T...)(ref T ts) { auto ss = readln.split; foreach(ref t; ts) { t = ss.front.to!(typeof(t)); ss.popFront; } } // 冪乗をmod取りつつ計算 @nogc @safe pure ulong modPow(ulong a, ulong n, ulong m) { ulong r = 1; while (n > 0) { if(n % 2 != 0) r = r * a % m; a = a * a % m; n /= 2; } return r; } // フェルマーの小定理から乗法逆元を計算 // 定理の要請により法は素数 @nogc @safe pure ulong modInv(ulong a, ulong m) { return modPow(a, m-2, m); } // mod取りつつ順列を計算 @nogc @safe pure ulong modPerm(ulong n, ulong k, ulong m) { if (n < k) return 0; ulong r = 1; for (ulong i = n-k+1; i <= n; i++) { r *= i; r %= m; } return r; } // mod取りつつ順列を計算 @nogc @safe pure ulong modFact(ulong n, ulong m) { return modPerm(n, n, m); } // mod取りつつ組み合わせを計算 // modInvを使っているので法は素数 @nogc @safe pure ulong modComb(ulong n, ulong r, ulong m) { return modPerm(n, r, m)*modInv(modFact(r, m), m) % m; } immutable ulong MOD = 1000000007; void main() { ulong n; readInto(n); ulong[] p = readToArray!(ulong); ulong cnt = 0; for(ulong i = 0; i < n; i++) { ulong min = p[i]; ulong min_idx = i; for(ulong j = i+1; j < n; j++) { if (p[j] < min) { min_idx = j; } } if (min_idx != i) { if (cnt != 0) { writeln("NO"); return; } ulong tmp = p[i]; p[i] = p[min_idx]; p[min_idx] = tmp; cnt++; } } writeln("YES"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.range, std.string, std.math, std.typecons; void main() { auto nk = readln.split.to!(int[]); auto N = nk[0]; auto K = nk[1]; long cnt; foreach (long i; 0..N) { if (i < K) continue; else { cnt += N / (i+1) * (i+1-K); if (N % (i+1) >= K) cnt += N % (i+1) - K + 1; if (!K) cnt -= 1; } } writeln(cnt); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { long a,b,c; scan(a,b,c); long max = max(a,b,c); long min = min(a,b,c); (max - min).writeln(); }
D
import std.stdio, std.string, std.conv, std.array, std.algorithm; void main() { auto abc = readln.chomp.split(" ").map!(to!int); writeln( abc.count!"a==5" == 2 && abc.count!"a==7" == 1 ? "YES" : "NO" ); }
D
/+ dub.sdl: name "C" dependency "dunkelheit" version="1.0.0" +/ import std.stdio, std.algorithm, std.range, std.conv, std.math; // import dkh.foundation, dkh.scanner; immutable int MD = 3*(10^^5); immutable int S = MD/2; int main() { Scanner sc = new Scanner(stdin); scope(exit) assert(!sc.hasNext); int n; sc.read(n); int[] limo = new int[MD+1], rimo = new int[MD+1]; foreach (i; 0..n+1) { int l, r; if (i == 0) l = r = 0; else sc.read(l, r); l += S; r += S; limo[0]++; limo[l]--; rimo[r]++; rimo[MD]--; } foreach (i; 0..MD) { limo[i+1] += limo[i]; rimo[i+1] += rimo[i]; } long sm = 0; foreach (i; 0..MD) { int d = min(limo[i], rimo[i]); sm += 2*d; } writeln(sm); return 0; } /* IMPORT /home/yosupo/.dub/packages/dunkelheit-1.0.0/dunkelheit/source/dkh/scanner.d */ // module dkh.scanner; // import dkh.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; private File f; this(File f) { this.f = f; } private char[512] lineBuf; private char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { assert(succW()); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(Args...)(auto ref Args args) { import std.exception : enforce; static if (args.length != 0) { enforce(readSingle(args[0])); read(args[1..$]); } } bool hasNext() { return succ(); } } /* IMPORT /home/yosupo/.dub/packages/dunkelheit-1.0.0/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /home/yosupo/.dub/packages/dunkelheit-1.0.0/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto N = RD; auto c = RD!string; auto cnt = new long[](N+1); foreach (i; 0..N) { cnt[i+1] = cnt[i]; if (c[i] == 'R') ++cnt[i+1]; } long ans = long.max; foreach (i; 0..N+1) { auto l = i - cnt[i]; auto r = max(0, cnt[$-1] - cnt[i] - l); ans.chmin(l+r); debug writeln("i:", i, " ans:", l+r); } writeln(ans); stdout.flush; debug readln; }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int N; string S; scan(N); scan(S); writeln(S[0 .. N / 2] == S[N / 2 .. $] ? "Yes" : "No"); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
void main() { auto N = ri, K = ri, X = ri, Y = ri; if(N <= K) { writeln(N*X); return; } else { writeln(K*X + (N-K)*Y); return; } } // =================================== import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; 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
// Try AtCoder // author: Leonardone @ NEETSDKASU import std.stdio : readln, writeln; import std.array : split; import std.string : chomp; import std.conv : to; auto gets() { return readln.chomp; } auto getVals(T)() { return gets.split.to!(T[]); } void main() { auto xs = getVals!int; auto A = xs[0]; auto B = xs[1]; auto C = xs[2]; auto D = xs[3]; auto E = xs[4]; auto F = xs[5]; int ansSugar = 0, ansWater = 100 * A; for (int a = 0; 100 * A * a <= F; a++) { int sizeA = 100 * A * a; for (int b = 0; sizeA + 100 * B * b <= F; b++) { int water = sizeA + 100 * B * b; for (int c = 0; water + C * c <= F; c++) { int sizeC = C * c; if (sizeC * 100 > E * water) { break; } for (int d = 0; water + sizeC + D * d <= F; d++) { int sugar = sizeC + D * d; if (sugar * 100 > E * water) { break; } if (ansWater == 0) { ansSugar = sugar; ansWater = water; } else if (sugar*(ansWater+ansSugar) > ansSugar*(water+sugar)) { ansSugar = sugar; ansWater = water; } } } } } writeln(ansWater+ansSugar, " ", ansSugar); }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional, std.traits; import std.algorithm, std.container; void main() { readln; auto list = scanArray; long r=long.max; foreach(s; [true, false]) { long sum; bool b=s; long res; foreach(n; list) { if(b){ if(sum+n<=0) { res+= (-(sum+n))+1; sum=1; }else{ sum=sum+n; } }else{ if(sum+n>=0) { res+= ((sum+n))+1; sum=-1; }else{ sum=sum+n; } } b=!b; } r=min(res, r); } writeln(r); } class UnionFind{ UnionFind parent = null; void merge(UnionFind a) { if(same(a)) return; a.root.parent = this.root; } UnionFind root() { if(parent is null)return this; return parent = parent.root; } bool same(UnionFind a) { return this.root == a.root; } } void scanValues(TList...)(ref TList list) { auto lit = readln.splitter; foreach (ref e; list) { e = lit.fornt.to!(typeof(e)); lit.popFront; } } T[] scanArray(T = long)() { return readln.split.to!(long[]); } void scanStructs(T)(ref T[] t, size_t n) { t.length = n; foreach (ref e; t) { auto line = readln.split; foreach (i, ref v; e.tupleof) { v = line[i].to!(typeof(v)); } } } long scanULong(){ long x; while(true){ const c = getchar; if(c<'0'||c>'9'){ break; } x = x*10+c-'0'; } return x; } T scanElem(T = long)() { char[] res; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { res ~= cast(char) c; c = getchar; } return res.strip.to!T; } template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } struct Factor { long n; long c; } Factor[] factors(long n) { Factor[] res; for (long i = 2; i ^^ 2 <= n; i++) { if (n % i != 0) continue; int c; while (n % i == 0) { n = n / i; c++; } res ~= Factor(i, c); } if (n != 1) res ~= Factor(n, 1); return res; } long[] primes(long n) { if(n<2)return []; auto table = new long[n+1]; long[] res; for(int i = 2;i<=n;i++) { if(table[i]==-1) continue; for(int a = i;a<table.length;a+=i) { table[a] = -1; } res ~= i; } return res; } bool isPrime(long n) { if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (long i = 3; i ^^ 2 <= n; i += 2) if (n % i == 0) return false; return true; }
D
import std.stdio, std.string, std.conv, std.algorithm, std.range; void main(){ int m, f, r; string rs; while(true){ scanf("%d %d %d", &m, &f, &r); if (m == -1 && f == -1 && r == -1) break; if (m == -1 || f == -1){ rs ~= "F"; } else if (m+f>=80){ rs ~= "A"; } else if (m+f>=65){ rs ~= "B"; } else if (m+f>=50){ rs ~= "C"; } else if (m+f>=30){ if (r >= 50){ rs ~= "C"; }else{ rs ~= "D"; } } else { rs ~= "F"; } rs ~= "\r\n"; } writeln(rs.chomp); }
D
import std.stdio, std.math, std.conv, std.array, std.string, std.algorithm; void main() { uint c = to!uint(readln().chomp); for (uint i = 0; i < c; ++i) { double[] dx = map!(to!double)(readln().chomp.split).array; double[] ap = dx[0..2]; double[] bp = dx[3..5]; double ar = dx[2]; double br = dx[5]; double d = dist(ap, bp); if (d > (ar+br)^^2) { writeln(0); }else if (d < (ar-br)^^2) { if (br < ar) { writeln(2); } else { writeln(-2); } } else { writeln(1); } } } double dist(double[] ax, double[] bx) { return (ax[0]-bx[0])^^2 + (ax[1]-bx[1])^^2; }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; void main() { string[] input = new string[](8); while ((input = readln.chomp.split(" ")).length != 0) { auto points = input.map!(to!double); if (points[0] <= points[6] && points[2] >= points[4] && points[1] <= points[7] && points[3] >= points[5]) { writeln("YES"); } else { writeln("NO"); } } }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; import std.typecons; import std.bigint; // import dcomp.foundation, dcomp.scanner; // import dcomp.container.deque; int main() { auto sc = new Scanner(stdin); int n, m, k; sc.read(n, m, k); m++; int[] a = new int[n]; a.each!((ref x) => sc.read(x)); long[] dp = a.map!(to!long).array; long[] ndp = new long[n]; auto deq = Deque!int(); foreach (int ph; 2..k+1) { ndp[] = -(10L^^18); deq.clear(); foreach (int i; 0..n) { if (deq.length && deq[0] == i-m) { deq.removeFront(); } if (deq.length) { ndp[i] = dp[deq[0]] + 1L * ph * a[i]; } while (deq.length && dp[deq.back] <= dp[i]) { deq.removeBack(); } deq.insertBack(i); } swap(dp, ndp); } writeln(dp.fold!max); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; //fold(for old compiler) static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } unittest { import std.stdio; auto l = [1, 2, 3, 4, 5]; assert(l.fold!"a+b"(10) == 25); } } version (X86) static if (__VERSION__ < 2071) { int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/deque.d */ // module dcomp.container.deque; struct Deque(T) { import core.exception : RangeError; import core.memory : GC; import std.range : ElementType, isInputRange; import std.traits : isImplicitlyConvertible; struct Payload { T *d; size_t st, length, cap; @property bool empty() const { return length == 0; } alias opDollar = length; ref inout(T) opIndex(size_t i) inout { // version(assert) if (length <= i) throw new RangeError(); return d[(st+i >= cap) ? (st+i-cap) : st+i]; } private void expand() { import std.algorithm : max; assert(length == cap); auto nc = max(size_t(4), 2*cap); T* nd = cast(T*)GC.malloc(nc * T.sizeof); foreach (i; 0..length) { nd[i] = this[i]; } d = nd; st = 0; cap = nc; } void clear() { st = length = 0; } void insertFront(T v) { if (length == cap) expand(); if (st == 0) st += cap; st--; length++; this[0] = v; } void insertBack(T v) { if (length == cap) expand(); length++; this[length-1] = v; } void removeFront() { assert(!empty, "Deque.removeFront: Deque is empty"); st++; length--; if (st == cap) st = 0; } void removeBack() { assert(!empty, "Deque.removeBack: Deque is empty"); length--; } } struct RangeT(A) { alias T = typeof(*(A.p)); alias E = typeof(A.p.d[0]); T *p; size_t a, b; @property bool empty() const { return b <= a; } @property size_t length() const { return b-a; } @property RangeT save() { return RangeT(p, a, b); } @property RangeT!(const A) save() const { return typeof(return)(p, a, b); } alias opDollar = length; @property ref inout(E) front() inout { return (*p)[a]; } @property ref inout(E) back() inout { return (*p)[b-1]; } void popFront() { version(assert) if (empty) throw new RangeError(); a++; } void popBack() { version(assert) if (empty) throw new RangeError(); b--; } ref inout(E) opIndex(size_t i) inout { return (*p)[i]; } RangeT opSlice() { return this.save; } RangeT opSlice(size_t i, size_t j) { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } RangeT!(const A) opSlice() const { return this.save; } RangeT!(const A) opSlice(size_t i, size_t j) const { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } } alias Range = RangeT!Deque; alias ConstRange = RangeT!(const Deque); alias ImmutableRange = RangeT!(immutable Deque); Payload *p; private void I() { if (!p) p = new Payload(); } private void C() const { // version(assert) if (!p) throw new RangeError(); } //some value this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) {I; p = new Payload(); foreach (v; values) { insertBack(v); } } //range this(Range)(Range r) if (isInputRange!Range && isImplicitlyConvertible!(ElementType!Range, T) && !is(Range == T[])) {I; p = new Payload(); foreach (v; r) { insertBack(v); } } @property bool empty() const { return (!p || p.empty); } @property size_t length() const { return (p ? p.length : 0); } alias opDollar = length; ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; } ref inout(T) front() inout {C; return (*p)[0]; } ref inout(T) back() inout {C; return (*p)[$-1]; } void clear() { if (p) p.clear(); } void insertFront(T v) {I; p.insertFront(v); } void insertBack(T v) {I; p.insertBack(v); } void removeFront() {C; p.removeFront(); } void removeBack() {C; p.removeBack(); } Range opSlice() {I; return Range(p, 0, length); } } unittest { import std.algorithm : equal; import std.range.primitives : isRandomAccessRange; import std.container.util : make; auto q = make!(Deque!int); assert(isRandomAccessRange!(typeof(q[]))); //insert,remove assert(equal(q[], new int[](0))); q.insertBack(1); assert(equal(q[], [1])); q.insertBack(2); assert(equal(q[], [1, 2])); q.insertFront(3); assert(equal(q[], [3, 1, 2]) && q.front == 3); q.removeFront; assert(equal(q[], [1, 2]) && q.length == 2); q.insertBack(4); assert(equal(q[], [1, 2, 4]) && q.front == 1 && q.back == 4 && q[$-1] == 4); q.insertFront(5); assert(equal(q[], [5, 1, 2, 4])); //range assert(equal(q[][1..3], [1, 2])); assert(equal(q[][][][], q[])); //const range const auto rng = q[]; assert(rng.front == 5 && rng.back == 4); //reference type auto q2 = q; q2.insertBack(6); q2.insertFront(7); assert(equal(q[], q2[]) && q.length == q2.length); //construct with make auto a = make!(Deque!int)(1, 2, 3); auto b = make!(Deque!int)([1, 2, 3]); assert(equal(a[], b[])); } unittest { import std.algorithm : equal; import std.range.primitives : isRandomAccessRange; import std.container.util : make; auto q = make!(Deque!int); q.clear(); assert(equal(q[], new int[0])); foreach (i; 0..100) { q.insertBack(1); q.insertBack(2); q.insertBack(3); q.insertBack(4); q.insertBack(5); assert(equal(q[], [1,2,3,4,5])); q.clear(); assert(equal(q[], new int[0])); } } unittest { Deque!int a; Deque!int b; a.insertFront(2); assert(b.length == 0); } unittest { import std.algorithm : equal; import std.range : iota; Deque!int a; foreach (i; 0..100) { a.insertBack(i); } assert(equal(a[], iota(100))); } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; if (f.eof) return false; line = lineBuf[]; f.readln(line); } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { //string or char[10] etc //todo optimize auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { //static assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); fout.writeln("1 2 3"); fout.writeln("ab cde"); fout.writeln("1.0 1.0 2.0"); fout.close; Scanner sc = new Scanner(File(fileName, "r")); int a; int[2] b; char[2] c; string d; double e; double[] f; sc.read(a, b, c, d, e, f); assert(a == 1); assert(equal(b[], [2, 3])); assert(equal(c[], "ab")); assert(equal(d, "cde")); assert(e == 1.0); assert(equal(f, [1.0, 2.0])); } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File, writeln; import std.datetime; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); foreach (i; 0..1_000_000) { fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2); } fout.close; writeln("Scanner Speed Test(3*1,000,000 int)"); StopWatch sw; sw.start; Scanner sc = new Scanner(File(fileName, "r")); foreach (i; 0..500_000) { int a, b, c; sc.read(a, b, c); assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 500_000..700_000) { int[3] d; sc.read(d); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 700_000..1_000_000) { int[] d; sc.read(d); assert(d.length == 3); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } writeln(sw.peek.msecs, "ms"); }
D
import core.bitop; import core.checkedint; import core.simd; import core.stdc.stdio; import core.stdc.stdlib; import core.stdc.string; import std.algorithm; import std.array; import std.ascii; import std.bigint; import std.bitmanip; import std.complex; import std.container; import std.conv; import std.datetime; import std.format; import std.functional; import std.math; import std.meta; import std.numeric; import std.random; import std.range; import std.regex; //import std.stdio; import std.string; import std.typecons; import std.variant; __gshared: /+ bool read(T...)(T ptrs) if (ptrs.length > 0) { return readf(' ' ~ replicate("%s ", ptrs.length), ptrs) == ptrs.length; } +/ T[ ] allocate(T)(size_t n) { return (cast(T*)malloc(n * T.sizeof))[0 .. n]; } auto pairwise(R)(R range) if (isForwardRange!R) { return lockstep(range, dropOne(range)); } struct Dsu(int n) { int[n] p; void init() { iota(n).copy(p[ ]); } int get(int x) { return x == p[x] ? x : (p[x] = get(p[x])); } void merge(int x, int y) { p[get(x)] = get(y); } } int n; Dsu!10_000 dsu; void main() { scanf("%d", &n); dsu.init(); foreach (i; 0 .. n) { int x; scanf("%d", &x); dsu.merge(i, x - 1); } int result = 0; foreach (i; 0 .. n) if (i == dsu.get(i)) result++; printf("%d\n", result); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; int[10^^5*2+2] LR, DU; void main() { auto hwn = readln.split.to!(int[]); auto H = hwn[0]; auto W = hwn[1]; auto N = hwn[2]; auto rc = readln.split.to!(int[]); auto SR = rc[0]; auto SC = rc[1]; auto S = readln.chomp.to!(char[]); auto T = readln.chomp.to!(char[]); int a, b, c, d; a = 0, b = SC, c = SC, d = W+1; foreach (i; 0..N) { if (S[i] == 'L') { ++a; } else if (S[i] == 'R') { --d; } if (a == c || b == d || a+1 == d) { writeln("NO"); return; } if (T[i] == 'L') { if (d <= W) { ++d; } else if (b > 0) { --b; } } else if (T[i] == 'R') { if (a > 0) { --a; } else if (c <= W) { ++c; } } } a = 0, b = SR, c = SR, d = H+1; foreach (i; 0..N) { if (S[i] == 'U') { ++a; } else if (S[i] == 'D') { --d; } if (a == c || b == d || a+1 == d) { writeln("NO"); return; } if (T[i] == 'U') { if (d <= W) { ++d; } else if (b > 0) { --b; } } else if (T[i] == 'D') { if (a > 0) { --a; } else if (c <= W) { ++c; } } } writeln("YES"); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; void main() { int[] buf = readln.chomp.split.to!(int[]); int a = buf[0], b = buf[1]; if (a + b == 15) { writeln("+"); } else if (a * b == 15) { writeln("*"); } else { writeln("x"); } }
D
void main() { problem(); } void problem() { auto X = scan!long; long solve() { long answer; auto num500 = X / 500; auto num5 = (X - num500 * 500) / 5; return num500 * 1000 + num5 * 5; } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { int a, b; char op; scan(a, op, b); if (op == '+') { writeln(a+b); } else { writeln(a-b); } } 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.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = readln.chomp.to!int; auto l = new long[](n+1); l[0] = 2; l[1] = 1; foreach (i; 2..n+1) l[i] = l[i-1]+l[i-2]; writeln(l[n]); }
D
import std.stdio; import std.range; import std.array; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.container; import std.typecons; import std.random; import std.csv; import std.regex; import std.math; import core.time; import std.ascii; import std.digest.sha; import std.outbuffer; void main() { int n = readln.chomp.to!int; writeln = (n + 2) / 2; }
D
// Cheese-Cracker: cheese-cracker.github.io import std.stdio, std.conv, std.functional, std.string, std.algorithm; import std.container, std.range, std.typecons, std.numeric, std.math, std.random; alias ll = long; alias rbt = redBlackTree; alias tup = Tuple!(long, long); static string[] inp; T rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;} T[] rdarr(T = long)(T fix = 0){ auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } void show(A...)(A a) { debug{ foreach(t; a){ write(t, "| "); } writeln; } } /*******************************It's a Me, Mario!*******************************************/ void play(){ int n; n = rd!int; ll[] arr = rdarr; foreach(i; 0..n-1){ if(arr[i] <= arr[i+1]){ writeln("YES"); return; } } writeln("NO"); } int main(){ long t = 1; t = rd; // Toggle! while(t--) play(); // Let's play! stdout.flush; return 0; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto q = RD!int; auto ans = new long[](q); foreach (i; 0..q) { auto n = RD!int; if (n == 2) { ans[i] = 2; } else ans[i] = n % 2 == 0 ? 0 : 1; } foreach (e; ans) writeln(e); stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } T binarySearch(alias pred, T)(T ok, T ng) { while (abs(ok-ng) > 1) { auto mid = (ok+ng)/2; if (unaryFun!pred(mid)) ok = mid; else ng = mid; } return ok; } void main() { auto T = RD!int; auto ans = new int[](T); foreach (ti; 0..T) { auto s = RD!string; auto t = RD!string; auto arr = new int[][](26); foreach (i; 0..s.length) { arr[s[i]-'a'] ~= cast(int)i; } ans[ti] = 1; int pos = -1; foreach (i; 0..t.length) { auto c = t[i]-'a'; bool f(int x) { return arr[c][x] > pos; } auto r = binarySearch!(f)(cast(int)arr[c].length, -1); if (r == arr[c].length) { ++ans[ti]; pos = -1; r = binarySearch!(f)(cast(int)arr[c].length, -1); if (r == arr[c].length) { ans[ti] = -1; break; } } pos = arr[c][r]; debug writeln("pos:", pos); } } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } int N; int[] U, V; void main() { try { for (; ; ) { N = readInt(); U = new int[N - 1]; V = new int[N - 1]; foreach (i; 0 .. N - 1) { U[i] = readInt() - 1; V[i] = readInt() - 1; } auto deg = new int[N]; foreach (i; 0 .. N - 1) { ++deg[U[i]]; ++deg[V[i]]; } const ans = (deg.count(2) == 0); writeln(ans ? "YES" : "NO"); } } catch (EOFException e) { } }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int h, w; rd(h, w); auto c=new char[][](h); foreach(i; 0..h) c[i]=readln.chomp.to!(char[]); auto t=readln.chomp.to!(char[]); import std.typecons; alias P=Tuple!(int, "i", int, "j"); P st, gl; foreach(i; 0..h)foreach(j; 0..w){ if(c[i][j]=='S') st=P(i, j); if(c[i][j]=='E') gl=P(i, j); } auto dir=[[-1, 0], [0, 1], [1, 0], [0, -1]]; // LURD auto nums=[0, 1, 2, 3]; int tot=0; do{ auto cur=st; foreach(char ch; t){ int x=ch-'0'; auto d=dir[nums[x]]; auto nex=P(cur.i+d[0], cur.j+d[1]); if(nex.i<0 || nex.i>=h || nex.j<0 || nex.j>=w) break; if(c[nex.i][nex.j]=='#') break; if(c[nex.i][nex.j]=='E'){tot++; break;} cur=nex; } }while(nextPermutation(nums)); writeln(tot); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } } void wr(T...)(T x){ import std.stdio; foreach(e; x) write(e, " "); writeln(); }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T read(T)(){ return read.to!T; } T[] read(T)(long n){ return n.iota.map!(_ => read!T).array; } T[][] read(T)(long m, long n){ return m.iota.map!(_ => read!T(n)).array; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ int n = read.to!int; bool[][] as; foreach(i; 0 .. n){ as ~= readln.to!(char[]).map!(x => x == '#').array; } string ans = "YES"; foreach(i; 0 .. n) foreach(j; 0 .. n){ if(as[i][j]) continue; if(j - 1 < 0 || i + 2 >= n || j + 1 >= n){ "NO".writeln; return; } if(as[i + 1][j - 1] || as[i + 1][j] || as[i + 1][j + 1] || as[i + 2][j]){ "NO".writeln; return; } as[i][j] = 1; as[i + 1][j - 1] = 1; as[i + 1][j] = 1; as[i + 1][j + 1] = 1; as[i + 2][j] = 1; } "YES".writeln; return; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string; immutable int M = 30; long x, y; int cnt = 0; int ask(long c, long d) { debug { cnt += 1; if ((x^c) > (y^d)) return 1; else if ((x^c) == (y^d)) return 0; else return -1; } writeln("? ", c, " ", d); stdout.flush; return readln.chomp.to!int; } void answer(long a, long b) { writeln("! ", a, " ", b); stdout.flush; } void main() { debug { auto s = readln.split.map!(to!long); x = s[0]; y = s[1]; } int f = ask(0, 0); int ff = f; if (f == 0) { long a = 0; foreach_reverse (i; 0..M) { long c = (1L << (i + 1)) - 1; long d = (1L << i) - 1; c += a; d += a; f = ask(c, d); if (f == -1) { a += 1L << i; } } answer(a, a); return; } bool a_large = f == 1; long a = 0; long b = 0; auto same = new bool[](M); foreach_reverse (i; 0..M) { long c = 1L << i; f = ask(a+c, b+c); if (a_large && f == -1) { a += c; f = ask(a, b); a_large = f == 1; } else if (!a_large && f == 1) { b += c; f = ask(a, b); a_large = f == 1; } else { same[i] = true; } } foreach (i; 0..M) { if (!same[i]) { continue; } long c = 1L << i; f = ask(a+c, b); if (f == -1) { a += c; b += c; } } answer(a, b); debug{cnt.writeln;} }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto p = RD!int-1; auto k = RD!int; auto a = RD!string; auto x = RD; auto y = RD; ans[ti] = long.max; long[int] memo; foreach (i; 0..n-p) { long dfs(int pp) { if (pp >= n) return 0; auto m = memo.get(pp, -1); if (m != -1) return m; long c = a[pp] == '0' ? x : 0; auto res = dfs(pp+k); if (pp+k < n) { auto res2 = (pp+k-p)*y + res; ans[ti].chmin(res2); } memo[pp] = c + res; return c + res; } ans[ti].chmin(dfs(p+i) + i*y); } } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio, std.string, std.conv, std.algorithm; void main(){ int n; rd(n); long a, b; rd(a, b); long[] cand; for(int i=2; i*i<=a; i++){ if(a%i==0){ cand~=i; while(a%i==0) a/=i; } } if(a>1) cand~=a; for(int i=2; i*i<=b; i++){ if(b%i==0){ cand~=i; while(b%i==0) b/=i; } } if(b>1) cand~=b; foreach(i; 0..(n-1)){ long s, t; rd(s, t); long[] nex; foreach(x; cand){ if(s%x==0 || t%x==0) nex~=x; } cand.swap(nex); } if(cand.length>0){ writeln(cand[0]); }else{ writeln(-1); } } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x) e=l[i].to!(typeof(e)); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto H = RD; auto W = RD; auto C = new string[](H); foreach (i; 0..H) { C[i] = RD!string; } foreach (i; 0..H) { writeln(C[i]); writeln(C[i]); } stdout.flush(); debug readln(); }
D
import std.stdio, std.string, std.conv, std.array, std.algorithm; void main() { while (true) { auto a = readln.split.map!(to!int); if (a[0] == 0 && a[1] == 0) break; if (a[0] < a[1]) writeln(a[0], " ", a[1]); else writeln(a[1], " ", a[0]); } }
D
import std.stdio; immutable mod = 4294967311L; long[] inv; void main(){ long x; int n, y, op; scanf("%d", &n); inv = new long[1_000_001]; inv[1] = 1; foreach(_; 0..n){ scanf("%d%d", &op, &y); if(op >= 3 && y < 0) x = -x, y = -y; if(op == 1) x += y; else if(op == 2) x -= y; else if(op == 3) x *= y; else if(op == 4) x = mul(x, inv_(y)); x %= mod; } if(x < 0) x += mod; if(x > int.max) x -= mod; writeln(x); } long inv_(int x){ return inv[x] ? inv[x] : (inv[x]=mul(inv_(mod%x), mod-mod/x)); } long mul(long x, long y){ return ( ((x*(y>>16)%mod)<<16) + (x*(y&0xffff)) ) % mod; }
D
import std.stdio, std.string, std.conv; void main() { foreach (i; 1 .. 10001) { int x = readln.chomp.to!int; if (x == 0) break; writeln("Case ", i, ": ", x); } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n, t; scan(n, t); auto a = readln.split.to!(int[]); a ~= 0; int mn = a[0]; int mx = a[0]; int md = 0; int ans; foreach (i ; 1 .. n + 1) { if (a[i] > mn) { mx = max(mx, a[i]); } else { if (md < mx - mn) { ans = 1; md = mx - mn; } else if (md == mx - mn) { ans++; } mn = mx = a[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 core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } alias sread = () => readln.chomp(); void main() { long A = lread(); long B = lread(); long C = lread(); long X = lread(); long i; foreach (a; 0 .. A + 1) foreach (b; 0 .. B + 1) foreach (c; 0 .. C + 1) if ((a * 500 + b * 100 + c * 50) == X) { i++; } i.writeln(); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n; readV(n); writeln(n < 1000 ? "ABC" : "ABD"); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { auto S = sread(); long[2] cnt; foreach (c; S) cnt[c - 'A']++; writeln((cnt[0] * cnt[1] != 0) ? "Yes" : "No"); }
D
void main() { long h, w; rdVals(h, w); if (h == 1 || w == 1) 1.writeln; else writeln((h * w + 1) / 2); } 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.string; import std.stdio; import std.range; import std.algorithm; import std.array; import std.conv; void bfs(ref int[] table, int curr) { // 上下左右 int[] directions = [1, 14, -1, -14]; int[] que; que ~= curr; while(que != null) { auto point = que.front; que.popFront; foreach(d; directions) { if (point + d < table.length && table[point + d] == 1) { que ~= point + d; table[point + d] = 0; } } } } void main() { while(true) { int[] table; int count = 0; table ~= repeat(0).take(14).array; foreach(t; 0..12) { string line = readln.chomp; table ~= [0] ~ line.array.map!((x) => x.to!(int) - '0').array ~ [0]; } table ~= repeat(0).take(14).array; foreach(i; 1..13) { foreach(j; 1..13) { int num = j + i * 14; if (table[num] == 1) { bfs(table, num); ++count; } } } writeln(count); readln; if (stdin.eof) goto end; } end:; }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int N, R; scan(N, R); auto ans = N >= 10 ? R : R + 100 * (10 - N); writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nab = readln.split.to!(long[]); auto N = nab[0]; auto A = nab[1]; auto B = nab[2]; if ((B-A)%2 == 0) { writeln((B-A)/2); } else { writeln(min(A + (B-A)/2, N-B+1 + (B-A-1)/2)); } }
D
import std.stdio, std.math, std.algorithm, std.array, std.string, std.conv, std.container, std.range; pragma(inline, true) T[] Reads(T)() { return readln.split.to!(T[]); } alias reads = Reads!int; pragma(inline, true) void scan(Args...)(ref Args args) { string[] ss = readln.split; foreach (i, ref arg ; args) arg = ss[i].parse!int; } void main() { int a,b,c; scan(a,b,c); writeln(max(0, c - (a - b))); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); int[] a; for(int x=6; x<=n; x*=6) a~=x; for(int x=9; x<=n; x*=9) a~=x; a~=1; auto dp=new int[](n+1); const int inf=1_000_000_000; fill(dp, inf); dp[0]=0; foreach(i; 0..n)foreach(e; a){ if(i+e<=n) dp[i+e]=min(dp[i+e], dp[i]+1); } writeln(dp[n]); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x) e=l[i].to!(typeof(e)); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.typecons; import std.numeric, std.math; import core.bitop; string FMT_F = "%.10f"; T RD(T = long)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto A = RD; auto B = RD; auto C = RD; writeln(min(B / A, C)); stdout.flush(); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.datetime; void main() { auto S = readln.chomp; auto s = readln.split.map!(to!int); auto X = s[0]; auto Y = s[1]; int tmp = 0; bool isX = true; int[] yoko; int[] tate; foreach (c; S) { if (c == 'F') { tmp += 1; } else if (isX) { yoko ~= tmp; isX ^= 1; tmp = 0; } else { tate ~= tmp; isX ^= 1; tmp = 0; } } if (isX) yoko ~= tmp; else tate ~= tmp; auto dpX = new bool[int][](yoko.length+1); dpX[0][0] = true; foreach (i; 0..yoko.length) { foreach (k; dpX[i].keys) { dpX[i+1][k + yoko[i]] = true; if (i > 0) dpX[i+1][k - yoko[i]] = true;; } } auto dpY = new bool[int][](tate.length+1); dpY[0][0] = true; foreach (i; 0..tate.length) { foreach (k; dpY[i].keys) { dpY[i+1][k + tate[i]] = true; dpY[i+1][k - tate[i]] = true; } } if (X in dpX[yoko.length] && Y in dpY[tate.length]) { writeln("Yes"); } else { writeln("No"); } }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int r, c, si, sj, gi, gj; rd(r, c, si, sj, gi, gj); const int mod = 10 ^^ 9 + 7; const int inf = 10 ^^ 9; auto dist = new int[][](r, c), dp = new int[][](r, c); foreach (i; 0 .. r) foreach (j; 0 .. c) dist[i][j] = inf; struct T { int y, x; } auto dir = [T(1, 0), T(-1, 0), T(0, 1), T(0, -1)]; auto dr = [0, r - 1], dc = [0, c - 1]; auto q = new Queue!(T)(r * c); dist[si][sj] = 0; dp[si][sj] = 1; q.insertBack(T(si, sj)); void relieve(int y, int x, int ny, int nx) { if (0 <= ny && ny < r && 0 <= nx && nx < c) { if (dist[y][x] + 1 == dist[ny][nx]) { (dp[ny][nx] += dp[y][x]) %= mod; } if (dist[ny][nx] == inf) { dist[ny][nx] = dist[y][x] + 1; dp[ny][nx] = dp[y][x]; q.insertBack(T(ny, nx)); } } } while (!q.empty) { auto cur = q.front; q.removeFront; foreach (d; dir) { relieve(cur.y, cur.x, cur.y + d.y, cur.x + d.x); } foreach (d; dr) relieve(cur.y, cur.x, d, cur.x); foreach (d; dc) relieve(cur.y, cur.x, cur.y, d); } writeln(dist[gi][gj], " ", dp[gi][gj]); } class Queue(T) { private: int n, l = 0, r = 0; T[] arr; public: this(int size) { n = size + 1; arr.length = n; } bool empty() { return l == r; } bool full() { return l == (r + 1) % n; } T front() { return arr[l]; } void insertBack(T x) { assert(full == false); arr[r] = x; (r += 1) %= n; } void removeFront() { assert(empty == false); (l += 1) %= n; } } void rd(T...)(ref T x) { import std.stdio : readln; import std.string : split; import std.conv : to; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; import std.datetime; import std.numeric; import std.math; import std.string; string my_readln() { return chomp(readln()); } void main() { auto tokens = my_readln().split(); auto N = tokens[0].to!ulong; auto M = tokens[1].to!ulong; ulong[][2][] v; v.length = N; foreach (i; 0..M) { auto tokens2 = my_readln().split(); auto x = tokens2[0].to!ulong - 1; auto y = tokens2[1].to!ulong - 1; v[y][0] ~= x; v[x][1] ~= y; } ulong[] memo; memo.length = N; ulong search(ulong pos) { ulong r; foreach (e; v[pos][1]) { r = max(r, memo[e] == 0 ? search(e) + 1 : memo[e] + 1); } stderr.writeln("a", pos, " ", r); memo[pos] = r; return r; } ulong ans; foreach (i; 0..N) { if (v[i][0].length != 0) continue; ans = max(ans, search(i)); stderr.writeln("b", i, " ", ans); } writeln(ans); stdout.flush(); }
D
void main() { auto N = ri; if(N == 1) { writeln(1); return; } auto arr = generate_prime_list(N); ulong[ulong] m; foreach(i; 1..N+1) { foreach(j; arr) { while(i % j == 0) { m[j]++; i /= j; } } if(arr.canFind(i)) m[i]++; } ulong mod = 1000000007; m.values.map!(i => i + 1).reduce!((i, j) => (i * j) % mod).writeln; } ulong fact(ulong n) { if(n == 1) return 1; else return n * fact(n-1); } ulong[] generate_prime_list(T)(T N) if(isIntegral!T) { ulong[] prime_list = [2]; bool not_prime = false; foreach(i; 3..N.to!ulong+1) { foreach(j; prime_list) { if(i % j == 0) { not_prime = true; break; } } if(!not_prime) prime_list ~= i; not_prime = false; } return prime_list; } // =================================== 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 N = readln.chomp.to!int; auto vs = readln.split.to!(int[]); auto cs = readln.split.to!(int[]); int r; foreach (i; 0..N) { if (vs[i] - cs[i] > 0) r += vs[i] - cs[i]; } writeln(r); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(int g, int[][] ps) { int n = cast(int)ps.length; int ans = int.max; for (int i = 0; i < (1 << n); i++) { int score = 0; int solved = 0; // 解いた問題数 for (int j = 0; j < n; j++) { if (i & (1 << j)) { // j は全て解く score += 100 * (j+1) * ps[j][0] + ps[j][1]; solved += ps[j][0]; } } if (score >= g) { ans = min(ans, solved); } else { // score が足りないなら、まだ解いていない問題から点数の高い問題を解く for (int j = n-1; j >= 0; j--) { if (i & (1 << j)) continue; int s = 100 * (j+1) * ps[j][0]; // 得られるスコア int x = g - score; // 不足分のスコア if (x <= s) { int r = x / (100 * (j+1)); int m = x % (100 * (j+1)); solved += r + (m > 0 ? 1 : 0); ans = min(ans, solved); } break; } } } return ans; } void main() { auto dg = readints; int d = dg[0], g = dg[1]; int[][] ps; for (int i = 0; i < d; i++) { ps ~= readints; } writeln(calc(g, ps)); }
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() { int d = readint; string[] ss = ["Christmas"]; for (int i = 0; i < 25 - d; i++) ss ~= "Eve"; writeln(ss.join(" ")); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp.to!int; auto c = new int[][][](n); foreach (i; 0..n-1) { auto t = readln.chomp.split.to!(int[]); auto cost = t[2] % 2 ? 1 : 2; auto a = new int[](2); a[0] = t[1] - 1; a[1] = cost; c[t[0]-1] ~= a; auto b = new int[](2); b[0] = t[0] - 1; b[1] = cost; c[t[1]-1] ~= b; } auto res = new int[](n); auto f = new bool[](n); void solve(int index, int cost) { if (f[index]) { return; } f[index] = true; res[index] = cost; foreach (e; c[index]) { solve(e[0], cost + e[1]); } } solve(0, 0); writeln("0"); foreach (i; 1..n) { writeln(res[i] % 2); } }
D
import std; void main() { int t; scanf("%d", &t); getchar(); foreach(_; 0..t) { int n; scanf("%d", &n); getchar(); long[] a = readln.strip.split(" ").to!(long[]); long[] b = readln.strip.split(" ").to!(long[]); foreach (i; 0..n) { if (a[i] < b[i]) swap(a[i], b[i]); } writeln(maxElement(a)*maxElement(b)); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math; void main() { auto N = readln.chomp.to!int; auto AS = readln.split.to!(int[]); auto cnt = int.max; foreach (a; AS) { int i; for (; a && a%2 == 0; ++i) a /= 2; cnt = min(cnt, i); } writeln(cnt); }
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 bool[](t); foreach (ti; 0..t) { auto s = RD!string; auto ss = RD!string; if (s.length < ss.length) continue; if ((ss.length - s.length) % 2) s.popFront; int i, j; while (i < s.length && j < ss.length) { if (s[i] == ss[j]) { ++i; ++j; } else { i += 2; } } debug writeln("i:", i, " j:", j); if (j == ss.length) ans[ti] = true; } foreach (e; ans) { writeln(e ? "YES" : "NO"); } stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; writeln(N % 2 == 0 ? N : N * 2); stdout.flush(); debug readln(); }
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 n = lread(); auto a = aryread(); // writeln(a); long cnt = 1; foreach (i; 0 .. n) { if (a[i] == cnt) { // writeln(a[i], ' ', i); cnt += 1; } else { continue; } } if (cnt == 1) { writeln(-1); return; } else { writeln(a.length - cnt + 1); } } 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.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 x = readln.chomp.to!int; writeln(x ^^ 3); }
D
import std.algorithm; import std.array; import std.ascii; import std.container; import std.conv; import std.format; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int n; int[] a; int dfs(int depth, bool gu) { if (depth == n) { return gu; } int cnt = 0; for (int i = -1; i <= 1; ++i) { cnt += dfs(depth + 1, gu || ((a[depth] + i) % 2 == 0)); } return cnt; } void main() { n = readln.chomp.to!int; a = readln.chomp.split.map!(to!int).array; writeln = dfs(0, false); }
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(){ long n = scan!long; long s = scan!long; long infty = n + 999; long ans = infty; for(long b = 2; b * b <= n; b ++) if(digsum(n, b) == s) ans.lowerTo(b), log("ans:", ans); long[] ds = (n - s).divisors; foreach(d; ds) if(digsum(n, d + 1) == s) ans.lowerTo(d + 1), log("ans:", ans); if(n == s) ans.lowerTo(n + 1); if(ans >= infty) "-1".writeln; else ans.writeln; } long digsum(long n, long b){ long ans; log("n:", n, "b:", b); while(n > 0){ ans += n % b; n /= b; } log("sum:", ans); return ans; } /// 約数の集合 /// 1とその数自身も含む、正の約数の集合。 /// 例 divisors(48) = [1, 2, 3, 4, 6, 8, 12, 16, 24, 48] /// O(√N) long[] divisors(long a){ long[] res, res2; for(long d = 1; d * d <= a; d ++){ if(a % d == 0) res ~= d, res2 ~= a / d; } foreach_reverse(d; res2) if(res[$ - 1] < d) res ~= d; return res; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void main() { auto N = readln.chomp.to!int; auto rs = new int[][N]; int[] as = [0]; foreach (i; 1..N) { auto a = readln.chomp.to!int-1; as ~= a; rs[a] ~= i; } int solve(int i) { if (rs[i].empty) return 1; int[] cs; foreach (j; rs[i]) cs ~= solve(j); sort!"a > b"(cs); int r; foreach (k, c; cs) r = max(r, k.to!int + c + 1); return r; } writeln(solve(0)-1); }
D
import std.stdio; import std.algorithm; import std.conv; import std.datetime; import std.numeric; import std.math; import std.string; string my_readln() { return chomp(readln()); } void main() {//try{ auto tokens = split(my_readln()); auto S = to!string(tokens[0]); ulong ans, cnt_b; foreach (i, e; S) { if (e == 'B') ++cnt_b; else ans += cnt_b; } writeln(ans); stdout.flush(); /*}catch (Throwable e) { writeln(e.toString()); } readln();*/ }
D
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.typecons, std.range, std.container, std.math, std.algorithm.searching, std.functional,std.mathspecial, std.numeric; void main(){ auto abc=readln.split.map!(to!int).array; auto gcd=gcd(abc[0],abc[1]); writeln(abc[2]%gcd==0?"YES":"NO"); }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; import std.typecons; alias Edge = Tuple!(int, "from", int, "to", long, "cost"); Edge[] edges = new Edge[](0); int[][] ordg; int[][] revg; bool[] isOk; bool[] isRevOk; void main() { int n = read.to!int; int m = read.to!int; long p = read.to!long; ordg = new int[][](n, 0); revg = new int[][](n, 0); isOk = new bool[](n); isRevOk = new bool[](n); foreach (i; 0..m) { auto f = read.to!int-1; auto t = read.to!int-1; edges ~= Edge(f, t, - (read.to!long - p)); ordg[f] ~= t; revg[t] ~= f; } dfs(0); rdfs(n-1); auto d = new long[](n); fill(d, long.max); d[0] = 0; int step = 0; while(true) { bool update = false; foreach (i; 0..m) { if (! (isOk[edges[i].from] && isRevOk[edges[i].from])) continue; if (! (isOk[edges[i].to] && isRevOk[edges[i].to])) continue; if (d[edges[i].from] != long.max && d[edges[i].from] + edges[i].cost < d[edges[i].to]) { update = true; d[edges[i].to] = d[edges[i].from] + edges[i].cost; } } step++; if (step > n) { writeln(-1); return; } if (!update) break; } auto ret = -d[n-1]; writeln(max(0, ret)); } void dfs(int v) { if (isOk[v]) return; isOk[v] = true; foreach (e; ordg[v]) { dfs(e); } } void rdfs(int v) { if (isRevOk[v]) return; isRevOk[v] = true; foreach (e; revg[v]) { rdfs(e); } } string read() { static string[] ss; while (!ss.length) ss = readln.chomp.split; auto res = ss[0]; ss.popFront; return res; }
D
void main() { long[] tmp = rdRow; long n = tmp[0], a = tmp[1]; long[] x = rdRow; long lim = 50 * 50 + 1; long[][] dp = new long[][](n+1, lim); foreach (i, y; x) { foreach_reverse (j; 1 .. i+1) { foreach (k; 0 .. lim-y) { dp[j+1][k+y] += dp[j][k]; } } ++dp[1][y]; } long result; foreach (i; 1 .. n+1) { result += dp[i][i*a]; } result.writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
import 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; void main() { auto T = readln.chomp.to!int; while (T--) { auto s = readln.split.map!(to!long); auto a = s[0]; auto b = s[1]; auto c = s[2]; auto r = s[3]; if (a > b) swap(a, b); auto u = c - r; auto v = c + r; if (u >= b || v <= a) { writeln(b - a); } else { u = max(u, a); v = min(v, b); writeln(b - a - (v - u)); } } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto q = RD!int; auto ans = new long[](q); foreach (i; 0..q) { auto c = RD; auto m = RD; auto x = RD; auto a = c + m + x; ans[i] = min(min(c, m), a/3); } foreach (e; ans) { writeln(e); } stdout.flush(); debug readln(); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { auto S = sread(); long K = lread(); long N = S.length; long[] len; long i; while (i < N) { long l; char prev = S[i]; while (i + l < N && S[i + l] == prev) l++; len ~= l; i += l; } if (len.length == 1) { writeln(len[0] * K / 2); return; } long ans = len.map!"a/2"().sum() * K; if (S[0] == S[$ - 1]) { ans -= (len[0] / 2) * (K - 1); ans -= (len[$ - 1] / 2) * (K - 1); ans += ((len[0] + len[$ - 1]) / 2) * (K - 1); } writeln(ans); }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto n = readln.strip.to !(int); auto a = readln.splitter.map !(to !(int)).array; auto b = readln.splitter.map !(to !(int)).array; foreach (i; 0..n) { if (a[i] > b[i]) { swap (a[i], b[i]); } } auto c = new int [n]; c[] = b[] - a[]; auto total = sum (c); auto f = new bool [total + 1]; f[0] = true; foreach (i; 0..n) { foreach_reverse (j; c[i]..total + 1) { f[j] |= f[j - c[i]]; } } auto half = total / 2; while (!f[half]) { half -= 1; } int res = 0; res += (sum (a) + half) ^^ 2; res += (sum (a) + total - half) ^^ 2; foreach (i; 0..n) { res += (n - 2) * a[i] ^^ 2; res += (n - 2) * b[i] ^^ 2; } writeln (res); } }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto K = s[1]; auto A = readln.split.map!(to!long).array; long ans = 1L << 60; foreach (i; 0..(1<<N)) { long h_max = A[0]; int visible = 1; long tmp = 0; foreach (j; 1..N) { if (h_max < A[j]) { h_max = A[j]; visible += 1; } else if (i & (1 << j)) { tmp += h_max - A[j] + 1; h_max = h_max + 1; visible += 1; } } if (visible >= K) ans = min(ans, tmp); } ans.writeln; }
D
import std.stdio; import std.string; import std.array; // split import std.conv; // to void main() { string s = chomp(readln()); for(int i=0; i<s.length; ++i){ if(i%2 == 0){ write(s[i]); } } }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop; // dfmt on void main() { long N = lread(); auto A = aryread(); auto ans = new long[](N); foreach (a; A) { ans[a - 1]++; } foreach (a; ans) { writeln(a); } }
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; bool calc(int x, int y) { if (x == 2 || y == 2) return false; auto a = [1, 3, 5, 7, 8, 10, 12]; auto b = [4, 6, 9, 11]; if (a.canFind(x) && a.canFind(y)) return true; if (b.canFind(x) && b.canFind(y)) return true; return false; } void main() { auto xy = readints; int x = xy[0], y = xy[1]; writeln(calc(x, y) ? "Yes" : "No"); }
D
void main() { problem(); } void problem() { auto N = scan!int; auto X = scan!int; auto T = scan!int; int solve() { auto t = N % X == 0 ? N / X : 1 + N / X; return t * T; } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto a=new int[](n); foreach(i; 0..n) rd(a[i]); auto dp=new int[](n); dp[0]=1; bool up=true; foreach(i; 1..n){ if(up){ if(a[i-1]<a[i]) dp[i]=dp[i-1]+1; else if(a[i-1]==a[i]) dp[i]=dp[i-1]; else dp[i]=2, up=false; }else{ if(a[i-1]>a[i]) dp[i]=dp[i-1]+1; else if(a[i-1]==a[i]) dp[i]=dp[i-1]; else dp[i]=2, up=true; } } writeln(reduce!(max)(dp)); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop; // dfmt on void main() { long N, M; scan(N, M); auto A = new long[](N); auto B = new long[](N); auto C = new long[](M); auto D = new long[](M); foreach (i; 0 .. N) { scan(A[i], B[i]); } foreach (i; 0 .. M) { scan(C[i], D[i]); } alias T = Tuple!(long, long); foreach (i; 0 .. N) { auto m = T(long.max, 0); foreach (j; 0 .. M) { m = m.min(T(abs(A[i] - C[j]) + abs(B[i] - D[j]), j + 1)); } writeln(m[1]); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto t = RD!int; auto ans = new bool[](t); foreach (i; 0..t) { auto n = RD!int; auto a = RDA; auto b = RDA; auto c = new long[](n); foreach (j; 0..n) { c[j] = b[j] - a[j]; } int l, r=1; foreach (j; 0..n) { if (c[j] != 0) { l = j; break; } } foreach_reverse (j; 0..n) { if (c[j] != 0) { r = j+1; break; } } debug writeln(l, ":", r); bool ok = c[l] >= 0; foreach (j; l+1..r) { if (c[j] < 0) ok = false; if (c[j] != c[j-1]) ok = false; } ans[i] = ok; } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush(); debug readln(); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp.count('7'); if (n) { writeln("Yes"); } else { writeln("No"); } }
D
import core.bitop; import std.algorithm; import std.array; import std.ascii; import std.container; import std.conv; import std.format; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int n, k; void main() { int n = readln.chomp.to!int; int k = readln.chomp.to!int; int ans = int.max; foreach (i; 0 .. 1 << n) { int v = 1; foreach (j; 0 .. n) { if ((i >> j) & 1) { v *= 2; } else { v += k; } } ans = min(ans, v); } ans.writeln; }
D
void main() { int[] tmp = readln.split.to!(int[]); int h = tmp[0], w = tmp[1], k = tmp[2]; int[][] field = new int[][](h, w); int cnt; int row; foreach (i; 0 .. h) { string s = readln.chomp; if (s.canFind('#')) { int col; foreach (j, x; s) { if (x == '#') { ++cnt; field[i][col..j+1] = cnt; col = j.to!int + 1; } if (j == w - 1) { if (field[i][col..$].canFind(0)) { field[i][col..$] = cnt; } } } if (row) { foreach (r; 1 .. row+1) { foreach (j; 0 .. w) { field[i-r][j] = field[i][j]; } } row = 0; } } else { ++row; if (i == h - 1) { foreach (r; 0 .. row) { foreach (j; 0 .. w) { field[i-r][j] = field[i-row][j]; } } } } } foreach (x; field) { foreach (j, y; x) { y.write; if (j != w - 1) " ".write; else writeln; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto s = readln.strip; auto t = s[4..$]; auto v = t.to !(long); long n = 1989 - 1; int len = 1; while (len <= t.length) { do { n += 10L ^^ (len - 1); } while (n.text[$ - len..$] != t[$ - len..$]); len++; } writeln (n); } }
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 = this.find(x); long ry = this.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 std.stdio; import std.string; import std.conv; void main() { string s = readln.chomp; ulong k = readln.chomp.to!ulong; if (s[0] != '1') { s[0].writeln; return; } foreach(i, c; s) { if (c != '1') { if (i < k) { s[i].writeln; return; } } } 1.writeln; }
D
module app; import core.bitop; import std.algorithm; import std.array; import std.bigint; import std.container.rbtree; import std.conv; import std.stdio; import std.string; import std.traits; struct Input { long n, a, b; } void parseInput(T)(out Input input, T file) { with (file) with (input) { auto ar = readln().strip().split().map!(to!long).array(); n = ar[0]; a = ar[1]; b = ar[2]; } } auto main2(Input* input) { with (input) { auto q = n / (a + b); auto r = n % (a + b); return q * a + min(a, r); } } alias retType = ReturnType!main2; static if (!is(retType == void)) { unittest { writeln("begin unittest"); } auto _placeholder_ = ReturnType!main2.init; unittest // example1 { string example = `8 3 4`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == 4); } unittest // example2 { string example = `8 0 4`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == 0); } unittest // example3 { string example = `6 2 4`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == 2); } unittest { writeln("end unittest"); } void parseExample(out Input input, string example) { struct Adapter { string[] _lines; this(string input) { _lines = input.splitLines(); } string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; } } parseInput(input, Adapter(example)); } } void printResult(T)(T result) { static if (isFloatingPoint!T) writefln("%f", result); else writeln(result); } void main() { Input input = void; parseInput(input, stdin); static if (is(retType == void)) main2(&input); else { auto result = main2(&input); printResult(result); } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { auto S = sread(); long cnt; char prev = '@'; foreach (i; 0 .. S.length) if (prev != S[i]) { prev = S[i]; cnt++; } writeln(cnt - 1); }
D