code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.algorithm; import std.array; import std.ascii; 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 log(A...)(A arg) { stderr.writeln(arg); } int size(T)(in T s) { return cast(int)s.length; } void main() { int N; scanf("%d\n", &N); auto T = readln.chomp.split(" ").map!(to!int).array; auto A = readln.chomp.split(" ").map!(to!int).array; auto fixed = new bool[N]; auto limit = new long[N]; int t = 0; foreach (i; 0 .. N) { int prev = t; t = max(t, T[i]); if (prev == t) { limit[i] = t; } else { fixed[i] = true; limit[i] = t; } } int a = 0; foreach_reverse(i; 0 .. N) { int prev = a; a = max(a, A[i]); if (fixed[i]) { if (limit[i] > a) { writeln(0); return; } } if (prev == a) { limit[i] = min(limit[i], a); } else { if (limit[i] < a) { writeln(0); return; } fixed[i] = true; } } const long mod = cast(long)(1e9 + 7); long ans = 1; foreach (i; 0 .. N) { if (fixed[i]) continue; ans = (ans * limit[i]) % mod; } writeln(ans); }
D
/+ dub.sdl: name "E" dependency "dunkelheit" version=">=0.9.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; int main() { Scanner sc = new Scanner(stdin); scope(exit) assert(!sc.hasNext); int n; sc.read(n); int[] wpos = new int[n], bpos = new int[n]; foreach (i; 0..2*n) { char c; int a; sc.read(c, a); a--; if (c == 'W') { wpos[a] = i; } else { bpos[a] = i; } } int base = 0; foreach (i; 0..n) { foreach (j; i+1..n) { if (wpos[i] > wpos[j]) base++; if (bpos[i] > bpos[j]) base++; } } debug writeln("BASE ", base); int[][] wdp = new int[][](n, n+1), bdp = new int[][](n, n+1); foreach (i; 0..n) { foreach (j; 1..n+1) { wdp[i][j] = wdp[i][j-1]; if (wpos[i] < bpos[j-1]) wdp[i][j]++; bdp[i][j] = bdp[i][j-1]; if (bpos[i] < wpos[j-1]) bdp[i][j]++; } } debug writeln(wdp); debug writeln(bdp); int[][] dp = new int[][](2*n+1, n+1); foreach (i; 1..2*n+1) { foreach (w; max(0, i-n)..min(n+1, i+1)) { int b = i-w; dp[i][w] = 10^^9; if (w) { dp[i][w] = min(dp[i][w], dp[i-1][w-1] + wdp[w-1][b]); } if (b) { dp[i][w] = min(dp[i][w], dp[i-1][w] + bdp[b-1][w]); } } } debug { foreach (v; dp) { writeln(v); } } writeln(base + dp[2*n][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(Args...)(auto ref Args args) { import std.exception; static if (args.length != 0) { enforce(readSingle(args[0])); read(args[1..$]); } } bool hasNext() { return succ(); } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto A = RDR.ARR; long o, e; foreach (i; 0..N) { if (A[i] % 2 == 0) ++e; else ++o; } e += o / 2; e = e >= 1 ? 1 : 0; o = o % 2; writeln(o+e == 1 ? "YES" : "NO"); stdout.flush(); debug readln(); }
D
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } enum A = 2050L; void main() { try { for (; ; ) { const numCases = readInt(); foreach (caseId; 0 .. numCases) { const N = readLong(); long ans; if (N % A == 0) { for (long m = N / A; m > 0; m /= 10) { ans += m % 10; } } else { ans = -1; } writeln(ans); } } } catch (EOFException e) { } }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { string s; readV(s); long k; readV(k); auto i = s.countUntil!(si => si != '1'); if (i == -1 || i >= k) writeln(1); else writeln(s[i]); }
D
import std.algorithm; import std.conv; import std.range; import std.stdio; import std.string; long ask (int lo, int hi) { writeln ("? ", lo, " ", hi); stdout.flush (); return readln.strip.to !(long); } void solve (int n) { auto total = ask (1, n); int lo = 1; int hi = n; while (lo < hi) { int me = (lo + hi) / 2; auto cur = ask (1, me); if (cur < total) { lo = me + 1; } else { hi = me; } } int k = hi; auto all1 = ask (1, k); auto cut1 = ask (1, k - 1); auto delta1 = cast (int) (all1 - cut1); int j = k - delta1; auto all2 = ask (1, j - 1); auto cut2 = ask (1, j - 2); auto delta2 = cast (int) (all2 - cut2); int i = j - 1 - delta2; writeln ("! ", i, " ", j, " ", k); stdout.flush (); } void main () { auto tests = readln.strip.to !(int); foreach (test; 0..tests) { auto n = readln.strip.to !(int); solve (n); } }
D
import std.stdio, std.algorithm, std.string, std.conv, std.array, std.range, std.math; int read() { return readln.chomp.to!int; } int[] reads() { return readln.split.to!(int[]); } void solve() { auto a = reads(); writeln(abs(a[0] - a[1]) > 1 ? ":(" : "Yay!"); } void main() { solve(); readln; }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; void scan(T...)(ref T args) { 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; } enum mod = 1_000_000_007L; struct Mint { long x; this(long a) { x = a % mod; if (x < 0) x += mod; } this(Mint a) { x = a.x; } ref Mint opAssign(long a) { this = Mint(a); return this; } ref Mint opOpAssign(string op)(Mint rhs) { static if (op == "+") { x += rhs.x; if (x >= mod) x -= mod; } static if (op == "-") { x -= rhs.x; if (x < 0) x += mod; } static if (op == "*") { (x *= rhs.x) %= mod; } static if (op == "/") { this *= rhs.inv(); } return this; } ref Mint opOpAssign(string op)(long rhs) { static if (op == "^^") { this = powmod(this, rhs); return this; } else { return mixin("this " ~ op ~ "= Mint(rhs)"); } } const Mint powmod(Mint a, long b) { Mint res = 1, p = a; while (b > 0) { if (b & 1) res *= p; p *= p; b /= 2; } return res; } const Mint inv() { return powmod(this, mod - 2); } Mint opUnary(string op)() if (s == "-") { return Mint(-x); } Mint opBinary(string op, T)(const T rhs) { return mixin("Mint(this) " ~ op ~ "= rhs"); } Mint opBinaryRight(string op)(const long rhs) { return mixin("Mint(rhs) " ~ op ~ "= this"); } bool opEquals(Mint a, Mint b) { return a.x == b.x; } bool opEquals(long rhs) { long y = rhs % mod; if (y < 0) y += mod; return x == y; } string toString() { import std.conv : to; return x.to!string; } } unittest { long powmod(long a, long b) { return b > 0 ? powmod(a, b / 2)^^2 % mod * a^^(b & 1) % mod : 1L; } auto a = Mint(2), b = Mint(3); assert(a + b == 5); assert(a + 5 == 7); assert(1 + b == 4); assert(a - b == mod - 1); assert(a * b == 6); assert(a / b == 2 * powmod(3, mod - 2)); assert(a^^10 == 1024); Mint z; assert(z == 0); (z += 2) *= 3; assert(z == 6); } enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { string s; scan(s); auto p = ["dream", "dreamer", "erase", "eraser"]; while (!s.empty) { bool e; foreach (str ; p) { if (str.length > s.length) continue; if (str == s[$ - str.length .. $]) { s = s[0 .. $ - str.length]; e = true; break; } } if (!e) { writeln("NO"); return; } } writeln("YES"); }
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 a, b, q; readV(a, b, q); long[] s; readC(a, s); long[] t; readC(b, t); auto ss = s.assumeSorted, ts = t.assumeSorted; auto nearest(R)(long x, R y, bool d) { if (d) { auto r = y.lowerBound(x); return !r.empty ? r.back : -1; } else { auto r = y.upperBound(x); return !r.empty ? r.front : -1; } } auto calc(R)(long x, R s, R t) { auto r = 10L^^18; foreach (i; 0..4) { auto x1 = nearest(x, s, i&1); if (x1 == -1) continue; auto x2 = nearest(x1, t, (i>>1)&1); if (x2 == -1) continue; r = min(r, (x-x1).abs+(x1-x2).abs); } return r; } foreach (_; 0..q) { long x; readV(x); writeln(min(calc(x, ss, ts), calc(x, ts, ss))); } }
D
import std.functional, std.algorithm, std.container, std.typetuple, std.typecons, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv; N abs(N)(N a) { return (a < 0 ? -1 : 1) * a; } enum MOD = 10L^^9 + 7; long fact(long n) { if (n <= 1) { return 1; } else { return n * fact(n - 1) % MOD; } } void main() { long[] NM = readln.chomp.split.to!(long[]); long N = NM[0], M = NM[1]; if (N == M || abs(N - M) == 1) { long k = 1; if (N == M) { k = 2; } writeln(k * fact(N) * fact(M) % MOD); } else { writeln(0); } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { string s = read!string; writeln(s.count!(c => c == 'x') <= 7 ? "YES" : "NO"); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; void main() { int n, m; scan(n, m); auto a = iota(n).map!(i => readln.split.map!(x => x.to!int - 1).array).array; auto closed = new bool[](m); int ans = n; foreach (k ; 0 .. m) { auto cnt = new int[](m); int tmp; foreach (i ; 0 .. n) { while (!a[i].empty && closed[a[i].front]) { a[i].popFront(); } cnt[a[i].front]++; tmp = max(tmp, cnt[a[i].front]); } ans = min(ans, tmp); foreach (i ; 0 .. m) { if (cnt[i] == tmp) { closed[i] = true; break; } } debug { writeln(closed); } } 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.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 x; readV(x); auto m = 1; foreach (b; 2..x.nsqrt+1) foreach (p; 2..10) { auto c = b^^p; if (c > x) break; m = max(m, c); } writeln(m); } pure T nsqrt(T)(T n) { import std.algorithm, std.conv, std.range, core.bitop; if (n <= 1) return n; T m = T(1) << (n.bsr/2+1); return iota(1, m).map!"a * a".assumeSorted!"a <= b".lowerBound(n).length.to!T; }
D
import std; const MOD = 10^^9 + 7; 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; } long calc(int n, int k) { long[long] memo; for (int g = k; g >= 1; g--) { long cnt = modPow(k / g, n, MOD); for (int i = g + g; i <= k; i += g) { cnt -= memo[i]; if (cnt < 0) cnt += MOD; } memo[g] = cnt; } long ans = 0; foreach (g, cnt; memo) { auto x = g * cnt % MOD; ans = (ans + x) % MOD; } return ans; } void main() { int n, k; scan(n, k); writeln(calc(n, k)); } 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; import std.algorithm; import std.conv; import std.string; import std.array; import std.math; void main(string[] args) { auto input = readln().chomp.split.map!(to!int).array; if(input[1] == 100){input[1]++;} auto ans = input[1] * pow(100,input[0]); ans.writeln; }
D
unittest { assert( [ "10" ].parse.expand.solve == 5 ); assert( [ "1111111111111111111" ].parse.expand.solve == 162261460 ); assert( [ "111" ].parse.expand.solve == 27 ); // 追加ケース assert( [ "101" ].parse.expand.solve == 15 ); // 追加ケース assert( [ "011" ].parse.expand.solve == 9 ); // 追加ケース assert( [ "010" ].parse.expand.solve == 5 ); // 追加ケース } import std.conv; import std.range; import std.stdio; import std.typecons; void main() { stdin.byLineCopy.parse.expand.solve.writeln; } auto parse( Range )( Range input ) if( isInputRange!Range && is( ElementType!Range == string ) ) { auto l = input.front; return tuple( l ); } auto solve( string l ) { const MOD = 10L ^^ 9 + 7; auto dp = new Tuple!( long, long )[ 1 + l.length ]; dp[ 0 ][ 0 ] = 1; dp[ 0 ][ 1 ] = 0; foreach( i, b; l ) { // i桁目までlと同じにする場合のパターン数を計算 // (a[i],b[i])は l[i]==1 だったら (1,0)or(0,1) の2パターン、 l[i]==0 だったら (0,0) の1パターン dp[ 1 + i ][ 0 ] = ( dp[ i ][ 0 ] * ( b == '1' ? 2 : 1 ) ) % MOD; // i桁目まででl未満が確定している場合のパターン数を計算 // i桁目で初めてl未満が確定する場合の(a[i],b[i])は (0,0) の1パターン(ただし、l[i]==0 のときは上に含まれるので0パターン) // i-1桁目まででl未満が確定済の場合の(a[i],b[i])は (1,0)or(0,1)or(0,0) の3パターン dp[ 1 + i ][ 1 ] = ( ( dp[ i ][ 0 ] * ( b == '1' ? 1 : 0 ) ) + ( dp[ i ][ 1 ] * 3 ) % MOD ) % MOD; } return ( dp[ $ - 1 ][ 0 ] + dp[ $ - 1 ][ 1 ] ) % MOD; }
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; immutable long INF = 1L << 59; void main() { auto C = 3.iota.map!(_ => readln.split.map!(to!int).array).array; auto A = new int[][](3, 2); auto B = new int[][](2, 3); foreach (i; 0..3) A[i][0] = C[i][1] - C[i][0], A[i][1] = C[i][2] - C[i][1]; foreach (i; 0..3) B[0][i] = C[1][i] - C[0][i], B[1][i] = C[2][i] - C[1][i]; bool ok = true; foreach (i; 0..2) foreach (j; 0..2) if (A[i][j] != A[i+1][j]) ok = false; foreach (i; 0..2) foreach (j; 0..2) if (B[i][j] != B[i][j+1]) ok = false; writeln(ok ? "Yes" : "No"); }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; long calc(long n) { long ans = n * (n + 1) / 2 - n; return ans; } void main() { int n; scan(n); writeln(calc(n)); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { auto a = readln.chomp.to!BigInt(); auto b = readln.chomp.to!BigInt(); if (a > b) { writeln("GREATER"); } else if (a < b) { writeln("LESS"); } else if (a == b) { writeln("EQUAL"); } } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; alias Pair = Tuple!(int, "a", int, "b"); void main() { int N; scan(N); if (N == 1) { writeln("Hello World"); } else { int a, b; scan(a); scan(b); writeln(a + b); } } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
module Main; import std.stdio; import std.string; import std.conv; import std.array; import std.math; import std.algorithm; void main() { string input; immutable limitList = 50022; bool[] listPrimeNumber = new bool[](limitList); int number, primeNumberSmaller, primeNumberBigger; listPrimeNumber.fill(true); foreach (i; 2..limitList.to!double.sqrt.to!int) { if (listPrimeNumber[i]) { for (int j = i*2; j < limitList; j += i) listPrimeNumber[j] = false; } } while ((input = readln.chomp).length != 0) { number = input.to!int; primeNumberSmaller = getPrimeNumber(listPrimeNumber, number, false); primeNumberBigger = getPrimeNumber(listPrimeNumber, number, true); writeln(primeNumberSmaller, " ", primeNumberBigger); } } int getPrimeNumber(in bool[] list, in int input, in bool flagBigger) { int temp; if (flagBigger) { temp = input + 1; } else { temp = input - 1; } if (list[temp]) { return temp; } else { return getPrimeNumber(list, temp, flagBigger); } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { readint; auto ab = readints; int a = ab[0], b = ab[1]; auto ps = readints; int x = 0, y = 0, z = 0; foreach (p; ps) { if (p <= a) x++; else if (p <= b) y++; else z++; } writeln(min(x, y, z)); }
D
/* imports all std modules {{{*/ import std.algorithm, std.array, std.ascii, std.base64, std.bigint, std.bitmanip, std.compiler, std.complex, std.concurrency, std.container, std.conv, std.csv, std.datetime, std.demangle, std.encoding, std.exception, std.file, std.format, std.functional, std.getopt, std.json, std.math, std.mathspecial, std.meta, std.mmfile, std.net.curl, std.net.isemail, std.numeric, std.parallelism, std.path, std.process, std.random, std.range, std.regex, std.signals, std.socket, std.stdint, std.stdio, std.string, std.system, std.traits, std.typecons, std.uni, std.uri, std.utf, std.uuid, std.variant, std.zip, std.zlib; /*}}}*/ /+---test 3 7 10 ---+/ /+---test 14 12 112 ---+/ void main(string[] args) { const H = readln.chomp.to!long; const W = readln.chomp.to!long; const N = readln.chomp.to!long; ceil(N*1. / max(H, W)).to!int.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; int overlap(int a, int b, int c, int d) { int lo = max(a, c); int hi = min(b, d); return max(0, hi - lo); } void main() { int a, b, c, d; scan(a, b, c, d); writeln(overlap(a, b, c, d)); } 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; import std.string; import std.conv; import std.algorithm; import std.range; void main() { int ans; foreach (dchar[] line; stdin.lines) { line = line.chomp; if (line == line.retro.array) ans++; } ans.writeln; }
D
import std.stdio, std.conv, std.array,std.string,std.algorithm; void main() { auto input1=readln.chomp; writeln(input1[0],input1.length-2,input1[input1.length-1]); }
D
import std.stdio,std.algorithm; long n,no,ne,mino,so,se,temp; void main() { scanf(" %d",&n); mino=long.max; for(int i=0;i<n;++i) { scanf(" %d",&temp); if(temp%2) { mino=min(mino,temp); so+=temp; ++no; }else{ se+=temp; ++ne; } } writeln(se+so-(no%2?mino:0)); }
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() { long x; scan(x); long t; while (t * (t + 1) / 2 < x) t++; writeln(t); } 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; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
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() { foreach (line; stdin.byLine) { line.split.map!(to!int).reduce!("a+b").writeln; } }
D
void main(){ int m = _scan(); writeln(48-m); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nuv = readln.split.to!(int[]); auto N = nuv[0]; auto u = nuv[1]-1; auto v = nuv[2]-1; int[][] T; T.length = N; foreach (_; 0..N-1) { auto ab = readln.split.to!(int[]); auto A = ab[0]-1; auto B = ab[1]-1; T[A] ~= B; T[B] ~= A; } auto LS = new int[](N); void walk(int i, int p, int l) { LS[i] = l; foreach (j; T[i]) if (j != p) walk(j, i, l+1); } walk(v, -1, 0); int solve(int i, int p, int l) { if (l >= LS[i]) return LS[i]; auto r = LS[i]-1; foreach (j; T[i]) if (j != p) { r = max(r, solve(j, i, l+1)); } return r; } writeln(solve(u, -1, 0)); }
D
import std.conv, std.stdio; import std.algorithm, std.array, std.range, std.string; import std.numeric; void main() { auto buf = readln.chomp.split.to!(long[]); (buf[0] * (buf[1] / buf[0].gcd(buf[1]))).writeln; }
D
import std.stdio; import std.algorithm; import std.string; import std.conv; import std.math; void main(){ int[] primes = [2,3,5]; for(int i=7;primes.length <= 10000;i++){ bool flg = true; for(int j=0;primes[j]*primes[j]<=i;j++){ if(i%primes[j]==0){ flg = false; break; } } if(flg) primes ~= i; } while(true){ int n = to!int(chomp(readln())); if(n == 0) break; int sum = 0; for(int i=0;i<n;i++) sum += primes[i]; writeln(sum); } }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n, q; rd(n, q); auto tree=new SquareRootDecomposition(n); while(q--){ auto args=readln.split.to!(int[]); if(args[0]==0){ tree.update(args[1], args[2]+1, args[3]); }else{ writeln(tree.rmin(args[1], args[2]+1)); } } } class SquareRootDecomposition{ import std.algorithm; int D=1, nil=-1; int[] val, buc, laz; this(int n){ while(D*D<n) D++; val.length=D*D; buc.length=laz.length=D; fill(val, (1<<31)-1); fill(buc, (1<<31)-1); fill(laz, nil); } void update(int ql, int qr, int x){ foreach(k; 0..D){ int l=k*D, r=(k+1)*D; if(r<=ql || qr<=l){ // }else if(ql<=l && r<=qr){ buc[k]=x; laz[k]=x; }else{ push(k); foreach(i; max(l, ql)..min(r, qr)) val[i]=x; buc[k]=reduce!(min)(val[l..r]); } } } int rmin(int ql, int qr){ int ret=(1<<31)-1; foreach(k; 0..D){ int l=k*D, r=(k+1)*D; if(r<=ql || qr<=l){ // }else if(ql<=l && r<=qr){ chmin(ret, buc[k]); }else{ push(k); foreach(i; max(l, ql)..min(r, qr)) chmin(ret, val[i]); } } return ret; } void push(int k){ if(laz[k]<0) return; val[(k*D)..((k+1)*D)]=laz[k]; laz[k]=-1; } void chmin(T)(ref T l, T r){ if(l>r) l=r; } } 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 core.bitop; import std.algorithm; import std.array; import std.ascii; import std.container; import std.conv; import std.format; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; void main() { string str = readln.chomp; writeln = str.indexOf("9") == -1 ? "No" : "Yes"; }
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 nm = readln.split.to!(int[]); auto N = nm[0]; auto M1 = nm[1]; auto M2 = nm[2]; int[] as, bs, cs; foreach (_; 0..N) { auto abc = readln.split.to!(int[]); as ~= abc[0]; bs ~= abc[1]; cs ~= abc[2]; } auto DP = new int[][][](N+1, 10*N+1, 10*N+1); foreach (ref dp1; DP) foreach (ref dp2; dp1) dp2[] = int.max/3; DP[0][0][0] = 0; foreach (i; 0..N) { foreach (j; 0..10*N+1) { foreach (k; 0..10*N+1) { DP[i+1][j][k] = min(DP[i+1][j][k], DP[i][j][k]); auto jj = j + as[i]; auto kk = k + bs[i]; if (jj <= 10*N && kk <= 10*N) { DP[i+1][jj][kk] = min(DP[i+1][jj][kk], DP[i][j][k] + cs[i]); } } } } auto r = int.max/3; foreach (j; 1..10*N+1) { foreach (k; 1..10*N+1) { if (j * M2 == k * M1) { r = min(r, DP[N][j][k]); } } } writeln(r == int.max/3 ? -1 : r); }
D
void main() { problem(); } void problem() { auto S = scan; auto N = S.length; bool solve() { auto head = S[0..(N-1)/2]; auto tail = S[(N+3)/2-1..$]; S.deb; head.deb; tail.deb; auto s = S; for(long i = 0; i < s.length/2; i++) { if (s[i] != s[$-i-1]) return false; } s = head; for(long i = 0; i < s.length/2; i++) { if (s[i] != s[$-i-1]) return false; } s = tail; for(long i = 0; i < s.length/2; i++) { if (s[i] != s[$-i-1]) return false; } return true; } writeln(solve() ? "Yes" : "No"); } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int a; readV(a); int b; readV(b); int h; readV(h); writeln((a+b)*h/2); }
D
void main() { long n = rdElem; string s = "ACGT"; long[][][][] dp = new long[][][][](n+1, 4, 4, 4); foreach (i, a; s) { foreach (j, b; s) { foreach (k, c; s) { if (a == 'A' && b == 'G' && c == 'C') continue; if (a == 'A' && b == 'C' && c == 'G') continue; if (a == 'G' && b == 'A' && c == 'C') continue; dp[3][i][j][k] = 1; } } } foreach (i; 3 .. n) { foreach (j, a; s) { foreach (k, b; s) { foreach (l, c; s) { foreach (m, d; s) { if (b == 'A' && c == 'G' && d == 'C') continue; if (b == 'A' && c == 'C' && d == 'G') continue; if (b == 'G' && c == 'A' && d == 'C') continue; if (a == 'A' && c == 'G' && d == 'C') continue; if (a == 'A' && b == 'G' && d == 'C') continue; dp[i+1][k][l][m] = (dp[i+1][k][l][m] + dp[i][j][k][l]) % mod; } } } } } long result; foreach (i, a; s) { foreach (j, b; s) { foreach (k, c; s) { result = (result + dp[n][i][j][k]) % mod; } } } result.writeln; } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; enum double eps = 1.0e-9; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
import std.stdio; import std.conv; import std.string; void main() { int n = readln.chomp.to!int - 1; int[] a = readln.split.to!(int[]); int ans = 0; for (int i = 0; i < n; i++) { if (a[i] == a[i + 1]) { ans++; i++; } } writeln(ans); }
D
immutable long mod=998244353; immutable int M=1_000_00*4; void main(){ import std.stdio, std.algorithm; long n, a, b, k; rd(n, a, b, k); auto fact=genFact(M, mod); auto invFact=genInv(fact, mod); long comb(long nn, long rr){ if(nn<rr) return 0; long ret=fact[nn]%mod; (ret*=invFact[rr])%=mod; (ret*=invFact[nn-rr])%=mod; return ret; } long tot=0; for(long x=0; x<=n; x++)if(k-a*x>=0){ if((k-a*x)%b==0){ long y=(k-a*x)/b; if(y>n) continue; (tot+=comb(n, x)*comb(n, y)%mod)%=mod; } } writeln(tot); } long[] genFact(int n, long mod){ auto fact=new long[](n); fact[0]=fact[1]=1; foreach(i; 2..n) fact[i]=i*fact[i-1]%mod; return fact; } long[] genInv(long[] arr, long mod){ auto inv=new long[](arr.length); inv[0]=inv[1]=1; foreach(i, elem; arr[1..$]) inv[i]=powmod(arr[i], mod-2, mod); return inv; } long powmod(long a, long x, long mod){ if(x==0) return 1; else if(x==1) return a; else if(x&1) return a*powmod(a, x-1, mod)%mod; else return powmod(a*a%mod, x/2, mod); } 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; enum conv = [ 'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000 ]; void main() { foreach(input; stdin.byLine()) { int arabia = 0; if(input.length == 1) arabia = conv[input[0]]; else { foreach(i; 1 .. input.length) { if(conv[input[i - 1]] < conv[input[i]]) { arabia -= conv[input[i - 1]]; } else { arabia += conv[input[i - 1]]; } } arabia += conv[input[$ - 1]]; } arabia.writeln; } }
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; scan(n); int ans = 1; while (ans <= n / 2) { ans *= 2; } 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.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 = 998244353; //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 main() { auto H = RD; auto A = RD; writeln((H+A-1) / A); stdout.flush; debug readln; }
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, m; scan(n, m); auto adj = new int[][](n, 0); foreach (i ; 0 .. m) { int ai, bi; scan(ai, bi); ai--, bi--; adj[ai] ~= bi; adj[bi] ~= ai; } bool nibu = true; auto col = new int[](n); col[] = -1; col[0] = 0; void dfs(int u) { foreach (v ; adj[u]) { if (col[v] == -1) { col[v] = col[u] ^ 1; dfs(v); } else if (col[v] == col[u]) { nibu = false; return; } } } dfs(0); debug { writeln(col); } int x = col.count!"a == 0".to!int; int y = n - x; long ans; if (nibu) { ans = 1L * x * y - m; } else { ans = 1L * n * (n - 1) / 2 - m; } 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.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { readln.split.to!(int[]).map!(a => a-1).reduce!((a,b) => a*b).writeln; }
D
import std.conv, std.stdio; import std.algorithm, std.array, std.string, std.range; void main() { auto buf = readln.chomp.split.to!(int[]), k = buf[0], n = buf[1], a = readln.chomp.split.to!(int[]), l = a[0] + k - a[$-1]; foreach (i, e; a[1..$]) l = l.max(e - a[i]); (k - l).writeln; }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.ascii; void main() { int a, e, k; foreach (i; 0..6) { if (i == 0) a = readln.chomp.to!int; else if (i == 4) e = readln.chomp.to!int; else if (i == 5) k = readln.chomp.to!int; else readln; } if (e - a <= k) { writeln("Yay!"); } else { writeln(":("); } }
D
import std.stdio, std.string, std.conv; void main(){ auto ip = readln.chomp.to!(dchar[]); ip[3] = '8'; writeln(ip); }
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 s = readln.chomp.to!(char[]); auto t = readln.chomp.to!(char[]); bool[char] done; auto f = true; foreach (i; 0..s.length) { if (s[i] != t[i]) { if (done.get(s[i], true) && done.get(t[i], true)) { foreach (j; i+1..s.length) { if (s[j] == t[i]) { s[j] = s[i]; } else if (s[j] == s[i]) { s[j] = t[i]; } } s[i] = t[i]; } else { f = false; break; } } done[s[i]] = false; } if (f) { writeln("Yes"); } else { writeln("No"); } }
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 A = lreads(N); auto dp = new long[][](2, N + 1); dp[0][] = dp[1][] = long.min; dp[0][0] = 0; foreach (i; 0 .. N) { dprint(dp[0][i], dp[1][i]); if (dp[1][i] != long.min) { long a = A[i] + 1; long r = (a < 2) ? 0 : a % 2; dp[r][i + 1] = dp[r][i + 1].max(dp[1][i] + a / 2); } if (dp[1][i] != long.min) { long a = A[i]; long r = (a < 2) ? 0 : a % 2; dp[r][i + 1] = dp[r][i + 1].max(dp[1][i] + a / 2); } if (dp[0][i] != long.min) { long a = A[i]; long r = (a < 2) ? 0 : a % 2; dp[r][i + 1] = dp[r][i + 1].max(dp[0][i] + a / 2); } } writeln(max(dp[0][N], dp[1][N])); }
D
import std.stdio; import std.algorithm; import std.string; import std.range; import std.array; import std.conv; import std.complex; import std.math; import std.ascii; import std.bigint; import std.container; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } const real eps = 1e-10; void main(){ while(true) { auto e = readInt(); if(e == 0) return; long m = long.max; for(long i; i^^3 <= e; ++i) { for(long j; i^^3 + j^^2 <= e; ++j) { m = min(m, i+j+(e-i^^3-j^^2)); } } writeln(m); } }
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 s = readln.chomp; if (s.length % 2) s.length -= 1; else s.length -= 2; while (s[0..s.length/2] != s[s.length/2..$]) { s.length -= 2; } writeln(s.length); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.container; void main(){ auto arr = readln.chomp.split.map!(to!int).array; int a = arr[0], b = arr[1]; auto s = readln.chomp; auto t = readln.chomp; auto dp = new int[][][](a+2, b+2, 2); foreach(i; 0..a+1) foreach(j; 0..b+1) dp[i][j][0] = dp[i][j][1] = int.min; foreach(i; 0..a+1){ foreach(j; 0..b+1){ if(i != a){ if(s[i] == 'I') dp[i+1][j][1] = max(dp[i+1][j][1], dp[i][j][0] + 1, 1); else dp[i+1][j][0] = max(dp[i+1][j][0], dp[i][j][1] + 1); } if(j != b){ if(t[j] == 'I') dp[i][j+1][1] = max(dp[i][j+1][1], dp[i][j][0] + 1, 1); else dp[i][j+1][0] = max(dp[i][j+1][0], dp[i][j][1] + 1); } } } int ans = int.min; foreach(i; 0..a+2) foreach(j; 0..b+2) ans = max(ans, dp[i][j][1]); writeln(ans); }
D
import std.stdio; void main(){ auto cin = new Cin(); //auto j = new Joint(); auto NK = cin.line(); auto D = cin.line(); auto N = NK[0]; solve( N , D ).writeln(); } auto solve( size_t n , size_t[] ng ){ bool[10] nglist; nglist[] = true; bool up = false; foreach( elm ; ng ){ nglist[elm] = false; } forD:foreach( i ; n..n+100_000 ){ up = false; foreach_reverse( j ; 0..5 ){ n = i%(10^^(j+1))/(10^^j); if( !up && n==0 ){ continue; }else{ up = true; } if( nglist[n] == false ){ continue forD; } } return i; } return 0; } unittest{ assert( solve(1111,[1,3,4,5,6,7,8,9]) == 2000 ); assert( solve(9999,[0]) == 9999 ); assert( solve(9999,[9]) == 10000 ); assert( solve(10000,[0,9]) == 11111 ); assert( solve(10000,[1,0]) == 22222 ); assert( solve(5555,[5]) == 6000 ); assert( solve(5959,[9]) == 6000 ); } import std.stdio,std.conv,std.string; import std.algorithm,std.array; class Cin { T[] line( T = size_t , string token = " " )( size_t m = 1 ){ T[] arr = []; foreach( i ; 0..m ){ arr ~= this.read!T(); } return arr; } T[][] rect( T = real , string token = " " )( size_t m = 1 ){ T[][] arr = new T[][](); foreach( i ; 0..m ){ arr[i] = this.read!T(token); } return arr; } private T[] read( T = size_t )( string token = " " ){ T[] arr; foreach( elm ; readln().chomp().split(token) ){ arr ~= elm.to!T(); } return arr; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; enum next = [ "Sunny": "Cloudy", "Cloudy": "Rainy", "Rainy": "Sunny" ]; void main() { writeln(next[readln.chomp]); }
D
import std.conv, std.stdio; import std.algorithm, std.array, std.range, std.string; void main() { switch (readln.chomp[$-1]) { case '0', '1', '6', '8': return "pon".writeln; case '2', '4', '5', '7', '9': return "hon".writeln; default: return "bon".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.stdio, std.bitmanip; class SegmentTree { int[] table; int N, table_size; this(int n) { N = n; table_size = 1; while (table_size < n) table_size *= 2; table_size *= 2; table = new int[](table_size); } void add(int pos, int num) { add_rec(0, 0, table_size/2-1, pos, num); } void add_rec(int i, int left, int right, int pos, int num) { table[i] += num; if (left == right) return; auto mid = (left + right) / 2; if (pos <= mid) add_rec(i*2+1, left, mid, pos, num); else add_rec(i*2+2, mid+1, right, pos, num); } int sum(int pl, int pr) { return sum_rec(0, pl, pr, 0, table_size/2-1); } int sum_rec(int i, int pl, int pr, int left, int right) { if (pl > right || pr < left) return 0; else if (pl <= left && right <= pr) return table[i]; else return sum_rec(i*2+1, pl, pr, left, (left+right)/2) + sum_rec(i*2+2, pl, pr, (left+right)/2+1, right); } } void main() { alias Tuple!(int, "l", int, "r") Seg; int N, M; scanf("%d %d", &N, &M); int l, r; auto segs = new Seg[][](M+1); foreach (i; 0..N) { scanf("%d %d", &l, &r); segs[r-l+1] ~= Seg(l, r); } auto st = new SegmentTree(M+2); foreach (d; 1..M+1) { foreach (s; segs[d-1]) { N -= 1; st.add(s.l, 1); st.add(s.r+1, -1); } int ans = N; for (int i = d; i <= M; i += d) { ans += st.sum(0, i); } ans.writeln; //writeln(d, " ", ans, " ", N); } }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional, std.traits; import std.algorithm, std.container; void main() { long N = scanElem; auto S = readln.strip; auto T = readln.strip; long res; foreach(i;1..N+1) { if(S[$-i..$]==T[0..i]) { res=i; } } writeln(N*2-res); } class UnionFind{ UnionFind parent = null; void merge(UnionFind a) { if(same(a)) return; a.root.parent = this.root; } UnionFind root() { if(parent is null)return this; return parent = parent.root; } bool same(UnionFind a) { return this.root == a.root; } } void scanValues(TList...)(ref TList list) { auto lit = readln.splitter; foreach (ref e; list) { e = lit.fornt.to!(typeof(e)); lit.popFront; } } T[] scanArray(T = long)() { return readln.split.to!(long[]); } void scanStructs(T)(ref T[] t, size_t n) { t.length = n; foreach (ref e; t) { auto line = readln.split; foreach (i, ref v; e.tupleof) { v = line[i].to!(typeof(v)); } } } long scanULong(){ long x; while(true){ const c = getchar; if(c<'0'||c>'9'){ break; } x = x*10+c-'0'; } return x; } T scanElem(T = long)() { char[] res; int c = ' '; while (isWhite(c) && c != -1) { c = getchar; } while (!isWhite(c) && c != -1) { res ~= cast(char) c; c = getchar; } return res.strip.to!T; } template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } template cumulativeFold(fun...) if (fun.length >= 1) { import std.meta : staticMap; private alias binfuns = staticMap!(binaryFun, fun); auto cumulativeFold(R)(R range) if (isInputRange!(Unqual!R)) { return cumulativeFoldImpl(range); } auto cumulativeFold(R, S)(R range, S seed) if (isInputRange!(Unqual!R)) { static if (fun.length == 1) return cumulativeFoldImpl(range, seed); else return cumulativeFoldImpl(range, seed.expand); } private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args) { import std.algorithm.internal : algoFormat; static assert(Args.length == 0 || Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); static if (args.length) alias State = staticMap!(Unqual, Args); else alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); foreach (i, f; binfuns) { static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles, { args[i] = f(args[i], e); }()), algoFormat("Incompatible function/seed/element: %s/%s/%s", fullyQualifiedName!f, Args[i].stringof, E.stringof)); } static struct Result { private: R source; State state; this(R range, ref Args args) { source = range; if (source.empty) return; foreach (i, f; binfuns) { static if (args.length) state[i] = f(args[i], source.front); else state[i] = source.front; } } public: @property bool empty() { return source.empty; } @property auto front() { assert(!empty, "Attempting to fetch the front of an empty cumulativeFold."); static if (fun.length > 1) { import std.typecons : tuple; return tuple(state); } else { return state[0]; } } void popFront() { assert(!empty, "Attempting to popFront an empty cumulativeFold."); source.popFront; if (source.empty) return; foreach (i, f; binfuns) state[i] = f(state[i], source.front); } static if (isForwardRange!R) { @property auto save() { auto result = this; result.source = source.save; return result; } } static if (hasLength!R) { @property size_t length() { return source.length; } } } return Result(range, args); } } struct Factor { long n; long c; } Factor[] factors(long n) { Factor[] res; for (long i = 2; i ^^ 2 <= n; i++) { if (n % i != 0) continue; int c; while (n % i == 0) { n = n / i; c++; } res ~= Factor(i, c); } if (n != 1) res ~= Factor(n, 1); return res; } long[] primes(long n) { if(n<2)return []; auto table = new long[n+1]; long[] res; for(int i = 2;i<=n;i++) { if(table[i]==-1) continue; for(int a = i;a<table.length;a+=i) { table[a] = -1; } res ~= i; } return res; } bool isPrime(long n) { if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (long i = 3; i ^^ 2 <= n; i += 2) if (n % i == 0) return false; return true; }
D
import std.stdio,std.conv, std.algorithm; import std.container,std.array,std.range; import std.string,std.typecons,std.numeric; import std.math,std.random,std.functional,std.regex; double getExpected(int[] vs,double[] rs,int n,int m){ double res = 0; double sum = rs.sum; foreach(i;0..m){ res += vs[i] * rs[i] / sum; } return res; } void main(){ int t = read(); // ?????????????????° t ++; double max_p = 0; double last_p = 0; foreach(i;0..t){ int n,m; // m (??°?????????)< n read(n,m); auto vs = new int [m]; auto rs = new double [m]; foreach(j;0..m) read(vs[j],rs[j]); auto p = getExpected(vs,rs,n, m); if (i == t-1)last_p = p; else max_p = max_p.max(p); //p.writeln; } //max_p.writeln; //last_p.writeln; if(last_p + 0.0000001 <= max_p) "YES".writeln; else "NO".writeln; } int read(){int n;read(n);return n;} void read(T...) (auto ref T args){mixin(readMixin);} void readArray(T)(auto ref T args){mixin(readMixin);} const readMixin = q{ auto line = readln().split(); foreach(i,ref arg; args){ if (i >= line.length) return; arg = line[i].to!(typeof(arg)); } }; void times(int n,void delegate() stmt){ foreach(i;0..n)stmt();} void Swap(T)(ref T a,ref T b){T t = a;a = b;b = t;}
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { int n, k; scan(n, k); writeln(k == 1 ? 0 : n - k); }
D
import std.stdio, std.conv, std.array, std.string; import std.algorithm; import std.container; import std.range; import core.stdc.stdlib; void main() { auto N = readln.chomp.to!int; int[][] gameOrders; gameOrders.length = N; foreach(i; N.iota()) { gameOrders[i] = readln.split.to!(int[]); } int days = 0; auto ordersPerPlayer = gameOrders.map!(o => o.empty() ? -1 : o[0]).array; while(true){ bool[int] gamedPlayers; for(int player=0; player<N; player++) { if (ordersPerPlayer[player] == -1) continue; if (ordersPerPlayer[ordersPerPlayer[player]-1] == player+1) { gamedPlayers[player] = true; gamedPlayers[ordersPerPlayer[ordersPerPlayer[player]-1]-1] = true; } } if (gamedPlayers.length == 0) { writeln(-1); exit(0); } foreach(player; gamedPlayers.keys) { gameOrders[player].popFront(); ordersPerPlayer[player] = gameOrders[player].empty ? -1 : gameOrders[player][0]; } days++; if (!gameOrders.any!(o => !o.empty)) break; } writeln(days); }
D
import std.stdio, std.range, std.conv; import std.math; void main(){ int w, h; while(true){ scanf("%d %d", &h, &w); if(h == 0 && w == 0) break; foreach(i; 0..h){ foreach(j; 0..w){ write(((i+j)%2) ? "." : "#"); } writeln(); } writeln(); } }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; alias sread = () => readln.chomp(); ulong bignum = 1_000_000_007; alias Pair = Tuple!(long, "number", long, "times"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { auto n = lread(); auto p = aryread(); long count; foreach (i; 1 .. n - 1) { if(p[i - 1] < p[i] && p[i] < p[i + 1]) count++; if(p[i - 1] > p[i] && p[i] > p[i + 1]) count++; } count.writeln(); }
D
//dlang template---{{{ import std.stdio; import std.conv; import std.string; import std.array; import std.algorithm; // MIT-License https://github.com/kurokoji/nephele class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } //Digit count---{{{ int DigitNum(int num) { int digit = 0; while (num != 0) { num /= 10; digit++; } return digit; } //}}} //}}} void main() { Scanner sc = new Scanner; int a, b; sc.scan(a, b); if ((a * b) % 2 == 0) writeln("Even"); else writeln("Odd"); }
D
void main() { auto N = ri; ulong res; foreach(i; iota(1, N+1, 2)) { ulong tmp; foreach(j; 1..i+1) { if(i % j == 0) tmp++; } if(tmp == 8) res++; } res.writeln; } // =================================== import std.stdio; import std.string; import std.functional; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.conv, std.stdio, std.string; void main() { auto a = readln.chomp.to!int; auto s = readln.chomp; ((3200 <= a) ? s : "red").writeln; }
D
// import chie template :) {{{ static if (__VERSION__ < 2090) { import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons, std.format, std.bitmanip, std.numeric; } else { import std; } // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan(T...)(ref T args) { foreach (ref arg; args) { arg = next!(typeof(arg)); } } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } // }}} // alias {{{ alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less); alias MinHeap(T) = Heap!(T, "a > b"); // }}} // memo {{{ /* - ある値が見つかるかどうか <https://dlang.org/phobos/std_algorithm_searching.html#canFind> canFind(r, value); -> bool - 条件に一致するやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#filter> // 2で割り切れるやつ filter!"a % 2 == 0"(r); -> Range - 合計 <https://dlang.org/phobos/std_algorithm_iteration.html#sum> sum(r); - 累積和 <https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold> // 今の要素に前の要素を足すタイプの一般的な累積和 // 累積和のrangeが帰ってくる(破壊的変更は行われない) cumulativeFold!"a + b"(r, 0); -> Range - rangeをarrayにしたいとき array(r); -> Array - 各要素に同じ処理をする <https://dlang.org/phobos/std_algorithm_iteration.html#map> // 各要素を2乗 map!"a * a"(r) -> Range - ユニークなやつだけ残す <https://dlang.org/phobos/std_algorithm_iteration.html#uniq> uniq(r) -> Range - 順列を列挙する <https://dlang.org/phobos/std_algorithm_iteration.html#permutations> permutation(r) -> Range - ある値で埋める <https://dlang.org/phobos/std_algorithm_mutation.html#fill> fill(r, val); -> void - バイナリヒープ <https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap> // 昇順にするならこう(デフォは降順) BinaryHeap!(Array!T, "a > b") heap; heap.insert(val); heap.front; heap.removeFront(); - 浮動小数点の少数部の桁数設定 // 12桁 writefln("%.12f", val); - 浮動小数点の誤差を考慮した比較 <https://dlang.org/phobos/std_math.html#.approxEqual> approxEqual(1.0, 1.0099); -> true - 小数点切り上げ <https://dlang.org/phobos/std_math.html#.ceil> ceil(123.4); -> 124 - 小数点切り捨て <https://dlang.org/phobos/std_math.html#.floor> floor(123.4) -> 123 - 小数点四捨五入 <https://dlang.org/phobos/std_math.html#.round> round(4.5) -> 5 round(5.4) -> 5 */ // }}} bool[100001] makePrimeTable() { bool[100001] ret; ret[2 .. $] = true; for (size_t i = 2; i * i < ret.length; ++i) { if (!ret[i]) continue; for (size_t j = i + i; j < ret.length; j += i) { ret[j] = false; } } return ret; } void main() { auto cin = new Scanner; int q; cin.scan(q); enum isPrime = makePrimeTable; long[] tot = new long[100001]; foreach (i; 0 .. tot.length) { tot[i] = (i & 1) && isPrime[i] && isPrime[(i + 1) / 2]; } foreach (i; 1 .. tot.length) { tot[i] += tot[i - 1]; } foreach (i; 0 .. q) { int l, r; cin.scan(l, r); writeln(tot[r] - tot[l - 1]); } }
D
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons; void main() { const tmp = readln.split.to!(int[]); writeln(tmp[1] % tmp[0] == 0 ? tmp[0] + tmp[1] : tmp[1] - tmp[0]); }
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() { immutable long MOD = 924844033; auto s = readln.split.map!(to!int); auto N = s[0]; auto K = s[1]; auto F = new long[](N+1); F[0] = F[1] = 1; foreach (i; 2..N+1) F[i] = F[i-1] * i % MOD; auto C = new long[][](N+10, N+10) ; C[0][0] = 1; foreach (i; 0..N+9) { foreach (j; 0..N+9) { (C[i+1][j] += C[i][j]) %= MOD; (C[i+1][j+1] += C[i][j]) %= MOD; } } auto dp = new long[][][](N+1, N+1, 2); dp[0][0][0] = 1; foreach (i; 0..N) { foreach (j; 0..N) { dp[i+1][j][0] = (dp[i][j][0] + dp[i][j][1]) % MOD; dp[i+1][j+1][1] = dp[i][j][0]; } } auto dp2 = new long[][](2, N+1); dp2[0][0] = 1; int cur = 0, tar = 1; foreach (_; 0..2) { for (int i = 0; i < K; ++i) { int n = N / K; if (i < N % K) n += 1; for (int j = 0; j <= N; ++j) { for (int k = 0; j + k <= N && k <= n; ++k) { dp2[tar][j+k] += dp2[cur][j] * dp[n-1][k].sum % MOD; dp2[tar][j+k] %= MOD; } } swap(cur, tar); fill(dp2[tar], 0L); } } for (int i = N; i >= 0; --i) { dp2[cur][i] = dp2[cur][i] * F[N-i] % MOD; for (int j = i + 1; j <= N; ++j) { (dp2[cur][i] -= dp2[cur][j] * C[j][i] % MOD) %= MOD; } } writeln((dp2[cur][0] + MOD) % MOD); }
D
void main() { problem(); } void problem() { const N = scan!long; const S = scan; long solve() { long ans; const totalR = S.count('R'); const totalG = S.count('G'); const totalB = S.count('B'); long[] R = new long[N]; long[] G = new long[N]; long[] B = new long[N]; int countR, countG, countB; foreach(i; 0..N) { R[i] = totalR - countR; G[i] = totalG - countG; B[i] = totalB - countB; if (S[i] == 'R') countR++; if (S[i] == 'G') countG++; if (S[i] == 'B') countB++; } long[][char] RGB; RGB['R'] = R; RGB['G'] = G; RGB['B'] = B; char[][char] OTHERS; OTHERS['R'] = ['G', 'B']; OTHERS['G'] = ['R', 'B']; OTHERS['B'] = ['R', 'G']; foreach(i; 0..N-2) { const c1 = S[i]; foreach(j; i+1..N-1) { const spread = j - i; const c2 = S[j]; if (c1 == c2) continue; const others = OTHERS[c1]; const c3 = others[0] == c2 ? others[1] : others[0]; // [c1, c2, c3].deb; RGB[c3][j+1].deb; ans += RGB[c3][j+1]; if (j + spread < N && S[j + spread] == c3) { [i, j, j + spread].deb; [S[i], S[j], S[j + spread]].deb; ans--; } } } return ans; } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
import std.stdio; import std.conv; import std.algorithm; import std.range; import std.string; import std.typecons; import std.math; import std.range; class State { public: bool[] xx; int[] yy; bool[] zz; bool[] ww; Chunks!(dchar[]) board; } bool ad(State state, int y) { if (y == 8) { for (y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { if (state.board[x][y] == 'Q') { if (state.yy[x] != y) return false; } } } return true; } for (int x = 0; x < 8; x++) { if (state.xx[x] || state.zz[x + y] || state.ww[y - x + 8 - 1]) continue; state.yy[y] = x; state.xx[x] = true; state.zz[x + y] = true; state.ww[y - x + 8 - 1] = true; if (ad(state, y + 1)) { return true; } state.ww[y - x + 8 - 1] = !true; state.zz[x + y] = !true; state.xx[x] = !true; state.yy[y] = 0; } return false; } void main() { auto b = new dchar[8 * 8]; b.fill(' '); State state = new State(); state.xx = new bool[8]; state.yy = new int[8]; state.zz = new bool[8 * 2]; state.ww = new bool[8 * 2]; state.board = b.chunks(8);; foreach (i; iota(readln().strip.to!int)) { int x, y; scanf("%d %d", &x, &y); state.board[x][y] = 'Q'; } ad(state, 0); for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { if (state.xx[x] && state.yy[y] == x) write('Q'); else write('.'); } writeln(); } }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; import std.numeric; void main() { string x; scan(x); int st, ans; foreach (ch ; x) { if (ch == 'S') { st++; } else { if (st > 0) { st--; } else { ans++; } } } ans += st; 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.algorithm, std.string, std.conv, std.array, std.range, std.math; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.to!(int[]); } void main() { int n = readint(); int[] a = readints(); int m = a.reduce!max; int mid = m / 2; int md = 1145141919, r = 0; foreach (i; a) if (i != m) { int d = abs(mid - i); if (d < md) { md = d; r = i; } } writeln(m, " ", r); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto as = readln.split.to!(int[]); long[][] DP; DP.length = N; foreach (ref dp; DP) dp.length = N; auto x = (N-1)%2; long solve(int i, int j) { if (i > j) return 0; if (DP[i][j]) return DP[i][j]; if ((i+j)%2 == x) { return DP[i][j] = max(solve(i+1, j) + as[i], solve(i, j-1) + as[j]); } else { return DP[i][j] = min(solve(i+1, j) - as[i], solve(i, j-1) - as[j]); } } writeln(solve(0, N-1)); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n, a, b, c, d; scan(n, a, b, c, d); auto s = readln.chomp; a--, b--, c--, d--; bool ac, bd; ac = bd = true; foreach (i ; a .. c) { if (s[i] == '#' && s[i + 1] == '#') { ac = false; break; } } foreach (i ; b .. d) { if (s[i] == '#' && s[i + 1] == '#') { bd = false; break; } } bool dc; if (c < d) { dc = true; } else { foreach (i ; b - 1 .. d) { if (s[i] == '.' && s[i + 1] == '.' && s[i + 2] == '.') { dc = true; break; } } } yes(ac && bd && dc); } int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new int[][](n, 0); foreach (i; 0 .. m) { int u, v; scan(u, v); if (is1indexed) { u--, v--; } adj[u] ~= v; if (isUndirected) { adj[v] ~= u; } } return adj; } alias Edge = Tuple!(int, "to", int, "cost"); Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new Edge[][](n, 0); foreach (i; 0 .. m) { int u, v, c; scan(u, v, c); if (is1indexed) { u--, v--; } adj[u] ~= Edge(v, c); if (isUndirected) { adj[v] ~= Edge(u, c); } } return adj; } void yes(bool b) { writeln(b ? "Yes" : "No"); } void YES(bool b) { writeln(b ? "YES" : "NO"); } T[] readArr(T)() { return readln.split.to!(T[]); } T[] readArrByLines(T)(int n) { return iota(n).map!(i => readln.chomp.to!T).array; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "l ", long, "r"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long a, b, c, d, k; scan(a, b, c, d, k); long h; if (b < d) h += (c - a) * 60 + (d - b); else h += (c - a - 1) * 60 + (d - b) + 60; writeln(h - k); } 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.math; void main() { double n; scanf("%lf", &n); auto y = n / 1.08; auto n2 = n.to!int; auto y2 = y.ceil.to!int; if (floor(y2 * 1.08).to!int == n2 || floor((y2+1) * 1.08).to!int == n2) { y2.write; } else { ":(".write; } }
D
import std.stdio, std.string, std.conv, std.array, std.algorithm; void main() { auto a = readln.split.map!(to!int); writeln("a ", a[0] == a[1] ? "==" : (a[0] < a[1] ? "<" : ">"), " b"); }
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 nk = readints; int n = nk[0], k = nk[1]; auto xs = readints; int[] pos, neg; foreach (x; xs) { if (x >= 0) pos ~= x; else neg ~= x; } neg.reverse(); long ans = long.max; for (int i = 0; i < pos.length; i++) { long p = pos[i]; int rest = k - i - 1; if (rest == 0) ans = min(ans, pos[i]); if (rest < 0) break; if (0 < rest && rest <= neg.length) { ans = min(ans, 2 * pos[i] + neg[rest-1] * -1); } } for (int i = 0; i < neg.length; i++) { int rest = k - i - 1; if (rest == 0) ans = min(ans, -neg[i]); if (rest < 0) break; if (0 < rest && rest <= pos.length) { ans = min(ans, 2 * -neg[i] + pos[rest-1]); } } writeln(ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; int[][10^^5] NS; int[10^^5] ORIGIN, DEPS; void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; foreach (i; 0..N+M-1) { auto ab = readln.split.to!(int[]); NS[ab[1]-1] ~= ab[0]-1; } int root; foreach (i; 0..N) { if (NS[i].empty) { root = i; break; } } ORIGIN[0..N] = -1; int deps(int me) { if (me == root) return 0; if (ORIGIN[me] != -1) return DEPS[me]; auto cs = NS[me]; int p, d = -1; foreach (c; cs) { auto dd = deps(c); if (d < dd) { p = c; d = dd; } } ORIGIN[me] = p; DEPS[me] = d+1; return d+1; } foreach (i; 0..N) { if (i == root || ORIGIN[i] != -1) continue; deps(i); } foreach (org; ORIGIN[0..N]) { writeln(org+1); } }
D
void main() { long n, a, b; rdVals(n, a, b); long[] h = n.rdCol; bool check(long x) { long cnt; foreach (y; h) { long rem = max(0, y-x*b); cnt += (rem + a - b - 1) / (a - b); } return cnt <= x; } long ok = 10 ^^ 10, ng = -1; while (abs(ok - ng) > 1) { long m = (ok + ng) / 2; if (check(m)) ok = m; else ng = m; } ok.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
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { auto s = sread(); auto tmp = new long[](4); //北西南東 foreach (i; 0 .. s.length) { if (s[i] == 'N') { tmp[0] = 1; } else if (s[i] == 'W') { tmp[1] = -1; } else if (s[i] == 'S') { tmp[2] = -1; } else if (s[i] == 'E') { tmp[3] = 1; } } // writeln(tmp); if (tmp[0] + tmp[2] == 0 && tmp[1] + tmp[3] == 0) { writeln("Yes"); } else { writeln("No"); } } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto AS = readln.split.to!(long[]); auto DP = new long[][](N+5, 3); foreach (ref dp; DP) dp[] = long.min/3; DP[0][0] = 0; foreach (i; 0..N) { foreach (j; 0..3) { DP[i+2][j] = max(DP[i+2][j], DP[i][j] + AS[i]); if (j < 2) DP[i+1][j+1] = max(DP[i+1][j+1], DP[i][j]); } } if (N%2 == 0) { writeln(max(DP[N-2][0] + AS[N-2], DP[N-1][1] + AS[N-1])); } else { writeln(max(DP[N-1][2] + AS[N-1], DP[N-2][1] + AS[N-2], DP[N-3][0] + AS[N-3])); } }
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 q = RD!int; auto ans = new long[](q); foreach (i; 0..q) { auto n = RD; long cnt; while (n != 1) { if (n % 2 == 0) { n /= 2; } else if ((n * 2) % 3 == 0) { n = (n * 2) / 3; } else if ((n * 4) % 5 == 0) { n = (n * 4) /5; } else { cnt = -1; break; } ++cnt; } ans[i] = cnt; } foreach (e; ans) writeln(e); stdout.flush(); debug readln(); }
D
import std.stdio, std.string, std.conv, std.bigint, std.typecons, std.algorithm, std.array, std.math, std.range, std.functional; void main() { auto N = readln.chomp.to!int; N.iota.map!(i => readln.split.to!(ulong[]).pipe!(range => range[1] - range[0] + 1)).sum.writeln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { int n; scan(n); int[string] a; foreach (i ; 0 .. n) { auto s = readln.chomp; if (s in a) { a[s]++; } else { a[s] = 1; } } int m; scan(m); foreach (i ; 0 .. m) { auto s = readln.chomp; if (s in a) { a[s]--; } else { a[s] = -1; } } int ans; foreach (i ; a.byValue) { ans = max(ans, i); } 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 core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "l ", long, "r"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long a, b, n; scan(a, b, n); writeln((a * min(b - 1, n) / b) - a * ((min(b - 1, n) / b))); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.algorithm; import std.range; import std.stdio; import std.string; immutable int letters = 26; void main () { string s; while ((s = readln.strip) != "") { auto a = new int [] [letters]; foreach (int i, c; s.map !(x => x - 'a').array) { a[c] ~= i; } int n = cast (int) (s.length); int res = int.max; foreach (b; a.filter !(x => !x.empty)) { debug {writeln (b);} int cur = b[0]; foreach (j; 1..b.length) { cur = max (cur, (b[j] - b[j - 1] - 1)); } cur = max (cur, n - b[$ - 1] - 1); res = min (res, cur); } writeln (res); } }
D
import std.stdio; import std.conv; import std.string; import std.array; void main(){ int n=to!int(readln.chomp); while(n--){ auto s=new bool[32]; auto input=readln.chomp; for(int i=0;i<8;++i){ int a=(input[i]<='9')?input[i]-'0':input[i]-'a'+10; for(int j=0;j<4;++j){ s[4*i+j]=(a&(1<<(3-j)))?true:false; } } //for(int i=0;i<32;++i)stderr.writef("%d",s[i]?1:0); //stderr.writeln(); string ans; int tmp=0; for(int i=0;i<24;++i){ tmp+=s[i+1]?(1<<(23-i)):0; } ans~=((s[0])?"-":"")~to!string(tmp); tmp=0; bool f=false; for(int i=25;i<32;++i){ tmp+=(s[i])?(78125<<(31-i)):0; } int cnt=0; if(tmp==0)cnt=0; else{ if(tmp<1000000)++cnt; if(tmp<100000)++cnt; } while(tmp%10==0&&tmp!=0)tmp/=10; ans~="."~"0".replicate(cnt)~to!string(tmp); writeln(ans); } }
D
import std.algorithm; import std.ascii; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } void main() { auto s = readln.chomp; bool found = false; foreach (c; lowercase) { auto p = s.indexOf(c); if (p == -1) { writeln(c); found = true; break; } } if (!found) writeln("None"); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.math; void main() { int n, m; scan(n, m); auto cnt = new int[](n); auto red = new bool[](n); cnt[] = 1; red[0] = 1; foreach (i ; 0 .. m) { int xi, yi; scan(xi, yi); xi--, yi--; if (red[xi]) { red[yi] = 1; if (cnt[xi] - 1 == 0) red[xi] = 0; } cnt[xi]--; cnt[yi]++; } auto ans = iota(n).count!(i => red[i] && cnt[i] > 0); writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; auto as = [0] ~ readln.split.to!(int[]); int r; foreach (i; 1..N+1) { if (as[as[i]] == i) ++r; } writeln(r/2); }
D
void main(){ int n = _scan(); int[string] dic; foreach(i; 0..n){ dic[readln().chomp()]++; } dic.length.writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std.stdio,std.string,std.conv; int main() { string s; while((s = readln.chomp).length != 0) { string[] _s = s.split(" "); int a = _s[0].to!int; int b = _s[1].to!int; int n = _s[2].to!int; int ans = 0; a %= b; foreach(i;0..n) { a *= 10; ans += (a/b); a %= b; } ans.writeln; } return 0; }
D
void main(){ string s = readln().chomp(); s.count('1').writeln(); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; long n; rd(n); long m=1; while(m<n){ if((n&m)!=m){ writeln(m); return; } m*=2; } writeln(n+1); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; import std.ascii; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; bool calc(string s) { if (s[0] != 'A') return false; if (s[2..$-1].count!(c => c == 'C') != 1) return false; if (s.count!(c => isUpper(c)) > 2) return false; return true; } void main() { auto s = readln.chomp; writeln(calc(s) ? "AC" : "WA"); }
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; alias C = Tuple!(int, int); immutable C[] dirs = [ tuple (1, 0), tuple (-1, 0), tuple (0, 1), tuple (0, -1) ]; enum inf = int.max; void main() { auto s = readln.strip.splitter; immutable int h = s.front.to!int;s.popFront; immutable int w = s.front.to!int; auto a = new char[][h]; foreach (j; 0 .. h) { auto z = readln; a[j] = z[0 .. w].dup; } auto d = new int[][](h, w); void clear () { foreach (j; 0 .. h) foreach (i; 0 .. w) d[j][i] = inf; } C[] bfs (in int sy, in int sx, out int dy, out int dx, out int dist) { d[sy][sx] = 0; C[] q; q ~= C (sy, sx); int left = 0; while (left < q.length) { auto t = q[left++]; immutable int y = t[0], x = t[1]; dy = y; dx = x; foreach (const ref p; dirs) { immutable j = y + p[0]; if (j < 0 || j >= h) continue; immutable i = x + p[1]; if (i < 0 || i >= w) continue; if (d[j][i] < inf || a[j][i] == '#') continue; q ~= C (j, i); d[j][i] = d[y][x] + 1; } } dist = d[dy][dx]; return q; } int res; foreach (j; 0 .. h) foreach (i; 0 .. w) if (a[j][i] != '#') { int y, x, dd; clear (); bfs (j, i, y, x, dd); res = max (res, dd); } /* C[] u; clear (); foreach (j; 0 .. h) foreach (i; 0 .. w) if (a[j][i] != '#' && d[j][i] == inf) { int y, x, dd; bfs (j, i, y, x, dd); u ~= tuple (y, x); } clear (); int res; foreach (p; u) { int y, x, dd; res = max (res, dd); } */ writeln (res); }
D
import std.stdio, std.math, std.algorithm, std.array, std.string, std.conv, std.container, std.range; T Read(T)() { return readln.chomp.to!(T); } T[] Reads(T)() { return readln.split.to!(T[]); } alias read = Read!(int); alias reads = Reads!(int); int[][] c; void main() { int n = read(); foreach (i;0..n) c ~= reads(); long s; foreach (i; 0..n) foreach (j; 0..n) if (i < j) s += min(c[i][j], c[j][i]); writeln(s); }
D