code
stringlengths
4
1.01M
language
stringclasses
2 values
/+ dub.sdl: name "D" dependency "dunkelheit" version=">=0.9.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; long solve(bool[] v) { long ans = 0; long sm = 0; foreach (f; v) { if (f) { sm++; continue; } else { if (sm) { sm--; ans++; } } } return ans; } int main() { Scanner sc = new Scanner(stdin); scope(exit) assert(!sc.hasNext); int n; long l; sc.read(n, l); long[] x, t; sc.read(x, t); bool[] lty, rty; bool lst = false; long offset = 0; long cnt = 1 + n; long base = 0; foreach (i; 0..n) { long k = (t[i] - 1) / (2*l); t[i] -= k*(2*l); offset += k*(2*l); bool lf = t[i] <= 2*x[i]; bool rf = t[i] <= 2*(l-x[i]); if (!lf && !rf) { cnt += 1; lst |= (i == n-1); continue; } bool f = (lf && rf); if (!f) base += 1; if (x[i] <= l/2) { lty ~= f; } else { rty ~= f; } } rty = rty.retro.array; while (lty.length && lty.back == true) { lty = lty[0..$-1]; rty ~= true; } /* if (lty.length && lty.back == false) { lty = lty[0..$-1]; base--; }*/ long ls = solve(lty), rs = solve(rty); base -= ls + rs; if (rs != rty.count(false)) lst = true; cnt += base; if (lst) cnt++; // writeln(lty, " ", rty, " ", cnt, " ", base, " ", lst); cnt = (cnt + 1) / 2 * 2; writeln(cnt * l + offset); return 0; } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */ // module dkh.scanner; // import dkh.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(Args...)(auto ref Args args) { import std.exception; static if (args.length != 0) { enforce(readSingle(args[0])); read(args[1..$]); } } bool hasNext() { return succ(); } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; int[26] CS; void main() { auto S = readln.chomp.to!(char[]); foreach (c; S) ++CS[c-97]; foreach (i, n; CS) { if (!n) { writeln(S ~ cast(char)(i+97)); return; } } foreach_reverse (i, c; S) { while (c < 'z') { ++c; bool found; foreach (d; S[0..i]) { if (d == cast(char)c) { found = true; break; } } if (!found) { writeln(S[0..i] ~ cast(char)c); return; } } } writeln("-1"); }
D
import std.stdio, std.conv, std.string; import std.algorithm, std.array, std.container, std.typecons; import std.numeric, std.math; import core.bitop; T RD(T = 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; } T[] ARR(T)(in string str) { return str.split.to!(T[]); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool INSIDE(T)(T x, T b, T e) { return x >= b && x < e; } long mod = pow(10, 9) + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } struct UnionFind { void init(long n) { par = new long[](n); foreach (i; 0..n) par[i] = i; } long root(long i) { return par[i] == i ? i : (par[i] = root(par[i])); } bool same(long i, long j) { return root(i) == root(j); } void unite(long i, long j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; } long[] par; } void main() { auto H = RD!long; auto W = RD!long; auto c = new long[][](10); foreach (i; 0..10) { c[i] ~= RDR.split.to!(long[]); } auto A = new long[][](H); foreach (i; 0..H) { A[i] ~= RDR.split.to!(long[]); } auto bestCost = new long[](10); foreach (i; 0..10) { auto cost = new long[](10); fill(cost, long.max); cost[i] = 0; long[] open = [i]; while (!open.empty) { auto node = open.front; open.popFront; cost[1] = min(cost[1], cost[node] + c[node][1]); foreach (j; 0..10) { if (i == j || j == 1) continue; if (cost[node] + c[node][j] < cost[1] && cost[node] + c[node][j] < cost[j]) { cost[j] = cost[node] + c[node][j]; open ~= j; } } } bestCost[i] = cost[1]; } long ans; foreach (i; 0..H) { foreach (j; 0..W) { if (abs(A[i][j]) == 1) continue; ans += bestCost[A[i][j]]; } } writeln(ans); stdout.flush(); }
D
void main(){ int n = _scan(); (n/2 +n%2).writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { long N, M; scan(N, M); long t = 1900 * M + 100 * (N - M); if (M == 0) { writeln(t); return; } long p = 1 << M; writeln(t * p); // long ans; // long i = 1; // while (true) // { // long tmp = (i * t + ((p ^^ i) - 1)) / (p ^^ i); // writeln(tmp); // if (tmp == 0) // { // writeln(ans); // return; // } // ans += tmp; // i++; // } }
D
import std.stdio,std.math,std.string,std.conv,std.typecons,std.format; import std.algorithm,std.range,std.array; T[] readarr(T=long)(){return readln.chomp.split.to!(T[]);} void scan(T...)(ref T args){auto input=readln.chomp.split;foreach(i,t;T)args[i]=input[i].to!t;} //END OF TEMPLATE void main(){ ulong m,n; ulong[] a; scan(n,m); a=readarr!ulong; auto sum=sum(a); (a.filter!(x=>x.to!real>=sum.to!real/(4*m).to!real).array.length>=m?"Yes":"No").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); auto m = n, s = 0; while (m > 0) { s += m%10; m /= 10; } writeln(n%s == 0 ? "Yes" : "No"); }
D
import std.stdio; import std.string; import std.array; import std.range; import std.algorithm; import std.conv; void main(string[] args) { readln(); string input = readln().chomp; int cur = 0; int min = 0; int e = 0; foreach(c; input){ if(c == 'E'){e++; cur--;} else{cur++;} if(cur < min){min = cur;} } writeln(min+e); }
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 ASSA ---+/ /+---test ASSS ---+/ void main(string[] args) { const S = readln.chomp.to!string; size_t[char] x; foreach (c; S) { if (c !in x) x[c] = 0; ++x[c]; } if (x.length == 2 && x.values[0] == x.values[1]) { "Yes".writeln; } else { "No".writeln; } }
D
import std.stdio, std.conv, std.string; import std.algorithm, std.array, std.container, std.typecons; import std.numeric, std.math; import core.bitop; T RD(T = 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; } T[] ARR(T)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool INSIDE(T)(T x, T b, T e) { return x >= b && x < e; } long mod = pow(10, 9) + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } struct UnionFind { void init(long n) { par = new long[](n); foreach (i; 0..n) par[i] = i; } long root(long i) { return par[i] == i ? i : (par[i] = root(par[i])); } bool same(long i, long j) { return root(i) == root(j); } void unite(long i, long j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; } long[] par; } long[] dijkstra(long from, long to, long[][] edges) { long[] path; auto cost = new long[](edges.length); fill(cost, long.max); cost[from] = 0; long[][] open = [[from]]; while (!open.empty) { auto n = open.front; open.popFront; auto p = n[0]; foreach (i; 0..edges.length) { if (edges[p][i] == -1) continue; auto c = cost[p] + edges[p][i]; if (c < cost[to] && c < cost[i]) { cost[i] = c; if (i == to) path = i ~ n; else open ~= i ~ n; } } } return cost[to] ~ path; } void main() { auto H = RD!long; auto W = RD!long; auto N = RD!long; auto a = RDR.ARR!long; auto ans = new long[][](H, W); long pos; foreach (i; 0..H) { foreach (j; 0..W) { auto x = i % 2 == 1 ? W - j - 1 : j; ans[i][x] = pos + 1; --a[pos]; if (a[pos] == 0) ++pos; } } foreach (i; 0..H) { write(ans[i][0]); foreach (j; 1..W) { write(" ", ans[i][j]); } writeln(); } stdout.flush(); }
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, "damage", long, "cost"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { auto s = sread(); foreach (_; iota(s.length)) { write('x'); } 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 core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; alias Goods = Tuple!(long, "w", long, "v"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { auto s = sread(); auto t = sread(); long m = s.length, n = t.length; auto lcs = new long[][](m + 1, n + 1); foreach (i; iota(m)) { foreach (j; iota(n)) { if (s[i] == t[j]) lcs[i + 1][j + 1] = lcs[i][j] + 1; else lcs[i + 1][j + 1] = max(lcs[i][j + 1], lcs[i + 1][j]); } } lcs.traceback(t, m, n).writeln(); } auto traceback(T)(T lcs, string s, long m, long n) { auto str = new char[](0); while (m > 0 && n > 0) { if(lcs[m][n] == lcs[m][n - 1]) n--; else if(lcs[m][n] == lcs[m - 1][n]) m--; else { m--, n--; str ~= s[n]; } } str.reverse(); return str; }
D
import std.stdio; import std.algorithm; import std.string; import std.conv; void main() { auto N = to!int(readln.chomp); foreach (i; 0..N) { auto input = readln.split.map!(to!int); if (input[2] >= 5 && input[3] >= 2) { writeln((input[0] * input[2] + input[1] * input[3]) * 4 / 5); } else if (input[2] >= 5) { int a = input[0] * input[2] + input[1] * input[3]; int b = (input[0] * input[2] + input[1] * 2) * 4 / 5; writeln(min(a, b)); } else if (input[3] >= 2) { int a = input[0] * input[2] + input[1] * input[3]; int b = (input[0] * 5 + input[1] * input[3]) * 4 / 5; writeln(min(a, b)); } else { int a = input[0] * input[2] + input[1] * input[3]; int b = (input[0] * 5 + input[1] * 2) * 4 / 5; writeln(min(a, b)); } } }
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(); if (S.length & 1) { writeln("No"); return; } foreach (i; 0 .. S.length) { if (S[i] != "hi"[i & 1]) { writeln("No"); return; } } writeln("Yes"); }
D
import std.stdio; import std.array; import std.string; import std.conv; import std.algorithm; import std.typecons; import std.range; void main() { auto input = readln().split.map!(to!int); int H = input[0]; int W = input[1]; string[] blocks = new string[](H); foreach (i; iota(H)) blocks[i] = readln().chomp; int ans = 0; foreach (w; iota(W-1)) { int[][] cost = new int[][](H+1, H+1); foreach (h1; iota(H, -1, -1)) { foreach (h2; iota(H-1, -1, -1)) { if (h1 == H || h2 == H) { cost[h1][h2] = 0; } else if (blocks[H-h1-1][w] == blocks[H-h2-1][w+1]){ cost[h1][h2] = cost[h1+1][h2+1] + 1; } else { cost[h1][h2] = cost[h1+1][h2+1]; } } } int[][] dp = new int[][](H+1, H+1); foreach (h1; iota(H+1)) { foreach (h2; iota(H+1)) { if (h1 == 0 && h2 == 0) { continue; } else if (h1 == 0) { dp[h1][h2] = dp[h1][h2-1] + cost[h1][h2-1]; } else if (h2 == 0) { dp[h1][h2] = dp[h1-1][h2] + cost[h1-1][h2]; } else { dp[h1][h2] = min(dp[h1][h2-1] + cost[h1][h2-1], dp[h1-1][h2] + cost[h1-1][h2]); } } } ans += dp[H][H]; } 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.container; import std.datetime; void main() { auto path = new int[][](5, 5); auto key = [' ', 'U', 'L', ' ', 'D', ' ', ' ', ' ', 'R']; auto dx = [0, 0, -1, 0, 0, 0, 0, 0, 1]; auto dy = [0, -1, 0, 0, 1, 0, 0, 0, 0]; foreach (i; 0..5) { auto x = readln.chomp; foreach (j, e; x) { if (e == '1') { path[i][j] |= 8; path[i][j+1] |= 2; } } if (i == 4) continue; auto y = readln.chomp; foreach (j, e; y) { if (e == '1') { path[i][j] |= 4; path[i+1][j] |= 1; } } } int x = 1, y = 0, dir = 8; write("R"); while (x != 0 || y != 0) { auto d = dir << 1; if (d == 16) d = 1; if (path[y][x] & d) { x += dx[d]; y += dy[d]; write(key[d]); dir = d; continue; } d = dir; if (path[y][x] & d) { x += dx[d]; y += dy[d]; write(key[d]); continue; } d = dir >> 1; if (d == 0) d = 8; if (path[y][x] & d) { x += dx[d]; y += dy[d]; write(key[d]); dir = d; continue; } d = d >> 1; if (d == 0) d = 8; x += dx[d]; y += dy[d]; write(key[d]); dir = d; } writeln(""); }
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, "l ", long, "r"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long h, w; scan(h, w); auto mat = new string[](h); foreach (i; iota(h)) { mat[i] = sread(); } auto h_skip = new bool[](h), w_skip = new bool[](w); h_skip[] = true; w_skip[] = true; foreach (i; iota(h)) { foreach (j; iota(w)) { h_skip[i] &= (mat[i][j] == '.'); w_skip[j] &= (mat[i][j] == '.'); } } foreach (i; iota(h)) { if (h_skip[i]) continue; foreach (j; iota(w)) { if (w_skip[j]) continue; mat[i][j].write(); } 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.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; void main(){ string s = readln.chomp; string ans; if(s.length % 2){ if(s[0] == s[$ - 1]) ans = "Second"; // like "aba" else ans = "First"; // like "acb" } else{ if(s[0] == s[$ - 1]) ans = "First"; // like "acba" else ans = "Second"; // like "acab" } ans.writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; foreach (i; 0..10) { foreach (j; i..10) { if (N == i*j) { writeln("Yes"); return; } } } writeln("No"); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { auto s = readln.split; writeln(s[0][$-1] == s[1][0] && s[1][$-1] == s[2][0] ? "YES" : "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; void main() { auto l = readln(); writeln(l.canFind('7') ? "Yes" : "No"); }
D
import std.stdio, std.string, std.algorithm, std.functional; void main() { readln.chomp.map!(a => a=='p' ? -1:1).sum.pipe!"a/2".writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } 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 W = RD; auto H = RD; auto N = RD; auto xya = new long[3][](N); foreach (i; 0..N) { xya[i] = [RD, RD, RD]; } long l, r = W, t, b = H; foreach (i; 0..N) { if (xya[i][2] == 1) l = max(l, xya[i][0]); else if (xya[i][2] == 2) r = min(r, xya[i][0]); else if (xya[i][2] == 3) t = max(t, xya[i][1]); else b = min(b, xya[i][1]); } writeln(max(r - l, 0) * max(b - t, 0)); stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto K = RD; long ans = 1; foreach (i; 0..N) { if (ans <= K) ans *= 2; else ans += K; } writeln(ans); stdout.flush(); debug readln(); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; // dfmt on void main() { long H, W; scan(H, W); string[] S; foreach (_; 0 .. H) S ~= sread(); auto L = new long[][](H, W); auto R = new long[][](H, W); auto U = new long[][](H, W); auto D = new long[][](H, W); foreach (y; 0 .. H) { foreach (x; 0 .. W) { if (S[y][x] == '#') { L[y][x] = 0; R[y][x] = -1; U[y][x] = 0; D[y][x] = -1; continue; } if (x == 0) L[y][x] = 1; else L[y][x] = L[y][x - 1] + 1; if (y == 0) U[y][x] = 1; else U[y][x] = U[y - 1][x] + 1; } foreach_reverse (x; 0 .. W) if (S[y][x] != '#') { if (x == W - 1) R[y][x] = 0; else R[y][x] = R[y][x + 1] + 1; } } foreach_reverse (y; 0 .. H) foreach (x; 0 .. W) if (S[y][x] != '#') { if (y == H - 1) D[y][x] = 0; else D[y][x] = D[y + 1][x] + 1; } long ans = long.min; foreach (y; 0 .. H) foreach (x; 0 .. W) if (S[y][x] != '#') { long t = L[y][x] + R[y][x] + U[y][x] + D[y][x] - 1; // writefln("(%d %d) %d %d %d %d : %d", y, x, L[y][x], R[y][x], U[y][x], D[y][x], t); ans = ans.max(t); } writeln(ans); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(int a, int b, int c, int x, int y) { int minCost = int.max; // AB ピザを 2*i 個かう for (int i = 0; i <= max(x, y); i++) { int cost = 2 * c * i + max(x - i, 0) * a + max(y - i, 0) * b; minCost = min(minCost, cost); } return minCost; } void main() { auto xs = readints; int a = xs[0], b = xs[1], c = xs[2], x = xs[3], y = xs[4]; auto ans = calc(a, b, c, x, y); writeln(ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto N = readln.chomp.to!int; auto n = N; int s; while (n) { s += n % 10; n /= 10; } writeln(N % s ? "No" : "Yes"); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.numeric; void main() { auto n = readln.chomp.to!int; auto f = new int[][](n, 10); auto p = new int[][](n, 10); foreach (i; 0..n) { f[i] = readln.chomp.split.to!(int[]); } foreach (i; 0..n) { p[i] = readln.chomp.split.to!(int[]); } auto m = int.min; foreach (i; 1..2^^10) { auto cnt = new int[](n); foreach (j; 0..10) { if (i & 1 << j) { foreach (k; 0..n) { if (f[k][j]) { cnt[k]++; } } } } int lm; foreach (j; 0..n) { lm += p[j][cnt[j]]; } m = max(lm, m); } m.writeln; }
D
import std.stdio; import std.conv; import std.string; void main() { string[] input = split(readln()); int a = to!int(input[0])-1; int b = to!int(input[1]); int ans = 0; int tap = 1; while (tap < b) { tap += a; ans++; } writeln(ans); }
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() { dchar[] s; readV(s); writeln(s.uniq.walkLength-1); }
D
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } void main() { auto I = readln.split.to!(long[]); auto A = I[0]; auto B = I[1]; long solve() { foreach(i; 0..30000) { auto cp8 = (i * 108) / 100 - i; auto cp10 = (i * 110) / 100 - i; if (cp8 == A && cp10 == B) return i; } return -1; } solve().writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } 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 c1 = RD!(char[]); auto c2 = RD!(char[]); c2.reverse(); bool ans = true; foreach (i; 0..c1.length) { if (c1[i] != c2[i]) ans = false; } writeln(ans ? "YES" : "NO"); stdout.flush(); debug readln(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.numeric, std.typecons; void main() { auto N = readln.chomp.to!int; int[][] AAS; AAS.length = N; foreach (ref AA; AAS) AA = readln.split.to!(int[]); foreach (k; 0..N) foreach (i; 0..N) foreach (j; 0..N) if (AAS[i][k] + AAS[k][j] < AAS[i][j]) goto not; if (false) { not: writeln("-1"); return; } long sum_l = 0; foreach (i; 0..N) { foreach (j; 0..N) { auto flg = true; foreach (k; 0..N) if (k != i && k != j && AAS[i][j] == AAS[i][k] + AAS[k][j]) { flg = false; break; } if (flg) sum_l += AAS[i][j]; } } writeln(sum_l / 2); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto A = RD; auto B = RD; auto X = RD; writeln(A <= X && A + B >= X ? "YES" : "NO"); stdout.flush(); debug readln(); }
D
import std.stdio; import std.conv; import std.algorithm; import std.range; import std.string; import std.typecons; void main() { auto a = readln().strip; auto b = readln().strip; size_t sub = 0; while (true) { sub = a.indexOf(b, sub); if (sub == cast(size_t)-1) { break; } else { writeln(sub); sub++; } } }
D
unittest { assert( [ "oxoxoxoxoxoxox" ].parse.expand.solve == "YES" ); assert( [ "xxxxxxxx" ].parse.expand.solve == "NO" ); } import std.algorithm; import std.conv; import std.range; import std.stdio; import std.typecons; void main() { stdin.byLineCopy.parse.expand.solve.writeln; } auto parse( Range )( Range input ) if( isInputRange!Range && is( ElementType!Range == string ) ) { auto s = input.front; return tuple( s ); } auto solve( string s ) { return ( 8 <= 15 - s.count( 'x' ) ) ? "YES" : "NO"; }
D
void main() { int n = readln.chomp.to!int; int[] a = readln.split.to!(int[]); double m = a.sum / n.to!double; double diff = double.max; ulong frame; foreach (i, x; a) { if (abs(x.to!double - m) < diff) { frame = i; diff = abs(x.to!double - m); } } frame.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, std.numeric; void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; int[][] AS; AS.length = N; foreach (ref A; AS) { A = readln.split.to!(int[]).map!"a-1".array; } auto ii = new int[](N); auto ms = new bool[](M); int r = int.max; foreach (_; 0..M) { auto cnt = new int[](M); foreach (i, ref j; ii) { while (ms[AS[i][j]]) { ++j; } ++cnt[AS[i][j]]; } int jj, max_c; foreach (j, c; cnt) if (c > max_c) { max_c = c; jj = j.to!int; } r = min(r, max_c); ms[jj] = true; } writeln(r); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.math; void main() { int x = readln.chomp.to!(int); int[1001] arr; for (int i = 1; i <= x; i++) { for (int j = 2; j <= 100; j++) { int t = pow(i, j); if (t <= x) { arr[i] = max(arr[i], t); } else { break; } } arr[i] = max(arr[i - 1], arr[i]); } writeln(arr[x]); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; enum P = 10L^^9+7; long pow(long x, long n) { long y = 1; while (n) { if (n%2 == 1) y = (y * x) % P; x = x^^2 % P; n /= 2; } return y; } void main() { auto nk = readln.split.to!(long[]); auto N = nk[0]; auto K = nk[1]; auto cs = new long[](K+1); foreach_reverse (k; 1..K+1) { auto x = K / k; cs[k] = pow(x, N); auto kk = k; for (;;) { kk += k; if (kk > K) break; cs[k] = (cs[k] - cs[kk] + P) % P; } } long r; foreach (i, c; cs) r = (r + c * i.to!long % P) % P; writeln(r); }
D
import std.algorithm; import std.stdio; import std.string; void main() { bool[string] cups = ["A": true, "B": false, "C": false]; foreach (string line; stdin.lines) { string[] cup = line.chomp.split(","); swap(cups[cup[0]],cups[cup[1]]); } foreach (k; cups.keys) { if (cups[k] == true) { k.writeln; break; } } }
D
/+ dub.sdl: name "C" dependency "dcomp" version=">=0.7.4" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; int main() { Scanner sc = new Scanner(stdin); int n; int[] a; sc.read(n); sc.read(a); bool[] ok = new bool[1<<24]; ok[1] = true; foreach (d; a) { bool[] ok2 = new bool[1<<24]; foreach (i, f; ok) { if (!f) continue; int x = d; if (!(i & (1<<x))) { ok2[i | (1<<x)] = true; } x = (24-d) % 24; if (!(i & (1<<x))) { ok2[i | (1<<x)] = true; } } ok = ok2; } int ans = 0; foreach (i, f; ok) { if (!f) continue; // writeln("debug ", i); int buf = 10000; foreach (x; 0..24) { foreach (y; x+1..24) { if (!(i & (1<<x))) continue; if (!(i & (1<<y))) continue; buf = min(buf, y-x); buf = min(buf, (x-y + 24) % 24); } } ans = max(ans, buf); } writeln(ans); return 0; } /* 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/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; } /* 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 std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { int n, z, w; scan(n, z, w); auto a = readln.split.to!(int[]); if (n == 1) { writeln(abs(a[0] - w)); return; } if (abs(a[n - 1] - w) > abs(a[n - 1] - a[n - 2])) { writeln(abs(a[n - 1] - w)); } else { writeln(abs(a[n - 1] - a[n - 2])); } } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
void main() { auto N = readAs!long; ulong current = ulong.max; foreach(long i; 1..N.to!real.sqrt.ceil.to!long+1) { if(N % i == 0) { current = min(current, abs(i + N / i)); } } (current-2).writeln; } ulong[] getDivisors(ulong N) { ulong[] res; foreach(i; 1..N.to!real.sqrt.ceil.to!ulong) { if(N % i == 0) { res ~= i; res ~= N / i; } } return res; } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; if (N == 1) { writeln("Hello World"); } else { auto A = readln.chomp.to!int; auto B = readln.chomp.to!int; writeln(A + B); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto S = readln.chomp; auto len = S.length; if (len%2 == 1) { writeln("No"); return; } foreach (i; 0..len/2) { auto c = S[len-i-1]; switch (S[i]) { case 'b': if (c != 'd') goto default; break; case 'd': if (c != 'b') goto default; break; case 'p': if (c != 'q') goto default; break; case 'q': if (c != 'p') goto default; break; default: writeln("No"); return; } } writeln("Yes"); }
D
/+ dub.sdl: name "A" dependency "dunkelheit" version="1.0.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; int main() { Scanner sc = new Scanner(stdin); scope(exit) assert(!sc.hasNext); int n; sc.read(n); int ans = 10^^9; foreach (a; 1..n) { int b = n-a; string s = a.to!string; string t = b.to!string; int buf = s.map!(c => c-'0').sum + t.map!(c => c-'0').sum; ans = min(ans, buf); } writeln(ans); return 0; } /* IMPORT /home/yosupo/.dub/packages/dunkelheit-1.0.0/dunkelheit/source/dkh/scanner.d */ // module dkh.scanner; // import dkh.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; private File f; this(File f) { this.f = f; } private char[512] lineBuf; private char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { assert(succW()); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(Args...)(auto ref Args args) { import std.exception : enforce; static if (args.length != 0) { enforce(readSingle(args[0])); read(args[1..$]); } } bool hasNext() { return succ(); } } /* IMPORT /home/yosupo/.dub/packages/dunkelheit-1.0.0/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /home/yosupo/.dub/packages/dunkelheit-1.0.0/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; void times(alias fun)(int n) { foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } // fold was added in D 2.071.0. 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); } } } void main() { readln.chomp.to!int.pipe!(N => N*(N+1)/2).writeln; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; // dfmt on void main() { long N = lread(); auto A = new long[](N); auto B = new long[](N); foreach (i; 0 .. N) scan(A[i], B[i]); long ans; foreach_reverse (i; 0 .. N) { long m = (A[i] + ans) % B[i]; if (m == 0) continue; long d = B[i] - m; ans += d; } writeln(ans); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.math; long n, m; void main() { scan(n, m); auto cmb = new long[][](n + 1, n + 1); auto dp = new long[][](n + 1, n + 1); foreach (i ; 0 .. n + 1) { cmb[i][0] = cmb[i][i] = 1; dp[i][0] = dp[i][i] = 1; } foreach (i ; 0 .. n + 1) { foreach (j ; 1 .. i) { cmb[i][j] = (cmb[i-1][j] + cmb[i-1][j-1]); if (cmb[i][j] >= m) cmb[i][j] -= m; dp[i][j] = (dp[i-1][j-1] + (j + 1) * dp[i-1][j] % m); if (dp[i][j] >= m) dp[i][j] -= m; } } debug { //writeln(cmb); //writeln(dp); } auto tpm = new long[](n*n + 1); auto tpm1 = new long[](n*n + 1); tpm[0] = tpm1[0] = 1; foreach (i ; 1 .. n*n + 1) { tpm[i] = tpm[i-1] * 2 % m; tpm1[i] = tpm1[i-1] * 2 % (m-1); } long f(int k) { long res; foreach (x ; 0 .. k + 1) { //res += powmod(2, powmod(2, n - k, m - 1), m) * dp[k][x] % m * powmod(2, (n - k) * x, m) % m; res += powmod(2, tpm1[n - k], m) * dp[k][x] % m * tpm[(n-k)*x] % m; if (res >= m) res -= m; } return res; } long ans; foreach (int k ; 0 .. n.to!int + 1) { if (k & 1) { ans -= cmb[n][k] * f(k) % m; if (ans < 0) ans += m; } else { ans += cmb[n][k] * f(k) % m; if (ans >= m) ans -= m; } } writeln(ans); } // x^y % mod long powmod(long x, long y, long mod) { return y > 0 ? powmod(x, y>>1, mod)^^2 % mod * x^^(y&1) % mod : 1L; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void main() { auto S = readln.chomp; writeln(S[$-1] == 's' ? S ~ "es" : S ~ "s"); }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto n = readln.strip.to !(int); auto a = readln.splitter.map !(to !(long)).array; auto m = a.maxElement * (n - 1L); auto t = a.sum (0L); auto s = max (m, t); s += ((n - 1) - (s % (n - 1))) % (n - 1); writeln (s - t); } }
D
import std.stdio, std.string, std.conv; import std.array, std.algorithm, std.range; void main() { immutable T = ["A":0,"B":1,"AB":2,"O":3]; int[4] c; foreach(v;stdin.byLine().map!(s=>T[s.split(",")[1]])) ++c[v]; foreach(v;c) writeln(v); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; void main() { int[] buf = readln.chomp.split.to!(int[]); int n = buf[0], k = buf[1]; int[] arr = readln.chomp.split.to!(int[]); int ans = 0; n -= 1; while (n > 0) { n -= k - 1; ans++; } writeln(ans); }
D
import std.stdio; import std.string; import std.conv; import std.array; import std.algorithm; import std.range; void main(){ auto N=readln.chomp.to!int; if(N/10==9||N%10==9)writeln("Yes"); else writeln("No"); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { long n, k; readV(n, k); auto x = n/k, y = 0L; if (k%2 == 0) y = x + (n%k >= k/2 ? 1 : 0); writeln(x^^3+y^^3); }
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.container; void main() { auto n = readln.chomp.to!int; foreach (c; 1..n+1) { readln; auto area = new char[][](8, 8); foreach (i; 0..8) { area[i] = readln.chomp.map!(to!char).array; } auto y = readln.chomp.to!int - 1; auto x = readln.chomp.to!int - 1; void solve(int i, int j) { area[i][j] = '0'; foreach (h; max(i-3,0)..min(i+4, 8)) { if (area[h][j] == '1') solve(h, j); } foreach (w; max(j-3,0)..min(j+4, 8)) { if (area[i][w] == '1') solve(i, w); } } solve(x, y); writeln("Data ", c, ":"); foreach (i; 0..8) { foreach (j; 0..8) { write(area[i][j]); } writeln(""); } } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { int w = readint; int n = readint; auto xs = new int[w + 1]; for (int i = 0; i < xs.length; i++) xs[i] = i; for (int i = 0; i < n; i++) { auto ab = readln.chomp.split(",").to!(int[]); int a = ab[0], b = ab[1]; swap(xs[a], xs[b]); } foreach (x; xs[1 .. $]) writeln(x); }
D
import std.stdio, std.string, std.array, std.conv; long modexp(long x, long n) { if (n == 0) return 1; long mod = 1_000_000_007; long result = modexp(x, n/2); result = result * result % mod; if (n % 2 == 1) result = result * x % mod; return result; } void main() { long[] tmp = readln.chomp.split.to!(long[]); long m = tmp[0], n = tmp[1]; modexp(m, n).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!int); auto N = s[0]; auto Q = s[1]; auto st = new LazySegmentTree!(long, long, (a,b)=>a+b, (a,b)=>a+b, (a,b)=>a+b, (a,b)=>a*b, 0L, 0L)(N+1); while (Q--) { s = readln.split.map!(to!int); int q = s[0]; int l = s[1]; int r = s[2]; if (q == 0) { long v = s[3]; st.update(l, r, v); } else { st.query(l, r).writeln; } } } class LazySegmentTree(T, L, alias opTT, alias opTL, alias opLL, alias opPrd, L eL, T eT) { T[] table; L[] lazy_; int n; int size; this(int n) { this.n = n; size = 1; while (size <= n) size <<= 1; size <<= 1; table = new T[](size); lazy_ = new T[](size); table[] = eT; lazy_[] = eL; } void push(int i, int a, int b) { if (lazy_[i] == eL) return; table[i] = opTL(table[i], opPrd(lazy_[i], b - a + 1)); if (i * 2 + 1 < size) { lazy_[i*2] = opLL(lazy_[i*2], lazy_[i]); lazy_[i*2+1] = opLL(lazy_[i*2+1], lazy_[i]); } lazy_[i] = eL; } T query(int l, int r) { if (l > r) return eT; return query(l, r, 1, 0, n-1); } T query(int l, int r, int i, int a, int b) { if (b < l || r < a) return eT; push(i, a, b); if (l <= a && b <= r) { return table[i]; } else { return opTT(query(l, r, i*2, a, (a+b)/2), query(l, r, i*2+1, (a+b)/2+1, b)); } } void update(int l, int r, L val) { if (l > r) return; update(l, r, 1, 0, n-1, val); } void update(int l, int r, int i, int a, int b, L val) { if (b < l || r < a) { push(i, a, b); } else if (l <= a && b <= r) { lazy_[i] = opLL(lazy_[i], val); push(i, a, b); } else { push(i, a, b); update(l, r, i*2, a, (a+b)/2, val); update(l, r, i*2+1, (a+b)/2+1, b, val); table[i] = opTT(table[i*2], table[i*2+1]); } } }
D
import std.algorithm; import std.array; import std.bigint; import std.bitmanip; import std.conv; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; // 差の絶対値 @nogc @safe pure T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; } // 切り上げ除算 @nogc @safe pure T divCeil(T)(const ref T a, const ref T b) { return (a+b-1)/b; } T[] readToArray(T)() { return readln.split.to!(T[]); } void readInto(T...)(ref T ts) { auto ss = readln.split; foreach(ref t; ts) { t = ss.front.to!(typeof(t)); ss.popFront; } } // 冪乗をmod取りつつ計算 @nogc @safe pure ulong modPow(ulong a, ulong n, ulong m) { ulong r = 1; while (n > 0) { if(n % 2 != 0) r = r * a % m; a = a * a % m; n /= 2; } return r; } // フェルマーの小定理から乗法逆元を計算 // 定理の要請により法は素数 @nogc @safe pure ulong modInv(ulong a, ulong m) { return modPow(a, m-2, m); } // mod取りつつ順列を計算 @nogc @safe pure ulong modPerm(ulong n, ulong k, ulong m) { if (n < k) return 0; ulong r = 1; for (ulong i = n-k+1; i <= n; i++) { r *= i; r %= m; } return r; } // mod取りつつ順列を計算 @nogc @safe pure ulong modFact(ulong n, ulong m) { return modPerm(n, n, m); } // mod取りつつ組み合わせを計算 // modInvを使っているので法は素数 @nogc @safe pure ulong modComb(ulong n, ulong r, ulong m) { return modPerm(n, r, m)*modInv(modFact(r, m), m) % m; } immutable ulong MOD = 1000000007; void main() { ulong a, b; readInto(a, b); ulong ret; if ( (a + b) % 2 == 0) { writeln((a+b)/2); } else if (diff(a,b) % 2 == 0) { writeln(diff(a,b)%2); } else { writeln("IMPOSSIBLE"); } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; void main() { int n, k; scan(n, k); auto a = readln.split.to!(int[]); auto dp = new int[](k + 1); dp[0] = 0; // 0: 先手負け(=後手勝ち)、 1: 先手勝ち foreach (i ; 1 .. k + 1) { dp[i] = 0; foreach (ai ; a) { if (i - ai < 0) { break; } if (dp[i - ai] == 0) { dp[i] = 1; break; } } } debug { writeln(dp); } writeln(dp[k] ? "First" : "Second"); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.conv; import std.string; import core.bitop; void main() { auto n = readln.chomp.to!uint; if (n == 0) return; writeln(1U << (n.bsr)); }
D
import std; auto input() { return readln().chomp(); } alias sread = () => readln.chomp(); void main() { string s = input(); long s_len = s.length; //writeln(s); //writeln(s_len); writeln(s[0 .. ((s.length) - 8)]); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
D
import std.stdio, std.string, std.conv; void main() { auto n = readln.chomp.to!int; string[] result; foreach (i; 0..n) { result ~= readln.chomp.replace("Hoshino", "Hoshina"); } foreach (r; result) r.writeln; }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!T;r.popFront;}} void main() { int n; readV(n); int[] p; readA(n, p); auto r = 0; foreach (i; 0..n-1) if (p[i] == i+1) { ++r; swap(p[i], p[i+1]); } if (p[n-1] == n) ++r; writeln(r); }
D
module app; import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; void main(string[] argv){ // input int n; n = to!int(readln().chop()); int[] a1; a1 = map!(to!int)(readln().chop().split()).array(); int[] a2; a2 = map!(to!int)(readln().chop().split()).array(); int answer; foreach(temp; map!((x) => get_candies_num(x,a1,a2))(iota(0,n))) { if(answer < temp){ answer = temp; } } writeln(answer); } // x行目で曲がった時に回収できる飴の個数 int get_candies_num(int x, int[] a1, int[] a2){ int result; result = a1[0..x+1].sum() + a2[x..$].sum(); return result; }
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, m; readV(n, m); auto g = GraphW!(int, long, 10L^^18)(n); foreach (_; 0..m) { int a, b; long c; readV(a, b, c); --a; --b; g.addEdge(a, b, -c); } auto d = g.bellmanFord(0); if (d[n-1] == -g.inf) writeln("inf"); else writeln(-d[n-1]); } struct GraphW(N = int, W = int, W i = 10^^9) { alias Node = N, Wt = W, inf = i; struct Edge { Node src, dst; Wt wt; alias cap = wt; } Node n; Edge[][] g; alias g this; this(Node n) { this.n = n; g = new Edge[][](n); } void addEdge(Node u, Node v, Wt w) { g[u] ~= Edge(u, v, w); } void addEdgeB(Node u, Node v, Wt w) { g[u] ~= Edge(u, v, w); g[v] ~= Edge(v, u, w); } } template BellmanFord(Graph) { import std.traits; alias Node = TemplateArgsOf!Graph[0], Wt = TemplateArgsOf!Graph[1]; void bellmanFord(ref Graph g, Node s, out Wt[] dist, out Node[] prev) { auto n = g.n, sent = n; dist = new Wt[](n); dist[] = g.inf + g.inf; dist[s] = 0; prev = new Node[](n); prev[] = sent; foreach (k; 0..n) foreach (i; 0..n) foreach (e; g[i]) if (dist[e.dst] > dist[e.src] + e.wt) { dist[e.dst] = dist[e.src] + e.wt; prev[e.dst] = e.src; if (k == n-1) dist[e.dst] = -g.inf; } } Wt[] bellmanFord(ref Graph g, Node s) { Wt[] dist; Node[] prev; bellmanFord(g, s, dist, prev); return dist; } } auto bellmanFord(G, N)(G g, N s) { return BellmanFord!G.bellmanFord(g, s); }
D
import std.stdio; import std.algorithm; import std.conv; import std.math; import std.string; void main() { auto tokens = split(chomp(readln())); auto h = to!int(tokens[0]); auto w = to!int(tokens[1]); int[][] cell; cell.length = h; foreach (y; 0..h) { foreach (token; split(chomp(readln()))) { cell[y] ~= to!int(token); } } int[][] result; foreach (y; 0..h-1) { foreach (x; 0..w) { if (cell[y][x] % 2 == 1) { --cell[y][x]; ++cell[y+1][x]; result ~= [y+1, x+1, y+2, x+1]; } } } foreach (x; 0..w-1) { if (cell[h-1][x] % 2 == 1) { --cell[h-1][x]; ++cell[h-1][x+1]; result ~= [h, x+1, h, x+2]; } } writeln(result.length); foreach (e; result) { writeln(e[0], " ", e[1], " ", e[2], " ", e[3]); } stdout.flush(); }
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 s = readln.split.map!(to!int); auto K = s[0]; auto N = s[1]; auto A = readln.split.map!(to!int).array; int ans = 1 << 29; foreach (i; 0..N) { auto a = A[i]; auto b = A[(i+1)%N]; auto c = (b > a) ? b - a : b + K - a; ans = min(ans, K - c); } ans.writeln; }
D
import std.stdio; import std.conv; import std.array; void main() { int a = readln.split[0].to!int; int b = readln.split[0].to!int; int c = readln.split[0].to!int; int d = readln.split[0].to!int; int e = readln.split[0].to!int; int k = readln.split[0].to!int; if (e - a <= k){ writeln("Yay!"); } else { writeln(":("); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto A = RD; writeln(N % 500 <= A ? "Yes" : "No"); stdout.flush(); debug readln(); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; enum L = 2001; void main() { int n; scan(n); auto s = readln.chomp; if (s[0] != 'B' || s[2*n - 1] != 'B') { writeln(0); return; } auto b = new int[](2*n); foreach (i ; 0 .. 2*n) { if (s[i] == 'B') b[i] = 1; else b[i] = 0; } auto a = new int[](2*n); long ans = 1; long cnt; foreach (i ; 0 .. 2*n) { if ((cnt & 1) ^ b[i]) { cnt++; } else { ans *= cnt; ans %= mod; cnt--; } } if (cnt > 0) { writeln(0); return; } foreach (i ; 1 .. n + 1) { ans *= i; ans %= mod; } writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
// import chie template :) {{{ import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii; // }}} // tbh.scanner {{{ class Scanner { import std.stdio; import std.conv : to; import std.array : split; import std.string : chomp; private File file; private dchar[][] str; private uint idx; this(File file = stdin) { this.file = file; this.idx = 0; } private dchar[] next() { if (idx < str.length) { return str[idx++]; } dchar[] s; while (s.length == 0) { s = file.readln.chomp.to!(dchar[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(uint len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } } // }}} void main() { auto cin = new Scanner; string s = cin.next!string; writeln("2018" ~ s[4 .. $]); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; alias Pair = Tuple!(int, "a", int, "b"); void main() { string s, t; scan(s); scan(t); int N = s.length.to!int; auto rs = new int[](26); auto rt = new int[](26); rs[] = -1; rt[] = -1; bool ok = true; foreach (i, ch ; s) { if (rs[ch - 'a'] == -1) { rs[ch - 'a'] = t[i] - 'a'; if (rt[t[i] - 'a'] != -1 && rt[t[i] - 'a'] != ch - 'a') ok = false; else rt[t[i] - 'a'] = ch - 'a'; } else { if (rs[ch - 'a'] != t[i] - 'a') ok = false; } } writeln(ok ? "Yes" : "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); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto hw = readln.split.to!(int[]); auto H = hw[0]; auto W = hw[1]; hw = readln.split.to!(int[]); auto h = hw[0]; auto w = hw[1]; writeln(H*W - h*W - w*H + h*w); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.conv; void main() { auto S = readln.chomp; S.check.writeln; } int check(string s) { if(s.length & 1) s.popBack(); else { s.popBack; s.popBack; } while(true) { if(s[0..$ / 2] == s[$ / 2..$]) return s.length.to!int; else { s.popBack; s.popBack; } } } // =================================== T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (isBasicType!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } int ri() { return readAs!int; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.math; void main() { int h, w; scan(h, w); auto ban = new char[][](h, w); iota(h).each!(i => ban[i] = readln.chomp.to!(char[])); auto cnt = new int[](26); foreach (i ; 0 .. h) { foreach (j ; 0 .. w) { cnt[ban[i][j] - 'a']++; } } if (h & 1) swap(h, w); auto odd = cnt.count!"a % 2 == 1".to!int; auto even = cnt.count!"a % 2 == 0 && a % 4 != 0".to!int; auto m4 = cnt.count!"a % 4 == 0".to!int; bool ok; if ((h & 1) && (w & 1)) { if (odd == 1 && even <= (h / 2 + w / 2)) { ok = 1; } } else if (w & 1) { if (odd == 0 && even <= (h / 2)) { ok = 1; } } else { if (odd == 0 && even == 0) { ok = 1; } } writeln(ok ? "Yes" : "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; import core.bitop; // dfmt off ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); void aryWrite(T = long)(T[] ary){ ary.map!(x => x.text()).join(' ').writeln(); } alias Pair = Tuple!(long, "H", long, "W", long, "cost"); alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less); // dfmt on void main() { auto s = lread(); auto dp = new long[](s + 1); foreach (i; iota(3, s + 1)) { dp[i] += 1; foreach (j; iota(i)) { if (i - j >= 3) { dp[i] += dp[j]; dp[i] %= MOD; } } dp[i] %= MOD; } // dp.writeln(); dp[s].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
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std; // dfmt on void main() { long X, Y; scan(X, Y); if (Y & 1) { writeln("No"); return; } if (X * 2 <= Y && Y <= X * 4) { writeln("Yes"); return; } writeln("No"); }
D
void main() { auto s = rs; if(s.length == 2) writeln(s); else writeln(s[2], s[1], s[0]); } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
void main(){ int k = _scan(); string s = readln().chomp(); if( s.length <= k ){ s.writeln(); return; }else{ ( s[0..k] ~ "..." ).writeln(); } } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
// import chie template :) {{{ static if (__VERSION__ < 2090) { import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons, std.format, std.bitmanip, std.numeric; } else { import std; } // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan(T...)(ref T args) { foreach (ref arg; args) { arg = next!(typeof(arg)); } } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } // }}} // ModInt {{{ struct ModInt(ulong modulus) { import std.traits : isIntegral, isBoolean; import std.exception : enforce; private { ulong val; } this(const ulong n) { val = n % modulus; } this(const ModInt n) { val = n.value; } @property { ref inout(ulong) value() inout { return val; } } T opCast(T)() { static if (isIntegral!T) { return cast(T)(val); } else if (isBoolean!T) { return val != 0; } else { enforce(false, "cannot cast from " ~ this.stringof ~ " to " ~ T.stringof ~ "."); } } ModInt opAssign(const ulong n) { val = n % modulus; return this; } ModInt opOpAssign(string op)(ModInt rhs) { static if (op == "+") { val += rhs.value; if (val >= modulus) { val -= modulus; } } else if (op == "-") { if (val < rhs.value) { val += modulus; } val -= rhs.value; } else if (op == "*") { val = val * rhs.value % modulus; } else if (op == "/") { this *= rhs.inv; } else if (op == "^^") { ModInt res = 1; ModInt t = this; while (rhs.value > 0) { if (rhs.value % 2 != 0) { res *= t; } t *= t; rhs /= 2; } this = res; } else { enforce(false, op ~ "= is not implemented."); } return this; } ModInt opOpAssign(string op)(ulong rhs) { static if (op == "+") { val += rhs; if (val >= modulus) { val -= modulus; } } else if (op == "-") { if (val < rhs) { val += modulus; } val -= rhs; } else if (op == "*") { val = val * rhs % modulus; } else if (op == "/") { this *= ModInt(rhs).inv; } else if (op == "^^") { ModInt res = 1; ModInt t = this; while (rhs > 0) { if (rhs % 2 != 0) { res *= t; } t *= t; rhs /= 2; } this = res; } else { enforce(false, op ~ "= is not implemented."); } return this; } ModInt opUnary(string op)() { static if (op == "++") { this += 1; } else if (op == "--") { this -= 1; } else { enforce(false, op ~ " is not implemented."); } return this; } ModInt opBinary(string op)(const ulong rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinary(string op)(ref const ModInt rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinary(string op)(const ModInt rhs) const { mixin("return ModInt(this) " ~ op ~ "= rhs;"); } ModInt opBinaryRight(string op)(const ulong lhs) const { mixin("return ModInt(this) " ~ op ~ "= lhs;"); } long opCmp(ref const ModInt rhs) const { return cast(long)value - cast(long)rhs.value; } bool opEquals(const ulong rhs) const { return value == ModInt(rhs).value; } ModInt inv() const { ModInt ret = this; ret ^^= modulus - 2; return ret; } string toString() const { import std.format : format; return format("%s", val); } } // }}} // alias {{{ alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less); alias MinHeap(T) = Heap!(T, "a > b"); // }}} // memo {{{ /* - ある値が見つかるかどうか <https://dlang.org/phobos/std_algorithm_searching.html#canFind> canFind(r, value); -> bool - 条件に一致するやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#filter> // 2で割り切れるやつ filter!"a % 2 == 0"(r); -> Range - 合計 <https://dlang.org/phobos/std_algorithm_iteration.html#sum> sum(r); - 累積和 <https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold> // 今の要素に前の要素を足すタイプの一般的な累積和 // 累積和のrangeが帰ってくる(破壊的変更は行われない) cumulativeFold!"a + b"(r, 0); -> Range - rangeをarrayにしたいとき array(r); -> Array - 各要素に同じ処理をする <https://dlang.org/phobos/std_algorithm_iteration.html#map> // 各要素を2乗 map!"a * a"(r) -> Range - ユニークなやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#uniq> uniq(r) -> Range - 順列を列挙する <https://dlang.org/phobos/std_algorithm_iteration.html#permutations> permutation(r) -> Range - ある値で埋める <https://dlang.org/phobos/std_algorithm_mutation.html#fill> fill(r, val); -> void - バイナリヒープ <https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap> // 昇順にするならこう(デフォは降順) BinaryHeap!(Array!T, "a > b") heap; heap.insert(val); heap.front; heap.removeFront(); - 浮動小数点の少数部の桁数設定 // 12桁 writefln("%.12f", val); - 浮動小数点の誤差を考慮した比較 <https://dlang.org/phobos/std_math.html#.approxEqual> approxEqual(1.0, 1.0099); -> true - 小数点切り上げ <https://dlang.org/phobos/std_math.html#.ceil> ceil(123.4); -> 124 - 小数点切り捨て <https://dlang.org/phobos/std_math.html#.floor> floor(123.4) -> 123 - 小数点四捨五入 <https://dlang.org/phobos/std_math.html#.round> round(4.5) -> 5 round(5.4) -> 5 */ // }}} void main() { auto cin = new Scanner; long n; cin.scan(n); string[] s = cin.nextArray!string(n); string name = "MARCH"; long[] cnt = new long[5]; foreach (e; s) { foreach (i, c; name) { if (c == e[0]) { cnt[i]++; break; } } } long res; foreach (i; 0 .. 5) { foreach (j; i + 1 .. 5) { foreach (k; j + 1 .. 5) { res += cnt[i] * cnt[j] * cnt[k]; } } } writeln(res); }
D
import std.stdio, std.string; void main() { readln.replace(",", " ").writeln; }
D
void main() { auto N = ri; auto H = ri; auto W = ri; writeln(max(0, N - H + 1) * max(0, N - W + 1)); } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.string, std.conv; void main(){ auto ip = readln.split.to!(int[]); if(ip[0] + ip[1] > ip[2] + ip[3]){ writeln("Left"); }else if(ip[0] + ip[1] < ip[2] + ip[3]){ writeln("Right"); }else{ writeln("Balanced"); } }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; void main(){ auto ip = readln.split.to!(int[]), a = ip[0], b = ip[1]; ((a * b) % 2 == 0 ? "Even" : "Odd").writeln; }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; } enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { string o, e; scan(o); scan(e); string ans; int i; while (!o.empty || !e.empty) { if (i % 2 == 0) { ans ~= o.front; o.popFront(); } else { ans ~= e.front; e.popFront(); } i++; } writeln(ans); }
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; long calc(long L, long R) { long ans = long.max; foreach (i; L..R+1) { foreach (j; i+1..R+1) { ans = min(ans, i * j % 2019); if (ans == 0) return ans; } } return ans; } void main() { int L, R; scan(L, R); writeln(calc(L, R)); }
D
void main() { long n = rdElem; string s = rdStr; long[] r = new long[n+1], g = new long[n+1], b = new long[n+1]; foreach (i, x; s) { r[i+1] += r[i]; g[i+1] += g[i]; b[i+1] += b[i]; if (x == 'R') ++r[i+1]; if (x == 'G') ++g[i+1]; if (x == 'B') ++b[i+1]; } long cnt; foreach (i; 0 .. n-2) { foreach (j; i+1 .. n-1) { long dist = j - i; if (s[i] == 'R') { if (s[j] == 'G') { cnt += b[n] - b[j]; if (j + dist < n && s[j+dist] == 'B') --cnt; } if (s[j] == 'B') { cnt += g[n] - g[j]; if (j + dist < n && s[j+dist] == 'G') --cnt; } } if (s[i] == 'G') { if (s[j] == 'R') { cnt += b[n] - b[j]; if (j + dist < n && s[j+dist] == 'B') --cnt; } if (s[j] == 'B') { cnt += r[n] - r[j]; if (j + dist < n && s[j+dist] == 'R') --cnt; } } if (s[i] == 'B') { if (s[j] == 'R') { cnt += g[n] - g[j]; if (j + dist < n && s[j+dist] == 'G') --cnt; } if (s[j] == 'G') { cnt += r[n] - r[j]; if (j + dist < n && s[j+dist] == 'R') --cnt; } } } } cnt.writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; enum double eps = 1.0e-9; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
import std.algorithm; import std.ascii; import std.array; import std.container; import std.conv; import std.numeric; import std.stdio; import std.string; import std.typecons; void log(A...)(A arg) { stderr.writeln(arg); } int size(T)(in T s) { return cast(int)s.length; } const int INF = 10000; int op(char c, int a, int b) { if (c == '+') return a + b; if (c == '-') return a - b; assert(false); } int next_op_index(in string s, int i) { for (int j = i; j < s.size; j++) { if (s[j] == '+' || s[j] == '-') return j; } throw new Exception(""); } int prev_op_index(in string s, int i) { for (int j = i; j >= 0; j--) { if (s[j] == '+' || s[j] == '-') return j; } throw new Exception(""); } void main() { string s = readln.chomp; auto t_index = new int[s.size]; t_index[] = -1; int cur = 0; foreach (i, c; s) { if (c == '(' || c == ')') continue; t_index[i] = cur++; } auto buf = new char[cur]; foreach (i, c; s) { if (c == '(' || c == ')') continue; buf[t_index[i]] = c; } string t = buf.idup; int N = t.size; auto open_banned = new bool[N]; auto close_banned = new bool[N]; foreach (int i, c; s) { try { if (c == '(') close_banned[t_index[next_op_index(s, i)]] = true; else if (c == ')') open_banned[t_index[prev_op_index(s, i)]] = true; } catch (Exception e) {} } auto dp_min = new int[][](N + 1, N + 1); auto dp_max = new int[][](N + 1, N + 1); foreach (ref a; dp_max) a[] = -INF; foreach (ref a; dp_min) a[] = INF; foreach (i, c; t) { if (c.isDigit) dp_max[i][i + 1] = dp_min[i][i + 1] = cast(int)(c - '0'); } for (int k = 2; k <= N; k++) { for (int i = 0; i + k <= N; i++) { for (int j = i + 1; j < i + k; j++) { if (! (t[j] == '+' || t[j] == '-')) continue; int lcount = cast(int)t[i .. j].count!isDigit; int rcount = cast(int)t[j .. i + k].count!isDigit; if (lcount >= 2 && close_banned[j]) continue; if (rcount >= 2 && open_banned[j]) continue; switch (t[j]) { case '+': dp_max[i][i + k] = max(dp_max[i][i + k], dp_max[i][j] + dp_max[j + 1][i + k]); dp_min[i][i + k] = min(dp_min[i][i + k], dp_min[i][j] + dp_min[j+ 1][i + k]); break; case '-': dp_max[i][i + k] = max(dp_max[i][i + k], dp_max[i][j] - dp_min[j + 1][i + k]); dp_min[i][i + k] = min(dp_min[i][i + k], dp_min[i][j] - dp_max[j + 1][i + k]); break; case '(': default: break; } } } } /* foreach (L; dp_max) log(L); log(); foreach (L; dp_min) log(L); */ writeln(dp_max[0][t.size]); }
D
import std.stdio; void main() { char b = 'a'; foreach(c; readln) { if (b == c) { write("Bad"); return; } b = c; } write("Good"); }
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() { int[4] s; foreach (i; 0..3) { auto d = readln.chomp.split.to!(int[]); s[d[0]-1]++; s[d[1]-1]++; } int[4] cnt; foreach (i; 0..4) { cnt[s[i]]++; } if (cnt[1] == 2 && cnt[2] == 2) { writeln("YES"); } else { writeln("NO"); } }
D
import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; void main(string[] args) { string s = readln.chomp; char[] st; foreach(c; s){ if(c == 'T' && !st.empty && st.back == 'S'){ st.popBack(); }else{ st ~= c; } } writeln(st.length); }
D
import std; auto input() { return readln().chomp(); } void main() { long x, y; scan(x, y); if (x % y == 0) { writeln(-1); } else { writeln(x); } } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
D
import std.stdio; import std.algorithm; import std.string; import std.functional; import std.array; import std.conv; import std.math; import std.typecons; import std.regex; import std.range; real[12][1<<12] dp; int[] cakes; int n; real saiki(int u,int v){ if(dp[u][v] > 0) return dp[u][v]; if( u == ((1<<n) - 1)) return cakes[v]; real res = 10000000; for(int i=0;i<n;i++){ if(( (1<<i) & u ) == 0){ res = min(res,saiki(u | (1<<i) ,i) + 2.0 * sqrt(to!real(cakes[i]*cakes[v]) )); } } return dp[u][v] = res; } void main(){ while(true){ string s = readln(); if(stdin.eof()) break; dp = new real[12][1<<12]; auto s1 = s.split().to!(int[]); n = to!int(s1.length) - 1; cakes = (s1[1..$]).dup(); cakes ~= 0; real ans = 1e8; for(int i=0;i<n;i++){ ans = min(saiki(1<<i,i) + cakes[i],ans); } writeln(ans <= s1[0] ? "OK" : "NA"); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math; int[10^^5] XS; void main() { auto nk = readln.split.to!(int[]); auto N = nk[0]; auto K = nk[1]; foreach (i, x; readln.split.to!(int[])) { XS[i] = x; } int l, r = K - 1, min_res = int.max; while (r < N) { auto ld = XS[l]; auto rd = XS[r]; int res; if ((ld < 0) == (rd < 0)) { ld = abs(ld); rd = abs(rd); res = max(ld, rd); } else { ld = abs(ld); rd = abs(rd); res = ld > rd ? rd * 2 + ld : ld * 2 + rd; } min_res = min(min_res, res); ++l; ++r; } writeln(min_res); }
D
import core.bitop, std.bitmanip; import core.checkedint; import std.algorithm, std.functional; import std.array, std.container; import std.bigint; import std.conv; import std.math, std.numeric; import std.range, std.range.interfaces; import std.stdio, std.string; import std.typecons; void main() { auto s = readln.chomp; int[] sinks; sinks ~= 0; foreach (i; 0 .. s.length - 1) { if (s[i] == 'R' && s[i+1] == 'L') { sinks ~= i.to!int; } } sinks ~= s.length.to!int; debug { sinks.writeln; } auto ans = new int[] (s.length); int j = 0; foreach (i; 0 .. s.length) { if (i > sinks[j+1]) { ++j; } int goToSink = s[i] == 'L' ? sinks[j] : sinks[j+1]; int dist = i.to!int - goToSink; int finalField = dist % 2 == 0 ? goToSink : goToSink + 1; ans[finalField] += 1; debug { writeln(i, ' ', j, ' ', finalField); } } foreach (i; 0 .. s.length) { if (i > 0) { write(" "); } write(ans[i]); } writeln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto K = RD; auto x = RDR.ARR; long ans; foreach (i; 0..N) { ans += min(x[i], abs(x[i] - K)) * 2; } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; immutable long MOD = 998244353; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!long).array; foreach (i; 0..N-1) (A[i+1] += A[i]) %= MOD; long a = 1; long b = 0; long ans = 0; foreach_reverse (i; 0..N) { (ans += A[i] * a % MOD) %= MOD; a = a * 2 % MOD; if (b > 0) a += powmod(2, b-1, MOD); a %= MOD; b += 1; } ans.writeln; } long powmod(long a, long x, long m) { long ret = 1; while (x) { if (x % 2) ret = ret * a % m; a = a * a % m; x /= 2; } return ret; }
D