code
stringlengths
4
1.01M
language
stringclasses
2 values
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { writeln((lread() - lread()) % lread()); }
D
import std.stdio; import std.array; import std.conv; import std.string; import std.algorithm; void main() { auto n = map!(to!int)(readln.strip.split); int[100][100] v1; int[100] v2; foreach(i;0..n[0]) foreach(int j,string e; readln.strip.split) v1[i][j] = e.to!int; foreach(i;0..n[1]) v2[i] = readln.strip.to!int; foreach(i;0..n[0]) { int sum = 0; foreach(j;0..n[1]) { sum += v1[i][j] * v2[j]; } writeln(sum); } }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; long n, m; rd(n, m); long ans = 1; for (long k = 1; k * k <= m; k++) { if (m % k == 0) { if (n * k <= m) { ans = max(ans, k); } if (n * (m / k) <= m) { ans = max(ans, m / k); } } } writeln(ans); } void rd(T...)(ref T x) { import std.stdio : readln; import std.string : split; import std.conv : to; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
D
import std.stdio; import std.string; void main() { char[] s = readln.chomp.dup; char[] t = readln.chomp.dup; string ans = "No"; foreach (i; 0..s.length){ s = s[$ - 1] ~ s[0..$ - 1]; if (s == t){ ans = "Yes"; break; } } writeln(ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; int[10^^5] AS; int[int] RI; void main() { auto N = readln.chomp.to!int; foreach (i; 0..N) { auto A = readln.chomp.to!int; AS[i] = A; RI[A] = i; } sort(AS[0..N]); int c; foreach (i; 0..N) { if (i%2 != RI[AS[i]]%2) ++c; } writeln(c/2); }
D
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.typecons, std.range, std.container, std.math, std.algorithm.searching, std.functional,std.mathspecial; void main(){ writeln(totalSum(readln.chomp)); } long totalSum(string str){ long total=0; foreach(i,s;str){ auto num=[s].to!long; auto headPattern=2^^i; foreach(k;0..str.length-i){ auto tailLength=str.length-i-1-k; auto tailPattern=tailLength==0?1:2^^(tailLength-1); total+=num*10^^k*headPattern*tailPattern; } } return total; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto abk = readln.split.to!(int[]); auto A = abk[0]; auto B = abk[1]; auto K = abk[2]; foreach_reverse (k; 1..101) { if (A % k == 0 && B % k == 0 && --K == 0) { writeln(k); return; } } }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; import std.typecons; import std.bigint; // import dcomp.foundation, dcomp.scanner; // import dcomp.container.deque; int main() { auto sc = new Scanner(stdin); long n, m, k; sc.read(n, m, k); m++; long[] a = new long[n]; a.each!((ref x) => sc.read(x)); long[] dp = a.dup; long[] ndp = new long[n]; auto deq = Deque!int(); foreach (ph; 2..k+1) { ndp[] = -(10L^^18); deq.clear(); foreach (i; 0..n) { if (deq.length && deq.front == i-m) { deq.removeFront(); } if (deq.length) { ndp[i] = dp[deq.front] + ph * a[i]; } while (deq.length && dp[deq.back] <= dp[i]) { deq.removeBack(); } deq.insertBack(i.to!int); } dp[] = ndp[]; } writeln(dp.fold!max); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; //fold(for old compiler) static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } unittest { import std.stdio; auto l = [1, 2, 3, 4, 5]; assert(l.fold!"a+b"(10) == 25); } } version (X86) static if (__VERSION__ < 2071) { int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/deque.d */ // module dcomp.container.deque; struct Deque(T) { import core.exception : RangeError; import core.memory : GC; import std.range : ElementType, isInputRange; import std.traits : isImplicitlyConvertible; struct Payload { T *d; size_t st, length, cap; @property bool empty() const { return length == 0; } alias opDollar = length; ref inout(T) opIndex(size_t i) inout { version(assert) if (length <= i) throw new RangeError(); return d[(st+i >= cap) ? (st+i-cap) : st+i]; } private void expand() { import std.algorithm : max; assert(length == cap); auto nc = max(size_t(4), 2*cap); T* nd = cast(T*)GC.malloc(nc * T.sizeof); foreach (i; 0..length) { nd[i] = this[i]; } d = nd; st = 0; cap = nc; } void clear() { st = length = 0; } void insertFront(T v) { if (length == cap) expand(); if (st == 0) st += cap; st--; length++; this[0] = v; } void insertBack(T v) { if (length == cap) expand(); length++; this[length-1] = v; } void removeFront() { assert(!empty, "Deque.removeFront: Deque is empty"); st++; length--; if (st == cap) st = 0; } void removeBack() { assert(!empty, "Deque.removeBack: Deque is empty"); length--; } } struct RangeT(A) { alias T = typeof(*(A.p)); alias E = typeof(A.p.d[0]); T *p; size_t a, b; @property bool empty() const { return b <= a; } @property size_t length() const { return b-a; } @property RangeT save() { return RangeT(p, a, b); } @property RangeT!(const A) save() const { return typeof(return)(p, a, b); } alias opDollar = length; @property ref inout(E) front() inout { return (*p)[a]; } @property ref inout(E) back() inout { return (*p)[b-1]; } void popFront() { version(assert) if (empty) throw new RangeError(); a++; } void popBack() { version(assert) if (empty) throw new RangeError(); b--; } ref inout(E) opIndex(size_t i) inout { return (*p)[i]; } RangeT opSlice() { return this.save; } RangeT opSlice(size_t i, size_t j) { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } RangeT!(const A) opSlice() const { return this.save; } RangeT!(const A) opSlice(size_t i, size_t j) const { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } } alias Range = RangeT!Deque; alias ConstRange = RangeT!(const Deque); alias ImmutableRange = RangeT!(immutable Deque); Payload *p; private void I() { if (!p) p = new Payload(); } private void C() const { version(assert) if (!p) throw new RangeError(); } //some value this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) {I; p = new Payload(); foreach (v; values) { insertBack(v); } } //range this(Range)(Range r) if (isInputRange!Range && isImplicitlyConvertible!(ElementType!Range, T) && !is(Range == T[])) {I; p = new Payload(); foreach (v; r) { insertBack(v); } } @property bool empty() const { return (!p || p.empty); } @property size_t length() const { return (p ? p.length : 0); } alias opDollar = length; ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; } ref inout(T) front() inout {C; return (*p)[0]; } ref inout(T) back() inout {C; return (*p)[$-1]; } void clear() { if (p) p.clear(); } void insertFront(T v) {I; p.insertFront(v); } void insertBack(T v) {I; p.insertBack(v); } void removeFront() {C; p.removeFront(); } void removeBack() {C; p.removeBack(); } Range opSlice() {I; return Range(p, 0, length); } } unittest { import std.algorithm : equal; import std.range.primitives : isRandomAccessRange; import std.container.util : make; auto q = make!(Deque!int); assert(isRandomAccessRange!(typeof(q[]))); //insert,remove assert(equal(q[], new int[](0))); q.insertBack(1); assert(equal(q[], [1])); q.insertBack(2); assert(equal(q[], [1, 2])); q.insertFront(3); assert(equal(q[], [3, 1, 2]) && q.front == 3); q.removeFront; assert(equal(q[], [1, 2]) && q.length == 2); q.insertBack(4); assert(equal(q[], [1, 2, 4]) && q.front == 1 && q.back == 4 && q[$-1] == 4); q.insertFront(5); assert(equal(q[], [5, 1, 2, 4])); //range assert(equal(q[][1..3], [1, 2])); assert(equal(q[][][][], q[])); //const range const auto rng = q[]; assert(rng.front == 5 && rng.back == 4); //reference type auto q2 = q; q2.insertBack(6); q2.insertFront(7); assert(equal(q[], q2[]) && q.length == q2.length); //construct with make auto a = make!(Deque!int)(1, 2, 3); auto b = make!(Deque!int)([1, 2, 3]); assert(equal(a[], b[])); } unittest { import std.algorithm : equal; import std.range.primitives : isRandomAccessRange; import std.container.util : make; auto q = make!(Deque!int); q.clear(); assert(equal(q[], new int[0])); foreach (i; 0..100) { q.insertBack(1); q.insertBack(2); q.insertBack(3); q.insertBack(4); q.insertBack(5); assert(equal(q[], [1,2,3,4,5])); q.clear(); assert(equal(q[], new int[0])); } } unittest { Deque!int a; Deque!int b; a.insertFront(2); assert(b.length == 0); } unittest { import std.algorithm : equal; import std.range : iota; Deque!int a; foreach (i; 0..100) { a.insertBack(i); } assert(equal(a[], iota(100))); } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; if (f.eof) return false; line = lineBuf[]; f.readln(line); } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { //string or char[10] etc //todo optimize auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { //static assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); fout.writeln("1 2 3"); fout.writeln("ab cde"); fout.writeln("1.0 1.0 2.0"); fout.close; Scanner sc = new Scanner(File(fileName, "r")); int a; int[2] b; char[2] c; string d; double e; double[] f; sc.read(a, b, c, d, e, f); assert(a == 1); assert(equal(b[], [2, 3])); assert(equal(c[], "ab")); assert(equal(d, "cde")); assert(e == 1.0); assert(equal(f, [1.0, 2.0])); } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File, writeln; import std.datetime; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); foreach (i; 0..1_000_000) { fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2); } fout.close; writeln("Scanner Speed Test(3*1,000,000 int)"); StopWatch sw; sw.start; Scanner sc = new Scanner(File(fileName, "r")); foreach (i; 0..500_000) { int a, b, c; sc.read(a, b, c); assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 500_000..700_000) { int[3] d; sc.read(d); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 700_000..1_000_000) { int[] d; sc.read(d); assert(d.length == 3); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } writeln(sw.peek.msecs, "ms"); }
D
import std.stdio; int main() { for(int i=1; i<=9; i++) { for(int j=1; j<=9; j++) { writeln(i, "x", j, "=", i*j); } } return 0; }
D
import std.stdio, std.algorithm, std.range, std.conv; void main(){ iota(1, 10).map!(a => iota(1, 10).map!(b => text(a, "x", b, "=", a*b)).join("\n")).join("\n").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) { y.modpow(mod - 2); x.modm(y); } void main() { auto X = RD; auto K = RD; auto D = RD; long ans; if (abs(X) / D >= K) { ans = abs(X) - D * K; } else { auto cnt = abs(X) / D; auto y = abs(X) - D * cnt; auto rem = K - cnt; if (rem % 2) ans = abs(y - D); else ans = y; } writeln(ans); stdout.flush; debug readln; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; alias Goods = Tuple!(long, "w", long, "v"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { auto n = lread(); auto b = aryread(); b = b[0] ~ b ~ b[$ - 1]; long ans; foreach(i; iota(n)) { ans += min(b[i], b[i + 1]); } ans.writeln(); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); auto S = new string[](N); auto T = new long[](N); foreach (i; 0 .. N) scan(S[i], T[i]); auto X = sread(); long s; while (S[s] != X) s++; long ans; foreach (i; s + 1 .. N) ans += T[i]; writeln(ans); }
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 x, y; { int[] tmp = readln.chomp.split.map!(to!int).array; x = tmp[0], y = tmp[1]; } int ans1, ans2, ans3, ans4; if(y >= x) ans1 = y - x; else ans1 = int.max; if(y >= -x) ans2 = 1 + (y - -x); else ans2 = int.max; if(-y >= x) ans3 = 1 + (-y - x); else ans3 = int.max; if(-y >= -x) ans4 = 2 + (-y - -x); else ans4 = int.max; int ans = min(ans1, ans2, ans3, ans4); ans.writeln; }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std; // dfmt on void main() { long R, G, B, N; scan(R, G, B, N); long ans; foreach (r; 0 .. N*2) foreach (g; 0 .. N*2) { long remain = N - R * r - G * g; if (remain < 0) continue; if (remain % B == 0) ans++; } writeln(ans); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto a = RD!string; foreach (i; 0..2^^3) { auto arr = new int[](3); arr[0] = i & 0b100; arr[1] = i & 0b010; arr[2] = i & 0b001; long cnt; bool ok = true; foreach (c; a) { auto num = c - 'A'; if (arr[num]) ++cnt; else --cnt; if (cnt < 0) { ok = false; break; } } if (!ok) continue; if (cnt == 0) { ans[ti] = true; break; } } } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush; debug readln; }
D
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } void main() { try { for (; ; ) { const S = readToken(); writeln(S[$ - 1] % 2); } } catch (EOFException e) { } }
D
// xxxxx import std.random; import std.stdio; void main () { rndGen.seed (unpredictableSeed); writeln (uniform (0, 2) ? "Even" : "Odd"); }
D
// Try Codeforces // author: Leonardone @ NEETSDKASU import std.stdio, std.array, std.conv, std.string, std.algorithm; auto gets() { return readln().chomp(); } T[] getVals(T)() { return to!(T[])(readln().chomp().split(" ")); } void main() { auto xs = getVals!int(); auto n = xs[0], k = xs[1]; auto baloon = gets(); int[char] mp; foreach (b; baloon) { mp[b]++; } int x = 0; foreach (int c; mp) { x = max(x, c); } writeln(x <= k ? "YES" : "NO"); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } //long mod = 10^^9 + 7; long mod = 998244353; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto K = RD!int; int a, b; for (int i = 5; i*i <= K; ++i) { if (K % i == 0) { if (K / i >= 5) { a = i; b = K / i; break; } } } if (a == 0) writeln(-1); else { auto str = "aiueo"; foreach (i; 0..a) { foreach (j; 0..b) { write(str[((i%5)+j)%5]); } } writeln(); } stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto t = RD!int; auto ans = new string[](t); foreach (i; 0..t) { auto s = RD!string; foreach (j; 0..s.length) { if (s[j] == '?') { int num1 = -1, num2 = -1; if (j != 0) num1 = ans[i][$-1] - 'a'; if (j != s.length-1) if (s[j+1] != '?') num2 = s[j+1] - 'a'; int num3; if (num1 != -1) { num3 = (num1+1) % 3; } if (num3 == num2) { num3 = (num2+1) % 3; } ans[i] ~= cast(char)('a' + num3); } else ans[i] ~= s[j]; } bool ok = true; foreach (j; 1..ans[i].length) { if (ans[i][j] == ans[i][j-1]) { ok = false; break; } } debug writeln(ans[i]); if (!ok) ans[i].length = 0; } foreach (e; ans) { if (e.empty) writeln(-1); else writeln(e); } stdout.flush; debug readln; }
D
import core.thread; import std.conv, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex; // Input class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { tokens = readln.split; if (stdin.eof) throw new EOFException; } auto token = tokens[0]; tokens.popFront; return token; } int readInt() { return to!int(readToken); } long readLong() { return to!long(readToken); } real readReal() { return to!real(readToken); } // chmin/chmax void chmin(T)(ref T t, in T f) { if (t > f) t = f; } void chmax(T)(ref T t, in T f) { if (t < f) t = f; } // Pair struct Pair(S, T) { S x; T y; int opCmp( const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; } int opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; } string toString() const { return "(" ~ to!string(x) ~ ", " ~ to!string(y) ~ ")"; } } auto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); } // Array int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = as.length; for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; } int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a < val); }); } int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); } T[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; } int N, P; string S; string solve() { if (P == 1) { if (N == 1) { if (S < "a") return "a"; return ""; } else { return ""; } } else if (P == 2) { if (N == 1) { if (S < "a") return "a"; if (S < "b") return "b"; return ""; } else if (N == 2) { if (S < "ab") return "ab"; if (S < "ba") return "ba"; return ""; } else { return ""; } } else { foreach_reverse (k; 0 .. N) { string ret = S[0 .. k]; bool ok = true; foreach (i; k .. N) { bool found; foreach (x; 0 .. P) { if (i == k && x <= S[k] - 'a') { continue; } if (i >= 1 && ret[i - 1] - 'a' == x) { continue; } if (i >= 2 && ret[i - 2] - 'a' == x) { continue; } found = true; ret ~= cast(char)('a' + x); break; } if (!found) { ok = false; break; } } if (ok) { return ret; } } return ""; } } void main(string[] args) { try { for (; ; ) { N = readInt; P = readInt; S = readToken; string res = solve; if (res == "") { writeln("NO"); } else { writeln(res); } } } catch (EOFException) {} }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nk = readln.split.to!(int[]); auto N = nk[0]; auto K = nk[1]; long[] as; foreach (i, a; readln.split) { as ~= a.to!long; if (i) as[i] += as[i-1]; } long[long] cs; cs[0] = 1; long r; foreach (i, a; as) { if (i >= K-1) { if (i-K == -1) { --cs[0]; } else { auto k = (as[i-K] - (i.to!long-K) - 1) % K; --cs[k]; } } auto j = (a - i.to!long - 1) % K; if (j in cs) r += cs[j]; ++cs[j]; } writeln(r); }
D
/+ dub.sdl: name "B" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; // import dcomp.modint; int main() { auto sc = new Scanner(stdin); int n; long a, b, c, d; sc.read(n, a, b, c, d); n--; foreach (i; 0..n+1) { //up i, down n-i long ma = a + d*i - c*(n-i); long mi = a + c*i - d*(n-i); if (mi <= b && b <= ma) { writeln("YES"); return 0; } } writeln("NO"); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/modint.d */ // module dcomp.modint; // import dcomp.numeric.primitive; struct ModInt(uint MD) if (MD < int.max) { import std.conv : to; uint v; this(int v) {this(long(v));} this(long v) {this.v = (v%MD+MD)%MD;} static auto normS(uint x) {return (x<MD)?x:x-MD;} static auto make(uint x) {ModInt m; m.v = x; return m;} auto opBinary(string op:"+")(ModInt r) const {return make(normS(v+r.v));} auto opBinary(string op:"-")(ModInt r) const {return make(normS(v+MD-r.v));} auto opBinary(string op:"*")(ModInt r) const {return make((long(v)*r.v%MD).to!uint);} auto opBinary(string op:"/")(ModInt r) const {return this*inv(r);} auto opOpAssign(string op)(ModInt r) {return mixin ("this=this"~op~"r");} static ModInt inv(ModInt x) {return ModInt(extGcd!int(x.v, MD)[0]);} string toString() {return v.to!string;} } struct DModInt(string name) { import std.conv : to; static uint MD; uint v; this(int v) {this(long(v));} this(long v) {this.v = ((v%MD+MD)%MD).to!uint;} static auto normS(uint x) {return (x<MD)?x:x-MD;} static auto make(uint x) {DModInt m; m.MD = MD; m.v = x; return m;} auto opBinary(string op:"+")(DModInt r) const {return make(normS(v+r.v));} auto opBinary(string op:"-")(DModInt r) const {return make(normS(v+MD-r.v));} auto opBinary(string op:"*")(DModInt r) const {return make((long(v)*r.v%MD).to!uint);} auto opBinary(string op:"/")(DModInt r) const {return this*inv(r);} auto opOpAssign(string op)(DModInt r) {return mixin ("this=this"~op~"r");} static DModInt inv(DModInt x) { return DModInt(extGcd!int(x.v, MD)[0]); } string toString() {return v.to!string;} } template isModInt(T) { const isModInt = is(T : ModInt!MD, uint MD) || is(S : DModInt!S, string s); } T[] factTable(T)(size_t length) if (isModInt!T) { import std.range : take, recurrence; import std.array : array; return T(1).recurrence!((a, n) => a[n-1]*T(n)).take(length).array; } T[] invFactTable(T)(size_t length) if (isModInt!T) { import std.algorithm : map, reduce; import std.range : take, recurrence, iota; import std.array : array; auto res = new T[length]; res[$-1] = T(1) / iota(1, length).map!T.reduce!"a*b"; foreach_reverse (i, v; res[0..$-1]) { res[i] = res[i+1] * T(i+1); } return res; } T[] invTable(T)(size_t length) if (isModInt!T) { auto f = factTable!T(length); auto invf = invFactTable!T(length); auto res = new T[length]; foreach (i; 1..length) { res[i] = invf[i] * f[i-1]; } return res; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } version (X86) static if (__VERSION__ < 2071) { import core.bitop : bsf, bsr, popcnt; int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; if (f.eof) return false; line = lineBuf[]; f.readln(line); } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/numeric/primitive.d */ // module dcomp.numeric.primitive; import std.traits; import std.bigint; T pow(T, U)(T x, U n) if (!isFloatingPoint!T && (isIntegral!U || is(U == BigInt))) { return pow(x, n, T(1)); } T pow(T, U)(T x, U n, T e) if (isIntegral!U || is(U == BigInt)) { while (n) { if (n & 1) e *= x; x *= x; n /= 2; } return e; } T lcm(T)(in T a, in T b) { import std.numeric : gcd; return a / gcd(a,b) * b; } T[3] extGcd(T)(in T a, in T b) if (!isIntegral!T || isSigned!T) { if (b==0) { return [T(1), T(0), a]; } else { auto e = extGcd(b, a%b); return [e[1], e[0]-a/b*e[1], e[2]]; } }
D
// Vicfred // https://atcoder.jp/contests/abc162/tasks/abc162_b // simulation import std.conv; import std.stdio; import std.string; void main() { int n = readln.chomp.to!int; long sum = 0; foreach(i; 1..n+1) { if(i%3 != 0 && i%5 != 0) sum += i; } sum.writeln; }
D
void main() { long n, m, k; rdVals(n, m, k); foreach (i; 0 .. n+1) { foreach (j; 0 .. m+1) { long black = i * m + j * n -2 * i * j; if (black == k) { "Yes".writeln; return; } } } "No".writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio; import std.string; import std.format; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.concurrency; import std.traits; import std.uni; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; enum long INF = long.max/3; enum long MOD = 10L^^9+7; void main() { long x = readln.split.reduce!"a~b".to!long; foreach(i; 0..x) { if (i*i == x) { "Yes".writeln; return; } } "No".writeln; } // ---------------------------------------------- void times(alias fun)(long n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(long n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { // `(x+y-1)/y` will only work for positive numbers ... T t = x / y; if (t * y < x) t++; return t; } T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { T t = x / y; if (t * y > x) t--; return t; } ref T ch(alias fun, T, S...)(ref T lhs, S rhs) { return lhs = fun(lhs, rhs); } unittest { long x = 1000; x.ch!min(2000); assert(x == 1000); x.ch!min(3, 2, 1); assert(x == 1); x.ch!max(100).ch!min(1000); // clamp assert(x == 100); x.ch!max(0).ch!min(10); // clamp assert(x == 10); } mixin template Constructor() { import std.traits : FieldNameTuple; this(Args...)(Args args) { // static foreach(i, v; args) { foreach(i, v; args) { mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v; } } } void scanln(Args...)(auto ref Args args) { import std.meta; template getFormat(T) { static if (isIntegral!T) { enum getFormat = "%d"; } else static if (isFloatingPoint!T) { enum getFormat = "%g"; } else static if (isSomeString!T || isSomeChar!T) { enum getFormat = "%s"; } else { static assert(false); } } enum string fmt = [staticMap!(getFormat, Args)].join(" "); string[] inputs = readln.chomp.split; foreach(i, ref v; args) { v = inputs[i].to!(Args[i]); } } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { private template RebindableOrUnqual(T) { static if (is(T == class) || is(T == interface) || isDynamicArray!T || isAssociativeArray!T) alias RebindableOrUnqual = Rebindable!T; else alias RebindableOrUnqual = Unqual!T; } private auto extremum(alias map, alias selector = "a < b", Range)(Range r) if (isInputRange!Range && !isInfinite!Range && is(typeof(unaryFun!map(ElementType!(Range).init)))) in { assert(!r.empty, "r is an empty range"); } body { alias Element = ElementType!Range; RebindableOrUnqual!Element seed = r.front; r.popFront(); return extremum!(map, selector)(r, seed); } private auto extremum(alias map, alias selector = "a < b", Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seedElement) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void) && is(typeof(unaryFun!map(ElementType!(Range).init)))) { alias mapFun = unaryFun!map; alias selectorFun = binaryFun!selector; alias Element = ElementType!Range; alias CommonElement = CommonType!(Element, RangeElementType); RebindableOrUnqual!CommonElement extremeElement = seedElement; // if we only have one statement in the loop, it can be optimized a lot better static if (__traits(isSame, map, a => a)) { // direct access via a random access range is faster static if (isRandomAccessRange!Range) { foreach (const i; 0 .. r.length) { if (selectorFun(r[i], extremeElement)) { extremeElement = r[i]; } } } else { while (!r.empty) { if (selectorFun(r.front, extremeElement)) { extremeElement = r.front; } r.popFront(); } } } else { alias MapType = Unqual!(typeof(mapFun(CommonElement.init))); MapType extremeElementMapped = mapFun(extremeElement); // direct access via a random access range is faster static if (isRandomAccessRange!Range) { foreach (const i; 0 .. r.length) { MapType mapElement = mapFun(r[i]); if (selectorFun(mapElement, extremeElementMapped)) { extremeElement = r[i]; extremeElementMapped = mapElement; } } } else { while (!r.empty) { MapType mapElement = mapFun(r.front); if (selectorFun(mapElement, extremeElementMapped)) { extremeElement = r.front; extremeElementMapped = mapElement; } r.popFront(); } } } return extremeElement; } private auto extremum(alias selector = "a < b", Range)(Range r) if (isInputRange!Range && !isInfinite!Range && !is(typeof(unaryFun!selector(ElementType!(Range).init)))) { return extremum!(a => a, selector)(r); } // if we only have one statement in the loop it can be optimized a lot better private auto extremum(alias selector = "a < b", Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seedElement) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void) && !is(typeof(unaryFun!selector(ElementType!(Range).init)))) { return extremum!(a => a, selector)(r, seedElement); } auto minElement(alias map = (a => a), Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { return extremum!map(r); } auto minElement(alias map = (a => a), Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void)) { return extremum!map(r, seed); } auto maxElement(alias map = (a => a), Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { return extremum!(map, "a > b")(r); } auto maxElement(alias map = (a => a), Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void)) { return extremum!(map, "a > b")(r, seed); } } // popcnt with ulongs was added in D 2.071.0 static if (__VERSION__ < 2071) { ulong popcnt(ulong x) { x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L); x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L); x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL); x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL); x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL); x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL); return x; } }
D
import std.stdio; int[][int] graph; bool[int] used; void dependent(int n) { if (!used[n]) { foreach(i; graph[n]) { dependent(i); } writeln(n); used[n] = true; } } void main() { int m, n; scanf("%d\n%d\n", &m, &n); foreach(i; 1..(m + 1)) {graph[i] = []; used[i] = false;} foreach(i; 0..n) { int to, from; scanf("%d %d", &from, &to); graph[to] ~= from; } foreach(i; 1..m) dependent(i); }
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]]; } double norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); } double dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; } //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); } T binarySearch(alias pred, T)(T ok, T ng) { while (abs(ok-ng) > 1) { auto mid = (ok+ng)/2; if (unaryFun!pred(mid)) ok = mid; else ng = mid; } return ok; } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto a = new long[](n); auto b = new long[](n); foreach (i; 0..n) { a[i] = RD; b[i] = RD; } bool f(long x) { long cnt; foreach (i; 0..n) { if (cnt > b[i]) continue; if (x-cnt-1 > a[i]) continue; ++cnt; if (cnt == x) break; } return cnt == x; } ans[ti] = binarySearch!(f)(0, n+1); } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
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() { while (true) { int n, m; scan(n, m); if (n == 0 && m == 0) return; auto t = iota(n).map!(i => readln.chomp.to!int).array; auto h = iota(m).map!(i => readln.chomp.to!int).array; solve(n, m, t, h); } } void solve(int n, int m, int[] t, int[] h) { int ts = t.sum, hs = h.sum; int ps = 10000; int st = -1, sh = -1; foreach (i ; 0 .. n) { foreach (j ; 0 .. m) { auto a = ts - t[i] + h[j]; auto b = hs - h[j] + t[i]; if (a == b && t[i] + h[j] < ps) { st = i, sh = j; ps = t[i] + h[j]; } } } if (st != -1) { writeln(t[st], " ", h[sh]); } else { writeln(-1); } } 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.string, std.conv; void main() { int[] tmp = readln.split.to!(int[]); int x = tmp[0], a = tmp[1]; writeln(x < a ? 0 : 10); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto n = RD!int; auto s = RD!string; long cnt; bool ans = true; foreach (i; 0..n) { if (s[i] == '(') ++cnt; else --cnt; if (cnt < -1) { ans = false; break; } } writeln(ans && cnt == 0 ? "Yes" : "No"); stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional; import std.algorithm, std.container; 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)); } } } T scanElem(T = long)() { string 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); } } } 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; } 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; } void main() { auto N = scanElem; auto M = scanElem; long[] aList = scanArray; long sum; long[long] map; map[0]++; foreach(n; aList) { sum += n; map[sum%M]++; } long res; foreach(k, e; map) { foreach(i; 1..e) { res+=i; } } writeln(res); }
D
import std.stdio; void main(){ auto s=readln(); char x=s[0]; char y=s[2]; if(x<y) writeln("<"); else if(x>y) writeln(">"); else writeln("="); }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); } void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); } string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ long x0 = scan!long, y0 = scan!long, x1 = scan!long, y1 = scan!long; Vector v1 = Vector(x1 - x0, y1 - y0); int q = scan!int; foreach(_; 0 .. q){ Vector v2 = Vector(scan!long - x0, scan!long - y0); string ans; if(v2.square == 0) ans = "ON_SEGMENT"; else if(v2.arg == v1.arg){ if(v2.square > v1.square) ans = "ONLINE_FRONT"; else ans = "ON_SEGMENT"; } else if(v2.arg == -v1.arg) ans = "ONLINE_BACK"; else if(v2.isLeftTo(v1)) ans = "COUNTER_CLOCKWISE"; else ans = "CLOCKWISE"; ans.print; } } struct Vector{ long x, y; Argument arg; long square; real length; this(long x, long y){ this.x = x, this.y = y; if(x != 0 || y != 0) arg = Argument(x, y); square = x * x + y * y; length = square.to!real.sqrt; } bool opEquals(Vector v){ return x == v.x && y == v.y; } Vector opUnary(string s)(){ if(s == "+") return Vector(x, y); if(s == "-") return Vector(-x, -y); assert(0, "Operator unary " ~ s ~ " not implemented"); } Vector opBinary(string s)(Vector v){ if(s == "+") return Vector(x + v.x, y + v.y); if(s == "-") return Vector(x - v.x, y - v.y); assert(0, "Operator " ~ s ~ " not implemented"); } Vector opBinary(string s)(long k){ if(s == "*") return Vector(x * k, y * k); assert(0, "Operator " ~ s ~ " not implemented"); } Vector opBinaryRight(string s)(long k){ if(s == "*") return Vector(k * x, k * y); assert(0, "Operator " ~ s ~ " not implemented"); } int quadrant(){ return arg.quadrant; } bool isLeftTo(Vector v){ return arg.isLeftTo(v.arg); } bool follows(Vector v){ return arg.follows(v.arg); } } struct Argument{ long x, y; this(long x, long y){ assert(x != 0 || y != 0); this.x = x, this.y = y; } bool opEquals(Argument a){ return quadrant == a.quadrant && y * a.x == x * a.y; } Argument opUnary(string s)(){ if(s == "+") return Argument(x, y); if(s == "-") return Argument(-x, -y); assert(0, "Operator unary " ~ s ~ " not implemented"); } Argument opBinary(string s)(Argument a){ if(s == "+") return Argument(x * a.x - y * a.y, y * a.x + x * a.y); if(s == "-") return Argument(x * a.x + y * a.y, y * a.x - x * a.y); assert(0, "Operator " ~ s ~ " not implemented"); } // 象限 // (1, 0) ≦ this < (0, 1) のとき 1 など int quadrant(){ if(x > 0 && y >= 0) return 1; if(x <= 0 && y > 0) return 2; if(x < 0 && y <= 0) return 3; if(x >= 0 && y < 0) return 4; assert(0); } // a から見て this は左側である(aと平行は含まない) bool isLeftTo(Argument a){ return y * a.x - x * a.y > 0; } // (1, 0) から反時計回りに見て this < a である bool follows(Argument a){ int q = this.quadrant, aq = a.quadrant; if(q != aq) return q < aq; else return a.isLeftTo(this); } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.container; import std.array; import std.math; import std.range; void main() { int m = readln.chomp.split.back.to!int; string s = readln.chomp; int l = 0, r = 1; // bool[string] set; bool[ulong[2]] set; auto hasher = new RollingHash(s); foreach (i; 0 .. m) { string v = readln; auto f = function(char c, ref int w) { if (c == '+') { ++w; } else { --w; } }; if (v[0] == 'L') { f(v[1], l); } else { f(v[1], r); } set[hasher.substr(l, r)] = true; // set[s[l..r]] = true; } writeln = set.length; } class RollingHash { enum ulong MOD1 = 1_000_000_007; enum ulong MOD2 = 1_000_000_009; enum ulong B1 = 1_009; enum ulong B2 = 1_007; ulong[] b1s, b2s; ulong[] array1; ulong[] array2; this(string s) { array1 ~= 0; array2 ~= 0; b1s ~= 1; b2s ~= 1; foreach (c; s) { array1 ~= ((array1.back + ulong(c)) * B1) % MOD1; array2 ~= ((array2.back + ulong(c)) * B2) % MOD2; b1s ~= (b1s.back * B1) % MOD1; b2s ~= (b2s.back * B2) % MOD2; } } ulong[2] substr(uint l, uint r) { ulong h1 = (array1[r] - ((b1s[r - l] * array1[l]) % MOD1) + MOD1) % MOD1; ulong h2 = (array2[r] - ((b2s[r - l] * array2[l]) % MOD2) + MOD2) % MOD2; return [h1, h2]; } }
D
void main() { long[] tmp = rdRow; long n = tmp[0], a = tmp[1], b = tmp[2]; long l = b - 1; long r = n - a; long div = (b - a) / 2; long rem = (b - a) % 2; if (rem) { long s = b - a; long t = s / 2 + a; long u = n - b; long v = (n - a - u) / 2 + u + 1; min(l, r, t, v).writeln; } else { min(l, r, div).writeln; } } T rdElem(T = long)() { //import std.stdio : readln; //import std.string : chomp; //import std.conv : to; return readln.chomp.to!T; } alias rdStr = rdElem!string; dchar[] rdDchar() { //import std.conv : to; return rdStr.to!(dchar[]); } T[] rdRow(T = long)() { //import std.stdio : readln; //import std.array : split; //import std.conv : to; return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdElem).array; } T[][] rdMat(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdRow).array; } void wrMat(T = long)(T[][] mat) { //import std.stdio : write, writeln; 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.container; import std.typecons; import std.ascii; import std.uni;
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 H = RD; long ans, cnt = 1; while (H != 0) { ans += cnt; H /= 2; cnt *= 2; } writeln(ans); stdout.flush; debug readln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; immutable long MOD = 10^^9 + 7; void main() { auto N = readln.chomp.to!int; auto C = readln.split.map!(x => x.to!int-1).array; auto G = new int[][](N); foreach (i; 0..N-1) { auto s = readln.split.map!(to!int); G[s[0]-1] ~= s[1]-1; G[s[1]-1] ~= s[0]-1; } auto sub = new int[](N); auto colorsub = new int[](N); colorsub[] = N; void dfs1(int n, int p) { sub[n] = 1; foreach (m; G[n]) if (m != p) { dfs1(m, n); sub[n] += sub[m]; } } dfs1(0, -1); auto ans = new long[](N); auto prev = new int[](N); prev[] = -1; auto tmp = new long[](N); void dfs(int n, int p) { auto c = C[n]; if (prev[c] != -1) { tmp[prev[c]] -= sub[n]; } else { colorsub[c] -= sub[n]; } auto prevprev = prev[c]; prev[c] = n; foreach (m; G[n]) if (m != p) { tmp[n] = sub[m]; dfs(m, n); ans[c] += 1L * tmp[n] * (tmp[n] + 1) / 2; } prev[c] = prevprev; } dfs(0, -1); foreach (i; 0..N) { ans[i] += 1L * colorsub[i] * (colorsub[i] + 1) / 2; } auto S = 1L * N * (N + 1) / 2; ans.each!(a => writeln(S - a)); }
D
import std.algorithm; import std.array; import std.bigint; import std.bitmanip; import std.conv; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; } T[] readToArray(T)() { return readln.split.to!(T[]); } void readInto(T...)(ref T ts) { auto ss = readln.split; foreach(ref t; ts) { t = ss.front.to!(typeof(t)); ss.popFront; } } // 冪乗をmod取りつつ計算 @nogc @safe pure ulong modPow(ulong a, ulong n, ulong m) { ulong r = 1; while (n > 0) { if(n % 2 != 0) r = r * a % m; a = a * a % m; n /= 2; } return r; } // フェルマーの小定理から乗法逆元を計算 // 定理の要請により法は素数 @nogc @safe pure ulong modInv(ulong a, ulong m) { return modPow(a, m-2, m); } // mod取りつつ順列を計算 @nogc @safe pure ulong modPerm(ulong n, ulong k, ulong m) { if (n < k) return 0; ulong r = 1; for (ulong i = n-k+1; i <= n; i++) { r *= i; r %= m; } return r; } // mod取りつつ順列を計算 @nogc @safe pure ulong modFact(ulong n, ulong m) { return modPerm(n, n, m); } // mod取りつつ組み合わせを計算 // modInvを使っているので法は素数 @nogc @safe pure ulong modComb(ulong n, ulong r, ulong m) { return modPerm(n, r, m)*modInv(modFact(r, m), m) % m; } immutable ulong MOD = 1000000007; void main() { ulong n, a, b; readInto(n, a, b); writeln(n*a < b ? n*a : b); }
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; } /* 有向グラフ トポロジカルソートみたいなもの ※トポロジカルソートみたいなものは先日の日経予選にもあり ソース、シンクを考えると、題意のパスはどれかのソースからどれかのシンクに至っている 入次数を調べることでソースたちを見つける 他の点にもすべて入次数を持たせておく ソースから幅優先探索 ソースの値は0とする 自分の子たちに自分の値+1をつける ただしすでに値がある場合は、改善される場合だけ上書き いずれにしてもこれらをされた子は入次数を減らす 入次数が残り0になった子はその子から続きの探索をする */ class Node{ Node[] kids; int parentcount; int value; int id; this(int id){ this.id = id; } } void main(){ int n = read.to!int; int m = read.to!int; Node[] nodes; foreach(i; 0 .. n) nodes ~= new Node(i); foreach(j; 0 .. m){ int x = read.to!int - 1; int y = read.to!int - 1; nodes[x].kids ~= nodes[y]; nodes[y].parentcount += 1; } Node[] queue; int iq; foreach(nd; nodes) if(nd.parentcount == 0){ nd.value = 0; queue ~= nd; } while(iq < queue.length){ Node nd = queue[iq]; foreach(kid; nd.kids){ kid.value = nd.value + 1; kid.parentcount -= 1; if(kid.parentcount == 0) queue ~= kid; } iq ++; } int ans = 0; foreach(nd; nodes) if(ans < nd.value) ans = nd.value; ans.writeln; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; ulong MIDDLE = 100_000; alias Pair = Tuple!(long, "flag", long, "num"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { long h, w; scan(h, w); long min_dif = INF; foreach(i; iota(1, w)) { long split = h * i, rest = h * (w - i); long middle1 = h / 2, middle2 = (w - i) / 2; long right = (w - i) * middle1, left = (w - i) * (h - middle1); long upper = h * middle2, lower = h * (w - i - middle2); auto dif1 = only(split, right, left).reduce!(max) - only(split, right, left).reduce!(min); auto dif2 = only(split, upper, lower).reduce!(max) - only(split, upper, lower).reduce!(min); min_dif = only(dif1, dif2, min_dif).reduce!(min); } foreach(i; iota(1, h)) { long split = w * i, rest = w * (h - i); long middle1 = w / 2, middle2 = (h - i) / 2; long right = (h - i) * middle1, left = (h - i) * (w - middle1); long upper = w * middle2, lower = w * (h - i - middle2); auto dif1 = only(split, right, left).reduce!(max) - only(split, right, left).reduce!(min); auto dif2 = only(split, upper, lower).reduce!(max) - only(split, upper, lower).reduce!(min); min_dif = only(dif1, dif2, min_dif).reduce!(min); } min_dif.writeln(); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { string s = sread(); long[2] cnt; foreach (c; s) { cnt[c / '1']++; } writeln(min(cnt[0], cnt[1]) * 2); }
D
import std.stdio, std.string, std.conv; import std.array, std.algorithm, std.range; void main() { int[string] c; string mw; c[mw]=0; string ml; foreach(w;readln().split()) { if(w in c){ if(++c[w]>c[mw]) mw=w; } else c[w]=1; if(ml.length < w.length) ml=w; } writeln(mw," ",ml); }
D
import std.stdio, std.string, std.conv; void main() { auto input = getStdin!(string[]); foreach (line; input) { auto num = line.split(" ").to!(int[]); int width = num[1]; int height = num[0]; if (width == 0 && height == 0) break; for (int i_h = 0; i_h < height; i_h++) { for (int j_w = 0; j_w < width; j_w++) { if (i_h == 0 || i_h == height - 1) { "#".write; } else { if (j_w == 0 || j_w == width - 1) { "#".write; } else { ".".write; } } if (j_w == width - 1) { "".writeln; } } } "".writeln; } } T getStdin(T)() { string[] cmd; string line; while ((line = chomp(stdin.readln())) != "") cmd ~= line; return to!(T)(cmd); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new int[](t); auto a = new char[][][](t); foreach (ti; 0..t) { auto n = RD!int; auto k = RD!int; a[ti] = new char[][](n, n); ans[ti] = k % n == 0 ? 0 : 2; foreach (i; 0..n) { foreach (j; 0..n) a[ti][i][j] = '0'; } (){ foreach (i; 0..n) { foreach (j; 0..n) { if (k == 0) return; a[ti][j][(i+j)%n] = '1'; --k; } }}(); } foreach (ti, e; ans) { writeln(e); foreach (ee; a[ti]) writeln(ee); } stdout.flush; debug readln; }
D
import std.stdio, std.string, std.array, std.conv; void main() { int n = readln.chomp.to!int; int[] d = readln.chomp.split.to!(int[]); int sum = 0; foreach (i; 0 .. n-1) { foreach (j; i+1 .. n) { sum += d[i] * d[j]; } } sum.writeln; }
D
import std.stdio; import std.string; import std.algorithm; import std.conv; void main() { int N = readln.chomp.to!int; auto A = readln.chomp.split.map!(to!int); int ans = (1e9).to!int; foreach(a; A) { int tmp; while (a % 2 == 0 && a > 0) { tmp++; a /= 2; } ans = min(ans, tmp); } ans.writeln; }
D
void main() { long n = rdElem; long[] h = rdRow; bool ok = true; long cnt = 1; foreach (i; 1 .. n) { if (h[i] > h[i-1]) ++cnt; else if (h[i] < h[i-1]) { if (h[i-1] - h[i] > cnt) { "No".writeln; return; } cnt = 0; } } "Yes".writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; void main() { int n; scan(n); writeln(n & 1 ? n * 2 : n); } 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 = readln.chomp.to!long; long[] a = readln.split.to!(long[]); long num = 1; long cnt; foreach (x; a) { if (x == num) ++num; else ++cnt; } writeln(cnt != n ? cnt : -1); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container; /* N:0 W:1 S:2 E:3 */ void main() { auto hwk = readln.split.to!(int[]); auto H = hwk[0]; auto W = hwk[1]; auto K = hwk[2]; auto xy = readln.split.to!(int[]); auto sy = xy[0]-1; auto sx = xy[1]-1; auto gy = xy[2]-1; auto gx = xy[3]-1; auto MAP = new char[][H]; foreach (i; 0..H) MAP[i] = readln.chomp.to!(char[]); auto G = new int[][][](H, W, 4); foreach (i; 0..H) { foreach (j; 0..W) { if (MAP[i][j] == '@') continue; if (i > 0 && MAP[i-1][j] != '@') { G[i][j][0] = G[i-1][j][0] + 1; } if (j > 0 && MAP[i][j-1] != '@') { G[i][j][1] = G[i][j-1][1] + 1; } } } foreach_reverse (i; 0..H) { foreach_reverse (j; 0..W) { if (MAP[i][j] == '@') continue; if (i < H-1 && MAP[i+1][j] != '@') { G[i][j][2] = G[i+1][j][2] + 1; } if (j < W-1 && MAP[i][j+1] != '@') { G[i][j][3] = G[i][j+1][3] + 1; } } } auto DP = new int[][](H, W); foreach (ref dp; DP) dp[] = int.max; auto ss = [[sx, sy, 0, -1]]; while (!ss.empty) { auto h = ss[0]; ss = ss[1..$]; auto x = h[0]; auto y = h[1]; auto c = h[2]; auto p = h[3]; foreach (i, d; [[0, -1], [-1, 0], [0, 1], [1, 0]]) { if (G[y][x][i] == 0) continue; if ((i == 0 && p == 2) || (i == 1 && p == 3) || (i == 2 && p == 0) || (i == 3 && p == 1)) continue; foreach (n; 1..min(K+1, G[y][x][i]+1)) { auto dx = x + n * d[0]; auto dy = y + n * d[1]; if (DP[dy][dx] == c) continue; if (DP[dy][dx] < c) break; DP[dy][dx] = c; if (dx == gx && dy == gy) { writeln(c+1); return; } ss ~= [dx, dy, c+1, i.to!int]; } } } writeln(-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() { long n; long s; scan(n); scan(s); for (long b = 2; b*b <= n; b++) { if (dsum(b, n) == s) { writeln(b); return; } } long ans = inf6; for (long p = 1; p*p < n; p++) { auto q = s - p; if ((n - q) % p == 0) { long b = (n - q) / p; if (b > 1 && dsum(b, n) == s) { ans = min(ans, b); } } } if (ans < inf6) { writeln(ans); return; } if (n == s) { writeln(n + 1); return; } writeln(-1); } long dsum(long b, long n) { return n < b ? n : dsum(b, n / b) + (n % b); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto abc = readln.split.to!(int[]); auto K = readln.chomp.to!int; auto max = abc.reduce!max; writeln(abc.sum - max + max*2^^K); }
D
import std.stdio; import std.conv; import std.algorithm; import std.range; import std.string; import std.math; import std.format; void main() { foreach (string line; stdin.lines) { int n = line.chomp.to!int; int cnt = 0; for (int a = 0; a <= 9; a++) { for (int b = 0; b <= 9; b++) { for (int c = 0; c <= 9; c++) { for (int d = 0; d <= 9; d++) { if (a + b + c + d == n) { cnt++; } } } } } cnt.writeln; } }
D
void main() { string s = readln.chomp; long cnt; long times; foreach (i, c; s) { if (c == 'W') { times += i - cnt; ++cnt; } } times.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 chie template :) {{{ static if (__VERSION__ < 2090) { import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons, std.format, std.bitmanip, std.numeric; } else { import std; } // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan(T...)(ref T args) { foreach (ref arg; args) { arg = next!(typeof(arg)); } } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } // }}} // ModInt {{{ struct ModInt(ulong modulus) { import std.traits : isIntegral, isBoolean; import std.exception : enforce; private { ulong val; } this(const ulong n) { val = n % modulus; } this(const ModInt n) { val = n.value; } @property { ref inout(ulong) value() inout { return val; } } T opCast(T)() { static if (isIntegral!T) { return cast(T)(val); } else if (isBoolean!T) { return val != 0; } else { enforce(false, "cannot cast from " ~ this.stringof ~ " to " ~ T.stringof ~ "."); } } ModInt opAssign(const ulong n) { val = n % modulus; return this; } ModInt opOpAssign(string op)(ModInt rhs) { static if (op == "+") { val += rhs.value; if (val >= modulus) { val -= modulus; } } else if (op == "-") { if (val < rhs.value) { val += modulus; } val -= rhs.value; } else if (op == "*") { val = val * rhs.value % modulus; } else if (op == "/") { this *= rhs.inv; } else if (op == "^^") { ModInt res = 1; ModInt t = this; while (rhs.value > 0) { if (rhs.value % 2 != 0) { res *= t; } t *= t; rhs /= 2; } this = res; } else { enforce(false, op ~ "= is not implemented."); } return this; } ModInt opOpAssign(string op)(ulong rhs) { static if (op == "+") { val += ModInt(rhs).value; if (val >= modulus) { val -= modulus; } } else if (op == "-") { auto r = ModInt(rhs); if (val < r.value) { val += modulus; } val -= r.value; } else if (op == "*") { val = val * ModInt(rhs).value % modulus; } else if (op == "/") { this *= ModInt(rhs).inv; } else if (op == "^^") { ModInt res = 1; ModInt t = this; while (rhs > 0) { if (rhs % 2 != 0) { res *= t; } t *= t; rhs /= 2; } this = res; } else { enforce(false, op ~ "= is not implemented."); } return this; } ModInt opUnary(string op)() { static if (op == "++") { this += 1; } else if (op == "--") { this -= 1; } else { enforce(false, op ~ " is not implemented."); } return this; } ModInt opBinary(string op)(const ulong rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinary(string op)(ref const ModInt rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinary(string op)(const ModInt rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinaryRight(string op)(const ulong lhs) const { mixin("return ModInt(this) " ~ op ~ "= lhs;"); } long opCmp(ref const ModInt rhs) const { return cast(long)value - cast(long)rhs.value; } bool opEquals(const ulong rhs) const { return value == ModInt(rhs).value; } ModInt inv() const { ModInt ret = this; ret ^^= modulus - 2; return ret; } string toString() const { import std.format : format; return format("%s", val); } } // }}} // alias {{{ alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less); alias MinHeap(T) = Heap!(T, "a > b"); // }}} // memo {{{ /* - ある値が見つかるかどうか <https://dlang.org/phobos/std_algorithm_searching.html#canFind> canFind(r, value); -> bool - 条件に一致するやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#filter> // 2で割り切れるやつ filter!"a % 2 == 0"(r); -> Range - 合計 <https://dlang.org/phobos/std_algorithm_iteration.html#sum> sum(r); - 累積和 <https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold> // 今の要素に前の要素を足すタイプの一般的な累積和 // 累積和のrangeが帰ってくる(破壊的変更は行われない) cumulativeFold!"a + b"(r, 0); -> Range - rangeをarrayにしたいとき array(r); -> Array - 各要素に同じ処理をする <https://dlang.org/phobos/std_algorithm_iteration.html#map> // 各要素を2乗 map!"a * a"(r) -> Range - ユニークなやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#uniq> uniq(r) -> Range - 順列を列挙する <https://dlang.org/phobos/std_algorithm_iteration.html#permutations> permutation(r) -> Range - ある値で埋める <https://dlang.org/phobos/std_algorithm_mutation.html#fill> fill(r, val); -> void - バイナリヒープ <https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap> // 昇順にするならこう(デフォは降順) BinaryHeap!(Array!T, "a > b") heap; heap.insert(val); heap.front; heap.removeFront(); - 浮動小数点の少数部の桁数設定 // 12桁 writefln("%.12f", val); - 浮動小数点の誤差を考慮した比較 <https://dlang.org/phobos/std_math.html#.approxEqual> approxEqual(1.0, 1.0099); -> true - 小数点切り上げ <https://dlang.org/phobos/std_math.html#.ceil> ceil(123.4); -> 124 - 小数点切り捨て <https://dlang.org/phobos/std_math.html#.floor> floor(123.4) -> 123 - 小数点四捨五入 <https://dlang.org/phobos/std_math.html#.round> round(4.5) -> 5 round(5.4) -> 5 */ // }}} void main() { auto cin = new Scanner; int s, w; cin.scan(s, w); if (w >= s) { writeln("unsafe"); } else { writeln("safe"); } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); char[] S = sread().dup; long Q = lread(); auto bit = new BinaryIndexedTree[]('z' - 'a' + 1); foreach (i; 0 .. bit.length) bit[i] = BinaryIndexedTree(N); foreach (i; 0 .. N) { bit[S[i] - 'a'].add(i + 1, 1); } outer: foreach (_; 0 .. Q) { auto input = readln.split(); long a = input[1].to!long(); if (input[0] == "1") { char b = input[2][0]; if (S[a - 1] == b) continue outer; bit[S[a - 1] - 'a'].add(a, -1); bit[b - 'a'].add(a, 1); S[a - 1] = cast(char) b; } if (input[0] == "2") { long b = input[2].to!long(); long ans; foreach (c; 0 .. bit.length) ans += (bit[c].sum(b) - bit[c].sum(a - 1) != 0); writeln(ans); } } } /// BIT (1-indexed) alias BinaryIndexedTree = BinaryIndexedTreeImpl!long; struct BinaryIndexedTreeImpl(T) { T[] _data; this(long n) { _data = new T[](n + 1); } T sum(long i) { T ans; for (; 0 < i; i -= (i & -i)) ans += _data[i]; return ans; } void add(long i, T x) { for (; i < _data.length; i += (i & -i)) _data[i] += x; } }
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; int n; void main() { scan(n); auto a = new int[][](n, n); iota(n).each!(i => a[i][] = readln.split.to!(int[])); foreach (i ; 0 .. n) { foreach (j ; 0 .. n) { if (a[i][j] != 1) { bool flag; foreach (k ; 0 .. n) { if (k == i) continue; int x = a[i][j] - a[k][j]; foreach (l ; 0 .. n) { if (x == a[i][l]) { flag = true; break; } } if (flag) break; } if (!flag) { writeln("No"); return; } } } } writeln("Yes"); } 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, std.range, std.algorithm, std.array, std.functional, std.container, std.typecons; import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array, std.functional, std.container, std.typecons; enum MOD = 1000000007; ulong _p(ulong n, ulong k) { if(k == 0) return 1; return (n * p(n-1, k-1)) % MOD; } alias p = memoize!_p; void main() { auto input = readln.split.to!(int[]); int N = input[0], K = input[1]; int[][] tree = new int[][N]; foreach(i; 0..(N-1)) { input = readln.split.to!(int[]); int a = input[0], b = input[1]; tree[a-1] ~= b-1; tree[b-1] ~= a-1; } bool[] seen = new bool[N]; ulong dfs(ulong n, ulong k) { seen[n] = true; int[] children = tree[n].filter!(c => !seen[c]).array; ulong ck = K - tree[n].length; ulong ptns = 1; foreach(c; children) { ptns = (ptns * dfs(c, ck)) % MOD; } ulong one = (ptns * p(children.length + (ck - 1), children.length)) % MOD; return one; } writeln((dfs(0, K) * K) % MOD); }
D
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } enum INF = 10L^^18; void main() { try { for (; ; ) { const numCases = readInt; foreach (caseId; 0 .. numCases) { const N = readInt; auto A = new long[N]; foreach (i; 0 .. N) { A[i] = readLong; } auto ls = new long[N + 1]; auto rs = new long[N + 1]; ls[0] = -INF; foreach (i; 0 .. N) { ls[i + 1] = max(ls[i], A[i]); } rs[N] = +INF; foreach_reverse (i; 0 .. N) { rs[i] = min(A[i], rs[i + 1]); } bool ans; foreach (i; 1 .. N) { ans = ans || (ls[i] > rs[i]); } writeln(ans ? "YES" : "NO"); } } } catch (EOFException e) { } }
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; char[] str; void main() { int N = readln.chomp.to!int; str = readln.chomp.to!(char[]); int cnt = 0; foreach(i; 0..N) { char[] s = readln.chomp.to!(char[]); if (rec(0, 0, s, [])) cnt++; } cnt.writeln; } bool rec(int depth, int index, char[] s, int[] ary) { if (ary.length == str.length) { return true; } else if (depth<s.length){ if (s[depth] == str[index]) { if (ary.length<2 || depth-ary[$-1]==ary[$-1]-ary[$-2]) { int[] _ary = ary.dup; if (rec(depth+1, index+1, s, _ary~depth)) return true; } } if (rec(depth+1, index, s, ary)) return true; } return false; }
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; auto k = "CODEFESTIVAL2016"; int cnt; foreach (i; 0..s.length) { if (s[i] != k[i]) { cnt++; } } cnt.writeln; }
D
//prewritten code: https://github.com/antma/algo import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.traits; import std.numeric; class InputReader { private: ubyte[] p; ubyte[] buffer; size_t cur; public: this () { buffer = uninitializedArray!(ubyte[])(16<<20); p = stdin.rawRead (buffer); } final ubyte skipByte (ubyte lo) { while (true) { auto a = p[cur .. $]; auto r = a.find! (c => c >= lo); if (!r.empty) { cur += a.length - r.length; return p[cur++]; } p = stdin.rawRead (buffer); cur = 0; if (p.empty) return 0; } } final ubyte nextByte () { if (cur < p.length) { return p[cur++]; } p = stdin.rawRead (buffer); if (p.empty) return 0; cur = 1; return p[0]; } template next(T) if (isSigned!T) { final T next () { T res; ubyte b = skipByte (45); if (b == 45) { while (true) { b = nextByte (); if (b < 48 || b >= 58) { return res; } res = res * 10 - (b - 48); } } else { res = b - 48; while (true) { b = nextByte (); if (b < 48 || b >= 58) { return res; } res = res * 10 + (b - 48); } } } } template next(T) if (isUnsigned!T) { final T next () { T res = skipByte (48) - 48; while (true) { ubyte b = nextByte (); if (b < 48 || b >= 58) { break; } res = res * 10 + (b - 48); } return res; } } } int dist (int x, int k) { x = (x - 1) % k; return min (x, k - x); } void main() { auto r = new InputReader; immutable n = r.next!uint; immutable k = r.next!uint; immutable a = r.next!uint; immutable b = r.next!uint; long rmin = long.max, rmax = long.min; ulong nk = n.to!ulong * k; foreach (s; 1 .. k + 1) { if (dist (s, k) != a) { continue; } debug stderr.writeln ("s = ", s); foreach (l; 1 .. k + 1) { immutable sl = l + s; if (dist (sl, k) != b) { continue; } debug stderr.writeln ("l = ", l); ulong m = l; while (m <= nk) { long z = nk / gcd (nk, m); rmin = min (rmin, z); rmax = max (rmax, z); m += k; } } } writeln (rmin, ' ', rmax); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto ab = readln.split.to!(int[]); auto A = ab[0]; auto B = ab[1]; int r; foreach (_; 0..2) { if (A > B) { r += A; --A; } else { r += B; --B; } } writeln(r); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); auto A = aryread(); // auto B = A.map!(x => BigInt(x)).array; auto l = lcm(A); long ans; foreach (a; A) { long x = (l * invmod(a, MOD)) % MOD; ans = (ans + x) % MOD; } writeln(ans); } T invmod(T = long)(T x, T m) { T powmod(T = long)(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; } return powmod(x, m - 2, m); } long lcm(long[] A) { long[long] f; foreach (a; A) { auto d = factorize(a); foreach (key, value; d) { f[key] = max(f.get(key, 0), value); } } // writeln(f); long ans = 1; foreach (key, value; f) { ans = powmod(key, value, MOD) * ans % MOD; } return ans; } /// x^^n % m T powmod(T = long)(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; } /// 素因数分解 long[long] factorize(long x) { assert(0 < x, "x is negative"); long[long] ps; while ((x & 1) == 0) { x /= 2; ps[2] = (2 in ps) ? ps[2] + 1 : 1; } for (long i = 3; i * i <= x; i += 2) while (x % i == 0) { x /= i; ps[i] = (i in ps) ? ps[i] + 1 : 1; } if (x != 1) ps[x] = (x in ps) ? ps[x] + 1 : 1; return ps; }
D
void main() { string s = rdStr; bool ok = true; foreach (i, x; s) { if (i & 1) { if (x != 'i') ok = false; } else { if (x != 'h') ok = false; } } if (s.length & 1) ok = false; writeln(ok ? "Yes" : "No"); } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
import std; void main() { int n; scan(n); auto a = readints; int[int] d; foreach (x; a) d[x]++; for (int i = 1; i <= n; i++) { writeln(d.get(i, 0)); } } 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
void main() { auto N = ri; auto S = rs; auto warr = new int[](N); warr[0] = (S[0] == 'W' ? 1 : 0); foreach(i; 0..N-1) warr[i+1] = (S[i+1] == 'W' ? 1 : 0) + warr[i]; auto f(int n) { return n+1 - warr[n]; } int res = int.max; foreach(i; 0..N) { int tmp; tmp += f(N-1) - f(i); tmp += i < 1 ? 0 : warr[i-1]; res = min(res, tmp); } res.writeln; } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.conv, std.stdio, std.algorithm, std.string, std.range; void main() { const N = readln.chomp.to!int; foreach (x; 1..10) foreach (y; x..10) { if (x*y == N) { "Yes".writeln; return; } } "No".writeln; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}} void main() { int n; readV(n); writeln(n/3); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto A = readln.chomp.to!(wchar[]); auto B = readln.chomp.to!(wchar[]); auto C = readln.chomp.to!(wchar[]); int r; foreach (i; 0..N) { auto a = A[i]; auto b = B[i]; auto c = C[i]; if (a == b && b == c) { } else if (a == b || b == c || c == a) { ++r; } else { r += 2; } } writeln(r); }
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, "x", long, "y"); alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less); void main() { long w, h, n; scan(w, h, n); auto square = new long[][](h, w); foreach (i; iota(n)) { long x, y, a; scan(x, y, a); switch (a) { case 1: draw(square, 0, x, 0, h); break; case 2: draw(square, x, w, 0, h); break; case 3: draw(square, 0, w, 0, y); break; case 4: draw(square, 0, w, y, h); break; default: assert(0); } } long cnt; foreach (i; iota(h)) { foreach (j; iota(w)) { if (square[i][j] == 0) cnt++; } } cnt.writeln(); } void draw(T)(ref T wall, long right, long left, long lower, long upper) { foreach (i; iota(lower, upper)) { foreach (j; iota(right, left)) { wall[i][j]++; } } } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
void main(){ int n = _scan(); // 操作を行う回数 int k = _scan(); // 足す値 int ans = 1; foreach(elm; 0..n){ if(ans*2 < ans+k)ans *=2; else ans +=k; } writeln(ans); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; void times(alias pred)(int n) { foreach(i; 0..n) pred(); } auto rep(alias pred, T = typeof(pred()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = pred(); return res; } void main() { int N = readln.chomp.to!int; long[] ary = new long[2^^(N+1)]; foreach(i; 0..2^^N) { ary[2^^N-1+i] = readln.chomp.to!long; } for(int i=N-1; i>=0; i--) { foreach(j; 2^^i-1..2^^(i+1)-1) { ary[j] = f(ary[j*2+1], ary[j*2+2]); } } ary.front.writeln; } long f(long x, long y) { if (x==y) return x; return abs(x-y); }
D
import std.stdio : readf, readln, writeln; import std.string; import std.array; import std.conv; void main() { int[] v; v = readln.split.to!(int[]); int H = v[0]; int W = v[1]; string[] s; for (int i=0; i<H; i++) s ~= readln.chomp; int[][] dw; int[][] dh; for (int i = 0; i < H; i++) { int[] cw; int count = 0; for (int j = 0; j < W; j++) { if (s[i][j] == '#') { cw ~= 0; } else if (j == 0 || s[i][j - 1] == '#') { count = 0; for (int k = j; k < W; k++) { if (s[i][k] == '.') { count++; } else { break; } } cw ~= count; } else { cw ~= count; } } dw ~= cw; } for (int j = 0; j < W; j++) { int[] ch; int count = 0; for (int i = 0; i < H; i++) { if (s[i][j] == '#') { ch ~= 0; } else if (i == 0 || s[i - 1][j] == '#') { count = 0; for (int k = i; k < H; k++) { if (s[k][j] == '.') { count++; } else { break; } } ch ~= count; } else { ch ~= count; } } dh ~= ch; } int max = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { int sum = dw[i][j] + dh[j][i]; if (max < sum) { max = sum; } } } writeln(max - 1); }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.7.4" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; // import dcomp.geo.primitive; bool solve() { alias P = Point2D!double; P[] pol = new P[3]; P p; foreach (i; 0..3) { double x, y; if (sc.read(x, y) != 2) return false; pol[i] = P(x, y); } double x, y; sc.read(x, y); p = P(x, y); if (contains(pol, p) == 2) { writeln("YES"); } else { writeln("NO"); } return true; } int main() { EPS!double = 1e-10; while (solve()) {} return 0; } Scanner sc; static this() { sc = new Scanner(stdin); } /* IMPORT /Users/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 /Users/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; } } /* IMPORT /Users/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 /Users/yosupo/Program/dcomp/source/dcomp/geo/primitive.d */ // module dcomp.geo.primitive; import std.traits; template EPS(R) { R EPS; } int sgn(R)(R a) { if (a < -EPS!R) return -1; if (a > EPS!R) return 1; return 0; } struct Point2D(T) { T[2] d; this(T x, T y) {this.d = [x, y];} this(T[2] d) {this.d = d;} @property ref inout(T) x() inout {return d[0];} @property ref inout(T) y() inout {return d[1];} ref inout(T) opIndex(size_t i) inout {return d[i];} auto opBinary(string op:"+")(Point2D r) const {return Point2D(x+r.x, y+r.y);} auto opBinary(string op:"-")(Point2D r) const {return Point2D(x-r.x, y-r.y);} static if (isFloatingPoint!T) { T abs() { import std.math : sqrt; return (x*x+y*y).sqrt; } T arg() { import std.math : atan2; return atan2(y, x); } } } bool near(T)(Point2D!T a, Point2D!T b) { return !sgn((a-b).abs); } T dot(T)(in Point2D!T l, in Point2D!T r) { return l[0]*l[0] + l[1]*r[1]; } T cross(T)(in Point2D!T l, in Point2D!T r) { return l[0]*r[1] - l[1]*r[0]; } int ccw(R)(Point2D!R a, Point2D!R b, Point2D!R c) { assert(!near(a, b)); if (near(a, c) || near(b, c)) return 0; int s = sgn(cross(b-a, c-a)); if (s) return s; if (dot(b-a, c-a) < 0) return 2; if (dot(a-b, c-b) < 0) return -2; return 0; } int contains(R)(Point2D!R[] pol, Point2D!R p) { import std.algorithm : swap; int res = -1; foreach (i; 0..pol.length) { auto a = pol[i] - p, b = pol[(i+1)<pol.length?i+1:0] - p; if (ccw(a, b, Point2D!R(0, 0)) == 0) return 1; if (a.y > b.y) swap(a, b); if (a.y <= 0 && 0 < b.y) { if (cross(a, b) < 0) res *= -1; } } return res+1; } int argcmp(T)(Point2D!T l, Point2D!T r) if (isIntegral!T) { int sgn(Point2D!T p) { if (p[1] < 0) return -1; if (p[1] > 0) return 1; if (p[0] < 0) return 2; return 0; } int lsgn = sgn(l); int rsgn = sgn(r); if (lsgn < rsgn) return -1; if (lsgn > rsgn) return 1; T x = cross(l, r); if (x > 0) return -1; if (x < 0) return 1; return 0; } /* 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; 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() { auto args = readln.chomp.split.map!(to!int); int a = args[0]; int b = args[1]; int c = args[2]; int x = args[3]; int y = args[4]; int z = x + y; int ans = int.max; for (int i = 0; i <= z * 2; ++i) { int m = i / 2; int rx = x - m; int ry = y - m; int v = i * c + rx.max(0) * a + ry.max(0) * b; ans = min(ans, v); } ans.writeln; }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; void main() { auto n = readln.chomp; int sum; for (int i = 0; i < n.length; ++i) { sum += (n[i] - '0'); } if (n.to!int % sum == 0) { "Yes".writeln; } else { "No".writeln; } }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } 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 = lread(); auto F = new uint[](N); auto P = new long[10][](N); foreach (i; 0 .. N) { auto a = aryread(); foreach (j; 0 .. 10) F[i] |= a[j] << j; } foreach (i; 0 .. N) P[i][] = aryread()[0 .. 10]; long ans = long.min; foreach (x; 1 .. (1 << 10)) { long sum; foreach (i, f; F) { sum += P[i][popcnt(x & f)]; } ans = ans.max(sum); } // foreach (f; F) // writefln("%011b", f); // writeln(P); 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 n, m, nq; readV(n, m, nq); auto t = new int[][](n+2, n+2); foreach (_; 0..m) { int l, r; readV(l, r); ++t[1][r]; --t[l+1][r]; --t[1][n+1]; ++t[l+1][n+1]; } foreach (j; 0..n+2) foreach (i; 0..n+1) t[i+1][j] += t[i][j]; foreach (i; 0..n+2) foreach (j; 0..n+1) t[i][j+1] += t[i][j]; foreach (_; 0..nq) { int p, q; readV(p, q); writeln(t[p][q]); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto A = RD; auto B = RD; long ans = -1; foreach (i; 1..10^^5) { if (cast(long)(i*0.08) == A && cast(long)(i*0.1) == B) { ans = i; break; } } writeln(ans); stdout.flush; debug readln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { auto c = new string[](2); c[0] = readln.chomp; c[1] = readln.chomp; writeln(equal(c[0], c[1].retro) ? "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() { int[] tmp = readln.split.to!(int[]); int a = tmp[0], b = tmp[1]; writeln(a < 9 && b < 9 ? "Yay!" : ":("); } 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.uni;
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void main() { int a, b, x; readV(a, b, x); writeln(a <= x && x <= a+b ? "YES" : "NO"); }
D
import core.stdc.stdio; import std.container; import std.algorithm; import std.typecons; import std.stdio; void main(){ int n,m,k; scanf("%d%d%d",&n,&m,&k); alias Tuple!(int,"cost",int,"to") ct; ct[][] graph = new ct[][n]; alias Tuple!(int,"a",int,"b",int,"l") edge; edge[] es = new edge[m]; foreach(ref e;es){ scanf("%d%d%d",&e.a,&e.b,&e.l); graph[--e.a]~=ct(e.l,--e.b); graph[e.b]~=ct(e.l,e.a); } auto pq = BinaryHeap!(ct[],"a>b")(new ct[n+m],0); int[] nc = new int[n]; nc[] = 1145141919; foreach(i;0..k){ int a; scanf("%d",&a); nc[--a]=0; pq.insert(ct(0,a)); } while(!pq.empty){ auto t=pq.front; pq.popFront; with(t){ if(cost>nc[to]) continue; foreach(c;graph[to]){ if(nc[c.to]>cost+c.cost){ nc[c.to]=cost+c.cost; pq.insert(ct(cost+c.cost,c.to)); } } } } int ans; foreach(e;es) ans = max(ans,e.l+nc[e.a]+nc[e.b]); printf("%d\n",(ans+1)/2); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; immutable int INF = 1 << 29; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!long).array; long m = 0; foreach (i; 0..N) { if (A[i] > m) { writeln(i + 1); return; } if (A[i] == m) { m += 1; } } writeln(-1); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Node = Tuple!(long, "p", long, "dist"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { auto n = lread(); long lim = 101; // 入力の制限より大きい最小の素数 auto ary = new long[](lim); foreach (a; iota(1, n)) { auto p_factor = prime_factorization(a + 1, lim); ary[] += p_factor[]; } ary[] += 1; // ary.take(n).writeln(); long gt_5, gt_3, gt_15, gt_25, gt_75; foreach (e; ary) { if (e >= 3) gt_3++; if (e >= 5) gt_5++; if (e >= 15) gt_15++; if (e >= 25) gt_25++; if (e >= 75) gt_75++; } long ans; ans += (gt_5 * (gt_5 - 1) / 2) * (gt_3 - 2); ans += (gt_15) * (gt_5 - 1); ans += (gt_25) * (gt_3 - 1); ans += (gt_75); ans.writeln(); } auto prime_factorization(long a, long lim) { auto p_factor = new long[](lim); foreach (i; iota(1, a)) { while (a % (i + 1) == 0) { a /= (i + 1); p_factor[i]++; } if (a == 0) break; } return p_factor; } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; 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 writeA(T)(size_t n,T t){foreach(i,v;t.enumerate){write(v);if(i<n-1)write(" ");}writeln;} void main() { int n; readV(n); int[] a; readA(n, a); auto b = new int[](n); if (n%2 == 0) { foreach (i; 0..n/2) b[i] = a[n-i*2-1]; foreach (i; 0..n/2) b[i+n/2] = a[i*2]; } else { foreach (i; 0..n/2+1) b[i] = a[n-i*2-1]; foreach (i; 0..n/2) b[i+n/2+1] = a[i*2+1]; } writeA(n, b); }
D
import std.stdio, std.conv, std.string, std.math, std.regex, std.range, std.ascii, std.algorithm; void main(){ auto ip = readln.chomp; switch(ip){ case "A": writeln("T"); break; case "T": writeln("A"); break; case "C": writeln("G"); break; case "G": writeln("C"); break; default: } }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); long bignum = 1_000_000_007; T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { auto c = new long[](0); foreach (_; 0 .. 3) { auto tmp = aryread(); c ~= tmp; } bool check = true; long b1, b2, b3, a2, a3; b1 = c[0]; b2 = c[1]; b3 = c[2]; if(!((c[3] - b1) == (c[4] - b2) && (c[3] - b1) == (c[5] - b3))) check = false; if(!((c[6] - b1) == (c[7] - b2) && (c[6] - b1) == (c[8] - b3))) check = false; if(check) writeln("Yes"); else writeln("No"); }
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!long; writeln(N * (N-1) / 2); }
D
import std.stdio, std.conv, std.string; import std.algorithm, std.array, std.container; import std.numeric, std.math; import core.bitop; string my_readln() { return chomp(readln()); } long mod = pow(10, 9) + 7; long moda(long x, long y) { return (x + y) % mod; } long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; } long modm(long x, long y) { return (x * y) % mod; } void main() { auto N = my_readln.to!long; ulong[32] d; foreach (i; 0..32) { auto x = N % 2; //stderr.writeln(N, " ", x); N /= 2; long sign; if (i % 2 == 0) sign = 1; else sign = -1; if (sign * sgn(x) >= 0) { d[i] += abs(x); } else { ++d[i+1]; ++d[i]; } while (d[i] > 1) { ++d[i+2]; ++d[i+1]; d[i] -= 2; } } string ans; bool hasStart = false; foreach_reverse (e; d) { if (hasStart) { ans ~= e.to!string; } else if (e == 1) { ans ~= e.to!string; hasStart = true; } } if (!hasStart) ans ~= "0"; writeln(ans); stdout.flush(); }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; void main(){ string[] input = readln().split(); int a = input[0].to!int(), b = input[1].to!int(); while(b != 0){ a = a % b; swap(a, b); } a.writeln(); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string; void solve() { auto s = readln.split.map!(to!long); auto K = s[0]; auto X = s[1]; long ans; if (X == 9) { ans = 9 * K; } else { ans = 9 * (K - 1) + X; } ans.writeln; } void main() { auto T = readln.chomp.to!int; while (T--) solve; }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; import std.datetime; import std.numeric; import std.math; import std.string; string my_readln() { return chomp(readln()); } void main() { auto tokens = my_readln().split(); auto H = tokens[0].to!ulong; auto W = tokens[1].to!ulong; string[] a; foreach (i; 0..H) { a ~= my_readln(); } long[1010][1010] memo; foreach (ref e; memo) foreach (ref ee; e) ee = -1; ulong mod = pow(10, 9) + 7; ulong search(long i, long j) { if (i == W || j == H) return 0; if (memo[i][j] != -1) return memo[i][j]; if (a[j][i] == '#') return 0; if (i == W - 1 && j == H - 1) return 1; ulong r; r = (r + search(i+1, j)) % mod; r = (r + search(i, j+1)) % mod; //stderr.writeln(i, " ", j, " ", r); memo[i][j] = r; return r; } ulong ans = search(0, 0); writeln(ans); stdout.flush(); }
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; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto X = RD; auto remain = X % 100; auto cnt = X / 100; writeln(cnt*5 >= remain ? 1 : 0); stdout.flush; debug readln; }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.container; import std.array; import std.math; import std.range; import std.typecons; import std.ascii; import std.format; void main() { auto a = readln.chomp.split.map!(to!int); auto h = a[0]; auto w = a[1]; char[][] map = new char[][](h + 2, w + 2); foreach (ref arr; map) { arr[] = '.'; } foreach (i; 0..h) { auto s = readln; foreach (j; 0..w) { map[i + 1][j + 1] = s[j]; } } for (int i = 1; i <= h; ++i) { for (int j = 1; j <= w; ++j) { if (map[i][j] == '#') { write = '#'; continue; } int cnt = 0; for (int dx = -1; dx <= 1; ++dx) { for (int dy = -1; dy <= 1; ++dy) { cnt += map[i + dy][j + dx] == '#'; } } cnt.write; } writeln; } }
D
import std.stdio, std.string, std.conv, std.math; void main() { int n = readln.chomp.to!int; long debt = 100000; foreach(i;0..n) { debt += debt * 0.05; debt = (((debt / 1000.0).ceil) * 1000).to!long; } debt.writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nm = readln.split.to!(long[]); auto N = nm[0]; auto M = nm[1]; auto as = readln.split.to!(long[]); auto af = as[0]; int x; while (af%2 == 0) { af /= 2; ++x; } foreach (a; as[1..$]) { int y; while (a%2 == 0) { a /= 2; ++y; } if (x != y) { writeln(0); return; } } auto d = as[0] / 2; foreach (a; as[1..$]) { d = d / gcd(d, a/2) * a/2; if (d > M) { writeln(0); return; } } writeln(M/d - M/(d*2)); }
D