code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional; import std.algorithm, std.container; long check(long n) { long i=1; while(n>0) { n-=i; i++; } return n==0?i:-1; } void main() { auto N = scanElem; if(N==1) { writeln("Yes"); writeln("2"); writeln("1 1"); writeln("1 1"); return; } long k = check(N); if(k==-1) { writeln("No"); return; } writeln("Yes"); writeln(k); long[] space = [0]; foreach(i; 0..k) { write(k-1); write(" "); foreach(s; space) { write(s+1); write(" "); } foreach(n; 1..k-space.length) { write(space[$-1]+n+1); write(" "); } writeln(""); space ~= space[$-1]+k-1-i; space[0..$-2]+=1; if(i==k-2) space=space[0..$-1]; } } class UnionFind{ UnionFind parent = null; void merge(UnionFind a) { if(same(a)) return; a.root.parent = this.root; } UnionFind root() { if(parent is null)return this; return parent = parent.root; } bool same(UnionFind a) { return this.root == a.root; } } void scanValues(TList...)(ref TList list) { auto lit = readln.splitter; foreach (ref e; list) { e = lit.fornt.to!(typeof(e)); lit.popFront; } } T[] scanArray(T = long)() { return readln.split.to!(long[]); } void scanStructs(T)(ref T[] t, size_t n) { t.length = n; foreach (ref e; t) { auto line = readln.split; foreach (i, ref v; e.tupleof) { v = line[i].to!(typeof(v)); } } } 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; import std.string, std.conv, std.array, std.algorithm; import std.uni, std.math, std.container; import core.bitop, std.datetime; void main(){ auto N = readln.chomp.to!int; auto A = readln.split.to!(int[]); auto ans = int.max; foreach(x; -100 .. 101){ ans = min(ans, calc_dif2(x, A)); } ans.writeln; } int calc_dif2(int x, int[] A){ int ret; foreach(a;A){ ret += (a - x)^^2; } return ret; }
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } /* ナップサック問題(価値の上限が小さい) xs[i][v]: i番目まで(index <= i)で、価値vを達成するのに必要な容量 xs[0][0] = 0 xs[0][v] = infinity xs[i][v] = min( ws[i], // i個目を取る前提 v - vs[i] < 0 の場合 xs[i - 1][v - vs[i]] + ws[i], // i個目を取る前提 v - vs[i] >= 0 xs[i - 1][v] // i個目を取らない前提 ) 求めるものは xs[n - 1][v] <= npw であるような最大のv */ void main(){ long n = read.to!long; long npw = read.to!long; long[] ws, vs; foreach(i; 0 .. n){ ws ~= read.to!long; vs ~= read.to!long; } long infty = npw * 100; long vmax; foreach(v; vs) vmax += v; long[][] xs = [[0]]; foreach(v; 1 .. vmax + 1) xs[0] ~= infty; foreach(i; 0 .. n){ long[] q = []; foreach(v; 0 .. vmax + 1){ q ~= min( xs[$ - 1][v], (v - vs[i] < 0)? ws[i]: xs[$ - 1][v - vs[i]] + ws[i] ); } debug writeln("q:", q, " i:", i); xs ~= q; } long ans; foreach(v; 0 .. vmax + 1) if(xs[n][v] <= npw) ans = v; ans.writeln; }
D
import std.stdio; import std.range; import std.array; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.container; import std.typecons; import std.random; import std.csv; import std.regex; import std.math; import core.time; import std.ascii; import std.digest.sha; import std.outbuffer; long n, m; long cal(long use) { long s = n + use / 2; long c2 = (m - use) / 2; return min(s, c2); } void main() { auto nm = readln.chomp.split.map!(to!long); n = nm[0]; m = nm[1]; long left = 0; long right = m + 1; while (right - left > 2) { long midL = (left * 2 + right) / 3; long midR = (left + right * 2) / 3; long resL = midL.cal; long resR = midR.cal; if (resL < resR) { left = midL; } else { right = midR; } } debug stderr.writeln([left, right]); debug stderr.writeln([left.cal, cal((left * 2 + right) / 3), cal((left + right * 2) / 3), right.cal]); // cal((left + right * 2) / 3) がそれ? よく分からないので全部試す… long ans = 0; for (long ite = left; ite < right; ++ite) { ans = max(ans, ite.cal); } ans.writeln; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { long x, a, b; scan(x, a, b); long z = b - a; if (z <= 0) { writeln("delicious"); return; } bool c = z <= x; writeln(c ? "safe" : "dangerous"); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { auto abc = readints; int a = abc[0], b = abc[1], c = abc[2]; writeln(b - a == c - b ? "YES" : "NO"); }
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; struct Queue(T) { private { size_t cap, head, tail; T[] data; } this (size_t n) in { assert(n > 0, "The capacity of a queue must be a positive integer."); } body { cap = n + 1; data = new T[](cap); } void clear() { head = tail = 0; } bool empty() { return head == tail; } bool full() { return head == (tail + 1) % cap; } size_t length() { return head <= tail ? tail - head : cap - head + tail; } T front() in { assert(!empty, "The queue is empty."); } body { return data[head]; } void removeFront() in { assert(!empty, "The queue is empty."); } body { (++head) %= cap; } alias popFront = removeFront; void insertBack(T x) in { assert(!full, "The queue is full."); } body { data[tail++] = x; tail %= cap; } alias insert = insertBack; T[] array() { return head <= tail ? data[head .. tail].dup : data[head .. $] ~ data[0 .. tail]; } string toString() { import std.format : format; if (head <= tail) { return format("[%(%s, %)]", data[head .. tail]); } else { return format("[%(%s, %)", data[head .. $]) ~ format(", %(%s, %)]", data[0 .. tail]); } } } unittest { auto a = Queue!(int)(5); //a.front; a.insert(2); a.insert(5); a.insert(3); //a.insert(8); import std.stdio; a.removeFront; a.removeFront; a.insert(11); a.insert(7); a.insert(123); a.insert(543); a.removeFront; a.removeFront; a.removeFront; a.insert(1212); a.insert(9898); a.insert(3154); a.removeFront; a.removeFront; a.removeFront; a.removeFront; a.removeFront; } unittest { auto q = Queue!(int)(5); assert(q.empty); q.insert(3); q.insert(1); q.insert(5); q.insert(4); q.insert(8); // q = [3,1,5,4,8] auto hoge = q.array; assert(hoge == [3,1,5,4,8]); assert(q.full); q.popFront(); q.popFront(); // q = [5,4,8] hoge = q.array; assert(hoge == [5,4,8]); hoge[0] = 3; hoge[2] = 11; assert(q.front == 5); assert(q.length == 3); q.popFront(); q.popFront(); // q = [8] q.insert(11); q.insert(-8); // q = [8,11,-8] assert(q.front == 8); assert(q.length == 3); q.insert(23); q.insert(31); // q = [8,11,-8,23,31] assert(q.full); assert(q.length == 5); auto a = new int[](0); foreach (e ; q) { a ~= e; } assert(a == [8, 11, -8, 23, 31]); foreach (i ; 0 .. 5) { q.popFront(); } assert(q.length == 0); assert(q.empty); hoge = q.array; assert(hoge == []); } unittest { auto q = Queue!(string)(4); q.insert("Hello"); q.insert(","); q.insert("World"); q.insert("!"); auto hoge = q.array; assert(hoge == ["Hello", ",", "World", "!"]); assert(q.full); assert(q.length == 4); q.popFront(); q.popFront(); assert(q.front == "World"); assert(q.length == 2); q.clear(); assert(q.empty); assert(q.length == 0); q.insert("hoge"); q.insert("fuga"); q.insert("melon"); q.popFront(); q.popFront; q.popFront; q.insert("hayo"); q.insert("hoge"); q.insert("pippi"); hoge = q.array; hoge[0] = "aho"; assert(q.front == "hayo"); q.insert("huni"); q.popFront; q.popFront; q.insert("ueue"); assert(q.front == "pippi"); assert(q.length == 3); } void main() { int N; scan(N); auto a = readln.split.to!(long[]); long ans; while (a.any!(ai => ai >= N)) { foreach (i ; 0 .. N) { auto d = max(0, a[i] - (N - 1)); auto t = (d + N - 1) / N; ans += t; a[i] -= N * t; foreach (j ; 0 .. N) { if (j != i) { a[j] += t; } } } debug { writeln(a); } } writeln(ans); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
void main() { problem(); } void problem() { auto L = scan!long; auto R = scan!long; auto d = scan!long; long solve() { long ans; foreach(i; L..R+1) { if (i % d == 0) ans++; } return ans; } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n; readV(n); long[] a; readA(n, a); auto b = cumulativeSum(a), m = 10L^^18; foreach (i; 1..n) m = min(m, (b[0..i] - b[i..$]).abs); writeln(m); } class CumulativeSum(T) { size_t n; T[] s; this(T[] a) { n = a.length; s = new T[](n+1); s[0] = T(0); foreach (i; 0..n) s[i+1] = s[i] + a[i]; } T opSlice(size_t l, size_t r) { return s[r]-s[l]; } size_t opDollar() { return n; } } auto cumulativeSum(T)(T[] a) { return new CumulativeSum!T(a); }
D
void main() { long[] tmp = rdRow; long n = tmp[0], k = tmp[1]; long[] x = rdRow; long result = 1L << 60; foreach (i; 0 .. n-k+1) { long time = abs(x[i]-x[i+k-1]) + min(x[i].abs, x[i+k-1].abs); result = min(result, time); } result.writeln; } T rdElem(T = long)() { //import std.stdio : readln; //import std.string : chomp; //import std.conv : to; return readln.chomp.to!T; } alias rdStr = rdElem!string; dchar[] rdDchar() { //import std.conv : to; return rdStr.to!(dchar[]); } T[] rdRow(T = long)() { //import std.stdio : readln; //import std.array : split; //import std.conv : to; return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdRow!T).array; } void wrMat(T = long)(T[][] mat) { //import std.stdio : write, writeln; foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto xy = readln.split.to!(long[]); auto X = xy[0]; auto Y = xy[1]; auto a = X; long r; while (a <= Y && a ) { ++r; a *= 2; } 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, std.bitmanip, std.regex; void main() { immutable long MOD = 10^^9 + 7; auto N = readln.chomp.to!int; auto A = N.iota.map!(_ => readln.chomp.to!long).array; auto X = A.reduce!"a^b"(); auto B = new bool[](40); foreach (a; A) B[bsf(a)] = true; int ans = 0; foreach_reverse (i; 0..33) { if ((X >> i) & 1) { if (!B[i]) { writeln(-1); return; } ans += 1; X ^= (1L << (i + 1)) - 1; } } writeln(X == 0 ? ans : -1); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nk = readln.split.to!(int[]); writeln(nk[0] - nk[1] + 1); }
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 hs = readln.split.to!(int[]); hs ~= 0; int c; foreach (i; 0..N) c += max(0, hs[i] - hs[i+1]); writeln(c); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { long N = lread(); long pt, px, py; foreach (_; 0 .. N) { long t, x, y; scan(t, x, y); long d = abs(px - x) + abs(py - y); // writeln(d); if (t - pt < d) { writeln("No"); return; } if (((t - pt) - d) % 2 == 1) { writeln("No"); return; } pt = t; px = x; py = y; } writeln("Yes"); }
D
import std.stdio,std.conv,std.string; void main(){ int sum; auto N = readln().chomp().to!int(); auto list = readArray!int(); foreach( elm ; list ){ while( elm%2 == 0 ){ elm /= 2; sum++; } } writeln(sum); } T[] readArray(T)(){ T[] ret; foreach( elm ; readln().split() ){ ret ~= elm.to!T(); } return ret; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!long; long r; foreach (x; 1..N) { if (x^^2 > N) break; if (N%x == 0) { auto m = N/x - 1; if (x*m + x == N && N/m == N%m) r += m; } } writeln(r); }
D
void main() { long n = rdElem; long[] a = rdRow; long q = rdElem; foreach (i; 0 .. q) { long p = rdElem; long r = a.binSrch!"a <= b"(p); if (r != -1 && a[r] == p) 1.writeln; else 0.writeln; } } long binSrch(alias pred = "a < b")(long[] arr, long val) { alias predFun = binaryFun!pred; long l = -1, r = arr.length; if (!predFun(l, r)) swap(l, r); while (abs(r - l) > 1) { long m = (l + r) / 2; if (predFun(arr[m], val)) l = m; else r = m; } return l; } 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; import core.bitop;
D
import std.stdio, std.string, std.conv, std.regex; void main() { auto n = readln.split.to!(int[]); if(n[0] > n[1]){ writeln(n[0] - n[1]); } else { writeln("0"); } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long X = lread(); while (true) { long l = factorize(X).length; if (l == 1) { writeln(X); return; } X++; } } /// 素因数分解 long[] factorize(long x) { assert(0 < x, "x is negative"); long[] result; while ((x & 1) == 0) { x /= 2; result ~= 2; } for (long i = 3; i * i <= x; i += 2) while (x % i == 0) { x /= i; result ~= i; } if (x != 1) result ~= x; return result; }
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 x = RD; auto a = RD; auto b = RD; writeln(abs(x - a) < abs(x - b) ? "A" : "B"); stdout.flush(); debug readln(); }
D
import std; bool calc(int k, int a, int b) { return a <= b / k * k; } void main() { int k; scan(k); int a, b; scan(a, b); writeln(calc(k, a, b) ? "OK" : "NG"); } 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 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; T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } alias sread = () => readln.chomp(); enum MOD = 10 ^^ 9 + 7; void main() { auto S = sread(); solve(S).writeln(); } string solve(string S) { long a = S[0 .. 2].to!long(); long b = S[2 .. $].to!long(); if ((12 < a && b == 0) || (12 < b && a == 0) || (12 < a && 12 < b) || S == "0000") return "NA"; if (a <= 12 && b <= 12 && !(a == 0 || b == 0)) return "AMBIGUOUS"; if (a <= 12 && a != 0) return "MMYY"; return "YYMM"; }
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; void main() { long[] ary = [6000, 4000, 3000, 2000]; foreach(int i; 0..4) { int[] input = readln.split.to!(int[]); writeln(ary[input[0]-1]*input[1]); } }
D
import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; void main(){ string s = readln.chomp; int[] ans = [-1, -1]; // search for "aa" for(int i = 0; i < s.length - 1; i ++){ if(s[i] == s[i + 1]) ans = [i + 1, i + 2]; } // search for "aba" for(int i = 0; i < s.length - 2; i ++){ if(s[i] == s[i + 2]) ans = [i + 1, i + 3]; } ans.map!(to!string).join(" ").writeln; }
D
import std.stdio, std.string, std.algorithm, std.conv, std.range, std.math; void main() { int[int] counter; foreach (string line; lines(stdin)) { int i = line.chomp.to!int; counter[i]++; } int max = counter.values.reduce!(max); foreach (k; counter.keys.sort) { if (counter[k] == max) k.writeln; } }
D
import std.stdio; // readln import std.array; // split import std.conv; // to import std.typecons; bool[][] g; void main(){ string[] s = split(readln()); int N = to!int(s[0]); int M = to!int(s[1]); g = new bool[][](N,N); alias Tuple!(int,int) pair; pair[] p = new pair[](M); for(int i = 0; i < M; i++){ s = split(readln()); int a = to!int(s[0]); int b = to!int(s[1]); a--; b--; p[i] = pair(a,b); g[a][b] = g[b][a] = true; } int cnt = 0; for(int i = 0; i < M; i++){ bool[] used = new bool[](N); foreach (ref elem; used) { elem = false; } g[p[i][0]][p[i][1]] = g[p[i][1]][p[i][0]] = 0; dfs(0,used); g[p[i][0]][p[i][1]] = g[p[i][1]][p[i][0]] = 1; if(!check(used)){ cnt++; } } writeln(cnt); } void dfs(int cur, bool[] used){ used[cur] = true; for(int i = 0; i < g.length; i++){ if(g[cur][i] && !used[i]){ dfs(i, used); } } } bool check(bool[] used){ for(int i = 0; i < used.length; i++){ if(!used[i]) return false; } return true; }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { int N, M; scan(N, M); auto cnt = new int[](M); foreach (i ; 0 .. N) { auto ln = readln.split.to!(int[]); foreach (ai ; ln[1 .. $]) { cnt[ai - 1]++; } } int ans = cnt.count!(ci => ci == N).to!int; writeln(ans); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
void main(){ string n = readln().chomp(); int cnt, ans; foreach(i; 0..n.length){ if(cnt==0)cnt++; else if(n[i-1]==n[i])cnt++; else cnt=1; ans = max(cnt, ans); } ( ans>=3?"Yes":"No" ).writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range; const long mod = 10^^9+7; // 1要素のみの入力 T inelm(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] inln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split())ln ~= elm.to!T(); return ln; }
D
import std.stdio; import std.array; import std.conv; import std.string; import std.algorithm; void main() { int[] m; while((m=map!(to!int)(readln.strip.split).array)!=[0,0]) { int n=m[0],x=m[1],sum = 0; foreach(i;1..n) foreach(j;i+1..x-i) { int k = x-i-j; if(j<k && k<=n) sum ++; } writeln(sum); } }
D
void main() { auto arr = 5.iota.map!(_ => ri).array; int res = int.max; do { int tmp; foreach(v; arr) { if(tmp % 10 != 0) tmp += 10 - (tmp % 10); tmp += v; } res = min(res, tmp); } while(nextPermutation(arr)); res.writeln; } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; foreach (x; N..1000) { if (x/100 == (x/10)%10 && (x/10)%10 == x%10) { writeln(x); return; } } }
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; void main() { string s = readln.chomp; writeln(s[0 .. 4], " ", s[4 .. $]); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.string, std.algorithm, std.range, std.conv, std.typecons, std.math; void main(){ auto S = readln.chomp; ulong min = 0, max = 0; foreach (i,c; S) { if (c == 'A') { min = i; break; } } foreach_reverse (i,c; S) { if (c == 'Z') { max = i; break; } } writeln(max - min + 1); }
D
import std.stdio; import std.conv; import std.array; void main() { int N = readln.split[0].to!int; auto reader = readln.split; int[] a = new int[N]; for(uint i = 0; i < N; i++){ a[i] = reader[i].to!int; } int cnt = 0; for(uint i = 0; i < N; i++){ if (a[i] != i + 1) cnt++; } if (cnt == 0 || cnt == 2){ writeln("YES"); } else { writeln("NO"); } }
D
import std.stdio; void main() { auto s = readln(); if(s[2]==s[3] && s[4]==s[5]) writeln("Yes"); else writeln("No"); }
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() { int N; string S; scan(N); scan(S); auto rh = RollingHash(S); int ans; foreach (i ; 0 .. N) { foreach (j ; i .. N) { while (ans < j - i && j + ans < N && rh[i .. i + ans + 1] == rh[j .. j + ans + 1]) ans++; } } writeln(ans); } struct RollingHash { enum MAX_LENGTH = 500000; enum ulong MASK30 = (1UL << 30) - 1; enum ulong MASK31 = (1UL << 31) - 1; enum ulong MOD = (1UL << 61) - 1; enum ulong POSITIVIZER = MOD * ((1UL << 3) - 1); uint base; ulong[] basePow = new ulong[](MAX_LENGTH + 1); ulong[] hash; this(string s) { auto rnd = Random(unpredictableSeed); base = uniform(129, int.max, rnd).to!uint; basePow[0] = 1; foreach (i ; 1 .. MAX_LENGTH + 1) { basePow[i] = calcMod(mul(basePow[i - 1], base)); } hash = new ulong[](s.length + 1); foreach (i ; 0 .. s.length) { hash[i + 1] = calcMod(mul(hash[i], base) + s[i]); } } // hash(s[begin .. end]) ulong slice(int begin, int end) { return calcMod(hash[end] + POSITIVIZER - mul(hash[begin], basePow[end - begin])); } alias opSlice = slice; ulong mul(ulong l, ulong r) { auto lu = l >> 31; auto ld = l & MASK31; auto ru = r >> 31; auto rd = r & MASK31; auto middleBit = ld * ru + lu * rd; return ((lu * ru) << 1) + ld * rd + ((middleBit & MASK30) << 31) + (middleBit >> 30); } ulong mul(ulong l, uint r) { auto lu = l >> 31; auto rd = r & MASK31; auto middleBit = lu * rd; return (l & MASK31) * rd + ((middleBit & MASK30) << 31) + (middleBit >> 30); } ulong calcMod(ulong val) { val = (val & MOD) + (val >> 61); if (val >= MOD) val -= MOD; return val; } } 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
void main() { long a = rdElem, b = rdElem, c = rdElem; long s = 3 * c; long[][] x = new long[][](3, 3); x[0][0] = a, x[0][1] = b, x[0][2] = s - a - b; x[1][0] = 4 * c - 2 * a - b, x[1][1] = c, x[1][2] = 2 * a + b - 2 * c; x[2][0] = a + b - c, x[2][1] = s - b - c, x[2][2] = s - a - c; x.wrMat; } enum long mod = 10L^^9 + 7; enum long inf = 1L << 60; enum double eps = 1.0e-9; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string; void main() { auto N = readln.chomp.to!int; auto X = new long[](N); auto S = new long[](N); foreach (i; 0..N) { auto s = readln.split.map!(to!long); X[i] = s[0]; S[i] = s[1]; } long[] Y; foreach (i; 0..N) { Y ~= S[i]; if (i < N - 1) Y ~= X[i] - X[i+1]; } long ans = 0; long tmp = 0; for (int i = 0, j = 0; i < N * 2 - 1; ++i) { if (j < i) { j = i; tmp = 0; } while (j < N * 2 - 1 && tmp + Y[j] >= 0) { tmp += Y[j]; ++j; ans = max(ans, tmp); } } ans.writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto S = readln.chomp; char last; int cnt; foreach (c; S) { if (c != last) ++cnt; last = c; } writeln(cnt); }
D
import std.stdio; import std.conv; import std.string; void main() { int times; while((times = readln.chomp.to!(int)) != 0){ char[] str = cast(char[])readln.chomp; for(int i = 0; i < times; i++) { char[] next_str; char prev; uint n = 1; prev = str[0]; foreach(s; str[1..$]) { if (prev == s) ++n; else { next_str ~= text(n, prev); prev = s; n = 1; } } next_str ~= text(n, prev); str = next_str; } writeln(str); } }
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; void main() { auto n = readln.chomp.to!int; foreach (_; 0..n) { auto str = readln.chomp; char[] res; res ~= str[0]; char pre = res[0]; for (int p = 1; p < str.length; p += 3) { if (str[p..p+2] == "->" && res[$-1] == pre) { res ~= str[p+2]; } else if (str[p..p+2] == "<-" && res[0] == pre) { res = str[p+2] ~ res; } pre = str[p+2]; } res.writeln; } }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } alias sread = () => readln.chomp(); void main() { auto nx = aryread(); long[] m = new long[](nx[0]); foreach (i; 0 .. nx[0]) { m[i] = lread(); } long x = nx[1]; x -= m.sum(); long result = m.length; result += x / reduce!min(m); writeln(result); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto t = RD!int; auto ans = new long[][](t, 2); foreach (ti; 0..t) { auto n = RD; auto x = RD; auto y = RD; if (x + y < n + 1) { ans[ti][0] = 1; } else { ans[ti][0] = min((x+y) - (n+1) + 2, n); } ans[ti][1] = min(x+y-1, n); } foreach (e; ans) writeln(e[0], " ", e[1]); stdout.flush; debug readln; }
D
import std.stdio, std.math, std.conv, std.array, std.string, std.algorithm; void main() { string str; while((str = readln()).length != 0) { double v = to!double(str.chomp); double t = (v/9.8)^^2 * 4.9; int nt = to!int(floor(to!real(t))); int h = nt + (5 - nt%5); writeln((h+5)/5); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.typecons; import std.numeric, std.math; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto n = RD; auto a = RDR.ARR; long f(long x) { auto dp = new long[](n+1); dp[1] = x; long cnt; foreach (i; 1..n) { dp[i+1] = dp[i] + a[i]; if (-sgn(dp[i+1]) != sgn(dp[i])) { cnt += abs(dp[i+1]) + 1; dp[i+1] = -sgn(dp[i]); } } //stderr.writeln(dp); //stderr.writeln(cnt); return cnt; } auto x1 = a[0] == 0 ? 1 : a[0]; auto x2 = a[0] == 0 ? -1 : -sgn(a[0]); long ans1 = f(x1) + (a[0] == 0 ? 1 : 0); long ans2 = f(x2) + abs(a[0]) + 1; writeln(min(ans1, ans2)); stdout.flush(); }
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(long a, long b) { if (a == b) return a; if (a % 2 == 0) { if (b % 2 == 0) { if ((b - a) / 2 % 2 == 0) return b; return b ^ 1; } if ((b - a + 1) / 2 % 2 == 0) return 0; return 1; } else { return a ^ calc(a + 1, b); } } void main() { auto ab = reads!long; long a = ab[0], b = ab[1]; writeln(calc(a, b)); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.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!string; foreach (c; n) { ans[ti].chmax(c-'0'); } } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; void main() { int N = readln.chomp.to!int; long[] as = readln.split.to!(long[]); auto b = as.count!"a%4==0"; if (N%2!=0 && b>0) N--; auto c = as.filter!"a%4!=0".count!"a%2==0"; writeln(N <= 2*b + c ? "Yes":"No"); } // ---------------------------------------------- void times(alias fun)(int n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { auto minElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto minimum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b < minimum) { element = a; minimum = b; } } return element; } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto maximum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b > maximum) { element = a; maximum = b; } } return element; } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Clue = Tuple!(long, "x", long, "y", long, "h"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); long cnt; void main() { auto x = lread(); long th = x / 500; long five = (x - 500 * th) / 5; writeln(1000 * th + 5 * five); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
void main() { auto N = ri; auto S = rs; int c, res; foreach(i; S) { if(i == 'I') c++; else c--; res = max(res, c); } res.writeln; } // =================================== import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { int n; scan(n); int ans; foreach (i ; 0 .. n + 1) { if (i*i <= n) { ans = i*i; } else { break; } } writeln(ans); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; immutable long MOD = 10^^9 + 7; void main() { auto s = readln.split; auto N = s[0].to!int; auto L = s[1].to!long; auto A = s[2].to!long; long[] T, X; foreach (i; 0..N) { s = readln.split; T ~= s[0].to!long; X ~= s[1].to!long; } T ~= L; X ~= 0; long t = 0; long ans = 0; foreach (i; 0..N+1) { long interaval = T[i] - t; ans += interaval / A; t = T[i] + X[i]; } ans.writeln; }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; import std.typecons; immutable int dirs = 4; immutable int [dirs] dRow = [ -1, 0, +1, 0]; immutable int [dirs] dCol = [ 0, -1, 0, +1]; immutable char [dirs] dName = ['N', 'W', 'S', 'E']; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto s = readln.strip; alias Coord = Tuple !(int, q{row}, int, q{col}); auto cur = Coord (0, 0); bool [Coord] vis; int res = 0; foreach (char c; s) { auto dir = dName[].countUntil (c); cur.row += dRow[dir]; cur.col += dCol[dir]; res += (cur in vis) ? 1 : 5; vis[cur] = true; cur.row += dRow[dir]; cur.col += dCol[dir]; } writeln (res); } }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); } void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); } string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ int n = scan!int, m = scan!int, k = scan!int; foreach(i; 0 .. k) int sx = scan!int, sy = scan!int; foreach(i; 0 .. k) int fx = scan!int, fy = scan!int; int ans = (n - 1) + (m - 1) + (n * m - 1); ans.writeln; foreach(i; 0 .. n - 1) "U".write; foreach(j; 0 .. m - 1) "L".write; string x = "R"; foreach(i; 0 .. n){ foreach(j; 0 .. m - 1) x.write; if(i < n - 1) "D".write; if(x == "R") x = "L"; else x = "R"; } "".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 = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD!int; auto s = RD!string; long[2] ans; foreach (i; 1..N) { if (s[i-1] > s[i]) { ans = [i-1, i]; break; } } if (ans == [0, 0]) writeln("NO"); else { writeln("YES"); writeln(ans[0]+1, " ", ans[1]+1); } stdout.flush(); debug readln(); }
D
import std.stdio; import std.ascii; import std.algorithm; import std.array; import core.stdc.stdio; int main() { int t = readInt!int; while(t--) { string astr = readString; auto a = new int[](astr.length); auto count = new int[](4); foreach(i, ref ai; a) { switch(astr[i]) { case 'A': ai = 0; break; case 'N': ai = 1; break; case 'T': ai = 2; break; case 'O': ai = 3; break; default: assert(0); } count[ai]++; } long minops = long.min; int[] minA; foreach(test; [[0, 1, 2, 3],[0, 1, 3, 2], [0, 2, 1, 3], [0, 2, 3, 1], [0, 3, 1, 2], [0, 3, 2, 1], [1, 0, 2, 3], [1, 0, 3, 2], [1, 2, 0, 3], [1, 2, 3, 0], [1, 3, 0, 2], [1, 3, 2, 0], [2, 0, 1, 3], [2, 0, 3, 1], [2, 1, 0, 3], [2, 1, 3, 0], [2, 3, 0, 1], [2, 3, 1, 0], [3, 0, 1, 2], [3, 0, 2, 1], [3, 1, 0, 2], [3, 1, 2, 0], [3, 2, 0, 1], [3, 2, 1, 0]]) { int[] testOrig; foreach(ti; test) { foreach(i; 0 .. count[ti]) { testOrig ~= ti; } } long value = fixString(testOrig, a); if (value > minops) { minops = value; minA = testOrig; } } string mapi = "ANTO"; string ans; foreach(ai; minA) { ans ~= mapi[ai]; } writeln(ans); } return 0; } long fixString(int[] orig, int[] dest) { int[][] pos = new int[][](4); auto ft = FT(cast(int)orig.length); foreach(i, a; dest) { pos[a] ~= cast(int) i; } long res = 0; foreach(i, a; orig) { int posCh = pos[a][0]; pos[a] = pos[a][1 .. $]; ft.add(posCh, 1); res += posCh - ft.sum(0, posCh - 1); } return res; } struct FT { int[] bit; int n; this(int n) { this.n = n; bit = new int[](n); } int sum(int r) { int ret = 0; for (; r >= 0; r = (r & (r + 1)) - 1) ret += bit[r]; return ret; } int sum(int l, int r) { return sum(r) - sum(l - 1); } void add(int idx, int delta) { for (; idx < n; idx = idx | (idx + 1)) bit[idx] += delta; } }; /* INPUT ROUTINES */ int currChar; static this() { currChar = getchar(); } char topChar() { return cast(char) currChar; } void popChar() { currChar = getchar(); } auto readInt(T)() { T num = 0; int sgn = 0; while (topChar.isWhite) popChar; while (topChar == '-' || topChar == '+') { sgn ^= (topChar == '-'); popChar; } while (topChar.isDigit) { num *= 10; num += (topChar - '0'); popChar; } if (sgn) return -num; return num; } string readString() { string res = ""; while (topChar.isWhite) popChar; while (!topChar.isWhite) { res ~= topChar; popChar; } return res; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new int[][][](t); foreach (ti; 0..t) { auto n = RD!int; auto s = new string[](n); foreach (i; 0..n) s[i] = RD!string; if (s[0][1] == s[1][0]) { if (s[n-1][n-2] == s[0][1]) ans[ti] ~= [n, n-1]; if (s[n-2][n-1] == s[0][1]) ans[ti] ~= [n-1, n]; } else if (s[n-1][n-2] == s[n-2][n-1]) { if (s[0][1] == s[n-1][n-2]) ans[ti] ~= [1, 2]; if (s[1][0] == s[n-1][n-2]) ans[ti] ~= [2, 1]; } else { if (s[0][1] == '1') ans[ti] ~= [1, 2]; if (s[1][0] == '1') ans[ti] ~= [2, 1]; if (s[n-1][n-2] == '0') ans[ti] ~= [n, n-1]; if (s[n-2][n-1] == '0') ans[ti] ~= [n-1, n]; } } foreach (e; ans) { writeln(e.length); foreach (ee; e) writeln(ee[0], " ", ee[1]); } stdout.flush; debug readln; }
D
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } void main() { try { for (; ; ) { const numCases = readInt(); foreach (caseId; 0 .. numCases) { const N = readInt(); auto A = new long[N]; foreach (i; 0 .. N) { A[i] = readLong(); } long ans; foreach (i; 0 .. N - 1) { ans += max(A[i] - A[i + 1], 0); } writeln(ans); } } } catch (EOFException e) { } }
D
module main; import std.stdio : writeln, stdin; import core.stdc.stdio; int main(string[] argv) { int n, l, r; scanf("%d %d %d", &n, &l, &r); l = l - 1; r = r - 1; int [] arr = new int[n]; int [] b = new int[n]; for(int i = 0; i<n; i++) scanf("%d", &arr[i]); for (int i = 0; i < n; i++) scanf("%d", &b[i]); for (int i = 0; i < l; i++) { if (arr[i] != b[i]) { writeln("LIE"); return 0; } } for (int i = r + 1; i < n; i++) { if (arr[i] != b[i]) { writeln("LIE"); return 0; } } writeln("TRUTH"); return 0; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto t = RD!int; auto ans = new long[](t); ans[] = long.max; foreach (i; 0..t) { auto n = RD!int; auto k = RD!int; auto d = RD!int; auto a = RDA!int; int[int] cnt; foreach (j; 0..d) { ++cnt[a[j]]; } ans[i] = min(ans[i], cnt.keys.length); foreach (j; d..n) { ++cnt[a[j]]; if (cnt[a[j-d]] == 1) { cnt.remove(a[j-d]); } else { --cnt[a[j-d]]; } ans[i] = min(ans[i], cnt.keys.length); debug writeln(ans[i]); } } foreach (e; ans) writeln(e); stdout.flush(); debug readln(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string; void main() { auto ret = ""; auto h = readln.split[0].to!int; foreach (_; 0..h) { auto line = readln; ret ~= line ~ line; } write(ret); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int r; scan(r); auto ans = r*r; 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 core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); auto cnt = new long[][](2, 30); cnt[1][] = long.max; foreach (i; 0 .. N) { auto S = sread(); cnt[i & 1][] = 0; foreach (c; S) cnt[i & 1][c - 'a']++; // writeln(cnt[i & 1]); foreach (j; 0 .. 30) cnt[i & 1][j] = cnt[(i + 1) & 1][j].min(cnt[i & 1][j]); } // writeln(cnt[(N + 1) & 1]); string ans; foreach (i; 0 .. 30) { ans ~= repeat(cast(char)('a' + i)).take(cnt[(N + 1) & 1][i]).array; } writeln(ans); }
D
void main() { problem(); } int mod(string x, int d) { int r; auto xs = x.length; foreach(i; 0..xs) { r = (r * 2 + (x[i] - '0')) % d; } return r; } int mod(BigInt b, int popcount) { BigInt q, r; b.divMod(BigInt(popcount), q, r); return r.toInt; } void problem() { int calc(BigInt x, int popcount) { if (x == 0) return 0; int m = x.mod(popcount); int ans = 1; while(m > 0) { ans++; m %= m.popcnt; } return ans; } auto N = scan!int; auto X = scan; void solve() { const basePopCount = cast(int)X.count('1'); const baseFirstModPlus = X.mod(basePopCount + 1); const baseFirstModMinus = basePopCount == 1 ? 0 : X.mod(basePopCount - 1); foreach(i; 0..N) { const deltaPlus = X[i] == '0'; const popCount = basePopCount + (deltaPlus ? 1 : -1); if (popCount == 0) { writeln(0); continue; } int firstMod; auto deltaMod = powmod(2U, (N - i - 1).unsigned, popCount.unsigned); if (deltaPlus) { firstMod = (baseFirstModPlus + deltaMod) % popCount; } else { firstMod = baseFirstModMinus - deltaMod; firstMod = firstMod < 0 ? popCount + firstMod : firstMod; } int ans = 1; while(firstMod > 0) { ans++; firstMod %= firstMod.popcnt; } ans.writeln; } } solve(); } // ---------------------------------------------- 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 Queue = Tuple!(long, "from", long, "to"); import std.bigint, std.functional, core.bitop; // -----------------------------------------------
D
import std.stdio; import std.string; import std.array; import std.conv; void main() { int A=to!int(chomp(readln())); int B=to!int(chomp(readln())); int C=to!int(chomp(readln())); int X=to!int(chomp(readln())); int ans=0; for(int i=0;i<=A;i++) { for(int j=0;j<=B;j++) { for(int k=0;k<=C;k++){ if(500*i+100*j+50*k==X) ans++; } } } writeln(ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nm = readln.split.to!(long[]); auto N = nm[0]; auto M = nm[1]; long al; long[long] AM = [0: 0]; foreach (i, a; readln.split.to!(long[])) { al = (al + a) % M; ++AM[al]; } long r = AM[0]; foreach (_, n; AM) r += n * (n-1) / 2; writeln(r); }
D
import std.stdio, std.algorithm, std.string; void main(){ readln.split.reduce!q{ a < b ?"<" : a==b ? "=" : ">" }.writeln; }
D
import std.algorithm; import std.math; import std.range; import std.ascii; import std.array; import std.container; import std.conv; import std.numeric; import std.stdio; import std.string; import std.typecons; void log(A...)(A arg) { stderr.writeln(arg); } class UnreliableRollingHash(ulong P) { // fast but unreliable rolling hash // base is P, modulus is 2^64 ulong[] H; this(in string s) { auto N = s.length; this.H = new ulong[](N + 1); foreach (int i, c; s) { H[i + 1] = (H[i] * P + cast(int)(c)); } } ulong hash(int l, int r) { // [l, r) return H[r] - H[l] * pow(P, r - l); } } void main() { int N, M; scanf("%d %d\n", &N, &M); auto s = readln.chomp; auto hasher = new UnreliableRollingHash!991(s); int l = 0, r = 1; bool[ulong] appeared; foreach (_; 0 .. M) { auto q = readln.chomp; switch (q) { case "L++": l++; break; case "L--": l--; break; case "R++": r++; break; case "R--": r--; break; default: assert(false); } appeared[hasher.hash(l, r)] = true; } writeln(appeared.keys.length); }
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 long MOD = 10^^9 + 7; void solve() { auto N = readln.chomp.to!int; auto S = readln.chomp; int ans = 1 << 29; foreach (i; 0..N) { if (S[i] == '>' || S[N - i - 1] == '<') { ans = i; break; } } ans.writeln; } void main() { auto T = readln.chomp.to!int; while (T--) solve; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(int n) { auto dp = new int[n + 1]; for (int i = 1; i <= n; i++) dp[i] = i; foreach (coin; [6, 9]) { int c = 1; while (true) { c *= coin; if (c > n) break; for (int i = c; i <= n; i++) { dp[i] = min(dp[i], dp[i - c] + 1); } } } return dp[n]; } void main() { int n = readint; int ans = calc(n); writeln(ans); }
D
/+ dub.sdl: name "B" dependency "dcomp" version=">=0.7.3" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; int main() { Scanner sc = new Scanner(stdin); int n; sc.read(n); int[] a; sc.read(a); a[]-=1; bool[] used = new bool[n]; int r = n-1; writeln(1); foreach (int i, d; a) { used[d] = true; while (r >= 0 && used[r]) r--; int s = 1 + (i+1) - (n-1-r); writeln(s); } return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } version (X86) static if (__VERSION__ < 2071) { import core.bitop : bsf, bsr, popcnt; int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; // import dcomp.array; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { FastAppender!(E[]) buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/array.d */ // module dcomp.array; T[N] fixed(T, size_t N)(T[N] a) {return a;} struct FastAppender(A, size_t MIN = 4) { import std.algorithm : max; import std.conv; import std.range.primitives : ElementEncodingType; import core.stdc.string : memcpy; private alias T = ElementEncodingType!A; private T* _data; private uint len, cap; @property size_t length() const {return len;} bool empty() const { return len == 0; } void reserve(size_t nlen) { import core.memory : GC; if (nlen <= cap) return; void* nx = GC.malloc(nlen * T.sizeof); cap = nlen.to!uint; if (len) memcpy(nx, _data, len * T.sizeof); _data = cast(T*)(nx); } void free() { import core.memory : GC; GC.free(_data); } void opOpAssign(string op : "~")(T item) { if (len == cap) { reserve(max(MIN, cap*2)); } _data[len++] = item; } void insertBack(T item) { this ~= item; } void removeBack() { len--; } void clear() { len = 0; } ref inout(T) back() inout { assert(len); return _data[len-1]; } ref inout(T) opIndex(size_t i) inout { return _data[i]; } T[] data() { return (_data) ? _data[0..len] : null; } } /* This source code generated by dcomp and include dcomp's source code. dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp) dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt) */
D
import std.algorithm; import std.array; import std.ascii; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; import core.bitop; void log(A...)(A arg) { stderr.writeln(arg); } int size(T)(in T s) { return cast(int)s.length; } class Tree { int N; // Graph(Tree)???????????° int[][] G; // Graph(Tree)?????£??\???????????¨??? int[] D; // ????????????root??????????????¢ int LOG_N; // log2(N)????????? int[][] P; // P[k][v] : ??????v???2^k?????????????\? void dfs(int prev, int index, int d) { P[0][index] = prev; D[index] = d; for (int i = 0; i < G[index].size; i++) { int next = G[index][i]; if (next == prev) continue; dfs(index, next, d + 1); } } this(int[][] G) { this.N = G.size; this.G = G; this.LOG_N = bsr(N) + 1; this.P = new int[][](LOG_N, N); this.D = new int[N]; D[] = -1; dfs(-1, 0, 0); for (int k = 0; k < LOG_N - 1; k++) { for (int v = 0; v < N; v++) { if (P[k][v] < 0) P[k + 1][v] = -1; else P[k + 1][v] = P[k][ P[k][v] ]; } } } // ??????v???n????????????????????? int nth_parent(int v, int n) { for (int k = 0; k < LOG_N; k++) if (n & (1 << k)) v = P[k][v]; return v; } // 2??????u??¨v???LCA????????? int lca(int u, int v) { if (D[u] > D[v]) swap(u, v); v = nth_parent(v, D[v] - D[u]); if (u == v) return u; for (int k = LOG_N - 1; k >= 0; k--) { if (P[k][u] != P[k][v]) { u = P[k][u]; v = P[k][v]; } } return P[0][u]; } // 2??????u??¨v????????¢????????? int dist(int u, int v) { int root = lca(u, v); return (D[u] - D[root]) + (D[v] - D[root]); } } void main() { int N = readln.chomp.to!int; auto G = new int[][N]; for (int i = 0; i < N; i++) { int k; scanf("%d ", &k); for (int j = 0; j < k; j++) { int c; scanf("%d", &c); G[i] ~= c; } } auto t = new Tree(G); int Q; scanf("%d", &Q); for (int q = 0; q < Q; q++) { int u, v; scanf("%d %d", &u, &v); writeln(t.lca(u, v)); } }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto a = readln.split.to!(int[]); if (a[0] != a[$ - 1]) { writeln(n - 1); return; } int mx = 1; foreach (i; 1 .. n) { if (a[0] != a[i]) { mx = max(mx, i, n - i - 1); } } writeln(mx); } void rd(T...)(ref T x) { import std.stdio : readln; import std.string : split; import std.conv : to; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int l; readV(l); auto r = l.bsr, n = r+1; struct Edge { int u, v, w; } Edge[] e; foreach (i; 1..n) { e ~= Edge(i, i+1, 0); e ~= Edge(i, i+1, 1<<(i-1)); } l -= (1<<r); auto t = (1<<r); while (l > 0) { r = l.bsr; e ~= Edge(r+1, n, t); l -= (1<<r); t += (1<<r); } writeln(n, " ", e.length); foreach (ei; e) writeln(ei.u, " ", ei.v, " ", ei.w); } pragma(inline) { pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; } pure T bitSet(T)(T n, size_t i) { return n | (T(1) << i); } pure T bitReset(T)(T n, size_t i) { return n & ~(T(1) << i); } pure T bitComp(T)(T n, size_t i) { return n ^ (T(1) << i); } pure T bitSet(T)(T n, size_t s, size_t e) { return n | ((T(1) << e) - 1) & ~((T(1) << s) - 1); } pure T bitReset(T)(T n, size_t s, size_t e) { return n & (~((T(1) << e) - 1) | ((T(1) << s) - 1)); } pure T bitComp(T)(T n, size_t s, size_t e) { return n ^ ((T(1) << e) - 1) & ~((T(1) << s) - 1); } import core.bitop; pure int bsf(T)(T n) { return core.bitop.bsf(ulong(n)); } pure int bsr(T)(T n) { return core.bitop.bsr(ulong(n)); } pure int popcnt(T)(T n) { return core.bitop.popcnt(ulong(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; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { int n, a, b; scan(n, a, b); int ans = min(a * n, b); writeln(ans); }
D
void main() { long[] tmp = rdRow; long h = tmp[0], w = tmp[1]; long choco = h * w; long result = 1L << 60; foreach (i; 1 .. h) { long s1 = i * w; long s2 = (h - i) * (w / 2); long s3 = choco - s1 - s2; long diff = max(s1, s2, s3) - min(s1, s2, s3); result = min(result, diff); s2 = ((h - i) / 2) * w; s3 = choco - s1 - s2; diff = max(s1, s2, s3) - min(s1, s2, s3); result = min(result, diff); } foreach (j; 1 .. w) { long s1 = h * j; long s2 = (h / 2) * (w - j); long s3 = choco - s1 - s2; long diff = max(s1, s2, s3) - min(s1, s2, s3); result = min(result, diff); s2 = h * ((w - j) / 2); s3 = choco - s1 - s2; diff = max(s1, s2, s3) - min(s1, s2, s3); result = min(result, diff); } result.writeln; } T rdElem(T = long)() { //import std.stdio : readln; //import std.string : chomp; //import std.conv : to; return readln.chomp.to!T; } alias rdStr = rdElem!string; dchar[] rdDchar() { //import std.conv : to; return rdStr.to!(dchar[]); } T[] rdRow(T = long)() { //import std.stdio : readln; //import std.array : split; //import std.conv : to; return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdRow!T).array; } void wrMat(T = long)(T[][] mat) { //import std.stdio : write, writeln; foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } void main(){ long n = read.to!long; char[][] ss; for(int i = 0; i < n; i ++) ss ~= readln.chomp.to!(char[]); int[char] count; foreach(c; "abcdefghijklmnopqrstuvwxyz") count[c] = int.max; foreach(s; ss){ int[char] tempcount; foreach(c; s){ tempcount[c] += 1; } foreach(c; count.keys){ if(c in tempcount){ if(tempcount[c] < count[c]) count[c] = tempcount[c]; } else count[c] = 0; } } string ans; foreach(c; "abcdefghijklmnopqrstuvwxyz") for(int i = 0; i < count[c]; i ++) ans ~= c; ans.writeln; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); ulong bignum = 1_000_000_007; alias Pair = Tuple!(long, "number", long, "times"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { auto s = sread(); long[dchar] count; foreach (e; s) { count[e]++; } long remove; if ('0' in count && '1' in count) remove = min(count['0'], count['1']); writeln(remove * 2); }
D
import std.stdio,std.string,std.conv; int main() { string[] s = readln.chomp.split(" "); string len_max = s[0],ans = s[0]; int[string] map; int count_max = 1; foreach(i;0..(s.length)) { if(len_max.length < s[i].length) { len_max = s[i]; } map[s[i]]++; if(map[s[i]] > count_max) { count_max = map[s[i]]; ans = s[i]; } } writeln(ans," ",len_max); return 0; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; long[] perm_prod(const long[] A, const long[] P) { auto ret = A.dup; foreach (i; 0..A.length) { ret[i] = A[P[i].to!int]; } return ret; } long[] perm_pow(const long[] A, long x) { long[] P = A.dup; long[] ret = new long[](P.length); foreach (i; 0..P.length) ret[i] = i; while (x) { if (x % 2) ret = perm_prod(ret, P); P = perm_prod(P, P); x /= 2; } return ret; } void main() { auto N = readln.chomp.to!int; auto X = readln.split.map!(to!long).array; auto s = readln.split.map!(to!long); auto M = s[0].to!int; auto K = s[1]; auto A = readln.split.map!(x => x.to!int - 1).array; auto D = new long[](N - 1); foreach (i; 0..N-1) D[i] = X[i + 1] - X[i]; auto B = new long[](N - 1); foreach (i; 0..N-1) B[i] = i; foreach (i; 0..M) swap(B[A[i] - 1], B[A[i]]); B = perm_pow(B, K); D = perm_prod(D, B); auto ans = new long[](N); ans[0] = X[0]; foreach (i; 0..N-1) ans[i + 1] = ans[i] + D[i]; ans.each!writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto xyz = readln.split.to!(int[]); writeln(xyz[2], " ", xyz[0], " ", xyz[1]); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto M = RD; auto D = RD; long ans; foreach (i; 0..M) { foreach (j; 0..D) { auto d1 = (j+1) % 10; auto d10 = (j+1) / 10; if (d1 >= 2 && d10 >= 2 && d1*d10 == i+1) { ++ans; } } } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, 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; alias Pair = Tuple!(int, "a", int, "b"); void main() { int N, M, L; scan(N, M, L); auto dist = new long[][](N, N); foreach (i ; 0 .. N) { foreach (j ; 0 .. N) { if (i != j) dist[i][j] = inf6; } } foreach (i ; 0 .. M) { int ai, bi, ci; scan(ai, bi, ci); ai--, bi--; dist[ai][bi] = dist[bi][ai] = ci; } foreach (k ; 0 .. N) { foreach (i ; 0 .. N) { foreach (j ; 0 .. N) { chmin(dist[i][j], dist[i][k] + dist[k][j]); } } } auto d = new int[][](N, N); foreach (i ; 0 .. N) { foreach (j ; 0 .. N) { if (i == j) { d[i][j] = 0; continue; } d[i][j] = dist[i][j] <= L ? 1 : inf; } } foreach (k ; 0 .. N) { foreach (i ; 0 .. N) { foreach (j ; 0 .. N) { chmin(d[i][j], d[i][k] + d[k][j]); } } } int Q; scan(Q); while (Q--) { int si, ti; scan(si, ti); si--, ti--; writeln(d[si][ti] < inf ? d[si][ti] - 1 : -1); } } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto d=new int[](n); foreach(i; 0..n) rd(d[i]); int r=0; foreach(int i, int e; d){ if(r<i*10){writeln("no"); return;} if(r<i*10+e) r=i*10+e; } reverse(d); r=0; foreach(int i, int e; d){ if(r<i*10){writeln("no"); return;} if(r<i*10+e) r=i*10+e; } writeln("yes"); } void chmax(T)(ref T x, T y){ if(x<y) x=y; } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string; enum AEIOU = "aeiou"; void main() { auto c = readln[0]; writeln(AEIOU.canFind(c) ? "vowel" : "consonant"); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n, q; rd(n, q); while(q--){ int v, w; rd(v, w); v--; w--; if(n==1){writeln(v+1); continue;} auto fun(int i){ int ret=0; while(i>0) i=(i-1)/n, ret++; return ret; } auto dv=fun(v), dw=fun(w); foreach(_; 0..(dw-dv)) w=(w-1)/n; if(v==w){writeln(v+1); continue;} while(v!=w){ v=(v-1)/n; w=(w-1)/n; } writeln(v+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
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);} void readM(T)(size_t r,size_t c,ref T[][]t){t=new T[][](r);foreach(ref v;t)readA(c,v);} void main() { int h, w; readV(h, w); int[][] c; readM(10, 10, c); int[][] a; readM(h, w, a); auto g = GraphM!()(10).init; foreach (i; 0..10) foreach (j; 0..10) g[i][j] = c[i][j]; auto d = g.floydWarshal; auto r = 0; foreach (i; 0..h) foreach (j; 0..w) if (a[i][j] != -1) r += d[a[i][j]][1]; writeln(r); } struct GraphM(W = int, W i = 10^^9) { alias Wt = W, inf = i; int n; Wt[][] g; alias g this; this(int n) { this.n = n; g = new Wt[][](n, n); } ref auto init() { foreach (i; 0..n) { g[i][] = inf; g[i][i] = 0; } return this; } } template FloydWarshal(Graph) { import std.algorithm, std.array, std.traits; alias Wt = TemplateArgsOf!Graph[0]; Wt[][] floydWarshal(ref Graph g) { Wt[][] dist; int[][] inter; floydWarshal(g, dist, inter); return dist; } void floydWarshal(ref Graph g, out Wt[][] dist, out int[][] inter) { auto n = g.n, sent = n; dist = g.g.map!(i => i.dup).array; inter = new int[][](n, n); foreach (i; 0..n) inter[i][] = sent; foreach (k; 0..n) foreach (i; 0..n) foreach (j; 0..n) if (dist[i][j] > dist[i][k] + dist[k][j]) { dist[i][j] = dist[i][k] + dist[k][j]; inter[i][j] = k; } } } auto floydWarshal(G)(G g) { return FloydWarshal!(typeof(g)).floydWarshal(g); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto N = RD; writeln((N+1)/2); stdout.flush; debug readln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto nm = readln.split.to!(int[]); writeln( (100 * (nm[0] - nm[1]) + 1900 * nm[1]) * (2^^nm[1]) ); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { auto nk = readints; int n = nk[0], k = nk[1]; writeln(n % k == 0 ? 0 : 1); }
D
import std.stdio, std.math, std.algorithm, std.array, std.string, std.conv, std.container, std.range; T[] Reads(T)() { return readln.split.to!(T[]); } alias reads = Reads!(int); void scan(Args...)(ref Args args) { import std.conv : parse; string[] ss = readln.split; int i; foreach (ref arg ; args) arg = ss[i++].parse!int; } bool f(string s) { int[] cnt = new int[26]; foreach(c;s) cnt[c - 'A']++; return cnt.count(2) == 2; } void main() { string s = readln.chomp; writeln(f(s) ? "Yes" : "No"); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n; scan(n); auto a = readln.split.to!(int[]); int[int] c; foreach (ai ; a) { int b = ai; for (int j = 2; j*j <= ai; j++) if (b % j == 0) { int t; while (b % j == 0) { t++; b /= j; } if (j in c) c[j] = max(c[j], t); else c[j] = t; } if (b > 1) { if (b in c) c[b] = max(c[b], 1); else c[b] = 1; } } debug { writeln(c); } long M = 1; foreach (k, v ; c) { (M *= k^^v) %= mod; } long ans; foreach (ai ; a) { auto b = M * powmod(ai, mod - 2) % mod; (ans += b) %= mod; } writeln(ans); } long powmod(int x, int y) { return y > 0 ? powmod(x, y>>1)^^2 % mod * x^^(y&1) % mod : 1; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } 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; void main(){ int a; scanf("%d", &a); if(a>=30){ writeln("Yes"); }else{ writeln("No"); } }
D
void main() { problem(); } void problem() { const K = scan!long; long solve() { long ans; foreach(a; 1..K+1) { foreach(b; 1..K+1) { foreach(c; 1..K+1) { ans += a.gcd(b).gcd(c); } } } return ans; } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, 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); } // -----------------------------------------------
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; bool ans; foreach (i; 0..100) { auto remain = N - 4 * i; if (remain < 0) continue; if (remain % 7 == 0) ans = true; } writeln(ans ? "Yes" : "No"); stdout.flush(); debug readln(); }
D
import std.stdio, std.string, std.conv; import std.array, std.algorithm, std.range; void main() { bool[4][5][5] m; foreach(i;0..9) foreach(j,b;readln().chomp().map!"a!='0'"().array()) if(b) foreach(k;0..2) m[i/2+k*(i%2)][j+k*(1-i%2)][k*2+i%2]=true; string r; int x=0,y=0,d=0; do{ foreach(i;-1..3) { auto n = (d+i+4)%4; if(m[y][x][n]) { x += [1,0,-1,0][n]; y += [0,1,0,-1][n]; d=n; r~="RDLU"[n]; break; } } }while(x!=0 || y!=0); writeln(r); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // 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); auto A = new string[](H); foreach (i; 0 .. H) A[i] = sread(); string[] B; foreach (i; 0 .. H) if (A[i].any!"a == '#'") { B ~= A[i]; } auto C = new string[](B.length); foreach (i; 0 .. W) { bool b; foreach (j; 0 .. B.length) if (B[j][i] == '#') { b = true; break; } if (b) foreach (j; 0 .. B.length) { C[j] ~= B[j][i]; } } foreach (c; C) writeln(c); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(string s) { int n = cast(int)s.length; auto es = new int[n]; auto ws = new int[n]; int e = 0; int w = 0; for (int i = 0; i < n; i++) { switch (s[i]) { case 'E': e++; break; case 'W': w++; break; default: break; } es[i] = e; ws[i] = w; } int ans = int.max; for (int i = 0; i < n; i++) { // i がリーダー // リーダーの左側で W を向いている人数 int left = i > 0 ? ws[i - 1] : 0; // リーダーの右側で E を向いている人数 int right = i == n-1 ? 0 : es[n - 1] - es[i]; ans = min(ans, left + right); } return ans; } void main() { readint; auto s = readln.chomp; auto ans = calc(s); writeln(ans); }
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; } // fold was added in D 2.071.0. template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } void main() { readln.chomp.pipe!(str => (str.length%2==0) != (str.front==str.back) ).pipe!(a => a?"Second":"First").writeln; }
D