code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(int[] a) { int ans = 0; while (a.any!(e => e > 0)) { for (int i = 0; i < a.length; i++) { if (a[i] == 0) continue; int x = a[i]; int last = i; for (int j = i + 1; j < a.length; j++) { if (a[j] == 0) break; x = min(x, a[j]); last = j; } for (int j = i; j <= last; j++) { a[j] -= x; } ans += x; break; } } return ans; } void main() { readint; auto a = readints; writeln(calc(a)); }
D
import std.stdio; import std.math; import std.conv; import std.string; int solve(int x) { return pow(x,3); } void main() { writeln(solve(to!int(chomp(readln)))); }
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; long MOD = 10^^9 + 7; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto K = s[1]; auto B = new int[][](4*K+1, 4*K+1); auto W = new int[][](4*K+1, 4*K+1); auto P = new Tuple!(int, int, bool)[](N); foreach (i; 0..N) { auto S = readln.split; P[i] = tuple(S[0].to!int, S[1].to!int, S[2] == "B"); } foreach (i; 0..N) { int x = P[i][0] % (2 * K); int y = P[i][1] % (2 * K); if (P[i][2]) { B[x+1][y+1] += 1; B[x+1][y+1+2*K] += 1; B[x+1+2*K][y+1] += 1; B[x+1+2*K][y+1+2*K] += 1; } else { W[x+1][y+1] += 1; W[x+1][y+1+2*K] += 1; W[x+1+2*K][y+1] += 1; W[x+1+2*K][y+1+2*K] += 1; } } foreach (i; 0..4*K+1) foreach (j; 0..4*K) B[i][j+1] += B[i][j]; foreach (j; 0..4*K+1) foreach (i; 0..4*K) B[i+1][j] += B[i][j]; foreach (i; 0..4*K+1) foreach (j; 0..4*K) W[i][j+1] += W[i][j]; foreach (j; 0..4*K+1) foreach (i; 0..4*K) W[i+1][j] += W[i][j]; int acm(int x1, int y1, int x2, int y2, int[][] A) { return A[x2+1][y2+1] - A[x2+1][y1] - A[x1][y2+1] + A[x1][y1]; } int ans = 0; foreach (i; 0..2*K) { foreach (j; 0..2*K) { int b = acm(i, j, i+K-1, j+K-1, B) + acm(i+K, j+K, i+2*K-1, j+2*K-1, B); int w = acm(i, j+K, i+K-1, j+2*K-1, W) + acm(i+K, j, i+2*K-1, j+K-1, W); ans = max(ans, b + w); } } ans.writeln; }
D
import std.stdio, std.conv, std.string; import std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } int DEBUG_LEVEL = 0; void print()(){ writeln(""); } void print(T, A ...)(T t, lazy A a){ write(t), print(a); } void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); } void main(string[] args){ if(args.length > 1 && args[1] == "-debug"){ if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int; else DEBUG_LEVEL = 1; } long n = read.to!long; string s = readln.chomp; long r = 0, b = 0; foreach(c; s) if(c == 'R') r += 1; else b += 1; if(r > b) "Yes".writeln; else "No".writeln; }
D
import std.stdio, std.string, std.range, std.algorithm, std.conv; void main(){ readln; auto S = readln.chomp.split.map!(to!int).array; readln; auto T = readln.chomp.split.map!(to!int).array; int c = 0; foreach(i; T){ if(!S.find(i).empty) c++; } c.writeln(); // filter!("a")(chain(S,T)).writeln(); }
D
import std.stdio, std.array, std.conv, std.typecons, std.algorithm; T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; } void main() { immutable i = readln.split.to!(ulong[]); immutable a = i[0], b = i[1]; auto ans = a >= 13 ? b : (a >= 6 ? b/2 : 0); writeln(ans); }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop; // dfmt on void main() { long N = lread(); auto A = aryread(); auto cnt = new long[](100001); foreach (a; A) { if (a != 0) cnt[a - 1]++; cnt[a]++; cnt[a + 1]++; } writeln(cnt.reduce!max); }
D
import std.stdio, std.string, std.conv; import std.array, std.algorithm, std.range; void main() { immutable n = readln().chomp().to!int(); auto a = iota(n).map!(_=>readln().split().map!(to!int).array()).array(); int idx(int i, int j){ return i*(n+1)+j; } auto s = new int[(n+1)^^2]; foreach(i;0..n) foreach(j;0..n) s[idx(i+1,j+1)] = s[idx(i+1,j)]+s[idx(i,j+1)]-s[idx(i,j)]+a[i][j]; int m=int.min; foreach(i1;0..n) foreach(i2;i1+1..n+1) foreach(j1;0..n) foreach(j2;j1+1..n+1) m=max(m,s[idx(i2,j2)]-s[idx(i2,j1)]-s[idx(i1,j2)]+s[idx(i1,j1)]); writeln(m); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; enum P = 10L^^9+7; void main() { auto nk = readln.split.to!(long[]); auto N = nk[0]; auto K = nk[1]; auto cs = new long[](N+1); foreach (long i; 0..N+1) { cs[i] = i; if (i) cs[i] += cs[i-1]; } long r; foreach (k; K..N+1) { auto d = (cs[N] - cs[N-k] - cs[k-1] + 1) % P; r += d %= P; } writeln((r+1)%P); }
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } class Node{ int id; int groupId; Node[] adjs; bool isVisited = 0; this(int id){ this.id = id; } void setGroupId(int gid){ this.groupId = gid; this.isVisited = true; foreach(adj; adjs) if(! adj.isVisited) adj.setGroupId(gid); } } void main(){ auto n = read.to!int; auto m = read.to!int; auto ps = readln.chomp.split.to!(int[]).map!(i => i - 1)().array; Node[] nodes; foreach(i; 0 .. n) nodes ~= new Node(i); foreach(j; 0 .. m){ auto x = read.to!int - 1; auto y = read.to!int - 1; nodes[x].adjs ~= nodes[y]; nodes[y].adjs ~= nodes[x]; } foreach(nd; nodes){ if(nd.isVisited) continue; nd.setGroupId(nd.id); } int count = 0; foreach(i; 0 .. n){ if(nodes[i].groupId == nodes[ps[i]].groupId){ count += 1; } } count.writeln; }
D
void main() { long a = readln.chomp.to!long - 1; long b = readln.chomp.to!long - 1; bool[3] ok; ok[] = true; ok[a] = ok[b] = false; foreach (i, x; ok) { if (x) writeln(i + 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 core.stdc.stdio; import std.algorithm; void main(){ int n; scanf("%d",&n); int[] a = new int[n]; foreach(ref v;a) scanf("%d",&v); long[][] dp = new long[][](n,n); int l = n%2; if(l) foreach(i;0..n) dp[0][i]=a[i]; foreach(s;1..n){ l^=1; foreach(i;0..n) if(l) dp[s][i]=max(dp[s-1][i]+a[(i+s)%n],dp[s-1][(i+1)%n]+a[i]); else dp[s][i]=a[(i+s)%n]>a[i]?dp[s-1][i]:dp[s-1][(i+1)%n]; } long ans; foreach(d;dp[n-1]) ans=max(d,ans); printf("%lld\n",ans); }
D
class UF{ int[] par; this(int n){ par.length=n; foreach(i; 0..n) par[i]=i; } int find(int x){ if(x==par[x]) return par[x]; else return par[x]=find(par[x]); } void unite(int x, int y){ if((x=find(x))!=(y=find(y))) par[x]=y; } bool same(int x, int y){ return find(x)==find(y); } } void main(){ import std.stdio, std.string, std.conv, std.algorithm; import std.typecons; int n, m; rd(n, m); alias Edge=Tuple!(int, int); auto edge=new Edge[](0); foreach(_; 0..m){ int a, b; rd(a, b); edge~=Edge(a-1, b-1); } int cnt=0; foreach(i; 0..m){ auto uf=new UF(n); foreach(j; 0..m){ if(i==j) continue; uf.unite(edge[j][0], edge[j][1]); } if(!uf.same(edge[i][0], edge[i][1])) cnt++; } writeln(cnt); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; void main() { int n, h, w; scan(n, h, w); iota(n).map!(i => readln.split.to!(int[])).count!(a => a[0] >= h && a[1] >= w).writeln; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 10^^9 + 7; void main() { int n, w; scan(n, w); auto dp = new long[](w + 1); foreach (i ; 0 .. n) { int wi, vi; scan(wi, vi); foreach_reverse (j ; 0 .. w - wi + 1) { dp[j + wi] = max(dp[j + wi], dp[j] + vi); } } writeln(dp[w]); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int h, w, k; scan(h, w, k); auto c = new int[][](w, w); foreach (s ; 0 .. (1 << (w - 1))) { if (s & (s >> 1)) continue; auto p = iota(0, w).array; foreach (i ; 0 .. w - 1) { if (s & (1 << i)) { swap(p[i], p[i + 1]); } } foreach (i ; 0 .. w) { c[i][p[i]]++; } } debug { writefln("%(%s\n%)", c); } auto dp = new long[][](h + 1, w); dp[0][0] = 1; foreach (i ; 1 .. h + 1) { foreach (j ; 0 .. w) { foreach (s ; 0 .. w) { dp[i][j] += dp[i - 1][s] * c[s][j] % mod; dp[i][j] %= mod; } } } debug { writefln("%(%s\n%)", dp); } auto ans = dp[h][k - 1]; 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.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); auto a = s.indexOf('A'), z = s.lastIndexOf('Z'); writeln(z-a+1); }
D
import std.stdio, std.string, std.algorithm, std.range, std.conv, std.typecons, std.math; void main(){ auto x = readln.chomp.to!ulong; auto n = x / 11 * 2; auto m = x % 11; if (1 <= m && m <= 6) { n++; } else if (7 <= m) { n += 2; } writeln(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 = "%.15f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } int binarySearch(alias pred, T)(in T[] arr, bool isLower = true) { int ok, ng; if (isLower) { ok = cast(int)arr.length; ng = -1; } else { ok = -1; ng = cast(int)arr.length; } while (abs(ok-ng) > 1) { auto mid = (ok+ng) / 2; if (unaryFun!pred(arr[mid])) ok = mid; else ng = mid; } return ok; } void main() { auto N = RD; auto S = RD!string; auto pos = new long[][](26); foreach (i; 0..N) { auto num = S[i] - 'a'; pos[num] ~= i; } long ans; foreach (i; 0..N-1) { auto num = S[i] - 'a'; auto pi = pos[num].binarySearch!((long a) => a > i)(); foreach (j; pos[num][pi..$]) { auto len1 = j - i; auto len2 = N - j; auto len = min(len1, len2); if (len <= ans) continue; foreach (k; 0..len) { if (S[i+k] != S[j+k]) break; ans = max(ans, k+1); } } } writeln(ans); stdout.flush(); debug readln(); }
D
import std.array, std.stdio, std.conv, std.string, std.math, std.random, std.range,std.functional, std.typecons; import std.algorithm.searching, std.algorithm.sorting, std.algorithm.iteration, std.algorithm.comparison; void main() { int Q = readln().strip.to!int; foreach(i;0..Q) { auto aList = readln().strip; auto bList = readln().strip; writeln(lcs(aList, bList)); } } int lcs(string aList, string bList) { int[][] memo; memo.length = aList.length+1; memo[0].length = bList.length+1; foreach(i; 1..aList.length+1) { memo[i].length = bList.length+1; char a = aList[i-1]; foreach(j; 1..bList.length+1) { char b = bList[j-1]; if(a==b) { memo[i][j] = memo[i-1][j-1]+1; }else{ memo[i][j] = max( memo[i-1][j], memo[i][j-1], ); } } } return memo[aList.length][bList.length]; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; void main() { readln; readln.split.to!(int[]).count!"a % 2 == 0".writeln; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto t = RD!int; auto ans = new string[](t); auto k = new int[](t); foreach (ti; 0..t) { auto n = RD!int; auto s = RD!string; auto list = new int[][](n); foreach (i; 0..n) { foreach (j; i..n) list[i] ~= s[j]-'a'; if ((n-i) % 2 == 0) { foreach (j; 0..i) list[i] ~= s[j]-'a'; } else { foreach_reverse (j; 0..i) list[i] ~= s[j]-'a'; } } auto pos = list.MIN_POS(); k[ti] = cast(int)(pos+1); foreach (i; 0..n) { ans[ti] ~= cast(char)(list[pos][i]+'a'); } } foreach (ti; 0..t) { writeln(ans[ti]); writeln(k[ti]); } stdout.flush; debug readln; }
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 R = scanElem; auto G = scanElem; writeln(G*2-R); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.container; import std.array; import std.math; import std.range; import std.typecons; import std.ascii; import std.format; class Iku { int dist; long cost; } class Node { int now; long cost; } void bfs(int k, int n, Iku[][] adj, long[] memo) { Node[] queue; Node atom = new Node(); atom.now = k; atom.cost = 0; queue ~= atom; bool[] done = new bool[n + 1]; while (queue.length) { Node node = queue.front; queue = queue[1..$]; if (done[node.now]) { continue; } done[node.now] = true; memo[node.now] = node.cost; foreach (Iku iku; adj[node.now]) { Node next = new Node(); next.cost = node.cost + iku.cost; next.now = iku.dist; queue ~= next; } } } void main() { int n = readln.chomp.to!int; Iku[][] adj = new Iku[][](n + 1, 0); foreach (i; 0..n - 1) { auto l = readln.chomp.split.map!(to!int).array; Iku left = new Iku(); left.dist = l[1]; left.cost = l[2]; adj[l[0]] ~= left; Iku right = new Iku(); right.dist = l[0]; right.cost = l[2]; adj[l[1]] ~= right; } long[] memo = new long[n + 1]; memo[] = -1; auto l = readln.chomp.split.map!(to!int).array; int q = l[0]; int k = l[1]; bfs(k, n, adj, memo); foreach (i; 0..q) { auto m = readln.chomp.split.map!(to!int).array; int x = m[0]; int y = m[1]; long sum = memo[x] + memo[y]; writeln = sum; } }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n, m; rd(n); rd(m); auto a=new int[](n); foreach(i; 0..n) rd(a[i]); auto mx=reduce!(max)(a); int sub=0; foreach(e; a) sub+=(mx-e); if(m<=sub){ writeln(mx, " ", mx+m); return; } auto k2=mx+m; m-=sub; writeln(mx+(m+n-1)/n, " ", k2); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x) e=l[i].to!(typeof(e)); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { long n = lread(); string s = sread(); long k = lread(); char c = s[k - 1]; foreach(i;s) { if(i == c) { write(i); } else { write("*"); } } writeln(); }
D
import std.stdio, std.array, std.conv, std.string, std.algorithm, std.math; void main() { while (true) { auto input = readln.chomp.split; int a = input[0].to!int; int b = input[2].to!int; char op = input[1].to!char; if (op == '?') { break; } switch (op) { case '+': writeln(a + b); break; case '-': writeln(a - b); break; case '*': writeln(a * b); break; case '/': writeln(a / b); break; default: break; } } }
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; long MOD = 10L^^9+7; void main() { int N = readln.chomp.to!int; int[] s1 = readln.chomp.to!(char[]).map!(a => a-'A').array.to!(int[]); int[] s2 = readln.chomp.to!(char[]).map!(a => a-'A').array.to!(int[]); long ans = 1; bool flg = true; for(int i=0; i<N; i++) { long t; if (i==0) { if (s1[i]==s2[i]) { t = 3; } else { t = 3 * 2; } } else if (flg) { if (s1[i]==s2[i]) { if (s1[i-1]==s1[i]) { 0.writeln; return; } else { t = 2; } } else { if (s1[i-1]==s1[i] || s2[i-1]==s2[i]) { 0.writeln; return; } else { t = 2; } } } else { if (s1[i]==s2[i]) { if (s1[i-1]==s1[i] || s2[i-1]==s2[i]) { 0.writeln; return; } else { t = 1; } } else { if (s1[i-1]==s1[i] || s2[i-1]==s2[i]) { 0.writeln; return; } else { t = 3; } } } ans = ans*t%MOD; flg = s1[i]==s2[i]; if (!flg) i++; } ans.writeln; } // ---------------------------------------------- void times(alias fun)(int n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { auto minElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto minimum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b < minimum) { element = a; minimum = b; } } return element; } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto maximum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b > maximum) { element = a; maximum = b; } } return element; } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { int x,a,b; scan(x); scan(a); scan(b); auto ans = a + (x - a) / b * b; ans = x - ans; 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
void main() { int n = readln.chomp.to!int; int k = readln.chomp.to!int; int x = readln.chomp.to!int; int y = readln.chomp.to!int; writeln(n > k ? x * k + (n - k) * y : x * n); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
void solve(){ } void main(){ int n = inelm(); int a = inelm(); (n*n-a).writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range; const long mod = 10^^9+7; alias instr = () => readln().chomp(); T inelm(T= int)(){ return to!(T)( readln().chomp() ); } T[] inary(T = int)(){ return readln().chomp().split().to!(T[])(); }
D
import std.algorithm; import std.array; import std.ascii; import std.container; import std.conv; import std.format; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int a, b, c, d, e, f; double max_noudo = 0; int ans_suwar = 0; int ans_sugar = 0; bool[][] done; void search(int water = 0, int sugar = 0) { // stderr.writeln(water, " ", sugar, " : ", water * 100 + sugar, " ", f); // verify if (water * 100 + sugar > f) { return; } if (done[water][sugar]) { return; } if (water * e < sugar) { return; } double noudo = cast(double)(100 * sugar) / (water * 100 + sugar); if (noudo > max_noudo) { max_noudo = noudo; ans_suwar = 100 * water + sugar; ans_sugar = sugar; } // input sugar search(water, sugar + c); search(water, sugar + d); // input water search(water + a, sugar); search(water + b, sugar); done[water][sugar] = true; } void main() { auto input = readln.chomp.split.map!(to!int); a = input[0]; b = input[1]; c = input[2]; d = input[3]; e = input[4]; f = input[5]; ans_suwar = 100 * a; done = new bool[][](f / 100 + 1, f + 1); search(); writeln(ans_suwar, " ", ans_sugar); }
D
//prewritten code: https://github.com/antma/algo import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.traits; final class InputReader { private: ubyte[] p, buffer; bool eof; bool rawRead () { if (eof) { return false; } p = stdin.rawRead (buffer); if (p.empty) { eof = true; return false; } return true; } ubyte nextByte(bool check) () { static if (check) { if (p.empty) { if (!rawRead ()) { return 0; } } } auto r = p.front; p.popFront (); return r; } public: this () { buffer = uninitializedArray!(ubyte[])(16<<20); } bool seekByte (in ubyte lo) { while (true) { p = p.find! (c => c >= lo); if (!p.empty) { return false; } if (!rawRead ()) { return true; } } } template next(T) if (isSigned!T) { T next () { if (seekByte (45)) { return 0; } T res; ubyte b = nextByte!false (); if (b == 45) { while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 - (b - 48); } } else { res = b - 48; while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 + (b - 48); } } } } template next(T) if (isUnsigned!T) { T next () { if (seekByte (48)) { return 0; } T res = nextByte!false () - 48; while (true) { ubyte b = nextByte!true (); if (b < 48 || b >= 58) { break; } res = res * 10 + (b - 48); } return res; } } T[] nextA(T) (in int n) { auto a = uninitializedArray!(T[]) (n); foreach (i; 0 .. n) { a[i] = next!T; } return a; } } int ceildiv (in int x, in int y) { return (x + y - 1) / y; } bool test (const int n, const int d) { if (n >= d) return true; for (int x = 1; x < 200_000 ; ++x) { int y = x + ceildiv (d, x + 1); if (y <= n) return true; } return false; } void main() { auto r = new InputReader (); immutable nt = r.next!uint (); foreach (tid; 0 .. nt) { int n = r.next!int; int d = r.next!int; writeln (test (n, d) ? "YES" : "NO"); } }
D
import std.stdio,std.string,std.conv,std.array,std.algorithm; void main(){ for(;;){ auto rcs = readln().chomp().split(); auto scorelist = [to!int(rcs[0]),to!int(rcs[1]),to!int(rcs[2])]; if( scorelist[0]==-1 && scorelist[1]==-1 && scorelist[2]==-1 ){ break; } if( scorelist[0]==-1 || scorelist[1]==-1 ){ writeln("F"); } else if( 80 <= scorelist[0]+scorelist[1] ){ writeln("A"); } else if ( 65 <= scorelist[0]+scorelist[1] ) { writeln("B"); } else if ( 50 <= scorelist[0]+scorelist[1] ) { writeln("C"); } else if ( 30 <= scorelist[0]+scorelist[1] ) { if( 50 <= scorelist[2] ){ writeln("C"); } else{ writeln("D"); } } else { writeln("F"); } } }
D
import std.stdio, std.string, std.conv; import std.typecons; import std.algorithm, std.array, std.range, std.container; void main() { auto data = readln.split; auto N = data[0].to!long, X = data[1].to!long; long height(long level) { return 2^^(level+2) - 3; } long p_num(long level) { return 2^^(level+1) - 1; } long solve(long level, long x) { if (level == 0 || x == 0) return x; auto h = height(level-1); if (x <= h+1) return solve(level-1, x-1); else if (x <= 2*h+2) return p_num(level-1) + 1 + solve(level-1, x-h-2); else return p_num(level); } writeln(solve(N, X)); }
D
import std.stdio; import std.ascii; import std.algorithm; import core.stdc.stdio; int main() { int t = readInt!int; foreach(ti; 0 .. t) { string str = readString; auto a = new int[](str.length); foreach(i, ref ai; a) switch(str[i]) { case '0': ai = 0 ^ (i & 1); continue; case '1': ai = 1 ^ (i & 1); continue; default: ai = -1; continue; } debug writeln(a); int c0 = 0; int c1 = 0; long result = 0; foreach(i, ai; a) { if (ai == 0) c0++, c1 = 0; if (ai == 1) c1++, c0 = 0; if (ai == -1) c0++, c1++; if (ai == 0) result += c0; if (ai == 1) result += c1; if (ai == -1) result += max(c0, c1); } result.writeln; } return 0; } /* INPUT ROUTINES */ int currChar; static this() { currChar = getchar(); } char topChar() { return cast(char) currChar; } void popChar() { currChar = getchar(); } auto readInt(T)() { T num = 0; int sgn = 0; while (topChar.isWhite) popChar; while (topChar == '-' || topChar == '+') { sgn ^= (topChar == '-'); popChar; } while (topChar.isDigit) { num *= 10; num += (topChar - '0'); popChar; } if (sgn) return -num; return num; } string readString() { string res = ""; while (topChar.isWhite) popChar; while (!topChar.isWhite) { res ~= topChar; popChar; } return res; } /**MODULAR SYSTEM*/ struct Z(immutable long m) { long rep; this(long num) { rep = num; } Z!m opBinary(string operator)(Z!m rhs) { static if (operator == "+") { long result = rhs.rep + this.rep; if (result >= m) result -= m; return Z!m(result); } else static if (operator == "-") { long result = this.rep - rhs.rep; if (result < 0) result += m; return Z!m(result); } else static if (operator == "*") { long result = this.rep * rhs.rep; if (result >= m) result %= m; return Z!m(result); } else static assert(text("Operator ", operator, " not supported")); } Z!m opBinary(string operator)(long exponent) if (operator == "^^") { assert(exponent >= 0); Z!m base = this; Z!m result = 1; while (exponent) { if (exponent & 1) result = result * base; base = base * base; exponent >>= 1; } return result; } invariant { assert(rep >= 0 && rep < m); } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; // dfmt on void main() { long H, W; scan(H, W); long cnt; foreach (i; 0 .. H) { auto A = sread(); foreach (c; A) { cnt += c == '#'; } } writeln((cnt == H + W - 1) ? "Possible" : "Impossible"); }
D
import std.stdio, std.math, std.algorithm, std.array, std.string, std.conv, std.container, std.range; T[] Reads(T)() { return readln.split.to!(T[]); } alias reads = Reads!int; pragma(inline, true) void scan(Args...)(ref Args args) { string[] ss = readln.split; foreach (i, ref arg ; args) arg = ss[i].parse!int; } enum MOD = 10^^9 + 7; pragma(inline, true) int mul(long x, long y) { return (x * y) % MOD; } int[] f; void make_table(int n) { f = new int[n + 1]; f[0] = f[1] = 1; foreach (i ; 2..n+1) f[i] = mul(i, f[i-1]); } int powmod(S,T,U)(S x, T n, U MOD) { int r = 1; while (n > 0) { if (n & 1) r = mul(r, x); x = mul(x, x); n >>= 1; } return r; } pragma(inline, true) int mod_inv(int x) { return powmod(x, MOD - 2, MOD); } pragma(inline, true) int comb(int n, int r) { if (n - r < 0 || n < 0 || r < 0) return 0; return mul(mul(f[n], f[r].mod_inv), f[n - r].mod_inv); } void main() { int n, k; scan(n, k); make_table(2010); foreach (i; 1..k+1) { int x = mul(comb(n-k+1, i), comb(k-1, i-1)); writeln(x); } }
D
import std.stdio, std.string, std.conv; import std.typecons; import std.algorithm, std.array, std.range, std.container; import std.math; void main() { auto data = readln.split; auto A = data[0].to!long, B = data[1].to!long; long ans; foreach (i; 1 .. 41) { immutable power = 2L^^i; if (B < power) break; auto A_i = A - (A/power) * power, B_i = B - (B/power) * power; auto a = (A & power) != 0 ? 1 : 0, b = (B & power) != 0 ? 1 : 0; //writeln(A_i, B_i); if (A - A_i + power <= B - B_i) { //writeln("here1 ", ans); ( (power-A_i)*a + (B_i+1)*b ) % 2 == 0 ? 0 : (ans += power); //writeln(ans); } else { //writeln("here2 ", ans); if ((A & power) != 0) (B-A+1) % 2 == 0 ? 0 : (ans += power); //writeln(ans); } } B = B - A/2 * 2, A = A - A/2 * 2; //writeln(A, ", , ", B); if (A == 1) { if (B%2 == 0) { if (B/2 % 2 == 1) ans++; } else if ((B-1)%4 != 0) {} else { ans++; } } else if (A == 0) { if (B % 2 == 1 ) { if ((B-1)%4 == 0) ans++; } else if (B%4 == 0) {} else { ans++; } } writeln(ans); }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; import std.typecons; void main() { auto a = read.to!int; auto b = read.to!int; if (a + b == 15) writeln("+"); else if (a * b == 15) writeln("*"); else writeln("x"); } string read() { static string[] ss; while (!ss.length) ss = readln.chomp.split; auto res = ss[0]; ss.popFront; return res; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { auto ab = readints; int a = ab[0], b = ab[1]; int ans = a * b - a - b + 1; writeln(ans); }
D
import std.stdio; import std.algorithm; import std.string; import std.range; import std.array; import std.conv; import std.complex; import std.math; import std.ascii; import std.bigint; import std.container; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } const real eps = 1e-10; void main(){ auto notPrime = new bool[](123456*2+1); notPrime[0] = notPrime[1] = true; for(int i; i < notPrime.length; ++i) { if(!notPrime[i]) { for(int j = 2; i*j < notPrime.length; ++j) { notPrime[i*j] = true; } } } while(true) { auto n = readInt(); if(n == 0) { return; } int ans; for(int i = n+1; i <= 2*n; ++i) { if(!notPrime[i]) { ++ans; } } writeln(ans); } }
D
// Vicfred // https://atcoder.jp/contests/abc160/tasks/abc160_b import std.conv; import std.stdio; import std.string; void main() { int n = readln.chomp.to!int; long ans = (n/500)*1000; n -= (n/500)*500; ans += (n/5)*5; ans.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 a=scanElem; auto o=scanString; auto b=scanElem; if(o=="+")end(a+b);end(a-b); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { readln(); string s = sread(); long[] l = s.map!(c => c == 'I' ? 1 : -1) .array .to!(long[]); l = 0 ~ l; long m; foreach (i; 1 .. l.length) { l[i] += l[i - 1]; m = max(m, l[i]); } writeln(m); }
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!double; writeln(sqrt(N).to!int^^2); }
D
void main() { long n = rdElem; long[] p = rdRow; long cnt; long pmax = p[0]; foreach (x; p) { if (x <= pmax) ++cnt; pmax = min(pmax, x); } cnt.writeln; } T rdElem(T = long)() { //import std.stdio : readln; //import std.string : chomp; //import std.conv : to; return readln.chomp.to!T; } alias rdStr = rdElem!string; dchar[] rdDchar() { //import std.conv : to; return rdStr.to!(dchar[]); } T[] rdRow(T = long)() { //import std.stdio : readln; //import std.array : split; //import std.conv : to; return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdRow!T).array; } void wrMat(T = long)(T[][] mat) { //import std.stdio : write, writeln; foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new char[][](t); foreach (ti; 0..t) { auto n = RD!int; auto k = RD!int; foreach (i; 0..k) ans[ti] ~= 'a'; while (ans[ti].length < n) { ans[ti] ~= "cba"; } ans[ti].length = n; } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } 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 = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto T = RD!int; auto ans = new long[](T); foreach (i; 0..T) { auto s = RD; auto t = RD; auto e = RD; auto a = (s + t + e) / 2 + 1; a = max(a, s); ans[i] = (s+e) < a ? 0 : s + e - a + 1; } foreach (e; ans) writeln(e); stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD!int; auto M = RD!int; auto x = RDR.ARR; auto p = RDR.ARR; long g = x[1] - x[0]; foreach (i; 2..N) { g = gcd(g, x[i] - x[i-1]); } long ans; foreach (i; 0..M) { if (g % p[i] == 0) { ans = i+1; break; } } if (ans == 0) writeln("NO"); else { writeln("YES"); writeln(x[0], " ", 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(); } bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); } void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ 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 solve(){ foreach(_; 0 .. scan!int){ int n = scan!int; if(n == 1) "-1".writeln; else{ if(n % 9 == 1){ foreach(i; 0 .. n - 2) "5".write; "99".writeln; } else{ foreach(i; 0 .. n - 1) "5".write; "9".writeln; } } } }
D
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } // floor(a^(1/k)) ulong floorKthRoot(ulong a, ulong k) { import core.bitop : bsr; import std.algorithm : min; if (a == 0) { return 0; } else if (k <= 1) { return a; } else if (k == 2) { ulong b = a, x = 0, y = 0; for (int e = bsr(a) & ~1; e >= 0; e -= 2) { x <<= 1; y <<= 1; if (b >= (y | 1) << e) { b -= (y | 1) << e; x |= 1; y += 2; } } return x; } else if (k <= 40) { // min x s.t. x^k >= 2^64 enum ulong[] HIS = [0, 0, 4294967296UL, 2642246, 65536, 7132, 1626, 566, 256, 139, 85, 57, 41, 31, 24, 20, 16, 14, 12, 11, 10, 9, 8, 7, 7, 6, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4]; ulong lo = 1UL << (bsr(a) / k); ulong hi = min(1UL << (bsr(a) / k + 1), HIS[cast(size_t)(k)]); for (; lo + 1 < hi; ) { const ulong mid = (lo + hi) / 2; ulong b = mid * mid; foreach (i; 2 .. k) b *= mid; ((b <= a) ? lo : hi) = mid; } return lo; } else if (k <= 63) { return ((1UL << k) <= a) ? 2 : 1; } else { return 1; } } enum S = "codeforces"; enum L = cast(int)(S.length); void main() { try { for (; ; ) { const K = readLong(); const long a = floorKthRoot(K, L); foreach (m; 0 .. L + 1) { long prod = 1; foreach (i; 0 .. L) { prod *= ((i < m) ? (a + 1) : a); } if (prod >= K) { foreach (i; 0 .. L) { foreach (_; 0 .. ((i < m) ? (a + 1) : a)) { write(S[i]); } } writeln(); break; } } } } catch (EOFException e) { } }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { int n, m; char[] s; scan(n, m); scan(s); foreach (i ; 0 .. m) { int l, r; char c1, c2; scan(l, r, c1, c2); l--, r--; foreach (j ; l .. r + 1) { if (s[j] == c1) s[j] = c2; } } writeln(s); } long revnum(long n) { long res; while (n > 0) { res *= 10; res += (n % 10); n /= 10; } return res; } unittest { assert(revnum(0) == 0); assert(revnum(8) == 8); assert(revnum(10) == 1); assert(revnum(123) == 321); assert(revnum(10100) == 101); writeln("ok"); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto x = readln.strip.to !(int); int step = 0; int pos = 0; while (pos < x) { step += 1; pos += step; } writeln (step + (pos - x == 1)); } }
D
void main() { problem(); } void problem() { const N = scan!long; long solve() { long ans; foreach(i; 1..N+1) { if (i % 3 != 0 && i % 5 != 0) ans += i; } 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; 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
/+ dub.sdl: name "B" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; int main() { Scanner sc = new Scanner(stdin); int n, m, x; sc.read(n, m, x); foreach (i; 0..n+1) { foreach (j; 0..m+1) { int k = n-i; int l = m-j; if (i*l + k*j == x) { writeln("Yes"); return 0; } } } writeln("No"); return 0; } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; if (f.eof) return false; line = lineBuf[]; f.readln(line); } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } version (X86) static if (__VERSION__ < 2071) { import core.bitop : bsf, bsr, popcnt; int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } }
D
import std.stdio, std.string, std.array, std.conv, std.numeric; int arrayLCM(int[] x) { while (x.length > 1) { int a = x[0]; int b = x[1]; int r = gcd(a, b); x[1] = a * b / r; x = x[1..$]; } return x[0]; } void main() { int n = readln.chomp.to!int; int[] a = readln.chomp.split.to!(int[]); arrayLCM(a).writeln; }
D
import std.stdio, std.conv, std.algorithm, std.range, std.string, std.numeric; void main() { size_t a,b; auto n = readln.chomp.to!size_t - 1; auto ary = readln.chomp.split.to!(size_t[]); a = ary[0]; b = ary[1]; foreach(cnt;0..n){ auto tmp = readln.chomp.split.to!(size_t[]); auto inp_x = tmp[0]; auto inp_y = tmp[1]; immutable ratio = max((a-1)/inp_x+1,(b-1)/inp_y+1); a = inp_x * ratio; b = inp_y * ratio; } writeln(a+b); }
D
import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; void main(){ string s = readln.chomp; int ans; char x = 'a'; foreach(c; s){ if(x != 'a') if(x != c) ans += 1; x = c; } ans.writeln; }
D
import std.stdio, std.string, std.conv, std.array, std.algorithm; void main() { int count; while (true) { auto x = readln.chomp; if (x == "0") break; writeln("Case " ~ to!string(++count) ~ ": " ~ x); } }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; auto s = readln.chomp.to!(char[]); auto n = s.length; auto a = new long[](n + 1), c = new long[](n + 1), q = new long[](n + 1); foreach (i; 0 .. n) { a[i + 1] += a[i]; c[i + 1] += c[i]; q[i + 1] += q[i]; if (s[i] == 'A') a[i + 1]++; if (s[i] == 'C') c[i + 1]++; if (s[i] == '?') q[i + 1]++; } long mod = 10 ^^ 9 + 7; long powmod(long b, long ex) { if (ex == 0) return 1L; if (ex & 1) return b * powmod(b, ex - 1) % mod; else return powmod(b * b % mod, ex / 2); } // (A, C), (?, C), (C, ?), (?, ?) long tot = 0; foreach (i; 0 .. n) { if (s[i] == 'B' || s[i] == '?') { if (i > 0 && i + 1 < n) { (tot += a[i] * (c[n] - c[i + 1]) % mod * powmod(3, q[i] + (q[n] - q[i + 1]))) %= mod; if (q[i] > 0) { (tot += q[i] * (c[n] - c[i + 1]) % mod * powmod(3, q[i] + (q[n] - q[i + 1]) - 1)) %= mod; } if (q[n] - q[i + 1] > 0) { (tot += a[i] * (q[n] - q[i + 1]) % mod * powmod(3, q[i] + (q[n] - q[i + 1]) - 1)) %= mod; } if (q[i] > 0 && (q[n] - q[i + 1]) > 0) { (tot += q[i] * (q[n] - q[i + 1]) % mod * powmod(3, q[i] + (q[n] - q[i + 1]) - 2)) %= mod; } } } } writeln(tot); } void rd(T...)(ref T x) { import std.stdio : readln; import std.string : split; import std.conv : to; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
D
import std.algorithm; import std.array; import std.bigint; import std.bitmanip; import std.conv; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; } T[] readToArray(T)() { return readln.split.to!(T[]); } void readInto(T...)(ref T ts) { auto ss = readln.split; foreach(ref t; ts) { t = ss.front.to!(typeof(t)); ss.popFront; } } // 冪乗をmod取りつつ計算 @nogc @safe pure ulong modPow(ulong a, ulong n, ulong m) { ulong r = 1; while (n > 0) { if(n % 2 != 0) r = r * a % m; a = a * a % m; n /= 2; } return r; } // フェルマーの小定理から乗法逆元を計算 // 定理の要請により法は素数 @nogc @safe pure ulong modInv(ulong a, ulong m) { return modPow(a, m-2, m); } // mod取りつつ順列を計算 @nogc @safe pure ulong modPerm(ulong n, ulong k, ulong m) { if (n < k) return 0; ulong r = 1; for (ulong i = n-k+1; i <= n; i++) { r *= i; r %= m; } return r; } // mod取りつつ順列を計算 @nogc @safe pure ulong modFact(ulong n, ulong m) { return modPerm(n, n, m); } // mod取りつつ組み合わせを計算 // modInvを使っているので法は素数 @nogc @safe pure ulong modComb(ulong n, ulong r, ulong m) { return modPerm(n, r, m)*modInv(modFact(r, m), m) % m; } immutable ulong MOD = 1000000007; void main() { ulong r; readInto(r); writeln(3*r*r); }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std; // dfmt on void main() { long N = lread(); auto A = aryread(); auto B = aryread(); if (A == B) { writeln("Yes"); return; } long s; foreach (i; 0 .. N) s += max(0, A[i] - B[i]); long t; foreach (i; 0 .. N) { long x = max(0, B[i] - A[i]); t += x / 2; } writeln(s <= t ? "Yes" : "No"); }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ int x = read.to!int; int a = read.to!int; int ans; if(x < a) ans = 0; else ans = 10; 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) { y.modpow(mod - 2); x.modm(y); } void main() { auto X = RDA; auto pos = X.MIN_POS(); writeln(pos+1); stdout.flush; debug readln; }
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() { auto S = sread(); writeln((S[0] == S[1] && S[1] == S[2]) ? "No" : "Yes"); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}} void main() { int n; readV(n); string[] w; readC(n, w); auto h = [w[0]: true]; foreach (i; 1..n) { if (w[i][0] != w[i-1][$-1] || w[i] in h) { writeln("No"); return; } h[w[i]] = true; } writeln("Yes"); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons; import std.math, std.numeric; void main() { readln.chomp.count!"a == '1'".writeln; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int N; scan(N); auto x = readln.split.to!(long[]); long ans = infl; foreach (p ; 0 .. 100 + 1) { long t; foreach (i ; 0 .. N) { t += (x[i] - p)^^2; } chmin(ans, t); } writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.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 t = RD!int; auto ans = new int[](t); foreach (ti; 0..t) { auto n = RD!int; auto k = RD!int; auto s = RD!string; int len = n / k; int l, r = k-1; while (l <= r) { auto cnt = new int[](26); foreach (i; 0..len) { auto c1 = s[i*k+l] - 'a'; auto c2 = s[i*k+r] - 'a'; ++cnt[c1]; ++cnt[c2]; } int x; foreach (i; 0..26) { x.chmax(cnt[i]); } if (l == r) ans[ti] += len - x / 2; else ans[ti] += len * 2 - x; debug writeln("ans:", ans[ti]); ++l; --r; } } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int a, p; scan(a, p); auto ans = (3*a + p) / 2; writeln(ans); } int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new int[][](n, 0); foreach (i; 0 .. m) { int u, v; scan(u, v); if (is1indexed) { u--, v--; } adj[u] ~= v; if (isUndirected) { adj[v] ~= u; } } return adj; } alias Edge = Tuple!(int, "to", int, "cost"); Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new Edge[][](n, 0); foreach (i; 0 .. m) { int u, v, c; scan(u, v, c); if (is1indexed) { u--, v--; } adj[u] ~= Edge(v, c); if (isUndirected) { adj[v] ~= Edge(u, c); } } return adj; } void yes(bool b) { writeln(b ? "Yes" : "No"); } void YES(bool b) { writeln(b ? "YES" : "NO"); } T[] readArr(T)() { return readln.split.to!(T[]); } T[] readArrByLines(T)(int n) { return iota(n).map!(i => readln.chomp.to!T).array; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.algorithm, std.string, std.conv, std.array, std.range, std.math; int read() { return readln.chomp.to!int; } int[] reads() { return readln.split.to!(int[]); } void main() { int n = read(); int[] a = reads(); int ans = -114514810; foreach (i; 0..n) foreach (j; 0..n) { if (i == j) continue; ans = max(ans, abs(a[i] - a[j])); } writeln(ans); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto A = RD; auto B = RD; auto ans1 = A + B; auto ans2 = A - B; auto ans3 = A * B; writeln(max(ans1, max(ans2, ans3))); stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto N = RD; auto M = RD; writeln(N*(N-1)/2 + M*(M-1)/2); stdout.flush; debug readln; }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; immutable long mod = 10^^9 + 7; void main() { int n; scan(n); auto ps = sieveEratos(n + 1); auto cnt = new int[](n + 1); foreach (m ; 2 .. n + 1) { foreach (pi ; ps) { if (pi*pi > m) break; while (m % pi == 0) { cnt[pi]++; m /= pi; } } if (m > 1) cnt[m]++; } long ans = 1; foreach (cnti ; cnt) { (ans *= (cnti + 1)) %= mod; } writeln(ans); } int[] sieveEratos(int N) { auto s = new bool[](N); s[] = 1; s[0] = s[1] = 0; for (int p = 2; p*p < N; p++) { if (s[p]) { for (int q = p*p; q < N; q += p) { s[q] = 0; } } } return iota(N).filter!(i => s[i]).array; } 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 core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; alias Score = Tuple!(long, "p", long, "c"); 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 d, g; scan(d, g); auto scores = new Score[](d); foreach (ref e; scores) { long r, c; scan(r, c); e = Score(r, c); } auto m = INF; foreach (i; iota(1 << d)) { long p, plus, moves; auto b = i.bitary(d); foreach (j; iota(b.length)) { if (b[j]) { p += scores[j].p * 100 * (j + 1); p += scores[j].c; moves += scores[j].p; } else plus = j; } long n = scores[plus].p; while (n > 1 && p < g) { p += 100 * (plus + 1); moves++, n--; } // moves.writeln(); if (p >= g) m = min(moves, m); } m.writeln(); } auto bitary(long n, long m) { auto b = new long[](m); long i; while (n > 0) { b[i++] = n % 2; n = n >> 1; } b.reverse(); return b; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; void main() { int d; scan(d); write("Christmas "); foreach (i ; 0 .. 25 - d) { write("Eve "); } writeln; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; // dfmt on void main() { long N, P; scan(N, P); auto A = aryread(); auto dp = new long[][](N + 1, 2); dp[0][0] = 1; foreach (i; 0 .. N) { dp[i + 1][0] = dp[i][0]; dp[i + 1][1] = dp[i][1]; if (A[i] & 1) { dp[i + 1][0] += dp[i][1]; dp[i + 1][1] += dp[i][0]; } else { dp[i + 1][0] += dp[i][0]; dp[i + 1][1] += dp[i][1]; } } writeln(dp[N][P]); }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto a = readln.split.to!(long[]); // 3 2 4 1 2 // 0 3 5 9 10 12 auto acc = new long[](n + 1); foreach (i; 0 .. n) acc[i + 1] = acc[i] + a[i]; long inf = 1_000_000_000_000_000_000; auto mn = inf; for (int i = 0, j = 2, k = 2; j <= n - 2; j++) { // (0,i] (i,j] (j,k] (k,n] while (i < j && acc[i] <= acc[j] - acc[i]) { i++; } while (k < n && acc[k] - acc[j] <= acc[n] - acc[k]) { k++; } for (int x = 0; x <= 1; x++) { for (int y = 0; y <= 1; y++) { auto min_val = min(acc[i - x], acc[j] - acc[i - x], acc[k - y] - acc[j], acc[n] - acc[k - y]); auto max_val = max(acc[i - x], acc[j] - acc[i - x], acc[k - y] - acc[j], acc[n] - acc[k - y]); mn = min(mn, max_val - min_val); } } } writeln(mn); } 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
void main(){ import std.stdio, std.conv, std.string, std.algorithm; auto s=readln.chomp; int n=s.length.to!int; auto ps=new int[][](n, 26); foreach(ref e; ps) fill(e, -1); auto bar=new int[](26); fill(bar, -1); foreach_reverse(i; 0..n){ bar[s[i]-'a']=i; foreach(j; 0..26){ if(bar[j]!=-1){ ps[i][j]=bar[j]; } } } auto dp=new int[](n); dp~=1; foreach_reverse(i; 0..n){ bool ok=false; int dpdp=n+1; foreach(j; 0..26){ if(ps[i][j]==-1){ dp[i]=1; ok=true; break; }else{ dpdp=min(dpdp, dp[ps[i][j]+1]); } } if(ok) continue; assert(dpdp<n+1); dp[i]=dpdp+1; } // writeln(dp); string t; int k=0; foreach(i; 0..dp[0]){ foreach(j; 0..26){ if(ps[k][j]==-1){ assert(dp[k]==1); t~=(j+'a').to!char; break; }else{ if(dp[k]==dp[ps[k][j]+1]+1){ k=ps[k][j]+1; t~=(j+'a').to!char; break; } } } } writeln(t); }
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 = N.iota.map!(_ => readln.split.map!(to!int).array).array; auto B = N.iota.map!(_ => readln.split.map!(to!int).array).array; int source = N*2; int sink = N*2+1; auto FF = new FordFulkerson(N*2+2, source, sink); foreach (i; 0..N) FF.add_edge(source, i, 1); foreach (i; 0..N) FF.add_edge(N+i, sink, 1); foreach (i; 0..N) foreach (j; 0..N) if (A[i][0] < B[j][0] && A[i][1] < B[j][1]) FF.add_edge(i, N+j, 1); FF.run.writeln; } class FordFulkerson { int N, source, sink; int[][] adj; int[][] flow; bool[] used; this(int n, int s, int t) { N = n; source = s; sink = t; assert (s >= 0 && s < N && t >= 0 && t < N); adj = new int[][](N); flow = new int[][](N, N); used = new bool[](N); } void add_edge(int from, int to, int cap) { adj[from] ~= to; adj[to] ~= from; flow[from][to] = cap; } int dfs(int v, int min_cap) { if (v == sink) return min_cap; if (used[v]) return 0; used[v] = true; foreach (to; adj[v]) { if (!used[to] && flow[v][to] > 0) { auto bottleneck = dfs(to, min(min_cap, flow[v][to])); if (bottleneck == 0) continue; flow[v][to] -= bottleneck; flow[to][v] += bottleneck; return bottleneck; } } return 0; } int run() { int ret = 0; while (true) { foreach (i; 0..N) used[i] = false; int f = dfs(source, int.max); if (f > 0) ret += f; else return ret; } } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; void main() { string s = readln.strip; foreach (i; 0 .. 26) { auto c = (i + 97).to!(char); if (s.find (c).empty) { writeln (s, c); return; } } int l = s.length.to!(int); auto a = s.map! (c => c.to!(int) - 97).array; string res = "~"; foreach (i; 0 .. l) { foreach (j; 0 .. 26) { if (a[0 .. i].find (j).empty && j > a[i]) { auto t = s[0 .. i] ~ (j + 97).to!(char); if (res > t) { res = t; } } } } if (res == "~") { res = "-1"; } writeln (res); }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std; // dfmt on void main() { long N = lread(); writeln((N + 1) / 2); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); void main() { long n, m; scan(n, m); // writeln(m); auto a = aryread(); // writeln(a); long sum_x; sum_x = sum(a); // writeln(sum_x); long cnt; foreach (i; 0 .. n) { if (sum_x <= a[i] * (4 * m)) { cnt += 1; } } // writeln(cnt); if (cnt >= m) { writeln("Yes"); } else { writeln("No"); } } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string; void main() { long[][][] MEMO; MEMO.length = 51; foreach (ref memo1; MEMO) { memo1.length = 51; foreach (ref memo2; memo1) { memo2.length = (50*50+1); foreach (ref memo3; memo2) { memo3 = -1; } } } auto na = readln.chomp.split(" ").map!(to!int).array; auto n = na[0]; auto a = na[1]; auto xs = readln.chomp.split(" ").map!(to!int).array; long solve(int i, int num, int sum) { if (MEMO[i][num][sum] != -1) return MEMO[i][num][sum]; MEMO[i][num][sum] = i == n ? (sum != 0 && sum == num*a ? 1 : 0) : (solve(i+1, num, sum) + solve(i+1, num+1, sum+xs[i])); return MEMO[i][num][sum]; } writeln(solve(0, 0, 0)); }
D
void main() { long n, m; rdVals(n, m); long[] num = new long[n]; num[] = -1; foreach (i; 0 .. m) { long s, c; rdVals(s, c); --s; if (num[s] == -1 || num[s] == c) num[s] = c; else { writeln(-1); return; } } if (n >= 2 && num[0] == 0) { writeln(-1); return; } if (num[0] == -1) { if (n == 1) num[0] = 0; else num[0] = 1; } foreach (x; num) { if (x == - 1) 0.write; else x.write; } writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { long N = lread(); auto S = sread(); long m = long.max; auto sum = new long[][](2, N + 1); foreach (i; 0 .. N) sum[0][i + 1] = sum[0][i] + (0 ^^ (S[i] != 'W')); foreach (i; 0 .. N) sum[1][i + 1] = sum[1][i] + (0 ^^ (S[i] != 'E')); iota(N).map!(x => (sum[0][x] - sum[0][0]) + (sum[1][N] - sum[1][x + 1])) .reduce!min().writeln(); }
D
void main() { long n = readln.chomp.to!long; string digits = "0357"; long cnt; foreach (i; 0 .. 1000000) { long x = i; long len; string nums; while (x) { ++len; nums ~= digits[x%4]; x /= 4; } long num; foreach_reverse (j, c; nums) { num += (c - '0') * 10 ^^ j; } if (num > n) break; if (!nums.canFind('0') && nums.canFind('3') && nums.canFind('5') && nums.canFind('7')) ++cnt; } cnt.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
void main(){ string s = readln().chomp(); string outs; foreach(i; 0..s.length){ outs ~= "x"; } outs.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.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto np = readln.split.to!(long[]); auto N = np[0]; auto P = np[1]; if (N == 1) { writeln(P); return; } long r = 1; auto p = P; for (long x = 2; x^^2 <= P; ++x) { long y; while (p%x == 0) { ++y; p /= x; } r *= x^^(y/N); } writeln(r); }
D
import std.conv, std.stdio; import std.algorithm, std.array, std.range, std.string; import std.functional; void main() { auto n = readln.chomp.to!int; long answer; foreach (a; 1..n+1) foreach (b; 1..n+1) { immutable g = gcd(a, b); foreach (c; 1..n+1) answer += gcd(g, c); } answer.writeln; } alias gcd = memoize!_gcd; long _gcd(in long a, in long b) { if (a == 0) return b; if (b == 0) return a; if (a & b & 1) return gcd(max(a, b) - min(a, b), min(a, b)); if (!((a | b) & 1)) return gcd(a >> 1, b >> 1) << 1; if (a & 1) return gcd(a, b >> 1); if (b & 1) return gcd(a >> 1, b); assert (false); } unittest { assert (gcd(0, 0) == 0); assert (gcd(0, 1) == 1); assert (gcd(0, 2) == 2); assert (gcd(1, 0) == 1); assert (gcd(1, 1) == 1); assert (gcd(1, 2) == 1); assert (gcd(2, 0) == 2); assert (gcd(2, 1) == 1); assert (gcd(2, 2) == 2); assert (gcd(20, 30) == 10); assert (gcd(16, 24) == 8); }
D
import std.stdio, std.algorithm, std.string, std.conv, std.array, std.range, std.math; int read() { return readln.chomp.to!int; } int[] reads() { return readln.split.to!(int[]); } void main() { int n = read(); int[] a = reads(); long f(long b) { return iota(n) .map!(i => abs(a[i] - (b + i+1))) .sum; } enum N = 10^^10 + 1; long lb = -N, rb = N; while (rb - lb > 2) { long m1 = (lb+lb+rb) / 3; long m2 = (lb+rb+rb) / 3; if (f(m1) >= f(m2)) lb = m1; else rb = m2; } assert(rb - lb == 2); debug { writeln(lb, " ", rb); } long mid = lb + (rb - lb) / 2; writeln(f(mid)); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto A = RD; auto B = RD; writeln(A+B >= 10 ? "error" : (A+B).to!string); stdout.flush(); debug readln(); }
D
import std.stdio; import std.range; import std.array; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.container; import std.typecons; import std.random; import std.csv; import std.regex; import std.math; import core.time; import std.ascii; import std.digest.sha; import std.outbuffer; import std.numeric; import std.bigint; import core.checkedint; import core.bitop; import std.digest.sha; void main() { int n = readln.chomp.to!int; bool[] done = new bool[](n + 1); done[0] = true; foreach (d; [4, 7]) { foreach (i; 0..n - d + 1) { done[i + d] |= done[i]; } } writeln = done[n] ? "Yes" : "No"; } void tie(R, Args...)(R arr, ref Args args) if (isRandomAccessRange!R || isArray!R) in { assert (arr.length == args.length); } body { foreach (i, ref v; args) { alias T = typeof(v); v = arr[i].to!T; } } void verbose(Args...)(in Args args) { stderr.write("["); foreach (i, ref v; args) { if (i) stderr.write(", "); stderr.write(v); } stderr.writeln("]"); }
D
//prewritten code: https://github.com/antma/algo import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.traits; final class InputReader { private: ubyte[] p, buffer; bool eof; bool rawRead () { if (eof) { return false; } p = stdin.rawRead (buffer); if (p.empty) { eof = true; return false; } return true; } ubyte nextByte(bool check) () { static if (check) { if (p.empty) { if (!rawRead ()) { return 0; } } } auto r = p.front; p.popFront (); return r; } public: this () { buffer = uninitializedArray!(ubyte[])(16<<20); } bool seekByte (in ubyte lo) { while (true) { p = p.find! (c => c >= lo); if (!p.empty) { return false; } if (!rawRead ()) { return true; } } } template next(T) if (isSigned!T) { T next () { if (seekByte (45)) { return 0; } T res; ubyte b = nextByte!false (); if (b == 45) { while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 - (b - 48); } } else { res = b - 48; while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 + (b - 48); } } } } template next(T) if (isUnsigned!T) { T next () { if (seekByte (48)) { return 0; } T res = nextByte!false () - 48; while (true) { ubyte b = nextByte!true (); if (b < 48 || b >= 58) { break; } res = res * 10 + (b - 48); } return res; } } T[] nextA(T) (in int n) { auto a = uninitializedArray!(T[]) (n); foreach (i; 0 .. n) { a[i] = next!T; } return a; } } bool test (in ulong[] a, uint k) { debug stderr.writeln (a); bool[int] m; foreach (x; a) { ulong y = x; int o; debug stderr.writeln ("x = ", x); while (y > 0) { uint t = (y % k).to!uint; debug stderr.writeln (y, ' ', t); if (t > 1) return false; if (t == 1) { if (o in m) return false; m[o] = true; } y /= k; ++o; } } return true; } void main() { auto r = new InputReader (); immutable nt = r.next!uint (); foreach (tid; 0 .. nt) { const n = r.next!uint; const k = r.next!uint; auto a = r.nextA!ulong (n); writeln (test (a, k) ? "YES" : "NO"); } }
D
// import chie template :) {{{ static if (__VERSION__ < 2090) { import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons, std.format, std.bitmanip, std.numeric; } else { import std; } // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan(T...)(ref T args) { foreach (ref arg; args) { arg = next!(typeof(arg)); } } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } // }}} // ModInt {{{ struct ModInt(ulong modulus) { import std.traits : isIntegral, isBoolean; import std.exception : enforce; private { ulong val; } this(const ulong n) { val = n % modulus; } this(const ModInt n) { val = n.value; } @property { ref inout(ulong) value() inout { return val; } } T opCast(T)() { static if (isIntegral!T) { return cast(T)(val); } else if (isBoolean!T) { return val != 0; } else { enforce(false, "cannot cast from " ~ this.stringof ~ " to " ~ T.stringof ~ "."); } } ModInt opAssign(const ulong n) { val = n % modulus; return this; } ModInt opOpAssign(string op)(ModInt rhs) { static if (op == "+") { val += rhs.value; if (val >= modulus) { val -= modulus; } } else if (op == "-") { if (val < rhs.value) { val += modulus; } val -= rhs.value; } else if (op == "*") { val = val * rhs.value % modulus; } else if (op == "/") { this *= rhs.inv; } else if (op == "^^") { ModInt res = 1; ModInt t = this; while (rhs.value > 0) { if (rhs.value % 2 != 0) { res *= t; } t *= t; rhs /= 2; } this = res; } else { enforce(false, op ~ "= is not implemented."); } return this; } ModInt opOpAssign(string op)(ulong rhs) { static if (op == "+") { val += rhs; if (val >= modulus) { val -= modulus; } } else if (op == "-") { if (val < rhs) { val += modulus; } val -= rhs; } else if (op == "*") { val = val * rhs % modulus; } else if (op == "/") { this *= ModInt(rhs).inv; } else if (op == "^^") { ModInt res = 1; ModInt t = this; while (rhs > 0) { if (rhs % 2 != 0) { res *= t; } t *= t; rhs /= 2; } this = res; } else { enforce(false, op ~ "= is not implemented."); } return this; } ModInt opUnary(string op)() { static if (op == "++") { this += 1; } else if (op == "--") { this -= 1; } else { enforce(false, op ~ " is not implemented."); } return this; } ModInt opBinary(string op)(const ulong rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinary(string op)(ref const ModInt rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinary(string op)(const ModInt rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinaryRight(string op)(const ulong lhs) const { mixin("return ModInt(this) " ~ op ~ "= lhs;"); } long opCmp(ref const ModInt rhs) const { return cast(long)value - cast(long)rhs.value; } bool opEquals(const ulong rhs) const { return value == ModInt(rhs).value; } ModInt inv() const { ModInt ret = this; ret ^^= modulus - 2; return ret; } string toString() const { import std.format : format; return format("%s", val); } } // }}} // alias {{{ alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less); alias MinHeap(T) = Heap!(T, "a > b"); // }}} // memo {{{ /* - ある値が見つかるかどうか <https://dlang.org/phobos/std_algorithm_searching.html#canFind> canFind(r, value); -> bool - 条件に一致するやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#filter> // 2で割り切れるやつ filter!"a % 2 == 0"(r); -> Range - 合計 <https://dlang.org/phobos/std_algorithm_iteration.html#sum> sum(r); - 累積和 <https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold> // 今の要素に前の要素を足すタイプの一般的な累積和 // 累積和のrangeが帰ってくる(破壊的変更は行われない) cumulativeFold!"a + b"(r, 0); -> Range - rangeをarrayにしたいとき array(r); -> Array - 各要素に同じ処理をする <https://dlang.org/phobos/std_algorithm_iteration.html#map> // 各要素を2乗 map!"a * a"(r) -> Range - ユニークなやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#uniq> uniq(r) -> Range - 順列を列挙する <https://dlang.org/phobos/std_algorithm_iteration.html#permutations> permutation(r) -> Range - ある値で埋める <https://dlang.org/phobos/std_algorithm_mutation.html#fill> fill(r, val); -> void - バイナリヒープ <https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap> // 昇順にするならこう(デフォは降順) BinaryHeap!(Array!T, "a > b") heap; heap.insert(val); heap.front; heap.removeFront(); - 浮動小数点の少数部の桁数設定 // 12桁 writefln("%.12f", val); - 浮動小数点の誤差を考慮した比較 <https://dlang.org/phobos/std_math.html#.approxEqual> approxEqual(1.0, 1.0099); -> true - 小数点切り上げ <https://dlang.org/phobos/std_math.html#.ceil> ceil(123.4); -> 124 - 小数点切り捨て <https://dlang.org/phobos/std_math.html#.floor> floor(123.4) -> 123 - 小数点四捨五入 <https://dlang.org/phobos/std_math.html#.round> round(4.5) -> 5 round(5.4) -> 5 */ // }}} void main() { auto cin = new Scanner; int h, w; cin.scan(h, w); string[] s = cin.nextArray!string(h); bool[] rmh = new bool[h], rmw = new bool[w]; foreach (i; 0 .. h) { bool f = true; foreach (j; 0 .. w) { if (s[i][j] == '#') { f = false; break; } } rmh[i] = f; } foreach (i; 0 .. w) { bool f = true; foreach (j; 0 .. h) { if (s[j][i] == '#') { f = false; break; } } rmw[i] = f; } foreach (i; 0 .. h) { if (rmh[i]) continue; foreach (j; 0 .. w) { if (rmw[j]) continue; write(s[i][j]); } writeln; } }
D
import std.stdio, std.conv, std.string, std.math; void main(){ auto ip = readln.split.to!(string[]); auto A = ip[0]; auto B = ip[1]; auto C = ip[2]; if(A[$-1] == B[0] && B[$-1] == C[0]) "YES".writeln; else "NO".writeln; }
D
void main() { long a, b, c, d; rdVals(a, b, c, d); foreach (i; 0 .. 1000) { if (i & 1) { a -= d; if (a <= 0) { "No".writeln; return; } } else { c -= b; if (c <= 0) { "Yes".writeln; return; } } } } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; enum double eps = 1.0e-9; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; alias Goods = Tuple!(long, "w", long, "v"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { auto n = lread(); auto a = aryread(); long abs_min = INF, abssum, minuscnt; foreach (e; a) { abssum += abs(e); abs_min = min(abs(e), abs_min); if (e <= 0) minuscnt++; } if (minuscnt % 2) writeln(abssum - 2 * abs_min); else writeln(abssum); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto N = readln.chomp.to!int; auto K = readln.chomp.to!int; auto ret = 1; foreach (_; 0..N) ret = min(ret * 2, ret + K); writeln(ret); }
D