code
stringlengths
4
1.01M
language
stringclasses
2 values
void main() { long[] tmp = readln.split.to!(long[]); long n = tmp[0], m = tmp[1]; long mod = 10 ^^ 9 + 7; long factorial(long x) { long result = 1; foreach (i; 2 .. x+1) { result = result * i % mod; } return result; } long cnt; if (n == m) { cnt = factorial(n) * factorial(m) % mod; cnt = cnt * 2 % mod; } else if (abs(n - m) == 1) { cnt = factorial(n) * factorial(m) % mod; } cnt.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(int n) { // n! を素因数分解 int[int] factors; for (int i = 2; i <= n; i++) { int x = i; for (int j = 2; j * j <= x; j++) { while (x % j == 0) { factors[j]++; x /= j; } } if (x > 1) factors[x]++; } // if (factors.empty) return 0; // v2.070.1 だと empty でコンパイルエラーになるので、代わりに length 使う if (factors.length == 0) return 0; int[] t; // p_i^k の k の部分 foreach (k, v; factors) t ~= v; // do[i][約数の個数] auto dp = new int[][](t.length, 76); for (int i = 0; i <= t[0]; i++) dp[0][i + 1] = 1; for (int i = 1; i < t.length; i++) { for (int j = 1; j <= 75; j++) { // i-1 までの約数の個数 for (int k = 0; k <= t[i]; k++) { int x = j * (k + 1); if (x <= 75) { dp[i][x] += dp[i - 1][j]; } } } } return dp[t.length - 1][75]; } void main() { int n = readint; writeln(calc(n)); }
D
void main() { long n, k; rdVals(n, k); long[] list = new long[k+1]; long result; foreach_reverse (i; 1 .. k+1) { list[i] = (list[i] + modPow(k/i, n)) % mod; result = (result + list[i] * i % mod) % mod; foreach (d; divisors(i)) { if (d == i) continue; list[d] = (list[d] - list[i] + mod) % mod; } } result.writeln; } long modPow(long x, long y) { long result = 1; while (y > 0) { if (y & 1) result = result * x % mod; x = x * x % mod; y >>= 1; } return result; } long[] divisors(long x) { long[] result; for (long i = 1; i * i <= x; ++i) { if (x % i == 0) { result ~= i; if (i != x / i) result ~= x / i; } } return result; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; enum double eps = 1.0e-9; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } void main(){ int d = read.to!int; int n = read.to!int; if(n == 100) n += 1; string ans; ans = n.to!string; foreach(i; 0 .. d) ans ~= "00"; ans.writeln; }
D
void main(){ string s = readln().chomp(); string t = readln().chomp(); sort!("a<b")( cast(ubyte[]) s); sort!("a>b")( cast(ubyte[]) t); writeln(s<t?"Yes":"No"); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range; const long mod = 10^^9+7; // 1要素のみの入力 T inelm(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] inln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split())ln ~= elm.to!T(); return ln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; enum P = 10L^^9+7; long[2][10^^5+10] DP; void main() { auto L = readln.chomp.to!(char[]); DP[0][1] = 2; DP[0][0] = 1; foreach (i; 1..L.length) { if (L[i] == '0') { DP[i][1] = DP[i-1][1]; DP[i][0] = (DP[i-1][0] * 3) % P; } else { DP[i][1] = (DP[i-1][1] * 2) % P; DP[i][0] = (DP[i-1][1] + DP[i-1][0] * 3) % P; } } writeln((DP[L.length-1][0] + DP[L.length-1][1]) % P); }
D
void main() { long n = readln.chomp.to!long; string s = readln.chomp; long cnt; foreach (i; 0 .. 1000) { dchar[] a =[ (i / 100 + '0').to!dchar, (i % 100 / 10 + '0').to!dchar, (i % 10 + '0').to!dchar ]; bool ok; long index; foreach (x; s) { if (x == a[index]) { ++index; } if (index == 3) { ++cnt; break; } } } cnt.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio; 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 a = readln.chomp.to!BigInt; auto b = readln.chomp.to!BigInt; if (a > b) { writeln("GREATER"); } else if (b > a) { writeln("LESS"); } else { writeln("EQUAL"); } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { string s; scan(s); string a = "2019/04/30"; writeln(s <= a ? "Heisei" : "TBD"); } int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new int[][](n, 0); foreach (i ; 0 .. m) { int u, v; scan(u, v); if (is1indexed) { u--, v--; } adj[u] ~= v; if (isUndirected) { adj[v] ~= u; } } return adj; } alias Edge = Tuple!(int, "to", int, "cost"); Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new Edge[][](n, 0); foreach (i ; 0 .. m) { int u, v, c; scan(u, v, c); if (is1indexed) { u--, v--; } adj[u] ~= Edge(v, c); if (isUndirected) { adj[v] ~= Edge(u, c); } } return adj; } void yes(bool b) {writeln(b ? "Yes" : "No");} void YES(bool b) {writeln(b ? "YES" : "NO");} T[] readArr(T)() {return readln.split.to!(T[]);} 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
//dlang template---{{{ import std.stdio; import std.conv; import std.string; import std.array; import std.algorithm; // MIT-License https://github.com/kurokoji/nephele class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } //}}} void main() { Scanner sc = new Scanner; int N; sc.scan(N); if (N == 1) { writeln("Hello World"); } else if (N == 2) { int A, B; sc.scan(A, B); writeln(A + B); } }
D
import std.stdio, std.string, std.conv; immutable hurui = (int n){ bool[] table = new bool[](n + 1); table[] = true; table[1] = false; for(int i = 2; i * i <= n; ++i) { for(int j = i + i; j <= n; j += i) { table[j] = false; } } return table; }(60000); void main() { while(true) { string line = readln.chomp; if(line is null)break; auto num = line.chomp.to!int; for(int i = num - 1; i >= 0; --i) { if(hurui[i]) { write(i, " "); break; } } for(int i = num + 1; i <= 60000; ++i) { if(hurui[i]) { i.writeln; break; } } } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.bigint; int count_ds(int i) { int r; while (i) { r += i%10; i /= 10; } return r; } void main() { auto N = readln.chomp.to!int; int r = int.max; foreach (i; 1..N/2+1) { r = min(r, count_ds(i) + count_ds(N-i)); } writeln(r); }
D
import std.stdio, std.conv, std.string; void main(){ (n => n * (n + 1) / 2)(r).writeln; } int r() { return readln.chomp.to!int; }
D
import std.stdio, std.string, std.conv, std.math; void main() { writeln(readln.chomp.to!int ^^ 3); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.typecons; import std.numeric, std.math; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto M = RD; auto diff = M - N*2; if (diff > 0) { N += diff / 4; M -= diff / 2; } writeln(min(N, M/2)); stdout.flush(); }
D
import std.stdio, std.string, std.uni; void main() { string str = readln.chomp; foreach (x; str) { if (x.isUpper) { x.toLower.write; } else if (x.isLower) { x.toUpper.write; } else { x.write; } } writeln; }
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(){ string s = scan; if(s[$ - 1] == 's') s ~= 'e'; (s ~ 's').print; }
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 long[](t); foreach (ti; 0..t) { auto x = RD!int; auto y = RD!int; ans[ti] += min(x.abs, y.abs) * 2; auto d = max(x.abs, y.abs) - min(x.abs, y.abs); if (d == 0) continue; ans[ti] += (d-1) * 2 + 1; } 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 K; sc.scan(K); if (K % 2 == 0) { writeln((K * K / 2) / 2); } else { writeln( (K - (K / 2)) * ((K - (K / 2)) - 1)); } }
D
import std.stdio, std.conv, std.array, std.range, std.algorithm, std.string, std.math; void main(){ auto tmp = readln.chomp.split.to!(int[]); auto a = tmp[0], b = tmp[1]; char[100][100] field; foreach(i; 0 .. 50) foreach(j; 0 .. 100) field[i][j] = '.'; foreach(i; 50 .. 100) foreach(j; 0 .. 100) field[i][j] = '#'; foreach(k; 0 .. a - 1){ field[51 + (k / 50) * 2][(k % 50) * 2] = '.'; } foreach(k; 0 .. b - 1){ field[(k / 50) * 2][(k % 50) * 2] = '#'; } writeln("100 100"); foreach(i; 0 .. 100) field[i].to!string.writeln; }
D
import std.functional, std.algorithm, std.container, std.typetuple, std.typecons, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv; void main() { int[] NK = readln.chomp.split.to!(int[]); int N = NK[0], K = NK[1]; int[] A = readln.chomp.split.to!(int[]); int ans = 1; int cnt = K; ans += (N - K) / (K - 1); if ((N - K) % (K - 1) != 0) { ans++; } 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() { auto b = scanString.count!"a=='9'"!=0; if(b)end("Yes");end("No"); }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n, t; rd(n, t); int mn = 10 ^^ 9; foreach (_; 0 .. n) { int c, tm; rd(c, tm); if (tm <= t) { mn = min(mn, c); } } if (mn < 10 ^^ 9) { writeln(mn); } else { writeln("TLE"); } } 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.conv,std.string; void main() { auto a = readint(); auto b = readint(); auto h = readint(); writeln(((a+b)*h)/2); } int readint() { return to!(int)(chomp(readln())); }
D
import std.stdio, std.string, std.conv, std.array, std.algorithm; import std.uni, std.range, std.math, std.container, std.datetime; import core.bitop, std.typetuple, std.typecons; immutable long MOD = 1_000_000_007; alias tie = TypeTuple; alias triplet = Tuple!(int, int, int); void main(){ long m, n; readVars(m, n); auto ans = modpow(m, n, MOD); writeln(ans); } int gcd(int x, int y){ return y ? gcd(y, x % y) : x; } long modpow(long x, long y, long mod){ return y ? ((modpow(x, y>>1, mod)^^2) % mod * x^^(y&1)) % mod : 1; } void readVars(T...)(auto ref T args){ auto line = readln.split; foreach(ref arg ; args){ arg = line.front.to!(typeof(arg)); line.popFront; } if(!line.empty){ throw new Exception("args num < input num"); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math; void main() { readln; int[int] as; foreach (a; readln.split.to!(int[])) as[a] += 1; int r; foreach (a, n; as) { if (n >= a) { r += n - a; } else { r += n; } } writeln(r); }
D
import std.algorithm; import std.conv; import std.stdio; import std.string; void main() { auto abcxy = readln.split.map!( to!int ); writeln( solve( abcxy[ 0 ], abcxy[ 1 ], abcxy[ 2 ], abcxy[ 3 ], abcxy[ 4 ] ) ); } int solve( in int a, in int b, in int c, in int x, in int y ) { auto cc = c * 2; if( a + b < cc ) return ( a * x ) + ( b * y ); if( x < y ) { if( b < cc ) return ( cc * x ) + ( b * ( y - x ) ); else return ( cc * x ) + ( cc * ( y - x ) ); } else { if( a < cc ) return ( cc * y ) + ( a * ( x - y ) ); else return ( cc * y ) + ( cc * ( x - y ) ); } } unittest { assert( solve( 1500, 2000, 1600, 3, 2 ) == 7900 ); assert( solve( 1500, 2000, 1900, 3, 2 ) == 8500 ); assert( solve( 1500, 2000, 500, 90000, 100000 ) == 100000000 ); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; import std.numeric; // gcd, fft void main() { auto n = readln.chomp.to!size_t; auto t = new long[](n); foreach (i; 0..n) t[i] = readln.chomp.to!long; writeln(t.reduce!lcm); } auto lcm(long a, long b) { return a / gcd(a, b) * b; }
D
void main() { long n, k; rdVals(n, k); foreach (i; 1 .. 100) { if (k ^^ (i - 1) <= n && n < k ^^ i) { i.writeln; return; } } } 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 core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { long n = lread(); auto w = aryread(); long diff = w.sum(); foreach (i; 0 .. n - 1) { long beforesum; long aftersum; foreach (j, e; w) { if(j <= i) beforesum += e; if(j > i) aftersum += e; } diff = min(abs(aftersum - beforesum), diff); } diff.writeln(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nmq = readln.split.to!(int[]); auto N = nmq[0]; auto M = nmq[1]; auto Q = nmq[2]; int[] as, bs, cs; long[] ds; foreach (_; 0..Q) { auto abcd = readln.split.to!(int[]); as ~= abcd[0]-1; bs ~= abcd[1]-1; cs ~= abcd[2]; ds ~= abcd[3]; } auto ns = new int[](N); long solve(int i, int p) { long r; if (i == N) { foreach (k; 0..Q) { if (ns[bs[k]] - ns[as[k]] == cs[k]) r += ds[k]; } return r; } else { foreach (j; p..M+1) { ns[i] = j; r = max(r, solve(i+1, j)); } return r; } } writeln(solve(0, 1)); }
D
import std.algorithm; import std.range; import std.stdio; import std.math; import std.array; import std.string; import std.conv; /// 数列の読み込み auto readNums(T = int)() { return readln().chomp.split.map!(to!T).array; } /// 数の読み込み auto readNum(T = int)() { return readNums!T.front; } /// 指定行数読み込み auto readRows(size_t rows) { return generate(() => readln().chomp).take(rows).array; } void main() { immutable n = readNum(); const row1 = readNums(); const row2 = readNums(); int candies = 0; foreach(i; 0 .. n) { candies = max(candies, row1[0 .. i + 1].sum + row2[i .. $].sum); } writeln(candies); }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.container, std.math, std.typecons; import std.ascii, std.getopt; void main() { int Q, N; Q = readln.chomp.to!int; foreach (qi ; 0 .. Q) { N = readln.chomp.to!int; solve(N).writeln; } } int solve(int N) { int cnt; while (N > 9) { int[] made; string ns = N.to!string; foreach (i ; 1 .. ns.length) { int x = ns[0 .. i].to!int, y = ns[i .. $].to!int; made ~= x * y; } N = made.reduce!max; cnt++; } return cnt; }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(int[] a) { auto dq = new Deque!int(a.length); dq.pushBack(a[0]); foreach (e; a.drop(1)) { int lo = 0; int hi = dq.size() - 1; int best = hi + 1; while (lo <= hi) { int m = (lo + hi) / 2; if (dq[m] >= e) { best = min(best, m); hi = m - 1; } else lo = m + 1; } if (best == 0) dq.pushFront(e); else dq[best-1] = e; } return dq.size(); } void main() { int n = readint; int[] a; foreach (_; 0..n) a ~= readint; writeln(calc(a)); } class Deque(T) { private T[] _buffer; private size_t _size; private int _front; private int _back; this(size_t size) { _buffer = new T[size * 3]; _size = 0; _front = _back = cast(int)size * 3 / 2; } @property bool empty() { return _size == 0; } int size() { return cast(int)_size; } void pushFront(T elt) { _buffer[empty ? _front : --_front] = elt; _size++; } void pushBack(T elt) { _buffer[empty ? _back : ++_back] = elt; _size++; } T opIndex(size_t index) { return _buffer[_front + index]; } T opIndexAssign(T value, size_t index) { return _buffer[_front + index] = value; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; long P = 10^^9+7; long[10^^5*2+50] F, RF; long pow(long x, long n) { long y = 1; while (n) { if (n%2 == 1) y = (y * x) % P; x = x^^2 % P; n /= 2; } return y; } long inv(long x) { return pow(x, P-2); } void init() { F[0] = F[1] = 1; foreach (i, ref x; F[2..$]) x = (F[i+1] * (i+2)) % P; { RF[$-1] = 1; auto x = F[$-1]; auto k = P-2; while (k) { if (k%2 == 1) RF[$-1] = (RF[$-1] * x) % P; x = x^^2 % P; k /= 2; } } foreach_reverse(i, ref x; RF[0..$-1]) x = (RF[i+1] * (i+1)) % P; } long comb(N)(N n, N k) { if (k > n) return 0; auto n_b = F[n]; // n! auto nk_b = RF[n-k]; // 1 / (n-k)! auto k_b = RF[k]; // 1 / k! auto nk_b_k_b = (nk_b * k_b) % P; // 1 / (n-k)!k! return (n_b * nk_b_k_b) % P; // n! / (n-k)!k! } Tuple!(long, long)[] prime_division(long n) { Tuple!(long, long)[] res; foreach (long i; 2..10^^6+1) { if (n%i == 0) { long cnt; while (n%i == 0) { ++cnt; n /= i; } res ~= tuple(i, cnt); } } if (n != 1) res ~= tuple(n, 1L); return res; } void main() { init(); auto nm = readln.split.to!(long[]); auto N = nm[0]; auto M = nm[1]; auto ps = prime_division(M); long r = 1; foreach (p; ps) { r = r * comb(N+p[1]-1, N-1) % P; } writeln(r); }
D
void main() { int[] a = new int[5]; foreach (i; 0 .. 5) { a[i] = readln.chomp.to!int; } int k = readln.chomp.to!int; writeln(a[$-1] - a[0] > k ? ":(" : "Yay!"); } 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, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto D = RD; auto x = (D * 2 + 1); auto ans = N / x; writeln(ans + (N % x != 0 ? 1 : 0)); stdout.flush(); debug readln(); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.container; import std.array; import std.math; import std.range; import std.typecons; import std.ascii; bool check(string s) { int index = 0; foreach (c; s) { if (c=='(') { ++index; } else { if (index == 0) { return false; } --index; } } return index == 0; } void main() { int n = readln.chomp.to!int; string s = readln.chomp; string sub, ans; int index = 0; foreach (c; s) { final switch(c) { case '(': ans ~= sub; ans ~= '('; sub = ""; ++index; break; case ')': if (index == 0) { sub = sub ~ ")"; ans = "(" ~ ans; } else { sub = sub ~ ")"; --index; } break; } } //()())))((()(()))((((( foreach (i; 0..index) { sub ~= ")"; } ans ~= sub; assert(ans.check); writeln = ans; }
D
import std.stdio, std.string, std.conv; void main() { auto S = readln.chomp.to!(dchar[]); S[3] = '8'; writeln(S); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int a, b, c; rd(a, b, c); int[int] cnt; cnt[a]=1; if(b in cnt) writeln(c); else if(c in cnt) writeln(b); else writeln(a); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
void main() { long n, q; rdVals(n, q); auto uf = UnionFind(n); foreach (i; 0 .. q) { long com, x, y; rdVals(com, x, y); if (com) { writeln(uf.same(x, y) ? 1 : 0); } else { uf.unite(x, y); } } } struct UnionFind { long[] par, cnt; this(long n) { par.length = n, cnt.length = n; foreach (i; 0 .. n) { par[i] = i; } cnt[] = 1L; } long root(long x) { if (par[x] == x) return x; else return par[x] = root(par[x]); } bool same(long x, long y) { return root(x) == root(y); } void unite(long x, long y) { x = root(x), y = root(y); if (x == y) return; if (cnt[x] > cnt[y]) swap(x, y); cnt[y] += cnt[x]; par[x] = y; } long size(long x) { return cnt[root(x)]; } } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
void main() { string s = readln.chomp[0..$-2]; ulong len = s.length / 2; while (s[0..len] != s[len..$]) { s = s[0..$-2]; --len; } s.length.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n, k; scan(n, k); auto s = readln.chomp.to!(char[]); s[k - 1] = (s[k - 1] - 'A' + 'a').to!char; writeln(s); } int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new int[][](n, 0); foreach (i; 0 .. m) { int u, v; scan(u, v); if (is1indexed) { u--, v--; } adj[u] ~= v; if (isUndirected) { adj[v] ~= u; } } return adj; } alias Edge = Tuple!(int, "to", int, "cost"); Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new Edge[][](n, 0); foreach (i; 0 .. m) { int u, v, c; scan(u, v, c); if (is1indexed) { u--, v--; } adj[u] ~= Edge(v, c); if (isUndirected) { adj[v] ~= Edge(u, c); } } return adj; } void yes(bool b) { writeln(b ? "Yes" : "No"); } void YES(bool b) { writeln(b ? "YES" : "NO"); } T[] readArr(T)() { return readln.split.to!(T[]); } T[] readArrByLines(T)(int n) { return iota(n).map!(i => readln.chomp.to!T).array; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio; void main() { int cnt, max; foreach(c;readln) { if (c == 'A' || c == 'C' || c == 'T' || c == 'G') { ++cnt; if (cnt > max) max = cnt; } else { cnt = 0; } } max.write; }
D
import std.stdio, std.conv, std.string; import std.algorithm, std.array, std.container; import std.numeric, std.math; import core.bitop; T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T)(in string str) { return str.split.to!(T[]); } 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; } long mod = pow(10, 9) + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } struct UnionFind { void init(long n) { par = new long[](n); foreach (i; 0..n) par[i] = i; } long root(long i) { return par[i] == i ? i : (par[i] = root(par[i])); } bool same(long i, long j) { return root(i) == root(j); } void unite(long i, long j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; } long[] par; } void main() { auto N = RD!long; auto K = RD!long; writeln(N / 2 + (N % 2) >= K ? "YES" : "NO"); stdout.flush(); }
D
import std.stdio, std.string, std.conv, std.array, std.algorithm, std.range, std.typecons, std.numeric, std.math; void main() { ulong n = readln.strip.to!ulong; auto as = readln.strip.split.to!(ulong[]).array; auto bs = readln.strip.split.to!(ulong[]).array; ulong ans = 0; foreach (i; 0..n) { if (bs[i] >= as[i]) { ans += as[i]; bs[i] -= as[i]; if (bs[i] >= as[i+1]) { ans += as[i+1]; as[i+1] = 0; } else { ans += bs[i]; as[i+1] -= bs[i]; } } else { ans += bs[i]; } } writeln(ans); }
D
import std.algorithm, std.array, std.container, std.range, std.bitmanip; import std.numeric, std.math, std.bigint, std.random, core.bitop; import std.string, std.regex, std.conv, std.stdio, std.typecons; void main() { auto n = readln.chomp.to!size_t; auto cards = new bool[](52); foreach (_; 0..n) { auto c = readln.chomp; cards[cardStoI(c)] = true; } foreach (int i, c; cards) if (!c) writeln(cardItoS(i)); } int cardStoI(string s) { auto rd = s.split; auto suite = rd[0], rank = rd[1].to!int; return suiteStoI(suite) * 13 + rank - 1; } string cardItoS(int s) { return suiteItoS(s / 13) ~ " " ~ (s % 13 + 1).to!string; } const auto suites = ["S", "H", "C", "D"]; int suiteStoI(string s) { return suites.countUntil!(a => a == s).to!int; } string suiteItoS(int s) { return suites[s]; }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.ascii; void main() { auto s = readln.chomp.array; if (s.length == 3) { s.reverse(); } s.writeln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n; scan(n); auto cnt = new int[][](11, 11); foreach (i ; 1 .. n + 1) { int f = i.to!string.front - '0'; int b = i.to!string.back - '0'; cnt[f][b]++; } long ans; foreach (i ; 1 .. 10) { foreach (j ; 1 .. 10) { ans += 1L * cnt[i][j] * cnt[j][i]; } } writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } 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; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.to!(int[]); } void calc(int n, int a, int b) { while (true) { if (a + 1 < b) { // to right a++; } else if (a > 1) { // to left a--; } else { writeln("Borys"); break; } if (a < b - 1) { // to left b--; } else if (b + 1 < n) { // to right b++; } else { writeln("Alice"); break; } } } void main() { auto nab = readints; int n = nab[0], a = nab[1], b = nab[2]; calc(n, a, b); }
D
void main() { long n, m; rdVals(n, m); string s = rdStr; long[] lists; while (n > 0) { long ok; foreach_reverse (i; 1 .. m+1) { if (n - i < 0) continue; if (s[n-i] == '0') { lists ~= i; n -= i; ok = true; break; } } if (!ok) { writeln(-1); return; } } foreach_reverse (i, x; lists) { x.write; if (i == 0) writeln; else " ".write; } } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
void main() { int[] abc = readln.split.to!(int[]); writeln(abc.sum - 2 * abc.reduce!max == 0 ? "Yes" : "No"); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
void main() { long n, p; rdVals(n, p); string s = rdStr; long result; if (p == 2 || p == 5) { foreach (i, x; s) { if ((x - '0') % p) continue; result += i + 1; } result.writeln; return; } long[] exp = new long[n]; exp[n-1] = 1; foreach_reverse (i; 0 .. n-1) { exp[i] = 10 * exp[i+1] % p; } long[] list = new long[p]; long num = 0; foreach_reverse (i, x; s) { num = (num + (x - '0') * exp[i]) % p; ++list[num]; } result += list[0]; foreach (l; list) { result += (l * (l - 1)) >> 1; } result.writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; enum double eps = 1.0e-9; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
void main() { auto s = rs.to!(dchar[]); s[5] = s[13] = ' '; s.writeln; } // =================================== import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { int n = readint; int h = readint; int w = readint; int ans = (n - h + 1) * (n - w + 1); writeln(ans); }
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 AB = RD + RD; auto CD = RD + RD; writeln(AB == CD ? "Balanced" : AB > CD ? "Left" : "Right"); stdout.flush(); debug readln(); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; import std.range; void main(){ auto Z=readln.split.to!(int[]),a=Z[0],b=Z[1]; if((a*b)%2==0)writeln("Even"); else writeln("Odd"); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = 10 ^^ 5 + 1; auto P = new bool[](N); foreach (i; 2 .. N) P[i] = f(i); auto PP = new bool[](N); foreach (i; 3 .. N) PP[i] = ((i & 1) != 0) && P[i] && P[(i + 1) / 2]; auto S = new long[](N + 1); foreach (i; 0 .. N) S[i + 1] = S[i] + PP[i]; long Q = lread(); foreach (_; 0 .. Q) { long l, r; scan(l, r); writeln(S[r + 1] - S[l - 1]); } } bool f(long x) { foreach (i; 2 .. x) { if (x < i * i) return true; if (x % i == 0) return false; } return true; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto ab = readln.split.to!(int[]); auto A = ab[0]; auto B = ab[1]; writeln(B % A == 0 ? A + B : B - A); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; immutable long MOD = 10^^9 + 7; void main() { auto K = readln.chomp; auto D = readln.chomp.to!int; auto N = K.length.to!int; auto dp = new long[][][](N+1, D, 2); dp[0][0][0] = 1; foreach (i; 0..N) { foreach (j; 0..D) { foreach (k; 0..2) { int d = 10; if (!k) d = K[i] - '0' + 1; foreach (x; 0..d) { (dp[i+1][(j+x)%D][k||x<K[i]-'0'] += dp[i][j][k]) %= MOD; } } } } long ans = (dp[N][0][0] + dp[N][0][1] - 1) % MOD; ans = (ans + MOD) % MOD; ans.writeln; }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { int n, w; scan(n, w); auto dp = new int[](w + 1); foreach (i ; 1 .. n + 1) { int val, wei, m; scan(val, wei, m); for (int k; m > 0; k++) { int take = min(m, 1<<k); m -= take; foreach_reverse (j ; take * wei .. w + 1) { dp[j] = max(dp[j], dp[j - take * wei] + val * take); } } } writeln(dp[w]); } 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.conv, std.stdio; import std.algorithm, std.array, std.range, std.string; void main() { immutable n = readln.chomp.to!size_t, s = readln.chomp; auto count = ['R': long(0), 'G': 0, 'B': 0]; foreach (l; s) count[l] += 1; auto answer = count['R'] * count['G'] * count['B']; foreach (i; 0..s.length) foreach (k; i+2..s.length) { if ((i ^ k) & 1) continue; immutable j = (i + k) >> 1; if (s[i] != s[j] && s[j] != s[k] && s[k] != s[i]) answer -= 1; } answer.writeln; }
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(); auto A = new int[N]; foreach (i; 0 .. N) { A[i] = readInt(); } int ans; if (N == 1) { ans = 1; } else { int num; foreach (i; 0 .. N - 1) { if (A[i] < A[i + 1]) { ++num; } } if (K == 1) { if (num == 0) { ans = 1; } else { ans = -1; } } else { ans = (num + (K - 1) - 1) / (K - 1); chmax(ans, 1); } } writeln(ans); } } } catch (EOFException e) { } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { string s; scan(s); writeln(s[0] == s[1] || s[1] == s[2] || s[2] == s[3] ? "Bad" : "Good"); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
void main() { string x = readln.chomp; auto g = x.group.array; long slen; long len = x.length; foreach (t; g) { if (t[0] == 'S') { slen += t[1].to!long; } else { long lmin = min(slen, t[1].to!long); len -= 2 * lmin; slen -= lmin; } } len.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio; import std.string; import std.format; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.concurrency; import std.traits; import std.uni; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; enum long INF = long.max/5; enum long MOD = 10L^^9+7; void main() { string s = readln.chomp; string t = readln.chomp; long K = 2000000; string x = ""; while(x.length < K) { x ~= t; } string y = ""; while(y.length <= x.length) { y ~= s; } long l = 0; long r = K.floor(t.length) + 1; while(r - l > 1) { long c = (l + r)/2; string z = x[0..t.length*c]; if (searchPattern(y, z).empty) { r = c; } else { l = c; } } if (l == K.floor(t.length)) { writeln(-1); } else { writeln(l); } } /* KMP法 @return: out[0] == -1 out[i] == max { ys.length | ys is a prefix of xs[0..i] and a suffix of xs[0..i] } 計算量 - 全体: O(n) - 各ステップ: 最悪 O(log n) */ long[] solveKmp(T)(T[] xs) { long n = xs.length; long[] kmp = new long[n+1]; long[] mp = new long[n+1]; long j = -1; kmp[0] = mp[0] = j; foreach(i; 0..n) { while(j >= 0 && xs[i] != xs[j]) { j = kmp[j]; } kmp[i+1] = mp[i+1] = ++j; if (i+1 < n && xs[i+1] == xs[j]) { kmp[i+1] = kmp[j]; } } return mp; } /* textからpatternに一致する開始インデックスを列挙する - O(n + m) */ long[] searchPattern(T)(T[] text, T[] pattern) { long n = text.length; long m = pattern.length; assert(m <= n); long[] indices = []; long[] kmp = solveKmp!T(pattern); long i = 0; // index for `text` long j = 0; // index for `pattern` while(i < n) { while(j >= 0 && text[i] != pattern[j]) { j = kmp[j]; } j++; i++; if (j == m) { assert(i >= m); indices ~= i-m; j = kmp[j-1]; i--; } } return indices; } // ---------------------------------------------- void times(alias fun)(long n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(long n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { // `(x+y-1)/y` will only work for positive numbers ... T t = x / y; if (y > 0 && t * y < x) t++; if (y < 0 && t * y > x) t++; return t; } T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { T t = x / y; if (y > 0 && t * y > x) t--; if (y < 0 && t * y < x) t--; return t; } ref T ch(alias fun, T, S...)(ref T lhs, S rhs) { return lhs = fun(lhs, rhs); } unittest { long x = 1000; x.ch!min(2000); assert(x == 1000); x.ch!min(3, 2, 1); assert(x == 1); x.ch!max(100).ch!min(1000); // clamp assert(x == 100); x.ch!max(0).ch!min(10); // clamp assert(x == 10); } mixin template Constructor() { import std.traits : FieldNameTuple; this(Args...)(Args args) { // static foreach(i, v; args) { foreach(i, v; args) { mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v; } } } void scanln(Args...)(auto ref Args args) { enum sep = " "; enum n = Args.length; enum fmt = n.rep!(()=>"%s").join(sep); string line = readln.chomp; static if (__VERSION__ >= 2074) { line.formattedRead!fmt(args); } else { enum argsTemp = n.iota.map!( i => "&args[%d]".format(i) ).join(", "); mixin( "line.formattedRead(fmt, " ~ argsTemp ~ ");" ); } } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // popcnt with ulongs was added in D 2.071.0 static if (__VERSION__ < 2071) { ulong popcnt(ulong x) { x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L); x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L); x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL); x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL); x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL); x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL); return x; } }
D
import core.stdc.stdio; import std.typecons; import std.stdio; import std.algorithm; import std.math; void main(){ int v,e; scanf("%d%d",&v,&e); int[][] graph = new int[][v]; foreach(i;0..e){ int a,b; scanf("%d%d",&a,&b); graph[--a]~=--b; graph[b]~=a; } int[] d=new int[v]; int[] s=new int[v]; int[] t=new int[v]; void dfs(int i,int b,ref int e,ref int f){ if(d[i]) return; d[i]=b; e++; foreach(c;graph[i]) dfs(c,3-b,f,e); } foreach(i;0..v) dfs(i,1,s[i],t[i]); foreach(i;0..v) foreach(c;graph[i]) if(d[i]==d[c]){ printf("-1\n"); return; } int g; int[int] h; foreach(i;0..v) if(s[i]||t[i]){ g+=min(s[i],t[i]); int w=abs(s[i]-t[i]); if(w in h) h[w]++; else h[w]=1; } int[] dp=new int[v+1]; static immutable int inf=1145141919; dp[]=inf; dp[0]=0; foreach(q;h.byKeyValue){ foreach(ref x;dp) if(x<inf)x=0; foreach(i;q.key..v+1) if(dp[i-q.key]<q.value) dp[i]=min(dp[i],dp[i-q.key]+1); } // writeln(g," ",dp); long ans=0; foreach(i,x;dp) if(x<inf) ans=max(ans,cast(long)(g+i)*(v-g-i)); printf("%lld\n",ans-e); }
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 char[][](t); foreach (ti; 0..t) { auto n = RD!int; auto a = RDA(-1); ans[ti].length = n; ans[ti][] = '0'; auto cnt = new long[](n); foreach (i; 0..n) { ++cnt[a[i]]; } bool ok = true; foreach (i; 0..n) { if (cnt[i] == 0) { ok = false; break; } } ans[ti][0] = ok ? '1' : '0'; foreach (i; 0..n-1) { ans[ti][n-i-1] = cnt[i] != 0 ? '1' : '0'; if (a.front == i) a.popFront; else if (a.back == i) a.popBack; else break; if (cnt[i] >= 2) break; } } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.7.4" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; int main() { Scanner sc = new Scanner(stdin); string s; sc.read(s); string t = "AKIHABARA"; int cnt = 0; foreach (c; s) { while (true) { if (cnt == t.length.to!int || (t[cnt] != c && t[cnt] != 'A')) { writeln("NO"); return 0; } if (t[cnt] == c) break; cnt++; } cnt++; } if (cnt < t.length.to!int - 1) { writeln("NO"); return 0; } writeln("YES"); return 0; } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; // import dcomp.array; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { FastAppender!(E[]) buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/array.d */ // module dcomp.array; T[N] fixed(T, size_t N)(T[N] a) {return a;} struct FastAppender(A, size_t MIN = 4) { import std.algorithm : max; import std.conv; import std.range.primitives : ElementEncodingType; import core.stdc.string : memcpy; private alias T = ElementEncodingType!A; private T* _data; private uint len, cap; @property size_t length() const {return len;} bool empty() const { return len == 0; } void reserve(size_t nlen) { import core.memory : GC; if (nlen <= cap) return; void* nx = GC.malloc(nlen * T.sizeof); cap = nlen.to!uint; if (len) memcpy(nx, _data, len * T.sizeof); _data = cast(T*)(nx); } void free() { import core.memory : GC; GC.free(_data); } void opOpAssign(string op : "~")(T item) { if (len == cap) { reserve(max(MIN, cap*2)); } _data[len++] = item; } void insertBack(T item) { this ~= item; } void removeBack() { len--; } void clear() { len = 0; } ref inout(T) back() inout { assert(len); return _data[len-1]; } ref inout(T) opIndex(size_t i) inout { return _data[i]; } T[] data() { return (_data) ? _data[0..len] : null; } } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } import core.bitop : popcnt; static if (!__traits(compiles, popcnt(ulong.max))) { public import core.bitop : popcnt; int popcnt(ulong v) { return popcnt(cast(uint)(v)) + popcnt(cast(uint)(v>>32)); } } bool poppar(ulong v) { v^=v>>1; v^=v>>2; v&=0x1111111111111111UL; v*=0x1111111111111111UL; return ((v>>60) & 1) != 0; } /* This source code generated by dcomp and include dcomp's source code. dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp) dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt) */
D
import std.stdio;import std.conv;import std.algorithm;import std.array; auto raia(){ return readln().split().map!(to!int); } //read as a int[] static const int mint =60; static const int hour = 60*mint; void main() { auto it = raia(); writeln( it[0] /hour,":",it[0]%hour/mint,":",it[0]%mint); }
D
import std.stdio, std.conv, std.string; import std.algorithm, std.array, std.container; import std.numeric, std.math; import core.bitop; T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; } string RDR()() { return readln.chomp; } long mod = pow(10, 9) + 7; long moda(long x, long y) { return (x + y) % mod; } long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; } long modm(long x, long y) { return (x * y) % mod; } void main() { auto N = RD!long; long[long] cnt; foreach (i; 0..N) { ++cnt[RD!long]; } long ans; foreach (key; cnt.keys) { if (cnt[key] % 2 == 1) ++ans; } writeln(ans); stdout.flush(); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; auto s=readln.chomp.to!(char[]); bool[char] v; v['a']=true; v['i']=true; v['u']=true; v['e']=true; v['o']=true; int n=s.length.to!(int); foreach(i; 0..n){ if((s[i] in v)==null){ if(s[i]=='n'){ // }else{ if(i+1<n){ if((s[i+1] in v)==null){ writeln("NO"); return; } }else{ writeln("NO"); return; } } }else{ // } } writeln("YES"); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x) e=l[i].to!(typeof(e)); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto S = readln.chomp; auto N = S.length.to!int; auto X = new int[](N+1); int[][int] Y; X[0] = 0; Y[0] = [0]; foreach (i; 0..N) { int c = S[i] - 'a'; X[i+1] = X[i] ^ (1 << c); Y[X[i+1]] ~= i+1; } auto dp = new int[](N+1); dp.fill(1 << 29); dp[0] = 0; int[int] used; foreach (i; 0..N) { foreach (j; 0..26) { int next = X[i] ^ (1 << j); if (next in Y && (!(next in used) || used[next] > dp[i])) { used[next] = dp[i]; foreach (m; Y[next]) { dp[m] = min(dp[m], dp[i]+1); } } } if (X[i] in Y && (!(X[i] in used) || used[X[i]] > dp[i])) { used[X[i]] = dp[i]; foreach (m; Y[X[i]]) { dp[m] = min(dp[m], dp[i]+1); } } } dp[N].writeln; }
D
import std; // dfmt off T lread(T=long)(){return readln.chomp.to!T;} T[] lreads(T=long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);} void arywrite(T=long)(T[] ary){ary.map!(text).join(' ').writeln;} void scan(TList...)(ref TList Args){auto line = readln.split; foreach (i,T;TList){T val=line[i].to!(T);Args[i]=val;}} void dprint(TList...)(TList Args){debug{auto s=new string[](TList.length); static foreach(i,a;Args) s[i]=text(a);s.map!(text).join(' ').writeln;}} alias sread=()=>readln.chomp();enum MOD =10^^9+7; alias PQueue(T,alias less="a<b") = BinaryHeap!(Array!T,less); // dfmt on void main() { long X = lread(); long r = 5000; foreach (a; -r .. r + 1) foreach (b; -r .. r + 1) if ((a ^^ 5) - (b ^^ 5) == X) { arywrite([a, b]); return; } }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio; void main() { auto s = readln.split.map!(to!int); auto H = s[0]; auto N = s[1]; auto A = iota(N).map!(_ => readln.split.map!(to!int).array).array; auto dp = new int[](H+1); dp[] = 1 << 29; dp[0] = 0; foreach (i; 0..H) foreach (a; A) { if (i + a[0] <= H) dp[i + a[0]] = min(dp[i + a[0]], dp[i] + a[1]); else dp[H] = min(dp[H], dp[i] + a[1]); } dp[H].writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { bool n, w, s, e; foreach (c; readln.chomp) switch (c) { case 'N': n = true; break; case 'W': w = true; break; case 'S': s = true; break; case 'E': e = true; break; default: } writeln((n==s)&&(w==e) ? "Yes" : "No"); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);} void readM(T)(size_t r,size_t c,ref T[][]t){t=new T[][](r);foreach(ref v;t)readA(c,v);} void main() { int n; readV(n); int[][] a; readM(n, 3, a); auto dp = new int[][](n, 3); dp[0] = a[0]; foreach (i; 1..n) foreach (j; 0..3) foreach (k; 0..3) if (j != k) dp[i][j] = max(dp[i][j], dp[i-1][k] + a[i][j]); writeln(dp[$-1].reduce!max); }
D
import std; long calcEven(int[] a) { assert(a.length % 2 == 0); long sum1 = 0; long sum2 = 0; for (int i = 0; i < a.length; i += 2) { sum1 += a[i]; sum2 += a[i+1]; } return max(sum1, sum2); } long calc(int[] a) { // if (a.length % 2 == 0) return calcEven(a); alias Key = Tuple!(int, int); long ans = long.min; long rec(int p, int rest, ref long[Key] memo) { if (p >= a.length) { return 0; } if (Key(p, rest) in memo) { long m = memo[Key(p, rest)]; return m; } long x = rec(p + 2, rest, memo) + a[p]; if (rest > 0) { x = max(x, rec(p + 1, rest - 1, memo)); } return memo[Key(p, rest)] = x; } long[Key] memo; return rec(0, a.length % 2 == 0 ? 1 : 2, memo); } void main() { readint; auto a = readints; writeln(calc(a)); } void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int;
D
import std.stdio, std.array, std.conv, std.string, std.algorithm, std.range; void main() { foreach (string line; lines(stdin)) { auto d = line.chomp.to!int; int ans = 0; for (int i = d; i < 600; i += d) { ans += i * i * d; } ans.writeln; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.range; enum long PRIM = 10^^9+7; void main() { auto nm = readln.chomp.split(" ").map!(to!long); auto n = nm[0]; auto m = nm[1]; auto xs = readln.chomp.split(" ").map!(to!long); auto ys = readln.chomp.split(" ").map!(to!long); long xa; foreach (x; 0..n) xa = (xa + x*xs[x] - (n-x-1)*xs[x])%PRIM; long ya; foreach (y; 0..m) ya = (ya + y*ys[y] - (m-y-1)*ys[y])%PRIM; writeln((xa*ya)%PRIM); }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); int s = 0; foreach (_; 0 .. n) { int l, r; rd(l, r); s += (r - l + 1); } writeln(s); } 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; import std.string; import std.conv; import std.algorithm; import std.math; import std.regex; import std.range; void main() { char[] a; foreach(i; 0..3) { a ~= readln.chomp[i]; } a.writeln; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto rd = readln.split.to!(int[]), n = rd[0], m = rd[1]; auto a = m * 1900 + (n-m) * 100; writeln(a * 2^^m); }
D
auto solve(long target, in int[] cards) { import std.algorithm, std.range; immutable n = cards.length, m = cards.sum; auto dp = new long[][](n+1, m+1); dp[0][0] = 1; foreach (card; cards) foreach_reverse (usedCards; 0..n) foreach (currentSum; 0..m+1-card) dp[usedCards+1][currentSum+card] += dp[usedCards][currentSum]; return 1.iota(n+1).map!(i => (i*target <= m) ? dp[i][cast(size_t)(i*target)] : 0).sum; } unittest { import std.range, std.array; assert (8.solve([7, 9, 8, 9]) == 5); assert (8.solve([6, 6, 9]) == 0); assert (5.solve([3, 6, 2, 8, 7, 6, 5, 9]) == 19); assert (3.solve(3.repeat.take(33).array) == 8589934591); } void main() { import std.stdio, std.string, std.array, std.conv; immutable target = readln.chomp.split[1].to!long, cards = readln.chomp.split.to!(int[]); target.solve(cards).writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; alias A = Tuple!(int, "i", int, "j"); A[300*300+1] AS; long[300*300+1] DS; void main() { auto hwd = readln.split.to!(int[]); auto H = hwd[0]; auto W = hwd[1]; auto D = hwd[2]; foreach (j; 0..H) foreach(i, e; readln.split.to!(int[])) AS[e] = A(i.to!int, j); foreach (i; 1..D+1) { long d; DS[i] = d; while (i <= H*W-D) { auto a = AS[i]; i += D; auto ad = AS[i]; d += abs(a.i - ad.i) + abs(a.j - ad.j); DS[i] = d; } } auto Q = readln.chomp.to!int; foreach (_; 0..Q) { auto lr = readln.split.to!(int[]); auto L = lr[0]; auto R = lr[1]; writeln(DS[R] - DS[L]); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto s = readln.chomp; int i; foreach (c; s) { if (i == 0 && c == 'C') ++i; if (i == 1 && c == 'F') ++i; } writeln(i == 2 ? "Yes" : "No"); }
D
import std.stdio; void main() { int i; for(i=0;i<1000;i++) { writeln("Hello World"); } }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(i;0..n){t[i]=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t)v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}} void main() { int n; readV(n); int d, x; readV(d, x); int[] a; readM(n, a); writeln(a.map!(ai => (d+ai-1)/ai).sum + x); }
D
import std.stdio, std.conv, std.array, std.string; import std.algorithm; import std.container; import std.range; import core.stdc.stdlib; import std.math; void main() { auto N = readln.chomp.to!ulong; auto An = readln.split.to!(int[]); auto Bn = readln.split.to!(int[]); auto Cn = 0 ~ readln.split.to!(int[]); int prev = -1; int satisfaction = 0; foreach(i; An) { i--; satisfaction += Bn[i]; if (prev == i-1) satisfaction += Cn[i]; prev = i; } writeln(satisfaction); }
D
void main() { rs.count!"a == '1'".writeln; } // =================================== 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
void main(){ int n = _scan(); writeln("ABC", n); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.algorithm; import std.range; import std.stdio; void main(){ readln(); readln().split().retro().join(" ").writeln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto n = RD!int; auto s = new string[](n); int[2][][int] info1; int[2][][int] info2; auto toNum = ['a':0, 'i':1, 'u':2, 'e':3, 'o':4]; foreach (i; 0..n) { s[i] = RD!string; int last; int cnt; foreach (c; s[i]) { auto num = toNum.get(c, -1); if (num != -1) { ++cnt; last = num; } } info1[cnt] ~= [i, last]; info2[cnt*10+last] ~= [i, last]; } int[] ansSec; auto used = new bool[](n); auto keys2 = info2.keys; foreach (key2; keys2) { auto arr2 = info2[key2]; auto len2 = arr2.length; foreach (i; 0..len2/2) { auto j = arr2[i*2][0]; auto k = arr2[i*2+1][0]; ansSec ~= [j, k]; used[j] = true; used[k] = true; } } int[] ansFst; auto keys1 = info1.keys; foreach (key1; keys1) { auto arr1 = info1[key1]; auto len1 = arr1.length; int[] tmp; foreach (i; 0..len1) { auto j = arr1[i][0]; if (used[j]) continue; tmp ~= j; if (tmp.length == 2) { ansFst ~= tmp; tmp.length = 0; } } } auto diff = max(0, (cast(int)ansSec.length - cast(int)ansFst.length) / 4); foreach (i; 0..diff) { ansFst ~= [ansSec[$-2], ansSec[$-1]]; ansSec.length = ansSec.length - 2; } auto len = min(ansFst.length, ansSec.length); writeln(len/2); foreach (i; 0..len) { auto j = ansFst[i]; auto k = ansSec[i]; writeln(s[j], " ", s[k]); } stdout.flush(); debug readln(); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.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, M; scan(N, M); bool[8][8] G; foreach (_; 0 .. M) { long a, b; scan(a, b); G[a - 1][b - 1] = G[b - 1][a - 1] = true; } // writeln(G); auto ary = [1, 2, 3, 4, 5, 6, 7].take(N - 1).array; long cnt; do { // writeln(ary); bool b = G[0][ary[0]]; foreach (i; 0 .. N - 2) b = b && G[ary[i]][ary[i + 1]]; cnt += b; } while (nextPermutation(ary)); writeln(cnt); }
D
import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } class Cell{ char c; int i, j; int _value; bool isVisited = false; Cell[] adjCells; this(char c, int i, int j){ this.c = c; this.i = i; this.j = j; } int value(int x){ this._value = x; this.isVisited = true; return this._value; } int value(){ return this._value; } void add(Cell ce){ this.adjCells ~= ce; ce.adjCells ~= this; } void runUntil(int k){ if(k > 0){ foreach(ce; this.adjCells){ if(ce.c == '.' && (!ce.isVisited || ce.value > this.value + 1)){ ce.value = this.value + 1; ce.runUntil(k - 1); } } } } } X[] q; class X{ Cell cell; int value; this(Cell cell, int value){ this.cell = cell; this.value = value; } void go(){ cell.value = this.value; if(this.value > 0){ foreach(ce; cell.adjCells){ if(ce.c == '.' && !ce.isVisited){ ce.isVisited = true; q ~= new X(ce, this.value - 1); } } } q = q[1 .. $]; } } void main(){ int h = read.to!int; int w = read.to!int; int k = read.to!int; Cell[][] cells; foreach(i; 0 .. h){ char[] cs = readln.chomp.to!(char[]); Cell[] xs; cells ~= xs; foreach(j, c; cs){ Cell x = new Cell(c, i, cast(int) j); cells[i] ~= x; if(i > 0) x.add(cells[i - 1][j]); if(j > 0) x.add(cells[i][j - 1]); } } foreach(i; 0 .. h) foreach(j; 0 .. w) if(cells[i][j].c == 'S'){ q ~= new X(cells[i][j], k); } while(q.length){ q[0].go(); } debug writeln("k=", k); debug foreach(i; 0 .. h) cells[i].map!(x => x.isVisited? '*': x.c).writeln; int minToWall = 1000; foreach(i; 0 .. h) foreach(j; 0 .. w) if(cells[i][j].isVisited){ int t = min(i, h - 1 - i, j, w - 1 - j); if(t < minToWall) minToWall = t; } debug writeln("minToWall: ", minToWall); int ans = 1 + (minToWall + (k - 1)) / k; 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 = "%.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; writeln(x < 1200 ? "ABC" : "ARC"); stdout.flush(); debug readln(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto ab = readln.split.to!(long[]); auto a = ab[0]; auto b = ab[1]; if (a > b) swap(a, b); long r; while (b) { r *= 10; r += a; --b; } writeln(r); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { long N, M; scan(N, M); auto T = new bool[long][](N); foreach (_; 0 .. M) { long a, b; scan(a, b); T[a - 1][b - 1] = T[b - 1][a - 1] = true; } foreach (i; 0 .. N) if (0 in T[i] && N - 1 in T[i]) { writeln("POSSIBLE"); return; } writeln("IMPOSSIBLE"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; long[] as; auto MEMO = new long[](N+1); foreach (a; readln.split.to!(long[])) { ++MEMO[a]; as ~= a; } long r; foreach (n; MEMO) { r += n * (n-1) / 2; } foreach (a; as) { auto n = MEMO[a]; writeln(r - n * (n-1) / 2 + (n-1) * (n-2) / 2); } }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { int n, k, q; scan(n, k, q); auto t = new int[n]; for (int i = 0; i < q; i++) { int a = readint - 1; t[a]++; } for (int i = 0; i < n; i++) { int point = k - (q - t[i]); writeln(point > 0 ? "Yes" : "No"); } }
D