code
stringlengths
4
1.01M
language
stringclasses
2 values
//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; void main() { auto n = readln.strip.to!int; auto s = readln.strip[0 .. n]; foreach (i; 1 .. n) { if (s[i-1] > s[i]) { debug stderr.writeln (i); writeln ("YES"); writeln (i, ' ', i + 1); return; } } writeln ("NO"); }
D
import std.algorithm; import std.array; import std.bigint; import std.conv; import std.exception; import std.math; import std.numeric; import std.random; import std.range; import std.stdio; import std.string; import std.typecons; import core.bitop; immutable int NA = -1; immutable string A = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; immutable string B = "AxxxxxxHIxxxMxOxxxxTUVWXYx"; void main () { char [dchar] d; foreach (i; 0..A.length) { d[A[i]] = B[i]; } char [] s; while ((s = to !(char []) (readln ().strip ())) != "") { char [] b = s.dup; reverse (b); char [] c; foreach (x; b) { c ~= d[x]; } writeln (s == c ? "YES" : "NO"); } }
D
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } void main() { try { for (; ; ) { const numCases = readInt(); foreach (caseId; 0 .. numCases) { const N = readInt(); const K = readInt(); const S = readToken(); auto app = new bool[][](K, 2); foreach (i; 0 .. N) { if (S[i] != '?') { app[i % K][S[i] - '0'] = true; } } bool ok = true; auto cnt = new int[2]; foreach (r; 0 .. K) { if (app[r][0] && app[r][1]) { ok = false; } foreach (s; 0 .. 2) { if (app[r][s]) { ++cnt[s]; } } } bool ans; if (ok) { if (2 * cnt[0] <= K && 2 * cnt[1] <= K) { ans = true; } } writeln(ans ? "YES" : "NO"); } } } catch (EOFException e) { } }
D
// cheese-cracker [2022-02-06] void solve(){ long n = scan; long k = scan; if(k == 1 || (n % 2 == 0)){ writeln("YES"); }else{ writeln("NO"); return; } long m = n * k; long x = 1; for(int i = 0; i < n; ++i){ for(int j = 0; j < k; ++j){ write(x, " "); x += 2; if(x > m){ x = 2; } } writeln; } } void main(){ long tests = scan; // Toggle! while(tests--) solve; } /*_________________________*That's All Folks!*__________________________*/ import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math, std.numeric; string[] tk; alias tup = Tuple!(long, long); T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;} T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; } void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new string[][](t, 2); foreach (ti; 0..t) { auto n = RD!int; auto s = RD!string; long cnt0, cnt1; foreach (i; 0..n) { if (s[i] == '0') ++cnt0; else ++cnt1; } long c0, c1; foreach (i; 0..n) { if (s[i] == '0') { ans[ti][0] ~= c0 % 2 ? ')' : '('; ++c0; } else { ans[ti][0] ~= c1 < cnt1/2 ? '(' : ')'; ++c1; } } foreach (i; 0..n) { if (s[i] == '1') ans[ti][1] ~= ans[ti][0][i]; else ans[ti][1] ~= ans[ti][0][i] == '(' ? ')' : '('; } bool ok = true; long cnt; foreach (i; 0..n) { if (ans[ti][1][i] == '(') ++cnt; else --cnt; if (cnt < 0) { ok = false; break; } } if (cnt != 0) ok = false; if (!ok) ans[ti].length = 0; } foreach (e; ans) { if (e.empty) writeln("NO"); else { writeln("YES"); writeln(e[0]); writeln(e[1]); } } stdout.flush; debug readln; }
D
import std.stdio; import std.algorithm; void main() { int n; scanf("%d", &n); int a, cnt1, cnt0; for (int i = 0; i < n; ++i) { scanf("%d", &a); if (a & 1) ++cnt1; else ++cnt0; } int ans = min(cnt0, cnt1); cnt1 -= ans; ans += cnt1 / 3; printf("%d", ans); }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n, m, r; rd(n, m, r); auto a = readln.split.to!(int[]); auto b = readln.split.to!(int[]); auto dp = new int[][](n + 1, r + 1); for (int i = 0; i <= n; i++) { fill(dp[i], -1); } dp[0][0] = 0; foreach (i; 0 .. n) { for (int j = 0; j <= r; j++) { if (dp[i][j] >= 0) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); for (int k = 1; j + a[i] * k <= r; k++) { dp[i + 1][j + a[i] * k] = max(dp[i + 1][j + a[i] * k], dp[i][j] + k); } } } } int ans = r, bmax = reduce!(max)(b); for (int j = 0; j <= r; j++) { auto cnt = dp[n][j]; if (cnt >= 0) { ans = max(ans, r - j + cnt * bmax); } } writeln(ans); } 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.algorithm; void main() { immutable s1 = readln.chomp; immutable s2 = readln.chomp; int[][] dp = new int[][](s1.length+1, s2.length+1); for( int i=0; i<=s1.length; ++i ) dp[i][0] = i; for( int i=0; i<=s2.length; ++i ) dp[0][i] = i; for( int i=1; i<=s1.length; ++i ) { for( int j=1; j<=s2.length; ++j ) { immutable cost = s1[i-1] == s2[j-1] ? 0 : 1; dp[i][j] = min(dp[i-1][j] + 1, dp[i][j-1] + 1, dp[i-1][j-1] + cost); } } dp[$-1][$-1].writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; struct UFTree(T) { struct Node { T parent; T rank = 1; } /// T min_size, max_size; /// this(T n) { nodes.length = n; sizes.length = n; foreach (i, ref node; nodes) { node = Node(i.to!T); sizes[i] = 1; } min_sizes.length = n + 1; min_sizes[1] = n; min_size = 1; max_size = 1; } /// bool unite(T a, T b) { a = root(a); b = root(b); if (a == b) return false; auto a_size = sizes[a]; auto b_size = sizes[b]; --min_sizes[a_size]; --min_sizes[b_size]; ++min_sizes[a_size + b_size]; foreach (nxt_size; min(a_size, b_size)..min_sizes.length) if (min_sizes[nxt_size] != 0) { min_size = nxt_size.to!T; break; } max_size = max(max_size, a_size + b_size); if (nodes[a].rank < nodes[b].rank) { sizes[a] += sizes[b]; nodes[b].parent = a; } else { sizes[b] += sizes[a]; nodes[a].parent = b; if (nodes[a].rank == nodes[b].rank) ++nodes[b].rank; } return true; } /// bool is_same(T a, T b) { return root(a) == root(b); } /// T size(T i) { return sizes[root(i)]; } private: Node[] nodes; T[] sizes; T[] min_sizes; T root(T i) { if (nodes[i].parent == i) return i; return nodes[i].parent = root(nodes[i].parent); } } /// UFTree!T uftree(T)(T n) { return UFTree!T(n); } void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; auto uft = uftree(N); foreach (_; 0..M) { auto ab = readln.split.to!(int[]); uft.unite(ab[0]-1, ab[1]-1); } int r; foreach (i; 0..N) r = max(r, uft.size(i)); writeln(r); }
D
import std.conv,std.stdio,std.math,std.string;void main(){readln.chomp.to!int.pow(2).writeln;}
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { string s; scan(s); writeln(s.canFind("AC") ? "Yes" : "No"); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { auto primes = () { long[] primes; long i; while (primes.length < 55) { long x = 5 * i + 2; if (factorize(x).length == 1) primes ~= x; i++; } return primes; }(); long N = lread(); primes[0 .. N].map!text().join(' ').writeln(); } long[] factorize(long x) { assert(0 < x, "x is negative"); long[] result; while ((x & 1) == 0) { x /= 2; result ~= 2; } for (long i = 3; i * i <= x; i += 2) while (x % i == 0) { x /= i; result ~= i; } if (x != 1) result ~= x; return result; }
D
// 提出解 void solve(){ foreach(_; 0 .. scan!int){ long n = scan!long, m = scan!long; long ans; if(n == 1 && m == 1) ans = 0; else if(n == 1 || m == 1) ans = 1; else ans = 2; ans.print; } } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // // 愚直解 void jury(){ } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // // テストケース void gen(){ } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // // テンプレ import std; bool DEBUG = 0; void main(string[] args){ if(args.canFind("-debug")) DEBUG = 1; if(args.canFind("-gen")) gen; else if(args.canFind("-jury")) jury; else solve; } void log(A ...)(lazy A a){ if(DEBUG) print(a); } void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ stdout.write(t, " "), print(a); } string unsplit(T)(T xs, string d = " "){ return xs.array.to!(string[]).join(d); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } T mini(T)(ref T x, T y){ if(x > y) x = y; return x; } T maxi(T)(ref T x, T y){ if(x < y) x = y; return x; } T mid(T)(T l, T r, bool delegate(T) f){ T m = (l + r) / 2; (f(m)? l: r) = m; return f(l)? f(r - 1)? r: mid(l, r, f): l; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // // ライブラリ(基本) class UnionFind{ this(int n){ foreach(i; 0 .. n) R ~= i, K ~= [i]; } int[] R; int[][] K; void unite(int a, int b){ int ra = R[a], rb = R[b]; if(ra == rb) return; if(K[ra].length < K[rb].length) unite(b, a); else foreach(k; K[rb]) R[k] = ra, K[ra] ~= k; } int find(int a){ return R[a]; } int getSize(int a){ return K[R[a]].length.to!int; } } class Queue(T){ private T[] xs; private uint i, j; this(T[] xs = []){ this.xs = xs; j = xs.length.to!uint; } uint length(){ return j - i; } bool isEmpty(){ return j == i; } void enq(T x){ while(j + 1 >= xs.length) xs.length = xs.length * 2 + 1; xs[j ++] = x; } T deq(){ assert(i < j); return xs[i ++]; } T peek(){ assert(i < j); return xs[i]; } void flush(){ i = j; } alias empty = isEmpty, front = peek, popFront = deq, pop = deq, push = enq, top = peek; Queue opOpAssign(string op)(T x) if(op == "~"){ enq(x); return this; } T opIndex(uint li){ assert(i + li < j); return xs[i + li]; } static Queue!T opCall(T[] xs){ return new Queue!T(xs); } T[] array(){ return xs[i .. j]; } override string toString(){ return array.to!string; } } class Stack(T){ private T[] xs; private uint j; this(T[] xs = []){ this.xs = xs; j = xs.length.to!uint; } uint length(){ return j; } bool isEmpty(){ return j == 0; } void push(T x){ while(j + 1 >= xs.length) xs.length = xs.length * 2 + 1; xs[j ++] = x; } T pop(){ assert(j > 0); return xs[-- j]; } T peek(){ assert(j > 0); return xs[j - 1]; } void clear(){ j = 0; } alias empty = isEmpty, front = peek, popFront = pop, top = peek; Stack opOpAssign(string op)(T x) if(op == "~"){ push(x); return this; } T opIndex(uint li){ assert(j > 0 && j - 1 >= li); return xs[j - 1 - li]; } static Stack!T opCall(T[] xs = []){ return new Stack!T(xs); } T[] array(){ return xs[0 .. j]; } override string toString(){ return array.to!string; } } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // // ライブラリ(追加)
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { int a, b; scan(a, b); writeln(max(a + b, a - b, a * b)); }
D
/* imports all std modules {{{*/ import std.algorithm, std.array, std.ascii, std.base64, std.bigint, std.bitmanip, std.compiler, std.complex, std.concurrency, std.container, std.conv, std.csv, std.datetime, std.demangle, std.encoding, std.exception, std.file, std.format, std.functional, std.getopt, std.json, std.math, std.mathspecial, std.meta, std.mmfile, std.net.curl, std.net.isemail, std.numeric, std.parallelism, std.path, std.process, std.random, std.range, std.regex, std.signals, std.socket, std.stdint, std.stdio, std.string, std.system, std.traits, std.typecons, std.uni, std.uri, std.utf, std.uuid, std.variant, std.zip, std.zlib; /*}}}*/ /+---test 4 ---+/ void main(string[] args) { const r = readln.chomp.to!long; (3*r.pow(2)).writeln; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); alias route = Tuple!(long, "From", long, "To"); long bignum = 1_000_000_007; 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 x, a; scan(x, a); if(x < a) writeln(0); else writeln(10); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; void main() { immutable x = readln.split.to!(int[]); (x.countUntil!"a == 0" + 1).writeln; } void scan(T...)(ref T args) { auto line = readln.split; // @suppress(dscanner.suspicious.unmodified) foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.algorithm; import std.array; import std.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 nm = readints; long m = nm[1]; auto a = readints; int[long] d = [0: 1]; long sum = 0; long ans = 0; for (int i = 0; i < a.length; i++) { sum = (sum + a[i]) % m; ans += d.get(sum, 0); d[sum]++; } writeln(ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto x_y = readln.to!(wchar[]); writeln(x_y[0] > x_y[2] ? ">" : x_y[0] < x_y[2] ? "<" : "="); }
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } const long mod = 1_000_000_007; void main(){ long h = read.to!long; long w = read.to!long; long k = read.to!long; // 解説見て考えた // HWK(K + 1)(K - 1)P(N - 2, K - 2) / 3 + P(H + W, K)K(K + 3) / 2 long a = h * w; a %= mod; a *= k, a %= mod; a *= k + 1, a %= mod; a *= k - 1, a %= mod; a *= perm(h + w - 2, k - 2), a %= mod; a *= inv(3), a %= mod; long b = perm(h + w, k); b *= k, b %= mod; b *= k + 3, b %= mod; b *= inv(2), b %= mod; long ans = a + b; ans %= mod; ans.writeln; } // mod p における乗法逆元 long inv(long x){ assert(x > 0); assert(x < mod); static long[] _inv = [0, 1]; while(x >= _inv.length){ _inv ~= _inv[mod % $] * (mod - mod / _inv.length) % mod; } return _inv[x]; } // 順列 P long perm(long n, long k){ long ans = 1; for(long i = 0; i < k; i ++){ ans *= n - i; ans %= mod; } return ans; } /* 交差点の個数で捉えると、1回の切断のスコアは (交差点の個数) + (割線の本数) + 1 と読み替えられる 長さKの操作列のうちのi回目である場合、そのスコアは (i回目での交差点の個数) + i + 1 よって、長さKの操作列のスコアは = Σ[i] (i回目での交差点の個数) + K(K + 3) / 2 求めるものは、P(N, K)種類ある操作列すべてにわたる和 Σ[操作列]( Σ[i] (i回目での交差点の個数) + K(K + 3) / 2 ) = Σ[操作列]( Σ[i] Σ[交差点](i回目以前に登場? 1: 0) + K(K + 3) / 2 ) = Σ[交差点]( Σ[操作列, i](i回目以前に登場? 1: 0) ) + Σ[操作列]( K(K + 3) / 2 ) 前半を求めるために、縦線aと横線bの交わる交差点に注目する aの登場する位置、bの登場する位置を同じa、bで表すとしたとき、 「a < b <= i となるような操作列およびiのとり方が何通りあるか」 である それは ・AとBと(K - 2)個の□を並べ、そのうちの1個を黒く塗ったとき  黒よりも右側にAもBもない場合の数 と考えればよく、 それは 2(1 + 3 + 6 + 10 + 15) のような形の和である。 つまり Σ[i in [1, K - 1]]( i(i + 1) / 2 ) であり K(K + 1)(K - 1) / 3 さらに□に文字をはめると考えると、 K(K + 1)(K - 1)P(N - 2, K - 2) / 3 交差点はHW個あるので、先程の前半と言っていたものは HWK(K + 1)(K - 1)P(N - 2, K - 2) / 3 求めるものは HWK(K + 1)(K - 1)P(N - 2, K - 2) / 3 + P(H + W, K)K(K + 3) / 2 たとえば H = 2, W = 1, K = 2のとき P(2 + 1, 2) 2 (4 - 4 + 3) 2 1 / 3(2 + 1)(2 + 1 - 1) + P(2 + 1, 2) 2 5 / 2 = 6 2 3 2 1 / 3 3 2 + 6 2 5 / 2 = 4 + 30 = 34 */
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string; immutable long BASE = 31; immutable long MOD = 10^^9 + 9; immutable long BASE_INV = powmod(BASE, MOD-2, MOD); void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto S = readln.chomp.map!(c => to!long(c - 'a') + 1).array; auto P = new long[](N+1); P[0] = 1; foreach (i; 0..N) P[i+1] = P[i] * BASE % MOD; auto D = new bool[long][](N+1); int l = 0; int r = 0; long ans = 0; long hash = S[0]; while (M--) { auto Q = readln.chomp; if (Q == "L--") { l -= 1; hash = hash * BASE % MOD + S[l]; hash %= MOD; } else if (Q == "L++") { hash = (hash - S[l]) % MOD; hash = hash * BASE_INV % MOD; hash = (hash + MOD) % MOD; l += 1; } else if (Q == "R--") { hash = (hash - S[r] * P[r - l] % MOD) % MOD; hash = (hash + MOD) % MOD; r -= 1; } else { r += 1; hash = (hash + S[r] * P[r - l] % MOD) % MOD; hash = (hash + MOD) % MOD; } if (hash !in D[r-l+1]) { D[r-l+1][hash] = true; ans += 1; } } ans.writeln; } long powmod(long a, long x, long m) { long ret = 1; while (x) { if (x % 2) ret = ret * a % m; a = a * a % m; x /= 2; } return ret; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto H = readln.chomp.to!long; long d, x = 1; while (H) { d += x; x *= 2; H /= 2; } writeln(d); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; alias Pair = Tuple!(int, "a", int, "b"); void main() { int N, M, X, Y; scan(N, M, X, Y); auto xmax = readln.split.to!(int[]).reduce!max; auto ymin = readln.split.to!(int[]).reduce!min; auto ok = xmax < ymin && xmax < Y && X < ymin; writeln(ok ? "No War" : "War"); } 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.algorithm, std.conv, std.range, std.stdio, std.string; void main() { for (;;) { auto rd = readln.split.map!(to!int), m = rd[0], f = rd[1], r = rd[2]; if (m == -1 && f == -1 && r == -1) break; if (m == -1 || f == -1) writeln("F"); else if (m + f >= 80) writeln("A"); else if (m + f >= 65 && m + f < 80) writeln("B"); else if (m + f >= 50 && m + f < 65) writeln("C"); else if (m + f >= 30 && m + f < 50) { if (r >= 50) writeln("C"); else writeln("D"); } else writeln("F"); } }
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 v = min(K, N - 1); long ans; auto c = Combination_mod(10 ^^ 6); // n個の中から重複ありでk個選ぶ long h(long n, long k) { return c(n + k - 1, n - 1); } // xは0人の部屋の数 foreach (x; 0 .. v + 1) { // c(N, x): 0人の部屋を選ぶ // h(N-x, x): 0人になった部屋から移動した人の移動先を選ぶ ans = (ans + (c(N, x) * h(N - x, x) % MOD)) % MOD; } writeln(ans); } /// Number of k-combinations % m (precalculated) alias Combination_mod = Combination_modImpl!long; struct Combination_modImpl(T) { T _n, _m; T[] _fact, _factinv; this(T maxnum, T mod = 10 ^^ 9 + 7) { _n = maxnum, _m = mod, _fact = new T[](_n + 1), _factinv = new T[](_n + 1), _fact[0] = 1; foreach (i; 1 .. _n + 1) _fact[i] = _fact[i - 1] * i % _m; T powmod(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; } foreach (i; 0 .. _n + 1) _factinv[i] = powmod(_fact[i], _m - 2, _m); } T opCall(T n, T k, T dummy = 10 ^^ 9 + 7) { return n < k ? 0 : ((_fact[n] * _factinv[n - k] % _m) * _factinv[k] % _m); } }
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; writeln(x^1); 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; scan(a, b); auto ans = a * (a - 1) / 2 + b * (b - 1) / 2; 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 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 S = RD!string; writeln(S == "ABC" ? "ARC" : "ABC"); 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 = 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!(string, "x", long, "y"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long k, x; scan(k, x); if(500 * k >= x) writeln("Yes"); else writeln("No"); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; import std.typecons; int n; rd(n); int m; rd(m); auto lose=new int[](n), g=new int[][](n); foreach(i; 0..m){ int a, b; rd(a, b); g[a-1]~=(b-1); lose[b-1]++; } bool multi=false; auto used=new bool[](n); foreach(_; 0..n){ int[] cand; foreach(i; 0..n){ if(used[i]) continue; if(lose[i]>0) continue; cand~=i; } if(cand.length>1) multi=true; auto i=cand[0]; foreach(j; g[i]) lose[j]--; writeln(i+1); used[i]=true; } if(multi) writeln(1); else writeln(0); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.stdio, std.conv, std.string, std.range, std.array, std.algorithm; void main() { int n = readln().chomp().to!int; string minS = readln().chomp(); for (int i = 0; i < n - 1; i++){ string dic = readln().chomp(); if (dic < minS){ minS = dic; } } writeln(minS); }
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(){ while(true) { auto l = readInts(); auto N = l[0]; auto A = l[1]; auto B = l[2]; auto C = l[3]; auto X = l[4]; if(N == 0) { return; } auto Y = readInts(); int c = 0; auto n = 0; while(c <= 10000 && n < N) { if(X == Y[n]) { ++n; if(n == N){ break; } } X = (A * X + B) % C; ++c; } if(n == N) { writeln(c); } else { writeln(-1); } } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } 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 P = RD; auto A = RDR.ARR; long o, e; foreach (i; 0..N) { if (A[i] % 2 == 0) ++e; else ++o; } long ans = (2^^e); if (!(P == 0 && o == 0)) ans *= ((2^^o)/2); writeln(ans); stdout.flush(); debug readln(); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n; readV(n); int[] a; readA(n, a); auto cl = new long[](n), cr = new long[](n); cl[0] = a[0]; foreach (i; 1..n) cl[i] = cl[i-1]+a[i]; cr[0] = a[$-1]; foreach (i; 1..n) cr[i] = cr[i-1]+a[$-1-i]; auto r = 10L^^18; foreach (i; 2..n-1) { auto d1 = divide(cl[0..i]); auto d2 = divide(cr[0..n-i]); r = min(r, max(d1[1], d2[1])-min(d1[0], d2[0])); } writeln(r); } auto divide(long[] c) { auto t = c[$-1], cs = c.assumeSorted; if (t%2 == 0 && cs.contains(t/2)) return [t/2, t/2]; auto a = cs.lowerBound(t/2+1), b = cs.upperBound(t/2); if (a.empty) return [t-c[0], c[0]]; auto a1 = a.back, a2 = t-a1, b2 = b.front, b1 = t-b2; if (a2-a1 < b2-b1) return [a1, a2]; else return [b1, b2]; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { long A, B; scan(A, B); auto S = sread(); bool a = S[A] == '-'; bool b = (S[0 .. A] ~ S[A + 1 .. $]).all!(c => '0' <= c && c <= '9'); writeln((a && b) ? "Yes" : "No"); }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; bool calc(int[] hs) { hs[0]--; for (int i = 1; i < hs.length; i++) { if (hs[i - 1] < hs[i]) hs[i]--; if (hs[i - 1] > hs[i]) return false; } return true; } void main() { readint; auto hs = readints; writeln(calc(hs) ? "Yes" : "No"); }
D
import std.stdio; import std.conv; import std.string; void main() { string s = readln().chomp; if (s[$ - 1] == '0' || s[$ - 1] == '2' || s[$ - 1] == '4' || s[$ - 1] == '6' || s[$ - 1] == '8' ) { writeln(s); } else { writeln(s.to!(int) * 2); } }
D
import std.stdio, std.string, std.range, std.algorithm, std.math, std.typecons, std.conv; void main() { auto a = readln.split.to!(int[]); ((a[0] <= a[2] && a[2] <= a[1]) ? "Yes" : "No").writeln; }
D
void main() { auto S = rs; ulong[] arr = new ulong[](S.length); arr[0] = S[0] == 'B' ? 1 : 0; foreach(i, v; S[1..$]) arr[i+1] = arr[i] + (v == 'B' ? 1 : 0); ulong res; foreach(i, v; S[1..$]) { if(v == 'W') res += arr[i]; } // arr.writeln; res.writeln; } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.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]; bool found = false; for (int i = 1; i <= b; i++) { if (a * i % b == c) { found = true; break; } } writeln(found ? "YES" : "NO"); }
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 N, A; scan(N, A); auto X = aryread(); long S = X.sum(); auto dp = new long[][][](N + 1, N + 1, S + 1); dp[0][0][0] = 1; foreach (i; 0 .. N) foreach (j; 0 .. N) foreach (k; 0 .. S + 2) { if (0 <= k - X[i]) dp[i + 1][j + 1][k] += dp[i][j][k - X[i]]; dp[i + 1][j][k] += dp[i][j][k]; } // foreach (row; dp[0]) // writeln(row); // writeln(); // foreach (row; dp[N]) // writeln(row); long ans; foreach (i; 1 .. N + 1) { ans += dp[N][i][A * i]; } writeln(ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto as = readln.split.to!(int[]); int[][] T; T.length = N; foreach (_; 1..N) { auto uv = readln.split.to!(int[]); auto u = uv[0]-1; auto v = uv[1]-1; T[u] ~= v; T[v] ~= u; } auto res = new int[](N); auto DP = new int[](N); DP[] = int.max; size_t idx; void solve(int i, int p) { auto a = as[i]; size_t len = idx, pi; int pa; if (idx == 0 || DP[idx-1] < a) { DP[idx++] = a; } else if (DP[0] >= a) { pa = DP[0]; DP[0] = a; } else { size_t l, r = idx-1; while (l+1 < r) { auto m = (l+r)/2; if (DP[m] >= a) { r = m; } else { l = m; } } pi = r; pa = DP[r]; DP[r] = a; } res[i] = idx.to!int; foreach (j; T[i]) if (j != p) solve(j, i); if (idx != len) { idx = len; } else { DP[pi] = pa; } } solve(0, -1); foreach (r; res) writeln(r); }
D
import std.algorithm; import std.array; import std.ascii; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void log(A...)(A arg) { stderr.writeln(arg); } int size(T)(in T s) { return cast(int)s.length; } void main() { bool solve() { auto s = readln.chomp; if (s == ".") return false; auto t = readln.chomp; if (s == t) { writeln("IDENTICAL"); return true; } auto ss = s.split("\"").array; auto ts = t.split("\"").array; if (ss.length != ts.length) { writeln("DIFFERENT"); return true; } foreach (int i, x; ss) { if (i % 2 != 0) continue; if (x != ts[i]) { writeln("DIFFERENT"); return true; } } int c = 0; foreach (int i, x; ss) { if (i % 2 == 0) continue; c += (x != ts[i]); } writeln(c == 1 ? "CLOSE" : "DIFFERENT"); return true; } while (solve()) {} }
D
import std.stdio; import std.conv; import std.array; import std.math; import std.algorithm; import std.string; public int gcd(int m, int n) { if ((m == 0) || (n == 0)) return 0; while (m != n) { if (m > n) m = m - n; else n = n - m; } return m; } public int lcm(int m, int n) { if ((m == 0) || (n == 0)) return 0; return ((m / gcd(m, n) * n)); } int main() { string s; while((s = readln()).length != 0) { string[] _s = split(s); int a = to!int(_s[0]); int b = to!int(_s[1]); int x = gcd(a,b); int y = lcm(a,b); printf("%d %d\n",x,y); } return 0; }
D
module app; import core.bitop; import std.algorithm; import std.array; import std.bigint; import std.conv; import std.stdio; import std.string; struct Input { string s; } void parseInput(T)(out Input input, T file) { with (file) with (input) { s = readln().strip(); } } struct Output { } auto main2(Input* input) { foreach (c; input.s) write("x"); } void main() { Input input = void; parseInput(input, stdin); main2(&input); }
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, m; readV(n, m); auto g = GraphM!int(n).init; foreach (_; 0..m) { int a, b, c; readV(a, b, c); --a; --b; g[a][b] = g[b][a] = c; } auto d = g.floydWarshal, ans = 0; foreach (i; 0..n) foreach (j; i+1..n) if (g[i][j] < g.inf && g[i][j] > d[i][j]) ++ans; writeln(ans); } struct GraphM(W = int, W i = 10^^9) { alias Wt = W, inf = i; int n; Wt[][] g; alias g this; this(int n) { this.n = n; g = new Wt[][](n, n); } ref auto init() { foreach (i; 0..n) { g[i][] = inf; g[i][i] = 0; } return this; } } template FloydWarshal(Graph) { import std.algorithm, std.array, std.traits; alias Wt = TemplateArgsOf!Graph[0]; Wt[][] floydWarshal(ref Graph g) { Wt[][] dist; int[][] inter; floydWarshal(g, dist, inter); return dist; } void floydWarshal(ref Graph g, out Wt[][] dist, out int[][] inter) { auto n = g.n, sent = n; dist = g.g.map!(i => i.dup).array; inter = new int[][](n, n); foreach (i; 0..n) inter[i][] = sent; foreach (k; 0..n) foreach (i; 0..n) foreach (j; 0..n) if (dist[i][j] > dist[i][k] + dist[k][j]) { dist[i][j] = dist[i][k] + dist[k][j]; inter[i][j] = k; } } } auto floydWarshal(G)(G g) { return FloydWarshal!(typeof(g)).floydWarshal(g); }
D
import std.stdio, std.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() { string s; scan(s); int n = s.length.to!int; if (n % 2) { writeln("No"); return; } foreach (i ; 0 .. n / 2) { if (s[2*i .. 2*i + 2] != "hi") { writeln("No"); return; } } writeln("Yes"); } 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 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() { string s; scan(s); foreach (i ; 0 .. s.length) { if (i & 1 ^ 1) { write(s[i]); } } writeln(); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.conv, std.string, std.algorithm, std.math, std.array, std.container, std.typecons; void main() { long n = readln.chomp.to!long; long[] a = readln.chomp.split.to!(long[]); long[] count = new long[n]; foreach(ai;a) count[ai-1]++; foreach(ci;count) writeln(ci); }
D
void main() { auto a = ri; writeln(a + a*a + a*a*a); } // =================================== 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; } long rl() { return readAs!long; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto x = RD; auto y = RD; long[long] g = [1:0, 2:2, 3:0, 4:1, 5:0, 6:1, 7:0, 8:0, 9:1, 10:0, 11:1, 12:0]; auto ans = g[x] == g[y]; writeln(ans ? "Yes" : "No"); stdout.flush(); debug readln(); }
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 L; scanf("%d", &L); int n = 20, m = (n-2)*2; int l = L; foreach (i; 0..n) { m += l&1; if (i == n-1) m += l&1; l >>= 1; } writeln(n," ",m); foreach (i;0..n-2) { writeln(i+1," ",i+2," ",0); writeln(i+1," ",i+2," ",1<<i); } l = L; foreach (i; 0..n) { if (l>>i&1) { l ^= 1<<i; if (i+1 < n) { writeln(i+1," ",n," ",l); } else { writeln(n-1," ",n," ",l); writeln(n-1," ",n," ",l+(1<<(i-1))); } } } }
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 = N.iota.map!(_ => readln.chomp.to!int).array; auto dp = new bool[][](N+1, 10001); dp[0][0] = true; foreach (i; 0..N) { foreach (j; 0..10001) { if (dp[i][j]) { dp[i+1][j] = true; if (j + A[i] <= 10000) { dp[i+1][j+A[i]] = true; } } } } int ans = 0; foreach (i; 0..10001) { if (i % 10 > 0 && dp[N][i]) ans = i; } ans.writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.15f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } struct Cmb { void init(long n) { long powmod(long a, long p) { long ans = 1; long mul = a; while (p > 0) { if ((p & 1) == 1) modm(ans, mul); p >>= 1; modm(mul, mul); } return ans; } ++n; fact = new long[2][](n); fact[0][0] = 1; foreach (i; 1..n) { fact[i][0] = fact[i-1][0]; modm(fact[i][0], i); } fact[n-1][1] = powmod(fact[n-1][0], mod - 2); foreach_reverse (i; 0..n-1) { fact[i][1] = fact[i+1][1]; modm(fact[i][1], i+1); } } long get(long a, long b) { long r = fact[a][0]; modm(r, fact[b][1]); modm(r, fact[a-b][1]); return r; } long[2][] fact; } void main() { auto H = RD; auto W = RD; auto A = RD; auto B = RD; Cmb cmb; cmb.init(H+W); long ans; foreach (x; 0..W-B) { auto c1 = cmb.get(B+x+H-A-1, B+x); auto c2 = cmb.get(W-B-x-1+A-1, A-1); c1.modm(c2); ans.moda(c1); } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio, std.string, std.algorithm, std.array, std.range, std.conv, std.typecons, std.math, std.container, std.format; void main(string[] args) { if (readln.split.to!(long[]).sum >= 22) { writeln("bust"); } else { writeln("win"); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math; void main() { auto ab = readln.split.to!(float[]); writeln(ceil((ab[0] + ab[1]) / 2).to!int); }
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; } long n = read.to!long; long m = read.to!long; Node[] nodes; foreach(i; 0 .. n) nodes ~= new Node(i, 0); foreach(nd; nodes){ nd.group = new Group(nd); } Edge[] edges; foreach(i; 0 .. m){ long x = read.to!long - 1; long y = read.to!long - 1; long z = read.to!long - 1; edges ~= new Edge(nodes[x], nodes[y], i); } foreach(ed; edges){ if(ed.node1.group.id != ed.node2.group.id){ ed.node1.group.eat(ed.node2.group); } } long ans; foreach(nd; nodes) if(nd.group.id == nd.id) ans += 1; ans.writeln; } class Edge{ Node node1; Node node2; long value; this(Node node1, Node node2, long value){ this.node1 = node1, this.node2 = node2; this.value = value; } } class Node{ long id; long value; Group group; this(long id, long value){ this.id = id; this.value = value; } } class Group{ long value; long pendingEdgeCount; long id; Node[] nodes; this(Node node){ this.id = node.id; this.nodes = [node]; this.value = node.value; } void eat(Group gp){ if(gp.nodes.length > this.nodes.length) gp.eat(this); else{ foreach(nd; gp.nodes){ this.nodes ~= nd; nd.group = this; this.value += nd.value; } this.pendingEdgeCount += gp.pendingEdgeCount; } } }
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() { string s; long k; scan(s); scan(k); k--; int pos = -1; char nm = '1'; foreach (i ; 0 .. s.length) { if (s[i] != '1') { nm = s[i]; pos = i.to!int; break; } } if (nm == '1') { writeln(1); } else { if (k < pos) { writeln(1); } else { writeln(nm); } } } 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 input=readln.chomp.split; auto n=input[0].to!ulong; auto a=input[1].to!ulong; auto b=input[2].to!ulong; auto ans=n/(a+b)*a; ans+=n%(a+b)>a?a:n%(a+b); writeln(ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto xyz = readln.split.to!(int[]); int c; xyz[0] -= xyz[2]; while (xyz[0] >= xyz[1] + xyz[2]) { xyz[0] -= (xyz[1] + xyz[2]); ++c; } writeln(c); }
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 N = RD; auto M = RD; auto edges = new long[][](N); foreach (i; 0..M) { auto a = RD-1; auto b = RD-1; edges[a] ~= b; edges[b] ~= a; } long[] open = [0]; auto ans = new long[](N); auto dist = new long[](N); dist[] = long.max; while (!open.empty) { auto from = open.front; open.popFront; foreach (to; edges[from]) { if (dist[from]+1 < dist[to]) { dist[to] = dist[from] + 1; ans[to] = from + 1; open ~= to; } } } writeln("Yes"); foreach (e; ans[1..$]) writeln(e); stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.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; long[string] to; to["Vacant"] = 0; to["Male"] = 1; to["Female"] = 2; writeln(0); stdout.flush(); auto state = to[RD!string]; if (state == 0) return; long pos; int ok, ng; ok = 0; ng = cast(int)N+1; while (abs(ok-ng) > 1) { auto mid = (ok+ng) / 2; auto eo = (mid - pos) % 2; writeln(mid); stdout.flush(); auto state2 = to[RD!string]; if (state2 == 0) return; bool same; if (eo == 0) same = state == state2; else same = state != state2; if (same) { pos = mid; state = state2; ok = mid; } else ng = mid; } stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto s = RD-1; ++ans[ti]; long x = 1; while (s != 0) { ++ans[ti]; x += 2; s = max(0, s-x); } } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
//dlang template---{{{ import std.stdio; import std.conv; import std.string; import std.array; import std.algorithm; import std.typecons; import std.math; import std.range; // MIT-License https://github.com/kurokoji/nephele class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } //Digit count---{{{ int DigitNum(int num) { int digit = 0; while (num != 0) { num /= 10; digit++; } return digit; } //}}} //}}} void main() { Scanner sc = new Scanner; int N, A, B; sc.scan(N, A, B); write(min(A, B)); write(" "); if (A + B > N) writeln(abs((N - A) - B)); else writeln("0"); }
D
import std.stdio, std.string, std.array, std.conv; void main() { long n = readln.chomp.to!long; int[] a = new int[32]; int[] b = new int[32]; int[] c = new int[32]; int[] d = new int[32]; long m = n << 1; long l = n >> 1; foreach_reverse (i; 0 .. 32) { a[i] = n & 1; b[i] = !a[i]; n /= 2; c[i] = m & 1; m /= 2; d[i] = l & 1; l /= 2; } a.to!(string[]).join.writeln; b.to!(string[]).join.writeln; c.to!(string[]).join.writeln; d.to!(string[]).join.writeln; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { string s = sread(); foreach (c; s) if (c == '1') { write('9'); } else if (c == '9') { write('1'); } else { write(c); } writeln(); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; import std.numeric; long a, b, x, y; rd(a, b, x, y); auto g=gcd(x, y); x/=g; y/=g; writeln(min(a/x, b/y)); } 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, std.conv, std.range, std.stdio, std.string; void main() { auto rd = readln.split, n = rd[0].to!size_t, a = rd[1].to!long, b = rd[2].to!long; auto x = readln.split.to!(long[]); auto ans = 0L; foreach (i; 0..n-1) ans += min((x[i+1]-x[i])*a, b); writeln(ans); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { char[] s, t; scan!(char[])(s); scan!(char[])(t); auto S = cast(byte[]) s; auto T = cast(byte[]) t; S.sort(); T.sort(); T.reverse(); // writeln(S); // writeln(T); writeln(S < T ? "Yes" : "No"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; int[2][10^^5*2] DP; void main() { auto S = readln.chomp.to!string; foreach_reverse (i; 0..S.length) { if (i == S.length-1) { DP[i][0] = 1; DP[i][1] = int.min; } else if (i == S.length-2) { DP[i][0] = S[i] == S[i+1] ? int.min : 2; DP[i][1] = 1; } else { if (S[i] == S[i+1]) { DP[i][0] = DP[i+1][1] + 1; } else { DP[i][0] = max(DP[i+1][0], DP[i+1][1]) + 1; } if (i >= S.length-3 || (DP[i] == DP[i+2] && S[i+1] == S[i+3])) { DP[i][1] = DP[i+2][0] + 1; } else { DP[i][1] = max(DP[i+2][0], DP[i+2][1]) + 1; } } } writeln(max(DP[0][0], DP[0][1])); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto a=readln.split.to!(int[]); auto freq=new int[](105); foreach(e; a) freq[e]++; int nice=0; foreach(e; freq){ if(e==1) nice++; } if(nice&1){ if(n==nice){ writeln("NO"); return; } bool o=reduce!((r, e)=>(r || (e>=3)))(false, freq); if(o==false){ writeln("NO"); return; } writeln("YES"); int _nice=0; char[] ans; bool done=false; foreach(i; 0..n){ if(freq[a[i]]>=3){ if(done==false){ ans~='A'; done=true; }else{ ans~='B'; } }else if(_nice*2<nice-1){ if(freq[a[i]]==1){ ans~='A'; _nice++; }else{ ans~='B'; } }else{ ans~='B'; } } writeln(ans); return; } writeln("YES"); int _nice=0; char[] ans; foreach(i; 0..n){ if(_nice*2<nice){ if(freq[a[i]]==1){ ans~='A'; _nice++; }else{ ans~='B'; } }else{ ans~='B'; } } writeln(ans); } 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; immutable long INF = 1L << 60; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto X = s[1].to!long; auto Y = s[2].to!long; auto A = readln.split.map!(to!long).array; auto mem = new long[][](N+1, 2); foreach (i; 0..N+1) mem[i].fill(-1); long dfs(int n, int turn) { if (mem[n][turn] >= 0) return mem[n][turn]; long ret; if (turn == 0) { long yp = n == 0 ? Y : A[n-1]; ret = abs(yp - A[$-1]); } else { long xp = n == 0 ? X : A[n-1]; ret = abs(xp - A[$-1]); } if (turn == 0) { // 先手 foreach (i; n+1..N) { ret = max(ret, dfs(i, 1)); } } else { foreach (i; n+1..N) { ret = min(ret, dfs(i, 0)); } } mem[n][turn] = ret; return ret; } dfs(0, 0).writeln; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { auto A = BigInt(readln().chomp()); auto B = BigInt(readln().chomp()); auto c = A.opCmp(B); // writeln(c); switch (c) { case -1: writeln("LESS"); break; case 0: writeln("EQUAL"); break; case 1: writeln("GREATER"); break; default: assert(0); } }
D
void main() { int n = readln.chomp.to!int; if (n == 1) { "Hello World".writeln; } else { writeln(readln.chomp.to!int + readln.chomp.to!int); } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.container; import std.typecons;
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, s; foreach (i; 1..n) { if (s == 1) { if (a[i-1] > a[i]) { cnt++; s = 0; } } else if (s == -1) { if (a[i-1] < a[i]) { cnt++; s = 0; } } else { if (a[i - 1] < a[i]) { s = 1; } else if (a[i-1] > a[i]) { s = -1; } } } cnt.writeln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; void main() { auto N = readln.chomp.to!int; int ds(int x) { int ret = 0; while (x > 0) ret += x % 10, x /= 10; return ret; } int ans = 1 << 29; foreach (i; 1..N) { int j = N - i; ans = min(ans, ds(i) + ds(j)); } ans.writeln; }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; void main() { int N = to!int(chomp(readln())); int res; for(int i = 0; i <= N; i++) { res += i; } res.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; void main() { auto x = readln.chomp.split.to!(int[]); auto a = max(x[0], x[2]); auto b = min(x[1], x[3]); writeln(max(b-a, 0)); }
D
/+ dub.sdl: name "C" dependency "dunkelheit" version=">=0.9.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; int main() { Scanner sc = new Scanner(stdin); scope(exit) assert(!sc.hasNext); int n; sc.read(n); int[] a, b; sc.read(a, b); bool check(long f) { long[51] g; foreach (i; 0..51) { long v = (1L << i); foreach (j; 1..51) { if (!(f & (1L << j))) continue; v |= g[i % j]; } g[i] = v; } foreach (i; 0..n) { int x = a[i], y = b[i]; if (!(g[x] & (1L << y))) return false; } return true; } long z = 0; foreach (i; 1..52) { if (check(z)) break; if (i == 51) { writeln("-1"); return 0; } z |= (1L << i); } foreach_reverse (i; 1..51) { if (!(z & (1L << i))) continue; z ^= (1L << i); if (check(z)) continue; z ^= (1L << i); } writeln(z); return 0; } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */ // module dkh.scanner; // import dkh.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(Args...)(auto ref Args args) { import std.exception; static if (args.length != 0) { enforce(readSingle(args[0])); read(args[1..$]); } } bool hasNext() { return succ(); } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; 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; } enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; struct Mint { long x; this(long a) { x = a % mod; if (x < 0) x += mod; } this(Mint a) { x = a.x; } ref Mint opAssign(long a) { this = Mint(a); return this; } ref Mint opOpAssign(string op)(Mint rhs) { static if (op == "+") { x += rhs.x; if (x >= mod) x -= mod; } static if (op == "-") { x -= rhs.x; if (x < 0) x += mod; } static if (op == "*") { (x *= rhs.x) %= mod; } static if (op == "/") { this *= rhs.inv(); } return this; } ref Mint opOpAssign(string op)(long rhs) { static if (op == "^^") { this = powmod(this, rhs); return this; } else { return mixin("this " ~ op ~ "= Mint(rhs)"); } } const Mint powmod(Mint a, long b) { Mint res = 1, p = a; while (b > 0) { if (b & 1) res *= p; p *= p; b /= 2; } return res; } const Mint inv() { return powmod(this, mod - 2); } Mint opUnary(string op)() if (s == "-") { return Mint(-x); } Mint opBinary(string op, T)(const T rhs) { return mixin("Mint(this) " ~ op ~ "= rhs"); } Mint opBinaryRight(string op)(const long rhs) { return mixin("Mint(rhs) " ~ op ~ "= this"); } bool opEquals(Mint a, Mint b) { return a.x == b.x; } bool opEquals(long rhs) { long y = rhs % mod; if (y < 0) y += mod; return x == y; } string toString() { import std.conv : to; return x.to!string; } } unittest { long powmod(long a, long b) { return b > 0 ? powmod(a, b / 2)^^2 % mod * a^^(b & 1) % mod : 1L; } auto a = Mint(2), b = Mint(3); assert(a + b == 5); assert(a + 5 == 7); assert(1 + b == 4); assert(a - b == mod - 1); assert(a * b == 6); assert(a / b == 2 * powmod(3, mod - 2)); assert(a^^10 == 1024); Mint z; assert(z == 0); (z += 2) *= 3; assert(z == 6); } void main() { long N, M; scan(N, M); auto ans = min(N, M / 2); M -= 2*N; if (M > 0) { ans += M / 4; } writeln(ans); }
D
import std.stdio,std.conv,std.string; void main() { auto num = readln.strip.to!long; if (num < 1200) { writeln("ABC"); } else if (num < 2800) { writeln("ARC"); } else { writeln("AGC"); } }
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 nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; long x = 1; int c; foreach (a; readln.split.to!(long[])) { auto y = x / gcd(x, a/2) * (a/2); if (y < 0 || M < y) { writeln(0); return; } x = y; int d; while (a%2 == 0) { ++d; a /= 2; } if (c == 0) { c = d; } else if (c != d) { writeln(0); return; } } writeln((M - x) / (x*2) + 1); }
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; bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); } void main(string[] args){ args ~= ["", ""]; string cmd = args[1]; if(cmd == "-debug") DEBUG = 1; if(cmd == "-gen") gen; else if(cmd == "-jury") jury; else solve; } void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ std.stdio.write(t, " "), print(a); } string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void gen(){ } void jury(){ } void solve(){ int h = scan!int, w = scan!int, k = scan!int; int[][] uf; foreach(i; 0 .. h) uf ~= scan!(char[]).map!(c => c == '#'? 1 : 0).array; int ans = 0; foreach(x; 0 .. 1<<h) foreach (y; 0 .. 1<<w){ int tmp; foreach(i; 0 .. h) foreach(j; 0 .. w){ if((1<<i) & x) continue; if((1<<j) & y) continue; tmp += uf[i][j]; } if(tmp == k) log(x, y), ans += 1; } ans.writeln; }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; import std.numeric; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n; readV(n); int[] a; readA(n, a); writeln(a.reduce!gcd); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int m; rd(m); writeln(24+(24-m)); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } } void wr(T...)(T x){ import std.stdio; foreach(e; x) write(e, " "); writeln(); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int a, b, c, d; readV(a, b, c, d); writeln((a-c).abs <= d || (a-b).abs <= d && (b-c).abs <= d ? "Yes" : "No"); }
D
import std.stdio,std.string,std.conv; int main() { string[char] ID; ID[' '] = "101"; ID['\''] = "000000"; ID[','] = "000011"; ID['-'] = "10010001"; ID['.'] = "010001"; ID['?'] = "000001"; ID['A'] = "100101"; ID['B'] = "10011010"; ID['C'] = "0101"; ID['D'] = "0001"; ID['E'] = "110"; ID['F'] = "01001"; ID['G'] = "10011011"; ID['H'] = "010000"; ID['I'] = "0111"; ID['J'] = "10011000"; ID['K'] = "0110"; ID['L'] = "00100"; ID['M'] = "10011001"; ID['N'] = "10011110"; ID['O'] = "00101"; ID['P'] = "111"; ID['Q'] = "10011111"; ID['R'] = "1000"; ID['S'] = "00110"; ID['T'] = "00111"; ID['U'] = "10011100"; ID['V'] = "10011101"; ID['W'] = "000010"; ID['X'] = "10010010"; ID['Y'] = "10010011"; ID['Z'] = "10010000"; string[string] Id; Id["00000"] = "A"; Id["00001"] = "B"; Id["00010"] = "C"; Id["00011"] = "D"; Id["00100"] = "E"; Id["00101"] = "F"; Id["00110"] = "G"; Id["00111"] = "H"; Id["01000"] = "I"; Id["01001"] = "J"; Id["01010"] = "K"; Id["01011"] = "L"; Id["01100"] = "M"; Id["01101"] = "N"; Id["01110"] = "O"; Id["01111"] = "P"; Id["10000"] = "Q"; Id["10001"] = "R"; Id["10010"] = "S"; Id["10011"] = "T"; Id["10100"] = "U"; Id["10101"] = "V"; Id["10110"] = "W"; Id["10111"] = "X"; Id["11000"] = "Y"; Id["11001"] = "Z"; Id["11010"] = " "; Id["11011"] = "."; Id["11100"] = ","; Id["11101"] = "-"; Id["11110"] = "'"; Id["11111"] = "?"; string s; string str = ""; while((s = readln.chomp).length != 0) { foreach(i;0..s.length) { str ~= ID[s[i]]; } while(str.length % 5 != 0) { str ~= "0"; } string ans = ""; while(str.length != 0) { string temp = str[0..5]; ans ~= Id[temp]; str = str[5..$]; } ans.writeln; } return 0; }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { string n = readln.chomp; writeln(n.canFind('9') ? "Yes" : "No"); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
void main() { long n, ma, mb; rdVals(n, ma, mb); Medicine[] m = n.rdCol!Medicine; long limit = 10 * n + 1; long[][][] dp = new long[][][](n+1, limit, limit); foreach (i; 0 .. n+1) { foreach (j; 0 .. 10*n+1) { dp[i][j][] = inf; } } dp[0][0][0] = 0; foreach (i, x; m) { foreach (j; 0 .. limit) { foreach (k; 0 .. limit) { if (dp[i][j][k] == inf) continue; dp[i+1][j+x.a][k+x.b] = min(dp[i+1][j+x.a][k+x.b], dp[i][j][k]+x.c); dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k]); } } } long result = inf; foreach (i; 1 .. 51) { long a = ma * i, b = mb * i; if (a >= limit || b >= limit) break; result = min(result, dp[n][a][b]); } if (result == inf) result = -1; result.writeln; } struct Medicine { long a, b, c; } 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 chie template :) {{{ import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons, std.format; // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } // }}} void main() { auto cin = new Scanner; char[] s; cin.scan(s); size_t N = s.length; size_t M = (N - 1) / 2; size_t L = (N + 3) / 2; char[] t = s[0 .. M]; char[] u = s[L - 1 .. $]; auto s_r = s.dup; s_r.reverse; auto t_r = t.dup; t_r.reverse; auto u_r = u.dup; u_r.reverse; if (s == s_r && t == t_r && u == u_r) { writeln("Yes"); } else { writeln("No"); } }
D
void main() { auto N = ri; fact(N).writeln; } ulong fact(ulong n) { if(n<=1) return 1; else return n*fact(n-1)%(10UL^^9 + 7); } // =================================== import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
// Try AtCoder // author: Leonardone @ NEETSDKASU import std.algorithm : min; import std.stdio : readln, writeln; import std.string : chomp; import std.array : split; import std.conv : to; auto gets(T)() { return readln.chomp; } auto getVals(T)() { return readln.chomp.split.to!(T[]); } void main() { auto xs = getVals!long; auto n = getVals!long[0]; n <<= 2; auto a = n / 8, ar = n % 8; auto b = ar / 4, br = ar % 4; auto c = br / 2; auto d = br % 2; auto ans = d * xs[0]; ans += min(c * 2 * xs[0], c * xs[1]); ans += min(b * 4 * xs[0], b * 2 * xs[1], b * xs[2]); ans += min(a * 8 * xs[0], a * 4 * xs[1], a * 2 * xs[2], a * xs[3]); writeln(ans); }
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() { end((scanElem+scanElem)*scanElem/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; } 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 dp = new long[string][](N+1); dp[0][""] = 1; auto str = "ACGT"; foreach (i; 0..N) { foreach (key; dp[i].keys) { foreach (j; 0..4) { auto nKey = key ~ str[j]; if (nKey.length > 4) nKey.popFront; else if (nKey.length < 3) { auto tmp = dp[i+1].get(nKey, 0); tmp.moda(dp[i][key]); dp[i+1][nKey] = tmp; continue; } if (nKey[$-3..$] == "AGC") continue; if (nKey[$-3..$] == "ACG") continue; if (nKey[$-3..$] == "GAC") continue; if (nKey[0..2] == "AG" && nKey[$-1] == 'C') continue; if (nKey[0] == 'A' && nKey[$-2..$] == "GC") continue; auto tmp = dp[i+1].get(nKey, 0); tmp.moda(dp[i][key]); dp[i+1][nKey] = tmp; } } } long ans; foreach (key; dp[N].keys) ans.moda(dp[N][key]); writeln(ans); stdout.flush; debug readln; }
D
const long mod=998244353; const int M=1_000_00*4; void main(){ import std.stdio, std.algorithm; long n, a, b, k; rd(n, a, b, k); auto fact=genFact(M, mod); auto invFact=genInv(fact, mod); long comb(long nn, long rr){ if(nn<rr) return 0; long ret=fact[nn]%mod; (ret*=invFact[rr])%=mod; (ret*=invFact[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); } long[] genFact(int n, long mod){ auto fact=new long[](n); fact[0]=fact[1]=1; foreach(i; 2..n) fact[i]=i*fact[i-1]%mod; return fact; } long[] genInv(long[] arr, long mod){ auto inv=new long[](arr.length); inv[0]=inv[1]=1; foreach(i, elem; arr[1..$]) inv[i]=powmod(arr[i], mod-2, mod); return inv; } long powmod(long a, long x, long mod){ if(x==0) return 1; else if(x==1) return a; else if(x&1) return a*powmod(a, x-1, mod)%mod; else return powmod(a*a%mod, x/2, mod); } 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; import std.string; enum conv = [ 'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000 ]; void main() { foreach (input; stdin.byLine()){ int total = 0; if (input.length == 1) total = conv[input[0]]; else { foreach (i; 1..input.length) { if (conv[input[i - 1]] < conv[input[i]]) { total -= conv[input[i - 1]]; } else { total += conv[input[i - 1]]; } } total += conv[input[$ - 1]]; } total.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[]); long SUM; foreach (a; AS) SUM += a; long x; foreach (a; AS) { if (x + a >= SUM/2) { writeln(min((x+a)*2 - SUM, SUM - x*2)); return; } x += a; } }
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(); long bignum = 1_000_000_007; 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; } } auto manhattan(long x1, long y1, long x2, long y2) { return abs(y2 - y1) + abs(x2 - x1); } void main() { auto n = lread(); auto t = lread(); long c_multiple; foreach (i; 1 .. n) { auto clock = lread(); auto tmp = gcd(t, clock); t = t / tmp * clock; } t.writeln(); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = readln.chomp.to!int; writeln(n * 800 - (n/15) * 200); }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ int n = read.to!int; int k = read.to!int; int x = n * (n - 1) / 2 - (n - 1); if(k > x){ "-1".writeln; return; } int y = x - k; int m = (n - 1) + y; m.writeln; foreach(i; 1 .. n){ writeln("1 ", i + 1); } foreach(i; 1 .. n) foreach(j; 1 .. i){ if(y == 0) return; writeln(i + 1, " ", j + 1); y -= 1; } } /* 連結なので少なくともn - 1本の辺がある。 その意味で C(n, 2) - (n - 1) が最大 これは、うにグラフのときに実現される 少なくするためには、うにの枝をつなげばよい つないだ枝は距離1になる 他への影響はない */
D