code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional, std.traits; import std.algorithm, std.container; import core.stdc.stdlib; void main() { auto N = scanElem; long res; foreach(div; 1L..N) { auto m = (N-div)/div; if((N-div)%div==0 && m>div) { res+=m; } if((N-div)/div<div)break; } writeln(res); } long gcd(long a, long b) { if(b == 0) return a; return gcd(b, a % b); } class UnionFind{ UnionFind parent = null; void merge(UnionFind a) { if(same(a)) return; a.root.parent = this.root; } UnionFind root() { if(parent is null)return this; return parent = parent.root; } bool same(UnionFind a) { return this.root == a.root; } } void scanValues(TList...)(ref TList list) { auto lit = readln.splitter; foreach (ref e; list) { e = lit.fornt.to!(typeof(e)); lit.popFront; } } T[] scanArray(T = long)() { return readln.split.to!(T[]); } void scanStructs(T)(ref T[] t, size_t n) { t.length = n; foreach (ref e; t) { auto line = readln.split; foreach (i, ref v; e.tupleof) { v = line[i].to!(typeof(v)); } } } long scanULong(){ long x; while(true){ const c = getchar; if(c<'0'||c>'9'){ break; } x = x*10+c-'0'; } return x; } T scanElem(T = long)() { char[] res; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { res ~= cast(char) c; c = getchar; } return res.strip.to!T; } template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } struct Factor { long n; long c; } Factor[] factors(long n) { Factor[] res; for (long i = 2; i ^^ 2 <= n; i++) { if (n % i != 0) continue; int c; while (n % i == 0) { n = n / i; c++; } res ~= Factor(i, c); } if (n != 1) res ~= Factor(n, 1); return res; } long[] primes(long n) { if(n<2)return []; auto table = new long[n+1]; long[] res; for(int i = 2;i<=n;i++) { if(table[i]==-1) continue; for(int a = i;a<table.length;a+=i) { table[a] = -1; } res ~= i; } return res; } bool isPrime(long n) { if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (long i = 3; i ^^ 2 <= n; i += 2) if (n % i == 0) return false; return true; }
D
void main() { int[] tmp = readln.split.to!(int[]); int x = tmp[0], t = tmp[1]; max(0, x-t).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 core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "a", long, "b"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { auto x = lread(); bool prime; while(!prime) { if(x.isPrime) prime = true; x++; } writeln(x - 1); } bool isPrime(long x) { auto limit = cast(long) sqrt(cast(real) x).floor(); bool ret = true; foreach (i; iota(2, limit)) { if(!(x % i)) { ret = false; break; } } return ret; } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!T;r.popFront;}} void main() { int n; readV(n); int[] a; readA(n, a); auto mi = a.reduce!min, ma = a.reduce!max; auto h = new int[](ma-mi+3); foreach (ai; a) h[ai-mi+1]++; auto r = 0; foreach (i; 1..ma-mi+2) r = max(r, h[i-1]+h[i]+h[i+1]); writeln(r); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { long a, b; scan(a, b); writeln(a + (b / 2)); }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; import std.string; import std.uni; import std.range; void main() { auto a = readln.strip.repeat(2).join(""); if (a.indexOf(readln.strip) != -1) writeln("Yes"); else writeln("No"); }
D
import std.stdio, std.conv, std.array, std.string; import std.algorithm; import std.container; import std.range; import core.stdc.stdlib; import std.math; void main() { auto S = readln.chomp; auto easy = true; for(int i=0; i<S.length; i++) { if (i%2 == 0 && S[i] == 'L') easy = false; if (i%2 == 1 && S[i] == 'R') easy = false; } writeln(easy ? "Yes" : "No"); }
D
import std.stdio, std.conv, std.string; void main() { string[] inputs = split(readln()); int N = to!int(inputs[0]); int A = to!int(inputs[1]); int B = to!int(inputs[2]); int time_charge = A * N; writeln(time_charge < B ? time_charge : B); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}} void main() { int n, a, b; readV(n, a, b); int r = 0; foreach (i; 1..n+1) { auto s = calc(i); if (s >= a && s <= b) r += i; } writeln(r); } auto calc(int n) { auto r = 0; for (; n > 0; n /= 10) r += n%10; return r; }
D
void solve(){ } void main(){ string s = instr(); int head = s[0..2].to!int(); int tail = s[2..4].to!int(); int stat; if(1<=head && head<=12){ stat = stat|1; } if(1<=tail && tail<=12 ){ stat = stat|2; } switch(stat){ case 1: writeln("MMYY"); break; case 2: writeln("YYMM"); break; case 3: writeln("AMBIGUOUS"); break; default: writeln("NA"); } } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range; const long mod = 10^^9+7; alias instr = () => readln().chomp(); T inelm(T= int)(){ return to!(T)( readln().chomp() ); } T[] inary(T = int)(){ return readln().chomp().split().to!(T[])(); }
D
import std.stdio; import std.array; import std.algorithm; import std.conv; void main() { auto inp = readln().split.map!(to!uint); writeln(inp[0] * inp[1], ' ', 2 * inp.reduce!((a, b) => a + b)); }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; import std.typecons; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto n = readln.strip.to !(int); auto a = readln.strip.map !(q{a - '0'}).array; a[] -= 1; int [int] f; f[0] = 1; int cur = 0; long res = 0; foreach (ref c; a) { cur += c; f[cur] += 1; res += f[cur] - 1; } writeln (res); } }
D
import std.stdio : writeln, stdin; import std.conv : to; import std.range : dropOne; import core.stdc.stdio; int main(string[] argv) { int cnt = 1; char last = 'Z'; int n; scanf("%d",&n); foreach(words; stdin.byLine) { foreach(word;words){ if(word != last){ if (last != 'Z') { if (last == 'a' || last == 'e' || last == 'i' || last == 'o' || last == 'u' || last == 'y'){ if ((last != 'e' && last != 'o') || cnt != 2) cnt = 1; } for(int i = 0; i < cnt; ++i) printf("%c",last); } cnt = 0; } last = word; cnt++; } if (last != 'Z') { if (last == 'a' || last == 'e' || last == 'i' || last == 'o' || last == 'u' || last == 'y'){ if ((last != 'e' && last != 'o') || cnt != 2) cnt = 1; } for(int i = 0; i < cnt; ++i) printf("%c",last); } } return 0; }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.container; import std.array; import std.math; import std.range; import std.typecons; import std.ascii; import std.format; void main() { auto input = readln.chomp.split.map!(to!int); auto n = input[0]; auto m = input[1]; int[] as; int[] bs; foreach (i; 0..m) { auto ab = readln.chomp.split.map!(to!int); as ~= ab.front; bs ~= ab.back; } int cnt = 0; foreach (i; 0..m) { // skip i bool[] done = new bool[n + 1]; int[] queue = [1]; while (queue.length) { int node = queue.front; queue.popFront; if (done[node]) { continue; } done[node] |= true; foreach (j; 0..m) { if (i == j) { continue; } if (as[j] == node) { queue ~= bs[j]; } if (bs[j] == node) { queue ~= as[j]; } } } bool flag = true; for (int j = 1; j <= n; ++j) { flag &= done[j]; } cnt += !flag; } cnt.writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nm = readln.split.to!(int[]); writeln(nm[0] == nm[1] ? "Yes" : "No"); }
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() {//try{ auto tokens = split(my_readln()); auto N = to!ulong(tokens[0]); auto W = to!ulong(tokens[1]); long[] w, v; foreach (i; 0..N) { auto tokens2 = split(my_readln()); { w ~= to!long(tokens2[0]); v ~= to!long(tokens2[1]); } } long[][] dp; dp.length = 100100; foreach (ref e; dp) e.length = 110; foreach (i; 0..N) { foreach (j; 0..W+1) { if (j >= w[i]) dp[j][i+1] = max(dp[j-w[i]][i] + v[i], dp[j][i]); else dp[j][i+1] = dp[j][i]; } } writeln(dp[W][N]); stdout.flush(); /*}catch (Throwable e) { writeln(e.toString()); } readln();*/ }
D
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array, std.functional, std.container, std.typecons; void main() { auto N = readln.split[0].to!int; auto p = readln.split.to!(int[]); auto ctr = 0; foreach(i, pi; p) { if(pi != i+1) ctr++; } if(ctr == 0 || ctr == 2) { writeln("YES"); } else { writeln("NO"); } }
D
import std.stdio; import std.string; import std.conv; void main() { auto input = split( readln() ); long N = to!long(input[0]), K = to!long(input[1]); long cnt = 0; long b = K+1; while ( b <= N ) { cnt += (b-K) * (N/b); long s = N%b - K + 1; if (s > 0) { cnt += s; } if (K == 0) { cnt -= 1; } b += 1; } writeln(cnt); }
D
import std.stdio; import std.string; import std.algorithm; import std.conv; void main() { auto s1 = split(readln); //auto a = map(to!int)(s1); auto s2 = split(readln); long count; long sum; foreach (val; s2) { if (to!long(val) <= to!long(s1[1])) sum += to!long(val); if (sum > to!long(s1[2])) { sum = 0; count++; } } writeln(count); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; immutable long INF = 1L << 59; void main() { auto T = readln.chomp.to!int; while (T--) { auto s = readln.split.map!(to!long); auto N = s[0]; auto X = s[1]; auto Y = s[2]; auto D = s[3]; long tmp1 = INF; long tmp2 = INF; long tmp3 = INF; if (abs(X - Y) % D == 0) { tmp1 = abs(X - Y) / D; } if (abs(Y - 1) % D == 0) { tmp2 = abs(X - 1) / D + (abs(X - 1) % D > 0) + abs(Y - 1) / D; } if (abs(Y - N) % D == 0) { tmp3 = abs(X - N) / D + (abs(X - N) % D > 0) + abs(Y - N) / D; } long ans = min(tmp1, tmp2, tmp3); writeln(ans == INF ? -1 : ans); } }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.string; void main() { int r, g; r = readln.chomp.to!int; g = readln.chomp.to!int; writeln(g - r + g); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container; void main() { auto S = readln.chomp; auto T = readln.chomp; int r; foreach (i, s; S) { if (s != T[i]) ++r; } writeln(r); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto X = RD; auto A = RD; auto B = RD; writeln((X - A) % B); stdout.flush(); debug readln(); }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; long n, m; rd(n, m); import std.math : sqrt; if (n == 1) { writeln(m); return; } for (long k = 6 * 10L ^^ 8; k >= 1; k--) { if (m >= k * n && (m - k * n) % k == 0) { writeln(k); return; } } import std.exception : enforce; enforce(false); } 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 chie template :) {{{ import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons; // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } // }}} void main() { auto cin = new Scanner; char[] s, t; cin.scan(s, t); foreach (i; 0 .. s.length) { s = s[$ - 1] ~ s[0 .. $ - 1]; if (s == t) { writeln("Yes"); return; } } writeln("No"); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; import std.bitmanip; // BitArray void main() { auto s = readln.chomp, n = s.length; auto ans = 0L; foreach (i; 0..1<<(n-1)) { auto j = [size_t(0)] ~ i.bitsSet.map!"a+1".array ~ [n]; auto r = 0L; foreach (k; 0..j.length-1) r += s[j[k]..j[k+1]].to!long; ans += r; } writeln(ans); }
D
import std.stdio; void main() { foreach (i; 1 .. 10) foreach (j; 1 .. 10) writeln(i, "x", j, "=", i*j); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto A = RD; auto B = RD; writeln(A * B - A - B + 1); stdout.flush(); debug readln(); }
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; void main() { int n = readln.chomp.to!int; int[] a = readln.chomp.split.map!(to!int).array; int one = 0; bool notwo = true; int four = 0; foreach (v; a) { if (v % 4 == 0) { ++four; continue; } if (v % 2 == 0) { notwo &= false; continue; } ++one; } writeln = one <= four + notwo ? "Yes" : "No"; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto m = RD!int; auto x = RD!int; auto y = RD!int; auto s = new string[](n); foreach (i; 0..n) s[i] = RD!string; int cnt; foreach (i; 0..n) { foreach (j; 0..m) { if (s[i][j] == '.') ++cnt; } } if (x*2 <= y) { ans[ti] = cnt*x; } else { foreach (i; 0..n) { int j = 1; while (j < m) { if (s[i][j] == '.' && s[i][j-1] == '.') { ans[ti] += y; cnt -= 2; j += 2; } else ++j; } } ans[ti] += x * cnt; } } foreach (e; ans) writeln(e); 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[]); void main() { long a, b; char op; scan(a, op, b); // writeln(a); // writeln(op); // writeln(b); if (op == '+') { writeln(a + b); } else { writeln(a - b); } } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
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 s = readln.chomp; int res, c; foreach (e; s) { if (e == 'I') { c++; } else { c--; } res = max(c, res); } res.writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.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; long ans; foreach (i; 1..N+1) { if (i * i > N) break; ans = i * i; } writeln(ans); stdout.flush(); debug readln(); }
D
void main() { problem(); } void problem() { const N = scan!ulong; ulong solve() { ulong ans; foreach(i; 1..N) { ans += (N - 1) / i; } return ans; } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new int[](t); foreach (ti; 0..t) { auto n = RD!int; auto k = RD!int; auto s = RD!string; auto cnt = new int[](n+1); int streak, lc; bool start; foreach (i; 0..n) { if (s[i] == 'W') { start = true; ++cnt[streak]; streak = 0; if (i == 0) ++ans[ti]; else if (s[i-1] == 'W') ans[ti] += 2; else ++ans[ti]; } else { if (start) ++streak; ++lc; } } debug writeln("ans:", ans[ti]); debug writeln(cnt); k = min(k, lc); foreach (i; 1..n+1) { auto c = min(k / i, cnt[i]); k -= i * c; if (c != 0) ans[ti] += c * (i * 2 + 1); debug writeln("k:", k, " ans:", ans[ti]); } if (start) ans[ti] += k * 2; else if (k >= 1) ans[ti] += (k-1) * 2 + 1; } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
// Cheese-Cracker: cheese-cracker.github.io void solve(){ long l = scan; long r = scan; long k = scan; long len = r - l + 1; if(len == 1){ writeln( (l == 1) ? "NO" : "YES"); return; } long tim = len / 2; if(r % 2 == 1 && l % 2 == 1){ tim += 1; } writeln( (k >= tim) ? "YES" : "NO"); } void main(){ long tests = scan; // Toggle! while(tests--) solve; } /************** ***** That's All Folks! ***** **************/ import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math, std.numeric; string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;} T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; } void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } } alias ll = long, tup = Tuple!(long, "x", long, "y");
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.exception; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; import core.bitop; void main () { string s; while ((s = readln.strip) != "") { char c = s[$ - 1]; s = s[0..$ - 1]; long n = s.to !(long); n -= 1; long full = n / 4; long res = full * 16; n -= full * 4; n %= 2; if (n == 1) { res += 7; } res += 6 - "fedabc".find (c).length; res += 1; writeln (res); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } //long mod = 10^^9 + 7; long mod = 998244353; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD!int; auto cnt = new long[](30); foreach (i; 0..N) { auto s = RD!string; ++cnt[s[0]-'a']; } long ans; foreach (e; cnt) { auto x = (e+1) / 2; auto y = e / 2; ans += x * (x-1) / 2; ans += y * (y-1) / 2; } writeln(ans); stdout.flush(); debug readln(); }
D
module main; import core.stdc.stdio; import std.algorithm; int gcd(int a, int b){ while (a > 0 && b > 0){ if (a > b){ a = a % b; } else { b = b % a; } } return a + b; } int main(string[] argv) { int n; scanf("%d", &n); int [] arr = new int[n]; for(int i = 0; i<n; i++) scanf("%d", &arr[i]); arr.sort!(); int cur = arr[1] - arr[0]; for (int i = 1; i < n; i++){ cur = gcd(cur, arr[i] - arr[i - 1]); } int ans = 0; for (int i = 1; i < n; i++){ int len = arr[i] - arr[i - 1]; ans += len / cur - 1; } printf("%d", ans); return 0; }
D
import std.stdio, std.range, std.conv, std.algorithm, std.array, std.string, std.ascii, std.math; void main() { readln; auto a = readln.strip, b = readln.strip; uint sum; foreach (i, e; a) { auto tmpA = e.to!int, tmpB = b[i].to!int; auto minA = min(tmpA, tmpB), maxB = max(tmpA, tmpB); auto cl = maxB - minA, tl = 10 - maxB + minA; if (cl < tl) { sum += cl; } else { sum += tl; } } writeln(sum); }
D
module sigod.codeforces.p290D; import std.conv; import std.stdio; import std.string; void main() { string str = stdin.readln().strip(); int value = stdin.readln().strip().to!int(); stdout.write(solve(str, value)); } string solve(string str, int value) { string result; foreach (ref ch; str.toLower()) { if (ch < value + 97) { result ~= (ch ~ "").toUpper(); } else { result ~= ch; } } return result.idup; } unittest { assert(solve("AprilFool", 14) == "AprILFooL"); }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { string s; while ((s = readln.strip) != "") { auto n = s.length.to !(int); s ~= s; int res = 1; int cur = 1; foreach (i; 1..n * 2) { if (s[i - 1] != s[i]) { cur += 1; res = max (res, cur); } else { cur = 1; } } res = min (res, n); writeln (res); } }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { long H, W; scan(H, W); auto C = new string[](H); foreach (i; 0 .. H) scan(C[i]); foreach (i; 0 .. H * 2) { writeln(C[(i / 2)]); } }
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.stdlib; void main() { auto N = readln.chomp.to!int; long ans = 0; foreach (i; 1..N+1) if (i % 3 != 0 && i % 5 != 0) ans += i; ans.writeln; }
D
import std.stdio, std.string, std.conv; void main(){ for(uint i;;++i){ auto a = readln.chomp.to!int; if(!a) break; writeln("Case ",i+1,": ",a); } }
D
//dlang template---{{{ import std.stdio; import std.conv; import std.string; import std.array; import std.algorithm; // MIT-License https://github.com/kurokoji/nephele class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } //Digit count---{{{ int DigitNum(int num) { int digit = 0; while (num != 0) { num /= 10; digit++; } return digit; } //}}} //}}} void main() { Scanner sc = new Scanner; int A, B, C, X; sc.scan(A, B, C, X); int res = 0; foreach (i; 0 .. A + 1) { foreach (j; 0 .. B + 1) { foreach (k; 0 .. C + 1) { if ((i * 500) + (j * 100) + (k * 50) == X) res++; } } } writeln(res); }
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 n = readint; writeln(n < 1000 ? "ABC" : "ABD"); }
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 X = RD!char; auto Y = RD!char; writeln(X-'A' == Y-'A' ? "=" : X-'A' < Y-'A' ? "<" : ">"); stdout.flush(); debug readln(); }
D
// import chie template :) {{{ static if (__VERSION__ < 2090) { import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons, std.format, std.bitmanip, std.numeric; } else { import std; } // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan(T...)(ref T args) { foreach (ref arg; args) { arg = next!(typeof(arg)); } } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } // }}} // ModInt {{{ struct ModInt(ulong modulus) { import std.traits : isIntegral, isBoolean; import std.exception : enforce; private { ulong val; } this(const ulong n) { val = n % modulus; } this(const ModInt n) { val = n.value; } @property { ref inout(ulong) value() inout { return val; } } T opCast(T)() { static if (isIntegral!T) { return cast(T)(val); } else if (isBoolean!T) { return val != 0; } else { enforce(false, "cannot cast from " ~ this.stringof ~ " to " ~ T.stringof ~ "."); } } ModInt opAssign(const ulong n) { val = n % modulus; return this; } ModInt opOpAssign(string op)(ModInt rhs) { static if (op == "+") { val += rhs.value; if (val >= modulus) { val -= modulus; } } else if (op == "-") { if (val < rhs.value) { val += modulus; } val -= rhs.value; } else if (op == "*") { val = val * rhs.value % modulus; } else if (op == "/") { this *= rhs.inv; } else if (op == "^^") { ModInt res = 1; ModInt t = this; while (rhs.value > 0) { if (rhs.value % 2 != 0) { res *= t; } t *= t; rhs /= 2; } this = res; } else { enforce(false, op ~ "= is not implemented."); } return this; } ModInt opOpAssign(string op)(ulong rhs) { static if (op == "+") { val += ModInt(rhs).value; if (val >= modulus) { val -= modulus; } } else if (op == "-") { auto r = ModInt(rhs); if (val < r.value) { val += modulus; } val -= r.value; } else if (op == "*") { val = val * ModInt(rhs).value % modulus; } else if (op == "/") { this *= ModInt(rhs).inv; } else if (op == "^^") { ModInt res = 1; ModInt t = this; while (rhs > 0) { if (rhs % 2 != 0) { res *= t; } t *= t; rhs /= 2; } this = res; } else { enforce(false, op ~ "= is not implemented."); } return this; } ModInt opUnary(string op)() { static if (op == "++") { this += 1; } else if (op == "--") { this -= 1; } else { enforce(false, op ~ " is not implemented."); } return this; } ModInt opBinary(string op)(const ulong rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinary(string op)(ref const ModInt rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinary(string op)(const ModInt rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinaryRight(string op)(const ulong lhs) const { mixin("return ModInt(this) " ~ op ~ "= lhs;"); } long opCmp(ref const ModInt rhs) const { return cast(long)value - cast(long)rhs.value; } bool opEquals(const ulong rhs) const { return value == ModInt(rhs).value; } ModInt inv() const { ModInt ret = this; ret ^^= modulus - 2; return ret; } string toString() const { import std.format : format; return format("%s", val); } } // }}} // alias {{{ alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less); alias MinHeap(T) = Heap!(T, "a > b"); // }}} // memo {{{ /* - ある値が見つかるかどうか <https://dlang.org/phobos/std_algorithm_searching.html#canFind> canFind(r, value); -> bool - 条件に一致するやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#filter> // 2で割り切れるやつ filter!"a % 2 == 0"(r); -> Range - 合計 <https://dlang.org/phobos/std_algorithm_iteration.html#sum> sum(r); - 累積和 <https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold> // 今の要素に前の要素を足すタイプの一般的な累積和 // 累積和のrangeが帰ってくる(破壊的変更は行われない) cumulativeFold!"a + b"(r, 0); -> Range - rangeをarrayにしたいとき array(r); -> Array - 各要素に同じ処理をする <https://dlang.org/phobos/std_algorithm_iteration.html#map> // 各要素を2乗 map!"a * a"(r) -> Range - ユニークなやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#uniq> uniq(r) -> Range - 順列を列挙する <https://dlang.org/phobos/std_algorithm_iteration.html#permutations> permutation(r) -> Range - ある値で埋める <https://dlang.org/phobos/std_algorithm_mutation.html#fill> fill(r, val); -> void - バイナリヒープ <https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap> // 昇順にするならこう(デフォは降順) BinaryHeap!(Array!T, "a > b") heap; heap.insert(val); heap.front; heap.removeFront(); - 浮動小数点の少数部の桁数設定 // 12桁 writefln("%.12f", val); - 浮動小数点の誤差を考慮した比較 <https://dlang.org/phobos/std_math.html#.approxEqual> approxEqual(1.0, 1.0099); -> true - 小数点切り上げ <https://dlang.org/phobos/std_math.html#.ceil> ceil(123.4); -> 124 - 小数点切り捨て <https://dlang.org/phobos/std_math.html#.floor> floor(123.4) -> 123 - 小数点四捨五入 <https://dlang.org/phobos/std_math.html#.round> round(4.5) -> 5 round(5.4) -> 5 */ // }}} void main() { auto cin = new Scanner; int n; cin.scan(n); int[] x = cin.nextArray!int(n); int mini = int.max; foreach (p; 1 .. 101) { int sum; foreach (e; x) { sum += (p - e) ^^ 2; } mini = min(mini, sum); } writeln(mini); }
D
// problem: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_3_A // require: graph/articulation_points.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; //LRDU bool solve2(int a, int b, int x1, int x2) { if (x1 == 0 && x2 == 0) { return a == 0 && b == 0; } else if (a == b) { return true; } else if (a > b) { return abs(x1) >= a - b; } else { return abs(x2) >= b - a; } } void solve(int T) { while (T--) { auto s = readln.split.map!(to!int); auto A = s[0]; auto B = s[1]; auto C = s[2]; auto D = s[3]; s = readln.split.map!(to!int); auto X = s[0]; auto Y = s[1]; auto x1 = s[2] - X; auto y1 = s[3] - Y; auto x2 = s[4] - X; auto y2 = s[5] - Y; auto AB = A - B; auto ans = solve2(A, B, x1, x2) && solve2(C, D, y1, y2); writeln(ans ? "Yes" : "No"); } } void main() { auto T = readln.chomp.to!int; solve(T); }
D
pragma(inline, true) void chmin(T)(ref T a, T b) { if (a > b) a = b; } int[] init() { enum INF = 1 << 28; int[] dp = new int[100010]; foreach (i; 1..100001) { dp[i] = INF; int p6 = 1, p9 = 1; while (p6 <= i || p9 <= i) { if (i - p6 >= 0) chmin(dp[i], dp[i - p6] + 1); if (i - p9 >= 0) chmin(dp[i], dp[i - p9] + 1); p6 *= 6; p9 *= 9; } } return dp; } void main() { import std.conv : to; import std.stdio : readln, writeln; import std.string : chomp; int[] dp = init(); writeln(dp[readln.chomp.to!int]); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "damage", long, "cost"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { auto h = lread(); h.kill.writeln(); } long kill(long h) { if (h == 1) return 1; auto tmp = kill(h / 2); return 1 + 2 * tmp; } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
//dlang template---{{{ import std.stdio; import std.conv; import std.string; import std.array; import std.algorithm; import std.typecons; import std.math; import std.range; // MIT-License https://github.com/kurokoji/nephele class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } //Digit count---{{{ int DigitNum(int num) { int digit = 0; while (num != 0) { num /= 10; digit++; } return digit; } //}}} //}}} void main() { Scanner sc = new Scanner; int A, B, C; sc.scan(A, B, C); if (B / A < C) { writeln(B / A); } else { writeln(C); } }
D
import std.stdio, std.typecons, std.algorithm; alias Tuple!(int, "top", int, "bottom") Bundle; const int INF = (1 << 24); void main() { uint times; scanf("%d", &times); uint[][] dp = new uint[][](times, times); Bundle[] cards; foreach(t; 0..times) { uint top, bottom; scanf("%d %d", &top, &bottom); cards ~= Bundle(top, bottom); } foreach(ref d; dp) foreach(ref x; d) x = INF; for(int i = 0; i < times - 1; i++) dp[i][i] = 0; dp[times - 1][times - 1] = 0; // dp[i][j] -> i 番目 から j 番目を計算する時の最適コスト for(uint len = 1; len < times; len++) { for(uint s = 0; s + len < times; s++) { int f = s + len; for(uint i = s; i < f; i++) { uint cost = (cards[s].top * cards[i].bottom) * (cards[i + 1].top * cards[f].bottom); cost += dp[s][i] + dp[i + 1][f]; if (cost < dp[s][f]) dp[s][f] = cost; } } } writeln(dp[0][times - 1]); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); void main() { string s; scan(s); // writeln(s[0]); long x; long y; foreach (i; 0 .. s.length) { if (s[i] == '0') { x += 1; } else if (s[i] == '1') { y += 1; } } writeln(2 * (min(x, y))); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.stdio; import std.algorithm; import std.string; import std.functional; import std.array; import std.conv; import std.math; import std.typecons; import std.regex; import std.range; void main(){ int n = readln().chomp().to!int; for(int i=0;i<n;i++){ int point,o,r; while(true){ string s = readln().chomp(); if(s == "HIT"){ r = 2 * r + 1; if(r & (1<<3)){ r ^= 1 << 3; point++; } }else if(s == "OUT"){ o++; if(o == 3) break; }else if(s == "HOMERUN"){ point += 1 + (r & 1) + (r & 2)/2 + (r & 4)/4; r = 0; } } writeln(point); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto S = readln.chomp; writeln(S[1] == 'B' ? "ARC" : "ABC"); }
D
void main() { ("aiueo".canFind(rs) ? "vowel" : "consonant").writeln; } // =================================== import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } // 26文字より少ない → まだ使っていない最小の文字を追加する // zyx.....cba → -1 // その他→後ろから見ていって最初に下がった場所をカエる(例 ~~~~~dzfc → ~~~~~fcqz) // void main(){ char[] s = readln.chomp.to!(char[]); if(s.length < 26){ bool[char] xset; foreach(c; s) xset[c] = 1; foreach(char c; 'a' .. 'z' + 1){ if(c !in xset){ writeln(s ~ c); return; } } } for(int i = 24; i >= 0; i --){ if(s[i] < s[i + 1]){ int j1 = i + 1; for(int j = i + 1; j <= 25; j ++){ if(s[i] < s[j]) if(s[j] < s[j1]) j1 = j; } char[] s2 = s[i .. j1] ~ s[j1 + 1 .. $]; writeln(s[0 .. i] ~ s[j1]); return; } } writeln("-1"); return; }
D
import std.stdio,std.math,std.string,std.conv,std.typecons,std.format; import std.algorithm,std.range,std.array,std.numeric; T[] readarr(T=long)(){return readln.chomp.split.to!(T[]);} void scan(T...)(ref T args){auto input=readln.chomp.split;foreach(i,t;T)args[i]=input[i].to!t;} struct Queue(T){T[]e;auto enq(T t){e~=t;}auto enq(T[]ts){e~=ts;}T deq(){T tp=e[0];e=e.length>1?e[1..$]:[];return tp;}} struct Stack(T){T[]e;auto push(T t){e~=t;}auto push(T[]ts){e~=ts;}T pop(){T tp=e[$-1];e=e.length>1?e[0..$-1]:[];return tp;}} //END OF TEMPLATE void main(){ ((readarr[0]+1)/2).writeln; }
D
void main() { long[] tmp = readln.split.to!(long[]); long n = tmp[0], m = tmp[1]; writeln((100 * (n - m) + 1900 * m) * 2 ^^ m); } 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
void main(){ int s, w; scanf("%d %d", &s, &w); if( s<=w )writeln("unsafe"); else writeln("safe"); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { const tmp = readln.split.to!(int[]); writeln(min(1, tmp[0] % tmp[1])); }
D
import std.stdio, std.conv, std.array, std.string; import std.algorithm; import std.container; import std.range; import core.stdc.stdlib; import std.math; void main() { auto N = readln.chomp; auto days = ["SUN","MON","TUE","WED","THU","FRI","SAT"]; foreach(i; 0..7) { if (N == days[i]) { writeln(7-i); } } }
D
import std.algorithm; import std.stdio; import std.string; void main() { auto n = readln; auto s = readln.strip; auto lts = new int[ s.length ]; auto rts = new int[ s.length ]; auto result = int.max; lts[ 0 ] = 0; rts[ $-1 ] = 0; for( int index = 1; index < s.length; index++ ) { lts[ index ] = lts[ index - 1 ] + ( s[ index - 1 ] == 'E' ? 0 : 1 ); } for( int index = cast( int ) s.length-2; 0 <= index; index-- ) { rts[ index ] = rts[ index + 1 ] + ( s[ index + 1 ] == 'W' ? 0 : 1 ); } for( int index = 0; index < s.length; index++ ) { result = min( result, lts[ index ] + rts[ index ] ); } writeln( result ); }
D
void main(){ int[] ab = _scanln(); if( ab[0]>ab[1] )writeln(ab[0]+ab[0]-1); else if( ab[0]<ab[1] )writeln(ab[1]+ab[1]-1); else writeln(ab[0]*2); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998244353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto n = RD!int; auto m = RD!int; ans[ti] = n % m == 0; } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random; import std.typecons, std.functional, std.traits,std.concurrency; import std.algorithm, std.container; import core.bitop, core.time, core.memory; import std.bitmanip; import std.regex; enum INF = long.max/3; enum MOD = 10L^^9+7; //辞書順順列はiota(1,N),nextPermituionを使う void end(T)(T v) if(isIntegral!T||isSomeString!T||isSomeChar!T) { import core.stdc.stdlib; writeln(v); exit(0); } T[] scanArray(T = long)() { static char[] scanBuf; readln(scanBuf); return scanBuf.split.to!(T[]); } dchar scanChar() { int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } return cast(dchar)c; } T scanElem(T = long)() { import core.stdc.stdlib; static auto scanBuf = appender!(char[])([]); scanBuf.clear; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { scanBuf ~= cast(char) c; c = getchar; } return scanBuf.data.to!T; } dchar[] scanString(){ return scanElem!(dchar[]); } void main() { auto a=scanString; auto b=scanString; auto c=scanString; if(a[$-1]==b[0]&&b[$-1]==c[0])end("YES");end("NO"); }
D
import std.stdio; import std.conv; import std.string; void main() { auto N = readln.chomp.to!long; writeln(N / 3); }
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 k = readln.chomp.to!int; auto x = 1; foreach (i; 0..n) { if (x < k) { x *= 2; } else { x += k; } } x.writeln; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}} void main() { int n; readV(n); writeln(n%calc(n) == 0 ? "Yes" : "No"); } auto calc(int n) { int r = 0; for (; n > 0; n /= 10) r += n%10; return r; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.typecons; import std.numeric, std.math; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto S = RD!string; long ans; char last; foreach (c; S) { if (c != last) ++ans; last = c; } writeln(ans-1); stdout.flush(); //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; import std.ascii; void main() { auto c1 = readln.chomp; auto c2 = readln.chomp; if (c1[0] == c2[2] && c1[1] == c2[1] && c1[2] == c2[0]) { writeln("YES"); } else { writeln("NO"); } }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!T;r.popFront;}} void readM(T)(size_t r,size_t c,ref T[][]t){t=new T[][](r);foreach(ref v;t)readA(c,v);} void main() { int n; readV(n); int[][] a; readM(n, n, a); auto b = new bool[][](n, n); foreach (i; 0..n) b[i][i] = true; auto r = 0L; foreach (i; 0..n-1) foreach (j; i+1..n) r += a[i][j]; foreach (i; 0..n) foreach (j; 0..n) foreach (k; 0..n) { if (a[i][j] > a[i][k]+a[k][j]) { writeln(-1); return; } else if (a[i][j] == a[i][k]+a[k][j] && i != k && j != k && !b[i][j]) { r -= a[i][j]; b[i][j] = b[j][i] = true; } } writeln(r); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto abx = readln.split.to!(int[]); auto c = abx[2] - abx[0]; writeln(c >= 0 && c <= abx[1] ? "YES" : "NO"); }
D
import std.stdio, std.string, std.conv; void main() { string t = readln.chomp; string p = readln.chomp; int i = 0; while (true) { i = indexOf(t, p, i).to!int; if (i == - 1) break; i.writeln; ++i; } }
D
import std.stdio, std.string, std.conv; void main() { int[] tmp = readln.split.to!(int[]); int a = tmp[0], b = tmp[1]; writeln(0 < a && a < 10 && 0 < b && b < 10 ? a * b : -1); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto cs = new int[](101); foreach (n; 2..N+1) { foreach (d; 2..N+1) { while (n%d == 0) { cs[d] += 1; n /= d; } } } int a, b, c, d, e; foreach (x; cs) { if (x >= 74) ++a; if (x >= 24) ++b; if (x >= 14) ++c; if (x >= 4) ++d; if (x >= 2) ++e; } writeln(a + b*(e-1) + c*(d-1) + d*(d-1)/2*(e-2)); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { int M; scan(M); auto d = new long[](M); auto c = new long[](M); long cs; long ds; foreach (i ; 0 .. M) { scan(d[i], c[i]); cs += c[i]; ds += d[i] * c[i]; } auto ans = cs - 1 + (max(0, ds - 9) + 8) / 9; writeln(ans); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { long d,n; scan(d,n); if(n % 100) { writeln(n * pow(100, d)); } else { writeln((n + 1) * pow(100, d)); } }
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); if (n == 1) { writeln("Hello World"); } else { int a; readV(a); int b; readV(b); writeln(a+b); } }
D
import std.stdio; import std.algorithm; import std.conv; import std.typecons; void main() { int n; scanf("%d", &n); auto ipt = new Tuple!(int, int)[n]; foreach(i;0..n) { int a, b; scanf("%d %d", &a, &b); ipt[i] = tuple(a, b); } ipt.sort!((a, b) => a[1] < b[1]); int t; foreach(x; ipt){ t += x[0]; if (t > x[1]) { write("No"); return; } } write("Yes"); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!T;r.popFront;}} void main() { int n, z, w; readV(n, z, w); int[] a; readA(n, a); if (n == 1) writeln((a[$-1]-w).abs); else writeln(max((a[$-1]-w).abs, (a[$-2]-a[$-1]).abs)); }
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; bool check(string S, string T) { while (!S.empty && !T.empty) { if (S.length > T.length) { swap(S, T); } if (T.startsWith(S)) { T = T[S.length..$]; } else { return false; } } return true; } void main() { auto N = readln.chomp.to!int; auto S = readln.chomp; auto T = readln.chomp; bool loop = check(S, T); for (int i = 0, j = 0; !loop; i = (i + 1) % S.length.to!int, j = (j + 1) % T.length.to!int) { if (S[i] < T[j]) { break; } else if (S[i] > T[j]) { swap(S, T); break; } } int X = (N - 1) / S.length.to!int + 1; X = X * S.length.to!int; while (X > N || (N - X) % T.length != 0) { X -= S.length.to!int; } string ans = ""; while (ans.length < X) ans ~= S; while (ans.length < N) ans ~= T; ans.writeln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio; void main() { auto X = readln.chomp.to!int; auto dp = new int[](100); dp[] = 1 << 29; dp[0] = 0; foreach (i; 0..100) { foreach (j; 1..6) { if (i + j < 100) { dp[i + j] = min(dp[i + j], dp[i] + 1); } } } if (dp[X % 100] <= X / 100) { writeln(1); } else { writeln(0); } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.ascii; void main() { auto h = readln.chomp.to!int; auto w = readln.chomp.to!int; auto n = readln.chomp.to!int; auto p = max(h, w); writeln(n / p + (n % p > 0)); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int h, w; rd(h, w); auto c=new char[][](h, w); foreach(i; 0..h) c[i]=readln.chomp.to!(char[]); auto dd=[[1, 0], [0, 1], [-1, 0], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1]]; foreach(i; 0..h)foreach(j; 0..w)if(c[i][j]!='#'){ int cnt=0; foreach(d; dd){ auto ni=i+d[0], nj=j+d[1]; if(ni<0 || nj<0 || ni>=h || nj>=w) continue; if(c[ni][nj]=='#') cnt++; } c[i][j]=to!char(cnt+'0'); } foreach(i; 0..h) writeln(c[i]); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.stdio; import std.conv; import std.algorithm; import std.string; import std.file; import std.math; int main() { string l; l = readln().split[0]; int c = to!int(l); // iteration int d = 100000; // debt for(int i=0; i<c; i++){ d *= 1.05; if(d > d / 1000 * 1000) d += 1000; d = d / 1000 * 1000; } printf("%d\n", d); return 0; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.math; long n, m; void main() { scan(n, m); auto cmb = new long[][](n + 1, n + 1); auto dp = new long[][](n + 1, n + 1); foreach (i ; 0 .. n + 1) { cmb[i][0] = cmb[i][i] = 1; dp[i][0] = dp[i][i] = 1; } foreach (i ; 0 .. n + 1) { foreach (j ; 1 .. i) { cmb[i][j] = (cmb[i-1][j] + cmb[i-1][j-1]); if (cmb[i][j] >= m) cmb[i][j] -= m; dp[i][j] = (dp[i-1][j-1] + (j + 1) * dp[i-1][j] % m); if (dp[i][j] >= m) dp[i][j] -= m; } } debug { //writeln(cmb); //writeln(dp); } auto tpm = new long[](n*n + 1); auto tpm1 = new long[](n*n + 1); tpm[0] = tpm1[0] = 1; foreach (i ; 1 .. n*n + 1) { tpm[i] = tpm[i-1] * 2 % m; tpm1[i] = tpm1[i-1] * 2 % (m-1); } long f(int k) { long res; auto t = powmod(2, tpm1[n - k], m); foreach (x ; 0 .. k + 1) { //res += powmod(2, powmod(2, n - k, m - 1), m) * dp[k][x] % m * powmod(2, (n - k) * x, m) % m; res += t * dp[k][x] % m * tpm[(n-k)*x] % m; if (res >= m) res -= m; } return res; } long ans; foreach (int k ; 0 .. n.to!int + 1) { if (k & 1) { ans -= cmb[n][k] * f(k) % m; if (ans < 0) ans += m; } else { ans += cmb[n][k] * f(k) % m; if (ans >= m) ans -= m; } } writeln(ans); } // x^y % mod long powmod(long x, long y, long mod) { return y > 0 ? powmod(x, y>>1, mod)^^2 % mod * x^^(y&1) % mod : 1L; } 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, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "a", long, "b"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { string s, t; scan(s, t); writeln(t ~ s); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
void main() { long n = rdElem; long p = 1; foreach (i; 1 .. n+1) { p = p * i % mod; } p.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 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.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, std.bitmanip, std.datetime; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!long).array; long ans = N; int left = 0; long xxx = 0; foreach (i; 0..N) { while (left < i && (xxx + A[i] != (xxx ^ A[i]))) { xxx ^= A[left]; left += 1; } xxx ^= A[i]; ans += i - left; } ans.writeln; }
D
void main(){ int n = _scan(); int[] ans_arr = [1, 2, 4, 8, 16, 32, 64]; int ans; foreach(elm; ans_arr){ if(elm>n)break; ans = elm; } ans.writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
/+ dub.sdl: name "C" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner, dcomp.algorithm; // import dcomp.numeric.primitive; int main() { auto sc = new Scanner(stdin); int n; sc.read(n); long f = 1; foreach (i; 0..n) { long x; sc.read(x); f = lcm(f, x); } writeln(f); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } version (X86) static if (__VERSION__ < 2071) { import core.bitop : bsf, bsr, popcnt; int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; if (f.eof) return false; line = lineBuf[]; f.readln(line); } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/algorithm.d */ // module dcomp.algorithm; import std.range.primitives; import std.traits : isFloatingPoint, isIntegral; T binSearch(alias pred, T)(T l, T r) if (isIntegral!T) { while (r-l > 1) { T md = (l+r)/2; if (!pred(md)) l = md; else r = md; } return r; } T binSearch(alias pred, T)(T l, T r, int cnt = 60) if (isFloatingPoint!T) { foreach (i; 0..cnt) { T md = (l+r)/2; if (!pred(md)) l = md; else r = md; } return r; } E minimum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed) if (isInputRange!Range && !isInfinite!Range) { import std.algorithm, std.functional; return reduce!((a, b) => binaryFun!pred(a, b) ? a : b)(seed, range); } ElementType!Range minimum(alias pred = "a < b", Range)(Range range) { assert(!range.empty, "range must not empty"); auto e = range.front; range.popFront; return minimum!pred(range, e); } E maximum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed) if (isInputRange!Range && !isInfinite!Range) { import std.algorithm, std.functional; return reduce!((a, b) => binaryFun!pred(a, b) ? b : a)(seed, range); } ElementType!Range maximum(alias pred = "a < b", Range)(Range range) { assert(!range.empty, "range must not empty"); auto e = range.front; range.popFront; return maximum!pred(range, e); } Rotator!Range rotator(Range)(Range r) { return Rotator!Range(r); } struct Rotator(Range) if (isForwardRange!Range && hasLength!Range) { size_t cnt; Range start, now; this(Range r) { cnt = 0; start = r.save; now = r.save; } this(this) { start = start.save; now = now.save; } @property bool empty() { return now.empty; } @property auto front() { assert(!now.empty); import std.range : take, chain; return chain(now, start.take(cnt)); } @property Rotator!Range save() { return this; } void popFront() { cnt++; now.popFront; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/numeric/primitive.d */ // module dcomp.numeric.primitive; import std.traits; import std.bigint; T pow(T, U)(T x, U n) if (!isFloatingPoint!T && (isIntegral!U || is(U == BigInt))) { return pow(x, n, T(1)); } T pow(T, U)(T x, U n, T e) if (isIntegral!U || is(U == BigInt)) { while (n) { if (n & 1) e *= x; x *= x; n /= 2; } return e; } T lcm(T)(in T a, in T b) { import std.numeric : gcd; return a / gcd(a,b) * b; } T[3] extGcd(T)(in T a, in T b) if (!isIntegral!T || isSigned!T) { if (b==0) { return [T(1), T(0), a]; } else { auto e = extGcd(b, a%b); return [e[1], e[0]-a/b*e[1], e[2]]; } }
D
import std.stdio; import std.algorithm; import std.string; import std.functional; import std.array; import std.conv; import std.math; import std.typecons; import std.regex; import std.range; void main(){ int n = readln().chomp().to!int; for(int i=0;i<n;i++){ char[] s = readln().chomp().to!(char[]); if(s.length>=6){ for(int j=0;j<s.length-6;j++){ if(s[j]=='H'&&s[j+1]=='o'&&s[j+2]=='s'&&s[j+3]=='h'&&s[j+4]=='i'&&s[j+5]=='n'&&s[j+6]=='o') s[j+6] = 'a'; } } s.writeln(); } }
D
import std.stdio, std.string, std.conv, std.algorithm; void main(string[] args) { readln(); auto p = readln().chomp.split.map!(to!int); int a; for(int i; i<p.length; i++){ if(p[i]==i+1){a++; i++;} } a.writeln; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } string[] M; long H, W; bool[][] used; void main() { scan(H, W); used = new bool[][](H, W); foreach (_; 0 .. H) M ~= readln().chomp; long sum; foreach (y; 0 .. H) foreach (x; 0 .. W) { auto t = dfs(y, x); sum += t[0] * t[1]; } writeln(sum); } Tuple!(long, long) dfs(long y, long x) { if (used[y][x]) return Tuple!(long, long)(0, 0); used[y][x] = true; long w, b; if (M[y][x] == '.') w = 1; else b = 1; if (0 <= y - 1 && !used[y - 1][x] && M[y][x] != M[y - 1][x]) { auto tmp = dfs(y - 1, x); w += tmp[0]; b += tmp[1]; } if (y + 1 < H && !used[y + 1][x] && M[y][x] != M[y + 1][x]) { auto tmp = dfs(y + 1, x); w += tmp[0]; b += tmp[1]; } if (0 <= x - 1 && !used[y][x - 1] && M[y][x] != M[y][x - 1]) { auto tmp = dfs(y, x - 1); w += tmp[0]; b += tmp[1]; } if (x + 1 < W && !used[y][x + 1] && M[y][x] != M[y][x + 1]) { auto tmp = dfs(y, x + 1); w += tmp[0]; b += tmp[1]; } return Tuple!(long, long)(w, b); }
D
import std.stdio, std.conv, std.string; void main() { auto ip = readln.split.to!(int[]); if(ip[0] % 3 == 0 || ip[1] % 3 == 0 || (ip[0] + ip[1]) % 3 == 0){ writeln("Possible"); } else { writeln("Impossible"); } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { int n, a; scan(n); scan(a); writeln(n % 500 <= a ? "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); } } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto d = readln.chomp.split.to!(int[]); if (d[0] == d[1] && d[1] == d[2]) writeln("Yes"); else writeln("No"); }
D