code
stringlengths
4
1.01M
language
stringclasses
2 values
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto a=new int[](n); foreach(i; 0..n) rd(a[i]); auto s=a.sum; if(s%10>0){ writeln(s); return; } auto all=reduce!((r, e)=>r&&(e%10==0))(true, a); if(all){ writeln(0); return; } int mn=1000000000; foreach(e; a){ if(e%10>0){ mn=min(mn, e); } } import std.exception; if(mn<1000000000){ writeln(s-mn); }else{ enforce(false); } } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x) e=l[i].to!(typeof(e)); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.container; import std.datetime; char affine(char s, int a, int b) { auto c = s - 'a'; return ((a * c + b) % 26 + 'a').to!char; } void main() { auto n = readln.chomp.to!int; while (n--) { auto str = readln.chomp.map!(to!char).array.split; auto target = str.filter!(a=> a.length == 4).array; int a, b; ll:while (1) { foreach (bb; 0..26) { foreach (e; target) { auto sub = e.map!(x=> affine(x.to!char, a, bb)).array; if (sub == "this" || sub == "that") { b = bb; break ll; } } } a++; } foreach (ref e; str) { e = e.map!(x => affine(x.to!char, a, b)).array; } str.join(" ").writeln; } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; void main() { auto dp = new long[][](11, 1001); dp[0][0] = 1; foreach (i; 0..101) { foreach_reverse (j; i..1001) { foreach_reverse (k; 1..10) { dp[k][j] += dp[k-1][j-i]; } } } while (1) { auto ns = readln.split.map!(to!int); if (!(ns[0] | ns[1])) break; dp[ns[0]][ns[1]].writeln; } }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; long a, b, x; rd(a, b, x); long mod = 1_000_000_000 + 7; auto k = (x - a + 1 + (a - b - 1)) / (a - b); if (k < 0) { k = 0; } auto ans = x % mod + (k % mod) * (b % mod); writeln(ans % mod); } void rd(T...)(ref T x) { import std.stdio : readln; import std.string : split; import std.conv : to; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
D
import std.stdio, std.string, std.conv; import std.array, std.algorithm, std.range; void main() { for(int n; 0!=(n=readln().chomp().to!int()); ) { immutable a = iota(n).map!(_=>readln().chomp().to!int()).array().idup; int m=int.min; auto dp = new int[][](n,n); foreach(i;0..n) foreach(j;i..n) m=max(m,dp[i][j]=(i<j?dp[i][j-1]:0)+a[j]); writeln(m); } }
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<<30; enum mod = 10L^^9 + 7; void main() { int n; scan(n); auto a = readln.split.to!(int[]); int f(int x) { int res; foreach (i ; 0 .. n) { res += (x - a[i])^^2; } return res; } int asum = a.sum(); int ans = min(f(asum / n), f((asum + n - 1) / n)); 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 inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { string s; scan(s); auto n = s.length.to!int; int ans1, ans2; foreach (i ; 0 .. n) { int a = s[i] - '0'; if ((a ^ (i & 1)) == 0) ans1++; if ((a ^ (i & 1)) == 1) ans2++; } int ans = min(ans1, ans2); writeln(ans); } int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new int[][](n, 0); foreach (i; 0 .. m) { int u, v; scan(u, v); if (is1indexed) { u--, v--; } adj[u] ~= v; if (isUndirected) { adj[v] ~= u; } } return adj; } alias Edge = Tuple!(int, "to", int, "cost"); Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new Edge[][](n, 0); foreach (i; 0 .. m) { int u, v, c; scan(u, v, c); if (is1indexed) { u--, v--; } adj[u] ~= Edge(v, c); if (isUndirected) { adj[v] ~= Edge(u, c); } } return adj; } void yes(bool b) { writeln(b ? "Yes" : "No"); } void YES(bool b) { writeln(b ? "YES" : "NO"); } T[] readArr(T)() { return readln.split.to!(T[]); } T[] readArrByLines(T)(int n) { return iota(n).map!(i => readln.chomp.to!T).array; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, 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 arr = new long[](N+1); arr[] = 1; foreach (i; 2..N+1) { long x = i; while (x <= N) { ++arr[x]; x += i; } } long ans; foreach (i; 1..N+1) { ans += i * arr[i]; } writeln(ans); stdout.flush; debug readln; }
D
// Vicfred // https://atcoder.jp/contests/abc134/tasks/abc134_c // greedy import std.algorithm; import std.stdio; import std.conv; import std.string; void main() { const long n = readln.chomp.to!long; long[] as = new long[n]; foreach(i; 0..n) { as[i] = readln.chomp.to!long; } const long maxima = as.maxCount[0]; const long count = as.maxCount[1]; long second = -1; for(long i = 0; i < n; i++) { if(as[i] > second && as[i] != maxima) { second = as[i]; } } foreach(item; as) { if(item == maxima && count == 1) { second.writeln; continue; } maxima.writeln; } }
D
import std.stdio, std.string, std.conv, std.math; void main() { auto ip = readln.split.to!(int[]); if(0 <= (ip[1] - ip[2])){ writeln("delicious"); } else if(ip[0] >= abs(ip[1] - ip[2])){ writeln("safe"); } else { writeln("dangerous"); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string; void main() { auto abc = readln.chomp.split(" ").map!(to!int); writeln( abc[1] - abc[0] == abc[2] - abc[1] ? "YES" : "NO" ); }
D
import std; const MOD = 998244353; const N = 2 * 10^^5 + 10; long[N] fact; long[N] invfact; void initFact() { fact[0] = fact[1] = 1; foreach (i; 2..N) fact[i] = fact[i-1] * i % MOD; invfact[N-1] = modPow(fact[N-1], MOD-2, MOD); foreach_reverse (i; 0..N-1) invfact[i] = invfact[i+1] * (i+1) % MOD; } long nck(long n, long k) { long x = fact[n] * invfact[k] % MOD; return x * invfact[n-k] % MOD; } long calc(int n, int m, int k) { initFact(); long ans = 0; foreach (i; 0..k+1) { auto x = m * modPow(m-1, n-i-1, MOD) % MOD; x = x * nck(n-1, i) % MOD; ans = (ans + x) % MOD; } return ans; } void main() { int n, m, k; scan(n, m, k); writeln(calc(n, m, k)); } 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; long modPow(long x, long k, long m) { if (k == 0) return 1; if (k % 2 == 0) return modPow(x * x % m, k / 2, m); return x * modPow(x, k - 1, m) % m; }
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 a = RD; auto b = RD; writeln(b < a ? a-1 : a); stdout.flush(); debug readln(); }
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.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 N = readInt(); int ans; foreach (x; 1 .. N) { if (x < N - x) { ++ans; } } writeln(ans); } } catch (EOFException e) { } }
D
import std.conv, std.functional, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, 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 K = readLong(); const A = readLong(); const B = readLong(); long ans = 1 + K; if (A <= B && A - 1 <= K) { long tmp = A; tmp += (B - A) * ((K - (A - 1)) / 2); if ((K - (A - 1)) % 2 != 0) { tmp += 1; } chmax(ans, tmp); } writeln(ans); } } catch (EOFException 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; import std.container; alias sread = () => readln.chomp(); ulong bignum = 1_000_000_007; alias Pair = Tuple!(long, "begin", long, "end"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { auto s = sread(); if(s.remove_before || s.remove_bitween) writeln("YES"); else writeln("NO"); } bool remove_before(string s) { auto test = s[$ - 7 .. $]; // writeln(test); if (test == "keyence") return true; return false; } bool remove_bitween(string s) { auto lim = iota(1, 8); foreach (i; lim) { auto test = s[0 .. i] ~ s[$ - "keyence".length + i .. $]; // test.writeln(); if(test == "keyence") return true; } return false; }
D
void main() { long n = readln.chomp.to!long; long[] a = new long[5]; foreach (i; 0 .. 5) { a[i] = readln.chomp.to!long; } long m = a.reduce!min; writeln((n + m - 1) / m + 4); } 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 core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); string[] ST = new string[](2); scan(ST[0], ST[1]); string ans; foreach (i; 0 .. 2 * N) { ans ~= ST[i & 1][i / 2]; } writeln(ans); }
D
/+ dub.sdl: name "C" dependency "dcomp" version=">=0.3.2" lflags "-stack_size" "100000000" +/ import std.algorithm, std.conv, std.range, std.stdio; // import dcomp.scanner; int[128][1001] dp; int calc(int n, int d) { if (n == 1) return d; if (dp[n][d] != -1) return dp[n][d]; int ans = 0; foreach (i; 0..128) { ans = max(ans, calc(n-1, d^i) + i); } return dp[n][d] = ans; } int main(string[] argv) { auto sc = new Scanner(); int t; sc.read(t); foreach (i; 0..1001) { dp[i][] = -1; } foreach (unused; 0..t) { int n, d; sc.read(n, d); writeln(calc(n, d)); } return 0; } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File, stdin; 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 = stdin) { this.f = f; } string[] buf; private bool succ() { while (!buf.length) { if (f.eof) return false; buf = f.readln.split; } return true; } private bool readSingle(T)(ref T x) { if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { //string or char[10] etc x = buf.front; buf.popFront; } else { static if (isStaticArray!T) { //static assert(buf.length == T.length); } x = buf.map!(to!E).array; buf.length = 0; } } else { x = buf.front.to!T; buf.popFront; } 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); } } } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); fout.writeln("1 2 3"); fout.writeln("ab cde"); fout.writeln("1.0 1.0 2.0"); fout.close; Scanner sc = new Scanner(File(fileName, "r")); int a; int[2] b; char[2] c; string d; double e; double[] f; sc.read(a, b, c, d, e, f); assert(a == 1); assert(equal(b[], [2, 3])); assert(equal(c[], "ab")); assert(equal(d, "cde")); assert(e == 1.0); assert(equal(f, [1.0, 2.0])); }
D
/* imports all std modules {{{*/ import std.algorithm, std.array, std.ascii, std.base64, std.bigint, std.bitmanip, std.compiler, std.complex, std.concurrency, std.container, std.conv, std.csv, std.datetime, std.demangle, std.encoding, std.exception, std.file, std.format, std.functional, std.getopt, std.json, std.math, std.mathspecial, std.meta, std.mmfile, std.net.curl, std.net.isemail, std.numeric, std.parallelism, std.path, std.process, std.random, std.range, std.regex, std.signals, std.socket, std.stdint, std.stdio, std.string, std.system, std.traits, std.typecons, std.uni, std.uri, std.utf, std.uuid, std.variant, std.zip, std.zlib; /*}}}*/ /+---test 4 10 30 40 20 ---+/ /+---test 2 10 10 ---+/ /+---test 6 30 10 60 10 60 50 ---+/ void main(string[] args) { readln; const heights = readln.split.map!(to!long).array; const N = heights.length; auto dp = new long[N]; foreach (i; 0..N-1) { dp[i+1] = dp[i] + abs(heights[i] - heights[i+1]); if (i >= 1) { dp[i+1] = dp[i+1].min(dp[i-1] + abs(heights[i-1] - heights[i+1])); } } dp[$-1].writeln; }
D
import std.stdio, std.conv, std.string, std.math, std.regex, std.range, std.ascii, std.algorithm; void main(){ auto ip = readln.split.to!(int[]), r=ip[0], D=ip[1], x=ip[2]; int[] X = new int[](10); X[0] = x; foreach(i; 0..10){ X[i+1] = (r*X[i]-D); writeln(X[i+1]); } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n; scan(n); auto s = readln.chomp; int rc, bc; foreach (ch ; s) { if (ch == 'R') rc++; else bc++; } auto ans = rc > bc; writeln(ans ? "Yes" : "No"); } int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new int[][](n, 0); foreach (i; 0 .. m) { int u, v; scan(u, v); if (is1indexed) { u--, v--; } adj[u] ~= v; if (isUndirected) { adj[v] ~= u; } } return adj; } alias Edge = Tuple!(int, "to", int, "cost"); Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new Edge[][](n, 0); foreach (i; 0 .. m) { int u, v, c; scan(u, v, c); if (is1indexed) { u--, v--; } adj[u] ~= Edge(v, c); if (isUndirected) { adj[v] ~= Edge(u, c); } } return adj; } void yes(bool b) { writeln(b ? "Yes" : "No"); } void YES(bool b) { writeln(b ? "YES" : "NO"); } T[] readArr(T)() { return readln.split.to!(T[]); } T[] readArrByLines(T)(int n) { return iota(n).map!(i => readln.chomp.to!T).array; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio; import std.string; import std.format; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.concurrency; import std.traits; import std.uni; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; enum long INF = long.max/3; enum long MOD = 10L^^9+7; void main() { long N, M; scanln(N, M); long[] as = new long[M]; long[] bs = new long[M]; foreach(i; 0..M) { scanln(as[i], bs[i]); as[i]--; bs[i]--; } long[] xs = new long[M]; long x = (N*N - N)/2; auto qf = QuickFind(N); foreach_reverse(i; 0..M) { xs[i] = x; x -= qf.unite(as[i], bs[i]); } xs.each!writeln; } struct QuickFind { private: size_t[] i2g; size_t[][] gs; public: this(size_t size) { init(size); } void init(size_t size) { gs.length = size; i2g.length = size; foreach(i; 0..size) { gs[i] = [i]; i2g[i] = i; } } long unite(size_t x, size_t y) { if (gs[i2g[x]].length < gs[i2g[y]].length) { swap(x, y); } long res = gs[i2g[x]].length * gs[i2g[y]].length; size_t gx = i2g[x], gy = i2g[y]; if (gx == gy) return 0; foreach(i; gs[gy]) { i2g[i] = gx; } gs[gx] ~= gs[gy]; gs[gy].length = 0; return res; } bool same(size_t x, size_t y) { return findSet(x) == findSet(y); } size_t findSet(size_t index) { return i2g[index]; } bool isRoot(size_t index) { return findSet(index) == index; } size_t[] getSet(size_t index) { return gs[findSet(index)]; } size_t[][] getAllSet() { return gs; } } // ---------------------------------------------- void times(alias fun)(long n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(long n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { // `(x+y-1)/y` will only work for positive numbers ... T t = x / y; if (t * y < x) t++; return t; } T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { T t = x / y; if (t * y > x) t--; return t; } ref T ch(alias fun, T, S...)(ref T lhs, S rhs) { return lhs = fun(lhs, rhs); } unittest { long x = 1000; x.ch!min(2000); assert(x == 1000); x.ch!min(3, 2, 1); assert(x == 1); x.ch!max(100).ch!min(1000); // clamp assert(x == 100); x.ch!max(0).ch!min(10); // clamp assert(x == 10); } mixin template Constructor() { import std.traits : FieldNameTuple; this(Args...)(Args args) { // static foreach(i, v; args) { foreach(i, v; args) { mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v; } } } void scanln(Args...)(auto ref Args args) { import std.meta; template getFormat(T) { static if (isIntegral!T) { enum getFormat = "%d"; } else static if (isFloatingPoint!T) { enum getFormat = "%g"; } else static if (isSomeString!T || isSomeChar!T) { enum getFormat = "%s"; } else { static assert(false); } } enum string fmt = [staticMap!(getFormat, Args)].join(" "); string[] inputs = readln.chomp.split; foreach(i, ref v; args) { v = inputs[i].to!(Args[i]); } } // 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) { private template RebindableOrUnqual(T) { static if (is(T == class) || is(T == interface) || isDynamicArray!T || isAssociativeArray!T) alias RebindableOrUnqual = Rebindable!T; else alias RebindableOrUnqual = Unqual!T; } private auto extremum(alias map, alias selector = "a < b", Range)(Range r) if (isInputRange!Range && !isInfinite!Range && is(typeof(unaryFun!map(ElementType!(Range).init)))) in { assert(!r.empty, "r is an empty range"); } body { alias Element = ElementType!Range; RebindableOrUnqual!Element seed = r.front; r.popFront(); return extremum!(map, selector)(r, seed); } private auto extremum(alias map, alias selector = "a < b", Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seedElement) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void) && is(typeof(unaryFun!map(ElementType!(Range).init)))) { alias mapFun = unaryFun!map; alias selectorFun = binaryFun!selector; alias Element = ElementType!Range; alias CommonElement = CommonType!(Element, RangeElementType); RebindableOrUnqual!CommonElement extremeElement = seedElement; // if we only have one statement in the loop, it can be optimized a lot better static if (__traits(isSame, map, a => a)) { // direct access via a random access range is faster static if (isRandomAccessRange!Range) { foreach (const i; 0 .. r.length) { if (selectorFun(r[i], extremeElement)) { extremeElement = r[i]; } } } else { while (!r.empty) { if (selectorFun(r.front, extremeElement)) { extremeElement = r.front; } r.popFront(); } } } else { alias MapType = Unqual!(typeof(mapFun(CommonElement.init))); MapType extremeElementMapped = mapFun(extremeElement); // direct access via a random access range is faster static if (isRandomAccessRange!Range) { foreach (const i; 0 .. r.length) { MapType mapElement = mapFun(r[i]); if (selectorFun(mapElement, extremeElementMapped)) { extremeElement = r[i]; extremeElementMapped = mapElement; } } } else { while (!r.empty) { MapType mapElement = mapFun(r.front); if (selectorFun(mapElement, extremeElementMapped)) { extremeElement = r.front; extremeElementMapped = mapElement; } r.popFront(); } } } return extremeElement; } private auto extremum(alias selector = "a < b", Range)(Range r) if (isInputRange!Range && !isInfinite!Range && !is(typeof(unaryFun!selector(ElementType!(Range).init)))) { return extremum!(a => a, selector)(r); } // if we only have one statement in the loop it can be optimized a lot better private auto extremum(alias selector = "a < b", Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seedElement) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void) && !is(typeof(unaryFun!selector(ElementType!(Range).init)))) { return extremum!(a => a, selector)(r, seedElement); } auto minElement(alias map = (a => a), Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { return extremum!map(r); } auto minElement(alias map = (a => a), Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void)) { return extremum!map(r, seed); } auto maxElement(alias map = (a => a), Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { return extremum!(map, "a > b")(r); } auto maxElement(alias map = (a => a), Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void)) { return extremum!(map, "a > b")(r, seed); } } // popcnt with ulongs was added in D 2.071.0 static if (__VERSION__ < 2071) { ulong popcnt(ulong x) { x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L); x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L); x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL); x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL); x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL); x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL); return x; } }
D
import 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() { auto n = readln.chomp.to!int; auto area = new int[][](n+1, n+1); foreach (i; 1..n+1) { auto tmp = readln.chomp.split.map!(to!int).array; foreach (j; 1..n+1) { area[i][j] = tmp[j-1] + area[i-1][j] + area[i][j-1] - area[i-1][j-1]; } } auto res = int.min; foreach (i; 1..n+1) { foreach (j; 1..n+1) { foreach (k; 0..i) { foreach (l; 0..j) { res = max(res, area[i][j] - area[k][j] - area[i][l] + area[k][l]); } } } } res.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; immutable long MOD = 10^^9 + 7; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto K = s[1]; long ans = 0; foreach (i; K..N+2) { long mn = 1L * i * (i - 1) / 2; long mx = 1L * N * (N + 1) / 2 - 1L * (N - i) * (N - i + 1) / 2; ans += (mx - mn + 1) % MOD; ans %= MOD; } ans.writeln; }
D
import core.stdc.stdio; import std.algorithm; void main(){ int n; scanf("%d",&n); int[] a = new int[n]; long sum; foreach(ref v;a){ scanf("%d",&v); sum+=v; } bool Solve(long m){ int g=114514; int c,e,f; long cs,es,fs=sum; while(c<g){ bool ok = true; while(cs<m){ cs+=a[e]; es-=a[e]; e=(e+1)%n; if(e==c) ok=false; } if(!ok) goto next; while(es<m){ es+=a[f]; fs-=a[f]; f=(f+1)%n; if(f==c) ok=false; } if(!ok) goto next; if(fs>=m) return true; next:; g=min(g,e); fs+=a[c]; cs-=a[c]; c++; } return false; } long l=0; long r=sum/3+1; while(r-l>1){ long m=(l+r)/2; if(Solve(m)) l=m; else r=m; } printf("%lld\n",l); }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random; import std.typecons, std.functional, std.traits,std.concurrency; import std.algorithm, std.container; import core.bitop, core.time, core.memory; import std.bitmanip; import std.regex; enum INF = long.max/3; enum MOD = 10L^^9+7; //辞書順順列はiota(1,N),nextPermituionを使う void end(T)(T v) if(isIntegral!T||isSomeString!T||isSomeChar!T) { import core.stdc.stdlib; writeln(v); exit(0); } T[] scanArray(T = long)() { static char[] scanBuf; readln(scanBuf); return scanBuf.split.to!(T[]); } dchar scanChar() { int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } return cast(dchar)c; } T scanElem(T = long)() { import core.stdc.stdlib; static auto scanBuf = appender!(char[])([]); scanBuf.clear; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { scanBuf ~= cast(char) c; c = getchar; } return scanBuf.data.to!T; } dchar[] scanString(){ return scanElem!(dchar[]); } void main() { auto N = scanElem; auto A = scanElem; auto B = scanElem; writeln(min(N*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; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto a = RDR.ARR; string ans; long last; while (!a.empty) { if (a.front > last && a.front < a.back) { last = a.front; a.popFront; ans ~= "L"; } else if (a.back > last && a.back < a.front) { last = a.back; a.popBack; ans ~= "R"; } else if (a.front > last) { last = a.front; a.popFront; ans ~= "L"; } else if (a.back > last) { last = a.back; a.popBack; ans ~= "R"; } else { break; } } writeln(ans.length); writeln(ans); stdout.flush(); debug readln(); }
D
void main(){ int a, b; scanf("%d %d", &a, &b); if(a==1)a=100; if(b==1)b=100; if(a>b)writeln("Alice"); else if(a<b)writeln("Bob"); else writeln("Draw"); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range; const long mod = 10^^9+7; // 1要素のみの入力 T inelm(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] inln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split())ln ~= elm.to!T(); return ln; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n, k; rd(n, k); auto s=readln.chomp.to!(char[]); int[char] freq; foreach(c; s){ if(c in freq) freq[c]++; else freq[c]=1; } int mn=10^^9; for(char c='A'; c<'A'+k; c++){ if(c in freq) mn=min(mn, freq[c]); else mn=min(mn, 0); } writeln(mn*k); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x) e=l[i].to!(typeof(e)); }
D
import std.stdio,std.conv,std.algorithm,std.array; int[] raia(){ return readln().split().map!(to!int).array; } //read as int[] string[] rasa(){ return readln().split(); } //read as string[] void main() { auto it = raia(); auto W = it[0]; auto H = it[1]; auto x = it[2]; auto y = it[3]; auto r = it[4]; if( x-r>= 0 && x+r <= W && y-r>= 0 && y+r <= H ) { writeln("Yes"); } else { writeln("No"); } }
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, x; scan(N, x); auto a = readln.split.to!(int[]); long ans; foreach (i ; 1 .. N) { int d = max(a[i] + a[i - 1] - x, 0); ans += d; int t = min(d, a[i]); a[i] -= t; } writeln(ans); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.stdio; import std.algorithm; import std.math; import std.conv; import std.string; T readNum(T)(){ return readStr.to!T; } T[] readNums(T)(){ return readStr.split.to!(T[]); } string readStr(){ return readln.chomp; } void main(){ auto nm = readNums!int; if(nm[0] == nm[1]){ writeln("Yes"); } else { writeln("No"); } }
D
/+ dub.sdl: name "C" dependency "dunkelheit" version=">=0.9.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; int main() { Scanner sc = new Scanner(stdin); long x, y; sc.read(x, y); int c = 1; while (x*2 <= y) { x *= 2; c++; } writeln(c); return 0; } /* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/scanner.d */ // module dkh.scanner; // import dkh.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int 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); } } } /* 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.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; auto b = new long[](N+1); foreach_reverse (i; 0..N) { b[i] = b[i+1]; b[i].moda(A[i]); } long ans; foreach (i; 0..N) { long t = A[i]; t.modm(b[i+1]); debug writeln(t); ans.moda(t); } writeln(ans); stdout.flush; debug readln; }
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; immutable inf = 10L^^16; alias Edge = Tuple!(int, "to", long, "flow", long, "cap", int, "rev"); void main() { int n, m; scan(n, m); auto adj = new Edge[][](n, 0); void addEdge(int u, int v, long cap) { adj[u] ~= Edge(v, 0, cap, adj[v].length.to!int); adj[v] ~= Edge(u, 0, 0, adj[u].length.to!int - 1); } foreach (i ; 0 .. m) { int u, v, c; scan(u, v, c); addEdge(u, v, c); } long ans = Dinic(adj, 0, n - 1); writeln(ans); } long Dinic(Edge[][] adj, int s, int t) { int n = adj.length.to!int; int[] level = new int[](n); int[] prog = new int[](n); auto q = Queue!(int)(n); bool bfs() { level[] = -1; level[s] = 0; q.clear(); q.push(s); while (!q.empty) { auto u = q.front; q.pop; foreach (e ; adj[u]) { if (level[e.to] == -1 && e.cap - e.flow > 0) { level[e.to] = level[u] + 1; q.push(e.to); } } } return level[t] != -1; } long dfs(int u, long f) { if (u == t) return f; for (; prog[u] < adj[u].length; prog[u]++) { auto e = &adj[u][prog[u]]; if (level[u] + 1 == level[e.to] && e.flow < e.cap) { long d = dfs(e.to, min(f, e.cap - e.flow)); if (d > 0) { e.flow += d; adj[e.to][e.rev].flow -= d; return d; } } } return 0; } long F; while (bfs()) { prog[] = 0; while (true) { auto df = dfs(s, inf); if (df) F += df; else break; } } return F; } 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 %= N; } void clear() { head = tail = 0; } } 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.array, std.conv, std.string; void main() { string[] input = split(readln()); int a = to!int(input[0]), b = to!int(input[1]), c = to!int(input[2]); int ans = 0; for (int i = a; i <= b; ++i) { if (c % i == 0) ans++; } writeln(ans); }
D
/+ dub.sdl: name "C" dependency "dunkelheit" version=">=0.9.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; // import dkh.algorithm; int main() { Scanner sc = new Scanner(stdin); scope(exit) sc.read!true; int n; sc.read(n); int[] a1, a2; sc.read(a1, a2); int calc(int x) { return a1[0..x+1].sum + a2[x..$].sum; } writeln(iota(n).map!(i => calc(i)).maximum); return 0; } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/algorithm.d */ // module dkh.algorithm; import std.traits : isFloatingPoint, isIntegral; T binSearch(alias pred, T)(T l, T r) if (isIntegral!T) { while (r-l > 1) { T md = l + (r-l) / 2; if (!pred(md)) l = md; else r = md; } return r; } T binSearch(alias pred, T)(T l, T r, int cnt = 60) if (isFloatingPoint!T) { foreach (i; 0..cnt) { T md = (l+r)/2; if (!pred(md)) l = md; else r = md; } return r; } import std.range.primitives; E minimum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed) if (isInputRange!Range && !isInfinite!Range) { import std.algorithm, std.functional; return reduce!((a, b) => binaryFun!pred(a, b) ? a : b)(seed, range); } ElementType!Range minimum(alias pred = "a < b", Range)(Range range) { assert(!range.empty, "minimum: range must not empty"); auto e = range.front; range.popFront; return minimum!pred(range, e); } E maximum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed) if (isInputRange!Range && !isInfinite!Range) { import std.algorithm, std.functional; return reduce!((a, b) => binaryFun!pred(a, b) ? b : a)(seed, range); } ElementType!Range maximum(alias pred = "a < b", Range)(Range range) { assert(!range.empty, "maximum: range must not empty"); auto e = range.front; range.popFront; return maximum!pred(range, e); } Rotator!Range rotator(Range)(Range r) { return Rotator!Range(r); } struct Rotator(Range) if (isForwardRange!Range && hasLength!Range) { size_t cnt; Range start, now; this(Range r) { cnt = 0; start = r.save; now = r.save; } this(this) { start = start.save; now = now.save; } @property bool empty() const { return now.empty; } @property auto front() const { assert(!now.empty); import std.range : take, chain; return chain(now, start.take(cnt)); } @property Rotator!Range save() { return this; } void popFront() { cnt++; now.popFront; } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */ // module dkh.scanner; // import dkh.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(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); } } } /* 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.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto S = RD!string; long ans; foreach (i; 0..S.length/2) { if (S[i] != S[$-1-i]) ++ans; } writeln(ans); stdout.flush; debug readln; }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; } enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { long x; scan(x); bool check(long y) { long c = 11 * (y / 2); if (y % 2) c += 6; return c >= x; } long ng = 0, ok = x; while (abs(ok - ng) > 1) { auto md = (ok + ng) / 2; (check(md) ? ok : ng) = md; } auto ans = ok; writeln(ans); }
D
import std.stdio; import std.algorithm; import std.string; import std.conv; void main() { int n = readln.chomp.to!int; for (int i = 0; i < n; i++) { string X = readln.chomp; string Y = readln.chomp; lcs(X, Y).writeln; } } int lcs(string x, string y) { int[][] dp = new int[][](y.length + 1, x.length + 1); for (int i = 1; i <= y.length; i++) { for (int j = 1; j <= x.length; j++) { if (x[j - 1] == y[i - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } return dp[$ - 1][$ - 1]; }
D
void main() { writeln(-readln.chomp.to!int + 2 * readln.chomp.to!int); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; alias Key = Tuple!(int, "cost", int, "mask"); void chmin(T)(ref T a, in T b) { if (a > b) a = b; } long calc(int num, Key[] keys) { int n = cast(int)keys.length; // dp[i 番目の鍵][宝箱の鍵(ビットマスク)] auto dp = new long[][](n + 1, 1 << num); for (int i = 0; i < dp.length; i++) dp[i][] = int.max; dp[0][0] = 0; for (int i = 0; i < n; i++) { int cost = keys[i].cost; int mask = keys[i].mask; for (int j = 0; j < (1 << num); j++) { chmin(dp[i+1][j], dp[i][j]); chmin(dp[i+1][j | mask], dp[i][j] + cost); } } auto ans = dp[n][(1 << num) - 1]; return ans == int.max ? -1 : ans; } void main() { int n, m; scan(n, m); Key[] keys; foreach (_; 0..m) { int a, b; scan(a, b); auto c = readints; int mask = 0; foreach (e; c) mask |= (1 << (e-1)); keys ~= Key(a, mask); } writeln(calc(n, keys)); } void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int;
D
void main() { (rs.startsWith("YAKI") ? "Yes" : "No").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
void main() { auto N = ri; auto S = rs; if(S[0..N/2] == S[N/2..$]) writeln("Yes"); else writeln("No"); } // =================================== 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; import std.string; import std.conv; import std.algorithm; void main() { auto s = readln().split()[0].to!int(); if (s==4 || s==2 || s==1) { writeln("4"); return; } auto i = 1; while(true) { if (s == 1) { writeln(i+1); return; } i++; s = f(s); } } int f(int n) { return n%2 == 0 ? n/2 : 3*n + 1; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.bigint; enum P = 998244353L; long pow(long x, long n) { long y = 1; while (n) { if (n%2 == 1) y = (y * x) % P; x = x^^2 % P; n /= 2; } return y; } void main() { auto N = readln.chomp.to!int; auto DS = readln.split.to!(int[]); if (DS[0] != 0) { writeln(0); return; } auto ds = new long[](N); foreach (d; DS) ++ds[d]; if (ds[0] != 1) { writeln(0); return; } long r = 1; int c; foreach (i, d; ds) { if (i != 0) { r = (r * pow(ds[i-1], d)) % P; } c += d; if (c == N) break; } writeln(r); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; alias Pair = Tuple!(int, "a", int, "b"); void main() { int A, B; scan(A, B); auto ans = max(0, A - 2*B); 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
void main() { int[] tmp = readln.split.to!(int[]); int n = tmp[0], x = tmp[1]; int[] m = new int[n]; foreach (i; 0 .. n) { m[i] = readln.chomp.to!int; } int cnt = n; x -= m.sum; cnt += x / m.reduce!min; cnt.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); auto A = aryread(); long ans = long.max; { auto B = A.dup; long sum = B[0]; long cost; if (B[0] <= 0) { cost += 1 - B[0]; B[0] += (1 - B[0]); sum = B[0]; } foreach (i; 1 .. N) { if (0 < sum) { if (sum + B[i] < 0) { sum += B[i]; continue; } long x = -1 - B[i] - sum; cost += abs(x); B[i] += x; sum += B[i]; } else { if (0 < sum + B[i]) { sum += B[i]; continue; } long x = 1 - B[i] - sum; cost += abs(x); B[i] += x; sum += B[i]; } } // writeln(cost); // writeln(B); ans = ans.min(cost); } { auto B = A.dup; long cost; long sum = B[0]; if (0 <= B[0]) { cost += B[0] + 1; B[0] -= (B[0] + 1); sum = B[0]; } foreach (i; 1 .. N) { if (0 < sum) { if (sum + B[i] < 0) { sum += B[i]; continue; } long x = -1 - B[i] - sum; cost += abs(x); B[i] += x; sum += B[i]; } else { if (0 < sum + B[i]) { sum += B[i]; continue; } long x = 1 - B[i] - sum; cost += abs(x); B[i] += x; sum += B[i]; } } // writeln(cost); // writeln(B); ans = ans.min(cost); } writeln(ans); }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto a = readln.splitter.map !(to !(int)).array; writeln (min (a[0], a[1]) < max (a[2], a[3]) && min (a[2], a[3]) < max (a[0], a[1]) ? "YES" : "NO"); } }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { long A, B, C; scan(A, B, C); long c; foreach_reverse (i; 1 .. 101) { if (A % i == 0 && B % i == 0) { c++; if (C == c) { writeln(i); return; } } } }
D
import std.functional, std.algorithm, std.container, std.typetuple, std.typecons, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv; long f(ulong x) { long t = (x + 1) / 2; if ((x + 1) % 2 == 0) { return t % 2; } else { return (t % 2) ^ x; } } void main() { long[] AB = readln.chomp.split.to!(long[]); long A = AB[0], B = AB[1]; long ans = f(B); if (A > 0) { ans ^= f(A - 1); } writeln(ans); }
D
import std.stdio; void main() { for (int i = 1; i <= 9; i++){ for (int j = 1; j <= 9; j++){ writeln(i, "x", j, "=", i * j); } } }
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 ip = readln.split.to!(int[]); writeln((ip[0] - 1)*(ip[1] - 1)); } //writeln("滲み出す混濁の紋章 不遜なる狂気の器 湧き上がり 否定し 痺れ 瞬き 眠りを妨げる 爬行する鉄の王女 絶えず自壊する泥の人形 結合せよ 反発せよ 地に満ち己の無力を知れ 破道の九十 黒棺")
D
import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; import 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 = readln.split.map!(to!long).array; int oddcount; int x4count; foreach(a; as){ if(a % 4 == 0) x4count += 1; if(a % 2 == 1) oddcount += 1; } string ans; if(oddcount + x4count == n){ if(oddcount - 1 <= x4count) ans = "Yes"; else ans = "No"; } else{ if(oddcount <= x4count) ans = "Yes"; else ans = "No"; } ans.writeln; }
D
import std.stdio; import std.conv; import std.string; int maximizeHapiness(int money) { int hapiness500 = money / 500 * 1000; int hapiness5 = (money%500) / 5 * 5; return hapiness500 + hapiness5; } void main(){ string[] input = split(readln()); auto money = to!int(input[0]); writeln(maximizeHapiness(money)); }
D
void main() { string[] tmp = readln.split; int a = tmp[0].to!int, b = tmp[2].to!int; string op = tmp[1]; writeln(op == "+" ? a + b : a - b); } 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.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto RGBN = RDR.ARR; long ans; foreach (i; 0..3001) { long x = RGBN[0] * i; foreach (j; 0..3001) { auto y = x + RGBN[1] * j; if (y > RGBN[3]) break; auto z = RGBN[3] - y; if (z % RGBN[2] == 0) ++ans; } } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } 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; long ans; foreach (i; 1..N) { auto cnt = (N-1) / i; ans += 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; /*}}}*/ /* libnum {{{*/ void amax(N, Args...)(ref N x, Args y) { x = x.max(y); } void amin(N, Args...)(ref N x, Args y) { x = x.min(y); } auto maxE(R)(R r) if (isInputRange!R) { ElementType!R x = r.front; r.popFront(); foreach (y; r) x.amax(y); return x; } auto minE(R)(R r) if (isInputRange!R) { ElementType!R x = r.front; r.popFront(); foreach (y; r) x.amin(y); return x; } /*}}}*/ /+---test 5 4 2 5 1 3 ---+/ /+---test 4 4 3 2 1 ---+/ /+---test 6 1 2 3 4 5 6 ---+/ /+---test 8 5 7 4 2 6 8 1 3 ---+/ /+---test 1 1 ---+/ void main(string[] args) { const N = readln.chomp.to!long; const P = readln.split.map!(to!long).array; long m = long.max, ans; foreach (p; P) { if (p <= m) ++ans; m.amin(p); } 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.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() { writeln("NO"); stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } double[] rotate(double[] vec, double rad) { return [cos(rad) * vec[0] - sin(rad) * vec[1], sin(rad) * vec[0] + cos(rad) * vec[1]]; } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto l = RD!int; auto r = RD!int; auto c = RDA!int(-1); auto cnt = new int[](n); foreach (i; 0..l) ++cnt[c[i]]; foreach (i; l..n) --cnt[c[i]]; long[] ls, rs; long odd_l, odd_r; long cnt_l, cnt_r; foreach (i; 0..n) { if (cnt[i] > 0) { ls ~= cnt[i]; cnt_l += cnt[i]; if (cnt[i] % 2) ++odd_l; } else if (cnt[i] < 0) { rs ~= -cnt[i]; cnt_r += -cnt[i]; if ((-cnt[i]) % 2) ++odd_r; } } { auto x = min(odd_l, odd_r); odd_l -= x; odd_r -= x; cnt_l -= x; cnt_r -= x; ans[ti] += x; if (odd_l != 0) { auto y = min(odd_l, cnt_r); odd_l -= y; cnt_l -= y; cnt_r -= y; ans[ti] += y; } else if (odd_r != 0) { auto y = min(odd_r, cnt_l); odd_r -= y; cnt_l -= y; cnt_r -= y; ans[ti] += y; } if (odd_l != 0) { cnt_l -= odd_l; ans[ti] += odd_l; } else if (odd_r != 0) { cnt_r -= odd_r; ans[ti] += odd_r; } ans[ti] += (cnt_l + cnt_r) / 2; } } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
// Try Codeforces // author: Leonardone @ NEETSDKASU import std.stdio, std.array, std.conv, std.string, std.algorithm; auto gets() { return readln().chomp(); } auto getV(T)() { return to!T(readln().chomp()); } auto getVals(T)() { return to!(T[])(readln().chomp().split(" ")); } auto isOdd(int p) { return p % 2 != 0; } void main() { int n = getV!int(); auto xs = getVals!int(); auto odds = 0; foreach (x ; xs) { if (isOdd(x)) { odds++; } } if (odds == 0) { writeln("Second"); return; } writeln("First"); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; import std.exception; auto args=readln.split.to!(int[]); auto n=args[0], a=args[1], b=args[2]; int mx=0; for(int ka=1; ka<n; ka++){ auto kb=n-ka; auto xa=a/ka, xb=b/kb; mx=max(mx, min(xa, xb)); } writeln(mx); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } } void wr(T...)(T x){ import std.stdio; foreach(e; x) write(e, " "); writeln(); }
D
//prewritten code: https://github.com/antma/algo import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.traits; enum string vowels = "aeiou"; void main() { int k = readln.strip.to!int; char[] z; z.reserve (k); for (int h = 5; h * h <= k; ++h) { if (k % h == 0) { int w = k / h; foreach (i; 0 .. h) { foreach (j; 0 .. w) { z ~= vowels[(i + j) % 5]; } } writeln (z); return; } } writeln (-1); }
D
/+ dub.sdl: name "C" dependency "dunkelheit" version=">=0.9.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; int main() { Scanner sc = new Scanner(stdin); scope(exit) assert(!sc.hasNext); int n; long[] m; sc.read(n, m); long[] x = m.dup; foreach (i; 0..n-1) { x[i+1] = max(x[i+1], x[i]); } foreach_reverse (i; 0..n-1) { x[i] = max(x[i], x[i+1]-1); } writeln(x.sum - m.sum); return 0; } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */ // module dkh.scanner; // import dkh.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(Args...)(auto ref Args args) { import std.exception; static if (args.length != 0) { enforce(readSingle(args[0])); read(args[1..$]); } } bool hasNext() { return succ(); } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std.stdio, std.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!long; if (N % 2 == 0) { writeln(N / 2); return; } long f = 2; for (; f * f <= N; ++f) { if (N % f == 0) { writeln(1 + (N - f) / 2); return; } } writeln(1); }
D
import std.stdio; void main() { int n; scanf("%d", &n); printf("%lld\n", 1 + 6L * n * (n + 1) / 2); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string; void main() { auto X = readln.chomp.to!long; foreach (a; 1..X+1) { foreach (b; 1..X+1) { if (a % b == 0 && a * b > X && a < b * X) { writeln(a, " ", b); return; } } } writeln(-1); }
D
version (all) { import std.math, std.conv, std.stdio, std.ascii, std.range, std.array, std.regex, std.format, std.bigint, std.traits, std.random, std.string, std.numeric, std.variant, std.typecons, std.container, std.algorithm, std.typetuple, std.exception, core.checkedint; } void main() { readln.strip; auto a = readln.split.map!(to!uint).array; foreach (el; a) { write(1 + a.filter!(c => c > el).array.length, ' '); } 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!long; long s; foreach (i; 1..N+1) { if (i%3 != 0 && i%5 != 0) s += i; } writeln(s); }
D
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } void main() { try { for (; ; ) { const numCases = readInt(); foreach (caseId; 0 .. numCases) { const M = readLong(); const N = readLong(); int ans; if (M == 1 && N == 1) { ans = 0; } else if (M == 1 || N == 1) { ans = 1; } else { ans = 2; } writeln(ans); } } } catch (EOFException e) { } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "a", long, "b"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { auto n = lread(); auto a = aryread(); auto q = lread(); auto l = new long[](MAX); foreach (e; a) l[e]++; long s = a.sum(); foreach (_; iota(q)) { long b, c; scan(b, c); s -= b * l[b]; s += c * l[b]; l[c] += l[b]; l[b] = 0; s.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.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range, core.bitop; void main() { auto PP = new long[](10^^5*2+1); foreach (i; 1..10^^5*2+1) { PP[i] = PP[i % popcnt(i)] + 1; } auto N = readln.chomp.to!int; auto X = readln.chomp; long bc; foreach (b; X) if (b == '1') ++bc; auto cs = new long[][](N, 2); auto xs = new long[][](N, 2); long x1 = 1, x2 = 1; foreach_reverse (i, b; X) { if (i != N-1) { cs[i][0] = cs[i+1][0]; cs[i][1] = cs[i+1][1]; } if (b == '1') { if (bc > 1) (cs[i][0] += x1) %= (bc-1); (cs[i][1] += x2) %= (bc+1); } xs[i][0] = x1; xs[i][1] = x2; if (bc > 1) (x1 *= 2) %= (bc-1); (x2 *= 2) %= (bc+1); } x1 = 0; x2 = 0; foreach (i, b; X) { if (b == '1') { if (bc == 1) { writeln(0); } else { auto n = x1 + (i == N-1 ? 0 : cs[i+1][0]); writeln(PP[n % (bc-1)] + 1); } if (bc > 1) (x1 += xs[i][0]) %= (bc-1); (x2 += xs[i][1]) %= (bc+1); } else { auto n = x2 + xs[i][1] + (i == N-1 ? 0 : cs[i+1][1]); writeln(PP[n % (bc+1)] + 1); } } }
D
module app; import std.stdio; import std.algorithm; import std.range; import std.array; import std.string; import std.conv; void main(string[] args) { auto five_hund = readln.chop.to!int; auto one_hund = readln.chop.to!int; auto fifty = readln.chop.to!int; immutable auto target_yen = readln.chop.to!int; int answer; foreach(fh; iota(0, five_hund+1)) { foreach(oh; iota(0, one_hund+1)) { foreach(f; iota(0, fifty+1)) { if(fh*500 + oh*100 + f*50 == target_yen){ answer++; } } } } writeln(answer); }
D
import std.stdio, std.string, std.conv; import std.typecons; import std.algorithm, std.array, std.range, std.container; import std.math; void main() { auto data = readln.split, N = data[0].to!long, M = data[1].to!long; auto A = readln.split.to!(long[]); long sum = 0; long[long] hash; hash[0] = 1; foreach (i; 0 .. N) { sum += A[i] % M; if (sum >= M) sum -= M; if (sum !in hash) hash[sum] = 1; else hash[sum]++; } ulong ans; foreach (n; hash.byValue) { ans += n*(n-1)/2; } writeln(ans); }
D
unittest { assert( [ "A B" ].parse.expand.solve == "<" ); assert( [ "E C" ].parse.expand.solve == ">" ); assert( [ "F F" ].parse.expand.solve == "=" ); } 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 xy = input.front.split.to!( string[] ); return tuple( xy[ 0 ], xy[ 1 ] ); } auto solve( string x, string y ) { if( x < y ) return "<"; if( y < x ) return ">"; return "="; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto hw = readln.split.to!(int[]); auto h = hw[0]; auto w = hw[1]; auto ab = readln.split.to!(int[]); auto a = ab[0]; auto b = ab[1]; writeln(h * w - (h / a) * (w / b) * a * b); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.numeric; void main() { auto n = readln.chomp.to!int; auto x = readln.chomp.split.to!(int[]); auto sum = long.max; foreach (i; 0..100) { long lsum; foreach (e; x) { lsum += (i - e) ^^ 2; } sum = min(sum, lsum); } sum.writeln; }
D
import std.algorithm; import std.range; import std.ascii; import std.array; import std.container; import std.conv; import std.numeric; import std.stdio; import std.string; import std.typecons; void log(A...)(A arg) { stderr.writeln(arg); } int size(T)(in T s) { return cast(int)s.length; } long modPow(long mod)(long x, long n) { if (n == 0) return 1; if (n % 2 == 0) return modPow!mod(x * x % mod, n / 2); return x * modPow!mod(x % mod, n - 1) % mod; } long extgcd(long a, long b, ref long x, ref long y) { long g = a; x = 1; y = 0; if (b != 0) { g = extgcd(b, a % b, y, x); y -= (a / b) * x; } return g; } long inverse(long mod)(long v) { long x, y; if (extgcd(v, mod, x, y) == 1) return (x + mod) % mod; return 0; } class RollingHash(long[] Ps, long mod) { string s; int N; long[][] H; this(in string s) { this.s = s; this.N = s.size; this.H = new long[][](Ps.length, N + 1); foreach (k, P; Ps) { long x = 1; foreach (int i, c; s) { H[k][i + 1] = (H[k][i] + x * cast(int)(c - 'a' + 1)) % mod; x = (x * P) % mod; } } } long[] hash(int l, int r) { // [l, r) auto hs = iota(0, Ps.length, 1).map!(delegate(k) { long h = (H[k][r] - H[k][l] + mod) % mod; return h * modPow!mod(inverse!mod(Ps[k]), l) % mod; }); return hs.array; } } void main() { int N, M; scanf("%d %d\n", &N, &M); auto s = readln.chomp; auto hasher = new RollingHash!([991, 9999991], cast(long)(1e9+7))(s); int l = 0, r = 1; bool[long[]] appeared; foreach (_; 0 .. M) { auto q = readln.chomp; switch (q) { case "L++": l++; break; case "L--": l--; break; case "R++": r++; break; case "R--": r--; break; default: assert(false); } appeared[hasher.hash(l, r).idup] = true; } writeln(appeared.keys.size); }
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 = 998244353; //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 main() { auto N = RD; auto R = RD; writeln(N >= 10 ? R : R + 100 * (10 - N)); stdout.flush; debug readln; }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { long N, A, B; scan(N, A, B); auto d = abs(A - B); if (d % 2 == 0) { writeln(d / 2); return; } auto ans = min(A - 1, N - B) + (d + 1) / 2; writeln(ans); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
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 { long h; } void parseInput(T)(out Input input, T file) { with (file) with (input) { h = readln().strip().to!long; } } struct Output { } auto main2(Input* input) { return beat(input.h); } long beat(long n) { if (n == 1) return 1; else return 1 + beat(n / 2) * 2; } unittest { writeln("begin unittest"); } unittest // example1 { string example = `2`; Input input = void; parseExample(input, example); auto result = main2(&input); writeln(result); assert(result == 3); } unittest // example2 { string example = `4`; Input input = void; parseExample(input, example); auto result = main2(&input); writeln(result); assert(result == 7); } unittest // example3 { string example = `1000000000000`; Input input = void; parseExample(input, example); auto result = main2(&input); writeln(result); assert(result == 1099511627775); } unittest { writeln("end unittest"); } 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; import std.string; import std.conv; import std.algorithm; import std.array; void main() { auto data = readln().split(); auto A = data[0].to!int(), B = data[1].to!int(), C = data[2].to!int(); writeln(min(B/A, C)); }
D
import std.stdio, std.string, std.conv; import std.typecons; import std.algorithm, std.array, std.range, std.container; import std.math; void main() { auto data = readln.split.to!(long[]), N = data[0], M = data[1]; auto p = readln.split.to!(long[]).map!(x=>x-1); long[] list = N.iota().array; //long[] size = repeat(1L, N).array; ulong root(ulong num) { auto parent = list[num]; return parent == num ? num : ( list[num] = root(list[num]) ) ; } void unite(ulong num1, ulong num2) { auto root1 = root(num1), root2 = root(num2); if (root1 != root2) { //auto result = size[root1] * size[root2]; list[root1] = root2; //size[root2] += size[root1]; } } bool same(ulong num1, ulong num2) { return root(num1) == root(num2); } foreach (i; 0 .. M) { data = readln.split.to!(long[]), unite(data[0]-1, data[1]-1); } long ans = 0; N.iota.each!(i => same(i, p[i])? ans++ : 0); writeln(ans); }
D
import std.algorithm; import std.array; import std.ascii; import std.container; import std.conv; import std.format; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; void main() { int n = readln.chomp.to!int; long min = 1; long sum = readln.chomp.to!long - 1; foreach (i; 1..n) { long a = readln.chomp.to!long; sum += (a - 1) / (min + 1); if (min + 1 == a) { min++; } } sum.writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.typecons; import std.numeric, std.math; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto K = RD; auto cnt = new long[](10^^5+1); foreach (i; 0..N) { auto a = RD; auto b = RD; cnt[a] += b; } long pos, ans; foreach (i, e; cnt) { pos += e; if (pos >= K) { ans = i; break; } } writeln(ans); stdout.flush(); }
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 A = RD; auto B = RD; writeln(min(A*N, B)); 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() { int n, m; readV(n, m); auto g = Graph!int(n); foreach (_; 0..m) { int x, y; readV(x, y); --x; --y; g.addEdge(x, y); } auto t = g.topologicalSort; auto dp = new int[](n); foreach (u; t) foreach (v; g[u]) dp[v] = max(dp[v], dp[u]+1); writeln(dp.reduce!max); } struct Graph(N = int) { alias Node = N; Node n; Node[][] g; alias g this; this(Node n) { this.n = n; g = new Node[][](n); } void addEdge(Node u, Node v) { g[u] ~= v; } void addEdgeB(Node u, Node v) { g[u] ~= v; g[v] ~= u; } } auto topologicalSort(Graph)(ref Graph g) { import std.container; alias Node = g.Node; auto n = cast(Node)(g.length), h = new int[](n); foreach (u; 0..n) foreach (v; g[u]) ++h[v]; auto st = SList!Node(); foreach (i; 0..n) if (h[i] == 0) st.insertFront(i); Node[] ans; while (!st.empty()) { auto u = st.front; st.removeFront(); ans ~= u; foreach (v; g[u]) { --h[v]; if (h[v] == 0) st.insertFront(v); } } return ans; }
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 n, m; cin.scan(n, m); writeln(n * (n - 1) / 2 + m * (m - 1) / 2); }
D
import std.stdio; import std.string; import std.algorithm; void main() { int a, b; foreach(c; readln().chomp()) { if (c == '0') ++a; if (c == '1') ++b; } write(min(a, b) * 2); }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(int[] a) { int n = cast(int)a.length; auto indices = new int[n]; for (int i = 0; i < n; i++) { indices[a[i]-1] = i; } int maxlen = 1; int len = 1; for (int i = 1; i < n; i++) { if (indices[i-1] < indices[i]) { len++; maxlen = max(maxlen, len); } else len = 1; } return n - maxlen; } void main() { int n = readint; int[] a; foreach (_; 0..n) a ~= readint; writeln(calc(a)); }
D
import std; void main(){ auto S = readln().chomp(); if (S == "ABC") writeln("ARC"); else if (S == "ARC") writeln("ABC"); else 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; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!int).array; bool[int] S; for (int i = 0; i * i <= 10^^6; ++i) { S[i*i] = true; } int ans = -(10^^6)-1; foreach (a; A) if (!(a in S)) ans = max(ans, a); ans.writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; alias MP = Tuple!(int, "i", int, "p"); int[11] PS, CS; void main() { auto dg = readln.split.to!(int[]); auto D = dg[0]; auto G = dg[1] / 100; foreach (i; 1..D+1) { auto pc = readln.split.to!(int[]); PS[i] = pc[0]; CS[i] = pc[1] / 100; } int solve(int i, int c, uint m) { if (i == D+1) { foreach_reverse (j; 1..D+1) { if (m & (1<<j)) continue; if (c >= PS[j]) continue; return j * c; } return 0; } auto r1 = solve(i + 1, c, m); if (c >= PS[i]) { auto r2 = PS[i] * i + CS[i] + solve(i + 1, c - PS[i], m | (1<<i)); return max(r1, r2); } else { return r1; } } foreach (i; 1..1001) { if (solve(1, i, 0) >= G) { writeln(i); return; } } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto d=readln.chomp.to!int; write("Christmas"); foreach(i; 0..25-d) { write(" Eve"); } writeln(""); }
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } /* ---------------------------------------- i番目から、j番目の前までを1つにするのにかかるコストの最小値をx[i, j] x[i, j] = (sum[j] - sum[i]) + min(k; i .. j) (x[i, k] + x[k, j]) ただし x[i, i] = 0 求めるものは x[0, n] ---------------------------------------- 上記だとメモ化再帰になってしまいやりづらい 長さがiで、左端がjである区間を1つにするのにかかるコストの最小値をx[i, j] ※先程のiは今のj、先程のjは今のj + i x[i, j] = (sum[i + j] - sum[j]) + min(k; 1 .. i) (x[k, j] + x[i - k, j + k]) ただし x[1, j] = 0 sum[i] はiの前までの和 求めるものは x[n, 0] */ const long infty = 400 * 400 * 1_000_000_000 * 10; void main(){ int n = read.to!int; long[] as = readln.chomp.split.map!(to!long).array; long[] sums = [0]; foreach(a; as) sums ~= sums[$ - 1] + a; long[][] xs = [[]]; foreach(i; 1 .. n + 1){ xs ~= [[]]; foreach(j; 0 .. n + 1 - i){ if(i == 1) xs[i] ~= 0; else{ long m = long.max; foreach(k; 1 .. i){ m = min(m, xs[k][j] + xs[i - k][j + k]); } debug writeln("i:", i, " j:", j, " m:", m); xs[i] ~= sums[i + j] - sums[j] + m; } debug writeln("i:", i, " xs[i]:", xs[i]); } } xs[n][0].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 inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; alias Edge = Tuple!(int, "to", int, "w"); void main() { int n; scan(n); auto adj = new Edge[][](n, 0); foreach (i ; 0 .. n - 1) { int u, v, w; scan(u, v, w); u--, v--; w %= 2; adj[u] ~= Edge(v, w); adj[v] ~= Edge(u, w); } auto ans = new int[](n); ans[] = -1; debug { writeln(adj); } void dfs(int v, int par, int col) { ans[v] = col; foreach (e ; adj[v]) if (e.to != par) { if (e.w == 0) { dfs(e.to, v, col); } else { dfs(e.to, v, col ^ 1); } } } dfs(0, -1, 0); foreach (i ; 0 .. n) { writeln(ans[i]); } } int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new int[][](n, 0); foreach (i; 0 .. m) { int u, v; scan(u, v); if (is1indexed) { u--, v--; } adj[u] ~= v; if (isUndirected) { adj[v] ~= u; } } return adj; } //alias Edge = Tuple!(int, "to", int, "cost"); Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new Edge[][](n, 0); foreach (i; 0 .. m) { int u, v, c; scan(u, v, c); if (is1indexed) { u--, v--; } adj[u] ~= Edge(v, c); if (isUndirected) { adj[v] ~= Edge(u, c); } } return adj; } void yes(bool b) { writeln(b ? "Yes" : "No"); } void YES(bool b) { writeln(b ? "YES" : "NO"); } T[] readArr(T)() { return readln.split.to!(T[]); } T[] readArrByLines(T)(int n) { return iota(n).map!(i => readln.chomp.to!T).array; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio; import std.algorithm; import std.conv; import std.array; import std.string; import std.math; import std.functional; import std.range; void main(string[] args) { readln; readln.solve.writeln; } auto solve(string input) { return input.split.map!(to!int).map!(a => divCount(a, 2)).minElementAlt; // return input.split.map!(to!int).map!(a => divCount(a, 2)).minElement; } unittest { assert(solve("8 12 40") == 2, "1"); assert(solve("5 6 8 10") == 0, "2"); assert(solve("382253568 723152896 37802240 379425024 404894720 471526144") == 8, "3"); } // ---------------------------------------- pure auto minElementAlt(Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { import std.algorithm: min; auto res = r.front; foreach(v; r) { res = min(v, res); } return res; } unittest { assert([4, 6, 3, 0, 1].minElementAlt == 0); } pure auto divCount(int n, int base) { auto count = 0; while (n % base == 0) { n /= base; count++; } return count; } unittest { assert(divCount(12, 2) == 2); assert(divCount(125, 2) == 0); assert(divCount(81, 3) == 4); }
D