code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T read(T)(){ return read.to!T; } T[] read(T)(long n){ return n.iota.map!(_ => read!T).array; } T[][] read(T)(long m, long n){ return m.iota.map!(_ => read!T(n)).array; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ int n = read.to!int; int m = read.to!int; long[] xs; foreach(i; 0 .. n) xs ~= read.to!long; int[long] xcnt; foreach(x; xs){ if(x !in xcnt) xcnt[x] = 0; xcnt[x] += 1; } int[] as, bs; // a: 同じ数のペアの個数 b: 全個数 as = new int[](m); bs = new int[](m); long[] ks = xcnt.keys; foreach(x; ks){ int k = x % m; as[k] += xcnt[x] / 2; bs[k] += xcnt[x]; } long ans; foreach(k; 0 .. m){ if(m - k < k) continue; if(k == 0) ans += bs[k] / 2; else if(k + k == m) ans += bs[k] / 2; else{ if(bs[k] >= bs[m - k]){ ans += bs[m - k]; ans += min((bs[k] - bs[m - k]) / 2, as[k]); } else{ ans += bs[k]; ans += min((bs[m - k] - bs[k]) / 2, as[m - k]); } } } ans.writeln; }
D
void main() { problem(); } void problem() { const A = scan!long; const B = scan!long; const N = scan!long; long calc(long x) { return (A*x / B) - A*(x/B); } long solve() { if (B > N) { return calc(N); } const k = N / B; return calc(B * k - 1); } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); import std.bigint, std.functional; // -----------------------------------------------
D
import std.functional, std.algorithm, std.container, std.typetuple, std.typecons, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv; void main() { string s = readln.chomp; int K = readln.chomp.to!int; int[string] h; foreach (i; 0..s.length+1) { foreach (j; i+1..s.length+1) { string sub = s[i..j]; if (sub.length <= K) { h[sub] = 0; } } } string[] subs = h.keys; subs.sort!"a<b"; writeln(subs[K - 1]); }
D
import std.algorithm; import std.range; import std.stdio; import std.typecons; import std.traits; import std.array; struct Input { T next(T)() { import std.conv; return to!T(nextWord); } string nextWord() { if (_nextWords.empty) _nextWords.insertFront(readln().split); string word = _nextWords.front; _nextWords.removeFront; return word; } import std.container; DList!string _nextWords; } Input input; void read(T...)(ref T args) { import std.traits; static foreach(i; 0 .. T.length) static if(isArray!(T[i]) && !is(T[i] == string)) foreach(ref e; args[i]) read(e); else static if(isTuple!(T[i])) static foreach(n; 0 .. T[i].Types.length) read(args[i][n]); else args[i] = input.next!(typeof(args[i]))(); } auto itup(alias k, alias F)() { alias T = Parameters!F; foreach(i; 0 .. k) { T t; get(t); F(t); } } alias get = read; void wone(T)(T t, char end) { static if(isArray!T && !is(T == string)) { foreach(i; 0 .. t.length - 1) write(t[i], ' '); write(t[$ - 1], end); } else write(t, end); } void wr(T...)(T t) { static foreach(i; 0 .. T.length) static if(i + 1 < T.length) wone(t[i], ' '); else wone(t[i], '\n'); } void ans(T...)(T t) { import core.stdc.stdlib; wr(t); exit(0); } void main() { int n; get(n); auto a = new int[n]; get(a); int[2][] ps; int i = 0; int res = 0; for(; i < n;) { int si = i, fi = i; while(fi + 1 < n && a[fi + 1] > a[fi]) { fi++; } ps ~= [si, fi]; res = max(res, fi - si + 1); i = fi + 1; } for(int j = 1; j < ps.length; j++) { auto curr = ps[j]; auto prev = ps[j - 1]; int comb = 0; if (prev[0] < prev[1]) { if(a[prev[1] - 1] < a[curr[0]]) { comb = max(comb, curr[1] - curr[0] + 1 + prev[1] - prev[0]); } } if (curr[0] < curr[1]) { if(a[prev[1]] < a[curr[0] + 1]) { comb = max(comb, curr[1] - curr[0] + prev[1] - prev[0] + 1); } } res = max(res, comb); } ans(res); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; import std.math; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}} void main() { int h, w, d; readV(h, w, d); auto a = new int[][](h, w); foreach (i; 0..h) readA(w, a[i]); auto hw = h*w, x = new int[](hw), y = new int[](hw); foreach (i; 0..h) foreach (j; 0..w) { x[a[i][j]] = i; y[a[i][j]] = j; } auto m = new int[](h*w+1); foreach (i; d+1..hw+1) m[i] = m[i-d] + (x[i]-x[i-d]).abs + (y[i]-y[i-d]).abs; int q; readV(q); foreach (_; 0..q) { int l, r; readV(l, r); writeln(m[r]-m[l]); } }
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, "before", long, "after"); alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less); void main() { long n, k; scan(n, k); auto a = aryread(); auto cnt = new Pair[](n); foreach (i; iota(n)) { foreach (j; iota(n)) { if (a[i] > a[j]) { if (i > j) cnt[i].before++; else cnt[i].after++; } } } long init, addition; foreach (e; cnt) { init += e.after; addition += e.before + e.after; } auto ans = k * init % MOD; ans += ((k * (k - 1)) / 2) % MOD * addition; writeln(ans % MOD); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto a = readln.split.to!(int[]); auto g = new int[][](n); foreach (_; 1 .. n) { int u, v; rd(u, v); g[u - 1] ~= v - 1; g[v - 1] ~= u - 1; } auto num_r = new int[](n), num_b = new int[](n); void f(int i, int p) { if (a[i] == 1) { num_r[i] = 1; } else if (a[i] == 2) { num_b[i] = 1; } foreach (j; g[i]) { if (j != p) { f(j, i); num_r[i] += num_r[j]; num_b[i] += num_b[j]; } } } f(0, -1); int bad = 0; void f2(int i, int p) { foreach (j; g[i]) { if (j != p) { if (num_r[j] > 0 && num_b[j]) { bad++; } else if (num_r[0] - num_r[j] > 0 && num_b[0] - num_b[j] > 0) { bad++; } f2(j, i); } } } f2(0, -1); writeln(n - 1 - bad); } 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 core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "x", long, "y"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long h, n; scan(h, n); auto a = aryread(); writeln(a.sum() >= h ? "Yes" : "No"); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std; alias P = Tuple!(long, "x", long, "y"); const MOD = 10^^9 + 7; long calc(P[] ps, int zeros) { long[P] m; foreach (p; ps) m[p]++; bool[P] used; long[] group; foreach (p; ps) { if (used.get(p, false)) continue; // p に属するグループ数 auto a1 = p; auto a2 = P(-p.x, -p.y); long acnt = m.get(a1, 0) + m.get(a2, 0); // p と垂直な位置関係にある点のグループ数 auto b1 = P(-p.y, p.x); auto b2 = P(p.y, -p.x); long bcnt = m.get(b1, 0) + m.get(b2, 0); long cnt = 0; // s のグループからのみ選ぶ組み合わせ数 cnt += modpow(2, acnt) - 1; // -1 は何も選ばない分を引いている // t のグループからのみ選ぶ組み合わせ数 cnt += modpow(2, bcnt) - 1; // s からも t からも選ばない cnt += 1; cnt %= MOD; group ~= cnt; used[a1] = used[a2] = used[b1] = used[b2] = true; } long ans = 0; if (group.length > 0) { ans += group.fold!((a, b) => a * b % MOD)(1L); ans -= 1; // 全てのグループにおいて何も選ばなかった分の 1 を引く } ans += zeros; ans += MOD; return ans % MOD; } void main() { int n; scan(n); P[] ps; int zeros = 0; foreach (_; 0..n) { long a, b; scan(a, b); if (a == 0 && b == 0) zeros++; else { long g = gcd(abs(a), abs(b)); ps ~= P(a/g, b/g); } } writeln(calc(ps, zeros)); } 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) { if (k == 0) return 1; if (k % 2 == 0) return modpow(x * x % MOD, k / 2); return x * modpow(x, k - 1) % MOD; }
D
void main() { int n = readln.chomp.to!int; int[] tmp = readln.split.to!(int[]); int d = tmp[0], x = tmp[1]; foreach (i; 0 .. n) { int a = readln.chomp.to!int; int day = 1; while (day <= d) { ++x; day += a; } } x.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { readln; writeln(sort(readln.split).group.walkLength == 3 ? "Three" : "Four"); }
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 X = readln.split[0].to!long; long money = 100; foreach (i; 1 .. 4000) { money = cast(long) floor(money * 1.01); if (money >= X) { writeln(i); break; } } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } void main() { string s; while ((s = readln.chomp) != null) { auto ab = s.split.map!(to!int).array; int a = ab[0], b = ab[1]; writeln(gcd(a, b)); } }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { string s; scan(s); writeln(s.count!"a == '1'"); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { writeln("ABC" ~ readln.chomp); } 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.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; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto M = RD; auto a = new long[](N+1); foreach (i; 0..M) { auto L = RD-1; auto R = RD; ++a[L]; --a[R]; } foreach (i; 0..N) { a[i+1] += a[i]; } long ans; foreach (i; 0..N) { if (a[i] == M) { ++ans; } } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio; import std.algorithm; import std.math; import std.string; import std.conv; import std.range; void main() { while (true) { int sum = readln.chomp.to!int; if (sum == 0) break; foreach (i; 0..9) { sum -= readln.chomp.to!int; } sum.writeln; } }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto rd1 = readln.split.to!(size_t[]), n = rd1[0], m = rd1[1]; auto r = new int[](n); foreach (_; 0..m) { auto rd2 = readln.split.to!(size_t[]), a = rd2[0]-1, b = rd2[1]-1; ++r[a]; ++r[b]; } foreach (ri; r) writeln(ri); }
D
module app; import core.bitop; import std.algorithm; import std.array; import std.bigint; import std.conv; import std.stdio; import std.string; import std.traits; struct Input { int n; int[] a; } void parseInput(T)(out Input input, T file) { with (file) with (input) { n = readln().strip().to!int; a = readln().strip().split().map!(to!int).array(); } } auto main2(Input* input) { with (input) { foreach (ai; a) { if ((ai & 1) == 0) if (ai % 3 != 0 && ai % 5 != 0) return "DENIED"; } return "APPROVED"; } } alias retType = ReturnType!main2; static if (!is(retType == void)) { unittest { writeln("begin unittest"); } auto _placeholder_ = ReturnType!main2.init; unittest // example1 { string example = `5 6 7 9 10 31`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == "APPROVED"); } unittest // example2 { string example = `3 28 27 24`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == "DENIED"); } unittest // example3 { string example = ``; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == _placeholder_); } 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 printResult(T)(T result) { static if (isFloatingPoint!T) writefln("%f", result); else writeln(result); } void main() { Input input = void; parseInput(input, stdin); alias retType = ReturnType!main2; static if (is(retType == void)) main2(&input); else { auto result = main2(&input); printResult(result); } }
D
import std.stdio, std.algorithm, std.string; int main(string[] argv) { string[] s = readln().chomp().split(); s.sort; writeln(join(s, " ")); return 0; }
D
import std.stdio, std.string, std.conv, std.range, std.array, std.algorithm; import std.uni, std.math, std.container, std.typecons, std.typetuple; import core.bitop, std.datetime; immutable long mod = 10^^9 + 7; immutable int inf = mod; void main(){ auto s = readln.split; auto stack = new SList!int; int x, y, res; foreach(si ; s){ if(si == "+" || si == "-" || si == "*"){ x = stack.front; stack.removeFront; y = stack.front; stack.removeFront; if (si == "+") { res = x + y; } else if (si == "-") { res = y - x; } else{ res = x * y; } stack.insert(res); } else { stack.insert(si.to!int); } } int ans = stack.front; writeln(ans); } void readVars(T...)(auto ref T args){ auto line = readln.split; foreach(ref arg ; args){ arg = line.front.to!(typeof(arg)); line.popFront; } if(!line.empty){ throw new Exception("args num < input num"); } }
D
import std.stdio; import std.algorithm; import std.string; import std.functional; import std.conv; void main(){ readln.split.map!(to!int).unaryFun!(a => a[0] < a[1] ? "a < b" : a[0] > a[1] ? "a > b" : "a == b").writeln; }
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; if(n>=1200)end("ARC");end("ABC"); }
D
import std.stdio, std.string, std.conv, std.algorithm, std.ascii; void main() { foreach(_; 0 .. readln.chomp.to!int) { auto line = readln; auto keys = line.split.filter!(str => str.length == 4); loop:foreach(a; [0, 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25]) { foreach(b; 0 .. 26) { foreach(key; keys) { string decoded = key.decode(a, b); if(decoded == "this" || decoded == "that") { line.decode(a, b).write; break loop; } } } } } } string decode(string encoded, int a, int b) { string decoded; foreach(c; encoded) { if(c.isAlpha) { decoded ~= (a * (c - 'a') + b) % 26 + 'a'; } else { decoded ~= c; } } return decoded; }
D
import std.stdio; import std.conv; import std.string; void main() { int[] input = readln.split.to!(int[]); int sx = input[0]; int sy = input[1]; int tx = input[2]; int ty = input[3]; char[] ans; foreach (i; 0..(ty - sy)) ans ~= 'U'; foreach (i; 0..(tx - sx)) ans ~= 'R'; foreach (i; 0..(ty - sy)) ans ~= 'D'; foreach (i; 0..(tx - sx)) ans ~= 'L'; ans ~= 'L'; foreach (i; 0..(ty - sy + 1)) ans ~= 'U'; foreach (i; 0..(tx - sx + 1)) ans ~= 'R'; ans ~= 'D'; ans ~= 'R'; foreach (i; 0..(ty - sy + 1)) ans ~= 'D'; foreach (i; 0..(tx - sx + 1)) ans ~= 'L'; ans ~= 'U'; 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() { auto N = RD; auto K = RD; auto ans = N % K; ans = min(ans, K - ans); writeln(ans); stdout.flush; debug readln; }
D
module app; import core.bitop; import std.algorithm; import std.array; import std.bigint; import std.conv; import std.stdio; import std.string; void main() { Input input = void; parseInput(input, stdin); main2(&input); // writeln(result); } auto main2(Input* input) { uint[100000] seikaisuu; foreach (ai; input.a) { seikaisuu[ai-1]++; } for (int i = 0; i < input.n; i++) { if (input.q - seikaisuu[i] >= input.k) { writeln("No"); } else { writeln("Yes"); } } } struct Input { uint n,k,q; uint[] a; } void parseInput(T)(out Input input, T file) { with (file) with (input) { auto nkq = readln().strip().split().array().map!(a=>a.to!uint); n = nkq[0]; k = nkq[1]; q = nkq[2]; a.length = q; foreach (i; 0..q) { a[i] = readln().strip().to!uint; } } } void parseExample(out Input input, string example) { struct Adapter { import std.string; string[] _lines; this(string input) { _lines = input.splitLines(); } string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; } } Adapter a = Adapter(example); parseInput(input, a); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; long calc(int[] xs) { long sum = 0; long[long] d = [0: 1]; foreach (x; xs) { sum += x; d[sum]++; } long ans = 0; foreach (k, v; d) { ans += (v * (v - 1)) / 2; } return ans; } void main() { readint; auto xs = readints; writeln(calc(xs)); }
D
import std.stdio; import std.range; import std.conv; import std.algorithm; import std.string; import std.array; import std.conv; void main() { readln(); writeln(readln().strip.split(" ").reverse.join(" ")); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; void main(){ uint[] arr=new uint[10]; for(uint i;i<10;++i){ arr[i]=readln().chomp().to!int(); } arr.sort().reverse(); for(uint i;i<3;++i){ writeln(arr[i]); } }
D
import std.stdio; import std.conv; import std.string; int main() { string[] str = chomp(readln).split(" "); int a = to!(int)(str[0]); int b = to!(int)(str[1]); writeln(a*b, " ", a*2+b*2); return 0; }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; import std.string; import std.math; void main() { int n = readln.chomp.to!int; int[] arr = readln.chomp.split.map!(to!int).array; long ans = 1000000000000007; for (int i = 0; i < 2; ++i) { auto a = arr.dup; long tmp; long[] sum_a = new long[](n); // 偶数番目が正の数 if (i == 0) { for (int j = 0; j < n; ++j) { sum_a[j] = a[j] + (j == 0 ? 0 : sum_a[j - 1]); if (j % 2 == 0 && sum_a[j] <= 0) { long dif = 1 - sum_a[j]; a[j] += dif; sum_a[j] += dif; tmp += dif.abs; } else if (j % 2 == 1 && sum_a[j] >= 0) { long dif = -1 - sum_a[j]; a[j] += dif; sum_a[j] += dif; tmp += dif.abs; } } } else { for (int j = 0; j < n; ++j) { sum_a[j] = a[j] + (j == 0 ? 0 : sum_a[j - 1]); if (j % 2 == 1 && sum_a[j] <= 0) { long dif = 1 - sum_a[j]; a[j] += dif; sum_a[j] += dif; tmp += dif.abs; } else if (j % 2 == 0 && sum_a[j] >= 0) { long dif = -1 - sum_a[j]; a[j] += dif; sum_a[j] += dif; tmp += dif.abs; } } } ans = ans.min(tmp); } ans.writeln; return; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto S = readln.chomp.to!(char[]); int r; foreach (i; 1..N-1) { int[char] CNT; foreach (c; S[0..i]) { CNT[c] = 1; } foreach (c; S[i..N]) { if (c in CNT) ++CNT[c]; } int rr; foreach (_, v; CNT) if (v > 1) ++rr; r = max(r, rr); } writeln(r); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); writeln((N + 1) / 2 - 1); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!long).array; long[] B = new long[](N); long cnt = 0; while (A.reduce!max >= N) { long T = 0; fill(B, 0); foreach (i; 0..N) { if (A[i] >= N) { B[i] = - A[i] / N; T += A[i] / N; A[i] %= N; } } foreach (i; 0..N) { A[i] += T + B[i]; } cnt += T; } cnt.writeln; }
D
import std.stdio, std.conv, std.string; import std.algorithm, std.array, std.container; import std.numeric, std.math; import core.bitop; T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T)(in string str) { return str.split.to!(T[]); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } long mod = pow(10, 9) + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } struct UnionFind { void init(long n) { par = new long[](n); foreach (i; 0..n) par[i] = i; } long root(long i) { return par[i] == i ? i : (par[i] = root(par[i])); } bool same(long i, long j) { return root(i) == root(j); } void unite(long i, long j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; } long[] par; } void main() { auto K = RD!long; auto A = RD!long; auto B = RD!long; auto c = max(B - A, 2); auto d = min(K, A - 1); long ans = d + 1; K -= d; ans += c * (K / 2); ans += K % 2; writeln(ans); stdout.flush(); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { string s; scan(s); int N = s.length.to!int; foreach (l ; 0 .. N) { foreach (r ; l .. N + 1) { auto t = s[0 .. l] ~ s[r .. N]; if (t == "keyence") { writeln("YES"); return; } } } writeln("NO"); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!long; long n = long.max; foreach (_; 0..5) n = min(n, readln.chomp.to!long); writeln((N + n - 1) / n + 4); }
D
import std.algorithm, std.string, std.array, std.stdio, std.range, std.conv; long abs(long x) { return x * (x < 0 ? -1 : 1); } void main() { long N = readln.chomp.to!long; long[] a = readln.chomp.split.to!(long[]); long ans = long.max; long[] dp1, dp2; dp1.length = N; dp2.length = N; for (size_t i = 0; i < N - 1; i++) { if (i == 0) { dp1[i] = a[i]; dp2[i] = a[1..$].sum; } else { dp1[i] = dp1[i-1] + a[i]; dp2[i] = dp2[i-1] - a[i]; } long x = dp1[i], y = dp2[i]; long z = abs(x - y); if (z < ans) { ans = z; } } writeln(ans); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto rd = readln.split.to!(int[]), a = rd[0], b = rd[1], c = rd[2], d = rd[3]; writeln(max(a*b, c*d)); }
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, S; scan(N, S); auto A = aryread(); auto dp = new long[][][](3, N + 1, S + 2); dp[0][0][0] = 1; immutable m = 998244353; foreach (i; 0 .. N) { dp[0][i + 1][0] = 1; foreach (j; 0 .. S + 1) { dp[1][i + 1][j] += dp[0][i][j] + dp[1][i][j]; dp[1][i + 1][j] %= m; dp[1][i + 1][min(S + 1, j + A[i])] += dp[0][i][j] + dp[1][i][j]; dp[1][i + 1][min(S + 1, j + A[i])] %= m; dp[2][i + 1][j] += dp[2][i][j] + dp[1][i][j]; dp[2][i + 1][j] %= m; dp[2][i + 1][min(S + 1, j + A[i])] += dp[1][i][j] + dp[0][i][j]; dp[2][i + 1][min(S + 1, j + A[i])] %= m; } } writeln(dp[2][N][S]); }
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; if (N != 1) { ans = 10; ans.modpow(N); long cnt1 = 9; cnt1.modpow(N); cnt1.modm(2); long cnt2 = 8; cnt2.modpow(N); ans.mods(cnt1); ans.moda(cnt2); } writeln(ans); stdout.flush; debug readln; }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; import std.datetime; import std.numeric; import std.math; import std.string; string my_readln() { return chomp(readln()); } void main() { auto tokens = my_readln().split(); auto N = tokens[0].to!uint; uint[] h; foreach (token; my_readln().split()) { h ~= token.to!uint; } uint ans; while (true) { uint l = N, r = N; foreach (i; 0..N) { if (h[i] >= 1) { l = i; break; } } if (l == N) break; foreach (j; l+1..N) { if (h[j] == 0) { r = j; break; } } foreach (i; l..r) { --h[i]; } ++ans; } writeln(ans); stdout.flush(); }
D
import std.stdio; import std.string; import std.conv; void main(){ (readln.chomp.to!int ^^ 3).writeln; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void main() { long N = lread(); auto s = sread(); long[2] a; foreach (c; s) { a[c / 'R']++; } writeln((a[0] < a[1]) ? "Yes" : "No"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nab = readln.split.to!(int[]); auto N = nab[0]; auto A = nab[1]; auto B = nab[2]; auto S = readln.chomp; int b, n; foreach (c; S) { switch (c) { case 'a': if (n < A+B) { writeln("Yes"); ++n; } else { writeln("No"); } break; case 'b': if (n < A+B && b < B) { ++b; ++n; writeln("Yes"); } else { writeln("No"); } break; default: writeln("No"); } } }
D
import std.stdio, std.string, std.conv, std.array; string inv( string v ) { return to!string(2 - to!int(v)); } string mult( string x, string y ) { if ( x == "0" || y == "0" ) return "0"; else if ( x == "1" || y == "1" ) return "1"; else return "2"; } string add( string x, string y ) { if ( x == "2" || y == "2" ) return "2"; else if ( x == "1" || y == "1" ) return "1"; else return "0"; } void main() { while ( true ) { string input = strip( readln ); if ( input == "." ) break; int count = 0; foreach ( p; [ "0", "1", "2" ] ) { foreach ( q; [ "0", "1", "2" ] ) { foreach ( r; [ "0", "1", "2" ] ) { auto exp = replace( replace( replace( input, "P", p ), "Q", q ), "R", r ); Tokenizer tk = new Tokenizer( exp ); // stderr.writeln( exp ); if ( apply( tk ) == "2" ) { count++; } } } } writeln( count ); } } class Tokenizer { string str; int index; this( string str ) { this.str = str; this.index = 0; } string nextToken() { auto ans = str[ index..index+1 ]; index++; return ans; } bool eof() { return index == str.length; } } string apply( Tokenizer tk ) { if ( !tk.eof ) { auto nt = tk.nextToken; switch( nt ) { case "0": case "1": case "2": return nt; case "-": auto token = apply( tk ); return inv( token ); case "(": auto token1 = apply( tk ); auto op = tk.nextToken(); auto token2 = apply( tk ); tk.nextToken(); // ) if ( op == "+" ) { return add( token1, token2 ); } else if ( op == "*" ) { return mult( token1, token2 ); } else { return ""; } default: return ""; } } else { 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; import std.numeric; void main() { auto n = readln.chomp.to!int; auto h = readln.chomp.split.to!(int[]); int res, cnt; foreach (i; 1..n) { if (h[i-1] >= h[i]) { cnt++; } else { res = max(res, cnt); cnt = 0; } } res = max(res, cnt); res.writeln; }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; import std.range; import std.math; void main(){ auto a=readln.chomp; writeln("A",a[8],"C"); }
D
void main(){ int x, y; scanf("%d %d", &x, &y); (x + y/2).writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string; immutable int INF = 1 << 29; void main() { auto T = readln.chomp.to!int; while (T--) { auto s = readln.split.map!(to!long); auto N = s[0]; auto K = s[1]; long cnt = 0; while (N > 0) { long m = N % K; if (m == 0) { N /= K; cnt += 1; } else { N -= m; cnt += m; } } cnt.writeln; } }
D
//dlang template---{{{ import std.stdio; import std.conv; import std.string; import std.array; import std.algorithm; import std.typecons; import std.math; import std.range; // MIT-License https://github.com/kurokoji/nephele class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } //Digit count---{{{ int DigitNum(int num) { int digit = 0; while (num != 0) { num /= 10; digit++; } return digit; } //}}} //}}} void main() { Scanner sc = new Scanner; int N, A, B; sc.scan(N, A, B); 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; } 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 t = RD!int; auto nk = new long[2][](t); foreach (i; 0..t) { nk[i] = [RD, RD]; } foreach (i; 0..t) { auto n = nk[i][0]; auto k = nk[i][1]; long ans; while (n != 0) { auto x = n % k; if (x == 0) { n /= k; ++ans; } else { n -= x; ans += x; } } writeln(ans); } stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T read(T)(){ return read.to!T; } T[] read(T)(long n){ return n.iota.map!(_ => read!T).array; } T[][] read(T)(long m, long n){ return m.iota.map!(_ => read!T(n)).array; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ int n = read.to!int; long[] as; foreach(i; 0 .. n) as ~= read.to!long; string ans; int i = 0, j = n - 1; long x = -1; while(i <= j){ if(as[i] < as[j]){ if(x < as[i]) x = as[i], ans ~= "L", i += 1; else if(x < as[j]) x = as[j], ans ~= "R", j -= 1; else break; } else if(as[j] < as[i]){ if(x < as[j]) x = as[j], ans ~= "R", j -= 1; else if(x < as[i]) x = as[i], ans ~= "L", i += 1; else break; } else{ if(x >= as[i]) break; int c1 = i; while(c1 < j && as[c1] < as[c1 + 1]) c1 += 1; int c2 = j; while(i < c2 && as[c2] < as[c2 - 1]) c2 -= 1; if(c1 - i < j - c2) x = as[j], ans ~= "R", j -= 1; else x = as[i], ans ~= "L", i += 1; } } ans.length.writeln; ans.writeln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum 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; bool check(int x) { if (2*x <= n) { return true; } foreach (i ; n - x .. x) { if (s[i] != s[n - x]) { return false; } } return true; } int ok = 1, ng = n + 1; while (abs(ok - ng) > 1) { int mid = (ok + ng) / 2; (check(mid) ? ok : ng) = mid; } writeln(ok); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; import std.range; void main(){ auto Q=readln.split.to!(int[]),A=Q[0],B=Q[1]; if(A==B)writeln("Draw"); else if(A==1&&A<B)writeln("Alice"); else if(B==1&&A>B)writeln("Bob"); else if(A<B)writeln("Bob"); else if(A>B)writeln("Alice"); }
D
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; int n, m; int[] a; void main() { scan(n, m); a = readln.split.to!(int[]); auto imos = new int[](m + 2); auto b = new long[](m + 2); long move; foreach (i ; 0 .. n - 1) { int s = a[i], t = a[i + 1]; if (s < t) { imos[s + 1]++; imos[t]--; } else { imos[1]++; imos[t]--; imos[s + 1]++; } b[t] += (t - s + m) % m - 1; move += min((a[i + 1] - a[i] + m) % m, 1 + (a[i + 1] - 1 + m) % m); } foreach (i ; 0 .. m) { imos[i + 1] += imos[i]; } debug { writeln("move:", move); } long ans = move; foreach (i ; 1 .. m) { long tmp = move - imos[i] + b[i]; ans = min(ans, tmp); move = tmp; debug { writeln("move:", move); } } writeln(ans); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
void main() { int[] tmp = readln.split.to!(int[]); int a = tmp[0], b = tmp[1]; writeln((a + b) % 24); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.string, std.conv, 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; alias Edge = Tuple!(int, "to", int, "idx"); void main() { int N; scan(N); auto adj = new Edge[][](N, 0); foreach (i ; 0 .. N - 1) { int ai, bi; scan(ai, bi); ai--, bi--; adj[ai] ~= Edge(bi, i); adj[bi] ~= Edge(ai, i); } auto ans = new int[](N - 1); ans[] = -1; int K; void dfs(int v, int p, int c) { int col = 1; foreach (e ; adj[v]) if (e.to != p) { if (col == c) col++; ans[e.idx] = col; K = max(K, col); dfs(e.to, v, col); col++; } } dfs(0, -1, -1); writeln(K); foreach (i ; 0 .. N - 1) { writeln(ans[i]); } } 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 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, X; scan(N, X); auto x = aryread(); x.map!(a => abs(a - X)) .reduce!gcd().writeln(); }
D
import std.stdio, std.algorithm, std.conv; void main() { int[101] count; int max_; foreach(n; stdin.byLine().map!(to!int)) { ++count[n]; max_ = max(max_, count[n]); } foreach(i; 1..101) { if(count[i] == max_) i.writeln; } }
D
void main(){ int[int] dic; int ans; int[] colors = inln(); foreach(elm; colors){ dic[elm]++; if(dic.get(elm,0)==1)ans++; } ans.writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range; const long mod = 10^^9+7; // 1要素のみの入力 T inelm(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] inln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split())ln ~= elm.to!T(); return ln; }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; int N, M; int[] a, b; bool[] data; int[][] e; int[] memo; int ans; void Func(int n, int b){ memo ~= n; foreach(i ; e[n]){ bool check = false; if(i == b) continue; foreach(j ; memo){ if(i == j){ check = true; } if(check){ data[j] = true; } } if(!check){ Func(i, n); } } memo.popBack(); } void main() { auto input = readln.chomp.split.to!(int[]); N = input[0]; M = input[1]; a = new int[M]; b = new int[M]; data = new bool[N]; e = new int[][N]; foreach(i ; 0..M){ auto input2 = readln.chomp.split.to!(int[]); a[i] = input2[0]; b[i] = input2[1]; --a[i], --b[i]; e[a[i]] ~= b[i]; e[b[i]] ~= a[i]; } Func(0, 0); foreach(i; 0..M){ if(!data[a[i]] || !data[b[i]]){ ++ans; } } ans.writeln; }
D
//dlang template---{{{ import std.stdio; import std.conv; import std.string; import std.array; import std.algorithm; import std.typecons; import std.math; import std.range; // MIT-License https://github.com/kurokoji/nephele class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } //Digit count---{{{ int DigitNum(int num) { int digit = 0; while (num != 0) { num /= 10; digit++; } return digit; } //}}} //}}} void main() { Scanner sc = new Scanner; int N, M; sc.scan(N, M); int[] like = new int[M]; like[] = 0; foreach (i; 0 .. N) { int K; sc.scan(K); int tmp; foreach (j; 0 .. K) { sc.scan(tmp); like[tmp - 1]++; } } int sum = 0; foreach (i; 0 .. M) { if (like[i] == N) { sum++; } } writeln(sum); }
D
import std.stdio; import std.array; import std.conv; import std.string; import std.algorithm; void main() { int[] m; while((m=map!(to!int)(readln.strip.split).array)!=[0,0]) { int n=m[0],x=m[1],sum = 0; for(int i=1;i<n;i++) { for(int j=i+1;j<x-i;j++) { int k = x-i-j; if(j<k && k<=n) { sum += 1; } } } writeln(sum); } }
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 readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}} void main() { int[] a; readC(5, a); auto b = a.map!(ai => (ai+9)/10*10).array; auto r = 1000; foreach (i; 0..5) r = min(r, b.sum-b[i]+a[i]); writeln(r); }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; void times(alias fun)(int n) { foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } // fold was added in D 2.071.0. 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); } } } void main() { readln; readln.split.sort().uniq.array.length.pipe!"(a+1)/2*2-1".writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto A = RD; auto B = RD; writeln(A*B); stdout.flush; debug readln; }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp.to!int; writeln(((n-1)/111+1)*111); }
D
import std.stdio, std.string, std.conv, std.range, std.array, std.algorithm; import std.uni, std.math, std.container, std.typecons, std.typetuple; import core.bitop, std.datetime; immutable long mod = 10^^9 + 7; void main(){ auto col = new int[](8); int n = readln.chomp.to!int; auto a = readln.split.to!(int[]); int over = 0; foreach(ai ; a){ if(ai >= 3200){ over += 1; } else { col[ai / 400] = 1; } } auto mina = sum(col); if (mina == 0){ mina = 1; } auto maxa = sum(col) + over; writeln(mina, " ", maxa); } void readVars(T...)(auto ref T args){ auto line = readln.split; foreach(ref arg ; args){ arg = line.front.to!(typeof(arg)); line.popFront; } if(!line.empty){ throw new Exception("args num < input num"); } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp.to!int; auto p = readln.chomp.split.to!(int[]); int cnt; foreach (i; 0..n) { if (p[i] != i + 1) { cnt++; } } if (cnt <= 2) { writeln("YES"); } else { writeln("NO"); } }
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; void main() { auto pat = ['z', '.', '?', '!', ' ']; foreach (line; stdin.byLine) { auto str = line.chomp; if (str.length % 2) { writeln("NA"); continue; } int c; string res = ""; foreach (i, s; str) { if (i % 2) { if (s >= '1' && s <= '5') { if (c == 'z') { c = pat[s - '1']; } else { c += s - '1'; } res ~= c.to!char; } else { res = "NA"; break; } } else { if (s >= '1' && s <= '6') { c = 'a' + (s - '1') * 5; } else { res = "NA"; break; } } } res.writeln; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void solve() { auto abcd = readln.split.to!(long[]); auto A = abcd[0]; auto B = abcd[1]; auto C = abcd[2]; auto D = abcd[3]; if (A < B || D < B) { writeln("No"); return; } if (C >= B) { writeln("Yes"); return; } auto g = gcd(B, D); writeln(A%g + B-g > C ? "No" : "Yes"); } void main() { auto T = readln.chomp.to!int; foreach (_; 0..T) solve(); }
D
import std.algorithm; import std.array; import std.stdio; import std.conv; import std.string; import std.range; void main(){ int[] ary; int[] temp1, temp2; while(true){ auto input = readln.split; int n = input[0].to!int; int r = input[1].to!int; if(n == 0 && r == 0){ break; } ary = iota(1, n + 1).retro.array; temp1 = new int[n]; temp2 = new int[n]; foreach(i; 0..r){ auto input2 = readln.split; int p = input2[0].to!int; int c = input2[1].to!int; temp1[0..(p-1)] = ary[0..(p-1)]; temp2[0..c] = ary[(p-1)..(p-1) + c]; ary[c..c + (p-1)] = temp1[0..(p-1)]; ary[0..c] = temp2[0..c]; } ary[0].writeln; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto Q = readln.chomp.to!int; foreach (_; 0..Q) { auto X = readln.chomp; auto Y = readln.chomp; auto DP = new int[][](X.length, Y.length); foreach_reverse (i; 0..X.length) { foreach_reverse (j; 0..Y.length) { if (X[i] == Y[j]) { DP[i][j] = max( DP[i][j], (i+1 != X.length && j+1 != Y.length ? DP[i+1][j+1] : 0) + 1 ); } else { DP[i][j] = max( i+1 != X.length ? DP[i+1][j] : 0, j+1 != Y.length ? DP[i][j+1] : 0 ); } } } writeln(DP[0][0]); } }
D
void main(){ auto NK = readLine!long(); long[] list = new long[](cast(size_t)NK[0]); long K = NK[1]; foreach( i ; 0..K ){ auto type = readLine!int()[0]; auto toList = readLine!int(); foreach( to ; toList ){ list[to-1] += type; } } list.count(0).writeln(); } import std; string alphabet = "abcdefghijklmnopqrstuvwxyz"; string readStr(){ return readln().chomp(); } T[] readLine( T = long )(){ return readln().split().to!(T[])(); }
D
import std.stdio, std.conv, std.array,std.string,std.algorithm; void main() { auto input1=readln.chomp,input2=readln.chomp; writeln((2*input2.to!int)-input1.to!int); }
D
import std.stdio; import std.conv; import std.string; void main() { auto X = readln.split.to!(int[]); if(X.count(5) == 2 && X.count(7) == 1) { writeln("YES"); } else { writeln("NO"); } }
D
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math, std.container, std.typecons; import core.stdc.stdio; // foreach, foreach_reverse, writeln void main() { int n; scanf("%d", &n); long[] d = new long[n]; foreach (i; 0..n) { scanf("%lld", &d[i]); } alias Tuple!(long,int) P; P[] raw_p = new P[n]; foreach (i; 0..n) { raw_p[i][0] = d[i]; raw_p[i][1] = i; } auto p = raw_p.sort.assumeSorted; int[] sz = new int[n]; P[] ans; foreach_reverse (i; 1..n) { int v = p[i][1]; sz[v]++; int diff = (n-sz[v]) - sz[v]; int pi = p.lowerBound(P(d[v]-diff,-1)).length.to!int; if (pi == n || p[pi][0] != d[v]-diff) { writeln(-1); return; } int par = p[pi][1]; sz[par] += sz[v]; ans ~= P(v+1, par+1); } int[][] to = new int[][](n); foreach (edge; ans) { int a = edge[0].to!int-1, b = edge[1]-1; if (d[a] > d[b]) swap(a,b); to[a] ~= b; } int root = p[0][1]; long dfs(int v, int depth) { long ret = depth; foreach (int u; to[v]) ret += dfs(u,depth+1); return ret; } if (dfs(root, 0) != d[root]) { writeln(-1); return; } foreach (edge; ans) { writeln(edge[0], ' ', edge[1]); } }
D
void main() { problem(); } void problem() { auto N = scan!int; auto STONES = scan!string.to!(char[]); long solve() { long left = 0; long right = N-1; long ans; while(true) { if (STONES[right] != 'R') right--; if (STONES[left] != 'W') left++; if (right <= left) break; if (STONES[right] == 'R' && STONES[left] == 'W') { right--; left++; ans++; } } return ans; } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
import std.stdio; long fact(int N) { if (N == 0) return 1L; return N * fact(N - 1); } void main() { int N; scanf("%d\n", &N); writeln(fact(N)); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto S = RD!string; long ans = 1; foreach (i; 1..N) { if (S[i] != S[i-1]) ++ans; } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio; import std.conv; import std.string; import std.range; import std.algorithm; void main(){ while(true){ int n = readln().chomp().to!int(); if(n == 0){ break; } string str = readln().chomp(); foreach(i; 0..n){ string res; int count = 1; char current = '$'; foreach(c; str){ if(current == '$'){ current = c; }else if(current == c){ ++count; }else{ res ~= text(count, current); current = c; count = 1; } } res ~= text(count, current); str = res; } str.writeln(); } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { int x = readint; int a = readint; int b = readint; int donut = (x - a) / b; writeln(x - a - b * donut); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; import std.typecons : Tuple; ulong safeMod(long x, long m) @safe pure nothrow @nogc { x %= m; if (x < 0) x += m; return x; } long ctPowMod(long x, long n, int m) @safe pure nothrow @nogc { if (m == 1) return 0; uint _m = cast(uint) m; ulong r = 1; ulong y = safeMod(x, m); while (n) { if (n & 1) r = (r * y) % _m; y = (y * y) % _m; n >>= 1; } return r; } bool ctIsPrime(int n) @safe pure nothrow @nogc { if (n <= 1) return false; if (n == 2 || n == 7 || n == 61) return true; if (n % 2 == 0) return false; long d = n - 1; while (d % 2 == 0) d /= 2; foreach (a; [2, 7, 61]) { long t = d; long y = ctPowMod(a, t, n); while (t != n - 1 && y != 1 && y != n - 1) { y = y * y % n; t <<= 1; } if (y != n - 1 && t % 2 == 0) { return false; } } return true; } enum bool isPrime(int n) = ctIsPrime(n); Tuple!(long, long) invGcd(long a, long b) @safe pure nothrow @nogc { a = safeMod(a, b); if (a == 0) return Tuple!(long, long)(b, 0); long s = b, t = a, m0 = 0, m1 = 1; while (t) { long u = s / t; s -= t * u; m0 -= m1 * u; long tmp = s; s = t; t = tmp; tmp = m0; m0 = m1; m1 = tmp; } if (m0 < 0) m0 += b / s; return Tuple!(long, long)(s, m0); } int ctPrimitiveRoot(int m) @safe pure nothrow @nogc { if (m == 2) return 1; if (m == 167_772_161) return 3; if (m == 469_762_049) return 3; if (m == 754_974_721) return 11; if (m == 998_244_353) return 3; int[20] divs; divs[0] = 2; int cnt = 1; int x = (m - 1) / 2; while (x % 2 == 0) x /= 2; for (int i = 3; (cast(long) i) * i <= x; i += 2) if (x % i == 0) { divs[cnt++] = i; while (x % i == 0) x /= i; } if (x > 1) divs[cnt++] = x; for (int g = 2;; g++) { bool ok = true; foreach (i; 0 .. cnt) if (ctPowMod(g, (m - 1) / divs[i], m) == 1) { ok = false; break; } if (ok) return g; } } enum primitiveRoot(int m) = ctPrimitiveRoot(m); long powMod(long x, long n, long m) @safe pure nothrow @nogc { assert(0 <= n && 1 <= m); if (m == 1) return 0; ulong r = 1, y = safeMod(x, m); while (n) { if (n & 1) r = (r * y) % m; y = (y * y) % m; n >>= 1; } return r; } long invMod(long x, long m) @safe pure nothrow @nogc { assert(1 <= m); auto z = invGcd(x, m); assert(z[0] == 1); return z[1]; } Tuple!(long, long) crt(long[] r, long[] m) @safe pure nothrow @nogc { assert(r.length == m.length); long r0 = 0, m0 = 1; foreach (i; 0 .. r.length) { assert(1 <= m[i]); long r1 = safeMod(r[i], m[i]); long m1 = m[i]; if (m0 < m1) { auto tmp = r0; r0 = r1; r1 = tmp; tmp = m0; m0 = m1; m1 = tmp; } if (m0 % m1 == 0) { if (r0 % m1 != r1) return Tuple!(long, long)(0, 0); continue; } long g, im; { auto tmp = invGcd(m0, m1); g = tmp[0]; im = tmp[1]; } long u1 = m1 / g; if ((r1 - r0) % g) return Tuple!(long, long)(0, 0); long x = (r1 - r0) / g % u1 * im % u1; r0 += x * m0; m0 *= u1; if (r0 < 0) r0 += m0; } return Tuple!(long, long)(r0, m0); } long floorSum(long n, long m, long a, long b) @safe pure nothrow @nogc { long ans; if (m <= a) { ans += (n - 1) * n * (a / m) / 2; a %= m; } if (m <= b) { ans += n * (b / m); b %= m; } long y_max = (a * n + b) / m, x_max = (y_max * m - b); if (y_max == 0) return ans; ans += (n - (x_max + a - 1) / a) * y_max; ans += floorSum(y_max, a, m, (a - x_max % a) % a); return ans; } void main() { auto N = readln.chomp.to!long * 2; long[] ps; for (long p = 1; p^^2 <= N; ++p) if (N%p == 0) { ps ~= p; if (N/p != p) ps ~= N/p; } long res = long.max; foreach (p; ps) { auto ret = crt([0, -1], [p, N/p]); if (ret[0]) res = min(res, ret[0]); } writeln(res == long.max ? N : res); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { long n, x; scan(n, x); long[] m = new long[n]; foreach (ref e; m) { e = lread(); } foreach (e; m) { x -= e; } long min_cost = m[0]; foreach (e; m) { min_cost = min(e, min_cost); } writeln(n + x / min_cost); }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { const tmp = readln.split.to!(long[]); const N = tmp[0], M = tmp[1]; iota(1,M/N+1).retro.filter!(x => M % x == 0).front.writeln; }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { string s; readV(s); string t; readV(t); auto n = s.length; foreach (_; 0..n) { if (s == t) { writeln("Yes"); return; } s = s[$-1] ~ s[0..$-1]; } writeln("No"); }
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 full = (1<<26) - 1; void main() { string s; scan(s); int n = s.length.to!int; long ans; void dfs(int i, long val, long d) { if (i == n) { debug { writeln(val + d); } ans += val + d; return; } dfs(i + 1, val, d*10 + s[i] - '0'); dfs(i + 1, val + d, s[i] - '0'); } dfs(1, 0, s[0] - '0'); 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.conv, std.string, std.random, std.range, std.typecons, std.math; import std.algorithm.comparison, std.algorithm.iteration; import std.algorithm.mutation, std.algorithm.searching, std.algorithm.sorting; void main() { auto input = readln().strip.split.to!(int[]); auto A = input[0]; auto B = input[1]; auto K = input[2]; int count; foreach_reverse(i; 0..101) { count += A%i == 0 && B%i==0 ? 1 : 0; if(count==K) { writeln(i); return; } } }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; import std.typecons; import std.bigint; // import dcomp.foundation, dcomp.scanner; // import dcomp.container.deque; int main() { auto sc = new Scanner(stdin); int n, m, k; sc.read(n, m, k); m++; int[] a = new int[n]; a.each!((ref x) => sc.read(x)); long[] dp = a.map!(to!long).array; long[] ndp = new long[n]; int[] deq = new int[n]; foreach (int ph; 2..k+1) { ndp[] = -(10L^^18); int l = 0, r = 0; foreach (int i; 0..n) { if (l < r && deq[l] == i-m) { l++; } if (l < r) { ndp[i] = dp[deq[l]] + 1L * ph * a[i]; } while (l < r && dp[deq[r-1]] <= dp[i]) { r--; } deq[r] = i; r++; } swap(dp, ndp); } writeln(dp.fold!max); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; //fold(for old compiler) static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } unittest { import std.stdio; auto l = [1, 2, 3, 4, 5]; assert(l.fold!"a+b"(10) == 25); } } version (X86) static if (__VERSION__ < 2071) { int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/deque.d */ // module dcomp.container.deque; struct Deque(T) { import core.exception : RangeError; import core.memory : GC; import std.range : ElementType, isInputRange; import std.traits : isImplicitlyConvertible; struct Payload { T *d; size_t st, length, cap; @property bool empty() const { return length == 0; } alias opDollar = length; ref inout(T) opIndex(size_t i) inout { version(assert) if (length <= i) throw new RangeError(); return d[(st+i >= cap) ? (st+i-cap) : st+i]; } private void expand() { import std.algorithm : max; assert(length == cap); auto nc = max(size_t(4), 2*cap); T* nd = cast(T*)GC.malloc(nc * T.sizeof); foreach (i; 0..length) { nd[i] = this[i]; } d = nd; st = 0; cap = nc; } void clear() { st = length = 0; } void insertFront(T v) { if (length == cap) expand(); if (st == 0) st += cap; st--; length++; this[0] = v; } void insertBack(T v) { if (length == cap) expand(); length++; this[length-1] = v; } void removeFront() { assert(!empty, "Deque.removeFront: Deque is empty"); st++; length--; if (st == cap) st = 0; } void removeBack() { assert(!empty, "Deque.removeBack: Deque is empty"); length--; } } struct RangeT(A) { alias T = typeof(*(A.p)); alias E = typeof(A.p.d[0]); T *p; size_t a, b; @property bool empty() const { return b <= a; } @property size_t length() const { return b-a; } @property RangeT save() { return RangeT(p, a, b); } @property RangeT!(const A) save() const { return typeof(return)(p, a, b); } alias opDollar = length; @property ref inout(E) front() inout { return (*p)[a]; } @property ref inout(E) back() inout { return (*p)[b-1]; } void popFront() { version(assert) if (empty) throw new RangeError(); a++; } void popBack() { version(assert) if (empty) throw new RangeError(); b--; } ref inout(E) opIndex(size_t i) inout { return (*p)[i]; } RangeT opSlice() { return this.save; } RangeT opSlice(size_t i, size_t j) { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } RangeT!(const A) opSlice() const { return this.save; } RangeT!(const A) opSlice(size_t i, size_t j) const { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } } alias Range = RangeT!Deque; alias ConstRange = RangeT!(const Deque); alias ImmutableRange = RangeT!(immutable Deque); Payload *p; private void I() { if (!p) p = new Payload(); } private void C() const { version(assert) if (!p) throw new RangeError(); } //some value this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) {I; p = new Payload(); foreach (v; values) { insertBack(v); } } //range this(Range)(Range r) if (isInputRange!Range && isImplicitlyConvertible!(ElementType!Range, T) && !is(Range == T[])) {I; p = new Payload(); foreach (v; r) { insertBack(v); } } @property bool empty() const { return (!p || p.empty); } @property size_t length() const { return (p ? p.length : 0); } alias opDollar = length; ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; } ref inout(T) front() inout {C; return (*p)[0]; } ref inout(T) back() inout {C; return (*p)[$-1]; } void clear() { if (p) p.clear(); } void insertFront(T v) {I; p.insertFront(v); } void insertBack(T v) {I; p.insertBack(v); } void removeFront() {C; p.removeFront(); } void removeBack() {C; p.removeBack(); } Range opSlice() {I; return Range(p, 0, length); } } unittest { import std.algorithm : equal; import std.range.primitives : isRandomAccessRange; import std.container.util : make; auto q = make!(Deque!int); assert(isRandomAccessRange!(typeof(q[]))); //insert,remove assert(equal(q[], new int[](0))); q.insertBack(1); assert(equal(q[], [1])); q.insertBack(2); assert(equal(q[], [1, 2])); q.insertFront(3); assert(equal(q[], [3, 1, 2]) && q.front == 3); q.removeFront; assert(equal(q[], [1, 2]) && q.length == 2); q.insertBack(4); assert(equal(q[], [1, 2, 4]) && q.front == 1 && q.back == 4 && q[$-1] == 4); q.insertFront(5); assert(equal(q[], [5, 1, 2, 4])); //range assert(equal(q[][1..3], [1, 2])); assert(equal(q[][][][], q[])); //const range const auto rng = q[]; assert(rng.front == 5 && rng.back == 4); //reference type auto q2 = q; q2.insertBack(6); q2.insertFront(7); assert(equal(q[], q2[]) && q.length == q2.length); //construct with make auto a = make!(Deque!int)(1, 2, 3); auto b = make!(Deque!int)([1, 2, 3]); assert(equal(a[], b[])); } unittest { import std.algorithm : equal; import std.range.primitives : isRandomAccessRange; import std.container.util : make; auto q = make!(Deque!int); q.clear(); assert(equal(q[], new int[0])); foreach (i; 0..100) { q.insertBack(1); q.insertBack(2); q.insertBack(3); q.insertBack(4); q.insertBack(5); assert(equal(q[], [1,2,3,4,5])); q.clear(); assert(equal(q[], new int[0])); } } unittest { Deque!int a; Deque!int b; a.insertFront(2); assert(b.length == 0); } unittest { import std.algorithm : equal; import std.range : iota; Deque!int a; foreach (i; 0..100) { a.insertBack(i); } assert(equal(a[], iota(100))); } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; if (f.eof) return false; line = lineBuf[]; f.readln(line); } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { //string or char[10] etc //todo optimize auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { //static assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } 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])); } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File, writeln; import std.datetime; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); foreach (i; 0..1_000_000) { fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2); } fout.close; writeln("Scanner Speed Test(3*1,000,000 int)"); StopWatch sw; sw.start; Scanner sc = new Scanner(File(fileName, "r")); foreach (i; 0..500_000) { int a, b, c; sc.read(a, b, c); assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 500_000..700_000) { int[3] d; sc.read(d); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 700_000..1_000_000) { int[] d; sc.read(d); assert(d.length == 3); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } writeln(sw.peek.msecs, "ms"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; long solve(long N) { if (N == -1) return 0; if (N%2 == 0) { return N ^ ((N/2)%2 == 0 ? 0 : 1); } else { return (N/2)%2 == 1 ? 0 : 1; } } void main() { auto ab = readln.split.to!(long[]); writeln(solve(ab[0]-1) ^ solve(ab[1])); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto ab = readln.split.to!(int[]); auto A = ab[0]; auto B = ab[1]; writeln(A * B - A - B + 1); }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); } void main(string[] args){ args ~= ["", ""]; string cmd = args[1]; if(cmd == "-debug") DEBUG = 1; if(cmd == "-gen") gen; else if(cmd == "-jury") jury; else solve; } void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ std.stdio.write(t, " "), print(a); } string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void gen(){ } void jury(){ } void solve(){ foreach(_; 0 .. scan!int){ int n = scan!int; int[] as = scan!int(n); int[int] acnt; foreach(a; as){ if(a !in acnt) acnt[a] = 0; acnt[a] += 1; } int ans = 0; int i = 0; while(i in acnt && acnt[i] >= 2) i ++; ans += i; while(i in acnt && acnt[i] >= 1) i ++; ans += i; ans.writeln; } }
D
void main() { long[] tmp = readln.split.to!(long[]); long a = tmp[0], b = tmp[1]; if (a == 0 || b == 0) { if (a < b) writeln(abs(b.abs-a.abs)); else writeln(abs(b.abs-a.abs) + 1); } else if (a / a.abs == b / b.abs) { if (a <= b) writeln(b - a); else writeln(a - b + 2); } else { writeln(abs(b.abs-a.abs) + 1); } } 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, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n; long k; readV(n, k); long[] a; readA(n, a); if (k == 0) { writeln(a.sum); return; } auto m = 40; auto dp = new long[][](m+1, 2); foreach (ref dpi; dp) dpi[] = -1; dp[$-1][1] = 0; foreach_reverse (i; 0..m) foreach (j; 0..2) { auto d = j ? k.bit(i) : 1; foreach (b; 0..d+1) if (dp[i+1][j] >= 0) dp[i][j && b == k.bit(i)].maxU(dp[i+1][j] + (1L<<i)*a.count!(ai => ai.bit(i) == 1-b)); } writeln(dp[0].reduce!max); } auto bit(long a, size_t b) { return (a>>b)&1; } auto maxU(T)(ref T a, T b) { return a = max(a, b); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n, m; rd(n, m); auto as=readln.split.to!(long[]); auto bs=readln.split.to!(long[]); auto dub=new long[](n+1); foreach(i; 0..n) dub[i+1]=dub[i]+as[i]; dub~=1_000_000_000_000_000_000; int x=0; foreach(b; bs){ while(dub[x+1]<b) x++; // dub[x] < b <= dub[x+1] writeln(x+1, " ", b-dub[x]); } } 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
// Try Codeforces // author: Leonardone @ NEETSDKASU import std.stdio, std.string, std.array, std.algorithm, std.conv; void main() { auto nm = to!(int[])(split(chomp(readln()))); int n = nm[0], m = nm[1]; auto xss = appender!(string[])(); for (int i = 0; i < n; i++) { xss.put(chomp(readln())); if (!xss.data[i].count!( (a) => a == 'R' || a == 'G' || a == 'B' )) { writeln("NO"); return; } } auto ans = false; if (n % 3 == 0) { int u = n / 3; bool flag = true; for (int x = 0; x < m; x++) { for (int y = 0; y < n; y++) { auto p = xss.data[y][x]; auto a = xss.data[y/u*u][x]; auto b = xss.data[(y/u+1)%3*u][x]; auto c = xss.data[(y/u+2)%3*u][x]; flag = flag && (p == a && p != b && p != c) && (x == 0 || p == xss.data[y][x-1]); } } ans = ans || flag; } if (m % 3 == 0) { int u = m / 3; bool flag = true; for (int y = 0; y < n; y++) { for (int x = 0; x < m; x++) { auto p = xss.data[y][x]; auto a = xss.data[y][x/u*u]; auto b = xss.data[y][(x/u+1)%3*u]; auto c = xss.data[y][(x/u+2)%3*u]; flag = flag && (p == a && p != b && p != c) && (y == 0 || p == xss.data[y-1][x]); } } ans = ans || flag; } writeln(ans ? "YES" : "NO"); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new int[](t); foreach (ti; 0..t) { auto n = RD!int; auto k = RD!int; auto a = RDA!int; int x = int.max; int pos; foreach (i; 0..n) { if (a[i] < x) { x = a[i]; pos = i; } } foreach (i; 0..n) { if (i == pos) continue; auto rem = k - a[i]; ans[ti] += rem / x; } } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
import std.stdio; import std.uni; import std.conv; import std.container; import std.functional; import std.algorithm; import std.array; import std.typecons; import std.range; import std.numeric; import std.traits; import std.math; void main(string[] args) { long[100][100] mat; auto t = next!int; foreach(tc; 0 .. t) { auto n = next!int; auto m = next!int; foreach(i; 0 .. n) foreach(j; 0 .. m) mat[i][j] = next!int; long cost = 0; foreach(i; 0 .. n) { foreach(j; 0 .. m) { int si = n - 1 - i; int sj = m - 1 - j; long localcost = 0; if (si == i) { if (sj == j) { localcost = 0; } else { localcost = abs(mat[i][j] - mat[i][sj]); mat[i][j] = mat[i][sj]; } } else { if (sj == j) { localcost = abs(mat[i][j] - mat[si][j]); mat[i][j] = mat[si][j]; } else { localcost = int.max; long[] vals = [mat[i][j], mat[i][sj], mat[si][j], mat[si][sj]]; auto svals = sort(vals); long usedval = vals[1]; localcost = abs(mat[i][j] - usedval) + abs(mat[i][sj] - usedval) + abs(mat[si][j] - usedval) + abs(mat[si][sj] - usedval); mat[i][j] = usedval; mat[i][sj] = usedval; mat[si][j] = usedval; mat[si][sj] = usedval; } } cost += localcost; } } cost.writeln; } } static this() { popChar; } struct Any(T) { static auto read() { static if (is(T == char)) { auto res = frontChar; popChar; return res; } } } struct Unsigned(T) { auto read() { auto res = T(0); while(!frontChar.isDigit) popChar; do { res = res * T(10) + T(frontChar - '0'); popChar; } while(frontChar.isDigit); return res; } } struct Matrix(T, alias rows, alias cols) { T[][] _cell; alias _cell this; // Ring constructor this(int n) { final switch(n) { case 0: _cell = new T[][](rows, cols); break; case 1: debug assert(rows == cols); _cell = new T[][](rows, cols); foreach(i; 0 .. rows) _cell[i][i] = T(1); break; } } // Copy constructor this(Matrix!(T, rows, cols) other) { _cell = new T[][](rows, cols); foreach(i; 0 .. rows) _cell[i][] = other[i][]; } auto opBinary(string op)(Matrix!(T, rows, cols) other) if (op == "+" || op == "-") { auto res = Matrix!(T, rows, cols)(0); foreach(i; 0 .. rows) foreach(j; 0 .. cols) res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]}); return res; } auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other) if (op == "*") { debug assert(cols == otherRows); auto res = Matrix!(T, rows, otherCols)(0); foreach(i; 0 .. rows) foreach(k; 0 .. cols) foreach(j; 0 .. otherCols) res[i][j] += this[i][k] * other[k][j]; return res; } ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other) { static if (op == "*") { debug assert(rows == cols); alias n = rows; auto res = Matrix!(T, n, n)(0); T factor; foreach(i; 0 .. n) foreach(k; 0 .. n) foreach(j; 0 .. n) res[i][j] += this[i][k] * other[k][j]; foreach(i; 0 .. n) this[i][] = res[i][]; } else { foreach(i; 0 .. rows) foreach(j; 0 .. cols) mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];}); } return this; } mixin powMethod; } struct Mod(T, alias mod) { alias This = Mod!(T, mod); T _rep; this(T t) { _rep = t % mod; if (_rep < 0) _rep += mod; } static auto plain(T t) { auto res = Mod!(T, mod)(); res._rep = t; return res; } static auto fromPositive(T t) { pragma(inline, true); return Mod!(T, mod).plain(t % mod); } static auto fromNegative(T t) { pragma(inline, true); return Mod!(T, mod).plain(t % mod + mod); } string toString() { return to!string(_rep); } debug invariant { assert(_rep >= 0 && _rep < mod); } auto opBinary(string op)(This b) { static if (op == "+") { T resrep = _rep + b._rep; if (resrep >= mod) resrep -= mod; return This.plain(resrep); } else static if (op == "-") { T resrep = _rep - b._rep; if (resrep < 0) resrep += mod; return This.plain(resrep); } else static if (op == "*") { return This.fromPositive(_rep * b._rep); } } auto opOpAssign(string op)(This b) { mixin(q{_rep }, text(op, "="), q{b._rep;}); static if (op == "+") { if (_rep >= mod) _rep -= mod; } else static if (op == "-") { if (_rep < 0) _rep += mod; } else static if (op == "*") { _rep %= mod; } return this; } auto opBinary(string op)(long exp) if (op == "^^") { return pow(exp); } mixin powMethod; } mixin template powMethod() { auto pow(this ThisType)(long exp) { auto res = ThisType(1); auto pow = ThisType(this); while(exp) { if (exp & 1) res *= pow; pow *= pow; exp >>= 1; } return res; } } // INPUT char frontChar; void popChar() { import core.stdc.stdio; frontChar = cast(char) getchar; if (feof(stdin)) frontChar = ' '; } auto makeArray(T, S...)(S s) { enum brackets = () { string res; foreach(i; 0 .. s.length) res ~= "[]"; return res; } (); mixin(q{alias Type = T}, brackets, q{;}); return new Type(s); } template isNumberLike(T) { enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}())); } void popWhite() { import std.ascii; while (frontChar.isWhite) popChar; } void print(T...)(T t) { static foreach(ti; t) { static if (is(typeof(ti) == string)) { write(ti, " "); } else static if (isArray!(typeof(ti))) { foreach(e; ti) print(e); } else static if (isTuple!(typeof(ti))) { static foreach(i; ti.length) writesp(ti[i]); } else { write(ti, " "); } } } void println(T...)(T t) { static if (t.length == 0) writeln; static foreach(ti; t) { print(ti); writeln; } } auto next(alias T, S...)(S s) { pragma(inline, true); static if (s.length > 0) { auto res = makeArray!(typeof(next!T()))(s); S t; enum loops = () { string res; foreach(i; 0 .. s.length) { auto ti = text(q{t[}, i, q{]}); res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)}); } return res; } (); enum indexing = () { string res = "res"; foreach(i; 0 .. s.length) res ~= text(q{[t[}, i, q{]]}); return res; } (); mixin(loops, indexing, q{ = next!T;}); return res; } else { static if (isNumberLike!T) { import std.ascii: isDigit; T res; while(frontChar.isWhite) popChar; T multiplier = T(1); if (frontChar == '-') { multiplier = T(-1); popChar; } else if (frontChar == '+') { popChar; } debug assert(frontChar.isDigit); do { res = res * T(10) + T(frontChar - '0'); popChar; } while(frontChar.isDigit); return multiplier * res; } else static if (is(T == string)) { import std.ascii: isWhite; string res; while(frontChar.isWhite) popChar; while(!frontChar.isWhite) { res ~= frontChar; popChar; } return res; } else static if (is(T == char)) { while(frontChar.isWhite) popChar; auto res = frontChar; popChar; return res; } else { return T.read; } } }
D