code
stringlengths
4
1.01M
language
stringclasses
2 values
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; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; void main() { int[] xs = readln.split.map!(to!char).map!(to!int).array; string f(string op)() { mixin("return xs.front" ~ (op=="="?"==":op) ~ "xs.back ? \"" ~ op ~ "\" : \"\";"); } writeln(f!"<"~f!">"~f!"="); } // ---------------------------------------------- void scanln(Args...)(ref Args args) { foreach(i, ref v; args) { "%d".readf(&v); (i==args.length-1 ? "\n" : " ").readf; } // ("%d".repeat(args.length).join(" ") ~ "\n").readf(args); } void times(alias fun)(int n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { auto minElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto minimum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b < minimum) { element = a; minimum = b; } } return element; } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto maximum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b > maximum) { element = a; maximum = b; } } return element; } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; import std.range; import std.regex; import std.math; void main(){ auto a=readln.chomp; writeln(a.count('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 XS = readln.split.to!(long[]); long r = long.max; foreach (long p; 0..101) { long rr; foreach (x; XS) rr += (p - x)^^2; r = min(r, rr); } writeln(r); }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { const N = readln.chomp.to!long; const as = readln.split.to!(long[]); writeln(as.reduce!max - as.reduce!min); }
D
import std.stdio, std.conv, std.string; void main() { while ( true ) { immutable int n = to!int( readln().strip() ); if ( n == 0 ) break; int count = 0; bool right = false; bool left = false; bool isUp = false; foreach ( s; split( readln() ) ) { switch ( s ) { case "lu": left = true; break; case "ld": left = false; break; case "ru": right = true; break; case "rd": right = false; break; default : } if ( isUp && !right && !left ) { isUp = false; count++; } else if ( !isUp && right && left ) { isUp = true; count++; } } writeln( count ); } }
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.map!(to!int); auto N = s[0]; auto M = s[1]; auto G = new int[][](N); auto D = new int[](N); foreach (i; 0..M) { s = readln.split.map!(to!int); G[s[0]-1] ~= s[1]-1; D[s[1]-1] += 1; } DList!int q; foreach (i; 0..N) if (D[i] == 0) q.insertBack(i); auto dp = new long[](N+1); while (!q.empty) { auto n = q.front; q.removeFront; foreach (m; G[n]) { dp[m] = max(dp[m], dp[n] + 1); D[m] -= 1; if (D[m] == 0) q.insertBack(m); } } dp.reduce!max.writeln; }
D
import std.stdio, std.string, std.algorithm, std.conv, std.array, std.math; void main(){ auto hw=readln.split.to!(long[]); auto h=hw[0], w=hw[1]; if(h % 3 == 0 || w % 3 == 0){ writeln(0); return; } writeln(min(VtHr(h, w), HrVt(h, w), h, w)); } long VtHr(long h, long w){ return HrVt(w, h); } auto HrVt(long h, long w){ return min(TCut(h, w, h/3), TCut(h, w, h/3+1)); } long TCut(long h, long w, long cutAt) { auto top=cutAt*w; if(w%2==0) { return abs(top-(w/2)*(h-cutAt)); }else{ return max(abs(top-(w/2)*(h-cutAt)), abs(top-(w/2+1)*(h-cutAt))); } }
D
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; int n, m; int[][] a; void main() { scan(n, m); int lim = 2 * max(n, m); a = new int[][](lim + 1, lim + 1); iota(1, n + 1).each!(i => a[i][1 .. m + 1] = readln.chomp.map!(b => (b - '0').to!int).array); debug { writefln("%(%(%s %)\n%)", a); writeln(); } foreach (i ; 1 .. lim + 1) { foreach (j ; 1 .. lim + 1) { a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1]; } } debug { writefln("%(%(%s %)\n%)", a); writeln(); } long ans = 4L * n * m; foreach (k ; 2 .. max(n, m) + 1) { long anst = 0; foreach (i ; 1 .. (n + k - 1) / k + 1) { foreach (j ; 1 .. (m + k - 1) / k + 1) { long x = a[i*k][j*k] - a[(i-1)*k][j*k] - a[i*k][(j-1)*k] + a[(i-1)*k][(j-1)*k]; anst += min(x, k*k - x); } } ans = min(ans, anst); debug { writefln("k:%d, anst:%d", k, anst); } } 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
void main() { auto S = rs.map!(i => i == '0').array; auto k = S.length; ulong cnt; while(true) { bool flag = false; if(S.length <= 1) break; foreach(i; 0..S.length-1) { if(S[i] != S[i+1]) { cnt++; flag = true; S = S[0..i] ~ S[i+2..$]; break; } } if(!flag) break; } writeln(cnt*2); } // =================================== 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
// Vicfred // https://atcoder.jp/contests/abc052/tasks/arc067_a // math import std.algorithm; import std.array; import std.conv; import std.functional; import std.math; import std.range; import std.stdio; import std.string; uint[] sieve(in uint limit) nothrow @safe { if (limit < 2) return []; auto composite = new bool[limit]; foreach (immutable uint n; 2 .. cast(uint)(limit ^^ 0.5) + 1) if (!composite[n]) for (uint k = n * n; k < limit; k += n) composite[k] = true; return iota(2, limit).filter!(i => !composite[i]).array; } void main() { uint[] primes = sieve(1000); long n = readln.chomp.to!long; long[long] exponents; foreach(prime; primes) exponents[prime] = 0; for(long i = 2; i <= n; ++i) { long m = i; foreach(prime; primes) { long div = 0; while(m%prime == 0) { div += 1; m /= prime; } exponents[prime] += div; } } long ans = 1; foreach(exp; exponents.values) { ans *= (exp+1); ans %= 10^^9+7; } ans.writeln; }
D
import std.stdio, std.algorithm, std.string; void main() { int[string] count; int maxCount; string maxWord; auto input = readln.split; foreach(str; input) { ++count[str]; if(maxCount < count[str]) { maxCount = count[str]; maxWord = str; } } size_t maxLen = 0; string maxLenWord; foreach(str; input) { if(maxLen < str.length) { maxLen = str.length; maxLenWord = str; } } writeln(maxWord, " ", maxLenWord); }
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 T = RD; auto t = RDR.ARR; long loss; foreach (i; 1..N) { loss += max(0, T - (t[i] - t[i-1])); } writeln(T * N - loss); stdout.flush(); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n; readV(n); auto g = GraphW!int(n); alias Edge = g.Edge; foreach (_; 0..n-1) { int u, v, w; readV(u, v, w); --u; --v; g.addEdgeB(u, v, w); } auto d = new long[](n); auto bfs = bfsw(g); bfs.findEdge ~= (Edge e) { d[e.dst] = d[e.src] + e.wt; }; bfs.run(0); foreach (di; d) writeln(di%2 ? 1 : 0); } struct GraphW(W = int, W i = 10^^9) { alias Node = int, Wt = W, inf = i; struct Edge { Node src, dst; Wt wt; alias cap = wt; } Node n; Edge[][] g; alias g this; this(Node n) { this.n = n; g = new Edge[][](n); } void addEdge(Node u, Node v, Wt w) { g[u] ~= Edge(u, v, w); } void addEdgeB(Node u, Node v, Wt w) { g[u] ~= Edge(u, v, w); g[v] ~= Edge(v, u, w); } } struct BFSW(Graph) { import std.algorithm, std.container; Graph g; alias Node = g.Node, Edge = g.Edge; void delegate(Node)[] findNode; void delegate(Edge)[] findEdge; this(ref Graph g) { this.g = g; } auto run(Node s) { auto visited = new bool[](g.n); auto q = DList!(Node)(s); visited[s] = true; findNode.each!(f => f(s)); while (!q.empty) { auto u = q.front; q.removeFront(); foreach (e; g[u]) if (!visited[e.dst]) { visited[e.dst] = true; findNode.each!(f => f(e.dst)); findEdge.each!(f => f(e)); q.insertBack(e.dst); } } } } auto bfsw(G)(ref G g) { return BFSW!G(g); }
D
import std.stdio, std.conv, std.array, std.string; import std.algorithm; import std.container; import std.range; import core.stdc.stdlib; import std.math; void main() { auto N = readln.chomp.to!int; auto Dn = readln.split.to!(int[]); int total = 0; foreach(i; 0..N) { for(int o=i+1; o<N; o++) { total += Dn[i] * Dn[o]; } } writeln(total); }
D
// import chie template :) {{{ import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii; // }}} // tbh.scanner {{{ class Scanner { import std.stdio; import std.conv : to; import std.array : split; import std.string : chomp; private File file; private dchar[][] str; private uint idx; this(File file = stdin) { this.file = file; this.idx = 0; } private dchar[] next() { if (idx < str.length) { return str[idx++]; } dchar[] s; while (s.length == 0) { s = file.readln.chomp.to!(dchar[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(uint len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } } // }}} void main() { auto cin = new Scanner; int N = cin.next!int; int[] arr = cin.nextArray!int(N); int res; while (true) { foreach (ref i; arr) { if (i % 2 != 0) { writeln(res); return; } i /= 2; } ++res; } }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std; // dfmt on void main() { long N = lread(); auto H = aryread(); foreach_reverse (i; 0 .. N - 1) { // writeln(i); if (H[i + 1] - H[i] < -1) { writeln("No"); return; } if (H[i + 1] - H[i] == -1) { H[i]--; } } writeln("Yes"); }
D
import std.stdio, std.string, std.conv; void main() { auto n = readln.strip.to!int; if(n%2==0){ writeln(n); return; } writeln(n*2); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp.to!int; auto a = readln.chomp.split.to!(int[]); int cnt = 1; int res; foreach (e; a) { if (cnt == e) { cnt++; } else { res++; } } if (res == n) { writeln(-1); } else { res.writeln; } }
D
import std.stdio, std.string, std.conv; void main() { auto x = readln.chomp.to!int; writeln(x*x*x); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { auto abc = readints; int a = abc[0], b = abc[1], c = abc[2]; if (a <= c && c <= b) writeln("Yes"); else writeln("No"); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; 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 main() { long a,b,c; scan(a,b,c); long k = lread(); long three_max = max(a,b,c); three_max = three_max * pow(2, k); three_max -= max(a,b,c); writeln(three_max + a + b + c); }
D
import std.stdio; import std.array; import std.algorithm; import std.conv; import std.numeric; import std.string; void main() { auto l = readln.chomp.split.map!(to!int).array; int H = l[0], W = l[1]; int[][] c = new int[][](10, 10); int[][] A = new int[][](H, W); foreach(i; 0..10) { c[i][] = readln.chomp.split.map!(to!int).array; } foreach(i; 0..H) { A[i][] = readln.chomp.split.map!(to!int).array; } foreach(k; 0..10) { foreach(j; 0..10) { foreach(i; 0..10) { c[i][j] = min(c[i][j], c[i][k] + c[k][j]); } } } int ans; foreach(i; 0..H) { foreach(j; 0..W) { if (A[i][j] != -1 && A[i][j] != 1) { ans += c[A[i][j]][1]; } } } ans.writeln; }
D
void main() { long[] abc = readln.split.to!(long[]); if (abc.all!"a % 2 == 0") { if (abc.all!(x => x == abc[0])) { writeln(-1); } else { long cnt; while (abc.all!"a % 2 == 0") { long[] tmp = new long[3]; foreach (i; 0 .. 3) { tmp[i] = (abc[(i+1)%3] + abc[(i+2)%3]) / 2; } abc = tmp; ++cnt; } cnt.writeln; } } else { 0.writeln; } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; immutable long INF = 1L << 50; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto P = s[2].to!long; auto G = new Edge!long[][](N); foreach (_; 0..M) { s = readln.split.map!(to!int); auto u = s[0] - 1; auto v = s[1] - 1; long c = -s[2] + P; G[u] ~= Edge!long(v, c); } auto dist = bellman_ford!(long, INF)(G, 0); if (dist[N-1] == -INF) { writeln(-1); } else { writeln(max(0L, -dist[N-1])); } } struct Edge(T) { int to; T cost; } T[] bellman_ford(T, T inf)(const Edge!(T)[][] graph, int start) { int n = graph.length.to!int; auto dist = new T[](n); dist[] = inf; dist[start] = 0; foreach (const i; 0..n) { foreach (const u; 0..n) { foreach (const e; graph[u]) { if (dist[u] != inf && dist[e.to] > dist[u] + e.cost) { dist[e.to] = dist[u] + e.cost; if (i == n - 1) { dist[e.to] = -inf; } } } } } auto stack = new int[](n); int sp = 0; foreach (const i; 0..n) { if (dist[i] == -inf) { stack[sp++] = i; } } while (sp > 0) { int u = stack[--sp]; foreach (const e; graph[u]) { if (dist[e.to] == -inf) continue; dist[e.to] = -inf; stack[sp++] = e.to; } } return dist; }
D
void main() { long n = rdElem; dchar[] s = rdDchar; Fenwick[] tree = new Fenwick[26]; foreach (i; 0 .. 26) tree[i] = Fenwick(n); foreach (i, x; s) { tree[x-'a'].add(i+1, 1); } long q = rdElem; foreach (i; 0 .. q) { long num, pos; string c; rdVals(num, pos, c); if (num == 1) { dchar d = c.to!dchar; if (d == s[pos-1]) continue; tree[s[pos-1]-'a'].add(pos, -1); tree[d-'a'].add(pos, 1); s[pos-1] = d; } else { long end = c.to!long; long cnt; foreach (j; 0 .. 26) { if (tree[j].sum(end) - tree[j].sum(pos-1)) ++cnt; } cnt.writeln; } } } struct Fenwick { long[] tree; this(long n) { tree.length = n + 1; } void add(long idx, long val) { for (long i = idx; i < tree.length; i += i & -i) { tree[i] += val; } } long sum(long idx) { long result; for (long i = idx; i > 0; i -= i & -i) { result += tree[i]; } return result; } } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; 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, K; scan(N, K); if (K == 1) { writeln(N == 1 ? 1 : 0); return; } auto adj = new int[][](N, 0); foreach (i ; 0 .. N - 1) { int ai, bi; scan(ai, bi); ai--, bi--; adj[ai] ~= bi; adj[bi] ~= ai; } long dfs(int v, int p, int dep) { long res = 1; long t = dep > 0 ? K - 2 : K - 1; foreach (u ; adj[v]) if (u != p) { res *= t * dfs(u, v, dep + 1) % mod; res %= mod; t--; if (t < 0) t = 0; } debug { writefln("v: %d, res: %d", v, res); } return res; } auto ans = dfs(0, -1, 0) * K % mod; writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.conv; import std.string; import std.algorithm; import std.array; import std.stdio; void main() { auto limit = readln.chomp.split(' ').map!(to!long).array[2]; auto booksA = readln.chomp.split(' ').map!(to!long).array; auto booksB = readln.chomp.split(' ').map!(to!long).array; long count; long time; size_t endA = booksA.length; foreach (size_t i, e;booksA) { if (time + e <= limit) { time += e; ++count; } else { endA = i; break; } } size_t endB = booksB.length; foreach (size_t i, e;booksB) { if (time + e <= limit) { time += e; ++count; } else { endB = i; break; } } auto answer = count; foreach_reverse (i;0..endA) { --count; time -= booksA[i]; while (endB < booksB.length && time + booksB[endB] <= limit) { time += booksB[endB]; ++endB; ++count; } if (count > answer) { answer = count; } } answer.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; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto S = RD!string; long[char] cnt; foreach (c; S) { ++cnt[c]; } bool ans; auto keys = cnt.keys; if (keys.length == 2) { if (cnt[keys[0]] == cnt[keys[1]]) ans = true; } writeln(ans ? "Yes" : "No"); stdout.flush(); debug readln(); }
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; import std.typecons; void main() { auto s = read(); const string t = "CODEFESTIVAL2016"; int ret = 0; foreach(i; 0..t.length) { if (s[i] != t[i]) ret++; } writeln(ret); } string read() { static string[] ss; while (!ss.length) ss = readln.chomp.split; auto res = ss[0]; ss.popFront; return res; }
D
import std.container; import std.range; import std.algorithm; import std.array; import std.string; import std.conv; import std.stdio; import std.container; int de(string s, string t) { int c; foreach (i;0..t.length) { if (s[i] != t[i]) { ++c; } } return c; } void main() { auto s = readln.chomp; auto t = readln.chomp; auto m = int.max; while (s.length >= t.length) { auto r = de(s[0..t.length], t); if (r < m) { m = r; } s = s[1..$]; } writeln(m); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); auto A = aryread(); // auto B = A.map!(x => BigInt(x)).array; auto l = lcm(A); long ans; foreach (a; A) { long x = (l * powmod(a, MOD - 2, MOD)) % MOD; ans = (ans + x) % MOD; } writeln(ans); } long lcm(long[] A) { long[long] f; foreach (a; A) { auto d = factorize(a); foreach (key, value; d) { f[key] = max(f.get(key, 0), value); } } // writeln(f); long ans = 1; foreach (key, value; f) { ans = powmod(key, value, MOD) * ans % MOD; } return ans; } /// x^^n % m T powmod(T = long)(T x, T n, T m) { if (n < 1) return 1; if (n & 1) { return x * powmod(x, n - 1, m) % m; } T tmp = powmod(x, n / 2, m); return tmp * tmp % m; } /// 素因数分解 long[long] factorize(long x) { assert(0 < x, "x is negative"); long[long] ps; while ((x & 1) == 0) { x /= 2; ps[2] = (2 in ps) ? ps[2] + 1 : 1; } for (long i = 3; i * i <= x; i += 2) while (x % i == 0) { x /= i; ps[i] = (i in ps) ? ps[i] + 1 : 1; } if (x != 1) ps[x] = (x in ps) ? ps[x] + 1 : 1; return ps; }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { auto x = lread(); if (x >= 30) { writeln("Yes"); } else { writeln("No"); } } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}} void main() { int n, y; readV(n, y); foreach (i; 0..n+1) foreach (j; 0..n-i+1) { auto k = n-i-j; if (i*10000 + j*5000 + k*1000 == y) { writeln(i, " ", j, " ", k); return; } } writeln("-1 -1 -1"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto K = readln.chomp.to!long; long s; foreach (a; 1..K+1) foreach (b; 1..K+1) { auto x = gcd(a, b); foreach (c; 1..K+1) s += gcd(c, x); } writeln(s); }
D
import std.stdio, std.string, std.conv; import std.array, std.algorithm, std.range; void main() { auto nq = readln().split().map!(to!int); immutable n=nq[0], q=nq[1]; int[][] r; for(int i=n; i>1; i=(i+1)/2) r~=new int[i]; r~=new int[1]; foreach(_;0..q) { auto av = readln().split().map!(to!int); auto a=av[0]-1, v=r[0][a]+av[1]; foreach(t;r) { auto b = a^1; auto p = t[a]; t[a]=v; if(b>=t.length) { a/=2; } else if(max(p,t[b])!=max(v,t[b])) { a/=2; v=max(v,t[b]); } else { break; } } a=0; foreach_reverse(t;r[0..$-1]) { a*=2; if(a+1<t.length && t[a]<t[a+1]) ++a; } writeln(a+1," ",r[0][a]); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto N = readln.chomp.to!int; for (int a = 0; a * 4 <= 100; ++a) { for (int b = 0; b * 7 <= 100; ++b) { if (a * 4 + b * 7 == N) { writeln("Yes"); return; } } } writeln("No"); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto s=readln.chomp.to!(char[]); auto e=new int[](n+1), w=new int[](n+1); foreach(i; 0..n){ if(s[i]=='E') e[i+1]++; else w[i+1]++; } foreach(i; 0..n){ e[i+1]+=e[i]; w[i+1]+=w[i]; } int mn=n; foreach(i; 0..n){ int k=w[i]+(e[n]-e[i+1]); mn=min(mn, k); } writeln(mn); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x) e=l[i].to!(typeof(e)); }
D
import 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, M; scan(N, M); alias T = Tuple!(long, long); auto G = new T[][](N); alias T2 = Tuple!(long, long, long); auto LRD = new T2[](M); foreach (i; 0 .. M) { long L, R, D; scan(L, R, D); L--, R--; LRD[i] = T2(L, R, D); G[R] ~= T(L, D); } auto pos = new long[](N); pos[] = -2; long eval(long x) { // writefln("%s %s", x, G[x]); if (pos[x] == -1) return 0; if (pos[x] == -2) { pos[x] = -1; if (G[x].length == 0) { pos[x] = 0; return 0; } long d; foreach (t; G[x]) { d = max(d, t[1] + eval(t[0])); } pos[x] = d; return d; } else { return pos[x]; } } foreach (i; 0 .. N) eval(i); foreach (lrd; LRD) { if (pos[lrd[1]] - pos[lrd[0]] != lrd[2]) { writeln("No"); return; } } writeln("Yes"); // writeln(pos); }
D
import std.stdio, std.string; void main(){ auto a = readln.split; writeln(a[0][$-1]==a[1][0] && a[1][$-1]==a[2][0] ? "YES" : "NO"); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto A = RD; auto B = RD; auto C = RD; auto D = RD; auto cnt1 = (C+B-1) / B; auto cnt2 = (A+D-1) / D; writeln(cnt1 <= cnt2 ? "Yes" : "No"); stdout.flush; debug readln; }
D
import std.conv, std.stdio; import std.string; void main() { immutable n = readln.chomp.to!int; ((n % 10 == 7 || n % 100 / 10 == 7 || n / 100 == 7) ? "Yes" : "No").writeln; }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.ascii; void main() { auto s = readln.chomp.split.to!(int[]).sum; writeln(s > 21 ? "bust" : "win"); }
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() { foreach (line; stdin.byLine) { auto str = line.chomp; string ans = ""; int i; while (i < str.length) { if (str[i] == '@') { foreach (j; 0..str[i+1]-'0') { ans ~= str[i+2]; } i += 3; } else { ans ~= str[i]; i++; } } ans.writeln; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto S = readln.chomp; auto K = readln.chomp.to!long; foreach (i; 0..K) { if (S[i] != '1' || i == K-1) { writeln(S[i]); return; } } }
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() { auto S = sread(); S.map!"a=='-'?-1:1".sum().writeln(); }
D
/+ dub.sdl: name "C" dependency "dcomp" version=">=0.3.2" lflags "-stack_size" "100000000" +/ import std.algorithm, std.conv, std.range, std.stdio; import core.sys.posix.stdlib; // import dcomp.scanner; int n; Scanner sc; static this(){ sc = new Scanner(); } bool que(string[2] s) { writeln(s[0]); writeln(s[1]); stdout.flush; string buf; sc.read(buf); if (buf == "end") { exit(0); } return buf == "T"; } int main(string[] argv) { sc.read(n); string[2] s; while (true) { auto rng = iota(4).find!((i){ string s0 = s[0] ~ (i%2 ? "." : "#"); string s1 = s[1] ~ (i/2 ? "." : "#"); return que([s0, s1]); }); if (rng.empty) break; auto i = rng.front; string s0 = s[0] ~ (i%2 ? "." : "#"); string s1 = s[1] ~ (i/2 ? "." : "#"); s = [s0, s1]; } while (true) { auto rng = iota(4).find!((i){ string s0 = (i%2 ? "." : "#") ~ s[0]; string s1 = (i/2 ? "." : "#") ~ s[1]; return que([s0, s1]); }); if (rng.empty) break; auto i = rng.front; string s0 = (i%2 ? "." : "#") ~ s[0]; string s1 = (i/2 ? "." : "#") ~ s[1]; s = [s0, s1]; } return 0; } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File, stdin; 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 = stdin) { this.f = f; } string[] buf; private bool succ() { while (!buf.length) { if (f.eof) return false; buf = f.readln.split; } return true; } private bool readSingle(T)(ref T x) { if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { //string or char[10] etc x = buf.front; buf.popFront; } else { static if (isStaticArray!T) { //static assert(buf.length == T.length); } x = buf.map!(to!E).array; buf.length = 0; } } else { x = buf.front.to!T; buf.popFront; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); fout.writeln("1 2 3"); fout.writeln("ab cde"); fout.writeln("1.0 1.0 2.0"); fout.close; Scanner sc = new Scanner(File(fileName, "r")); int a; int[2] b; char[2] c; string d; double e; double[] f; sc.read(a, b, c, d, e, f); assert(a == 1); assert(equal(b[], [2, 3])); assert(equal(c[], "ab")); assert(equal(d, "cde")); assert(e == 1.0); assert(equal(f, [1.0, 2.0])); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto n = RD!int; auto a = RDA; long x = a[0]; long y; foreach (i; 0..n) { x.chmin(a[i]); auto rem = a[i] - x; auto d = rem - y; if (d < 0) { x += d; } if (x < 0) break; a[i] -= x; y = a[i]; } x = a[$-1]; y = 0; foreach_reverse (i; 0..n) { x.chmin(a[i]); auto rem = a[i] - x; auto d = rem - y; if (d < 0) { x += d; } if (x < 0) break; a[i] -= x; y = a[i]; } auto s = a.sum; ans[ti] = s == 0; } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush; debug readln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; int INF = 1 << 29; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto G = new int[][](N, N); foreach (i; 0..N) G[i][i] = true; foreach (_; 0..M) { s = readln.split.map!(to!int); G[s[0]-1][s[1]-1] = true; G[s[1]-1][s[0]-1] = true; } auto D = new int[](N); fill(D, -1); Tuple!(int, int) dfs(int n, int parity) { if (D[n] == parity) return tuple(0, 0); if (D[n] != -1 && D[n] != parity) return tuple(-INF, -INF); D[n] = parity; auto ret = tuple(0, 0); ret[0] += parity == 0 ? 1 : 0; ret[1] += parity == 1 ? 1 : 0; foreach (m; 0..N) { if (!G[n][m]) { auto t = dfs(m, parity^1); if (t[0] < 0) return tuple(-INF, -INF); ret[0] += t[0]; ret[1] += t[1]; } } return ret; } int[] V, W; foreach (i; 0..N) { if (D[i] == -1) { auto t = dfs(i, 0); if (t[0] < 0) { writeln(-1); return; } V ~= t[0]; W ~= t[1]; } } immutable int MAX = 1000; auto dp = new bool[][](N+1, N+1); dp[0][0] = true; foreach (i; 0..V.length) foreach_reverse (j; 0..N+1) foreach_reverse (k; 0..N+1) if (dp[j][k]) dp[j+V[i]][k+W[i]] = true, dp[j+W[i]][k+V[i]] = true; int ans = INF; foreach (i; 0..N+1) { int j = N - i; if (dp[i][j]) { ans = min(ans, i*(i-1)/2+j*(j-1)/2); } } ans.writeln; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; long n, m; rd(n, m); if(n>m) swap(n, m); if((m-n)>=2){ writeln(0); return; } long tot=1, mod=10^^9+7; for(int i=1; i<=n; i++) (tot*=i)%=mod; for(int i=1; i<=m; i++) (tot*=i)%=mod; if(n==m) (tot*=2)%=mod; writeln(tot); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x) e=l[i].to!(typeof(e)); }
D
import 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; // dfmt on void main() { auto S = sread(); long cnt; foreach (c; S) cnt += c == 'o'; bool b = 8 <= cnt + (15 - S.length); writeln(b ? "YES" : "NO"); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip, std.datetime; void main() { long[long] s; auto N = readln.chomp.to!int; foreach (_; 0..N) { auto t = readln.split.map!(to!long); auto a = t[0]; auto x = t[1]; if (a in s) s[a] = max(s[a], x); else s[a] = x; } auto M = readln.chomp.to!int; foreach (_; 0..M) { auto t = readln.split.map!(to!long); auto a = t[0]; auto x = t[1]; if (a in s) s[a] = max(s[a], x); else s[a] = x; } s.values.sum.writeln; }
D
import std.stdio; import std.string; import std.conv; import std.array; void main(){ int n=to!int(chomp(readln())),b=0; string s=""; bool f=false; for(int i=0;i<n;++i){ string[] input=split(readln()); if(input[0]=="(") b+=to!int(input[1]); else b-=to!int(input[1]); if(b<0) f=true; } writeln((b==0&&!f)?"YES":"NO"); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { int x = readint; int ans = 1; for (int b = 2; b <= x; b++) { int a = b * b; while (a <= x) { ans = max(ans, a); a *= b; } } writeln(ans); }
D
import std.stdio,std.conv,std.string; void main(){ int D,N; auto DN = readArray!int(); D = DN[0]; N = DN[1]; if( N == 100 ){ (100^^D*101).writeln(); }else{ (100^^D*N).writeln(); } } T[] readArray(T)(){ T[] ret; foreach( elm ; readln().split() ){ ret ~= elm.to!T(); } return ret; }
D
unittest { assert( [ "A" ].parse.expand.solve == "T" ); assert( [ "G" ].parse.expand.solve == "C" ); } import std.conv; import std.range; import std.stdio; import std.typecons; void main() { stdin.byLineCopy.parse.expand.solve.writeln; } auto parse( Range )( Range input ) if( isInputRange!Range && is( ElementType!Range == string ) ) { auto b = input.front; return tuple( b ); } auto solve( string b ) { switch( b ) { case "A": return "T"; case "C": return "G"; case "G": return "C"; case "T": return "A"; default : assert( false ); } }
D
void main() { string l = rdStr; long len = l.length; long[] exp = new long[len]; exp[len-1] = 1; foreach_reverse (i; 0 .. len-1) { exp[i] = (3 * exp[i+1]) % mod; } long num = 1; long result; foreach (i, x; l) { if (x == '0') continue; result = (result + (num * exp[i]) % mod) % mod; num = (2 * num) % mod; } result = (result + num) % mod; result.writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; enum double eps = 1.0e-9; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
import 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; int[2] ans; ans[0] = 101; ans[1] = -1; while (n--) { auto x = readln.split.map!(to!int); if (ans[1] < x[1]) { ans = x.array; } else if (ans[1] == x[1] && ans[0] > x[0]) { ans = x.array; } } writeln(ans[0], " ", ans[1]); }
D
import std.stdio; import std.string; import std.conv; int main() { while (true) { string[] str = readln().split(); if (str[0] == "0" && str[1] == "0") break; for (int i = 0; i < str[0].to!int(); i++) { for (int j = 0; j < str[1].to!int(); j++) { char s; if (i == 0 || i == str[0].to!int() - 1 || j == 0 || j == str[1].to!int() - 1) s = '#'; else s = '.'; write(s); } writeln(); } writeln(); } 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; } 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; long ans = long.max; foreach (i; 1..N) { auto a = i; auto b = N - i; long cnt; while (a != 0) { cnt += a % 10; a /= 10; } while (b != 0) { cnt += b % 10; b /= 10; } ans = min(ans, cnt); } writeln(ans); stdout.flush(); debug readln(); }
D
import std.algorithm, std.string, std.array, std.range, std.stdio, std.conv; void main() { string s = readln.chomp; writeln(s[0], s[1..$-1].length, s[$-1]); }
D
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math, std.container, std.typecons; import core.stdc.stdio; // foreach, foreach_reverse, writeln void main() { int n, k; scanf("%d%d", &n, &k); long ans = 0; long f(long x) { return x*x*x; } ans += f(n/k); if (k%2 == 0) ans += f((n+k/2)/k); writeln(ans); }
D
import std.stdio, std.string, std.conv, std.algorithm; void main() { auto d = readln.chomp.to!(char[]); d.reverse; d.to!string.writeln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; bool solve() { auto s = readln.split.map!(to!int); auto P = s[0]; auto Q = s[1]; auto A = s[2]; auto N = s[3]; if (P == 0) return false; long dfs(long p, long q, int n, long prod, long prev) { if (p == 0) return 1; if (prod * prev > A) return 0; if (n == N) return 0; if (p * prev > q * (N - n)) return 0; long ret = 0; for (long i = prev; prod * i <= A; ++i) { long x = i * p - q; long y = i * q; if (x < 0) continue; if (x != 0 && prod * i * i > A) continue; long g = gcd(x, y); ret += dfs(x / g, y / g, n + 1, prod * i, i); } return ret; } dfs(P, Q, 0, 1, 1).writeln; return true; } void main() { while (solve) {} }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } enum MOD = (10 ^^ 9) + 7; void main() { long X = lread(); long l = 1; long r = 50000; while (1 < r - l) { long m = (l + r) / 2; if (m * (m + 1) / 2 < X) { l = m; } else { r = m; } } writeln((l * (l + 1) / 2 == X) ? l : r); }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n, m; long k; rd(n, m, k); auto a = readln.split.to!(long[]); for (auto i = n - 1, r = k; i >= 0; i--) { if ((r -= a[i]) < 0) { if ((--m) == 0) { writeln(n - i - 1); return; } r = k - a[i]; } } writeln(n); } void rd(T...)(ref T x) { import std.stdio : readln; import std.string : split; import std.conv : to; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
D
import std.stdio, std.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; scan(N); auto a = readln.split.to!(int[]); auto b = readln.split.to!(int[]); // dp(s, x) = (sに含まれるカードを左からソートした状態で詰めて最後がxになるときの最小操作回数) auto dp = new int[][](1 << N, 51); fillAll(dp, inf); dp[0][0] = 0; foreach (s ; 0 .. 1 << N) { foreach (x ; 0 .. 51) { if (dp[s][x] == inf) continue; int p = 0; int i = s.popcnt; foreach (j ; 0 .. N) if (!(s & 1 << j)) { int pos = p + i; p++; int y = abs(j - i) % 2 == 0 ? a[j] : b[j]; debug { writefln("pos: %s, y: %s", pos, y); } if (y >= x) { chmin(dp[s | 1 << j][y], dp[s][x] + abs(pos - i)); } } } } debug { writefln("%(%s\n%)", dp); } int ans = inf; foreach (x ; 0 .. 51) { chmin(ans, dp[(1 << N) - 1][x]); } ans = ans < inf ? ans : -1; writeln(ans); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N, K; scan(N, K); long R, S, P; scan(R, S, P); string T = sread(); auto dp = new long[][](N + 1 + K, 3); foreach (i; 0 .. N) { dp[i + K][0] = max(dp[i][1], dp[i][2]) + ((T[i] == 's') ? R : 0); dp[i + K][1] = max(dp[i][0], dp[i][2]) + ((T[i] == 'p') ? S : 0); dp[i + K][2] = max(dp[i][0], dp[i][1]) + ((T[i] == 'r') ? P : 0); } long ans; foreach (i; 0 .. K) { ans += max(dp[N + K - 1 - i][0], dp[N + K - 1 - i][1], dp[N + K - 1 - i][2]); } writeln(ans); }
D
import std.stdio; import std.algorithm; import std.string; import std.range; import std.array; import std.conv; import std.complex; import std.math; import std.ascii; import std.bigint; import std.container; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } void readlnTo(T...)(ref T t) { auto s = readln().split(); assert(s.length == t.length); foreach(ref ti; t) { ti = s[0].to!(typeof(ti)); s = s[1..$]; } } const real eps = 1e-10; void main(){ long a, b; readlnTo(a, b); if(a*b <= 0) { writeln("Zero"); return; } if(a > 0) { writeln("Positive"); return; } writeln((b-a+1)%2 == 1 ? "Negative": "Positive"); }
D
import std.stdio, std.range, std.conv, std.string, std.array, std.functional, std.math; import std.algorithm.comparison, std.algorithm.iteration, std.algorithm.mutation, std.algorithm.searching, std.algorithm.setops, std.algorithm.sorting; import std.container.binaryheap; import std.typecons; void main() { const input = readln().strip.to!(long); writeln(input/2 * (input/2 + input%2)); }
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.math; import core.time; int[][] ofs = [ [0, 1], [1, 0], [-1, 0], [0, -1] ]; void fill(bool[][] field, bool[][] done, int x, int y, bool color) { foreach (d; ofs) { int nx = d[0] + x; int ny = d[1] + y; if (nx < 0 || field[0].length <= nx || ny < 0 || field.length <= ny) { continue; } if (field[ny][nx] != color) { continue; } if (done[ny][nx]) { continue; } done[ny][nx] = true; fill(field, done, nx, ny, color); } } alias Ret = Tuple!(int, "a", int, "b"); Ret cal(bool[][] field) { if (field.length == 0) { return Ret.init; } bool[][] done = new bool[][](field.length, field[0].length); Ret ret; foreach (int y, arr; field) { foreach (int x, color; arr) { if (done[y][x]) { continue; } done[y][x] = true; if (field[y][x]) { ret.b++; } else { ret.a++; } fill(field, done, x, y, color); } } return ret; } void main() { auto inp = readln.chomp.split.map!(to!int); int a = inp.front; int b = inp.back; bool[][] field = new bool[][](100, 100); foreach (ref arr; field[0..50]) { foreach (ref v; arr) { v = true; } } --a; --b; foreach (i; 0..a) { int x = (i * 2) % 100; int y = (i * 2) / 100 * 2; field[y][x] = false; } foreach (i; 0..b) { int x = (i * 2) % 100; int y = (i * 2) / 100 * 2; x = 99 - x; y = 99 - y; field[y][x] = true; } writeln(field.length, " ", field[0].length); foreach (arr; field) { foreach (v; arr) { write = v ? "#" : "."; } writeln; } }
D
import std.functional, std.algorithm, std.container, std.typetuple, std.typecons, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv; void main() { int[] NK = readln.chomp.split.to!(int[]); int N = NK[0], K = NK[1]; int[] A = readln.chomp.split.to!(int[]); int ans = 1; ans += (N - K) / (K - 1); if ((N - K) % (K - 1) != 0) { ans++; } writeln(ans); }
D
import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; import std.container; 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; long a = read.to!long; long b = read.to!long; long ans; if(n == 1 && a < b) ans = 0; else if(a > b) ans = 0; else ans = (a + b * (n -1)) - (a * (n - 1) + b) + 1; ans.writeln; }
D
import std.stdio; import std.string; import std.range; import std.conv; void main() { auto N = readln.chomp; if(N[0]=='9' || N[1]=='9'){ writeln("Yes"); }else{ writeln("No"); } }
D
import std.stdio, std.conv, std.string, std.algorithm, std.math, std.array; bool p(string s) { for(int i=0; i<s.length/2; i++) { if(s[i] != s[$-1-i]) return false; } return true; } bool q(string s) { return p(s) && p(s[0..$/2]); } void main() { string s = readln[0..$-1]; if(q(s)) writeln("Yes"); else writeln("No"); }
D
import std.stdio, std.string, std.array, std.conv, std.algorithm; long P = 10^^9+7; void main() { auto n = readln.chomp.to!long; long ret = 1; foreach (i; 0..n) { ret *= i + 1; ret %= P; } writeln(ret); }
D
import std.stdio, std.string, std.conv, std.algorithm; void main() { long[] buf = readln.chomp.split.to!(long[]); long q = buf[0], h = buf[1], s = buf[2], d = buf[3]; long N = readln.chomp.to!(long); long min_cost_per_1L = min(q * 4, h * 2, s); long min_cost_per_2L = min(q * 8, h * 4, s * 2, d); writeln( min((N / 2 * min_cost_per_2L + N % 2 * min_cost_per_1L), (N * min_cost_per_1L)) ); }
D
import std.stdio,std.ascii,std.conv,std.string,std.algorithm,std.range,std.functional,std.math,core.bitop; void main() { auto a=readln.chomp.to!int; auto b=readln.chomp.to!int; auto h=readln.chomp.to!int; writeln((a+b)*(h/2)); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void main() { auto t = RD!int; auto ans = new int[](t); foreach (ti; 0..t) { auto x = RD; auto y = RD; auto a = RD; auto b = RD; auto d = y - x; if (d % (a+b) != 0) ans[ti] = -1; else ans[ti] = d / (a+b); } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
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[] xs) { int ans = 0; int p = 1; while (p < xs.length) { if (xs[p-1] == xs[p]) { ans++; p += 2; } else p++; } return ans; } void main() { readint; auto xs = readints; int ans = calc(xs); writeln(ans); }
D
void main(){ import std.stdio, std.conv, std.string, std.algorithm; long n, a, b, k; rd(n, a, b, k); const long mod=998244353; const int M=1_000_00*4; auto fact=new long[](M); fact[0]=fact[1]=1; foreach(i; 2..M) fact[i]=i*fact[i-1]%mod; auto inv_fact=new long[](M); long powmod(long a, long x){ if(x==0) return 1; else if(x==1) return a; else if(x&1) return a*powmod(a, x-1)%mod; else return powmod(a*a%mod, x/2); } foreach(i; 0..M) inv_fact[i]=powmod(fact[i], mod-2); long comb(long nn, long rr){ long ret=fact[nn]%mod; (ret*=inv_fact[rr])%=mod; (ret*=inv_fact[nn-rr])%=mod; return ret; } long tot=0; for(long x=0; x<=n; x++)if(k-a*x>=0){ if((k-a*x)%b==0){ long y=(k-a*x)/b; if(y>n) continue; (tot+=comb(n, x)*comb(n, y)%mod)%=mod; } } writeln(tot); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x) e=l[i].to!(typeof(e)); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!long).array; auto S = A.sum; auto md = 1L << 59; long x = 0; foreach (a; A) { x += a; auto y = S - x; md = min(md, abs(x - y)); } md.writeln; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.stdio; import std.string; import std.range; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { int n = readint(); int ans = 1; while (ans * 2 <= n) { ans *= 2; } writeln(ans); }
D
/+ dub.sdl: name "A" 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); string s; sc.read(s); writeln(s[0..$-8]); 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) { import std.algorithm : max; import std.range.primitives : ElementEncodingType; import core.stdc.string : memcpy; private alias T = ElementEncodingType!A; private T* _data; private size_t 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; if (len) memcpy(nx, _data, len * T.sizeof); _data = cast(T*)(nx); } void opOpAssign(string op : "~")(T item) { if (len == cap) { reserve(max(4, 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 core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); auto d0 = divisor(N - 1); auto d1 = divisor(N); // writeln(d0); // writeln(d1);s bool[long] dict; foreach (d; d0) dict[d] = true; long ans = d0.length - 1; foreach (d; d1) { if (d == 1 || (d in dict) || N < d) continue; long n = N; while (0 < n && n % d == 0) n /= d; if (n % d == 1) { // writeln(d); ans++; } } writeln(ans); } long[] divisor(long N) { long[] ret; foreach (i; 1 .. N + 1) { if (N < i * i) break; if (N % i == 0) { ret ~= i; if (i * i != N) ret ~= N / i; } } return ret; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n, m; scan(n, m); auto adj = new int[][](n, 0); foreach (i ; 0 .. m) { int ai, bi; scan(ai, bi); adj[ai - 1] ~= bi - 1; adj[bi - 1] ~= ai - 1; } bool nibu = true; int[2] cnt; auto c = new int[](n); c[] = -1; void dfs(int v, int col) { c[v] = col; cnt[col]++; foreach (u ; adj[v]) { if (c[u] == -1) { dfs(u, col ^ 1); continue; } if (c[u] != (col ^ 1)) { nibu = false; return; } } } dfs(0, 0); long ans = nibu ? 1L * cnt[0] * cnt[1] - m : 1L * n * (n - 1) / 2 - m; writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.algorithm, std.string, std.array, std.range, std.stdio, std.conv; void main() { int[] nm = readln.chomp.split.to!(int[]); int n = nm[0], m = nm[1]; writeln((n - 1) * (m - 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; int[] AS; foreach (_; 0..N) AS ~= readln.chomp.to!int; int[] cs; foreach (a; AS) { if (cs.empty || cs[$-1] >= a) { cs ~= a; continue; } else if (cs[0] < a) { cs[0] = a; continue; } size_t l, r = cs.length-1; while (l+1 < r) { auto m = (l+r)/2; if (cs[m] < a) { r = m; } else { l = m; } } cs[r] = a; } writeln(cs.length); }
D
import std.stdio, std.string, std.array, std.conv; void main() { int n = readln.chomp.to!int; int[] point = new int[2]; foreach (i; 0 .. n) { string[] tmp = readln.chomp.split; if (tmp[0] == tmp[1]) { point[] += 1; } else if (tmp[0] < tmp[1]) { point[1] += 3; } else { point[0] += 3; } } writeln(point[0], " ", point[1]); }
D
import std.stdio,std.conv,std.string,std.algorithm; void main(){ auto k=readln.chomp.split.to!(int[])[0]; auto a=readln.chomp.split.to!(int[]); int[] d; foreach(i;1..a.length){ d~=a[i]-a[i-1]; } d~=a[0]+(k-a[$-1]); (k-max(d)).writeln; } auto max(int[] arr){ int m; foreach(e;arr){ if(m<=e) m=e; } return m; }
D
import core.stdc.stdio; import std.stdio; import std.algorithm; static immutable int unSet=-114514; struct Data{ int a,l,r,m; } Data Comb(const ref Data a,const ref Data b){ Data r; r.a=a.a+b.a; r.l=max(a.l,a.a+b.l); r.r=max(a.r+b.a,b.r); r.m=max(a.m,b.m,a.r+b.l); return r; } struct Node{ Node* l,r,p; int delay=unSet,size,v; Data d; bool rev; int State(){ if(p !is null){ if(p.l==&this) return -1; if(p.r==&this) return 1; } return 0; } void Propagate(){ if(rev){ if(l !is null) l.rev=!l.rev; if(r !is null) r.rev=!r.rev; swap(l,r); swap(d.l,d.r); rev=false; } if(delay!=unSet){ if(l !is null) l.delay=delay; if(r !is null) r.delay=delay; int s=delay*(delay<0?1:size); d=Data(delay*size,s,s,s); v=delay; delay=unSet; } } void Prepare(){ if(State) p.Prepare; Propagate; } void Update(){ Propagate; d=Data(v,v,v,v); size=1; if(l !is null){ l.Propagate; d=Comb(l.d,d); size+=l.size; } if(r !is null){ r.Propagate; d=Comb(d,r.d); size+=r.size; } } void Rotate(){ Node* par=p,ch; if(p.l==&this){ ch=r; r=p; r.l=ch; }else{ ch=l; l=p; l.r=ch; } if(ch) ch.p=p; p=par.p; par.p=&this; if(p !is null){ if(p.l==par) p.l=&this; if(p.r==par) p.r=&this; } par.Update; Update; } void Splay(){ Prepare; while(State){ int s=State*p.State; if(!s) Rotate; else if(s==1){ p.Rotate; Rotate; }else{ Rotate; Rotate; } } } void Expose(){ Node* x=&this; while(x){ x.Splay; x=x.p; } x=&this; while(x.p){ x.p.r=x; x=x.p; x.Update; } Splay; } void Evert(){ Expose; r=null; rev=!rev; Update; } void Link(Node* t){ Evert; t.Expose; p=t; t.r=&this; t.Update; } void Cut(){ Expose; if(l !is null) l.p=null; l=null; Update; } void Set(int x){ Expose; r=null; delay=x; Update; } int Get(){ Expose; r=null; Update; return d.m; } } void main(){ int n,q; scanf("%d%d",&n,&q); Node[] ns=new Node[n]; foreach(ref nd;ns){ int w; scanf("%d",&w); nd.v=w; nd.d=Data(w,w,w,w); } foreach(i;0..n-1){ int s,e; scanf("%d%d",&s,&e); ns[--s].Link(&ns[--e]); } /* void Output(){ foreach(i,nd;ns){ writeln("Node ",i); writeln("Rev ",nd.rev); write("Parent "); if(nd.p is null) writeln("NULL"); else writeln(nd.p-ns.ptr); write("Left "); if(nd.l is null) writeln("NULL"); else writeln(nd.l-ns.ptr); write("Right "); if(nd.r is null) writeln("NULL"); else writeln(nd.r-ns.ptr); writeln("Value ",nd.v); writeln("Data ",nd.d); writeln; } }*/ // writeln("Begin"); // Output; foreach(_;0..q){ int t,a,b,c; // writeln("Query ",_); scanf("%d%d%d%d",&t,&a,&b,&c); if(t==1){ ns[--a].Evert; // writeln("Evert ",a); // Output; ns[--b].Set(c); // writeln("Set ",b); // Output; }else if(t==2){ ns[--a].Evert; // writeln("Evert ",a); // Output; printf("%d\n",ns[--b].Get); // writeln("Get ",b); // Output; }else assert(false); } }
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 Cake = Tuple!(long, "beauty", long, "tasty", long, "popular"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { long n, d; scan(n, d); long monitor = 2 * d + 1; ((n + monitor - 1)/ monitor).writeln(); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { readint; string s = read!string; int lparen = 0; int h = 0; foreach (c; s) { if (c == '(') h++; else { h--; lparen = min(lparen, h); } } foreach (_; 0..-lparen) write('('); write(s); foreach (_; 0..h-lparen) write(')'); writeln(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string; int[int][10^^5 + 1] MEMO; void main() { auto s = readln.chomp; int v, p; foreach (gp; s) { switch (gp) { case 'g': if (p == 0) { ++p; } else { --p; ++v; } break; case 'p': if (p == 0) { --v; ++p; } else { --p; } break; default: } } writeln(v); }
D
void main() { problem(); } void problem() { auto K = scan!int; long solve() { int unitMod = 7 % K; if (unitMod == 0) return 1; int mod = unitMod; foreach(i; 0..K) { mod = (mod * 10) + unitMod; mod %= K; if (mod == 0) return 2 + i; } return -1; } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { readln.split.to!(int[]).reduce!"a ^ b".writeln; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.string, std.conv, std.array, std.algorithm; import std.uni, std.math, std.container, std.typecons; import core.bitop, std.datetime, std.range; void main(){ auto rd = readln.split.to!(int[]); auto n = rd[0], q = rd[1]; auto uf = new UnionFind(n); foreach(lp ; 0 .. q){ rd = readln.split.to!(int[]); auto c = rd[0], x = rd[1], y = rd[2]; if(c == 0){ uf.unite(x, y); } else{ writeln(uf.is_same(x, y) ? 1 : 0); } } } class UnionFind{ private: int[] ds; int[] p; int[] rank; public: this(int N){ ds = iota(N).array; p = iota(N).array; rank = new int[](N); } int find_root(int x){ if(x != p[x]){ p[x] = find_root(p[x]); } return p[x]; } bool is_same(int x, int y){ return find_root(x) == find_root(y); } void unite(int x, int y){ auto u = find_root(x); auto v = find_root(y); if(rank[u] < rank[v]){ p[u] = v; } else{ p[v] = u; if(rank[u] == rank[v]) ++rank[u]; } } }
D
import std.stdio; import std.algorithm; import std.string; import std.range; import std.array; import std.conv; import std.complex; import std.math; import std.ascii; import std.bigint; import std.container; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } const real eps = 1e-10; void main(){ auto n = readLong(); long[] a; foreach(i; iota(n)) { a ~= readLong(); } long ans; foreach(i; iota(n)) { ans += a[i] / 2; a[i] %= 2; if(i < n-1) { if(a[i+1] > 0) { ans += a[i]; a[i+1] -= a[i]; } } } writeln(ans); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto Q = s[1]; auto st = new LazySegmentTree!(long, long, min, (a,b)=>a+b, (a,b)=>a+b, (a,b)=>a, 1L<<59, 0L)(N+1); st.table[] = 0L; while (Q--) { s = readln.split.map!(to!int); int q = s[0]; int l = s[1]; int r = s[2]; if (q == 0) { long v = s[3]; st.update(l, r, v); } else { st.query(l, r).writeln; } } } class LazySegmentTree(T, L, alias opTT, alias opTL, alias opLL, alias opPrd, T eT, L eL) { T[] table; L[] lazy_; int n; int size; this(int n) { this.n = n; size = 1; while (size <= n) size <<= 1; size <<= 1; table = new T[](size); lazy_ = new T[](size); table[] = eT; lazy_[] = eL; } void push(int i, int a, int b) { if (lazy_[i] == eL) return; table[i] = opTL(table[i], opPrd(lazy_[i], b - a + 1)); if (i * 2 + 1 < size) { lazy_[i*2] = opLL(lazy_[i*2], lazy_[i]); lazy_[i*2+1] = opLL(lazy_[i*2+1], lazy_[i]); } lazy_[i] = eL; } T query(int l, int r) { if (l > r) return eT; return query(l, r, 1, 0, n-1); } T query(int l, int r, int i, int a, int b) { if (b < l || r < a) return eT; push(i, a, b); if (l <= a && b <= r) { return table[i]; } else { return opTT(query(l, r, i*2, a, (a+b)/2), query(l, r, i*2+1, (a+b)/2+1, b)); } } void update(int l, int r, L val) { if (l > r) return; update(l, r, 1, 0, n-1, val); } void update(int l, int r, int i, int a, int b, L val) { if (b < l || r < a) { push(i, a, b); } else if (l <= a && b <= r) { lazy_[i] = opLL(lazy_[i], val); push(i, a, b); } else { push(i, a, b); update(l, r, i*2, a, (a+b)/2, val); update(l, r, i*2+1, (a+b)/2+1, b, val); table[i] = opTT(table[i*2], table[i*2+1]); } } }
D
import std.stdio, std.conv, std.string, std.math, std.regex, std.range, std.ascii, std.algorithm; void main(){ auto ip = readln.split.to!(int[]), N=ip[0], K=ip[1]; auto S = readln.chomp.to!(dchar[]); S[K-1] += 0x20; writeln(S); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string; void main() { auto abck = readln.split.to!(long[]); auto A = abck[0]; auto B = abck[1]; auto K = abck[3]; writeln(K&1 ? B - A : A - B); }
D