code
stringlengths
4
1.01M
language
stringclasses
2 values
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; } bool calc(int rows, int cols, int k) { for (int i = 0; i <= rows; i++) { for (int j = 0; j <= cols; j++) { int n = (cols - j) * i + (rows - i) * j; if (n == k) return true; } } return false; } void main() { auto xs = readints; int n = xs[0], m = xs[1], k = xs[2]; writeln(calc(n, m, k) ? "Yes" : "No"); }
D
import std.conv, std.stdio; import std.algorithm, std.array, std.range, std.string; void main() { readln; auto a = Solver(readln.chomp.split.to!(int[])); immutable q = readln.chomp.to!size_t; foreach (_; 0..q) { auto buf = readln.chomp.split.to!(int[]); a.query(buf[0], buf[1]).writeln; } } private: struct Solver { this (int[] _a) { this.a = new long[10001]; foreach (e; _a) { this.a[e] += 1; this.s += e; } } long query(in int b, in int c) { a[c] += a[b]; s += (c - b) * a[b]; a[b] = 0; return s; } invariant { assert (s == _s); } long[] a; long s; private long _s() @property const { long ret; foreach (i, e; a) ret += i * e; return ret; } }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; void times(alias fun)(int n) { foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } // fold was added in D 2.071.0. 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); } } } void main() { readln.chomp.uniq.array.length.pipe!"a-1".writeln; }
D
import std.stdio,std.conv,std.algorithm,std.array; int oneint(){ return readln().split().map!(to!int).array()[0];} //read from stdin as only one int void main(){ for(int i=1,x;(x = oneint())!=0;i++){ writeln("Case ",i,": ",x); } }
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; const mod = 10^^9 + 7; long calc(string s) { auto dp = new long[][](s.length + 1, 4); dp[0][0] = 1; for (int i = 1; i <= s.length; i++) { long factor = s[i-1] == '?' ? 3 : 1; for (int j = 0; j <= 3; j++) { dp[i][j] = (dp[i-1][j] * factor) % mod; } auto c = s[i-1]; if (c == 'A' || c == '?') dp[i][1] = (dp[i][1] + dp[i-1][0]) % mod; if (c == 'B' || c == '?') dp[i][2] = (dp[i][2] + dp[i-1][1]) % mod; if (c == 'C' || c == '?') dp[i][3] = (dp[i][3] + dp[i-1][2]) % mod; } return dp[s.length][3]; } void main() { string s = read!string; writeln(calc(s)); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.bigint; void main() { auto n = readln.chomp.to!int; auto s = readln.chomp; int r = int.max; foreach (i; 0..n-3) { int t; t += min(abs(s[i] - 39), abs(s[i] - 65), abs(s[i] - 91)); t += min(abs(s[i+1] - 42), abs(s[i+1] - 67), abs(s[i+1] - 93)); t += min(abs(s[i+2] - 58), abs(s[i+2] - 84), abs(s[i+2] - 110)); t += min(abs(s[i+3] - 45), abs(s[i+3] - 71), abs(s[i+3] - 97)); r = min(r, t); } writeln(r); }
D
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } void main() { try { for (; ; ) { const B = readInt(); const G = readInt(); const N = readInt(); int ans; foreach (b; 0 .. N + 1) { const g = N - b; if (0 <= b && b <= B && 0 <= g && g <= G) { ++ans; } } writeln(ans); } } catch (EOFException e) { } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(int a, int b, int c, int x) { if (x % 100 == 50) { return c == 0 ? 0 : calc(a, b, c - 1, x - 50); } assert(x % 100 == 0); // x は 100 の倍数で考える int ans = 0; // 500 円の枚数を固定 for (int i = 0; i <= a; i++) { int m = x - 500 * i; if (m < 0) break; int t = m / 100; if (b + c/2 < t) continue; // b, c で m 円払えない // 100 円の枚数を最小化する int lo = t - min(t, c / 2); // 100 円の枚数を最大化する int hi = min(t, b); ans += hi - lo + 1; } return ans; } void main() { int a = readint; int b = readint; int c = readint; int x = readint; writeln(calc(a, b, c, x)); }
D
import std.stdio; import std.string; import std.array; // split import std.conv; // to void main() { string s = chomp(readln()); int a = to!int(s); // 第0要素を整数に変換 if(a > 999){ writeln("ABD"); } else { writeln("ABC"); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto S = readln.chomp; if (S == "AAA" || S == "BBB") { writeln("No"); } else { 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.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto q = RD!int; auto ans = new long[](q); long[] list = [1]; while (true) { auto x = list[$-1] * 3; if (x < list[$-1]) break; list ~= x; } foreach (i; 0..q) { auto n = RD!int; int pos; while (list[pos] < n) { ++pos; } if (pos != 0) --pos; auto l = 2L^^pos; auto r = 2L^^(list.length); foreach (j; l..r) { long x; foreach (k; 0..32) { auto bit = 1L << k; if (j & bit) { x += list[k]; } } if (x >= n) { ans[i] = x; break; } } } foreach (e; ans) writeln(e); stdout.flush(); debug readln(); }
D
import core.bitop; import std.algorithm; import std.array; import std.ascii; import std.container; import std.conv; import std.format; import std.math; import std.random; import std.range; import std.stdio; import std.string; import std.typecons; void main() { immutable k = readln.chomp.to!long; enum N = 50; long v = k / N; immutable remain = k % N; N.writeln; stderr.writeln(v, " ", remain); foreach (i; 0..N) { if (i) { write = " "; } if (i < remain) { write = v + N * 2 - remain; } else { write = v + N - (remain + 1); } } writeln; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = readln.chomp.to!size_t; auto t = readln.split.to!(int[]), s = t.sum; auto m = readln.chomp.to!size_t; foreach (_; 0..m) { auto rd = readln.splitter; auto p = rd.front.to!size_t-1; rd.popFront(); auto x = rd.front.to!int; writeln(s - t[p] + x); } }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; void times(alias fun)(int n) { foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } // fold was added in D 2.071.0. 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); } } } void main() { 3.rep!(() => readln.chomp.map!(c => c-'a').pipe!(str => DList!int(str))).pipe!((ary) { int now = 0; while(!ary[now].empty) { int tmp = ary[now].front; ary[now].removeFront; now = tmp; } return now; }).pipe!(i => "ABC"[i]).writeln; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void minAssign(T)(ref T dst, T src) { dst = min(dst, src); } alias sread = () => readln.chomp(); void main() { long N = lread(); static immutable dp = () { enum Nmax = 10^^5; auto dp = new long[](Nmax + 1); dp[] = long.max; dp[0] = 0; foreach (i; 0 .. Nmax) if (dp[i] != long.max) { dp[i + 1].minAssign(dp[i] + 1); size_t j = 6; while (i + j < dp.length) { dp[i + j].minAssign(dp[i] + 1); j *= 6; } j = 9; while (i + j < dp.length) { dp[i + j].minAssign(dp[i] + 1); j *= 9; } } return dp; }(); dp[N].writeln(); }
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 h = lread(); func(h).writeln(); } long func(long h) { if (h == 1) { return 1; } return func(h / 2) * 2 + 1;//2体に分裂する+1,h/2を2体倒す(h/2)*2 } 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, 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(){ string s = scan; int x; foreach(c; s){ x += (c - '0'); x %= 9; } if(x % 9 == 0) "Yes".writeln; else "No".writeln; }
D
import std.algorithm; import std.conv; import std.stdio; import std.string; import std.math; void main() { auto ab = readln.split.map!( to!int ); auto add = ab[ 0 ] + ab[ 1 ]; auto sub = ab[ 0 ] - ab[ 1 ]; auto mul = ab[ 0 ] * ab[ 1 ]; writeln( max( add, sub, mul ) ); }
D
import std.stdio, std.string, std.conv; import std.typecons; import std.algorithm, std.array, std.range, std.container; import std.math; void main() { auto N = readln.split[0].to!int; auto S = readln.split[0].to!(dchar[]); ulong[] wnum = [0], bnum = [S.count('#')]; foreach (d; S) { if (d == '.') wnum ~= wnum[$-1]+1, bnum ~= bnum[$-1]; else wnum ~= wnum[$-1], bnum ~= bnum[$-1]-1; } //writeln(wnum, bnum); ulong ans = int.max; foreach (i; 0 .. wnum.length) { ulong tmp; tmp = i-wnum[i] + (N-i)-bnum[i]; ans = min(ans, tmp); } writeln(ans); }
D
import std.stdio; import std.string; import std.conv; import std.array; import std.algorithm; import std.math; void main() { int n = readln.chomp.to!int; auto a = readln.chomp.split.map!(to!int).array; auto m = reduce!(min, max)(a); if (m[0] == m[1]) { writeln(0); } else if (m[0] >= 0) { writeln(n - 1); for (int i = 0; i < n - 1; ++i) { writeln(i + 1, " ", i + 2); } } else if (m[1] < 0) { writeln(n - 1); for (int i = n - 1; i > 0; --i) { writeln(i + 1, " ", i); } } else { if (abs(m[0]) > abs(m[1])) { writeln(n - 1 + n - 1); int p; for (int i = 0; i < n; ++i) { if (a[i] == m[0]) { p = i; break; } } for (int i = 0; i < n; ++i) { if (i == p) continue; writeln(p + 1, " ", i + 1); } for (int i = n - 1; i > 0; --i) { writeln(i + 1, " ", i); } } else { writeln(n - 1 + n - 1); int p; for (int i = 0; i < n; ++i) { if (a[i] == m[1]) { p = i; break; } } for (int i = 0; i < n; ++i) { if (i == p) continue; writeln(p + 1, " ", i + 1); } for (int i = 0; i < n - 1; ++i) { writeln(i + 1, " ", i + 2); } } } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; enum PS = [300000, 200000, 100000]; void main() { auto xy = readln.split.to!(int[]); auto X = xy[0]; auto Y = xy[1]; int r; if (X <= 3) r += PS[X-1]; if (Y <= 3) r += PS[Y-1]; if (X == 1 && Y == 1) r += 400000; writeln(r); }
D
/+ dub.sdl: name "B" dependency "dunkelheit" version=">=0.9.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; int main() { Scanner sc = new Scanner(stdin); int n; sc.read(n); int[] p = new int[n]; int[] rp = new int[n]; foreach (i; 0..n) { sc.read(p[i]); p[i]--; rp[p[i]] = i; } int ans = 0; int l = 0; while (l < n) { int r = l+1; while (r < n && rp[r-1] < rp[r]) r++; ans = max(ans, r-l); l = r; } writeln(n-ans); return 0; } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/scanner.d */ // module dkh.scanner; // import dkh.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; 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 unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(bool enforceEOF = false, T, Args...)(ref T x, auto ref Args args) { import std.exception; enforce(readSingle(x)); static if (args.length == 0) { enforce(enforceEOF == false || !succ()); } else { read!enforceEOF(args); } } void read(bool enforceEOF = false, Args...)(auto ref Args args) { import std.exception; static if (args.length == 0) { enforce(enforceEOF == false || !succ()); } else { enforce(readSingle(args[0])); read!enforceEOF(args); } } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std; void main(){ auto s = readln.chomp; if (s == "ABC") "ARC".writeln; else "ABC".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, std.bitmanip; immutable long INF = 1L << 59; void main() { auto s = readln.split.map!(to!int); auto D = s[0]; auto G = s[1].to!long; auto PC = D.iota.map!(_ => readln.split.map!(to!long).array).array; int ans = 1 << 29; foreach (mask; 0..(1<<D)) { long tmp = 0; int cnt = 0; foreach (i; 0..D) { if (mask & (1 << i)) { tmp += PC[i][0] * (i + 1) * 100 + PC[i][1]; cnt += PC[i][0]; } } for (int i = D-1; i >= 0 && tmp < G; --i) { if (mask & (1 << i)) continue; long hoge = (G - tmp - 1) / ((i + 1) * 100) + 1; hoge = min(hoge, PC[i][0]); tmp += hoge * (i + 1) * 100; cnt += hoge; } ans = min(ans, cnt); } ans.writeln; }
D
import std.stdio; import std.algorithm; import std.array; import std.string; import std.conv; void main(){ auto buf = readln.chomp.split.to!(int[]); swap(buf[0], buf[1]); swap(buf[0], buf[2]); foreach(i; buf){ write(i, " "); } }
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; long INF = long.max/3; void main() { int N = readln.chomp.to!int; long[] as = new long[N]; int[] bs = new int[N+1]; string[] input = ["+"] ~ readln.split.array; foreach(i; 0..N) { as[i] = input[2*i+1].to!long; bs[i] = input[2*i] == "-" ? 1 : 0; } long f(int i, int j) { if (j>2) return memoize!f(i, 2); if (i==N) return 0; return (j+1).iota.map!( k => memoize!f(i+1, k + bs[i+1]) ).reduce!max + as[i]*((-1)^^j); } f(0, 0).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
module main; import std.stdio : readln, writeln, write, writefln, writef; import std.conv : to; import std.array : split, replace; import std.string : strip; import std.algorithm : max, min, map, reduce, sort, reverse; import std.functional : memoize; version = A; version (A) { void main() { auto sequence = read_one!string(); auto instructions = read_one!string(); size_t position = 0; foreach (instruction; instructions) { if (sequence[position] == instruction) { position += 1; } } writeln(position + 1); } } version (B) { void main() { auto n = read_one!int(); } } version (C) { void main() { auto n = read_one!int(); } } version (D) { void main() { auto n = read_one!int(); } } version (E) { void main() { } } T read_one(T)() { return readln().strip().to!T(); } T[] read_some(T)() { T[] ret; foreach (e; readln().strip().split()) { ret ~= e.to!T(); } return ret; } T[m] read_fixed(int m, T)() { T[m] ret; foreach (i, e; readln().strip().split()) { ret[i] = e.to!T(); } return ret; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int a, b; rd(a, b); auto s=readln.chomp.to!(char[]); if(count(s, '-')!=1){writeln("No"); return;} if(s[a]=='-') writeln("Yes"); else writeln("No"); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } } 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)); } double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto s = RD!string; auto ss = RD!string; (){ foreach (i; 0..s.length) { foreach (j; i..min(s.length, i+ss.length)) { auto len = j - i + 1; bool ok = true; foreach (k; 0..len) { if (s[i+k] != ss[k]) { ok = false; break; } } if (!ok) continue; foreach_reverse (k; 0..ss.length-len) { if (j <= k) { ok = false; break; } auto p = j - k - 1; if (s[p] != ss[len+k]) { ok = false; break; } } if (ok) { ans[ti] = true; return; } } }}(); } foreach (e; ans) { writeln(e ? "YES" : "NO"); } stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random; import std.typecons, std.functional, std.traits,std.concurrency; import std.algorithm, std.container; import core.bitop, core.time, core.memory; import std.bitmanip; import std.regex; enum INF = long.max/3; enum MOD = 10L^^9+7; //辞書順順列はiota(1,N),nextPermituionを使う void end(T)(T v) if(isIntegral!T||isSomeString!T||isSomeChar!T) { import core.stdc.stdlib; writeln(v); exit(0); } T[] scanArray(T = long)() { static char[] scanBuf; readln(scanBuf); return scanBuf.split.to!(T[]); } dchar scanChar() { int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } return cast(dchar)c; } T scanElem(T = long)() { import core.stdc.stdlib; static auto scanBuf = appender!(char[])([]); scanBuf.clear; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { scanBuf ~= cast(char) c; c = getchar; } return scanBuf.data.to!T; } dchar[] scanString(){ return scanElem!(dchar[]); } void main() { auto a=scanChar; foreach(e;['a','i','u','e','o'])if(e==a)end("vowel");end("consonant"); }
D
import std.stdio,std.string,std.conv; void main(){ writeln( readln().chomp().to!int^^3 ); }
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 = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto n = RD!int; auto h1 = RDA; auto h2 = RDA; auto dp = new long[][](n+1, 3); foreach (i; 0..n) { dp[i+1][0] = max(dp[i][1], dp[i][2]) + h1[i]; dp[i+1][1] = max(dp[i][0], dp[i][2]) + h2[i]; dp[i+1][2] = max(dp[i][0], max(dp[i][1], dp[i][2])); } writeln(max(dp[n][0], max(dp[n][1], dp[n][2]))); stdout.flush(); debug readln(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; int[][10^^5] T; void main() { auto N = readln.chomp.to!int; foreach (b; 1..N) { auto a = readln.chomp.to!int - 1; T[a] ~= b; } int solve(int i) { int[] rr; foreach (j; T[i]) { rr ~= solve(j); } sort!"a > b"(rr); int max_r; foreach (j, r; rr) { max_r = max(max_r, r + j.to!int + 1); } return max_r; } writeln(solve(0)); }
D
import std.stdio, std.string, std.array, std.conv; struct Card { char suit, value; } void bubble(int n, Card[] x) { foreach (i; 0 .. n) { foreach_reverse (j; i+1 .. n) { if (x[j].value < x[j-1].value) { Card t = x[j]; x[j] = x[j-1]; x[j-1] = t; } } } } void selection(int n, Card[] x) { foreach (i; 0 .. n) { int minj = i; foreach (j; i .. n) { if (x[j].value < x[minj].value) { minj = j; } } Card t = x[i]; x[i] = x[minj]; x[minj] = t; } } bool isStable(int n, Card[] x, Card[] y) { foreach (i; 0 .. n) { if (x[i].suit != y[i].suit) return false; } return true; } void main() { int n = readln.chomp.to!int; string[] _a = readln.chomp.split; Card[] a, b; foreach (x; _a) { a ~= Card(x[0], x[1]); b ~= Card(x[0], x[1]); } bubble(n, a); selection(n, b); foreach (i, x; a) { write(x.suit, x.value); if (i != n - 1) " ".write; else writeln; } "Stable".writeln; foreach (j, y; b) { write(y.suit, y.value); if (j != n - 1) " ".write; else writeln; } writeln(isStable(n, a, b) ? "Stable" : "Not stable"); }
D
import std.stdio, std.conv, std.algorithm, std.string; alias L = long; L M = 998244353; L[] X; L[L] C; L h(L m, L[] U){ L f; foreach(u; U[0 .. m]) f = f * 2 % M + u; foreach(i, u; U) if(i / m & 1 ^ U[i % m] ^ u) return f + u; return f + 1; } void main(){ L n = readln.chomp.to!L, b = n * 2; foreach(c; readln.chomp) X ~= c - '0'; for(L d = n | 1; d > 2; d -= 2) if(n % d < 1){ L c = h(n / d, X); foreach(e, q; C) e % d || (c += M - q); C[d] = c % M; } n = h(n, X) * b; foreach(d, c; C) n += M - c * (b - b / d) % M; writeln(n % M); }
D
import std.stdio, std.string, std.array, std.conv, std.algorithm.iteration; void main() { immutable nm = readln.chomp.split(" ").map!(to!int).array; immutable n = nm[0]; immutable m = nm[1]; string[] a; a.length = n; foreach(i; 0..n) a[i] = readln.chomp; string[] b; b.length = m; foreach(i; 0..m) b[i] = readln.chomp; while (a[0].length >= m) { auto aa = a[].map!(l => l[0..m]).array; while(aa.length >= m) { if (aa[0..m] == b) { writeln("Yes"); return; } aa = aa[1..$]; } foreach(i, l; a) a[i] = l[1..$]; } writeln("No"); }
D
import std.stdio, std.string, std.array, std.conv; long cnt; int[] y; void insertionSort(int n, int g, int[] x) { foreach (i; g .. n) { int v = x[i]; int j = i - g; while (j >= 0 && x[j] > v) { x[j+g] = x[j]; j -= g; ++cnt; } x[j+g] = v; } } void shellSort(int n, int[] x) { int h = 1; while (true) { if (h > n) break; y ~= h; h = 3 * h + 1; } foreach_reverse (z; y) { insertionSort(n, z, x); } } void main() { int n = readln.chomp.to!int; int[] a = new int[n]; foreach (i; 0 .. n) { a[i] = readln.chomp.to!int; } cnt = 0; shellSort(n, a); y.length.writeln; foreach_reverse (i, z; y) { z.write; if (i) " ".write; else writeln; } cnt.writeln; foreach (i, x; a) { x.writeln; } }
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 I = readln.split.to!(long[]); auto N = I[0]; auto M = I[1]; long[long] decimals; foreach(i; 0..M) { I = readln.split.to!(long[]); auto key = N - I[0]; if (key in decimals && I[1] != decimals[key]) { writeln(-1); return; } if (N != 1 && key == N-1 && I[1] == 0) { writeln(-1); return; } decimals[key] = I[1]; } ulong solve() { long ans; long keta = 1; foreach(i; 0..N) { if (i in decimals) { ans += keta * decimals[i]; } else { ans += keta * (i == N-1 && N > 1 ? 1 : 0); } keta *= 10; } return ans; } solve().writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp; int X; foreach (c; N.to!(char[])) X += c-48; writeln(N.to!int % X == 0 ? "Yes" : "No"); }
D
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } void main() { try { for (; ; ) { const numCases = readInt; foreach (caseId; 0 .. numCases) { const N = readInt; auto A = new long[N]; foreach (i; 0 .. N) { A[i] = readLong; } bool good; if (N == 3) { good = (A[1] % 2 == 0); } else { foreach (i; 1 .. N - 1) { good = good || (A[i] >= 2); } } long ans; if (good) { foreach (i; 1 .. N - 1) { ans += (A[i] + 1) / 2; } } else { ans = -1; } writeln(ans); } } } catch (EOFException e) { } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { auto S = sread(); foreach (c; S) write("x"); writeln(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.bigint; alias M = Tuple!(int, "t", int, "d"); int[10^^5*2] TS; M[10^^5*2] QS; void main() { auto nq = readln.split.to!(int[]); auto N = nq[0]; auto Q = nq[1]; foreach (int i, c; readln.chomp.to!(char[])) { TS[i] = c-65; } foreach (i; 0..Q) { auto td = readln.chomp.to!(char[]); QS[i] = M(td[0] - 65, td[2] == 'L' ? -1 : 1); } int r; int i; foreach_reverse (q; QS) { if (i < N && q.t == TS[max(i, 0)] && q.d < 0) { if (i < 0) i = 1; else ++i; } if (i > 0 && i-1 < N && q.t == TS[i-1] && q.d > 0) --i; } r += max(i, 0); i = N; foreach_reverse (q; QS) { if (i > 0 && q.t == TS[min(i-1, N-1)] && q.d > 0) { if (i > N) i = N-1; else --i; } if (i > 0 && i < N && q.t == TS[i] && q.d < 0) ++i; } r += (N-min(i, N)); writeln(N-r); }
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(){ string s = scan; if(s == "hi" || s == "hihi" || s == "hihihi" || s == "hihihihi" || s == "hihihihihi") "Yes".writeln; else "No".writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new int[][](t); foreach (ti; 0..t) { auto l = RD!int; auto r = RD!int; if (l*2 > r) ans[ti] = [-1, -1]; else ans[ti] = [l, l*2]; } foreach (e; ans) { writeln(e[0], " ", e[1]); } stdout.flush; debug readln; }
D
import std.stdio, std.string, std.range, std.algorithm; void main() { string s = strip(readln()); size_t n = s.length; auto odd = filter!(a => a%2 == 0)(iota(n)); foreach (c; map!(i => s[i])(odd)) { write(c); } writeln(); }
D
void main() { problem(); } void problem() { auto a = scan!long; long solve() { return a + a*a + a*a*a; } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
import std.algorithm, std.string, std.array, std.range, std.stdio, std.conv; void main() { int[] g1 = [1,3,5,7,8,10,12], g2 = [4,6,9,11], g3 = [2]; int[] xy = readln.chomp.split.to!(int[]); int x = xy[0], y = xy[1]; bool found; foreach (g; [g1, g2, g3]) { if ([x, y].all!(e1 => g.canFind(e1))) { found = true; break; } } writeln(found ? "Yes" : "No"); }
D
void main(){ int n = _scan(); if(n>81){ writeln("No"); return; } foreach(i; 1..81+1){ if(i<=9 && n/i<=9 && n%i==0){ writeln("Yes"); return; } } writeln("No"); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
void main() { (ri / 3).writeln; } // =================================== import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
//dlang template---{{{ import std.stdio; import std.conv; import std.string; import std.array; import std.algorithm; import std.typecons; import std.math; import std.range; // MIT-License https://github.com/kurokoji/nephele class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } //Digit count---{{{ int DigitNum(int num) { int digit = 0; while (num != 0) { num /= 10; digit++; } return digit; } //}}} //}}} void main() { Scanner sc = new Scanner; int N; string A, B, C; sc.scan(N, A, B, C); if (A == B && A == C && B == C) { writeln("0"); return; } int cnt = 0; foreach (i; 0 .. N) { int num = 2; if (A[i] == B[i]) num--; if (A[i] == C[i]) num--; int tmp = 2; if (B[i] == A[i]) tmp--; if (B[i] == C[i]) tmp--; int tmp2 = 2; if (C[i] == A[i]) tmp2--; if (B[i] == C[i]) tmp2--; cnt += min(tmp, tmp2, num); } writeln(cnt); }
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<<30; enum mod = 10L^^9 + 7; void main() { int h, w, a, b; scan(h, w, a, b); auto fact = new long[](h + w + 1); auto rfact = new long[](h + w + 1); fact[0] = 1; foreach (i ; 1 .. h + w + 1) { fact[i] = (i * fact[i-1]) % mod; } rfact[h + w] = powmod(fact[h + w], mod - 2); foreach_reverse (i ; 0 .. h + w) { rfact[i] = (rfact[i+1] * (i + 1)) % mod; } long comb(int n, int r) { return fact[n] * rfact[n-r] % mod * rfact[r] % mod; } long ans; foreach (i ; 0 .. w - b) { ans += comb(h - a - 1 + b + i, h - a - 1) * comb(a - 1 + w - b - 1 - i, a - 1) % mod; if (ans > mod) ans-= mod; } writeln(ans); } long powmod(long x, long y) { return y > 0 ? powmod(x, y>>1)^^2 % mod * x^^(y & 1) % mod : 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); } } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); long[long] D; foreach (x; 1 .. N + 1) { auto f = factorize(x); foreach (key, val; f) { D[key] = D.get(key, 0) + val; } } long ans = 1; foreach (key, value; D) { ans *= value + 1; ans %= MOD; } writeln(ans); } /// 素因数分解 long[long] factorize(long x) { assert(0 < x, "x is negative"); long[long] ps; while ((x & 1) == 0) { x /= 2; ps[2] = (2 in ps) ? ps[2] + 1 : 1; } for (long i = 3; i * i <= x; i += 2) while (x % i == 0) { x /= i; ps[i] = (i in ps) ? ps[i] + 1 : 1; } if (x != 1) ps[x] = (x in ps) ? ps[x] + 1 : 1; return ps; }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { auto n = lread(); auto k = lread(); long ans = 1; foreach (i; 0 .. n) { ans = min(ans * 2, ans + k); } writeln(ans); } 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, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto AS = new int[](2^^N); foreach (i; 0..2^^N) { AS[i] = readln.chomp.to!int; } while (AS.length > 1) { int[] NAS; for (int i; i < AS.length; i += 2) { if (AS[i] == AS[i+1]) { NAS ~= AS[i]; } else { NAS ~= abs(AS[i] - AS[i+1]); } } AS = NAS; } writeln(AS[0]); }
D
import std.stdio, std.string, std.conv, std.algorithm; void main() { int[] tmp = readln.split.to!(int[]); int h = tmp[0], w = tmp[1]; string[] grid = new string[h]; foreach (i; 0 .. h) { string s = readln.chomp; grid[i] = s; } int[][] a = new int[][](h, w); foreach (i; 0 .. h) { foreach (j; 0 .. w) { if (j == 0) { if (grid[i][j] == '.') a[i][j] = 1; else a[i][j] = 0; } else { if (grid[i][j] == '.') a[i][j] = a[i][j-1] + 1; else a[i][j] = 0; } } } int[][] b = new int[][](h, w); foreach (i; 0 .. h) { foreach_reverse (j; 0 .. w) { if (j == w - 1) { if (grid[i][j] == '.') b[i][j] = 1; else b[i][j] = 0; } else { if (grid[i][j] == '.') b[i][j] = b[i][j+1] + 1; else b[i][j] = 0; } } } int[][] c = new int[][](h, w); foreach (j; 0 .. w) { foreach (i; 0 .. h) { if (i == 0) { if (grid[i][j] == '.') c[i][j] = 1; else c[i][j] = 0; } else { if (grid[i][j] == '.') c[i][j] = c[i-1][j] + 1; else c[i][j] = 0; } } } int[][] d = new int[][](h, w); foreach (j; 0 .. w) { foreach_reverse (i; 0 .. h) { if (i == h - 1) { if (grid[i][j] == '.') d[i][j] = 1; else d[i][j] = 0; } else { if (grid[i][j] == '.') d[i][j] = d[i+1][j] + 1; else d[i][j] = 0; } } } int ans = 0; foreach (i; 0 .. h) { foreach (j; 0 .. w) { ans = max(ans, a[i][j]+b[i][j]+c[i][j]+d[i][j]-3); } } ans.writeln; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}} void main() { int a, b; readV(a, b); writeln((a+b+1)/2); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto G = new Tuple!(int, int)[][](N); auto uf = new UnionFind(N); foreach (i; 0..M) { s = readln.split.map!(to!int); G[s[0]-1] ~= tuple(s[1]-1, s[2] & 1); G[s[1]-1] ~= tuple(s[0]-1, s[2] & 1); uf.unite(s[0]-1, s[1]-1); } uf.table.map!(u => u < 0).sum.writeln; } class UnionFind { int N; int[] table; this(int n) { N = n; table = new int[](N); fill(table, -1); } int find(int x) { return table[x] < 0 ? x : (table[x] = find(table[x])); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (table[x] > table[y]) swap(x, y); table[x] += table[y]; table[y] = x; } }
D
void main() { string s = readln.chomp; int n = s.to!int; int fx; foreach (x; s) { fx += x - '0'; } writeln(n % fx == 0 ? "Yes" : "No"); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
void main() { auto S = rs; long res; res += S.count!(a => a == '+'); res -= S.count!(a => a == '-'); res.writeln; } // =================================== import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { auto S = sread(); long ans = long.max; { char prev = S[0]; long cnt; foreach (i; 0 .. S.length) if (S[i] != prev) { prev = S[i]; cnt++; } ans = ans.min(cnt); } { char prev = S[$ - 1]; long cnt; foreach_reverse (i; 0 .. S.length) if (S[i] != prev) { prev = S[i]; cnt++; } ans = ans.min(cnt); } writeln(ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto A = new long[][](N, N); foreach (i; 0..N) { foreach (j, a; readln.split.to!(long[])) { A[i][j] = a; } } foreach (k; 0..N) { foreach (i; 0..N) { foreach (j; 0..N) { if (A[i][j] > A[i][k] + A[k][j]) { writeln("-1"); return; } } } } long r; foreach (i; 0..N) { foreach (j; 0..N) { foreach (k; 0..N) { if (k != i && k != j && A[i][j] == A[i][k] + A[k][j]) goto ng; } r += A[i][j]; ng: } } writeln(r/2); }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; import std.string; void main() { string str = readln.chomp; string[2] ss = ["eraser", "dreamer"]; // array.lengthの返り値の型はulongなので // intに代入しようとすると(dmd64 v2.070.1では)CEになる int p = str.length.to!int; while (p > 0) { bool flag = false; if (str[p - 1] == 'r') { for (int i = 0; i < 2; ++i) { if (p < ss[i].length.to!int) { break; } if (ss[i] == str[(p - ss[i].length.to!int)..p]) { p -= ss[i].length.to!int; flag = true; break; } } } else if (str[p - 1] == 'e' && p >= 5) { if (str[(p - 5)..p] == "erase") { flag = true; p -= 5; } } else if (str[p - 1] == 'm' && p >= 5) { if (str[(p - 5)..p] == "dream") { flag = true; p -= 5; } } if (!flag) { writeln("NO"); return; } } writeln("YES"); return; }
D
import std.stdio, std.conv, std.string; import std.algorithm, std.array, std.container; import std.numeric, std.math; import core.bitop; string my_readln() { return chomp(readln()); } long mod = pow(10, 9) + 7; long moda(long x, long y) { return (x + y) % mod; } long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; } long modm(long x, long y) { return (x * y) % mod; } void main() { auto tokens = my_readln.split; auto N = tokens[0].to!long; auto A = tokens[1].to!long; auto B = tokens[2].to!long; writeln(min(A, B), " ", max(0, (A + B) - N)); stdout.flush(); }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { auto tmp = readln.split.to!(long[]); writeln(tmp.all!(t => t < 10) ? tmp[0] * tmp[1] : -1); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto rd = readln.split.to!(size_t[]), n = rd[0], t = rd[1]; auto a = readln.split.to!(int[]); auto b = new int[](n-1), m = a[0]; foreach (i; 0..n-1) { b[i] = a[i+1] - m; m = min(m, a[i+1]); } auto c = b.reduce!max; writeln(b.count(c)); }
D
import std.stdio, std.string, std.conv, std.array, std.algorithm; void main(){ auto a = readln.split.map!(to!int); int n = a[0]; int m = a[1]; int c; int cnt,gomi; for(uint i;i<m;++i){ auto b = readln.split.map!(to!int); c = b[0]; if(c<n){ cnt += n-c; if(n-c>gomi) gomi = n-c; } } writeln(cnt-gomi); }
D
import std.stdio, std.string, std.conv, std.algorithm, std.range, std.typecons; void main() { auto S = readln.split.to!(ulong[]); auto N = S[0], A = S[1], B = S[2]; auto X = readln.split.to!(ulong[]); ulong res = 0; foreach (i; 0..N-1) { res += min((X[i+1] - X[i]) * A, B); } writeln(res); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } 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 = 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 S = RD!string; auto T = S.idup; long ans = 1; auto last = [S[0]]; S.popFront; while (!S.empty) { if (last.length == 2) { last = [S[0]]; S.popFront; ++ans; } else if (S[0] == last[0]) { if (S.length == 1) { break; } last = S[0..2]; S = S[2..$]; ++ans; } else { last = [S[0]]; S.popFront; ++ans; } } long ans2 = 1; last = [T[0], T[1]]; T = T[2..$]; while (!T.empty) { if (last.length == 2) { last = [T[0]]; T.popFront; ++ans2; } else if (T[0] == last[0]) { if (T.length == 1) { break; } last = T[0..2]; T = T[2..$]; ++ans2; } else { last = [T[0]]; T.popFront; ++ans2; } } writeln(max(ans, ans2)); 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; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto S = RD!string; auto p1 = new bool[](10); auto p2 = new bool[](100); auto p3 = new bool[](1000); foreach (c; S) { auto x = [c].to!long; foreach (i; 0..p2.length) { if (p2[i]) { auto y = i*10 + x; p3[y] = true; } } foreach (i; 0..p1.length) { if (p1[i]) { auto y = i*10 + x; p2[y] = true; } } p1[x] = true; } long ans; foreach (e; p3) { if (e) ++ans; } writeln(ans); stdout.flush; debug readln; }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; bool calc(long a, long b, long c) { if (c - a - b <= 0) return false; return 4 * a * b < (c - a - b) ^^ 2; } void main() { long a, b, c; scan(a, b, c); writeln(calc(a, b, c) ? "Yes" : "No"); } void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int;
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { string a; readV(a); string b; readV(b); if (a.length > b.length) writeln("GREATER"); else if (a.length < b.length) writeln("LESS"); else if (a > b) writeln("GREATER"); else if (a < b) writeln("LESS"); else writeln("EQUAL"); }
D
import std.stdio, std.conv, std.string; import std.algorithm, std.array, std.container, std.typecons; import std.numeric, std.math; import core.bitop; T RD(T = long)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool INSIDE(T)(T x, T b, T e) { return x >= b && x < e; } long mod = pow(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; } struct UnionFind { void init(long n) { par = new long[](n); foreach (i; 0..n) par[i] = i; } long root(long i) { return par[i] == i ? i : (par[i] = root(par[i])); } bool same(long i, long j) { return root(i) == root(j); } void unite(long i, long j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; } long[] par; } long[] dijkstra(long from, long to, long[][] edges) { long[] path; auto cost = new long[](edges.length); fill(cost, long.max); cost[from] = 0; long[][] open = [[from]]; while (!open.empty) { auto n = open.front; open.popFront; auto p = n[0]; foreach (i; 0..edges.length) { if (edges[p][i] == -1) continue; auto c = cost[p] + edges[p][i]; if (c < cost[to] && c < cost[i]) { cost[i] = c; if (i == to) path = i ~ n; else open ~= i ~ n; } } } return cost[to] ~ path; } void main() { auto S = RD!string; auto tokens = S.split("/"); writeln(tokens[1].to!long >= 5 ? "TBD" : "Heisei"); stdout.flush(); }
D
void main() { dchar[] s = rdDchar; dchar[] t; foreach_reverse (x; s) { if (x == 'b') t ~= 'd'; if (x == 'd') t ~= 'b'; if (x == 'p') t ~= 'q'; if (x == 'q') t ~= 'p'; } writeln(s == t ? "Yes" : "No"); } enum long mod = 10L^^9 + 7; enum long inf = 1L << 60; enum double eps = 1.0e-9; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
import std.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; void main() { int x; scan(x); x -= 400; auto ans = 8 - (x / 200); writeln(ans); } void scan(T...)(ref T args) { auto line = readln.split; // @suppress(dscanner.suspicious.unmodified) 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, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons; void main() { const MAX = 1000000; auto budget = readln.chomp.to!int; auto hasuu = budget % 100; int purchasedCount; while(hasuu > 0) { if (hasuu < 5) { purchasedCount++; break; } purchasedCount++; hasuu -= 5; } writeln(purchasedCount*100 <= budget ? "1" : "0"); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; import std.numeric; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n; readV(n); int[] a; readA(n, a); auto b = new int[](n); b[0] = a[0]; foreach (i; 1..n) b[i] = gcd(b[i-1], a[i]); auto c = new int[](n); c[$-1] = a[$-1]; foreach_reverse (i; 0..n-1) c[i] = gcd(c[i+1], a[i]); auto r = max(b[$-2], c[1]); foreach (i; 1..n-1) r = max(r, gcd(b[i-1], c[i+1])); writeln(r); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.math; import std.regex; const dx = [-1, 0, 1, -1, 1, -1, 0, 1]; const dy = [-1, -1, -1, 0, 0, 1, 1, 1]; void main() { auto ip = readln.split.to!(int[]), H = ip[0], W = ip[1]; char[][] S; foreach(_; 0..H) S ~= readln.chomp.dup; foreach(i; 0..H) { foreach(j; 0..W) { if(S[i][j] != '#') { int count; foreach(m; -1..2) { if(i+m < 0 || i+m >= H) continue; foreach(n; -1..2) { if(j+n < 0 || j+n >= W) continue; if(S[i+m][j+n] == '#') count++; } } S[i][j] = ('0' + count).to!char; } } } foreach(i; 0..H) { writeln(S[i]); } }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons; immutable int inf = 10^^9 + 7; int n; void main() { scan(n); long ans = 10L^^5; foreach (i ; 0 .. n) { ans = (ans * 105) / 100; ans = ((ans + 999) / 1000) * 1000; } 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); }
D
import std.stdio; import std.algorithm; import std.math; import std.conv; import std.string; T readNum(T)(){ return readStr.to!T; } T[] readNums(T)(){ return readStr.split.to!(T[]); } string readStr(){ return readln.chomp; } void main(){ auto a = readNum!int; writeln(a^^3); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N, M; scan(N, M); auto cnt = new long[](N + 1); foreach (_; 0 .. M) { long a, b; scan(a, b); cnt[a]++; cnt[b]++; } writeln(cnt.all!"a%2==0"() ? "YES" : "NO"); }
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } void main(){ int n = read.to!int; long[] as; foreach(i; 0 .. n + 1) as ~= read.to!long; long[long] pos; long i0, i1; foreach(i, a; as){ if(a !in pos) pos[a] = i; else i0 = pos[a], i1 = i; } foreach(k; 1 .. n + 2){ long q = pascal(k, n + 1 - k); q += mod - pascal(k - 1, i0 + (n - i1) - (k - 1)), q %= mod; q.writeln; } } /* // 仮実装 long pascal(long a, long b){ static long[long[2]] memo; if(a == 0) return 1; if(b == 0) return 1; if(a < 0) return 0; if(b < 0) return 0; if([a, b] !in memo){ memo[[a, b]] = (pascal(a, b - 1) + pascal(a - 1, b)) % mod; } return memo[[a, b]]; } */ // mod p における乗法逆元 long inv(long x){ assert(x > 0); assert(x < mod); static long[] _inv = [0, 1]; while(x >= _inv.length){ _inv ~= _inv[mod % $] * (mod - mod / _inv.length) % mod; } return _inv[x]; } // mod p における階乗 long perm(long x){ assert(x >= 0); assert(x < mod); static long[] _perm = [1]; while(x >= _perm.length){ _perm ~= _perm[$ - 1] * _perm.length % mod; } return _perm[x]; } // mod p における階乗の逆元 long invperm(long x){ assert(x >= 0); assert(x < mod); static long[] _invperm = [1]; while(x >= _invperm.length){ _invperm ~= _invperm[$ - 1] * inv(_invperm.length) % mod; } return _invperm[x]; } // mod p における二項係数(定義に注意:(a + b)! / a! / b! ) long pascal(long a, long b){ if(a < 0 || b < 0) return 0; assert(a >= 0 && b >= 0); assert(a < mod && b < mod); long res = perm((a + b) % mod); res *= invperm(a), res %= mod; res *= invperm(b), res %= mod; return res; } const long mod = 1_000_000_007;
D
import std.stdio, std.algorithm, std.conv, std.array, std.string; void main() { auto hw = readln.split.to!(int[]); auto h = hw[0]; auto w = hw[1]; auto ret = ""; foreach (_; 0..(w+1)) ret ~= "#"; ret ~= "#\n"; foreach (_; 0..h) { ret ~= "#"; ret ~= readln.chomp; ret ~= "#\n"; } foreach (_; 0..(w+2)) ret ~= "#"; writeln(ret); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nt = readln.split.to!(long[]); auto N = nt[0]; auto T = nt[1]; auto ts = readln.split.to!(long[]); ts ~= long.max; long st; foreach (i; 0..N) st += min(T, ts[i+1] - ts[i]); writeln(st); }
D
void main() { long[] tmp = readln.split.to!(long[]); long a = tmp[0], b = tmp[1]; writeln(a * b / gcd(a, b)); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { long A, B, C, D; scan(A, B, C, D); bool b = abs(A - B) <= D && abs(B - C) <= D; bool c = abs(A - C) <= D; writeln((b || c) ? "Yes" : "No"); }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { int n; scan(n); auto a = readln.split.to!(int[]); auto cnt = new int[](10^^5 + 10); int over; foreach (ai ; a) { if (ai > 10^^5) { over++; } else { cnt[ai]++; } } debug { writeln(cnt[0 .. 20]); } int ans = over; foreach (i ; 0 .. 10^^5 + 1) { int d = cnt[i]; if (cnt[i] >= i && d > cnt[i] - i) { d = cnt[i] - i; } ans += d; } writeln(ans); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.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; foreach (i; 0..n-1) { ans[ti].chmax(a[i]*a[i+1]); } } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = readln.chomp.to!size_t; auto a = new int[](n), b = new bool[](n); foreach (i; 0..n) a[i] = readln.chomp.to!int-1; auto c = 0, d = 0; for (;;) { ++d; b[c] = true; c = a[c]; if (c == 1) { writeln(d); break; } if (b[c]) { writeln(-1); break; } } }
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 w = readint; int n = readint; auto xs = new int[w + 1]; for (int i = 0; i < xs.length; i++) xs[i] = i; for (int i = 0; i < n; i++) { auto ab = readln.chomp.split(",").to!(int[]); int a = ab[0], b = ab[1]; swap(xs[a], xs[b]); } foreach (x; xs[1 .. $]) writeln(x); }
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; 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; long calc(int[] a) { long ans = 0; int hi = 0; int sum = 0; for (int lo = 0; lo < a.length; lo++) { // hi を伸ばせるところまで伸ばす while (hi < a.length && sum + a[hi] == (sum ^ a[hi])) { sum += a[hi++]; } ans += hi - lo + 1 - 1; // lo を右にずらすのでその分だけ引く sum -= a[lo]; hi = max(hi, lo); } return ans; } void main() { readint; auto a = readints; writeln(calc(a)); }
D
import std.stdio, std.string, std.array, std.algorithm, std.conv, std.typecons, std.numeric, std.math; struct LazySegTree(alias opt, alias opu, alias _add, alias mul, alias E, alias F, T, U) if (is(typeof(E) : T)) { import std.functional : binaryFun; alias OPT = binaryFun!opt; alias OPU = binaryFun!opu; alias ADD = binaryFun!_add; alias MUL = binaryFun!mul; /// this(size_t n, T[] ts) { this.n = 1; while (this.n < n) this.n *= 2; this.tree.length = this.n * 2 - 1; foreach (ref e; this.tree) e = E; foreach (i, e; ts) this.replace(i, e); this.ltree.length = this.n * 2 - 1; foreach (ref f; this.ltree) f = F; } /// void replace(size_t i, T e) { i += this.n - 1; this.tree[i] = e; while (i > 0) { i = (i-1) / 2; this.tree[i] = OPT(this.tree[i*2+1], this.tree[i*2+2]); } } /// void update(size_t i, T e) { replace(i, OPT(e, tree[i + this.n - 1])); } void update(size_t a, size_t b, U e) { void impl(size_t i, size_t l, size_t r) { eval(i, l, r); if (r <= a || b <= l) return; if (a <= l && r <= b) { import std.conv; ltree[i] = OPU(ltree[i], e); eval(i, l, r); return; } impl(i*2+1, l, (l+r)/2); impl(i*2+2, (l+r)/2, r); tree[i] = OPT(tree[i*2+1], tree[i*2+2]); } impl(0, 0, this.n); } /// T query(size_t a, size_t b) { T impl(size_t i, size_t l, size_t r) { eval(i, l, r); if (r <= a || b <= l) return E; if (a <= l && r <= b) return this.tree[i]; return OPT( impl(i*2+1, l, (l+r)/2), impl(i*2+2, (l+r)/2, r) ); } return impl(0, 0, this.n); } private: size_t n; T[] tree; U[] ltree; void eval(size_t i, size_t l, size_t r) { if (ltree[i] == F) return; import std.conv; tree[i] = ADD(tree[i], MUL(ltree[i], (r-l).to!U)); if (i < this.n - 1) { ltree[2*i+1] = OPU(ltree[2*i+1], ltree[i]); ltree[2*i+2] = OPU(ltree[2*i+2], ltree[i]); } ltree[i] = F; } } /// auto lazy_seg_tree(alias opt, alias opu, alias add, alias mul, alias E, alias F, T, U)(size_t n, T[] arr = []) { return LazySegTree!(opt, opu, add, mul, E, F, T, T)(n, arr); } /// auto lazy_seg_tree(alias opt, alias opu, alias add, alias mul, alias E, alias F, T, U)(T[] arr) { return LazySegTree!(opt, opu, add, mul, E, F, T, T)(arr.length, arr); } /// auto lazy_sum_seg_tree(T)(size_t n, T[] arr = []) { return lazy_seg_tree!("a + b", "a + b", "a + b", "a * b", 0, 0, T, T)(n, arr); } /// auto lazy_sum_seg_tree(T)(T[] arr) { return lazy_sum_seg_tree!T(arr.length, arr); } void main() { auto nq = readln.split.to!(int[]); auto N = nq[0]; auto Q = nq[1]; auto segt = lazy_sum_seg_tree!long(N); foreach (_; 0..Q) { auto q = readln.split.to!(int[]); if (q[0] == 0) { segt.update(q[1]-1, q[2], q[3]); } else { writeln(segt.query(q[1]-1, q[2])); } } }
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; void calc(int a, int b) { if (a > b) return calc(b, a); assert(a <= b); int d = b - a; if (d % 2 == 1) { writeln("IMPOSSIBLE"); return; } writeln(a + d / 2); } void main() { int a, b; scan(a, b); calc(a, b); }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; import std.numeric; void main() { int n; scan(n); long ans = 1; foreach (i ; 0 .. n) { long t; scan(t); ans = lcm(ans, t); } writeln(ans); } long lcm(long a, long b) { return a / gcd(a, b) * b; } 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 long[](t); foreach (ti; 0..t) { auto n = RD!int; auto r = RDA; auto m = RD!int; auto b = RDA; long x; foreach (i; 0..n+1) { long y; foreach (j; 0..m+1) { ans[ti].chmax(x+y); if (j != m) y += b[j]; } if (i != n) x += r[i]; } } foreach (e; ans) writeln(e); debug readln; }
D
module app; import core.bitop; import std.algorithm; import std.array; import std.bigint; import std.conv; import std.stdio; import std.string; import std.traits; struct Input { int n, k; } void parseInput(T)(out Input input, T file) { with (file) with (input) { auto ar = readln().strip().split().map!(to!int).array(); n = ar[0].to!int; k = ar[1].to!int; } } auto main2(Input* input) { with (input) { auto keta = 1; auto temp = n; while (temp >= k) { temp /= k; keta++; } return keta; } } alias retType = ReturnType!main2; static if (!is(retType == void)) { unittest { writeln("begin unittest"); } auto _placeholder_ = ReturnType!main2.init; unittest // example1 { string example = `11 2`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == 4); } unittest // example2 { string example = `1010101 10`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == 7); } unittest // example3 { string example = `314159265 3`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == 18); } unittest { writeln("end unittest"); } void parseExample(out Input input, string example) { struct Adapter { string[] _lines; this(string input) { _lines = input.splitLines(); } string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; } } parseInput(input, Adapter(example)); } } void printResult(T)(T result) { static if (isFloatingPoint!T) writefln("%f", result); else writeln(result); } void main() { Input input = void; parseInput(input, stdin); alias retType = ReturnType!main2; static if (is(retType == void)) main2(&input); else { auto result = main2(&input); printResult(result); } }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; struct Spl{auto r="".splitter;auto rd(){r=readln.splitter;}auto bd(T)(ref T t){t=r.front.to!T;r.popFront;}} auto _s = Spl(); auto getV(T...)(ref T t){foreach(ref u;t)_s.bd(u);} auto getA(T)(size_t n,ref T t){t=new T(n);foreach(ref u;t)_s.bd(u);} auto readV(T...)(ref T t){_s.rd; getV(t);} auto readA(T)(size_t n,ref T t){_s.rd;getA(n,t);} void main() { int n; readV(n); int[] w; readA(n, w); int s = w.sum; foreach (i; 1..n) s = min(s, (w[0..i].sum-w[i..$].sum).abs); writeln(s); }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; import std.array; int n; rd(n); auto a = readln.split .to!(int[]) .map!((e) => (e - 1)) .array; int num = 0; foreach (i; 0 .. (n - 1)) { if (i == a[i]) { num++; swap(a[i], a[i + 1]); } } if (a[n - 1] == n - 1) num++; writeln(num); } 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.algorithm, std.range, std.array, std.conv, std.string, std.math, std.container, std.typecons; void main() { auto N = readln.chomp.to!int; auto A = readln.split.to!(int[]); auto B = readln.split.to!(int[]); N.iota.map!(i => A[0..i+1].sum + B[i..$].sum).reduce!max.writeln; }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n, m; rd(n, m); int[] a; for (int i = 2; i * i <= m; i++) { if (m % i == 0) { int cnt = 0; while (m % i == 0) { cnt++; m /= i; } a ~= cnt; } } if (m > 1) { a ~= 1; } const long mod = 10 ^^ 9 + 7; const size_t M = 10 ^^ 5 * 2; static long[M] fac, inv; fac[0] = fac[1] = 1; foreach (i; 2 .. M) fac[i] = (fac[i - 1] * i) % mod; long _pow(long a, long x) { if (x == 0) return 1; else if (x == 1) return a; else if (x & 1) return a * _pow(a, x - 1) % mod; else return _pow(a * a % mod, x / 2); } foreach (i; 0 .. M) inv[i] = _pow(fac[i], mod - 2); long cmb(long a, long b) { if (a < b) return 0; else return fac[a] * inv[b] % mod * inv[a - b] % mod; } long tot = 1; foreach (e; a) { (tot *= cmb(e + n - 1, e)) %= mod; } writeln(tot); } void rd(T...)(ref T x) { import std.stdio, std.string, std.conv; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto A = RD; auto B = RD; bool ans; if (A % 3 == 0) ans = true; else if (B % 3 == 0) ans = true; else if ((A + B) % 3 == 0) ans = true; writeln(ans ? "Possible" : "Impossible"); stdout.flush(); debug readln(); }
D