code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 10^^9 + 7; void main() { int n; scan(n); auto a = new int[](n); auto b = new int[](n); auto c = new int[](n); iota(n).each!(i => scan(a[i], b[i], c[i])); auto dp = new int[][](n + 1, 4); fillAll(dp, -inf); dp[0][0] = 0; foreach (i ; 0 .. n) { foreach (j ; 0 .. 4) { if (j != 1) { dp[i+1][1] = max(dp[i+1][1], dp[i][j] + a[i]); } if (j != 2) { dp[i+1][2] = max(dp[i+1][2], dp[i][j] + b[i]); } if (j != 3) { dp[i+1][3] = max(dp[i+1][3], dp[i][j] + c[i]); } } } auto ans = max(dp[n][1], dp[n][2], dp[n][3]); writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.functional; alias memoize!(c) cm; ulong c(ulong n, ulong k){ if(k==0 || n==k) return 1; return cm(n-1, k) + cm(n-1, k-1); } int main(){ auto s = readln; ulong[char] maji; foreach(i; 0..s.length-1){ maji[s[i]]++; } bool shinu = false; foreach(i; maji){ if(i%2){ if(shinu){ 0.writeln; return 0; } shinu = true; } } ulong[char] anman; ulong total, kazoeruyatu = 1; foreach(i,j; maji) total += (anman[i] = j%2 ? (j-1)/2 : j/2); foreach(i; anman){ kazoeruyatu *= cm(total, i); total -= i; } kazoeruyatu.writeln; return 0; }
D
// Cheese-Cracker: cheese-cracker.github.io void theCode(){ int n = scan!int; auto w = scan!(dchar[]); int l1, l2, r1, r2; for(int i = n/2; i < n; ++i){ if(w[i] == '0'){ l1 = 0; r1 = i; l2 = 0; r2 = i-1; writeln(l1+1, " ", r1+1, " ", l2+1, " ", r2+1); return; } } for(int i = 0; i < n/2; ++i){ if(w[i] == '0'){ l1 = i; r1 = n-1; l2 = i+1; r2 = n-1; writeln(l1+1, " ", r1+1, " ", l2+1, " ", r2+1); return; } } for(int i = 0; i < n; ++i){ assert(w[i] == '1'); } l1 = 0; r1 = n/2; l2 = 1; r2 = min(n/2 + 1, n-1); writeln(l1+1, " ", r1+1, " ", l2+1, " ", r2+1); } void main(){ long tests = scan; // Toggle! while(tests--) theCode(); } /********* That's All Folks! *********/ import std.stdio, std.random, std.functional, std.container, std.algorithm; import std.numeric, std.range, std.typecons, std.string, std.math, std.conv; string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;} T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; } void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } } alias ll = long, tup = Tuple!(long, "x", long, "y");
D
import core.bitop; 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 Pass = Tuple!(long, "x", long, "y"); long[MAX] fac; long[MAX] inv; long[MAX] finv; void combinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; foreach (i; iota(2, MAX)) { fac[i] = i * fac[i - 1] % MOD; inv[i] = MOD - (inv[MOD % i] * (MOD / i)) % MOD; finv[i] = inv[i] * finv[i - 1] % MOD; } } auto comb(long n, long k) { if (n < k || n < 0 || k < 0) return 0; else return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } void main() { long x, y; scan(x, y); long fst, snd; fst = 2 * x - y; snd = 2 * y - x; if (fst < 0 || snd < 0) writeln(0); else if (fst % 3 || snd % 3) writeln(0); else { fst /= 3; snd /= 3; combinit(); (fst + snd).comb(fst).writeln(); } } 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
import std.conv, std.stdio; import std.algorithm, std.array, std.range, std.string; import std.numeric; void main() { auto n = readln.chomp.to!ulong; if (n & 1) return 0.writeln; n /= 2; ulong ret; while (n) { n /= 5; ret += n; } ret.writeln; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); long bignum = 1_000_000_007; T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { long n, m; scan(n, m); abs((n - 2) * (m - 2)).writeln(); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.math; import std.regex; void main() { auto N = readln.chomp.to!int; int count; foreach(_; 0..N) { auto ip = readln.split.to!(int[]); count += ip[1] - ip[0] + 1; } writeln(count); }
D
void main() { string s = rdStr; string f(string x, dchar y) { string t; long len = x.length.to!long - 1; foreach (i; 0 .. len) { if (x[i] == y || x[i+1] == y) t ~= y; else t ~= s[i]; } return t; } long result = inf; foreach (c; lowercase) { string u = s; long cnt; while (u.any!(x => x != c)) { u = f(u, c); ++cnt; } result = min(result, cnt); } 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 wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.conv, std.string; void main(){ auto ip = readln.split.to!(int[]); if(ip[0] == 2 || ip[1] == 2){ writeln("No"); } else { writeln("Yes"); } }
D
import std.stdio, std.algorithm, std.array, std.string, std.conv; void main() { int t; scanf("%d", &t); getchar(); foreach(_; 0..t) { int n; scanf("%d", &n); getchar(); auto str1 = readln.strip(); auto str2 = readln.strip(); bool flag = true; for (int i = 0; i < n; i++) { if ((str1[i] == str2[i]) && (str1[i] == '1')) { writeln("NO"); flag = false; break; } } if (flag) writeln("YES"); } }
D
import std.algorithm; import std.array; import std.ascii; 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 log(A...)(A arg) { stderr.writeln(arg); } int size(T)(in T s) { return cast(int)s.length; } void main() { auto s = readln.chomp; int x = 0; foreach (c; s) { if (c == 'C') { if (x == 0) x++; } else if (c == 'F') { if (x == 1) x++; } } writeln(x == 2 ? "Yes" : "No"); }
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(){ long n,m; scan(n,m); auto a=scanarr!long; auto ans=n-a.sum; (ans<0?-1:ans).writeln; }
D
import std.stdio: writeln; void main() { foreach(int i; 0 .. 1000) { writeln("Hello World"); } }
D
import std; int calc(long x) { long s = 100; int k = 0; while (s < x) { s += s / 100; k++; } return k; } void main() { long x; scan(x); writeln(calc(x)); } void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int;
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { string s; while ((s = readln.chomp) != null) { auto ab = s.split.map!(to!int).array; int a = ab[0], b = ab[1]; writeln(gcd(a, b)); } }
D
// import chie template :) {{{ import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii; // }}} // tbh.scanner {{{ class Scanner { import std.stdio; import std.conv : to; import std.array : split; import std.string : chomp; private File file; private dchar[][] str; private uint idx; this(File file = stdin) { this.file = file; this.idx = 0; } private dchar[] next() { if (idx < str.length) { return str[idx++]; } dchar[] s; while (s.length == 0) { s = file.readln.chomp.to!(dchar[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(uint len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } } // }}} void main() { auto cin = new Scanner; auto s = cin.next!(dchar[]); int res; foreach (c; s) { if (c == '1') ++res; } writeln(res); }
D
import std.stdio; import std.string; import std.array; // split import std.conv; // to void main() { string s = chomp(readln()); write("ABC"); writeln(s); }
D
import std.stdio; void main() { int n, m; scanf("%d %d\n", &n, &m); auto add = new int[n]; auto sub = new int[n]; foreach (i; 0..m) { int l, r; scanf("%d %d\n", &l, &r); ++add[l-1]; ++sub[r-1]; } int y, cnt; foreach(i; 0..n) { y += add[i]; if (y == m) ++cnt; y -= sub[i]; } cnt.write; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int N, M; scan(N, M); auto deg = new int[](N); foreach (i ; 0 .. M) { int ai, bi; scan(ai, bi); ai--, bi--; deg[ai]++; deg[bi]++; } deg.each!(d => d.writeln); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; const long MOD = 998244353; const long INF = 1L << 59; void main() { auto N = readln.chomp.to!int; auto G = new int[][](N); foreach (_; 0..N) { auto s = readln.split.map!(to!int); G[s[0] - 1] ~= s[1] - 1; G[s[1] - 1] ~= s[0] - 1; } auto loop = new bool[](N); auto used = new bool[](N); auto prev = new int[](N); fill(prev, -1); bool dfs(int n, int p) { prev[n] = p; used[n] = true; foreach (m; G[n]) { if (used[m] && m != p) { for (int x = n; ; x = prev[x]) { loop[x] = true; if (x == m) break; } return true; } if (m != p && dfs(m, n)) { return true; } } return false; } dfs(0, -1); auto Q = readln.chomp.to!int; while (Q--) { auto s = readln.split.map!(to!int); writeln( (loop[s[0] - 1] && loop[s[1] - 1]) ? 2 : 1 ); } }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; import std.array; int n, m; rd(n, m); auto g=new int[][](n, n); foreach(_; 0..m){ int a, b; rd(a, b); g[a-1][b-1]=g[b-1][a-1]=1; } auto v=new int[](0); v~=0; auto usd=new bool[](n); usd[0]=true; int f(int k){ if(k==n){ bool ok=true; foreach(i; 1..n){ ok&=g[v[i-1]][v[i]]==1; } return ok ? 1 : 0; }else{ int ret=0; foreach(j; 1..n)if(!usd[j]){ v~=j; usd[j]=true; ret+=f(k+1); v.popBack; usd[j]=false; } return ret; } } writeln(f(1)); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int N; scan(N); auto row = new int[](N); auto col = new int[](N); iota(N).each!(i => scan(row[i], col[i])); auto dp = new int[][](N + 1, N + 1); fillAll(dp, -1); int rec(int l, int r) { if (r - l == 1) { return 0; } if (dp[l][r] != -1) { return dp[l][r]; } dp[l][r] = inf; foreach (i ; l + 1 .. r) { chmin(dp[l][r], rec(l, i) + rec(i, r) + row[l] * row[i] * col[r - 1]); } return dp[l][r]; } auto ans = rec(0, N); writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } double euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); } double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; } double norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); } double dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; } //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 string[][](t); foreach (ti; 0..t) { auto n = RD!int; auto k = RD!int; if (k > (n+1)/2) continue; ans[ti].length = n; foreach (i; 0..n) { foreach (j; 0..n) { if (i/2 < k && i % 2 != 1) { if (i == j) ans[ti][i] ~= "R"; else ans[ti][i] ~= "."; } else ans[ti][i] ~= "."; } } } foreach (e; ans) { if (e.empty) writeln(-1); else { foreach (ee; e) writeln(ee); } } stdout.flush; debug readln; }
D
import std; auto input() { return readln().chomp(); } alias sread = () => readln.chomp(); alias aryread(T = long) = () => readln.split.to!(T[]); void main() { long n, m; scan(n, m); auto ACしたか = new bool[](n + 1); // ACしたか[n] = false; // writeln(ACしたか); auto 何回WAしたか = new long[](n + 1); long ans1 = 0; long ans2 = 0; foreach (_; 0 .. m) { long p; string s; scan(p, s); // writeln(p, s); if ((ACしたか[p] == false) && (s == "WA")) { 何回WAしたか[p] += 1; } if (s == "AC") { ACしたか[p] = true; } } foreach (i; 0 .. n + 1) { if (ACしたか[i] == true) ans1 += 何回WAしたか[i]; } foreach (i; 0 .. n + 1) { if (ACしたか[i] == true) { ans2 += 1; } } writeln(ans2, ' ', ans1); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
D
import std.stdio; import std.conv; import std.string; void main(){ auto A =readln.chomp.to!int; if(A<1200){ "ABC".writeln; }else{ "ARC".writeln; } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } 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 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 main() { auto a = RD; auto b = RD; string ans; foreach (i; 0..max(a, b)) ans ~= to!string(min(a, b)); 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, core.stdc.stdio; void main() { auto T = readln.chomp.to!int; auto cnt = new int[](10); while (T--) { cnt[] = 0; auto S = readln.chomp; auto N = S.length.to!int; int zero = 0; int even = 0; int div3 = 0; foreach (i; 0..N) { int d = S[i]-'0'; if (d == 0) zero += 1; if (d % 2 == 0) even += 1; div3 = (div3 + d) % 3; } if (zero >= 1 && even >= 2 && div3 == 0) { writeln("red"); } else { writeln("cyan"); } } }
D
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array, std.functional; int happiness(string s) { int r = 0; foreach(i; 0..s.length) { if (s[i] == 'L' && i != 0 && s[i-1] == 'L') r++; if (s[i] == 'R' && i != s.length - 1 && s[i+1] == 'R') r++; } return r; } string inverse(string s, int l, int r) { assert(l<=r); char[] res = s.dup; foreach(i; 0..(r-l+1)) { if(s[l+i] == 'L') res[r-i] = 'R'; if(s[l+i] == 'R') res[r-i] = 'L'; } return cast(string)res; } void main() { auto input = readln.split.to!(int[]); auto N = input[0], K = input[1]; auto S = readln.split[0]; auto h = happiness(S); auto num = 1; foreach(i; 1..S.length) { if(S[i] != S[i-1]) num++; } while(K > 0 && num >= 3) { K--; num-=2; h+=2; } if(K > 0 && num == 2) { K--; num--; h++; } h.writeln; }
D
import std.stdio; import std.conv; import std.algorithm; import std.range; import std.string; import std.typecons; import std.math; import std.range; void main() { readln; auto a = readln().strip.split(" ").map!(to!int); auto b = new typeof(a[0])[10000]; foreach (i; a) { b[i]++; } bool aaf = false; foreach (i, j; b) { foreach (k; iota(j)) { write(aaf ? " " : "", i); aaf = true; } } 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; readV(n); int[] a; readA(n, a); auto m = 10^^9; foreach (x; a.reduce!min..a.reduce!max+1) m = min(m, a.map!(ai => (x-ai)^^2).sum); writeln(m); }
D
void main() { auto S = rs; string A = S; char[] arr = ['0', '1']; ulong k = 0; ulong res = ulong.max, cnt; foreach(i, v; A) { if(v != arr[i % 2]) { cnt++; } } res = cnt; cnt = 0; foreach(i, v; A) { if(v != arr[(i+1) % 2]) { cnt++; } } res = min(res, cnt); res.writeln; } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; long[100] WS, VS; void main() { auto nw = readln.split.to!(int[]); auto N = nw[0]; auto W = nw[1]; long[][] MEMO; MEMO.length = N; foreach (i; 0..N) { auto wv = readln.split.to!(long[]); WS[i] = wv[0]; VS[i] = wv[1]; MEMO[i].length = 10^^5+1; } long solve(int i, long v) { if (!v) return 0; if (i == N) return (long.max / 4) ; if (MEMO[i][v]) return MEMO[i][v]; return MEMO[i][v] = min( solve(i+1, v), v < VS[i] ? (long.max / 4) : solve(i+1, v-VS[i]) + WS[i] ); } foreach_reverse (v; 0..10^^5+1) { if (solve(0, v) <= W) { writeln(v); return; } } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.numeric; void main() { auto n = readln.chomp.to!int; auto a = new int[](n); foreach (i; 0..n) { a[i] = readln.chomp.to!int; } auto a2 = a.dup; a2.sort!((a,b)=>a > b); foreach (i; 0..n) { if (a[i] == a2[0]) { writeln(a2[1]); } else { writeln(a2[0]); } } }
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, c; readV(a, b, c); writeln(b-a == c-b ? "YES" : "NO"); }
D
import std; const MOD = 998244353; const N = 2 * 10^^5; long calc(int n, int m, int k) { auto binom = new Binom(N); long ans = 0; foreach (i; 0..k+1) { ans += (m * modpow(m-1, n-i-1) % MOD) * binom(n-1, i) % MOD; ans %= MOD; } return ans; } void main() { int n, m, k; scan(n, m, k); writeln(calc(n, m, k)); } void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T=string)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readints = reads!int; long modpow(long x, long k) { if (k == 0) return 1; if (k % 2 == 0) return modpow(x * x % MOD, k / 2); return x * modpow(x, k - 1) % MOD; } class Binom { private long[] _fact; private long[] _invfact; this(size_t size) { _fact = new long[size + 10]; _invfact = new long[size + 10]; init(); } private void init() { auto n = _fact.length; _fact[0] = 1; foreach (i; 1..n) _fact[i] = _fact[i-1] * i % MOD; _invfact[n-1] = modpow(_fact[n-1], MOD-2); foreach_reverse (i; 0..n-1) _invfact[i] = _invfact[i+1] * (i+1) % MOD; } long get(int n, int k) const { return (_fact[n] * _invfact[k] % MOD) * _invfact[n-k] % MOD; } long opCall(int n, int k) const { return (_fact[n] * _invfact[k] % MOD) * _invfact[n-k] % MOD; } }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { long N = lread(); auto s = sread(); long result; foreach (i; 0 .. N) { uint a, b; foreach (c; s[0 .. i]) a |= 1 << (c - 'a'); foreach (c; s[i .. $]) b |= 1 << (c - 'a'); result.maxAssign((a & b).popcnt); } writeln(result); }
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 k, a, b; scan(k, a, b); if (b - a <= 2) { writeln(k + 1); return; } if (k < a - 1) { writeln(k + 1); return; } long ans = a; k -= a - 1; ans += (b - a) * (k / 2); if (k & 1) ans++; writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
void main() { string s = readln.chomp; string t = "keyence"; bool ok; foreach (i; 0 .. 8) { if (s[0..i] == t[0..i] && s[$-7+i..$] == t[i..$]) { ok = true; } } 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
void main() { auto N = readAs!ulong; auto arr = 5.iota.map!(i => readAs!ulong); writeln((ceil(N /arr.reduce!min.to!real) + 4).to!ulong); } // =================================== 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.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; struct Matrix(size_t height, size_t width) { long M; long[width][height] arr; this(long[width][height] arr, long M = 10L^^9+7) { this.arr = arr; this.M = M; } pure Matrix!(height, rhs_width) opBinary(string op, size_t rhs_width)(Matrix!(width, rhs_width) rhs) { static if (op == "*") { long[rhs_width][height] res; foreach (y; 0..height) { foreach (x; 0..rhs_width) { foreach (i; 0..width) { res[y][x] = (res[y][x] + (this.arr[y][i] * rhs.arr[i][x]) % M) % M; } } } return Matrix!(height, rhs_width)(res, M); } else { static assert(0, "Operator "~op~" not implemented"); } } pure Matrix!(height, width) opBinary(string op)(long n) { static if (op == "^^" && height == width) { long[width][height] rr; foreach (i; 0..width) rr[i][i] = 1; auto r = Matrix!(height, width)(rr, M); auto x = this; while (n) { if (n%2 == 1) r = r * x; x = x * x; n /= 2; } return r; } else { static assert(0, "Operator "~op~" not implemented"); } } } Matrix!(h, w) matrix(size_t h, size_t w)(long[w][h] arr, long M = 10L^^9+7) { return Matrix!(h, w)(arr, M); } void main() { auto labm = readln.split.to!(long[]); auto L = labm[0]; auto A = labm[1]; auto B = labm[2]; auto M = labm[3]; long[18] CS; if (A < 10) CS[0] = (10-A-1)/B+1; foreach (i; 1..18) { auto dm = 10L^^(i+1)-1-A; if (dm < 0) continue; auto last = B*(L-1); CS[i] = ((dm > last) ? last : dm)/B+1; foreach (j; 0..i) CS[i] -= CS[j]; if (dm >= last) break; } long[3][1] xss = [[0, A, 1]]; auto xs = matrix(xss, M); foreach (i, d; CS) { long[3][3] cm = [ [(10L^^(i+1)%M).to!long, 0, 0], [1L, 1, 0], [0L, B, 1] ]; auto m = matrix(cm, M); m = m^^d; xs = xs * m; } writeln(xs.arr[0][0]); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; long[10^^5] HS, MEMO; void main() { auto N = readln.chomp.to!int; foreach (i, h; readln.split.to!(long[])) HS[i] = h; MEMO[0..N] = -1; long solve(int i) { if (MEMO[i] != -1) return MEMO[i]; if (i == N-1) return 0; if (i == N-2) return MEMO[i] = solve(i+1) + abs(HS[i] - HS[i+1]); return MEMO[i] = min(solve(i+1) + abs(HS[i] - HS[i+1]), solve(i+2) + abs(HS[i] - HS[i+2])); } writeln(solve(0)); }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std; // dfmt on void main() { long N = lread(); auto T = new long[](N); auto X = new long[](N); auto Y = new long[](N); foreach (i; 0 .. N) scan(T[i], X[i], Y[i]); long t; long x, y; foreach (i; 0 .. N) { long d = abs(x - X[i]) + abs(y - Y[i]); if ((T[i] - t) < d) { writeln("No"); return; } if (((T[i] - t) - d) & 1) { writeln("No"); return; } t = T[i]; x = X[i]; y = Y[i]; } writeln("Yes"); }
D
void main() { int n = readln.chomp.to!int; int[] a = readln.split.to!(int[]); int cnt; foreach (x; a) { while (x % 2 == 0) { x /= 2; ++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 core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Clue = Tuple!(long, "x", long, "y", long, "h"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); long cnt; void main() { long n, x, y; scan(n, x, y); x--, y--; if (x > y) swap(x, y); auto dist = new long[](n - 1); foreach (i; iota(n)) { foreach (j; iota(i + 1, n)) { dist[min(j - i, abs(x - i) + abs(y - j) + 1) - 1]++; } } foreach (e; dist) e.writeln(); } bool is_mindist(long i, long j, long x, long y, long dist) { return dist == min(j - i, abs(x - i) + abs(y - j) + 1); } 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 core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { long x, t; scan(x, t); writeln((x - t).max(0)); }
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 HS = readln.split.to!(int[]); int c, max_c; foreach_reverse (i; 0..N) { if (i+1 == N) continue; if (HS[i] >= HS[i+1]) { ++c; } else { max_c = max(c, max_c); c = 0; } } writeln(max(c, max_c)); }
D
/* imports all std modules {{{*/ import std.algorithm, std.array, std.ascii, std.base64, std.bigint, std.bitmanip, std.compiler, std.complex, std.concurrency, std.container, std.conv, std.csv, std.datetime, std.demangle, std.encoding, std.exception, std.file, std.format, std.functional, std.getopt, std.json, std.math, std.mathspecial, std.meta, std.mmfile, std.net.curl, std.net.isemail, std.numeric, std.parallelism, std.path, std.process, std.random, std.range, std.regex, std.signals, std.socket, std.stdint, std.stdio, std.string, std.system, std.traits, std.typecons, std.uni, std.uri, std.utf, std.uuid, std.variant, std.zip, std.zlib; /*}}}*/ /+:---test RUDLUDR ---+/ /+---test DULL ---+/ void main(string[] args) { const S = readln.chomp; foreach (i, c; S) { ++i; if ((i%2 == 1 && c == 'L') || (i%2 == 0 && c == 'R')) { "No".writeln; return; } } "Yes".writeln; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto rd = readln.split.to!(int[]), n = rd[0], a = rd[1], b = rd[2]; writeln(min(a*n, b)); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto T = RD!int; auto ans = new long[](T); foreach (i; 0..T) { auto n = RD!int; auto k = RD!int; auto a = RDR.ARR; auto diff = new long[](n-k); foreach (j; k..n) { diff[j-k] = a[j] - a[j-k]; } auto pos = MIN_POS(diff); ans[i] = (a[pos+k] + a[pos]) / 2; } foreach (e; ans) writeln(e); stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto A = RD; auto B = RD; long ans; foreach (i; 1..N+1) { auto str = i.to!string; long cnt; foreach (c; str) { cnt += [c].to!long; } if (cnt >= A && cnt <= B) { ans += i; debug writeln(i); } } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio; import std.string; import std.conv; void main() { string[] inputs = split(readln()); int A = to!int(inputs[0]); int B = to!int(inputs[1]); if(A == B) "Draw".writeln; else if(A == 1 && B > 1) "Alice".writeln; else if(B == 1 && A > 1) "Bob".writeln; else if(A > B) "Alice".writeln; else "Bob".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, std.bitmanip; immutable long MOD = 10^^9 + 7; void main() { auto N = readln.chomp.to!long; if (N <= 2) { writeln("No"); return; } long S = N * (N - 1) / 2; foreach_reverse (i; 2..N+1) { if (gcd(S-i, i) > 1) { writeln("Yes"); writeln(1, " ", i); write(N-1); foreach (j; 1..N+1) if (j != i) write(" ", j); writeln; return; } } writeln("No"); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { readln.split.to!(int[]).reduce!"(a + b) % 24".writeln; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.range, std.string, std.math, std.typecons; void main() { auto nm = readln.split.to!(long[]); auto N = nm[0]; auto M = nm[1]; long ret; if (N == 1 && M == 1) { ret = 1; } else if (N == 1) { ret = M - 2; } else if (M == 1) { ret = N - 2; } else if (N == 2 || M == 2) { ret = 0; } else { ret = N * M - N * 2 - M * 2 + 4; } writeln(ret); }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons, std.numeric; void main() { auto tmp = readln.split.to!(long[]); auto N = tmp[0], X = tmp[1]; auto xs = readln.split.to!(long[]); xs.map!(x => abs(x - X)).reduce!((a,b) => gcd(a,b)).writeln; }
D
import std.stdio; import std.conv; import std.string; import std.algorithm; import std.range; import std.functional; import std.math; import core.bitop; void main() { auto input = readln.chomp.split.to!(long[]); long n = input[0]; long m = input[1]; long x = input[2]; auto as = readln.chomp.split.to!(long[]); long Z; long N; foreach (a; as) { if (a < x) { Z++; } else { N++; } } min(Z,N).writeln(); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); void main() { long a, b, c; scan(a, b, c); long max_x = max(a, b, c); long sum_x = a + b + c; long x, y; if (((max_x % 2 == 0) && (sum_x % 2 == 0)) || (max_x % 2 != 0) && (sum_x % 2 != 0)) { x = (max_x * 3 - sum_x) / 2; writeln(x); return; } y = ((max_x * 3 + 3) - sum_x) / 2; writeln(y); } //https://rclone.org/ void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; void main() { int n, m; scanf("%d %d\n", &n, &m); auto r = new int[m]; foreach(i;0..n) { auto input = readln.chomp.split(" ").to!(int[]); foreach(a;input[1..$]) ++r[a-1]; } size_t c; foreach(a; r) if (a == n) ++c; c.write; }
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!(string, "s", long, "p"); alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less); void main() { auto s = sread(); if (s[0 .. $ - 1].all_eq() || s[1 .. $].all_eq()) writeln("Yes"); else writeln("No"); } bool all_eq(string s) { auto t = s[0]; foreach (e; s) { if (e != t) return false; } return true; } 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; import core.bitop; 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, "x", long, "c"); alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less); void main() { auto s = sread(); auto t = sread(); auto nexts = new long[][](s.length, 26); foreach (ref e; nexts) e[] = INF; foreach_reverse (i; iota(2 * s.length)) { foreach (j; iota(26)) { if (s[(i + 1) % s.length] == 'a' + j) nexts[i % s.length][j] = 1; else nexts[i % s.length][j] = nexts[(i + 1) % s.length][j] + 1; } } long idx = s.countUntil(t[0]); foreach (e; t[1 .. $]) { if (idx < 0) break; if (nexts[idx % s.length][e - 'a'] <= s.length) idx += nexts[idx % s.length][e - 'a']; else idx = -1; } writeln(idx >= 0 ? idx + 1 : idx); } 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; import std.conv; import std.array; void main() { int[] T = new int[5]; foreach (i; 0..5){ T[i] = readln.split[0].to!int; } int ans = 0; int minMod = 0; foreach (t; T){ if (t % 10 == 0){ ans += t; } else { if (minMod == 0 || t % 10 < minMod){ minMod = t % 10; } ans += (t / 10 + 1) * 10; } } if (minMod != 0){ ans -= (10 - minMod); } writeln(ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto ab = readln.split.to!(int[]); writeln(ab[0] * ab[1]); }
D
void main(){ int[] hen = _scanln(); (hen[0]*hen[1]/2).writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio; import std.string; import std.conv; void main() { // input int n = to!int(readln.chomp); int k = to!int(readln.chomp); int[] x, y; x.length = y.length = k; for(int i=0; i<k; ++i) { auto temp = readln.split; x[i] = to!int(temp[0]); y[i] = to!int(temp[1]); } // output for(int i=0; i<k; ++i) { if(x[i] > n / 2) x[i] = n - x[i] + 1; if(y[i] > n / 2) y[i] = n - y[i] + 1; int min = x[i] < y[i] ? x[i] : y[i]; writeln((min + 2) % 3 + 1); } }
D
import std.stdio, std.string, std.conv; import std.typecons; import std.algorithm, std.array, std.range, std.container; import std.math; void main() { auto N = readln.split[0].to!int; foreach (n; 1 .. 10) { if (N <= n*111) { writeln(n*111); return; } } }
D
import std.stdio; import std.conv; import std.string; import std.format; void main() { string s = chomp(readln()); int min = to!int(s.split(" ")[0]); int max = to!int(s.split(" ")[1]); int val = to!int(s.split(" ")[2]); int result = 0; for (int i = min; i <= max; i++ ) { if (val % i == 0) { result++; } } writeln(result); }
D
import std.conv, std.stdio; import std.algorithm, std.range, std.string; void main() { immutable n = readln.chomp.to!size_t; size_t[immutable(ubyte)[]] d; foreach (i; 0..n) { auto s = cast(ubyte[])(readln.chomp); auto k = s.sort().array.idup; if (auto p = k in d) (*p)++; else d[k] = 1; } ulong answer; foreach (v; d.byValue) answer += v*(v-1)/2; answer.writeln; }
D
import std.stdio: writeln, writefln, readln; import std.array: array, split; import std.algorithm: map; import std.string: chomp; import std.conv: to; void main() { int[3][3] bingo; foreach(byte i; 0..3) { int[] input = readln.split.map!(to!int).array; foreach(int j; 0..3) bingo[i][j] = input[j]; } int n = readln.chomp.to!int; bool[int] won; foreach(_; 0..n) { int x = readln.chomp.to!int; won[x] = true; } // Check rows foreach(int[3] i; bingo) { if (won.get(i[0], false) && won.get(i[1], false) && won.get(i[2], false)) { writeln("Yes"); return; } } // Check columns foreach(byte i; 0..3) { if (won.get(bingo[0][i], false) && won.get(bingo[1][i], false) && won.get(bingo[2][i], false)) { writeln("Yes"); return; } } bool diagonal_a = true; // Check a diagonal foreach(byte i; 0..3) diagonal_a = diagonal_a && won.get(bingo[i][i], false); // The other diagonal bool diagonal_b = true; foreach(byte i; 0..3) diagonal_b = diagonal_b && won.get(bingo[i][2-i], false); if (diagonal_a || diagonal_b) writeln("Yes"); else writeln("No"); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Clue = Tuple!(long, "x", long, "y", long, "h"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); long cnt; void main() { auto s = sread(); if(s[2] == s[3] && s[4] == s[5]) writeln("Yes"); else writeln("No"); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.stdio, std.string, std.range, std.conv, std.algorithm; void main(){ int s; foreach (i; 0..5) { s += max(readln.chomp.to!int, 40); } writeln(s / 5); }
D
void main() { problem(); } void problem() { auto N = scan!int; auto K = scan!int; void solve() { bool[int] snk; foreach(i; 1..N+1) { snk[i] = true; } foreach(_; 0..K) { auto d = scan!int; foreach(i; scan!int(d)) { snk[i] = false; } } int answer; foreach(key; snk.keys()) { if (snk[key] == true) answer++; } writeln(answer); } solve(); } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
import core.bitop; import std.algorithm; import std.array; import std.ascii; import std.container; import std.conv; import std.format; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; void main() { string s = readln.chomp; string t = readln.chomp; string ans = '~'.repeat.take(s.length).array; foreach (i; 0 .. s.length - t.length + 1) { bool ok = true; foreach (j, c; t) { ok &= s[i + j] == '?' || s[i + j] == c; } if (ok) { char[] sub = s.map!(a => cast(char)(a == '?' ? 'a' : a)).array; foreach (j, c; t) { sub[i + j] = c; } ans = min(ans, sub.idup); } } if (ans.front == '~') { "UNRESTORABLE".writeln; return; } ans.writeln; }
D
import std.stdio; import std.string; import std.conv; void main() { int R = to!int(chomp(readln())); int G = to!int(chomp(readln())); // (R + P)/2 = G // R + P = 2G // P = 2G - R writeln(2*G - R); }
D
void main(){ int[] abc = _scanln(); writeln(abc.count(5)==2 && abc.count(7)==1? "YES": "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
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv, std.string; // import dcomp.foundation, dcomp.scanner; int main() { Scanner sc = new Scanner(stdin); string s; sc.read(s); if (s.startsWith("YAKI")) { writeln("Yes"); } else { writeln("No"); } return 0; } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; if (f.eof) return false; line = lineBuf[]; f.readln(line); } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } version (X86) static if (__VERSION__ < 2071) { import core.bitop : bsf, bsr, popcnt; int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } }
D
import std.stdio, std.range, std.conv, std.string; import std.algorithm.comparison, std.algorithm.iteration, std.algorithm.mutation, std.algorithm.searching, std.algorithm.setops, std.algorithm.sorting; void main() { long s = readln().strip.to!(long); long[] list; list ~= s; while(1) { auto p = list[$-1]; auto n = p % 2 == 0 ? p/2 : p*3+1; foreach(e; list) { if(e==n){ writeln(1+list.length); return; } } list ~= n; } }
D
import std.stdio, std.string, std.conv, std.range, std.algorithm; void main() { auto N = readln.chomp.to!int; auto K = readln.chomp.to!int; auto X = readln.chomp.to!int; auto Y = readln.chomp.to!int; if (N < K) { writeln(N * X); } else { writeln(K * X + (N - K) * Y); } }
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; Tuple!(int,string)[] insort(Tuple!(int,string)[] arr){ for(int i=1;i<arr.length;i++){ int j = i; while(arr[j][0] > arr[j-1][0]){ swap(arr[j],arr[j-1]); j--; if(j==0) break; } } return arr; } void main(){ int n =readln().chomp().to!int; while(true){ Tuple!(int,string)[] list; for(int i=0;i<n;i++){ auto s = readln().split(); list ~= tuple( to!int(s[1])*3 + to!int(s[3]),s[0]); } list = insort(list); foreach(i;list) writeln(i[1],",",i[0]); n =readln().chomp().to!int; if(n==0)break; writeln(); } }
D
void main() { string s = readln.chomp; string t = readln.chomp; bool ok; foreach (i; 0 .. s.length) { if (s == t) ok = true; s = s[$-1] ~ s[0..$-1]; } 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.container; import std.typecons;
D
import std.stdio, std.algorithm, std.string, std.conv, std.array, std.range, std.math; int read() { return readln.chomp.to!int; } int[] reads() { return readln.split.to!(int[]); } void solve() { int n = read(); int[] a = reads(); int r, m; long ans; foreach (l; 0..n) { while (r < n && !(m & a[r])) { m ^= a[r++]; } ans += r - l; m ^= a[l]; } writeln(ans); } void main() { solve(); readln; }
D
import std.stdio; import std.string; import std.math; import std.conv; import std.algorithm; import std.bigint; void main(){ auto n = to!int(chomp(readln())); for(int i=0;i<n;i++){ auto s = split(readln()); real[] a; foreach(j;s) a ~= to!real(j); real R2 = (a[0] - a[3])*(a[0] - a[3]) + (a[1] - a[4])*(a[1] - a[4]); if(R2 > (a[2] + a[5])*(a[2] + a[5])) writeln("0"); else if(R2 <= (a[2] + a[5])*(a[2] + a[5])){ if( sqrt(R2) + a[2] < a[5]) writeln("-2"); else if(sqrt(R2) + a[5] < a[2]) writeln("2"); else writeln("1"); } } }
D
// Vicfred // https://atcoder.jp/contests/abc045/tasks/arc061_a // brute force, bitmask import std.algorithm; import std.array; import std.conv; import std.stdio; import std.string; void main() { string s = readln.strip; const long n = s.length; long ans = 0; for(int bit = 0; bit < (1 << n - 1); bit++) { string tmp; for(int idx = 0; idx < n; idx++) { tmp ~= s[idx]; if(!(bit & (1<<idx))) { ans += tmp.to!long; tmp = ""; } } } ans.writeln; }
D
import std.stdio; import std.conv; import std.algorithm; import std.array; import std.string; import std.uni; import std.math; import std.container.rbtree; import std.range; import std.numeric; void main() { auto t = to!long(readln().chomp); OUTER: foreach(_; 0..t) { auto arr = readln().chomp.splitter(" ").map!(to!long).array; auto rev = arr.retro.array; if(arr[0] > arr[2] && arr[1] > arr[2] && arr[0] > arr[3] && arr[1] > arr[3]) writeln("NO"); else if(rev[0] > rev[2] && rev[1] > rev[2] && rev[0] > rev[3] && rev[1] > rev[3]) writeln("NO"); else writeln("YES"); } }
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; bool isA(string s) { if(s[0..2] != ">'") { return false; } if(s[$-1] != '~') { return false; } auto m = s[2..$-1]; if(m.length % 2 == 0) { return false; } if(m.length <= 1) { return false; } if(m[$/2] != '#') { return false; } if(m.count!(a => a != '=') != 1) { return false; } return true; } bool isB(string s) { if(s[0..2] != ">^") { return false; } if(s[$-2..$] != "~~") { return false; } auto m = s[2..$-2]; if(m.length % 2 == 1) { return false; } if(m.empty()) { return false; } while(!m.empty()) { if(m[0..2] != "Q=") { return false; } m = m[2..$]; } return true; } void main(){ auto n = readLong(); foreach(i; iota(n)) { auto s = readln().chomp(); if(isA(s)) { writeln("A"); } else if(isB(s)) { writeln("B"); } else { writeln("NA"); } } }
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 a, b; scan(a, b); if (a + b == 15) { writeln("+"); } else if (a * b == 15) { writeln("*"); } else { writeln("x"); } } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { long x; scan(x); auto ans = (x / 500) * 1000 + ((x % 500) / 5) * 5; writeln(ans); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string; void main() { auto line = readln.split; auto a = line[0].to!long; auto b = line[2].to!long; writeln(line[1] == "+" ? a + b : a - b); }
D
import std.stdio; import std.string; string toSwapCase(string s) { if (s == s.toLower) { return s.toUpper; } if (s == s.toUpper) { return s.toLower; } return s; } void main() { auto str = readln; foreach (ch; str.split("")) { write(toSwapCase(ch)); } }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, 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 main() { int n; readV(n); auto l = new long[](n+1); l[0] = 2; l[1] = 1; foreach (i; 2..n+1) l[i] = l[i-1]+l[i-2]; writeln(l[n]); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N; ulong K; scan(N, K); auto A = aryread!ulong(); auto dp = new ulong[][](2, 61); foreach_reverse (i; 0 .. 60) { ulong[2] cnt; foreach (j; 0 .. N) { cnt[0] += A[j] & (1UL << i); cnt[1] += (A[j] & (1UL << i)) ^ (1UL << i); } if (K & (1UL << i)) { dp[0][i] = dp[0][i + 1] + cnt[1]; dp[1][i] = dp[0][i + 1] + cnt[0]; if (dp[1][i + 1]) dp[1][i] = dp[1][i].max(dp[1][i + 1] + cnt[1], dp[1][i + 1] + cnt[0],); } else { dp[0][i] = dp[0][i + 1] + cnt[0]; if (dp[1][i + 1]) dp[1][i] = max(dp[1][i + 1] + cnt[1], dp[1][i + 1] + cnt[0]); } } // writeln(dp[0]); // writeln(dp[1]); max(dp[0][0], dp[1][0]).writeln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto n = RD!int; auto s = RD!(char[]); long cnt; foreach (i; 0..n) { cnt += s[i] == '(' ? 1 : -1; } if (cnt != 0) { writeln(0); writeln("1 1"); } else { long ans; int best_i, best_j; foreach (i; 0..n) { foreach (j; i..n) { swap(s[i], s[j]); long c, d; foreach (k; 0..n) { c += s[k] == '(' ? 1 : -1; if (c == 0) ++d; else if (c < 0) { c = 0; d = 0; } } if (c != 0) ++d; if (d > ans) { best_i = i; best_j = j; ans = d; } swap(s[i], s[j]); } } writeln(ans); writeln(best_i+1, " ", best_j+1); } stdout.flush(); debug readln(); }
D
import std.stdio; import std.uni; import std.conv; import std.container; import std.functional; import std.algorithm; import std.array; import std.typecons; import std.range; import std.numeric; import std.traits; long pmod(long a, long m) { if (a < 0) return (a % m) + m; return a % m; } void main(string[] args) { enum p = 1_000_000_000 + 7; auto n = next!string; auto tenpow = new long[](n.length); tenpow[n.length - 1] = 1; foreach_reverse(i; 0 .. n.length - 1) tenpow[i] = (tenpow[i + 1] * 10) % p; long rightNum = 0; foreach(d; n) rightNum = ((rightNum * 10) % p + long(d - '0')) % p; long leftSum = 0; long leftNum = 0; long res = 0; foreach(i; 0 .. n.length) { long li = cast(long)i; long dig = n[i] - '0'; rightNum = pmod(rightNum - (dig * tenpow[i]) % p, p); res = (res%p + (rightNum * (li + 1))%p + (leftSum * tenpow[i])%p)%p; leftNum = ((leftNum * 10) % p + dig)%p; leftSum = (leftSum + leftNum) % p; } res.writeln; } static this() { popChar; } struct Any(T) { static auto read() { static if (is(T == char)) { auto res = frontChar; popChar; return res; } } } struct Unsigned(T) { auto read() { auto res = T(0); while(!frontChar.isDigit) popChar; do { res = res * T(10) + T(frontChar - '0'); popChar; } while(frontChar.isDigit); return res; } } struct Matrix(T, alias rows, alias cols) { T[][] _cell; alias _cell this; // Ring constructor this(int n) { final switch(n) { case 0: _cell = new T[][](rows, cols); break; case 1: debug assert(rows == cols); _cell = new T[][](rows, cols); foreach(i; 0 .. rows) _cell[i][i] = T(1); break; } } // Copy constructor this(Matrix!(T, rows, cols) other) { _cell = new T[][](rows, cols); foreach(i; 0 .. rows) _cell[i][] = other[i][]; } auto opBinary(string op)(Matrix!(T, rows, cols) other) if (op == "+" || op == "-") { auto res = Matrix!(T, rows, cols)(0); foreach(i; 0 .. rows) foreach(j; 0 .. cols) res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]}); return res; } auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other) if (op == "*") { debug assert(cols == otherRows); auto res = Matrix!(T, rows, otherCols)(0); foreach(i; 0 .. rows) foreach(k; 0 .. cols) foreach(j; 0 .. otherCols) res[i][j] += this[i][k] * other[k][j]; return res; } ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other) { static if (op == "*") { debug assert(rows == cols); alias n = rows; auto res = Matrix!(T, n, n)(0); T factor; foreach(i; 0 .. n) foreach(k; 0 .. n) foreach(j; 0 .. n) res[i][j] += this[i][k] * other[k][j]; foreach(i; 0 .. n) this[i][] = res[i][]; } else { foreach(i; 0 .. rows) foreach(j; 0 .. cols) mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];}); } return this; } mixin powMethod; } struct Mod(T, alias mod) { alias This = Mod!(T, mod); T _rep; this(T t) { _rep = t % mod; if (_rep < 0) _rep += mod; } static auto plain(T t) { auto res = Mod!(T, mod)(); res._rep = t; return res; } static auto fromPositive(T t) { pragma(inline, true); return Mod!(T, mod).plain(t % mod); } static auto fromNegative(T t) { pragma(inline, true); return Mod!(T, mod).plain(t % mod + mod); } string toString() { return to!string(_rep); } debug invariant { assert(_rep >= 0 && _rep < mod); } auto opBinary(string op)(This b) { static if (op == "+") { T resrep = _rep + b._rep; if (resrep >= mod) resrep -= mod; return This.plain(resrep); } else static if (op == "-") { T resrep = _rep - b._rep; if (resrep < 0) resrep += mod; return This.plain(resrep); } else static if (op == "*") { return This.fromPositive(_rep * b._rep); } } auto opOpAssign(string op)(This b) { mixin(q{_rep }, text(op, "="), q{b._rep;}); static if (op == "+") { if (_rep >= mod) _rep -= mod; } else static if (op == "-") { if (_rep < 0) _rep += mod; } else static if (op == "*") { _rep %= mod; } return this; } auto opBinary(string op)(long exp) if (op == "^^") { return pow(exp); } mixin powMethod; } mixin template powMethod() { auto pow(this ThisType)(long exp) { auto res = ThisType(1); auto pow = ThisType(this); while(exp) { if (exp & 1) res *= pow; pow *= pow; exp >>= 1; } return res; } } // INPUT char frontChar; void popChar() { import core.stdc.stdio; frontChar = cast(char) getchar; if (feof(stdin)) frontChar = ' '; } auto makeArray(T, S...)(S s) { enum brackets = () { string res; foreach(i; 0 .. s.length) res ~= "[]"; return res; } (); mixin(q{alias Type = T}, brackets, q{;}); return new Type(s); } template isNumberLike(T) { enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}())); } void popWhite() { import std.ascii; while (frontChar.isWhite) popChar; } void print(T...)(T t) { static foreach(ti; t) { static if (is(typeof(ti) == string)) { write(ti, " "); } else static if (isArray!(typeof(ti))) { foreach(e; ti) print(e); } else static if (isTuple!(typeof(ti))) { static foreach(i; ti.length) writesp(ti[i]); } else { write(ti, " "); } } } void println(T...)(T t) { static if (t.length == 0) writeln; static foreach(ti; t) { print(ti); writeln; } } auto next(alias T, S...)(S s) { pragma(inline, true); static if (s.length > 0) { auto res = makeArray!(typeof(next!T()))(s); S t; enum loops = () { string res; foreach(i; 0 .. s.length) { auto ti = text(q{t[}, i, q{]}); res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)}); } return res; } (); enum indexing = () { string res = "res"; foreach(i; 0 .. s.length) res ~= text(q{[t[}, i, q{]]}); return res; } (); mixin(loops, indexing, q{ = next!T;}); return res; } else { static if (isNumberLike!T) { import std.ascii: isDigit; T res; while(frontChar.isWhite) popChar; T multiplier = T(1); if (frontChar == '-') { multiplier = T(-1); popChar; } else if (frontChar == '+') { popChar; } debug assert(frontChar.isDigit); do { res = res * T(10) + T(frontChar - '0'); popChar; } while(frontChar.isDigit); return multiplier * res; } else static if (is(T == string)) { import std.ascii: isWhite; string res; while(frontChar.isWhite) popChar; while(!frontChar.isWhite) { res ~= frontChar; popChar; } return res; } else static if (is(T == char)) { while(frontChar.isWhite) popChar; auto res = frontChar; popChar; return res; } else { return T.read; } } }
D
import std.stdio; import std.uni; import std.conv; import std.container; import std.functional; import std.algorithm; import std.array; import std.typecons; import std.range; import std.numeric; void main(string[] args) { inputFile = stdin; debug inputFile = File(args[1]); auto t = next!int; foreach(tc; 0 .. t) { auto n = next!int; auto a = next!int(n); if (iota(0, n - 1).all!(i => a[i] > a[i + 1])) writeln("NO"); else writeln("YES"); } } ulong bitCnt(long x) { ulong cnt = 0; while(x) {cnt += x & 1; x>>=1;} return cnt; } alias Arr(T) = Mat!(T, 1); struct Mat(T, size_t dimension) { T[] _baseArray; T[] _slice; size_t[dimension] _sizes; size_t dim(size_t i) {return _sizes[i];} size_t[dimension] _indexer; this(T[] baseArray, size_t[dimension] sizes) { _baseArray = baseArray; _sizes = sizes; _indexer[dimension - 1] = 1; static foreach_reverse(i; 0 .. dimension - 1) _indexer[i] = _indexer[i + 1] * _sizes[i + 1]; auto requiredSize = _indexer[0] * _sizes[0]; debug assert(_baseArray.length >= requiredSize); _slice = _baseArray[0 .. requiredSize]; } this(size_t[dimension] sizes) { size_t reqSize = 1; static foreach(i; 0 .. dimension) reqSize *= sizes[i]; this(new T[](reqSize), sizes); } ref auto opIndex() { pragma(inline, true); return _slice[]; } ref auto opIndex(I...)(I i) { pragma(inline, true); debug assert(i.length <= dimension); size_t index = mixin(iota(0, i.length).map!(j => text("i[", j, "]*_indexer[", j, "]")).join("+")); static if (i.length == dimension) { return _slice[index]; } else { enum sliceDimension = dimension - i.length; size_t[sliceDimension] remSizes = _sizes[i.length .. $]; auto basePortion = _slice[index .. index + _indexer[i.length - 1]]; return Mat!(T, sliceDimension)(basePortion, remSizes); } } static if (dimension == 2) { auto identity() { debug assert(dim(0) == dim(1)); auto n = dim(0); auto id = Mat!(T, 2)([n, n]); foreach(k; 0 .. n) id[k, k] = T(1); return id; } bool canMultiply(Mat!(T, 2) b) { return this.dim(1) == b.dim(0); } auto opBinary(string op)(Mat!(T, 2) b) if (op == "*") { debug assert(canMultiply(b)); auto res = Mat!(T, 2)([this.dim(0), b.dim(1)]); foreach(i; 0 .. res.dim(0)) foreach(k; 0 .. this.dim(1)) foreach(j; 0 .. res.dim(1)) res[i, j] = res[i, j] + this[i, k] * b[k, j]; return res; } auto opBinary(string op)(ulong exp) if (op == "^^") { return this.pow(identity, exp); } mixin interiorBinPow; } } struct Mod(T, ulong mod) { alias This = Mod!(T, mod); T _rep; T rep() {return _rep;} this(W)(W w) { _rep = ((cast(T) w)%mod + mod) % mod; } static auto plainConstruct(T t) { pragma(inline, true); debug assert(t >= 0 && t < mod); auto res = Mod!(T, mod).init; res._rep = t; return res; } string toString() { return to!string(rep); } debug invariant { assert(_rep >= 0 && _rep < mod); } auto opBinary(string op)(This b) { static if (op == "+") { T resrep = _rep + b._rep; if (resrep >= mod) resrep -= mod; return This.plainConstruct(resrep); } else static if (op == "-") { T resrep = _rep - b._rep; if (resrep < 0) resrep += mod; return This.plainConstruct(resrep); } else static if (op == "*") { return This(_rep * b._rep); } } } mixin template interiorBinPow() { alias ThisType = typeof(this); auto pow(ThisType res, ulong exp) { if (exp < 0) debug assert(0); auto pow = this; while(exp) { if (exp & 1) res = res * pow; pow = pow * pow; exp >>= 1; } return res; } } // INPUT File inputFile; DList!string _words; void _wordsFill() { while (_words.empty) _words.insertBack(inputFile.readln.split); } string popWord() { _wordsFill; auto word = _words.front; _words.removeFront; return word; } string peekWord() { _wordsFill; return _words.front; } T next(T)() { return to!T(popWord); } T peek(T)() { return to!T(peekWord); } auto next(T, S...)(S s) { static if (S.length == 0) { return to!T(popWord); } else { auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]); foreach(ref elem; res) elem = next!T(s[1 .. $]); return res; } }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string; immutable long INF = 1L << 59; void main() { auto N = readln.chomp.to!int; auto P = readln.split.map!(to!int).array; auto S = readln.split.map!(to!long).array; auto X = new long[](N); auto G = new int[][](N); foreach (i; 0..N-1) { G[P[i]-1] ~= i+1; G[i+1] ~= P[i]-1; } bool ok = true; long ans = 0; long dfs(int n, int p) { long ret = S[n]; if (ret == -1) ret = INF; foreach (m; G[n]) { if (m == p) continue; auto v = dfs(m, n); if (v < S[n]) ok = false; ret = min(ret, v); } X[n] = ret; return ret; } void dfs2(int n, int p, long s) { if (X[n] == INF) return; long a = X[n] - s; ans += a; foreach (m; G[n]) { if (m == p) continue; dfs2(m, n, X[n]); } } dfs(0, -1); if (!ok) { writeln(-1); return; } dfs2(0, -1, 0); ans.writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(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; foreach (i; n..n+10^^5) { auto x = i; bool ok = true; while (x != 0) { auto y = x % 10; x /= 10; if (y == 0) continue; if (i % y) { ok = false; break; } } if (ok) { ans[ti] = i; break; } } } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
/+ dub.sdl: name "A" dependency "dunkelheit" version=">=0.9.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; int main() { Scanner sc = new Scanner(stdin); scope(exit) assert(!sc.hasNext); int h, w; sc.read(h, w); bool[][] g = new bool[][](h, w); foreach (i; 0..h) { string s; sc.read(s); foreach (j; 0..w) { g[i][j] = (s[j] == '#'); } } bool[][] g2 = new bool[][](h, w); foreach (i; 0..h) { foreach (j; 0..w) { if (!g[i][j]) continue; int[] hv, wv; foreach (i2; 0..h) { if (g[i2][j]) hv ~= i2; } foreach (j2; 0..w) { if (g[i][j2]) wv ~= j2; } foreach (i2; hv) { foreach (j2; wv) { g2[i2][j2] = true; } } } } foreach (i; 0..h) { foreach (j; 0..w) { if (g[i][j] != g2[i][j]) { writeln("No"); return 0; } } } writeln("Yes"); // foreach (v; g2) { // writeln(v.map!(x => x ? '#' : '.')); // } return 0; } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */ // module dkh.scanner; // import dkh.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(Args...)(auto ref Args args) { import std.exception; static if (args.length != 0) { enforce(readSingle(args[0])); read(args[1..$]); } } bool hasNext() { return succ(); } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD!uint; auto a = RDR.ARR!uint; long[2] cnt; foreach (i; 0..N) { auto j = a[i] - 1; ++cnt[j]; } long x; if (cnt[1] != 0) { --cnt[1]; x = 2; write(2); } else { --cnt[0]; x = 1; write(1); } foreach (i; 1..N) { if (x % 2 == 1 && cnt[1] != 0) { --cnt[1]; x += 2; write(" 2"); } else if (cnt[0] != 0) { --cnt[0]; x += 1; write(" 1"); } else { --cnt[1]; x += 2; write(" 2"); } } writeln(); stdout.flush(); debug readln(); }
D
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.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 = readInt(); auto A = new int[N]; foreach (i; 0 .. N) { A[i] = readInt(); } bool ans; void dfs(int i, bool any, int sum) { ans = ans || (any && sum == 0); if (i == N) { return; } dfs(i + 1, any, sum); dfs(i + 1, true, sum + A[i]); dfs(i + 1, true, sum - A[i]); } dfs(0, false, 0); 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.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto n = RD!int; long lp, lc; ans[ti] = true; foreach (i; 0..n) { auto p = RD; auto c = RD; if (p < c || p < lp || c < lc) { ans[ti] = false; } auto d1 = p - lp; auto d2 = c - lc; if (d1 < d2) ans[ti] = false; lp = p; lc = c; } } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto n = RD!int; auto m = RD!int; auto p = RDA!int; auto h = RDA!int; auto edges = new int[][](n); foreach (i; 0..n-1) { auto x = RD!int-1; auto y = RD!int-1; edges[x] ~= y; edges[y] ~= x; } bool ok = true; long[2] dfs(int pos, int last) { long cnt0, cnt1; foreach (to; edges[pos]) { if (to == last) continue; auto r = dfs(to, pos); cnt0 += r[0]; cnt1 += r[1]; } auto d = cnt0 - cnt1; auto v = h[pos] - d; auto remain = p[pos] - abs(v); if (remain < 0 && v > 0) { debug writeln("d:", d, " v:", v, " remain:", remain); debug writeln("cnt:", cnt0, " ", cnt1); cnt0 += p[pos]; v -= p[pos]; auto u = min(cnt1, abs(remain/2)); cnt0 += u; cnt1 -= u; v -= u*2; remain += u*2; debug writeln("d:", d, " v:", v, " remain:", remain); debug writeln("cnt:", cnt0, " ", cnt1); } if (remain < 0 || remain % 2) { debug writeln("false pos:", pos, " remain:", remain); ok = false; } if (v > 0) cnt0 += v; else cnt1 += abs(v); cnt0 += remain / 2; cnt1 += remain / 2; return [cnt0, cnt1]; } auto r = dfs(0, -1); debug writeln("m:", m, " ", r); if (r[0] + r[1] != m) ok = false; ans[ti] = ok; } foreach (e; ans) { writeln(e ? "YES" : "NO"); } stdout.flush; debug readln; }
D