code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, 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 main() { string s; readV(s); auto n = s.length; auto t = new dchar[]((n+1)/2); foreach (i; 0..(n+1)/2) t[i] = s[i*2]; writeln(t); }
D
// Vicfred // https://atcoder.jp/contests/abc163/tasks/abc163_c // map import std.algorithm; import std.array; import std.conv; import std.stdio; import std.string; void main() { int n = readln.chomp.to!int; int[] a = readln.split.map!(to!int).array; int[] boss = new int[n+1]; foreach(person; a) boss[person] += 1; for(int i = 1; i <= n; i++) boss[i].writeln; }
D
import std.stdio; import std.string; import std.conv; void main() { string[] input = split(readln()); int A = to!int(input[0]); int B = to!int(input[1]); if(A + B >= 10){ writeln("error"); } else { writeln(A + B); } }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random; import std.typecons, std.functional, std.traits,std.concurrency; import std.algorithm, std.container; import core.bitop, core.time, core.memory; import std.bitmanip; import std.regex; enum INF = long.max/3; enum MOD = 10L^^9+7; //辞書順順列はiota(1,N),nextPermituionを使う void end(T)(T v) if(isIntegral!T||isSomeString!T||isSomeChar!T) { import core.stdc.stdlib; writeln(v); exit(0); } T[] scanArray(T = long)() { static char[] scanBuf; readln(scanBuf); return scanBuf.split.to!(T[]); } dchar scanChar() { int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } return cast(dchar)c; } T scanElem(T = long)() { import core.stdc.stdlib; static auto scanBuf = appender!(char[])([]); scanBuf.clear; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { scanBuf ~= cast(char) c; c = getchar; } return scanBuf.data.to!T; } dchar[] scanString(){ return scanElem!(dchar[]); } void main() { auto list = [0,1,3,1,2,1,2,1,1,2,1,2,1]; if(list[scanElem]==list[scanElem])end("Yes");end("No"); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int k; rd(k); struct E{int u, v, w;} E[] edges; int n=2; void f(int x){ if(x==2){ edges~=E(1, 2, 0); edges~=E(1, 2, 1); }else{ if(x&1){ f(x-1); edges~=E(1, n, x-1); }else{ f(x/2); foreach(ref e; edges) e.w*=2; edges~=E(n, n+1, 0); edges~=E(n, n+1, 1); n++; } } } f(k); writeln(n, " ", edges.length); foreach(e; edges){ writeln(e.u, " ", e.v, " ", e.w); } } 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.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 calc(string[] g) { auto rows = g.length; auto cols = g[0].length; bool[int] rowSkip, colSkip; for (int r = 0; r < rows; r++) { bool isSkip = true; for (int c = 0; c < cols; c++) { if (g[r][c] == '#') { isSkip = false; break; } } if (isSkip) rowSkip[r] = true; } for (int c = 0; c < cols; c++) { bool isSkip = true; for (int r = 0; r < rows; r++) { if (g[r][c] == '#') { isSkip = false; break; } } if (isSkip) colSkip[c] = true; } for (int r = 0; r < rows; r++) { if (r in rowSkip) continue; for (int c = 0; c < cols; c++) { if (c in colSkip) continue; write(g[r][c]); } writeln(); } } void main() { auto hw = readints; int h = hw[0]; string[] g; for (int i = 0; i < h; i++) { g ~= read!string; } calc(g); }
D
import std.stdio, std.string, std.range, std.algorithm, std.math, std.typecons, std.conv; void main() { auto a = readln.split; ((a[0][$-1] == a[1][0] && a[1][$-1] == a[2][0]) ? "YES" : "NO").writeln; }
D
//prewritten code: https://github.com/antma/algo import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.traits; final class InputReader { private: ubyte[] p, buffer; bool eof; bool rawRead () { if (eof) { return false; } p = stdin.rawRead (buffer); if (p.empty) { eof = true; return false; } return true; } ubyte nextByte(bool check) () { static if (check) { if (p.empty) { if (!rawRead ()) { return 0; } } } auto r = p.front; p.popFront (); return r; } public: this () { buffer = uninitializedArray!(ubyte[])(16<<20); } bool seekByte (in ubyte lo) { while (true) { p = p.find! (c => c >= lo); if (!p.empty) { return false; } if (!rawRead ()) { return true; } } } template next(T) if (isSigned!T) { T next () { if (seekByte (45)) { return 0; } T res; ubyte b = nextByte!false (); if (b == 45) { while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 - (b - 48); } } else { res = b - 48; while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 + (b - 48); } } } } template next(T) if (isUnsigned!T) { T next () { if (seekByte (48)) { return 0; } T res = nextByte!false () - 48; while (true) { ubyte b = nextByte!true (); if (b < 48 || b >= 58) { break; } res = res * 10 + (b - 48); } return res; } } T[] nextA(T) (in int n) { auto a = uninitializedArray!(T[]) (n); foreach (i; 0 .. n) { a[i] = next!T; } return a; } } void main() { auto r = new InputReader (); immutable n = r.next!uint (); auto a = r.nextA!uint (n); auto b = new bool[n + 1]; foreach (i; a) { b[i] = true; } int odds, evens; for (int i = 1; i <= n; ++i) { if (!b[i]) { if (i & 1) odds++; else evens++; } } int[] pos; foreach (i; 0 .. n) { if (a[i] == 0) pos ~= i; } immutable int m = pos.length.to!int; auto dp = new int[][][] (m + 1, odds + 1, 3); foreach (i; 0 .. m + 1) foreach (j; 0 .. odds + 1) dp[i][j][] = -1; auto vo = new int[m]; auto ve = new int[m]; b = new bool[m]; foreach (i; 0 .. m) { immutable int k = pos[i]; if (k > 0) { if (a[k-1] != 0) { if (a[k-1] & 1) { ve[i]++; } else { vo[i]++; } } else b[i] = true; } if (k+1 < n) { if (a[k+1] != 0) { if (a[k+1] & 1) { ve[i]++; } else { vo[i]++; } } } } debug stderr.writeln (vo); debug stderr.writeln (ve); debug stderr.writeln (b); debug stderr.writeln (odds); debug stderr.writeln (evens); int f (const int k, const int o, const int last) { if (dp[k][o][last] >= 0) return dp[k][o][last]; immutable int r = m - k; if (!r) return dp[k][o][last] = 0; immutable int co = odds - o, ce = k - co, e = evens - ce; assert (e >= 0); int res = int.max; if (o > 0) { int w = f (k + 1, o - 1, 1) + vo[k]; if (b[k] && last == 0) ++w; res = min (res, w); } if (e > 0) { int w = f (k + 1, o, 0) + ve[k]; if (b[k] && last == 1) ++w; res = min (res, w); } return dp[k][o][last] = res; } int res = f (0, odds, 2); foreach (i; 1 .. n) { if (a[i-1] != 0 && a[i] != 0 && 0 != ((a[i-1] ^ a[i]) & 1)) { ++res; } } writeln (res); }
D
void main() { problem(); } void problem() { auto A = scan!int; auto B = scan!int; auto C = scan!int; auto D = scan!int; bool solve() { while(true) { C -= B; if (C <= 0) return true; A -= D; if (A <= 0) return false; } } writeln(solve() ? "Yes" : "No"); } // ---------------------------------------------- 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.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto T = readln.chomp.to!int; foreach (_t; 0..T) { auto N = readln.chomp.to!int; writeln(N/2); } }
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; long sum_a, min_a = long.max; int mc; foreach (a; readln.split.to!(long[])) { if (a < 0) ++mc; a = abs(a); sum_a += a; min_a = min(min_a, a); } writeln(sum_a - (mc%2 == 1 ? min_a*2 : 0)); }
D
import std.algorithm; import std.array; import std.ascii; import std.bigint; import std.complex; import std.container; import std.conv; import std.functional; import std.math; import std.range; import std.stdio; import std.string; 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; const long p = 1_000_000_000 + 7; void main(){ auto N = readInt(); auto T = readLongs(); auto A = readLongs(); T = 0 ~ T ~ T.back; A = A[0] ~ A ~ 0; long ans = 1; foreach(i; iota(1,N+1)) { long hMinT = 1; long hMaxT = T[i]; if(T[i-1] < T[i]) { hMinT = T[i]; } long hMinA = 1; long hMaxA = A[i]; if(A[i] > A[i+1]) { hMinA = A[i]; } ans *= max(0, (min(hMaxT, hMaxA) - max(hMinT, hMinA) + 1)); ans %= p; } writeln(ans); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto color=new char[](n*2), a=new int[](n*2); auto pos_w=new int[](n*2), pos_b=new int[](n*2); fill(pos_w, -1); fill(pos_b, -1); foreach(i; 0..(n*2)){ rd(color[i], a[i]); a[i]--; if(color[i]=='W') pos_w[a[i]]=i; else pos_b[a[i]]=i; } auto cnt_w=new int[][](n*2, n*2), // [i, n*2)にあるj以下の数の個数(白色の) cnt_b=new int[][](n*2, n*2); // auto cnt=new int[][](n*2, n*2); for(int i=n*2-1; i>=0; i--){ for(int h=0; h<=i; h++){ if(color[i]=='W') cnt_w[h][a[i]]++; else cnt_b[h][a[i]]++; // cnt[h][a[i]]++; } } foreach(i; 0..(n*2)){ for(int j=1; j<(n*2); j++){ cnt_w[i][j]+=cnt_w[i][j-1]; cnt_b[i][j]+=cnt_b[i][j-1]; // cnt[i][j]+=cnt[i][j-1]; } } // writeln(cnt_w); // writeln(cnt_b); auto dp=new int[][](n+1, n+1); for(int i=0; i<=n; i++) fill(dp[i], 1_000_000_000); dp[0][0]=0; for(int i=0; i<=n; i++)for(int j=0; j<=n; j++){ // 白:0, 1, ..., i-1まで置いた;次置くのはi if(i<n){ int cost_w=0; if(i-1>=0) cost_w+=cnt_w[pos_w[i]][i-1]; if(j-1>=0) cost_w+=cnt_b[pos_w[i]][j-1]; chmin(dp[i+1][j], dp[i][j]+cost_w); } if(j<n){ int cost_b=0; if(j-1>=0) cost_b+=cnt_b[pos_b[j]][j-1]; if(i-1>=0) cost_b+=cnt_w[pos_b[j]][i-1]; chmin(dp[i][j+1], dp[i][j]+cost_b); } } writeln(dp[n][n]); // writeln(dp); } void chmin(T)(ref T l, T r){if(l>r)l=r;} 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[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { auto S = sread(); long K = lread(); long one; while (S[one] == '1') one++; if (K <= one) { writeln(1); return; } writeln(S[one]); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; int N; uint[100] FS; int[11][100] PS; int bit_count(uint i) { i = i - ((i >>> 1) & 0x55555555); i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); i = (i + (i >>> 4)) & 0x0f0f0f0f; i = i + (i >>> 8); i = i + (i >>> 16); return i & 0x3f; } void main() { N = readln.chomp.to!int; foreach (i; 0..N) { auto f = readln.split.to!(int[]); foreach (j, b; f) if (b) FS[i] |= (1<<j); } foreach (i; 0..N) PS[i][] = readln.split.to!(int[])[]; int solve(int i, uint key) { if (i == 10) { if (!key) return int.min; int sum_c; foreach (j; 0..N) sum_c += PS[j][bit_count(FS[j] & key)]; return sum_c; } else { return max(solve(i+1, key), solve(i+1, key | (1<<i))); } } writeln(solve(0, 0)); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; long P = 10^^9+7; long[10^^5*2+10] F, RF; void init() { F[0] = F[1] = 1; foreach (i, ref x; F[2..$]) x = (F[i+1] * (i+2)) % P; { RF[$-1] = 1; auto x = F[$-1]; auto k = P-2; while (k) { if (k%2 == 1) RF[$-1] = (RF[$-1] * x) % P; x = x^^2 % P; k /= 2; } } foreach_reverse(i, ref x; RF[0..$-1]) x = (RF[i+1] * (i+1)) % P; } long comb(N)(N n, N k) { auto n_b = F[n]; // n! auto nk_b = RF[n-k]; // 1 / (n-k)! auto k_b = RF[k]; // 1 / k! auto nk_b_k_b = (nk_b * k_b) % P; // 1 / (n-k)!k! return (n_b * nk_b_k_b) % P; // n! / (n-k)!k! } void main() { init(); auto nmk = readln.split.to!(long[]); auto N = nmk[0]; auto M = nmk[1]; auto K = nmk[2]; long r; foreach (long d; 1..N) { auto x = (((N-d)*M^^2)%P*d)%P; if (K == 2) { r = (r+x)%P; } else { auto y = comb(N*M-2, K-2); r = (r + (x*y)%P)%P; } } foreach (long d; 1..M) { auto x = (((M-d)*N^^2)%P*d)%P; if (K == 2) { r = (r+x)%P; } else { auto y = comb(N*M-2, K-2); r = (r + (x*y)%P)%P; } } 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; void main() { auto N = readln.chomp.to!int; string d1 = "..##"; string d2 = ".#.#"; string r1 = ""; string r2 = ""; bool right_end = false; while (true) { bool t = false; foreach (k; 0..4) { if (right_end) { r1 = d1[k] ~ r1; r2 = d2[k] ~ r2; } else { r1 = r1 ~ d1[k]; r2 = r2 ~ d2[k]; } writeln(r1); writeln(r2); stdout.flush; string tf = readln.chomp; if (tf == "T") { t = true; break; } else if (tf == "end") return; if (right_end) { r1 = r1[1..$]; r2 = r2[1..$]; } else { r1 = r1[0..$-1]; r2 = r2[0..$-1]; } } if (!t) { right_end = true; N += 1; } } }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; void main() { writeln(readln.chomp.equal(readln.chomp.retro) ? "YES" : "NO"); } // ---------------------------------------------- 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,std.conv,std.string,std.algorithm,std.array; void main(){ auto s=readln().chomp().split().map!(to!int); int a=s[0],b=s[1]; if(a<=8&&b<=8) writeln("Yay!"); else writeln(":("); }
D
// import chie template :) {{{ import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons; // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string : chomp; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.chomp.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } } // }}} void main() { auto cin = new Scanner; int a, b, x; cin.scan(a, b, x); if (a <= x && a + b >= x) { writeln("YES"); } else { writeln("NO"); } }
D
import std.stdio; import std.algorithm; import std.string; import std.conv; import std.math; void main(){ while(true){ int n = to!int(chomp(readln())); if(n == 0) break; int ans = 0; int nf=8; while(nf>0){ ans += n / pow(5,nf) ; nf--; } writeln(ans); } }
D
import std.stdio; import std.algorithm; import std.container; import core.stdc.stdio; import std.typecons; int n,k; int[][] sg; int[][] graph; int[2][] queue; bool[] visited; int gSize=0; void buildGraph(int idx,int r){ visited[] = false; int qc=0; visited[idx]=true; foreach(c;sg[idx]){ queue[qc++] = [1,c]; visited[c]=true; } for(int i=0;i<qc;i++){ int s=queue[i][0]; int p=queue[i][1]; if(s>r) return; graph[idx] ~= p; gSize++; foreach(c;sg[p]){ if(!visited[c]){ visited[c] = true; queue[qc++] = [s+1,c]; } } } } void main(){ scanf("%d%d",&n,&k); graph = new int[][n]; sg = new int[][n]; int[] cs=new int[n]; int[] rs=new int[n]; queue = new int[2][n]; visited = new bool[n]; for(int i=0;i<n;i++){ scanf("%d%d",&cs[i],&rs[i]); } for(int i=0;i<k;i++){ int a,b; scanf("%d%d",&a,&b); sg[--a] ~= --b; sg[b] ~= a; } for(int i=0;i<n;i++){ buildGraph(i,rs[i]); } visited[] = false; alias Tuple!(int,"cost",int,"idx") ci; alias BinaryHeap!(ci[],"a>b") pq; pq q = pq(new ci[gSize+1],0); q.insert(ci(0,0)); int ans; while(1){ ci s = q.front; q.removeFront; if(s.idx==n-1){ ans=s.cost; break; } if(visited[s.idx]) continue; visited[s.idx]=true; foreach(c;graph[s.idx]){ if(!visited[c]){ q.insert(ci(s.cost+cs[s.idx],c)); } } } printf("%d\n",ans); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto s = readln.chomp; auto cnt = s.count!(a=>a=='o'); auto d = 15 - s.length; if (cnt + d >= 8) { 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; 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 = lread(); long n = 1; foreach (i; 2 .. 32) { long j = 2; while (i ^^ j <= X) { n.maxAssign(i ^^ j); j++; } } writeln(n); }
D
import std; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N, K; scan(N, K); auto C = new long[](K + 1); long ans; foreach_reverse (x; 1 .. K + 1) { C[x] = powmod(K / x, N, MOD); long i = 2; while (x * i <= K) { C[x] = (MOD - C[x * i] + C[x]) % MOD; i++; } ans = (ans + C[x] * x) % MOD; } writeln(ans); } /// x^^n % m T powmod(T = long)(T x, T n, T m = 10 ^^ 9 + 7) { 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; }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional, std.traits; import std.algorithm, std.container; import core.stdc.stdlib, core.bitop; enum MOD=pow(10,9)+7; void main() { auto S = scanString; long[2][] dp; dp.length = S.length+1; dp[0][1]=1; foreach(long i, b; S) { i++; if(b=='1') { dp[i][0] = dp[i-1][0]*3+dp[i-1][1]; dp[i][1] = dp[i-1][1]*2; }else{ dp[i][0] = dp[i-1][0]*3; dp[i][1] = dp[i-1][1]; } dp[i][0] %= MOD; dp[i][1] %= MOD; } writeln((dp[$-1][0]+dp[$-1][1])%MOD); } long gcd(long a, long b) { if(b == 0) return a; return gcd(b, a % b); } class UnionFind{ UnionFind parent = null; void merge(UnionFind a) { if(same(a)) return; a.root.parent = this.root; } UnionFind root() { if(parent is null)return this; return parent = parent.root; } bool same(UnionFind a) { return this.root == a.root; } } string scanString() { return scanElem!string; } void scanValues(TList...)(ref TList list) { auto lit = readln.splitter; foreach (ref e; list) { e = lit.fornt.to!(typeof(e)); lit.popFront; } } T[] scanArray(T = long)() { return readln.split.to!(T[]); } void scanStructs(T)(ref T[] t, size_t n) { t.length = n; foreach (ref e; t) { auto line = readln.split; foreach (i, ref v; e.tupleof) { v = line[i].to!(typeof(v)); } } } long scanULong(){ long x; while(true){ const c = getchar; if(c<'0'||c>'9'){ break; } x = x*10+c-'0'; } return x; } T scanElem(T = long)() { char[] res; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { res ~= cast(char) c; c = getchar; } return res.strip.to!T; } template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } struct Factor { long n; long c; } //素因数分解 Factor[] factors(long n) { Factor[] res; for (long i = 2; i ^^ 2 <= n; i++) { if (n % i != 0) continue; int c; while (n % i == 0) { n = n / i; c++; } res ~= Factor(i, c); } if (n != 1) res ~= Factor(n, 1); return res; } //約数をすべて列挙 long[] divisors(long n) { long[] list; void func(Factor[] fs, long n) { if(fs.empty){ list ~= n; return; } foreach(c; 0..fs[0].c+1) { func(fs[1..$], n * (fs[0].n ^^ c)); } } func(factors(n), 1); sort(list); return list; } //nまでの素数のリスト long[] primes(long n) { if(n<2)return []; auto table = new long[n+1]; long[] res; for(int i = 2;i<=n;i++) { if(table[i]==-1) continue; for(int a = i;a<table.length;a+=i) { table[a] = -1; } res ~= i; } return res; } //素数判定 bool isPrime(long n) { if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (long i = 3; i ^^ 2 <= n; i += 2) if (n % i == 0) return false; return true; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, core.bitop; void main() { auto hwk = readln.split.to!(int[]); auto H = hwk[0]; auto W = hwk[1]; auto K = hwk[2]; char[][] C; foreach (_; 0..H) C ~= readln.chomp.to!(char[]); int r = int.max; foreach (x; 0..1<<(H-1)) { auto N = popcnt(x)+1; int[] cs, ccs; cs.length = N; ccs.length = N; int rr; foreach (j; 0..W) { { int k; foreach (i; 0..H) { if (C[i][j] == '1') ++ccs[k]; if (x & (1<<i)) ++k; } } foreach (k; 0..N) if (cs[k] + ccs[k] > K) { if (ccs[k] > K) goto ng; ++rr; cs[] = 0; break; } foreach (k; 0..N) cs[k] += ccs[k]; ccs[] = 0; } r = min(r, rr + N-1); ng: } writeln(r); }
D
import std.stdio; import std.string; import std.conv; void main() { auto line = chomp(readln()).split(" "); int H = to!int(line[0]), W = to!int(line[1]); while(H || W) { writeln(repeat("#", W)); foreach(int i; 2 .. H) { writeln("#" ~ repeat(".", W - 2) ~ "#"); } writeln(repeat("#", W)); writeln(); line = chomp(readln()).split(" "); H = to!int(line[0]); W = to!int(line[1]); } } string repeat(string s, int n) { string r; while(n) { if(n % 2) { r ~= s; } n >>= 1; s ~= s; } return r; }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random; import std.typecons, std.functional, std.traits,std.concurrency; import std.algorithm, std.container; import core.bitop, core.time, core.memory; import std.bitmanip; import std.regex; enum INF = long.max/3; enum MOD = 10L^^9+7; //辞書順順列はiota(1,N),nextPermituionを使う void end(T)(T v) if(isIntegral!T||isSomeString!T||isSomeChar!T) { import core.stdc.stdlib; writeln(v); exit(0); } T[] scanArray(T = long)() { static char[] scanBuf; readln(scanBuf); return scanBuf.split.to!(T[]); } dchar scanChar() { int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } return cast(dchar)c; } T scanElem(T = long)() { import core.stdc.stdlib; static auto scanBuf = appender!(char[])([]); scanBuf.clear; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { scanBuf ~= cast(char) c; c = getchar; } return scanBuf.data.to!T; } dchar[] scanString(){ return scanElem!(dchar[]); } void main() { auto n=scanElem; (n*(n+1)/2).end(); }
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(); long A, B; scan(A, B); auto P = aryread(); long a, b, c; foreach (p; P) { if (p < A + 1) { a++; } else if (B < p) { c++; } else { b++; } } min(a, b, c).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; auto A = RDR.ARR; auto b = new long[](N+1); foreach_reverse (i; 0..N) { b[i] = gcd(b[i+1], A[i]); } auto c = new long[](N+1); long x; long ans; foreach (i; 0..N) { c[i] = gcd(b[i+1], x); x = gcd(x, A[i]); ans = max(ans, c[i]); } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } void main() { auto H = readln.chomp.to!(int); auto W = readln.chomp.to!(int); auto N = readln.chomp.to!(int); auto sizePerPaint = H > W ? H : W; writeln(N / sizePerPaint + (N % sizePerPaint == 0 ? 0 : 1)); }
D
// import chie template :) {{{ import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons; // }}} // 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 scan()() {} void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } } // }}} void main() { auto cin = new Scanner; int w, h; dchar c; cin.scan(w, h, c); writeln("+" ~ "-".repeat(w - 2).join ~ "+"); foreach (i; 0 .. h - 2) { if (i == (h - 2) / 2) { writeln("|" ~ ".".repeat((w - 2) / 2).join ~ cast(char)c ~ ".".repeat((w - 2) / 2).join ~ "|"); } else { writeln("|" ~ ".".repeat(w - 2).join ~ "|"); } } writeln("+" ~ "-".repeat(w - 2).join ~ "+"); }
D
import std.stdio; import std.string; import std.conv; import std.range; import std.algorithm; void main() { int count = 0; string input; while ((input = readln.chomp).length != 0) { if (hantei(input)) count++; } writeln(count); } bool hantei(string input) { for (int i = 0; i < (input.length / 2); i++) { if (input[i] != input[(input.length - 1) - i]) return false; } return true; }
D
void main() { auto s = rs; writeln(s[0], s.length-2, s[$-1]); } // =================================== import std.stdio; import std.string; import std.functional; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
void main() { int[] abc = readln.split.to!(int[]); writeln(abc.sum - abc.reduce!max); } 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; 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 r = readln.chomp.to!int; if (r < 1200) { writeln("ABC"); } else if (r < 2800) { writeln("ARC"); } else { writeln("AGC"); } }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto s = readln.chomp.dup; s.reverse(); writeln(s); }
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; void main() { long n, m; scan(n, m); auto s = readln.chomp; auto t = readln.chomp; auto l = lcm(n, m); auto sg = l / m; auto tg = l / n; foreach (i ; 0 .. n / sg) { if (s[i*sg] != t[i*tg]) { writeln(-1); return; } } writeln(l); } long lcm(long a, long b) { return a / gcd(a, b) * b; } 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, std.string, std.conv; void main() { auto x = readln.chomp.to!int; int sum, i; while (x > sum) sum += ++i; writeln(i); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto K = RD; auto R = RD; auto S = RD; auto P = RD; auto T = RD!string; long ans; auto hand = ['r':'p', 's':'r', 'p':'s']; auto point = ['r':R, 's':S, 'p':P]; auto a = new char[](N); foreach_reverse (i; 0..N) { if (i+K >= N) { a[i] = hand[T[i]]; ans += point[a[i]]; } else { auto h = hand[T[i]]; if (a[i+K] != h) { a[i] = h; ans += point[h]; } } } writeln(ans); stdout.flush; debug readln; }
D
import std.stdio; import std.string; import std.conv; void main() { auto M = readln.chomp.to!int; auto x = 48-M; writeln(x); }
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; void main() { auto s = readln.chomp; write(s); if (s[$ - 1] == 's') { writeln("es"); } else { writeln("s"); } }
D
import std.stdio, std.string, std.conv, std.algorithm; void main() { int[string] memo; foreach(str; ["A", "B", "AB", "O"]) { memo[str] = 0; } while(true) { auto line = readln; if(!line)break; auto f = line.chomp.split(","); ++memo[f[1]]; } foreach(str; ["A", "B", "AB", "O"]) { memo[str].writeln; } }
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 readInt() { return readInts()[0]; } auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } void main() { auto n = readInt(); auto a = readLongs(); auto arev = a.dup; arev.reverse; a = -1 ~ a; arev = -1 ~ arev; int[] s = [0]; auto t = new int[](n); auto u = new int[](n); foreach(i; 0..n) { while(!(a[s[$-1]] < a[i+1])) { s = s[0..$-1]; } t[i] = i-s[$-1]; s ~= i+1; } s = [0]; foreach(i; 0..n) { while(!(arev[s[$-1]] < arev[i+1])) { s = s[0..$-1]; } u[n-i-1] = i-s[$-1]; s ~= i+1; } auto si = new long[](n); foreach(i; 0..n) { auto l = t[i]+u[i]+1; si[l-1] = max(si[l-1], a[i+1]); } foreach_reverse(i; 0..n-1) { si[i] = max(si[i+1], si[i]); } foreach(i; 0..n) { writeln(si[i]); } }
D
import std.stdio, std.string, std.conv, std.regex; void main() { auto n = readln.chomp; if(n[0] == '9' || n[1] == '9'){ writeln("Yes"); } else { writeln("No"); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; long P = 10^^9+7; long[10^^5+50] F, RF; long pow(long x, long n) { long y = 1; while (n) { if (n%2 == 1) y = (y * x) % P; x = x^^2 % P; n /= 2; } return y; } long inv(long x) { return pow(x, P-2); } void init() { F[0] = F[1] = 1; foreach (i, ref x; F[2..$]) x = (F[i+1] * (i+2)) % P; { RF[$-1] = 1; auto x = F[$-1]; auto k = P-2; while (k) { if (k%2 == 1) RF[$-1] = (RF[$-1] * x) % P; x = x^^2 % P; k /= 2; } } foreach_reverse(i, ref x; RF[0..$-1]) x = (RF[i+1] * (i+1)) % P; } void main() { init(); auto N = readln.chomp.to!long; auto xs = readln.split.to!(long[]); long r, x = 1; foreach (i; 0..N-1) { r = ((((xs[i+1] - xs[i]) * x) % P * F[N-1]) % P + r) % P; x = (x + inv(i+2)) % P; } writeln(r); }
D
// Vicfred // https://atcoder.jp/contests/abc159/tasks/abc159_b // string manipulation import std.stdio; import std.string; bool palindrome(string s) { long n = s.length; for(long i = 0; i < n/2; i++) if(s[i] != s[$-i-1]) return false; return true; } void main() { string s = readln.chomp; long n = s.length; if(s[0..(n-1)/2].palindrome && s[(n+1)/2..$].palindrome && s.palindrome) "Yes".writeln; else "No".writeln; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm, std.math; long q, h, s, d; rd(q, h, s, d); long n; rd(n); s=min(s, 2*h, 4*q); long c=n*s; if(n%2==0){ c=min(c, n/2*d); }else{ c=min(c, s+(n-1)/2*d); } writeln(c); } 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
long solve(long a, long b, long h){ return (a+b)*h/2; } void main(){ int a = inelm(); int b = inelm(); int h = inelm(); solve(a, b, h).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.string; import std.conv; int main() { int line = readln.chomp.to!int; int n = readln.chomp.to!int; int[31] values; foreach(int i;0..line) { values[i] = i+1; } foreach(int i;0..n) { auto input = readln.chomp.split(","); int a = to!int(input[0]); int b = to!int(input[1]); int value = values[a-1]; values[a-1] = values[b-1]; values[b-1] = value; } foreach(int i;0..line) { writeln(values[i]); } return 0; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto a=readln.split.to!(long[]); long tot=0; for(auto i=0, j=0, s=0L; i<n; i++){ while(j<n){ if((s+a[j])==(s^a[j])) s+=a[j], j++; else break; } // [i, j) : valid tot+=(j-i).to!(long); s-=a[i]; } 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.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; long P = 998244353L; long[10^^5*3+50] F, RF; long pow(long x, long n) { long y = 1; while (n) { if (n%2 == 1) y = (y * x) % P; x = x^^2 % P; n /= 2; } return y; } long inv(long x) { return pow(x, P-2); } void init() { F[0] = F[1] = 1; foreach (i, ref x; F[2..$]) x = (F[i+1] * (i+2)) % P; { RF[$-1] = 1; auto x = F[$-1]; auto k = P-2; while (k) { if (k%2 == 1) RF[$-1] = (RF[$-1] * x) % P; x = x^^2 % P; k /= 2; } } foreach_reverse(i, ref x; RF[0..$-1]) x = (RF[i+1] * (i+1)) % P; } long comb(N)(N n, N k) { if (k > n) return 0; auto n_b = F[n]; // n! auto nk_b = RF[n-k]; // 1 / (n-k)! auto k_b = RF[k]; // 1 / k! auto nk_b_k_b = (nk_b * k_b) % P; // 1 / (n-k)!k! return (n_b * nk_b_k_b) % P; // n! / (n-k)!k! } void main() { init(); auto nabk = readln.split.to!(long[]); auto N = nabk[0]; auto A = nabk[1]; auto B = nabk[2]; auto K = nabk[3]; long r; foreach (a; 0..N+1) { auto k = a * A; if (k > K) continue; auto b = (K-k)/B; if (b > N) continue; k += b * B; if (k != K) continue; (r += comb(N, a) * comb(N, b) % P) %= P; } writeln(r); }
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 N, W; scan(N, W); auto w = new int[](N); auto v = new int[](N); iota(N).each!(i => scan(v[i], w[i])); auto dp = new int[](W + 1); foreach (i ; 0 .. N) { foreach (j ; 0 .. W + 1) { if (j - w[i] >= 0) chmax(dp[j], dp[j - w[i]] + v[i]); } } auto ans = dp[W]; writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { while (1) { auto s = readln.chomp; if (s == ".") return; writeln(isBalance(s) ? "yes" : "no"); } } bool isBalance(string s) { auto st = Stack!(char)(200); foreach (ch ; s) { if (ch == '(' || ch == '[') st.push(ch); else if (ch == ')') { if (st.empty || st.top != '(') return false; else st.pop; } else if (ch == ']') { if (st.empty || st.top != '[') return false; else st.pop; } } return st.empty; } struct Stack(T) { private: int N, peek; T[] data; public: this(int size) { N = size; data = new T[](N); } bool empty() @property { return peek == 0; } bool full() @property { return peek == N; } void push(T x) @property { assert(!full); data[peek++] = x; } void pop() @property { assert(!empty); --peek; } T top() @property { return data[peek - 1]; } void clear() @property { peek = 0; } int length() @property { return peek; } } 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; 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() { int maxN = 10^^6; int[] f, f_odd; for(int n=1; ;n++) { f ~= func(n); if (f.back > maxN) break; if (f.back%2!=0) f_odd~= f.back; } int INF = 1<<30; int[] ary = new int[maxN+1]; ary[] = INF; ary[0] = 0; foreach(int i; 0..maxN) { for(int n=0; n<f.length; n++) { if (i+f[n]<=maxN) { ary[i+f[n]] = min(ary[i+f[n]], ary[i]+1); } else { break; } } } int[] _ary = new int[maxN+1]; _ary[] = INF; _ary[0] = 0; foreach(int i; 0..maxN) { for(int n=0; n<f_odd.length; n++) { if (i+f_odd[n]<=maxN) { _ary[i+f_odd[n]] = min(_ary[i+f_odd[n]], _ary[i]+1); } else { break; } } } while(true) { int N = readln.chomp.to!int; if (N==0) break; writeln(ary[N], " ", _ary[N]); } } int func(int n) { return n*(n+1)*(n+2)/6; }
D
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } void main() { try { for (; ; ) { const numCases = readInt(); foreach (caseId; 0 .. numCases) { const A = readLong(); const B = readLong(); const N = readLong(); long ans; switch (N % 3) { case 0: ans = A; break; case 1: ans = B; break; case 2: ans = A ^ B; break; default: assert(false); } writeln(ans); } } } catch (EOFException e) { } }
D
// Cheese-Cracker: cheese-cracker.github.io void theCode(){ ll k = scan; k -= 1; ll rt = to!ll(floor(sqrt(to!double(k)))); if((rt + 1) *(rt + 1) <= k){ rt += 1; } ll lef = k - rt*rt; ll d = min(rt, lef); ll b = max(0, lef - rt); ll x = (rt+1) - b; ll y = d + 1; writeln(y, " ", x); } void main(){ long tests = scan; // Toggle! while(tests--) theCode(); } /********* That's All Folks! *********/ import std.stdio, std.random, std.functional, std.container, std.algorithm; import std.numeric, std.range, std.typecons, std.string, std.math, std.conv; string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;} T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; } void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } } alias ll = long, tup = Tuple!(long, "x", long, "y");
D
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; } struct UnionFind { void init(int n) { par = new int[](n); foreach (i; 0..n) par[i] = i; cnt = new int[](n); fill(cnt, 1); } int root(int i) { return par[i] == i ? i : (par[i] = root(par[i])); } bool same(int i, int j) { return root(i) == root(j); } void unite(int i, int j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; cnt[j] += cnt[i]; } int size(int i) { return cnt[root(i)]; } int[] par, cnt; } void main() { auto H = RD!int; auto W = RD!int; auto s = new string[](H); foreach (i; 0..H) s[i] = RD!string; UnionFind uf; uf.init(H*W); foreach (y; 0..H) { int id = y * W; foreach (x; 0..W-1) { if (s[y][x] != s[y][x+1]) uf.unite(id+x, id+x+1); } if (y != H-1) { foreach (x; 0..W) { if (s[y][x] != s[y+1][x]) uf.unite(id+x, id+x+W); } } } auto cnt = new long[][](H*W, 2); foreach (y; 0..H) { foreach (x; 0..W) { auto r = uf.root(y*W+x); auto pos = s[y][x] == '#' ? 1 : 0; ++cnt[r][pos]; } } long ans; foreach (i; 0..cnt.length) { ans += cnt[i][0] * cnt[i][1]; } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { int a,b; scan(a,b); a %= 3, b %= 3; writeln(a == b && a != 0 ? "Impossible" : "Possible"); } 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
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto a=readln.split.to!(long[]); auto dp=new int[](n); dp[0]=1; foreach(i; 1..n){ dp[i]=1; if(a[i]<=a[i-1]*2){ dp[i]=max(dp[i], dp[i-1]+1); } } writeln(dp.reduce!(max)); } 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
void main() { int n = readln.chomp.to!int; int a = readln.chomp.to!int; writeln(n % 500 <= a ? "Yes" : "No"); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio; void main() { auto N = readln.chomp.to!int; auto A = readln.chomp; if (A.front == 'W' || A.back == 'W') { writeln(0); return; } immutable long MOD = 10^^9 + 7; long ans = 1; long cnt = 0; foreach (a; A) { if (cnt == 0 && a == 'W') { writeln(0); return; } else if (cnt == 0) { cnt += 1; } else if (a == 'W') { if (cnt % 2 == 1) { cnt += 1; } else { ans = ans * cnt % MOD; cnt -= 1; } } else { if (cnt % 2 == 1) { ans = ans * cnt % MOD; cnt -= 1; } else { cnt += 1; } } } if (cnt != 0) { writeln(0); } else { foreach (i; 1..N+1) ans = ans * i % MOD; writeln(ans); } }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; auto s=readln.chomp.to!(char[]); s[3]='8'; writeln(s); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv, std.exception; auto l=readln.split; enforce(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
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; void main() { int n, a, b, c; scan(n, a, b, c); int ans = n - (a + b - c); 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; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { auto xs = readints; writeln(xs[0] ^ xs[1] ^ xs[2]); }
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 ds = new int[][](n, 1); auto n2s = iota(n).array; foreach(i ; 0 .. n){ ds[i][0] = i; } foreach(lp ; 0 .. q){ rd = readln.split.to!(int[]); auto c = rd[0], x = rd[1], y = rd[2]; if(c == 0){ merge(ds, n2s, x, y); } else{ writeln(n2s[x] == n2s[y] ? 1 : 0); } } //writefln("%(%s\n%)", ds); } void merge(int[][] ds, int[] n2s, int x, int y){ auto u = n2s[x]; auto v = n2s[y]; if(u == v) return; if(ds[u].length > ds[v].length) swap(u, v); ds[v] ~= ds[u]; foreach(z ; ds[u]){ n2s[z] = v; } destroy(ds[u]); }
D
import std.stdio; import std.regex; void main(){ auto io = new IO(); auto N = io.line()[0]; auto A = io.line(N); size_t paired = 0; size_t surplus = 0; foreach( i,a ; A ){ if( surplus == 1 ){ paired += (a+1)/2; surplus = ( (a+1)/2 ) ? (a+1)%2 : 0 ; }else{ paired += a/2; surplus = a%2; } //writeln( i,",",a,":",paired," ",surplus ); } paired.writeln(); return; } import std.stdio,std.conv,std.string; import std.algorithm,std.array,std.math; class IO { T[] line( T = size_t , string token = " " )( size_t m = 1 ){ T[] arr = []; foreach( i ; 0..m ){ arr ~= this.read!T(); } return arr; } T[][] rect( T = size_t , string token = " " )( size_t m = 1 ){ T[][] arr = new T[][](m); foreach( i ; 0..m ){ arr[i] = this.read!T(token); } return arr; } private T[] read( T = size_t )( string token = " " ){ T[] arr; foreach( elm ; readln().chomp().split(token) ){ arr ~= elm.to!T(); } return arr; } } // T -> T[] pure T[] step( T = size_t )( in T begin , in T end , in T step = 1 ){ T[] ret; for( T i = begin ; i < end ; i += step ){ ret ~= i; } return ret; } unittest{ assert( step(0,10,2) == [0,2,4,6,8] ); } // T[] -> T[] pure T[] fill( T )( T[] args , in T filling ){ foreach( ref arg ; args ){ arg = filling; } return args; } T[] map( T )( T[] args , T function(T) f ){ foreach( ref arg ; args ){ arg = f(arg); } return args; } unittest{ assert( [true,false,true,false].fill(true) == [true,true,true,true] ); assert( [1,-2,3,-4].map!int(x=>x*2) == [2,-4,6,-8] ); } // T[] -> number pure T sum( T )( in T[] args ){ T ret = 0; foreach( i ; 0..args.length ){ ret += args[i]; } return ret; } pure T ave(T)( in T[] args ){ return args.sum()/args.length; } pure real multi( in real[] args ){ real ret = 1.0; foreach( i ; 0..args.length ){ ret *= args[i]; } return ret; } // T[] -> bool pure bool any( T )( in T[] args , in T target ){ foreach( arg ; args ){ if( arg == target ){return true;} } return false; } pure bool all( T )( in T[] args , in T cond ){ foreach( arg ; args ){ if( arg != cond ){return false;} } return true; } pure bool have( T )( in T[] args , in T[] target ... ){ bool[] found = new bool[](target.length); foreach( arg ; args ){ foreach( i,t ; target ){ if( arg == t ){ found[i] = true; } } } foreach( f ; found ){ if( f == false ){return false;} } return true; } unittest{ assert( [1,2,3,4,5].sum() == 15 ); assert( [1,2,3,4,5].ave() == 3 ); assert( cmp( [0.3,0.3].multi() , 0.09 ) , "multi() test failed" ); assert( cmp( [0.3,0.3,0.3].multi() , 0.027 ) , "multi() test failed" ); assert( [1,1,1].all(1) == true ); assert( [1,1,2].all(1) == false ); assert( [1,1,2].any(2) == true ); assert( [1,1,2].any(3) == false ); assert( [1,2,3,4,5].have([1,3,5]) == true ); assert( [1,2,3,4,5].have([1,3,5,7]) == false ); }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop; // dfmt on void main() { auto S = sread(); bool f(string s) { return s.length % 2 == 0 && s[0 .. $ / 2] == s[$ / 2 .. $]; } foreach_reverse (i; 0 .. S.length) { if (f(S[0 .. i])) { writeln(i); return; } } }
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 s = readln.chomp.replace("C", "A").replace("G", "A").replace("T", "A"); int res, cnt; foreach (e; s) { if (e == 'A') { cnt++; } else { res = max(res, cnt); cnt = 0; } } res = max(res, cnt); res.writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto AS = readln.split.to!(long[]); auto BS = readln.split.to!(long[]); long r; foreach (i; 0..N) { if (BS[i] <= AS[i]) { r += BS[i]; } else { r += AS[i]; auto d = BS[i] - AS[i]; if (AS[i+1] < d) { r += AS[i+1]; AS[i+1] = 0; } else { r += d; AS[i+1] -= d; } } } writeln(r); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.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 = RD!string; long cnt0, cnt1; foreach (i; 0..n) { if (s[i] == '<') ++cnt0; else if (s[i] == '>') ++cnt1; if (s[(n+i-1)%n] == '-' || s[i] == '-') ++ans[ti]; } if (cnt0 == 0 || cnt1 == 0) ans[ti] = n; } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio; import std.algorithm; import std.conv; import std.datetime; import std.numeric; import std.math; import std.string; string my_readln() { return chomp(readln()); } void main() { auto tokens = split(my_readln()); auto S = to!string(tokens[0]); if (S.length == 2) writeln(S); else { foreach_reverse(e; S) write(e); writeln(); } 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; import std.container : DList; 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 list = new LinkedList(); int q = readint; for (int i = 0; i < q; i++) { auto qu = readints; switch (qu[0]) { case 0: // insert list.insert(qu[1]); break; case 1: // move if (qu[1] >= 0) list.moveNext(qu[1]); else list.movePrev(-qu[1]); break; case 2: // erase list.erase(); break; default: break; } } list.dump(); } class Node { int value; Node next; Node prev; this(int value) { this.value = value; } } class LinkedList { private Node _sentinel; private Node _cursor; this() { _sentinel = new Node(int.max); _cursor = _sentinel; } // カーソル位置の直前に新しい要素を追加する。カーソルはその要素を指す void insert(int value) { auto x = new Node(value); auto prev = _cursor.prev; if (prev !is null) { prev.next = x; x.prev = prev; } _cursor.prev = x; x.next = _cursor; _cursor = x; } // カーソルを d だけ進める void moveNext(int d) { for (int i = 0; i < d; i++) { _cursor = _cursor.next; } } // カーソルを d だけ戻す void movePrev(int d) { for (int i = 0; i < d; i++) { _cursor = _cursor.prev; } } // カーソル位置の要素を消す void erase() { auto prev = _cursor.prev; auto next = _cursor.next; if (prev !is null) prev.next = next; next.prev = prev; _cursor = next; } void dump() { auto x = _cursor; while (x.prev !is null) x = x.prev; while (x !is _sentinel) { writeln(x.value); x = x.next; } } }
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 X = RD; long ans, y; while (y != 360) { y %= 360; y += X; ++ans; } writeln(ans); stdout.flush; debug readln; }
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 A, B, M; scan(A, B, M); auto a = readln.split.to!(int[]); auto b = readln.split.to!(int[]); int ans = a.reduce!min + b.reduce!min; foreach (i ; 0 .. M) { int xi, yi, ci; scan(xi, yi, ci); xi--, yi--; chmin(ans, a[xi] + b[yi] - ci); } assert(ans >= 0); 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; }
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.chomp; auto T = readln.chomp; auto N = S.length.to!int; auto M = T.length.to!int; auto st = new SegmentTree(N); auto tt = new SegmentTree(M); foreach (i; 0..N) if (S[i] == 'A') st.add(i, 1); foreach (i; 0..M) if (T[i] == 'A') tt.add(i, 1); auto Q = readln.chomp.to!int; foreach (q; 0..Q) { auto s = readln.split.map!(to!int); auto a = s[0]-1; auto b = s[1]-1; auto c = s[2]-1; auto d = s[3]-1; auto n = b - a + 1; auto m = d - c + 1; auto sa = st.sum(a, b); auto sb = n - sa; auto ta = tt.sum(c, d); auto tb = m - ta; sa += sb * 2; ta += tb * 2; writeln((sa-ta)%3==0 ? "YES" : "NO"); } } class SegmentTree { int[] table; int size; this(int n) { assert(bsr(n) < 29); size = 1 << (bsr(n) + 2); table = new int[](size); } void add(int pos, int num) { return add(pos, num, 0, 0, size/2-1); } void add(int pos, int num, int i, int left, int right) { table[i] += num; if (left == right) return; auto mid = (left + right) / 2; if (pos <= mid) add(pos, num, i*2+1, left, mid); else add(pos, num, i*2+2, mid+1, right); } int sum(int pl, int pr) { return sum(pl, pr, 0, 0, size/2-1); } int sum(int pl, int pr, int i, int left, int right) { if (pl > right || pr < left) return 0; else if (pl <= left && right <= pr) return table[i]; else return sum(pl, pr, i*2+1, left, (left+right)/2) + sum(pl, pr, i*2+2, (left+right)/2+1, right); } }
D
import std.stdio; import std.range; import std.array; import std.algorithm; import std.string; import std.conv; void main(){ auto num = readln.chomp.to!int; string[] persons; for(auto i = 0; i < num; i++){ persons ~= readln.chomp; } long[string] groups; groups.divide_march(persons); auto combinations = [ "MAR","MAC","MAH","MRC","MRH","MCH", "ARC","ARH","ACH", "RCH" ]; long answer = 0; combinations.each!((comb){ long temp = groups[""~comb[0]] * groups[""~comb[1]] * groups[""~comb[2]]; answer += temp; }); writeln(answer); } void divide_march(ref long[string] groups, string[] persons){ "MARCH".each!((c){ auto index = ""~c; groups[index] = persons.filter!((p) => p[0] == c).array.length; }); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto s = readln.chomp; writeln(s.replace(",", " ")); }
D
import std.stdio; import std.string; import std.conv; import std.range; import std.algorithm; import std.math; import std.regex; void main() { auto ip = readln.split.to!(int[]), a = ip[0], b = ip[1]; ((a*b)&1 ? "Odd" : "Even").writeln; }
D
import std.array : split; import std.conv : to; import std.stdio; import std.string : strip; private { string[] temp; } void main() { read(); int n = get!int(0); int count = 0; foreach (i; 1 .. n + 1) { count += (n - (i - 1)) * i - i + 1; } stdout.write(count); } void read() { temp = split(strip(stdin.readln())); } T get(T)(int p) { return to!(T)(temp[p]); }
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 Pair = Tuple!(long, "x", long, "y", long, "cost"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { auto k = lread(); auto q = new DList!long(iota(1, 10).array()); foreach (_; iota(k - 1)) { auto t = q.front(); q.removeFront(); auto res = t % 10; if (res) q.insertBack(10 * t + res - 1); q.insertBack(10 * t + res); if (res < 9) q.insertBack(10 * t + res + 1); } q.front.writeln(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.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() { auto m = 10^^5+1, p = primes(m); auto pb = new bool[](m); foreach (pi; p) pb[pi] = true; auto b = new int[](m); foreach (i; 1..m) b[i] = pb[i] && pb[(i+1)/2]; auto bc = cumulativeSum(b); int q; readV(q); foreach (_; 0..q) { int l, r; readV(l, r); writeln(bc[l..r+1]); } } 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); } pure T[] primes(T)(T n) { import std.algorithm, std.bitmanip, std.conv, std.range; auto sieve = BitArray(); sieve.length((n+1)/2); sieve = ~sieve; foreach (p; 1..((nsqrt(n)-1)/2+1)) if (sieve[p]) for (auto q = p*3+1; q < (n+1)/2; q += p*2+1) sieve[q] = false; auto r = sieve.bitsSet.map!(to!T).map!("a*2+1").array; r[0] = 2; return r; } pure T factor(T)(T n, const T[] p) { auto ma = nsqrt(n)+1; foreach (pi; p) if (pi > ma) return n; else if (n%pi == 0) return pi; return n; } pure T nsqrt(T)(T n) { import std.algorithm, std.conv, std.range, core.bitop; if (n <= 1) return n; T m = 1 << (n.bsr/2+1); return iota(1, m).map!"a*a".assumeSorted!"a <= b".lowerBound(n).length.to!T; }
D
import std.stdio, std.string, std.conv, std.algorithm; void main() { int n = readln.chomp.to!int; foreach (i; 1 .. n+1) { if (i % 3 == 0 || i.to!string.canFind("3")) write(" ", i); } writeln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!long).array; immutable long MOD = 10^^9 + 7; long ans = 1; long pos = 1; long cnt = 0; foreach (i; 0..N) { if (A[i] >= pos) { cnt += 1; pos += 2; } else { ans = ans * (cnt + 1) % MOD; } } while (cnt) { ans = ans * cnt % MOD; cnt -= 1; } ans.writeln; }
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(){ long x = readln.chomp.to!long; long i; for(i = 0; i * (i + 1) / 2 < x; i ++){ } i.writeln; }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop; // dfmt on void main() { auto S = sread(); DList!char stack; foreach (i, c; S) { if (!stack.empty && stack.back == 'S' && c == 'T') { stack.removeBack(); } else { stack.insertBack(c); } } long ans; foreach (_; stack) { ans++; } writeln(ans); }
D
import std.stdio, std.conv, std.string; import std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } int DEBUG_LEVEL = 0; void print()(){ writeln(""); } void print(T, A ...)(T t, lazy A a){ write(t), print(a); } void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); } void main(string[] args){ if(args.length > 1 && args[1] == "-debug"){ if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int; else DEBUG_LEVEL = 1; } int n = read.to!int; int[] bs; foreach(i; 0 .. n) bs ~= read.to!int - 1; int[] ans; bool f = 0; while(1){ f = 0; foreach_reverse(i; 0 .. n){ if(bs[i] == i){ foreach(j; i .. n - 1) bs[j] = bs[j + 1]; bs[n - 1] = -1; ans ~= i + 1; f = 1; break; } } if(! f) break; } if(ans.length == bs.length) foreach_reverse(a; ans) a.writeln; else "-1".writeln; } /* 逆順で考える。 「i(N ... 1)回目の操作では、長さiの列があって、j番目にあるjを除去する」 注意すべきは、除去が進むとどんどん位置は先頭方向にずれていく 一旦、位置<数 になったものは、もう取れなくなる だからもし候補がいくつかあったら、一番後方にあるものを優先して取る 実際に配列から抜き取りをすると遅くなる? N=100だから大丈夫 */
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.to!(int[]); } long calc(long x, long y) { if (x % y == 0) return -1; for (int i = 2; ; i++) { if (y % i != 0) return x * i; } } void main() { auto xy = readints; int x = xy[0], y = xy[1]; writeln(calc(x, y)); }
D
#!/usr/bin/rdmd import std.stdio; import std.conv: to; import std.array: split; import std.ascii: newline; immutable greater = "a > b"; immutable less = "a < b"; immutable equa = "a == b"; void main() { int[2] a; foreach (string line; lines(stdin)) { if(line == newline) break; else { a = to!(int[])(split(line)); if(a[0] > a[1]) writeln(greater); else if(a[0] < a[1]) writeln(less); else writeln(equa); } } }
D
import std.stdio; import std.string; import std.conv; import std.array; import std.math; void main() { auto buf = readln.split; int N = buf[0].to!int; int K = buf[1].to!int; int[] h = readln.split.to!(int[]); int[] cost = new int[N]; cost[0] = 0; foreach (i; 1..N){ int min_c = cost[i - 1] + abs(h[i] - h[i - 1]); foreach (j; 2..(K+1)){ if (i - j < 0) break; int c = cost[i - j] + abs(h[i] - h[i - j]); if (c < min_c) min_c = c; } cost[i] = min_c; } writeln(cost[$ - 1]); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void main() { auto rs = new int[](10^^4+1); foreach (x; 1..101) foreach (y; 1..101) foreach (z; 1..101) { auto n = x^^2 + y^^2 + z^^2 + x*y + y*z + z*x; if (n > 10^^4) continue; ++rs[n]; } auto N = readln.chomp.to!int; foreach (i; 1..N+1) writeln(rs[i]); }
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 lcm(long x, long y) { return x * y / gcd(x, y); } 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 s = new string[](N); foreach (i; 0..N) { s[i] = RD!string; } long cnt_ab; long cnt_a, cnt_b, cnt_a_b; foreach (i; 0..N) { foreach (j; 1..s[i].length) { if (s[i][j-1] == 'A' && s[i][j] == 'B') { ++cnt_ab; } } if (s[i][$-1] == 'A' && s[i][0] == 'B') { ++cnt_a_b; } else { if (s[i][$-1] == 'A') ++cnt_a; if (s[i][0] == 'B') ++cnt_b; } } auto ans = max(0, cnt_a_b - 1); if (cnt_a_b != 0) { if (cnt_a != 0) { ++ans; --cnt_a; } if (cnt_b != 0) { ++ans; --cnt_b; } } ans += min(cnt_a, cnt_b); ans += cnt_ab; writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void main() { auto nk = readln.split.to!(int[]); auto N = nk[0]; auto K = nk[1]; auto as = readln.split.to!(long[]); auto cs = new long[](N); foreach (i, a; as) { if (i) cs[i] = cs[i-1]; if (a > 0) cs[i] += a; } long max_r, sum_a; foreach (a; as[0..K]) sum_a += a; max_r = max(0, sum_a) + cs[$-1] - cs[K-1]; foreach (i, a; as[K..$]) { sum_a += -as[i] + a; auto c = cs[$-1] + cs[i] - cs[i+K]; max_r = max(max_r, c, sum_a + c); } writeln(max_r); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto M = RD; auto edges = new long[][](N); auto deg = new long[](N); foreach (i; 0..N-1+M) { auto A = RD-1; auto B = RD-1; edges[A] ~= B; ++deg[B]; } long[] open; foreach (i; 0..N) { if (deg[i] == 0) { open ~= i; break; } } auto par = new long[](N); while (!open.empty) { auto from = open.front; open.popFront; foreach (to; edges[from]) { --deg[to]; if (deg[to] == 0) { par[to] = from+1; open ~= to; } } } foreach (i; 0..N) { writeln(par[i]); } stdout.flush; debug readln; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "a", long, "b"); alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less); void main() { long n, a, b; scan(n, a, b); auto h = new long[](n); foreach (i; iota(n)) h[i] = lread(); long ok = (h.reduce!(max) + b - 1) / b, ng = -1; while (abs(ok - ng) > 1) { long mid = (ok + ng) / 2; bool solve() { long cnt; foreach (e; h) { if (e - mid * b > 0) { cnt += ((e - mid * b) + (a - b) - 1) / (a - b); } } return (cnt <= mid); } if (solve()) ok = mid; else ng = mid; } writeln(ok); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
//dlang template---{{{ import std.stdio; import std.conv; import std.string; import std.array; import std.algorithm; import std.typecons; import std.math; import std.range; // MIT-License https://github.com/kurokoji/nephele class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } //Digit count---{{{ int DigitNum(int num) { int digit = 0; while (num != 0) { num /= 10; digit++; } return digit; } //}}} //}}} void main() { Scanner sc = new Scanner; int X; sc.scan(X); if (X == 7 || X == 5 || X == 3) writeln("YES"); else writeln("NO"); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, 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 main() { string s; readV(s); auto a = s.map!(c => cast(int)(c-'0')).array; foreach (i; 0..1<<3) { auto b = a.dup; foreach (j; 0..3) if (i.bitTest(j)) b[j+1] = -b[j+1]; if (b.sum == 7) { write(a[0]); foreach (j; 0..3) { write(b[j+1] < 0 ? "-" : "+"); write(a[j+1]); } writeln("=7"); return; } } } 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.conv; import std.range; import std.stdio; import std.string; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto n = readln.strip.to !(int); auto a = readln.splitter.map !(to !(int)).array; bool [int] b; foreach (i; 0..n) { foreach (j; i + 1..n) { b[a[j] - a[i]] = true; } } writeln (b.length); } }
D
// Cheese-Cracker: cheese-cracker.github.io const ll PRIME = 10^^9 + 7L; void theCode(){ int n; n = scan!int; auto arr = scanArray; ll summ = 0; foreach(el; arr){ summ += el; summ %= PRIME; } auto freq = new ll[61]; foreach(el; arr){ for(int s = 0; s <= 60; ++s){ ll mask = 1L<<s; if(mask & el) ++freq[s]; } } ll res = 0; foreach(el; arr){ ll andsum = 0; for(int s = 0; s <= 60; ++s){ ll val = 1L<<s; if(val & el){ val %= PRIME; andsum += val * freq[s]; andsum %= PRIME; } } ll cursum = summ + n.to!long * (el % PRIME); cursum %= PRIME; ll term = (cursum - andsum + PRIME) % PRIME; res += andsum * term; res %= PRIME; } writeln(res); } void main(){ long tests = 1; tests = scan; // Toggle! while(tests--) theCode(); } /********* That's All Folks! *********/ import std.stdio, std.conv, std.functional, std.string, std.algorithm; import std.container, std.range, std.typecons, std.numeric, std.math, std.random; static string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;} T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; } void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } } alias ll = long, tup = Tuple!(long, "x", long, "y");
D