code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto ab = readln.split.to!(long[]); auto A = ab[0]; auto B = ab[1]; if (B < A) swap(A, B); long s = 1, r = 1; long[] rest; void solve(long x) { if (A%x != 0) return; if (B%x != 0) return; if (gcd(x, s) != 1) return; ++r; s *= x; return; } for (long x = 2; x^^2 <= A; ++x) { solve(x); if (A%x == 0) rest ~= A/x; } reverse(rest); foreach (x; rest) solve(x); if (A != 1) solve(A); writeln(r); }
D
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array, std.functional, std.container, std.typecons; int M = 1000000007; int[] tbl = [ 1, 10, 9, 12, 3, 4 ]; void main() { string S = readln.chomp; auto N = S.length; ulong[] items = new ulong[0]; int baseMod13 = 0; foreach(i; 0..N) { char c = S[$-1-i]; if(c == '?') items ~= i % 6; c = c != '?' ? c : '0'; baseMod13 += (tbl[i % 6]*(c - '0')) % 13; } baseMod13 %= 13; int mod13 = ((13+5) - baseMod13) % 13; ulong[13][] dp = new ulong[13][items.length]; if (dp.length == 0) { writeln(baseMod13 == 5 ? 1 : 0); return; } foreach(i; 0..10) { int mod = (tbl[items[0]] * i)%13; dp[0][mod]++; } foreach(i; 1..dp.length) { foreach(j; 0..13) { ulong m = 0; foreach(k; 0..10) { int mod = (tbl[items[i]] * k) % 13; int diff = ((13+j) - mod) % 13; m = (m + dp[i-1][diff]) % M; } dp[i][j] = m % M; } } dp[$-1][mod13].writeln; }
D
import std.stdio; import std.string; import std.conv; import std.range; import std.array; import std.algorithm; void main(){ auto a=readln.chomp.to!int; auto b=readln.chomp.to!int; if(a%500<=b)writeln("Yes"); else writeln("No"); }
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 q = readln().chomp().to!int; for(int i=0;i<q;i++){ int c,a,n; int[] s = readln().split().to!(int[]); c = s[0]; a = s[1]; n = s[2]; int res; if(n>0){ res = min(c,min(a,n)); int temp = min(c,min(a,n)); c -= temp; a -= temp; } if(a>0){ res += min(to!int(floor(to!real(c)/2.0)),a); c -= 2*min(to!int(floor(to!real(c)/2.0)),a); } if(c>0) res += to!int(floor(to!real(c)/3.0)); writeln(res); } }
D
import std.stdio; import std.conv; import std.algorithm; import std.range; import std.string; import std.typecons; import std.math; import std.random; import std.range; import std.functional; import std.container; class bt(T) { t!T root; bool containsKey(T key) { return !!find(key); } void deleteKey(T key) { auto n = find(key); auto n2 = n; if (!n) return; if (n.left && n.right) { n = n.getSuccessor; } if (!n.left && !n.right) { n.parent.replaceChild(n, null); } else if (n.left && !n.right) { n.parent.replaceChild(n, n.left); } else if (!n.left && n.right) { n.parent.replaceChild(n, n.right); } n2.key = n.key; } private t!T find(T key) { return root ? root.find(key) : null; } void insert(T key) { t!T y = null; auto x = root; auto z = new t!T(key); while (x) { y = x; if (z.key < y.key) { x = x.left; } else { x = x.right; } } z.parent = y; if (!y) { root = z; } else { if (z.key < y.key) { y.left = z; } else { y.right = z; } } } } class t(T) { T key; this(T k) { key = k; } t left, right, parent; t!T getMin() { return this.left ? this.left.getMin : this; } t!T getSuccessor() { if (this.right) return this.right.getMin(); auto x = this; auto y = parent; while (y && x == y.right) { x = y; y = y.parent; } return y; } void replaceChild(t!T c, t!T r) { if (left == c) { if (r) r.parent = this; left = r; return; } if (right == c) { if (r) r.parent = this; right = r; return; } } t!T find(T k) { if (key == k) return this; if (key > k) return left ? left.find(k) : null; else return right ? right.find(k) : null; } } void in_(T)(t!T t) { if (!t) return; in_(t.left); write(" ", t.key); in_(t.right); } void pre(T)(t!T t) { if (!t) return; write(" ", t.key); pre(t.left); pre(t.right); } void main() { auto c = readln.strip.to!int; bt!int b = new bt!int; foreach (_; 0..c) { auto instr = readln.strip.split(" "); if (instr.length == 1) { in_(b.root); writeln(); pre(b.root); writeln(); } else { auto key = instr[1].to!int; if (instr[0] == "find") { writeln(b.containsKey(key) ? "yes" : "no"); } else if (instr[0] == "delete") { b.deleteKey(key); } else { b.insert(key); } } } }
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 bool[](t); foreach (ti; 0..t) { auto n = RD!int; ans[ti] = n % 4 == 0; } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush; debug readln; }
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() { string s; readV(s); auto n = s.length; foreach_reverse (i; 2..n) { if (i%2 == 1) continue; if (s[0..i/2] == s[i/2..i]) { writeln(i); return; } } }
D
import std.stdio; void main() { int m1, m2, d1, d2; scanf("%d %d\n%d %d\n", &m1, &d1, &m2, &d2); if (m1 == m2) { 0.write; } else { 1.write; } }
D
/* imports all std modules {{{*/ import std.algorithm, std.array, std.ascii, std.base64, std.bigint, std.bitmanip, std.compiler, std.complex, std.concurrency, std.container, std.conv, std.csv, std.datetime, std.demangle, std.encoding, std.exception, std.file, std.format, std.functional, std.getopt, std.json, std.math, std.mathspecial, std.meta, std.mmfile, std.net.curl, std.net.isemail, std.numeric, std.parallelism, std.path, std.process, std.random, std.range, std.regex, std.signals, std.socket, std.stdint, std.stdio, std.string, std.system, std.traits, std.typecons, std.uni, std.uri, std.utf, std.uuid, std.variant, std.zip, std.zlib; /*}}}*/ /+---test Sunny ---+/ /+---test Rainy ---+/ void main(string[] args) { const S = readln.chomp; [ "Sunny": "Cloudy", "Cloudy": "Rainy", "Rainy": "Sunny", ][S].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; T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } alias sread = () => readln.chomp(); void main() { string s = sread(); if (s.length == 3) { writeln(s[2],s[1],s[0]); return; } s.writeln(); }
D
void main() { auto k = ri; auto s = rs; if (s.length <= k) writeln(s); else writeln(s[0..k] ~ "..."); } // =================================== 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; } long rl() { return readAs!long; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
void main() { string s = rdStr; if (s[0] == '>') s = '<' ~ s; if (s[$-1] == '<') s ~= '>'; auto g = s.group.array; long result; for (long i = 0; i < g.length; i += 2) { long x = g[i][1], y = g[i+1][1]; if (x < y) { result += x * (x - 1) / 2; result += y * (y + 1) / 2; } else { result += x * (x + 1) / 2; result += y * (y - 1) / 2; } } 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 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
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std; // dfmt on void main() { long N = lread(); long ans = N % 1000; writeln((1000 - ans) % 1000); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; void main(){ auto F=readln.split.to!(int[]),a=F[0],b=F[1],c=F[2]; if(a+b==c||a+c==b||b+c==a)writeln("Yes"); else writeln("No"); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.numeric; void main() { auto n = readln.chomp.to!int; long[char] cnt; foreach (_; 0..n) { cnt[readln.chomp[0]]++; } long sum; auto ini = ['M', 'A', 'R', 'C', 'H']; foreach (i; 0..ini.length) { if (!cnt.get(ini[i], 0)) continue; long ltmp = cnt[ini[i]]; foreach (j; i+1..ini.length) { if (!cnt.get(ini[j], 0)) continue; ltmp *= cnt[ini[j]]; foreach (k; j+1..ini.length) { if (!cnt.get(ini[k], 0)) continue; ltmp *= cnt[ini[k]]; sum += ltmp; ltmp /= cnt[ini[k]]; } ltmp /= cnt[ini[j]]; } } sum.writeln; }
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]); auto N = my_readln().to!ulong(); auto H = my_readln().to!ulong(); auto W = my_readln().to!ulong(); auto x = N - H + 1; auto y = N - W + 1; writeln(x * y); stdout.flush(); /*}catch (Throwable e) { writeln(e.toString()); } readln();*/ }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; void main(){ auto F=readln.split.to!(int[]),A=F[0],B=F[1],C=F[2],D=F[3]; if(A+B<C+D)writeln("Right"); else if(A+B>C+D)writeln("Left"); else writeln("Balanced"); }
D
import std.stdio; import std.array; import std.conv; void main() { string[] input = split(readln()); int a=to!int(input[0]); int b=to!int(input[1]); if((a*b)%2==0) { writeln("Even"); }else { writeln("Odd"); } }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; void times(alias fun)(int n) { foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } void main() { 2.rep!(() => readln.chomp).pipe!( a => a.map!( b => b.length ).sum.iota.map!( i => a[i%2][i/2] ).fold!"a~b"("") ).writeln; } // ---------------------------------------------- // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { auto minElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto minimum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b < minimum) { element = a; minimum = b; } } return element; } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto maximum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b > maximum) { element = a; maximum = b; } } return element; } }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; const mx=1_000_00+5; auto isPrime=new bool[](mx); auto sub=new int[](mx); isPrime[2]=true; for(int i=3; i<=1_000_00; i+=2){ for(int j=3; j*j<=i; j+=2){ if(i%j==0) goto hell; } isPrime[i]=true; if(isPrime[(i+1)/2]) sub[i]=1; hell:; } int[] a; for(int i=3; i<=1_000_00; i++)if(sub[i]) a~=i; int q; rd(q); while(q--){ int l, r; rd(l, r); int cnt=0; foreach(e; a){ if(l<=e && e<=r) cnt++; } writeln(cnt); } } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } } void wr(T...)(T x){ import std.stdio; foreach(e; x) write(e, " "); writeln(); }
D
import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; void main(){ int k, t; { int[] tmp = readln.chomp.split.map!(to!int).array; k = tmp[0], t = tmp[1]; } int[] as = readln.chomp.split.map!(to!int).array; int amax = as.reduce!("max(a,b)"); int ans; if(k % 2 == 0){ if(amax <= k / 2) ans = 0; else ans = (amax - (k / 2)) * 2 - 1; } else{ if(amax <= (k + 1) / 2) ans = 0; else ans = (amax - ((k + 1) / 2)) * 2; } ans.writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto A = readln.chomp.to!int; auto B = readln.chomp.to!int; auto C = readln.chomp.to!int; auto D = readln.chomp.to!int; writeln(min(A, B) + min(C, D)); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; int[2][6] pt; void main() { foreach (i; 0..3) { auto ab = readln.split.to!(int[]); pt[i] = [ab[0], ab[1]]; pt[3+i] = [ab[1], ab[0]]; } foreach (i, a; pt) { foreach (j, b; pt) { foreach (k, c; pt) { if (i%3 == j%3 || j%3 == k%3 || k%3 == i%3) continue; if (a[1] == b[0] && b[1] == c[0]) { writeln("YES"); return; } } } } writeln("NO"); }
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, X; scan(N, M, X); auto C = new long[](N); auto A = new long[][](N); foreach (i; 0 .. N) { auto tmp = aryread(); C[i] = tmp[0]; A[i] = tmp[1 .. $]; } long ans = long.max; foreach (s; 0 .. 1 << N) { auto cnt = new long[](M); long cost; foreach (i; 0 .. N) if (s & (1 << i)) { cost += C[i]; cnt[] += A[i][]; } if (cnt.all!(a => X <= a)) { ans = ans.min(cost); } } writeln(ans == long.max ? -1 : ans); }
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 = 1L << 60; void main() { int n; scan(n); auto a = readln.split.to!(int[]); auto dp = new long[][](n + 1, n + 1); fillAll(dp, -inf); // 最大化問題なので -inf で初期化するべき // 先手は X - Y の最大化 // 後手は Y - X の最大化 // をすると考えれば先手後手の場合分けが不要になる long dfs(int l, int r) { auto v = &dp[l][r]; if (*v != -inf) { return *v; } if (r - l == 1) { return *v = a[l]; } return *v = max(a[l] - dfs(l + 1, r), a[r - 1] - dfs(l, r - 1)); } auto ans = dfs(0, 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); } } }
D
import std.stdio, std.conv, std.string, std.algorithm, std.math, std.array, std.container, std.typecons; void main() { readln; string s = readln.chomp; int count=0; for(int i=0; i<s.length-2; i++) { if(s[i]=='A'&&s[i+1]=='B'&&s[i+2]=='C') count++; } writeln(count); }
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() { long x = readln.chomp.to!long; long atom = x / 11; long remain = x - atom * 11; long ans = atom * 2; if (remain > 6) { ans += 2; } else if (remain > 0) { ++ans; } ans.writeln; }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto s = readln.chomp.split.to!(int[]); auto f = true; if (s[0] % s[1]) writeln(s[0]); else writeln(-1); }
D
void main() { long n = readln.chomp.to!long; long result; foreach (i; 1 .. 500001) { int x = 108 * i / 100; if (x == n) { result = i; break; } } if (result > 0) { result.writeln; } 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.stdio; import std.algorithm; import std.string; import std.range; import std.array; import std.conv; import std.complex; import std.math; import std.ascii; import std.bigint; import std.container; double offset(int lr, int rr) { return sqrt((lr+rr)^^2.-(lr-rr)^^2); } void main() { while(!stdin.eof()) { auto s = array(map!(a => to!int(a))(readln().strip().split())); if(s.empty()) break; int n = cast(int)s.length-1; int l = s[0]; int [] r = s[1..$]; double[][] dp = new double[][](1L<<12,n); for(int i; i < 1<<n; ++i){ for(int j; j < n; ++j){ dp[i][j] = l+100; } } for(int i; i < n; ++i){ dp[1<<i][i] = r[i]; } for(int i = 1; i < 1<<n; ++i){ for(int j; j < n; ++j) { if(i & 1<<j) continue; for(int k; k < n; ++k) { if(i & 1<<k) { dp[i|1<<j][j] = min(dp[i|1<<j][j], dp[i][k]+offset(r[k],r[j])); } } } } for(int i; i < n; ++i){ dp[(1<<n)-1][i] += r[i]; } if(reduce!(min)(dp[(1<<n)-1]) > l) writeln("NA"); else writeln("OK"); } }
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 k = RD!int; auto ans = new bool[](k); foreach (i; 0..k) { auto n = RD!int; auto s = RD!string; auto t = RD!string; int[2] pos; int cnt; foreach (j; 0..n) { if (s[j] != t[j]) { ++cnt; if (cnt >= 3) break; pos[cnt-1] = j; } } if (cnt == 2) { auto p1 = pos[0]; auto p2 = pos[1]; if (s[p1] == s[p2] && t[p1] == t[p2]) ans[i] = true; } } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush(); debug readln(); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; long calc(int k, int[] a) { long ans = long.max; for (int i = 0; i < a.length; i++) { if (i + k - 1 >= a.length) break; int lo = i; int hi = i + k - 1; // 0 -> lo -> hi の順に火をつける ans = min(ans, abs(a[lo]) + abs(a[hi] - a[lo])); // 0 -> hi -> lo の順に火をつける ans = min(ans, abs(a[hi]) + abs(a[hi] - a[lo])); } return ans; } void main() { auto nk = readints; int k = nk[1]; auto a = readints; writeln(calc(k, a)); /* int n = nk[0], k = nk[1]; auto xs = readints; int[] pos, neg; foreach (x; xs) { if (x >= 0) pos ~= x; else neg ~= x; } neg.reverse(); long ans = long.max; for (int i = 0; i < pos.length; i++) { long p = pos[i]; int rest = k - i - 1; if (rest == 0) ans = min(ans, pos[i]); if (rest < 0) break; if (0 < rest && rest <= neg.length) { ans = min(ans, 2 * pos[i] + neg[rest-1] * -1); } } for (int i = 0; i < neg.length; i++) { int rest = k - i - 1; if (rest == 0) ans = min(ans, -neg[i]); if (rest < 0) break; if (0 < rest && rest <= pos.length) { ans = min(ans, 2 * -neg[i] + pos[rest-1]); } } writeln(ans); */ }
D
/+ dub.sdl: name "C" dependency "dunkelheit" version="1.0.1" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; int main() { Scanner sc = new Scanner(stdin); scope(exit) assert(!sc.hasNext); int n; sc.read(n); int sm, _a, b_, ba; foreach (i; 0..n) { string s; sc.read(s); bool fa = s.back == 'A'; bool fb = s.front == 'B'; if (fa && fb) ba++; if (fa && !fb) _a++; if (!fa && fb) b_++; sm += s.count("AB"); } debug writeln(sm, _a, b_, ba); if (!_a || !b_) { sm += max(0, ba - 1); if (ba && (_a || b_)) sm++; } else { sm += min(_a, b_) + ba; } writeln(sm); return 0; } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/scanner.d */ // module dkh.scanner; // import dkh.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; private File f; this(File f) { this.f = f; } private char[512] lineBuf; private char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { assert(succW()); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(Args...)(auto ref Args args) { import std.exception : enforce; static if (args.length != 0) { enforce(readSingle(args[0])); read(args[1..$]); } } bool hasNext() { return succ(); } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional, std.traits; import std.algorithm, std.container; struct E{ long[] parentes; long h=-1; } E[] list; long calc(long i) { if(list[i].parentes.empty){ list[i].parentes = [0]; list[i].h=0; } if(list[i].h!=-1) return list[i].h; long m; foreach(parent; list[i].parentes) { if(calc(parent) > list[m].h){ m = parent; } } list[i].parentes = [m]; list[i].h = list[m].h+1; return list[i].h; } void main() { auto N = scanElem; auto M = scanElem; list.length=N+1; foreach(i;0..N-1+M) { auto parent = scanElem; auto node = scanElem; list[node].parentes~=parent; } foreach(i;1..N+1) { calc(i); } foreach(i;1..N+1) { writeln(list[i].parentes[0]); } } 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 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() { long H, N; scan(H, N); auto A = new long[](N); auto B = new long[](N); foreach (i; 0 .. N) scan(A[i], B[i]); auto ans = new long[](H + 1); ans[] = long.max >> 2; ans[H] = 0; foreach_reverse (h; 1 .. H + 1) foreach (i; 0 .. N) { ans[max(0, h - A[i])] = ans[max(0, h - A[i])].min(ans[h] + B[i]); } writeln(ans[0]); }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std; // dfmt on void main() { long N, A, B; scan(N, A, B); A -= B; auto H = lreads(N); long ok = 10 ^^ 9 + 100; long ng = 0; bool eval(long x) { long cnt; foreach (i; 0 .. N) { if (B * x < H[i]) { cnt += (H[i] - B * x + A - 1) / A; } if (x < cnt) return false; } return true; } while (1 < ok - ng) { long m = (ok + ng) / 2; if (eval(m)) { ok = m; } else { ng = m; } } writeln(ok); }
D
import std.stdio, std.string, std.range, std.algorithm; import std.conv; void main() { int n = readln.strip.to!int;if (n == 3 || n == 5 || n == 7) writeln("YES"); else writeln("NO"); }
D
/+ dub.sdl: name "D" dependency "dcomp" version=">=0.7.4" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; // import dcomp.graph.dijkstra; struct E { int to; int dist; } int main() { Scanner sc = new Scanner(stdin); int k; sc.read(k); E[][] g = new E[][k+1]; void add(int a, int b, int c) { if (b == 0) b = k; g[a] ~= E(b, c); } foreach (i; 0..k) { add(i, (i+1)%k, 1); if (i) add(i, 10*i % k, 0); } writeln(g.dijkstra!(int)(0).dist[k]); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/radixheap.d */ // module dcomp.container.radixheap; // import dcomp.foundation; // import dcomp.array; import std.functional : unaryFun; import std.traits : isSigned, isUnsigned; template RadixHeap(T, alias _pred = "a") if (isSigned!(typeof(unaryFun!_pred(T())))) { import std.traits : Unsigned; alias pred = unaryFun!_pred; alias S = typeof(pred(T())); alias U = Unsigned!S; U pred2(in T x) { return pred(x) ^ (U(1) << (U.sizeof*8-1)); } alias RadixHeap = RadixHeap!(T, pred2); } import core.bitop; struct RadixHeap(T, alias _pred = "a") if (isUnsigned!(typeof(unaryFun!_pred(T())))) { import std.algorithm; import core.exception : RangeError; alias pred = unaryFun!_pred; alias U = typeof(pred(T())); static int bsr1(U x) { return (x == 0) ? 0 : bsr(x)+1; } static struct Payload { FastAppender!(T[])[U.sizeof*8+1] v; size_t length; U last; void insert(T p) { U x = pred(p); assert(last <= x); length++; v[bsr1(x^last)] ~= p; } bool empty() const { return length == 0; } T front() { if (!v[0].length) { int i = 1; while (!v[i].length) i++; last = v[i].data[].map!pred.fold!"min(a, b)"; foreach (T p; v[i].data) { v[bsr1(pred(p)^last)] ~= p; } v[i].clear(); } return v[0].data[$-1]; } void removeFront() { front(); length--; v[0].removeBack(); } } Payload* p; private void I() { if (!p) p = new Payload(); } private void C() const { version(assert) if (!p) throw new RangeError(); } void insert(T x) {I; p.insert(x); } @property bool empty() const { return (p == null || p.empty); } @property size_t length() const { return (p ? p.length : 0); } T front() {C; return p.front; } void removeFront() {I; p.removeFront(); } } /* 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/graph/dijkstra.d */ // module dcomp.graph.dijkstra; // import dcomp.algorithm, dcomp.container.radixheap; struct DijkstraInfo(T) { T[] dist; int[] from; this(int n, T inf) { dist = new T[n]; dist[] = inf; from = new int[n]; } } DijkstraInfo!D dijkstra(D, T)(T g, int s, D inf = D.max) { import std.conv : to; import std.typecons : Tuple; import std.container : make, Array, heapify; import std.container.binaryheap : BinaryHeap; import std.traits : isIntegral; int V = g.length.to!int; auto dijk = DijkstraInfo!D(V, inf); with (dijk) { alias P = Tuple!(int, "to", D, "dist"); auto q = (){ static if (isIntegral!D) { return RadixHeap!(P, "a.dist")(); } else { return heapify!"a.dist>b.dist"(make!(Array!P)); } }(); q.insert(P(s, D(0))); dist[s] = D(0); from[s] = -1; while (!q.empty) { P p = q.front; q.removeFront(); if (dist[p.to] < p.dist) continue; foreach (e; g[p.to]) { if (p.dist+e.dist < dist[e.to]) { dist[e.to] = p.dist+e.dist; from[e.to] = p.to; q.insert(P(e.to, dist[e.to])); } } } } return dijk; } DijkstraInfo!D dijkstraDense(D, T)(T g, int s, D inf = D.max) { import std.conv : to; import std.typecons : Tuple; import std.container : make, Array, heapify; import std.range : enumerate; import std.algorithm : filter; int V = g.length.to!int; auto dijk = DijkstraInfo!D(V, inf); with (dijk) { alias P = Tuple!(int, "to", D, "dist"); bool[] used = new bool[](V); dist[s] = D(0); from[s] = -1; while (true) { auto rng = dist.enumerate.filter!(a => !used[a.index]); if (rng.empty) break; auto nx = rng.minimum!"a.value < b.value"; used[nx.index] = true; P p = P(nx.index.to!int, nx.value); if (p.dist == inf) continue; foreach (e; g[p.to]) { if (p.dist+e.dist < dist[e.to]) { dist[e.to] = p.dist+e.dist; from[e.to] = p.to; } } } } return dijk; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; // import dcomp.array; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { FastAppender!(E[]) buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } 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/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } import core.bitop : popcnt; static if (!__traits(compiles, popcnt(ulong.max))) { public import core.bitop : popcnt; int popcnt(ulong v) { return popcnt(cast(uint)(v)) + popcnt(cast(uint)(v>>32)); } } bool poppar(ulong v) { v^=v>>1; v^=v>>2; v&=0x1111111111111111UL; v*=0x1111111111111111UL; return ((v>>60) & 1) != 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/array.d */ // module dcomp.array; T[N] fixed(T, size_t N)(T[N] a) {return a;} struct FastAppender(A, size_t MIN = 4) { import std.algorithm : max; import std.conv; import std.range.primitives : ElementEncodingType; import core.stdc.string : memcpy; private alias T = ElementEncodingType!A; private T* _data; private uint len, cap; @property size_t length() const {return len;} bool empty() const { return len == 0; } void reserve(size_t nlen) { import core.memory : GC; if (nlen <= cap) return; void* nx = GC.malloc(nlen * T.sizeof); cap = nlen.to!uint; if (len) memcpy(nx, _data, len * T.sizeof); _data = cast(T*)(nx); } void free() { import core.memory : GC; GC.free(_data); } void opOpAssign(string op : "~")(T item) { if (len == cap) { reserve(max(MIN, cap*2)); } _data[len++] = item; } void insertBack(T item) { this ~= item; } void removeBack() { len--; } void clear() { len = 0; } ref inout(T) back() inout { assert(len); return _data[len-1]; } ref inout(T) opIndex(size_t i) inout { return _data[i]; } T[] data() { return (_data) ? _data[0..len] : null; } } /* This source code generated by dcomp and include dcomp's source code. dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp) dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt) */
D
import std.stdio, std.string, std.conv; string selectOp(int f) { return (f ? "+" : "-"); } void main() { string s = readln.chomp; int[] a; for (int i = 0; i < s.length; i++) { a ~= cast(char)s[i] - '0'; } for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { int e = a[0] + (i ? a[1] : -a[1]) + (j ? a[2] : -a[2]) + (k ? a[3] : -a[3]); if (e == 7) { writeln(s[0] ~ selectOp(i) ~ s[1] ~ selectOp(j) ~ s[2] ~ selectOp(k) ~ s[3] ~ "=7"); return; } } } } }
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 readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n; readV(n); int[] a; readA(n, a); writeln(a.sum-n); }
D
import std.stdio; import std.conv; import std.algorithm; import std.range; import std.string; import std.math; import std.format; void main() { int[] q; foreach (string line; stdin.lines) { int n = line.chomp.to!int; if (n == 0) { q.back.writeln; q.popBack; } else { q ~= n; } } }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main() { auto ab = readln.split.to!( int[] ); writeln( solve( ab[ 0 ], ab[ 1 ] ) ); } auto solve( in int a, in int b ) { auto d = b - a; auto h = iota( 1, d ).sum; return h - a; } unittest { assert( solve( 8, 13 ) == 2 ); assert( solve( 54, 65 ) == 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 S = readln.split[0]; int result = 10000; foreach (i; 0 .. S.length-2) { result = min( (S[i .. i+3].to!int - 753).abs, result); } result.writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } 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 N = RD; auto A = RDA; auto B = new long[](N); long[][long] list; foreach (i; 0..N) { B[i] = A[i] - i; list[B[i]] ~= i; } debug writeln(A); long ans; foreach (i; 0..N) { auto x = -(A[i] + i); auto l = list.get(x, []); bool f(long x) { return l[x] <= i; } auto pos = binarySearch!(f)(-1, cast(long)l.length); ans += l.length - (pos+1); debug writeln(ans); } writeln(ans); stdout.flush; debug readln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { foreach (i; 0..3) write(readln.chomp.to!(wchar[])[i]); writeln(""); }
D
import std; long calc(long a, long b, long n) { long x = min(b-1, n); return a * x / b; } void main() { long a, b, n; scan(a, b, n); writeln(calc(a, b, n)); } void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int;
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto s = RD!string; long cnt; string tmp; foreach (c; s) { auto num = [c].to!int; if (num % 2 == 1) { ++cnt; tmp ~= c; if (cnt == 2) break; } } if (cnt != 2) ans[ti] = -1; else ans[ti] = tmp.to!long; } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.algorithm; import std.range; import std.stdio; import std.typecons; import std.traits; import std.array; struct Input { T next(T)() { import std.conv; return to!T(nextWord); } string nextWord() { if (_nextWords.empty) _nextWords.insertFront(readln().split); string word = _nextWords.front; _nextWords.removeFront; return word; } import std.container; DList!string _nextWords; } Input input; void read(T...)(ref T args) { import std.traits; static foreach(i; 0 .. T.length) static if(isArray!(T[i]) && !is(T[i] == string)) foreach(ref e; args[i]) read(e); else static if(isTuple!(T[i])) static foreach(n; 0 .. T[i].Types.length) read(args[i][n]); else args[i] = input.next!(typeof(args[i]))(); } auto itup(alias k, alias F)() { alias T = Parameters!F; foreach(i; 0 .. k) { T t; get(t); F(t); } } alias get = read; void wone(T)(T t, char end) { static if(isArray!T && !is(T == string)) { foreach(i; 0 .. t.length - 1) write(t[i], ' '); write(t[$ - 1], end); } else write(t, end); } void wr(T...)(T t) { static foreach(i; 0 .. T.length) static if(i + 1 < T.length) wone(t[i], ' '); else wone(t[i], '\n'); } void ans(T...)(T t) { import core.stdc.stdlib; wr(t); exit(0); } void main() { int q; get(q); while(q--) { int[4] count = 0; string str; get(str); foreach(c; str) { switch(c) { case 'L': count[0]++; break; case 'R': count[1]++; break; case 'U': count[2]++; break; case 'D': count[3]++; break; default: assert(0); } } int l = min(count[0], count[1]); int r = min(count[2], count[3]); if(l == 0 && r > 1) { r = 1; } if(l > 1 && r == 0) { l = 1; } wr(2 * (l + r)); foreach(i; 0 .. r) write('U'); foreach(i; 0 .. l) write('R'); foreach(i; 0 .. r) write('D'); foreach(i; 0 .. l) write('L'); if(l + r) writeln; } }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { int n; scan(n); auto c = new int[](n); auto s = new int[](n); auto f = new int[](n); foreach (i ; 0 .. n-1) { int ci, si, fi; scan(ci,si,fi); c[i] = ci; s[i] = si; f[i] = fi; } foreach (i ; 0 .. n) { int ans; foreach (j ; i .. n-1) { if (ans < s[j]) { ans = s[j]; } else { if (ans % f[j]) { ans += f[j] - ans % f[j]; } } ans += c[j]; debug { writeln(j, " ", ans); } } writeln(ans); } } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void main() { auto ts = readln.split.to!(long[]); auto I = ts[0]; auto O = ts[1]; auto J = ts[3]; auto L = ts[4]; auto K = O + I/2*2 + J/2*2 + L/2*2; auto r = I%2 + J%2 + L%2; if (r == 3) { K += 3; } else if (r == 2 && I > 0 && J > 0 && L > 0) { K += 1; } writeln(K); }
D
import std.stdio, std.string, std.conv, std.range, std.array, std.algorithm; import std.uni, std.math, std.container, std.typecons, std.typetuple; import core.bitop, std.datetime; immutable long mod = 10^^9 + 7; immutable int inf = mod; void main(){ int n = readln.chomp.to!int; auto a = new int[](n + 1); foreach(i ; 1 .. n + 1){ readVars(a[i]); } auto dp = new int[](n + 1); auto ma = new int[](n + 1); ma[1 .. $] = inf; ma[0] = -1; foreach(i ; 1 .. n + 1){ int j = nibutan(n, ma, a[i]) + 1; ma[j] = a[i]; dp[i] = j; //writeln(ma); } int ans = dp.reduce!max; writeln(ans); } int nibutan(int n, int[] ma, int v){ int btm = -1, top = n; while (top - btm > 1) { int mid = (top + btm) / 2; if (ma[mid] < v) { btm = mid; } else { top = mid; } } return btm; } void readVars(T...)(auto ref T args){ auto line = readln.split; foreach(ref arg ; args){ arg = line.front.to!(typeof(arg)); line.popFront; } if(!line.empty){ throw new Exception("args num < input num"); } }
D
import std.stdio, std.string, std.algorithm, std.array, std.conv; void main() { for (int i = 0; i < 1000; i++) { "Hello World".writeln; } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; 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; auto dx = readints; int d = dx[0], x = dx[1]; int[] a; for (int i = 0; i < n; i++) { a ~= readint; } int ans = x; for (int i = 0; i < n; i++) { int p = 1; while (p <= d) { ans++; p += a[i]; } } writeln(ans); }
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 r, g, b; readV(r, g, b); writeln((g*10+b)%4 == 0 ? "YES" : "NO"); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp.to!int; auto a = readln.chomp.split.to!(int[]); auto b = readln.chomp.split.to!(int[]); auto c = readln.chomp.split.to!(int[]); auto prev = -1; int res; foreach (i; 0..n) { res += b[a[i]-1]; if (prev + 1 == a[i]) { res += c[a[i]-2]; } prev = a[i]; } res.writeln; }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { const N = readln.chomp.to!int; const hs = readln.split.to!(int[]); N.iota.filter!(i => reduce!max(0, hs[0..i]) <= hs[i]).walkLength.writeln; }
D
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; import std.datetime, std.bigint; void main() { string n; scan(n); writeln("ABC" ~ n); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.conv,std.string; void main() { while(true) { int sum=readln.chomp.to!int; if(sum==0)break; foreach(unused;0..9) { sum-=readln.chomp.to!int; } sum.writeln; } }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; T lcm(T)(T a, T b) { return a * (b / gcd(a, b)); } long calc(long a, long b, long c, long d) { // 区間 [1, n] で x で割り切れる数の個数 long f(long n, long x) { return n / x; } // 区間 [1, n] で x or y で割り切れる数の個数 long g(long n, long x, long y) { return f(n, x) + f(n, y) - f(n, lcm(x, y)); } // 区間 [1, n] で x or y で割り切れない数の個数 long h(long n, long x, long y) { return n - g(n, x, y); } return h(b, c, d) - h(a - 1, c, d); } void main() { long a, b, c, d; scan(a, b, c, d); writeln(calc(a, b, c, d)); }
D
void main() { long N = rdElem; foreach (h; 1 .. 3501) { foreach (n; 1 .. 3501) { long numer = h * n * N; long dinom = 4 * h * n - (h + n) * N; if (dinom <= 0) continue; long w = numer / dinom, rem = numer % dinom; if (w > 0 && rem == 0) { writeln(h, " ", n, " ", w); return; } } } } 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 core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "a", long, "b"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { char a; scan(a); if (a >= 'a') writeln("a"); else writeln("A"); } 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 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 N, K; scan(N, K); writeln(((K - 1) ^^ (N - 1)) * K); }
D
import std.algorithm; import std.stdio; import std.string; void main() { auto s = readln.strip; writeln( solve( s ) ); } int solve( in string s ) { return 700 + s.map!( a => ( a == 'o' ) ? 100 : 0 ).sum; } unittest { assert( solve( "oxo" ) == 900 ); assert( solve( "ooo" ) == 1000 ); assert( solve( "xxx" ) == 700 ); }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional, std.traits; import std.algorithm, std.container; import core.stdc.stdlib; void main() { auto A = scanElem; auto B = scanElem; auto T = scanElem; writeln((T/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!(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
void main() { auto arr = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]; writeln(arr[ri-1]); } // =================================== 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.algorithm, std.array, std.conv; int mex(int[] arr) { if (arr.maxElement == 2) return 2; else if (arr.minElement == arr.maxElement) return 1 - arr.minElement; else return 2; } void main() { int t; scanf("%d", &t); getchar(); foreach(j; 0..t){ int str_l; scanf("%d", &str_l); getchar(); auto str1 = readln.strip(); auto str2 = readln.strip(); int[] result; for (int i = 0; i < str_l; i++) { if (str1[i] == str2[i]) { result ~= cast(int)(str1[i] - '0'); } else result ~= 2; } int[] mex_res; debug{writeln(result);} for (int i = 0; i < str_l; i++) { mex_res ~= mex(result[i..i+1]); } debug{writeln(mex_res);} for (int i = 0; i < str_l; i++) { if (mex_res[i] == 0) { if (i == 0) { if (mex_res[i+1] == 1) { if (mex(result[i..i+2]) == 2) mex_res[i+1]++; } } else if (i == str_l - 1) { if (mex_res[i-1] == 1) { if (mex(result[i-1..i+1]) == 2) mex_res[i-1]++; } } else { if (mex_res[i-1] == 1) { if (mex(result[i-1..i+1]) == 2) mex_res[i-1]++; } else if (mex_res[i+1] == 1) { if (mex(result[i..i+2]) == 2) mex_res[i+1]++; } } } } writeln(mex_res.sum); } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.math; void main() { auto N = readln.chomp.to!int; bool[int] d; foreach(_; 0..N) { d[readln.chomp.to!int] = true; } writeln(d.length); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { int x, a, b; scan(x, a, b); if (b - a <= 0) { writeln("delicious"); } else if (b - a <= x) { writeln("safe"); } else { writeln("dangerous"); } } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD; long x = 1; long last = n; foreach (i; 2..n+10) { x = lcm(x, i); auto tmp = last - n / x; tmp.modm(i); ans[ti].moda(tmp); last = n / x; if (last == 0) break; } } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N, M, K; scan(N, M, K); long MOD = 998244353; auto C = Combination_mod(N, MOD); long ans; foreach (i; 0 .. K + 1) { long tmp = C(N - 1, i) * powmod(M - 1, N - i - 1, MOD); // writeln(tmp); ans = (ans + tmp) % MOD; } writeln(ans * M % MOD); } /// x^^n % m T powmod(T = long)(T x, T n, T m = 10 ^^ 9 + 7) { if (n < 1) return 1; if (n & 1) { return x * powmod(x, n - 1, m) % m; } T tmp = powmod(x, n / 2, m); return tmp * tmp % m; } /// Number of k-combinations % m (precalculated) alias Combination_mod = Combination_modImpl!long; struct Combination_modImpl(T) { T _n, _m; T[] _fact, _factinv; this(T maxnum, T mod = 10 ^^ 9 + 7) { _n = maxnum, _m = mod, _fact = new T[](_n + 1), _factinv = new T[](_n + 1), _fact[0] = 1; foreach (i; 1 .. _n + 1) _fact[i] = _fact[i - 1] * i % _m; T powmod(T x, T n, T m) { if (n < 1) return 1; if (n & 1) { return x * powmod(x, n - 1, m) % m; } T tmp = powmod(x, n / 2, m); return tmp * tmp % m; } foreach (i; 0 .. _n + 1) _factinv[i] = powmod(_fact[i], _m - 2, _m); } T opCall(T n, T k, T dummy = 10 ^^ 9 + 7) { return n < k ? 0 : ((_fact[n] * _factinv[n - k] % _m) * _factinv[k] % _m); } }
D
import std.stdio, std.conv, std.array, std.algorithm, std.string, std.math; void main() { uint count = to!uint(readln().chomp); for (uint i = 0; i < count; ++i) { double[] t = map!(to!double)(readln().chomp.split).array; double[] a = [t[0]-t[2], t[1]-t[3]]; double[] b = [t[4]-t[6], t[5]-t[7]]; if (is_zero(outer_product2(a, b))) { writeln("YES"); } else { writeln("NO"); } } } bool is_zero(double x) { if (abs(x) < 1.0e-10) { return true; } else { return false; } } double outer_product2(double[] a, double[] b) { return a[0]*b[1] - a[1]*b[0]; }
D
import std.stdio; import std.string; import std.conv; import std.range; import std.algorithm; void main() { int n = readln.chomp.to!int; int minv = readln.chomp.to!int; int maxv = -1_000_000_000; foreach (i; iota(1, n, 1)) { int l = readln.chomp.to!int; maxv = max(maxv, l - minv); minv = min(minv, l); } maxv.writeln; }
D
import std.stdio; import std.string; import std.format; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.concurrency; import std.traits; import std.uni; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; enum long INF = long.max/3; enum long MOD = 10L^^9+7; void main() { long K, A, B; scanln(K, A, B); long D = B-A; if (D <= 2) { (K+1).writeln; return; } long y = max(0, min(K.floor(2), (K+1-A).floor(2))); long x = K - 2*y; writeln(1 + x + y*D); } // ---------------------------------------------- void times(alias fun)(long n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(long n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { // `(x+y-1)/y` will only work for positive numbers ... T t = x / y; if (t * y < x) t++; return t; } T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { T t = x / y; if (t * y > x) t--; return t; } ref T ch(alias fun, T, S...)(ref T lhs, S rhs) { return lhs = fun(lhs, rhs); } unittest { long x = 1000; x.ch!min(2000); assert(x == 1000); x.ch!min(3, 2, 1); assert(x == 1); x.ch!max(100).ch!min(1000); // clamp assert(x == 100); x.ch!max(0).ch!min(10); // clamp assert(x == 10); } mixin template Constructor() { import std.traits : FieldNameTuple; this(Args...)(Args args) { // static foreach(i, v; args) { foreach(i, v; args) { mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v; } } } void scanln(Args...)(auto ref Args args) { import std.meta; template getFormat(T) { static if (isIntegral!T) { enum getFormat = "%d"; } else static if (isFloatingPoint!T) { enum getFormat = "%g"; } else static if (isSomeString!T || isSomeChar!T) { enum getFormat = "%s"; } else { static assert(false); } } enum string fmt = [staticMap!(getFormat, Args)].join(" "); string[] inputs = readln.chomp.split; foreach(i, ref v; args) { v = inputs[i].to!(Args[i]); } } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { private template RebindableOrUnqual(T) { static if (is(T == class) || is(T == interface) || isDynamicArray!T || isAssociativeArray!T) alias RebindableOrUnqual = Rebindable!T; else alias RebindableOrUnqual = Unqual!T; } private auto extremum(alias map, alias selector = "a < b", Range)(Range r) if (isInputRange!Range && !isInfinite!Range && is(typeof(unaryFun!map(ElementType!(Range).init)))) in { assert(!r.empty, "r is an empty range"); } body { alias Element = ElementType!Range; RebindableOrUnqual!Element seed = r.front; r.popFront(); return extremum!(map, selector)(r, seed); } private auto extremum(alias map, alias selector = "a < b", Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seedElement) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void) && is(typeof(unaryFun!map(ElementType!(Range).init)))) { alias mapFun = unaryFun!map; alias selectorFun = binaryFun!selector; alias Element = ElementType!Range; alias CommonElement = CommonType!(Element, RangeElementType); RebindableOrUnqual!CommonElement extremeElement = seedElement; // if we only have one statement in the loop, it can be optimized a lot better static if (__traits(isSame, map, a => a)) { // direct access via a random access range is faster static if (isRandomAccessRange!Range) { foreach (const i; 0 .. r.length) { if (selectorFun(r[i], extremeElement)) { extremeElement = r[i]; } } } else { while (!r.empty) { if (selectorFun(r.front, extremeElement)) { extremeElement = r.front; } r.popFront(); } } } else { alias MapType = Unqual!(typeof(mapFun(CommonElement.init))); MapType extremeElementMapped = mapFun(extremeElement); // direct access via a random access range is faster static if (isRandomAccessRange!Range) { foreach (const i; 0 .. r.length) { MapType mapElement = mapFun(r[i]); if (selectorFun(mapElement, extremeElementMapped)) { extremeElement = r[i]; extremeElementMapped = mapElement; } } } else { while (!r.empty) { MapType mapElement = mapFun(r.front); if (selectorFun(mapElement, extremeElementMapped)) { extremeElement = r.front; extremeElementMapped = mapElement; } r.popFront(); } } } return extremeElement; } private auto extremum(alias selector = "a < b", Range)(Range r) if (isInputRange!Range && !isInfinite!Range && !is(typeof(unaryFun!selector(ElementType!(Range).init)))) { return extremum!(a => a, selector)(r); } // if we only have one statement in the loop it can be optimized a lot better private auto extremum(alias selector = "a < b", Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seedElement) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void) && !is(typeof(unaryFun!selector(ElementType!(Range).init)))) { return extremum!(a => a, selector)(r, seedElement); } auto minElement(alias map = (a => a), Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { return extremum!map(r); } auto minElement(alias map = (a => a), Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void)) { return extremum!map(r, seed); } auto maxElement(alias map = (a => a), Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { return extremum!(map, "a > b")(r); } auto maxElement(alias map = (a => a), Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void)) { return extremum!(map, "a > b")(r, seed); } } // popcnt with ulongs was added in D 2.071.0 static if (__VERSION__ < 2071) { ulong popcnt(ulong x) { x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L); x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L); x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL); x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL); x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL); x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL); return x; } }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); void main() { long l, r, d; scan(l, r, d); long cnt; foreach (i; l .. r + 1) { if (i % d == 0) { cnt += 1; } } writeln(cnt); } 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; import std.numeric; void main() { auto n = readln.chomp.to!int; auto a = readln.chomp.split.to!(long[]); auto lsum = a[0]; auto rsum = a.sum - lsum; auto res = abs(lsum - rsum).to!long; foreach (i; 1..n-1) { lsum += a[i]; rsum -= a[i]; auto d = abs(lsum - rsum).to!long; if (d < res) { res = d; } } res.writeln; }
D
void main() { int[] tmp = readln.split.to!(int[]); int a = tmp[0], b = tmp[1], c = tmp[2], d = tmp[3]; max(a*b, c*d).writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.string,std.range, std.conv, std.array, std.algorithm, std.math, std.typecons, std.container, std.datetime; void main() { auto S = readln.chomp; string result; foreach (s; S) { final switch (s) { case '0': case '1': result ~= s; break; case 'B': if (result.length > 0) result = result[0..$-1]; break; } } writeln(result); }
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(){ while(true){ int h,w; string[] map; auto s = readln().split().to!(int[]); h = s[0]; w = s[1]; if(h==0&&w==0) break; for(int i=0;i<h;i++){ map ~= readln().chomp(); } int x,y; int[101][101] used; while(true){ used[y][x] = true; if(map[y][x] == '>') x++; else if(map[y][x] == '<') x--; else if(map[y][x] == '^') y--; else if(map[y][x] == 'v') y++; else if(map[y][x] == '.') { writeln(x," ",y); break; } if(used[y][x]){ writeln("LOOP"); break; } } } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons; import std.math, std.numeric; import core.bitop; void main() { int n; scan(n); auto a = iota(n).map!(i => readln.chomp.to!int).array; auto on = new bool[](32); int xs; foreach (i ; 0 .. n) { on[bsf(a[i])] = 1; xs ^= a[i]; } int ans; foreach_reverse (i ; 0 .. 32) { if ((xs >> i) & 1) { if (on[i]) { xs ^= (1<<i) - 1; ans++; } else { writeln(-1); return; } } } writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; void main() { auto N = readln().split()[0].to!int(); int[] h = readln().split().map!(x => x.to!int())().array; writeln(solve(h)); } int solve(int[] h) { if (h.length == 0) return 0; if (h.length == 1) return h[0]; auto lowest_i = h.length - h.minPos().length, lowest = h[lowest_i]; h = h.map!(x => x-lowest).array; return lowest + solve(h[0..lowest_i]) + solve(h[lowest_i+1 .. $]); }
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, k; scan(n, k); auto a = new int[][](2*k + 1, 2*k + 1); foreach (i ; 0 .. n) { int xi, yi; char ci; scan(xi, yi, ci); if (ci == 'W') yi += k; xi %= 2*k; yi %= 2*k; a[xi + 1][yi + 1]++; } debug { //writefln("%(%s\n%)", a); writeln; } foreach (i ; 1 .. 2*k + 1) { foreach (j ; 1 .. 2*k + 1) { a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1]; } } debug { //writefln("%(%s\n%)", a); } int query(int xs, int xt, int ys, int yt) { if (xt == 0) xt = 2*k; if (yt == 0) yt = 2*k; int res; if (xs < xt && ys < yt) { res = a[xt][yt] - a[xs][yt] - a[xt][ys] + a[xs][ys]; } else if (xs >= xt && ys < yt) { res = a[xt][yt] - a[xt][ys]; res += a[2*k][yt] - a[xs][yt] - a[2*k][ys] + a[xs][ys]; } else if (xs < xt && ys >= yt) { res = a[xt][yt] - a[xs][yt]; res += a[xt][2*k] - a[xt][ys] - a[xs][2*k] + a[xs][ys]; } else { res = a[xt][yt]; res += a[xt][2*k] - a[xt][ys]; res += a[2*k][yt] - a[xs][yt]; res += a[2*k][2*k] - a[xs][2*k] - a[2*k][ys] + a[xs][ys]; } return res; } int ans; foreach (x ; 0 .. k) { foreach (y ; 0 .. k) { int t = query(x, x + k, y, y + k); t += query(x + k, x, y + k, y); debug { writeln("t: ", t); } ans = max(ans, t, n - t); } } writeln(ans); } void yes(bool b) {writeln(b ? "Yes" : "No");} void YES(bool b) {writeln(b ? "YES" : "NO");} T[] readArr(T)() {return readln.split.to!(T[]);} 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
module main; import core.stdc.stdio; import std.algorithm.sorting; int gcd (int a, int b) { if (b == 0) return a; else return gcd (b, a % b); } int main(string[] argv) { int n; scanf("%d", &n); int [] a = new int[n]; for(int i = 0; i < n; i++) scanf("%d", &a[i]); a.sort(); int small = a[1] - a[0]; for(int i = 1; i < n - 1; i++) small = gcd(small, a[i + 1] - a[i]); printf("%d", (a[n - 1] - a[0]) / small - n + 1); return 0; }
D
import std.stdio; import std.algorithm; import std.string; import std.range; import std.array; import std.conv; import std.complex; import std.math; int calc2(int n) { if(n <= 1000) { return n+1; } else { return 2000-n+1; } } void main() { while(!stdin.eof()){ auto s = readln().strip(); if(s.empty()) break; int n = to!int(s); int sum; for(int i = max(0, n-2000); i <= min(n, 2000); ++i) { sum += calc2(i)*calc2(n-i); } writeln(sum); } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { int a, b, c; scan(a, b, c); writeln(a + b >= c ? "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
void main() { long n, m; rdVals(n, m); long[] a = new long[m], b = new long[m]; foreach (i; 0 .. m) { rdVals(a[i], b[i]); --a[i], --b[i]; } auto uf = UnionFind(n); long[] result = new long[m]; result[$-1] = (n * (n - 1)) >> 1; foreach_reverse (i; 1 .. m) { result[i-1] = result[i]; if (!uf.same(a[i], b[i])) { result[i-1] -= uf.size(a[i]) * uf.size(b[i]); uf.unite(a[i], b[i]); } } foreach (x; result) { x.writeln; } } struct UnionFind { long[] par, cnt; this(long n) { par.length = n, cnt.length = n; foreach (i; 0 .. n) { par[i] = i; } cnt[] = 1L; } long root(long x) { if (par[x] == x) return x; else return par[x] = root(par[x]); } bool same(long x, long y) { return root(x) == root(y); } void unite(long x, long y) { x = root(x), y = root(y); if (x == y) return; if (cnt[x] > cnt[y]) swap(x, y); cnt[y] += cnt[x]; par[x] = y; } long size(long x) { return cnt[root(x)]; } } 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.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; int[string] ss; foreach (_; 0..N) ++ss[readln.chomp]; int max_n; foreach (_, n; ss) max_n = max(max_n, n); string[] rs; foreach (s, n; ss) if (n == max_n) rs ~= s; sort(rs); foreach (r; rs) writeln(r); }
D
void main() { problem(); } void problem() { const MAX = 10L.pow(18); auto N = scan!int; auto A = scan!ulong(N); long solve() { ulong multi = 1; real floatMulti = 1; foreach(a; A) { floatMulti *= cast(real)a; multi *= a; } return floatMulti > MAX ? -1 : multi; } 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; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N, K; scan(N, K); auto s = new long[](N + 2); foreach (i; 0 .. N + 1) { s[i + 1] += s[i] + i; } long ans; foreach (i; K .. N + 1) { ans = (ans + (s[$ - 1] - s[$ - i - 1]) - (s[i]) + 1) % MOD; } writeln(ans + 1); }
D
import std.algorithm; import std.conv; import std.math; 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; int balance = 0; int lo = 0; long res = s.length; foreach (i, char c; s) { balance += (c == '+') ? +1 : -1; if (lo > balance) { lo = balance; res += i + 1; } } writeln (res); } }
D
import std.stdio, std.string, std.array, std.conv, std.algorithm; void main(){ int[] xs = readln.chomp.split.map!(to!int).array; int x = xs[0], y = xs[1]; writeln(x + y / 2); }
D
module Main; import std.stdio; import std.string; import std.conv; import std.array; import std.range; import std.algorithm; alias map!(to!string) stringize; void main() { string input; while ((input = readln.chomp).length != 0) { int[] tehai = new int[](9); int[] yama = new int[](9); int[] ans; fill(yama, 4); foreach (i; 0..13) { int index = (input[i] - '0').to!int; tehai[index-1]++; yama[index-1]--; } ans = agari(tehai, yama); if (ans.length == 0) { writeln(0); } else { writeln(ans.stringize.join(" ")); } } } int[] agari(int[] tehai, int[] yama) { int[] ans; foreach (i; 0..tehai.length) { if (yama[i] == 0) continue; tehai[i]++; foreach (j; 0..tehai.length) { if (tehai[j] < 2) continue; tehai[j] -= 2; if (yaku(tehai, 0)) ans ~= (i + 1).to!int; tehai[j] += 2; } tehai[i]--; } return ans; } bool yaku(int[] haipai, int num_yaku) { if (num_yaku >= 4) return true; int[] tehai; foreach (i; 0..haipai.length) { /* 順子が成立するかの判定 */ if (i >= 2 && haipai[i - 2] > 0 && haipai[i - 1] > 0 && haipai[i] > 0) { tehai = haipai.dup; tehai[i - 2]--; tehai[i - 1]--; tehai[i]--; return yaku(tehai, ++num_yaku); } /* 刻子が成立するかの判定 */ if (haipai[i] >= 3) { tehai = haipai.dup; tehai[i] -= 3; return yaku(tehai, ++num_yaku); } } return false; }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; void main() { auto s = readln.chomp.split.map!(to!int).array; if (s[0] * s[1] < s[2]) writeln(s[0] * s[1]); else s[2].writeln; }
D
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; immutable long mod = 10L^^9 + 7L; long n, m; void main() { scan(n, m); if (n < m) swap(n, m); if (n == m) { long ans = 1; foreach (i ; 1 .. n + 1) { ans *= (1L * i * i) % mod; ans %= mod; } (ans *= 2) %= mod; writeln(ans); } else if (n == m + 1) { long ans = 1; foreach (i ; 1 .. n) { ans *= (1L * i * i) % mod; ans %= mod; } (ans *= n) %= mod; writeln(ans); } else { writeln(0); } } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); }
D
import std.stdio; import std.algorithm; import std.conv; import std.array; import std.string; import std.math; import std.functional; import std.range; import std.typecons; import std.format; void main(string[] args) { readln.solve.writeln; } auto solve(string line1) { auto _ = line1.chomp.split.map!(to!int).array; int N = _[0]; int A = _[1]; int B = _[2]; long res = 0; foreach(d1; 0..2) foreach(d2; 0..10) foreach(d3; 0..10) foreach(d4; 0..10) foreach(d5; 0..10) { auto dsum = d1 + d2 + d3 + d4 + d5; auto num = 10000*d1 + 1000*d2 + 100*d3 + 10*d4 + d5; if (A <= dsum && dsum <= B && num <= N) { res += num; } } return res; } unittest { assert(solve("100 4 16") == 4554, "1"); assert(solve("10 1 2") == 13, "2"); assert(solve("20 2 5") == 84, "3"); }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; bool calc(int h, int w, string[] ss) { foreach (r; 0..h) { foreach (c; 0..w) { if (ss[r][c] == '#') { int k = 1; if (r > 0 && ss[r-1][c] == '#') k++; if (r < h-1 && ss[r+1][c] == '#') k++; if (c > 0 && ss[r][c-1] == '#') k++; if (c < w-1 && ss[r][c+1] == '#') k++; if (k == 1) return false; } } } return true; } void main() { int h, w; scan(h, w); string[] ss; foreach (_; 0..h) ss ~= read!string; writeln(calc(h, w, ss) ? "Yes" : "No"); } void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int;
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; immutable long MOD = 10^^9 + 7; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!int).array; auto cnt0 = new long[](N + 1); auto cnt1 = new long[](N + 1); long ans = 1; foreach (a; A) { if (a == 0) { cnt0[0] += 1; } else if (cnt0[a - 1] > 0) { ans = ans * (cnt0[a - 1]) % MOD; cnt0[a - 1] -= 1; cnt0[a] += 1; } else { writeln(0); return; } cnt1[a] += 1; } long hoge = A.map!(a => a == 0).sum; hoge = hoge == 1 ? 3 : 6; if (cnt1.map!(a => a > 3).any) { writeln(0); return; } ans = ans * hoge % MOD; 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; long N, cnt; bool ask(long n) { writeln("? ", n); stdout.flush; cnt += 1; debug { string sn = n.to!string; string sN = N.to!string; return (n <= N && sn <= sN) || (n > N && sn > sN); } else { return readln.chomp == "Y"; } } void main() { debug {N = readln.chomp.to!long;} long x = 10; for (; x <= 10^^9 && ask(x); x *= 10) {} if (x == 10L^^10) { // N = 1, 10, 100, ... x = 2; for (; x <= 2 * 10L^^9 && !ask(x); x *= 10) {} writeln("! ", x / 2); } else { long hi = x - 1; long lo = x / 10; while (hi - lo > 1) { long mid = (hi + lo) / 2; if (ask(mid * 10L^^9)) hi = mid; else lo = mid; } writeln("! ", hi); } debug {cnt.writeln;} }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop; // dfmt on void main() { long N, X; scan(N, X); auto L = aryread(); long x = 0; long ans; foreach (l; L) { x = x + l; ans += (x <= X); } writeln(ans + 1); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nx = readln.split.to!(long[]); auto N = nx[0]; auto x = nx[1]; auto as = readln.split.to!(long[]); long r; if (as[0] > x) { r += as[0] - x; as[0] = x; } foreach (i; 1..N) { auto s = as[i-1] + as[i]; if (s > x) { r += s - x; as[i] -= s - x; } } writeln(r); }
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } /* ナップサック問題 xs[i][w]: i個目より前までで、重さw以下とした場合の価値の最大値 xs[0][w] = 0 xs[i][w] = (w - ws[i] >= 0)? xs[i - 1][w - ws[i]] + vs[i] : xs[i - 1][w] */ void main(){ long n = read.to!long; long npw = read.to!long; long[] ws, vs; foreach(i; 0 .. n){ ws ~= read.to!long; vs ~= read.to!long; } long[][] xs = [[]]; foreach(w; 0 .. npw + 1) xs[0] ~= 0; foreach(i; 0 .. n){ long[] q = []; foreach(w; 0 .. npw + 1){ q ~= max( ((w - ws[i] >= 0) ? xs[$ - 1][w - ws[i]] + vs[i] : 0), xs[$ - 1][w] ); debug writeln("q:", q, " w:", w, " i:", i); } xs ~= q; } xs[n][npw].writeln; }
D