code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; const MOD = 10^^9 + 7; int calc(string s) { auto dp = new int[][](s.length + 1, 13); // dp[i][j] // i : i 桁目までみた // j : 13 で割った余 dp[0][0] = 1; for (int i = 0; i < s.length; i++) { // i 桁目 for (int j = 0; j < 13; j++) { // 余り foreach (k; s[i] == '?' ? iota(0, 10) : iota(s[i]-'0', s[i]-'0'+1)) { dp[i + 1][(10 * j + k) % 13] += dp[i][j]; dp[i + 1][(10 * j + k) % 13] %= MOD; } } } return dp[s.length][5]; } void main() { string s = read!string; writeln(calc(s)); }
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 main(){ auto n = readln.chomp.to!int; auto a = readln.chomp.to!int; ((n % 500) <= a ? "Yes" : "No").writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.bigint; void main() { auto abc = readln.split.to!(int[]).uniq.array; writeln(abc.length == 1 ? "Yes" : "No"); }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { auto s = new bool[](10^^6 + 1); s[] = 1; s[0] = s[1] = 0; for (int p = 2; p*p <= 10^^6; p++) { if (s[p]) { for (int q = p*p; q <= 10^^6; q += p) { s[q] = 0; } } } while (1) { int a, d, n; scan(a, d, n); if (!a) return; int m; while (m < n) { if (s[a]) m++; a += d; } a-=d; writeln(a); } } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
void main(){ auto S = readln().chomp(); auto T = readln().chomp(); if ( S[] == T[0..$-1] ){ writeln("Yes"); } else { writeln("No"); } } import std; string readStr(){ return readln().chomp(); } T[] readLine( T = long )(){ return readln().split().to!(T[])(); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; alias SugarWater = Tuple!(long, "swater", long, "sugar"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } auto charcount(string s) { char current = s[0]; auto ret = new long[](1); foreach (e; s) { if (e != current) { ret ~= [0]; current = e; } ret[$ - 1]++; } return ret; } void main() { auto s = sread(); auto t = sread(); char[char] trans; foreach (i; iota(s.length)) { if (s[i] in trans) { if (trans[s[i]] != t[i]) { writeln("No"); return; } } else { trans[s[i]] = t[i]; } } auto check = new long[](26); foreach (e; trans) check[e - 'a']++; foreach (e; check) { if (e > 1) { writeln("No"); return; } } writeln("Yes"); }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; void times(alias fun)(int n) { foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } void main() { string str = readln.chomp; str.length.iota.retro.filter!"a%2==0".find!( i => str[0..i/2]==str[i/2..i] ).front.writeln; } // ---------------------------------------------- // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { auto minElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto minimum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b < minimum) { element = a; minimum = b; } } return element; } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto maximum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b > maximum) { element = a; maximum = b; } } return element; } }
D
import std.stdio, std.conv, std.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; } void main() { auto S = RD!string; string ans; if (S[0] == 'S') ans = "Cloudy"; else if (S[0] == 'C') ans = "Rainy"; else ans = "Sunny"; writeln(ans); stdout.flush(); debug readln(); }
D
import std; void main() { int n; scan(n); int m = n % 10; if (m == 3) writeln("bon"); else if (m == 0 || m == 1 || m == 6 || m == 8) writeln("pon"); else writeln("hon"); } void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T=string)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readints = reads!int;
D
void main() { long k = rdElem; string s = rdStr; if (s.length <= k) s.writeln; else writeln(s[0..k], "..."); } 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
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() { long x, y; readV(x, y); writeln((x-y).abs <= 1 ? "Brown" : "Alice"); }
D
unittest { assert( [ "575" ].parse.expand.solve == "Yes" ); assert( [ "123" ].parse.expand.solve == "No" ); assert( [ "812" ].parse.expand.solve == "No" ); } import std.conv; import std.range; import std.stdio; import std.string; import std.typecons; void main() { stdin.byLineCopy.parse.expand.solve.writeln; } auto parse( Range )( Range input ) if( isInputRange!Range && is( ElementType!Range == string ) ) { auto n = input.front.strip; return tuple( n ); } auto solve( string n ) { return ( n[ 0 ] == n[ 2 ] ) ? "Yes" : "No"; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } 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 P = RD; ans[ti] = [(P-1)/2, P-1]; } foreach (e; ans) writeln(e[0], " ", e[1]); stdout.flush; debug readln; }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { string s; readV(s); string t; readV(t); auto sf = s.map!(si => si == 'A' ? 1 : 2).array; auto tf = t.map!(ti => ti == 'A' ? 1 : 2).array; auto sc = sf.cumulativeSum; auto tc = tf.cumulativeSum; int q; readV(q); foreach (_; 0..q) { int a, b, c, d; readV(a, b, c, d); --a; --c; writeln(sc[a..b]%3 == tc[c..d]%3 ? "YES" : "NO"); } } class CumulativeSum(T) { size_t n; T[] s; this(T[] a) { n = a.length; s = new T[](n+1); s[0] = T(0); foreach (i; 0..n) s[i+1] = s[i] + a[i]; } T opSlice(size_t l, size_t r) { return s[r]-s[l]; } size_t opDollar() { return n; } } auto cumulativeSum(T)(T[] a) { return new CumulativeSum!T(a); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; void main() { char[] input = readln().dup; input[5] = ' '; input[13] = ' '; input.writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto H = readln.chomp.to!int; auto W = readln.chomp.to!int; writeln((N - W + 1) * (N - H + 1)); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { auto xs = readints; int a = xs[0], b = xs[1], c = xs[2], d = xs[3]; if (a + b > c + d) writeln("Left"); else if (a + b == c + d) writeln("Balanced"); else writeln("Right"); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}} void main() { int a, b; readV(a, b); writeln(a*b%2 == 0 ? "Even" : "Odd"); }
D
void main() { auto O = rs, E = rs; foreach(i; 0..O.length) { O[i].write; if(!(i > E.length-1)) E[i].write; } writeln; } // =================================== import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto lr = readln.split.to!(long[]); auto L = lr[0]; auto R = lr[1]+1; long x = long.max; foreach (l; L..min(L+2020, R)) { foreach (r; l+1..min(L+2020,R)) { x = min(x, (l*r)%2019L); } } writeln(x); }
D
void main() { int[] tmp = readln.split.to!(int[]); int n = tmp[0], q = tmp[1]; string s = readln.chomp; int[] dp = new int[n]; foreach (i; 1 .. n) { int cnt; if (s[i-1] == 'A' && s[i] == 'C') { ++cnt; } dp[i] += dp[i-1] + cnt; } foreach (i; 0 .. q) { tmp = readln.split.to!(int[]); int l = tmp[0] - 1, r = tmp[1] - 1; writeln(dp[r] - dp[l]); } } 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; 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 n, m, x; scan(n, m, x); auto 参考書の値段とアルゴリズム = new long[][](n); // writeln(tmp); foreach (i; 0 .. n) { 参考書の値段とアルゴリズム[i] = aryread(); } // writeln(tmp); long func(long[] 買う買わない) { long 合計金額; auto 理解度 = new long[](m); if (買う買わない.length == n) { foreach (i; 0 .. (買う買わない.length)) { if (買う買わない[i] == 1) { 合計金額 += 参考書の値段とアルゴリズム[i][0]; foreach (アルゴリズム番号; 1 .. m + 1) { 理解度[アルゴリズム番号 - 1] += 参考書の値段とアルゴリズム[i][アルゴリズム番号]; } } } foreach (i; 0 .. m) { if (理解度[i] >= x) { continue; } else { return -1; } } return 合計金額; } else { // func(a~0); // func(a~1); long func_0 = func(買う買わない ~ 0); long func_1 = func(買う買わない ~ 1); if (func_0 != -1 && func_1 != -1) { return min(func_0, func_1); } else if (func_0 == -1 && func_1 != -1) { return func_1; } else if (func_1 == -1 && func_0 != -1) { return func_0; } else { return -1; } } } func([]).writeln(); // func([1,1,0]) // func([1,1,1]) // long func(long[] a); // func([]); // func([1]); // func([0]); // func([1,0]); // func([1,1]); // xxx func(xxx) = min(func(1xx), func(0xx)) // 1xx // 11x // 111 // 110 // 10x // 101 // 100 // writeln(func(tmp, x)); } 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.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; auto A = RDA; long ans = 1000; foreach (i; 0..N-1) { if (A[i] < A[i+1]) { auto cnt = ans / A[i]; ans = ans % A[i]; ans += A[i+1] * cnt; } } writeln(ans); stdout.flush; debug readln; }
D
/* imports all std modules {{{*/ import std.algorithm, std.array, std.ascii, std.base64, std.bigint, std.bitmanip, std.compiler, std.complex, std.concurrency, std.container, std.conv, std.csv, std.datetime, std.demangle, std.encoding, std.exception, std.file, std.format, std.functional, std.getopt, std.json, std.math, std.mathspecial, std.meta, std.mmfile, std.net.curl, std.net.isemail, std.numeric, std.parallelism, std.path, std.process, std.random, std.range, std.regex, std.signals, std.socket, std.stdint, std.stdio, std.string, std.system, std.traits, std.typecons, std.uni, std.uri, std.utf, std.uuid, std.variant, std.zip, std.zlib; /*}}}*/ /+---test 10 ZABCDBABCQ ---+/ /+---test 33 ABCCABCBABCCABACBCBBABCBCBCBCABCB ---+/ void main(string[] args) { const N = readln.chomp.to!long; const S = readln.chomp; long ans; long offset; while (true) { const x = S[offset..$].indexOf("ABC"); if (x == -1) break; offset += x + 1; ++ans; } ans.writeln; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { int k; while ((k = readint) != 0) { auto xs = readints; writeln(xs.sum / (k - 1)); } }
D
import std.stdio, std.conv, std.array, std.string; import std.algorithm; import std.container; import std.range; import core.stdc.stdlib; import std.math; import std.typecons; void main() { auto N = readln.chomp.to!int; int[] prices; foreach(i; 0..50000) { auto price = cast(int)(1.08f * cast(real)i); if (price == N) { prices ~= i; } } if (prices.length == 0) { writeln(":("); } else { writeln(prices[0]); } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.container; import std.random; void main() { auto x = readln.chomp.split.map!(to!int); auto a = x[0]; auto b = x[1]; auto op = a == b ? "==" : a > b ? ">" : "<"; writeln("a ", op, " b"); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; void main() { auto N = readln.chomp.to!int; bool[int] data; foreach(_; 0..N){ auto A = readln.chomp.to!int; if(A in data) data.remove(A); else data[A] = true; } writeln(data.length); }
D
import std.stdio, std.conv, std.string, std.array, std.algorithm, std.numeric, std.math, std.range; void main() { string s = readln.strip; long[] xs = new long[](s.length); char prev = 'x'; bool last_even = false; long last_marked = -1; long next = 0; long next_1 = 0; foreach (i; 0 .. s.length) { if (prev == 'R' && s[i] == 'L') { last_marked = i - 1; last_even = (i - 1) % 2 == 0; if ((i - 1) % 2 == 0) { xs[i - 1] = next; xs[i] = next_1; } else { xs[i - 1] = next_1; xs[i] = next; } next = 0; next_1 = 0; } if (s[i] == 'L') { if (last_even == (i % 2 == 0)) { xs[last_marked]++; } else { xs[last_marked + 1]++; } } else { if (i % 2 == 0) { next++; } else { next_1++; } } prev = s[i]; } xs.map!(x => x.to!string).join(" ").writeln; }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop; // dfmt on void main() { long N = lread(); auto ans = new long[](100_000_000); foreach (i; 1 .. 500) foreach (j; 1 .. 500) foreach (k; 1 .. 500) { ans[i * i + j * j + k * k + i * j + j * k + k * i]++; } foreach (i; 1 .. N + 1) { writeln(ans[i]); } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long r1, c1, r2, c2; scan(r1, c1, r2, c2); auto fact = new long[](r2 + c2 + 2); fact[0] = 1; foreach (i; 1 .. fact.length) fact[i] = fact[i - 1] * i % MOD; auto fact_inv = new long[](fact.length); foreach (i; 0 .. fact_inv.length) fact_inv[i] = powmod(fact[i], MOD - 2, MOD); long combination_mod(long n, long k) { if (n < k) return 1; return (fact[n] * fact_inv[k] % MOD) * fact_inv[n - k] % MOD; } long g(long r, long c) { long ans; foreach (y; 0 .. r + 1) { ans = (ans + combination_mod(y + c + 1, y + 1)) % MOD; } return ans; } writeln((MOD * 2 + g(r2, c2) - g(r2, c1 - 1) - g(r1 - 1, c2) + g(r1 - 1, c1 - 1)) % MOD); } T powmod(T = long)(T x, T n, T m) { if (n < 1) return 1; if (n & 1) { return x * powmod(x, n - 1, m) % m; } T tmp = powmod(x, n / 2, m); return tmp * tmp % m; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto X = readln.chomp.to!long; auto fs = new long[](1001); foreach (long i; 0..1001) fs[i] = i^^5; foreach (i; 0..1001) { foreach (j; 0..1001) { if (fs[i] - fs[j] == X) { writeln(i, " ", j); return; } if (fs[i] + fs[j] == X) { writeln(i, " ", -j); return; } if (-fs[i] + fs[j] == X) { writeln(-i, " ", -j); return; } } } }
D
import std.stdio, std.string, std.conv, std.algorithm, std.math; void main() { while(true) { auto inp = readln.split.map!(to!int); if(!inp[0] && !inp[1] && !inp[2]) break; real a = inp[0], b = inp[1], c = inp[2]; real r = min(a * a + b * b, b * b + c * c, c * c + a * a).sqrt; foreach(_; 0 .. readln.chomp.to!int) { if(readln.chomp.to!int * 2 > r) "OK".writeln; else "NA".writeln; } } }
D
void main() { int n = readln.chomp.to!int; int[] a = readln.split.to!(int[]); auto cnt4 = a.count!(x => x % 4 == 0); auto cnt2 = a.count!(x => x % 4 != 0 && x % 2 == 0); if (cnt4 >= n / 2) "Yes".writeln; else if (cnt4 >= (n - cnt2 + 1) / 2) "Yes".writeln; else "No".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.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } void main() { try { for (; ; ) { const T = readToken(); auto ans = T.dup; foreach_reverse (ref c; ans) { if (c == '?') { c = 'D'; } } writeln(ans); } } catch (EOFException e) { } }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop;//, core.stdc.stdio; int sum2d(int[][] A, int r1, int c1, int r2, int c2) { return A[r2+1][c2+1] - A[r2+1][c1] - A[r1][c2+1] + A[r1][c1]; } void main() { auto s = readln.split.map!(to!int); auto H = s[0]; auto W = s[1]; auto Q = s[2]; auto B = H.iota.map!(_ => readln.chomp).array; auto blue = new int[][](H+1, W+1); foreach (i; 0..H) foreach (j; 0..W) blue[i+1][j+1] = B[i][j] == '1' ? 1 : 0; foreach (i; 0..H) foreach (j; 0..W) blue[i+1][j+1] += blue[i+1][j]; foreach (j; 0..W) foreach (i; 0..H) blue[i+1][j+1] += blue[i][j+1]; auto tate = new int[][](H+1, 2*W); foreach (i; 0..H) foreach (j; 0..W-1) tate[i+1][2*j+2] = (B[i][j] == '1' && B[i][j+1] == '1') ? 1 : 0; foreach (i; 0..H) foreach (j; 0..2*W-1) tate[i+1][j+1] += tate[i+1][j]; foreach (j; 0..2*W-1) foreach (i; 0..H) tate[i+1][j+1] += tate[i][j+1]; auto yoko = new int[][](2*H, W+1); foreach (i; 0..H-1) foreach (j; 0..W) yoko[2*i+2][j+1] = (B[i][j] == '1' && B[i+1][j] == '1') ? 1 : 0; foreach (i; 0..2*H-1) foreach (j; 0..W) yoko[i+1][j+1] += yoko[i+1][j]; foreach (j; 0..W) foreach (i; 0..2*H-1) yoko[i+1][j+1] += yoko[i][j+1]; while (Q--) { s = readln.split.map!(to!int); int r1 = s[0] - 1, c1 = s[1] - 1, r2 = s[2] - 1, c2 = s[3] - 1; int a = sum2d(blue, r1, c1, r2, c2); int b = sum2d(tate, r1, c1*2, r2, c2*2); int c = sum2d(yoko, r1*2, c1, r2*2, c2); writeln(a - b - c); } }
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() { auto dp = new long[][](10 ^^ 5 + 1, 3); long N = lread(); auto ABC = new long[][](10 ^^ 5 + 1, 3); foreach (i; 0 .. N) scan(ABC[i][0], ABC[i][1], ABC[i][2]); foreach (i; 0 .. N) { foreach (j; 0 .. 3) dp[i + 1][j] = max(dp[i][(j + 1) % 3] + ABC[i][(j + 1) % 3], dp[i][(j + 2) % 3] + ABC[i][(j + 2) % 3]); } writeln(dp[N][0].max(dp[N][1]).max(dp[N][2])); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { readln; long r; foreach (a; readln.split.to!(long[])) r += a-1; writeln(r); }
D
import std.stdio; import std.conv; import std.string; void main() { int[] input = readln.split.to!(int[]); int k = input[0]; int s = input[1]; long ans = 0; foreach_reverse (i; 0..k + 1) { foreach_reverse (j; 0..k + 1) { auto tmp = s - i - j; if (tmp <= k && tmp >= 0){ ans++; } } } writeln(ans); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!long).array; long a = 0, b = A.sum; long ans = 1L << 60; foreach (i; 0..N) { if (i != 0) { ans = min(ans, abs(a - b)); } a += A[i], b -= A[i]; } ans.writeln; }
D
// Cheese-Cracker: cheese-cracker.github.io void theCode(){ int n = scan!int; auto arr = scanArray!int; auto freq = new int[2*n + 5]; auto take = new int[2*n + 5]; arr.each!(a => ++freq[a]); ll cnt = 0; for(int i = 0; i <= 2*n+1; ++i){ if(freq[i]){ freq[i] += take[i];} if(freq[i] > 1){ take[i+1] = 1; --freq[i]; } if(freq[i]) ++cnt; else if(take[i]) ++cnt; } show(freq, take); writeln(cnt); } void main(){ long tests = 1; tests = scan; // Toggle! while(tests--) theCode(); } /********* That's All Folks! *********/ import std.stdio, std.conv, std.functional, std.string, std.algorithm; import std.container, std.range, std.typecons, std.numeric, std.math, std.random; static string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;} T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; } void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } } alias ll = long, tup = Tuple!(long, "x", long, "y");
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "next", long, "cost"); alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less); void main() { long n, a, b; scan(n, a, b); auto x = aryread(); iota(n - 1).map!(t => min(a * (x[t + 1] - x[t]), b)).sum.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.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto A = RD; auto B = RD; auto C = RD; auto D = RD; long ans; foreach (z; C..D+1) { auto len = A + B; auto d = len - z - 1; auto tmp = (B-A+1)*(C-B+1); debug writeln("tmp:", tmp); if (d < 0) { d = abs(d); auto len2 = A+C; auto d2 = len2 - z - 1; if (d2 < 0) { d2 = abs(d2); auto h = min(B-A+1, d2-1); tmp -= (C-B+1) * h; debug writeln("tmp:", tmp); d = (C-B+1); tmp -= d * (d+1) / 2; auto BA = B-A+1 - h; auto d3 = max(0, d-BA); tmp += d3 * (d3+1) / 2; } else { tmp -= d * (d+1) / 2; auto BA = B-A+1; auto d3 = max(0, d-BA); tmp += d3 * (d3+1) / 2; } debug writeln("tmp:", tmp); } ans += max(0, tmp); debug writeln("z:", z, " ans:", tmp); } writeln(ans); stdout.flush; debug readln; }
D
// import chie template :) {{{ import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons; // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string : chomp; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.chomp.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } } // }}} void main() { auto cin = new Scanner; int n; cin.scan(n); int sum; foreach (i; 0 .. n) { sum += abs(cin.next!int - cin.next!int) + 1; } writeln(sum); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; import std.range; auto s=readln.chomp.to!(char[]); int[char] cnt; foreach(c; s){ if(!(c in cnt)) cnt[c]=1; } auto mi=s.length; foreach(tg; cnt.keys){ int i=-1, j; int r=0; while(j<s.length){ if(s[j]==tg){ r=max(r, j-i-1); i=j; } j++; } mi=min(mi, max(r, s.length-i-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, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void main() { auto Q = readln.chomp.to!int; foreach (_; 0..Q) { auto ab = readln.split.to!(long[]); auto A = ab[0]; auto B = ab[1]; if (A > B) swap(A, B); if (A == B || A+1 == B) { writeln(2*(A-1)); continue; } auto c = sqrt((A*B).to!double).to!long; if (c^^2 == A*B) --c; if (c * (c+1) >= A*B) { writeln(2*(c-1)); } else { writeln(2*c-1); } } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { int n; foreach (c; readln.chomp) if (c == '2') ++n; writeln(n); }
D
import std.stdio, std.array, std.conv, std.string; void main() { while (true) { string[] input = split(readln()); if (input[1] == "?") break; writeln(calc(input)); } } int calc(string[] s) { int a = to!int(s[0]), b = to!int(s[2]); string op = s[1]; if (op == "+") return a + b; if (op == "-") return a - b; if (op == "*") return a * b; if (op == "/") return a / b; return -1; }
D
void main() { auto io = new IO(); auto N = io.line!size_t()[0]; auto input = io.line!size_t(); size_t pair = 0; foreach( i,v ; input ){ if( i<v-1 && i==input[v-1]-1 ){ pair++; } } writeln(pair); } import std.stdio,std.string,std.conv; class IO { string str( size_t lines = 1 ) { return readln().chomp(); } T[] line( T = real , string sp = " " )( size_t lines = 1 ) { T[] ret; foreach( i ; 0..lines ) ret ~= readln().chomp().split(sp).convert!T(); return ret; } T[][] rect( T = real , string sp = " " )( size_t lines = 1 ) { T[][] ret = new T[][](lines); foreach( i ; 0..lines ) ret[i] = readln().chomp().split(sp).convert!T(); return ret; } } R[] convert( R , T )( T[] args ) pure { R[] ret = new R[](args.length); foreach( i ; 0..args.length ) ret[i] = args[i].to!R(); return ret; } T sum( T )( in T[] args ) pure nothrow { T ret = 0; foreach( elm ; args ) ret += elm; return ret; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nk = readln.split.to!(long[]); auto N = nk[0]; ulong K = nk[1]; long[31] rs; foreach (_; 0..N) { auto ab = readln.split.to!(long[]); ulong A = ab[0]; auto B = ab[1]; if ((A&K) == A) rs[30] += B; foreach (i, ref r; rs) { if ( (K&(1L<<i)) && !(A&(1L<<i)) && (((K>>(i+1))&(A>>(i+1))) == (A>>(i+1))) ) { r += B; } } } long r; foreach (ref rr; rs) r = max(r, rr); writeln(r); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } long disit_sum(long a) { long sum,disit_num; foreach(disit;0 .. 9) { disit_num = a / pow(10, disit); sum += disit_num % 10; } return sum; } void main() { long n = lread(); string s = sread(); foreach(e;s) { if(e == 'Y') { writeln("Four"); return; } } writeln("Three"); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; void main() { foreach (line; stdin.byLine) { auto x = line.split.map!(to!int).array; int sum; foreach (i; 0..x[2]) { x[0] %= x[1]; x[0] *= 10; sum += x[0] / x[1]; } sum.writeln; } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; void main(){ auto A=readln.chomp; writeln(A.count('1')); }
D
void main() { int[] tmp = readln.split.to!(int[]); int n = tmp[0], m = tmp[1]; int[] a = new int[n+1]; foreach (i; 0 .. m) { tmp = readln.split.to!(int[]); int l = tmp[0] - 1, r = tmp[1]; ++a[l]; --a[r]; } foreach (i; 0 .. n) { a[i+1] += a[i]; } a.count(m).writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.typecons;
D
import std.stdio; import std.array; import std.conv; import std.string; import std.algorithm; void main(){ int sScore = 0; int tScore = 0; string[] sInput = readln().split(); string[] tInput = readln().split(); foreach(string s; sInput){ sScore += s.to!int(); } foreach(string s; tInput){ tScore += s.to!int(); } if(sScore > tScore){ writeln(sScore); }else{ writeln(tScore); } }
D
import std.stdio, std.array, std.conv, std.string, std.algorithm, std.math; void main() { auto n = readln.chomp.to!int; auto a = readln.chomp.split; for (int i = n - 1; i >= 0; i--) { write(a[i]); if (i != 0) { write(" "); } } 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 N = readln.chomp; auto DP = new int[][](N.length+1, 2); DP[0][0] = 0; DP[0][1] = 1; foreach (i; 0..N.length) { auto n = (N[i] - '0').to!int; DP[i+1][0] = min(DP[i][0] + n, DP[i][1] + 10-n); DP[i+1][1] = min(n == 9 ? int.max : (DP[i][0] + n+1), DP[i][1] + 10-n-1); } writeln(DP[N.length][0]); }
D
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math, std.container, std.typecons; import core.stdc.stdio; // foreach, foreach_reverse, writeln class BIT { int[] data; this(int n) { data = new int[n+1]; } BIT dup() { BIT ret = new BIT(to!int(data.length)); ret.data = data.dup; return ret; } void add(int i, int x=1) { i++; while (i < data.length) { data[i] += x; i += i&-i; } } int sum(int i) { int ret = 0; i++; while (i) { ret += data[i]; i -= i&-i; } return ret; } } void main() { int n; scanf("%d", &n); int[] a = new int[n]; foreach (i; 0..n) scanf("%d", &a[i]); long L = long(n+1)*n/2; long M = (L+1)/2; int l = 0, u = (1<<30)-10; long count(int[] b) { int s = n; long ret = 0; BIT c = new BIT(n*2+1); foreach (i; 0..n) { c.add(s); s += b[i]; ret += c.sum(s); } //writeln(b); writeln(ret); return ret; } while (l+1 < u) { int m = (l+u)/2; int[] b = new int[n]; foreach (i; 0..n) { b[i] = a[i]<m ? -1 : 1; } if (count(b) < M) u = m; else l = m; } writeln(l); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; void main() { char a; scan(a); writeln('a' <= a && a <= 'z' ? 'a' : 'A'); } void scan(T...)(ref T args) { auto line = readln.split; // @suppress(dscanner.suspicious.unmodified) foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { auto s = readln.chomp; writeln(700 + s.count('o') * 100); }
D
import std.stdio, std.string, std.conv, std.range, std.algorithm; void main() { auto w = readln.chomp; int[char] char_count; auto beautiful = true; foreach (c; w) { char_count[c]++; } if (char_count.values.all!"a % 2 == 0") { "Yes".writeln; } else { "No".writeln; } }
D
// import chie template :) {{{ import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons, std.format; // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } // }}} void main() { auto cin = new Scanner; int a, b, t; cin.scan(a, b, t); int res; for (int i = a; i <= t; i += a) { res += b; } writeln(res); }
D
import std.algorithm; import std.array; import std.conv; import std.stdio; void main() { long[] n_and_k; long n; long k; n_and_k = readln.split.to!(long[]); n = n_and_k[0]; k = n_and_k[1]; writeln(min(n % k, k - n % k)); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); void main() { auto n = sread(); long sum_n; foreach (i; 0 .. n.length) { // writeln(n[i] - '0'); sum_n += (n[i] - '0'); } // writeln('s', sum_n); // writeln(n.length); auto nn = n.to!long(); // writeln(nn); auto tmp = new long[](n.length); tmp[0] = nn / (10 ^^ ((n.length) - 1)) - 1; foreach (i; 1 .. (n.length)) { tmp[i] = 9; } // writeln(tmp); writeln(max(tmp.sum(), sum_n)); } auto func(long n) { long sum_x; while (n > 0) { sum_x += n % 10; n /= 10; } return sum_x; } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
D
import std.stdio; import std.algorithm; import std.string; import std.conv; void main(){ int sum = 0; int num = 0; int count = 0; while(true){ auto s = readln(); if(stdin.eof()) break; string[] n1 = split(chomp(s),','); int[2] n; n = [to!int(n1[0]),to!int(n1[1])]; sum += n[0] * n[1]; num += n[1]; count++; } real ans = to!real(num)/to!real(count); int res; ans *= 10; if(ans%10 >= 5) res = to!int(ans)/10 + 1; else res = to!int(ans); writeln(sum); writeln(res); }
D
import std.stdio; import std.string; import std.math; import std.conv; import std.algorithm; import std.bigint; void main(){ while(true){ long n = to!long(chomp(readln())); if(n == 0) break; long[] a; a ~= 0; a ~= to!long(chomp(readln())); for(long i=1;i<n;i++){ a ~= a[i] + to!long(chomp(readln())); } long res = a[1]; for(long i=0;i<=n;i++){ for(long j=i+1;j<=n;j++){ res = max( a[j] - a[i] , res); } } writeln(res); } }
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } void main(){ long n = read.to!long; long[] as; foreach(i; 0 .. n) as ~= read.to!long; long old_a = 0; long ans = 0; foreach_reverse(a; as){ if(a >= old_a){ ans += a; } else if(a == old_a - 1){ ans += 0; } else{ ans = -1; break; } debug writeln("a:", a, " old_a:", old_a, " ans:", ans); old_a = a; } if(old_a > 0) ans = -1; ans.writeln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 10L^^15; void main() { int n, w; scan(n, w); auto ws = new int[](n); auto v = new int[](n); int vs; foreach (i ; 0 .. n) { scan(ws[i], v[i]); vs += v[i]; } auto dp = new long[](vs + 1); dp[] = inf; dp[0] = 0; foreach (i ; 0 .. n) { foreach_reverse (j ; 0 .. vs - v[i] + 1) { dp[j + v[i]] = min(dp[j + v[i]], dp[j] + ws[i]); } } foreach_reverse (i ; 0 .. vs + 1) { if (dp[i] <= w) { writeln(i); return; } } } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
void main() { long n, k; rdVals(n, k); long[] a = rdRow; long[] b = new long[n+1]; foreach (i, x; a) { b[i+1] = b[i] + x; } long[] list = new long[n*(n+1)>>1]; long idx; foreach (i; 0 .. n) { foreach (j; i .. n) { list[idx++] = b[j+1] - b[i]; } } long result; foreach_reverse (i; 0 .. 41) { long num = 1L << i; long cnt; foreach (l; list) { if (((result | num) & l) == (result | num)) ++cnt; } if (cnt >= k) result |= num; } result.writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
/* imports all std modules {{{*/ import std.algorithm, std.array, std.ascii, std.base64, std.bigint, std.bitmanip, std.compiler, std.complex, std.concurrency, std.container, std.conv, std.csv, std.datetime, std.demangle, std.encoding, std.exception, std.file, std.format, std.functional, std.getopt, std.json, std.math, std.mathspecial, std.meta, std.mmfile, std.net.curl, std.net.isemail, std.numeric, std.parallelism, std.path, std.process, std.random, std.range, std.regex, std.signals, std.socket, std.stdint, std.stdio, std.string, std.system, std.traits, std.typecons, std.uni, std.uri, std.utf, std.uuid, std.variant, std.zip, std.zlib; /*}}}*/ /+---test 3 1 4 3 ---+/ /+---test 2 5 5 ---+/ void main(string[] args) { const N = readln.chomp.to!long; struct Pair { long i, v; } Pair[2] M; foreach (i; 0..N) { const v = readln.chomp.to!long; if (M[0].v <= v) { swap(M[0], M[1]); M[0] = Pair(i, v); } else if (M[1].v <= v) { M[1] = Pair(i, v); } } auto ans = new long[N]; foreach (i; 0..N) { const p = M[0].i != i? M[0]: M[1]; p.v.writeln; } }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = readln.chomp.to!int; auto d = new int[](n); foreach (i; 0..n) d[i] = readln.chomp.to!int; writeln(d.sort().uniq.walkLength); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int x, a, b; readV(x, a, b); if (a >= b) writeln("delicious"); else if (x+a >= b) writeln("safe"); else writeln("dangerous"); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto rd = readln.split.to!(int[]), a = rd[0], b = rd[1], c = rd[2]; writeln(b-a == c-b ? "YES" : "NO"); }
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 N = input[0], D = input[1]; int[][] ps = new int[][N]; foreach(ref p; ps) { p = readln.split.to!(int[]); } int[] tbl = new int[0]; { int i = 1; int maxL = D * 40 * 40; while(i*i < maxL) { tbl ~= i*i; i++; } } auto dic = assumeSorted(tbl); ulong ans = 0; foreach(i; 0..(N-1)) { foreach(j; i..N) { int l = 0; foreach(k; 0..D) { auto a = ps[i][k] - ps[j][k]; l += a*a; } if (dic.contains(l)) ans++; } } ans.writeln; }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { string s; scan(s); auto cnt = new int[](3); foreach (ch ; s) { cnt[ch - 'a']++; } cnt.sort(); int k = cnt[0]; bool ok = (cnt == [k, k, k]) || (cnt == [k, k, k + 1]) || (cnt == [k, k + 1, k + 1]); writeln(ok ? "YES" : "NO"); } struct UnionFind { private { int N; int[] p; int[] rank; } this (int n) { N = n; p = iota(N).array; rank = new int[](N); } int find_root(int x) { if (p[x] != x) { p[x] = find_root(p[x]); } return p[x]; } bool same(int x, int y) { return find_root(x) == find_root(y); } void unite(int x, int y) { int u = find_root(x), v = find_root(y); if (u == v) return; if (rank[u] < rank[v]) { p[u] = v; } else { p[v] = u; if (rank[u] == rank[v]) { rank[u]++; } } } } 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); } } } struct Queue(T) { private { int N, head, tail; T[] data; } this(int n) { N = n + 1; data = new T[](N); } bool empty() { return head == tail; } bool full() { return (tail + 1) % N == head; } T front() { return data[head]; } void push(T x) { assert(!full); data[tail++] = x; tail %= N; } void pop() { assert(!empty); head = (head + 1) % N; } void clear() { head = tail = 0; } }
D
import std.stdio; import std.algorithm; import std.string; import std.functional; import std.array; import std.conv; import std.math; import std.typecons; import std.regex; import std.range; void main(){ while(true){ int n = readln().chomp().to!int; if(n==0) break; int[] k = readln().split().to!(int[]); int len = to!int(k.length); bool flg = false; foreach(i;k) { if(i<1) len--; if(i >= 2) flg = true; } if(flg){ len++; writeln(len); }else{ writeln("NA"); } } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto s = readln.chomp; auto c = new char[](s.length); int index; foreach (i; 0..s.length) { if (s[i] == 'B') { if (index != 0) { index--; } } else { c[index] = s[i]; index++; } } foreach (i; 0..index) { c[i].write; } writeln(""); }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { writeln(readln.chomp.split.to!(int[]).reduce!"a-b"+1); }
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 bool[](t); foreach (ti; 0..t) { auto a = RD!int; auto b = RD!int; auto c = RD!int; auto tot = a + b + c; if (tot % 9) continue; ans[ti] = min(a, b, c) >= tot / 9; } foreach (e; ans) { writeln(e ? "YES" : "NO"); } stdout.flush; debug readln; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); long bignum = 1_000_000_007; T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } auto manhattan(long x1, long y1, long x2, long y2) { return abs(y2 - y1) + abs(x2 - x1); } void main() { auto n = lread(); long t_prev, x_prev, y_prev; bool travel = true; foreach (_; 0 .. n) { long t, x, y; scan(t, x, y); auto limit = t - t_prev; auto m_dist = manhattan(x_prev, y_prev, x, y); limit -= m_dist; if(limit < 0 || limit % 2) travel = false; t_prev = t; x_prev = x; y_prev = y; } if(travel) writeln("Yes"); else writeln("No"); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(int[] xs) { int ans = 0; foreach (x; xs) { while (x > 0 && x % 2 == 0) { ans++; x /= 2; } } return ans; } void main() { readint; auto xs = readints; auto ans = calc(xs); writeln(ans); }
D
import std.stdio, std.algorithm; void main() { auto p = new long[2001]; foreach (i; 0 .. 1001) foreach (j; 0 .. 1001) p[i + j]++; while (true) { if (stdin.eof) break; int N; scanf("%d\n", &N); long ans; foreach (i; 0 .. min(N, 2000) + 1) if (0 <= N - i && N - i <= 2000) ans += p[i] * p[N - i]; writeln(ans); } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { readln.split.to!(int[]).reduce!"max(a-b, 0)".writeln; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.range, std.conv, std.string, std.array, std.functional, std.math; import std.algorithm.comparison, std.algorithm.iteration, std.algorithm.mutation, std.algorithm.searching, std.algorithm.setops, std.algorithm.sorting; import std.container.binaryheap; import std.typecons; struct Pair{ long a; long b; } void main() { auto input = readln().strip.split.to!(long[]); const N = input[0]; const M = input[1]; auto list = uninitializedArray!(Pair[])(M); foreach(i; 0..M) { input = readln().strip.split.to!(long[]); list[i] = Pair(input[0], input[1]); } long[] resList; UnionFind unionFind = new UnionFind(N+1); long count = N*(N-1)/2; foreach_reverse(e; list) { if(unionFind.same(e.a, e.b)) { resList ~= count; continue; } resList ~= count; count -= unionFind.count(e.a)*unionFind.count(e.b); unionFind.unite(e.a, e.b); } foreach_reverse(res; resList) { writeln(res); } } class UnionFind{ private long[] list; this(long n) { list.length = n; list.fill(-1); } void unite(long a, long b) { list[root(a)] += list[root(b)]; list[root(b)] = root(a); } bool same(long a, long b) { return root(a) == root(b); } long count(long n) { return -list[root(n)]; } private long root(long n) { if(list[n] < 0) { return n; } return list[n] = root(list[n]); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; int[][10^^5+1] GP; bool[10^^5+1] CS; int[10^^5+1] PS; int find_path(int N) { auto ss = [[0, 0]]; for (;;) { auto h = ss[0]; auto i = h[0]; auto s = h[1]; ss = ss[1..$]; PS[i] = s; if (s == N-1) return i+1; foreach (n; GP[s]) { if (CS[n]) continue; CS[n] = true; ss = [[i+1, n]] ~ ss; } } } int count_path(int s) { if (CS[s]) return 0; CS[s] = true; auto r = 1; foreach (n; GP[s]) r += count_path(n); return r; } void main() { auto N = readln.chomp.to!int; foreach (_; 0..N-1) { auto ab = readln.split.to!(int[]); auto a = ab[0] - 1; auto b = ab[1] - 1; GP[a] ~= b; GP[b] ~= a; } auto m = find_path(N); auto path = PS[0..m]; CS[] = false; foreach (p; path) CS[p] = true; int f, s; foreach (p; path[0..($+1)/2]) { f += 1; foreach (n; GP[p]) f += count_path(n); } foreach (p; path[($+1)/2..$]) { s += 1; foreach (n; GP[p]) s += count_path(n); } writeln(f > s ? "Fennec" : "Snuke"); }
D
import std.conv; import std.array; import std.string; import std.algorithm; import std.stdio; void main() { ulong r = 1; enum ulong RMax = 1000000000000000000UL; enum ulong EMax = 100000UL; readln; auto a = readln.chomp.split(' ').map!(to!ulong).array; foreach (e;a) { if (e == 0) { writeln("0"); return; } } foreach (e;a) { if ((r / EMax) * e > (RMax / EMax)) { writeln("-1"); return; } r *= e; } if (r > RMax) { writeln("-1"); } else { writeln(r); } }
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; } } // }}} // 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 k, a, b; cin.scan(k, a, b); int t = a / k + cast(int)(a % k != 0); if (a <= k * t && k * t <= b) { writeln("OK"); } else { writeln("NG"); } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; void main() { int n, m; scan(n, m); auto p = readln.split.to!(int[]); p[] -= 1; auto uf = UnionFind(n); foreach (i ; 0 .. m) { int xi, yi; scan(xi, yi); xi--, yi--; uf.merge(xi, yi); } int ans; foreach (i ; 0 .. n) { if (uf.same(i, p[i])) { ans++; } } ans.writeln; } struct UnionFind { private { int _size; int[] _parent; } this(int N) in { assert(N > 0); } body { _size = N; _parent = new int[](_size); foreach (i ; 0 .. _size) { _parent[i] = i; } } int findRoot(int x) in { assert(0 <= x && x < _size); } body { if (_parent[x] != x) { _parent[x] = findRoot(_parent[x]); } return _parent[x]; } bool same(int x, int y) in { assert(0 <= x && x < _size); assert(0 <= y && y < _size); } body { return findRoot(x) == findRoot(y); } void merge(int x, int y) in { assert(0 <= x && x < _size); assert(0 <= y && y < _size); } body { int u = findRoot(x); int v = findRoot(y); if (u == v) return; _parent[v] = u; } } 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
module app; import core.bitop; import std.algorithm; import std.array; import std.bigint; import std.conv; import std.stdio; import std.string; struct Input { int a; int b; } void parseInput(T)(out Input input, T file) { with (file) with (input) { a = readln().strip().to!int; b = readln().strip().to!int; } } struct Output { } auto main2(Input* input) { return 6 - input.a - input.b; } void parseExample(out Input input, string example) { struct Adapter { string[] _lines; this(string input) { _lines = input.splitLines(); } string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; } } parseInput(input, Adapter(example)); } void main() { Input input = void; parseInput(input, stdin); auto result = main2(&input); writeln(result); }
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 x = RD-1L; auto y = RD; auto k = RD; auto a = k*y; auto b = k + a - 1; ans[ti] = (b+x-1) / x + k; } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio,std.conv, std.algorithm, std.container,std.array,std.range,std.string,std.typecons,std.numeric; const dx = [1,0,-1,0], dy = [0,1,0,-1]; const readMixin = q{ auto line = readln().split(); if (line.length < args.length) return; foreach(i,ref arg; args) arg = line[i].to!(typeof(arg)); }; void read(T...) (auto ref T args){ mixin(readMixin);} void readArray(T)(auto ref T args){ mixin(readMixin);} //------------------------------ const coin = [10,50,100,500]; void main(){ int total; for(read(total); total ; read(total), write(total ? "\n":"")){ auto have = new int[4]; readArray(have); int howMany = int.max; auto uses = new int[4]; //0,0,0,0 ~ i0,i1,i2,i3 ?????§?????? foreach(i0;iota(have[0]+1)){ foreach(i1;iota(have[1]+1)){ foreach(i2;iota(have[2]+1)){ foreach(i3;iota(have[3]+1)){ auto tmpuses = [i0,i1,i2,i3]; auto pay = coin.dotProduct(tmpuses); if (pay < total) continue; int tmpHowMany = have.sum + howManyCoins(pay - total) - tmpuses.sum; if (tmpHowMany < howMany ){ howMany = tmpHowMany; uses = tmpuses; } } } } } foreach(i,u;uses){if(u >0)writeln(coin[i]," ",u);} } } int howManyCoins(int pay){ int res; foreach_reverse(c;coin){ while(pay >= c){ pay -= c;res ++; } } return res; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; void main() { int h, w; scan(h, w); if (h == 1 && w == 1) { writeln("No"); return; } auto ban = new char[][](h, w); iota(h).each!(i => ban[i] = readln.chomp.dup); int[] di = [0, 1, 0, -1]; int[] dj = [1, 0, -1, 0]; foreach (i ; 0 .. h) { foreach (j ; 0 .. w) { if (ban[i][j] == '.') continue; int cnt; foreach (k ; 0 .. 4) { int ni = i + di[k]; int nj = j + dj[k]; if (ni < 0 || ni >= h) continue; if (nj < 0 || nj >= w) continue; if (ban[ni][nj] == '#') cnt++; } if (cnt == 0) { debug { writefln("(%d, %d)", i, j); } writeln("No"); return; } } } writeln("Yes"); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.conv; import std.string; void main() { string[] buf; int w,h; buf = split(readln()); w = to!(int)(buf[0]); h = to!(int)(buf[1]); writeln(w*h,' ',2*w+2*h); }
D
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.math; void main(){ int n = readln.chomp.to!int; int[] tmp = readln.chomp.split.map!(to!int).array; int t = tmp[0], a = tmp[1]; int[] hs = readln.chomp.split.map!(to!int).array; real f(int x){ return abs(a.to!real - (t.to!real - x.to!real * 0.006)); } int i0 = 0; real min = f(hs[0]); for(int i = 0; i < n; i ++){ if(f(hs[i]) < min){ min = f(hs[i]); i0 = i; } } writeln(i0 + 1); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto X = RD; auto L = RDR.ARR; long ans = 1, a; foreach (i; 0..N) { a += L[i]; if (a <= X) { ++ans; } } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nhw = readln.split.to!(int[]); auto N = nhw[0]; auto H = nhw[1]; auto W = nhw[2]; int r; foreach (_; 0..N) { auto ab = readln.split.to!(int[]); if (ab[0] >= H && ab[1] >= W) ++r; } writeln(r); }
D
import core.bitop, std.bitmanip; import core.checkedint; import std.algorithm, std.functional; import std.array, std.container; import std.bigint; import std.conv; import std.math, std.numeric; import std.range, std.range.interfaces; import std.stdio, std.string; import std.typecons; void main() { auto s = readln.chomp; auto t = readln.chomp; auto input = new int[][] (300); foreach (i, e; s) { input[e] ~= (i+1).to!int; } debug { input.writeln; } auto pos = input.map!assumeSorted; long ans = 0; int cpos = 0; foreach (e; t) { if (pos[e].empty()) { writeln(-1); return; } auto r = pos[e].upperBound(cpos); if (r.empty()) { ans += s.length - cpos; cpos = 0; r = pos[e].upperBound(0); } auto nxt = r[0]; ans += nxt - cpos; debug { writeln(e, ' ', ans, ' ', nxt); } cpos = nxt; } ans.writeln; }
D
/+ dub.sdl: name "B" dependency "dcomp" version=">=0.7.4" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; int main() { Scanner sc = new Scanner(stdin); int n; sc.read(n); static struct E { int to; int idx; } E[][] g = new E[][](n); foreach (i; 0..n-1) { int a, b; sc.read(a, b); a--; b--; g[a] ~= E(b, i); g[b] ~= E(a, i); } long[] z; sc.read(z); ulong sm = 0; int err_id = -1; ulong f; long[] res = new long[n-1]; int[] sz = new int[n]; void dfs(int p, int b, int bidx) { sz[p] = 1; foreach (e; g[p]) { int d = e.to; if (d == b) continue; dfs(d, p, e.idx); sz[p] += sz[d]; } long dw = sz[p]; long up = n - dw; if (b == -1) return; if (dw == up) { err_id = bidx; f = 2 * dw * up; } else { long u = (z[p] - z[b]) / (up - dw); res[bidx] = u; sm += 2 * up * dw * u; } } dfs(0, -1, -1); if (err_id != -1) { res[err_id] = 1919; ulong x = 0; foreach (d; z) x += d; res[err_id] = (x - sm) / f; // res[err_id] = (z.sum().to!ulong - sm);// / f; } writeln(res.map!(to!string).join("\n")); return 0; } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; // import dcomp.array; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { FastAppender!(E[]) buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/array.d */ // module dcomp.array; T[N] fixed(T, size_t N)(T[N] a) {return a;} struct FastAppender(A, size_t MIN = 4) { import std.algorithm : max; import std.conv; import std.range.primitives : ElementEncodingType; import core.stdc.string : memcpy; private alias T = ElementEncodingType!A; private T* _data; private uint len, cap; @property size_t length() const {return len;} bool empty() const { return len == 0; } void reserve(size_t nlen) { import core.memory : GC; if (nlen <= cap) return; void* nx = GC.malloc(nlen * T.sizeof); cap = nlen.to!uint; if (len) memcpy(nx, _data, len * T.sizeof); _data = cast(T*)(nx); } void free() { import core.memory : GC; GC.free(_data); } void opOpAssign(string op : "~")(T item) { if (len == cap) { reserve(max(MIN, cap*2)); } _data[len++] = item; } void insertBack(T item) { this ~= item; } void removeBack() { len--; } void clear() { len = 0; } ref inout(T) back() inout { assert(len); return _data[len-1]; } ref inout(T) opIndex(size_t i) inout { return _data[i]; } T[] data() { return (_data) ? _data[0..len] : null; } } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } import core.bitop : popcnt; static if (!__traits(compiles, popcnt(ulong.max))) { public import core.bitop : popcnt; int popcnt(ulong v) { return popcnt(cast(uint)(v)) + popcnt(cast(uint)(v>>32)); } } bool poppar(ulong v) { v^=v>>1; v^=v>>2; v&=0x1111111111111111UL; v*=0x1111111111111111UL; return ((v>>60) & 1) != 0; } /* This source code generated by dcomp and include dcomp's source code. dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp) dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt) */
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int h, w, k; rd(h, w, k); const int mod = 10 ^^ 9 + 7; auto dp = new int[](w); dp[0] = 1; foreach (_; 0 .. h) { auto nex = new int[](w); foreach (bit; 0 .. (1 << (w - 1))) { if (bit & (bit >> 1)) continue; foreach (i; 0 .. w) { if (i - 1 >= 0 && bit & (1 << (i - 1))) { (nex[i - 1] += dp[i]) %= mod; } else if (i < w - 1 && bit & (1 << i)) { (nex[i + 1] += dp[i]) %= mod; } else { (nex[i] += dp[i]) %= mod; } } } dp.swap(nex); } writeln(dp[k - 1]); } void rd(T...)(ref T x) { import std.stdio : readln; import std.string : split; import std.conv : to; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
D
import std.stdio; import std.math; void main() { int n, m; int[] a, b; scanf("%d %d", &n, &m); a.length = n; b.length = m; foreach (i; 0 .. n) scanf("%d", &a[i]); foreach (i; 0 .. m) scanf("%d", &b[i]); int result; while (b.length) { if (a.length == 0) break; if (b[0] < a[0]) { b = b[1..$]; } else { a = a[1..$]; b = b[1..$]; } } writeln(a.length); }
D