code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio,std.conv,std.string; void main(){ auto m=readln.chomp.to!int; auto t=m/500; auto f=(m%500)/5; (t*1000+f*5).writeln; }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int a, b; string op; readV(a, op, b); writeln(op == "+" ? a+b : a-b); }
D
import std.stdio, std.string, std.conv, std.algorithm; void main() { readln; auto S = readln.chomp.map!(a => a == 'I' ? 1 : -1); auto c = 0; auto res = 0; foreach (s; S) { c += s; res = max(res, c); } writeln(res); }
D
import std.stdio; import std.conv; import std.string; void main() { int N = to!int(readln.chomp); int i = 1; while (i * i <= N) { i++; } i--; i *= i; i.writeln; }
D
void main() { int n = readln.chomp.to!int; long[] lucas = new long[n+1]; lucas[0] = 2, lucas[1] = 1; foreach (i; 2 .. n+1) { lucas[i] = lucas[i-1] + lucas[i-2]; } lucas[n].writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } double euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); } double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; } double norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); } double dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto m = RD!int; auto rb = RD!int-1; auto cb = RD!int-1; auto rd = RD!int-1; auto cd = RD!int-1; int dr = 1, dc = 1; while (true) { if (rb == rd || cb == cd) break; if (rb + dr >= n) dr = -1; if (rb + dr < 0) dr = 1; if (cb + dc >= m) dc = -1; if (cb + dc < 0) dc = 1; rb += dr; cb += dc; ++ans[ti]; } } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; int [] prepare (int limit) { auto res = limit.iota.array; for (int d = 2; d * d < limit; d++) { if (res[d] == d) { for (int e = d * d; e < limit; e += d) { if (res[e] == e) { res[e] = d; } } } } return res; } int [] roots (int limit) { auto p = prepare (limit); auto res = new int [limit]; foreach (d; 0..limit) { int cur = d; res[d] = 1; while (cur > 1) { bool parity = 0; int x = p[cur]; while (cur % x == 0) { cur /= x; parity ^= 1; } if (parity) { res[d] *= x; } } } return res; } immutable int limit = 10 ^^ 6 + 5; void main () { auto r = roots (limit); auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto n = readln.strip.to !(int); auto a = readln.splitter.map !(to !(int)).array; int [int] nums; foreach (ref c; a) { nums[r[c]] += 1; } auto answer = [0, 0, 0]; foreach (step; 0..3) { debug {writeln (nums);} int [int] next; foreach (k, v; nums) { answer[step] = max (answer[step], v); int p = (v & 1) ? k : 1; next[p] += v; } nums = next; } auto q = readln.strip.to !(int); foreach (s; 0..q) { auto z = readln.strip.to !(long); writeln (answer[min (z, $ - 1).to !(int)]); } } }
D
import std.stdio : write, writeln; import std.array; import std.range; import std.typecons; import std.bigint; import std.algorithm; void main(){ int cnt; bool flag1 = false; bool ans = true; foreach( c; next!string ){ if( c == '4' ){ ans &= flag1 || cnt>0; if( ++cnt > 2 ){ ans &= false; } flag1 = false; }else if( c == '1' ){ flag1 = true; cnt = 0; }else{ ans &= false; } } writeln = ans ? "YES" : "NO"; } import std.stdio : readln; import std.conv : to; import std.string : split, chomp; import std.traits; string[] input; string delim = " "; T next(T)() in { assert(hasNext()); } out { input.popFront; } body { return input.front.to!T; } T next( T : char )() in { assert(hasNext()); } out { input.front.popFront; } body { return input.front.front.to!T; } bool hasNext(){ if(input.length > 0){ if(input.front.length == 0){ input.popFront; return hasNext; } return true; } string str = readln; if(str.length > 0){ input ~= str.chomp.split(delim); return hasNext; }else{ return false; } } void dbg(T...)(T vs) { import std.stdio : stderr; foreach(v; vs) stderr.write(v.to!string ~ " "); stderr.write("\n"); } T clone(T)(T v){ T v_; static if(isInputRange!(T)){ foreach(ite; v){ v_ ~= ite.clone; } }else{ v_ = v; } return v_; }
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); long n, m; sc.read(n, m); n = min(n, 60); writeln(m % (2L ^^ n)); return 0; } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */ // module dkh.scanner; // import dkh.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(bool enforceEOF = false, T, Args...)(ref T x, auto ref Args args) { import std.exception; enforce(readSingle(x)); static if (args.length == 0) { enforce(enforceEOF == false || !succ()); } else { read!enforceEOF(args); } } void read(bool enforceEOF = false, Args...)(auto ref Args args) { import std.exception; static if (args.length == 0) { enforce(enforceEOF == false || !succ()); } else { enforce(readSingle(args[0])); read!enforceEOF(args); } } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T read(T)(){ return read.to!T; } T[] read(T)(long n){ return n.iota.map!(_ => read!T).array; } T[][] read(T)(long m, long n){ return m.iota.map!(_ => read!T(n)).array; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // /* Find gcd of given minutes. If any p is divisor of the gcd, then YES. Otherwise NO. */ void solve(){ int n = read!int; int m = read!int; long[] xs = read!long(n); long[] ps = read!long(m); long d = xs[1] - xs[0]; foreach(i; 0 .. n - 1) d = gcd(d, xs[i + 1] - xs[i]); bool f; int ans; foreach(int j, p; ps) if(d % p == 0) f = 1, ans = j; if(! f) writeln("NO"); else writeln("YES"), writeln(xs[0], " ", ans + 1); } long gcd(long a, long b){ if(a % b == 0) return b; if(a < b) return gcd(b, a); else return gcd(b, a % b); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto T = RD!int; auto ans = new long[](T); foreach (i; 0..T) { auto x = RD!(char[]); auto y = RD!(char[]); x.reverse; y.reverse; int pos; while (y[pos] == '0') ++pos; long tmp; foreach (j; pos..x.length) { if (x[j] == '1') { debug writeln("j:pos ", j, " ", pos); tmp = j - pos; break; } } ans[i] = tmp; } foreach (e; ans) writeln(e); stdout.flush(); debug readln(); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; import std.exception, std.random; long a, b; rd(a, b); // auto rnd=Random(unpredictableSeed); // long a=uniform(1, 100, rnd), b=uniform(1, 100, rnd); while(a>0 && b>0){ if(a>=b*2){ auto m=a/b; if(m&1) m--; enforce(m>=2); a-=b*m; }else if(a*2<=b){ auto m=b/a; if(m&1) m--; enforce(m>=2); b-=a*m; }else{ break; } } writeln(a, " ", b); // f(a, b); } void f(long a, long b){ import std.stdio; while(a>0 && b>0){ if(a>=b*2) a-=b*2; else if(a*2<=b) b-=a*2; else break; } writeln(a, " ", b); } 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.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto ab = new long[][](n); foreach (i; 0..n) { ab[i] = [RD, RD]; } auto d = new long[](n); foreach (i; 0..n-1) { d[i+1] = min(ab[i][1], ab[i+1][0]); } d[0] = min(ab[$-1][1], ab[0][0]); auto pos = d.MIN_POS; foreach (i; 0..n) { ans[ti] += ab[i][0]; ans[ti] -= d[i]; } debug writeln(ans); ans[ti] += d[pos]; debug writeln(ab); debug writeln(d); debug writeln("pos:", pos); } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
//dlang template---{{{ import std.stdio; import std.conv; import std.string; import std.array; import std.algorithm; import std.typecons; import std.math; import std.range; // MIT-License https://github.com/kurokoji/nephele class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } //}}} void main() { Scanner sc = new Scanner; int N; long sum = 0; sc.scan(N); foreach (i; 1 .. N + 1) { if (i % 3 != 0 && i % 5 != 0) sum += i; } writeln(sum); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "damage", long, "cost"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long n, m, k; scan(n, m, k); // 総当り bool flag; foreach (i; iota(n + 1)) { auto black = m * i; foreach (_; iota(m + 1)) { black += (n - 2 * i); if (black == k) flag = true; } } if (flag) writeln("Yes"); else writeln("No"); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import core.bitop, 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 dp = new long[][](4, S.length + 1); dp[0][0] = 1; foreach (i; 0 .. S.length) { dp[0][i + 1] = dp[0][i]; dp[1][i + 1] = dp[1][i]; dp[2][i + 1] = dp[2][i]; dp[3][i + 1] = dp[3][i]; if (S[i] == 'A') { dp[1][i + 1] += dp[0][i]; } if (S[i] == 'B') { dp[2][i + 1] += dp[1][i]; } if (S[i] == 'C') { dp[3][i + 1] += dp[2][i]; } if (S[i] == '?') { // writeln(i); dp[1][i + 1] += dp[0][i]; dp[2][i + 1] += dp[1][i]; dp[3][i + 1] += dp[2][i]; dp[0][i + 1] += dp[0][i] * 2; dp[1][i + 1] += dp[1][i] * 2; dp[2][i + 1] += dp[2][i] * 2; dp[3][i + 1] += dp[3][i] * 2; } dp[0][i + 1] %= MOD; dp[1][i + 1] %= MOD; dp[2][i + 1] %= MOD; dp[3][i + 1] %= MOD; } // writeln(dp[0]); // writeln(dp[1]); // writeln(dp[2]); // writeln(dp[3]); writeln(dp[3][S.length]); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.ascii; void main() { auto r = readln.chomp.to!int; writeln(3 * r * r); }
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[char] aa = ['J':0, 'O':1, 'I':2]; int N = readln.chomp.to!int; string str = readln.chomp; int[] dp = new int[2^^3]; dp[1] = 1; foreach(char c; str) { int[] _dp = dp.dup; dp[] = 0; foreach(int i; 0..2^^3) foreach(int j; 0..2^^3) { if ((i&j)!=0 && (j&1<<aa[c])!=0) { dp[j] = (_dp[i]+dp[j])%10007; } } } reduce!("(a+b)%10007")(0, dp).writeln; }
D
import std.stdio, std.array, std.conv, std.typecons, std.algorithm; T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; } void main() { immutable ip = readln.split.to!(ulong[]); immutable x = ip[0], a = ip[1]; ulong ret; if (x<a) ret = 0; else ret = 10; writeln(ret); }
D
import std.stdio, std.algorithm, std.array, std.string, std.conv; void main() { int t; scanf("%d", &t); foreach(_; 0..t) { int str_len; scanf("%d", &str_len); getchar(); auto str = readln.strip(); // writeln(str); int[] check = new int[str_len]; bool flag = true; for(int i = 0; i < str_len; i++) { if (flag == false) break; if (str[i] == 'a') { for (int j = i; j >= 0; j--) { check[j] += 1; if (check[j] == 0) { writeln(j + 1,' ', i + 1); flag = false; } } } else { for (int j = i; j >= 0; j--) { check[j] -= 1; if (check[j] == 0) { writeln(j + 1,' ', i + 1); flag = false; } } } } if (flag) writeln("-1 -1"); } }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; immutable long MOD = 10^^9 + 7; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto A = readln.split.map!(to!long).array; auto B = new long[](N+1); foreach (i; 0..N) B[i+1] = B[i] + A[i]; long[long] cnt; foreach (i; 1..N+1) cnt[B[i] % M] += 1; long ans = 0; foreach (i; 0..N) { if ((B[i] % M) in cnt) ans += cnt[B[i] % M]; cnt[B[i+1]%M] -= 1; } ans.writeln; }
D
import std.stdio; import std.string; import std.conv; void main() { string input = readln(); char X = input[0]; char Y = input[2]; if(X < Y) writeln("<"); else if(X > Y) writeln(">"); else writeln("="); }
D
void main() { long n = rdElem; long[string] list; string s = rdStr; ++list[s]; foreach (i; 1 .. n) { string w = rdStr; if (s[$-1] != w[0] || w in list) { "No".writeln; return; } ++list[w]; s = w; } "Yes".writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
void main() { string s = readln.chomp; s.count('1').writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
void main() { long n = rdElem; long[] a = rdRow; long ave1 = a.sum / n; long ave2 = (a.sum + n - 1) / n; long total1, total2; foreach (x; a) { total1 += (x - ave1) ^^ 2; total2 += (x - ave2) ^^ 2; } min(total1, total2).writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.bigint, std.container; alias X = Tuple!(long, "a", long, "b"); alias E = Tuple!(int, "to", long, "s"); X[10^^5] XS; E[][10^^5] G; bool[10^^5] F; void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; foreach (i; 0..M) { auto uvs = readln.split.to!(int[]); auto u = uvs[0]-1; auto v = uvs[1]-1; auto s = uvs[2].to!long; G[u] ~= E(v, s); G[v] ~= E(u, s); } long[] rs; XS[0] = X(1, 0); auto ss = [0]; F[0] = true; while (!ss.empty) { auto h = ss[0]; auto a = XS[h].a; auto b = XS[h].b; ss = ss[1..$]; foreach (n; G[h]) { if (F[n.to]) { auto xn = XS[n.to]; auto aa = a + xn.a; auto bb = n.s - b - xn.b; if ((aa == 0 && bb != 0) || (aa != 0 && bb % aa != 0)) { rs ~= -1; } else if (aa != 0 && bb % aa == 0) { rs ~= bb / aa; } } else { F[n.to] = true; XS[n.to] = X(-a, n.s - b); ss ~= n.to; } } } long r = -1; if (!rs.empty) { auto rr = rs[0]; if (rr == -1) { writeln(0); return; } foreach (rx; rs) { if (rx < 0 || rr != rx) { writeln(0); return; } } r = rr; } auto max_r = long.max, min_r = 1L; foreach (x; XS[0..N]) { if (x.a > 0 && x.b < 0) { min_r = max(min_r, -x.b / x.a + 1); } else if (x.a < 0 && x.b > 0) { max_r = min(max_r, x.b / -x.a - 1); } else if (x.a < 0 && x.b < 0) { writeln(0); return; } } if (min_r <= max_r) { if (r > 0) { writeln(min_r <= r && r <= max_r ? 1 : 0); } else { writeln(max_r - min_r + 1); } } else { writeln(0); } }
D
import std.stdio, std.algorithm, std.range, std.conv, std.string; import core.stdc.stdio; // foreach, foreach_reverse, writeln void main() { int n; scanf("%d", &n); int[] a = new int[n]; int[] b = new int[n]; foreach (i; 0..n) scanf("%d", &a[i]); foreach (i; 0..n) scanf("%d", &b[i]); int ans = 0; foreach (k; 0..29) { int mask = (1<<k)-1; int s = 0; if (n&1) { foreach (x; a) s ^= x>>k&1; foreach (x; b) s ^= x>>k&1; } int[] c = new int[n]; c[] = b[]&mask; sort(c); auto sorted = assumeSorted(c); foreach (x; a) { x &= mask; int l = n - to!int(sorted.lowerBound((1<<k)-x).length); s ^= l&1; } ans |= s<<k; } writeln(ans); }
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 T = new int[][](N, N); int x = 1, s = 2; foreach (i; 1..N) { if (T[0][i] == 0) { while (i < N) { T[0][i] = x; i += s; } ++x; s *= 2; } } foreach (i; 1..N) { foreach (j; i+1..N) { T[i][j] = T[i-1][j-1]; } } foreach (i; 0..N-1) { writeln(T[i][i+1..$].to!(string[]).join(" ")); } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; uint[] bits(uint n) { uint[] res; for(uint i = 0; i <= 9; i++) { if ((n & (1 << i)) != 0) { res ~= (1 << i); } } return res; } void main() { string line; while((line = readln.chomp) != null) bits(line.to!(int)).map!(to!(string)).join(" ").writeln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; alias Point = Tuple!(int, "x", int, "y", int, "i"); void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto G = new UndirectedGraph(N); foreach (_; 0..M) { s = readln.split.map!(to!int); G.add_edge(s[0], s[1]); } G.articulation_points.each!writeln; } class UndirectedGraph { int N; int[][] G; int[][] dfs_tree; int[] dfs_subtree; bool[] dfs_subtree_odd; this (int N) { this.N = N; G = new int[][](N); } void add_edge(int u, int v) { G[u] ~= v; G[v] ~= u; } int[] articulation_points() { dfs_tree = new int[][](N); dfs_subtree = new int[](N); dfs_subtree_odd = new bool[](N); auto ord = new int[](N); auto low = new int[](N); auto used = new bool[](N); int cnt = 0; int dfs(int n, int p) { ord[n] = cnt++; low[n] = ord[n]; used[n] = true; foreach (m; G[n]) { if (m == p) continue; if (used[m]) low[n] = min(low[n], ord[m]); else low[n] = min(low[n], dfs(m, n)), dfs_tree[n] ~= m; } return low[n]; } int dfs2(int n, int p) { dfs_subtree[n] = 1; foreach (m; dfs_tree[n]) { if (m == p) continue; auto s = dfs2(m, n); if (s % 2) dfs_subtree_odd[n] = true; dfs_subtree[n] += s; } return dfs_subtree[n]; } int[] ans; foreach (i; 0..N) if (!used[i]) dfs(i, -1); foreach (i; 0..N) { if (ord[i] == 0 && dfs_tree[i].length >= 2) { ans ~= i; } else if (ord[i] != 0 && dfs_tree[i].map!(j => (low[j] >= ord[i])).any) { ans ~= i; } } foreach (i; 0..N) if (ord[i] == 0) dfs2(i, -1); //ord.writeln; //low.writeln; //G.writeln; //dfs_tree.writeln; return ans; } }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; auto s=readln.chomp.to!(char[]); int x, y; rd(x, y); auto n=s.length; int[] dxs, dys; for(int i=0, t=1; i<n; i++, t^=1){ int d=0; while(i<n && s[i]=='F') i++, d++; if(d==0) continue; if(t&1) dxs~=d; else dys~=d; } // wr(dxs); wr(dys); auto xlen=dxs.length, ylen=dys.length; auto xrec=new bool[][](xlen+1, 8000*3); auto yrec=new bool[][](ylen+1, 8000*3); auto ofse=8000*3/2; xrec[0][ofse]=yrec[0][ofse]=true; foreach(i; 0..xlen)for(auto j=0; j<=22000; j++){ if(xrec[i][j]){ xrec[i+1][j+dxs[i]]=true; if((i>0) || (s[0]=='T')) xrec[i+1][j-dxs[i]]=true; } } foreach(i; 0..ylen)for(auto j=0; j<=22000; j++){ if(yrec[i][j]){ yrec[i+1][j+dys[i]]=true; yrec[i+1][j-dys[i]]=true; } } if(xrec[xlen][x+ofse] && yrec[ylen][y+ofse]) writeln("Yes"); else writeln("No"); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } } void wr(T...)(T x){ import std.stdio; foreach(e; x) stderr.write(e, " "); stderr.writeln(); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string; immutable long MOD = 10^^9 + 7; immutable long INF = 1L << 59; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1] + 1; auto K = s[4]; auto Ta = s[2]; auto Tb = s[3]; auto A = readln.split.map!(to!long).array; auto B = readln.split.map!(to!long).array ~ INF; if (N <= K || M - 1 <= K) { writeln(-1); return; } auto opt = new int[](N); long ans = 0; for (int i = 0, j = 0; j < M; ++j) { while (i < N && A[i] + Ta <= B[j]) opt[i++] = j; } foreach (i; 0..N) { if (i > K) break; int j = opt[i] + K - i; if (j > M) { ans = INF; break; } ans = max(ans, B[j] + Tb); } writeln((ans >= INF ? -1 : ans)); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } enum MOD = (10 ^^ 9) + 7; void main() { long H, W; scan(H, W); if (H % 3 == 0 || W % 3 == 0) { writeln(0); return; } long solve(long A, long B) { long ans = long.max; foreach (a; 1 .. A) { long S1 = a * B; { long S2 = (A - a) * (B / 2); long S3 = (A - a) * ((B + 1) / 2); long tmp = max(S1, S2, S3) - min(S1, S2, S3); // writefln("%d %d %d %d", S1, S2, S3, tmp); ans = ans.min(tmp); } { long S2 = ((A - a) / 2) * B; long S3 = ((A - a + 1) / 2) * B; long tmp = max(S1, S2, S3) - min(S1, S2, S3); ans = ans.min(tmp); } } return ans; } min(solve(H, W), solve(W, H)).writeln(); }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; import std.math : abs; int n; rd(n); auto m = n / 2 + 1; writeln(m); for (int i = 1, r = 1, c = 1; i <= n; i++) { writeln(r, " ", c); if (i & 1) { c++; } else { r++; } } } 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; 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 s = a.sum; auto m = a.maxElement; writeln (s % 2 == 1 || m + m > s ? "T" : "HL"); } }
D
import std.stdio; import std.array; import std.conv; import std.algorithm; void main() { string s, maxls, maxas; int[string] nums; while( (s=readln()).length != 0 ){ string[] input = split(s); foreach(int i, string ts; input){ nums[ts]++; if(maxls.length<ts.length) maxls=ts; } } int mn=-1; foreach(string key, int n; nums){ if(mn<n){ mn=n; maxas=key; } } writeln(maxas, " ", maxls); }
D
void main(){ int d = _scan(); switch(d){ case 22: writeln("Christmas Eve Eve Eve"); break; case 23: writeln("Christmas Eve Eve"); break; case 24: writeln("Christmas Eve"); break; default:writeln("Christmas"); break; } } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
void main() { import std.algorithm, std.array, std.stdio, std.string, std.conv; readln; auto yum = readln.chomp.split.to!(int[]); auto sum1 = yum.sum; auto sum2 = yum.map!(_=>_*_).sum; ((sum1*sum1 - sum2)/2).writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nml = readln.split.to!(int[]); auto N = nml[0]; auto M = nml[1]; long L = nml[2]; auto G = new long[][](N, N); foreach (ref g; G) foreach (ref e; g) e = long.max / 3; foreach (_; 0..M) { auto abc = readln.split.to!(int[]); auto A = abc[0]-1; auto B = abc[1]-1; long C = abc[2]; if (C > L) continue; G[A][B] = C; G[B][A] = C; } foreach (k; 0..N) { foreach (i; 0..N) { foreach (j; 0..N) { if (G[i][j] > G[i][k] + G[k][j]) G[i][j] = G[i][k] + G[k][j]; } } } auto H = new long[][](N, N); foreach (i; 0..N) { foreach (j; 0..N) { H[i][j] = G[i][j] <= L ? 1 : long.max / 3; } } foreach (k; 0..N) { foreach (i; 0..N) { foreach (j; 0..N) { if (H[i][j] > H[i][k] + H[k][j]) H[i][j] = H[i][k] + H[k][j]; } } } auto Q = readln.chomp.to!int; foreach (_; 0..Q) { auto st = readln.split.to!(int[]); auto s = st[0]-1; auto t = st[1]-1; auto r = H[s][t]-1; writeln(r > N ? -1 : r); } }
D
import std.stdio; import core.stdc.stdio; import std.algorithm.comparison; int f(int x, int cnt) { return (x % 2 == 1 ? cnt : f(x / 2, cnt + 1)); } void main() { int n; scanf("%d", &n); int ans = 1 << 30; foreach(i; 0..n) { int x; scanf("%d", &x); ans = min(ans, f(x, 0)); } writeln(ans); }
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, "begin", long, "end"); alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less); void main() { auto n = lread(); auto h = aryread(); foreach_reverse (i; iota(1, n)) { if (h[i - 1] > h[i]) { h[i - 1]--; } if(h[i - 1] > h[i]) { writeln("No"); return; } } writeln("Yes"); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.ascii; void main() { auto n = readln.chomp.to!int; if (n % 2 == 0) { n.writeln; } else { writeln(2 * n); } }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { int n; scan(n); auto a = readln.split.to!(int[]); int ans = 1; foreach (i ; 0 .. n) { ans *= 2^^(!(a[i] & 1)); } ans = 3^^n - ans; writeln(ans); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ long n = read.to!long; // how many times she made action long k = read.to!long; // how many candies are left at end foreach(i; 0 .. n + 1){ // how many times she added candies long j = n - i; // how many times she ate candies long q = i * (i + 1) / 2; // how many candies she added log("n:", n, "k:", k, "i:", i, "j:", j, "q:", q); if(q == j + k){ j.writeln; return; } } }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { int n; scan(n); auto ans = (n + 1) / 2; writeln(ans); } void scan(T...)(ref T args) { auto 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); } } } 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, m; rd(n, m); writeln((2 ^^ m) * ((1900 * m) + (100 * (n - m)))); } 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, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto K = RD; writeln(N % K == 0 ? 0 : 1); stdout.flush(); debug readln(); }
D
import std.algorithm; import std.bigint; import std.bitmanip; import std.concurrency : generator = Generator, yield; import std.container; import std.conv; import std.functional; import std.format; import std.math; import std.meta; import std.numeric; import std.random; import std.range; import std.stdio; import std.string; import std.traits; import std.typecons; auto solve(string S) { return 7 - ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"].countUntil(S); } void main() { auto input = stdin.byLine.map!split.joiner; string S; S = input.front.to!string; input.popFront; static if (is(ReturnType!solve == void)) solve(S); else { auto ans = solve(S); static if (isFloatingPoint!(ReturnType!solve)) writeln(format("%.9f", ans)); else static if (is(ReturnType!solve == bool)) writeln(ans ? YES : NO); else writeln(ans); } }
D
module app; import core.bitop; import std.algorithm; import std.array; import std.bigint; import std.container.rbtree; import std.conv; import std.stdio; import std.string; import std.traits; struct Input { string s; } void parseInput(T)(out Input input, T file) { with (file) with (input) { s = readln().strip(); } } auto main2(Input* input) { with (input) { switch (s) { case "hi": case "hihi": case "hihihi": case "hihihihi": case "hihihihihi": return "Yes"; default: return "No"; } } } alias retType = ReturnType!main2; static if (!is(retType == void)) { unittest { writeln("begin unittest"); } auto _placeholder_ = ReturnType!main2.init; unittest // example1 { string example = `hihi`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == "Yes"); } unittest // example2 { string example = `hi`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == "Yes"); } unittest // example3 { string example = `ha`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == "No"); } unittest { writeln("end unittest"); } void parseExample(out Input input, string example) { struct Adapter { string[] _lines; this(string input) { _lines = input.splitLines(); } string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; } } parseInput(input, Adapter(example)); } } void printResult(T)(T result) { static if (isFloatingPoint!T) writefln("%f", result); else writeln(result); } void main() { Input input = void; parseInput(input, stdin); static if (is(retType == void)) main2(&input); else { auto result = main2(&input); printResult(result); } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "x", long, "y", long, "cost"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { auto k = lread(); long s; foreach (i; iota(1, k + 1)) { foreach (j; iota(1, k + 1)) { auto tmp = gcd(i, j); foreach (l; iota(1, k + 1)) { s += gcd(tmp, l); } } } s.writeln(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random; import std.typecons, std.functional, std.traits,std.concurrency; import std.algorithm, std.container; import core.bitop, core.time, core.memory; import std.bitmanip; import std.regex; enum INF = long.max/3; enum MOD = 10L^^9+7; //辞書順順列はiota(1,N),nextPermituionを使う void end(T)(T v) if(isIntegral!T||isSomeString!T||isSomeChar!T) { import core.stdc.stdlib; writeln(v); exit(0); } T[] scanArray(T = long)() { static char[] scanBuf; readln(scanBuf); return scanBuf.split.to!(T[]); } dchar scanChar() { int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } return cast(dchar)c; } T scanElem(T = long)() { import core.stdc.stdlib; static auto scanBuf = appender!(char[])([]); scanBuf.clear; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { scanBuf ~= cast(char) c; c = getchar; } return scanBuf.data.to!T; } dchar[] scanString(){ return scanElem!(dchar[]); } void main() { auto n=scanElem+scanElem; if(n>=10)end("error");end(n); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n; readV(n); foreach (i; 0..n/4+1) if ((n-i*4)%7 == 0) { writeln("Yes"); return; } writeln("No"); }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { auto tmp = readln.split.to!(int[]); auto H = tmp[0], W = tmp[1]; string[] A; foreach (i; 0..H) { auto as = readln.chomp; if (as.all!(a => a == '.')) continue; A ~= as; } bool[] flags = new bool[W]; foreach (i; 0..W) { flags[i] = A.all!(as => as[i] == '.'); } foreach (i; 0..A.length) { foreach (j; 0..W) { if (flags[j]) continue; write(A[i][j]); } writeln; } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp.to!int; bool f; foreach (i; 1..10) { foreach (j; 1..10) { if (i * j == n) { f = true; } } } if (f) { writeln("Yes"); } else { writeln("No"); } }
D
import std.stdio, std.string, std.conv; void main(){ auto ip = readln.split.to!(int[]); writeln(ip[0] - ip[1] + 1); }
D
import std; // 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 A, B, C, D; scan(A, B, C, D); long t = (C + B - 1) / B; long a = (A + D - 1) / D; writeln((t <= a) ? "Yes" : "No"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp; foreach (c; N) if (c == '7') { writeln("Yes"); return; } writeln("No"); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { int n = readint; int k = readint; int ans = 1; for (int i = 0; i < n; i++) { ans = min(ans * 2, ans + k); } writeln(ans); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; long c; rd(n, c); auto x=new long[](n), v=new long[](n); foreach(i; 0..n) rd(x[i], v[i]); // import std.exception; // enforce(n<=100); x=0L~x~c; v=0L~v~0L; long mx=0, inf=2e18.to!(long); auto a=new long[](n+2); // a[i]:=[i, n+1] fill(a, -inf); a[n+1]=0; for(auto r=n, s=0L; r>0; r--){ s+=v[r]; a[r]=max(a[r+1], s-(c-x[r])); } // writeln(a); for(auto l=0, s=0L; l<=n; l++){ s+=v[l]; mx=max(mx, (s-x[l]*2)+a[l+1]); } auto b=new long[](n+2); // b[i]:=[0, i] fill(b, -inf); b[0]=0; for(auto l=1, s=0L; l<=n; l++){ s+=v[l]; b[l]=max(b[l-1], s-x[l]); } // writeln(b); for(auto r=n+1, s=0L; r>0; r--){ s+=v[r]; mx=max(mx, (s-(c-x[r])*2+b[r-1])); } long tot=reduce!"a+b"(0L, v); for(int i=0; i<=n; i++){ long cl=x[i], cr=c-x[i+1]; long cost=cl+cr+min(cl, cr); mx=max(mx, tot-cost); } writeln(mx); } 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.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; void main() { int n, m; scan(n, m); auto uf = new UnionFind(n); for (int i = 0; i < m; i++) { int x, y, z; scan(x, y, z); uf.unite(x, y); } bool[int] set; for (int i = 1; i <= n; i++) { set[uf.root(i)] = true; } auto ans = set.keys.length; writeln(ans); } class UnionFind { private int[] _data; private int[] _nexts; // 次の要素。なければ -1 private int[] _tails; // 末尾の要素 this(int n) { _data = new int[](n + 1); _nexts = new int[](n + 1); _tails = new int[](n + 1); _data[] = -1; _nexts[] = -1; for (int i = 0; i < _tails.length; i++) _tails[i] = i; } int root(int a) { if (_data[a] < 0) return a; return _data[a] = root(_data[a]); } bool unite(int a, int b) { int ra = root(a); int rb = root(b); if (ra != rb) { if (_data[rb] < _data[ra]) swap(ra, rb); _data[ra] += _data[rb]; // ra に rb を繋げる _data[rb] = ra; // ra の末尾の後ろに rb を繋げる _nexts[_tails[ra]] = rb; // ra の末尾が rb の末尾になる _tails[ra] = _tails[rb]; } return ra != rb; } bool isSame(int a, int b) { return root(a) == root(b); } /// a が属する集合のサイズ int groupSize(int a) { return -_data[root(a)]; } /// a が属する集合の要素全て void doEach(int a, void delegate(int) fn) { auto node = root(a); while (node != -1) { fn(node); node = _nexts[node]; } } } void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int;
D
import std.stdio, std.conv, std.string; import std.algorithm, std.array, std.container; import std.numeric, std.math; import core.bitop; string my_readln() { return chomp(readln()); } long mod = pow(10, 9) + 7; long moda(long x, long y) { return (x + y) % mod; } long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; } long modm(long x, long y) { return (x * y) % mod; } void main() { auto S = my_readln; auto tokens = my_readln.split; auto K = tokens[0].to!ulong; string ans; foreach (i, e; S) { if (i == K - 1 || e != '1') { ans ~= e; break; } } writeln(ans); stdout.flush(); }
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() { auto a = readints; int x1 = a[0], y1 = a[1], x2 = a[2], y2 = a[3]; int x = x2; int y = y2; int dx = x2 - x1; int dy = y2 - y1; for (int i = 0; i < 2; i++) { swap(dx, dy); dx *= -1; x += dx; y += dy; write(x, " ", y, " "); } writeln(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { int i; foreach (c; readln.chomp.to!(char[])) { if (c == '+') ++i; if (c == '-') --i; } writeln(i); }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; immutable inf = 10^^9 + 7; void main() { int n; scan(n); auto f = new int[][](n, 10); auto p = new int[][](n, 11); foreach (i ; 0 .. n) { f[i] = readln.split.to!(int[]); } foreach (i ; 0 .. n) { p[i] = readln.split.to!(int[]); } long ans = -inf; foreach (cmb ; 1 .. (1<<10)) { long t; foreach (mise ; 0 .. n) { int cnt; foreach (j ; 0 .. 10) { if (f[mise][j] && (cmb & (1<<j))) { cnt++; } } t += p[mise][cnt]; } ans = max(ans, t); } writeln(ans); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.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 dump(T)(T[][] g) { for (int i = 0; i < g.length; i++) { for (int j = 0; j < g[i].length; j++) { stderr.write(" ", g[i][j]); } stderr.writeln(); } } void main() { auto nmq = readints; int n = nmq[0], m = nmq[1], Q = nmq[2]; auto g = new int[][](n + 1, n + 1); for (int i = 0; i < m; i++) { auto lr = readints; g[lr[0]][lr[1]]++; } debug dump(g); for (int r = n; r > 0; r--) { for (int c = 1; c <= n; c++) { g[r][c] += g[r][c - 1]; } if (r < n) { for (int c = 1; c <= n; c++) { g[r][c] += g[r + 1][c]; } } } debug { stderr.writeln("-- prepared --"); dump(g); } for (int i = 0; i < Q; i++) { auto pq = readints; int p = pq[0], q = pq[1]; writeln(g[p][q]); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto t = RD!int; auto ans = new long[](t); foreach (i; 0..t) { auto n = RD!int; auto s = RD!string; long cnt1, cnt2; foreach (j; 0..n) { if (s[j] == '1') break; ++cnt1; } foreach_reverse (j; 0..n) { if (s[j] == '1') break; ++cnt2; } if (cnt1 == n) ans[i] = n; else ans[i] = n*2 - min(cnt1, cnt2)*2; } foreach (e; ans) { writeln(e); } stdout.flush(); debug readln(); }
D
import std.stdio, std.string, std.algorithm; void main() { char[] input = chomp(readln()).dup; char[] input2 = chomp(readln()).dup; swap(input[0], input[2]); if(input == input2) { writeln("YES"); return; } else { writeln("NO"); return; } }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { long a,b; scan(a,b); if(a > 8 || b > 8) { writeln(":("); } else { writeln("Yay!"); } }
D
import std.stdio, std.string, std.conv; void main() { auto t = readln.split.to!(int[]); writeln(t[0] <= t[2] && t[2] <=t[0]+t[1] ? "YES" : "NO"); }
D
import core.stdc.stdio; import std.range; import std.algorithm; void main(){ int[] buf_p = new int[1001]; int[] buf_d = new int[1001*500]; while(1){ int n,m; scanf("%d%d",&n,&m); if(n==0&&m==0) break; int[] p = buf_p[0..++n]; foreach(ref v;p[1..$]) scanf("%d",&v); p[0]=0; int[] d=buf_d[0..n*(n-1)/2]; int c=0; foreach(i;0..n) foreach(j;i+1..n) d[c++] = p[i]+p[j]; sort(d); int ans; foreach(v;d){ if(v*2>m) break; auto lb = d.assumeSorted.lowerBound(m+1-v); if(!lb.empty) ans=max(ans,v+lb[$-1]); } printf("%d\n",ans); } }
D
/+ dub.sdl: name "B" dependency "dunkelheit" version="1.0.1" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner, dkh.algorithm; int main() { Scanner sc = new Scanner(stdin); scope(exit) assert(!sc.hasNext); string s; sc.read(s); if (s.count('o') + (15 - s.length.to!int) >= 8) { writeln("YES"); } else { writeln("NO"); } return 0; } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/algorithm.d */ // module dkh.algorithm; import std.traits : isFloatingPoint, isIntegral; T binSearch(alias pred, T)(T l, T r) if (isIntegral!T) { while (r-l > 1) { T md = l + (r-l) / 2; if (!pred(md)) l = md; else r = md; } return r; } T binSearch(alias pred, T)(T l, T r, int cnt = 60) if (isFloatingPoint!T) { foreach (i; 0..cnt) { T md = (l+r)/2; if (!pred(md)) l = md; else r = md; } return r; } import std.range.primitives; E minimum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed) if (isInputRange!Range && !isInfinite!Range) { import std.algorithm, std.functional; return reduce!((a, b) => binaryFun!pred(a, b) ? a : b)(seed, range); } ElementType!Range minimum(alias pred = "a < b", Range)(Range range) { assert(!range.empty, "minimum: range must not empty"); auto e = range.front; range.popFront; return minimum!pred(range, e); } E maximum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed) if (isInputRange!Range && !isInfinite!Range) { import std.algorithm, std.functional; return reduce!((a, b) => binaryFun!pred(a, b) ? b : a)(seed, range); } ElementType!Range maximum(alias pred = "a < b", Range)(Range range) { assert(!range.empty, "maximum: range must not empty"); auto e = range.front; range.popFront; return maximum!pred(range, e); } Rotator!Range rotator(Range)(Range r) { return Rotator!Range(r); } struct Rotator(Range) if (isForwardRange!Range && hasLength!Range) { size_t cnt; Range start, now; this(Range r) { cnt = 0; start = r.save; now = r.save; } this(this) { start = start.save; now = now.save; } @property bool empty() const { return now.empty; } @property auto front() const { assert(!now.empty); import std.range : take, chain; return chain(now, start.take(cnt)); } @property Rotator!Range save() { return this; } void popFront() { cnt++; now.popFront; } } /* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/scanner.d */ // module dkh.scanner; // import dkh.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; private File f; this(File f) { this.f = f; } private char[512] lineBuf; private char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { assert(succW()); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(Args...)(auto ref Args args) { import std.exception : enforce; static if (args.length != 0) { enforce(readSingle(args[0])); read(args[1..$]); } } bool hasNext() { return succ(); } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.typecons; import std.numeric, std.math; import core.bitop; string FMT_F = "%.10f"; T RD(T = long)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = 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; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } struct UnionFind { void init(long n) { par = new long[](n); foreach (i; 0..n) par[i] = i; cnt = new long[](n); fill(cnt, 1); } long root(long i) { return par[i] == i ? i : (par[i] = root(par[i])); } bool same(long i, long j) { return root(i) == root(j); } long unite(long i, long j) { i = root(i); j = root(j); if (i == j) return 0; par[j] = i; auto cnt_i = ((cnt[i] ^^ 2) - cnt[i]) / 2; auto cnt_j = ((cnt[j] ^^ 2) - cnt[j]) / 2; cnt[i] += cnt[j]; auto diff = ((cnt[i] ^^ 2) - cnt[i]) / 2 - (cnt_i + cnt_j); //stderr.writeln(i, ":", j, " ", cnt_i, " ", cnt_j, " ", diff); return diff; } long[] par; long[] cnt; } void main() { auto N = RD; auto a = RDR.ARR; auto avg = cast(double)a.sum / a.length; auto pos = MIN_POS!((a, b) => abs(a - avg) < abs(b - avg))(a); writeln(pos); stdout.flush(); }
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.datetime, std.bigint; void main() { int n, m; int[][] a; scan(n, m); a = new int[][](n, m); iota(n).each!(i => a[i][] = readln.split.to!(int[])); iota(n).each!(i => a[i][] -= 1); int top = n, btm = 0; bool check(int k) { int[] cnt = new int[](m); bool[] ok = new bool[](m); ok[] = true; int oknum = m; while (oknum > 0) { cnt[] = 0; foreach (i ; 0 .. n) { foreach (j ; 0 .. m) { if (ok[a[i][j]]) { cnt[a[i][j]]++; break; } } } bool flag = true; foreach (j ; 0 .. m) { if (cnt[j] > k) { ok[j] = false; oknum--; flag = false; } } if (flag) return true; } return false; } while (top - btm > 1) { int mid = (top + btm) / 2; (check(mid) ? top : btm) = mid; } writeln(top); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; void main() { int x; scan(x); int ans = 1; foreach (i ; 2 .. x + 1) { for (int d = i*i; d <= x; d *= i) { ans = max(ans, d); } } writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; void main() { auto ans = 'A'; foreach (line; stdin.byLine) { auto sw = line.split(",").map!(to!char); if (sw[0] == ans) ans = sw[1]; else if (sw[1] == ans) ans = sw[0]; } ans.writeln; }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; long modPow(long x, long k, long m) { if (k == 0) return 1; if (k % 2 == 0) return modPow(x * x % m, k / 2, m); return x * modPow(x, k - 1, m) % m; } const MOD = 10^^9 + 7; long calc(string s) { long ans = 0; int one = 0; // 1 の個数 for (int i = 0; i < s.length; i++) { if (s[i] == '1') { // s[i] = 0 とする // 範囲 [0, i) の各桁の 1 の組み合わせは (0, 1), (1, 0) の 2 通り // 範囲 [i + 1, $) の各桁の組み合わせは (0, 0), (0, 1), (1, 0) の 3 通り long a = modPow(2, one, MOD); long b = modPow(3, cast(int)s.length - i - 1, MOD); ans = (ans + (a * b)) % MOD; one++; } } // 上の for ループで i 番目を 0 にしたが、どの桁の 1 も 0 にしない場合 ans = (ans + modPow(2, one, MOD)) % MOD; return ans; } // 桁 DP による解法 long calc2(string s) { void madd(T)(ref T a, in T b) { a = (a + b) % MOD; } int n = cast(int)s.length; auto dp = new long[][](100010, 2); // dp[見た桁数][L 以下か] dp[0][0] = 1; for (int i = 0; i < n; i++) { if (s[i] == '0') { // L を維持: 選択肢は (0, 0) のみ madd(dp[i + 1][0], dp[i][0]); // L 未満: 選択肢は (0, 0), (1, 0), (0, 1) の 3 通り madd(dp[i + 1][1], dp[i][1] * 3); } else { // L を維持: 選択肢は (1, 0), (0, 1) の 2 通り madd(dp[i + 1][0], dp[i][0] * 2); // i 桁目を 0 にして L 未満へ遷移 madd(dp[i + 1][1], dp[i][0]); // L 未満: 選択肢は (0, 0), (1, 0), (0, 1) の 3 通り madd(dp[i + 1][1], dp[i][1] * 3); } } return (dp[n][0] + dp[n][1]) % MOD; } void main() { string s = read!string; writeln(calc2(s)); }
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, core.bitop; // dfmt on void main() { long N = lread(); auto S = sread(); long[string] D; foreach (i; 0 .. N) { auto K = D.keys; foreach (key; K) { if (key.length == 3) continue; D[key ~ S[i]] = 1; } D[S[i .. i + 1]] = 1; } writeln(D.keys.filter!"a.length == 3"().map!"1"().sum()); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio; immutable int MOD = 10^^9+7; immutable int MAX = 401; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto K = s[1]; auto A = readln.split.map!(to!long).array; auto B = readln.split.map!(to!long).array; auto p = new long[][](MAX, MAX); foreach (i; 0..MAX) { p[i][0] = 1; foreach (j; 1..MAX) { p[i][j] = p[i][j-1] * i % MOD; } } auto ps = new long[][](MAX, MAX+1); foreach (i; 0..MAX) { ps[i][0] = 1; foreach (j; 1..MAX) { ps[i][j] = (ps[i][j-1] + p[j][i]) % MOD; } } auto dp = new long[][](N+1, K+1); foreach (i; 0..N+1) fill(dp[i], 0); dp[0][0] = 1; foreach (i; 1..N+1) { foreach (j; 0..K+1) { foreach (k; 0..j+1) { auto wa = ps[j-k][B[i-1].to!int] - ps[j-k][A[i-1].to!int-1]; dp[i][j] += dp[i-1][k] * wa % MOD; dp[i][j] %= MOD; } } } //dp.writeln; writeln((dp[N][K]+MOD)%MOD); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; long[] arr = [N]; for (long i = 2; i*i <= N; ++i) { if (N % i == 0) { arr ~= i; } } auto x = arr[$-1]; auto y = N / x; writeln(x+y-2); stdout.flush(); debug readln(); }
D
import std.stdio,std.string,std.conv,std.array,std.algorithm; void main(){ for(;;write("\n")){ auto a = readln().chomp().split(); if( a[0]=="0" && a[1]=="0" ){ break; } int h=to!int(a[0]) , w=to!int(a[1]); foreach(y;0..h){ foreach(x;0..w){ if( 0<x && x<w-1 && 0<y && y<h-1 ){ write("."); }else{ write("#"); } } write("\n"); } } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void main() { auto rck = readln.split.to!(int[]); auto R = rck[0]; auto C = rck[1]; auto K = rck[2]; auto MAP = new long[][](R, C); foreach (_; 0..K) { auto rcv = readln.split.to!(int[]); auto r = rcv[0]-1; auto c = rcv[1]-1; long v = rcv[2]; MAP[r][c] = v; } auto DP = new long[][][](R+1, C+1, 4); foreach (r; 0..R) { foreach (c; 0..C) { foreach (k; 0..4) { if (k < 3) { DP[r][c+1][k+1] = max(DP[r][c+1][k+1], DP[r][c][k] + MAP[r][c]); DP[r+1][c][0] = max(DP[r+1][c][0], DP[r][c][k] + MAP[r][c]); } DP[r][c+1][k] = max(DP[r][c+1][k], DP[r][c][k]); DP[r+1][c][0] = max(DP[r+1][c][0], DP[r][c][k]); } } } long r; foreach (k; 0..4) r = max(r, DP[R][C-1][k]); writeln(r); }
D
/+ dub.sdl: name "B" dependency "dcomp" version=">=0.7.3" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; // import dcomp.dungeon; int main() { Scanner sc = new Scanner(stdin); int h, w; sc.read(h, w); auto dh = DungeonHelper(h, w); char[][] g = new char[][h]; foreach (i; 0..h) { sc.read(g[i]); } foreach (i; 0..h) { foreach (j; 0..w) { if (g[i][j] != '.') continue; char c = '0'; foreach (d; 0..8) { auto u = dh.move8([j, i], d); int ni = u[1], nj = u[0]; if (!dh.isInside(u)) continue; if (g[ni][nj] == '#') c++; } g[i][j] = c; } writeln(g[i]); } return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } 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/dungeon.d */ // module dcomp.dungeon; struct DungeonHelper { immutable static int[2][4] d4 = [ [1, 0], [0, 1], [-1, 0], [0, -1], ]; immutable static int[2][8] d8 = [ [1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1], ]; int h, w; this(int h, int w) { this.h = h; this.w = w; } bool isInside(int[2] p) const { int x = p[0], y = p[1]; return 0 <= x && x < w && 0 <= y && y < h; } int getID(int[2] p) const { int x = p[0], y = p[1]; return y*w+x; } int[2] move(int[2] p, int dir) const { int[2] res; res[] = p[] + d4[dir][]; return res; } int[2] move8(int[2] p, int dir) const { int[2] res; res[] = p[] + d8[dir][]; return res; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; // import dcomp.array; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { FastAppender!(E[]) buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/array.d */ // module dcomp.array; T[N] fixed(T, size_t N)(T[N] a) {return a;} struct FastAppender(A, size_t MIN = 4) { import std.algorithm : max; import std.conv; import std.range.primitives : ElementEncodingType; import core.stdc.string : memcpy; private alias T = ElementEncodingType!A; private T* _data; private uint len, cap; @property size_t length() const {return len;} bool empty() const { return len == 0; } void reserve(size_t nlen) { import core.memory : GC; if (nlen <= cap) return; void* nx = GC.malloc(nlen * T.sizeof); cap = nlen.to!uint; if (len) memcpy(nx, _data, len * T.sizeof); _data = cast(T*)(nx); } void free() { import core.memory : GC; GC.free(_data); } void opOpAssign(string op : "~")(T item) { if (len == cap) { reserve(max(MIN, cap*2)); } _data[len++] = item; } void insertBack(T item) { this ~= item; } void removeBack() { len--; } void clear() { len = 0; } ref inout(T) back() inout { assert(len); return _data[len-1]; } ref inout(T) opIndex(size_t i) inout { return _data[i]; } T[] data() { return (_data) ? _data[0..len] : null; } } /* This source code generated by dcomp and include dcomp's source code. dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp) dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt) */
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; void main() { int cnt; foreach (line; stdin.byLine) { auto s = line.chomp; auto t = s.dup.reverse; if (s == t) cnt++; } cnt.writeln; }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; long calc(int n, int k) { auto m = new long[k]; for (int i = 1; i <= n; i++) m[i % k]++; long ans = 0; for (int i = 1; i <= n; i++) { // a を固定 int a = i % k; int b = (k - a) % k; // a の余りとの和が k の倍数になるための b の値 int c = (k - a) % k; if ((b + c) % k == 0) ans += m[b] * m[c]; } return ans; } long calc2(int n, int k) { long ans = 0; for (int i = 1; i <= n; i++) { // a を固定 int a = i % k; long b = (k - a) % k; long c = (k - a) % k; if ((b + c) % k == 0) { ans += ((n + b) / k) * ((n + c) / k); } } return ans; } void main() { int n, k; scan(n, k); writeln(calc(n, k)); }
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() { readln; readln.split.to!(int[]).reduce!gcd.writeln; } 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.range; alias Point = Tuple!(int, "x", int, "y", int, "p"); int N; Point[100] RS, BS; int[][200] GS; void main() { N = readln.chomp.to!int; foreach (i; 0..N) { auto ab = readln.split.to!(int[]); RS[i] = Point(ab[0], ab[1], 0); } foreach (i; 0..N) { auto cd = readln.split.to!(int[]); BS[i] = Point(cd[0], cd[1], 0); } foreach (int i, r; RS[0..N]) { foreach (int j, b; BS[0..N]) { if (r.x < b.x && r.y < b.y) { GS[i] ~= 100+j; GS[100+j] ~= i; } } } int res; int[200] match = -1; foreach (i; 0..200) { if (match[i] < 0) { bool[200] used; bool search(int p) { used[p] = true; foreach (g; GS[p]) { auto s = match[g]; if (s < 0 || !used[s] && search(s)) { match[p] = g; match[g] = p; return true; } } return false; } if (search(i)) ++res; } } writeln(res); }
D
// import chie template :) {{{ import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint; // }}} void main() { int n = readln.chomp.to!int; writeln(24 + (24 - n)); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { int k = readint; int even = k / 2; writeln(even * (k - even)); }
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() { _lazy[] = NIL; _mini[] = int.max; auto nq = readints(); int n = nq[0], q = nq[1]; for (int i = 0; i < q; i++) { auto xs = readints(); if (xs[0] == 0) { int s = xs[1]; int t = xs[2]; int x = xs[3]; fill(s, t + 1, x); } else { int s = xs[1]; int t = xs[2]; int v = minimum(s, t + 1); writeln(v); } } } /////////////////////////////////////// const int N = 1 << 18; // ???????????° const int NIL = int.max; int[N * 2] _mini; // ?????????k????????????????????????????°???? int[N * 2] _lazy; // lazy[k]????????????k???????????¨??????lazy[k]??§???????????¶?????¨?????????????????¨??????lazy[k]=NIL?????¨???????????????????????¨?´?????????? void setLazy(int k, int v) { _lazy[k] = v; // ??????????????????????????¨?????? v ??§???????????¶????????????????????????????????¶????????????k????????§??????v??§?????? _mini[k] = v; } void push(int k) { // ?????¶?????????????????????????????°??????????????? if (_lazy[k] == NIL) { return; } setLazy(k * 2 + 0, _lazy[k]); setLazy(k * 2 + 1, _lazy[k]); // ????????????????????¬??????????????§???????????????????????? _lazy[k] = NIL; } void fix(int k) { _mini[k] = min(_mini[k * 2], _mini[k * 2 + 1]); } // ??????[queryL,queryR)???val??§???????????¶??? void fill(int queryL, int queryR, int val, int k = 1, int nodeL = 0, int nodeR = N) { // ?????¨???????????¨????????????????????????????????????????????????????????\????????????????????????????????? if (nodeR <= queryL || queryR <= nodeL) { return; } // ???????????????????????¨?????????????????¨??????????????????????????¶????????????????????????????????£?????¨??°??? if (queryL <= nodeL && nodeR <= queryR) { setLazy(k, val); return; } // ???????????????????????¨??????????????????push?????? push(k); int nodeM = (nodeL + nodeR) / 2; fill(queryL, queryR, val, k * 2 + 0, nodeL, nodeM); fill(queryL, queryR, val, k * 2 + 1, nodeM, nodeR); // ???????????????????????¨??????????????±???fix?????? fix(k); } // ??????[queryL, queryR)???????°????????±??????? int minimum(int queryL, int queryR, int k = 1, int nodeL = 0, int nodeR = N) { // ?????¨???????????¨??????????????????????????????????????? if (nodeR <= queryL || queryR <= nodeL) { return int.max; } // ???????????????????????¨?????????????????¨??????????????? if (queryL <= nodeL && nodeR <= queryR) { return _mini[k]; } // ???????????????????????¨??????????????????push?????? push(k); int nodeM = (nodeL + nodeR) / 2; int vl = minimum(queryL, queryR, k * 2 + 0, nodeL, nodeM); int vr = minimum(queryL, queryR, k * 2 + 1, nodeM, nodeR); return min(vl, vr); }
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, k; readV(n, k); writeln(n == k ? 1 : (n-2)/(k-1)+1); }
D
void main() { long[] tmp = readln.split.to!(long[]); long n = tmp[0], a = tmp[1], b = tmp[2]; max(0, (n - 2) * (b - a) + 1).writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto st = readln.split.to!(string[]); writeln(st[1] ~ st[0]); }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); } void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); } string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ int n = scan!int; Node[] nodes; foreach(i; 0 .. n) nodes ~= new Node(i); Edge[] edges; foreach(i; 0 .. n - 1){ edges ~= new Edge(i, nodes[scan!int - 1], nodes[scan!int - 1]); } Node nd0; foreach(nd; nodes) if(nd.edges.length >= 3) nd0 = nd; int v = 0; if(nd0){ nd0.edges[0].value = 0; nd0.edges[1].value = 1; nd0.edges[2].value = 2; v += 3; } foreach(ed; edges) if(ed.value < 0) ed.value = v, v += 1; foreach(ed; edges) ed.value.print; } class Edge{ int id; Node a, b; int value = -1; this(int id, Node a, Node b){ this.id = id; this.a = a, this.b = b; a.edges ~= this, b.edges ~= this; } } class Node{ int id; Edge[] edges; this(int id){ this.id = id; } }
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 readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}} void main() { int n; readV(n); int[] a; readC(n, a); a[] -= 1; auto v = new bool[](n), c = 0, b = 0; v[0] = true; for (;;) { b = a[b]; if (v[b]) { writeln(-1); return; } ++c; if (b == 1) { writeln(c); return; } v[b] = true; } }
D
import std.stdio; import std.algorithm; import std.conv; import std.numeric; import std.math; import std.string; void main() { auto tokens = split(chomp(readln())); auto N = to!ulong(tokens[0]); auto T = to!ulong(tokens[1]); ulong c_min; c_min = ulong.max; foreach (i; 0..N) { auto tokens1 = split(chomp(readln())); auto c = to!ulong(tokens1[0]); auto t = to!ulong(tokens1[1]); if (t <= T && c < c_min) { c_min = c; } } if (c_min == ulong.max) writeln("TLE"); else writeln(c_min); stdout.flush(); }
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, core.bitop; // dfmt on void main() { long A, B, C, X, Y; scan(A, B, C, X, Y); long ans = long.max; ans = ans.min(A * X + B * Y); ans = ans.min(min(X, Y) * C * 2 + max(0, X - min(X, Y)) * A + max(0, Y - min(X, Y)) * B); ans = ans.min(max(X, Y) * C * 2); writeln(ans); }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; import std.numeric; int n; rd(n); long l = 1; long lcm(long a, long b) { return a / gcd(a, b) * b; } while (n--) { long t; rd(t); l = lcm(l, t); } writeln(l); } 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, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto s = RD!string; if (s.length % 2) continue; bool ok = true; long cnt0, cnt1, cnt2; foreach (c; s) { if (c == '(') ++cnt0; else if (c == '?') ++cnt2; else { --cnt0; if (cnt0 < 0) { --cnt2; ++cnt0; } if (cnt2 < 0) { ok = false; break; } } } if (!ok) continue; long cnt3, cnt4; foreach_reverse (i; 0..s.length) { if (s[i] == '(') { ++cnt3; if (cnt3 > 0) break; } else if (s[i] == ')') { --cnt3; } else { ++cnt4; } } ans[ti] = cnt0 <= cnt4; } foreach (e; ans) writeln(e ? "YES" : "NO"); 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!int); auto H = s[0]; auto W = s[1]; auto A = H.iota.map!(_ => readln.chomp).array; auto M = max(H, W) * 3 + 10; auto B = new long[][](M, M); auto C = new long[][](M, M); auto X = new int[][](M); auto Y = new int[][](M); long ans = 0; foreach (i; 0..H) { foreach (j; 0..W) { if (A[i][j] == '#') { int r = i + j + max(H, W); int c = i - j + max(H, W); B[r][c] = 1; C[r][c] = 1; X[r] ~= c; Y[c] ~= r; } } } foreach (i; 0..M) { foreach (j; 0..M-1) { B[i][j+1] += B[i][j]; } } foreach (j; 0..M) { foreach (i; 0..M-1) { C[i+1][j] += C[i][j]; } } foreach (i; 0..M) { foreach (j; 0..X[i].length) { foreach (k; j+1..X[i].length) { int len = X[i][k] - X[i][j]; int a = X[i][j]; int b = X[i][k]; int u = i - len; int d = i + len; if (u >= 0) { ans += B[u][b] - B[u][a-1]; } if (d < M) { ans += B[d][b] - B[d][a-1]; } } } } foreach (i; 0..M) { foreach (j; 0..Y[i].length) { foreach (k; j+1..Y[i].length) { int len = Y[i][k] - Y[i][j]; int a = Y[i][j]; int b = Y[i][k]; int u = i - len; int d = i + len; if (u >= 0) { ans += C[b-1][u] - C[a][u]; } if (d < M) { ans += C[b-1][d] - C[a][d]; } } } } ans.writeln; }
D
import std.stdio; // ??\???????????????????????? import std.string; // chomp????????????????????????(?????????????????????) import std.conv; // to???????????????????????? import std.array; // split????????????????????? import std.algorithm; // map????????????????????? void main() { auto input = readln.split.map!(to!int); auto a = input[0]; auto b = input[1]; auto c = input[2]; if (a < b && b < c) { writeln("Yes"); } else { writeln("No"); } }
D