code
stringlengths
4
1.01M
language
stringclasses
2 values
module main; import std.stdio : readln, writeln, write, writefln, writef; import std.conv : to; import std.array : split, replace; import std.string : strip; import std.algorithm : max, min, map, reduce, sort, reverse; import std.functional : memoize; version = B; version (A) { void main() { auto sequence = read_one!string(); auto instructions = read_one!string(); size_t position = 0; foreach (instruction; instructions) { if (sequence[position] == instruction) { position += 1; } } writeln(position + 1); } } version (B) { T abs(T)(T x) { if (x < 0) return -x; return x; } void main() { auto n = read_one!int(); auto heights = new int[n+1]; foreach (i; 1..n+1) { heights[i] = read_one!int(); } auto answer = n * 2 - 1; foreach (i; 0..n) { answer += abs(heights[i] - heights[i+1]); } writeln(answer); // walks: heights[0] + (heights[i] - heights[i-1]).map!abs.sum // jumps: n-1 // eats: n } } version (C) { void main() { auto n = read_one!int(); } } version (D) { void main() { auto n = read_one!int(); } } version (E) { void main() { } } T read_one(T)() { return readln().strip().to!T(); } T[] read_some(T)() { T[] ret; foreach (e; readln().strip().split()) { ret ~= e.to!T(); } return ret; } T[m] read_fixed(int m, T)() { T[m] ret; foreach (i, e; readln().strip().split()) { ret[i] = e.to!T(); } return ret; }
D
//prewritten code: https://github.com/antma/algo import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.traits; import std.random; import std.typecons; import std.datetime.systime : Clock; class InputReader { private: ubyte[] p; ubyte[] buffer; size_t cur; public: this () { buffer = uninitializedArray!(ubyte[])(16<<20); p = stdin.rawRead (buffer); } final ubyte skipByte (ubyte lo) { while (true) { auto a = p[cur .. $]; auto r = a.find! (c => c >= lo); if (!r.empty) { cur += a.length - r.length; return p[cur++]; } p = stdin.rawRead (buffer); cur = 0; if (p.empty) return 0; } } final ubyte nextByte () { if (cur < p.length) { return p[cur++]; } p = stdin.rawRead (buffer); if (p.empty) return 0; cur = 1; return p[0]; } template next(T) if (isSigned!T) { final T next () { T res; ubyte b = skipByte (45); if (b == 45) { while (true) { b = nextByte (); if (b < 48 || b >= 58) { return res; } res = res * 10 - (b - 48); } } else { res = b - 48; while (true) { b = nextByte (); if (b < 48 || b >= 58) { return res; } res = res * 10 + (b - 48); } } } } template next(T) if (isUnsigned!T) { final T next () { T res = skipByte (48) - 48; while (true) { ubyte b = nextByte (); if (b < 48 || b >= 58) { break; } res = res * 10 + (b - 48); } return res; } } final T[] nextA(T) (int n) { auto a = uninitializedArray!(T[]) (n); foreach (i; 0 .. n) { a[i] = next!T; } return a; } } alias T = Tuple!(uint, uint); void main() { rndGen.seed ((Clock.currTime.toHash & 0xffffffffU).to!uint); auto r = new InputReader; immutable n = r.next!uint, m = r.next!uint; auto a = uninitializedArray!(T[]) (m); bool[ulong] h; foreach (i; 0 .. m) { uint u = r.next!uint - 1, v = r.next!uint - 1; if (u > v) swap (u, v); a[i] = tuple (u, v); h[(u.to!ulong << 32) + v] = true; } randomShuffle (a); bool check (int k) { foreach (const p; a) { uint u = (p[0] + k) % n, v = (p[1] + k) % n; if (u > v) swap (u, v); if (! (((u.to!ulong << 32) + v) in h)) return false; } return true; } foreach (k; 1 .. n) { if (check (k)) { writeln ("Yes"); return; } } writeln ("No"); }
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 c = RD!string; auto str = "aiueo"; bool ans; foreach (s; str) { if (s == c[0]) ans = true; } writeln(ans ? "vowel" : "consonant"); stdout.flush(); debug readln(); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); import std.typecons; alias Point=Tuple!(int, "x", int, "y"); auto u=new Point[](n), v=new Point[](n); foreach(i; 0..n) rd(u[i].x, u[i].y); foreach(i; 0..n) rd(v[i].x, v[i].y); auto mf=new maxFlow(n*n+2), s=n*n, t=s+1; foreach(i; 0..n) mf.addEdge(s, i, 1), mf.addEdge(n+i, t, 1); foreach(i; 0..n)foreach(j; 0..n){ if(u[i].x<v[j].x && u[i].y<v[j].y){ // writeln(i, " ", j); mf.addEdge(i, n+j, 1); } } writeln(mf.ford(s, t)); } class maxFlow{ import std.typecons, std.conv, std.algorithm; // , std.stdio; alias T=int; alias Edge=Tuple!(int, "to", int, "rev", T, "cap"); Edge[][] g; bool[] vis; auto inf=(1_000_000_000).to!(T); this(int sz){ g.length=sz; vis.length=sz; } void addEdge(int from, int to, T cap){ g[from]~=Edge(to, (g[to].length).to!(int), cap); g[to]~=Edge(from, (g[from].length-1).to!(int), (0).to!(T)); } T flow(int i, T curf, int sink){ if(i==sink) return curf; vis[i]=true; foreach(ref e; g[i]){ if(vis[e.to] || e.cap==0) continue; auto tmpf=flow(e.to, min(curf, e.cap), sink); if(tmpf>0){ e.cap-=tmpf; g[e.to][e.rev].cap+=tmpf; return tmpf; } } return 0; } T ford(int source, int sink){ auto maxf=(0).to!(T); while(true){ fill(vis, false); auto f=flow(source, inf, sink); if(f>0) maxf+=f; else return maxf; } } } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.stdio, std.conv, std.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 a = RDA; auto b1 = new long[](n); auto b2 = new long[](n); foreach (i; 0..n) { long[] nums; auto x = a[i]; while (x != 0) { nums ~= x % 10; x /= 10; } debug writeln(nums); foreach (j; 0..nums.length) { auto tmp = nums[j]; foreach (k; 0..j+j) { tmp.modm(10); } b2[i].moda(tmp); tmp.modm(10); b1[i].moda(tmp); } } debug writeln(b1); debug writeln(b2); long ans; foreach (i; 0..n) { auto x = b1[i] + b2[i]; x.modm(n); ans.moda(x); } writeln(ans); stdout.flush(); debug readln(); }
D
void main() { int[] tmp = readln.split.to!(int[]); int n = tmp[0], k = tmp[1]; writeln(n % k != 0 ? 1 : 0); } 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; 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.ascii; void main() { auto s = readln.chomp; int res; switch (s) { case "SUN": res = 7; break; case "MON": res = 6; break; case "TUE": res = 5; break; case "WED": res = 4; break; case "THU": res = 3; break; case "FRI": res = 2; break; case "SAT": res = 1; break; default: } res.writeln; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { auto s = sread(); long N = s.to!long(); long SN = s.map!(c => cast(long)(c - '0'))().sum(); writeln((N % SN == 0) ? "Yes" : "No"); }
D
import std.stdio; import std.string; void main() { auto n = readln; auto s = readln.strip; writeln( solve( s ) ); } int solve( in string s ) { auto lc = new int[ s.length ]; lc[ 0 ] = 0; foreach( i; 1 .. s.length ) { lc[ i ] = lc[ i - 1 ] + ( s[ i - 1 ] == 'E' ? 0 : 1 ); } auto rc = new int[ s.length ]; rc[ $-1 ] = 0; foreach_reverse( i; 0 .. s.length-1 ) { rc[ i ] = rc[ i + 1 ] + ( s[ i + 1 ] == 'W' ? 0 : 1 ); } auto result = lc[ 0 ] + rc[ 0 ]; foreach( i; 1 .. s.length ) { auto lrc = lc[ i ] + rc[ i ]; if( lrc < result ) result = lrc; } return result; } unittest { assert( solve( "WEEWW" ) == 1 ); assert( solve( "WEWEWEEEWWWE" ) == 4 ); assert( solve( "WWWWWEEE" ) == 3 ); }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; long solve (int [] a, int n) { if (a[1..$ - 1].all !(q{a == 0})) { return 0; } if (a[1..$ - 1].all !(q{a <= 1})) { return -1; } if (n == 3 && a[1] % 2 == 1) { return -1; } long res = 0; foreach (i; 1..n - 1) { res += a[i] / 2 + (a[i] & 1); } return res; } void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto n = readln.strip.to !(int); auto a = readln.splitter.map !(to !(int)).array; writeln (solve (a, n)); } }
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; void main(){ int n = readln().chomp().to!int; for(int i=0;i<n;i++){ int[] s = split(readln()).to!(int[]); int res = s[0] * s[2] + s[1] * s[3]; if(s[2] >= 5 && s[3] >= 2) res = to!int(to!real(res*0.8)); else if(s[2] >= 5) res = min(res, to!int(0.8*(s[0]*s[2]+s[1]*2))); else if(s[3] >= 2) res = min(res, to!int(0.8*(s[0]*5+s[1]*s[3]))); else res = min(res, to!int(0.8*(s[0]*5 + s[1]*2))); writeln(res); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto Q = RD; auto s = RD!string; auto t = new char[](Q); auto d = new char[](Q); foreach (i; 0..Q) { t[i] = RD!char; d[i] = RD!char; } long[256] cnt1, cnt2; long l, r = s.length - 1; foreach_reverse (i; 0..Q) { auto num = t[i] - 'A'; if (d[i] == 'L') { if (t[i] == s[l]) ++l; if (r != s.length - 1) { if (s[r+1] == t[i]) ++r; } } else if (d[i] == 'R') { if (t[i] == s[r]) --r; if (l != 0) { if (s[l-1] == t[i]) --l; } } } writeln(r - l + 1); stdout.flush(); debug readln(); }
D
import std.algorithm, std.string, std.range, std.stdio, std.conv; void main() { int N = readln.chomp.to!(int); int[] ns = readln.chomp.split.to!(int[]); int x = ns[$-1]; ulong[21][101] dp; dp[0][ns[0]]++; foreach (i; 1..(N-1)) { foreach (j; 21.iota) { int a = j + ns[i]; int b = j - ns[i]; if (0 <= a && a <= 20) { dp[i][a] += dp[i - 1][j]; } if (0 <= b && b <= 20) { dp[i][b] += dp[i - 1][j]; } } } writeln(dp[N-2][x]); }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; long calc(long n, long k) { long d = k / (n-1); return k + d + (k % (n-1) == 0 ? -1 : 0); } void main() { int t; scan(t); foreach (_; 0..t) { int n, k; scan(n, k); writeln(calc(n, 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;
D
import std.stdio, std.array, std.conv, std.string; void main() { while (true) { string[] input = split(readln()); int h = to!int(input[0]); int w = to!int(input[1]); if (h == 0 && w == 0) return; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { write("#"); } writeln(""); } writeln(""); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void main() { auto nxt = readln.split.to!(int[]); auto N = nxt[0]; auto X = nxt[1]; auto T = nxt[2]; writeln((N + X - 1) / X * T); }
D
import std.stdio, std.conv, std.string; void main() { int N = to!(int)(readln().chomp()); writeln(N/3); }
D
import std.stdio; import std.algorithm; import std.conv; import std.datetime; import std.numeric; import std.math; import std.string; string my_readln() { return chomp(readln()); } void main() {//try{ auto tokens = split(my_readln()); auto N = to!ulong(tokens[0]); ulong[] A; foreach (i; 0..N) { auto tokens2 = split(my_readln()); A ~= to!ulong(tokens2[0]); } string ans = "second"; foreach (e; A) { if (e % 2 == 1) { ans = "first"; break; } } writeln(ans); stdout.flush(); /*}catch (Throwable e) { writeln(e.toString()); } readln();*/ }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { auto S = sread(); long g, p; long ans; foreach (c; S) { if (c == 'p') { if (p < g) { p++; } else { g++; ans--; } } else if (c == 'g') { if (p < g) { p++; ans++; } else { g++; } } } writeln(ans); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); long K = lread(); long solve(long x, long i) { if (i == N) return x; return min(solve(x * 2, i + 1), solve(x + K, i + 1)); } solve(1, 0).writeln(); }
D
import std.stdio : readf, readln, writeln; import std.string; import std.array; import std.conv; void main() { int[] v; v = readln.split.to!(int[]); int H = v[0]; int W = v[1]; string[] s; for (int i=0; i<H; i++) s ~= readln.chomp; int[][] dw; int[][] dh; for (int i = 0; i < H; i++) { int[] cw; int count = 0; for (int j = 0; j < W; j++) { if (s[i][j] == '#') { cw ~= 0; } else if (j == 0 || s[i][j - 1] == '#') { count = 0; for (int k = j; k < W; k++) { if (s[i][k] == '.') { count++; } else { break; } } cw ~= count; } else { cw ~= count; } } dw ~= cw; } for (int j = 0; j < W; j++) { int[] ch; int count = 0; for (int i = 0; i < H; i++) { if (s[i][j] == '#') { ch ~= 0; } else if (i == 0 || s[i - 1][j] == '#') { count = 0; for (int k = i; k < H; k++) { if (s[k][j] == '.') { count++; } else { break; } } ch ~= count; } else { ch ~= count; } } dh ~= ch; } int max = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { int sum = dw[i][j] + dh[j][i]; if (max < sum) { max = sum; } } } writeln(max - 1); }
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.ascii; void main() { auto n = readln.chomp.to!int; auto a = readln.chomp.split.to!(int[]); auto m = a.filter!("a % 2").array; if (m.length % 2) { writeln("NO"); } else { writeln("YES"); } }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto s = readln.chomp, a = s.map!(c => cast(int)(c-'0')).sum, n = s.to!int; writeln(n%a == 0 ? "Yes" : "No"); }
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; int boo(int n) { return n < 22 ? n : 0; } int score(int[] c) { int ans; int one; foreach(ci; c) { if(ci == 1) { ++one; } else { ans += min(10, ci); } } while(one > 1) { ++ans; --one; } if(one == 0) { return boo(ans); } else { return max(boo(ans+1), boo(ans+11)); } } void main(){ while(true) { auto c = readInts(); if(c.length == 1 && c[0] == 0) { return; } writeln(score(c)); } }
D
void main() { string s = readln.chomp; auto g = s.group.array; writeln(g.length - 1); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { float A, B; scan(A, B); foreach (x; 1 .. 10_000) { // writeln(x, " ", (x * 108) / 100); if (A == floor(x * 0.08) && B == floor(x * 0.1)) { writeln(x); return; } } writeln(-1); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { auto C = new string[](2); C[0] = sread(); C[1] = sread(); writeln((C[0][0] == C[1][2] && C[0][1] == C[1][1] && C[0][2] == C[1][0]) ? "YES" : "NO"); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; 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; } enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { int N; scan(N); auto a = iota(N).map!(i => readln.split.to!(int[])).array; foreach (k ; 0 .. N) { foreach (i ; 0 .. N) { foreach (j ; i + 1 .. N) { if (a[i][j] > a[i][k] + a[k][j]) { writeln(-1); return; } } } } long ans; foreach (i ; 0 .. N) { foreach (j ; i + 1 .. N) { bool used = true; foreach (k ; 0 .. N) { if (k == i || k == j) continue; if (a[i][j] == a[i][k] + a[k][j]) { used = false; break; } } if (used) ans += a[i][j]; } } writeln(ans); }
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 mod = 10L^^9 + 7; void main() { int n; scan(n); auto a = readln.split.to!(long[]); auto r = new long[](n + 1); foreach (i ; 1 .. n + 1) { r[i] = pow(i, mod - 2, mod); } foreach (i ; 1 .. n + 1) { r[i] += r[i-1]; r[i] %= mod; } debug { writeln(r); } long ans; long nf = 1; foreach (i ; 2 .. n + 1) { nf *= i; nf %= mod; } foreach (i ; 0 .. n) { long p = r[i+1] + r[n - i]; p %= mod; p += mod - 1; p %= mod; ans += (nf * p) % mod * a[i] % mod; ans %= mod; } writeln(ans); } long pow(long x, long y, long mod) { return y > 0 ? pow(x, y>>1, mod)^^2 % mod * x^^(y&1) % mod : 1L; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); ulong bignum = 1_000_000_007; alias Pair = Tuple!(long, "number", long, "times"); 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); auto dif = abs(n - m); if(dif == 1) { auto set = product_mod(min(n, m)); set *= set; set %= bignum; set *= max(n, m); set %= bignum; set.writeln(); } else if(dif == 0) { auto set = product_mod(n); set *= set; set %= bignum; set *= 2; set %= bignum; set.writeln(); } else writeln(0); } long product_mod(long n) { if(n == 1) return 1; return n * product_mod(n - 1) % bignum; }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { auto x = lread(); long ans; foreach (p; 2 .. 11) { foreach (b; 1 .. 1001) { if (b ^^ p <= x) { ans = max(ans, b ^^ p); } } } writeln(ans); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.stdio, std.string, std.conv; import std.typecons; import std.algorithm, std.array, std.range, std.container; import std.math; void main() { auto b = readln.split[0]; if (b == "A") "T".writeln; if (b == "T") "A".writeln; if (b == "G") "C".writeln; if (b == "C") "G".writeln; }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { long n, k; scan(n, k); auto a = aryread(); // writeln(a); // writeln(ceil(a.length / k) * (k - 1) // long ans = ((a.length + k - 1) / k) * (k - 1); // writeln(ans); long i = 0; long cnt; while (i < n) { i += k; if (i == n) { cnt += 1; break; } i -= 1; cnt += 1; } writeln(cnt); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { long a, b, c, d, e, f, g, h, i; scan(a, b, c); scan(d, e, f); scan(g, h, i); bool x = a - b == d - e; x = x && d - e == g - h; x = x && b - c == e - f; x = x && e - f == h - i; x = x && a - d == b - e; x = x && b - e == c - f; x = x && d - g == e - h; x = x && e - h == f - i; writeln(x ? "Yes" : "No"); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { long N; scan(N); auto ans = N * (N - 1) / 2; writeln(ans); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.stdio, std.string, std.conv, std.math; void main(){ real x = 0, y = 0, rad = 90; while(true){ auto line = readln.chomp.split(","); auto speed = line[0].to!int; if(speed == 0) break; x += cos(rad / 180 * PI) * speed; y += sin(rad / 180 * PI) * speed; rad -= line[1].to!int; } writeln(cast(int)x); writeln(cast(int)y); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; } enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { long n; scan(n); long ans = infl; for (long i = 1; i*i <= n; i++) { if (n % i == 0) { chmin(ans, (i - 1) + (n / i - 1)); } } writeln(ans); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { int n = readint; if (n == 1) { writeln("Hello World"); } else { int a = readint; int b = readint; writeln(a + b); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.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 long[](t); foreach (ti; 0..t) { auto x = RD; auto y = RD; auto a = RD; auto b = RD; ans[ti] = abs(x-y) * a + min(abs(x), abs(y)) * b; ans[ti].chmin(x*a + y*a); } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
void main() { long n = rdElem; long[] a = rdRow; long[] l = new long[n+1]; foreach (i; 0 .. n) { l[i+1] = gcd(l[i], a[i]); } long[] r = new long[n+1]; foreach_reverse (i; 0 .. n) { r[i] = gcd(r[i+1], a[i]); } long result; foreach (i; 0 .. n) { result = max(result, gcd(l[i], r[i+1])); } result.writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio; import std.string; import std.conv; import std.math; void main () { double a = 100000; int n = to!int(chomp(readln())); foreach (i; 0 .. n) { a = a * 1.05; if(a % 1000) a = a - (a % 1000) + 1000; } writeln(to!int(a)); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.15f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; writeln(N^^3); stdout.flush(); debug readln(); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.container; import std.bigint; import std.math; void main() { readln; auto x = readln.chomp.split.map!(to!long).array; writeln(x.reduce!(min), " ", x.reduce!(max), " ", x.reduce!("a+b")); }
D
void main() { dchar[] s = readln.chomp.to!(dchar[]); s.sort!"a < b"; writeln(s == "abc" ? "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
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; void main() { int n; n = readln.chomp.to!int; while (n) { auto a = new int[9][9]; auto b = new int[9][9]; auto c = new int[9][9]; auto ans = new bool[9][9]; int s, t; foreach (i; 0..9) { a[i] = readln.split.map!(to!int).array; c[s][(i%3)*3..(i%3)*3+3] = a[i][0..3]; c[s+1][(i%3)*3..(i%3)*3+3] = a[i][3..6]; c[s+2][(i%3)*3..(i%3)*3+3] = a[i][6..9]; foreach (j; 0..9) { b[j][i] = a[i][j]; } if ((i+1) % 3 == 0) s += 3; t = ++t % 3; } foreach (i; 0..9) { foreach (j; 0..9) { foreach (k; 0..9) { if (j == k) continue; if (a[i][j] == a[i][k]) ans[i][j] = ans[i][k] = 1; if (b[i][j] == b[i][k]) ans[j][i] = ans[k][i] = 1; if (c[i][j] == c[i][k]) ans[i/3*3 + j/3][(i%3)*3 + (j%3)] = ans[i/3*3 + k/3][(i%3)*3 + (k%3)] = 1; } } } foreach (i; 0..9) { foreach (j; 0..9) { write(ans[i][j] ? "*" : " ", a[i][j]); } writeln(""); } if (--n) writeln(""); } }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int k; readV(k); writeln((k/2) * ((k+1)/2)); }
D
import std.stdio; import std.algorithm; import std.string; import std.conv; import std.math; void main(){ int[4] bty; while(true){ auto s = readln(); if(stdin.eof()) break; auto s1 = split(chomp(s),','); if(s1[1]=="AB") s1[1] = "C"; if(s1[1]=="O") s1[1] = "D"; bty[to!int (to!char(s1[1]) - 'A')]++; } foreach(i;bty) writeln(i); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; alias Score = Tuple!(long, "p", long, "c"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { long n, k; scan(n, k); auto a = aryread(); writeln((n - 1 + k - 2) / (k - 1)); }
D
void main() { auto S = rs.to!(dchar[]); auto N = S.length; bool isParin(dchar[] s) { debug writefln(s); return s == s.retro.array; } bool f1 = isParin(S); bool f2 = isParin(S[0..(N-1)/2]); bool f3 = isParin(S[(N+3)/2 - 1..$]); if(f1 && f2 && f3) writeln("Yes"); else writeln("No"); } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; void times(alias fun)(int n) { foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } void main() { 2.rep!(() => readln).back.chomp.map!"a=='x'".array.pipe!( ary => [ [false, false], [false, true], [ true, false], [ true, true] ].find!( seed => ary.fold!( (a, b) => [a[1], a[0]^a[1]^b] )(seed).equal(seed) ).pipe!( res => res.empty ? "-1" : res.front.pipe!( seed => ary.cumulativeFold!( (a, b) => [a[1], a[0]^a[1]^b] )(seed).map!(a => a[0]) ).map!"a?'W':'S'".array ) ).writeln; } // ---------------------------------------------- // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { auto minElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto minimum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b < minimum) { element = a; minimum = b; } } return element; } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto maximum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b > maximum) { element = a; maximum = b; } } return element; } }
D
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.typecons, std.range, std.container, std.math, std.algorithm.searching, std.functional,std.mathspecial; void main(){ auto a=readln.chomp.to!int; auto b=readln.chomp.to!int; auto h=readln.chomp.to!int; writeln((a+b)*h/2); }
D
import std.stdio, std.string, std.conv, std.range, std.array, std.algorithm; import std.uni, std.math, std.container, std.typecons, std.typetuple; import core.bitop, std.datetime; immutable long mod = 10^^9 + 7; immutable long inf = mod; void main(){ long n, m; readVars(n, m); auto ans = modpow(n, m, mod); writeln(ans); } bool is_prime(int x){ for(int i = 2; i*i <= x; ++i){ if (x % i == 0) { return false; } } return true; } T modpow(T)(T x, T y, T mod){ T res = 1; while(y > 0){ (res *= x^^(y&1)) %= mod; y >>= 1; x = x^^2 % mod; } return res; } void readVars(T...)(auto ref T args){ auto line = readln.split; foreach(ref arg ; args){ arg = line.front.to!(typeof(arg)); line.popFront; } if(!line.empty){ throw new Exception("args num < input num"); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void main() { auto n = RD!int; auto m = RD!int; auto s = new string[](n); int[string] set, set2; string ans1, ans2; foreach (i; 0..n) { s[i] = RD!string; auto tt = s[i].dup; tt.reverse; auto t = tt.idup; if (set.get(s[i], 0)) { ans1 ~= s[i]; ans2 = (t ~ ans2).idup; --set[s[i]]; } else { bool ok = true; foreach (j; 0..m/2) { if (s[i][j] != s[i][$-j-1]) ok = false; } if (ok) ++set2[s[i]]; else ++set[t]; } } string bestKey; long cnt; foreach (key; set2.keys) { if (set2[key] > cnt) { cnt = set2[key]; bestKey = key; } } auto ans = ans1; foreach (i; 0..cnt) { ans ~= bestKey; } ans ~= ans2; writeln(ans.length); writeln(ans); stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new int[](t); foreach (ti; 0..t) { auto x = RD-1; ans[ti] = 1; long y = 1; long cnt = 1; foreach (i; 0..10^^7) { y *= 2; long z = cnt*2 + y^^2; if (z > x) break; x -= z; ++ans[ti]; cnt = z; } } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.stdio; import std.string; import std.range; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } long lcm(long a, long b) { return a / gcd(a, b) * b; } void main() { int n = readint(); long[] xs; for (int i = 0; i < n; i++) { xs ~= readln.chomp.to!long; } auto ans = xs.reduce!lcm; writeln(ans); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.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 bool[](t); foreach (ti; 0..t) { auto n = RD!int; auto k = RD!int; auto h = RDA; long l = h[0], r = h[0] + k-1; bool ok = true; foreach (i; 1..n) { auto ll = max(l - (k-1), h[i]); if (ll > r || ll > h[i] + k-1) { ok = false; break; } auto rr = min(r + (k-1), h[i]+(k-1)*2); l = ll; r = rr; debug writeln("i:", i, " l:", l, " r:", r); } if (!ok) continue; ans[ti] = l == h.back; } foreach (e; ans) writeln(e ? "YES" : "NO"); debug readln; }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int N, K; scan(N, K); auto ans = f(N, K); writeln(ans); } int f(int N, int K) { return N > 0 ? f(N / K, K) + 1 : 0; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto k = RD!int; long tmp = n; tmp.modpow(k); ans[ti] = tmp; } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n, m; rd(n, m); auto a=readln.split.to!(int[]); auto b=new int[](n); foreach(e; a) b[e-1]=1; int c=0; bool f(){ return reduce!((r, e)=>(r && e==1))(true, b[0..m]); } foreach_reverse(i; 0..n){ if(f()) break; if(b[i]){ foreach(j; 0..n){ if(b[j]==0){ swap(b[j], b[i]); break; } } c++; } } writeln(c); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x) e=l[i].to!(typeof(e)); }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; const MOD = 10^^9 + 7; int[int] fac(int n) { int[int] ps; while (n % 2 == 0) { ps[2]++; n /= 2; } for (int i = 3; i * i <= n; i += 2) { while (n % i == 0) { ps[i]++; n /= i; } } if (n > 1) ps[n]++; return ps; } long calc(int n, int m) { long ans = 1; foreach (v; fac(m).values) { // v 個の素因数と n - 1 個の仕切りの並べ方 ans = (ans * nck(v + n - 1, n - 1)) % MOD; } return ans; } void main() { initFactTable(210000); int n, m; scan(n, m); writeln(calc(n, m)); } long[] fact; // 階乗 long[] factInv; // 階乗の逆元 void initFactTable(int n) { fact = new long[n + 1]; fact[0] = 1; for (int i = 1; i <= n; i++) fact[i] = i * fact[i - 1] % MOD; factInv = new long[n + 1]; factInv[n] = modPow(fact[n], MOD - 2, MOD); for (int i = n - 1; i >= 0; i--) factInv[i] = factInv[i + 1] * (i + 1) % MOD; } long modPow(long x, long k, long m) { if (k == 0) return 1; if (k % 2 == 0) return modPow(x * x % m, k / 2, m); return x * modPow(x, k - 1, m) % m; } long nck(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return (fact[n] * factInv[k] % MOD) * factInv[n - k] % MOD; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long A, B; scan(A, B); writeln((A % 3 == 0 || B % 3 == 0 || (A + B) % 3 == 0) ? "Possible" : "Impossible"); }
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 a = readln.split[0].to!int; auto b = readln.split[0].to!int; auto c = readln.split[0].to!int; auto d = readln.split[0].to!int; auto e = readln.split[0].to!int; auto k = readln.split[0].to!int; if ( e-a > k ) writeln(":("); else writeln("Yay!"); }
D
void main() { auto S = rs.to!(dchar[]); S[3] = '8'; S.writeln; } // =================================== import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; 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; // readln import std.array; // split import std.conv; // to void main(){ string[] s = split(readln()); int a = to!int(s[0]); int b = to!int(s[1]); int c = to!int(s[2]); if(a == b) writeln(c); else if(a == c) writeln(b); else writeln(a); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { auto nq = readints(); int n = nq[0], q = nq[1]; auto uf = new UnionFind(n); for (int i = 0; i < q; i++) { auto xs = readints(); int com = xs[0], x = xs[1], y = xs[2]; switch (com) { case 0: uf.unite(x, y); break; case 1: writeln(uf.isSame(x, y) ? 1 : 0); break; default: } } } class UnionFind { private int[] _data; this(int n) { _data = new int[](n + 1); _data[] = -1; } int root(int a) { if (_data[a] < 0) return a; return _data[a] = root(_data[a]); } bool unite(int a, int b) { int rootA = root(a); int rootB = root(b); if (rootA == rootB) return false; _data[rootA] += _data[rootB]; _data[rootB] = rootA; return true; } bool isSame(int a, int b) { return root(a) == root(b); } int size(int a) { return -_data[root(a)]; } }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ char[] s = readln.chomp.to!(char[]); int status = 0; long count = 0; long ans = 0; foreach(c; s){ if(c == 'A'){ if(status == 1) count += 1; else count = 1, status = 1; } else if(c == 'B'){ if(status == 1) status = 2; else count = 0, status = 0; } else if(c == 'C'){ if(status == 2){ ans += count; status = 1; } else count = 0, status = 0; } log(c, "count:", count, "status:", status, "ans:", ans); } ans.writeln; } /* 頭から見ていく 頭から見ていって損になることはない なぜなら「あえてやらない」=「ABCの形のまま残す」だが BC... あるいは C... の形が役に立つケースはないので A カウンタを+1する  現在位置は「A」 B 現在位置が「A」なら現在位置は「AB」 それ以外ならカウンタを0にして現在位置は「」 C 現在位置が「AB」なら 答えを+1する カウンタが正ならカウンタを−1して答えを+1する 現在位置は「A」 それ以外ならカウンタを0にして現在位置は「」 */
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; bool solve() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!int).array; auto d = A.reduce!max - A.reduce!min; if (d == 0) { if (A[0] == N - 1) return true; else if (A[0] <= N / 2) return true; else return false; } else if (d == 1) { auto a = A.reduce!max; auto b = A.reduce!min; int ca, cb; foreach (aa; A) { if (aa == a) ca += 1; else if (aa == b) cb += 1; } N = ca; a -= cb; if (a <= 0) return false; else if (a <= N / 2) return true; else return false; } else { return false; } } void main() { writeln(solve ? "Yes" : "No"); }
D
import std.stdio, std.conv, std.math, std.string, std.range, std.array, std.algorithm, std.typecons; void main(){ auto buf = readln().strip().split().map!(to!int)(); immutable N = buf[0]; immutable K = buf[1]; if((N-1)/2 + 1 >= K) { writeln("YES"); } else { writeln("NO"); } }
D
import std.stdio, std.string, std.array, std.algorithm, std.conv, std.typecons, std.numeric, std.math; void main() { for (;;) { auto H = readln.chomp.to!int; if (H == 0) return; int[][] MAP; MAP.length = H; foreach (i; 0..H) MAP[i] = readln.split.to!(int[]); auto changed = true; int r; while (changed) { foreach (i; 0..H) { foreach_reverse (l; 3..6) { foreach (x; 0..6-l) { foreach (j; x+1..x+l) if (MAP[i][x] != MAP[i][j]) goto ng; r += MAP[i][x] * l; foreach (j; x..x+l) MAP[i][j] = 0; goto ok; ng: } } ok: } changed = false; foreach_reverse (i; 0..H) { foreach (j; 0..5) { if (MAP[i][j] == 0) { foreach_reverse (k; 0..i) if (MAP[k][j] > 0) { changed = true; MAP[i][j] = MAP[k][j]; MAP[k][j] = 0; break; } } } } } writeln(r); } }
D
/+ dub.sdl: name "B" dependency "dunkelheit" version="1.0.1" +/ import std.stdio, std.algorithm, std.range, std.conv, std.string; // import dkh.foundation, dkh.scanner; int main() { Scanner sc = new Scanner(stdin); scope(exit) assert(!sc.hasNext); string s; sc.read(s); auto li = s.group.array; debug writeln(li); long ans = 0; auto m = li.length; long[] cnt = new long[m + 1]; long lsm = 0, rsm = 0; foreach (tok; li) { if (tok[0] == 'v') { rsm += tok[1] - 1; } } foreach (tok; li) { if (tok[0] == 'v') { lsm += tok[1] - 1; rsm -= tok[1] - 1; } if (tok[0] == 'o') { ans += lsm * tok[1] * rsm; } } writeln(ans); return 0; } /* IMPORT /home/yosupo/Programs/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/Programs/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/Programs/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; private File f; this(File f) { this.f = f; } private char[512] lineBuf; private 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) { assert(succW()); 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 : enforce; 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.string; import std.algorithm, std.array, std.container, std.typecons; import std.numeric, std.math; import core.bitop; T RD(T = long)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool INSIDE(T)(T x, T b, T e) { return x >= b && x < e; } long mod = pow(10, 9) + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } struct UnionFind { void init(long n) { par = new long[](n); foreach (i; 0..n) par[i] = i; } long root(long i) { return par[i] == i ? i : (par[i] = root(par[i])); } bool same(long i, long j) { return root(i) == root(j); } void unite(long i, long j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; } long[] par; } long[] dijkstra(long from, long to, long[][] edges) { long[] path; auto cost = new long[](edges.length); fill(cost, long.max); cost[from] = 0; long[][] open = [[from]]; while (!open.empty) { auto n = open.front; open.popFront; auto p = n[0]; foreach (i; 0..edges.length) { if (edges[p][i] == -1) continue; auto c = cost[p] + edges[p][i]; if (c < cost[to] && c < cost[i]) { cost[i] = c; if (i == to) path = i ~ n; else open ~= i ~ n; } } } return cost[to] ~ path; } void main() { auto N = RD; long[] ABC; ABC ~= RD; ABC ~= RD; ABC ~= RD; long[] l; foreach (i; 0..N) l ~= RD; auto memo = new long[](256); void comp(long[] arr, long x, ulong path) { memo[path] = x; foreach (i, e; arr) { auto bit = 1UL << i; if (bit & path) continue; comp(arr, x + e, path | bit); } } comp(l, 0, 0); long[2] f(long[] arr, long x, ulong used) { long use, cost = long.max; foreach (i, e; memo) { if (i & used || i == 0) continue; auto c = abs(x - e) + (popcnt(cast(uint)i) - 1) * 10; if (c < cost) { cost = c; use = i; } } return [cost, use]; } long cc(long i, long x) { return abs(x - memo[i]) + (popcnt(cast(uint)i) - 1) * 10; } long ans = long.max; foreach (i; 1..256) { foreach (j; 1..256) { if (i & j) continue; foreach (k; 1..256) { if (i & k || j & k) continue; ans = min(ans, cc(i, ABC[0]) + cc(j, ABC[1]) + cc(k, ABC[2])); /*long cost; ulong used; auto r1 = f(l, ABC[i], used); cost += r1[0]; used |= cast(ulong)r1[1]; auto r2 = f(l, ABC[j], used); cost += r2[0]; used |= cast(ulong)r2[1]; auto r3 = f(l, ABC[k], used); cost += r3[0]; if (cost < ans) { stderr.writeln(r1[0], " ", r2[0], " ", r3[0]); stderr.writefln("%b, %b, %b", r1[1], r2[1], r3[1]); } ans = min(ans, cost);*/ } } } writeln(ans); stdout.flush(); }
D
import std.algorithm; import std.conv; import std.math; import std.range; import std.stdio; import std.string; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto n = readln.strip.to !(int); auto a = readln.splitter.map !(to !(int)).array; long res = 0; foreach (i; 0..n) { auto lo = 0; if (i > 0) { lo = max (lo, a[i - 1]); } if (i + 1 < n) { lo = max (lo, a[i + 1]); } int delta = max (0, a[i] - lo); a[i] -= delta; res += delta; } debug {writeln (a, " ", res);} res += a[0]; res += a[n - 1]; foreach (i; 1..n) { res += abs (a[i] - a[i - 1]); } writeln (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; void main() { auto N = readln.chomp.to!int; auto S = readln.chomp; auto A = readln.split.map!(to!long).array; auto dp = new long[][](N+1, 4); foreach (i; 0..N+1) fill(dp[i], 1L<<59); dp[0][0] = 0; foreach (i; 0..N) { dp[i+1] = dp[i].dup; if (S[i] == 'h') { dp[i+1][0] = dp[i][0] + A[i]; dp[i+1][1] = min(dp[i][0], dp[i][1]); } else if (S[i] == 'a') { dp[i+1][1] = dp[i][1] + A[i]; dp[i+1][2] = min(dp[i][1], dp[i][2]); } else if (S[i] == 'r') { dp[i+1][2] = dp[i][2] + A[i]; dp[i+1][3] = min(dp[i][2], dp[i][3]); } else if (S[i] == 'd') { dp[i+1][3] = dp[i][3] + A[i]; } } dp[N].reduce!min.writeln; }
D
//prewritten code: https://github.com/antma/algo import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.traits; final class InputReader { private: ubyte[] p, buffer; bool eof; bool rawRead () { if (eof) { return false; } p = stdin.rawRead (buffer); if (p.empty) { eof = true; return false; } return true; } ubyte nextByte(bool check) () { static if (check) { if (p.empty) { if (!rawRead ()) { return 0; } } } auto r = p.front; p.popFront (); return r; } public: this () { buffer = uninitializedArray!(ubyte[])(16<<20); } bool seekByte (in ubyte lo) { while (true) { p = p.find! (c => c >= lo); if (!p.empty) { return false; } if (!rawRead ()) { return true; } } } template next(T) if (isSigned!T) { T next () { if (seekByte (45)) { return 0; } T res; ubyte b = nextByte!false (); if (b == 45) { while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 - (b - 48); } } else { res = b - 48; while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 + (b - 48); } } } } template next(T) if (isUnsigned!T) { T next () { if (seekByte (48)) { return 0; } T res = nextByte!false () - 48; while (true) { ubyte b = nextByte!true (); if (b < 48 || b >= 58) { break; } res = res * 10 + (b - 48); } return res; } } T[] nextA(T) (in int n) { auto a = uninitializedArray!(T[]) (n); foreach (i; 0 .. n) { a[i] = next!T; } return a; } } void main() { auto r = new InputReader (); immutable nt = r.next!uint (); foreach (tid; 0 .. nt) { const int n = r.next!uint; const int k = r.next!uint; auto a = r.nextA!uint (n); auto b = new int[n]; foreach (i; 1 .. n - 1) { if (a[i] > a[i-1] && a[i] > a[i+1]) { b[i] = 1; } } foreach (i; 1 .. n) { b[i] += b[i-1]; } int res = -1, peaks = int.min; foreach (j; k .. n + 1) { int i = j - k; //[i, j) int t = b[j - 2] - b[i]; if (peaks < t) { res = i; peaks = t; } } ++res; ++peaks; writeln (peaks, ' ', res); } }
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 string[](t); foreach (ti; 0..t) { auto n = RD!int; auto b = RD!string; ans[ti] ~= '1'; long x; if (b[0] == '1') x = 2; else x = 1; foreach (i; 1..n) { if (x == 2) { x = 1; if (b[i] == '0') ans[ti] ~= '1'; else ans[ti] ~= '0'; } else if (x == 1) { if (b[i] == '0') { ans[ti] ~= '0'; x = 0; } else { ans[ti] ~= '1'; x = 2; } } else { ans[ti] ~= '1'; if (b[i] == '0') x = 1; else x = 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.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto N = RD; writeln((1000 - (N%1000)) % 1000); stdout.flush; debug readln; }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; void main() { string[] inputs = split(readln()); int a = to!int(inputs[0]); int b = to!int(inputs[1]); int c = to!int(inputs[2]); if(a + b == c || b + c == a || c + a == b) "Yes".writeln; else "No".writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto n = RD!int; auto p = RDA!int; int[] arr; int num, cnt; foreach (i; 0..n*2) { if (p[i] > num) { if (cnt != 0) { arr ~= cnt; } num = p[i]; cnt = 0; } ++cnt; } arr ~= cnt; debug writeln(arr); auto dp = new bool[](n+1); dp[0] = true; foreach (e; arr) { foreach_reverse (i; 0..dp.length) { if (!dp[i]) continue; auto ni = i + e; if (ni < n+1) { dp[ni] = true; } } } ans[ti] = dp[n]; } foreach (e; ans) { writeln(e ? "YES" : "NO"); } stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } void main(){ auto n = read.to!long; auto h = read.to!long; auto w = read.to!long; auto a = n - h + 1; auto b = n - w + 1; (a * b).writeln; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}} void main() { int a, b, c, d; readV(a, b, c, d); if (a+b > c+d) writeln("Left"); else if (a+b < c+d) writeln("Right"); else writeln("Balanced"); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.container; import std.array; import std.math; import std.range; import std.typecons; import std.ascii; void main() { readln; auto a = readln.chomp.split.map!(to!int); writeln = a.reduce!max - a.reduce!min; }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); void main() { auto a = lread(); auto s = sread(); if (a >= 3200) writeln(s); else if (a < 3200) writeln("red"); } //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, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto K = readln.chomp.to!int; long[] rnrn; void make(int i, long n, long l) { rnrn ~= n; if (i == 10) return; foreach (m; max(0, l-1)..min(10, l+2)) { make(i+1, n*10+m, m); } } foreach (i; 1..10) make(0, i, i); sort(rnrn); writeln(rnrn[K-1]); }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { auto s = new bool[](10^^5 + 1); s[] = 1; s[0] = s[1] = 0; for (int p = 2; p*p <= 10^^5; p++) { if (s[p]) { for (int q = p*p; q <= 10^^5; q += p) { s[q] = 0; } } } auto t = new int[](10^^5 + 1); foreach (i ; 3 .. 10^^5 + 1) { if (s[i] && s[(i + 1) / 2]) { t[i] = 1; } } foreach (i ; 1 .. 10^^5 + 1) { t[i] += t[i-1]; } int q; int li,ri; scan(q); foreach (i ; 0 .. q) { scan(li, ri); writeln(t[ri] - t[li-1]); } } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void main() { auto n = RD; auto a = RD; auto b = RD; if (a < b) swap(a, b); long ans = 2; ans.modpow(n); ans.mods(1); long x1 = 1, x2 = 1, y1 = 1, y2 = 1; foreach (i; 0..a) { x1.modm(n); y1.modm(a); --a; if (b != 0) { x2.modm(n); y2.modm(b); --b; } --n; } y1.modpow(mod - 2); x1.modm(y1); ans.mods(x1); y2.modpow(mod - 2); x2.modm(y2); ans.mods(x2); writeln(ans); stdout.flush; debug readln; }
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; } long extgcd(long a, long b, ref long x, ref long y) { long g = a; x = 1; y = 0; if (b != 0) { g = extgcd(b, a % b, y, x); y -= (a / b) * x; } return g; } void main() { int a, b; scanf("%d %d\n", &a, &b); long x, y; extgcd(a, b, x, y); writeln(x, " ", y); }
D
import std; void main() { const N = readln().chomp().to!uint(); const S = readln().chomp(); size_t ans = count(S, 'R') * count(S, 'G') * count(S, 'B'); foreach (uint i; 0 .. N) foreach (uint j; i + 1 .. N) { if (S[i] == S[j]) continue; const k = 2 * j - i; if (k >= N) continue; if (S[j] == S[k] || S[i] == S[k]) continue; --ans; } writeln(ans); }
D
import std.stdio; import std.algorithm; import std.string; import std.conv; import std.math; void main(){ int[101] team; int count = 0; while(true){ auto s = split(chomp(readln()),','); int n1 = to!int(s[0])-1; int n2 = to!int(s[1]); if(n1==-1&&n2==0) break; team[n1] = n2; count++; } int[101] rank; int r = 1; bool flg = false; for(int i=30;i>=0;i--){ flg = false; for(int j=0;j<count;j++){ if(team[j] == i){ rank[j] = r; flg = true; } } if(flg) r++; } while(true){ auto s1 = readln(); if(stdin.eof()) break; int n = to!int(chomp(s1)); writeln(rank[n-1]); } }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; alias Pair = Tuple!(int, "x", int, "y"); void main() { while (1) { int n; scan(n); if (!n) return; auto a = new Pair[](n); foreach (i ; 1 .. n) { int ni, di; scan(ni, di); if (di == 0) { a[i] = Pair(a[ni].x-1, a[ni].y); } else if (di == 1) { a[i] = Pair(a[ni].x, a[ni].y - 1); } else if (di == 2) { a[i] = Pair(a[ni].x+1, a[ni].y); } else { a[i] = Pair(a[ni].x, a[ni].y+1); } } int lm = 1000, rm = -1000; int bm = 1000, tm = -1000; foreach (ai ; a) { lm = min(lm, ai.x); rm = max(rm, ai.x); bm = min(bm, ai.y); tm = max(tm, ai.y); } writeln(rm - lm + 1, " ", tm - bm + 1); } } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.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 X = RD!string; long ans; long cnt; foreach (c; X) { if (c == 'S') ++cnt; else if (cnt != 0) { ++ans; --cnt; } } writeln(X.length - ans*2); stdout.flush(); debug readln(); }
D
/+ dub.sdl: name "B" dependency "dcomp" version=">=0.3.2" lflags "-stack_size" "100000000" +/ import std.algorithm, std.conv, std.range, std.stdio; // import dcomp.scanner; // import dcomp.algorithm; int main(string[] argv) { auto sc = new Scanner(); int n, k; sc.read(n, k); int[26] co; foreach (i; 0..n) { string s; sc.read(s); int id = s[0] - 'A'; co[id]++; } // impossible -> true bool f(int md) { int sm = co[].map!(a => min(a, md)).sum; return sm < k*md; } writeln(binSearch!f(-1, 10^^8)-1); return 0; } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f = stdin) { this.f = f; } string[] buf; private bool succ() { while (!buf.length) { if (f.eof) return false; buf = f.readln.split; } return true; } private bool readSingle(T)(ref T x) { if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { //string or char[10] etc x = buf.front; buf.popFront; } else { static if (isStaticArray!T) { //static assert(buf.length == T.length); } x = buf.map!(to!E).array; buf.length = 0; } } else { x = buf.front.to!T; buf.popFront; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); fout.writeln("1 2 3"); fout.writeln("ab cde"); fout.writeln("1.0 1.0 2.0"); fout.close; Scanner sc = new Scanner(File(fileName, "r")); int a; int[2] b; char[2] c; string d; double e; double[] f; sc.read(a, b, c, d, e, f); assert(a == 1); assert(equal(b[], [2, 3])); assert(equal(c[], "ab")); assert(equal(d, "cde")); assert(e == 1.0); assert(equal(f, [1.0, 2.0])); } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/algorithm.d */ // module dcomp.algorithm; //[0,0,0,...,1,1,1]で、初めて1となる場所を探す。pred(l) == 0, pred(r) == 1と仮定 T binSearch(alias pred, T)(T l, T r) { while (r-l > 1) { T md = (l+r)/2; if (!pred(md)) l = md; else r = md; } return r; } import std.range.primitives; Rotator!Range rotator(Range)(Range r) if (isForwardRange!Range && hasLength!Range) { return typeof(return)(r); } struct Rotator(Range) if (isForwardRange!Range && hasLength!Range) { size_t cnt; Range start, now; this(Range r) { cnt = 0; start = r.save; now = r.save; } this(this) { start = start.save; now = now.save; } @property bool empty() { return now.empty; } @property auto front() { assert(!now.empty); import std.range : take, chain; return chain(now, start.take(cnt)); } @property Rotator!Range save() { return this; } void popFront() { cnt++; now.popFront; } } E minimum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, E) == void)) { import std.functional; while (!range.empty) { if (binaryFun!pred(range.front, seed)) { seed = range.front; } range.popFront; } return seed; } ElementType!Range minimum(alias pred = "a < b", Range)(Range range) if (isInputRange!Range && !isInfinite!Range) { assert(!range.empty, "range must not empty"); auto e = range.front; range.popFront; return minimum!pred(range, e); } unittest { assert(minimum([2, 1, 3]) == 1); assert(minimum!"a > b"([2, 1, 3]) == 3); assert(minimum([2, 1, 3], -1) == -1); assert(minimum!"a > b"([2, 1, 3], 100) == 100); } bool[ElementType!Range] toMap(Range)(Range r) { import std.algorithm : each; bool[ElementType!Range] res; r.each!(a => res[a] = true); return res; }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std; // dfmt on void main() { long X, Y; scan(X, Y); writeln(X % Y == 0 ? -1 : X); }
D
import std.algorithm, std.array, std.container, std.range, std.bitmanip; import std.numeric, std.math, std.bigint, std.random, core.bitop; import std.string, std.conv, std.stdio, std.typecons; void main() { auto rd = readln.split.map!(to!int); auto a = rd[0], b = rd[1]; writeln(a == b ? "a == b" : (a < b ? "a < b" : "a > b")); }
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() { int n = readln.chomp.to!int; bool[int] set; foreach (i; 0..n) { int a = readln.chomp.to!int; if (a in set) { set.remove(a); } else { set[a] = true; } } writeln = set.length; }
D
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math, std.container, std.typecons; import core.stdc.stdio; // foreach, foreach_reverse, writeln void main() { int n, k; scanf("%d%d", &n, &k); int[] x = new int[n]; foreach (i; 0..n) scanf("%d", &x[i]); int c = x.assumeSorted.lowerBound(0).length.to!int; int ans = 1<<30; foreach (i; 0..c+1) { if (i+k > n) break; int l = 0, r = 0; if (i != c) { if (c-i > k) continue; l = -x[i]; } r = max(0, x[i+k-1]); ans = min(ans, l*2+r); ans = min(ans, l+r*2); } writeln(ans); }
D
import std.stdio, std.conv, std.array, std.string; import std.algorithm; import std.container; import std.range; import core.stdc.stdlib; import std.math; import std.typecons; void main() { auto N = readln.chomp.to!int; char[] S = cast(char[])readln.chomp; foreach(i; 0..S.length) { S[i] = 'A' + (S[i] - 'A' + N)%26; } S.writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; int a, b, ab, r; foreach (_; 0..N) { auto s = readln.chomp; if (s[0] == 'B' && s[$-1] == 'A') { ++ab; } else if (s[0] == 'B') { ++b; } else if (s[$-1] == 'A') { ++a; } foreach (i; 0..s.length-1) { if (s[i..i+2] == "AB") ++r; } } if (ab > 0) { r += ab-1; if (a > 0) { --a; r += 1; } if (b > 0) { --b; r += 1; } } r += min(a, b); writeln(r); }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std; // dfmt on void main() { long H, W; scan(H, W); long ans = long.max; foreach (h; 1 .. H) { long a = h * W; long b = (H - h) * (W / 2); long c = (H - h) * (W - W / 2); if (a == 0 || b == 0 || c == 0) continue; dprint(a, b, c); ans = ans.min(max(a, b, c) - min(a, b, c)); } foreach (h; 1 .. H) { long a = h * W; long b = ((H - h) / 2) * W; long c = ((H - h) - (H - h) / 2) * W; if (a == 0 || b == 0 || c == 0) continue; dprint(a, b, c); ans = ans.min(max(a, b, c) - min(a, b, c)); } foreach (w; 1 .. W) { long a = w * H; long b = (W - w) * (H / 2); long c = (W - w) * (H - H / 2); if (a == 0 || b == 0 || c == 0) continue; dprint(a, b, c); ans = ans.min(max(a, b, c) - min(a, b, c)); } foreach (w; 1 .. W) { long a = w * H; long b = ((W - w) / 2) * H; long c = ((W - w) - (W - w) / 2) * H; if (a == 0 || b == 0 || c == 0) continue; dprint(a, b, c); ans = ans.min(max(a, b, c) - min(a, b, c)); } writeln(ans); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { readint; int k = readint; auto xs = readints; int ans = 0; foreach (x; xs) { // (0, i) のロボットが回収する int a = x * 2; // (k, i) のロボットが拐取する int b = (k - x) * 2; ans += min(a, b); } writeln(ans); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); void main() { auto n = lread(); auto a = aryread(); long ans; foreach (i; 0 .. n) { ans += a[i] - 1; } writeln(ans); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
D