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; void main() { auto N = readln.chomp.to!int; auto S = readln.chomp; int c; foreach (i; 0..N-2) { if (S[i..i+3] == "ABC") ++c; } writeln(c); }
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]; auto MAP = new char[][](N.to!size_t, M.to!size_t); foreach (i; 0..N) foreach (j, c; readln.chomp) MAP[i][j] = c; bool rb, cb; foreach (i; 0..N) { bool rrb = true; foreach (j; 0..M) if (MAP[i][j] == '#') { rrb = false; break; } if (rrb) { rb = true; break; } } foreach (j; 0..M) { bool ccb = true; foreach (i; 0..N) if (MAP[i][j] == '#') { ccb = false; break; } if (ccb) { cb = true; break; } } if (rb != cb) { writeln(-1); return; } foreach (i; 0..N) { bool cd; if (MAP[i][0] == '#') cd = true; foreach (j; 1..M) { if (MAP[i][j-1] == '.' && MAP[i][j] == '#') { if (cd) { writeln(-1); return; } else { cd = true; } } } } foreach (j; 0..M) { bool cd; if (MAP[0][j] == '#') cd = true; foreach (i; 1..N) { if (MAP[i-1][j] == '.' && MAP[i][j] == '#') { if (cd) { writeln(-1); return; } else { cd = true; } } } } auto cs = new int[][](N.to!size_t, M.to!size_t); int c; foreach (i; 0..N) foreach (j; 0..M) if (MAP[i][j] == '#' && cs[i][j] == 0) { void paint(int i, int j) { foreach (d; [[1,0], [0,1], [-1,0], [0,-1]]) { auto ii = i+d[0]; auto jj = j+d[1]; if (ii < 0 || ii >= N || jj < 0 || jj >= M || MAP[ii][jj] == '.' || cs[ii][jj] != 0) continue; cs[ii][jj] = c; paint(ii, jj); } } ++c; cs[i][j] = c; paint(i, j); } writeln(c); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; enum A = "AKIHABARA"; void main() { auto S = readln.chomp; size_t i, j; while (i < S.length && j < A.length) { if (S[i] == A[j]) { ++i; ++j; } else if (A[j] == 'A') { ++j; } else { writeln("NO"); return; } } writeln((i == S.length && j >= A.length-1) ? "YES" : "NO"); }
D
import std.stdio; import std.conv; import std.string; int main() { string str = chomp(readln); int val = to!(int)(str); int h = val/3600; val %= 3600; int m = val/60; val %= 60; int s = val; writeln(h, ":", m, ":", s); return 0; }
D
/+ dub.sdl: name "C" dependency "dunkelheit" version=">=0.9.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; int main() { Scanner sc = new Scanner(stdin); scope(exit) assert(!sc.hasNext); int[3] x; sc.read(x); x[].sort!"a%2<b%2"; int ans = 0; if (x[0]%2 != x[1]%2) { ans++; x[1]++; x[2]++; } else if (x[1]%2 != x[2]%2) { ans++; x[0]++; x[1]++; } ans += (3*max(x[0], x[1], x[2]) - x[].sum) / 2; writeln(ans); 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.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { string s; scan(s); int n = s.length.to!int; auto a = new int[](n + 1); foreach (i ; 0 .. n) { a[i + 1] = a[i] ^ (1<<(s[i] - 'a')); } debug { writeln(a); } auto opt = new int[](n + 1); auto dp = new int[](1<<26); dp[] = n; dp[0] = 0; foreach (i ; 1 .. n + 1) { int tmp = dp[a[i]]; foreach (j ; 0 .. 26) { tmp = min(tmp, dp[a[i] ^ (1<<j)]); } opt[i] = tmp + 1; dp[a[i]] = min(dp[a[i]], opt[i]); } debug { writeln(opt); } writeln(opt[n]); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.array; import std.math; import std.conv; void main(){ string str; while((str = readln()).length != 0){ string[] input = split(str); int a = to!int(input[0]); int b = to!int(input[1]); writeln(digit(a + b)); } } uint digit(in int num){ if(num <= 0) return 0; else return cast(uint)log10(num) + 1; }
D
import std.conv, std.stdio; import std.algorithm, std.array, std.range, std.string; import std.numeric; void main() { auto buf = readln.chomp.split.to!(int[]), b = readln.chomp.split.to!(int[]); size_t answer; foreach (i; 0..buf[0]) if (-buf[2] < readln.chomp.split.to!(int[]).dotProduct(b)) answer += 1; answer.writeln; }
D
/+ dub.sdl: name "C" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; int main() { auto sc = new Scanner(stdin); int n, m, q; sc.read(n, m, q); int Y = n*2 - 1; int X = m*2 - 1; int[][] mp = new int[][](Y, X); foreach (i; 0..n) { string s; sc.read(s); foreach (j; 0..m) { mp[i*2][j*2] = s[j] - '0'; } } foreach (i; 0..Y) { foreach (j; 0..X) { if (i % 2 && j % 2) continue; if (i % 2 == 0 && j % 2 == 0) continue; if (i % 2 == 1) { if (mp[i-1][j] && mp[i+1][j]) mp[i][j] = -1; } if (j % 2 == 1) { if (mp[i][j-1] && mp[i][j+1]) mp[i][j] = -1; } } } int[][] sm = new int[][](Y+1, X+1); foreach (i; 0..Y) { foreach (j; 0..X) { sm[i+1][j+1] = sm[i][j+1] + sm[i+1][j] + mp[i][j] - sm[i][j]; } } foreach (i; 0..q) { int y1, x1, y2, x2; sc.read(y1, x1, y2, x2); y1 = (y1-1)*2; x1 = (x1-1)*2; y2 = (y2-1)*2 + 1; x2 = (x2-1)*2 + 1; writeln(sm[y2][x2] - sm[y1][x2] - sm[y2][x1] + sm[y1][x1]); } return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } version (X86) static if (__VERSION__ < 2071) { import core.bitop : bsf, bsr, popcnt; int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* 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) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } }
D
import std.stdio; import std.algorithm; import std.array; import std.conv; import std.datetime; import std.numeric; import std.math; import std.string; string my_readln() { return chomp(readln()); } void main() {//try{ auto tokens = split(my_readln()); auto N = to!ulong(tokens[0]); ulong[] a, b, c; foreach (i; 0..N) { auto tokens2 = split(my_readln()); { a ~= to!ulong(tokens2[0]); b ~= to!ulong(tokens2[1]); c ~= to!ulong(tokens2[2]); } } ulong[3][] bn; bn.length = N; bn[0][0] = a[0]; bn[0][1] = b[0]; bn[0][2] = c[0]; foreach (i; 0..N-1) { bn[i+1][0] = max(bn[i][1], bn[i][2]) + a[i+1]; bn[i+1][1] = max(bn[i][0], bn[i][2]) + b[i+1]; bn[i+1][2] = max(bn[i][0], bn[i][1]) + c[i+1]; } writeln(max(max(bn[N-1][0], bn[N-1][1]), bn[N-1][2])); stdout.flush(); /*}catch (Throwable e) { writeln(e.toString()); } readln();*/ }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); auto A = aryread(); // long x = A.reduce!lcm - 1; // A.map!(y => x % y).writeln(); A.map!"a-1"().sum().writeln(); } T lcm(T)(T a, T b) { return (a * b) / gcd(a, b); }
D
import std.stdio, std.string, std.ascii; void main() { string s = readln.chomp; writeln( (s.length >= 6 && s.count!(isDigit) >= 1 && s.count!(isUpper) >= 1 && s.count!(isLower) >= 1) ? "VALID" : "INVALID" ); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { long K, S; scan(K, S); long ans; foreach (x; 0 .. K + 1) foreach (y; 0 .. K + 1) { if (x + y <= S && S - (x + y) <= K) { ans++; } } writeln(ans); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(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 K = RD; string s = "ACL"; string ans; foreach (i; 0..K) ans ~= s; writeln(ans); stdout.flush; debug readln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; long P = 10^^9+7; long[10^^6*2+50] F, RF; 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; } long inv(long x) { return pow(x, P-2); } /** * P を法とする階乗、逆元を予め計算しておく。 */ void init() { F[0] = F[1] = 1; foreach (i, ref x; F[2..$]) x = (F[i+1] * (i+2)) % P; { RF[$-1] = 1; auto x = F[$-1]; auto k = P-2; while (k) { if (k%2 == 1) RF[$-1] = (RF[$-1] * x) % P; x = x^^2 % P; k /= 2; } } foreach_reverse(i, ref x; RF[0..$-1]) x = (RF[i+1] * (i+1)) % P; } void main() { init(); auto xy = readln.split.to!(int[]); auto X = xy[0]; auto Y = xy[1]; foreach (i; 0..10^^6) { auto x = X - i*2; auto y = Y - i; if (x < 0 || y < 0 || x*2 != y) continue; auto j = y/2; writeln(F[i+j] * RF[i] % P * RF[j] % P); return; } writeln(0); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { long n, m; scan(n, m); if (n > m) swap(n, m); if (n == 1 && m == 1) { writeln(1); } else if (n == 1) { writeln(m - 2); } else { writeln((n-2)*(m-2)); } } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.conv; import std.array; import std.string; import std.algorithm; void main() { int n; n = to!int(chomp(readln())); int res = 0; for(int i = 0; i < n; i++) { string [] t; int l, r; t = chomp(readln()).split(); l = to!int(t[0]); r = to!int(t[1]); res += r-l+1; } writeln(res); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; int N, M; string[100] NS, MS; void main() { N = readln.chomp.to!int; foreach (i; 0..N) NS[i] = readln.chomp; M = readln.chomp.to!int; foreach (i; 0..M) MS[i] = readln.chomp; auto ns = NS[0..N]; auto ms = MS[0..M]; int max_p; foreach (s; ns) { int p; foreach (n; ns) if (n == s) ++p; foreach (m; ms) if (m == s) --p; max_p = max(max_p, p); } writeln(max_p); }
D
void main() { long n = readln.chomp.to!long; long[] a = readln.split.to!(long[]); long mod = 10 ^^ 9 + 7; long total; foreach (i; 0 .. 61) { long cnt; foreach (j; 0 .. n) { if ((1L << i) & a[j]) ++cnt; } long mul = (1L << i) % mod; mul *= cnt * (n - cnt) % mod; total = (total + mul) % mod; } total.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 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; } } // }}} // 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 a, b, n; cin.scan(a, b, n); long maxi; if (n < b) { maxi = a * n / b; } else { maxi = a * (b - 1) / b; auto t = min(b * 2 - 1, n); maxi = max(maxi, a * t / b - a * (t / b)); } writeln(maxi); }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; int calc(char[] s, char mas) { int n = s.length.to!int; if (s.count(mas) == n) return 0; char[] t = new char[n-1]; foreach (i; 0..n-1) { t[i] = s[i]; if (s[i+1] == mas) t[i] = s[i+1]; } return 1+calc(t, mas); } int main() { auto sc = new Scanner(stdin); char[] s; sc.read(s); int n = s.length.to!int; int ans = 100; foreach (c; 'a'..cast(char)('z'+1)) { if (s.count(c) == 0) continue; ans = min(ans, calc(s, c)); } writeln(ans); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } version (X86) static if (__VERSION__ < 2071) { import core.bitop : bsf, bsr, popcnt; int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* 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) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } }
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() { auto S = sread(); long K = lread(); bool[string] d; foreach (i; 1 .. min(S.length + 1, K * 2)) foreach (j; 0 .. S.length - i + 1) { d[S[j .. j + i]] = true; } writeln(d.keys.sort()[K - 1]); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string; int[500] CS; int[500] SS; int[500] FS; void main() { auto N = readln.chomp.to!int; foreach (i; 0..N-1) { auto csf = readln.split.to!(int[]); CS[i] = csf[0]; SS[i] = csf[1]; FS[i] = csf[2]; } foreach (s; 0..N-1) { int t = 0; foreach (i; s..N-1) { if (t <= SS[i]) { t = SS[i] + CS[i]; } else { auto d = t % FS[i]; t += (FS[i] - t % FS[i]) % FS[i] + CS[i]; } } writeln(t); } writeln(0); }
D
void main() { long i, o, t, j, l, s, z; rdVals(i, o, t, j, l, s, z); long k = o; if (i && j && l && (i & 1) + (j & 1) + (l & 1) >= 2) { k += 3; --i, --j, --l; } k += i / 2 * 2; k += j / 2 * 2; k += l / 2 * 2; k.writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; void main() { string str = readln(); write(toUpper(str)); }
D
import std.stdio; void main(){ int n = 1000; while(n--) writeln("Hello World"); }
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 rgb = RDR.ARR; writeln((rgb[0]*100 + rgb[1]*10 + rgb[2]) % 4 == 0 ? "YES" : "NO"); stdout.flush(); debug readln(); }
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 n = lread(); auto a = aryread(); auto b = aryread(); auto c = aryread(); long cost, prev = INF; foreach (e; a) { cost += b[e - 1]; if (e == prev + 1) cost += c[prev - 1]; prev = e; } cost.writeln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto s = RD!(char[]); auto K = RD; foreach (i, c; s) { auto num = c - 'a'; if (num == 0) continue; if (26-num > K) continue; s[i] = 'a'; K -= 26-num; } if (K != 0) { auto num = s[$-1] - 'a'; debug writeln(num, " ", K, " ", ((num+K)%26)); s[$-1] = [cast(char)('a'+((num+K)%26))].to!string[0]; } writeln(s); stdout.flush(); debug readln(); }
D
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math; import core.stdc.stdio; // foreach, foreach_reverse, writeln void main() { int n; scanf("%d", &n); int[] a = new int[n]; int[] b = new int[n]; bool same = true; foreach (i; 0..n) { scanf("%d%d", &a[i], &b[i]); if (a[i] != b[i]) same = false; } if (same) { writeln(0); return; } long ans = 0; int[] x; foreach (i; 0..n) { if (a[i] <= b[i]) { ans += b[i]; } else { x ~= b[i]; } } x.sort; foreach (i; 1..x.length) { ans += x[i]; } writeln(ans); }
D
import std; enum inf(T)()if(__traits(isArithmetic,T)){return T.max/4;} T scan(T=long)(){return readln.chomp.to!T;} void scan(T...)(ref T args){auto input=readln.chomp.split;foreach(i,t;T)args[i]=input[i].to!t;} T[] scanarr(T=long)(){return readln.chomp.split.to!(T[]);} //END OF TEMPLATE void main(){ ulong n; ulong m; scan(n,m); auto h=scanarr!ulong; ulong[][] j; j.length=n; ulong a,b; foreach(i;0..m){ scan(a,b); j[a-1]~= h[b-1]; j[b-1]~= h[a-1]; } ulong ans; foreach(i;0..n){ if(j[i].filter!(hi=>h[i]<=hi).array.length==0) ans++; } ans.writeln; }
D
void main() { auto N = ri; auto S = rs; auto arr = new int[](N); arr[0] = S[0] == '.' ? 1 : 0; foreach(i; 1..N) arr[i] = S[i] == '.' ? 1 + arr[i-1] : arr[i-1]; ulong res = S.count('.'); foreach(i; 0..N) { res = min(res, (i+1 - arr[i]) + arr.back - arr[i]); } res.writeln; } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio; import std.string; void main() { string ans = "WA"; char[] s = readln.strip.dup; if (s[0] == 'A') { ulong c = 0; foreach(i, e; s[0..$ - 1]) { if (e == 'C' && i > 1) { c = i; break; } } if (c > 1) { ans = "AC"; foreach(i, e; s) { if (i == 0 || i == c) continue; if (e < 97 || e > 122) { ans = "WA"; break; } } } } writeln(ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; alias C = Tuple!(int, "size", int, "p"); void solve(int N, int M) { auto CS = new int[](N); foreach (s; readln.split.to!(int[])[1..$]) CS[s-1] = 0; foreach (s; readln.split.to!(int[])[1..$]) CS[s-1] = 1; foreach (s; readln.split.to!(int[])[1..$]) CS[s-1] = 2; int s, min_s, max_s = 3^^N-1; foreach_reverse (c; CS) { s *= 3; s += c; } if (s == min_s || s == max_s) { writeln(0); return; } auto memo = new bool[](3^^N); memo[s] = true; auto ss = [s]; foreach (cnt; 1..M+1) { int[] nss; foreach (x; ss) { int a = -1, b = -1, c = -1; foreach (i; 0..N) { CS[i] = x%3; x /= 3; switch (CS[i]) { case 0: a = i.to!int; break; case 1: b = i.to!int; break; default: c = i.to!int; } } int add() { int y; foreach_reverse (x; CS) { y *= 3; y += x; } return y; } int r = -1; if (a > b) { CS[a] = 1; r = add(); CS[a] = 0; } else if (a < b) { CS[b] = 0; r = add(); CS[b] = 1; } if (r != -1 && !memo[r]) { if (r == min_s || r == max_s) { writeln(cnt); return; } nss ~= r; memo[r] = true; } r = -1; if (b > c) { CS[b] = 2; r = add(); CS[b] = 1; } else if (b < c) { CS[c] = 1; r = add(); CS[c] = 2; } if (r != -1 && !memo[r]) { if (r == min_s || r == max_s) { writeln(cnt); return; } nss ~= r; memo[r] = true; } } ss = nss; } writeln(-1); } void main() { for (;;) { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; if (N == 0 && M == 0) break; solve(N, M); } }
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!(long[]); auto A = l[0]; auto B = l[1]; auto C = l[2]; auto D = l[3]; auto n = B/C + B/D - B/(C*D/gcd(C,D)); auto m = (A-1)/C + (A-1)/D - (A-1)/(C*D/gcd(C,D)); writeln((B-A+1) - (n-m)); }
D
import std.stdio,std.conv,std.string,std.array; void main(){ auto x=readln.chomp.split; int a = to!int(x[0]), b = to!int(x[1]); if(a<b){writeln("a < b");} if(a>b){writeln("a > b");} if(a==b){writeln("a == b");} }
D
import std.stdio, std.string, std.algorithm, std.range, std.conv, std.math; void main() { auto NK = readln.chomp.split.map!(to!int); auto N = NK[0]; auto K = NK[1]; (K * pow(K - 1, N - 1)).writeln; }
D
import std.stdio; import std.string; import std.range; //standard input/output void main() { auto S = readln.chomp; int res = 700; if(S[0] == 'o') res += 100; if(S[1] == 'o') res += 100; if(S[2] == 'o') res += 100; writeln(res); /* int res = 700; foreach(i; S) if(i == 'o') res += 100; writeln(res); */ /* int res = 700; res += 100 * S.count('o'); writeln(res); */ }
D
import std.string; import std.stdio; import std.conv; import std.algorithm; import std.range; int n,s; long[1001][11] dp; void main(){ dp[0][0] = 1; for(int i=0;i<=100;i++){ for(int j=9;j>=1;j--){ for(int k=0;k<=1000;k++){ if(0<=i+k&&i+k<=1000) dp[j][i+k] += dp[j-1][k]; } } } while(true){ auto ss = split(readln()); n = to!int(ss[0]); s = to!int(ss[1]); if(n == 0 && s == 0) break; writeln(dp[n][s]); } }
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 S = N.iota.map!(_ => readln.chomp).array; auto trie = new Trie!(string)(); foreach (s; S) trie.insert(s); trie.dfs1(trie.root); auto Q = readln.chomp.to!int; while (Q--) { auto x = readln.split; auto idx = x[0].to!int - 1; auto dict = x[1].to!string; auto order = new int[](26); foreach (i, a; dict) order[a-'a'] = i.to!int; trie.dfs2(trie.root, S[idx], 1, order).writeln; } } class Trie(T) { class Node { Edge[] edges; bool terminal; int data; this(bool terminal, int data = 0) { this.terminal = terminal; this.data = data; } } class Edge { Node to; T label; this(Node to, T label) { this.to = to; this.label = label; } } Node root; this() { root = new Node(false); } bool lookup(T str) { return lookup(root, str); } private bool lookup(Node node, T str) { if (node.terminal && str.empty) return true; foreach (e; node.edges) { if (str.startsWith(e.label)) { return lookup(e.to, str[e.label.length..$]); } } return false; } void insert(T str) { if (lookup(str)) return; insert(root, str); } private void insert(Node node, T str) { if (str.empty) { node.edges ~= new Edge(new Node(true, 1), str); return; } foreach (e; node.edges) { int common_prefix = -1; foreach (i; 0..min(e.label.length, str.length)) { if (e.label[i] != str[i]) break; common_prefix = i.to!int; } if (common_prefix == -1) continue; if (common_prefix + 1 == e.label.length) { insert(e.to, str[e.label.length..$]); return; } auto new_node = new Node(false); auto new_label = e.label[common_prefix+1..$].dup.to!T; new_node.edges = [new Edge(e.to, new_label)]; e.to = new_node; e.label = e.label[0..common_prefix+1].dup; insert(new_node, str[common_prefix+1..$]); return; } auto new_node = new Node(false); node.edges ~= new Edge(new_node, str); insert(new_node, str[$..$]); } void dfs1(Node node) { foreach (e; node.edges) { dfs1(e.to); node.data += e.to.data; } } int dfs2(Node node, T str, int sm, int[] order) { if (str.empty) return sm; Node next_node; int common_prefix = -1; foreach (e; node.edges) { if (e.label.empty) { sm += 1; continue; } int x = order[e.label.front - 'a']; int y = order[str.front - 'a']; if (x < y) { sm += e.to.data; } else if (x == y) { next_node = e.to; common_prefix = e.label.length.to!int; } } return dfs2(next_node, str[common_prefix..$], sm, order); } }
D
void main() { string n = rdStr; long len = n.length; long total; foreach (x; n) { total += x - '0'; } max(total, (n[0]-'1')+9*(len-1)).writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
void main() { long n, q; rdVals(n, q); bool[] ball = new bool[n]; ball[0] = ball[1] = true; long now; foreach (i; 0 .. q) { long a, b; rdVals(a, b); --a, --b; if (now == a) now = b; else if (now == b) now = a; swap(ball[a], ball[b]); if (now) ball[now-1] = true; if (now < n - 1) ball[now+1] = true; } ball.count(true).writeln; } 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
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto M = RD; auto edges = new long[][](N); //auto edges2 = new long[][](N); foreach (i; 0..M) { auto u = RD-1; auto v = RD-1; edges[u] ~= v; //edges2[v] ~= u; } auto S = RD-1; auto T = RD-1; /*auto d = new bool[3][](N); { d[S][0] = true; long[2][] open = [[S, 0]]; while (!open.empty) { auto n = open.back; open.popBack; auto from = n[0]; auto c = n[1]; foreach (to; edges[from]) { auto nc = (c+1) % 3; if (d[to][nc]) continue; d[to][nc] = true; open ~= [to, nc]; } } }*/ long ans; auto cost = new long[][](N, 3); foreach (ref e; cost) e.fill(long.max); cost[S][0] = 0; long[2][] open = [[S, 0]]; while (!open.empty) { auto n = open.front; open.popFront; auto from = n[0]; auto c = n[1]; foreach (to; edges[from]) { auto nc = c + 1; auto cnt = nc % 3; //if (d[to][pc] == false) continue; if (nc < cost[to][cnt]) { cost[to][cnt] = nc; open ~= [to, nc]; } } } debug writeln(cost); if (cost[T][0] == long.max) { writeln(-1); } else { writeln(cost[T][0] / 3); } stdout.flush(); debug readln(); }
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; import std.container : DList; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { auto deque = Deque!int(400_000 * 3); int q = readint; for (int i = 0; i < q; i++) { auto xs = readints; switch (xs[0]) { case 0: if (xs[1] == 0) deque.insertFront(xs[2]); else deque.insertBack(xs[2]); break; case 1: writeln(deque[xs[1]]); break; case 2: if (xs[1] == 0) deque.removeFront(); else deque.removeBack(); break; default: break; } } } struct Deque(T) { private T[] _buffer; private int _front; private int _back; this(int size) { _buffer = new int[size]; _front = _back = size / 2; } bool empty() @property { return _front == _back; } int length() @property { return _back - _front; } void insertFront(T value) { if (empty) { _buffer[_front] = value; _back++; return; } _buffer[--_front] = value; } void insertBack(T value) { if (empty) { _buffer[_front] = value; _back++; return; } _buffer[_back++] = value; } void removeFront() { assert(!empty); ++_front; } void removeBack() { assert(!empty); --_back; } T opIndex(int index) { return _buffer[_front + index]; } }
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 i = readNums!int; writeln(i.sum - i.reduce!max); }
D
void main() { problem(); } void problem() { auto N = scan!ulong; ulong solve() { // Sum_{k=1..n} k/2 * floor(n/k) * floor(1 + n/k) ulong ans; foreach(k; 1..N+1) { ulong x = N / k; ans += x * (2*k + (x - 1) * k) / 2; } return ans; } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Queue = Tuple!(long, "from", long, "to"); // -----------------------------------------------
D
void main() { auto N = ri; auto A = N.iota.map!(v => ri).array; auto arr = new int[](N), arr2 = new int[](N); arr[0] = A[0]; arr2[$ - 1] = A[$ - 1]; foreach(i; 1..N) { arr[i] = max(arr[i-1], A[i]); } foreach_reverse(i; 0..N-1) { arr2[i] = max(arr2[i+1], A[i]); } debug arr.writeln; debug arr2.writeln; writeln(arr2[1]); foreach(i; 1..N-1) { writeln(max(arr[i-1], arr2[i+1])); } writeln(arr[$ - 2]); } // =================================== 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[] tmp = readln.split.to!(int[]); int x = tmp[0], a = tmp[1], b = tmp[2]; if (a - b >= 0) { "delicious".writeln; } else if (a - b + x >= 0) { "safe".writeln; } else { "dangerous".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.algorithm; void main(){ auto ip = readln.split.to!(int[]); if(ip[1] - ip[0] == ip[2] - ip[1]) "YES".writeln; else "NO".writeln; }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; void main() { int[] buf = readln.chomp.split.to!(int[]); int a = buf[0], b = buf[1]; int ans = a - 1 + (a <= b ? 1 : 0); writeln(ans); }
D
/+ dub.sdl: name "C" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; int main() { auto sc = new Scanner(stdin); int n, m; int[] a; sc.read(n, m, a); a[] -= 1; int[] cnt = new int[n]; a.each!(x => cnt[x]++); int[] buf = new int[n]; int zc = n; void add(int x, int c) { if (x < 0) return; if (buf[x] == 0) zc--; buf[x] += c; if (buf[x] == 0) zc++; } foreach (i; 0..n) { foreach (j; 0..cnt[i]) { add(i-j, 1); } } foreach (i; 0..m) { int x, y; sc.read(x, y); x--; y--; int z = a[x]; a[x] = y; //z -> y cnt[z]--; add(z-cnt[z], -1); add(y-cnt[y], 1); cnt[y]++; debug writeln(buf); writeln(zc); } return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } version (X86) static if (__VERSION__ < 2071) { import core.bitop : bsf, bsr, popcnt; int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* 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) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } }
D
import std.stdio; import std.string; import std.conv; import std.range; import std.array; import std.algorithm; void main() { string input = "4823108376"; while ((input = readln.chomp).length != 0) { while (input.length > 1) { string output; for (int i = 0; i < input.length-1; i++) { output ~= (((input[i] - '0')+(input[i+1] - '0'))%10).to!string; } input = output; } writeln(input); } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { long n, a, b, c, d, e; scan(n);scan(a);scan(b);scan(c);scan(d);scan(e); auto x = min(a, b, c, d, e); auto ans = (n - 1) / x + 5; writeln(ans); } int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new int[][](n, 0); foreach (i; 0 .. m) { int u, v; scan(u, v); if (is1indexed) { u--, v--; } adj[u] ~= v; if (isUndirected) { adj[v] ~= u; } } return adj; } alias Edge = Tuple!(int, "to", int, "cost"); Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new Edge[][](n, 0); foreach (i; 0 .. m) { int u, v, c; scan(u, v, c); if (is1indexed) { u--, v--; } adj[u] ~= Edge(v, c); if (isUndirected) { adj[v] ~= Edge(u, c); } } return adj; } void yes(bool b) { writeln(b ? "Yes" : "No"); } void YES(bool b) { writeln(b ? "YES" : "NO"); } T[] readArr(T)() { return readln.split.to!(T[]); } T[] readArrByLines(T)(int n) { return iota(n).map!(i => readln.chomp.to!T).array; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
unittest { assert( [ "01B0" ].parse.expand.solve == "00" ); assert( [ "0BB1" ].parse.expand.solve == "1" ); } 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 ) { auto ans = ""; foreach( c; s ) { if( c == 'B' ) { if( 0 < ans.length ) ans.length--; } else { ans ~= c; } } return ans; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N, K; scan(N, K); writeln(N - K + 1); }
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 Pair = Tuple!(long, "flag", long, "num"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } auto dp = new long[100_100]; void main() { auto n = lread(); auto h = aryread(); dp[1] = abs(h[1] - h[0]); foreach (i; iota(2, n)) { dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2])); } dp[n - 1].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; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { int r, d, x; scan(r, d, x); for (int i = 0; i < 10; i++) { x = r * x - d; writeln(x); } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; void main() { auto z = readln.strip.splitter.map! (i => i.to!int).array; int h = z[0]; int w = z[1]; string [] a = new string[h]; foreach (t; 0 .. h) { a[t] = readln[0 .. w]; } int s = 0; auto x = new int[w]; auto order = new int[w]; int k = 0, i = 0, j = w - 1; while (i < j) { order[k++] = i; order[k++] = j; i++;j--; } if (k < w) order[k++] = i; bool cmp (int i, int j, int l) { foreach (k; 0 .. l) { if (a[i][x[k]] != a[j][x[w-1-k]]) { return false; } } return true; } bool check (int l) { int st = 0; int t = h; while (t > 1) { int old_t = t; loop: foreach (i; 0 .. h) if (!(st & (1 << i))) { foreach (j; i + 1 .. h) if (!(st & (1 << i))) { if ((l == w) ? cmp (i, j, l) : cmp (i, j, l / 2) && cmp (j, i, l / 2)) { st |= (1 << i) | (1 << j); t -= 2; break loop; } } } if (old_t == t) return false; } if (t == 1) { foreach (i; 0 .. h) if (!(st & (1 << i))) { return (l == w) ? cmp (i, i, l) : cmp (i, i, l / 2); } } return true; } bool go (int k, int st) { if (k == w) { return check (w); } if (!(k & 1)) { if (!check (k)) return false; } foreach (i; 0 .. w) { if (!(st & (1 << i))) { x[order[k]] = i; if (go (k + 1, st | (1 << i))) return true; } } return false; } if (go (0, 0)) { writeln ("YES"); } else { writeln ("NO"); } }
D
import std.stdio; import std.conv; import std.string; void main() { string input = chomp(readln()); int output = to!(int)(input); output = output*output*output; writeln(output); }
D
/+ dub.sdl: name "B" dependency "dunkelheit" version=">=0.9.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; int main() { Scanner sc = new Scanner(stdin); scope(exit) sc.read!true; int k; long[] a; sc.read(k, a); long dw = 2, up = 2; foreach_reverse(d; a) { long mi = (dw + d - 1) / d * d; dw = max(dw, mi); long ma = up / d * d; up = min(up, ma); if (up < dw) { writeln("-1"); return 0; } up += d-1; } writeln(dw, " ", up); 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(bool enforceEOF = false, T, Args...)(ref T x, auto ref Args args) { import std.exception; enforce(readSingle(x)); static if (args.length == 0) { enforce(enforceEOF == false || !succ()); } else { read!enforceEOF(args); } } void read(bool enforceEOF = false, Args...)(auto ref Args args) { import std.exception; static if (args.length == 0) { enforce(enforceEOF == false || !succ()); } else { enforce(readSingle(args[0])); read!enforceEOF(args); } } } /* 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 core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; alias Score = Tuple!(long, "p", long, "c"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { auto n = lread(); auto a = aryread(); auto z = new long[](1); a = z ~ a ~ z; auto changes = new long[](n); long cost; foreach (i; iota(1, n + 1)) { changes[i - 1] = abs(a[i + 1] - a[i - 1]) - abs(a[i - 1] - a[i]) - abs(a[i] - a[i + 1]); cost += abs(a[i] - a[i - 1]); } cost += abs(a[$ - 2]); foreach(e; changes) { writeln(cost + e); } }
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 a, b; scan(a, b); if (13 <= a) { writeln(b); } else if (6 <= a) { writeln(b / 2); } else { writeln(0); } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int N; scan(N); auto H = readln.split.to!(int[]); auto ans = new int[](N); foreach (i ; 1 .. N) { if (H[i - 1] >= H[i]) ans[i] = ans[i - 1] + 1; else ans[i] = 0; } writeln(ans.reduce!max); } 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.string, std.conv; import std.algorithm; void main() { auto input = getStdin!(string[]); string s = input[0] ~ input[0]; string p = input[1]; if (s.count(p) > 0) { "Yes".writeln; } else { "No".writeln; } } T getStdin(T)() { string[] cmd; string line; while ((line = chomp(stdin.readln())) != "") cmd ~= line; return to!(T)(cmd); }
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 Mame = Tuple!(long, "point", long, "number"); 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(); bool easy = true; foreach (i; iota(s.length)) { if (i % 2) { if (s[i] == 'R') easy = false; } else { if (s[i] == 'L') easy = false; } // (i + 1).writeln(" ", s[i]); } if (easy) writeln("Yes"); else writeln("No"); }
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, a, b; scan(n, a, b); writeln(min(n*a, b)); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; immutable long MOD = 10^^9 + 7; void main() { auto N = readln.chomp.to!int; auto Comb = new Combination(); if (N == 2) { writeln(1); return; } if (N == 3) { writeln(4); return; } long ans = 0; auto steps = new long[](N+1); foreach (k; 2..N+1) { steps[k] = Comb.comb(k-1, N-k-1) * Comb.fact(k) % MOD * Comb.fact(N-k-1) % MOD; } foreach_reverse(k; 2..N+1) { if (steps[k] == 0) continue; steps[k] = steps[k] - steps[k-1]; steps[k] = (steps[k] % MOD + MOD) % MOD; ans = (ans + steps[k] * k % MOD) % MOD; } ans.writeln; } class Combination { immutable int MAX = 2*10^^6+1; immutable long MOD = 10^^9+7; long[] modinv; long[] f_mod; long[] f_modinv; this() { modinv = new long[](MAX); modinv[0] = modinv[1] = 1; foreach(i; 2..MAX) { modinv[i] = modinv[MOD.to!int % i] * (MOD - MOD / i) % MOD; } f_mod = new long[](MAX); f_modinv = new long[](MAX); f_mod[0] = f_mod[1] = 1; f_modinv[0] = f_modinv[1] = 1; foreach(i; 2..MAX.to!int) { f_mod[i] = (i * f_mod[i-1]) % MOD; f_modinv[i] = (modinv[i] * f_modinv[i-1]) % MOD; } } long comb(int n, int k) { if (n < 0 || k < 0 || n < k) return 0; return f_mod[n] * f_modinv[n-k] % MOD * f_modinv[k] % MOD; } long fact(int n) { if (n < 0) return 0; return f_mod[n]; } } 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
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric; import std.typecons, std.functional, std.traits; import std.algorithm, std.container; import core.stdc.stdlib, core.bitop; void main() { auto S = scanString; long res = long.max; if(S.all!"a=='0'"||S.all!"a=='1'"){ writeln(S.length); return; } foreach(i;0..S.length-1) { if(S[i]!=S[i+1]){ res = min(res, max(i+1, S.length-(i+1))); } } writeln(res); } enum MOD=10^^9+7L; class GridIter(T){ private const T[] grid; private size_t count; this(T[] g) { grid = g; } void popFront(){ count++; } bool empty(){ return count>=grid.length; } T front(){ return grid[count]; } } class Grid(T){ private T[] grid; size_t width, height; private this(){} this(size_t w, size_t h) { width = w; height = h; grid.length = width*height; } GridIter!T iter(){ return new GridIter!T(grid); } ref T opIndex(size_t x, size_t y) { return grid[x+width*y]; } ref T opIndex(E)(E e) if( isIntegral!(typeof(e.x)) && isIntegral!(typeof(e.y))) { return this[e.x, e.y]; } Grid!T opIndex(Tuple!(size_t, size_t) r1, Tuple!(size_t, size_t) r2) { auto res = new Grid!T(); auto startOffset = r1[0] + r2[0]*width; auto endOffset = r1[1] + (r2[1] - 1)*width; res.grid = grid[startOffset .. endOffset]; res.width = r1[1] - r1[0]; res.height = r2[1] - r2[0]; return res; } Grid!T opIndex(Tuple!(size_t, size_t) r1, size_t j) { return opIndex(r1, tuple(j, j+1)); } Grid!T opIndex(size_t i, Tuple!(size_t, size_t) r2) { return opIndex(tuple(i, i+1), r2); } Tuple!(size_t, size_t) opSlice(size_t dim)(size_t start, size_t end) if (dim == 0 || dim == 1) in { assert(start >= 0 && end <= this.opDollar!dim); } body{ return tuple(start, end); } size_t opDollar(size_t dim : 0)() { return width; } size_t opDollar(size_t dim : 1)() { return height; } override string toString(){ string res = "\n"; foreach_reverse(line; 0..height) { res ~= grid[line*width..(line+1)*width].to!string ~ '\n'; } return res; } } struct Node{ long[] connect; } struct Data2{ long x,y; alias a=x, b=y; ref long opIndex(size_t i) { assert(i==0||i==1); return i==0?x:y; } } struct Data3{ long x,y,z; alias a=x, b=y, c=z; ref long opIndex(size_t i) { assert(i==0||i==1||i==2); return i==0?x:(i==1?y:z); } } long lcm(long a, long b) pure { return a * b / gcd(a, b); } class UnionFind{ UnionFind parent = null; void merge(UnionFind a) { if(same(a)) return; a.root.parent = this.root; } UnionFind root() { if(parent is null)return this; return parent = parent.root; } bool same(UnionFind a) { return this.root == a.root; } } enum TermColor{ Blue, Red, Purple, Gray, Green, Brown, Default, } string termStr(TermColor c) pure { switch(c) { case TermColor.Blue: return "\033[38;2;122;158;194m"; case TermColor.Red: return "\033[38;2;204;130;66m"; case TermColor.Purple: return "\033[38;2;158;123;176m"; case TermColor.Gray: return "\033[38;2;106;135;89m"; case TermColor.Green: return "\033[38;2;165;194;97m"; case TermColor.Brown: return "\033[38;2;204;130;66m"; default: return "\033[0m"; } } void print(TermColor C = TermColor.Gray, T...)(T t) { debug stderr.write(C.termStr, t, TermColor.Default.termStr); } void println(TermColor C = TermColor.Gray, T...)(T t) { debug stderr.writeln(C.termStr, t, TermColor.Default.termStr); } void printfln(TermColor C = TermColor.Gray, T...)(T t) { debug stderr.writefln(C.termStr, t, TermColor.Default.termStr); } // debug{ // void write(T...)(T t) // { // stdout.write(TermColor.Blue.termStr, t, TermColor.Default.termStr); // } // void writeln(T...)(T t) // { // stdout.writeln(TermColor.Blue.termStr, t, TermColor.Default.termStr); // } // void writefln(T...)(T t) // { // stdout.writefln(TermColor.Blue.termStr, t, TermColor.Default.termStr); // } // } T[] scanArray(T = long)() { return readln.split.to!(T[]); } T scanElem(T = long)() { char[] res; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { res ~= cast(char) c; c = getchar; } return res.strip.to!T; } alias scanString = scanElem!string; template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } struct Factor { long n; long c; } //素因数分解 Factor[] factors(long n) pure { Factor[] res; for (long i = 2; i ^^ 2 <= n; i++) { if (n % i != 0) continue; long c; while (n % i == 0) { n = n / i; c++; } res ~= Factor(i, c); } if (n != 1) res ~= Factor(n, 1); return res; } //約数をすべて列挙 long[] divisors(long n) pure { long[] list; void func(Factor[] fs, long n) { if(fs.empty){ list ~= n; return; } foreach(c; 0..fs[0].c+1) { func(fs[1..$], n * (fs[0].n ^^ c)); } } func(factors(n), 1); sort(list); return list; } //nまでの素数のリスト long[] primes(long n) pure { if(n<2)return []; auto table = new long[n+1]; long[] res; for(long i = 2;i<=n;i++) { if(table[i]==-1) continue; for(long a = i;a<table.length;a+=i) { table[a] = -1; } res ~= i; } return res; } //素数判定 bool isPrime(long n) pure { if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (long i = 3; i ^^ 2 <= n; i += 2) if (n % i == 0) return false; return true; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "a", long, "b"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long h, w; scan(h, w); auto wall = new string[](h); foreach (ref e; wall) { e = sread(); } // wall.writeln(); bool ok = true; foreach (i; iota(h)) { foreach (j; iota(w)) { if (wall[i][j] == '#') { ok &= canpaint(wall, i, j); } } } if (ok) writeln("Yes"); else writeln("No"); } bool canpaint(ref string[] wall, long i, long j) { bool ret; // i.write(" "), j.writeln(); if (i - 1 >= 0) ret |= (wall[i - 1][j] == '#'); if (i + 1 < wall.length) ret |= (wall[i + 1][j] == '#'); if (j - 1 >= 0) ret |= (wall[i][j - 1] == '#'); if (j + 1 < wall[i].length) ret |= (wall[i][j + 1] == '#'); return ret; } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; int sum_p, max_p; foreach (_; 0..N) { auto p = readln.chomp.to!int; sum_p += p; max_p = max(max_p, p); } writeln(sum_p - max_p / 2); }
D
import std.stdio; import std.string; import std.conv; import std.array; import std.range; void main(){ auto F=readln.split.to!(int[]),A=F[0],B=F[1]; if(A+B>=24)writeln((A+B)-24); else writeln(A+B); }
D
import std.stdio; import std.conv; import std.string; import std.format; void main() { string s = chomp(readln()); string[] array = s.split(" "); int W = to!int(array[0]); int H = to!int(array[1]); int x = to!int(array[2]); int y = to!int(array[3]); int r = to!int(array[4]); // (0, 0) <= (x, y) <= (W, H) int left = x - r; int right = x + r; if ( (0 <= left && left <= W) && (0 <= right && right <= W)) { } else { writeln("No"); return; } int upper = y + r; int bottom = y - r; if ( (0 <= upper && upper <= H) && (0 <= bottom && bottom <= H)) { writeln("Yes"); } else { writeln("No"); } }
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; } int calc(int n, bool[][] adj) { bool isBridge() { auto uf = new UnionFind(n); int[] nodes = [0]; bool[int] used; while (nodes.length > 0) { int node = nodes[0]; nodes = nodes[1 .. $]; if (node in used) continue; used[node] = true; for (int i = 0; i < n; i++) { if (adj[node][i]) { uf.unite(node, i); nodes ~= i; } } } for (int i = 1; i < n; i++) { if (!uf.isSame(0, i)) { return true; } } return false; } int ans = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (adj[i][j]) { adj[i][j] = adj[j][i] = false; ans += isBridge(); adj[i][j] = adj[j][i] = true; } } } return ans; } void main() { auto nm = readints; int n = nm[0], m = nm[1]; auto adj = new bool[][](n, n); for (int i = 0; i < m; i++) { auto ab = readints; int a = ab[0] - 1, b = ab[1] - 1; adj[a][b] = adj[b][a] = true; } writeln(calc(n, adj)); } class UnionFind { private int[] _data; this(int n) { _data = new int[](n + 1); _data[] = -1; } int root(int a) { if (_data[a] < 0) return a; return _data[a] = root(_data[a]); } bool unite(int a, int b) { int rootA = root(a); int rootB = root(b); if (rootA == rootB) return false; _data[rootA] += _data[rootB]; _data[rootB] = rootA; return true; } bool isSame(int a, int b) { return root(a) == root(b); } int size(int a) { return -_data[root(a)]; } }
D
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array, std.functional, std.container, std.typecons; void main() { auto input = readln.split.to!(int[]); int H = input[0], W = input[1], A = input[2], B = input[3]; bool[] reversed = new bool[H]; foreach(i; 0..B) { reversed[i] = true; } foreach(i; 0..H) { foreach(j; 0..A) { write(reversed[i] ? 0 : 1); } foreach(j; A..W) { write(reversed[i] ? 1 : 0); } writeln; } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } //long mod = 10^^9 + 7; long mod = 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; } int binarySearch(alias pred, T)(in T[] arr, bool isLower = true) { int ok, ng; if (isLower) { ok = cast(int)arr.length; ng = -1; } else { ok = -1; ng = cast(int)arr.length; } while (abs(ok-ng) > 1) { auto mid = (ok+ng) / 2; if (unaryFun!pred(arr[mid])) ok = mid; else ng = mid; } return ok; } void main() { auto s = RD!string; auto t = RD!string; auto len = s.length; auto next = new long[][](26); foreach (i, c; s) { next[c-'a'] ~= i; } long i; long ans; bool f(long pos) { return pos >= i%len; } while (!t.empty) { auto n = next[t[0]-'a']; auto l = binarySearch!(f)(n); debug writeln(t[0], " ", i%len, " l:", l); if (l == n.length) { i += len - i%len; auto l2 = binarySearch!(f)(n); debug writeln(t[0], " ", i%len, " l2:", l2); if (l2 == n.length) { ans = -1; break; } else { i = (i / len) * len + n[l2] + 1; ans = i; } } else { i = (i / len) * len + n[l] + 1; ans = i; } t.popFront; debug writeln(ans); } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.string, std.algorithm, std.math, std.array, std.container, std.typecons; void main() { auto nm = readln.chomp.split.to!(int[]); if(nm[0]==nm[1]) writeln("Yes"); else writeln("No"); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto rd = readln.split.to!(long[]), x = rd[0], y = rd[1]; auto c = 1; for (;;) { x *= 2; if (x > y) break; ++c; } writeln(c); }
D
import std.stdio; import std.algorithm; import std.array; 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 = my_readln().split(); auto A = tokens[0].to!uint; auto B = tokens[1].to!uint; auto C = tokens[2].to!uint; writeln(A * B / 2); stdout.flush(); }
D
import std.stdio; import std.algorithm; import std.string; import std.range; import std.array; import std.conv; import std.complex; import std.math; import std.ascii; import std.bigint; import std.container; bool next(bool[][] m, int x, int y){ auto s = new bool[][](10+4, 10+4); foreach(i; -1..2){ foreach(j; -1..2){ s[x+i][y+j] = true; } } foreach(i; 0..10+4){ s[i][0] = false; s[i][1] = false; s[i][12] = false; s[i][13] = false; s[0][i] = false; s[1][i] = false; s[12][i] = false; s[13][i] = false; } auto t = new bool[][](14,14); foreach(i; 2..12) { foreach(j; 2..12) { if(m[i][j]){ t[i+2][j-1] = true; t[i+2][j] = true; t[i+2][j+1] = true; t[i-2][j-1] = true; t[i-2][j] = true; t[i-2][j+1] = true; t[i-1][j+2] = true; t[i][j+2] = true; t[i+1][j+2] = true; t[i-1][j-2] = true; t[i][j-2] = true; t[i+1][j-2] = true; } } } bool ret; foreach(i; 2..12) { foreach(j; 2..12) { m[i][j] = t[i][j] && s[i][j]; ret |= m[i][j]; } } return ret; } void main() { auto xy = map!(to!int)(readln().strip().split()); while(xy[0]|xy[1]){ auto m = new bool[][](10+4,10+4); m[xy[0]+2][xy[1]+2] = true; int n = to!int(readln().strip()); auto s = map!(to!int)(readln().strip().split()); bool f = true; foreach(i; 0..n){ f &= next(m, s[i*2]+2, s[i*2+1]+2); } writeln(f?"OK":"NA"); xy = map!(to!int)(readln().strip().split()); } }
D
import std.algorithm, std.array, std.container, std.range, std.bitmanip; import std.numeric, std.math, std.bigint, std.random, core.bitop; import std.string, std.conv, std.stdio, std.typecons; void main() { auto rd = readln.split.map!(to!int); auto a = rd[0], b = rd[1], c = rd[2]; auto d = 0; foreach (i; a..b + 1) if (c % i == 0) ++d; writeln(d); }
D
import std.stdio, std.string, std.conv, std.algorithm; void main() { int N = readln.chomp.to!(int); int[][] A; for (int i = 0; i < 2; i++) { A ~= readln.chomp.split.to!(int[]); } int ans; for (int i = 0; i < N; i++) { int t, row; for (int j = 0; j < N; j++) { if (j == i) { t += A[row][j]; row = 1; } t += A[row][j]; } ans = max(ans, t); } 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; void main() { auto a = new int[][](3, 3); foreach (i; 0..3) { a[i] = readln.chomp.split.to!(int[]); } auto n = readln.chomp.to!int; foreach (_; 0..n) { auto b = readln.chomp.to!int; foreach (i; 0..3) { foreach (j; 0..3) { if (a[i][j] == b) { a[i][j] = -1; } } } } auto f = false; foreach (i; 0..3) { int sum1, sum2, sum3, sum4; foreach (j; 0..3) { sum1 += a[i][j]; sum2 += a[j][i]; sum3 += a[j][j]; sum4 += a[j][2-j]; } if (sum1 == -3 || sum2 == -3 || sum3 == -3 || sum4 == -3) { f = true; } } if (f) { writeln("Yes"); } else { writeln("No"); } }
D
import std.conv, std.stdio; import std.algorithm, std.array, std.string, std.range; void main() { auto s = cast(ubyte[])readln.chomp; (s[2] == s[3] && s[4] == s[5] ? "Yes" : "No").writeln; }
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, std.typecons; // }}} // 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()() { } 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; } } // }}} void main() { auto cin = new Scanner; auto a = cin.nextArray!int(3); writeln(min(abs(a[1] - a[0]) + abs(a[2] - a[1]), abs(a[2] - a[1]) + abs(a[0] - a[2]), abs(a[0] - a[2]) + abs(a[1] - a[0]))); }
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; ulong idx = -1; bool matched; foreach(i; 0..(s.count-t.count)+1) { 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; } } 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, std.algorithm, std.conv, std.array, std.string, std.typecons; alias Edge = Tuple!(int, "to", int, "weight"); Edge[][10 ^^ 5 + 1] ES; long[10 ^^ 5 + 1] LS; void main() { auto N = readln.chomp.to!int; foreach (i; 0..N - 1) { auto e = readln.split.to!(int[]); ES[e[0]] ~= Edge(e[1], e[2]); ES[e[1]] ~= Edge(e[0], e[2]); } auto qk = readln.split.to!(int[]); auto Q = qk[0]; auto K = qk[1]; auto roots = [K]; LS[K] = 0; while (!roots.empty) { int[] nexts; foreach (r; roots) { foreach (e; ES[r]) { auto l = e.weight + LS[r]; if (e.to == K || (LS[e.to] != 0)) continue; LS[e.to] = l; nexts ~= e.to; } } roots = nexts; } foreach (_; 0..Q) { auto xy = readln.split.to!(int[]); writeln(LS[xy[0]] + LS[xy[1]]); } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp.to!int; auto s = readln.chomp.to!(char[]).array; auto k = readln.chomp.to!int; auto c = s[k-1]; foreach (ref e; s) { if (e != c) { e = '*'; } } s.writeln; }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { string s = readln.chomp; writeln(s[0 .. min($, 4)] == "YAKI" ? "Yes" : "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.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 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; } 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 S = RD!string; if (N % 2 == 1) writeln("No"); else { bool ans = true; foreach (i; 0..N/2) { if (S[i] != S[N/2+i]) ans = false; } writeln(ans ? "Yes" : "No"); } stdout.flush(); debug readln(); }
D
const int max = 1000000; void main(){ int s = _scan(); int[int] dic; dic[s]++; // 初項sでa_1, ... なので2から始まる foreach(i; 2..max+1){ if(s&1) s = 3*s +1; else s = s/2; dic[s]++; if(dic[s]==2){ i.writeln(); break; } } } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio, std.string, std.conv; import std.array, std.algorithm, std.range; string cnv(real w) { if(w<=48) return "light fly"; if(w<=51) return "fly"; if(w<=54) return "bantam"; if(w<=57) return "feather"; if(w<=60) return "light"; if(w<=64) return "light welter"; if(w<=69) return "welter"; if(w<=75) return "light middle"; if(w<=81) return "middle"; if(w<=91) return "light heavy"; return "heavy"; } void main() { stdin.byLine().map!(s=>cnv(s.to!real())).join("\n").writeln(); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; void main(){ auto N=readln.chomp.to!int; auto K=readln.chomp.to!int; auto X=readln.chomp.to!int; auto Y=readln.chomp.to!int; if(N>K)writeln(X*K+(N-K)*Y); else writeln(N*X); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.ascii; void main() { auto n = readln.chomp.to!int; auto a = readln.chomp.to!int; writeln(n*n-a); }
D
import std.stdio, std.math, std.conv; void main() { foreach(input; stdin.byLine) { foreach(n; 1..int.max) { if(9.8 * sqrt(5.0 / 4.9 * (n - 1)) >= input.to!double) { n.writeln; break; } } } }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = readln.chomp.to!size_t; auto a = readln.split.to!(int[]); auto calc(int fs) { auto t = 0, ans = 0L; foreach (i, ai; a) { auto s = i % 2 == 0 ? fs : -fs; t += ai; if (t * s <= 0) { ans += (s - t) * s; t = s; } } return ans; } writeln(min(calc(+1), calc(-1))); }
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; long x; scan(n, x); auto a = new long[](n + 1); // 長さ auto b = new long[](n + 1); // Pの枚数 a[0] = b[0] = 1; foreach (i ; 1 .. n + 1) { a[i] = 2L*a[i-1] + 3; b[i] = 2L*b[i-1] + 1; } long query(int L, long x) { if (L == 0) { return x == 1; } if (x <= 1) { return 0; } else if (x < (a[L] + 1) / 2) { return query(L-1, x - 1); } else if (x == (a[L] + 1) / 2) { return b[L-1] + 1; } else if (x < a[L] - 1) { return b[L-1] + 1 + query(L-1, x - (a[L] + 1) / 2); } else { return 2*b[L-1] + 1; } } long ans = query(n, x); writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { int h, w; scan(h, w); auto map = new char[][](h, w); iota(h).each!(i => map[i] = readln.chomp.to!(char[])); if (map[0][0] != '#') { writeln("Impossible"); return; } int pi, pj; while (!(pi == h-1 && pj == w-1)) { int ni = pi + 1, nj = pj; if (ni < h && map[ni][nj] == '#') { map[pi][pj] = '.'; pi = ni, pj = nj; continue; } ni = pi, nj = pj + 1; if (nj < w && map[ni][nj] == '#') { map[pi][pj] = '.'; pi = ni, pj = nj; continue; } writeln("Impossible"); return; } if (map[h-1][w-1] != '#') { writeln("Impossible"); return; } map[h-1][w-1] = '.'; debug { writefln("%(%-(%s %)\n%)", map); } foreach (i ; 0 .. h) { foreach (j ; 0 .. w) { if (map[i][j] == '#') { writeln("Impossible"); return; } } } writeln("Possible"); return; } 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.math, std.algorithm, std.array, std.string, std.conv, std.container, std.range; T[] Reads(T)() { return readln.split.to!(T[]); } alias reads = Reads!int; void scan(Args...)(ref Args args) { string[] ss = readln.split; foreach (i, ref arg ; args) arg = ss[i].parse!int; } uint MOD = 10^^9 + 7; pragma(inline) long mul(long x, long y) { return (x * (y % MOD)) % MOD; } long[] f; void make_table(int n) { f = new long[n+1]; f[0] = f[1] = 1; foreach (i ; 2..n+1) f[i] = mul(i, f[i-1]); } long powmod(S,T,U)(S x, T n, U MOD) { long r = 1; while (n>0) { if (n&1) r = mul(r, x); x = mul(x,x); n >>= 1; } return r; } long mod_inv(ulong x) { return powmod(x, MOD - 2, MOD); } long comb(long n, long r) { if (n - r < 0 || n < 0 || r < 0) return 0; return mul(mul(f[n], f[r].mod_inv), f[n - r].mod_inv); } void main() { int n, k; scan(n, k); make_table(2010); foreach (i; 1..k+1) { long x = mul(comb(n-k+1, i), comb(k-1, i-1)); writeln(x); } }
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; alias Edge = Tuple!(int, "u", int, "v"); long[] f(long x) { if (x == 0) { long[] ans = new long[](42); return ans; } else if (x == 1) { long[] ans = new long[](42); ans[0] = 1; return ans; } long b = x; long[] ans; if (b % 4 == 0 || b % 4 == 3) { ans ~= 0; } else { ans ~= 1; } foreach (i; 1..42) { long v = 1L << (i + 1); if (b % v < v / 2) { ans ~= 0; } else if (b % v % 2 == 0) { ans ~= 1; } else { ans ~= 0; } } return ans; } void main() { auto s = readln.split.map!(to!long); auto A = s[0]; auto B = s[1]; A = max(0L, A-1); long ans = 0; long[] fa = f(A); long[] fb = f(B); foreach (i; 0..fa.length) { ans |= (fa[i] ^ fb[i]) << i; } ans.writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void main() { auto N = readln.chomp.to!long; long[] AS; long a_sum; foreach (a; readln.split.to!(long[])) { AS ~= a; a_sum += a; } if (N == 1) { writeln("YES"); return; } auto x = N*(N+1) / 2; if (a_sum % x != 0) { writeln("NO"); return; } auto K = a_sum / x; long[] ds; foreach (i; 0..N) ds ~= AS[(i+1)%N] - AS[i]; long k; foreach (d; ds) { if (d > K || (K-d) % N != 0) { writeln("NO"); return; } k += (K-d) / N; } writeln(k == K ? "YES" : "NO"); }
D