code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int L, R; scan(L, R); auto D = R - L + 1; if (D >= 2019) { writeln(0); return; } long ans = 2019; foreach (i ; L .. R + 1) { foreach (j ; i + 1 .. R + 1) { ans = min(ans, (1L * i * j) % 2019); } } writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { int a = readint; int b = readint; int c = readint; int d = readint; writeln(min(a, b) + min(c, d)); }
D
import std.algorithm; import std.array; import std.bitmanip; import std.container; import std.conv; import std.functional; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; void main() { auto n = readln.chomp; int[10] cnt; foreach (i, ref e; cnt) { e = cast(int) n.count!(c => c - '0' == i); } debug { writeln(cnt); } long[20][20] binom; binom[0][0] = 1; foreach (i; 1..20) { foreach (j; 0..20) { binom[i][j] = binom[i-1][j] + (j > 0 ? binom[i-1][j-1] : 0); } } debug { binom.each!writeln; } bool next_variant(ref int[10] lim) { foreach_reverse (i; 0..10) { if (cnt[i] == 0) continue; if (lim[i] < cnt[i]) { ++lim[i]; return true; } else { lim[i] = 1; } } return false; } int[10] lim; foreach (i; 0..10) if (cnt[i] > 0) lim[i] = 1; long ans = 0; do { int left = sum(lim[0..$]); long cur = binom[left - 1][lim[0]]; left -= lim[0]; foreach (e; lim[1..10]) { cur *= binom[left][e]; left -= e; } ans += cur; } while (next_variant(lim)); ans.writeln; }
D
// unihernandez22 // https://atcoder.jp/contests/abc150/tasks/abc150_b // implementation import std.stdio: readln, writeln; import std.string: chomp; import std.algorithm: count; void main() { readln; string s = readln.chomp; s.count("ABC").writeln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto S = readln.chomp; auto N = S.length.to!int; auto C = new long[](26); foreach (i; 0..N) C[S[i] - 'a'] += 1; long ans = N.to!long * (N - 1).to!long / 2; foreach (i; 0..26) { ans -= C[i] * (C[i] - 1) / 2; } writeln(ans + 1); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; int[int] NS; void main() { auto N = readln.chomp.to!int; foreach (_; 0..N) { auto A = readln.chomp.to!int; if (A in NS) ++NS[A]; else NS[A] = 1; } int ret; foreach (a; NS.byValue) if (a & 1) ++ret; writeln(ret); }
D
import std.stdio, std.math, std.conv, std.string; bool is_prime(int n) { auto t = sqrt(n.to!double); for (int i = 2; i <= t; ++i) { if (n % i == 0) return false; } return true; } void main() { auto N = readln.chomp.to!int; int cnt; foreach (_; 0..N) { if (is_prime(readln.chomp.to!int)) ++cnt; } writeln(cnt); }
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 mn = dp[a[i]]; foreach (j ; 0 .. 26) { mn = min(mn, dp[a[i] ^ (1<<j)]); } opt[i] = mn + 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.regex; void main(){ auto io = new IO(); auto S = io.line!string()[0]; if ( S.have("N") ^ S.have("S") || S.have("W") ^ S.have("E") ){ writeln("No"); }else{ writeln("Yes"); } return; } import std.stdio,std.conv,std.string; import std.algorithm,std.array,std.math; class IO { T[] line( T = size_t , string token = " " )( size_t m = 1 ){ T[] arr = []; foreach( i ; 0..m ){ arr ~= this.read!T(); } return arr; } T[][] rect( T = size_t , string token = " " )( size_t m = 1 ){ T[][] arr = new T[][](m); foreach( i ; 0..m ){ arr[i] = this.read!T(token); } return arr; } private T[] read( T = size_t )( string token = " " ){ T[] arr; foreach( elm ; readln().chomp().split(token) ){ arr ~= elm.to!T(); } return arr; } } // T -> T[] pure T[] step( T = size_t )( in T begin , in T end , in T step = 1 ){ T[] ret; for( T i = begin ; i < end ; i += step ){ ret ~= i; } return ret; } unittest{ assert( step(0,10,2) == [0,2,4,6,8] ); } // T[] -> T[] pure T[] fill( T )( T[] args , in T filling ){ foreach( ref arg ; args ){ arg = filling; } return args; } T[] map( T )( T[] args , T function(T) f ){ foreach( ref arg ; args ){ arg = f(arg); } return args; } unittest{ assert( [true,false,true,false].fill(true) == [true,true,true,true] ); assert( [1,-2,3,-4].map!int(x=>x*2) == [2,-4,6,-8] ); } // T[] -> number pure T sum( T )( in T[] args ){ T ret = 0; foreach( i ; 0..args.length ){ ret += args[i]; } return ret; } pure T ave(T)( in T[] args ){ return args.sum()/args.length; } pure real multi( in real[] args ){ real ret = 1.0; foreach( i ; 0..args.length ){ ret *= args[i]; } return ret; } // T[] -> bool pure bool any( T )( in T[] args , in T target ){ foreach( arg ; args ){ if( arg == target ){return true;} } return false; } pure bool all( T )( in T[] args , in T cond ){ foreach( arg ; args ){ if( arg != cond ){return false;} } return true; } pure bool have( T )( in T[] args , in T[] target ... ){ bool[] found = new bool[](target.length); foreach( arg ; args ){ foreach( i,t ; target ){ if( arg == t ){ found[i] = true; } } } foreach( f ; found ){ if( f == false ){return false;} } return true; } unittest{ assert( [1,2,3,4,5].sum() == 15 ); assert( [1,2,3,4,5].ave() == 3 ); assert( cmp( [0.3,0.3].multi() , 0.09 ) , "multi() test failed" ); assert( cmp( [0.3,0.3,0.3].multi() , 0.027 ) , "multi() test failed" ); assert( [1,1,1].all(1) == true ); assert( [1,1,2].all(1) == false ); assert( [1,1,2].any(2) == true ); assert( [1,1,2].any(3) == false ); assert( [1,2,3,4,5].have([1,3,5]) == true ); assert( [1,2,3,4,5].have([1,3,5,7]) == false ); }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; void main() { string str = readln.chomp; int[char] count; for(int i=0; i<str.length; i++) { count[str[i]]++; } int odd = 0; char key; foreach(char k, int v; count) { if (v%2 != 0) { odd++; key = k; } } if (odd > 1) { writeln(0); } else { count[key]--; long x = 0; long y = 1; foreach(int v; count.values) { x += v/2; y *= fact(v/2); } writeln(fact(x)/y); } } long factorial(long n) { if (n==0) return 1; return n*factorial(n-1); } alias memoize!factorial fact;
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; alias Pair = Tuple!(int, "a", int, "b"); void main() { int x, y; scan(x, y); long ans; auto mc = ModComb(x + 1); foreach (a ; 0 .. x + 1) { auto xr = x - a; auto yr = y - 2*a; if (xr < 0 || yr < 0) continue; if (xr % 2 == 1 || xr / 2 != yr) continue; auto b = xr / 2; (ans += mc.c(a + b, a)) %= mod; } writeln(ans); } struct ModComb { int _N; long[] _fact, _factinv; long _mod; this(int n, long mod = 1_000_000_007L) { _N = n; _fact = new long[](_N + 1); _factinv = new long[](_N + 1); _mod = mod; _fact[0] = 1; foreach (i ; 1 .. _N + 1) { _fact[i] = (_fact[i-1] * i) % mod; } _factinv[_N] = _powmod(_fact[_N], mod - 2); foreach_reverse (i ; 0 .. _N) { _factinv[i] = (_factinv[i+1] * (i+1)) % mod; } } long c(int n, int k) { if (k < 0 || k > n) return 0; return f(n) * finv(n - k) % _mod * finv(k) % _mod; } long p(int n, int r) { if (r < 0 || r > n) return 0; return f(n) * finv(n - r) % _mod; } long f(int n) { return _fact[n]; } long finv(int n) { return _factinv[n]; } long _powmod(long x, long y) { return y > 0 ? _powmod(x, y>>1)^^2 % _mod * x^^(y & 1) % _mod : 1; } } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio, std.bitmanip; void main() { auto s = readln.split.map!(to!long); auto N = s[0].to!int; auto A = s[1]; auto B = s[2]; auto X = readln.split.map!(to!long); long ans = 0; foreach (i; 1..N) { ans += min((X[i]-X[i-1])*A, B); } ans.writeln; }
D
void main(){ string[] val = inln!string(); bool a = val[0]=="H"; bool b = val[1]=="H"; writeln( a^b?'D':'H'); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range; const long mod = 10^^9+7; // 1要素のみの入力 T inelm(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] inln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split())ln ~= elm.to!T(); return ln; }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; import std.uni; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { string s1, s2, s3; readV(s1, s2, s3); writeln(s1[0].toUpper, s2[0].toUpper, s3[0].toUpper); }
D
import std.stdio; import std.algorithm; import std.conv; import std.string; void main() { auto tokens = split(chomp(readln())); int a = to!int(tokens[0]); int b = to!int(tokens[1]); if ((a * b) % 2 == 0) writeln("No"); else writeln("Yes"); stdout.flush(); }
D
import std.stdio, std.conv, std.range, std.string, std.array; void main(){ auto ip = readln.chomp; writeln(ip.count('1')); }
D
import std.stdio; import std.string; import std.conv; int main() { int N = readln().chomp().to!int(); string[] num = readln().split(); for (int i = N - 1; i > 0; i--) write(num[i], " "); writeln(num[0]); return 0; }
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 a = RD!char; writeln(a-'a' >= 0 && a-'a' <= 26 ? 'a' : 'A'); stdout.flush; debug readln; }
D
void main() { int x = readln.chomp.to!int; writeln(x < 1200 ? "ABC" : "ARC"); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio; import std.string; import std.conv; import std.ascii; import std.algorithm; void main() { foreach (_; 0..readln.chomp.to!int) { auto script = readln; auto keys = script.split.filter!(str => str.length == 4); loop: foreach (a; [0, 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25]) { foreach (b; 0..26) { foreach (key; keys) { string decoded = key.decrypt(a, b); if (decoded == "this" || decoded == "that") { script.decrypt(a, b).write; break loop; } } } } } } string decrypt(string input, int a, int b) { string output; foreach (c; input) { output ~= isAlpha(c) ? ((c - 'a') * a + b) % 26 + 'a' : c; } return output; }
D
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } void main() { const N = readln.chomp.to!int; void solve() { void dfs(string s, char mx) { if (s.length == N) { s.writeln; return; } for(char c = 'a'; c <= mx; c++) { dfs(s ~ c, c == mx ? cast(char)(mx + 1) : mx); } } dfs("", 'a'); } solve(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto S = readln.chomp; auto N = S.length; int r1, r2; foreach (i; 0..N) { auto c = i%2 == 0 ? '0' : '1'; if (S[i] == c) ++r1; if (S[i] != c) ++r2; } writeln(min(r1, r2)); }
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 p, q, r; scan(p, q, r); auto ans = min(p + q, p + r, q + r); writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
/+ dub.sdl: name "A" dependency "dunkelheit" version="1.0.1" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner, dkh.algorithm; int main() { Scanner sc = new Scanner(stdin); scope(exit) assert(!sc.hasNext); int n; sc.read(n); writeln((n - 2) * 180); return 0; } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/algorithm.d */ // module dkh.algorithm; import std.traits : isFloatingPoint, isIntegral; T binSearch(alias pred, T)(T l, T r) if (isIntegral!T) { while (r-l > 1) { T md = l + (r-l) / 2; if (!pred(md)) l = md; else r = md; } return r; } T binSearch(alias pred, T)(T l, T r, int cnt = 60) if (isFloatingPoint!T) { foreach (i; 0..cnt) { T md = (l+r)/2; if (!pred(md)) l = md; else r = md; } return r; } import std.range.primitives; E minimum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed) if (isInputRange!Range && !isInfinite!Range) { import std.algorithm, std.functional; return reduce!((a, b) => binaryFun!pred(a, b) ? a : b)(seed, range); } ElementType!Range minimum(alias pred = "a < b", Range)(Range range) { assert(!range.empty, "minimum: range must not empty"); auto e = range.front; range.popFront; return minimum!pred(range, e); } E maximum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed) if (isInputRange!Range && !isInfinite!Range) { import std.algorithm, std.functional; return reduce!((a, b) => binaryFun!pred(a, b) ? b : a)(seed, range); } ElementType!Range maximum(alias pred = "a < b", Range)(Range range) { assert(!range.empty, "maximum: range must not empty"); auto e = range.front; range.popFront; return maximum!pred(range, e); } Rotator!Range rotator(Range)(Range r) { return Rotator!Range(r); } struct Rotator(Range) if (isForwardRange!Range && hasLength!Range) { size_t cnt; Range start, now; this(Range r) { cnt = 0; start = r.save; now = r.save; } this(this) { start = start.save; now = now.save; } @property bool empty() const { return now.empty; } @property auto front() const { assert(!now.empty); import std.range : take, chain; return chain(now, start.take(cnt)); } @property Rotator!Range save() { return this; } void popFront() { cnt++; now.popFront; } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/scanner.d */ // module dkh.scanner; // import dkh.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; private File f; this(File f) { this.f = f; } private char[512] lineBuf; private char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { assert(succW()); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(Args...)(auto ref Args args) { import std.exception : enforce; static if (args.length != 0) { enforce(readSingle(args[0])); read(args[1..$]); } } bool hasNext() { return succ(); } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std.stdio; import std.string; import std.conv; import std.range; import std.array; import std.algorithm; void main() { string input; while ((input = readln.chomp).length != 0) { auto line = input.map!(a => a.to!int - '0').array; while (line.length > 1) { for (int i = 0; i < (line.length - 1); i++) { line[i] = line[i] + line[i+1]; } line.popBack; } writeln(line.front % 10); } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { string s = read!string; char[] buf; foreach (c; s) { switch (c) { case '0': buf ~= '0'; break; case '1': buf ~= '1'; break; case 'B': if (buf.length > 0) { buf = buf[0..$-1]; } break; default: break; } } writeln(buf); }
D
//prewritten code: https://github.com/antma/algo import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.traits; final class InputReader { private: ubyte[] p, buffer; bool eof; bool rawRead () { if (eof) { return false; } p = stdin.rawRead (buffer); if (p.empty) { eof = true; return false; } return true; } ubyte nextByte(bool check) () { static if (check) { if (p.empty) { if (!rawRead ()) { return 0; } } } auto r = p.front; p.popFront (); return r; } public: this () { buffer = uninitializedArray!(ubyte[])(16<<20); } bool seekByte (in ubyte lo) { while (true) { p = p.find! (c => c >= lo); if (!p.empty) { return false; } if (!rawRead ()) { return true; } } } template next(T) if (isSigned!T) { T next () { if (seekByte (45)) { return 0; } T res; ubyte b = nextByte!false (); if (b == 45) { while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 - (b - 48); } } else { res = b - 48; while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 + (b - 48); } } } } template next(T) if (isUnsigned!T) { T next () { if (seekByte (48)) { return 0; } T res = nextByte!false () - 48; while (true) { ubyte b = nextByte!true (); if (b < 48 || b >= 58) { break; } res = res * 10 + (b - 48); } return res; } } T[] nextA(T) (in int n) { auto a = uninitializedArray!(T[]) (n); foreach (i; 0 .. n) { a[i] = next!T; } return a; } } void main() { auto r = new InputReader (); immutable nt = r.next!uint (); int[] ra (int n) { auto a = r.nextA!int (n); sort (a); return a; } foreach (tid; 0 .. nt) { long res = long.max; auto a = r.nextA!uint (3); auto t = new int[][3]; foreach (k; 0 .. 3) t[k] = ra (a[k]); void check (long u, long v, long w) { long q = (u - v) * (u - v); q += (u - w) * (u - w); q += (v - w) * (v - w); if (res > q) res = q; } void f (in int[] x, in int[] y, in int[] z) { size_t j, k; foreach (i; 0 .. x.length) { int xv = x[i]; while (j < y.length && y[j] < xv) ++j; while (k < z.length && z[k] < xv) ++k; void go (size_t u, size_t v) { if (u < y.length && v < z.length) check (xv, y[u], z[v]); } go (j, k); if (k > 0) { go (j, k - 1); } if (j > 0) { go (j - 1, k); if (k > 0) { go (j - 1, k - 1); } } } } f (t[0], t[1], t[2]); f (t[1], t[0], t[2]); f (t[2], t[0], t[1]); writeln (res); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; 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 r = RD; auto D = RD; auto x2000 = RD; long last = x2000; foreach (i; 0..10) { auto x = last * r - D; writeln(x); last = x; } stdout.flush(); debug readln(); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; immutable long MOD = 10^^9 + 7; void main() { auto N = readln.chomp.to!int; auto A = readln.chomp.map!(a => a == '1').array; auto B = readln.chomp.map!(a => a == '1').array; auto cnt = new long[](4); foreach (i; 0..N) { if (!A[i] && !B[i]) { cnt[0] += 1; } else if (A[i] && !B[i]) { cnt[1] += 1; } else if (!A[i] && B[i]) { cnt[2] += 1; } else { cnt[3] += 1; } } auto ans = cnt[0] * cnt[1] + cnt[0] * cnt[3] + cnt[1] * cnt[2]; ans.writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto AS = readln.split.to!(int[]); int c; foreach (a; AS) { while (a && a%2 == 0) { ++c; a /= 2; } } writeln(c); }
D
import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } class Node{ Node[] adjnodes; Node[] kids; Node parent; int id; this(int id){ this.id = id; } void add(Node nd){ this.adjnodes ~= nd; nd.adjnodes ~= this; } void setKids(){ foreach(nd; this.adjnodes){ if(!this.parent || nd.id != this.parent.id){ this.kids ~= nd; nd.parent = this; nd.setKids; } } } int getStatus(){ // 0: I am single; 1: I am paired; 2: Cannot pair if(this.kids.length == 0){ return 0 /* single */; } else if(this.kids.length == 1){ int s = this.kids[0].getStatus; if(s == 0 /* single */) return 1 /* paired */; else if(s == 1) return 0 /* single */; else return 2 /* error */; } else{ int s = 0 /* single */; foreach(nd; this.kids){ int s1 = nd.getStatus; if(s1 == 2 /* error */) return 2 /* error */; if(s1 == 0 /* single */){ if(s == 0 /* single */) s = 1 /* paired */; else return 2 /* error */; } } return s; } } } void main(){ int n = read.to!int; Node[int] nodes; foreach(i; 1 .. n + 1){ nodes[i] = new Node(i); } foreach(i; 1 .. n){ int a = read.to!int; int b = read.to!int; nodes[a].add(nodes[b]); } nodes[1].setKids; string ans; if(nodes[1].getStatus == 1 /* paired */) ans = "Second"; else ans = "First"; ans.writeln; }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.numeric; void main() { auto x = readln.chomp.to!int; immutable m = 200_000 + 1; auto f = new bool[](m); auto res = 1; foreach (i; 2..m) { if (f[i]) continue; else if (x <= i) { res = i; break; } foreach (j; iota(i, m, i)) { f[j] = true; } } res.writeln; }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { string _1, s, _2; readV(_1, s, _2); writeln('A', s[0], 'C'); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long X, Y; scan(X, Y); writeln(X + Y / 2); }
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() { string S; scan(S); auto N = S.length; bool ok = true; foreach (i ; 0 .. N) { if (i % 2 == 0) { if (S[i] == 'L') ok = false; } else { if (S[i] == 'R') ok = false; } } writeln(ok ? "Yes" : "No"); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio; import std.algorithm; import std.range; import std.array; import std.string; import std.conv; void main(){ int[] inputs = readln.chomp.split.map!(to!int).array; auto n = inputs[0]; auto a = inputs[1]; auto b = inputs[2]; auto answer = min(n * a, b); writeln(answer); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { string s; scan(s); int n = s.length.to!int; int btm = 1, top = n + 1; bool check(int k) { if (2*k <= n) { return true; } bool b = 1; char ch = s[n-k]; foreach (i ; n-k .. k) { b &= s[i] == ch; } return b; } while (top - btm > 1) { int mid = (btm + top) / 2; if (check(mid)) { btm = mid; } else { top = mid; } } writeln(btm); } 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.array, std.conv, std.algorithm.iteration, std.functional; void main() { auto ip = readln.chomp.split(" ").map!(pipe!((c => c.to!int), (i => i == 1 ? 14 : i))).array; auto a = ip[0]; auto b = ip[1]; writeln( a > b ? "Alice" : a < b ? "Bob" : "Draw" ); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { long H, W; scan(H, W); auto S = new string[](H); foreach (i; 0 .. H) S[i] = sread(); foreach (y; 0 .. H) foreach (x; 0 .. W) if (S[y][x] == '#') { bool u = (0 < y) ? S[y - 1][x] == '#' : false; bool d = (y + 1 < H) ? S[y + 1][x] == '#' : false; bool l = (0 < x) ? S[y][x - 1] == '#' : false; bool r = (x + 1 < W) ? S[y][x + 1] == '#' : false; if (!(u || d || l || r)) { writeln("No"); return; } } writeln("Yes"); }
D
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; int a, b; void main() { scan(a, b); a = min(a, b); long ans = 1L; foreach (i ; 1 .. a + 1) { ans *= i; } writeln(ans); } 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.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; void main() { string[] input = readln.split; int N = input[0].to!int; int Q = input[1].to!int; string ans = "kogakubu10gokan"; foreach(i; 0..N) { input = readln.split; if (input[0].to!int <= Q) ans = input[1]; } ans.writeln; }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.format; void main() { auto abbcca = readln.split.to!(int[]), ab = abbcca[0], bc = abbcca[1], ca = abbcca[2]; writeln(ab * bc / 2); }
D
import std.stdio, std.string, std.conv, std.array, std.algorithm; void main() { auto a = readln.split.map!(to!int); if(a[0]+a[1]==2||a[2]==1) writeln("Open"); else writeln("Close"); }
D
void main(){ long a; long[2] bb; scanf("%ld %ld.%ld", &a, &bb[0], &bb[1]); long b = bb[0]*100.to!long + bb[1]; ( a*b/100 ).writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio, std.string, std.conv, std.array; string mathed(char[] s, char[] t) { for (int i = cast(int)(s.length-t.length); i >= 0; i--) { if (s[i] == t[0] || s[i] == '?') { int ii = i; bool flag = true; for (int j = 0; j < t.length;) { if (!(s[i] == t[j] || s[i] == '?')) { flag = false; break; } i++; j++; } if (flag) { for (int j = 0; j < t.length; j++) { s[ii+j] = t[j]; } return cast(string)s.replace("?", "a"); } else { i = ii; } } } return "UNRESTORABLE"; } void main() { char[] s = readln.chomp.dup; char[] t = readln.chomp.dup; writeln(mathed(s, t)); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { int r, g; scan(r); scan(g); auto ans = r + (g - r) * 2; 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; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { auto abc = aryread(); // writeln(abc); long cnt5; long cnt7; foreach (i; 0 .. (abc.length)) { if (abc[i] == 5) { cnt5 += 1; } else if (abc[i] == 7) { cnt7 += 1; } } if (cnt5 == 2 && cnt7 == 1) { writeln("YES"); } else { writeln("NO"); } } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
void main(){ string s = readln().chomp(); string t = readln().chomp(); int ans; foreach(i; 0..s.length){ if(s[i] != t[i])ans++; } ans.writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.numeric; void main() { auto n = readln.chomp.to!int; auto s = readln.chomp; int res; auto prev = 'S'; foreach (e; s) { if (prev != e) { res++; prev = e; } } res.writeln; }
D
//dlang template---{{{ import std.stdio; import std.conv; import std.string; import std.array; import std.algorithm; import std.typecons; import std.math; import std.range; // MIT-License https://github.com/kurokoji/nephele class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } //Digit count---{{{ int DigitNum(int num) { int digit = 0; while (num != 0) { num /= 10; digit++; } return digit; } //}}} //}}} void main() { Scanner sc = new Scanner; int s; sc.scan(s); int[] a = new int[10000]; a[0] = s; int i = 1; while (1) { if (a[i - 1] % 2 == 0) { a[i] = a[i - 1] / 2; } else { a[i] = (3 * a[i - 1]) + 1; } foreach_reverse (j; 0 .. i) { if (a[i] == a[j]) { writeln(i + 1); return; } } i++; } }
D
import std.algorithm; import std.array; import std.ascii; import std.container; import std.conv; import std.format; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; void main() { int n = readln.chomp.to!int; int a = readln.chomp.to!int; writeln = n * n - a; }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n, m; readV(n, m); auto r = 1; foreach (f; 1..m.nsqrt+1) { if (m%f != 0) continue; if (m/f >= n) r = max(r, f); if (f >= n) r = max(r, m/f); } writeln(r); } pure T nsqrt(T)(T n) { import std.algorithm, std.conv, std.range, core.bitop; if (n <= 1) return n; T m = T(1) << (n.bsr/2+1); return iota(1, m).map!"a * a".assumeSorted!"a <= b".lowerBound(n).length.to!T; }
D
import std.stdio; import std.ascii; import std.algorithm; static import core.stdc.stdio; long[300_000+2] a; long[300_000+2] c; int main() { int[][] parent = new int[][](300_000+2, 20); int q = readInt!int; a[0] = readInt!long; c[0] = readInt!long; parent[0][] = -1; int getNthParent(int v, int n) { int i = 0; while (n) { if (n & 1) v = parent[v][i]; n >>= 1; i++; } return v; } foreach(i; 1 .. q + 1) { int type = readInt!int; if (type == 1) { int pi = readInt!int; long ai = readInt!long; long ci = readInt!long; parent[i][0] = pi; foreach(j; 1 .. 20) { if (parent[i][j - 1] > 0) parent[i][j] = parent[parent[i][j - 1]][j - 1]; else parent[i][j] = -1; } a[i] = ai; c[i] = ci; } else { int vi = readInt!int; long wi = readInt!long; int root = vi; int no = 0; long spent = 0; long bought = 0; foreach_reverse(j; 0 .. 20) { int possibleParent = parent[root][j]; if (possibleParent < 0) continue; if (a[possibleParent] > 0) { root = possibleParent; no += (long(1) << j); } } debug writeln("for node ", vi, " root is ", root, " depth = ", no); while (wi > 0 && no >= 0) { int p = getNthParent(vi, no); debug writeln("Now is ", no, " ", p, " ap = ", a[p], " wi = ", wi); long taken = min(wi, a[p]); wi -= taken; bought += taken; spent += taken * c[p]; a[p] -= taken; no--; } writeln(bought, " ", spent); stdout.flush; } } return 0; } /* INPUT ROUTINES */ int currChar; static this() { currChar = core.stdc.stdio.getchar(); } char topChar() { return cast(char) currChar; } void popChar() { currChar = core.stdc.stdio.getchar(); } auto readInt(T)() { T num = 0; int sgn = 0; while (topChar.isWhite) popChar; while (topChar == '-' || topChar == '+') { sgn ^= (topChar == '-'); popChar; } while (topChar.isDigit) { num *= 10; num += (topChar - '0'); popChar; } if (sgn) return -num; return num; } string readString() { string res = ""; while (topChar.isWhite) popChar; while (!topChar.isWhite) { res ~= topChar; popChar; } return res; } /**MODULAR SYSTEM*/ struct Z(immutable long m) { long rep; this(long num) { rep = num; } Z!m opBinary(string operator)(Z!m rhs) { static if (operator == "+") { long result = rhs.rep + this.rep; if (result >= m) result -= m; return Z!m(result); } else static if (operator == "-") { long result = this.rep - rhs.rep; if (result < 0) result += m; return Z!m(result); } else static if (operator == "*") { long result = this.rep * rhs.rep; if (result >= m) result %= m; return Z!m(result); } else static assert(text("Operator ", operator, " not supported")); } Z!m opBinary(string operator)(long exponent) if (operator == "^^") { assert(exponent >= 0); Z!m base = this; Z!m result = 1; while (exponent) { if (exponent & 1) result = result * base; base = base * base; exponent >>= 1; } return result; } invariant { assert(rep >= 0 && rep < m); } }
D
import std.stdio; void main() { foreach(i; 1 .. 10) { foreach(j; 1 .. 10) { writeln(i, 'x', j, '=', i * j); } } }
D
import std.stdio; void main(){for(int i;i<81;i++){ writeln(i/9+1,"x",i%9+1,"=",(i/9+1)*(i%9+1)); }}
D
import std.stdio; import std.algorithm; import std.string; import std.conv; import std.math; void main(){ while(true){ auto s = readln(); if(stdin.eof()) break; s = chomp(s); char win = 'd'; for(int i=0;i<3;i++){ if(s[3*i] != 's' && s[3*i] == s[3*i+1] && s[3*i] == s[3*i+2]) win = s[3*i]; } for(int i=0;i<3;i++){ if(s[i] != 's' && s[i] == s[i+3] && s[i+6] == s[i]) win = s[i]; } if(s[4] != 's'){ if(s[0] == s[4] && s[0] == s[8]) win = s[0]; else if(s[2] == s[4] && s[2] == s[6]) win = s[2]; } writeln(win); } }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional, std.traits; import std.algorithm, std.container; import core.stdc.stdlib; void main() { auto R = scanElem; auto G = scanElem; auto B = scanElem; auto N = scanElem; int c; for(auto r = 0; r*R <= N; r++) { for(auto g = 0; r*R+g*G <= N; g++) { if((N-r*R-g*G)%B==0) c++; } } writeln(c); } long gcd(long a, long b) { if(b == 0) return a; return gcd(b, 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; } } void scanValues(TList...)(ref TList list) { auto lit = readln.splitter; foreach (ref e; list) { e = lit.fornt.to!(typeof(e)); lit.popFront; } } T[] scanArray(T = long)() { return readln.split.to!(long[]); } void scanStructs(T)(ref T[] t, size_t n) { t.length = n; foreach (ref e; t) { auto line = readln.split; foreach (i, ref v; e.tupleof) { v = line[i].to!(typeof(v)); } } } long scanULong(){ long x; while(true){ const c = getchar; if(c<'0'||c>'9'){ break; } x = x*10+c-'0'; } return x; } 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; } 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) { Factor[] res; for (long i = 2; i ^^ 2 <= n; i++) { if (n % i != 0) continue; int c; while (n % i == 0) { n = n / i; c++; } res ~= Factor(i, c); } if (n != 1) res ~= Factor(n, 1); return res; } long[] primes(long n) { if(n<2)return []; auto table = new long[n+1]; long[] res; for(int i = 2;i<=n;i++) { if(table[i]==-1) continue; for(int a = i;a<table.length;a+=i) { table[a] = -1; } res ~= i; } return res; } bool isPrime(long n) { 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
/+ dub.sdl: name "B" dependency "dunkelheit" version="1.0.1" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner, dkh.algorithm; int main() { Scanner sc = new Scanner(stdin); scope(exit) assert(!sc.hasNext); string s; sc.read(s); int n = s.length.to!int; long r = n; long ans = 0; foreach_reverse (l; 0..n) { int k = 1; while (l + 2 * k < r && (s[l + k] != s[l] || s[l + 2 * k] != s[l])) k++; r = min(r, l + 2 * k); ans += (n - r); } writeln(ans); return 0; } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/algorithm.d */ // module dkh.algorithm; import std.traits : isFloatingPoint, isIntegral; T binSearch(alias pred, T)(T l, T r) if (isIntegral!T) { while (r-l > 1) { T md = l + (r-l) / 2; if (!pred(md)) l = md; else r = md; } return r; } T binSearch(alias pred, T)(T l, T r, int cnt = 60) if (isFloatingPoint!T) { foreach (i; 0..cnt) { T md = (l+r)/2; if (!pred(md)) l = md; else r = md; } return r; } import std.range.primitives; E minimum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed) if (isInputRange!Range && !isInfinite!Range) { import std.algorithm, std.functional; return reduce!((a, b) => binaryFun!pred(a, b) ? a : b)(seed, range); } ElementType!Range minimum(alias pred = "a < b", Range)(Range range) { assert(!range.empty, "minimum: range must not empty"); auto e = range.front; range.popFront; return minimum!pred(range, e); } E maximum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed) if (isInputRange!Range && !isInfinite!Range) { import std.algorithm, std.functional; return reduce!((a, b) => binaryFun!pred(a, b) ? b : a)(seed, range); } ElementType!Range maximum(alias pred = "a < b", Range)(Range range) { assert(!range.empty, "maximum: range must not empty"); auto e = range.front; range.popFront; return maximum!pred(range, e); } Rotator!Range rotator(Range)(Range r) { return Rotator!Range(r); } struct Rotator(Range) if (isForwardRange!Range && hasLength!Range) { size_t cnt; Range start, now; this(Range r) { cnt = 0; start = r.save; now = r.save; } this(this) { start = start.save; now = now.save; } @property bool empty() const { return now.empty; } @property auto front() const { assert(!now.empty); import std.range : take, chain; return chain(now, start.take(cnt)); } @property Rotator!Range save() { return this; } void popFront() { cnt++; now.popFront; } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/scanner.d */ // module dkh.scanner; // import dkh.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; private File f; this(File f) { this.f = f; } private char[512] lineBuf; private char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { assert(succW()); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(Args...)(auto ref Args args) { import std.exception : enforce; static if (args.length != 0) { enforce(readSingle(args[0])); read(args[1..$]); } } bool hasNext() { return succ(); } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!int).array; int ans = 0; foreach (i; 0..N) { foreach (j; i+1..N) { if (A[i] > A[j]) { ans ^= 1; } } } auto Q = readln.chomp.to!int; while (Q--) { auto lr = readln.split.map!(to!int); auto d = lr[1] - lr[0] + 1; ans ^= (d * (d - 1) / 2) % 2; writeln(ans%2 ? "odd" : "even"); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto k = RD!int; long x = n; for (long i = 2; i*i <= n; ++i) { if (n % i == 0) { x = i; break; } } ans[ti] = n + x + 2 * (k-1); } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; long n, m; rd(n, m); auto mn = max(0, n - m * 2); long mx = 0; for (auto k = 0L; k <= n; k++) { if (k * (k - 1) / 2 >= m) { mx = max(mx, n - k); } } writeln(mn, " ", mx); } void rd(T...)(ref T x) { import std.stdio : readln; import std.string : split; import std.conv : to; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
D
import std; enum inf(T)()if(__traits(isArithmetic,T)){return T.max/4;} T[]readarr(T=long)(){return readln.chomp.split.to!(T[]);} void scan(T...)(ref T args){auto input=readln.chomp.split;foreach(i,t;T)args[i]=input[i].to!t;} struct Queue(T){T[]e;auto enq(T t){e~=t;}auto enq(T[]ts){e~=ts;}T deq(){T tp=e[0];e=e.length>1?e[1..$]:[];return tp;}} struct Stack(T){T[]e;auto push(T t){e~=t;}auto push(T[]ts){e~=ts;}T pop(){T tp=e[$-1];e=e.length>1?e[0..$-1]:[];return tp;}} //END OF TEMPLATE void main(){ long n; n.scan; iota(1,n+1).filter!(i=>i%3!=0&&i%5!=0).fold!"a+b".writeln; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } bool calc(int rows, int cols, int k) { for (int i = 0; i <= rows; i++) { for (int j = 0; j <= cols; j++) { int n = (cols - j) * i + (rows - i) * j; if (n == k) return true; } } return false; } void main() { auto xs = readints; int n = xs[0], m = xs[1], k = xs[2]; writeln(calc(n, m, k) ? "Yes" : "No"); }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; void times(alias fun)(int n) { foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } // fold was added in D 2.071.0. template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } void main() { readln.chomp.uniq.array.length.pipe!"a-1".writeln; }
D
import std.stdio,std.conv,std.algorithm,std.array; int oneint(){ return readln().split().map!(to!int).array()[0];} //read from stdin as only one int void main(){ for(int i=1,x;(x = oneint())!=0;i++){ writeln("Case ",i,": ",x); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.bigint; void main() { auto n = readln.chomp.to!int; auto s = readln.chomp; int r = int.max; foreach (i; 0..n-3) { int t; t += min(abs(s[i] - 39), abs(s[i] - 65), abs(s[i] - 91)); t += min(abs(s[i+1] - 42), abs(s[i+1] - 67), abs(s[i+1] - 93)); t += min(abs(s[i+2] - 58), abs(s[i+2] - 84), abs(s[i+2] - 110)); t += min(abs(s[i+3] - 45), abs(s[i+3] - 71), abs(s[i+3] - 97)); r = min(r, t); } writeln(r); }
D
import std.stdio; import std.string; import std.array; // split import std.conv; // to void main() { string s = chomp(readln()); int a = to!int(s); // 第0要素を整数に変換 if(a > 999){ writeln("ABD"); } else { writeln("ABC"); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto S = readln.chomp; if (S == "AAA" || S == "BBB") { writeln("No"); } else { writeln("Yes"); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto q = RD!int; auto ans = new long[](q); long[] list = [1]; while (true) { auto x = list[$-1] * 3; if (x < list[$-1]) break; list ~= x; } foreach (i; 0..q) { auto n = RD!int; int pos; while (list[pos] < n) { ++pos; } if (pos != 0) --pos; auto l = 2L^^pos; auto r = 2L^^(list.length); foreach (j; l..r) { long x; foreach (k; 0..32) { auto bit = 1L << k; if (j & bit) { x += list[k]; } } if (x >= n) { ans[i] = x; break; } } } foreach (e; ans) writeln(e); stdout.flush(); debug readln(); }
D
import core.bitop; import std.algorithm; import std.array; import std.ascii; import std.container; import std.conv; import std.format; import std.math; import std.random; import std.range; import std.stdio; import std.string; import std.typecons; void main() { immutable k = readln.chomp.to!long; enum N = 50; long v = k / N; immutable remain = k % N; N.writeln; stderr.writeln(v, " ", remain); foreach (i; 0..N) { if (i) { write = " "; } if (i < remain) { write = v + N * 2 - remain; } else { write = v + N - (remain + 1); } } writeln; }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; void times(alias fun)(int n) { foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } // fold was added in D 2.071.0. template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } void main() { 3.rep!(() => readln.chomp.map!(c => c-'a').pipe!(str => DList!int(str))).pipe!((ary) { int now = 0; while(!ary[now].empty) { int tmp = ary[now].front; ary[now].removeFront; now = tmp; } return now; }).pipe!(i => "ABC"[i]).writeln; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void minAssign(T)(ref T dst, T src) { dst = min(dst, src); } alias sread = () => readln.chomp(); void main() { long N = lread(); static immutable dp = () { enum Nmax = 10^^5; auto dp = new long[](Nmax + 1); dp[] = long.max; dp[0] = 0; foreach (i; 0 .. Nmax) if (dp[i] != long.max) { dp[i + 1].minAssign(dp[i] + 1); size_t j = 6; while (i + j < dp.length) { dp[i + j].minAssign(dp[i] + 1); j *= 6; } j = 9; while (i + j < dp.length) { dp[i + j].minAssign(dp[i] + 1); j *= 9; } } return dp; }(); dp[N].writeln(); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { auto h = lread(); func(h).writeln(); } long func(long h) { if (h == 1) { return 1; } return func(h / 2) * 2 + 1;//2体に分裂する+1,h/2を2体倒す(h/2)*2 } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); } void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); } string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); } 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; } T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ string s = scan; int x; foreach(c; s){ x += (c - '0'); x %= 9; } if(x % 9 == 0) "Yes".writeln; else "No".writeln; }
D
import std.algorithm; import std.conv; import std.stdio; import std.string; import std.math; void main() { auto ab = readln.split.map!( to!int ); auto add = ab[ 0 ] + ab[ 1 ]; auto sub = ab[ 0 ] - ab[ 1 ]; auto mul = ab[ 0 ] * ab[ 1 ]; writeln( max( add, sub, mul ) ); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; enum PS = [300000, 200000, 100000]; void main() { auto xy = readln.split.to!(int[]); auto X = xy[0]; auto Y = xy[1]; int r; if (X <= 3) r += PS[X-1]; if (Y <= 3) r += PS[Y-1]; if (X == 1 && Y == 1) r += 400000; writeln(r); }
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); int n; sc.read(n); int[] p = new int[n]; int[] rp = new int[n]; foreach (i; 0..n) { sc.read(p[i]); p[i]--; rp[p[i]] = i; } int ans = 0; int l = 0; while (l < n) { int r = l+1; while (r < n && rp[r-1] < rp[r]) r++; ans = max(ans, r-l); l = r; } writeln(n-ans); return 0; } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/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); } } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* 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; void main(){ auto s = readln.chomp; if (s == "ABC") "ARC".writeln; else "ABC".writeln; }
D
module main; import std.stdio : readln, writeln, write, writefln, writef; import std.conv : to; import std.array : split, replace; import std.string : strip; import std.algorithm : max, min, map, reduce, sort, reverse; import std.functional : memoize; version = A; version (A) { void main() { auto sequence = read_one!string(); auto instructions = read_one!string(); size_t position = 0; foreach (instruction; instructions) { if (sequence[position] == instruction) { position += 1; } } writeln(position + 1); } } version (B) { void main() { auto n = read_one!int(); } } version (C) { void main() { auto n = read_one!int(); } } version (D) { void main() { auto n = read_one!int(); } } version (E) { void main() { } } T read_one(T)() { return readln().strip().to!T(); } T[] read_some(T)() { T[] ret; foreach (e; readln().strip().split()) { ret ~= e.to!T(); } return ret; } T[m] read_fixed(int m, T)() { T[m] ret; foreach (i, e; readln().strip().split()) { ret[i] = e.to!T(); } return ret; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int a, b; rd(a, b); auto s=readln.chomp.to!(char[]); if(count(s, '-')!=1){writeln("No"); return;} if(s[a]=='-') writeln("Yes"); else writeln("No"); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } } void wr(T...)(T x){ import std.stdio; foreach(e; x) write(e, " "); writeln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto s = RD!string; auto ss = RD!string; (){ foreach (i; 0..s.length) { foreach (j; i..min(s.length, i+ss.length)) { auto len = j - i + 1; bool ok = true; foreach (k; 0..len) { if (s[i+k] != ss[k]) { ok = false; break; } } if (!ok) continue; foreach_reverse (k; 0..ss.length-len) { if (j <= k) { ok = false; break; } auto p = j - k - 1; if (s[p] != ss[len+k]) { ok = false; break; } } if (ok) { ans[ti] = true; return; } } }}(); } foreach (e; ans) { writeln(e ? "YES" : "NO"); } stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random; import std.typecons, std.functional, std.traits,std.concurrency; import std.algorithm, std.container; import core.bitop, core.time, core.memory; import std.bitmanip; import std.regex; enum INF = long.max/3; enum MOD = 10L^^9+7; //辞書順順列はiota(1,N),nextPermituionを使う void end(T)(T v) if(isIntegral!T||isSomeString!T||isSomeChar!T) { import core.stdc.stdlib; writeln(v); exit(0); } T[] scanArray(T = long)() { static char[] scanBuf; readln(scanBuf); return scanBuf.split.to!(T[]); } dchar scanChar() { int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } return cast(dchar)c; } T scanElem(T = long)() { import core.stdc.stdlib; static auto scanBuf = appender!(char[])([]); scanBuf.clear; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { scanBuf ~= cast(char) c; c = getchar; } return scanBuf.data.to!T; } dchar[] scanString(){ return scanElem!(dchar[]); } void main() { auto a=scanChar; foreach(e;['a','i','u','e','o'])if(e==a)end("vowel");end("consonant"); }
D
import std.stdio,std.string,std.conv; void main(){ writeln( readln().chomp().to!int^^3 ); }
D
import std.stdio, std.string, std.array, std.conv; struct Card { char suit, value; } void bubble(int n, Card[] x) { foreach (i; 0 .. n) { foreach_reverse (j; i+1 .. n) { if (x[j].value < x[j-1].value) { Card t = x[j]; x[j] = x[j-1]; x[j-1] = t; } } } } void selection(int n, Card[] x) { foreach (i; 0 .. n) { int minj = i; foreach (j; i .. n) { if (x[j].value < x[minj].value) { minj = j; } } Card t = x[i]; x[i] = x[minj]; x[minj] = t; } } bool isStable(int n, Card[] x, Card[] y) { foreach (i; 0 .. n) { if (x[i].suit != y[i].suit) return false; } return true; } void main() { int n = readln.chomp.to!int; string[] _a = readln.chomp.split; Card[] a, b; foreach (x; _a) { a ~= Card(x[0], x[1]); b ~= Card(x[0], x[1]); } bubble(n, a); selection(n, b); foreach (i, x; a) { write(x.suit, x.value); if (i != n - 1) " ".write; else writeln; } "Stable".writeln; foreach (j, y; b) { write(y.suit, y.value); if (j != n - 1) " ".write; else writeln; } writeln(isStable(n, a, b) ? "Stable" : "Not stable"); }
D
import std.stdio, std.conv, std.algorithm, std.string; alias L = long; L M = 998244353; L[] X; L[L] C; L h(L m, L[] U){ L f; foreach(u; U[0 .. m]) f = f * 2 % M + u; foreach(i, u; U) if(i / m & 1 ^ U[i % m] ^ u) return f + u; return f + 1; } void main(){ L n = readln.chomp.to!L, b = n * 2; foreach(c; readln.chomp) X ~= c - '0'; for(L d = n | 1; d > 2; d -= 2) if(n % d < 1){ L c = h(n / d, X); foreach(e, q; C) e % d || (c += M - q); C[d] = c % M; } n = h(n, X) * b; foreach(d, c; C) n += M - c * (b - b / d) % M; writeln(n % M); }
D
import std.stdio, std.string, std.array, std.conv; long cnt; int[] y; void insertionSort(int n, int g, int[] x) { foreach (i; g .. n) { int v = x[i]; int j = i - g; while (j >= 0 && x[j] > v) { x[j+g] = x[j]; j -= g; ++cnt; } x[j+g] = v; } } void shellSort(int n, int[] x) { int h = 1; while (true) { if (h > n) break; y ~= h; h = 3 * h + 1; } foreach_reverse (z; y) { insertionSort(n, z, x); } } void main() { int n = readln.chomp.to!int; int[] a = new int[n]; foreach (i; 0 .. n) { a[i] = readln.chomp.to!int; } cnt = 0; shellSort(n, a); y.length.writeln; foreach_reverse (i, z; y) { z.write; if (i) " ".write; else writeln; } cnt.writeln; foreach (i, x; a) { x.writeln; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp; int X; foreach (c; N.to!(char[])) X += c-48; writeln(N.to!int % X == 0 ? "Yes" : "No"); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { auto S = sread(); foreach (c; S) write("x"); writeln(); }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); } void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); } string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); } 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; } T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ string s = scan; if(s == "hi" || s == "hihi" || s == "hihihi" || s == "hihihihi" || s == "hihihihihi") "Yes".writeln; else "No".writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new int[][](t); foreach (ti; 0..t) { auto l = RD!int; auto r = RD!int; if (l*2 > r) ans[ti] = [-1, -1]; else ans[ti] = [l, l*2]; } foreach (e; ans) { writeln(e[0], " ", e[1]); } stdout.flush; debug readln; }
D
import std.stdio, std.string, std.range, std.algorithm; void main() { string s = strip(readln()); size_t n = s.length; auto odd = filter!(a => a%2 == 0)(iota(n)); foreach (c; map!(i => s[i])(odd)) { write(c); } writeln(); }
D
void main() { problem(); } void problem() { auto a = scan!long; long solve() { return a + a*a + a*a*a; } 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 Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
void main(){ int n = _scan(); if(n>81){ writeln("No"); return; } foreach(i; 1..81+1){ if(i<=9 && n/i<=9 && n%i==0){ writeln("Yes"); return; } } writeln("No"); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
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<<30; enum mod = 10L^^9 + 7; void main() { int h, w, a, b; scan(h, w, a, b); auto fact = new long[](h + w + 1); auto rfact = new long[](h + w + 1); fact[0] = 1; foreach (i ; 1 .. h + w + 1) { fact[i] = (i * fact[i-1]) % mod; } rfact[h + w] = powmod(fact[h + w], mod - 2); foreach_reverse (i ; 0 .. h + w) { rfact[i] = (rfact[i+1] * (i + 1)) % mod; } long comb(int n, int r) { return fact[n] * rfact[n-r] % mod * rfact[r] % mod; } long ans; foreach (i ; 0 .. w - b) { ans += comb(h - a - 1 + b + i, h - a - 1) * comb(a - 1 + w - b - 1 - i, a - 1) % mod; if (ans > mod) ans-= mod; } writeln(ans); } long powmod(long x, long y) { return y > 0 ? powmod(x, y>>1)^^2 % mod * x^^(y & 1) % mod : 1; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); long[long] D; foreach (x; 1 .. N + 1) { auto f = factorize(x); foreach (key, val; f) { D[key] = D.get(key, 0) + val; } } long ans = 1; foreach (key, value; D) { ans *= value + 1; ans %= MOD; } writeln(ans); } /// 素因数分解 long[long] factorize(long x) { assert(0 < x, "x is negative"); long[long] ps; while ((x & 1) == 0) { x /= 2; ps[2] = (2 in ps) ? ps[2] + 1 : 1; } for (long i = 3; i * i <= x; i += 2) while (x % i == 0) { x /= i; ps[i] = (i in ps) ? ps[i] + 1 : 1; } if (x != 1) ps[x] = (x in ps) ? ps[x] + 1 : 1; return ps; }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { auto n = lread(); auto k = lread(); long ans = 1; foreach (i; 0 .. n) { ans = min(ans * 2, ans + k); } writeln(ans); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto AS = new int[](2^^N); foreach (i; 0..2^^N) { AS[i] = readln.chomp.to!int; } while (AS.length > 1) { int[] NAS; for (int i; i < AS.length; i += 2) { if (AS[i] == AS[i+1]) { NAS ~= AS[i]; } else { NAS ~= abs(AS[i] - AS[i+1]); } } AS = NAS; } writeln(AS[0]); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}} void main() { int a, b; readV(a, b); writeln((a+b+1)/2); }
D
void main() { string s = readln.chomp; int n = s.to!int; int fx; foreach (x; s) { fx += x - '0'; } writeln(n % fx == 0 ? "Yes" : "No"); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { auto S = sread(); long ans = long.max; { char prev = S[0]; long cnt; foreach (i; 0 .. S.length) if (S[i] != prev) { prev = S[i]; cnt++; } ans = ans.min(cnt); } { char prev = S[$ - 1]; long cnt; foreach_reverse (i; 0 .. S.length) if (S[i] != prev) { prev = S[i]; cnt++; } ans = ans.min(cnt); } writeln(ans); }
D