code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, core.bitop; enum P = 998244353; void main() { auto ns = readln.split.to!(long[]); auto N = ns[0]; auto S = ns[1]; auto as = readln.split.to!(long[]); auto DP = new long[][][](N+1, S+1, 2); foreach (i; 0..N) { if (as[i] < S) { DP[i+1][as[i]][0] = i+1; } else if (as[i] == S) { DP[i+1][S][1] = ((i+1) * (N-i)) % P; } foreach (s; 0..S) { DP[i+1][s][0] = (DP[i+1][s][0] + DP[i][s][0]) % P; if (as[i] + s < S) { DP[i+1][as[i] + s][0] = (DP[i+1][as[i] + s][0] + DP[i][s][0]) % P; } else if (as[i] + s == S) { DP[i+1][S][1] = (DP[i+1][S][1] + DP[i][s][0] * (N-i)) % P; } } DP[i+1][S][1] = (DP[i+1][S][1] + DP[i][S][1]) % P; } writeln(DP[N][S][1]); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; enum P = 10L^^9+7; void main() { auto N = readln.chomp.to!int; auto DP = new long[][](N+1, 4); DP[0][0] = 1; foreach (i; 0..N) { (DP[i+1][0] += DP[i][0] * 8 % P) %= P; (DP[i+1][1] += DP[i][0] + DP[i][1] * 9 % P) %= P; (DP[i+1][2] += DP[i][0] + DP[i][2] * 9 % P) %= P; (DP[i+1][3] += DP[i][1] + DP[i][2] + DP[i][3] * 10 % P) %= P; } writeln(DP[N][3]); }
D
import std.stdio, std.conv, std.string; import std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } int DEBUG_LEVEL = 0; void print()(){ writeln(""); } void print(T, A ...)(T t, lazy A a){ write(t), print(a); } void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); } void main(string[] args){ if(args.length > 1 && args[1] == "-debug"){ if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int; else DEBUG_LEVEL = 1; } long n = read.to!long; long ans; for(long k = 1; k * k <= n; k ++){ if(n % k != 0) continue; long m = n / k - 1; if(m > k) ans += m; } ans.writeln; } /* n = m k + k = (m + 1)k, 0 <= k < m kはnの約数 そのうちで n / k - 1 = m としたとき k < m であるようなものを言えばよい 約数は素因数分解ではなくて割ってみるでよい */
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.4.0" +/ import std.stdio, std.range, std.algorithm, std.conv; import core.bitop; // import dcomp.scanner; void main() { auto sc = new Scanner(stdin); int n; sc.read(n); int[][] g = new int[][](n); foreach(i; 0..n-1) { int a, b; sc.read(a, b); a--; b--; g[a] ~= b; g[b] ~= a; } long dfs(int p, int b) { long one, two; foreach (d; g[p]) { if (d == b) continue; long x = dfs(d, p); two |= one & x; one |= x; } int off = two ? two.bsr + 1 : 0; one = ((one >> off) + 1) << off; return one; } writeln(dfs(0, -1).bsr); } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } 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])); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto abc = readln.split.to!(int[]); writeln(abc[2] <= abc[0] + abc[1] ? "Yes" : "No"); }
D
import std; long calc(long[] a) { if (a.any!(e => e == 0)) return 0; long x = 1; foreach (e; a) { long y = x * e; if (y < x) return -1; // overflow if (y > 10L ^^ 18) return -1; x = y; } return x; } void main() { int n; scan(n); auto a = reads!long; writeln(calc(a)); } 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; 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() { auto tokens = split(my_readln()); auto X = to!ulong(tokens[0]); auto Y = to!ulong(tokens[1]); writeln(X + Y / 2); stdout.flush(); }
D
import std; bool calc(int k, int a, int b) { for (int i = 0; i <= b; i++) { auto x = i * k; if (a <= x && x <= b) return true; } return false; } void main() { int k; scan(k); int a, b; scan(a, b); writeln(calc(k, a, b) ? "OK" : "NG"); } void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int;
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int a, b; scan(a); scan(b); writeln(6 - a - b); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { auto s = sread(); auto a = s[0 .. 2].to!(long); auto b = s[2 .. 4].to!(long); if(a <= 12 && b <= 12) { if(a == 0 && b == 0) { writeln("NA"); } else if(a == 0) { writeln("YYMM"); } else if(b == 0) { writeln("MMYY"); } else { writeln("AMBIGUOUS"); } } else if(a > 12) { if(b == 0 || b > 12) { writeln("NA"); } else { writeln("YYMM"); } } else { if(a == 0 || a > 12) { writeln("NA"); } else { writeln("MMYY"); } } }
D
import std.algorithm; import std.conv; import std.stdio; import std.string; void main() { auto hw = readln.split.map!( to!byte ); auto s = new string[ hw[ 0 ] ]; foreach( ref sl; s ) { sl = readln.strip; } writeln( solve( hw[ 0 ], hw[ 1 ], s ) ); } string solve( in byte h, in byte w, in string[] s ) { foreach( y; 0 .. h ) { foreach( x; 0 .. w ) { if( s[ y ][ x ] == '#' ) { if( 0 <= y - 1 && s[ y - 1 ][ x ] == '#' ) continue; if( y + 1 < h && s[ y + 1 ][ x ] == '#' ) continue; if( 0 <= x - 1 && s[ y ][ x - 1 ] == '#' ) continue; if( x + 1 < w && s[ y ][ x + 1 ] == '#' ) continue; return "No"; } } } return "Yes"; } unittest { assert( solve( 3, 3, [ ".#.", "###", ".#." ] ) == "Yes" ); assert( solve( 5, 5, [ "#.#.#", ".#.#.", "#.#.#", ".#.#.", "#.#.#" ] ) == "No" ); assert( solve( 11, 11, [ "...#####...", ".##.....##.", "#..##.##..#", "#..##.##..#", "#.........#", "#...###...#", ".#########.", ".#.#.#.#.#.", "##.#.#.#.##", "..##.#.##..", ".##..#..##." ] ) == "Yes" ); assert( solve( 4, 3, [ "##.", ".##", "##.", ".##" ] ) == "Yes" ); }
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; auto ta = readints; int t = ta[0], a = ta[1]; auto hs = readints; int best = int.max; int ans = -1; for (int i = 0; i < hs.length; i++) { int s = t * 1000 - hs[i] * 6; // writeln(s); if (abs(s - a * 1000) < best) { best = abs(s - a * 1000); ans = i + 1; } } writeln(ans); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdlib; void main() { auto s = readln.split.map!(to!long); auto N = s[0]; auto A = s[1]; auto B = s[2]; auto C = s[3]; auto Q = N.iota.map!(_ => readln.chomp).array; dchar[] ans; void add(dchar plus, dchar minus) { ans ~= plus; if (plus == 'A') { A += 1; } else if (plus == 'B') { B += 1; } else { C += 1; } if (minus == 'A') { A -= 1; } else if (minus == 'B') { B -= 1; } else { C -= 1; } } foreach (i, q; Q) { if (q == "AB") { if (A == 0 && B == 0) { writeln("No"); return; } else if (A == 1 && B == 1) { if (i == N - 1 || Q[i+1] == "AC") { add('A', 'B'); } else { add('B', 'A'); } } else if (A == 0) { add('A', 'B'); } else if (B == 0) { add('B', 'A'); } else if (A == 1) { add('A', 'B'); } else if (B == 1) { add('B', 'A'); } else if (A <= B) { add('A', 'B'); } else { add('B', 'A'); } } else if (q == "BC") { if (B == 0 && C == 0) { writeln("No"); return; } else if (B == 1 && C == 1) { if (i == N - 1 || Q[i+1] == "AB") { add('B', 'C'); } else { add('C', 'B'); } } else if (B == 0) { add('B', 'C'); } else if (C == 0) { add('C', 'B'); } else if (B == 1) { add('B', 'C'); } else if (C == 1) { add('C', 'B'); } else if (B <= C) { add('B', 'C'); } else { add('C', 'B'); } } else { if (A == 0 && C == 0) { writeln("No"); return; } else if (A == 1 && C == 1) { if (i == N - 1 || Q[i+1] == "AB") { add('A', 'C'); } else { add('C', 'A'); } } else if (A == 0) { add('A', 'C'); } else if (C == 0) { add('C', 'A'); } else if (A == 1) { add('A', 'C'); } else if (C == 1) { add('C', 'A'); } else if (A <= C) { add('A', 'C'); } else { add('C', 'A'); } } } writeln("Yes"); ans.each!writeln; }
D
void main() { long n, l; rdVals(n, l); long[] a = rdRow; long idx; bool ok; foreach (i; 0.. n-1) { if (a[i] + a[i+1] >= l) { idx = i; ok = true; break; } } if (!ok) { "Impossible".writeln; return; } "Possible".writeln; foreach (i; 0 .. idx) { writeln(i + 1); } foreach_reverse (i; idx .. n - 1) { writeln(i + 1); } } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
import std.stdio, std.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() { while (solve) {} } bool solve() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; if (N == 0) return false; auto uf = new UnionFind(N); while (M--) { auto q = readln.split; auto a = q[1].to!int - 1; auto b = q[2].to!int - 1; if (q[0] == "!") { auto w = q[3].to!int; uf.unite(a, b, w); } else { if (uf.find(a) != uf.find(b)) { writeln("UNKNOWN"); } else { writeln(uf.weight[b] - uf.weight[a]); } } } return true; } class UnionFind { int N; int[] table; int[] weight; int[][] group; this(int n) { N = n; table = new int[](N); fill(table, -1); weight = new int[](N); group = N.iota.map!(i => [i]).array; } int find(int x) { return table[x] < 0 ? x : (table[x] = find(table[x])); } void unite(int x, int y, int diff) { int ox = x; int oy = y; x = find(x); y = find(y); if (x == y) return; if (table[x] > table[y]) { swap(x, y); swap(ox, oy); diff *= -1; } int nw = weight[ox] + diff; int ndiff = nw - weight[oy]; foreach (i; group[y]) weight[i] += ndiff; foreach (i; group[y]) group[x] ~= i; group[y] = []; table[x] += table[y]; table[y] = x; } }
D
/* imports all std modules {{{*/ import std.algorithm, std.array, std.ascii, std.base64, std.bigint, std.bitmanip, std.compiler, std.complex, std.concurrency, std.container, std.conv, std.csv, std.datetime, std.demangle, std.encoding, std.exception, std.file, std.format, std.functional, std.getopt, std.json, std.math, std.mathspecial, std.meta, std.mmfile, std.net.curl, std.net.isemail, std.numeric, std.parallelism, std.path, std.process, std.random, std.range, std.regex, std.signals, std.socket, std.stdint, std.stdio, std.string, std.system, std.traits, std.typecons, std.uni, std.uri, std.utf, std.uuid, std.variant, std.zip, std.zlib; /*}}}*/ /+---test 3 1 0 0 ---+/ /+---test 5 0 0 0 0 0 ---+/ void main(string[] args) { readln; const A = readln.split.map!(to!long).array; const N = A.length; auto boxes = new bool[N]; foreach_reverse (i; 1..N+1) { long x; for (long j = i*2; j <= boxes.length; j+=i) { x += boxes[j-1]; } boxes[i-1] = (x%2 != A[i-1]); } size_t[] ans; foreach (i, v; boxes) if (v) ans ~= i+1; ans.length.writeln; foreach (i, v; ans) { if (i != 0) " ".write; v.write; } writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto l = readln.split.to!(int[]); auto H = l[0]; auto W = l[1]; auto A = l[2]; auto B = l[3]; char[][] R; R.length = H; foreach (h; 0..H) { foreach (w; 0..W) { bool x; if (h < B) x = !x; if (w < A) x = !x; R[h] ~= x ? '1' : '0'; } } foreach (r; R) writeln(r); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nx = readln.split.to!(int[]); auto N = nx[0]; auto X = nx[1]; int r = 1, d; foreach (l; readln.split.to!(int[])) { d += l; if (d > X) break; ++r; } writeln(r); }
D
import std.stdio, std.string; void main(){ auto s = readln().chomp(); if( s.length == 4 && s[1]==s[2] && (s[0] == s[1] || s[2] == s[3])) writeln("Yes"); else writeln("No");}
D
import std.algorithm; import std.bigint; import std.container; import std.conv; import std.functional; import std.math; import std.meta; import std.random; import std.range; import std.stdio; import std.string; import std.traits; import std.typecons; long solve(string s, string t){ auto conn = new long[26][](s.length + 1); long[26] a; foreach (i; 0 .. 26) a[i] = -1; foreach (_; 0 .. 2) { foreach_reverse (i, c; s) { conn[i + 1] = a; a[c - 'a'] = i + 1; } conn[0] = a; } long pi, i; long ans; immutable long slen = s.length.to!long; foreach (c; t) { i = conn[pi.to!size_t][c - 'a']; if (i < 0) return ans = -1; if ((i - pi) % slen == 0) ans += slen; else ans += (slen + i - pi) % slen; pi = i; } return ans; } void main(){ auto input = stdin.byLine.map!split.joiner; string s; s = input.front.to!string; input.popFront; string t; t = input.front.to!string; input.popFront; static if (is(ReturnType!solve == void)) solve(s, t); else solve(s, t).writeln; }
D
void main() { long a, b; rdVals(a, b); writeln(a * b); } enum long mod = 10L^^9 + 7; enum long inf = 1L << 60; enum double eps = 1.0e-9; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
void main() { long a, b, c; rdVals(a, b, c); writeln((a * b) >> 1); } 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.range; import std.array; import std.functional; import std.algorithm; import std.conv; import std.container; import std.math; import std.numeric; import std.string; bool[11] vars; string input; bool check() { string[] eq = input.split("="); bool val(string s) { int index = 0; bool parse() { if (s[index] == 'T') { index++; return true; } if (s[index] == 'F') { index++; return false; } if ('a' <= s[index] && s[index] <= 'k') { bool x = vars[s[index] - 'a']; index++; return x; } if (s[index] == '-') { index++; return (!parse); } if (s[index] == '(') { index++; bool x = parse; if (s[index] == '*') { index++; bool y = parse; assert(s[index] == ')'); index++; return (x & y); } else if (s[index] == '+') { index++; bool y = parse; assert(s[index] == ')'); index++; return (x | y); } else { // assert(s[index] == '-' && s[index+1] == '>'); index += 2; bool y = parse; assert(s[index] == ')'); index++; return (y | (!x)); } } assert(0); } return parse; } return val(eq[0]) == val(eq[1]); } void main() { test: while (true) { input = readln.chomp; if (input == "#") break; for (int i = 0; i < (1 << 11); i++) { for (int j = 0; j < 11; j++) { vars[j] = (i & (1 << j)) ? true : false; } if (!check) { "NO".writeln; continue test; } } "YES".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 n; readV(n); writeln((n+110)/111*111); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!int).array; auto odd = A.map!(a => a % 2).sum; auto even = A.map!(a => 1 - a % 2).sum; if (odd == 0 || even == 0) { writeln(0); } else if (odd % 2 == 1) { writeln(N - 1); } else { writeln(N - 2); } }
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; void main(){ auto s = readln().chomp(); writeln(s[0..4], " ", s[4..$]); }
D
import std.stdio; import std.string; import std.format; 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.concurrency; import std.traits; import std.uni; import std.regex; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; enum long INF = long.max/5; void main() { string s = readln.chomp; writeln(s[2]==s[3] && s[4]==s[5] ? "Yes" : "No"); } // ---------------------------------------------- void times(alias fun)(long n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(long n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { // `(x+y-1)/y` will only work for positive numbers ... T t = x / y; if (y > 0 && t * y < x) t++; if (y < 0 && t * y > x) t++; return t; } T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { T t = x / y; if (y > 0 && t * y > x) t--; if (y < 0 && t * y < x) t--; return t; } ref T ch(alias fun, T, S...)(ref T lhs, S rhs) { return lhs = fun(lhs, rhs); } unittest { long x = 1000; x.ch!min(2000); assert(x == 1000); x.ch!min(3, 2, 1); assert(x == 1); x.ch!max(100).ch!min(1000); // clamp assert(x == 100); x.ch!max(0).ch!min(10); // clamp assert(x == 10); } mixin template Constructor() { import std.traits : FieldNameTuple; this(Args...)(Args args) { // static foreach(i, v; args) { foreach(i, v; args) { mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v; } } } void scanln(Args...)(auto ref Args args) { enum sep = " "; enum n = Args.length; enum fmt = n.rep!(()=>"%s").join(sep); string line = readln.chomp; static if (__VERSION__ >= 2074) { line.formattedRead!fmt(args); } else { enum argsTemp = n.iota.map!( i => "&args[%d]".format(i) ).join(", "); mixin( "line.formattedRead(fmt, " ~ argsTemp ~ ");" ); } } // 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); } } } } // popcnt with ulongs was added in D 2.071.0 static if (__VERSION__ < 2071) { ulong popcnt(ulong x) { x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L); x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L); x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL); x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL); x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL); x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL); return x; } }
D
import std.stdio; import std.algorithm; import std.conv; import std.math; import std.string; void main() { uint ans = 0 - 1; int[] a = readln.split.to!(int[]); foreach (i; 0..3){ foreach (j; 0..3) { foreach (k; 0..3) { int t_ans = 0; if (i == j || j == k || k == i) continue; t_ans += abs(a[j]-a[i]); t_ans += abs(a[j]-a[k]); ans = min(ans, t_ans); } } } writeln(ans); }
D
// unihernandez22 // https://atcoder.jp/contests/abc076/tasks/abc076_c // string manipulation import std.stdio; import std.string; void main() { string s = readln.chomp; string t = readln.chomp; long idx = -1; bool matched; for(long i = (s.count-t.count); i >= 0; i--) { if (s[i] == '?' || s[i] == t[0]) { matched = true; foreach(j; 0..t.count) { if (s[i+j] != '?' && s[i+j] != t[j]) { matched = false; break; } } if (matched) { idx = i; break; } } } if (idx == -1) { writeln("UNRESTORABLE"); return; } foreach(i; 0..s.count) { if (s[i] == '?' && (i < idx || i >= t.count+idx)) write("a"); else if (s[i] == '?') write(t[i-idx]); else write(s[i]); } writeln; }
D
import std.stdio; import std.algorithm; import std.string; import std.conv; import std.math; void main(){ int ans = 0; while(true){ auto s = readln(); if(stdin.eof()) break; s = chomp(s); for(int i=0;i<s.length;i++){ char c = s[i]; if('1' <= c && c <= '9' ){ int temp = 0; while('0' <= s[i+temp] && s[i+temp] <= '9'){ temp++; if(i+temp==s.length) break; } ans += to!int(s[i..i+temp]); i += temp; } } } writeln(ans); }
D
import std.stdio, std.algorithm, std.array, std.conv; void main(){ int[] ary = readln.split.to!(int[]); ary = array(filter!("a != 5")(ary)); ary[0] == 7 && ary.length == 1 ? write("YES") : write("NO"); }
D
import std.stdio, std.conv, std.string, std.math, std.regex, std.range, std.ascii, std.algorithm; void main(){ auto N = readln.chomp.to!int; auto S = readln.chomp.to!(char[]); auto K = readln.chomp.to!int; char C = S[K-1]; foreach(i; 0..N){ if(S[i] != C){ S[i] = '*'; } } writeln(S); }
D
void main() { string[] tmp = readln.split; long n = tmp[0].to!long, k = tmp[1].to!long; long[] d = readln.split.to!(long[]); bool[] ok = new bool[10]; ok[] = true; foreach (x; d) { ok[x] = false; } long okmin = ok.countUntil(true); long len = tmp[0].length; long[] money = new long[len+1]; foreach_reverse (i; 1 .. len+1) { money[i] += n % 10; money[i-1] += money[i] / 10; money[i] %= 10; n /= 10; bool up; while (!ok[money[i]]) { up = true; if (money[i] == 9) { ++money[i-1]; money[i] = 0; } else { ++money[i]; } } if (up) money[i+1..$] = okmin; } if (money[0] == 0) { money = money[1..$]; } else { while (!ok[money[0]]) { ++money[0]; } money[1..$] = okmin; } foreach (i, x; money) { x.write; } writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio; import std.string; import std.conv; void main() { string input; const string[] classBoxing = ["light fly", "fly", "bantam", "feather", "light", "light welter", "welter", "light middle", "middle", "light heavy", "heavy"]; while ((input = readln.chomp).length != 0) { double weight = input.to!double; uint index; if (weight <= 48.00) { index = 0; } else if (weight <= 51.00) { index = 1; } else if (weight <= 54.00) { index = 2; } else if (weight <= 57.00) { index = 3; } else if (weight <= 60.00) { index = 4; } else if (weight <= 64.00) { index = 5; } else if (weight <= 69.00) { index = 6; } else if (weight <= 75.00) { index = 7; } else if (weight <= 81.00) { index = 8; } else if (weight <= 91.00) { index = 9; } else { index = 10; } writeln(classBoxing[index]); } }
D
import std.algorithm, std.string, std.range, std.stdio, std.conv; void main() { int N = readln.chomp.to!int, K = readln.chomp.to!int, X = readln.chomp.to!int, Y = readln.chomp.to!int; int ans; if (N <= K) { ans = N * X; } else { int cost1 = K * X, cost2 = (N - K) * Y; ans = cost1 + cost2; } writeln(ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { writeln(readln.chomp.to!int ^^ 2 - readln.chomp.to!int); }
D
import std.stdio; import std.range; import std.array; import std.algorithm; import std.string; import std.conv; import std.typecons; import std.math; void main(){ string s = readln().chomp(); string t = readln().chomp(); solve(s,t).writeln(); } string solve(string s,string t){ string result; string doubleS = s ~ s; result = !find(doubleS, t).empty ? "Yes" : "No"; return result; }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.7.4" +/ import std.stdio, std.algorithm, std.range, std.conv, std.math; // import dcomp.foundation, dcomp.scanner; // import dcomp.geo.primitive, dcomp.geo.circle; int solve() { alias C = Circre2D!double; C[2] c; foreach (i; 0..2) { double x, y, r; sc.read(x, y, r); c[i] = C(Point2D!double(x, y), r); } double di = (c[0].p - c[1].p).abs; if (c[0].r > c[1].r + di) return 2; if (c[1].r > c[0].r + di) return -2; if (c[0].r + c[1].r >= di) return 1; return 0; } int main() { EPS!double = 1e-10; int n; sc.read(n); foreach (i; 0..n) { writeln(solve()); } return 0; } Scanner sc; static this() { sc = new Scanner(stdin); } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; // import dcomp.array; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { FastAppender!(E[]) buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/geo/circle.d */ // module dcomp.geo.circle; // import dcomp.geo.primitive; struct Circre2D(R) { Point2D!R p; R r; this(Point2D!R p, R r) { this.p = p; this.r = r; } } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/array.d */ // module dcomp.array; T[N] fixed(T, size_t N)(T[N] a) {return a;} struct FastAppender(A, size_t MIN = 4) { import std.algorithm : max; import std.conv; import std.range.primitives : ElementEncodingType; import core.stdc.string : memcpy; private alias T = ElementEncodingType!A; private T* _data; private uint len, cap; @property size_t length() const {return len;} bool empty() const { return len == 0; } void reserve(size_t nlen) { import core.memory : GC; if (nlen <= cap) return; void* nx = GC.malloc(nlen * T.sizeof); cap = nlen.to!uint; if (len) memcpy(nx, _data, len * T.sizeof); _data = cast(T*)(nx); } void free() { import core.memory : GC; GC.free(_data); } void opOpAssign(string op : "~")(T item) { if (len == cap) { reserve(max(MIN, cap*2)); } _data[len++] = item; } void insertBack(T item) { this ~= item; } void removeBack() { len--; } void clear() { len = 0; } ref inout(T) back() inout { assert(len); return _data[len-1]; } ref inout(T) opIndex(size_t i) inout { return _data[i]; } T[] data() { return (_data) ? _data[0..len] : null; } } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.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 core.bitop : popcnt; static if (!__traits(compiles, popcnt(ulong.max))) { public import core.bitop : popcnt; int popcnt(ulong v) { return popcnt(cast(uint)(v)) + popcnt(cast(uint)(v>>32)); } } bool poppar(ulong v) { v^=v>>1; v^=v>>2; v&=0x1111111111111111UL; v*=0x1111111111111111UL; return ((v>>60) & 1) != 0; } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/geo/primitive.d */ // module dcomp.geo.primitive; import std.traits; template EPS(R) { R EPS; } int sgn(R)(R a) { if (a < -EPS!R) return -1; if (a > EPS!R) return 1; return 0; } struct Point2D(T) { T[2] d; this(T x, T y) {this.d = [x, y];} this(T[2] d) {this.d = d;} @property ref inout(T) x() inout {return d[0];} @property ref inout(T) y() inout {return d[1];} ref inout(T) opIndex(size_t i) inout {return d[i];} auto opOpAssign(string op)(in Point2D r) { return mixin("this=this"~op~"r"); } auto opBinary(string op:"+")(in Point2D r) const {return Point2D(x+r.x, y+r.y);} auto opBinary(string op:"-")(in Point2D r) const {return Point2D(x-r.x, y-r.y);} static if (isFloatingPoint!T) { T abs() { import std.math : sqrt; return (x*x+y*y).sqrt; } T arg() { import std.math : atan2; return atan2(y, x); } Point2D rot(T ar) { import std.math : cos, sin; auto cosAr = cos(ar), sinAr = sin(ar); return Point2D(x*cosAr - y*sinAr, x*sinAr + y*cosAr); } } } bool near(T)(Point2D!T a, Point2D!T b) if (isIntegral!T) { return a == b; } bool near(T)(Point2D!T a, Point2D!T b) if (isFloatingPoint!T) { return !sgn((a-b).abs); } T dot(T)(in Point2D!T l, in Point2D!T r) { return l[0]*r[0] + l[1]*r[1]; } T cross(T)(in Point2D!T l, in Point2D!T r) { return l[0]*r[1] - l[1]*r[0]; } int ccw(R)(Point2D!R a, Point2D!R b, Point2D!R c) { assert(!near(a, b)); if (near(a, c) || near(b, c)) return 0; int s = sgn(cross(b-a, c-a)); if (s) return s; if (dot(b-a, c-a) < 0) return 2; if (dot(a-b, c-b) < 0) return -2; return 0; } inout(Point2D!R) at(R)(inout Point2D!R[] pol, size_t i) { return pol[i<pol.length?i:i-pol.length]; } int contains(R)(Point2D!R[] pol, Point2D!R p) { import std.algorithm : swap; int res = -1; foreach (i; 0..pol.length) { auto a = pol.at(i) - p, b = pol.at(i+1) - p; if (ccw(a, b, Point2D!R(0, 0)) == 0) return 1; if (a.y > b.y) swap(a, b); if (a.y <= 0 && 0 < b.y) { if (cross(a, b) < 0) res *= -1; } } return res+1; } R area2(R)(Point2D!R[] pol) { R u = 0; foreach (i; 0..pol.length) { auto a = pol.at(i), b = pol.at(i+1); u += cross(a, b); } return u; } int argcmp(T)(Point2D!T l, Point2D!T r) if (isIntegral!T) { int sgn(Point2D!T p) { if (p[1] < 0) return -1; if (p[1] > 0) return 1; if (p[0] < 0) return 2; return 0; } int lsgn = sgn(l); int rsgn = sgn(r); if (lsgn < rsgn) return -1; if (lsgn > rsgn) return 1; T x = cross(l, r); if (x > 0) return -1; if (x < 0) return 1; return 0; } /* This source code generated by dcomp and include dcomp's source code. dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp) dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt) */
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { auto S = sread(); long ans; foreach (stat; 0 .. (1 << (S.length - 1))) { long score; long tmp = S[0] - '0'; foreach (i; 1 .. S.length) { // writeln(tmp, " ", i, " ", stat & (1 << (i - 1))); if (stat & (1 << (i - 1))) { // writeln(tmp); score += tmp; tmp = S[i] - '0'; continue; } tmp = tmp * 10 + (S[i] - '0'); } score += tmp; // writefln("%05b %s", stat, score); ans += score; } writeln(ans); }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; import std.typecons; import std.bigint; // import dcomp.foundation, dcomp.scanner; // import dcomp.container.deque; int main() { auto sc = new Scanner(stdin); int n, m, k; sc.read(n, m, k); m++; int[] a = new int[n]; a.each!((ref x) => sc.read(x)); long[] dp = a.map!(to!long).array; long[] ndp = new long[n]; auto deq = Deque!int(); deq.insertBack(1); deq.removeBack(); auto p = *deq.p; foreach (int ph; 2..k+1) { ndp[] = -(10L^^18); p.clear(); foreach (int i; 0..n) { if (p.length && p[0] == i-m) { p.removeFront(); } if (p.length) { ndp[i] = dp[p[0]] + 1L * ph * a[i]; } while (p.length && dp[p[p.length-1]] <= dp[i]) { p.removeBack(); } p.insertBack(i); } swap(dp, ndp); } writeln(dp.fold!max); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; //fold(for old compiler) static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } unittest { import std.stdio; auto l = [1, 2, 3, 4, 5]; assert(l.fold!"a+b"(10) == 25); } } version (X86) static if (__VERSION__ < 2071) { int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/deque.d */ // module dcomp.container.deque; struct Deque(T) { import core.exception : RangeError; import core.memory : GC; import std.range : ElementType, isInputRange; import std.traits : isImplicitlyConvertible; struct Payload { T *d; size_t st, length, cap; @property bool empty() const { return length == 0; } alias opDollar = length; ref inout(T) opIndex(size_t i) inout { version(assert) if (length <= i) throw new RangeError(); return d[(st+i >= cap) ? (st+i-cap) : st+i]; } private void expand() { import std.algorithm : max; assert(length == cap); auto nc = max(size_t(4), 2*cap); T* nd = cast(T*)GC.malloc(nc * T.sizeof); foreach (i; 0..length) { nd[i] = this[i]; } d = nd; st = 0; cap = nc; } void clear() { st = length = 0; } void insertFront(T v) { if (length == cap) expand(); if (st == 0) st += cap; st--; length++; this[0] = v; } void insertBack(T v) { if (length == cap) expand(); length++; this[length-1] = v; } void removeFront() { assert(!empty, "Deque.removeFront: Deque is empty"); st++; length--; if (st == cap) st = 0; } void removeBack() { assert(!empty, "Deque.removeBack: Deque is empty"); length--; } } struct RangeT(A) { alias T = typeof(*(A.p)); alias E = typeof(A.p.d[0]); T *p; size_t a, b; @property bool empty() const { return b <= a; } @property size_t length() const { return b-a; } @property RangeT save() { return RangeT(p, a, b); } @property RangeT!(const A) save() const { return typeof(return)(p, a, b); } alias opDollar = length; @property ref inout(E) front() inout { return (*p)[a]; } @property ref inout(E) back() inout { return (*p)[b-1]; } void popFront() { version(assert) if (empty) throw new RangeError(); a++; } void popBack() { version(assert) if (empty) throw new RangeError(); b--; } ref inout(E) opIndex(size_t i) inout { return (*p)[i]; } RangeT opSlice() { return this.save; } RangeT opSlice(size_t i, size_t j) { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } RangeT!(const A) opSlice() const { return this.save; } RangeT!(const A) opSlice(size_t i, size_t j) const { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } } alias Range = RangeT!Deque; alias ConstRange = RangeT!(const Deque); alias ImmutableRange = RangeT!(immutable Deque); Payload *p; private void I() { if (!p) p = new Payload(); } private void C() const { version(assert) if (!p) throw new RangeError(); } //some value this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) {I; p = new Payload(); foreach (v; values) { insertBack(v); } } //range this(Range)(Range r) if (isInputRange!Range && isImplicitlyConvertible!(ElementType!Range, T) && !is(Range == T[])) {I; p = new Payload(); foreach (v; r) { insertBack(v); } } @property bool empty() const { return (!p || p.empty); } @property size_t length() const { return (p ? p.length : 0); } alias opDollar = length; ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; } ref inout(T) front() inout {C; return (*p)[0]; } ref inout(T) back() inout {C; return (*p)[$-1]; } void clear() { if (p) p.clear(); } void insertFront(T v) {I; p.insertFront(v); } void insertBack(T v) {I; p.insertBack(v); } void removeFront() {C; p.removeFront(); } void removeBack() {C; p.removeBack(); } Range opSlice() {I; return Range(p, 0, length); } } unittest { import std.algorithm : equal; import std.range.primitives : isRandomAccessRange; import std.container.util : make; auto q = make!(Deque!int); assert(isRandomAccessRange!(typeof(q[]))); //insert,remove assert(equal(q[], new int[](0))); q.insertBack(1); assert(equal(q[], [1])); q.insertBack(2); assert(equal(q[], [1, 2])); q.insertFront(3); assert(equal(q[], [3, 1, 2]) && q.front == 3); q.removeFront; assert(equal(q[], [1, 2]) && q.length == 2); q.insertBack(4); assert(equal(q[], [1, 2, 4]) && q.front == 1 && q.back == 4 && q[$-1] == 4); q.insertFront(5); assert(equal(q[], [5, 1, 2, 4])); //range assert(equal(q[][1..3], [1, 2])); assert(equal(q[][][][], q[])); //const range const auto rng = q[]; assert(rng.front == 5 && rng.back == 4); //reference type auto q2 = q; q2.insertBack(6); q2.insertFront(7); assert(equal(q[], q2[]) && q.length == q2.length); //construct with make auto a = make!(Deque!int)(1, 2, 3); auto b = make!(Deque!int)([1, 2, 3]); assert(equal(a[], b[])); } unittest { import std.algorithm : equal; import std.range.primitives : isRandomAccessRange; import std.container.util : make; auto q = make!(Deque!int); q.clear(); assert(equal(q[], new int[0])); foreach (i; 0..100) { q.insertBack(1); q.insertBack(2); q.insertBack(3); q.insertBack(4); q.insertBack(5); assert(equal(q[], [1,2,3,4,5])); q.clear(); assert(equal(q[], new int[0])); } } unittest { Deque!int a; Deque!int b; a.insertFront(2); assert(b.length == 0); } unittest { import std.algorithm : equal; import std.range : iota; Deque!int a; foreach (i; 0..100) { a.insertBack(i); } assert(equal(a[], iota(100))); } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; if (f.eof) return false; line = lineBuf[]; f.readln(line); } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { //string or char[10] etc //todo optimize auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { //static assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); fout.writeln("1 2 3"); fout.writeln("ab cde"); fout.writeln("1.0 1.0 2.0"); fout.close; Scanner sc = new Scanner(File(fileName, "r")); int a; int[2] b; char[2] c; string d; double e; double[] f; sc.read(a, b, c, d, e, f); assert(a == 1); assert(equal(b[], [2, 3])); assert(equal(c[], "ab")); assert(equal(d, "cde")); assert(e == 1.0); assert(equal(f, [1.0, 2.0])); } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File, writeln; import std.datetime; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); foreach (i; 0..1_000_000) { fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2); } fout.close; writeln("Scanner Speed Test(3*1,000,000 int)"); StopWatch sw; sw.start; Scanner sc = new Scanner(File(fileName, "r")); foreach (i; 0..500_000) { int a, b, c; sc.read(a, b, c); assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 500_000..700_000) { int[3] d; sc.read(d); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 700_000..1_000_000) { int[] d; sc.read(d); assert(d.length == 3); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } writeln(sw.peek.msecs, "ms"); }
D
import std.stdio; void main(){ foreach(i;1..10) foreach(j;1..10) writeln(i,"x",j,"=",i * j); }
D
import std.stdio; int main() { for(int i=1; i<=9; i++) for(int j=1; j<=9; j++) printf("%dx%d=%d\n", i, j, i*j); return 0; }
D
import std.stdio,std.string,std.conv; int main() { string s; while((s = readln.chomp).length != 0) { bool flag = true; for(int i=0;i<7;i += 3) { if(s[i] != 's' && s[i] == s[i+1] && s[i+1] == s[i+2]) { s[i].writeln; flag = false; break; } } if(flag) { foreach(i;0..3) { if(s[i] != 's' && s[i] == s[i+3] && s[i+3] == s[i+6]) { s[i].writeln; flag = false; break; } } } if(flag) { if(s[0] != 's' && s[0] == s[4] && s[4] == s[8]) { s[0].writeln; flag = false; } } if(flag) { if(s[2] != 's' && s[2] == s[4] && s[4] == s[6]) { s[2].writeln; flag = false; } } if(flag) { writeln("d"); } } return 0; }
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() { while (1) { auto n = readln.chomp.to!int; if (!n) break; int[] list; int cnt; foreach (i; 0..n) { auto x = readln.chomp.to!int; foreach (e; list) { if (x < e) cnt++; } list ~= x; } cnt.writeln; } }
D
import std.stdio, std.math, std.algorithm, std.array, std.string, std.conv, std.container, std.range; pragma(inline, true) T[] Reads(T)() { return readln.split.to!(T[]); } alias reads = Reads!int; pragma(inline, true) void scan(Args...)(ref Args args) { string[] ss = readln.split; foreach (i, ref arg ; args) arg = ss[i].parse!int; } void main() { string s = readln.chomp; string t = readln.chomp; int ans; foreach(i; 0..3) ans += s[i] == t[i]; 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; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void main() { auto N = RD; auto s = new string[](N); auto t = new long[](N); foreach (i; 0..N) { s[i] = RD!string; t[i] = RD; } auto X = RD!string; long ans; bool f; foreach (i; 0..N) { if (f) { ans += t[i]; } else { if (s[i] == X) { f = true; } } } writeln(ans); stdout.flush; debug readln; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "a", long, "b"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long r, g, b, n; scan(r, g, b, n); long cnt; foreach (i; iota(n / r + 1)) { foreach (j; iota(n / g + 1)) { auto k = n - (i * r) - (j * g); if (k >= 0 && k % b == 0) { // writefln("i is %s, j is %s, k is %s", i, j, k); cnt++; } } } cnt.writeln(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std; alias 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 n = lread(); auto p = aryread(); // writeln(p); auto min_x = new long[](n); //i番目にiまでの最小値 min_x[0] = p[0]; foreach (i; 1 .. n) { min_x[i] = min(min_x[i - 1], p[i]); } // writeln(min_x); long cnt; foreach (i; 0 .. n) { if (p[i] <= min_x[i]) { 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 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 a = RD!string; auto b = RD!string; auto aa = new int[](26); auto bb = new int[](26); foreach (i; 0..n) { ++aa[a[i]-'a']; ++bb[b[i]-'a']; } auto c = new int[](26); foreach (i; 0..26) { c[i] = aa[i] - bb[i]; } bool ok = true; foreach (i; 0..25) { if (c[i] < 0) { ok = false; break; } else if (c[i] % k) { ok = false; break; } else { c[i+1] += c[i]; } } if (c[$-1] != 0) ok = false; ans[ti] = ok; } foreach (e; ans) { writeln(e ? "Yes" : "No"); } stdout.flush; debug readln; }
D
import std.stdio; import std.uni; import std.conv; import std.container; import std.functional; import std.algorithm; import std.array; import std.typecons; import std.range; import std.numeric; void main(string[] args) { inputFile = stdin; debug inputFile = File(args[1]); auto t = next!int; foreach(tc; 0 .. t) { auto n = next!int; auto a = next!long(n); long[35] cnt; cnt[] = 0; foreach(ai; a) { int bits = 0; while(ai != 0) { bits++; ai >>= 1; } cnt[bits]++; } long res = 0; foreach(c; cnt) { res += (c * (c - 1)) / 2; } res.writeln; } } ulong bitCnt(T)(T x) { ulong cnt = 0; while(x) {cnt += x & 1; x>>=1;} return cnt; } alias Arr(T) = Mat!(T, 1); struct Mat(T, size_t dimension) { T[] _baseArray; T[] _slice; size_t[dimension] _sizes; size_t dim(size_t i) {return _sizes[i];} size_t[dimension] _indexer; this(T[] baseArray, size_t[dimension] sizes) { _baseArray = baseArray; _sizes = sizes; _indexer[dimension - 1] = 1; static foreach_reverse(i; 0 .. dimension - 1) _indexer[i] = _indexer[i + 1] * _sizes[i + 1]; auto requiredSize = _indexer[0] * _sizes[0]; debug assert(_baseArray.length >= requiredSize); _slice = _baseArray[0 .. requiredSize]; } this(size_t[dimension] sizes) { size_t reqSize = 1; static foreach(i; 0 .. dimension) reqSize *= sizes[i]; this(new T[](reqSize), sizes); } ref auto opIndex() { pragma(inline, true); return _slice[]; } ref auto opIndex(I...)(I i) { pragma(inline, true); debug assert(i.length <= dimension); size_t index = mixin(iota(0, i.length).map!(j => text("i[", j, "]*_indexer[", j, "]")).join("+")); static if (i.length == dimension) { return _slice[index]; } else { enum sliceDimension = dimension - i.length; size_t[sliceDimension] remSizes = _sizes[i.length .. $]; auto basePortion = _slice[index .. index + _indexer[i.length - 1]]; return Mat!(T, sliceDimension)(basePortion, remSizes); } } static if (dimension == 2) { auto identity() { debug assert(dim(0) == dim(1)); auto n = dim(0); auto id = Mat!(T, 2)([n, n]); foreach(k; 0 .. n) id[k, k] = T(1); return id; } bool canMultiply(Mat!(T, 2) b) { return this.dim(1) == b.dim(0); } auto opBinary(string op)(Mat!(T, 2) b) if (op == "*") { debug assert(canMultiply(b)); auto res = Mat!(T, 2)([this.dim(0), b.dim(1)]); foreach(i; 0 .. res.dim(0)) foreach(k; 0 .. this.dim(1)) foreach(j; 0 .. res.dim(1)) res[i, j] = res[i, j] + this[i, k] * b[k, j]; return res; } auto opBinary(string op)(ulong exp) if (op == "^^") { return this.pow(identity, exp); } mixin interiorBinPow; } } struct Mod(T, ulong mod) { alias This = Mod!(T, mod); T _rep; T rep() {return _rep;} this(W)(W w) { _rep = ((cast(T) w)%mod + mod) % mod; } static auto plainConstruct(T t) { pragma(inline, true); debug assert(t >= 0 && t < mod); auto res = Mod!(T, mod).init; res._rep = t; return res; } string toString() { return to!string(rep); } debug invariant { assert(_rep >= 0 && _rep < mod); } auto opBinary(string op)(This b) { static if (op == "+") { T resrep = _rep + b._rep; if (resrep >= mod) resrep -= mod; return This.plainConstruct(resrep); } else static if (op == "-") { T resrep = _rep - b._rep; if (resrep < 0) resrep += mod; return This.plainConstruct(resrep); } else static if (op == "*") { return This(_rep * b._rep); } } } mixin template interiorBinPow() { alias ThisType = typeof(this); auto pow(ThisType res, ulong exp) { if (exp < 0) debug assert(0); auto pow = this; while(exp) { if (exp & 1) res = res * pow; pow = pow * pow; exp >>= 1; } return res; } } // INPUT File inputFile; DList!string _words; void _wordsFill() { while (_words.empty) _words.insertBack(inputFile.readln.split); } string popWord() { _wordsFill; auto word = _words.front; _words.removeFront; return word; } string peekWord() { _wordsFill; return _words.front; } T next(T)() { return to!T(popWord); } T peek(T)() { return to!T(peekWord); } auto next(T, S...)(S s) { static if (S.length == 0) { return to!T(popWord); } else { auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]); foreach(ref elem; res) elem = next!T(s[1 .. $]); return res; } }
D
//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.numeric; 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; } } void main() { auto r = new InputReader; foreach (t; 0 .. r.next!uint) { auto a = r.next!uint; auto b = r.next!uint; writeln (gcd (a, b) == 1 ? "Finite" : "Infinite"); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto s = new string[](3); foreach (i; 0..3) s[i] = RD!string; long[string] set; foreach (e; s) ++set[e]; long cnt; foreach (key; set.keys) { if (set[key] > cnt) cnt = set[key]; } long ans1 = 3 - cnt; long[][char] set2; foreach (e; s) { set2[e[1]] ~= e[0]; } long cnt2 = 1; foreach (key; set2.keys) { if (set2[key].length == 1) continue; auto value = set2[key]; value.sort(); if (value[0]+1 == value[1]) { ++cnt2; } else if (value[0]+2 == value[1]) { cnt2 = max(cnt2, 2); break; } if (value.length == 3) { if (value[1]+1 == value[2]) { ++cnt2; } else if (value[1]+2 == value[2]) cnt2 = max(cnt2, 2); } } long ans2 = 3 - cnt2; writeln(min(ans1, ans2)); 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(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 int[](t); foreach (ti; 0..t) { auto n = RD!int; auto m = RD!int; auto pos = new int[][](m); auto yoko = new int[](n); auto tate = new int[](n); yoko[] = -1; tate[] = -1; foreach (i; 0..m) { auto y = RD!int-1; auto x = RD!int-1; pos[i] = [y, x]; yoko[y] = i; tate[x] = i; } bool done; while (!done) { debug { writeln(pos); writeln(yoko); writeln(tate); } void impl(int i) { if (yoko[i] == tate[i] || (yoko[i] != -1 && tate[i] != -1)) return; int id; if (yoko[i] != -1) id = yoko[i]; else id = tate[i]; if (pos[id][0] != i) { yoko[pos[id][0]] = -1; yoko[i] = id; auto last = pos[id][0]; pos[id][0] = i; ++ans[ti]; impl(last); } else if (pos[id][1] != i) { tate[pos[id][1]] = -1; tate[i] = id; auto last = pos[id][1]; pos[id][1] = i; ++ans[ti]; impl(last); } debug { writeln(pos); writeln(yoko); writeln(tate); } } foreach (i; 0..n) { impl(i); } done = true; foreach (i; 0..n) { if (yoko[i] == -1 || tate[i] == -1) continue; else if (yoko[i] == tate[i]) continue; done = false; foreach (j; 0..n) { if (!(yoko[j] != -1 && tate[j] != -1)) continue; auto id = yoko[i]; yoko[i] = -1; yoko[j] = id; tate[pos[id][1]] = -1; tate[j] = id; pos[id] = [j, j]; ans[ti] += 2; break; } break; } } } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; auto s=readln.chomp.to!(char[]); auto pat=['a', 'i', 'u', 'e', 'o']; int tot=0; foreach(char c; s){ if('0'<=c && c<='9'){ int x=c-'0'; if(x&1) tot++; }else{ if(count(pat, c)>0) tot++; } } writeln(tot); } /* vowel => even odd => NOT vowel */ 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)); } } void wr(T...)(T x){ import std.stdio; foreach(e; x) write(e, " "); writeln(); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string; void main() { auto s = readln.split.map!(to!long).array; auto B = s[0]; auto K = s[1]; auto A = readln.split.map!(to!long).array; long X = 0; B %= 2; foreach (i, a; A) { if (i != K - 1) { X += B * a % 2; X %= 2; } else { X += a % 2; X %= 2; } } writeln(X ? "odd" : "even"); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; immutable long MOD = 998244353; void main() { auto S = readln.chomp; auto N = S.length.to!int; S ~= S; char prev = S[0]; int tmp = 0; int ans = 0; foreach (i; 0..2*N) { if (S[i] == prev) { tmp = 1; } else { tmp += 1; } ans = max(ans, tmp); prev = S[i]; } ans = min(ans, N); ans.writeln; }
D
import std.stdio,std.conv,std.algorithm,std.array; int[] raia() { return readln().split().map!(to!int).array; } //read stdin as int[] void main(){ for(auto i =0; i< 10000;i++) {auto it = raia(); if(it[0] == 0 && it[1] == 0){ return;} else if( it[0] > it[1]){ writeln(it[1]," ",it[0]); }else{ writeln(it[0]," ",it[1]); } } }
D
import std.algorithm; import std.array; import std.conv; import std.stdio; import std.string; void main() { long n; long sum; n = readln.chomp.to!(long); for (long i = 1; i <= n; i++) { if (i % 15 == 0) { continue; } if (i % 3 == 0) { continue; } if (i % 5 == 0) { continue; } sum += i; } writeln(sum); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons; import std.math, std.numeric; void main() { int n, m, k; scan(n, m, k); foreach (i ; 0 .. n + 1) { foreach (j ; 0 .. m + 1) { if ((m - j)*i + (n - i)*j == k) { writeln("Yes"); return; } } } writeln("No"); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm; void main() { auto n = readln.split.join.to!int; foreach (i ; 0 .. n) { if (i*i > n) break; if (i*i == n) { writeln("Yes"); return; } } writeln("No"); } 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; void main(){ int n = readln.chomp.to!int; long[] as = readln.chomp.split.to!(long[]); int[long] ac; long ans; foreach(a; as){ if(a !in ac) ac[a] = 0; ac[a] += 1; ans += a; } int q = readln.chomp.to!int; foreach(_; 0 .. q){ long[] bc = readln.chomp.split.to!(long[]); long b = bc[0], c = bc[1]; if(b !in ac) ac[b] = 0; if(c !in ac) ac[c] = 0; ac[c] += ac[b]; ans += (c - b) * ac[b]; ac[b] = 0; ans.writeln; } }
D
import std.stdio, std.string, std.conv, std.range; void main() { long mod = 1_000_000_007; int[] tmp = readln.split.to!(int[]); int n = tmp[0], m = tmp[1]; bool[] ok = true.repeat(n+2).array; foreach (i; 0 .. m) { int a = readln.chomp.to!int; ok[a+1] = false; } long[] dp = new long[n+2]; dp[1] = 1; foreach (i; 2 .. n+2) { if (ok[i]) dp[i] = (dp[i-1] + dp[i-2]) % mod; } dp[n+1].writeln; }
D
import std.stdio, std.string, std.array, std.conv; void main() { int[] a; int q = readln.chomp.to!int; foreach (i; 0 .. q) { int[] tmp = readln.chomp.split.to!(int[]); if (tmp[0] == 0) { a ~= tmp[1]; } else if (tmp[0] == 1) { a[tmp[1]].writeln; } else { a = a[0..$-1]; } } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nx = readln.split.to!(long[]); auto N = nx[0]; auto x = nx[1]; auto as = readln.split.to!(long[]); long[] bs; foreach (a; as) bs ~= a; long min_s = long.max; foreach (k; 0..N) { long s; long[] cs; if (k == 0) { cs = bs; } else { cs.length = N; foreach (i; 0..N) { cs[(i+1)%N] = min(bs[(i+1)%N], bs[i]); } } bs = cs; foreach (i; 0..N) { s += bs[i]; } min_s = min(min_s, s + k*x); } writeln(min_s); }
D
void main() { int a = readln.chomp.to!int; int b = readln.chomp.to!int; int c = readln.chomp.to!int; int x = readln.chomp.to!int; int cnt; foreach (i; 0 .. a+1) { foreach (j; 0 .. b+1) { int tmp = (x - 500 * i - 100 * j) / 50; if (0 <= tmp && tmp <= c) ++cnt; } } cnt.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { writeln(readln.chomp.to!int <= 999 ? "ABC" : "ABD"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto hw = readln.split.to!(long[]); auto H = hw[0]; auto W = hw[1]; if (H == 1 || W == 1) { writeln(1); } else if((H*W)%2 == 0) { writeln(H*W/2); } else { writeln((H*(W-1)/2 + (H+1)/2)); } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; void main() { while (1) { auto field = new char[14][14]; field[0][] = '0'; field[$-1][] = '0'; foreach (i; 1..13) { field[i][] = '0'; auto str = readln.chomp; foreach (j; 1..13) { field[i][j] = str[j-1]; } } int cnt; foreach (i; 1..13) { foreach (j; 1..13) { if (field[i][j] == '0') continue; void solve(int x, int y) { field[x][y] = '0'; for (int i = -1; i <= 1; i+=2) { if (field[x][y+i] == '1') solve(x, y+i); if (field[x+i][y] == '1') solve(x+i, y); } } cnt++; solve(i,j); } } cnt.writeln; if (!readln) break; } }
D
import std.stdio; import std.algorithm; import std.conv; import std.string; void main() { auto n = readln.chomp.to!(long); auto t = readln.split.to!(long[]); auto m = readln.chomp.to!(long); long[] p; long[] x; long s = sum(t); foreach (i; 0..m){ auto px = readln.split.to!(ulong[]); (s - t[px[0] - 1] + px[1]).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 readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}} void main() { string[] s; readC(3, s); auto t = 0, i = new int[](3); for (;;) { if (i[t] >= s[t].length) { writeln(cast(char)('A'+t)); return; } t = cast(int)(s[t][i[t]++]-'a'); } }
D
import std.stdio; import std.algorithm; import std.math; import std.conv; import std.string; T readNum(T)(){ return readStr.to!T; } T[] readNums(T)(){ return readStr.split.to!(T[]); } string readStr(){ return readln.chomp; } void main(){ auto ap = readNums!int; writeln((ap[0]*3+ap[1])/2); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); ulong bignum = 1_000_000_007; alias SugarWater = Tuple!(long, "swater", long, "sugar"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { auto n = lread(); long mini = n; foreach (i; iota(0, n + 1)) { auto rest = n - i; auto nine = calmoves(i, 9); auto six = calmoves(rest, 6); mini = min(nine + six, mini); } mini.writeln(); } auto calmoves(long n, long div) { auto powers = [div]; while (powers[$ - 1] < n) powers ~= powers[$ - 1] * div; // powers.writeln(); long moves, current = powers.length - 1; while (current >= 0) { auto t = n / powers[current]; // writefln("%stimes : %s", n, t); moves += t; n -= powers[current] * t; current--; } return moves + n; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; int[101] T; void main() { auto N = readln.chomp.to!int; foreach (a; readln.split.to!(int[])) ++T[a]; size_t i; for (;; ++i) { if (T[i] == 0) continue; break; } auto h = i.to!int; if (T[i] == 2) { --h; } else if (T[i] > 2) { writeln("Impossible"); return; } int s = T[i]; foreach (_; 0..h) { ++i; if (T[i] < 2) { writeln("Impossible"); return; } s += T[i]; } writeln(s == N ? "Possible" : "Impossible"); }
D
module app; import core.bitop; import std.algorithm; import std.array; import std.bigint; import std.container.rbtree; import std.conv; import std.stdio; import std.string; import std.traits; struct Input { string n; } void parseInput(T)(out Input input, T adapter) { with (input) { n = adapter.readln(); } } struct Output { } auto main2(Input* input) { int sum = 0; foreach (c; input.n) sum += c - '0'; return (sum % 9) == 0 ? "Yes" : "No"; } alias retType = ReturnType!main2; static if (!is(retType == void)) { unittest { writeln("begin unittest"); } auto _placeholder_ = ReturnType!main2.init; unittest // example1 { string example = `123456789`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == "Yes"); } unittest // example2 { string example = `0`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == "Yes"); } unittest // example3 { string example = `31415926535897932384626433832795028841971693993751058209749445923078164062862089986280`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == "No"); } unittest { writeln("end unittest"); } void parseExample(out Input input, string example) { parseInput(input, new StringAdapter(example)); } } class StdinAdapter { // readf!"%s"は使えない // https://issues.dlang.org/show_bug.cgi?id=19820 uint readf(alias format, A...)(auto ref A args) { return std.stdio.readf!format(args); } string readln() { return std.stdio.readln().strip(); } } class StringAdapter { import std.format; string _s; this(string input) { _s = input; } uint readf(alias format, A...)(auto ref A args) { return _s.formattedRead!format(args); } string readln() { auto i = _s.countUntil("\n"); if (i == -1) i = _s.length; string ret = _s[0..i]; _s = _s[i..$]; return ret; } } void printResult(T)(T result) { static if (isFloatingPoint!T) writefln("%f", result); else writeln(result); } void main() { Input input = void; parseInput(input, new StdinAdapter()); static if (is(retType == void)) main2(&input); else { auto result = main2(&input); printResult(result); } }
D
import std.conv, std.stdio; import std.algorithm, std.array, std.range, std.string; enum p = 26; void main() { (readln.chomp.to!ulong - 1).solve.writeln; } private: auto solve(ulong n) { long a = p; auto i = 1; while (a <= n) { n -= a; a *= p; i += 1; } return n.to26ary(i); } unittest { foreach (i; 0..1000) i.solve.write(" "); foreach (i; 999_9999_9999_9000..1000_0000_0000_0002) i.solve.write(" "); } auto to26ary(ulong n, int i) { string ret; foreach (_; 0..i) { ret = cast(char)('a' + (n % p)) ~ ret; n /= p; } return ret; }
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() { const int MOD = 998244353; int k, n; scanf("%d%d", &k, &n); long[][] r = new long[][](2,k+1); long[] dp = new long[n+1]; dp[0] = 1; foreach (i; 0..k) { foreach (j; 0..n) { (dp[j+1] += dp[j]) %= MOD; } } foreach (i; 0..k+1) { r[0][i] = dp[n]; foreach_reverse (j; 0..n) { (dp[j+1] += MOD - dp[j]) %= MOD; } r[1][i] = (dp[n] + dp[n-1]) % MOD; foreach_reverse (j; 0..n) { (dp[j+1] += MOD - dp[j]) %= MOD; } long[] a = dp.dup; foreach (j; 0..n) { (a[j+1] += a[j]) %= MOD; } foreach (j; 0..n) { (dp[j+1] += a[j]*2) %= MOD; } } foreach (i; 2..2*k+1) { int a = 1-i%2; int b = -a; foreach (j; 1..k+1) { int nj = i-j; if (1 <= nj && nj <= k) b++; } b /= 2; //writeln(i," ",a," ",b); writeln(r[a][b]); } }
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; string[] ss; for (int i = 0; i < n; i++) { ss ~= read!string; } char[] ans; foreach (c; 'a'..'z'+1) { int cnt = int.max; foreach (s; ss) { cnt = min(cnt, s.count(c)); if (cnt == 0) break; } if (cnt > 0) { ans ~= repeat(cast(char) c, cnt).array; } } writeln(ans); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.numeric; void main() { auto s = readln.chomp; auto rn = s.count('0'); auto bn = s.count('1'); writeln(min(rn, bn) * 2); }
D
void main() { long n = readln.chomp.to!long; long mod = 10 ^^ 9 + 7; long[] factors = new long[n+1]; foreach (i; 1 .. n+1) { long d = i; long tmp; while (d % 2 == 0) { ++tmp; d /= 2; } factors[2] += tmp; long ind = 3; while (ind * ind <= d) { tmp = 0; while (d % ind == 0) { ++tmp; d /= ind; } factors[ind] += tmp; ind += 2; } if (d > 1) { ++factors[d]; } } long result = 1; foreach (i; 0 .. n+1) { if (factors[i] > 0) { result = result * (factors[i] + 1) % mod; } } result.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.conv, std.string, std.math, std.regex, std.range, std.ascii, std.algorithm; void main(){ auto N = readln.chomp.to!int; auto V = readln.split.to!(int[]); auto C = readln.split.to!(int[]); int X; int Y; foreach(i; 0..N){ if(V[i] > C[i]){ X += V[i]; Y += C[i]; } } if(X-Y<=0){ writeln(0); } else{ writeln(X-Y); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto D = readln.chomp.to!int; switch (D) { case 22: writeln("Christmas Eve Eve Eve"); break; case 23: writeln("Christmas Eve Eve"); break; case 24: writeln("Christmas Eve"); break; case 25: writeln("Christmas"); break; default: } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp.to!int; auto d = readln.chomp.split.to!(int[]); int sum; foreach (i; 0..n-1) { foreach (j; i+1..n) { sum += d[i] * d[j]; } } sum.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)); } double euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); } double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; } double norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); } double dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[][][](t); foreach (ti; 0..t) { auto n = RD!int; auto k = RD!int; if (n == 4 && k == 3) continue; foreach (i; 0..n/2) { ans[ti] ~= [i, n-i-1]; } auto x = k & (n/2-1); swap(ans[ti][x][1], ans[ti][0][1]); if (k & (n/2)) { if (x != n/2-1) swap(ans[ti][n/2-1][1], ans[ti][0][0]); else swap(ans[ti][n/2-2][1], ans[ti][0][0]); } debug { long tot; foreach (ee; ans[ti]) tot += ee[0] & ee[1]; if (tot != k) writeln("wa:n=", n, " k=", k); } } debug writeln("done"); debug readln; foreach (e; ans) { if (e.empty) writeln(-1); foreach (ee; e) writeln(ee[0], " ", ee[1]); } stdout.flush; debug readln; }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; import std.string; import std.uni; import std.range; import std.algorithm; void main() { while (!stdin.eof) { auto a = readln.strip; if (a == "-") break; auto m = readln.strip.to!int; foreach (x; iota(m).map!(x => readln.strip.to!int)) { a = a[x..$] ~ a[0..x]; } writeln(a); } }
D
//dlang template---{{{ import std.stdio; import std.conv; import std.string; import std.array; import std.algorithm; // MIT-License https://github.com/kurokoji/nephele class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } //Digit count---{{{ int DigitNum(int num) { int digit = 0; while (num != 0) { num /= 10; digit++; } return digit; } //}}} //}}} void main() { Scanner sc = new Scanner; int N; sc.scan(N); int[] A = new int[N]; foreach (i; 0 .. N) sc.scan(A[i]); int res = 0; Break1: while (true) { foreach (i; 0 .. N) { if (A[i] % 2 != 0) break Break1; else A[i] /= 2; } res++; } 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.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto s = RD!string; ans[ti] = s.length; { long cnt; foreach (i; 0..s.length) { if (s[i] == '?') { if (i % 2 == 0) ++cnt; } else if (s[i] == '1') { if (i % 2) --cnt; else ++cnt; } { long rem1 = (s.length - i) / 2 - (i % 2 == 0 ? 1 : 0); long rem2 = (s.length - i) / 2; if (cnt > rem2 || cnt + rem1 < 0) { ans[ti].chmin(i+1); break; } } } } { long cnt; foreach (i; 0..s.length) { if (s[i] == '?') { if (i % 2) ++cnt; } else if (s[i] == '1') { if (i % 2) ++cnt; else --cnt; } { long rem1 = (s.length - i) / 2 - (i % 2 == 0 ? 1 : 0); long rem2 = (s.length - i) / 2; if (cnt > rem1 || cnt + rem2 < 0) { ans[ti].chmin(i+1); break; } } } } } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { string vo = "aiueo"; char a; scan(a); writeln(vo.canFind(a) ? "vowel" : "consonant"); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto Q = s[1]; if (N == 1) { while (Q--) { s = readln.split.map!(to!int); auto v = s[0]; auto w = s[1]; writeln(min(v, w)); } return; } while (Q--) { s = readln.split.map!(to!int); auto v = s[0]-1; auto w = s[1]-1; while (v != w) { if (v > w) swap(v, w); int q = w % N; if (q == 0) q = N; w = (w-q) / N; } writeln(v+1); } }
D
void main() { long n = rdElem; writeln((n + 1) / 2); } 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, std.conv, std.string; import std.algorithm, std.array, std.container; import std.numeric, std.math; import core.bitop; T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; } string RDR()() { return readln.chomp; } long mod = pow(10, 9) + 7; long moda(long x, long y) { return (x + y) % mod; } long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; } long modm(long x, long y) { return (x * y) % mod; } void main() { auto N = RD!long; auto M = RD!long; long ans = (N - M) * 100 + M * 1900; ans *= pow(2, M); writeln(ans); stdout.flush(); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto di1 = readln.split.map!(to!int).array; auto di2 = readln.split.map!(to!int).array; auto dice1 = new Dice(di1); auto dice2 = new Dice(di2); auto rollings = ["", "T", "TT", "TTT", "N", "NT", "NTT", "NTTT", "W", "WT", "WTT", "WTTT", "E", "ET", "ETT", "ETTT", "S", "ST", "STT", "STTT", "NN", "NNT", "NNTT", "NNTTT"]; foreach (r; rollings) { auto d1 = dice1; auto d2 = dice2.dup; d2.roll(r); if (d1.di == d2.di) { writeln("Yes"); return; } } writeln("No"); } class Dice { int[] di; this(int[] di) { this.di = di; } int top() { return di[0]; } auto dup() { return new Dice(di.dup); } void roll(string si) { foreach (c; si) roll(c); } void roll(char d) { auto rolled = ['N': [1, 5, 2, 3, 0, 4], 'E': [3, 1, 0, 5, 4, 2], 'S': [4, 0, 2, 3, 5, 1], 'W': [2, 1, 5, 0, 4, 3], 'T': [0, 2, 4, 1, 3, 5]]; di = di.indexed(rolled[d]).array; } }
D
import std; void main() { int s, w; scan(s, w); writeln(s <= w ? "unsafe" : "safe"); } void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int;
D
import std.stdio, std.conv, std.string, std.math, std.regex, std.range, std.ascii, std.algorithm; void main(){ auto ip = readln.split.to!(int[]), A = ip[0], B = ip[1]; int X; if(A > B){ X += A; A--; } else { X += B; B--; } if(A > B){ X += A; } else { X += B; } writeln(X); }
D
void main(){ import std.stdio, std.algorithm, std.string, std.conv; struct P{ int idx1, idx2; } int K; rd(K); P[int] set; for(int k=1; k<=K; k++){ int n; rd(n); auto as=readln.split.to!(int[]); int s=reduce!"a+b"(as); P[] cand; foreach(int i, a; as){ int t=s-a; if(t in set){ if(set[t].idx1<k){ writeln("YES"); writeln(set[t].idx1, " ", set[t].idx2); writeln(k, " ", i+1); return; } }else{ set[t]=P(k, i+1); } } } writeln("NO"); } 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
void main() { int[] tmp = readln.split.to!(int[]); int n = tmp[0], y = tmp[1]; int yukichi = -1, ichiyou = -1, hideyo = -1; foreach (i; 0 .. n+1) { foreach (j; 0 .. n-i+1) { int k = n - i - j; if (10000 * i + 5000 * j + 1000 * k == y) { yukichi = i; ichiyou = j; hideyo = k; break; } } } writeln(yukichi, " ", ichiyou, " ", hideyo); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(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 long[](t); foreach (ti; 0..t) { auto n = RD!int; auto k = RD!int; auto z = RD!int; auto a = RDA!int; foreach (i; 0..min((k-1)/2, z)+1) { long cnt; foreach (j; 0..k+1-i*2) { cnt += a[j]; } foreach (j; 1..k+1-i*2) { auto best = a[j-1] + a[j]; ans[ti].chmax(cnt + best * i); if (j != k-i*2 && i < z) ans[ti].chmax(cnt + best * i - a[k-i*2] + a[k-i*2-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!int; auto a = RDA; long best = long.max; long cnt; foreach (i; 0..n/2) { cnt += a[i*2]; } best.chmin(cnt); foreach (i; 1..n/2+1) { auto l = ((i-1)*2) % n; auto r = (l+(n/2*2)) % n; cnt += a[r] - a[l]; best.chmin(cnt); debug writeln("l:", l, " r:", r, " cnt:", cnt); } cnt = 0; foreach (i; 0..n/2) { cnt += a[i*2+1]; } best.chmin(cnt); foreach (i; 1..n/2) { auto l = ((i-1)*2+1) % n; auto r = (l+(n/2*2)) % n; cnt += a[r] - a[l]; best.chmin(cnt); debug writeln("l:", l, " r:", r, " cnt:", cnt); } long ans; foreach (e; a) ans += e; ans -= best; writeln(ans); stdout.flush; debug readln; }
D
void main(){ int a, b; scanf("%d %d", &a, &b); writeln(a+b>=10?"error":(a+b).to!string); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range; const long mod = 10^^9+7; // 1要素のみの入力 T inelm(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] inln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split())ln ~= elm.to!T(); return ln; }
D
import std.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; bool calc(int n) { foreach (i; 1..10) { foreach (j; 1..10) { if (i * j == n) return true; } } return false; } void main() { int n = readint; writeln(calc(n) ? "Yes" : "No"); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto A = RD!string; auto B = RD!string; auto C = RD!string; writeln(A[$-1] == B[0] && B[$-1] == C[0] ? "YES" : "NO"); stdout.flush(); debug readln(); }
D