code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto n = RD!string; auto pat = new long[](n.length+1); foreach (i; 0..n.length) { pat[i+1] = pat[i]; long tmp = 10; tmp.modpow(i); tmp.modm(i+1); pat[i+1].moda(tmp); } long ans; foreach (i; 0..n.length) { { long num = n[i]-'0'; long left = i; left *= (i+1); left /= 2; left %= mod; long digit = 10; digit.modpow(n.length-i-1); num.modm(digit); left.modm(num); ans.moda(left); debug writeln("i:", i, " ans:", ans); } { long num = n[i]-'0'; num.modm(pat[n.length-i-1]); ans.moda(num); debug writeln("i:", i, " ans:", ans); } } writeln(ans); stdout.flush; debug readln; }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto n = readln.strip.to !(int); auto a = readln.splitter.map !(to !(int)).array; auto x = readln.strip.to !(int); auto f = new int [4] [n + 1]; f[0][0] = 0; foreach (i; 0..n) { f[i + 1][0] = f[i][].maxElement; f[i + 1][1] = f[i][0] + 1; if (i >= 1 && a[i] + a[i - 1] >= x * 2) { f[i + 1][2] = f[i][1] + 1; if (i >= 2 && a[i] + a[i - 1] + a[i - 2] >= x * 3) { f[i + 1][3] = f[i][2..$].maxElement + 1; } } } f[n][].maxElement.writeln; } }
D
//prewritten code: https://github.com/antma/algo import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.traits; final class InputReader { private: ubyte[] p, buffer; bool eof; bool rawRead () { if (eof) { return false; } p = stdin.rawRead (buffer); if (p.empty) { eof = true; return false; } return true; } ubyte nextByte(bool check) () { static if (check) { if (p.empty) { if (!rawRead ()) { return 0; } } } auto r = p.front; p.popFront (); return r; } public: this () { buffer = uninitializedArray!(ubyte[])(16<<20); } bool seekByte (in ubyte lo) { while (true) { p = p.find! (c => c >= lo); if (!p.empty) { return false; } if (!rawRead ()) { return true; } } } template next(T) if (isSigned!T) { T next () { if (seekByte (45)) { return 0; } T res; ubyte b = nextByte!false (); if (b == 45) { while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 - (b - 48); } } else { res = b - 48; while (true) { b = nextByte!true (); if (b < 48 || b >= 58) { return res; } res = res * 10 + (b - 48); } } } } template next(T) if (isUnsigned!T) { T next () { if (seekByte (48)) { return 0; } T res = nextByte!false () - 48; while (true) { ubyte b = nextByte!true (); if (b < 48 || b >= 58) { break; } res = res * 10 + (b - 48); } return res; } } T[] nextA(T) (in int n) { auto a = uninitializedArray!(T[]) (n); foreach (i; 0 .. n) { a[i] = next!T; } return a; } } void main() { auto r = new InputReader (); immutable nt = r.next!uint (); auto t = r.nextA!uint (nt); foreach (n; t) { int x = n; if (x & 1) { write ('7'); x -= 3; } while (x > 0) { write ('1'); x -= 2; } writeln; } }
D
/+ dub.sdl: name "A" dependency "dunkelheit" version=">=0.9.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; int main() { Scanner sc = new Scanner(stdin); scope(exit) sc.read!true; string s; sc.read(s); string err = "aeiou13579"; int ans = 0; foreach (c; err) { ans += s.count(c); } writeln(ans); return 0; } /* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/scanner.d */ // module dkh.scanner; // import dkh.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(bool enforceEOF = false, T, Args...)(ref T x, auto ref Args args) { import std.exception; enforce(readSingle(x)); static if (args.length == 0) { enforce(enforceEOF == false || !succ()); } else { read!enforceEOF(args); } } void read(bool enforceEOF = false, Args...)(auto ref Args args) { import std.exception; static if (args.length == 0) { enforce(enforceEOF == false || !succ()); } else { enforce(readSingle(args[0])); read!enforceEOF(args); } } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto k = RD; auto p = RDA; auto x = p[0]; foreach (i; 1..n) { auto y = p[i] * 100; auto d = y - x * k; if (d > 0) { d = (d+k-1) / k; ans[ti] += d; p[i] += d; } x += p[i]; } } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(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); } T binarySearch(alias pred, T)(T ok, T ng) { while (abs(ok-ng) > 1) { auto mid = (ok+ng)/2; if (unaryFun!pred(mid)) ok = mid; else ng = mid; } return ok; } void main() { auto t = RD!int; auto ans = new long[][](t); foreach (ti; 0..t) { auto x = RD; auto y = RD; if (x >= y) ans[ti] = [x-1, y]; else ans[ti] = [x-1, y]; } foreach (e; ans) writeln(e[0], " ", e[1]); stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto n = RD!int; auto a = new double[](n); auto ans = new long[](n); long diff; foreach (i; 0..n) { a[i] = RD!double; ans[i] = cast(long)a[i]; diff += ans[i]; } diff = -diff; foreach (i; 0..n) { if (diff == 0) break; if (a[i] == ans[i]) continue; if (sgn(a[i]) == sgn(diff)) { ans[i] += sgn(diff); diff -= sgn(diff); } } foreach (e; ans) writeln(e); stdout.flush(); debug readln(); }
D
void solve(string[] s){ foreach(elm; s){ writeln(elm); writeln(elm); } } void main(){ int[] hw = inln(); string[] s; foreach(_; 0..hw[0])s ~= readln().chomp(); solve(s); } 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.stdio,std.conv,std.string,std.algorithm; void main(){ for( int i;;++i ){ auto arg = readln().chomp().split; auto a = arg[0].to!int , b = arg[1].to!int; if( !a && !b ) break; if( a<b ){ writeln( a," ",b ); }else{ writeln( b," ",a ); } } }
D
void main(){ int r = _scan(); ( r^^2 ).writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import 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]]; } double norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); } double dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto n = RD!int; auto a = RDA!int; bool dfs(int l, int r, int x) { if (l >= r) return true; if (a[l] == a[r]) return dfs(l+1, r-1, x); bool res; if (a[l] == x || x == 0) res |= dfs(l+1, r, a[l]); if (a[r] == x || x == 0) res |= dfs(l, r-1, a[r]); return res; } ans[ti] = dfs(0, n-1, 0); } foreach (e; ans) { writeln(e ? "YES" : "NO"); } stdout.flush; debug readln; }
D
import std.stdio; import std.string; import std.algorithm; import std.conv; void main() { int[3] coins; int x, ans; for (int i = 0; i < 3; i++) { coins[i] = readln.chomp.to!int + 1; } x = readln.chomp.to!int; foreach (i; 0..coins[0]) { foreach (j; 0..coins[1]) { foreach (k; 0..coins[2]) { int sum = 500 * i + 100 * j + 50 * k; if (sum == x) { ans++; } else if (sum > x) { break; } } } } ans.writeln; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "a", long, "b"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { auto n = lread(); auto a = aryread(); if (a.lineup()) power_mod(2, n / 2).writeln(); else writeln(0); } bool lineup(T)(T ary) { long[long] dic; foreach (e; ary) { if (e in dic) dic[e]++; else dic[e] = 1; } bool ret = true; foreach (i; iota((ary.length % 2 ? 0 : 1), ary.length, 2)) { ret &= (i in dic && dic[i] == (i == 0 ? 1 : 2)); } return ret; } // 累積二乗法 long power_mod(long a, long n, long mod = MOD) { long ret = 1; while (n) { if (n % 2) { ret *= a; ret %= mod; } a^^=2; a %= mod; n >>= 1; } return ret % mod; } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.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; } string calc(Vec2 p0, Vec2 p1, Vec2 p2) { auto p01 = p1 - p0; auto p02 = p2 - p0; auto d = p01.cross(p02); if (d > 0) return "COUNTER_CLOCKWISE"; if (d < 0) return "CLOCKWISE"; // |p01||p02|cos(pi) ??§ cos(pi) = -1 ????????§?????????????????? if (p01.dot(p02) < 0) return "ONLINE_BACK"; if (p01.magSq() >= p02.magSq()) return "ON_SEGMENT"; return "ONLINE_FRONT"; } void main() { auto xs = readints(); auto p0 = Vec2(xs[0], xs[1]); auto p1 = Vec2(xs[2], xs[3]); int q = readint(); for (int i = 0; i < q; i++) { auto xy = readints(); auto p2 = Vec2(xy[0], xy[1]); auto ans = calc(p0, p1, p2); writeln(ans); } } struct Vec2 { immutable double x; immutable double y; this(double x, double y) { this.x = x; this.y = y; } Vec2 opAdd(Vec2 other) { return Vec2(this.x + other.x, this.y + other.y); } Vec2 opSub(Vec2 other) { return Vec2(this.x - other.x, this.y - other.y); } Vec2 opMul(double d) { return Vec2(this.x * d, this.y * d); } double dot(Vec2 other) { return this.x * other.x + this.y * other.y; } double cross(Vec2 other) { return this.x * other.y - other.x * this.y; } double mag() { return sqrt(magSq()); } double magSq() { return this.x * this.x + this.y * this.y; } Vec2 normalize() { auto m = mag(); if (m != 0 && m != 1) return Vec2(this.x / m, this.y / m); return this; } static double distance(Vec2 a, Vec2 b) { return (a - b).mag(); } }
D
// Vicfred // https://atcoder.jp/contests/abc156/tasks/abc156_c // brute force import std.algorithm; import std.array; import std.conv; import std.stdio; import std.string; void main() { int n = readln.chomp.to!int; int[] x = readln.split.map!(to!int).array; long minima = 1<<30; foreach(p; 1..101) { long dist = 0; foreach(person; x) { dist += (person-p)^^2; } minima = min(minima, dist); } minima.writeln; }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; import std.typecons; // import dcomp.foundation, dcomp.scanner; // import dcomp.array; // import dcomp.ldc.inline; int[] primeList(int n) { bool[] used = new bool[n+1]; FastAppender!(int[]) ans; foreach (i; 2..n+1) { if (used[i]) continue; ans ~= i; foreach (j; iota(i, n+1, i)) { used[j] = true; } } return ans.data; } int[long] fastFactor(ulong x, in BarrettULong[] primes) { int[long] mp; foreach (p; primes) { while (x%p == 0) { mp[p.v]++; x=x/p; } } if (x) mp[x]++; return mp; } long mul(long x, long y) { if (10L^^12 / x < y) return 10L^^12; return x*y; } int main() { auto sc = new Scanner(stdin); auto pr = primeList(100_100); auto bpr = pr.map!(x => BarrettULong(x)).array; int n; sc.read(n); int[2][long] g; bool one = false; foreach (i; 0..n) { long s; sc.read(s); // writeln(fastFactor(s, bpr)); long d0 = 1, d1 = 1; foreach (p, c; fastFactor(s, bpr)) { c %= 3; if (c == 1) { d0 = mul(d0, p); d1 = mul(d1, p); d1 = mul(d1, p); } else if (c == 2) { d0 = mul(d0, p); d0 = mul(d0, p); d1 = mul(d1, p); } } if (d0 == 1) { one = true; continue; } if (d0 < d1) { if (d0 in g) { g[d0][0]++; } else { g[d0] = [1, 0]; } } else { if (d1 in g) { g[d1][1]++; } else { g[d1] = [0, 1]; } } } int ans = 0; if (one) ans++; foreach (u; g) { ans += max(u[0], u[1]); } writeln(ans); return 0; } struct BarrettULong { ulong v, u; this(ulong x) { this.v = x; if (x == 1) return; version(LDC) { u = inlineIR!(` %r0 = zext i64 %0 to i128 %r1 = add i128 %r0, 18446744073709551615 %r2 = udiv i128 %r1, %r0 %r3 = trunc i128 %r2 to i64 ret i64 %r3`, ulong)(x); } else { asm { mov RBX, this; mov RDX, 1; mov RAX, x; dec RAX; div x; mov u[RBX], RAX; } } } ulong opBinaryRight(string op:"/")(ulong x) const { assert(v != 0); if (v == 1) return x; ulong r; version(LDC) { r = inlineIR!(` %r0 = zext i64 %0 to i128 %r1 = zext i64 %1 to i128 %r2 = mul i128 %r1, %r0 %r3 = lshr i128 %r2, 64 %r4 = trunc i128 %r3 to i64 ret i64 %r4`, ulong)(u, x); } else { asm { mov RBX, this; mov RAX, u[RBX]; mul x; mov r, RDX; } } return r; } ulong opBinaryRight(string op:"%")(ulong x) const { return x - x/this*v; } } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/array.d */ // module dcomp.array; T[N] fixed(T, int N)(T[N] a) {return a;} //this is not reference type!(please attention to copy) struct FastAppender(A) { import std.algorithm : max; import std.range.primitives : ElementEncodingType; import core.stdc.string : memcpy; private alias T = ElementEncodingType!A; private T* _data; private size_t len, cap; @property size_t length() {return len;} void reserve(size_t nlen) { import core.memory : GC; if (nlen <= cap) return; void* nx = GC.malloc(nlen * T.sizeof); cap = nlen; if (len) memcpy(nx, _data, len * T.sizeof); _data = cast(T*)(nx); } void opOpAssign(string op : "~")(T item) { if (len == cap) { reserve(max(4, cap*2)); } _data[len++] = item; } void clear() { len = 0; } T[] data() { return (_data) ? _data[0..len] : null; } } unittest { import std.stdio, std.algorithm; auto u = FastAppender!(int[])(); u ~= 4; u ~= 5; assert(equal(u.data, [4, 5])); } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; if (f.eof) return false; line = lineBuf[]; f.readln(line); } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { //string or char[10] etc //todo optimize auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { //static assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); fout.writeln("1 2 3"); fout.writeln("ab cde"); fout.writeln("1.0 1.0 2.0"); fout.close; Scanner sc = new Scanner(File(fileName, "r")); int a; int[2] b; char[2] c; string d; double e; double[] f; sc.read(a, b, c, d, e, f); assert(a == 1); assert(equal(b[], [2, 3])); assert(equal(c[], "ab")); assert(equal(d, "cde")); assert(e == 1.0); assert(equal(f, [1.0, 2.0])); } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File, writeln; import std.datetime; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); foreach (i; 0..1_000_000) { fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2); } fout.close; writeln("Scanner Speed Test(3*1,000,000 int)"); StopWatch sw; sw.start; Scanner sc = new Scanner(File(fileName, "r")); foreach (i; 0..500_000) { int a, b, c; sc.read(a, b, c); assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 500_000..700_000) { int[3] d; sc.read(d); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } foreach (i; 700_000..1_000_000) { int[] d; sc.read(d); assert(d.length == 3); int a = d[0], b = d[1], c = d[2]; assert(a == 3*i); assert(b == 3*i+1); assert(c == 3*i+2); } writeln(sw.peek.msecs, "ms"); } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/ldc/inline.d */ // module dcomp.ldc.inline; version(LDC) { pragma(LDC_inline_ir) R inlineIR(string s, R, P...)(P); } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; //fold(for old compiler) static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } unittest { import std.stdio; auto l = [1, 2, 3, 4, 5]; assert(l.fold!"a+b"(10) == 25); } }
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() { long H; scan(H); long[long] memo; long dfs(long h) { if (h == 0) { return 0; } if (h in memo) { return memo[h]; } return 2 * dfs(h / 2) + 1; } long ans = dfs(H); 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.stdio; import std.string; import std.format; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.concurrency; import std.traits; import std.uni; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; enum long INF = long.max/3; enum long MOD = 10L^^9+7; void main() { long A, B, C; scanln(A, B, C); writeln(min(C, B/A)); } // ---------------------------------------------- void times(alias fun)(long n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(long n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { // `(x+y-1)/y` will only work for positive numbers ... T t = x / y; if (t * y < x) t++; return t; } T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { T t = x / y; if (t * y > x) t--; return t; } ref T ch(alias fun, T, S...)(ref T lhs, S rhs) { return lhs = fun(lhs, rhs); } unittest { long x = 1000; x.ch!min(2000); assert(x == 1000); x.ch!min(3, 2, 1); assert(x == 1); x.ch!max(100).ch!min(1000); // clamp assert(x == 100); x.ch!max(0).ch!min(10); // clamp assert(x == 10); } mixin template Constructor() { import std.traits : FieldNameTuple; this(Args...)(Args args) { // static foreach(i, v; args) { foreach(i, v; args) { mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v; } } } void scanln(Args...)(auto ref Args args) { import std.meta; template getFormat(T) { static if (isIntegral!T) { enum getFormat = "%d"; } else static if (isFloatingPoint!T) { enum getFormat = "%g"; } else static if (isSomeString!T || isSomeChar!T) { enum getFormat = "%s"; } else { static assert(false); } } enum string fmt = [staticMap!(getFormat, Args)].join(" "); string[] inputs = readln.chomp.split; foreach(i, ref v; args) { v = inputs[i].to!(Args[i]); } } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { private template RebindableOrUnqual(T) { static if (is(T == class) || is(T == interface) || isDynamicArray!T || isAssociativeArray!T) alias RebindableOrUnqual = Rebindable!T; else alias RebindableOrUnqual = Unqual!T; } private auto extremum(alias map, alias selector = "a < b", Range)(Range r) if (isInputRange!Range && !isInfinite!Range && is(typeof(unaryFun!map(ElementType!(Range).init)))) in { assert(!r.empty, "r is an empty range"); } body { alias Element = ElementType!Range; RebindableOrUnqual!Element seed = r.front; r.popFront(); return extremum!(map, selector)(r, seed); } private auto extremum(alias map, alias selector = "a < b", Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seedElement) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void) && is(typeof(unaryFun!map(ElementType!(Range).init)))) { alias mapFun = unaryFun!map; alias selectorFun = binaryFun!selector; alias Element = ElementType!Range; alias CommonElement = CommonType!(Element, RangeElementType); RebindableOrUnqual!CommonElement extremeElement = seedElement; // if we only have one statement in the loop, it can be optimized a lot better static if (__traits(isSame, map, a => a)) { // direct access via a random access range is faster static if (isRandomAccessRange!Range) { foreach (const i; 0 .. r.length) { if (selectorFun(r[i], extremeElement)) { extremeElement = r[i]; } } } else { while (!r.empty) { if (selectorFun(r.front, extremeElement)) { extremeElement = r.front; } r.popFront(); } } } else { alias MapType = Unqual!(typeof(mapFun(CommonElement.init))); MapType extremeElementMapped = mapFun(extremeElement); // direct access via a random access range is faster static if (isRandomAccessRange!Range) { foreach (const i; 0 .. r.length) { MapType mapElement = mapFun(r[i]); if (selectorFun(mapElement, extremeElementMapped)) { extremeElement = r[i]; extremeElementMapped = mapElement; } } } else { while (!r.empty) { MapType mapElement = mapFun(r.front); if (selectorFun(mapElement, extremeElementMapped)) { extremeElement = r.front; extremeElementMapped = mapElement; } r.popFront(); } } } return extremeElement; } private auto extremum(alias selector = "a < b", Range)(Range r) if (isInputRange!Range && !isInfinite!Range && !is(typeof(unaryFun!selector(ElementType!(Range).init)))) { return extremum!(a => a, selector)(r); } // if we only have one statement in the loop it can be optimized a lot better private auto extremum(alias selector = "a < b", Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seedElement) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void) && !is(typeof(unaryFun!selector(ElementType!(Range).init)))) { return extremum!(a => a, selector)(r, seedElement); } auto minElement(alias map = (a => a), Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { return extremum!map(r); } auto minElement(alias map = (a => a), Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void)) { return extremum!map(r, seed); } auto maxElement(alias map = (a => a), Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { return extremum!(map, "a > b")(r); } auto maxElement(alias map = (a => a), Range, RangeElementType = ElementType!Range) (Range r, RangeElementType seed) if (isInputRange!Range && !isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void)) { return extremum!(map, "a > b")(r, seed); } } // popcnt with ulongs was added in D 2.071.0 static if (__VERSION__ < 2071) { ulong popcnt(ulong x) { x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L); x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L); x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL); x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL); x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL); x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL); return x; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; struct UFTree(T) { struct Node { T parent; T rank = 1; } /// T min_size, max_size; /// this(T n) { nodes.length = n; sizes.length = n; foreach (i, ref node; nodes) { node = Node(i.to!T); sizes[i] = 1; } min_sizes.length = n + 1; min_sizes[1] = n; min_size = 1; max_size = 1; } /// bool unite(T a, T b) { a = root(a); b = root(b); if (a == b) return false; auto a_size = sizes[a]; auto b_size = sizes[b]; --min_sizes[a_size]; --min_sizes[b_size]; ++min_sizes[a_size + b_size]; foreach (nxt_size; min(a_size, b_size)..min_sizes.length) if (min_sizes[nxt_size] != 0) { min_size = nxt_size.to!T; break; } max_size = max(max_size, a_size + b_size); if (nodes[a].rank < nodes[b].rank) { sizes[a] += sizes[b]; nodes[b].parent = a; } else { sizes[b] += sizes[a]; nodes[a].parent = b; if (nodes[a].rank == nodes[b].rank) ++nodes[b].rank; } return true; } /// bool is_same(T a, T b) { return root(a) == root(b); } /// T size(T i) { return sizes[root(i)]; } private: Node[] nodes; T[] sizes; T[] min_sizes; T root(T i) { if (nodes[i].parent == i) return i; return nodes[i].parent = root(nodes[i].parent); } } /// UFTree!T uftree(T)(T n) { return UFTree!T(n); } void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; auto ps = readln.split.to!(int[]); auto uft = uftree(N); foreach (_; 0..M) { auto xy = readln.split.to!(int[]); uft.unite(xy[0]-1, xy[1]-1); } int r; foreach (i, p; ps) { if (uft.is_same(i.to!int, p-1)) ++r; } writeln(r); }
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; long[] as; foreach (_; 0..N) as ~= readln.chomp.to!long; long min_p = 1; long r; foreach (a; as) { if (min_p == a) { min_p += 1; continue; } auto x = a / min_p; if (x == 0) continue; if (a%min_p == 0) { r += x-1; if (min_p == 1) min_p = 2; } else { r += x; } } writeln(r); }
D
void main() { long[] tmp = readln.split.to!(long[]); long n = tmp[0], k = tmp[1]; long[] lists = new long[10^^5+1]; foreach (i; 0 .. n) { tmp = readln.split.to!(long[]); long a = tmp[0], b = tmp[1]; lists[a] += b; } long num = 1; foreach (i, x; lists) { if (num <= k && k < num + x) { i.writeln; break; } num += x; } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nab = readln.split.to!(int[]); auto N = nab[0]; auto A = nab[1]; auto B = nab[2]; writeln(min(N*A, B)); }
D
import std.stdio; import std.string; import std.format; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.concurrency; import std.traits; import std.uni; import std.regex; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; enum long INF = long.max / 5; void main() { writeln(readln.chomp.to!long ^ 1); } // ---------------------------------------------- void times(alias fun)(long n) { // n.iota.each!(i => fun()); foreach (i; 0 .. n) fun(); } auto rep(alias fun, T = typeof(fun()))(long n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach (ref e; res) e = fun(); return res; } T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { // `(x+y-1)/y` will only work for positive numbers ... T t = x / y; if (y > 0 && t * y < x) t++; if (y < 0 && t * y > x) t++; return t; } T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) { T t = x / y; if (y > 0 && t * y > x) t--; if (y < 0 && t * y < x) t--; return t; } ref T ch(alias fun, T, S...)(ref T lhs, S rhs) { return lhs = fun(lhs, rhs); } unittest { long x = 1000; x.ch!min(2000); assert(x == 1000); x.ch!min(3, 2, 1); assert(x == 1); x.ch!max(100).ch!min(1000); // clamp assert(x == 100); x.ch!max(0).ch!min(10); // clamp assert(x == 10); } mixin template Constructor() { import std.traits : FieldNameTuple; this(Args...)(Args args) { // static foreach(i, v; args) { foreach (i, v; args) { mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v; } } } template scanln(Args...) { enum sep = " "; enum n = () { long n = 0; foreach (Arg; Args) { static if (is(Arg == class) || is(Arg == struct) || is(Arg == union)) { n += Fields!Arg.length; } else { n++; } } return n; }(); enum fmt = n.rep!(() => "%s").join(sep); enum argsString = () { string[] xs = []; foreach (i, Arg; Args) { static if (is(Arg == class) || is(Arg == struct) || is(Arg == union)) { foreach (T; FieldNameTuple!Arg) { xs ~= "&args[%d].%s".format(i, T); } } else { xs ~= "&args[%d]".format(i); } } return xs.join(", "); }(); void scanln(auto ref Args args) { string line = readln.chomp; static if (__VERSION__ >= 2074) { mixin( "line.formattedRead!fmt(%s);".format(argsString) ); } else { mixin( "line.formattedRead(fmt, %s);".format(argsString) ); } } } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // popcnt with ulongs was added in D 2.071.0 static if (__VERSION__ < 2071) { ulong popcnt(ulong x) { x = (x & 0x5555555555555555L) + (x >> 1 & 0x5555555555555555L); x = (x & 0x3333333333333333L) + (x >> 2 & 0x3333333333333333L); x = (x & 0x0f0f0f0f0f0f0f0fL) + (x >> 4 & 0x0f0f0f0f0f0f0f0fL); x = (x & 0x00ff00ff00ff00ffL) + (x >> 8 & 0x00ff00ff00ff00ffL); x = (x & 0x0000ffff0000ffffL) + (x >> 16 & 0x0000ffff0000ffffL); x = (x & 0x00000000ffffffffL) + (x >> 32 & 0x00000000ffffffffL); return x; } }
D
import std.conv, std.stdio; import std.algorithm, std.array, std.string, std.range; void main() { auto buf = readln.chomp.split.to!(int[]); (buf[0].c2 + buf[1].c2).writeln; } auto c2(in int n) { return n * (n - 1) / 2; }
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 N = scanElem; auto vList = scanArray; auto cList = scanArray; long res; foreach(i;0..N) { if(vList[i]>cList[i]) { res+=vList[i]-cList[i]; } } writeln(res); } 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
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 INF = 1L << 59; void main() { auto s = readln.split.map!(to!int); auto D = s[0]; auto G = s[1].to!long; auto PC = D.iota.map!(_ => readln.split.map!(to!long).array).array; int ans = 1 << 29; foreach (mask; 0..(1<<D)) { long tmp = 0; int cnt = 0; foreach (i; 0..D) { if (mask & (1 << i)) { tmp += PC[i][0] * (i + 1) * 100 + PC[i][1]; cnt += PC[i][0]; } } for (int i = D-1; i >= 0 && tmp < G; --i) { if (mask & (1 << i)) continue; long hoge = (G - tmp - 1) / ((i + 1) * 100) + 1; hoge = min(hoge, PC[i][0]); tmp += hoge * (i + 1) * 100; cnt += hoge; } if (tmp >= G) ans = min(ans, cnt); } ans.writeln; }
D
import std.stdio, std.math, std.algorithm, std.array, std.string, std.conv, std.container, std.range; T[] Reads(T)() { return readln.split.to!(T[]); } alias reads = Reads!int; void scan(Args...)(ref Args args) { string[] ss = readln.split; foreach (i, ref arg ; args) arg = ss[i].parse!int; } void main() { int x,y,z; scan(x,y,z); writeln(z, " ", x, " ",y); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); int id; int s12, s23, s31; int l, r; while(n--){ auto args=readln.split.to!(int[]); auto w=args[0], as=args[1..$]; int[] p=[1, 2, 3]; foreach(a; as){ if(a==0) swap(p[0], p[1]); else swap(p[1], p[2]); } auto o1=p[0], o2=p[1], o3=p[2]; if(o1==1 && o2==2 && o3==3) id++; if(o1==2 && o2==1 && o3==3) s12++; if(o1==1 && o2==3 && o3==2) s23++; if(o1==3 && o2==2 && o3==1) s31++; if(o1==2 && o2==3 && o3==1) l++; if(o1==3 && o2==1 && o3==2) r++; } if(id){ writeln("yes"); }else if(s12>=2 || s23>=2 || s31>=2){ writeln("yes"); }else if(l>=3 || r>=3){ writeln("yes"); }else if(l>=1 && r>=1){ writeln("yes"); }else if((l>=1 || r>=1) && s12>=1 && s23>=1){ writeln("yes"); }else if((l>=1 || r>=1) && s23>=1 && s31>=1){ writeln("yes"); }else if((l>=1 || r>=1) && s31>=1 && s12>=1){ 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)); } }
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 = 10L^^18; enum mod = 10L ^^ 9 + 7; void main() { int n; scan(n); auto a = readln.split.to!(long[]); auto as = new long[](n + 1); foreach (i ; 1 .. n + 1) { as[i] = as[i-1] + a[i-1]; } long ssum(int l, int r) { return as[r] - as[l]; } auto dp = new long[][](n + 1, n + 1); fillAll(dp, -1); long dfs(int l, int r) { if (dp[l][r] != -1) { return dp[l][r]; } if (r - l == 1) { return 0; } long ans = inf; foreach (i ; l+1 .. r) { ans = min(ans, ssum(l, r) + dfs(l, i) + dfs(i, r)); } return dp[l][r] = ans; } long ans = dfs(0, n); debug { writefln("%(%s\n%)", dp); } 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
// 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; import std.exception : enforce; int n, p; int[] a; void main() { scan(n, p); a = readln.split.to!(int[]); if (a.all!"!(a & 1)") { writeln(p == 0 ? 1L<<n : 0); return; } else { writeln(1L<<(n - 1)); return; } } 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.conv; import std.algorithm; import std.range; import std.string; import std.typecons; import std.math; import std.random; import std.range; import std.functional; import std.container; class t { int parent, left, right; this() { parent = left = right = -1; } int depth; int height; } void dep(t[] tt, int i, int d = 0) { if (i == -1) { return; } tt[i].depth = d; dep(tt, tt[i].left, d + 1); dep(tt, tt[i].right, d + 1); } int hei(t[] tt, int i) { auto a1 = 0, a2 = 0; if (tt[i].left != -1) a1 = hei(tt, tt[i].left) + 1; if (tt[i].right != -1) a2 = hei(tt, tt[i].right) + 1; tt[i].height = max(a1, a2); return tt[i].height; } void pre(t[] tt, int i) { if (i == -1) { return; } write(" ", i); pre(tt, tt[i].left); pre(tt, tt[i].right); } void in_(t[] tt, int i) { if (i == -1) { return; } in_(tt, tt[i].left); write(" ", i); in_(tt, tt[i].right); } void post(t[] tt, int i) { if (i == -1) { return; } post(tt, tt[i].left); post(tt, tt[i].right); write(" ", i); } void main() { auto c = readln.strip.to!int; auto tt = new t[c]; foreach (i; 0..c) tt[i] = new t(); foreach (i; 0..c) { auto a = readln().strip.split(" ").map!(to!int).array; if (a[1] != -1) { tt[a[0]].left = a[1]; tt[a[1]].parent = a[0]; } if (a[2] != -1) { tt[a[0]].right = a[2]; tt[a[2]].parent = a[0]; } } foreach (i; 0..c) { if (tt[i].parent == -1) { writeln("Preorder"); pre(tt, i); writeln(); writeln("Inorder"); in_(tt, i); writeln(); writeln("Postorder"); post(tt, i); writeln(); } } }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}} void main() { int a, b; readV(a, b); string s; readV(s); foreach (i, c; s) { if (i == a) { if (c != '-') { writeln("No"); return; } } else { if (c < '0' || c > '9') { writeln("No"); return; } } } writeln("Yes"); }
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 HS = readln.split.to!(int[]); int[] cs = [HS[0]-1, HS[0]]; foreach (h; HS[1..$]) { if (cs.length == 1) { auto c = cs[0]; if (h > c) { cs = [h-1, h]; } else if (c == h) { cs = [h]; } else { writeln("No"); return; } } else { if (h >= cs[1]) { cs = [h-1, h]; } else if (h == cs[0]) { cs = [h]; } else { writeln("No"); return; } } } writeln("Yes"); }
D
import std.algorithm; import std.conv; import std.numeric; import std.stdio; import std.string; void main() { int n = to!int(readln().chomp()); if (n % 2 == 0) { writeln(n); } else { writeln(n * 2); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; int max_a; foreach (a; readln.split.to!(int[])) { if (a == max_a + 1) { max_a = a; } } writeln(max_a == 0 ? -1 : (N - max_a)); }
D
import std.stdio, std.conv, std.string; void main(){ writeln(readln.chomp.to!int ^^3); }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; void main() { int N = readln.chomp.to!int; long[] as = readln.split.to!(long[]); writeln( 3L^^as.length - as.map!"a%2==0?2:1".reduce!"a*b" ); } // ---------------------------------------------- void scanln(Args...)(ref Args args) { foreach(i, ref v; args) { "%d".readf(&v); (i==args.length-1 ? "\n" : " ").readf; } // ("%d".repeat(args.length).join(" ") ~ "\n").readf(args); } void times(alias fun)(int n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { auto minElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto minimum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b < minimum) { element = a; minimum = b; } } return element; } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto maximum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b > maximum) { element = a; maximum = b; } } return element; } }
D
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.typecons, std.range, std.container, std.math, std.algorithm.searching, std.functional,std.mathspecial, std.numeric; void main(){ auto abc=readln.split.map!(to!int).array; ((abc[0]<=abc[2]&&abc[2]<=abc[1])?"Yes":"No").writeln; }
D
import std.stdio; import std.conv; import std.string; void main() { string s; while ((s=readln()).length != 0) { int cnt = 0; int n = to!(int)(chomp(s)); for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { for (int k = 0; k < 10; k++) { for (int l = 0; l < 10; l++) { if (i + j + k + l == n) cnt++; } } } } writeln(cnt); } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { auto S = sread(); auto cnt = new long[](S.length + 1); foreach (i; 0 .. S.length) if (S[i] == 'W') cnt[i + 1]++; foreach (i; 0 .. S.length) cnt[i + 1] += cnt[i]; long ans; foreach (i; 0 .. S.length) if (S[i] == 'B') { ans += cnt[$ - 1] - cnt[i + 1]; } writeln(ans); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); void main() { long a, b, c; scan(a, b, c); foreach (i; 1 .. 10 ^^ 3) { if ((a * i) % b == c) { writeln("YES"); return; } } writeln("NO"); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } }
D
import std; void main() { auto SW = readln.split.to!(int[]); auto S = SW[1], W = SW[0]; writeln(W <= S ? "unsafe" : "safe"); }
D
import std.stdio; import std.ascii; import std.conv; import std.string; import std.algorithm; import std.range; import std.functional; import std.math; import core.bitop; void main() { auto input = readln.chomp.split().map!(to!long); long n = input[0]; long k = input[1]; if (n % k == 0) { writeln("0"); } else { writeln("1"); } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { auto s = readln.chomp.to!(dchar[]).sort(); bool ok = s[0] == s[1] && s[1] != s[2] && s[2] == s[3]; 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
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto m=new int[](n), d=new int[](n); auto v=new int[](n), s=new int[](n); foreach(i; 0..n) rd(m[i], d[i], v[i], s[i]); int f(int v){ return (v-1)%360+1; } auto days=new int[](361); foreach(i; 0..n){ int st=(m[i]-1)*30+d[i]; int ed=st+v[i]-1; foreach(_; 0..3){ foreach(x; 1..1500){ if(x<st){ chmax(days[f(x)], max(0, s[i]-(st-x))); }else if(st<=x && x<=ed){ chmax(days[f(x)], s[i]); }else{// ed<x chmax(days[f(x)], max(0, s[i]-(x-ed))); } } st+=360; ed+=360; } } days[0]=100100100; writeln(reduce!(min)(days)); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } } void chmax(T1, T2)(ref T1 x, T2 y){ if(x<y) x=y; }
D
// cheese-cracker [2022-02-12] void solve(){ int n = scan!int; auto arr = scanArray; long summ = 0; bool f = 0; for(int i = 1; i < n-1; ++i){ if(arr[i] >= 2){ f = 1; } summ += arr[i]/2 + (arr[i] % 2 != 0); } if(f && !(n == 3 && arr[1] % 2 != 0) ){ writeln(summ); }else{ writeln(-1); } } void main(){ long tests = scan; // Toggle! while(tests--) solve; } /*_________________________*That's All Folks!*__________________________*/ import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math, std.numeric; string[] tk; alias tup = Tuple!(long, long); T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;} T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; } void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
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 = 1L << 50; enum mod = 10L^^9 + 7; void main() { int n; scan(n); auto h = readln.split.to!(int[]); auto a = readln.split.to!(int[]); auto sg = SegmentTree!(long, max, 0)(n + 1); long ans; foreach (i ; 0 .. n) { long t = a[i] + sg.query(0, h[i]); ans = max(ans, t); sg.assign(h[i], t); } writeln(ans); } struct SegmentTree(T, alias op, T initValue = 0) { import std.functional : binaryFun; alias f = binaryFun!(op); T[] _data; int _treeSize, _lineSize = 1; // 配列の要素数 n だけ与えて初期化 this(int n) { while (_lineSize < n) { _lineSize *= 2; } _treeSize = 2 * _lineSize - 1; _data = new T[](_treeSize); _data[] = initValue; } // 配列 a を与えて初期化 this(T[] a) { auto n = a.length; while (_lineSize < n) { _lineSize *= 2; } _treeSize = 2 * _lineSize - 1; _data = new T[](_treeSize); _data[] = initValue; _data[_lineSize - 1 .. _lineSize - 1 + n] = a[]; foreach_reverse (i; 0 .. _lineSize - 1) { _data[i] = f(_data[2 * i + 1], _data[2 * i + 2]); } } // a[i] = x void assign(int i, T x) { i += _lineSize - 1; _data[i] = x; while (i > 0) { i = (i - 1) / 2; _data[i] = f(_data[2 * i + 1], _data[2 * i + 2]); } } // a[i] = f(a[i], x) void act(int i, T x) { i += _lineSize - 1; _data[i] = f(_data[i], x); while (i > 0) { i = (i - 1) / 2; _data[i] = f(_data[2 * i + 1], _data[2 * i + 2]); } } alias add = act; // 要求区間 [a, b) に対して、対象区間 [l, r) の値を返す T query(int a, int b, int k = 0, int l = 0, int r = -1) { if (r == -1) { r = _lineSize; } if (a >= b) { // 要求区間が空である場合 return initValue; } if (b <= l || r <= a) { // 要求区間が対象区間外の場合 return initValue; } if (a <= l && r <= b) { // 要求区間が対象区間に含まれている場合 return _data[k]; } // どちらでもない場合は対象区間を2つに分けて再帰する auto leftValue = query(a, b, 2 * k + 1, l, l + (r - l) / 2); auto rightValue = query(a, b, 2 * k + 2, l + (r - l) / 2, r); return f(leftValue, rightValue); } } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto S = readln.chomp; for (;;) { if (S.empty) break; if (S.length >= 2 && S[0..2] == "hi") { S = S[2..$]; } else { writeln("No"); return; } } writeln("Yes"); }
D
import std.stdio ,std.conv , std.string; void main(){ auto input = readln(); foreach( i,c ; input ){ if(i%2==1){continue;} write(c); } writeln(""); } unittest{ } T[] readLine( T = size_t )(){ T[] ret; foreach( val ; readln().chomp().split() ){ ret ~= to!T(val); } return ret; }
D
void main() { problem(); } void problem() { const N = scan!long; const An = scan!long(cast(int)(N-1)); void solve() { long[long] members; foreach(i; 1..N+1) members[i] = 0; foreach(a; An) { members[a]++; } foreach(i; 1..N+1) writeln(members[i]); } solve(); } // ---------------------------------------------- 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
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);} auto g = [0, 2, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0]; void main() { int x, y; readV(x, y); --x; --y; writeln(g[x] == g[y] ? "Yes" : "No"); }
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 n, m; readV(n, m); auto g1 = Graph!()(n), g2 = GraphW!()(n); foreach (_; 0..m) { int l, r, d; readV(l, r, d); --l; --r; g1.addEdge(l, r); g2.addEdge(l, r, d); } auto t = topologicalSort(g1), x = new int[](n); x[] = -1; if (t.length != n) { writeln("No"); return; } foreach (ti; t) { foreach (e; g2[ti]) { if (x[ti] == -1) x[ti] = 0; auto nt = e.dst, nx = x[ti] + e.wt; if (x[nt] != -1 && x[nt] != nx) { writeln("No"); return; } x[nt] = nx; } } writeln("Yes"); } struct Graph(N = int) { alias Node = N; Node n; Node[][] g; alias g this; this(Node n) { this.n = n; g = new Node[][](n); } void addEdge(Node u, Node v) { g[u] ~= v; } void addEdgeB(Node u, Node v) { g[u] ~= v; g[v] ~= u; } } struct GraphW(N = int, W = int, W i = 10^^9) { alias Node = N, Wt = W, inf = i; struct Edge { Node src, dst; Wt wt; alias cap = wt; } Node n; Edge[][] g; alias g this; this(Node n) { this.n = n; g = new Edge[][](n); } void addEdge(Node u, Node v, Wt w) { g[u] ~= Edge(u, v, w); } void addEdgeB(Node u, Node v, Wt w) { g[u] ~= Edge(u, v, w); g[v] ~= Edge(v, u, w); } } auto topologicalSort(Graph)(ref Graph g) { import std.container; alias Node = g.Node; auto n = cast(Node)(g.length), h = new int[](n); foreach (u; 0..n) foreach (v; g[u]) ++h[v]; auto st = SList!Node(); foreach (i; 0..n) if (h[i] == 0) st.insertFront(i); Node[] ans; while (!st.empty()) { auto u = st.front; st.removeFront(); ans ~= u; foreach (v; g[u]) { --h[v]; if (h[v] == 0) st.insertFront(v); } } return ans; }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n, i; readV(n, i); writeln(n-i+1); }
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 n = RD!int; ans[ti] = n / 2; } foreach (e; ans) { writeln(e); } 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; void main() { auto s = readln.split.map!(to!long); auto N = s[0].to!int; auto C = s[1]; auto X = new long[](N); auto V = new long[](N); foreach (i; 0..N) { s = readln.split.map!(to!long); X[i] = s[0]; V[i] = s[1]; } auto dp1 = new long[](N); auto dp2 = new long[](N); dp1[0] = V[0] - X[0]; dp2[N-1] = V[N-1] - (-X[N-1] + C); foreach (i; 1..N) { dp1[i] = dp1[i-1] + V[i] - (X[i] - X[i-1]); } foreach_reverse (i; 0..N-1) { dp2[i] = dp2[i+1] + V[i] - (X[i+1] - X[i]); } long ans = max(0, dp1.reduce!max, dp2.reduce!max); auto dpdp1 = new long[](N); auto dpdp2 = new long[](N); dpdp1[0] = dp1[0]; dpdp2[N-1] = dp2[N-1]; foreach (i; 1..N) dpdp1[i] = max(dp1[i], dpdp1[i-1]); foreach_reverse (i; 0..N-1) dpdp2[i] = max(dp2[i], dpdp2[i+1]); foreach (i; 0..N-1) { long tmp = dp1[i] - X[i] + dpdp2[i+1]; ans = max(ans, tmp); } foreach_reverse (i; 1..N) { long tmp = dp2[i] - (-X[i]+C) + dpdp1[i-1]; ans = max(ans, tmp); } ans.writeln; }
D
import std.algorithm; import std.container; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.traits; class UnionFind(T) if (isIntegral!T) { private: T[] rank; T[] par; T size; public: this(T size) { rank = repeat(0.to!T).take(size).array; par = iota(size).array; this.size = size; } T length() { return size; } T getRoot(T n) { return par[n] = (par[n] == n ? n : getRoot(par[n])); } bool unite(T n1, T n2) { n1 = getRoot(n1); n2 = getRoot(n2); if (n1 == n2) return false; bool t = rank[n1] < rank[n2]; par[t ? n1 : n2] = t ? n2 : n1; if (rank[n1] == rank[n2]) rank[n1]++; return true; } bool haveSameRoot(T n1, T n2) { return getRoot(n1) == getRoot(n2); } T countRoots() { T roots = 0; par.each!((i, v){ if (getRoot(v) == i) roots++; }); return roots; } } void main() { auto nm = readln.chomp.split.to!(int[]); auto n = nm[0]; auto m = nm[1]; auto uf = new UnionFind!int(n); iota(m).each!((_){ auto xyz = readln.chomp.split.to!(int[]); uf.unite(xyz[0] - 1, xyz[1] - 1); }); uf.countRoots.writeln; }
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 S = readln.strip; long res; foreach(c;S)res+=c=='+'?1:-1; writeln(res); } 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
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std; // dfmt on void main() { long N = lread(); writeln(0); stdout.flush(); long ok = 0; long ng = N; auto S = sread(); if (S == "Vacant") return; while (true) { long m = (ok + ng) / 2; writeln(m); stdout.flush(); auto s = sread(); if (s == "Vacant") return; if ((S != s) == (m & 1)) { ok = m; } else { ng = m; } } }
D
import std.stdio; import std.string; import std.array; import std.range; import std.algorithm; import std.conv; import std.typecons; string disp(bool result){ if(result){ return "Yay!"; }else{ return ":("; } } bool solve(int a, int b){ if(a <= 8 && b <= 8){ return true; }else{ return false; } } void main(){ auto ab = readln().chomp().split().map!(to!int).array(); auto a = ab[0]; auto b = ab[1]; solve(a, b).disp().writeln(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto S = readln.chomp; while (!S.empty) { if (S.length >= 5 && S[$-5..$] == "dream") { S = S[0..$-5]; } else if (S.length >= 7 && S[$-7..$] == "dreamer") { S = S[0..$-7]; } else if (S.length >= 5 && S[$-5..$] == "erase") { S = S[0..$-5]; } else if (S.length >= 6 && S[$-6..$] == "eraser") { S = S[0..$-6]; } else { writeln("NO"); return; } } writeln("YES"); }
D
import std.array; import std.range; import std.stdio; import std.conv; import std.string; import std.algorithm; int bsearch(int[] a, int b) { size_t lo=0,hi=a.length-1; while(hi>=lo) { size_t mi = (hi+lo)/2; if(a[mi] > b) hi = mi - 1; else lo = mi + 1; } return a[hi]; } int[] uniq(int[] a) { int[] res; foreach(v; a) if(res.empty||res[$-1] != v) res ~= v; return res; } void main() { while(true) { int N,M; scanf("%d%d", &N, &M); if(N==0&&M==0) break; int[] inp = new int[](N+1); inp[0] = 0; foreach(i; 0..N) { scanf("%d", &inp[i+1]); } N++; int[] add2 = new int[](N*N); foreach(i; 0..N) foreach(j; 0..N) add2[j*N + i] = inp[i] + inp[j]; add2.sort; int[] a = uniq(add2); int res; foreach(v; a) { if(v > M) break; res = max(res, v + bsearch(a, M-v)); } writeln(res); } }
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); long[long] mp; foreach (i; 0..n) { long a, b; sc.read(a, b); mp[a] = b; } int m; sc.read(m); foreach (i; 0..m) { long a, b; sc.read(a, b); if (a !in mp) mp[a] = b; else mp[a] = max(mp[a], b); } writeln(mp.values.sum); return 0; } /* IMPORT /mnt/c/Users/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 /mnt/c/Users/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(Args...)(auto ref Args args) { import std.exception; static if (args.length != 0) { enforce(readSingle(args[0])); read(args[1..$]); } } bool hasNext() { return succ(); } } /* IMPORT /mnt/c/Users/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); } } } } /* 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
/+ dub.sdl: name "B" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner, dcomp.algorithm; int main() { auto sc = new Scanner(stdin); int n, m; sc.read(n, m); int[][] g = new int[][](n, m); foreach (i; 0..n) { foreach (j; 0..m) { sc.read(g[i][j]); g[i][j]--; } } int[] l = new int[n]; int[] cnt = new int[m]; n.iota.each!(x => cnt[g[x][0]]++); bool[] used = new bool[m]; int ans = cnt.maximum; while (true) { foreach (i; 0..m) { if (cnt[i] >= ans) { used[i] = true; } } foreach (i; 0..n) { while (l[i] < m && used[g[i][l[i]]]) { cnt[g[i][l[i]]]--; l[i]++; if (l[i] < m) cnt[g[i][l[i]]]++; } } if (l.maximum == m) break; ans = min(ans, cnt.maximum); } writeln(ans); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } version (X86) static if (__VERSION__ < 2071) { import core.bitop : bsf, bsr, popcnt; int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; if (f.eof) return false; line = lineBuf[]; f.readln(line); } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/algorithm.d */ // module dcomp.algorithm; import std.range.primitives; 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)/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; } 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, "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, "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() { return now.empty; } @property auto front() { assert(!now.empty); import std.range : take, chain; return chain(now, start.take(cnt)); } @property Rotator!Range save() { return this; } void popFront() { cnt++; now.popFront; } }
D
import std.algorithm.comparison; import std.conv; import std.stdio; import std.string; void main() { auto x = readln.strip.to!int; auto result = 1; foreach( int b; 1 .. x ) { foreach( int p; 2 .. x ) { auto bp = b ^^ p; if( x < bp ) break; result = max( result, bp ); } } writeln( result ); }
D
import std.stdio; import std.conv; import std.algorithm; import std.string; import std.array; void main() { bool[] state = new bool[](3); state[0] = true; state[1] = false; state[2] = false; while(true) { string line = readln.chomp; if (stdin.eof) break; auto data = line.split(",").map!((x) => to!(uint)(x[0] - 'A')).array; swap(state[data[0]], state[data[1]]); } foreach(i, s; state) if(s) writeln(to!(char)(i + 'A')); }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto s = readln.strip.dup; auto n = s.length.to !(int); string res; foreach (i; 0..n + 1) { if (i < n) { s[i] ^= 3; } if (s.count ("ab") == s.count ("ba")) { res = s.idup; } if (i < n) { s[i] ^= 3; } } writeln (res); } }
D
import std.stdio; import std.string; size_t lb(int[10][] arr, int n, int key) { long left = -1; long right = arr.length; while(right - left > 1) { long mid = left + (right - left) / 2; if (arr[mid][n] >= key) right = mid; else left = mid; } return right; } void main() { int n; scanf("%d\n", &n); auto s = readln.chomp; int[10][] lad = new int[10][n]; foreach(j; 0..10) { if (j == s[0] - 48) { ++lad[0][j]; } } foreach(i; 1..n) { foreach(j; 0..10) { lad[i][j] = lad[i-1][j]; if (j == s[i] - 48) { ++lad[i][j]; } } } int cnt; foreach(i; 0..10) { foreach(j; 0..10) { foreach(k; 0..10) { auto si = lb(lad, i, 1); if (si >= n) continue; auto sj = lb(lad, j, 1 + lad[si][j]); if (sj >= n) continue; auto sk = lb(lad, k, 1 + lad[sj][k]); if (sk < n) ++cnt; } } } cnt.write; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD; auto cnt = new long[](64); cnt[0] = 1; foreach (i; 1..64) { cnt[i] = cnt[i-1] * 2 + 1; } foreach (i; 0..64) { auto bit = 1L << i; if (n & bit) { ans[ti] += cnt[i]; } } } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { writeln(readln[5..7].to!int > 4 ? "TBD" : "Heisei"); }
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 int[][][](t); foreach (ti; 0..t) { auto n = RD!int; auto a = RDA!int; auto set = new bool[](n); int m; int[] arr; foreach (i; 0..n) { if (set[a[i]]) break; set[a[i]] = true; m.chmax(a[i]); if (m == i+1) { arr ~= i+1; } } auto set2 = new bool[](n); int m2; int[] arr2; foreach_reverse (i; 0..n) { if (set2[a[i]]) break; set2[a[i]] = true; m2.chmax(a[i]); if (m2 == n-i) { arr2 ~= i; } } arr2.reverse; int l; debug writeln(arr); debug writeln(arr2); (){ if (arr2.empty) return; foreach (e; arr) { while (arr2[l] < e) { ++l; if (l == arr2.length) return; } if (e == arr2[l]) { ans[ti] ~= [e, n - e]; } }}(); } foreach (e; ans) { writeln(e.length); foreach (ee; e) writeln(ee[0], " ", ee[1]); } stdout.flush; debug readln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { int n; scan(n); writeln(n*(n + 1) / 2); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { readint; auto ab = readints; int a = ab[0], b = ab[1]; auto ps = readints; auto n1 = ps.count!(e => e <= a); auto n2 = ps.count!(e => a < e && e <= b); auto n3 = ps.count!(e => e > b); writeln(min(n1, n2, n3)); }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int a, b, c, d; rd(a, b, c, d); writeln(max(0, min(b, d) - max(a, c))); } 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.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; void main() { readln.chomp.pipe!(s => writeln(s.front, s.length-2, s.back)); } // ---------------------------------------------- void times(alias fun)(int n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { auto minElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto minimum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b < minimum) { element = a; minimum = b; } } return element; } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto maximum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b > maximum) { element = a; maximum = b; } } return element; } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.math; import std.array; import std.range; import std.regex; void main(){ auto a=readln.chomp.to!int; if(a<1200)writeln("ABC"); else if(a>=1200&&a<2800)writeln("ARC"); else if(a>=2800)writeln("AGC"); }
D
import std.stdio; import std.conv; import std.algorithm; import std.string; import std.file; import std.math; int main() { string l; l = readln().split("\n")[0]; string res = ""; foreach(char x; l) res = x ~ res; write(res ~ "\n"); return 0; }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; int t; rd(t); while (t--) { long n, v, l, r; rd(n, v, l, r); auto num = (l - 1) / v; num += n / v - r / v; writeln(num); } } 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.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto s = readln.chomp; foreach (i, c; s) { if (i >= 1 && s[i-1] == c) { writeln(i, " ", i+1); return; } if (i >= 2 && s[i-2] == c) { writeln(i-1, " ", i+1); return; } } writeln("-1 -1"); }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; import std.concurrency; import core.bitop : popcnt; alias Generator = std.concurrency.Generator; const long INF = long.max/3; const long MOD = 10L^^9+7; void main() { writeln(48 - readln.chomp.to!int); } // ---------------------------------------------- void scanln(Args...)(ref Args args) { foreach(i, ref v; args) { "%d".readf(&v); (i==args.length-1 ? "\n" : " ").readf; } // ("%d".repeat(args.length).join(" ") ~ "\n").readf(args); } void times(alias fun)(int n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { auto minElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto minimum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b < minimum) { element = a; minimum = b; } } return element; } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto maximum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b > maximum) { element = a; maximum = b; } } return element; } }
D
import 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 N, T; scan(N, T); auto t = aryread(); long l = 0; long ans; foreach (i; 0 .. N) { ans += min(T, t[i] + T - l); l = t[i] + T; // writeln(l); } writeln(ans); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto s = RD!string; long cnt1, cnt2; foreach (c; s) { if (c == ')') { if (cnt1 > 0) { --cnt1; ++ans[ti]; } } else if (c == '(') { ++cnt1; } else if (c == ']') { if (cnt2 > 0) { --cnt2; ++ans[ti]; } } else { ++cnt2; } } } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
import std.stdio, std.string, std.conv; import std.array, std.algorithm, std.range; bool prime(int n) { for(int i=2; i*i<=n; ++i) if(n%i==0) return false; return true; } void main() { int[] ns; int m=0; for(int n; 0!=(n=readln().chomp().to!int()); ) m=max(m,n),ns~=n; auto a = new long[m+1]; for(int i=1,n=2; i<=m; ++n) if(prime(n)) a[i]=a[i-1]+n,++i; ns.map!(n=>a[n].to!string()).join("\n").writeln(); }
D
void main() { long a, b, c, d; rdVals(a, b, c, d); if (abs(c- a) <= d) "Yes".writeln; else if (abs(b - a) <= d && abs(c - b) <= d) "Yes".writeln; else "No".writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { auto s = readln.chomp; writeln(s.canFind('9') ? "Yes" : "No"); }
D
void main() { long[] tmp = rdRow; long n = tmp[0], k = tmp[1], s = tmp[2]; long t; if (n - k) { if (s == 10L ^^ 9) t = 1; else t = 10L ^^ 9; } foreach (i; 0 .. n) { if (i < k) s.write; else t.write; if (i == n - 1) writeln; else " ".write; } } T rdElem(T = long)() { //import std.stdio : readln; //import std.string : chomp; //import std.conv : to; return readln.chomp.to!T; } alias rdStr = rdElem!string; dchar[] rdDchar() { //import std.conv : to; return rdStr.to!(dchar[]); } T[] rdRow(T = long)() { //import std.stdio : readln; //import std.array : split; //import std.conv : to; return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdRow!T).array; } void wrMat(T = long)(T[][] mat) { //import std.stdio : write, writeln; foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.string, std.array, std.conv, std.algorithm; char[10^^5] SS; char conv(char who, char what, char whom) { switch (who) { case 'S': switch (what) { case 'o': switch (whom) { case 'S': return 'S'; case 'W': return 'W'; default: } break; case 'x': switch (whom) { case 'S': return 'W'; case 'W': return 'S'; default: } break; default: } break; case 'W': switch (what) { case 'o': switch (whom) { case 'S': return 'W'; case 'W': return 'S'; default: } break; case 'x': switch (whom) { case 'S': return 'S'; case 'W': return 'W'; default: } break; default: } break; default: } throw new Exception("Invalid charactor. [" ~ who ~ ", " ~ what ~ ", " ~ whom ~ "]"); } char[] solve(long n, char[] zoo) { zoo.length = n+1; for (size_t i = 2; i <= n; ++i) zoo[i] = conv(zoo[i-1], SS[i-1], zoo[i-2]); if (zoo[0] == zoo[n] && zoo[n-1] == conv(zoo[0], SS[0], zoo[1])) return zoo[0..n]; return null; } void main() { auto n = readln.chomp.to!long; foreach (i, s; readln.chomp) SS[i] = s; auto ret = solve(n, ['S', 'S']); if (!ret) ret = solve(n, ['S', 'W']); if (!ret) ret = solve(n, ['W', 'S']); if (!ret) ret = solve(n, ['W', 'W']); if (ret) writeln(ret); else writeln("-1"); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { writeln((lread() + lread()) * lread() / 2); }
D
void main(){ import std.stdio, std.conv, std.string, std.algorithm; long n, a, b, k; rd(n, a, b, k); const long mod=998244353; const int M=1_000_00*4; static fact=new long[](M); static inv_fact=new long[](M); { // init fact[0]=fact[1]=1; foreach(i; 2..M) fact[i]=i*fact[i-1]%mod; long powmod(long a, long x){ if(x==0) return 1; else if(x==1) return a; else if(x&1) return a*powmod(a, x-1)%mod; else return powmod(a*a%mod, x/2); } foreach(i; 0..M) inv_fact[i]=powmod(fact[i], mod-2); } long comb(long nn, long rr){ if(nn<rr) return 0; long ret=fact[nn]%mod; (ret*=inv_fact[rr])%=mod; (ret*=inv_fact[nn-rr])%=mod; return ret; } long tot=0; for(long x=0; x<=n; x++)if(k-a*x>=0){ if((k-a*x)%b==0){ long y=(k-a*x)/b; if(y>n) continue; (tot+=comb(n, x)*comb(n, y)%mod)%=mod; } } writeln(tot); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x) e=l[i].to!(typeof(e)); }
D
import std.stdio, std.conv, std.string, std.range, std.array, std.algorithm; import std.bigint; const char[] c = ['I', 'V', 'X', 'L', 'C', 'D', 'M']; const int[] a = [1, 5, 10, 50, 100, 500, 1000]; void main() { while (true){ string s = readln().chomp(); if (s == "") break; int sum, l; for (int i = 0; i < s.length; i++){ for (int j = 0; j < 7; j++){ if (s[i] == c[j]){ if (l >= a[j]){ sum += a[j]; } else { sum += a[j] - l * 2; } l = a[j]; } } } writeln(sum); } }
D
import std.stdio, std.conv, std.array, std.string; import std.algorithm; import std.container; import std.range; import core.stdc.stdlib; import std.math; import std.typecons; void main() { auto N = readln.chomp.to!int; auto An = readln.split.to!(long[]); auto total = An.sum; long current_length_twice; long cost; foreach(index; 0..N) { current_length_twice += An[index]*2; if (current_length_twice == total) break; if (current_length_twice > total) { long previous_length_twice = current_length_twice - An[index]*2; long shrinkable_length = current_length_twice/2 - index - 1; long adjust_size_shrink = current_length_twice - total; long adjust_size_enlong = total - previous_length_twice; if (adjust_size_shrink < adjust_size_enlong && shrinkable_length >= adjust_size_shrink) { cost = adjust_size_shrink; } else { cost = adjust_size_enlong; } break; } } cost.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.concurrency; void main() { readln.chomp.to!int.recurrence!"a[n-1]&(a[n-1]-1)".until!"a==0".array.back.writeln; } // ---------------------------------------------- void times(alias fun)(int n) { // n.iota.each!(i => fun()); foreach(i; 0..n) fun(); } auto rep(alias fun, T = typeof(fun()))(int n) { // return n.iota.map!(i => fun()).array; T[] res = new T[n]; foreach(ref e; res) e = fun(); return res; } // fold was added in D 2.071.0 static if (__VERSION__ < 2071) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { return reduce!fun(tuple(seed), r); } } } } // cumulativeFold was added in D 2.072.0 static if (__VERSION__ < 2072) { template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } } // minElement/maxElement was added in D 2.072.0 static if (__VERSION__ < 2072) { auto minElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto minimum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b < minimum) { element = a; minimum = b; } } return element; } auto maxElement(alias map, Range)(Range r) if (isInputRange!Range && !isInfinite!Range) { alias mapFun = unaryFun!map; auto element = r.front; auto maximum = mapFun(element); r.popFront; foreach(a; r) { auto b = mapFun(a); if (b > maximum) { element = a; maximum = b; } } return element; } }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; auto s=readln.chomp.to!(char[]); writeln(s[0..($-8)]); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.stdio; import std.conv; import std.string; import std.format; void main() { string s = chomp(readln()); int a = to!int(s.split(" ")[0]); int b = to!int(s.split(" ")[1]); int c = to!int(s.split(" ")[2]); if ( a < b && b < c ) { writeln("Yes"); } else { writeln("No"); } }
D
import std.algorithm; import std.array; import std.ascii; import std.bigint; import std.complex; import std.container; import std.conv; import std.functional; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } void readlnTo(T...)(ref T t) { auto s = readln().split(); assert(s.length == t.length); foreach(ref ti; t) { ti = s[0].to!(typeof(ti)); s = s[1..$]; } } const real eps = 1e-10; const long p = 1_000_000_000 + 7; bool test(ref long[] h, long a, long b, long n) { long m = 0; foreach(hi; h) { auto r = max(0, (hi - b*n)); m += (r+(a-b)-1)/(a-b); } return m <= n; } void main(){ long n, a, b; readlnTo(n, a, b); long[] h; foreach(i; iota(n)) { h ~= readLong(); } long u = int.max/2; long l = 0; while(l+1 < u) { auto m = (u+l)/2; if(test(h, a, b, m)) { u = m; } else { l = m; } } writeln(u); }
D
void main() { int[] tmp = readln.split.to!(int[]); int n = tmp[0], m = tmp[1]; writeln((n - 1) * (m - 1)); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio; import std.algorithm; import std.string; import std.functional; import std.array; import std.conv; import std.math; import std.typecons; import std.regex; import std.range; void main(){ string[8] map; for(int i=0;i<8;i++) map[i] = readln().chomp(); writeln(90); for(int j=0;j<8;j++){ for(int k=0;k<8;k++){ write(map[7-k][j]); } writeln(); } writeln(180); for(int j=0;j<8;j++){ for(int k=0;k<8;k++){ write(map[7-j][7-k]); } writeln(); } writeln(270); for(int j=0;j<8;j++){ for(int k=0;k<8;k++){ write(map[k][7-j]); } writeln(); } }
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 infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; int[] di = [1, 0, -1, 0]; int[] dj = [0, 1, 0, -1]; void main() { int h, w; scan(h, w); auto ban = iota(h).map!(i => readln.chomp).array; int black, white; auto visited = new bool[][](h, w); // 0 : #, 1 : . void dfs(int i, int j, int bh) { visited[i][j] = true; if (bh) { white++; } else { black++; } foreach (k ; 0 .. 4) { int ni = i + di[k]; int nj = j + dj[k]; if (ni < 0 || ni >= h || nj < 0 || nj >= w) continue; if (visited[ni][nj]) continue; if (bh && ban[ni][nj] != '#') continue; if (!bh && ban[ni][nj] != '.') continue; dfs(ni, nj, bh ^ 1); } } long ans; foreach (i ; 0 .. h) { foreach (j ; 0 .. w) { if (ban[i][j] == '#' && !visited[i][j]) { black = white = 0; dfs(i, j, 0); debug { writefln("%(%(%b %)\n%)", visited); } debug { writefln("(%d, %d), %d, %d", i, j, black, white); } ans += 1L * black * white; } } } 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; } long powmod(long x, long y, long mod = 1_000_000_007L) { long res = 1L; while (y > 0) { if (y & 1) { (res *= x) %= mod; } (x *= x) %= mod; y >>= 1; } return res; }
D
import std.stdio, std.string, std.conv; void main() { string s = readln.chomp; long mod = 1_000_000_007; long[][] dp = new long[][](100001, 13); dp[0][0] = 1; foreach (i, x; s) { if (x == '?') { foreach (j; 0 .. 13) { foreach (k; 0 .. 10) { dp[i+1][(10*j+k)%13] = (dp[i+1][(j*10+k)%13] + dp[i][j]) % mod; } } } else { foreach (j; 0 .. 13) { dp[i+1][(10*j+(x-'0'))%13] = (dp[i+1][(10*j+(x-'0'))%13] + dp[i][j]) % mod; } } } dp[s.length][5].writeln; }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.array; import std.math; void main(){ auto n=readln.chomp.to!int; auto a=readln.chomp.to!int; if(n%500<=a)writeln("Yes"); else writeln("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; void main() { int Q = readln.chomp.to!int; foreach(int i; 0..Q) { int[] input = readln.split.to!(int[]); int c = input[0]; int b = input[1]; int n = input[2]; int N = 0; for (; n>0; n--) { if (c>0 && b>0) { N++; c--; b--; } else { break; } } for (; b>0; b--) { if (c>1) { N++; c-=2; } } N += c/3; N.writeln; } }
D