code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto abc = readln.split.to!(int[]); auto AB = abc[0]; auto BC = abc[1]; writeln(AB * BC / 2); }
D
import std.stdio, std.algorithm, std.string, std.conv, std.array; int read_num() {return readln.chomp.to!(int);} void main() { while(true) { int[] line = readln.chomp.split(" ").map!(to!(int)).array; int times = line[0]; int len = line[1]; int[] arr; if (times == 0 && len == 0) break; foreach(t; 0..times) arr ~= read_num; int max, sum; int next = 0; foreach(x; arr[0..len]) max += x; sum = max; foreach(int i, x; arr[len..times]) { sum += x; sum -= arr[i]; if (sum > max) max = sum; } writeln(max); } }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; immutable inf = 10^^9 + 7; alias Edge = Tuple!(int, "to", int, "cost", int, "rev"); void main() { int n, m; scan(n, m); auto adj = new Edge[][](n, 0); foreach (i ; 0 .. m) { int u, v, c; scan(u, v, c); adj[u] ~= Edge(v, c, adj[v].length.to!int); adj[v] ~= Edge(u, 0, adj[u].length.to!int - 1); } int ans = FordFulkerson(n, m, adj, 0, n - 1); writeln(ans); } int FordFulkerson(int n, int m, Edge[][] adj, int s, int t) { bool[] visited = new bool[](n); int dfs (int u, int f) { if (u == t) { return f; } visited[u] = 1; foreach (ref e ; adj[u]) { if (e.cost > 0 && !visited[e.to]) { int d = dfs(e.to, min(f, e.cost)); if (d > 0) { e.cost -= d; adj[e.to][e.rev].cost += d; return d; } } } return 0; } int F; while (true) { visited[] = 0; int f = dfs(s, inf); if (f == 0) break; F += f; } return F; } 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() { auto input = getStdin!(string[])[0]; auto num = input.split(" ").to!(int[]); int a = num[0]; int b = num[1]; int c = num[2]; int count = 0; for (int i = a; i <= b; i++) { if (c % i == 0) { count++; } } count.writeln; } T getStdin(T)() { string[] cmd; string line; while ((line = chomp(stdin.readln())) != "") cmd ~= line; return to!(T)(cmd); }
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/5; enum long MOD = 10L^^9+7; void main() { char[] xs = readln.chomp.to!(char[]); long ans = 0; foreach(i; 0..xs.length/2) { if (xs[i] != xs[$-i-1]) ans++; } ans.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 (y > 0 && t * y < x) t++; if (y < 0 && t * y > x) t++; return t; } T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { T t = x / y; if (y > 0 && t * y > x) t--; if (y < 0 && 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) { enum sep = " "; enum n = Args.length; enum fmt = n.rep!(()=>"%s").join(sep) ~ "\n"; static if (__VERSION__ >= 2071) { readf!fmt(args); } else { enum argsTemp = n.iota.map!( i => "&args[%d]".format(i) ).join(", "); mixin( "readf(fmt, " ~ argsTemp ~ ");" ); } } // 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); } } } } // 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, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { string s; scan(s); yes(check(s)); } bool check(string s) { return s[2] == s[3] && s[4] == s[5]; } unittest { assert(check("coffee")); assert(!check("iphone")); assert(check("sippuu")); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.stdio; import std.ascii; import std.conv; import std.string; import std.algorithm; import std.range; import std.functional; import std.math; import core.bitop; void main() { auto a = readln.chomp.split().to!(long[]); auto f = (long a, long b, long c) => abs(a - b) + abs(b - c); min(f(a[0], a[1], a[2]), f(a[0], a[2], a[1]), f(a[1], a[0], a[2]), f(a[1], a[2], a[0]), f(a[2], a[0], a[1]), f(a[2], a[1], a[0])).writeln; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { int r = readint; int g = readint; // (r + p) / 2 = g int p = 2 * g - r; writeln(p); }
D
import std.conv; import std.string; import std.algorithm; import std.array; import std.stdio; void main() { auto s = readln.chomp; auto t = readln.chomp; uint a; foreach (i;0..s.length) { if (s[i] != t[i]) { ++a; } } a.writeln; }
D
import std.stdio,std.array,std.conv,std.string,std.algorithm; void main(){ string[] inputStr = readln().split; int n = to!int(inputStr[0]); int k = to!int(inputStr[1]); string[] d = readln().split; int count = 0; while(true){ count = 0; for(int j=0;j<to!string(n).length;j++){ for(int a=0;a<d.length;a++){ if((to!int(to!string(n)[j])-48) == to!int(d[a])) count++; } } if(count == 0) break; n++; } write(n); }
D
import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; void main() { int n = to!int(chomp(readln())); int k = to!int(chomp(readln())); int x = to!int(chomp(readln())); int y = to!int(chomp(readln())); int cost = 0; if(n > k) { cost = k*x+(n-k)*y; } else { cost = n * x; } writeln(to!string(cost)); } int[] stringText2IntArray(string text) { return map!(to!int)(split(readln())).array; }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { int N, A; scan(N); scan(A); writeln(N^^2 - A); } 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.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 int[](t); foreach (ti; 0..t) { auto n = RD!int; auto h = RDA; bool ok = true; long tot; foreach (i; 0..n) { tot += h[i]; if (tot < i*(i+1)/2) { ok = false; break; } } ans[ti] = ok; } foreach (e; ans) { writeln(e? "YES" : "NO"); } stdout.flush; debug readln; }
D
import std.stdio; import std.algorithm; import std.string; import std.conv; import std.math; void main(){ while(true){ auto s = readln(); if(stdin.eof()) break; auto s1 = split(s); real[] inp; for(int i=0;i<s1.length;i++){ inp ~= to!real(s1[i]); } if(inp[0]<=inp[6]&&inp[1]<=inp[7]&&inp[4]<=inp[2]&&inp[5]<=inp[3]) writeln("YES"); else writeln("NO"); } }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; import std.typecons; import std.bigint; // import dcomp.foundation, dcomp.scanner; // import dcomp.container.deque; int main() { auto sc = new Scanner(stdin); int n, m, k; sc.read(n, m, k); m++; int[] a = new int[n]; a.each!((ref x) => sc.read(x)); long[] dp = a.map!(to!long).array; long[] ndp = new long[n]; auto deq = Deque!(int, false).make(); auto p = deq.p; foreach (int ph; 2..k+1) { ndp[] = -(10L^^18); deq.clear(); foreach (int i; 0..n) { if (deq.length && (*p)[0] == i-m) { p.removeFront(); } if (deq.length) { ndp[i] = dp[(*p)[0]] + 1L * ph * a[i]; } while (deq.length && dp[(*p)[p.length-1]] <= dp[i]) { p.removeBack(); } p.insertBack(i); } swap(dp, ndp); } writeln(dp.fold!max); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; //fold(for old compiler) static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } unittest { import std.stdio; auto l = [1, 2, 3, 4, 5]; assert(l.fold!"a+b"(10) == 25); } } version (X86) static if (__VERSION__ < 2071) { int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/deque.d */ // module dcomp.container.deque; struct Deque(T, bool hasNull = true) { 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 (hasNull && !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); } } static Deque make() { Deque que; que.p = new Payload(); return que; } @property private bool hasPayload() const { return (!hasNull || p); } @property bool empty() const { return (!hasPayload || p.empty); } @property size_t length() const { return (hasPayload ? 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; void main(){ for(int i=1;i<=9;i++){ for(int j=1;j<=9;j++){ printf("%dx%d=%d\n",i,j,i*j); } } }
D
// import chie template :) {{{ import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons, std.format, std.bitmanip; // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } // }}} void main() { auto cin = new Scanner; long x = cin.next!long; if (x < 500) { writeln(x / 5 * 5); } else { writeln(x / 500 * 1000 + x % 500 / 5 * 5); } }
D
void main() { auto D = ri; iota(D).map!(v => v % 26 + 1).each!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; } long rl() { return readAs!long; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto rd = readln.split, a = rd[0].to!int, op = rd[1], b = rd[2].to!int; writeln(op == "+" ? a+b : a-b); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); void main() { long r, g, b, n; scan(r, g, b, n); // writeln(r, g, b, n); long cnt; foreach (i; 0 .. 3001) { foreach (j; 0 .. 3001) { long tmp; if (r * i + g * j <= n) { tmp = n - (r * i + g * j); if (tmp % b == 0) { cnt += 1; } } } } writeln(cnt); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.stdio; import std.string; import std.format; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.concurrency; import std.traits; import std.uni; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; enum long INF = long.max/3; enum long MOD = 10L^^9+7; void main() { long N, K; scanln(N, K); long[] as = readln.split.to!(long[]); long T = 45; long[][] bss = new long[][](T, 2); foreach(i; 0..T) { foreach(a; as) { foreach(j; 0..2) { bss[i][j] += ((a&(1L<<i))^((cast(long)j)<<i)); } } } long[][] dp = new long[][](T+1, 2); foreach(i; 0..T+1) dp[i][] = -1; dp[T][1] = 0; foreach_reverse(i; 0..T) { if (dp[i+1][0] >= 0) dp[i][0].ch!max(dp[i+1][0] + bss[i][0]); if (dp[i+1][0] >= 0) dp[i][0].ch!max(dp[i+1][0] + bss[i][1]); if (K>>i&1) { if (dp[i+1][1] >= 0) dp[i][0].ch!max(dp[i+1][1] + bss[i][0]); if (dp[i+1][1] >= 0) dp[i][1].ch!max(dp[i+1][1] + bss[i][1]); } else { if (dp[i+1][1] >= 0) dp[i][1].ch!max(dp[i+1][1] + bss[i][0]); } } dp[0].reduce!max.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
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.9.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; import std.typecons; int main() { Scanner sc = new Scanner(stdin); int h; int[] a; sc.read(h, a); h++; int[] as = new int[h+1]; foreach (i; 0..h) { as[i+1] = as[i] + a[i]; } int n = as.back; int[] ap = new int[n], bp = new int[n]; ap[0] = -1; bp[0] = -1; foreach (i; 1..h) { foreach (j; as[i]..as[i+1]) { ap[j] = bp[j] = as[i-1]; } } foreach (i; 0..h-1) { if (a[i] > 1 && a[i+1] > 1) { bp[as[i+1]+1]++; writeln("ambiguous"); writeln(ap.map!"a+1".map!(to!string).join(" ")); writeln(bp.map!"a+1".map!(to!string).join(" ")); return 0; } } writeln("perfect"); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; // import dcomp.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; 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 { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/stackpayload.d */ // module dcomp.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* This source code generated by dcomp and include dcomp's source code. dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp) dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt) */
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto n = RD!int; auto a = RDA; bool ok; foreach (i; 1..n) { if (a[i] >= a[i-1]) { ok = true; break; } } ans[ti] = ok; } foreach (e; ans) { writeln(e ? "YES" : "NO"); } stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } 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 t = RD!int; auto ans = new char[][](t); foreach (i; 0..t) { auto n = RD!int; auto a = RD!int; auto b = RD!int; auto c = RD!int; auto s = RD!string; int cnt; foreach (j; 0..n) { if (s[j] == 'R') { if (b == 0) ans[i] ~= '?'; else { ans[i] ~= 'P'; --b; ++cnt; } } else if (s[j] == 'P') { if (c == 0) ans[i] ~= '?'; else { ans[i] ~= 'S'; --c; ++cnt; } } else { if (a == 0) ans[i] ~= '?'; else { ans[i] ~= 'R'; --a; ++cnt; } } } if (cnt*2 < n) { ans[i].length = 0; } else { foreach (j; 0..n) { if (ans[i][j] == '?') { if (a != 0) { --a; ans[i][j] = 'R'; } else if (b != 0) { --b; ans[i][j] = 'P'; } else { --c; ans[i][j] = 'S'; } } } } } foreach (e; ans) { if (e.length == 0) { writeln("NO"); } else { writeln("YES"); writeln(e); } } stdout.flush(); debug readln(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto T = readln.chomp.to!int; foreach (_; 0..T) { auto S = readln.chomp; auto len = S.length; auto l1 = new size_t[](len); auto r1 = new size_t[](len); auto l2 = new size_t[](len); auto r2 = new size_t[](len); auto l3 = new size_t[](len); auto r3 = new size_t[](len); auto i1 = len, i2 = len, i3 = len; foreach (i, c; S) { switch (c) { case '1': i1 = i; break; case '2': i2 = i; break; default: i3 = i; } l1[i] = i1; l2[i] = i2; l3[i] = i3; } i1 = len, i2 = len, i3 = len; foreach_reverse (i, c; S) { switch (c) { case '1': i1 = i; break; case '2': i2 = i; break; default: i3 = i; } r1[i] = i1; r2[i] = i2; r3[i] = i3; } auto res = size_t.max; foreach (i; 0..len) { switch (S[i]) { case '1': if (l2[i] != len && r3[i] != len) { res = min(res, r3[i] - l2[i] + 1); } if (l3[i] != len && r2[i] != len) { res = min(res, r2[i] - l3[i] + 1); } break; case '2': if (l1[i] != len && r3[i] != len) { res = min(res, r3[i] - l1[i] + 1); } if (l3[i] != len && r1[i] != len) { res = min(res, r1[i] - l3[i] + 1); } break; default: if (l2[i] != len && r1[i] != len) { res = min(res, r1[i] - l2[i] + 1); } if (l1[i] != len && r2[i] != len) { res = min(res, r2[i] - l1[i] + 1); } } } writeln(res == size_t.max ? 0 : res); } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.traits; import std.random; void main() { int nt = readln.strip.to!int; foreach (tid; 0 .. nt) { int n = readln.strip.to!int; auto s = readln.take (n).array; auto res = "NO"; foreach (i; 0 .. n) { if (s[i] == '8' && (n - 1) - i >= 10) { res = "YES"; break; } } writeln (res); } }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; import std.exception; int n; rd(n); auto a=readln.split.to!(int[]); auto mn=reduce!(min)(a); int[] ids; foreach(int i; 0..n){ if(a[i]==mn) ids~=i; } int dist=n; foreach(i; 0..(ids.length-1)){ dist=min(dist, ids[i+1]-ids[i]); } writeln(dist); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } } void wr(T...)(T x){ import std.stdio; foreach(e; x) write(e, " "); writeln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto s = RDA!int; ans[ti] = 1; auto dp = new int[][](n, 21); foreach (i; 0..n) dp[i][] = int.max; foreach (i; 0..n/2) { dp[i][0] = s[i]; } foreach (int to; 1..n+1) { int[] index; for (long from = 1; from*from <= to; ++from) { if (to % from) continue; index ~= cast(int)from; index ~= cast(int)(to / from); } foreach (from; index) { foreach (k; 0..20) { if (dp[from-1][k] < s[to-1]) { dp[to-1][k+1].chmin(s[to-1]); } } } } (){ foreach_reverse (i; 0..21) { foreach (j; 0..n) { if (dp[j][i] != int.max) { ans[ti] = i+1; return; } } }}(); } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n, k; rd(n, k); auto a = readln.split.to!(int[]); auto all = reduce!((res, val) => (res && val == a[0]))(true, a); if (all) { writeln(0); return; } auto freq = new int[](3 * 10 ^^ 5); foreach (val; a) freq[val]++; auto mn = reduce!(min)(a), mx = reduce!(max)(a); int num = 0, s = 0; for (int h = mx; h > mn; h--) { freq[h] += freq[h + 1]; if ((s += freq[h]) > k) { num++; s = freq[h]; } } writeln(num + (s > 0)); } 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, 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; void main() { auto N = readln.chomp.to!int; auto S = readln.chomp; int i = 0; for (; i < N - 1; ++i) { if (S[i] > S[i + 1]) break; } S = S[0..i] ~ S[i+1..N]; S.writeln; }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto n = readln.strip.to !(int); int [] [] a; foreach (row; 0..n) { a ~= readln.splitter.map !(to !(int)).array; } auto m = n / 2; auto z = new int [m]; foreach (row; 0..n) { foreach (col; 0..n) { int d = min (row, n - 1 - row, col, n - 1 - col); z[d] ^= a[row][col]; } } foreach_reverse (d; 1..m) { z[d - 1] ^= z[d]; } foreach (d; 1..m) { z[d] ^= z[d - 1]; } int x = 0; int y = 0; foreach (row; 0..n) { foreach (col; 0..n) { if (row % 2 == 0 && col % 2 == 0 && row + col < n) { x ^= a[row][col]; } if ((n - 1 - row) % 2 == 0 && col % 2 == 0 && (n - 1 - row) + col < n) { y ^= a[row][col]; } } } writeln (x ^ y ^ z.fold !(q{a ^ b})); } }
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; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD!uint; auto s = new char[][](N); foreach (i; 0..N) { s[i] = RD!(char[]); } bool visit(uint y, uint x) { if (s[y+1][x] == '#' || s[y+1][x-1] == '#' || s[y+1][x+1] == '#' || s[y+2][x] == '#') return false; s[y][x] = '#'; s[y+1][x] = '#'; s[y+1][x-1] = '#'; s[y+1][x+1] = '#'; s[y+2][x] = '#'; return true; } bool ans = true; (){ foreach (y; 0..N) { foreach (x; 0..N) { if (s[y][x] == '#') continue; debug writeln(y, ":", x); if (x == 0 || x == N-1) { ans = false; return; } else if (y >= N-2) { ans = false; return; } auto r = visit(y, x); if (!r) { ans = false; return; } } }}(); writeln(ans ? "YES" : "NO"); stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static 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 q = RD!int; auto ans = new char[][](q); foreach (i; 0..q) { auto n = RD!int; auto k = RD; auto s = RD!string; int streak; foreach (j; 0..n) { if (s[j] == '1') { ++streak; ans[i] ~= s[j]; } else { auto cnt = cast(int)min(k, streak); if (cnt == 0) { ans[i] ~= '0'; } else { k -= cnt; ans[i][j-cnt] = '0'; ans[i] ~= '1'; } } } } foreach (e; ans) writeln(e); stdout.flush(); debug readln(); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { long h, w; scan(h, w); auto c = new string[][](h); foreach (i; 0 .. h) { c[i] = aryread!string(); } // writeln(c); foreach (i; 0 .. h) { foreach (_; 0 .. 2) { foreach (j; 0 .. c[i].length) { write(c[i][j]); } writeln(); } } } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); long ans; foreach (i; 1 .. N + 1) { if (i % 3 != 0 && i % 5 != 0) ans += i; } writeln(ans); }
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; void main() { auto s = readln.split.map!(to!int); auto H = s[0]; auto W = s[1]; auto K = s[2]; foreach (i; 0..H+1) { foreach (j; 0..W+1) { long tmp = i * W + j * H - (i * j) * 2; if (tmp == K) { writeln("Yes"); return; } } } writeln("No"); }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); } void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); } string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ int n = scan!int; long[] as = scan!long(n); long ans = 1; foreach(a; as) ans = lcm(ans, a); ans.writeln; } /// 最大公約数。最大の公約数。 /// 例 gcd(12, -18) = 6, gcd(-12, -18) = 6, gcd(12, 0) = 12, gcd(0, 0) = 0 /// ※本当は gcd(0, 0) は存在しないが便宜上 0 を返している long gcd(long a, long b){ if(a < 0) a = -a; if(b == 0) return a; if(b < 0) return gcd(a, -b); if(a % b == 0) return b; if(a < b) return gcd(b, a); else return gcd(b, a % b); } /// 最小公倍数。0以上で最小の公倍数。 /// 例 lcm(12, -18) = 36, lcm(-12, -18) = 36, lcm(12, 0) = 36, lcm(0, 0) = 0 long lcm(long a, long b){ if(a < 0) a = -a; if(b < 0) b = -b; if(a == 0 && b == 0) return 0; return (a / gcd(a, b) * b); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto N = RD; auto A = RDA; auto Q = RD; auto B = new long[](Q); auto C = new long[](Q); foreach (i; 0..Q) { B[i] = RD; C[i] = RD; } long ans; long[long] cnt; foreach (i; 0..N) { ++cnt[A[i]]; ans += A[i]; } foreach (i; 0..Q) { auto x = cnt.get(B[i], 0); auto y = cnt.get(C[i], 0); y += x; ans -= x * B[i]; ans += x * C[i]; cnt[C[i]] = y; cnt[B[i]] = 0; writeln(ans); } stdout.flush; debug readln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n, m; scan(n, m); auto ng = new bool[](n + 1); iota(m).map!(i => readln.chomp.to!int).each!(ai => ng[ai] = 1); auto f = new long[](n + 1); f[0] = 1; if (!ng[1]) f[1] = 1; foreach (i ; 2 .. n + 1) { if (ng[i]) continue; f[i] = (f[i - 1] + f[i - 2]) % mod; } debug { writeln(f); } writeln(f[$-1]); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.string, std.conv; void main(){ for(int i;;++i){ auto a = readln.chomp.to!int; if(!a) break; writeln("Case ",i+1,": ",a); } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { int q = readint; int[] v; for (int i = 0; i < q; i++) { auto xs = readints; switch (xs[0]) { case 0: v ~= xs[1]; break; case 1: writeln(v[xs[1]]); break; case 2: v = v[0..$ - 1]; break; default: break; } } }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { int N, x; scan(N, x); auto a = readln.split.to!(int[]); auto dp = new int[][](N, N); foreach (i ; 0 .. N) { dp[0][i] = a[i]; } foreach (k ; 1 .. N) { foreach (i ; 0 .. N) { dp[k][i] = min(a[(i - k + N) % N], dp[k - 1][i]); } } debug { writefln("%(%s\n%)", dp); } long ans = infl; long solve(int k) { long res = 1L * k * x; foreach (i ; 0 .. N) { res += dp[k][i]; } return res; } foreach (k ; 0 .. N) { chmin(ans, solve(k)); } writeln(ans); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.stdio; import std.conv; import std.string; import core.bitop; void main() { auto maxCoin500 = readln.chomp.to!uint; auto maxCoin100 = readln.chomp.to!uint; auto maxCoin50 = readln.chomp.to!uint; auto total = readln.chomp.to!uint; auto count = 0U; foreach (c5h;0..maxCoin500 + 1) { if (c5h * 500 > total) break; foreach (c1h;0..maxCoin100 + 1) { if (c5h * 500 + c1h * 100 > total) break; auto requireCoin50 = (total - c5h * 500 - c1h * 100) / 50; if (requireCoin50 <= maxCoin50) { ++count; } } } writeln(count); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; import std.range; import std.math; void main(){ auto n=readln.chomp.to!int; if(n<=999)writeln("ABC"); else writeln("ABD"); }
D
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } void main() { auto S = readln.chomp; string solve() { return S == "AAA" || S == "BBB" ? "No" : "Yes"; } solve().writeln; }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { long h, w; scan(h, w); if (h == 1 || w == 1) { writeln(1); return; } auto ans = (h*w + 1) / 2; writeln(ans); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; void main() { long K = readln.chomp.to!long; int N = 50; N.writeln; N.iota.map!(i => (K+i)/N + i).map!(a => a.to!string).join(" ").writeln; } // ---------------------------------------------- void times(alias fun)(int n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { auto minElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto minimum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b < minimum) { element = a; minimum = b; } } return element; } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto maximum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b > maximum) { element = a; maximum = b; } } return element; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string; void main() { readln; auto ts = readln.split.to!(long[]); auto m = readln.chomp.to!int; long d; long sum = ts.sum; foreach (_; 0..m) { auto px = readln.split; auto p = px[0].to!int; auto x = px[1].to!long; sum += d; d = ts[p-1] - x; writeln(sum -= d); } }
D
import std.stdio, std.string, std.conv, std.algorithm; void main() { int[] tmp = readln.split.to!(int[]); int a = tmp[0], b = tmp[1], c = tmp[2]; writeln(max(c-a+b, 0)); }
D
import std.array : split; import std.conv : to; import std.stdio; import std.string : strip; void main() { auto temp = split(strip(stdin.readln())); int n = to!int(temp[0]), t = to!int(temp[1]); string start_queue = strip(stdin.readln()); char[] queue = start_queue.dup; foreach (tick; 0 .. t) { int i = 0; while (i < queue.length - 1) { if (queue[i] == 'B' && queue[i + 1] == 'G') { queue[i] = 'G'; queue[i + 1] = 'B'; i += 2; } else { ++i; } } } stdout.write(queue); }
D
import std.stdio; import std.string; import std.algorithm; import std.conv; import std.array; import std.math; import std.range; void main() { while(1) { auto mfr = readln.chomp.split.map!(to!int).array; if(mfr == [-1, -1, -1]) return; if(mfr[0 .. 2].count(-1)) writeln("F"); else{ int smf = reduce!"a+b"(0, mfr[0 .. 2]); if(smf >= 80) writeln("A"); else if(smf >= 65 && smf < 80) writeln("B"); else if(smf >= 50 && smf < 65) writeln("C"); else if(smf >= 30 && smf < 50) writeln(mfr[2] >= 50 ? "C" : "D"); else writeln("F"); } } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; long P = 10^^9+7; long[10^^5*2+50] F, RF; long pow(long x, long n) { long y = 1; while (n) { if (n%2 == 1) y = (y * x) % P; x = x^^2 % P; n /= 2; } return y; } long inv(long x) { return pow(x, P-2); } /** * P を法とする階乗、逆元を予め計算しておく。 */ void init() { F[0] = F[1] = 1; foreach (i, ref x; F[2..$]) x = (F[i+1] * (i+2)) % P; { RF[$-1] = 1; auto x = F[$-1]; auto k = P-2; while (k) { if (k%2 == 1) RF[$-1] = (RF[$-1] * x) % P; x = x^^2 % P; k /= 2; } } foreach_reverse(i, ref x; RF[0..$-1]) x = (RF[i+1] * (i+1)) % P; } /** * ある数を法とする条件下で組み合わせを求める関数 * 法とする数 P はグローバル変数として存在しなければならない * Params: * n = 全体の数 * k = 選び出す数 */ long comb(N)(N n, N k) { if (k > n) return 0; auto n_b = F[n]; // n! auto nk_b = RF[n-k]; // 1 / (n-k)! auto k_b = RF[k]; // 1 / k! auto nk_b_k_b = (nk_b * k_b) % P; // 1 / (n-k)!k! return (n_b * nk_b_k_b) % P; // n! / (n-k)!k! } void main() { init(); auto nk = readln.split.to!(long[]); auto N = nk[0]; auto K = nk[1]; long r = 1; foreach (k; 1..min(N, K+1)) { auto p = comb(N, N-k); (r += p * comb(N-1, N-k-1) % P) %= P; } writeln(r); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { auto x = lread(); if (x == 1) { writeln(0); } else { writeln(1); } } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Clue = Tuple!(long, "x", long, "y", long, "h"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); long cnt; void main() { long n, m; scan(n, m); writeln((n * (n - 1)) / 2 + (m * (m - 1)) / 2); } 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; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(int[] a) { int n = cast(int)a.length; auto indices = new int[n]; for (int i = 0; i < n; i++) { indices[a[i]-1] = i; } int ans = 0; int end = 0; int len = 0; for (int beg = 0; beg < n; beg++) { while (end < n && (beg == end || indices[end-1] < indices[end])) { len++; end++; } ans = max(ans, len); if (beg == end) end++; else len--; } return n - ans; } void main() { int n = readint; int[] a; foreach (_; 0..n) a ~= readint; writeln(calc(a)); }
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() { auto S = sread(); writeln(S[1] == 'B' ? "ARC" : "ABC"); }
D
import std.stdio, std.string, std.algorithm, std.conv, std.range; void main() { int n = readln.chomp.to!int; string minWord = readln.chomp; for (int i = 0; i < n - 1; i++) { string comp = readln.chomp; if (comp < minWord) { minWord = comp; } } minWord.writeln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio; void main() { auto N = readln.chomp.to!int; auto A = readln.split; auto dp = new long[][](N, 3); dp[0][0] = A[0].to!long; dp[0][1] = dp[0][2] = - (10L^^15); foreach (i; 1..N) { auto sign = A[i*2-1]; auto num = A[i*2].to!long; if (sign == "+") { dp[i][0] = max(dp[i-1][0] + num, dp[i-1][1] - num, dp[i-1][2] + num); dp[i][1] = max(dp[i-1][1] - num, dp[i-1][2] + num); dp[i][2] = dp[i-1][2] + num; } else { dp[i][0] = max(dp[i-1][0] - num, dp[i-1][1] + num, dp[i-1][2] - num); dp[i][1] = max(dp[i-1][0] - num, dp[i-1][1] + num, dp[i-1][2] - num); dp[i][2] = max(dp[i-1][1] + num, dp[i-1][2] - num); } } dp[N-1].reduce!(max).writeln; }
D
/+ dub.sdl: name "A" 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, p; int[] a; sc.read(n, p, a); long[2] dp = [1, 0]; foreach (d; a) { d %= 2; if (d == 0) { dp[0] *= 2; dp[1] *= 2; } else { long sm = dp[0]+dp[1]; dp[0] = dp[1] = sm; } } writeln(dp[p]); 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
import std.stdio; import std.algorithm; import std.string; import std.range; import std.array; import std.conv; import std.complex; import std.math; import std.ascii; import std.bigint; import std.container; void main() { int n = to!int(readln().strip()); int[][string] d = ["N": [0,1], "E": [1,0], "S": [0,-1], "W": [-1, 0]]; while(n) { int[][] p; foreach(i; 0..n) { p ~= array(map!(to!int)(readln().strip().split())); } int m = to!int(readln().strip()); bool[21][21] q; int x = 10; int y = 10; foreach(i; 0..m) { auto s = readln().strip().split(); auto t = to!int(s[1]); foreach(j; 0..t) { x += d[s[0]][0]; y += d[s[0]][1]; q[x][y] = true; } } bool f = true; foreach(pi; p){ f &= q[pi[0]][pi[1]]; } writeln(f?"Yes":"No"); n = to!int(readln().strip()); } }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { const N = readln.chomp.to!long; auto Hs = readln.split.to!(long[]); foreach_reverse(i; 0..N-1) { if (Hs[i] > Hs[i+1]+1) { writeln("No"); return; } else if (Hs[i] == Hs[i+1]+1) { Hs[i]--; } } writeln("Yes"); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!int).array; int cnt = 1; foreach (a; A) { if (cnt == a) { cnt += 1; } } int ans = N - cnt + 1; writeln(cnt == 1 ? -1 : ans); }
D
import std.stdio, std.string, std.conv, std.math; void main() { readln.chomp.to!int.pow(3).writeln; }
D
import std.stdio; import std.array; import std.conv; void main(){ string[] d=split(readln()); if(to!int(d[0])<=to!int(d[2])&&to!int(d[2])<=to!int(d[1])){ writeln("Yes"); }else{ writeln("No"); } }
D
import std.stdio; import std.string; import std.conv; int main() { string s; while((s = readln()).length != 0) { int count = 0; int number = to!(int)(chomp(s)); foreach(int i;0..10) { foreach(int j;0..10) { foreach(int k;0..10) { foreach(int l;0..10) { if(i+k+j+l == number) count++; } } } } writeln(count); } return 0; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto S = readln.chomp.to!(wchar[]); int i; long r; foreach (j, s; S) { if (s == 'W') { r += j - i; ++i; } } writeln(r); }
D
import std.stdio, std.string, std.range, std.algorithm, std.math, std.typecons, std.conv; void main() { auto a = readln.split.to!(int[]); auto A = a[0], B = a[1], C = a[2]; foreach (i; 0.. B) { if (A * (i+1) % B == C) { writeln("YES"); return; } } writeln("NO"); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; enum MAX = 1_000_100; ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias INCOME = Tuple!(long, "after", long, "salary"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long n, m; scan(n, m); auto a = new string[](n); auto b = new string[](m); foreach (ref e; a) e = sread(); foreach (ref e; b) e = sread(); bool flag; foreach (i; iota(n - m + 1)) { foreach (j; iota(n - m + 1)) { bool check = true; foreach (col; iota(m)) { foreach (row; iota(m)) { if (a[i + col][j + row] != b[col][row]) check = false; } } flag = flag || check; } } if (flag) writeln("Yes"); else writeln("No"); } 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; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; alias E = Tuple!(int, "from", int, "to", long, "c"); E[5000] T; long[2500] CS; void main() { auto nmp = readln.split.to!(int[]); auto N = nmp[0]; auto M = nmp[1]; auto P = nmp[2].to!long; foreach (i; 0..M) { auto abc = readln.split.to!(int[]); T[i] = E(abc[0]-1, abc[1]-1, abc[2] - P); } foreach (i; 1..N) CS[i] = long.min / 2; foreach (i; 0..N) { foreach (j; 0..M) { auto e = T[j]; if (CS[e.to] < CS[e.from] + e.c) { CS[e.to] = CS[e.from] + e.c; } } } auto r = CS[N-1]; foreach (i; 0..N) { foreach (j; 0..M) { auto e = T[j]; if (CS[e.to] < CS[e.from] + e.c) { CS[e.to] = CS[e.from] + e.c; } } } writeln(r == CS[N-1] ? max(0, r) : -1); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { long[] ns; foreach (k; 3..10) { foreach (x; 0..3^^k) { int a, b, c; long n; foreach (_; 0..k) { n *= 10; if (x%3 == 0) { n += 3; ++a; } else if (x%3 == 1) { n += 5; ++b; } else { n += 7; ++c; } x /= 3; } if (a && b && c) ns ~= n; } } sort(ns); auto N = readln.chomp.to!long; int i; while (i < ns.length && ns[i] <= N) ++i; writeln(i); }
D
import std.stdio, std.string, std.array, std.conv, std.array, std.algorithm, std.numeric; void main(){ while (true){ auto a = readln.strip.split.map!(to!long).array; if (a.length == 0) break; auto g = gcd(a[0], a[1]); writeln(g, " ", a[0] / g * a[1]); } }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop; // dfmt on void main() { auto S = sread(); repeat('x').take(S.length).array.writeln(); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n, m; scan(n, m); auto c = new int[][](n, n); fillAll(c, inf3); foreach (i ; 0 .. n) { c[i][i] = 0; } foreach (i ; 0 .. m) { int ai, bi, ci; scan(ai, bi, ci); ai--, bi--; c[ai][bi] = c[bi][ai] = ci; } auto d = new int[][](n, n); fillAll(d, inf3); foreach (i ; 0 .. n) { foreach (j ; 0 .. n) { d[i][j] = c[i][j]; } } foreach (k ; 0 .. n) { foreach (i ; 0 .. n) { foreach (j ; 0 .. n) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } int ans; foreach (i ; 0 .. n) { foreach (j ; i + 1 .. n) { if (c[i][j] != inf3 && c[i][j] > d[i][j]) { ans++; } } } writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.string; void main() { char[] line = cast(char[]) readln.chomp; for(int i = 0; i < line.length - 4; i++) { char[] str = line[i..(i + 5)]; if (str == "apple") line[i..(i + 5)] = cast(char[])"peach"; else if (str == "peach") line[i..(i + 5)] = cast(char[])"apple"; } writeln(line); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { auto s = readln.chomp; char[] cs; for (int i = 0; i < s.length; i += 2) cs ~= s[i]; writeln(cs); }
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 += rhs; if (val >= modulus) { val -= modulus; } } else if (op == "-") { if (val < rhs) { val += modulus; } val -= rhs; } else if (op == "*") { val = val * rhs % modulus; } else if (op == "/") { this *= ModInt(rhs).inv; } else if (op == "^^") { ModInt res = 1; ModInt t = this; while (rhs > 0) { if (rhs % 2 != 0) { res *= t; } t *= t; rhs /= 2; } this = res; } else { enforce(false, op ~ "= is not implemented."); } return this; } ModInt opUnary(string op)() { static if (op == "++") { this += 1; } else if (op == "--") { this -= 1; } else { enforce(false, op ~ " is not implemented."); } return this; } ModInt opBinary(string op)(const ulong rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinary(string op)(ref const ModInt rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinary(string op)(const ModInt rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinaryRight(string op)(const ulong lhs) const { mixin("return ModInt(this) " ~ op ~ "= lhs;"); } long opCmp(ref const ModInt rhs) const { return cast(long)value - cast(long)rhs.value; } bool opEquals(const ulong rhs) const { return value == ModInt(rhs).value; } ModInt inv() const { ModInt ret = this; ret ^^= modulus - 2; return ret; } string toString() const { import std.format : format; return format("%s", val); } } // }}} // alias {{{ alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less); alias MinHeap(T) = Heap!(T, "a > b"); // }}} // memo {{{ /* - ある値が見つかるかどうか <https://dlang.org/phobos/std_algorithm_searching.html#canFind> canFind(r, value); -> bool - 条件に一致するやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#filter> // 2で割り切れるやつ filter!"a % 2 == 0"(r); -> Range - 合計 <https://dlang.org/phobos/std_algorithm_iteration.html#sum> sum(r); - 累積和 <https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold> // 今の要素に前の要素を足すタイプの一般的な累積和 // 累積和のrangeが帰ってくる(破壊的変更は行われない) cumulativeFold!"a + b"(r, 0); -> Range - rangeをarrayにしたいとき array(r); -> Array - 各要素に同じ処理をする <https://dlang.org/phobos/std_algorithm_iteration.html#map> // 各要素を2乗 map!"a * a"(r) -> Range - ユニークなやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#uniq> uniq(r) -> Range - 順列を列挙する <https://dlang.org/phobos/std_algorithm_iteration.html#permutations> permutation(r) -> Range - ある値で埋める <https://dlang.org/phobos/std_algorithm_mutation.html#fill> fill(r, val); -> void - バイナリヒープ <https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap> // 昇順にするならこう(デフォは降順) BinaryHeap!(Array!T, "a > b") heap; heap.insert(val); heap.front; heap.removeFront(); - 浮動小数点の少数部の桁数設定 // 12桁 writefln("%.12f", val); - 浮動小数点の誤差を考慮した比較 <https://dlang.org/phobos/std_math.html#.approxEqual> approxEqual(1.0, 1.0099); -> true - 小数点切り上げ <https://dlang.org/phobos/std_math.html#.ceil> ceil(123.4); -> 124 - 小数点切り捨て <https://dlang.org/phobos/std_math.html#.floor> floor(123.4) -> 123 - 小数点四捨五入 <https://dlang.org/phobos/std_math.html#.round> round(4.5) -> 5 round(5.4) -> 5 */ // }}} void main() { auto cin = new Scanner; int n; cin.scan(n); int[] ar = cin.nextArray!int(n - 1); int[] sum = new int[n]; foreach (e; ar) { sum[e - 1]++; } foreach (i; 0 .. n) { writeln(sum[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)); } 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 a = RD; writeln(a + a^^2 + a^^3); stdout.flush; debug readln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string; int[] LONGM = [1,3,5,7,8,10,12]; int[] SHORTM = [4,6,9,11]; void main() { auto xy = readln.split.to!(int[]); writeln( xy.all!(i => LONGM.canFind(i)) || xy.all!(i => SHORTM.canFind(i)) ? "Yes" : "No" ); }
D
import std.stdio, std.string, std.conv; void main() { auto input = getStdin!(string[]); foreach (line; input) { auto num = line.split(" "); int w = num[0].to!(int); int h = num[1].to!(int); if (w == 0 && h == 0) { break; } for (int i_w = 0; i_w < w; i_w++) { for (int j_h = 0; j_h < h; j_h++) { "#".write; } "".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.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; int[] dist, prev; void main() { int N; scan(N); foreach (x ; 1 .. 10) { if (N % x == 0 && N / x < 10) { writeln("Yes"); return; } } writeln("No"); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.string, std.conv; import std.algorithm, std.array, std.range; auto solve(string S_) { immutable N=S_.chomp.to!int(); immutable A=readln.chomp(); immutable B=readln.chomp(); immutable C=readln.chomp(); int k=0; foreach(i;0..N) { if(A[i]!=B[i]) { ++k; if(A[i]!=C[i] && B[i]!=C[i]) ++k; } else if(B[i]!=C[i]) { ++k; } } return k; } void main(){ for(string s; (s=readln.chomp()).length;) writeln(solve(s)); }
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 readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}} void main() { int n, m; readV(n, m); int[] a, b; readC(n, a, b); int[] c, d; readC(m, c, d); foreach (i; 0..n) { auto x = 0, yi = int.max; foreach (j; 0..m) { auto y = (a[i]-c[j]).abs + (b[i]-d[j]).abs; if (y < yi) { yi = y; x = j; } } writeln(x+1); } }
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() { auto n = lread(); auto prime = new long[](n); long len; foreach (i; iota(2, n + 1)) { long flag = 1; foreach (j; iota(2, i)) flag = flag && i % j; if (flag) prime[len++] = i; } auto prime_fact = new long[](n + 1); foreach (i; iota(1, n + 1)) { foreach (j; iota(len)) { while (i > 0 && !(i % prime[j])) { i /= prime[j]; prime_fact[prime[j]]++; } } } long ans = 1; foreach (e; prime_fact) { if (e) { ans *= e + 1; ans %= MOD; } } ans.writeln(); }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; immutable long mod = 998_244_353; immutable int limit = 100_005; long powMod (long a, int p) { long res = 1; for ( ; p != 0; p >>= 1) { if (p & 1) { res = (res * 1L * a) % mod; } a = (a * 1L * a) % mod; } return res; } alias invMod = a => powMod (a, mod - 2); auto fact = new long [limit]; long choose (int n, int k) { auto res = fact[n]; res = (res * 1L * invMod (fact[n - k])) % mod; res = (res * 1L * invMod (fact[k])) % mod; return res; } void main () { fact[0] = 1; foreach (i; 1..limit) { fact[i] = (fact[i - 1] * 1L * i) % mod; } auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto n = readln.strip.to !(int); auto a = readln.strip.map !(q{a == '1'}).array; int zeroes = 0; int pairs = 0; for (int i = 0; i < n; i++) { if (!a[i]) { zeroes += 1; } else if (i + 1 < n && a[i + 1]) { pairs += 1; i += 1; } } writeln (choose (zeroes + pairs, pairs)); } }
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 A, B, C; scan(A, B, C); writeln((A + B + C < 22) ? "win" : "bust"); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { int a, b; scan(a, b); writeln((a + b + 1) / 2); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { auto nm = readints; int n = nm[0], m = nm[1]; auto uf = new UnionFind(n); for (int i = 0; i < m; i++) { auto xyz = readints; int x = xyz[0], y = xyz[1]; uf.unite(x, y); } bool[int] d; for (int i = 1; i <= n; i++) { d[uf.root(i)] = true; } auto ans = d.keys.length; writeln(ans); } class UnionFind { private int[] _data; private int[] _nexts; // 次の要素。なければ -1 private int[] _tails; // 末尾の要素 this(int n) { _data = new int[](n + 1); _nexts = new int[](n + 1); _tails = new int[](n + 1); _data[] = -1; _nexts[] = -1; for (int i = 0; i < _tails.length; i++) _tails[i] = i; } int root(int a) { if (_data[a] < 0) return a; return _data[a] = root(_data[a]); } bool unite(int a, int b) { int ra = root(a); int rb = root(b); if (ra != rb) { if (_data[rb] < _data[ra]) swap(ra, rb); _data[ra] += _data[rb]; // ra に rb を繋げる _data[rb] = ra; // ra の末尾の後ろに rb を繋げる _nexts[_tails[ra]] = rb; // ra の末尾が rb の末尾になる _tails[ra] = _tails[rb]; } return ra != rb; } bool isSame(int a, int b) { return root(a) == root(b); } /// a が属する集合のサイズ int groupSize(int a) { return -_data[root(a)]; } /// a が属する集合の要素全て void doEach(int a, void delegate(int) fn) { auto node = root(a); while (node != -1) { fn(node); node = _nexts[node]; } } }
D
import std.stdio; import std.ascii; import std.conv; import std.string; import std.algorithm; import std.range; import std.functional; import std.math; import core.bitop; void main() { string s = readln.chomp; long k = readln.chomp.to!long; long firstone = -1; foreach (size_t i, char c; s) if (c != '1') { firstone = i; break; } // s[0..firstone].writeln; if (k <= firstone) { "1".writeln; return; } if (firstone == -1) { s[0].writeln; return; } s[firstone].writeln; }
D
import std.stdio,std.string,std.conv,std.algorithm; import std.algorithm:rev=reverse; void main(){ auto input=readln.chomp.split.to!(ulong[]); auto n=input[0]; auto a=input[1]; auto b=input[2]; auto ans=n/(a+b)*a; ans+=min(a,n%(a+b)); ans.writeln; }
D
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math, std.typecons; import core.stdc.stdio; // foreach, foreach_reverse, writeln alias Tuple!(int, int) Pair; void main() { int n; scanf("%d", &n); int[][] graph = new int[][n]; foreach (i; 0..n-1) { int v, w; scanf("%d%d", &v, &w); v--; w--; graph[v] ~= w; graph[w] ~= v; } Pair dfs(int v, int depth, int parent) { Pair ret = Pair(depth, v); foreach (u; graph[v]) { if (u == parent) continue; ret = max(ret, dfs(u, depth+1, v)); } return ret; } int s = dfs(0,0,-1)[1]; int t = dfs(s,0,-1)[1]; int[] path; int[] used = new int[n]; bool getpath(int v, int dist, int parent) { if (v == dist) { path ~= v; used[v] = true; return true; } foreach (u; graph[v]) { if (u == parent) continue; if (getpath(u, dist, v)) { path ~= v; used[v] = true; return true; } } return false; } getpath(s, t, -1); int[] generate() { int[] ret; foreach (v; path) { int cnt = 0; foreach (u; graph[v]) { if (used[u]) continue; cnt++; } int id = to!int(ret.length)+1; foreach (i; 0..cnt) { ret ~= id+1+i; } ret ~= id; } return ret; } int[] ans = generate(); path.reverse(); int[] ant = generate(); if (ans.length < n) { writeln(-1); } else { ans = min(ans, ant); foreach (i; ans) writeln(i); } }
D
import std.stdio; import std.conv; import std.string; import std.algorithm; import std.range; void main() { auto input = readln.split.to!(int[]); auto X = input[0]; auto Y = input[1]; auto Z = input[2]; auto ans = X / (Y + Z); (ans * (Y + Z) + Z > X ? ans - 1 : 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 N, M; scan(N, M); auto G = new long[][](N); foreach (i; 0 .. M) { long a, b; scan(a, b); a--, b--; G[a] ~= b; G[b] ~= a; } auto ans = new long[](N); ans[] = -1; ans[0] = 0; DList!long Q; Q.insertBack(0); while (!Q.empty) { long v = Q.front; Q.removeFront(); dprint(v, G[v]); foreach (w; G[v]) { dprint(ans[w]); if (ans[w] == -1) { ans[w] = v; Q.insertBack(w); } } } writeln("Yes"); foreach (a; ans[1 .. $]) writeln(a + 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 = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "a", long, "b"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long a, b; scan(a, b); foreach (i; iota(MAX).map!(x => x + 1)) { if (cast(long)(i * 0.08) == a && cast(long)(i * 0.10) == b) { i.writeln(); return; } } writeln(-1); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.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; } } enum MOD = (10 ^^ 9) + 7; void main() { auto S = sread(); long i = S.length; loop_w: while (0 < i) { foreach (t; ["dream", "dreamer", "erase", "eraser"]) { auto s = S[max(0, i - t.length) .. i]; if (s == t) { i -= s.length; continue loop_w; } } writeln("NO"); return; } writeln("YES"); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto a = RDA; if (n == 1) { ans[ti] = 1; continue; } ans[ti] = 3; bool f(int i, int j, int k) { return inside(a[j], min(a[i], a[k]), max(a[i], a[k])+1); } foreach (i; 0..n-2) { if (f(i, i+1, i+2)) ans[ti] += 2; else { ans[ti] += 3; if (i + 3 < n) { bool ok = true; (){ foreach (l; i..i+2) { foreach (m; l+1..i+4) { foreach (r; m+1..i+4) { if (f(l, m, r)) { ok = false; return; } } } }}(); if (ok) ++ans[ti]; } } } } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 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 n = RD!int; auto a = RDA(-1); auto pos = new int[][](n+1); foreach (i; 0..n) { pos[a[i]] ~= i; } int[] a0 = [n], a1 = [n]; foreach (i; 0..n) { pos[a[i]].popFront; if (a0.back == a[i]) a1 ~= a[i]; else if (a1.back == a[i]) a0 ~= a[i]; else { int x = n, y = n; if (!pos[a0.back].empty) x = pos[a0.back].front; if (!pos[a1.back].empty) y = pos[a1.back].front; if (x < y) a0 ~= a[i]; else a1 ~= a[i]; } } debug writeln("a0:", a0); debug writeln("a1:", a1); a0.popFront; a1.popFront; long ans; if (!a0.empty) { ++ans; foreach (i; 1..a0.length) { if (a0[i-1] != a0[i]) ++ans; } } if (!a1.empty) { ++ans; foreach (i; 1..a1.length) { if (a1[i-1] != a1[i]) ++ans; } } writeln(ans); stdout.flush; debug readln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; long[3] DR = [1, 100, 10000]; void main() { auto dn = readln.split.to!(long[]); auto D = dn[0]; auto N = dn[1]; if (N == 100) ++N; writeln(N * DR[D]); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { auto L = readln.chomp.map!(c => c == '1').array; auto N = L.length.to!int; auto dp = new long[][](N + 1, 2); dp[0][0] = 1; foreach (i ; 0 .. N) { if (L[i]) { dp[i + 1][0] = dp[i][0] * 2 % mod; dp[i + 1][1] = (dp[i][0] + dp[i][1] * 3 % mod) % mod; } else { dp[i + 1][0] = dp[i][0]; dp[i + 1][1] = dp[i][1] * 3 % mod; } } auto ans = (dp[N][0] + dp[N][1]) % mod; writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; bool calc(int[][] g) { for (int i = 0; i <= 100; i++) { int a1 = i; int b1 = g[0][0] - a1; int b2 = g[0][1] - a1; int b3 = g[0][2] - a1; int a2 = g[1][0] - b1; int a3 = g[2][0] - b1; auto as = [a1, a2, a3]; auto bs = [b1, b2, b3]; bool ok = true; for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { int x = as[j] + bs[k]; if (x < 0 || x > 100 || x != g[j][k]) { ok = false; } } } if (ok) return true; } return false; } void main() { auto g = new int[][](3, 3); for (int i = 0; i < 3; i++) { auto xs = readints; for (int j = 0; j < 3; j++) { g[i][j] = xs[j]; } } writeln(calc(g) ? "Yes" : "No"); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio; void main() { auto s = readln.split.map!(to!int); auto H = s[0]; auto W = s[1]; auto K = s[2]; auto A = iota(H).map!(_ => readln.chomp).array; int ans = 1 << 29; auto cnt = new int[](H); auto cnt2 = new int[](H); outer: foreach (mask; 0..1<<(H-1)) { int tmp = mask.popcnt; cnt[] = 0; cnt2[] = 0; foreach (j; 0..W) { int p = 0; bool flag = false; foreach (i; 0..H) { cnt2[p] += A[i][j] == '1'; if (cnt2[p] > K) continue outer; if (cnt[p] + cnt2[p] > K) flag = true; if (mask & (1 << i)) p += 1; } if (flag) { tmp += 1; foreach (i; 0..cnt.length) { cnt[i] = cnt2[i]; } } else { foreach (i; 0..cnt.length) { cnt[i] += cnt2[i]; } } cnt2[] = 0; } ans = min(ans, tmp); } ans.writeln; }
D