code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio; import std.string; import std.conv; void main(){ int x = readln().chomp().to!int(); writeln(x * x * x); }
D
unittest { assert( [ "erasedream" ].parse.expand.solve == "YES" ); assert( [ "dreameraser" ].parse.expand.solve == "YES" ); assert( [ "dreamerer" ].parse.expand.solve == "NO" ); } import std.conv; import std.range; import std.stdio; import std.typecons; void main() { stdin.byLineCopy.parse.expand.solve.writeln; } auto parse( Range )( Range input ) if( isInputRange!Range && is( ElementType!Range == string ) ) { auto s = input.front; return tuple( s ); } auto solve( string s ) { LOOP: while( 0 < s.length ) { foreach( w; [ "dream", "dreamer", "erase", "eraser" ] ) { if( w.length <= s.length && s[ $ - w.length .. $ ] == w ) { s.length -= w.length; continue LOOP; } } return "NO"; } return "YES"; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.bigint; void main() { auto N = readln.chomp.to!int; int r, b; foreach (c; readln.chomp.to!(char[])) { if (c == 'R') { ++r; } else if (c == 'B') { ++b; } } writeln(r > b ? "Yes" : "No"); }
D
module main; import std.stdio; int main(string[] argv) { int n,k; scanf("%d", &n); int [] a = new int[1005]; int [] b = new int[1005]; int [] c = new int[1005]; for(int i = 1; i<=n; i++) scanf("%d:%d", &a[i],&b[i]); for(int i=1;i<=n;i++) { c[i]=a[i]*60+b[i];//printf("%d ",c[i]); } for(int i=1;i<=n;i++) { int minn=100000000,pla=0,j; for(j=i;j<=n;j++) { if(c[j]<minn) { minn=c[j]; pla=j; } } //printf("pla:%d",pla); int t=c[pla]; c[pla]=c[i]; c[i]=t; } int ans=0; for(int i=1;i<n;i++) { //printf("%d ",c[i]); if(c[i+1]-c[i]>ans) ans=c[i+1]-c[i]; } if(n==1) { printf("23:59"); return 0; } else { if(1440-c[n]+c[1]>ans) ans=1440-c[n]+c[1]; ans--; printf("%02d:%02d",ans/60,ans%60); } return 0; }
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 abc = readints; int a = abc[0], b = abc[1], c = abc[2]; if (a + b >= c) writeln("Yes"); else writeln("No"); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void 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, m; readV(n, m); int[] a, b; readC(m, a, b); --a[]; --b[]; auto uf = UnionFind(n); auto f = new long[](m); f[$-1] = long(n)*(n-1)/2; foreach_reverse (i; 1..m) { if (uf.isSame(a[i], b[i])) { f[i-1] = f[i]; } else { f[i-1] = f[i] - uf.countNodes(a[i]).to!long*uf.countNodes(b[i]); uf.unite(a[i], b[i]); } } foreach (i; 0..m) writeln(f[i]); } struct UnionFind { import std.algorithm, std.range; int[] p; const size_t n; const int s; size_t cf; size_t[] cn; this(size_t n) { this.n = n; s = cast(int)n; p = new int[](n); p[] = s; cf = n; cn = new size_t[](n); cn[] = 1; } bool unite(int i, int j) { auto pi = subst(i), pj = subst(j); if (pi != pj) { p[pj] = pi; --cf; cn[pi] += cn[pj]; return true; } else { return false; } } bool isSame(int i, int j) { return subst(i) == subst(j); } @property countForests() { return cf; } auto countNodes(int i) { return cn[subst(i)]; } auto groups() { auto g = new int[][](n); foreach (i; 0..cast(int)n) g[subst(i)] ~= i; return g.filter!(l => !l.empty); } private: int subst(int i) { return p[i] == s ? i : (p[i] = subst(p[i])); } }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int a, b; readV(a, b); writeln(a.predSwitch!"a<=b"(5, 0, 12, b/2, b)); }
D
void main() { long n, k; rdVals(n, k); long total = 1; foreach (i; k .. n+1) { long l = (i * (i - 1)) >> 1; long r = i * n - l; total = (total + r - l + 1) % mod; } total.writeln; } 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
void main() { int[] tmp = readln.split.to!(int[]); int h = tmp[0], w = tmp[1]; int cnt; string[] s = new string[h]; foreach (i; 0 .. h) { s[i] = readln.chomp; cnt += s[i].count('.'); } int[] dy = [-1, 0, 1, 0], dx = [0, 1, 0, -1]; int[][] dist = new int[][](h, w); foreach (d; dist) { d[] = -1; } dist[0][0] = 0; coord[] que; que ~= coord(0, 0); while (!que.empty) { int y1 = que[0].y, x1 = que[0].x; que.popFront; foreach (dir; 0 .. 4) { int y2 = y1 + dy[dir], x2 = x1 + dx[dir]; if (y2 < 0 || h <= y2 || x2 < 0 || w <= x2) continue; if (s[y2][x2] == '#') continue; if (dist[y2][x2] == -1) { que ~= coord(y2, x2); dist[y2][x2] = dist[y1][x1] + 1; } } } writeln(dist[h-1][w-1] != -1 ? cnt - dist[h-1][w-1] - 1 : dist[h-1][w-1]); } alias Tuple!(int, "y", int, "x") coord; 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() { long n, q; rdVals(n, q); auto st = SegTree!("a < b ? a : b", long, (1L<<31)-1)(n); foreach (i; 0 .. q) { long com, x, y; rdVals(com, x, y); if (com) st.query(x, y+1).writeln; else st.update(x, y); } } struct SegTree(alias pred, T, T init) { import core.bitop; alias F = binaryFun!pred; long n; T[] tree; this(long n) { this.n = n == 1 ? 1 : 1L << bsr(n-1) + 2; tree.length = this.n << 1; tree[] = init; } ref T opIndex(long idx) pure nothrow { return tree[idx+this.n]; } void update(long idx, T x) { idx += this.n; this.tree[idx] = x; while (idx >>= 1) { tree[idx] = F(tree[(idx<<1)], tree[(idx<<1)+1]); } } T query(long x, long y) { T l = init, r = init; for (x += this.n, y += this.n; x < y; x >>= 1, y >>= 1) { if (x & 1) l = F(l, tree[x++]); if (y & 1) r = F(tree[--y], r); } return F(l, r); } } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
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; enum MAX = 1_000_100; ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias Pair = Tuple!(long, "a", long, "b"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { auto a = lread(); auto b = lread(); writeln(6 - a - b); } 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; } }
D
void main() { auto S = rs; int i = S[0..2].to!int; int j = S[2..$].to!int; bool YYMM = j <= 12 && 1 <= j; bool MMYY = i <= 12 && 1 <= i; if(YYMM && MMYY) writeln("AMBIGUOUS"); else if(YYMM) writeln("YYMM"); else if(MMYY) writeln("MMYY"); else writeln("NA"); } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio,std.conv, std.algorithm, std.container,std.array,std.range,std.string,std.typecons; const dx = [1,0,-1,0], dy = [0,1,0,-1]; const readMixin = q{ auto line = readln().split(); if (line.length < args.length) return; foreach(i,ref arg; args) arg = line[i].to!(typeof(arg)); }; void read(T...) (auto ref T args){ mixin(readMixin);} void readArray(T)(auto ref T args){ mixin(readMixin);} //------------------------------ const coin = [10,50,100,500]; void main(){ int total; //read(total); //while(total){ for(read(total);total;read(total),write(total ? "\n":"")){ auto have = new int[4]; readArray(have); int howMany = int.max; auto uses = new int[4]; foreach(i0;iota(have[0]+1)){ foreach(i1;iota(have[1]+1)){ foreach(i2;iota(have[2]+1)){ foreach(i3;iota(have[3]+1)){ auto tmpuses = [i0,i1,i2,i3]; auto pay = i0 * coin[0] + i1 * coin[1] + i2 * coin[2] + i3 * coin[3]; if (pay < total) continue; int tmpHowMany = have.sum + howManyCoins(pay - total) - tmpuses.sum; if (tmpHowMany < howMany ){ howMany = tmpHowMany; uses = tmpuses; } } } } } foreach(i,u;uses){if(u >0)writeln(coin[i]," ",u);} // read(total); // if (total) writeln(); } } int howManyCoins(int pay){ int res; foreach_reverse(c;coin){ while(pay >= c){ pay -= c;res ++; } } return res; }
D
module AOJ_Volume0028; import std.stdio,std.string,std.conv; int main() { int[101] count; int max; string s; while((s = readln.chomp).length != 0) { int value = s.to!int; count[value]++; } max = count[0]; foreach(i;0..100) { if(max < count[i]) max = count[i]; } foreach(i;0..100) { if(max == count[i]) writeln(i); } return 0; }
D
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array, std.functional, std.container, std.typecons; void main() { int N = readln.split[0].to!int; int[] a = readln.split.to!(int[]); int[] ans = new int[N]; foreach_reverse(i, c; a) { ulong n = i + 1; ans[i] = c ^ iota(i, N, n).map!(j => ans[j]).reduce!"a ^ b"; } int cnt = ans.reduce!"a+b"; writeln(cnt); foreach(i, c; ans) { if(c == 1) write(i+1, " "); } }
D
import std.stdio, std.string, std.conv; void main() { int[] tmp = readln.split.to!(int[]); int n = tmp[0], x = tmp[1]; int[] l = readln.split.to!(int[]); int d, cnt = 1; foreach (i; 0 .. n) { d += l[i]; if (d > x) break; ++cnt; } cnt.writeln; }
D
void main() { int[] tmp = readln.split.to!(int[]); int n = tmp[0], h = tmp[1], w = tmp[2]; int cnt; foreach (i; 0 .. n) { int[] ab = readln.split.to!(int[]); int a = ab[0], b = ab[1]; if (a >= h && b >= w) ++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
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { string s, t; scan(s); scan(t); auto n = s.length.to!int; auto m = t.length.to!int; auto dp = new int[][](26, n + 1); fillAll(dp, inf3); foreach (i ; 0 .. 26) { foreach_reverse (j ; 0 .. n) { dp[i][j] = dp[i][j + 1]; if ((s[j] - 'a') == i) { dp[i][j] = j; } } } int p; long ans; foreach (ch ; t) { int c = ch - 'a'; if (dp[c][p] < inf3) { ans += dp[c][p] - p + 1; p = dp[c][p] + 1; } else if (dp[c][0] < inf3) { ans += (n - p) + dp[c][0] + 1; p = dp[c][0] + 1; } else { ans = -1; break; } debug { writeln("ans:", ans); } } 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 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; } } auto dp = new long[][](110, 100_100); void main() { long N, W; scan(N, W); auto goods = new Goods[](N); foreach (i; iota(N)) { long w, v; scan(w, v); goods[i] = Goods(w, v); } foreach (i; iota(N)) { foreach (j; iota(W)) { dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]); if (j + goods[i].w <= W) { dp[i + 1][j + goods[i].w] = dp[i][j] + goods[i].v; } // dp[i + 1].take(W + 1).writeln(); } } // foreach (i, e; dp) // { // if (i < N) // e.take(W).writeln(); // } dp[N].take(W + 1).reduce!(max).writeln(); }
D
import std.stdio; import std.algorithm; import std.string; import std.functional; import std.array; import std.conv; import std.math; import std.typecons; import std.regex; import std.range; int[] dx = [-2,-2,-2,-1,-1,0,0,1,1,2,2,2]; int[] dy = [1,0,-1,2,-2,2,-2,2,-2,1,0,-1]; int[] spx,spy; int tl; bool saiki(int x,int y,int n){ if(tl==n) return true; bool ans = false; for(int i=0;i<12;i++){ int nx,ny; nx = x + dx[i]; ny = y + dy[i]; if(0<=nx&&nx<=9&&0<=ny&&ny<=9){ if(abs(nx-spx[n])<=1 && abs(ny-spy[n])<=1){ ans = ans || saiki(nx,ny,n+1); } } } return ans; } void main(){ while(true){ int x,y; auto s = readln().split().to!(int[]); x = s[0]; y = s[1]; if(x==0&&y==0) break; spx = new int[10]; spy = new int[10]; int n = readln().chomp().to!int; tl = n; auto s1 = readln().split().to!(int[]); for(int i=0;i<n;i++){ spx[i] = s1[2*i]; spy[i] = s1[2*i+1]; } writeln(saiki(x,y,0) ? "OK" : "NA"); } }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; alias Edge = Tuple!(int, "to", int, "c", int, "rev"); void main() { int n, m; scan(n, m); auto adj = new Edge[][](n, 0); foreach (i ; 0 .. m) { int ui, vi, ci; scan(ui, vi, ci); adj[ui] ~= Edge(vi, ci, adj[vi].length.to!int); adj[vi] ~= Edge(ui, 0, adj[ui].length.to!int - 1); } debug { //writeln(adj); } int dfs(int u, int b, bool[] visited) { if (u == n - 1) { debug { //writefln("%d", u); } return b; } visited[u] = true; //writef("%d ", u); foreach (ref v ; adj[u]) { if (v.c > 0 && !visited[v.to]) { int d = dfs(v.to, min(b, v.c), visited); if (d > 0) { v.c -= d; adj[v.to][v.rev].c += d; return d; } } } return 0; } int ans; while (true) { auto visited = new bool[](n); visited[0] = true; int F = dfs(0, 101101, visited); debug { //writeln("F:", F); //writeln(adj); } if (!F) break; ans += F; } writeln(ans); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto rd = readln.split.map!(to!int), a = rd[0], b = rd[1], c = rd[2]; auto r = iota(a, b+1).count!(i => c % i == 0); writeln(r); }
D
import std.algorithm; import std.array; import std.ascii; import std.bigint; import std.complex; import std.container; import std.conv; import std.functional; import std.math; import std.range; import std.stdio; import std.string; 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]; } void readlnTo(T...)(ref T t) { auto s = readln().split(); assert(s.length == t.length); foreach(ref ti; t) { ti = s[0].to!(typeof(ti)); s = s[1..$]; } } const real eps = 1e-10; long r2(long[] p1, long[] p2) { long[] d = [p1[0]-p2[0], p1[1]-p2[1]]; return d[0]*d[0] + d[1]*d[1]; } long dot(long[] x1, long[] x2) { return x1[0] * x2[0] + x1[1] * x2[1]; } long[] sub(long[] x1, long[] x2) { return [x1[0]-x2[0], x1[1]-x2[1]]; } long[] add(long[] x1, long[] x2) { return [x1[0]+x2[0], x1[1]+x2[1]]; } double d2(long[] p1, long[] p2, long[] x) { long[] p21 = sub(p2, p1); long[] p1x = sub(p1, x); return (dot(p21, p21)*dot(p1x,p1x) - pow(dot(p21, p1x), 2))/to!double(dot(p21,p21)); } long cross(long[] x, long[] y) { return x[0]*y[1] - x[1]*y[0]; } bool inTriangle(long[] p1, long[] p2, long[] p3, long[] x) { auto u = sub(p1, x); auto v = sub(p2, x); auto w = sub(p3, x); auto a = cross(u, v); auto b = cross(v, w); auto c = cross(w, u); return a*b > 0 && b*c > 0 && c*a > 0; } bool hasIntersection(long[] p1, long[] p2, long[] r0, long r) { auto p21 = sub(p2, p1); auto p1r = sub(p1, r0); auto f0 = dot(p1r, p1r) - r*r; auto f1 = dot(add(p21, p1r), add(p21, p1r)) - r*r; if(f0 * f1 <= 0) { return true; } if(f0 < 0 && f1 < 0) { return false; } if(dot(p21, p1r) > 0 || abs(dot(p21, p1r)) > dot(p21, p21)) { return false; } return -pow(dot(p21, p1r), 2) + dot(p1r, p1r)*dot(p21, p21) - r*r*dot(p21, p21) <= 0; } string test(long[] p1, long[] p2, long[] p3, long[] p4, long r) { if(inTriangle(p1, p2, p3, p4) && min(d2(p1, p2, p4), d2(p2, p3, p4), d2(p3, p1, p4)) >= r*r) { return "a"; } if(r2(p1, p4) <= r*r && r2(p2,p4) <= r*r && r2(p3,p4) <= r*r) { return "b"; } if(hasIntersection(p1, p2, p4, r) || hasIntersection(p2, p3 ,p4, r) || hasIntersection(p3, p1, p4, r)) { return "c"; } return "d"; } void main(){ while(true) { auto xy1 = readLongs(); if(xy1[0] == 0 && xy1[1] == 0) { return; } auto xy2 = readLongs(); auto xy3 = readLongs(); auto xy4 = readLongs(); auto r = readLong(); writeln(test(xy1, xy2, xy3, xy4, r)); } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007; void main() { int n; scan(n); auto p = readln.split.to!(int[]); p[] -= 1; int c; foreach (i ; 0 .. n) { if (i != p[i]) c++; } writeln(c <= 2 ? "YES" : "NO"); } int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new int[][](n, 0); foreach (i; 0 .. m) { int u, v; scan(u, v); if (is1indexed) { u--, v--; } adj[u] ~= v; if (isUndirected) { adj[v] ~= u; } } return adj; } alias Edge = Tuple!(int, "to", int, "cost"); Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new Edge[][](n, 0); foreach (i; 0 .. m) { int u, v, c; scan(u, v, c); if (is1indexed) { u--, v--; } adj[u] ~= Edge(v, c); if (isUndirected) { adj[v] ~= Edge(u, c); } } return adj; } void yes(bool b) { writeln(b ? "Yes" : "No"); } void YES(bool b) { writeln(b ? "YES" : "NO"); } T[] readArr(T)() { return readln.split.to!(T[]); } T[] readArrByLines(T)(int n) { return iota(n).map!(i => readln.chomp.to!T).array; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
void main(){ string s = readln().chomp(); if( s[2] == s[3] && s[4] == s[5])writeln("Yes"); else writeln("No"); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "a", long, "b"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { auto s = sread(); auto t = sread(); if (s.length < t.length) { writeln("UNRESTORABLE"); return; } long insert = -1; foreach (i; iota(s.length - t.length + 1)) { if (is_pass(s[i .. i + t.length], t)) insert = i; } if (insert >= 0) { fill_a(s, t, insert).writeln(); } else { writeln("UNRESTORABLE"); } } bool is_pass(string pat, string str) { bool ret = true; foreach (i; iota(pat.length)) { if (pat[i] == '?') continue; ret &= (pat[i] == str[i]); } return ret; } auto fill_a(string s, string t, long insert) { string ret; foreach (i, e; s) { if (i >= insert && i < insert + t.length) { ret ~= t[i - insert]; } else { ret ~= (e == '?' ? 'a' : e); } } return ret; } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.stdio, std.string, std.conv; import std.algorithm, std.array; auto solve(string S) { immutable N = S.length.to!int(); immutable s = S.map!(c=>(c-'0').to!long()).array(); auto v = new long[][](N,N); v[0][]=s; foreach(i;1..N) foreach(j;0..N-i) v[i][j]=v[i-1][j]*10+s[i+j]; long t=0; foreach(i;0..N) foreach(j;0..i+1) t+=v[N-1-i][j]*(1L<<max(0,j-1))*(1L<<max(0,i-j-1)); return t; } void main(){ for(string s; (s=readln.chomp()).length;) writeln(solve(s)); }
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 calc(string s, int k) { auto cs = new char[s.length]; for (int i = 0; i < s.length; i++) { cs[i] = s[i] != s[k - 1] ? '*' : s[i]; } writeln(cs); } void main() { readint; string s = read!string; int k = readint; calc(s, k); }
D
import std.stdio,std.string,std.conv,std.array; void main(){ loop:for(;;){ auto rcs = readln().chomp().split(); auto a = to!int(rcs[0]); auto b = to!int(rcs[2]); final switch(rcs[1]){ case "+":writeln(a+b);break; case "-":writeln(a-b);break; case "*":writeln(a*b);break; case "/":writeln(a/b);break; case "?":break loop; } } }
D
void main() { int s = readln.chomp.to!int; bool[] a = new bool[1000001]; int cnt = 1; while (!a[s]) { a[s] = true; ++cnt; if (s % 2) s = 3 * s + 1; else s /= 2; } 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.container; import std.typecons;
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 998_244_353, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "p", long, "dist"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { auto n = lread!ulong(); auto d = aryread!ulong(); auto count = new ulong[](n - 1); auto dist = new ulong[](n - 1); foreach (e; d) count[e]++; if (d[0] != 0 || count[0] > 1) { writeln(0); return; } dist[0] = 1; foreach (i; iota(1, n - 1)) { dist[i] = dist[i - 1] * powmod(count[i - 1], count[i], MOD); dist[i] %= MOD; } // dist.writeln(); dist[$ - 1].writeln(); } auto powmod(ulong x, ulong n, ulong m) { ulong ret = 1; while(n > 0) { ret *= x; ret %= m; n--; } return ret % m; } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto x = RD; auto y = RD; for (long i = 1; i*i < x; ++i) { auto xx = x - i; auto r = min(xx / i, y) - i; if (r > 0) ans[ti] += r; } } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio, std.string, std.algorithm, std.range, std.conv; void main() { // ①入力 auto N = readln.chomp.to!int; auto A = readln.chomp.to!int; // ②処理 int res = N^^2-A; // ③出力 writeln(res); }
D
import std.stdio; import std.string; import std.conv; import std.math; int main() { string s; while((s=readln.chomp).length != 0) { double value = s.to!double; double min_t = value/9.8; double just = 4.9*min_t*min_t; int number = cast(int)((just+10)/5.0); writeln(number); } return 0; }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; void times(alias fun)(int n) { foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } void main() { int n = readln.chomp.to!int; long[] ary = readln.split.to!(long[]); 2.iota.map!((j) { long s = 0; long x = 0; foreach(i, a; ary) { long b = x+a; x = i%2==j ? b>0 ? b : 1 : b<0 ? b : -1; s += abs(x - b); } return s; }).reduce!min.writeln; } // ---------------------------------------------- // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { auto minElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto minimum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b < minimum) { element = a; minimum = b; } } return element; } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto maximum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b > maximum) { element = a; maximum = b; } } return element; } }
D
import std.math, std.stdio, std.conv, std.string, std.range, std.algorithm; struct C{ double x, y, r; } void main() { foreach(unused;0..readln.chomp.to!int) { auto s = readln.split.map!(to!double); isOverlap(C(s[0], s[1], s[2]), C(s[3], s[4], s[5])).writeln; } } int isOverlap(C a, C b) { double d = sqrt((a.x - b.x) ^^ 2 + (a.y - b.y) ^^ 2); if(d > a.r + b.r) return 0; else if(d + b.r < a.r) return 2; else if(d + a.r < b.r) return -2; else return 1; }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional, std.traits; import std.algorithm, std.container; void main() { auto S = readln.strip; long res; foreach(long l;0..S.length) { foreach(long r;l..S.length) { long lcount = max(l-1, 0); long rcount=S.length; rcount-=r+2; rcount = max(rcount, 0); long count; count = 1<<lcount+rcount; res += S[l..r+1].to!long * count; } } writeln(res); } class UnionFind{ UnionFind parent = null; void merge(UnionFind a) { if(same(a)) return; a.root.parent = this.root; } UnionFind root() { if(parent is null)return this; return parent = parent.root; } bool same(UnionFind a) { return this.root == a.root; } } void scanValues(TList...)(ref TList list) { auto lit = readln.splitter; foreach (ref e; list) { e = lit.fornt.to!(typeof(e)); lit.popFront; } } T[] scanArray(T = long)() { return readln.split.to!(long[]); } void scanStructs(T)(ref T[] t, size_t n) { t.length = n; foreach (ref e; t) { auto line = readln.split; foreach (i, ref v; e.tupleof) { v = line[i].to!(typeof(v)); } } } long scanULong(){ long x; while(true){ const c = getchar; if(c<'0'||c>'9'){ break; } x = x*10+c-'0'; } return x; } T scanElem(T = string)() { char[] res; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { res ~= cast(char) c; c = getchar; } return res.strip.to!T; } template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } struct Factor { long n; long c; } Factor[] factors(long n) { Factor[] res; for (long i = 2; i ^^ 2 <= n; i++) { if (n % i != 0) continue; int c; while (n % i == 0) { n = n / i; c++; } res ~= Factor(i, c); } if (n != 1) res ~= Factor(n, 1); return res; } long[] primes(long n) { if(n<2)return []; auto table = new long[n+1]; long[] res; for(int i = 2;i<=n;i++) { if(table[i]==-1) continue; for(int a = i;a<table.length;a+=i) { table[a] = -1; } res ~= i; } return res; } bool isPrime(long n) { if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (long i = 3; i ^^ 2 <= n; i += 2) if (n % i == 0) return false; return true; }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; import std.typecons; import std.bigint; // import dcomp.foundation, dcomp.scanner; // import dcomp.container.deque; int main() { auto sc = new Scanner(stdin); int n, m, k; sc.read(n, m, k); m++; int[] a = new int[n]; a.each!((ref x) => sc.read(x)); long[] dp = a.map!(to!long).array; long[] ndp = new long[n]; auto deq = Deque!(int, false).make(); foreach (int ph; 2..k+1) { ndp[] = -(10L^^18); deq.clear(); foreach (int i; 0..n) { if (deq.length && deq.front == i-m) { deq.removeFront(); } if (deq.length) { ndp[i] = dp[deq.front] + 1L * ph * a[i]; } while (deq.length && dp[deq[deq.length-1]] <= dp[i]) { deq.removeBack(); } deq.insertBack(i); } swap(dp, ndp); } writeln(dp.fold!max); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; //fold(for old compiler) static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } unittest { import std.stdio; auto l = [1, 2, 3, 4, 5]; assert(l.fold!"a+b"(10) == 25); } } version (X86) static if (__VERSION__ < 2071) { int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/deque.d */ // module dcomp.container.deque; struct Deque(T, bool hasNull = true) { import core.exception : RangeError; import core.memory : GC; import std.range : ElementType, isInputRange; import std.traits : isImplicitlyConvertible; struct Payload { T *d; size_t st, length, cap; @property bool empty() const { return length == 0; } alias opDollar = length; ref inout(T) opIndex(size_t i) inout { version(assert) if (length <= i) throw new RangeError(); return d[(st+i >= cap) ? (st+i-cap) : st+i]; } private void expand() { import std.algorithm : max; assert(length == cap); auto nc = max(size_t(4), 2*cap); T* nd = cast(T*)GC.malloc(nc * T.sizeof); foreach (i; 0..length) { nd[i] = this[i]; } d = nd; st = 0; cap = nc; } void clear() { st = length = 0; } void insertFront(T v) { if (length == cap) expand(); if (st == 0) st += cap; st--; length++; this[0] = v; } void insertBack(T v) { if (length == cap) expand(); length++; this[length-1] = v; } void removeFront() { assert(!empty, "Deque.removeFront: Deque is empty"); st++; length--; if (st == cap) st = 0; } void removeBack() { assert(!empty, "Deque.removeBack: Deque is empty"); length--; } } struct RangeT(A) { alias T = typeof(*(A.p)); alias E = typeof(A.p.d[0]); T *p; size_t a, b; @property bool empty() const { return b <= a; } @property size_t length() const { return b-a; } @property RangeT save() { return RangeT(p, a, b); } @property RangeT!(const A) save() const { return typeof(return)(p, a, b); } alias opDollar = length; @property ref inout(E) front() inout { return (*p)[a]; } @property ref inout(E) back() inout { return (*p)[b-1]; } void popFront() { version(assert) if (empty) throw new RangeError(); a++; } void popBack() { version(assert) if (empty) throw new RangeError(); b--; } ref inout(E) opIndex(size_t i) inout { return (*p)[i]; } RangeT opSlice() { return this.save; } RangeT opSlice(size_t i, size_t j) { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } RangeT!(const A) opSlice() const { return this.save; } RangeT!(const A) opSlice(size_t i, size_t j) const { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } } alias Range = RangeT!Deque; alias ConstRange = RangeT!(const Deque); alias ImmutableRange = RangeT!(immutable Deque); Payload *p; private void I() { if (hasNull && !p) p = new Payload(); } private void C() const { version(assert) if (!p) throw new RangeError(); } //some value this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) {I; p = new Payload(); foreach (v; values) { insertBack(v); } } //range this(Range)(Range r) if (isInputRange!Range && isImplicitlyConvertible!(ElementType!Range, T) && !is(Range == T[])) {I; p = new Payload(); foreach (v; r) { insertBack(v); } } static Deque make() { Deque que; que.p = new Payload(); return que; } @property private bool hasPayload() const { return (!hasNull || p); } @property bool empty() const { return (!hasPayload || p.empty); } @property size_t length() const { return (hasPayload ? p.length : 0); } alias opDollar = length; ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; } ref inout(T) front() inout {C; return (*p)[0]; } ref inout(T) back() inout {C; return (*p)[$-1]; } void clear() { if (p) p.clear(); } void insertFront(T v) {I; p.insertFront(v); } void insertBack(T v) {I; p.insertBack(v); } void removeFront() {C; p.removeFront(); } void removeBack() {C; p.removeBack(); } Range opSlice() {I; return Range(p, 0, length); } } unittest { import std.algorithm : equal; import std.range.primitives : isRandomAccessRange; import std.container.util : make; auto q = make!(Deque!int); assert(isRandomAccessRange!(typeof(q[]))); //insert,remove assert(equal(q[], new int[](0))); q.insertBack(1); assert(equal(q[], [1])); q.insertBack(2); assert(equal(q[], [1, 2])); q.insertFront(3); assert(equal(q[], [3, 1, 2]) && q.front == 3); q.removeFront; assert(equal(q[], [1, 2]) && q.length == 2); q.insertBack(4); assert(equal(q[], [1, 2, 4]) && q.front == 1 && q.back == 4 && q[$-1] == 4); q.insertFront(5); assert(equal(q[], [5, 1, 2, 4])); //range assert(equal(q[][1..3], [1, 2])); assert(equal(q[][][][], q[])); //const range const auto rng = q[]; assert(rng.front == 5 && rng.back == 4); //reference type auto q2 = q; q2.insertBack(6); q2.insertFront(7); assert(equal(q[], q2[]) && q.length == q2.length); //construct with make auto a = make!(Deque!int)(1, 2, 3); auto b = make!(Deque!int)([1, 2, 3]); assert(equal(a[], b[])); } unittest { import std.algorithm : equal; import std.range.primitives : isRandomAccessRange; import std.container.util : make; auto q = make!(Deque!int); q.clear(); assert(equal(q[], new int[0])); foreach (i; 0..100) { q.insertBack(1); q.insertBack(2); q.insertBack(3); q.insertBack(4); q.insertBack(5); assert(equal(q[], [1,2,3,4,5])); q.clear(); assert(equal(q[], new int[0])); } } unittest { Deque!int a; Deque!int b; a.insertFront(2); assert(b.length == 0); } unittest { import std.algorithm : equal; import std.range : iota; Deque!int a; foreach (i; 0..100) { a.insertBack(i); } assert(equal(a[], iota(100))); } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; if (f.eof) return false; line = lineBuf[]; f.readln(line); } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { //string or char[10] etc //todo optimize auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { //static assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); fout.writeln("1 2 3"); fout.writeln("ab cde"); fout.writeln("1.0 1.0 2.0"); fout.close; Scanner sc = new Scanner(File(fileName, "r")); int a; int[2] b; char[2] c; string d; double e; double[] f; sc.read(a, b, c, d, e, f); assert(a == 1); assert(equal(b[], [2, 3])); assert(equal(c[], "ab")); assert(equal(d, "cde")); assert(e == 1.0); assert(equal(f, [1.0, 2.0])); } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File, writeln; import std.datetime; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); foreach (i; 0..1_000_000) { fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2); } fout.close; writeln("Scanner Speed Test(3*1,000,000 int)"); StopWatch sw; sw.start; Scanner sc = new Scanner(File(fileName, "r")); foreach (i; 0..500_000) { int a, b, c; sc.read(a, b, c); assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 500_000..700_000) { int[3] d; sc.read(d); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 700_000..1_000_000) { int[] d; sc.read(d); assert(d.length == 3); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } writeln(sw.peek.msecs, "ms"); }
D
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } void main() { try { for (; ; ) { const numCases = readInt(); foreach (caseId; 0 .. numCases) { const N = readToken(); int ans; foreach (c; N) { chmax(ans, c - '0'); } writeln(ans); } } } catch (EOFException e) { } }
D
import std.stdio; void main(){ for(int i=1;i<=9;i++){ for(int j=1;j<=9;j++){ writeln(i,"x",j,"=",i*j); } } }
D
void main() { long x = rdElem; long result; result += x / 500 * 1000; x %= 500; result += x / 5 * 5; result.writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
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() { long x, y; scan(x, y); writeln(abs(x - y) < 2 ? "Brown" : "Alice"); } long dsum(long b, long n) { return n < b ? n : dsum(b, n / b) + (n % b); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.conv; import std.array; void main() { string[] inputs = split(stdin.readln()); auto a = to!(int)(inputs[0]), op = to!(char)(inputs[1]), b = to!(int)(inputs[2]); writeln((op == '+') ? (a + b) : (a - b)); }
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } /* まず0の耳がない場合を考える その場合は、端から端まで歩くとしてよい。各耳に1が入る そして同じ箇所を往復することにより2ずつ石を入れれるので2で割った余りだけ見ればよい (a[i] - 1) % 2 1 1 0 0 1 0 0 1 0 1 0 1 などとなっている 両端から折り返して、どこかからどこかまでをカバーする そこまでに含まれてしまう0の数と、真ん中に含まれてしまう1の数が、調整しなければならない個数 1 1 0 0 1 0 0 1 0 1 0 1 ~~~ * ~~*~~~*~~ 真ん中に含まれる0の数 - 真ん中に含まれる1の数 が最大になればよい これは尺取り法でできる -------------------------------------------------------------------------------- さて0の耳がある場合については… 0の場合にも対応できるように改良する 2をどんどん引いて、0、1、2のいずれかにする 1 1 0 0 1 2 0 1 2 1 2 1 0というのはもともとの0 1というのは先程の言い方でいう0 2というのは先程の言い方でいう1 外れた場合 これを基準とする 0→ペナ0 1→ペナa 2→ペナa 外側に入った場合 p 0→ペナ2 (+2) 1→ペナ1 (-(a-1)) 2→ペナ0 (-a) 中央に入った場合 q 0→ペナ1 (+1) 1→ペナ0 (-a) 2→ペナ1 (-(a-1)) 点iから始めて、左に行って帰ってくる場合の最良を x[i] 点j - 1から始めて、右に行って帰ってくる場合の最良を y[j] x[i] = min(x[i - 1] + p[i], 0) y[j] = min(y[j + 1] + p[j], 0) 点i以左から始めて、点iに帰ってくる場合の最良を x2[i] 点j以右から始めて、点jに帰ってくる場合の最良を y2[i] x2[i] = min(x[i], x2[i - 1] + q[i]); y2[i] = min(y[j], y2[j + 1] + q[j]), 求めるものは min(x2[i] + y2[i]) */ void main(){ long l = read.to!long; long[] as; foreach(i; 0 .. l) as ~= read.to!long; debug writeln("as:", as); long[] ps, qs; foreach(i; 0 .. l){ long a = as[i]; if(a == 0) ps~= 2, qs ~= 1; else if(a % 2 == 1) ps ~= -(a - 1), qs ~= -a; else ps ~= -a, qs ~= -(a - 1); } debug writeln("ps:", ps); debug writeln("qs:", qs); long[] xs = new long[](l + 1); xs[0] = 0; foreach(i; 1 .. l + 1) xs[i] = min(xs[i - 1] + ps[i - 1], 0); debug writeln("xs:", xs); long[] ys = new long[](l + 1); ys[l] = 0; foreach_reverse(i; 0 .. l) ys[i] = min(ys[i + 1] + ps[i], 0); debug writeln("ys:", ys); long[] x2s = new long[](l + 1); x2s[0] = xs[0]; foreach(i; 1 .. l + 1) x2s[i] = min(x2s[i - 1] + qs[i - 1], xs[i]); debug writeln("x2s:", x2s); long[] y2s = new long[](l + 1); y2s[l] = ys[l]; foreach_reverse(i; 0 .. l) y2s[i] = min(y2s[i + 1] + qs[i], ys[i]); debug writeln("y2s:", y2s); long sum; foreach(a; as) sum += a; debug writeln("sum:", sum); long ans = sum + 10; foreach(i; 0 .. l + 1) ans = min(ans, sum + x2s[i] + y2s[i]); ans.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)); } void main() { try { for (; ; ) { const N = readInt(); auto X = new long[N]; auto Y = new long[N]; foreach (i; 0 .. N) { X[i] = readLong(); Y[i] = readLong(); } bool ans; if (N % 2 == 0) { auto dx = new long[N]; auto dy = new long[N]; foreach (i; 0 .. N) { dx[i] = X[(i + 1) % N] - X[i]; dy[i] = Y[(i + 1) % N] - Y[i]; } ans = true; foreach (i; 0 .. N / 2) { ans = ans && (-dx[i] == dx[i + N / 2]); ans = ans && (-dy[i] == dy[i + N / 2]); } } writeln(ans ? "YES" : "NO"); } } catch (EOFException e) { } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto n = RD!int; auto ans = new bool[](n); foreach (i; 0..n) { auto s1 = RD!string; auto s2 = RD!string; bool ok = true; char last; size_t pos; foreach (c; s2) { if (pos >= s1.length) { if (c == last) continue; else { ok = false; break; } } if (c == s1[pos]) { ++pos; last = c; continue; } else if (c == last) continue; else { ok = false; break; } } if (pos < s1.length) ok = false; ans[i] = ok; } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush(); debug readln(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void get(Args...)(ref Args args) { import std.traits, std.meta, std.typecons; static if (Args.length == 1) { alias Arg = Args[0]; static if (isArray!Arg) { args[0] = readln.split.to!Arg; } else static if (isTuple!Arg) { auto input = readln.split; static foreach (i; 0..Fields!Arg.length) { args[0][i] = input[i].to!(Fields!Arg[i]); } } else { args[0] = readln.chomp.to!Arg; } } else { auto input = readln.split; assert(input.length == Args.length); static foreach (i; 0..Args.length) { args[i] = input[i].to!(Args[i]); } } } void get_lines(Args...)(size_t N, ref Args args) { import std.traits, std.range; static foreach (i; 0..Args.length) { static assert(isArray!(Args[i])); args[i].length = N; } foreach (i; 0..N) { static if (Args.length == 1) { get(args[0][i]); } else { auto input = readln.split; static foreach (j; 0..Args.length) { args[j][i] = input[j].to!(ElementType!(Args[j])); } } } } uint bits_msb(uint v) { v = v | (v >> 1); v = v | (v >> 2); v = v | (v >> 4); v = v | (v >> 8); v = v | (v >> 16); return v ^ (v >> 1); } void solve() { int N; get(N); uint[] AS; get(AS); long[33] cs; foreach (a; AS) { a = bits_msb(a); static foreach (i; 0..33) { if (a & 1) ++cs[i]; a >>= 1; } } long r; foreach (c; cs) r += c * (c-1) / 2; writeln(r); } void main() { int T; get(T); foreach (_; 0..T) solve(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto n = RD; auto m = RD!int; auto k = RD; auto p = RDA; long ans; int pos; auto last = k; while (true) { long cnt; bool end = true; foreach (j; pos..m) { if (p[j] > last) { if (cnt == 0) { last += (((p[j] - last) - 1) / k + 1) * k; break; } else { last += cnt; pos = j; end = false; break; } } ++cnt; } if (cnt == 0) continue; debug writeln(pos, " ", cnt, " ", last); ++ans; if (end) break; } writeln(ans); stdout.flush(); debug 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; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto A = s[1]; auto B = s[2]; int ans = 0; int x; foreach (i; 1..10000) { if (A/i == 0 || B/i == 0) { x = min(A, B); } else { x = i; } if (A/x + B/x >= N) { ans = max(ans, x); } } ans.writeln; }
D
import std.stdio, std.string, std.conv; import std.typecons; import std.algorithm; void main(){ auto nk = readln.chomp.split.map!(to!int); int n = nk[0]; int k = nk[1]; int l = 1; int r = n*n - n*(n-k+1) + 1; int sum = 0; for(int x = r; x <= n*n; x += n-k+1){ sum += x; } writeln(sum); foreach(x; 0..n){ foreach(i; 0..(k-1)){ write(l); l++; write(" "); } foreach(i; k..(n+1)){ write(r); r++; if(i!=n)write(" "); } writeln(); } }
D
import std.conv, std.functional, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } int N; int[] A; void main() { try { for (; ; ) { N = readInt(); A = new int[N]; foreach (i; 0 .. N) { A[i] = readInt(); } int ans; for (int k = 1; k <= N; k <<= 1) { for (int i = 0; i < N; i += k) { debug { writeln(k, " ", i, " ", A[i .. i + k]); } bool ok = true; foreach (j; i + 1 .. i + k) { ok = ok && (A[j - 1] <= A[j]); } if (ok) { chmax(ans, k); } } } writeln(ans); } } catch (EOFException e) { } }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; int main(string[] argv) { int n, p1, m; scanf("%d %d %d", &n, &p1, &m); long p = p1; long [] d = new long[n + 1]; long [] t = new long[n + 1]; for(int i = 0; i < n; i++) { scanf("%lld %lld", &t[i], &d[i]); } d[n] = 1152921504606846976L; t[n] = m + 1; long curday = 1; long money = 0; long answer = 0; for(int i = 0; i <= n; i++) { long lastday = t[i]; long needdays = 0; if (money >= 0) { needdays = money / p; } if (lastday - curday > needdays) { answer += (lastday - curday) - needdays; } money -= p * (lastday - curday + 1); curday = lastday + 1; money += d[i]; if (money < 0) { answer++; } } printf("%lld\n", answer); return 0; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD; long x; foreach (i; 0..33) { auto bit = 1L << i; if (n & bit) x = bit; } ans[ti] = x - 1; } foreach (e; ans) writeln(e); stdout.flush; debug 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, core.stdc.stdlib; bool solve() { auto N = readln.chomp.to!int; int pa = 0, pb = 0; bool ok = true; while (N--) { auto s = readln.split.map!(to!int); auto a = s[0]; auto b = s[1]; if (a < b) ok = false; if (a < pa) ok = false; if (b < pb) ok = false; int da = a - pa; int db = b - pb; if (db > da) ok = false; pa = a; pb = b; } return ok; } void main() { auto T = readln.chomp.to!int; while (T--) writeln(solve ? "YES" : "NO"); }
D
import std.stdio, std.string, std.conv, std.range, std.algorithm, std.regex; void main() { auto HW = readln.chomp.split.map!(to!int); auto H = HW[0]; foreach (_; 0 .. H) { auto C = readln; C.write; C.write; } }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); void main() { auto r = lread(); writeln(r ^^ 2); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto S = readln.chomp; foreach (i; 0..S.length-1) if (S[i..i+2] == "AC") { writeln("Yes"); return; } writeln("No"); }
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)); } int N; int[] A; bool check(int ban) { int[] as; foreach (a; A) { if (a != ban) { as ~= a; } } const asLen = cast(int)(as.length); for (int l = 0, r = asLen - 1; l < r; ++l, --r) { if (as[l] != as[r]) { return false; } } return true; } bool solve() { for (int l = 0, r = N - 1; l < r; ++l, --r) { if (A[l] != A[r]) { if (check(A[l])) return true; if (check(A[r])) return true; return false; } } return true; } void main() { try { for (; ; ) { const numCases = readInt(); foreach (caseId; 0 .. numCases) { N = readInt(); A = new int[N]; foreach (i; 0 .. N) { A[i] = readInt(); } const ans = solve(); writeln(ans ? "YES" : "NO"); } } } catch (EOFException e) { } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } bool isPrime(int n) { if (n < 2) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (int i = 3; i * i <= n; i += 2) { if (n % i == 0) return false; } return true; } void main() { string s; while ((s = readln.chomp) != null) { int n = s.to!int; int ans = 0; for (int i = 1; i <= n; i++) { int a = i; int b = n - i + 1; if (isPrime(a) && isPrime(b)) ans++; } writeln(ans); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.typecons; import std.numeric, std.math; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto n = RD; auto cnt = new long[30][](n); foreach (i; 0..n) { auto S = RD!string; foreach (c; S) { ++cnt[i][c-'a']; } } string ans; foreach (i; 0..30) { long least = long.max; foreach (j; 0..n) { least = min(least, cnt[j][i]); } foreach (j; 0..least) ans ~= i + 'a'; } writeln(ans); stdout.flush(); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}} void main() { int a; readV(a); int b; readV(b); int c; readV(c); int x; readV(x); int r = 0; foreach (i; 0..a+1) foreach (j; 0..b+1) foreach (k; 0..c+1) if (i*500+j*100+k*50 == x) ++r; writeln(r); }
D
void main() { string[] tmp = readln.split; string x = tmp[0], y = tmp[1]; if (x < y) { '<'.writeln; } else if (x > y) { '>'.writeln; } else { '='.writeln; } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.container; import std.bigint; import std.math; void main() { auto nm = readln.split.map!(to!int); auto co = readln.split.map!(to!int); auto dp = new int[](nm[0] + 1); dp[] = int.max; foreach (c; co) { if (c > nm[0]) continue; dp[c] = 1; } foreach (i; 1..nm[0]) { foreach (c; co) { if (i+c > nm[0]) continue; dp[i+c] = min(dp[i+c], dp[i]+1); } } dp[nm[0]].writeln; }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { readint; auto a = readints; int lo = a.reduce!min; int hi = a.reduce!max; int ans = int.max; for (int i = lo; i <= hi; i++) { int cost = a.map!(e => (e - i) ^^ 2).sum; ans = min(ans, cost); } writeln(ans); }
D
import std.algorithm; import std.range; import std.ascii; import std.array; import std.container; import std.conv; import std.numeric; import std.stdio; import std.string; import std.typecons; void log(A...)(A arg) { stderr.writeln(arg); } long modPow(long mod)(long x, long n) { if (n == 0) return 1; if (n % 2 == 0) return modPow!mod(x * x % mod, n / 2); return x * modPow!mod(x % mod, n - 1) % mod; } class RollingHash(long[] Ps, long mod) { string s; long[][] H; this(in string s) { this.s = s; auto N = s.length; this.H = new long[][](Ps.length, N + 1); // H[k]: s[0]*Ps[k]^(n-1) + s[1]*Ps[k]^(n-2) + ... + s[n-1]*Ps[k]^0 foreach (k, P; Ps) { foreach (int i, c; s) { H[k][i + 1] = (H[k][i] * P + cast(int)(c - 'a' + 1)) % mod; } } } long[] hash(int l, int r) { // [l, r) auto hs = iota(0, Ps.length, 1).map!(delegate(k) { return (H[k][r] - H[k][l] * modPow!mod(Ps[k], r - l) % mod + mod) % mod; }); return hs.array; } } void main() { int N, M; scanf("%d %d\n", &N, &M); auto s = readln.chomp; auto hasher = new RollingHash!([991, 9999991], cast(long)(1e9+7))(s); int l = 0, r = 1; bool[long[]] appeared; foreach (_; 0 .. M) { auto q = readln.chomp; switch (q) { case "L++": l++; break; case "L--": l--; break; case "R++": r++; break; case "R--": r--; break; default: assert(false); } appeared[hasher.hash(l, r).idup] = true; } writeln(appeared.keys.length); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); ulong bignum = 1_000_000_007; alias SugarWater = Tuple!(long, "swater", long, "sugar"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { long a,b,c; scan(a,b,c); max(0, (c - a + b)).writeln(); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n, m, xc, yc; readV(n, m, xc, yc); int[] x; readA(n, x); int[] y; readA(m, y); x ~= xc; y ~= yc; writeln(x.reduce!max < y.reduce!min ? "No War" : "War"); }
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())); } void main() { auto r1 = readInts(); r1[2..$] -= 1; int n = r1[0]; int m = r1[1]; int s = r1[2]; auto g = r1[3..$]; while(n) { auto d = new int[][](101,101); foreach(di; d){ di.fill(int.max/1000); } foreach(i; 0..n) { d[i][i] = 0; } foreach(i; 0..m) { auto r2 = readInts(); r2[0..2] -= 1; d[r2[0]][r2[1]] = r2[2]; } foreach(k; 0..n) { foreach(i; 0..n) { foreach(j; 0..n) { d[i][j] = min(d[i][j], d[i][k]+d[k][j]); } } } int ans = int.max; foreach(i; 0..n) { ans = min(ans, d[s][i] + d[i][g[0]] + d[i][g[1]]); } writeln(ans); r1 = readInts(); r1[2..$] -= 1; n = r1[0]; m = r1[1]; s = r1[2]; g = r1[3..$]; } }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); } void main(string[] args){ args ~= ["", ""]; string cmd = args[1]; if(cmd == "-debug") DEBUG = 1; if(cmd == "-gen") gen; else if(cmd == "-jury") jury; else solve; } void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ std.stdio.write(t, " "), print(a); } string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void gen(){ } void jury(){ } void solve(){ int x = scan!int; (1 - x).print; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.typecons; import std.numeric, std.math; import core.bitop; string FMT_F = "%.10f"; T RD(T = long)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = 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 S = RD!string; long[2] cnt; foreach (c; S) { ++cnt[[c].to!long]; } writeln(min(cnt[0], cnt[1]) * 2); stdout.flush(); }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; void times(alias fun)(int n) { foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } long MOD = 10L^^9+7; void main() { int N = readln.chomp.to!int; getPrimes(N).map!(p => N.iota.map!"a+1".map!((n) { long res = 0; while(n%p == 0) { n/=p; res++; } return res; }).fold!((a, b) => (a+b)%MOD)(1L) ).fold!((a, b) => a*b%MOD)(1L).writeln; } int[] getPrimes(int limit) { bool[] isPrimes = new bool[limit+1]; isPrimes[2..$] = true; for (int i=2; i*i<=isPrimes.length; i++) { if (isPrimes[i]) { for (int j=i*i; j<isPrimes.length; j+=i) { isPrimes[j] = false; } } } int[] primes = []; foreach (int i, flg; isPrimes) { if (flg) primes ~= i; } return primes; } // ---------------------------------------------- // 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); } } } } // 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, std.typecons, std.container; import std.math, std.numeric, core.bitop; void main() { int d, g; scan(d, g); auto p = new int[](d); auto c = new int[](d); foreach (i ; 0 .. d) { scan(p[i], c[i]); } int ans = 1<<30; foreach (s ; 0 .. (1<<d)) { int score; int toita; foreach (i ; 0 .. d) { if (s & (1<<i)) { score += 100*(i+1)*p[i]; score += c[i]; toita += p[i]; } } foreach_reverse (i ; 0 .. d) { if (score >= g) { break; } if (s & (1<<i)) { continue; } int t = min(p[i], (g - score + (i+1)*100 - 1) / ((i+1)*100)); score += t*(i+1)*100; toita += t; } ans = min(ans, toita); } 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() { auto D = ri; switch(D) { case 25: writeln("Christmas"); break; case 24: writeln("Christmas Eve"); break; case 23: writeln("Christmas Eve Eve"); break; case 22: writeln("Christmas Eve Eve Eve"); break; default: break; } } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
// 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, P; scan(N, P); auto A = aryread(); long[2] cnt; foreach (a; A) cnt[a & 1]++; if (cnt[1] == 0 && P == 1) { writeln(0); return; } long ans = 2 ^^ cnt[0]; dprint(cnt, ans); long tmp; foreach (x; 0 .. N + 1) { if (cnt[1] < 2 * x + P) break; tmp += combination(cnt[1], 2 * x + P); } writeln(ans * tmp); } /// Number of k-combinations T combination(T = long)(T n, T k) { assert(0 <= k); assert(0 <= n); if (n < k) return 0; k = min(n - k, k); if (k == 0) return 1; if (k == 1) return n; return memoize!(combination!T)(n - 1, k - 1) + memoize!(combination!T)(n - 1, k); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; long[] as = [0]; foreach (i, a; readln.split.to!(long[])) as ~= a + as[i]; auto pq = new int[](N+1); auto rs = new int[](N+1); int i = 2, j = 1; while (i <= N) { while (j+1 < i && abs(as[j]*2 - as[i]) > abs(as[j+1]*2 - as[i])) ++j; pq[i++] = j; } i = N-2, j = N-1; while (i >= 0) { while (j-1 > i && abs(as[j]*2 - as[i] - as[N]) > abs(as[j-1]*2 - as[i] - as[N])) --j; rs[i--] = j; } long r = long.max; foreach (k; 2..N-1) { i = pq[k]; j = rs[k]; r = min(r, max(as[i], as[k]-as[i], as[j]-as[k], as[N]-as[j]) - min(as[i], as[k]-as[i], as[j]-as[k], as[N]-as[j])); } writeln(r); }
D
void main() { int[] tmp = readln.split.to!(int[]); int a = tmp[0], b = tmp[1]; string s = readln.chomp; bool ok = true; foreach (i, x; s) { if (i == a) { if (x != '-') { ok = false; } } else { if (!x.isNumber) { ok = false; } } } writeln(ok ? "Yes" : "No"); } 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
// 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() { auto S = sread(); long Q = lread(); auto reverse = false; DList!char ans; foreach (c; S) ans.insertBack(c); foreach (_; 0 .. Q) { auto q = readln.split(); if (q[0] == "1") { reverse = reverse == false; continue; } long f = q[1] == "2"; auto c = q[2][0]; if (f == reverse) { ans.insertFront(c); } else { ans.insertBack(c); } } if (reverse) { foreach_reverse (c; ans) write(c); } else { foreach (c; ans) write(c); } writeln(); }
D
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.range; void main() { auto N = readln.split[0].to!int; auto H = readln.split.to!(int[]); for(int i = N - 1; i > 0; i--) { if(H[i-1] > H[i]) H[i-1]--; } for (int i = 1; i < N; i++) { if (H[i - 1] > H[i]) { writeln("No"); return; } } writeln("Yes"); }
D
import std.stdio; import std.string; // chomp????????????????????????(?????????????????????) import std.conv; // to???????????????????????? void main() { int x = to!int(chomp(readln())); writeln(x ^^ 3); }
D
void main() { string s = rdStr; bool[] alpha = new bool[26]; if (s.length == 26) { alpha[s[$-1]-'a'] = true; foreach_reverse (i; 0 .. 25) { if (s[i] < s[i+1]) { s[0..i].write; long idx; foreach (j; s[i]-'a'+1 .. 26) { if (alpha[j]) { idx = j; break; } } (idx + 'a').to!dchar.writeln; return; } alpha[s[i]-'a'] = true; } writeln(-1); } else { alpha[] = true; foreach (x; s) { alpha[x-'a'] = false; } writeln(s, (alpha.countUntil(true) + 'a').to!dchar); } } 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.string, std.conv; import std.math; auto solve(string s_) { immutable n = s_.to!long(); immutable s = readln.chomp.to!long(); if(s==n) return s+1; immutable m = sqrt(n.to!real()).to!long+1; foreach(i;2..m) { long v=n, c=0; while(v) c+=v%i, v/=i; if(c==s) return i; } foreach_reverse(i;1..m) { auto v1=n/i, v2=n/(i+1), r=s-i-(n%(n/i)), v=v1-r/i; if(r>=0 && r%i==0 && v2<v && v<=v1 && v>i) return v; } return -1; } void main(){ for(string s; (s=readln.chomp()).length;) writeln(solve(s)); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; class Card{ private: string _name; string _mark; int _value; public: @property{ string name() const { return _name; } int value() const { return _value; } } this(string name){ _name = name; _mark = name[0..1]; _value = name[1].to!int(); } } void BubbleSort(Card[] arr, int n){ for(int i = 0; i < n; ++i){ for(int j = n-1; j > i; --j){ if(arr[j].value < arr[j-1].value) swap(arr[j], arr[j-1]); } } } void SelectionSort(Card[] arr, int n){ for(int i = 0; i < n; ++i){ int minj = i; for(int j = i; j < n; ++j){ if(arr[j].value < arr[minj].value) minj = j; } swap(arr[i], arr[minj]); } } int main(string[] argv) { auto n = readln.chomp.to!int(); Card[] b, s; string[] input = readln.split(); for(int i = 0; i < n; i++){ b ~= new Card(input[i]); s ~= new Card(input[i]); } BubbleSort(b, b.length.to!int()); foreach(i, e; b){ write(e.name); if(i == b.length.to!int() - 1) writeln(); else write(" "); } writeln("Stable"); SelectionSort(s, s.length.to!int()); foreach(i, e; s){ write(e.name); if(i == s.length.to!int() - 1) writeln(); else write(" "); } for(int i = 0; i < n; ++i){ if(b[i].name != s[i].name){ writeln("Not stable"); return 0; } } writeln("Stable"); return 0; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1<<30; enum mod = 10L^^9 + 7; void main() { int n, a; scan(n, a); auto x = readln.split.to!(int[]); x[] -= a; immutable ofs = n * 50; auto dp = new long[](ofs * 2 + 1); dp[x[0] + ofs] = 1; foreach (i ; 1 .. n) { if (x[i] > 0) { foreach_reverse (j ; x[i] .. 2*ofs + 1) { dp[j] += dp[j - x[i]]; } } else { foreach (j ; 0 .. 2*ofs + 1 + x[i]) { dp[j] += dp[j - x[i]]; } } dp[x[i] + ofs]++; } writeln(dp[ofs]); } 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; 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 k, string s) { // 0, 1 でグループ化して個数を記録する int[] g; // 先頭要素が 0 なら、先頭に 0 個の 1 があると見なす if (s.front == '0') g ~= 0; int lo = 0; while (lo < s.length) { int hi = lo; while (hi < s.length && s[lo] == s[hi]) hi++; g ~= hi - lo; lo = hi; } // 累積和 auto sum = new int[g.length]; sum[0] = g[0]; for (int i = 1; i < g.length; i++) { sum[i] = g[i] + sum[i - 1]; } int ans = 0; // 1 の個数, 0 の個数... のパターンで並んでいるので // 各 1 の位置から、2 * k + 1 個取り出す for (int i = 0; i < sum.length; i += 2) { int hi = min(i + 2 * k, cast(int)sum.length - 1); int a = sum[hi] - (i-1 >= 0 ? sum[i-1] : 0); ans = max(ans, a); } return ans; } void main() { auto nk = readints; int k = nk[1]; string s = read!string; writeln(calc(k, s)); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto S = RD!string; auto K = RD; /*string S; foreach (i; 0..3) { S ~= cast(char)(uniform(0, 3) + 'a'); } long _ans; auto K = uniform(1, 4); { string T; foreach (i; 0..K) T ~= S; long i = 1; while (i < T.length) { if (T[i] == T[i-1]) { ++_ans; i += 2; } else { ++i; } } debug writeln("S:", S); debug writeln("K:", K); debug writeln("T:", T); debug writeln("ans:", _ans); }*/ long cnt; long i = 1; while (i < S.length) { if (S[i] == S[i-1]) { ++cnt; i += 2; } else { ++i; } } debug writeln(cnt); long ans; if (S.length == 1) { ans = K/2; } else { if (S[0] != S[$-1]) { ans = cnt*K; } else { long cnt2, cnt3; foreach (c; S) { if (S[0] != c) break; ++cnt2; } foreach_reverse (c; S) { if (S[0] != c) break; ++cnt3; } if (cnt2 == S.length) { ans = S.length * K / 2; } else if (cnt2 % 2 == 1 && cnt3 % 2 == 1) { ans = cnt*K+K-1; } else { ans = cnt*K; } } } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio; import std.array; import std.algorithm; import std.conv; int gcd (int x, int y) { if(x < y) swap(x, y); if(y == 0) return x; return gcd(y, x % y); } void main () { int a, b; while(true) { string[] input = split(readln()); if(input.length == 0) break; a = to!int(input[0]); b = to!int(input[1]); int gcd_ab = gcd(a, b); int lcm_ab = a / gcd_ab * b; writeln(gcd_ab, " ", lcm_ab); } }
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, b; scan(a, b); int ans = max(2*a - 1, 2*b - 1, a + b); 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
/* imports all std modules {{{*/ import std.algorithm, std.array, std.ascii, std.base64, std.bigint, std.bitmanip, std.compiler, std.complex, std.concurrency, std.container, std.conv, std.csv, std.datetime, std.demangle, std.encoding, std.exception, std.file, std.format, std.functional, std.getopt, std.json, std.math, std.mathspecial, std.meta, std.mmfile, std.net.curl, std.net.isemail, std.numeric, std.parallelism, std.path, std.process, std.random, std.range, std.regex, std.signals, std.socket, std.stdint, std.stdio, std.string, std.system, std.traits, std.typecons, std.uni, std.uri, std.utf, std.uuid, std.variant, std.zip, std.zlib; /*}}}*/ /* libmod {{{*/ struct ModCalc(N) if (isIntegral!N) { public: N mod(N n) const { const x = n%m; return x < 0? x+m: x; } N inv(N a) const { N b = m, u = 1, v = 0; while (b) { const t = a / b; a -= t*b; swap(a, b); u -= t*v; swap(u, v); } return mod(u); } N add(N l, N r) const { return mod(mod(l) + mod(r)); } N sub(N l, N r) const { return mod(mod(l) - mod(r)); } N mul(N l, N r) const { return mod(mod(l) * mod(r)); } N div(N l, N r) const { assert(r != 0); return mod(mod(l) * inv(mod(r))); } N m = 1000000007; } /*}}}*/ /+---test 3 2 3 4 ---+/ /+---test 5 12 12 12 12 12 ---+/ /+---test 3 1000000 999999 999998 ---+/ enum M = ModCalc!long(); BigInt gcdm(BigInt a, BigInt b) { if (a < b) swap(a, b); auto r = a%b; while (r) { a = b; b = r; r = a%b; } return b; } void main(string[] args) { const N = readln.chomp.to!long; const A = readln.split.map!(to!long).array; BigInt z = 1; foreach (x; A) { z = z / gcdm(BigInt(x), z) * x; } long ans; foreach (x; A) { ans = M.add(M.div((z%M.m).to!long, x), ans); } ans.writeln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto Q = readln.chomp.to!int; long ans; while (Q--) { auto n = readln.chomp.to!long; if (n % 4 == 0) { writeln(n / 4); } else if (n % 4 == 1) { if (n <= 5) writeln(-1); else writeln((n-9)/4 + 1); } else if (n % 4 == 2) { if (n == 2) writeln(-1); else writeln((n-6)/4 + 1); } else { if (n <= 11) writeln(-1); else writeln((n-15)/4 + 2); } } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; void main() { readln.replace("peach", "~").replace("apple", "peach").replace("~", "apple").write; }
D
import std; enum inf(T)()if(__traits(isArithmetic,T)){return T.max/4;} T scan(T=long)(){return readln.chomp.to!T;} void scan(T...)(ref T args){auto input=readln.chomp.split;foreach(i,t;T)args[i]=input[i].to!t;} T[] scanarr(T=long)(){return readln.chomp.split.to!(T[]);} alias Queue=DList;auto enq(T)(ref Queue!T q,T e){q.insertBack(e);}T deq(T)(ref Queue!T q){T e=q.front;q.removeFront;return e;} alias Stack=SList;auto push(T)(ref Stack!T s,T e){s.insert(e);}T pop(T)(ref Stack!T s){T e=s.front;s.removeFront;return e;} struct UnionFind(T){T[T]u;ulong[T] rank;@property{bool inc(T e){return e in u;}auto size(){return u.keys.length;}auto dup(){T[] child=u.keys;T[] parent=u.values;auto res=UnionFind!T(child);child.each!(e=>res.add(e));size.iota.each!(i=>res.unite(child[i],parent[i]));return res;}}this(T e){e.add;}this(T[] es){es.each!(e=>e.add);}auto add(T a,T b=a){assert(b.inc);u[a]=a;rank[a];if(a!=b)unite(a,b);}auto find(T e){if(u[e]==e)return e;return u[e]=find(u[e]);}auto same(T a,T b){return a.find==b.find;}auto unite(T a,T b){a=a.find;b=b.find;if(a==b)return;if(rank[a]<rank[b])u[a]=b;else{u[b]=a;if(rank[a]==rank[b])rank[a]++;}}} struct PriorityQueue(T,alias less="a<b"){BinaryHeap!(T[],less) heap;@property{bool empty(){return heap.empty;}auto length(){return heap.length;}auto dup(){return PriorityQueue!(T,less)(array);}T[] array(){T[] res;auto tp=heap.dup;foreach(i;0..length){res~=tp.front;tp.removeFront;}return res;}void push(T e){heap.insert(e);}void push(T[] es){es.each!(e=>heap.insert(e));}}T look(){return heap.front;}T pop(){T tp=look;heap.removeFront;return tp;}this(T e){ heap=heapify!(less,T[])([e]);} this(T[] e){ heap=heapify!(less,T[])(e);}} //END OF TEMPLATE void main(){ auto n=scan!ulong; auto a=scanarr!ulong; ulong[ulong] boss; a.each!(i=>boss[i-1]++); n.iota.map!(i=>i in boss?boss[i]:0).each!writeln; }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; void times(alias fun)(int n) { foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } void main() { int po(int x) { if (x==2) { return 0; } else if ([4, 6, 9, 11].any!(a=>a==x)) { return 1; } else { return 2; } } readln.split.to!(int[]).map!po.pipe!"a[0]==a[1]".pipe!(a=>a?"Yes":"No").writeln; } // ---------------------------------------------- // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { auto minElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto minimum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b < minimum) { element = a; minimum = b; } } return element; } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto maximum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b > maximum) { element = a; maximum = b; } } return element; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto AS = readln.split.to!(long[]); if (N == 0) { writeln(AS[0] == 1 ? 1 : -1); return; } long r, c = 1; long[] BS; foreach (a; AS) { r += a; c -= a; if (c < 0) { writeln(-1); return; } BS ~= c; if (c <= c*2) { c *= 2; } else { c = 10L^^18; } } long d; foreach_reverse (i, a; AS[1..$]) { d = min(d + a, BS[i]); r += d; } writeln(r); }
D
import std.stdio; import std.string; import std.conv; import std.range; import std.array; import std.algorithm; void main(){ auto a=readln.chomp.to!int; writeln(a/3); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(string a, string b, string c) { int ans = 0; for (int i = 0; i < a.length; i++) { bool[char] m; m[a[i]] = m[b[i]] = m[c[i]] = true; ans += m.keys.length - 1; } return ans; } void main() { readint; string a = read!string; string b = read!string; string c = read!string; writeln(calc(a, b, c)); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto M = RD; auto ab = new long[2][](N); auto cd = new long[2][](M); foreach (i; 0..N) { ab[i] = [RD, RD]; } foreach (i; 0..M) { cd[i] = [RD, RD]; } foreach (i; 0..N) { long best = long.max, pos; foreach (j; 0..M) { auto d = abs(cd[j][0] - ab[i][0]) + abs(cd[j][1] - ab[i][1]); if (d < best) { best = d; pos = j; } } writeln(pos+1); } stdout.flush(); debug readln(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto T = readln.chomp.to!int; foreach (_t; 0..T) { auto N = readln.chomp.to!long; if (N == 1) { writeln("FastestFinger"); continue; } if (N == 2 || N%2 == 1) { writeln("Ashishgup"); continue; } auto n = N; int t; while (n%2 == 0) { ++t; n /= 2; } if (n == 1) { writeln("FastestFinger"); continue; } if (t > 1) { writeln("Ashishgup"); continue; } int c; for (long k = 3; k^^2 <= N; k += 2) { while (n%k == 0) { ++c; n /= k; } } if (n != 1) { ++c; } writeln(c == 1 ? "FastestFinger": "Ashishgup"); } }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!long).array; long ans = A.map!(a => abs(a)).sum; if (A.map!(a => a < 0).sum % 2 == 1) { ans -= A.map!(a => abs(a)).reduce!min * 2; } ans.writeln; }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; immutable mod = 10^^9 + 7; void main() { int n, k; scan(n, k); auto x = new int[](n), y = new int[](n); foreach (i ; 0 .. n) { scan(x[i], y[i]); } long ans = long.max; if (k == 2) { foreach (i ; 0 .. n) { foreach (j ; i + 1 .. n) { int xl = min(x[i], x[j]), xr = x[i] + x[j] - xl; int yb = min(y[i], y[j]), yt = y[i] + y[j] - yb; ans = min(ans, 1L * (xr - xl) * (yt - yb)); } } } else if (k == 3) { foreach (i ; 0 .. n) { foreach (j ; i + 1 .. n) { foreach (l ; j + 1 .. n) { int xl = [x[i], x[j], x[l]].reduce!min, xr = [x[i], x[j], x[l]].reduce!max; int yb = [y[i], y[j], y[l]].reduce!min, yt = [y[i], y[j], y[l]].reduce!max; ans = min(ans, 1L * (xr - xl) * (yt - yb)); } } } } else { foreach (i ; 0 .. n) { foreach (j ; i + 1 .. n) { foreach (l ; j + 1 .. n) { foreach (m ; l + 1 .. n) { int xl = [x[i], x[j], x[l], x[m]].reduce!min; int xr = [x[i], x[j], x[l], x[m]].reduce!max; int yb = [y[i], y[j], y[l], y[m]].reduce!min; int yt = [y[i], y[j], y[l], y[m]].reduce!max; int cnt; foreach (a ; 0 .. n) { if (xl <= x[a] && x[a] <= xr && yb <= y[a] && y[a] <= yt) cnt++; } if (cnt >= k) { ans = min(ans, 1L * (xr - xl) * (yt - yb)); } } } } } } writeln(ans); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
void main() { long[] a = readln.split.to!(long[]); writeln(a.sum >= 22 ? "bust" : "win"); } 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