code
stringlengths
4
1.01M
language
stringclasses
2 values
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 = 10L^^15; enum mod = 10L ^^ 9 + 7; void main() { int n; scan(n); auto a = readln.split.to!(int[]); auto dp = new long[][][](n + 1, n + 1, 2); fillAll(dp, -1); long dfs(int l, int r, int p) { if (dp[l][r][p] != -1) { return dp[l][r][p]; } if (r - l == 1) { return dp[l][r][p] = (-1)^^p * a[l]; } dp[l][r][p] = (p ? min(dfs(l, r-1, p^1) - a[r-1], dfs(l+1, r, p^1) - a[l]) : max(a[r-1] + dfs(l, r-1, p^1), a[l] + dfs(l+1, r, p^1))); return dp[l][r][p]; } auto ans = dfs(0, n, 0); debug { foreach (l ; 0 .. n) { foreach (r ; 0 .. n) { foreach (p ; 0 .. 2) { if (dp[l][r][p] != -1) { stderr.writefln("%d, %d, %d = %d", l, r, p, dp[l][r][p]); } } } } } writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.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() { readln; readln.chomp.count("ABC").writeln; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
void main() { long h1, m1, h2, m2, k; rdVals(h1, m1, h2, m2, k); long t1 = 60 * h1 + m1, t2 = 60 * h2 + m2; max(0, t2-t1-k).writeln; } enum long mod = 10L^^9 + 7; enum long inf = 1L << 60; enum double eps = 1.0e-9; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
void main(){ long n = _scan!long(); long[] a = _scanln!long(); long[long] dic; bool fl = true; foreach(i; 0..n){ if(dic.get(a[i], 0) == 0)dic[a[i]]++; else{ writeln("NO"); return; } } writeln("YES"); } 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.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 n = RD!int; auto a = RDA!int; auto q = RD!int; auto cnt = new int[](10^^5+1); long a2, a4, a6, a8; void update1(int x) { if (cnt[x] == 8) { ++a8; --a6; } else if (cnt[x] == 6) { ++a6; --a4; } else if (cnt[x] == 4) { ++a4; --a2; } else if (cnt[x] == 2) { ++a2; } } void update2(int x) { if (cnt[x] == 7) { --a8; ++a6; } else if (cnt[x] == 5) { --a6; ++a4; } else if (cnt[x] == 3) { --a4; ++a2; } else if (cnt[x] == 1) { --a2; } } foreach (i; 0..n) { ++cnt[a[i]]; update1(a[i]); } foreach (i; 0..q) { auto s = RD!string; auto x = RD!int; if (s == "+") { ++cnt[x]; update1(x); } else { --cnt[x]; update2(x); } auto ans = a4 >= 1 || a6 >= 1 || a8 >= 1; ans &= a2 + a4*2 + a6*3 + a8*4 >= 4; writeln(ans ? "YES" : "NO"); } stdout.flush; debug readln; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; version(unittest) {} else void main() { string line; while ((line = readln) !is null) { auto rd = line.split.to!(int[]), a = rd[0], b = rd[1]; writeln((a + b).numDigits); } } int numDigits(int n) { auto r = 1; for (; n >= 10; n /= 10) ++r; return r; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "a", long, "b"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long h, w; scan(h, w); long ans = INF; foreach (i; iota(1, h)) { long a, b, c, r; a = i * w; r = h * w - a; if ((h - i) % 2 == 0 || w % 2 == 0) { b = c = r / 2; ans = min(ans, abs(a - b)); } else { b = (max(h, w) / 2) * min(h - i, w); c = r - b; ans = min(ans, only(abs(a - b), abs(a - c), abs(b - c)).array().reduce!(max)); } } foreach (j; iota(1, w)) { long a, b, c, r; a = j * h; r = h * w - a; if (h % 2 == 0 || (w - j) % 2 == 0) { b = c = r / 2; ans = min(ans, abs(a - b)); } else { b = max(h, w) / 2 * min(h, w - j); c = r - b; ans = min(ans, only(abs(a - b), abs(a - c), abs(b - c)).array().reduce!(max)); } } ans.writeln(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.algorithm, std.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 readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}} void main() { int n, a, b; readV(n, a, b); int[] h; readC(n, h); auto calc(int x) { auto t = h.dup; t[] -= b*x; auto y = t.filter!"a>0".map!(ti => (ti+a-b-1)/(a-b).to!long).sum; return y <= x; } auto bs = iota(1, (h.reduce!max+b-1)/b+1).map!(x => tuple(x, calc(x))).assumeSorted!"a[1] < b[1]"; writeln(bs.upperBound(tuple(0, false)).front[0]); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto S = RD!string; long[4] used; foreach (i; 0..S.length) { if (S[i] == 'N') used[0] = 1; else if (S[i] == 'W') used[1] = 1; else if (S[i] == 'S') used[2] = 1; else used[3] = 1; } writeln(used[0] - used[2] == 0 && used[1] - used[3] == 0 ? "Yes" : "No"); stdout.flush(); debug readln(); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { int n = readint; int[][] a; for (int i = 0; i < n; i++) { a ~= readints; } auto dp = new int[][](n, 3); dp[0][0] = a[0][0]; dp[0][1] = a[0][1]; dp[0][2] = a[0][2]; for (int i = 1; i < n; i++) { for (int j = 0; j < 3; j++) { // 前日の行動 for (int k = 0; k < 3; k++) { // 今日の行動 if (j == k) continue; // 2 日連続で同じ活動はできない dp[i][k] = max(dp[i][k], dp[i - 1][j] + a[i][k]); } } } writeln(dp[n - 1].reduce!max); }
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 l = RD; auto r = RD; ans[ti] = r % max(r / 2 + 1, l); } foreach (e; ans) 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 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 K = RD; auto S = RD; long ans; foreach (i; 0..K+1) { foreach (j; 0..K+1) { auto x = S - cast(long)(i+j); if (x < 0) break; if (x <= K) ++ans; } } writeln(ans); stdout.flush(); debug readln(); }
D
import core.bitop; import std.algorithm; import std.array; import std.ascii; import std.container; import std.conv; import std.format; import std.math; import std.random; import std.range; import std.stdio; import std.string; import std.typecons; void main() { int n = readln.chomp.to!int; long[] a = readln.chomp.split.map!(to!long).array; long sum = a.sum - a.front; long sub = a.front; long ans = abs(sum - sub); foreach (v; a[1..$-1]) { sum -= v; sub += v; ans = min(ans, abs(sum - sub)); } 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 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; } 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) ans.modm(mul); p >>= 1; mul.modm(mul); } return ans; } ++n; fact = new long[2][](n); fact[0][0] = 1; foreach (i; 1..n) { fact[i][0] = fact[i-1][0]; fact[i][0].modm(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]; fact[i][1].modm(i+1); } } long get(long n, long r) { long res = fact[n][0]; res.modm(fact[r][1]); res.modm(fact[n-r][1]); return res; } long[2][] fact; } void main() { auto X = RD; auto Y = RD; if ((X+Y) % 3 == 0 && min(X, Y)*2 >= max(X, Y)) { Cmb cmb; cmb.init(max(X, Y)); auto num = (X+Y) / 3; auto a = min(X, Y); auto b = max(X, Y); auto d = (b - a) / 2; auto ans = cmb.get(num, num/2 - d); writeln(ans); } else writeln(0); stdout.flush(); debug readln(); }
D
void main(){ char c = _scan!char(); (c+1).to!char.writeln(); } 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.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() { if(scanString==scanString)end("H");end("D"); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto a=new int[](1_000_000); foreach(_; 0..n){ int l, r; rd(l, r); a[l-1]++; a[r]--; } foreach(i; 1..a.length){ a[i]+=a[i-1]; } int c=0; foreach(e; a)if(e>0) c++; writeln(c); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; int[string] h; void f (int dx) { int n = readln.strip.to! (int); foreach (i; 0 .. n) { string s = readln.strip(); if (s in h) { h[s] = h[s] + dx; } else { h[s] = dx; } } } void main() { f (1); f (-1); int r = 0; foreach (k, v; h) { r = max (r, v); } writeln (r); }
D
import std; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long A, B, N; scan(A, B, N); auto f = (long x) => (A * x) / B - A * (x / B); if (N < B) { writeln(f(N)); return; } writeln(max(f(B), f(B - 1))); }
D
import std.stdio; import std.algorithm; import std.string; import std.functional; import std.array; import std.conv; import std.math; void main(){ while(true){ int n,m; auto s = split(readln()).to!(int[]); n = s[0]; m = s[1]; if(n==0&&m==0) break; int count; int d; int i = -1; bool[1001] al; for(int j=0;j<=1000;j++){ al[j] = false; } while(true){ i = (i+1)%n; if(!al[i]) count++; else continue; if(count%m==0){ al[i] = true; d++; } if(d==n-1) break; } for(int j=0;j<n;j++){ if(!al[j]) writeln(j+1); } } }
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 x = l[0]; auto y = l[1]; auto s = l[2]; if(x == 0 && y == 0 && s == 0) { return; } int ans; for(int i = 1; i*(100+x)/100 < s; ++i) { int t = (s-i*(100+x)/100)*100/(100+x); while((t+1)*(100+x)/100 <= (s-i*(100+x)/100)) { ++t; } if(i*(100+x)/100 + t*(100+x)/100 == s) { ans = max(i*(100+y)/100 + t*(100+y)/100, ans); } } writeln(ans); } }
D
import std.stdio; import std.conv; import std.string; void main() { auto n =(readln.strip.split); while( n[1] != "?" ) { int a = to!int(n[0]),c = to!int(n[2]); switch(n[1]) { case "+": writeln(a+c); break; case "-": writeln(a-c); break; case "*": writeln(a*c); break; case "/": writeln(a/c); break; default: break; } n = (readln.strip.split); } }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; void times(alias pred)(int n) { foreach(i; 0..n) pred(); } auto rep(alias pred, T = typeof(pred()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = pred(); return res; } void main() { string str = readln.chomp.to!string; writeln(str.find('C').find('F').length>0 ? "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 main() { string s; readV(s); int w; readV(w); for (auto i = 0; i < s.length; i += w) write(s[i]); writeln; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "x", long, "y", long, "cost"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long n, m; scan(n, m); long s = aryread().sum(); if(s <= n) writeln(n - s); else writeln(-1); } bool is_exist(string s, char c) { return (s.countUntil(c) >= 0); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; void main(){ int n = readln.chomp.to!int; int[] as = readln.chomp.split.map!(to!int).map!(x => x - 1).array; int count = 0; foreach(i, a; as){ if(i < a && as[a] == i) count ++; } count.writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; uint[10^^5] AS; long[10^^5] BS; void main() { auto nk = readln.split; auto N = nk[0].to!int; auto K = nk[1].to!uint; foreach (i; 0..N) { auto ab = readln.split; AS[i] = ab[0].to!uint; BS[i] = ab[1].to!long; } long r_max; foreach (i; 0..32) { uint m; if (!(K&(1<<i))) { if (i == 31) { m = K; } else { continue; } } else { m = K&(~(1<<i)) | ((1<<i)-1); } long r; foreach (j; 0..N) { if ((AS[j]|m) == m) r += BS[j]; } r_max = max(r_max, r); } writeln(r_max); }
D
// import chie template :) {{{ static if (__VERSION__ < 2090) { 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, std.bitmanip, std.numeric; } else { import std; } // }}} // 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(T...)(ref T args) { foreach (ref arg; args) { arg = next!(typeof(arg)); } } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } // }}} // ModInt {{{ struct ModInt(ulong modulus) { import std.traits : isIntegral, isBoolean; import std.exception : enforce; private { ulong val; } this(const ulong n) { val = n % modulus; } this(const ModInt n) { val = n.value; } @property { ref inout(ulong) value() inout { return val; } } T opCast(T)() { static if (isIntegral!T) { return cast(T)(val); } else if (isBoolean!T) { return val != 0; } else { enforce(false, "cannot cast from " ~ this.stringof ~ " to " ~ T.stringof ~ "."); } } ModInt opAssign(const ulong n) { val = n % modulus; return this; } ModInt opOpAssign(string op)(ModInt rhs) { static if (op == "+") { val += rhs.value; if (val >= modulus) { val -= modulus; } } else if (op == "-") { if (val < rhs.value) { val += modulus; } val -= rhs.value; } else if (op == "*") { val = val * rhs.value % modulus; } else if (op == "/") { this *= rhs.inv; } else if (op == "^^") { ModInt res = 1; ModInt t = this; while (rhs.value > 0) { if (rhs.value % 2 != 0) { res *= t; } t *= t; rhs /= 2; } this = res; } else { enforce(false, op ~ "= is not implemented."); } return this; } ModInt opOpAssign(string op)(ulong rhs) { static if (op == "+") { val += ModInt(rhs).value; if (val >= modulus) { val -= modulus; } } else if (op == "-") { auto r = ModInt(rhs); if (val < r.value) { val += modulus; } val -= r.value; } else if (op == "*") { val = val * ModInt(rhs).value % modulus; } else if (op == "/") { this *= ModInt(rhs).inv; } else if (op == "^^") { ModInt res = 1; ModInt t = this; while (rhs > 0) { if (rhs % 2 != 0) { res *= t; } t *= t; rhs /= 2; } this = res; } else { enforce(false, op ~ "= is not implemented."); } return this; } ModInt opUnary(string op)() { static if (op == "++") { this += 1; } else if (op == "--") { this -= 1; } else { enforce(false, op ~ " is not implemented."); } return this; } ModInt opBinary(string op)(const ulong rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinary(string op)(ref const ModInt rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinary(string op)(const ModInt rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinaryRight(string op)(const ulong lhs) const { mixin("return ModInt(this) " ~ op ~ "= lhs;"); } long opCmp(ref const ModInt rhs) const { return cast(long)value - cast(long)rhs.value; } bool opEquals(const ulong rhs) const { return value == ModInt(rhs).value; } ModInt inv() const { ModInt ret = this; ret ^^= modulus - 2; return ret; } string toString() const { import std.format : format; return format("%s", val); } } // }}} // alias {{{ alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less); alias MinHeap(T) = Heap!(T, "a > b"); // }}} // memo {{{ /* - ある値が見つかるかどうか <https://dlang.org/phobos/std_algorithm_searching.html#canFind> canFind(r, value); -> bool - 条件に一致するやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#filter> // 2で割り切れるやつ filter!"a % 2 == 0"(r); -> Range - 合計 <https://dlang.org/phobos/std_algorithm_iteration.html#sum> sum(r); - 累積和 <https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold> // 今の要素に前の要素を足すタイプの一般的な累積和 // 累積和のrangeが帰ってくる(破壊的変更は行われない) cumulativeFold!"a + b"(r, 0); -> Range - rangeをarrayにしたいとき array(r); -> Array - 各要素に同じ処理をする <https://dlang.org/phobos/std_algorithm_iteration.html#map> // 各要素を2乗 map!"a * a"(r) -> Range - ユニークなやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#uniq> uniq(r) -> Range - 順列を列挙する <https://dlang.org/phobos/std_algorithm_iteration.html#permutations> permutation(r) -> Range - ある値で埋める <https://dlang.org/phobos/std_algorithm_mutation.html#fill> fill(r, val); -> void - バイナリヒープ <https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap> // 昇順にするならこう(デフォは降順) BinaryHeap!(Array!T, "a > b") heap; heap.insert(val); heap.front; heap.removeFront(); - 浮動小数点の少数部の桁数設定 // 12桁 writefln("%.12f", val); - 浮動小数点の誤差を考慮した比較 <https://dlang.org/phobos/std_math.html#.approxEqual> approxEqual(1.0, 1.0099); -> true - 小数点切り上げ <https://dlang.org/phobos/std_math.html#.ceil> ceil(123.4); -> 124 - 小数点切り捨て <https://dlang.org/phobos/std_math.html#.floor> floor(123.4) -> 123 - 小数点四捨五入 <https://dlang.org/phobos/std_math.html#.round> round(4.5) -> 5 round(5.4) -> 5 */ // }}} void main() { auto cin = new Scanner; int n; cin.scan(n); int[string] c; foreach (i; 0 .. n) { c[cin.next!string]++; } int cnt; foreach (e; c.byKey) { cnt++; } writeln(cnt); }
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 lo, int hi) { // abcba = 10001 * a + 1010 * b + 100 * c // 1 <= a <= 9 // 0 <= b, c <= 9 int ans = 0; for (int a = 1; a <= 9; a++) { for (int b = 0; b <= 9; b++) { for (int c = 0; c <= 9; c++) { int x = 10001 * a + 1010 * b + 100 * c; if (lo <= x && x <= hi) { ans++; } } } } return ans; } void main() { auto ab = readints; int a = ab[0], b = ab[1]; writeln(calc(a, b)); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; auto hs = readln.split.to!(int[]); auto bs = new bool[](N); foreach (_; 0..M) { auto ab = readln.split.to!(int[]); auto A = ab[0]-1; auto B = ab[1]-1; if (hs[A] > hs[B]) { bs[B] = true; } else if (hs[A] < hs[B]) { bs[A] = true; } else { bs[A] = true; bs[B] = true; } } int r; foreach (b; bs) if (!b) ++r; writeln(r); }
D
import std.stdio, std.conv, std.algorithm, std.string, std.array, std.numeric; long seq(int a, int m) { int cnt = 1; int x = 1; while(true) { x = (a * x) % m; if (x == 1) break; else cnt++; } return cnt.to!long; } long lcm(long a, long b) { return a * b / gcd(a, b); } long lcms(long[] arr) { long res = arr[0]; for(int i = 1; i < arr.length; i++) { res = lcm(res, arr[i]); } return res; } void main() { while(true) { int[] a = readln.chomp.split(" ").map!(to!int).array; if (a[0] == 0) break; lcms([seq(a[0], a[1]), seq(a[2], a[3]), seq(a[4], a[5])]).writeln; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; int[10^^5*2] BS, WS; void main() { auto N = readln.chomp.to!int; auto S = readln.chomp; foreach (i, c; S) { if (i) { BS[i] = BS[i-1]; WS[i] = WS[i-1]; } if (c == '#') { ++BS[i]; } else { ++WS[i]; } } int r = int.max; foreach (i; 0..N+1) { auto s = WS[N-1]; if (i) { s -= WS[i-1]; s += BS[i-1]; } r = min(r, s); } writeln(r); }
D
import std.stdio; import std.conv; import std.array; import std.string; void main() { int[] a; int n = to!(int)(chomp(readln())); string[] input = split(readln()); a.length = n; for (int i = 0; i < n; i++) { a[i] = to!(int)(input[i]); } for (int i = n - 1; i >= 0; i--) { write(a[i]); if (i != 0) { write(" "); } else { write("\n"); } } }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { long a, b, c, d; scan(a, b, c, d); long lcm = c * d / gcd(c, d); // writeln(lcm); long c_cnt = b / c - (a - 1) / c; long d_cnt = b / d - (a - 1) / d; long lcm_cnt = b / lcm - (a - 1) / lcm; // writeln(c_cnt); // writeln(d_cnt); // writeln(lcm_cnt); writeln((b - a + 1) - (c_cnt + d_cnt - lcm_cnt)); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.functional; bool[8][8] PS; void main() { auto nm = readln.split.to!(int[]); auto n = nm[0]; auto m = nm[1]; foreach (_; 0..m) { auto ab = readln.split.to!(int[]); PS[ab[0]-1][ab[1]-1] = true; PS[ab[1]-1][ab[0]-1] = true; } int search(int pos, int[] hists) { int[] nexts; foreach (i, c; PS[pos]) if (c && hists.find(i).empty) nexts ~= i.to!int; if (nexts.empty) return hists.length == n ? 1 : 0; return reduce!"a + b"(0, nexts.map!(n => search(n, hists ~ n))); } writeln(search(0, [0])); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; auto s=readln.chomp.to!(char[]); bool[char] map; foreach(c; s){ if(c in map){ writeln("no"); return; }else{ map[c]=true; } } 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.string, std.conv, std.algorithm; void main() { int n = readln.chomp.to!int; int[] p = new int[n+1]; foreach(i; 1 .. n+1) { int[] tmp = readln.chomp.split.to!(int[]); p[i-1] = tmp[0], p[i] = tmp[1]; } int[][] m = new int[][](n+1, n+1); foreach (l; 2 .. n+1) { foreach (i; 1 .. n-l+2) { int j = i + l - 1; m[i][j] = 1 << 21; foreach (k; i .. j) { m[i][j] = min(m[i][j], m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j]); } } } m[1][n].writeln; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n, m, t; rd(n, m, t); auto r=readln.split.to!(int[]); foreach(ref e; r) e--; auto d=new long[][](n, n); const long inf=1_000_000_000_000_000_000; foreach(i; 0..n) fill(d[i], inf); foreach(i; 0..n) d[i][i]=0; foreach(_; 0..m){ int a, b; long c; rd(a, b, c); a--; b--; d[a][b]=d[b][a]=c; } foreach(k; 0..n){ foreach(i; 0..n){ foreach(j; 0..n){ d[i][j]=min(d[i][j], d[i][k]+d[k][j]); } } } auto dp=new long[][](t, (1<<t)); foreach(i; 0..t) fill(dp[i], inf); foreach(i; 0..t) dp[i][0]=0; foreach(i; 0..t) dp[i][1<<i]=0; foreach(i; 0..(1<<t)){ foreach(tg; 0..t)if(!(i&(1<<tg))){ foreach(j; 0..t)if(i&(1<<j)){ dp[tg][i|(1<<tg)]=min(dp[tg][i|(1<<tg)], dp[j][i]+d[r[j]][r[tg]]); } } } long mi=inf; foreach(i; 0..t) mi=min(mi, dp[i][(1<<t)-1]); writeln(mi); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.stdio; import std.conv; import std.string; void main() { string[] buf; int a,b; buf = split(readln()); a = to!(int)(buf[0]); b = to!(int)(buf[1]); if(a<b) writeln("a < b"); else if(a>b) writeln("a > b"); else writeln("a == b"); }
D
unittest { assert( [ "B" ].solve == "A" ); assert( [ "a" ].solve == "a" ); } import std.conv; import std.range; import std.stdio; void main() { stdin.byLineCopy.solve.writeln; } auto solve( Range )( Range input ) if( isInputRange!Range && is( ElementType!Range == string ) ) { auto s = input.front.to!( dchar[] ); if( 'A' <= s.front && s.front <= 'Z' ) return "A"; else return "a"; }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std; // dfmt on void main() { long N, M; scan(N, M); auto G = new long[][](N); foreach (_; 0 .. M) { long a, b; scan(a, b); a--, b--; G[a] ~= b; G[b] ~= a; } bool dfs(long v, long d) { if (v == N - 1 && d == 2) return true; if (2 <= d) return false; bool ans; foreach (w; G[v]) ans = ans || memoize!dfs(w, d + 1); return ans; } writeln(dfs(0, 0) ? "POSSIBLE" : "IMPOSSIBLE"); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(int[] xs) { int n = cast(int)xs.length; return max(xs.sum, (xs[0] - 1) + (9 * (n - 1))); } void main() { auto s = readln.chomp; auto xs = s.map!(e => cast(int)(e - '0')).array; auto ans = calc(xs); writeln(ans); }
D
import std.stdio, std.string, std.conv, std.algorithm, std.math; void main() { int num, sum, selsum; while(true) { string line = readln; if(line is null)break; auto f = line.chomp.split(",").map!(to!int); sum += f[0] * f[1]; selsum += f[1]; ++num; } sum.writeln; (cast(int)(cast(real)selsum / num + 0.5)).writeln; }
D
import std.stdio,std.conv,std.string,std.algorithm; void main() { int[5000] dp; while(true) { int n=readln.chomp.to!int; if(!n) break; int ans=int.min; foreach(i;0..n) { int a=readln.chomp.to!int; if(!i) dp[i]=a; else dp[i]=max(dp[i-1]+a,a); ans=max(ans,dp[i]); } ans.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; auto A = N.iota.map!(_ => readln.chomp.to!long).array; if (A[0] != 0) { writeln(-1); return; } if ((N-1).iota.map!(i => A[i+1] - A[i] > 1).any) { writeln(-1); return; } long ans = 0; foreach (i; 1..N) { if (A[i] - A[i-1] == 1) ans += 1; else if (A[i] > 0) ans += A[i]; } ans.writeln; }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int N, M; scan(N, M); auto ba = new int[][](M, 0); foreach (i ; 0 .. M) { ba[i] = readln.split.to!(int[])[1 .. $]; ba[i][] -= 1; } auto p = readln.split.to!(int[]); int ans; foreach (s ; 0 .. 1 << N) { bool ok = true; foreach (i ; 0 .. M) { int tg; foreach (sj ; ba[i]) if (s & (1 << sj)) { tg ^= 1; } if (tg != p[i]) { ok = false; break; } } ans += ok; } 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
/+ dub.sdl: name "F" dependency "dunkelheit" version=">=0.9.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; import core.bitop; immutable int L = 20; int main() { Scanner sc = new Scanner(stdin); int[][] dp = new int[][](L+1, 1<<(L+1)); int[] ans = new int[1<<(L+1)]; int n, k; sc.read(n, k); foreach (l; 0..n+1) { string s; sc.read(s); foreach (i; 0..(1<<l)) { if (s[i] == '1') { dp[0][(1<<l)|i] = 1; } } } foreach (l; 0..L+1) { foreach (a; 0..(1<<l)) { foreach (b; 1..1<<(L+1-l)) { int x = dp[l][(b<<l)|a]; ans[(1<<l)|a]+=x; if (b) { int z = bsr(b); int msk = (1<<z)-1; b ^= (1<<z); if (b) { dp[l+1][b<<(l+1)|(a<<1|1)] += x; } int zm = ~b & msk; if (zm) { int topz = bsr(zm); int nb = b & ((1<<topz)-1); dp[l+1][(nb|(1<<topz))<<(l+1)|(a<<1)] += x; } } } } } int result = 0, rb = -1; foreach (i, d; ans) { if (d < k) continue; int b = bsr(i); if (rb < b) { rb = b; result = i.to!int; } } string s; while (result > 1) { if (result & 1) s ~= "1"; else s ~= "0"; result >>= 1; } writeln(s.retro.array); return 0; } /* IMPORT /home/yosupo/Programs/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(bool enforceEOF = false, T, Args...)(ref T x, auto ref Args args) { import std.exception; enforce(readSingle(x)); static if (args.length == 0) { enforce(enforceEOF == false || !succ()); } else { read!enforceEOF(args); } } void read(bool enforceEOF = false, Args...)(auto ref Args args) { import std.exception; static if (args.length == 0) { enforce(enforceEOF == false || !succ()); } else { enforce(readSingle(args[0])); read!enforceEOF(args); } } } /* IMPORT /home/yosupo/Programs/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/Programs/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); } } } } /* 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.conv, std.string, std.range, std.algorithm, std.array, std.functional, std.container, std.typecons; void main() { int N = readln.split[0].to!int; int[] A = new int[N]; foreach(ref a; A) { a = readln.split[0].to!int; } int top = A.reduce!max; int second = 0; int maxcnt = 0; foreach(a; A) { if(a == top) { maxcnt++; continue; } second = max(second, a); } foreach(a; A) { writeln(a == top && maxcnt == 1 ? second : top); } }
D
void main(){ int[] val = _scanln(); foreach(elm; val){ if( count(val, elm)==2 ){ writeln("Yes"); return; } } writeln("No"); } 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.stdio, std.conv, std.string, std.algorithm; void main() { auto ip = readln.split.to!(int[]); auto x = 0; if(ip[0] <= ip[1]){ x += ip[0]; } else { x += ip[0] - 1; } writeln(x); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.bigint; void main() { auto N = readln.chomp.to!int; writeln(N/2 - (N%2 == 0 ? 1 : 0)); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = readln.chomp.to!size_t; auto fi = new int[](45); fi[0] = fi[1] = 1; foreach (i; 2..n+1) fi[i] = fi[i-1] + fi[i-2]; writeln(fi[n]); }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; import std.datetime; import std.numeric; import std.math; import std.string; string my_readln() { return chomp(readln()); } void main() { //auto tokens = split(my_readln()); string S = my_readln(); string S2 = "keyence"; bool ans = false; ulong begin; ulong si; foreach (i, c; S) { if (c == S2[si]) { ++si; if (si == S2.length) { begin = i; break; } } else { begin = i - 1; break; } } ulong remain1 = S2.length - si; ulong remain2 = S.length - begin - remain1; if (S[0..begin+1] ~ S[begin+remain2..$] == S2) ans = true; stderr.writeln(S[0..begin+1], " ", S[begin+remain2..$]); writeln(ans ? "YES" : "NO"); stdout.flush(); }
D
void main() { string s = readln.chomp; string t; foreach (x; s) { if (x == 'B') { if (!t.empty) t.popBack; } else { t ~= x; } } t.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.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional, std.traits; import std.algorithm, std.container; import core.stdc.stdlib; void main() { auto N = scanElem; auto K = scanElem; writeln(N-K+1); } long gcd(long a, long b) { if(b == 0) return a; return gcd(b, a % b); } class UnionFind{ UnionFind parent = null; void merge(UnionFind a) { if(same(a)) return; a.root.parent = this.root; } UnionFind root() { if(parent is null)return this; return parent = parent.root; } bool same(UnionFind a) { return this.root == a.root; } } void scanValues(TList...)(ref TList list) { auto lit = readln.splitter; foreach (ref e; list) { e = lit.fornt.to!(typeof(e)); lit.popFront; } } T[] scanArray(T = long)() { return readln.split.to!(long[]); } void scanStructs(T)(ref T[] t, size_t n) { t.length = n; foreach (ref e; t) { auto line = readln.split; foreach (i, ref v; e.tupleof) { v = line[i].to!(typeof(v)); } } } long scanULong(){ long x; while(true){ const c = getchar; if(c<'0'||c>'9'){ break; } x = x*10+c-'0'; } return x; } T scanElem(T = long)() { char[] res; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { res ~= cast(char) c; c = getchar; } return res.strip.to!T; } template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } struct Factor { long n; long c; } Factor[] factors(long n) { Factor[] res; for (long i = 2; i ^^ 2 <= n; i++) { if (n % i != 0) continue; int c; while (n % i == 0) { n = n / i; c++; } res ~= Factor(i, c); } if (n != 1) res ~= Factor(n, 1); return res; } long[] primes(long n) { if(n<2)return []; auto table = new long[n+1]; long[] res; for(int i = 2;i<=n;i++) { if(table[i]==-1) continue; for(int a = i;a<table.length;a+=i) { table[a] = -1; } res ~= i; } return res; } bool isPrime(long n) { if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (long i = 3; i ^^ 2 <= n; i += 2) if (n % i == 0) return false; return true; }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; import std.ascii; void main() { int n; scan(n); foreach (i ; 0 .. n) { string s1,s2; scan(s1, s2); writeln(i2m(m2i(s1) + m2i(s2))); } } int m2i(string s) { int res; int[char] t = ['m':1000, 'c':100, 'x':10, 'i':1]; while (!s.empty) { if (s[0].isDigit) { res += (s[0] - '0') * t[s[1]]; s.popFrontN(2); } else { res += t[s[0]]; s.popFront; } } return res; } string i2m(int x) { char[] res; char[int] t = [3:'m', 2:'c', 1:'x', 0:'i']; int d; while (x > 0) { int r = x % 10; if (r > 0) { res ~= t[d]; } if (r > 1) { res ~= r + '0'; } x /= 10; d++; } res.reverse(); return res.to!string; } 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.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.to!(int[]); } long lowest(long p, long low) { return ((low + p - 1) / p) * p; } long calcMin(int[] xs) { long n = xs[$ - 1]; for (int i = cast(int)xs.length - 1; i >= 0; i--) { n = lowest(xs[i], n); } long m = n; for (int i = 0; i < xs.length; i++) { m -= m % xs[i]; } return m == 2 ? n : -1; } long calcMax(int[] xs, long min) { long lo = min; long hi = long.max / 2 - 1; long ans = min; while (lo <= hi) { long m = (lo + hi) / 2; bool ok = true; long x = m; for (int i = 0; i < xs.length; i++) { x -= x % xs[i]; } if (x != 2) ok = false; if (ok) { ans = max(ans, m); lo = m + 1; } else { hi = m - 1; } } return hi; } void main() { readint; auto xs = readints(); auto min = calcMin(xs); if (min == -1) { writeln(-1); } else { auto max = calcMax(xs, min); writeln(min, " ", max); } }
D
import std.algorithm, std.array, std.conv, std.stdio, std.string; void main() { readln; auto p = readln.chomp.split.to!(int[]); auto q = readln.chomp.split.to!(int[]); if (p > q) { auto r = p; p = q; q = r; } int ans; while (p != q) { p.nextPermutation; ans += 1; } ans.writeln; }
D
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } void main() { auto X = readln.chomp.to!int; bool isPrime(int n) { if (n == 2) { return true; } else if (n % 2 == 0 || n < 2) { return false; } else { for (int i = 3; i <= sqrt(float(n)); i += 2) { if (n % i == 0) { return false; } } } return true; } for(int i = X; true; i++) { if (i > 2 && i % 2 == 0) continue; if (isPrime(i)) { writeln(i); break; } } }
D
import std.stdio; import core.stdc.stdio; import std.algorithm; struct cube{ int x1; int x2; int y1; int y2; int z1; int z2; } int find(int[] d,int t){ for(int i=0;i<d.length;i++) if(d[i] == t) return i; return -1; } void main(){ int n,k; int[] xs,ys,zs; cube[] cs; scanf("%d%d",&n,&k); xs = new int[n*2]; ys = new int[n*2]; zs = new int[n*2]; cs = new cube[n]; for(int i=0;i<n;i++){ scanf("%d%d%d%d%d%d",&cs[i].x1,&cs[i].y1,&cs[i].z1,&cs[i].x2,&cs[i].y2,&cs[i].z2); xs[i*2] = cs[i].x1; xs[i*2+1] = cs[i].x2; ys[i*2] = cs[i].y1; ys[i*2+1] = cs[i].y2; zs[i*2] = cs[i].z1; zs[i*2+1] = cs[i].z2; } sort(xs); sort(ys); sort(zs); //xs.writeln; //ys.writeln; //zs.writeln; int[] count = new int[n*n*n*8]; for(int i=0;i<n;i++){ int xl = xs.find(cs[i].x1); int xr = xs.find(cs[i].x2); int yl = ys.find(cs[i].y1); int yr = ys.find(cs[i].y2); int zl = zs.find(cs[i].z1); int zr = zs.find(cs[i].z2); for(int x=xl;x<xr;x++){ for(int y=yl;y<yr;y++){ for(int z=zl;z<zr;z++){ count[(x*n*2+y)*n*2+z]++; } } } } long ans=0; for(int x=0;x<n*2-1;x++){ long xd = xs[x+1]-xs[x]; for(int y=0;y<n*2-1;y++){ long yd = ys[y+1]-ys[y]; for(int z=0;z<n*2-1;z++){ long zd = zs[z+1]-zs[z]; if(count[(x*n*2+y)*n*2+z]>=k){ // writeln(x," ",y," ",z," ",xd," ",yd," ",zd); ans += xd*yd*zd; } } } } printf("%lld\n",ans); }
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 t = read.to!int; foreach(_; 0 .. t){ long n = read.to!long; long k = read.to!long; long ans; while(n > 0){ if(n % k == 0) ans += 1, n /= k; else ans += n % k, n -= (n % k); } ans.writeln; } }
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 S = readln.chomp; auto N = S.length; int k = int.max; char last; foreach (i, c; S) { if (c != last) { if (i) { k = min(k, max(i, N-i).to!int); } last = c; } } writeln(k == int.max ? N : k); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto a = RDA!int; auto dp = new int[][](n+1, 2); foreach (i; 0..dp.length) { dp[i][] = n; } dp[0][0] = 0; foreach (i; 0..n) { dp[i+1][1].chmin(dp[i][0]+a[i]); if (i < n-1) dp[i+2][1].chmin(dp[i][0]+a[i]+a[i+1]); dp[i+1][0].chmin(dp[i][1]); if (i < n-1) dp[i+2][0].chmin(dp[i][1]); } ans[ti] = min(dp[n][0], dp[n][1]); } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int h, w; char s, t; rd(h, w, s, t); auto c=new char[][](h, w); foreach(i; 0..h) c[i]=readln.chomp.to!(char[]); const int n=26; auto d=new int[][](n, n); foreach(i; 0..n) fill(d[i], 1_000_000); auto dir=[[1, 0], [-1, 0], [0, 1], [0, -1]]; bool A(char ch){return ('A'<=ch && ch<='Z');} foreach(int i; 0..h)foreach(int j; 0..w){ if(A(c[i][j])==false) continue; d[c[i][j]-'A'][c[i][j]-'A']=0; foreach(dd; dir){ int ni=i+dd[0]*2, nj=j+dd[1]*2; if(ni<0 || ni>=h || nj<0 || nj>=w) continue; if(((dd[0]!=0 && c[ni][nj]=='|')||(dd[1]!=0 && c[ni][nj]=='-'))==false) continue; while((ni>=0 && ni<h && nj>=0 && nj<w) && A(c[ni][nj])==false){ ni+=dd[0]; nj+=dd[1]; } if((ni>=0 && ni<h && nj>=0 && nj<w)){ int x=c[i][j]-'A', y=c[ni][nj]-'A'; // writeln(c[i][j], " ", c[ni][nj]); d[x][y]=d[y][x]=1; } } } foreach(_; 0..n)foreach(i; 0..n)foreach(j; 0..n){ d[i][j]=min(d[i][j], d[i][_]+d[_][j]); } writeln(d[s-'A'][t-'A']); } void rd(Type...)(ref Type x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import core.bitop; 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); writeln((A + B) % 24); }
D
import std.stdio, std.string, std.conv, std.algorithm; void main() { long [] a = readln.chomp.split.to!(long[]); long N = a[0], M = a[1]; long ans; if (N == 1 && M == 1) { ans = 1; } else if (N == 1 || M == 1) { ans = max(N, M) - 2; } else if (N == 2 || M == 2) { ans = 0; } else { ans = (N - 2) * (M - 2); } writeln(ans); }
D
import std.stdio, std.string, std.conv, std.algorithm, std.array; void main(){ auto a = readln.split.map!(to!int); auto w = a[0]; auto h = a[1]; auto x = a[2]; auto y = a[3]; auto r = a[4]; if(w>=x+r && 0<=x-r && h>=y+r && 0<=y-r) writeln("Yes"); else writeln("No"); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!T;r.popFront;}} void main() { int n, m, x; readV(n, m, x); int[] a; readA(m, a); auto as = a.assumeSorted; writeln(min(as.lowerBound(x).length, as.upperBound(x).length)); }
D
import std.stdio; import std.conv; import std.array; void main(){ string[] input = readln.split; int N = input[0].to!int; int M = input[1].to!int; if(N == 1){ write(readln.split[0]); } else{ int[] A = new int[M]; int K; int ij; for(int i = 0;i < N;i++){ input = readln.split; K = input[0].to!int; for(int j = 0;j < K;j++){ ij = input[j+1].to!int; A[ij - 1]++; } } int cnt; for(int i = 0;i < M;i++){ if(A[i] == N) cnt++; } write(cnt); } }
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 S = scanString; dchar c; long i; foreach(_;S){ if(c==_){ i++; if(i==3)end("Yes"); }else{ i=1; } c=_; } end("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 main() { int n, x; readV(n, x); int[] a; readA(n, a); auto ans = 0L; foreach (i; 1..n) { if (a[i-1]+a[i] > x) { auto r = min(a[i], a[i-1]+a[i]-x); a[i] -= r; ans += r; if (a[i] == 0) { r = a[i-1]-x; ans += r; } } } 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.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 H = RD; auto W = RD; auto K = RD; auto dp = new long[][](H+1, W); dp[0][0] = 1; foreach (y; 0..H) { foreach (i; 0..2^^(W-1)) { if (W == 1) { dp[y+1][0].moda(dp[y][0]); continue; } auto bit = new bool[](W-1); foreach (j; 0..bit.length) { if ((1L << j) & i) bit[j] = true; } bool ok = true; foreach (j; 1..bit.length) { if (bit[j-1] && bit[j]) { ok = false; break; } } if (!ok) continue; foreach (j; 0..bit.length) { if (bit[j]) { dp[y+1][j+1].moda(dp[y][j]); dp[y+1][j].moda(dp[y][j+1]); } else { if (j == 0) dp[y+1][j].moda(dp[y][j]); else if (!bit[j-1]) dp[y+1][j].moda(dp[y][j]); if (j == bit.length-1) dp[y+1][W-1].moda(dp[y][W-1]); } } } } debug writeln(dp); writeln(dp[H][K-1]); stdout.flush; debug readln; }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; int main() { auto sc = new Scanner(stdin); int k; sc.read(k); k *= 2; if (k == 0) { writeln("miku"); return 0; } string s; char nw = 'a'; while (k) { foreach (i; 1..1000) { if (k < (i+1)*(i)) { k -= i*(i-1); s ~= nw.repeat.take(i).array; nw++; break; } } } writeln(s); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } version (X86) static if (__VERSION__ < 2071) { import core.bitop : bsf, bsr, popcnt; int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; 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 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; if (f.eof) return false; line = lineBuf[]; f.readln(line); } 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 { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { assert(buf.length == T.length); } x = buf; line.length = 0; } } 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); } } }
D
void main() { dchar[] s = readln.chomp.to!(dchar[]); long a = s.countUntil('A'); reverse(s); long z = s.countUntil('Z'); writeln(s.length - z - a); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; void main() { auto s = readln.split.map!(to!int); auto H = s[0]; auto W = s[1]; auto A = iota(H).map!(_ => readln.chomp).array; Tuple!(int, int)[] X; foreach (i; 0..H) foreach (j; 0..W) if (A[i][j] == 'B') { X ~= tuple(i, j); } int ans = 0; foreach (i; 0..X.length.to!int) foreach (j; i+1..X.length.to!int) { ans = max(ans, abs(X[i][0] - X[j][0]) + abs(X[i][1] - X[j][1])); } ans.writeln; }
D
void main() { long a; string b; rdVals(a, b); long[] list; foreach (x; b) { if (x.isNumber) { list ~= x - '0'; } } long[] num = new long[list.length]; foreach (i, x; list) { num[i] = a * x; } long result; foreach (i, x; num) { result += x * 10L^^(2-i); } result /= 100; result.writeln; } enum long mod = 10L^^9 + 7; enum long inf = 1L << 60; enum double eps = 1.0e-9; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; int n; void main() { while (true) { scan(n); if (n == 0) break; writeln(solve(n)); } } int solve(int n) { int res; foreach (r ; 1 .. n) { int x = r * (r + 1) / 2; int y = x - n; if (y < 0) continue; int btm = -1, top = r, mid; while (top - btm > 1) { mid = btm + (top - btm) / 2; if (mid * (mid + 1) / 2 >= y) { top = mid; } else { btm = mid; } } if (top * (top + 1) / 2 == y) { debug { writeln(top, " ", r); } res++; } } return res; } 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() { (x => (x / 11)*2 + ceil(x%11 / 6.).to!ulong)(readAs!long).writeln; } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.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; } int binarySearch(alias pred, T)(in T[] arr, bool isLower = true) { int ok, ng; if (isLower) { ok = cast(int)arr.length; ng = -1; } else { ok = -1; ng = cast(int)arr.length; } while (abs(ok-ng) > 1) { auto mid = (ok+ng) / 2; if (unaryFun!pred(arr[mid])) ok = mid; else ng = mid; } return ok; } void main() { auto N = RD; auto S = RD!string; auto pos = new long[][](26); foreach (i; 0..N) { auto num = S[i] - 'a'; pos[num] ~= i; } long ans; foreach (i; 0..N-1) { auto num = S[i] - 'a'; auto pi = pos[num].binarySearch!((long a) => a > i)(); foreach (j; pos[num][pi..$]) { auto len1 = j - i; auto len2 = N - j; auto len = min(len1, len2); if (len <= ans) continue; bool ok = true; foreach (k; 0..ans) { if (S[i+k] != S[j+k]) { ok = false; break; } } if (!ok) continue; foreach (k; ans..len) { if (S[i+k] != S[j+k]) break; ans = max(ans, k+1); } } } writeln(ans); stdout.flush(); debug readln(); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; auto s=readln.chomp.to!(char[]); auto t=readln.chomp.to!(char[]); if(s.length<t.length){writeln("UNRESTORABLE"); return;} auto mn=new char[](s.length); foreach(ref e; mn) e='z'; bool none=true; for(auto i=0; i<=s.length-t.length; i++){ bool ok=true; for(auto j=0; j<t.length; j++){ ok&=(s[i+j]==t[j] || s[i+j]=='?'); } if(ok){ none=false; auto u=new char[](s.length); foreach(k; 0..i) u[k]=(s[k]=='?' ? 'a' : s[k]); foreach(k; 0..(t.length)) u[i+k]=t[k]; foreach(k; (i+t.length)..(s.length)) u[k]=(s[k]=='?' ? 'a' : s[k]); if(u<mn) mn=u; } } if(none){ writeln("UNRESTORABLE"); }else{ writeln(mn); } } 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() { auto R = ri; auto G = ri; writeln((G - R)*2 + R); } // =================================== import std.stdio; import std.string; import std.functional; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
module app; import std.stdio; import std.string; void main() { auto abc = readln().strip(); if (abc == "5 5 7" || abc == "5 7 5" || abc == "7 5 5") write("YES"); else write("NO"); }
D
import std.stdio, std.string; void main() { string a = readln.chomp; if (a[0..4] == "YAKI") { writeln("Yes"); } else { writeln("No"); } }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; import std.datetime; import std.numeric; import std.math; import std.string; string my_readln() { return chomp(readln()); } void main() { auto tokens = my_readln().split(); auto s = tokens[0].to!uint; auto used = new uint[](1000000); used[s] = 1; uint last = s, ans; foreach (i; 1..1000000) { if (last % 2 == 0) { last /= 2; } else { last = last * 3 + 1; } if (used[last] == 1) { ans = i + 1; break; } used[last] = 1; } writeln(ans); stdout.flush(); }
D
import std.stdio,std.conv,std.string,std.algorithm,std.array; void main(){ auto sx=readln().split(); auto sa=readln().split(); auto sb=readln().split(); int x,a,b; x=to!int(sx[0]); a=to!int(sa[0]); b=to!int(sb[0]); writeln((x-a)%b); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.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 K = RD; auto X = RD; auto Y = RD; writeln(min(N, K) * X + max(N - K, 0) * Y); stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array, std.functional, std.container, std.typecons; void main() { auto input = readln.split.to!(int[]); int A = input[0], B = input[1]; writeln(max(A-B-B, 0)); }
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 A = RD!long; auto B = RD!long; auto C = RD!long; auto D = RD!long; auto E = RD!long; auto F = RD!long; double bestP = 0.0; long bestQ, bestSQ; foreach (ai; 0..31) { auto aq = A * ai; foreach (bi; 0..31) { if (ai + bi == 0) continue; auto bq = B * bi; auto wq = aq + bq; auto remain = F - wq * 100; if (remain < 0) continue; auto limit = min(remain, wq * E); long sq; foreach_reverse (i; 0..limit+1) { bool isDone = false; long s = i; while (s >= 0) { if (s % C == 0 || s % D == 0) { sq = i; isDone = true; break; } s -= C; } if (isDone) break; } //auto sq1 = ((limit / C) * C) + ((limit % C) / D) * D; //auto sq2 = ((limit / D) * D) + ((limit % D) / C) * C; //auto sq = max(sq1, sq2); auto q = wq * 100 + sq; auto p = sq * 100.0 / q; if (p >= bestP) { bestP = p; bestQ = q; bestSQ = sq; stderr.writeln(remain, " ", wq * E, " ", limit); } } } writeln(bestQ, " ", bestSQ); stdout.flush(); }
D
import std.stdio; import std.string; import std.math; import std.conv; import std.algorithm; import std.bigint; void main(){ while(true){ string input = readln(); if(stdin.eof()) break; auto s = to!real(chomp(input)); for(int i=1;;i++){ if(s*s < 19.6 * (5*i-5)) { writeln(i); break; } } } }
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 ans1 = 0L, s1 = 0L; foreach (i; 0..n) { s1 += a[i]; if (i%2 == 0) { if (s1 <= 0) { ans1 += -s1 + 1; s1 = 1; } } else { if (s1 >= 0) { ans1 += s1 + 1; s1 = -1; } } } auto ans2 = 0L, s2 = 0L; foreach (i; 0..n) { s2 += a[i]; if (i%2 == 1) { if (s2 <= 0) { ans2 += -s2 + 1; s2 = 1; } } else { if (s2 >= 0) { ans2 += s2 + 1; s2 = -1; } } } writeln(min(ans1, ans2)); }
D
import std.stdio; import std.conv; import std.algorithm; import std.string; void main(){ while(1){ auto a=map!(to!int)(readln.chomp.split); int m=a[0],f=a[1],r=a[2]; if(m<0&&f<0&&r<0)break; if(m<0||f<0)writeln("F"); else if(80<=m+f)writeln("A"); else if(65<=m+f)writeln("B"); else if(50<=m+f)writeln("C"); else if(30<=m+f)writeln(((r>=50)?"C":"D")); else writeln("F"); } }
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 n = RD!int; int l = 1, r = n; writeln("? ", l, " ", r); stdout.flush; auto m = RD!int; while (r - l > 1) { auto m2 = (l+r)/2; if (m <= m2) { auto ll = min(m, l); auto rr = m2; writeln("? ", ll, " ", rr); stdout.flush; auto a = RD!int; if (a == m) r = m2; else l = m2; } else { auto ll = m2; auto rr = max(m, r); writeln("? ", ll, " ", rr); stdout.flush; auto a = RD!int; if (a == m) l = m2; else r = m2; } } writeln("? ", l, " ", r); stdout.flush; m = RD!int; if (m == l) writeln("! ", r); else writeln("! ", l); stdout.flush; debug readln; }
D
import std.stdio; import std.conv; import std.algorithm; import std.range; import std.string; import std.math; void main() { int taro, hanako; readln; foreach (string line; stdin.lines) { auto words = line.chomp.split; if (words[0] > words[1]) { taro += 3; } else if (words[0] < words[1]) { hanako += 3; } else { taro++; hanako++; } } writeln(taro, " ", hanako); }
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 A = RD; auto B = RD; long[40] f(long x) { long[40] r; foreach (i; 0..40) { auto unit = pow(2LU, i); auto y = x / unit / 2 * unit; if ((x / unit) % 2 == 1) y += x % unit; r[i] = y; } return r; } auto cnt_a = f(A); //stderr.writeln(cnt_a); auto cnt_b = f(B+1); //stderr.writeln(cnt_b); long ans; foreach (i; 0..40) { auto cnt = cnt_b[i] - cnt_a[i]; if (cnt % 2 == 1) ans |= 1LU << i; } writeln(ans); stdout.flush(); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; // dfmt on void main() { long a, b; scan(a, b); if (a + b == 15) { writeln("+"); } else if (a * b == 15) { writeln("*"); } else { writeln("x"); } }
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; int[] di = [1,0,-1,0,1,1,-1,-1], dj = [0,1,0,-1,1,-1,1,-1]; int ans; void main() { while (1) { int n; scan(n); if (!n) return; auto ans = cntp(n); writeln(ans); } } int cntp(int n) { int cnt; foreach (a ; n + 1 .. 2*n + 1) { cnt += isPrime(a); } return cnt; } bool isPrime(int a) { for (int p = 2; p*p <= a; p++) { if (a % p == 0) { return false; } } return true; } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.string, std.array, std.conv, std.algorithm.iteration, std.functional; void main() { auto _x = readln; auto s = readln.chomp; int x, max; foreach (c; s) { switch (c) { case 'I': if (++x > max) max = x; break; case 'D': --x; break; default: } } writeln(max); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); void main() { long n = lread(); // writeln(n); auto l = new long[](100); l[0] = 2; l[1] = 1; // writeln(l); foreach (i; 2 .. n + 1) { l[i] = l[i - 1] + l[i - 2]; } writeln(l[n]); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
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 n = RD!int; auto m = RD!int; auto c = new string[](n); foreach (i; 0..n) c[i] = RD!string; auto cnt = new long[][](n, m); foreach_reverse (i; 0..n) { foreach (j; 0..m) { if (c[i][j] != '*') continue; cnt[i][j] = 1; if (i == n-1 || j == 0 || j == m-1) continue; long x = long.max; foreach (k; j-1..j+2) { x.chmin(cnt[i+1][k]); } cnt[i][j] += x; } } foreach (i; 0..n) { foreach (j; 0..m) { ans[ti] += cnt[i][j]; } } } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
import std.stdio; import std.range; import std.array; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.container; import std.typecons; import std.random; import std.csv; import std.regex; import std.math; import core.time; import std.ascii; import std.digest.sha; import std.outbuffer; void main() { int n = readln.chomp.to!int; string[] ss = readln.chomp.split; bool[bool[char]] roots; foreach (s; ss) { bool[char] root; foreach (c; s) { root[c] = true; } roots[root] = true; } roots.length.writeln; } void tie(R, Args...)(R arr, ref Args args) if (isRandomAccessRange!R || isArray!R) in { assert (arr.length == args.length); } body { foreach (i, ref v; args) { alias T = typeof(v); v = arr[i].to!T; } } void verbose(Args...)(in ref Args args) { stderr.write("["); foreach (i, ref v; args) { if (i) stderr.write(", "); stderr.write(v); } stderr.writeln("]"); }
D