code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons, std.datetime; void main() { const S = readln.chomp; auto d = Date.fromISOString(S.replace("/", "")); if (d <= Date(2019, 4, 30)) { writeln("Heisei"); } else { writeln("TBD"); } }
D
import std.stdio; import std.conv; import std.array; import std.algorithm; import std.range; import std.string; alias println = writeln; alias print = write; int readint() { return readln.chomp.to!int; } void main() { if (readint() == 1) { println("Hello World"); } else { println(readint() + readint()); } }
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() { auto n = lread(); auto a = aryread(); long tmpxor; foreach (i; iota(1, n)) { tmpxor ^= a[i]; } foreach (i; iota(n)) { tmpxor.write(" "); tmpxor ^= a[i]; tmpxor ^= a[(i + 1) % n]; } writeln(); } 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; import std.conv; import std.algorithm; import std.range; import std.string; void main() { while (true) { auto input = readln.chomp.split.map!(to!int); if (input.array == [0, 0]) break; int h = input[0]; int w = input[1]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (i == 0 || i == h - 1) { write("#"); } else { if (j == 0 || j == w - 1) { write("#"); } else { write("."); } } } writeln; } writeln; } }
D
//prewritten code: https://github.com/antma/algo import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.traits; final class InputReader { private: ubyte[] p, buffer; bool eof; bool rawRead () { if (eof) { return false; } p = stdin.rawRead (buffer); if (p.empty) { eof = true; return false; } return true; } ubyte nextByte(bool check) () { static if (check) { if (p.empty) { if (!rawRead ()) { return 0; } } } auto r = p.front; p.popFront (); return r; } public: this () { buffer = uninitializedArray!(ubyte[])(16<<20); } bool seekByte (in ubyte lo) { while (true) { p = p.find! (c => c >= lo); if (!p.empty) { return false; } if (!rawRead ()) { return true; } } } template next(T) if (isSigned!T) { T next () { if (seekByte (45)) { return 0; } T res; ubyte b = nextByte!false (); if (b == 45) { while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 - (b - 48); } } else { res = b - 48; while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 + (b - 48); } } } } template next(T) if (isUnsigned!T) { T next () { if (seekByte (48)) { return 0; } T res = nextByte!false () - 48; while (true) { ubyte b = nextByte!true (); if (b < 48 || b >= 58) { break; } res = res * 10 + (b - 48); } return res; } } T[] nextA(T) (in int n) { auto a = uninitializedArray!(T[]) (n); foreach (i; 0 .. n) { a[i] = next!T; } return a; } } void main() { auto r = new InputReader (); immutable nt = r.next!uint (); foreach (tid; 0 .. nt) { int x = r.next!int; int y = r.next!int; const long a = r.next!int; const long b = r.next!int; long res = (x + y) * a; if (x > y) swap (x, y); res = min (res, b * x + (y - x) * a); writeln (res); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //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; writeln((N+1) * N / 2); stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void 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 main() { auto H = RD; auto W = RD; auto N = RD; writeln(N / max(H, W) + (N % max(H, W) != 0 ? 1 : 0)); stdout.flush; debug readln; }
D
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; string s; void main() { scan(s); writeln(s[0] ~ (s.length.to!int - 2).to!string ~ s[$-1]); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.conv, std.string; void main() { auto ip = readln.split.to!(int[]); if(ip[0] + ip[1] <= ip[1] + ip[2] && ip[0] + ip[1] <= ip[2] + ip[0]){ writeln(ip[0] + ip[1]); } else if(ip[1] + ip[2] <= ip[0] + ip[1] && ip[1] + ip[2] <= ip[2] + ip[0]){ writeln(ip[1] + ip[2]); } else { writeln(ip[2] + ip[0]); } }
D
void main() { auto N = ri; if(N < 1200) { writeln("ABC"); } else if(N < 2800) { writeln("ARC"); } else writeln("AGC"); } // =================================== import std.stdio; import std.string; import std.functional; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio; void main(){ string s; int[2] score; while ((s = readln()) != "0\n") { score = 0; foreach (ch; s[1..$-1]) score[ch - 'A']++; int t = score[0] > score[1] ? 0 : 1; score[t]++; writeln(score[0], " ", score[1]); } }
D
import std.stdio; import std.conv; import std.string; import std.algorithm; import std.range; import std.functional; import std.math; import core.bitop; void main() { /// ABC089_D string s = readln.chomp; writeln((s.canFind('a') && s.canFind('b') && s.canFind('c')) ? "Yes" : "No"); }
D
void main(){ int k = _scan(); writeln((k/2)*((k+1)/2)); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import core.thread; import std.conv, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex; // Input class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { tokens = readln.split; if (stdin.eof) throw new EOFException; } auto token = tokens[0]; tokens.popFront; return token; } int readInt() { return to!int(readToken); } long readLong() { return to!long(readToken); } real readReal() { return to!real(readToken); } // chmin/chmax void chmin(T)(ref T t, in T f) { if (t > f) t = f; } void chmax(T)(ref T t, in T f) { if (t < f) t = f; } // Pair struct Pair(S, T) { S x; T y; int opCmp( const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; } int opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; } string toString() const { return "(" ~ to!string(x) ~ ", " ~ to!string(y) ~ ")"; } } auto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); } // Array int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = as.length; for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; } int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a < val); }); } int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); } T[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; } immutable LIM = 100_005; int N; int[] A; void main(string[] args) { try { for (; ; ) { N = readInt; A = new int[N]; foreach (i; 0 .. N) { A[i] = readInt; } long[] ps = new long[LIM]; foreach (i; 0 .. N) { ps[A[i]] += A[i]; } long[] dp = new long[LIM]; foreach (x; 0 .. LIM) { dp[x] = ps[x]; if (x - 1 >= 0) { chmax(dp[x], dp[x - 1]); } if (x - 2 >= 0) { chmax(dp[x], dp[x - 2] + ps[x]); } } writeln(dp[LIM - 1]); } } catch (EOFException) {} }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std; // dfmt on void main() { long N, A, B; scan(N, A, B); if (B < A) { writeln(0); return; } if (N == 1 && A != B) { writeln(0); return; } long m = A * (N - 1) + B; long M = A + B * (N - 1); writeln(M - m + 1); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; void main() { auto s = readln.splitter; auto a = s.front; s.popFront(); auto b = s.front; writeln(b, a); }
D
// import chie template :) {{{ import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons; // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string : chomp; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.chomp.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 main() { auto cin = new Scanner; int n; cin.scan(n); int[] ar = new int[n + 2]; foreach (i; 0 .. n) { cin.scan(ar[i + 1]); } int sum; foreach (i; 0 .. n + 1) { sum += abs(ar[i + 1] - ar[i]); } foreach (i; 1 .. n + 1) { writeln(sum - abs(ar[i] - ar[i - 1]) - abs(ar[i + 1] - ar[i]) + abs(ar[i + 1] - ar[i - 1])); } }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; void times(alias fun)(int n) { foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } void main() { int N = readln.chomp.to!int; int[] as = N.rep!(() => readln.chomp.to!int).map!"a-1".array; int i = 0; foreach(k; 0..N) { i = as[i]; if (i == 1) { (k+1).writeln; return; } } (-1).writeln; } // ---------------------------------------------- // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { auto minElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto minimum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b < minimum) { element = a; minimum = b; } } return element; } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto maximum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b > maximum) { element = a; maximum = b; } } return element; } }
D
import std.stdio, std.string, std.conv, std.range, std.algorithm; void main() { auto a = readln.chomp.to!int; auto b = readln.chomp.to!int; auto h = readln.chomp.to!int; ((a + b) * h / 2).writeln; }
D
import std.stdio, std.range, std.conv, std.string, std.array, std.functional, std.math; import std.algorithm.comparison, std.algorithm.iteration, std.algorithm.mutation, std.algorithm.searching, std.algorithm.setops, std.algorithm.sorting; import std.container.binaryheap; void main() { auto N = readln().strip.to!(long); long[4][4][4][] memo; memo.length = N+1; memo[0][3][3][3] = 1; foreach(i; 0..N) { foreach(c1; 0..4) { foreach(c2; 0..4) { foreach(c3; 0..4) { foreach(a; 0..4) { if(a==2&&c1==1&&c2==0)continue; if(a==2&&c1==0&&c2==1)continue; if(a==1&&c1==2&&c2==0)continue; if(a==2&&c1==1&&c3==0)continue; if(a==2&&c2==1&&c3==0)continue; memo[i+1][a][c1][c2] += memo[i][c1][c2][c3]; memo[i+1][a][c1][c2] %= pow(10, 9) + 7; } } } } } long res; foreach(c1; 0..4) { foreach(c2; 0..4) { foreach(c3; 0..4) { res += memo[N][c1][c2][c3]; res %= pow(10, 9) + 7; } } } writeln(res); }
D
// import chie template :) {{{ import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons; // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string : chomp; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.chomp.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 main() { auto cin = new Scanner; int n; cin.scan(n); auto a = cin.nextArray!int(n); int cnt; foreach (i; 1 .. n) { if (a[i - 1] == a[i]) { a[i] = -1; cnt++; } } writeln(cnt); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto ii = readln.split.to!(long[]); auto A = ii[0]; auto B = ii[1]; auto C = ii[2]; auto X = ii[3]; auto Y = ii[4]; writeln(min( A*X + B*Y, C*X*2 + max(0, (Y-X)*B), C*Y*2 + max(0, (X-Y)*A) )); }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; bool solve (string s) { if (s.length % 2 != 0) { return false; } if (s.front == ')' || s.back == '(') { return false; } return true; } void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto s = readln.strip; writeln (solve (s) ? "Yes" : "No"); } }
D
import std.stdio, std.conv, std.string, std.math; void main(){ auto ip = readln.chomp.to!int; writeln(800 * ip - 200 * (ip / 15)); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.math; bool isOk(long[] arr, size_t idx, long key) { if (arr[idx] >= key) return true; return false; } size_t binsearch(long[] arr, long key) { long left = -1; long right = arr.length; while(right - left > 1) { long mid = left + (right - left) / 2; if (isOk(arr, mid, key)) right = mid; else left = mid; } return right; } void main() { int n; scanf("%d\n", &n); auto ws = readln.chomp.split.to!(long[]); auto total = ws.reduce!"a+b"; auto ladder = new long[ws.length]; ladder[0] = ws[0]; foreach(i, w;ws[1..$]) ladder[i+1] = ladder[i] + w; auto sep = binsearch(ladder, total/2); min(2*ladder[sep]-total, total-2*ladder[sep-1]).write; }
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 = readln.split.to!(int[]); auto BS = readln.split.to!(int[]); foreach (i; 1..N) { AS[i] += AS[i-1]; BS[i] += BS[i-1]; } int r; foreach (i; 0..N) { r = max(r, AS[i] + BS[N-1] - (i == 0 ? 0 : BS[i-1])); } writeln(r); }
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; immutable int INF = 1 << 29; int[][] adj; void main() { auto N = readln.chomp.to!int; int E = 0; auto uf = new UnionFind(N); auto used = new bool[][](N, N); auto black = new string[](N); foreach (i; 0..N) black[i] = "0"; string[] ans = ["!"]; while (E < N - 1) { int a = uniform(0, N); int b = uniform(0, N); if (a == b) continue; if (used[a][b]) continue; if (uf.find(a) == uf.find(b)) continue; black[a] = black[b] = "1"; writeln("? " ~ black.join("")); stdout.flush; auto radius = readln.chomp.to!int; if (radius == 1) { ans ~= ("(" ~ a.to!string ~ "," ~ b.to!string ~ ")"); uf.unite(a, b); } E += radius; black[a] = black[b] = "0"; used[a][b] = true; used[b][a] = true; } ans.join(" ").writeln; stdout.flush; } 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
// Cheese-Cracker: cheese-cracker.github.io void theCode(){ int k = scan!int; int j = 0; for(int i = 1; i <= 3*k; ++i){ if(i % 3 == 0 || i % 10 == 3) continue; ++j; if(j == k){ writeln(i); } } } void main(){ long tests = scan; // Toggle! while(tests--) theCode(); } /********* That's All Folks! *********/ import std.stdio, std.random, std.functional, std.container, std.algorithm; import std.numeric, std.range, std.typecons, std.string, std.math, std.conv; string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;} T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; } void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } } alias ll = long, tup = Tuple!(long, "x", long, "y");
D
// cheese-cracker [2022-01-31] void solve(){ long n = scan; long ten = (n/10) % 10; long d = n % 7; long u = 7 - d; long down = n - d; long up = n + u; long tend = (down / 10) % 10; long tenu = (up / 10) % 10; if(tend == ten){ writeln(down); }else{ writeln(up); } } void main(){ long tests = scan; // Toggle! while(tests--) solve; } /*_________________________*That's All Folks!*__________________________*/ import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math, std.numeric; string[] tk; alias tup = Tuple!(long, long); T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;} T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; } void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
D
import core.stdc.stdio; import std.algorithm; static immutable int unSet=-114514; struct Data{ int a,l,r,m; } Data Comb(const ref Data a,const ref Data b){ Data r; r.a=a.a+b.a; r.l=max(a.l,a.a+b.l); r.r=max(a.r+b.a,b.r); r.m=max(a.m,b.m,a.r+b.l); return r; } struct Node{ Node* l,r,p; int delay=unSet,size,v; Data d; bool rev; int State(){ if(p !is null){ if(p.l==&this) return -1; if(p.r==&this) return 1; } return 0; } void Propagate(){ if(rev){ if(l !is null) l.rev=!l.rev; if(r !is null) r.rev=!r.rev; swap(l,r); swap(d.l,d.r); rev=false; } if(delay!=unSet){ if(l !is null) l.delay=delay; if(r !is null) r.delay=delay; int s=delay*(delay<0?1:size); d=Data(delay*size,s,s,s); v=delay; delay=unSet; } } void Prepare(){ if(State) p.Prepare; Propagate; } void Update(){ Propagate; d=Data(v,v,v,v); size=1; if(l !is null){ l.Propagate; d=Comb(l.d,d); size+=l.size; } if(r !is null){ r.Propagate; d=Comb(d,r.d); size+=r.size; } } void Rotate(){ Node* par=p,ch; if(p.l==&this){ ch=r; r=p; r.l=ch; }else{ ch=l; l=p; l.r=ch; } if(ch) ch.p=p; p=par.p; par.p=&this; if(p !is null){ if(p.l==par) p.l=&this; if(p.r==par) p.r=&this; } par.Update; Update; } void Splay(){ Prepare; while(State){ int s=State*p.State; if(!s) Rotate; else if(s==1){ p.Rotate; Rotate; }else{ Rotate; Rotate; } } } void Expose(){ Node* x=&this; while(x){ x.Splay; x=x.p; } x=&this; while(x.p){ x.p.r=x; x=x.p; x.Update; } Splay; } void Evert(){ Expose; r=null; rev=!rev; Update; } void Link(Node* t){ Evert; t.Expose; p=t; t.r=&this; t.Update; } void Set(int x){ Expose; r=null; delay=x; Update; } int Get(){ Expose; r=null; Update; return d.m; } } void main(){ int n,q; scanf("%d%d",&n,&q); Node[] ns=new Node[n]; foreach(ref nd;ns){ int w; scanf("%d",&w); nd.v=w; nd.d=Data(w,w,w,w); } foreach(i;0..n-1){ int s,e; scanf("%d%d",&s,&e); ns[--s].Link(&ns[--e]); } foreach(_;0..q){ int t,a,b,c; scanf("%d%d%d%d",&t,&a,&b,&c); if(t==1){ ns[--a].Evert; ns[--b].Set(c); }else{ ns[--a].Evert; printf("%d\n",ns[--b].Get); } } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, 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() { long a, b; scan(a, b); auto d = gcd(a, b); long[] divs; for (long i = 2; i * i < d; i++) { if (d % i == 0) { divs ~= i; while (d % i == 0) d /= i; } } if (d > 1) divs ~= d; auto ans = divs.length.to!int + 1; 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.stdio; import std.string; import std.conv; import std.math; int main() { string buf = strip(readln()); while ((buf = strip(readln())) != null) { writeln(is_right_triangle(buf)); } return 0; } string is_right_triangle(string data_set) { string[] sides = split(data_set); int a = to!(uint)(sides[0]); int b = to!(uint)(sides[1]); int c = to!(uint)(sides[2]); int max = 0; int s1, s2; if (a > b) { if (a > c) { max = a; s1 = b; s2 = c; } else { max = c; s1 = a; s2 = b; } } else { if (b > c) { max = b; s1 = a; s2 = c; } else { max = c; s1 = b; s2 = a; } } if (a < b + c && b < a + c && c < a + b) { if (pow(max, 2) == pow(s1, 2) + pow(s2, 2)) { return "YES"; } } return "NO"; }
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 n, int d) { int ans = 1; int p = d + 1; while (p + d < n) { ans++; p += d * 2 + 1; } return ans; } void main() { int n, d; scan(n, d); writeln(calc(n, d)); }
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)(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 N = RD; auto S = RD!string; long l, r; foreach (c; S) { if (c == '(') ++l; else if (l >= 1) --l; else ++r; } foreach (i; 0..r) write("("); write(S); foreach (i; 0..l) write(")"); writeln(); stdout.flush(); }
D
void main() { auto S = readln.to!(char[]); S[3] = '8'; S.writeln; } // =================================== import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.string, std.conv, std.array, std.algorithm; void main() { int[][] field; int[][] dp; string line; while (true) { line = readln.chomp; if (stdin.eof) break; field ~= line.split(",").map!(to!(int)).array; } foreach(int i, f; field) dp ~= new int[](f.length); int half = (field.length / 2).to!(int); for(int i = 1; i < field.length; i++) { for(int j = 0; j < field[i].length; j++) { if (half >= i) { if (j == field[i].length - 1) { field[i][j] += field[i - 1][j - 1]; } else if (j == 0) { field[i][j] += field[i - 1][j]; } else { field[i][j] += max(field[i - 1][j], field[i - 1][j - 1]); } } else if (half < i) { if (field[i].length / 2 <= i) { field[i][j] += max(field[i - 1][j], field[i - 1][j + 1]); } else { field[i][j] += max(field[i - 1][j], field[i - 1][j]); } } } } writeln(field[$ - 1][0]); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void main() { auto K = readln.chomp.to!long; long r = 1, k; foreach (_; 0..10^^7+1) { k = (k*10+7) % K; if (k == 0) { writeln(r); return; } ++r; } writeln(-1); }
D
import std.stdio, std.string, std.conv; void main() { auto ip = readln.split, A = ip[0], B = ip[1], C = ip[2]; if(A == B){ writeln(C); } if(B == C){ writeln(A); } if(C == A){ writeln(B); } }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); void main() { string s; scan(s); // writeln(s); foreach (i; 2 .. s.length) { // writeln(s[0 .. $ - i]); auto ss = s[0 .. $ - i]; if (func(ss)) { writeln(ss.length); return; } } // writeln(s[0 .. (s.length / 2)]); // writeln(s[(s.length / 2) .. s.length]); } auto func(string s) { if (s[0 .. (s.length / 2)] == s[(s.length / 2) .. s.length]) { return true; } else { return false; } } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional, std.traits; import std.algorithm, std.container; import core.stdc.stdlib; void main() { auto N = scanElem; auto K = scanElem; char[] S = readln.strip.to!(char[]); for(int i=0;i <N;i++) { if(K-1==i){ if(S[i]=='A') write('a'); if(S[i]=='B') write('b'); if(S[i]=='C') write('c'); }else write(S[i]); } writeln(""); } long gcd(long a, long b) { if(b == 0) return a; return gcd(b, a % b); } class UnionFind{ UnionFind parent = null; void merge(UnionFind a) { if(same(a)) return; a.root.parent = this.root; } UnionFind root() { if(parent is null)return this; return parent = parent.root; } bool same(UnionFind a) { return this.root == a.root; } } void scanValues(TList...)(ref TList list) { auto lit = readln.splitter; foreach (ref e; list) { e = lit.fornt.to!(typeof(e)); lit.popFront; } } T[] scanArray(T = long)() { return readln.split.to!(T[]); } void scanStructs(T)(ref T[] t, size_t n) { t.length = n; foreach (ref e; t) { auto line = readln.split; foreach (i, ref v; e.tupleof) { v = line[i].to!(typeof(v)); } } } long scanULong(){ long x; while(true){ const c = getchar; if(c<'0'||c>'9'){ break; } x = x*10+c-'0'; } return x; } T scanElem(T = long)() { char[] res; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { res ~= cast(char) c; c = getchar; } return res.strip.to!T; } template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } struct Factor { long n; long c; } Factor[] factors(long n) { Factor[] res; for (long i = 2; i ^^ 2 <= n; i++) { if (n % i != 0) continue; int c; while (n % i == 0) { n = n / i; c++; } res ~= Factor(i, c); } if (n != 1) res ~= Factor(n, 1); return res; } long[] primes(long n) { if(n<2)return []; auto table = new long[n+1]; long[] res; for(int i = 2;i<=n;i++) { if(table[i]==-1) continue; for(int a = i;a<table.length;a+=i) { table[a] = -1; } res ~= i; } return res; } bool isPrime(long n) { if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (long i = 3; i ^^ 2 <= n; i += 2) if (n % i == 0) return false; return true; }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { const tmp = readln.split.to!(long[]); const N = tmp[0], K = tmp[1]; writeln(2 * K - 1 <= N ? "YES" : "NO"); }
D
import std.stdio, std.string, std.conv, std.algorithm; void main() { int n = readln.chomp.to!int; long[] a = readln.split.to!(long[]); long[] b = readln.split.to!(long[]); long monster; foreach (i; 0 .. n) { if (a[i] < b[i]) { long c = min(a[i+1], b[i]-a[i]); a[i+1] -= c; monster += a[i] + c; } else { monster += b[i]; } } monster.writeln; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { long N, A, B, C; scan(N, A, B, C); ubyte mask = cast(ubyte)((1 << N) - 1); long[] L = new long[](N); foreach (i; 0 .. N) scan(L[i]); long ans = long.max; foreach (ubyte i; 0 .. 255) foreach (ubyte j; 0 .. 255) foreach (ubyte k; 0 .. 255) { ubyte a, b, c; a = cast(ubyte)(i + 1); b = cast(ubyte)(j + 1); c = cast(ubyte)(k + 1); if ((a & mask) != a || (b & mask) != b || (c & mask) != c) continue; if ((a & b) != 0 || (b & c) != 0 || (c & a) != 0) continue; long[] len = [0, 0, 0]; ubyte[] choice = [a, b, c]; long cost; foreach (l; 0 .. 8) { ubyte mask2 = cast(ubyte)(1 << l); foreach (m, x; choice) if ((x & mask2) != 0) { cost += (len[m] == 0) ? 0 : 10; len[m] += L[l]; } } cost += abs(len[0] - A); cost += abs(len[1] - B); cost += abs(len[2] - C); // if (cost < ans) // writefln("%d %d %d", a, b, c); ans = min(ans, cost); } writeln(ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container; long P = 10^^9+7; long[10^^5*5+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); } 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; } 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! } long perm(N)(N n, N k) { if (k > n) return 0; auto n_b = F[n]; auto n_k_b = RF[n-k]; return (n_b * n_k_b) % P; } void main() { init(); auto nm = readln.split.to!(long[]); auto N = nm[0]; auto M = nm[1]; long r; foreach (s; 0..N+1) { auto d = comb(N, s) * perm(M, s) % P * pow(perm(M-s, N-s), 2) % P * (-1)^^s; (r += d + P) %= P; } writeln(r); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto A = RD; auto B = RD; writeln(abs(A-B) % 2 == 0 ? "Alice" : "Borys"); stdout.flush(); debug readln(); }
D
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; string n; void main() { int n; scan(n); if (n / 100 == n % 10) { writeln("Yes"); } else { writeln("No"); } } 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.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto K = RD; writeln(K == 1 ? 0 : N - K); stdout.flush(); debug readln(); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; 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() { char[] s = sread().to!(char[]); s[5] = s[13] = ' '; s.writeln(); }
D
import std.stdio,std.conv,std.string,std.algorithm; void main(){ auto ab=readln().chomp().split().map!(to!int); int a=ab[0],b=ab[1]; if(a%2==0 || b%2==0) writeln("Even"); else writeln("Odd"); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long H, W; scan(H, W); long N = lread(); auto A = aryread(); long i; auto T = new long[](H * W); foreach (j, a; A) { foreach (_; 0 .. a) T[i++] = j + 1; } foreach (j; 0 .. H) { if (j & 1) { foreach_reverse (k; 1 .. W) { write(T[j * W + k], " "); } writeln(T[j * W]); } else { foreach (k; 0 .. W - 1) { write(T[j * W + k], " "); } writeln(T[(j + 1) * W - 1]); } } }
D
import std.stdio, std.conv, std.string, std.algorithm, std.range; void main() { auto a = readln.split[0].to!int; auto s = readln.split[0]; if(a >= 3200) s.writeln; else "red".writeln; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; long k, a, b; rd(k, a, b); if(a>=k){writeln(1); return;} auto d=a-b; if(d<=0){writeln(-1); return;} auto tmp=(k-a+d-1)/d; writeln(tmp*2+1); } 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
/+ dub.sdl: name "C" dependency "dunkelheit" version="1.0.1" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner, dkh.datastructure.segtree; void solve(Scanner sc) { int n, m; sc.read(n, m); int[] edges; int[] used = new int[3 * n]; foreach (i; 0..m) { int a, b; sc.read(a, b); a--; b--; if (edges.length.to!int < n) { if (used[a] || used[b]) continue; used[a] = used[b] = true; edges ~= i + 1; } } if (edges.length == n) { writeln("Matching"); edges.map!(to!string).join(" ").writeln; } else { writeln("IndSet"); iota(3 * n).filter!(i => !used[i]).take(n).map!(i => (i + 1).to!string).join(" ").writeln; } } int main() { Scanner sc = new Scanner(stdin); scope(exit) assert(!sc.hasNext); int t; sc.read(t); foreach (i; 0..t) { solve(sc); } return 0; } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/datastructure/segtree.d */ // module dkh.datastructure.segtree; import std.conv : to; import std.functional : binaryFun; import std.traits : isInstanceOf; struct SegTree(alias E, Args...) { import std.traits : ReturnType; alias Engine = E!Args; alias T = Engine.DataType; alias L = Engine.LazyType; Engine eng; this(size_t n) { eng = Engine(n.to!uint); } this(T[] first) { eng = Engine(first); } @property size_t length() const { return eng.length(); } @property size_t opDollar() const { return eng.length(); } struct Range { Engine* eng; size_t start, end; @property const(T) sum() { return eng.sum(start.to!uint, end.to!uint); } } const(T) opIndex(size_t k) { assert(0 <= k && k < eng.length()); return eng.single(k.to!uint); } void opIndexAssign(T x, size_t k) { assert(0 <= k && k < eng.length()); eng.singleSet(k.to!uint, x); } size_t[2] opSlice(size_t dim : 0)(size_t start, size_t end) { assert(0 <= start && start <= end && end <= eng.length()); return [start, end]; } Range opIndex(size_t[2] rng) { return Range(&eng, rng[0].to!uint, rng[1].to!uint); } static if (!is(L == void)) { void opIndexOpAssign(string op : "+")(L x, size_t[2] rng) { eng.add(rng[0].to!uint, rng[1].to!uint, x); } } } ptrdiff_t binSearchLeft(alias pred, TR)(TR t, ptrdiff_t a, ptrdiff_t b) if (isInstanceOf!(SegTree, TR)) { return TR.Engine.BinSearch!(false, pred)(t.eng, a.to!int, b.to!int); } ptrdiff_t binSearchRight(alias pred, TR)(TR t, ptrdiff_t a, ptrdiff_t b) if (isInstanceOf!(SegTree, TR)) { return TR.Engine.BinSearch!(true, pred)(t.eng, a.to!int, b.to!int); } alias SimpleSeg(T, alias opTT, T eT, alias Engine = SimpleSegEngine) = SegTree!(Engine, T, binaryFun!opTT, eT); struct SimpleSegEngine(T, alias opTT, T eT) { alias DataType = T; alias LazyType = void; alias BinSearch = binSearchSimple; uint n, sz, lg; T[] d; @property uint length() const {return n;} this(uint n) { import std.algorithm : each; this.n = n; if (n == 0) return; while ((2^^lg) < n) lg++; sz = 2^^lg; d = new T[](2*sz); d.each!((ref x) => x = eT); } this(T[] first) { import std.conv : to; import std.algorithm : each; n = first.length.to!uint; if (n == 0) return; while ((2^^lg) < n) lg++; sz = 2^^lg; d = new T[](2*sz); d.each!((ref x) => x = eT); foreach (i; 0..n) { d[sz+i] = first[i]; } foreach_reverse (i; 1..sz) { update(i); } } pragma(inline): void update(uint k) { d[k] = opTT(d[2*k], d[2*k+1]); } T single(uint k) { return d[k+sz]; } void singleSet(uint k, T x) { k += sz; d[k] = x; foreach (uint i; 1..lg+1) { update(k>>i); } } T sum(uint a, uint b) { assert(0 <= a && a <= b && b <= n); T sml = eT, smr = eT; a += sz; b += sz; while (a < b) { if (a & 1) sml = opTT(sml, d[a++]); if (b & 1) smr = opTT(d[--b], smr); a >>= 1; b >>= 1; } return opTT(sml, smr); } } int binSearchSimple(bool rev, alias pred, TR)(TR t, int a, int b) { import std.traits : TemplateArgsOf; alias args = TemplateArgsOf!TR; alias opTT = args[1]; auto x = args[2]; with (t) { static if (!rev) { if (pred(x)) return a-1; int pos = a; void f(int a, int b, int l, int r, int k) { if (b <= l || r <= a) return; if (a <= l && r <= b && !pred(opTT(x, d[k]))) { x = opTT(x, d[k]); pos = r; return; } if (l+1 == r) return; int md = (l+r)/2; f(a, b, l, md, 2*k); if (pos >= md) f(a, b, md, r, 2*k+1); } f(a, b, 0, sz, 1); return pos; } else { if (pred(x)) return b; int pos = b-1; void f(int a, int b, int l, int r, int k) { if (b <= l || r <= a) return; if (a <= l && r <= b && !pred(opTT(x, d[k]))) { x = opTT(d[k], x); pos = l-1; return; } if (l+1 == r) return; int md = (l+r)/2; f(a, b, md, r, 2*k+1); if (pos < md) f(a, b, l, md, 2*k); } f(a, b, 0, sz, 1); return pos; } } } alias LazySeg(T, L, alias opTT, alias opTL, alias opLL, T eT, L eL, alias Engine = LazySegEngine) = SegTree!(Engine, T, L , binaryFun!opTT, binaryFun!opTL, binaryFun!opLL, eT, eL); struct LazySegEngine(T, L, alias opTT, alias opTL, alias opLL, T eT, L eL) { import std.typecons : Tuple; alias DataType = T; alias LazyType = L; alias BinSearch = binSearchLazy; alias S = Tuple!(T, "d", L, "lz"); uint n, sz, lg; S[] s; this(uint n) { import std.conv : to; import std.algorithm : each; this.n = n; uint lg = 0; while ((2^^lg) < n) lg++; this.lg = lg; sz = 2^^lg; s = new S[](2*sz); s.each!((ref x) => x = S(eT, eL)); } this(T[] first) { import std.conv : to; import std.algorithm : each; n = first.length.to!uint; uint lg = 0; while ((2^^lg) < n) lg++; this.lg = lg; sz = 2^^lg; s = new S[](2*sz); s.each!((ref x) => x = S(eT, eL)); foreach (i; 0..n) { s[sz+i].d = first[i]; } foreach_reverse (i; 1..sz) { update(i); } } @property uint length() const { return n; } pragma(inline): private void lzAdd(uint k, in L x) { s[k].lz = opLL(s[k].lz, x); s[k].d = opTL(s[k].d, x); } public void push(uint k) { if (s[k].lz == eL) return; lzAdd(2*k, s[k].lz); lzAdd(2*k+1, s[k].lz); s[k].lz = eL; } private void update(uint k) { s[k].d = opTT(s[2*k].d, s[2*k+1].d); } T single(uint k) { k += sz; foreach_reverse (uint i; 1..lg+1) { push(k>>i); } return s[k].d; } void singleSet(uint k, T x) { k += sz; foreach_reverse (uint i; 1..lg+1) { push(k>>i); } s[k].d = x; foreach (uint i; 1..lg+1) { update(k>>i); } } T sum(uint a, uint b) { assert(0 <= a && a <= b && b <= n); if (a == b) return eT; a += sz; b--; b += sz; uint tlg = lg; while (true) { uint k = a >> tlg; if (a >> tlg != b >> tlg) { tlg++; break; } if (((a-1) >> tlg) + 2 == (b+1) >> tlg) return s[k].d; push(k); tlg--; } T sm = eT; foreach_reverse (l; 0..tlg) { uint k = a >> l; if ((a-1)>>l != a>>l) { sm = opTT(s[k].d, sm); break; } push(k); if (!((a >> (l-1)) & 1)) sm = opTT(s[2*k+1].d, sm); } foreach_reverse (l; 0..tlg) { uint k = b >> l; if (b>>l != (b+1)>>l) { sm = opTT(sm, s[k].d); break; } push(k); if ((b >> (l-1)) & 1) sm = opTT(sm, s[2*k].d); } return sm; } void add(uint a, uint b, L x) { assert(0 <= a && a <= b && b <= n); if (a == b) return; a += sz; b--; b += sz; uint tlg = lg; while (true) { uint k = a >> tlg; if (a >> tlg != b >> tlg) { tlg++; break; } if (((a-1) >> tlg) + 2 == (b+1) >> tlg) { lzAdd(k, x); foreach (l; tlg+1..lg+1) { update(a >> l); } return; } push(k); tlg--; } foreach_reverse (l; 0..tlg) { uint k = a >> l; if ((a-1)>>l != a>>l) { lzAdd(k, x); foreach (h; l+1..tlg) { update(a >> h); } break; } push(k); if (!((a >> (l-1)) & 1)) lzAdd(2*k+1, x); } foreach_reverse (l; 0..tlg) { uint k = b >> l; if (b>>l != (b+1)>>l) { lzAdd(k, x); foreach (h; l+1..tlg) { update(b >> h); } break; } push(k); if ((b >> (l-1)) & 1) lzAdd(2*k, x); } foreach (l; tlg..lg+1) { update(a >> l); } } } int binSearchLazy(bool rev, alias pred, TR)(TR t, int a, int b) { import std.traits : TemplateArgsOf; alias args = TemplateArgsOf!TR; alias opTT = args[2]; auto x = args[5]; with (t) { static if (!rev) { if (pred(x)) return a-1; int pos = a; void f(int a, int b, int l, int r, int k) { if (b <= l || r <= a) return; if (a <= l && r <= b && !pred(opTT(x, s[k].d))) { x = opTT(x, s[k].d); pos = r; return; } if (l+1 == r) return; push(k); int md = (l+r)/2; f(a, b, l, md, 2*k); if (pos >= md) f(a, b, md, r, 2*k+1); } f(a, b, 0, sz, 1); return pos; } else { if (pred(x)) return b; int pos = b-1; void f(int a, int b, int l, int r, int k) { if (b <= l || r <= a) return; if (a <= l && r <= b && !pred(opTT(x, s[k].d))) { x = opTT(s[k].d, x); pos = l-1; return; } if (l+1 == r) return; push(k); int md = (l+r)/2; f(a, b, md, r, 2*k+1); if (pos < md) f(a, b, l, md, 2*k); } f(a, b, 0, sz, 1); return pos; } } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/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; private File f; this(File f) { this.f = f; } private char[512] lineBuf; private char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { assert(succW()); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(Args...)(auto ref Args args) { import std.exception : enforce; static if (args.length != 0) { enforce(readSingle(args[0])); read(args[1..$]); } } bool hasNext() { return succ(); } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; enum P = 10L^^9+7; void main() { auto K = readln.chomp.to!(char[]); auto D = readln.chomp.to!long; auto DP1 = new long[][](2, D); auto DP2 = new long[][](2, D); foreach (i, c; K) { if (i == 0) { foreach (x; 0..c-'0') { ++DP1[0][x % D]; } ++DP1[1][(c-'0') % D]; } else { foreach (x; 0..D) { foreach (y; 0..10) { DP1[0][(x+y)%D] = (DP1[0][(x+y)%D] + DP2[0][x]) % P; } foreach (y; 0..c-'0') { DP1[0][(x+y)%D] = (DP1[0][(x+y)%D] + DP2[1][x]) % P; } DP1[1][(x+c-'0')%D] = (DP1[1][(x+c-'0')%D] + DP2[1][x]) % P; } } DP2[0][] = DP1[0][].dup; DP2[1][] = DP1[1][].dup; DP1[0][] = 0; DP1[1][] = 0; } writeln((DP2[1][0] + DP2[0][0] - 1 + P) % P); }
D
import std.stdio, std.conv, std.string, std.algorithm, std.range; void main() { while(true) { auto a = readln.split.map!(to!int), b = readln.split.map!(to!int); if(a.length != 4) break; auto hit = iota(4).map!(i => a[i] == b[i] ? 1 : 0).reduce!"a + b", blow = a.map!(n => count(b, n)).reduce!"a + b" - hit; writeln(hit, " ", blow); } }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { foreach (test; 0..readln.strip.to !(int)) { auto s = readln.strip; auto x = readln.strip.to !(int); auto n = s.length.to !(int); auto w = new char [n]; w[] = '1'; foreach (i; 0..n) { if (s[i] == '0') { if (i - x >= 0) { w[i - x] = '0'; } if (i + x < n) { w[i + x] = '0'; } } } bool ok = true; foreach (i; 0..n) { if (s[i] == '1') { if (!(i - x >= 0 && w[i - x] == '1') && !(i + x < n && w[i + x] == '1')) { ok = false; } } } writeln (ok ? w : "-1"); } }
D
// AtCoder My Practice // author: Leonardone @ NEETSDKASU import std.stdio : readln, writeln; import std.string : chomp; import std.conv : to; void main() { auto n = readln.chomp.to!uint; auto s1 = readln.chomp; auto s2 = readln.chomp; long ans, md = 1_000_000_007; uint i, pat; if (s1[0] == s2[0]) { ans = 3; i = 1; pat = 0; } else { ans = 6; i = 2; pat = 1; } while (i < n) { if (s1[i] == s2[i]) { if (pat == 0) { ans = (ans * 2) % md; } pat = 0; i += 1; } else { if (pat == 1) { ans = (ans * 3) % md; } else { ans = (ans * 2) % md; } pat = 1; i += 2; } } writeln(ans); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.ascii; void main() { auto s = readln.chomp; auto a = "AKIHABARA"; int i; auto f = false; foreach (e; s) { if (i < a.length && e == a[i]) { i++; } else if (i < a.length && a[i] == 'A') { i++; if (i < a.length && e == a[i]) { i++; } else { f = true; break; } } else { f = true; break; } } if (i == a.length - 1) { } else if (i != a.length) { f = true; } if (f) { writeln("NO"); } else { writeln("YES"); } }
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 m = to!int(readln().strip()); int[20][20] g; foreach(i; 0..20) { foreach(j; 0..20) { g[i][j] = int.max/100; } } foreach(i; 0..m) { auto s = map!(a => to!int(a))(readln().strip().split(",")); int a = s[0]-1; int b = s[1]-1; int c = s[2]; int d = s[3]; g[a][b] = c; g[b][a] = d; } auto s = map!(a => to!int(a))(readln().strip().split(",")); int x1 = s[0]-1; int x2 = s[1]-1; int y1 = s[2]; int y2 = s[3]; foreach(k; 0..n) { foreach(i; 0..n) { foreach(j; 0..n) { g[i][j] = min(g[i][j], g[i][k]+g[k][j]); } } } writeln(y1-y2-g[x1][x2]-g[x2][x1]); }
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 n = readint(); bool[string] map; for (int i = 0; i < n; i++) { auto ss = readln.split; switch (ss[0][0]) { case 'i': map[ss[1]] = true; break; case 'f': writeln(ss[1] in map ? "yes" : "no"); break; default: } } }
D
void main() { string s = rdStr; long len = s.length; long[] lists = new long[len]; auto g = s.group.array; long cnt; foreach (x; g) { long n = x[1].to!long; cnt += n; if (x[0] == 'R') { lists[cnt-1] += (n >> 1) + (n & 1); lists[cnt] += (n >> 1); } else { lists[cnt-n-1] += (n >> 1); lists[cnt-n] += (n >> 1) + (n & 1); } } foreach (i, x; lists) { x.write; if (i == len - 1) writeln; else " ".write; } } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; long P = 10^^9+7; long[10^^6*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); } 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; } 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 rc = readln.split.to!(long[]); auto r1 = rc[0]; auto c1 = rc[1]; auto r2 = rc[2]; auto c2 = rc[3]; writeln((comb(r2+c2+2, c2+1) - comb(r1+c2+1, c2+1) - comb(r2+c1+1, c1) + P*2 + comb(r1+c1, c1)) % P); }
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 H, N; scan(H, N); auto a = new int[](N); auto b = new int[](N); iota(N).each!(i => scan(a[i], b[i])); const lim = H + a.reduce!max + 1; auto dp = new long[][](N + 1, lim); fillAll(dp, infl); dp[0][0] = 0; foreach (i; 1 .. N + 1) { foreach (x; 0 .. lim) { chmin(dp[i][x], dp[i-1][x]); if (x - a[i-1] >= 0) { chmin(dp[i][x], dp[i][x - a[i-1]] + b[i-1]); } } } long ans = infl; foreach (x; H .. lim) { chmin(ans, dp[N][x]); } 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.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; long calc(long a, long b, long[] h) { // m 回で全ての魔物を倒せるか bool check(long m) { long k = 0; foreach (e; h) { long x = e - b * m; // m 回分の b ダメージ if (x > 0) { // a ダメージを何回与えると倒せるか k += (x + (a - b) - 1) / (a - b); } } return m >= k; } long lo = 1; long hi = int.max; long ans = hi; while (lo <= hi) { long m = (lo + hi) / 2; if (check(m)) { ans = min(ans, m); hi = m - 1; } else lo = m + 1; } return ans; } void main() { int n, a, b; scan(n, a, b); long[] h; foreach (_; 0..n) h ~= read!long; writeln(calc(a, b, h)); } void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int;
D
void main() { auto X = ri; if(X == 7 || X == 5 || X == 3) { writeln("YES"); } else writeln("NO"); } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void get(Args...)(ref Args args) { import std.traits, std.meta, std.typecons; static if (Args.length == 1) { alias Arg = Args[0]; static if (isArray!Arg) { args[0] = readln.split.to!Arg; } else static if (isTuple!Arg) { auto input = readln.split; static foreach (i; 0..Fields!Arg.length) { args[0][i] = input[i].to!(Fields!Arg[i]); } } else { args[0] = readln.chomp.to!Arg; } } else { auto input = readln.split; assert(input.length == Args.length); static foreach (i; 0..Args.length) { args[i] = input[i].to!(Args[i]); } } } void get_lines(Args...)(size_t N, ref Args args) { import std.traits, std.range; static foreach (i; 0..Args.length) { static assert(isArray!(Args[i])); args[i].length = N; } foreach (i; 0..N) { static if (Args.length == 1) { get(args[0][i]); } else { auto input = readln.split; static foreach (j; 0..Args.length) { args[j][i] = input[j].to!(ElementType!(Args[j])); } } } } int celiPow2(int n) @safe pure nothrow @nogc { int x = 0; while ((1u << x) < cast(uint)(n)) x++; return x; } struct Segtree(S, alias op, alias e) { import std.functional : binaryFun, unaryFun; import std.traits : isCallable, Parameters; static if (is(typeof(e) : string)) { auto unit() { return mixin(e); } } else { alias unit = e; } this(int n) { auto buf = new S[](n); buf[] = unit(); this(buf); } this(S[] v) { _n = cast(int) v.length; log = celiPow2(_n); size = 1 << log; d = new S[](2 * size); d[] = unit(); foreach (i; 0 .. _n) d[size + i] = v[i]; foreach_reverse (i; 1 .. size) update(i); } void set(int p, S x) { assert(0 <= p && p < _n); p += size; d[p] = x; foreach (i; 1 .. log + 1) update(p >> i); } S get(int p) { assert(0 <= p && p < _n); return d[p + size]; } S prod(int l, int r) { assert(0 <= l && l <= r && r <= _n); S sml = unit(), smr = unit(); l += size; r += size; while (l < r) { if (l & 1) sml = binaryFun!(op)(sml, d[l++]); if (r & 1) smr = binaryFun!(op)(d[--r], smr); l >>= 1; r >>= 1; } return binaryFun!(op)(sml, smr); } S allProd() { return d[1]; } int maxRight(alias f)(int l) { return maxRight(l, &unaryFun!(f)); } int maxRight(F)(int l, F f) if (isCallable!F && Parameters!(F).length == 1) { assert(0 <= l && l <= _n); assert(f(unit())); if (l == _n) return _n; l += size; S sm = unit(); do { while (l % 2 == 0) l >>= 1; if (!f(binaryFun!(op)(sm, d[l]))) { while (l < size) { l = 2 * l; if (f(binaryFun!(op)(sm, d[l]))) { sm = binaryFun!(op)(sm, d[l]); l++; } } return l - size; } sm = binaryFun!(op)(sm, d[l]); l++; } while ((l & -l) != l); return _n; } int minLeft(alias f)(int r) { return minLeft(r, &unaryFun!(f)); } int minLeft(F)(int r, F f) if (isCallable!F && Parameters!(F).length == 1) { assert(0 <= r && r <= _n); assert(f(unit())); if (r == 0) return 0; r += size; S sm = unit(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!f(binaryFun!(op)(d[r], sm))) { while (r < size) { r = 2 * r + 1; if (f(binaryFun!(op)(d[r], sm))) { sm = binaryFun!(op)(d[r], sm); r--; } } return r + 1 - size; } sm = binaryFun!(op)(d[r], sm); } while ((r & -r) != r); return 0; } private: int _n = 0, size = 1, log = 0; S[] d = [unit(), unit()]; void update(int k) { d[k] = binaryFun!(op)(d[2 * k], d[2 * k + 1]); } } void main() { int N, K; get(N, K); auto segt = Segtree!(int, "a >= b ? a : b", () => 0)(300_001); while (N--) { int A; get(A); auto l = segt.prod(max(0, A-K), min(300_000, A+K)+1); segt.set(A, l + 1); } writeln(segt.allProd()); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto ab = readln.chomp.split.to!(int[]); auto d = ab[1] - ab[0]; int sum; foreach (i; 1..d) { sum += i; } writeln(sum - ab[0]); }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { auto S = readln.chomp; iota(S.length-2).map!(i => S[i..i+3]).map!(s => abs(753-s.to!int)).reduce!min.writeln; }
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 main(){ auto ip1 = readln.chomp; auto ip2 = readln.chomp; auto ip3 = readln.chomp; ("" ~ ip1[0] ~ ip2[1] ~ ip3[2]).writeln; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; long calc(int a, int[] xs) { int n = cast(int)xs.length; int m = xs.sum; auto dp = new long[][](n + 1, m + 1); foreach (x; xs) { for (int i = n-1; i > 0; i--) { for (int j = 0; j < m; j++) { if (dp[i][j] > 0) { dp[i + 1][j + x] += dp[i][j]; } } } dp[1][x]++; } long ans = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j <= m; j++) { if (dp[i][j] > 0 && (j % i == 0) && (j / i == a)) { ans += dp[i][j]; } } } return ans; } void main() { auto na = readints; int a = na[1]; auto xs = readints; writeln(calc(a, xs)); }
D
void main() { long[] tmp = readln.split.to!(long[]); long n = tmp[0], k = tmp[1]; long[] a = readln.split.to!(long[]); long mod = 10 ^^ 9 + 7; long[] l = new long[n], r = new long[n]; foreach (i; 0 .. n) { foreach (j; 0 .. n) { if (a[i] > a[j]) { if (i > j) ++l[i]; else ++r[i]; } } } long lsum = k * (k - 1) / 2 % mod; long rsum = k * (k + 1) / 2 % mod; long result; foreach (i; 0 .. n) { long total = (l[i] * lsum) % mod + (r[i] * rsum) % mod; total %= mod; result = (result + total) % mod; } result.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.math; void main() { int n, m; scan(n, m); auto cnt = new int[](n); auto red = new bool[](n); cnt[] = 1; red[0] = 1; foreach (i ; 0 .. m) { int xi, yi; scan(xi, yi); xi--, yi--; cnt[xi]--; cnt[yi]++; if (red[xi]) red[yi] = 1; if (cnt[xi] == 0) red[xi] = 0; } auto ans = iota(n).count!(i => red[i]); 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.conv; import std.algorithm; import std.range; import std.string; void main() { bool[][string] cards; string[] suits = [ "S", "H", "C", "D" ]; foreach (suit; suits) { cards[suit] = new bool[13]; } readln; foreach (string line; stdin.lines) { auto card = line.chomp.split; string suit = card[0]; int num = card[1].to!int; cards[suit][num-1] = true; } foreach (suit; suits) { foreach (i, num; cards[suit]) { if (num == false) { writeln(suit, " ", i + 1); } } } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto N = readln.chomp.to!int; auto dx = readln.split.to!(int[]); auto D = dx[0]; auto X = dx[1]; int[] AS; AS.length = N; foreach (i; 0..N) AS[i] = readln.chomp.to!int; foreach (d; 0..D) { foreach (a; AS) if (d%a == 0) ++X; } writeln(X); }
D
import std.algorithm; import std.range; import std.stdio; import std.string; immutable string vowels = "aeiou"; void main () { string s; while ((s = readln.strip) != "") { string [] res; main_loop: while (!s.empty) { foreach (i; 2..s.length) { if (!vowels.canFind (s[i - 2]) && !vowels.canFind (s[i - 1]) && !vowels.canFind (s[i - 0]) && (s[i - 2] != s[i - 1] || s[i - 1] != s[i - 0])) { res ~= s[0..i]; s = s[i..$]; continue main_loop; } } res ~= s; s = ""; } res.join (" ").writeln; } }
D
import std.stdio, std.string, std.array, std.conv; void main() { int n = readln.chomp.to!int; int[] a = readln.chomp.split.to!(int[]); a[] -= 1; int[] b = readln.chomp.split.to!(int[]); int[] c = readln.chomp.split.to!(int[]); int prev = int.min, sum; foreach (x; a) { sum += b[x]; if (prev == x - 1) sum += c[prev]; prev = x; } sum.writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { for (;;) { auto wh = readln.split.to!(int[]); auto w = wh[0]; auto h = wh[1]; if (w == 0 && h == 0) return; auto MAP = new int[][](h, w); foreach (i; 0..h) { foreach (j, n; readln.split.to!(int[])) MAP[i][j] = n; } int c = 1; foreach (j; 0..w) foreach (i; 0..h) if (MAP[i][j] == 1) { ++c; void go(int x, int y) { MAP[y][x] = c; foreach (dx; [-1,0,1]) foreach (dy; [-1,0,1]) { auto xx = x+dx; auto yy = y+dy; if (xx < 0 || xx >= w || yy < 0 || yy >= h || MAP[yy][xx] != 1) continue; go(xx, yy); } } go(j, i); } writeln(c-1); } }
D
//dlang template---{{{ import std.stdio; import std.conv; import std.string; import std.array; import std.algorithm; // MIT-License https://github.com/kurokoji/nephele class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } //Digit count---{{{ int DigitNum(int num) { int digit = 0; while (num != 0) { num /= 10; digit++; } return digit; } //}}} //}}} void main() { Scanner sc = new Scanner; string s; sc.scan(s); int res = 0; foreach (i; 0 .. 3) { if (s[i] == '1') res++; } writeln(res); }
D
void main() { long n, m; rdVals(n, m); long[] h = rdRow; long[][] edge = new long[][](n); foreach (i; 0 .. m) { long a, b; rdVals(a, b); --a, --b; edge[a] ~= b, edge[b] ~= a; } long result; foreach (i; 0 .. n) { bool ok = true; foreach (e; edge[i]) { if (h[i] <= h[e]) ok = false; } if (ok) ++result; } result.writeln; } enum long mod = 10^^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 core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } enum MOD = 10 ^^ 9 + 7; void main() { long N = lread(); string S = sread(); long[] T = new long[N + 1]; foreach (i; 0 .. N) T[i + 1] = T[i] + (S[i] == '#'); // writeln(S); // writeln(T); long ans = long.max; foreach (i; 0 .. N) { long a = (T[i + 1]); long b = ((N - i - 1) - (T[$ - 1] - T[i])); // writefln("%d %d %d", i, a, b); ans = ans.min(a + b); } writeln(ans); }
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(); } void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ int n = read.to!int; if(n % 2){ 0.writeln; return; } long ans = 1; foreach(i; 0 .. n / 2){ ans *= 2; } ans.writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.15f"; 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; auto K = RD; auto A = RDA; long x, y; foreach (i; 0..N) { x = gcd(x, A[i]); y = max(y, A[i]); } debug writeln(x); writeln(y >= K && K % x == 0 ? "POSSIBLE" : "IMPOSSIBLE"); stdout.flush(); debug readln(); }
D
void main() { problem(); } void problem() { auto X = scan!char; string solve() { return 'A' <= X && X <= 'Z' ? "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.stdio, std.string, std.conv; import std.array, std.algorithm, std.range; import std.numeric; void solve() { char['z'+1] t; t[' ']=' '; auto s = readln().chomp(); foreach(a;1..26) if(gcd(a,26)!=1) continue; else foreach(b;0..26) { foreach(i,v;iota(26).map!(y=>(a*y+b)%26).array()) t[v+'a']=cast(char)(i+'a'); auto r = s.map!(c=>t[c]).array(); if(r.split().any!(w=>w=="that"||w=="this")) { writeln(r);return; } } } void main() { foreach(_;0..readln().chomp().to!int()) solve(); }
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 n, m; scan(n, m); auto from = new bool[](n); auto to = new bool[](n); foreach (_; iota(m)) { long a, b; scan(a, b); if (a == 1) to[b - 1] = true; if (b == n) from[a - 1] = true; } long ans; foreach (i; iota(n)) { ans |= (from[i] && to[i]); } if (ans) writeln("POSSIBLE"); else writeln("IMPOSSIBLE"); } 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 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 n, k; cin.scan(n, k); long w = n / k; writeln(min(n - w * k, abs(n - (w + 1) * k))); }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop; // dfmt on void main() { long N = lread(); alias T = Tuple!(long, char[]); DList!T Q; Q.insert(T(1, repeat('a').take(N).array())); while (!Q.empty) { auto t = Q.front; Q.removeFront(); if (t[0] == N) { writeln(t[1]); continue; } char c = 'a'; foreach (i; 0 .. t[0]) if (c < t[1][i]) c = t[1][i]; foreach (char x; 'a' .. cast(char)(c + 2)) { auto s = t[1].dup; s[t[0]] = x; Q.insertBack(T(t[0] + 1, s)); } } }
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 calc(long[] a) { long[long] freq; foreach (x; a) freq[x]++; long sum = 0; foreach (v; freq.values) { sum += v * (v - 1) / 2; } long[long] map; // 各ボールごとの組み合わせ数 foreach (k, v; freq) { map[k] = v * (v - 1) / 2; } foreach (x; a) { long ans = sum - map[x]; long k = freq[x] - 1; if (k > 1) ans += k * (k - 1) / 2; writeln(ans); } } void main() { readint; auto a = reads!long; calc(a); } void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int;
D
import std.stdio, std.string, std.conv, std.algorithm; void main() { int[] tmp = readln.split.to!(int[]); int p = tmp[0], q = tmp[1], r = tmp[2]; writeln(min(p+q, q+r, r+p)); }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; int solve (string s) { s = s.strip ('1'); if (s.empty) { return 0; } if (s.all !(q{a == '0'})) { return 1; } return 2; } void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { readln.strip.solve.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 long[](t); foreach (ti; 0..t) { auto n = RD!int; auto s = RD!int; auto cnt = n / 2 + 1; ans[ti] = s / cnt; } 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 t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto n = RD!int; auto k1 = RD!int; auto k2 = RD!int; auto w = RD!int; auto b = RD!int; auto x = min(k1, k2); auto y = abs(k1 - k2); auto z = n - max(k1, k2); w -= x + y / 2; b -= z + y / 2; ans[ti] = w <= 0 && b <= 0; } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush; debug readln; }
D
import std.stdio,std.string,std.conv,std.algorithm; void main(){ int inte; int[] ins; ins ~= to!int(chomp(readln())); ins ~= to!int(chomp(readln())); ins ~= to!int(chomp(readln())); ins ~= to!int(chomp(readln())); ins ~= to!int(chomp(readln())); ins ~= to!int(chomp(readln())); ins ~= to!int(chomp(readln())); ins ~= to!int(chomp(readln())); ins ~= to!int(chomp(readln())); ins ~= to!int(chomp(readln())); sort!("a > b")(ins); writeln( ins[0] ); writeln( ins[1] ); writeln( ins[2] ); }
D
import std.stdio, std.conv, std.string, std.algorithm; void main(){ auto a = readln.split.map!(to!int); writeln(a[0]*a[1], " ", (a[0]+a[1])*2); }
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 long[](t); foreach (ti; 0..t) { auto n = RD!int; auto a = RDA; auto b = RDA; long cnt, cnt0, cnt1; long last0, last1; foreach (i; 0..n) { if (b[i] == 0) { if (a[i] < last0) { ++cnt0; } last0 = a[i]; ++cnt; } else { if (a[i] < last1) { ++cnt1; } last1 = a[i]; } } if (min(cnt, n-cnt) == 0) ans[ti] = max(cnt0, cnt1) == 0; else ans[ti] = true; } foreach (e; ans) writeln(e ? "Yes" : "No"); 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; 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; bool isSq(int n) { int x = cast(int)sqrt(1.0 * n); return x * x == n; } int calc(int d, int[][] a) { int ans = 0; for (int i = 0; i < a.length; i++) { for (int j = i + 1; j < a.length; j++) { int len = 0; for (int k = 0; k < d; k++) { len += abs(a[i][k] - a[j][k]) ^^ 2; } if (isSq(len)) ans++; } } return ans; } void main() { int n, d; scan(n, d); int[][] a; foreach (_; 0..n) a ~= readints; writeln(calc(d, a)); }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { auto N = readln.chomp.to!long; auto a = readln.split.to!(long[]); foreach (i; 1..N) { a[i] += a[i-1]; } auto s = a.back; a[0..$-1].map!(a => abs(a * 2 - s)).reduce!min.writeln; }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; void main(){ auto Q=readln.split.to!(int[]),A=Q[0],B=Q[1],C=Q[2],D=Q[3]; if(A*B>C*D)writeln(A*B); else writeln(C*D); }
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 abcd = RDA; ans[ti] = [abcd[1], abcd[2], abcd[2]]; } foreach (e; ans) writeln(e[0], " ", e[1], " ", e[2]); stdout.flush; debug readln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.math; void main() { int n, q; scan(n, q); auto sd = SqrtDecomp(n); foreach (_ ; 0 .. q) { auto line = readln.split.to!(int[]); if (line[0] == 0) { sd.add(line[1] - 1, line[2]); } else { writeln(sd.sum(line[1] - 1, line[2])); } } } struct SqrtDecomp { private { int sqrtN; int N, K; int[] bucketSum; int[] data; } this(int n) { N = n; sqrtN = sqrt(n.to!real).to!int; K = (N + sqrtN - 1) / sqrtN; bucketSum = new int[](K); data = new int[](K * sqrtN); } void add(int i, int x) { int k = i / sqrtN; data[i] += x; bucketSum[k] += x; } int sum(int l, int r) { int res; foreach (k ; 0 .. K) { int bl = k * sqrtN, br = (k + 1) * sqrtN; if (r <= bl || br <= l) continue; if (l <= bl && br <= r) { res += bucketSum[k]; } else { foreach (i ; max(l, bl) .. min(r, br)) { res += data[i]; } } } return res; } } 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