code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio, std.string, std.conv, std.math, std.regex; void main() { auto n = readln.split.to!(int[]); if(abs(n[1] - n[0]) < abs(n[2] - n[0])){ writeln('A'); } else { writeln('B'); } }
D
import core.bitop, std.bitmanip; import core.checkedint; import std.algorithm, std.functional; import std.array, std.container; import std.bigint; import std.conv; import std.math, std.numeric; import std.range, std.range.interfaces; import std.stdio, std.string; import std.typecons; void main() { auto s = readln.chomp; bool ok = true; foreach (i, e; s) { if (i % 2 == 0) { ok &= e == 'R' || e == 'U' || e == 'D'; } else { ok &= e == 'L' || e == 'U' || e == 'D'; } } writeln(ok ? "Yes" : "No"); }
D
import std; bool calc(int k, int a, int b) { // return a <= b / k * k; return (a + k - 1) / k * k <= b; } void main() { int k; scan(k); int a, b; scan(a, b); writeln(calc(k, a, b) ? "OK" : "NG"); } 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, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional, std.traits; import std.algorithm, std.container; import core.stdc.stdlib; void main() { char[] S = readln.strip.to!(char[]); auto a = S[0..2].to!int; auto b = S[2..4].to!int; ; bool isYYMM=b>0&&b<=12; bool isMMYY=a>0&&a<=12; if(isYYMM&&isMMYY){ writeln("AMBIGUOUS"); return; } if(isYYMM||isMMYY){ if(isYYMM){ writeln("YYMM"); return; }else{ writeln("MMYY"); return; } }else{ writeln("NA"); return; } } long gcd(long a, long b) { if(b == 0) return a; return gcd(b, a % b); } class UnionFind{ UnionFind parent = null; void merge(UnionFind a) { if(same(a)) return; a.root.parent = this.root; } UnionFind root() { if(parent is null)return this; return parent = parent.root; } bool same(UnionFind a) { return this.root == a.root; } } void scanValues(TList...)(ref TList list) { auto lit = readln.splitter; foreach (ref e; list) { e = lit.fornt.to!(typeof(e)); lit.popFront; } } T[] scanArray(T = long)() { return readln.split.to!(T[]); } 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; import std.algorithm; import std.string; import std.conv; void main() { auto value =[6, 4, 3, 2]; foreach (i; 0..4) { auto a = readln.split.map!(to!int); writeln(value[a[0]-1] * a[1] * 1000); } }
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 h, w; readV(h, w); string[] s; readC(h, s); auto b = new int[][](h+2, w+2); foreach (i; 0..h) foreach (j; 0..w) b[i+1][j+1] = s[i][j] == '#'; foreach (i; 1..h+1) foreach (j; 1..w+1) { if (!b[i][j]) continue; auto r = b[i-1][j]+b[i+1][j]+b[i][j-1]+b[i][j+1]; if (r == 0) { 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 readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n; readV(n); int t, a; readV(t, a); t *= 1000; a *= 1000; int[] h; readA(n, h); auto d = 10^^9, p = 0; foreach (int i, hi; h) { auto e = (t-hi*6-a).abs; if (e < d) { p = i+1; d = e; } } writeln(p); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.math; import std.regex; import std.range; void main() { auto ip = readln.split.to!(ulong[]), N = ip[0], M = ip[1]; /*auto a = new bool[][](N, M); //N: tate, M: yoko // omote: false, ura: true foreach(i; 0..N) { //tate foreach(j; 0..M) { //yoko foreach(k; -1..2) { //tate if(i+k < 0 || i+k >= N) continue; foreach(l; -1..2) { //yoko if(j+l < 0 || j+l >= M) continue; a[i+k][j+l] = !a[i+k][j+l]; } } } } //a.writeln; uint count; foreach(b; a) { foreach(c; b) { if(c) count++; } } count.writeln; */ if(N == 1 && M == 1) 1.writeln; else if(N == 1) (M - 2).writeln; else if(M == 1) (N - 2).writeln; else ((N-2)*(M-2)).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() { auto r = -1; foreach (i; 1..11) { if (!ask(10L^^i)) { r = i; break; } } if (r == -1) { foreach (i; 1..11) { if (ask(10L^^i-1)) { writeln("! ", 10L^^(i-1)); return; } } } auto mi = 10L^^(r-1), ma = 10L^^r-1; while (ma-mi > 1) { auto c = (ma+mi)/2; if (ask(c*10)) ma = c; else mi = c; } writeln("! ", ask(mi*10) ? mi : ma); } auto ask(long n) { writeln("? ", n); stdout.flush(); string s; readV(s); return s == "Y"; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.bigint; struct PriorityQueue(alias _fun, T) { import std.functional : binaryFun; import std.algorithm : swap; alias fun = binaryFun!_fun; /// this(T[] ts) { foreach (t; ts) enqueue(t); } /// PriorityQueue!(_fun, T) enqueue(T e) { if (this.tree.length == 0) this.tree.length = 1; if (this.tree.length == this.n) this.tree.length *= 2; this.tree[this.n] = e; auto i = this.n; this.n += 1; while (i) { auto j = (i-1)/2; if (fun(this.tree[i], this.tree[j])) { swap(this.tree[i], this.tree[j]); i = j; } else break; } return this; } alias insertFront = enqueue; alias insert = enqueue; /// T dequeue() { auto ret = this.tree[0]; this.n -= 1; this.tree[0] = this.tree[this.n]; this.tree = this.tree[0..$-1]; size_t i; for (;;) { auto l = i*2+1; auto r = i*2+2; if (l >= this.n) break; size_t j; if (r >= this.n) { j = l; } else { j = fun(this.tree[r], this.tree[l]) ? r : l; } if (fun(this.tree[j], this.tree[i])) { swap(this.tree[i], this.tree[j]); i = j; } else break; } return ret; } /// @property T front() { return this.tree[0]; } /// @property bool empty() { return this.n == 0; } /// void popFront() { this.dequeue(); } alias removeFront = popFront; /// @property size_t length() { return this.n; } private: size_t n; T[] tree; } /// PriorityQueue!(fun, T) priority_queue(alias fun, T)(T[] ts = []) { return PriorityQueue!(fun, T)(ts); } // alias P = Tuple!(int, "l", int, "r", long, "c"); alias P = Tuple!(int, "p", long, "c", long, "max_c"); void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; auto ps = new P[](M); P[][] G; G.length = N; foreach (i; 1..N) G[i] ~= P(i-1, 0, long.max); foreach (i; 0..M) { auto lrc = readln.split.to!(int[]); auto l = lrc[0]-1; auto r = lrc[1]-1; long c = lrc[2]; G[l] ~= P(r, c, long.max); } auto Q = priority_queue!("a.max_c < b.max_c", P)([P(0, 0, 0)]); long res = -1; while (!Q.empty) { auto p = Q.dequeue(); foreach (ref r; G[p.p]) if (r.max_c > p.max_c + r.c) { r.max_c = p.max_c + r.c; if (r.p == N-1) { if (res == -1) { res = r.max_c; } else { res = min(res, r.max_c); } } Q.enqueue(r); } } writeln(res); 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; struct UnionFind { int N; int[] parent; // 根は -(連結成分のサイズ) としておく int cc; this(int n) in { assert(n > 0); } body { N = n; parent = new int[](N); parent[] = -1; cc = n; } int root(int x) in { assert(0 <= x && x < N); } body { if (parent[x] < 0) return x; else return parent[x] = root(parent[x]); } int size(int x) in { assert(0 <= x && x < N); } body { return -parent[root(x)]; } bool same(int x, int y) in { assert(0 <= x && x < N); assert(0 <= y && y < N); } body { return root(x) == root(y); } bool merge(int x, int y) in { assert(0 <= x && x < N); assert(0 <= y && y < N); } body { import std.algorithm : swap; x = root(x); y = root(y); if (x == y) return false; if (size(x) < size(y)) swap(x, y); // size(x) >= size(y) parent[x] += parent[y]; parent[y] = x; cc--; return true; } } unittest { auto uf = UnionFind(10); uf.merge(0, 8); uf.merge(2, 7); uf.merge(3, 6); assert(!uf.same(0, 6)); assert(!uf.same(4, 5)); assert(uf.same(2, 7)); assert(uf.size(8) == 2); assert(uf.size(2) == 2); assert(uf.size(3) == 2); assert(uf.size(4) == 1); uf.merge(2, 6); uf.merge(0, 5); uf.merge(0, 9); assert(uf.same(8, 9)); assert(uf.same(9, 5)); assert(uf.same(6, 7)); assert(uf.same(7, 3)); assert(uf.size(2) == 4); assert(uf.size(6) == 4); assert(uf.size(5) == 4); assert(uf.size(8) == 4); assert(uf.size(9) == 4); assert(uf.size(1) == 1); assert(!uf.same(1, 9)); assert(!uf.same(5, 7)); assert(!uf.same(0, 3)); assert(!uf.same(8, 2)); } void main() { int N, M; scan(N, M); auto a = new int[](M); auto b = new int[](M); foreach (i ; 0 .. M) { scan(a[i], b[i]); } a[] -= 1; b[] -= 1; int ans; foreach (i ; 0 .. M) { auto uf = UnionFind(N); foreach (j ; 0 .. M) { if (i == j) continue; uf.merge(a[j], b[j]); } ans += uf.cc > 1; } 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; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; void main() { string n; scan(n); if (n[0 .. 3].to!int % 111 == 0 || n[1 .. 4].to!int % 111 == 0) { writeln("Yes"); } else { writeln("No"); } } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } struct Queue(T) { private { int N, head, tail; T[] data; } this(int n) { N = n + 1; data = new T[](N); } bool empty() { return head == tail; } bool full() { return (tail + 1) % N == head; } T front() { return data[head]; } void push(T x) { assert(!full); data[tail++] = x; tail %= N; } void pop() { assert(!empty); head = (head + 1) % N; } void clear() { head = tail = 0; } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int n, m; scan(n, m); writeln(n == m ? "Yes" : "No"); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.string, std.conv; import std.typecons; import std.algorithm, std.array, std.range, std.container; import std.math; // mod must be a prime number struct Mod(uint mod) if (mod < int.max) { import std.conv : to; uint n; this(int m) { this(long(m)); } this(long m) { this.n = m >= 0L ? cast(uint)(m % mod) : cast(uint)(m % mod + mod); } this(ulong m) { this.n = cast(uint)(m % mod); } static auto normalize_(uint x) { return (x<=mod)? x : x-mod; } static auto make_(uint n_) { Mod!mod m; m.n = n_; return m; } auto opBinary(string op:"+")(Mod b) const { return make_(normalize_(n+b.n)); } auto opBinary(string op:"-")(Mod b) const { return make_(normalize_(n+mod-b.n)); } auto opBinary(string op:"*")(Mod b) const { return make_( ((n*b.n) % mod).to!uint ); } auto opBinary(string op:"/")(Mod b) const { return this*inv(b); } auto opBinary(string op:"^^")(Mod b) const { return make_( ( n ^^ ( b.n % (mod-1) ) % mod ).to!uint ); } auto opUnary(string op: "-")() const { return make_(normalize_( mod-this.n )); } auto opOpAssign(string op)(Mod b) if (op == "+" || op == "-" || op == "*" || op == "/" || op == "^^") { return mixin("this=this"~op~"b"); } static Mod inv(Mod x) { return x^^(mod-2); } auto opAssign(ulong b) { n = cast(uint)(b%mod); return this; } auto opAssign(long b) { return opAssign( b < 0 ? cast(ulong)(b%mod + mod) : cast(ulong)(b) ); } auto opAssign(int b) { return opAssign(long(b)); } auto opBinary(string op)(long b) const if (op == "+" || op == "-" || op == "*" || op == "/" || op == "^^") { auto m = Mod!mod(b); return mixin("this"~op~"m"); } auto opUnary(string op)(long b) const if (op == "-") { auto m = Mod!mod(b); return mixin(op~"m"); } auto opOpAssign(string op)(long b) if (op == "+" || op == "-" || op == "*" || op == "/" || op == "^^") { auto m = Mod!mod(b); return mixin("this"~op~"=m"); } string toString() const { return n.to!string; } } /+ void main() { //static immutable uint mod = 1_000_000_007; alias Int = Mod!1_000_000_007; auto data = readln.split; auto H = data[0].to!int, W = data[1].to!int, K = data[2].to!int; if (W == 1) { writeln("1"); return; } bool[][] all_pattern(uint n) { if (n == 1) return [[false], [true]]; auto next = all_pattern(n-1); auto result = next.map!"a~false".array; foreach (pat; next) { if (!pat[$-1]) result ~= pat ~ true; } return result; } auto pattern = all_pattern(W-1); Int[10][100] A; A[0][0] = Int(1); //pattern.writeln; foreach (h; 0 .. H) foreach (w; 0 .. W) { Int X, Y, Z; if (w == 0) { Y = pattern.count!((a,b) => a[0]==b)(false), Z = Int(pattern.length) - Y; //writeln("--here"); Y.writeln; Z.writeln; writeln("here--"); A[h+1][w] += Y * A[h][w]; A[h+1][w+1] += Z * A[h][w]; } else if (w == W-1) { Y = pattern.count!((a,b) => a[W-2]==b)(false), X = Int(pattern.length) - Y; A[h+1][w-1] += X * A[h][w]; A[h+1][w] += Y * A[h][w]; //writeln("--here"); X.writeln; Y.writeln; writeln("here--"); } else { X = pattern.count!((a,b) => a[w-1]==b)(true), Z = pattern.count!((a,b) => a[w]==b)(true), Y = Int(pattern.length)-X-Z; A[h+1][w-1] += X * A[h][w]; A[h+1][w] += Y * A[h][w]; A[h+1][w+1] += Z * A[h][w]; //writeln("--here"); X.writeln; Y.writeln; Z.writeln; writeln("here--"); } //writeln(A[h+1][w]); } A[H][K-1].writeln; } +/ void main() { static immutable uint mod = 1_000_000_007; auto data = readln.split; auto H = data[0].to!int, W = data[1].to!int, K = data[2].to!int; if (W == 1) { writeln("1"); return; } bool[][] all_pattern(uint n) { if (n == 1) return [[false], [true]]; auto next = all_pattern(n-1); auto result = next.map!"a~false".array; foreach (pat; next) { if (!pat[$-1]) result ~= pat ~ true; } return result; } auto patterns = all_pattern(W-1); ulong[10][101] A; A[0][0] = 1; //patterns.writeln; foreach (h; 0 .. H) foreach (w; 0 .. W) { ulong X, Y, Z; if (w == 0) { Z = patterns.count!(a => a[w]) % mod, Y = (patterns.length - Z) % mod; A[h+1][w] += Y * A[h][w]; A[h+1][w] %= mod; A[h+1][w+1] += Z * A[h][w]; A[h+1][w+1] %= mod; } else if (w == W-1) { X = patterns.count!(a => a[w-1]) % mod, Y = (patterns.length - X) % mod; A[h+1][w-1] += X * A[h][w]; A[h+1][w-1] %= mod; A[h+1][w] += Y * A[h][w]; A[h+1][w] %= mod; } else { X = patterns.count!(a => a[w-1]) % mod, Z = patterns.count!(a => a[w]), Y = (patterns.length-X-Z) % mod; A[h+1][w-1] += X * A[h][w]; A[h+1][w-1] %= mod; A[h+1][w] += Y * A[h][w]; A[h+1][w] %= mod; A[h+1][w+1] += Z * A[h][w]; A[h+1][w+1] %= mod; } /*foreach (row; patterns) { if (w == 0) { if (row[w]) { A[h+1][w+1] += A[h][w]; A[h+1][w+1] %= mod; } else { A[h+1][w] += A[h][w]; A[h+1][w] %= mod; } } else if (w == W-1) { if (row[w-1]) { A[h+1][w-1] += A[h][w]; A[h+1][w-1] %= mod; } else { A[h+1][w] += A[h][w]; A[h+1][w] %= mod; } } else { if (row[w-1]) { A[h+1][w-1] += A[h][w]; A[h+1][w-1] %= mod; } else if (row[w]) { A[h+1][w+1] += A[h][w]; A[h+1][w+1] %= mod; } else { A[h+1][w] += A[h][w]; A[h+1][w] %= mod; } } } */ } A[H][K-1].writeln; /*alias Int = Mod!1_000_000_007; Int[10][101] A; A[0][0] = 1; //patterns.writeln; foreach (h; 0 .. H) foreach (w; 0 .. W) { foreach (row; patterns) { if (w == 0) { if (row[0]) { A[h+1][w+1] += A[h][w]; } else { A[h+1][w] += A[h][w]; } } else if (w == W-1) { if (row[w-1]) { A[h+1][w-1] += A[h][w]; } else { A[h+1][w] += A[h][w]; } } else { if (row[w-1]) { A[h+1][w-1] += A[h][w]; } else if (row[w]) { A[h+1][w+1] += A[h][w]; } else { A[h+1][w] += A[h][w]; } } } } A[H][K-1].writeln; */ }
D
import std.algorithm, std.stdio, std.range; int N; immutable W = 10, M = 12; immutable dy = [-2,-2,-2,-1,-1,0,0,1,1,2,2,2], dx = [-1,0,1,-2,2,-2,2,-2,2,-1,0,1]; immutable ddy = [-1,-1,-1,0,0,0,1,1,1], ddx = [-1,0,1,-1,0,1,-1,0,1]; int[] ys, xs; void main(){ for(;init;){ writeln(solve ? "OK" : "NA"); } } bool init(){ ys = []; xs = []; int y, x; scanf("%d%d", &y, &x); ys ~= y; xs ~= x; if(y == 0 && x == 0){ return false; } scanf("%d", &N); foreach(_; 0..N){ scanf("%d%d", &y, &x); ys ~= y; xs ~= x; } return true; } bool solve(){ auto dp = new bool[][](N + 1, 9); dp[0][4] = true; foreach(i; 0..N){ foreach(j; 0..9)if(dp[i][j]){ foreach(k; 0..M){ int ny = ys[i] + ddy[j] + dy[k], nx = xs[i] + ddx[j] + dx[k]; if(0<=ny && ny<W && 0<=nx && nx<W)foreach(l; 0..9){ if(ys[i+1] + ddy[l] == ny && xs[i+1] + ddx[l] == nx){ dp[i+1][l] = true; } } } } } return count(dp[N], true) > 0; }
D
import std.stdio; import std.string; import std.algorithm; import std.conv; import std.array; import std.range; import std.math; int main(string[] argv) { auto gunVeSekerCifti = stdin.readln.chomp.split.map!( to!int ); int gunSayisi = gunVeSekerCifti[0]; size_t sekerTalebi = gunVeSekerCifti[1]; int[] sekerKutulariDizisi = stdin.readln.chomp.split.map!( to!int ).array(); int toplamSekerSayisi = reduce!((a,b) => a + b)(0, sekerKutulariDizisi); int maksimumSekerVerebilmeLimiti = gunSayisi * 8; if (sekerTalebi > maksimumSekerVerebilmeLimiti || sekerTalebi > toplamSekerSayisi ) { writeln(-1); return 0; } size_t toplam = 0; for ( int i = 0; i < gunSayisi; i++ ) { size_t deger = sekerKutulariDizisi[i]; //deger > 8 ? toplam += 8 : toplam += deger ; size_t geciciDeger = deger; if ( deger > 8 ) geciciDeger = 8; toplam += geciciDeger ; if ( toplam >= sekerTalebi ) { writeln( i + 1 ); return 0; } else { if ( deger > 8 ) sekerKutulariDizisi[i + 1] += deger - 8; } } writeln(-1); return 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; enum mod = 1_000_000_007L; void main() { string s; scan(s); int n = s.length.to!int; int ans; foreach (i ; 0 .. n / 2) { if (s[i] != s[n - 1 - i]) ans++; } writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.algorithm; import std.array; import std.conv; import std.range; import std.stdio; import std.string; void main() { auto nums = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]; int n; scanf("%d", &n); int len = n / 2; while (len > 0) { foreach_reverse (i; 0..10) if ((len - 1) * 2 <= n - nums[i]) { write(i); n -= nums[i]; break; } len = len - 1; } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 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 modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto t = RD!int; auto ans = new int[](t); foreach (ti; 0..t) { auto n = RD!int; auto m = RD!int; auto a = RDA!int; auto s = a.sum; ans[ti] = min(s, m); } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; void main(){ string s = readln.chomp; long ans; for(long x = 0; x < 2 ^^ (s.length - 1); x ++){ long a = 0, tmp = ("" ~ s[0]).to!long; for(int i = 0; i < s.length - 1; i ++){ if(x & 2 ^^ i){ tmp *= 10, tmp += ("" ~ s[i + 1]).to!long; } else{ a += tmp, tmp = ("" ~ s[i + 1]).to!long; } } if(tmp > 0) a += tmp, tmp = 0; ans += a; } ans.writeln; }
D
import core.stdc.stdio; import std.stdio; void main(){ int n; scanf("%d",&n); char[] buf = new char[n+1]; scanf("%s",buf.ptr); buf = buf[0..n]; bool[] used = new bool[n]; int[] op = new int[n]; bool Solve(char[] data,int c){ used[] = false; int ic=n; int a; for(int i=n-1;i>=0;i--) if(a<c&&data[i]=='O') while(--ic>i) if(!used[ic]&&data[ic]=='I'){ used[ic]=true; used[i]=true; a++; break; } if(a!=c) return false; int os=0; foreach(int i,u;used) if(u&&data[i]=='O') op[os++]=i; int oi=-1; foreach(int i,u;used) if(!u&&data[i]!='O') while(++oi<os) if(op[oi]>i){ a--; break; } return a==0; } int l,r=n+1; while(r-l>1){ int m=(r+l)/2; if(Solve(buf,m)) l=m; else r=m; } printf("%d\n",l); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; string s; rd(s); if(s.length<4){writeln("No"); return;} auto t="YAKI"; foreach(i; 0..t.length){ if(s[i]!=t[i]){writeln("No"); return;} } writeln("Yes"); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.stdio, std.conv, std.array, std.string; import std.algorithm; import std.container; import std.range; import core.stdc.stdlib; import std.math; void main() { auto N = readln.chomp.to!int / 2; auto S = readln.chomp; auto T = S[0..N]; writeln(T ~ T == S ? "Yes" : "No"); }
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!int; int an = s; int[int] cnt; foreach (i; 2..1_000_001) { cnt[an]++; int am; if (an % 2) { am = 3 * an + 1; } else { am = an / 2; } if (cnt.get(am, 0)) { writeln(i); break; } else { an = am; } } }
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) { auto w = line.chomp.to!double; string str; if (w <= 48) str = "light fly"; else if (w <= 51) str = "fly"; else if (w <= 54) str = "bantam"; else if (w <= 57) str = "feather"; else if (w <= 60) str = "light"; else if (w <= 64) str = "light welter"; else if (w <= 69) str = "welter"; else if (w <= 75) str = "light middle"; else if (w <= 81) str = "middle"; else if (w <= 91) str = "light heavy"; else str = "heavy"; str.writeln; } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.array; void main(){ auto N=readln.chomp.to!int; auto K=readln.chomp.to!int; auto X=readln.chomp.to!int; auto Y=readln.chomp.to!int; if(N>K){ writeln(X*K+(N-K)*Y); } else writeln(N*X); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto hw = readln.split.to!(int[]); auto H = hw[0]; auto W = hw[1]; int[][] ps; auto memo = new bool[][](H, W); foreach (i; 0..H) { foreach (j, c; readln.chomp) { if (c == '#') { ps ~= [j.to!int, i.to!int]; memo[i][j] = true; } } } int c = -1; while (!ps.empty) { int[][] pps; foreach (p; ps) { foreach (d; [[1,0], [0,1], [-1,0], [0,-1]]) { auto x = p[0]+d[0]; auto y = p[1]+d[1]; if (x < 0 || x >= W || y < 0 || y >= H || memo[y][x]) continue; memo[y][x] = true; pps ~= [x, y]; } } ps = pps; ++c; } writeln(c); }
D
import std.stdio; import std.string; import std.conv; void main() { auto N =readln.chomp.to!int; auto A =readln.chomp.to!int; writeln(N*N-A); }
D
import std.stdio, std.conv, std.algorithm, std.range, std.array, std.string, std.uni, std.bigint, std.math; void main() { auto n = readln.chomp.to!uint; auto an = readln.split.to!(long[]); min(func(an, 0), func(an, 1)).writeln; } ulong func(long[] an, uint mod) { long sum = 0; ulong res = 0; foreach(i,a; an) { sum += a; if (i % 2 == mod && sum <= 0) { res += 1 - sum; sum = 1; } if (i % 2 != mod && sum >= 0) { res += 1 + sum; sum = -1; } } return res; }
D
import std.stdio; import std.array; import std.conv; void main() { while (1) { auto input = to!(int[])(split(readln())); int total = input[0] + input[1]; if (input[0] == -1 && input[1] == -1 && input[2] == -1) break; if (input[0] == -1 || input[1] == -1) { writeln("F"); } else if (total >= 80) { writeln("A"); } else if (total >= 65) { writeln("B"); } else if (total >= 50) { writeln("C"); } else if (total >= 30) { if (input[2] >= 50) writeln("C"); else writeln("D"); } else { writeln("F"); } } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; void main() { int n; long x; scan(n, x); auto a = new long[](n + 1); // 長さ auto b = new long[](n + 1); // Pの枚数 a[0] = b[0] = 1; foreach (i ; 1 .. n + 1) { a[i] = 2L*a[i-1] + 3; b[i] = 2L*b[i-1] + 1; } long query(int L, long x) { if (L == 0) { return x == 1; } if (x <= 1) { return 0; } else if (x < (a[L] + 1) / 2) { return query(L-1, x - 1); } else if (x == (a[L] + 1) / 2) { return b[L-1] + 1; } else if (x < a[L] - 1) { return b[L-1] + 1 + query(L-1, x - (a[L] + 1) / 2); } else { return 2*b[L-1] + 1; } } long ans = query(n, x); writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void main() { auto S = readln.chomp; if (S == "RRR") { writeln(3); } else if (S == "SRR" || S == "RRS") { writeln(2); } else if (S == "SSS") { writeln(0); } else { writeln(1); } }
D
import std.algorithm; import std.array; import std.ascii; import std.bigint; import std.complex; import std.container; import std.conv; import std.functional; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } void readlnTo(T...)(ref T t) { auto s = readln().split(); assert(s.length == t.length); foreach(ref ti; t) { ti = s[0].to!(typeof(ti)); s = s[1..$]; } } const real eps = 1e-10; void main(){ int h, w; readlnTo(h, w); string[] a; foreach(i; iota(h)) { a ~= readln().chomp(); } int c; foreach(i; iota(h)) { foreach(j; iota(w)) { if(a[i][j] == '#') { ++c; } } } writeln(c == h+w-1 ? "Possible": "Impossible"); }
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; const MOD = 10^^9 + 7; long[][] pascal(int n, int mod) { auto g = new long[][](n + 1, n + 1); g[0][0] = 1; for (int i = 1; i < n; i++) { for (int j = 0; j <= i; j++) { g[i][j] = g[i-1][j]; if (j > 0) { g[i][j] += g[i-1][j-1]; g[i][j] %= mod; } } } return g; } void calc(int n, int k) { auto C = pascal(n + n, MOD); for (int i = 1; i <= k; i++) { // N - K 個の赤ボールのすき間から i 箇所を選ぶ long x = C[n - k + 1][i]; // K 個の青ボールを i 箇所に配る配り方 // H(i, K - i) = nck(i + K - i - 1, K - i) long y = C[k - 1][k - i]; writeln(x * y % MOD); } } void main() { int n, k; scan(n, k); 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() { long a, b; scan(a, b); auto ans = xorsum(b) ^ xorsum(a - 1); writeln(ans); } long xorsum(long n) { if (n <= 0) { return 0; } if (n % 4 == 0) { return n; } else if (n % 4 == 1) { return 1; } else if (n % 4 == 2) { return n + 1; } else { return 0; } } 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
#!/usr/bin/rdmd import std.stdio; void main() { foreach(i ; 1..10) foreach(j ; 1..10) writeln(i, "x", j, "=", i*j); }
D
import std.stdio, std.conv, std.array, std.string; import std.algorithm; import std.container; import std.range; import core.stdc.stdlib; import std.math; void main() { auto N = readln.chomp.to!int; auto Bn = readln.split.to!(int[]); int total = Bn[0] + Bn[N-2]; foreach(i; 0..N-2) { total += min(Bn[i], Bn[i+1]); } writeln(total); }
D
void main(){ int a, b; char op; scanf("%d %c %d", &a, &op,&b); writeln(op=='+'?a+b:a-b); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range; const long mod = 10^^9+7; // 1要素のみの入力 T inelm(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] inln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split())ln ~= elm.to!T(); return ln; }
D
void main() { int n = readln.chomp.to!int; string s = readln.chomp; int x, xmax; foreach (c; s) { if (c == 'I') ++x; else --x; xmax = max(xmax, x); } xmax.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; 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!long; long res; foreach (i; 1..n+1) { auto x = (i.to!long) ^^ 2; if (x > n) break; res = x; } res.writeln; }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int r, g, b, n; scan(r, g, b, n); int ans; foreach (i ; 0 .. 3001) { foreach (j ; 0 .. 3001) { int rem = n - i * r - j * g; if (rem < 0) continue; if (rem % b) continue; ans++; } } writeln(ans); } int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new int[][](n, 0); foreach (i; 0 .. m) { int u, v; scan(u, v); if (is1indexed) { u--, v--; } adj[u] ~= v; if (isUndirected) { adj[v] ~= u; } } return adj; } alias Edge = Tuple!(int, "to", int, "cost"); Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new Edge[][](n, 0); foreach (i; 0 .. m) { int u, v, c; scan(u, v, c); if (is1indexed) { u--, v--; } adj[u] ~= Edge(v, c); if (isUndirected) { adj[v] ~= Edge(u, c); } } return adj; } void yes(bool b) { writeln(b ? "Yes" : "No"); } void YES(bool b) { writeln(b ? "YES" : "NO"); } T[] readArr(T)() { return readln.split.to!(T[]); } T[] readArrByLines(T)(int n) { return iota(n).map!(i => readln.chomp.to!T).array; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.stdio, std.string, std.conv, std.ascii, std.array; void main() { foreach(_; 0 .. readln.chomp.to!int) { line = readln.chomp; add.writeln; } } string line; int add() { int lhs = mul; loop: while(true) { switch(line[0]) { case '+': line.popFront; lhs += mul; break; case '-': line.popFront; lhs -= mul; break; default: break loop; } } return lhs; } int mul() { int lhs = prim; loop: while(true) { switch(line[0]) { case '*': line.popFront; lhs *= prim; break; case '/': line.popFront; lhs /= prim; break; default: break loop; } } return lhs; } int prim() { int r; if(line[0] == '(') { line.popFront; r = add; line.popFront; } else if(line[0] == '-') { line.popFront; r = -mul; } else while(line[0] != '=') { if(line[0].isDigit) { r = r * 10 + (line[0] - '0'); line.popFront; } else break; } return r; }
D
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } void main() { auto N = readln.chomp.to!(int); auto P = readln.split.to!(int[]); void solve() { int answer; int min = 999999; foreach(i; 0..N) { min = min < P[i] ? min : P[i]; if (min >= P[i]) answer++; } answer.writeln(); } solve(); }
D
// xxx import std.algorithm; import std.random; import std.stdio; import std.string; void main () { auto normal = ["no", "not bad", "cool", "don't think so", "great!", "don't touch me!"]; auto grumpy = ["no", "don't even", "worse", "no way", "terrible", "go die in a hole", "are you serious?"]; rndGen.seed (unpredictableSeed); while (true) { auto i = uniform (0, 10); writeln (i); stdout.flush (); auto s = readln.strip; if (normal.canFind (s) && !grumpy.canFind (s)) { writeln ("normal"); break; } if (grumpy.canFind (s) && !normal.canFind (s)) { writeln ("grumpy"); break; } } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto m = RD!int; auto a = new int[][](min(n, m)); foreach (i; 0..n) { if (n <= m) a[i] = RDA!int; else { auto tmp = RDA!int; foreach (j; 0..m) { a[j] ~= tmp[j]; } } } if (m < n) swap(n, m); debug writeln(a); foreach (i; 0..(n+m-1)/2) { int cnt, loop; foreach (j; 0..min(i+1, n)) { cnt += a[j][i-j]; cnt += a[n-j-1][m-i-1+j]; ++loop; } loop *= 2; ans[ti] += min(cnt, loop-cnt); debug writeln("i:", i, " cnt:", cnt, " loop:", loop, " ans[ti]:", ans[ti]); } } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string; 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 P = readln.split.map!(to!long).array; auto B = (N-1).iota.map!(i => A[i+1] - A[i]).array; auto G = B.reduce!((a, b) => gcd(a, b)); foreach (i; 0..M) { if (G % P[i] == 0) { writeln("YES"); writeln(A.front, " ", i + 1); return; } } writeln("NO"); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto a = RD; auto b = RD; auto c = RD; auto tot = a + b*2 + c*3; auto half = tot / 2; debug writeln(half); half -= min(c, half / 3) * 3; debug writeln(half); half -= min(b, half / 2) * 2; debug writeln(half); half -= min(a, half); debug writeln(half); ans[ti] = half * 2 + tot % 2; } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio; void main(string[] args) { int n; int[1440] time; scanf("%d", &n); for ( int i = 0; i < 1440; i++ ) { time[ i ] = 0; } int h, m; for ( int i = 0; i < n; i++ ) { scanf("%d:%d", &h, &m); time[h*60+m] = 1; } int mm=0; int cur=0; int cur1=0; int t = 0; for ( int i = 0; i < 1440; i++){ if (time[i] == 0){ cur = cur + 1; } else { if (t == 0){ cur1 = cur; cur = 0; t = 1; } if (cur > mm){ mm = cur; } cur = 0; } } if (cur+cur1 > mm){ mm = cur+cur1; } writeln(mm/60/10,mm/60%10,":",mm%60/10,mm%60%10); }
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; writeln (a.minElement == a.maxElement ? n : 1); } }
D
// import chie template :) {{{ 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()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } // }}} void main() { auto cin = new Scanner; writeln(iota(1, cin.next!long() + 1).filter!"a % 3 != 0 && a % 5 != 0".sum); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nmk = readln.split.to!(int[]); auto N = nmk[0]; auto M = nmk[1]; auto K = nmk[2]; foreach (y; 0..N+1) { foreach (x; 0..M+1) { if (x*y + (M-x)*(N-y) == K) { writeln("Yes"); return; } } } writeln("No"); return; }
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 x = readln.chomp.replace(" ", "").to!int; auto y = sqrt(x.to!double); if ( y - y.to!int > 0) { writeln("No"); } else { writeln("Yes"); } }
D
void main() { long n = readln.chomp.to!long; long x = 1, y = 1; foreach (i; 0 .. n) { long[] tmp = readln.split.to!(long[]); long t = tmp[0], a = tmp[1]; long m = max((x+t-1)/t, (y+a-1)/a); x = m * t, y = m * a; } writeln(x + y); } 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; void main() { auto s = readln.chomp; auto cnt = 0; char last = s[0]; foreach (c; s[1..$]) { if (c != last) { ++cnt; last = c; } } writeln(cnt); }
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.container; import std.random; void main() { int i; while (1) { auto n = readln.chomp.to!int; if (!n) break; writeln("Case ", ++i, ": ", 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; const mod = 10^^9 + 7; long calc(string s) { auto dp = new long[][](s.length + 1, 4); dp[0][0] = 1; for (int i = 1; i <= s.length; i++) { for (int j = 0; j <= 3; j++) { dp[i][j] = (dp[i-1][j] * (s[i-1] == '?' ? 3 : 1)) % mod; } auto c = s[i-1]; if (c == 'A' || c == '?') dp[i][1] = (dp[i][1] + dp[i-1][0]) % mod; if (c == 'B' || c == '?') dp[i][2] = (dp[i][2] + dp[i-1][1]) % mod; if (c == 'C' || c == '?') dp[i][3] = (dp[i][3] + dp[i-1][2]) % mod; } return dp[s.length][3]; } void main() { string s = read!string; writeln(calc(s)); }
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; void main() { int r; scan(r); writeln(3 * r * r); }
D
import std.algorithm; import std.array; import std.ascii; import std.bigint; import std.complex; import std.container; import std.conv; import std.functional; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } void readlnTo(T...)(ref T t) { auto s = readln().split(); assert(s.length == t.length); foreach(ref ti; t) { ti = s[0].to!(typeof(ti)); s = s[1..$]; } } const real eps = 1e-10; const long p = 1_000_000_000 + 7; void main(){ auto n = readLong(); auto a = readLongs(); auto count = new long[](200000); foreach(ai; a) { count[ai]++; } long ans = 0; foreach(i; iota(1, 150000)) { ans = max(ans, count[i-1] + count[i] + count[i+1]); } writeln(ans); }
D
void main() { int n = readln.chomp.to!int; writeln(n / 1000 ? "ABD" : "ABC"); } 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.uni;
D
import std.stdio, std.math, std.algorithm, std.array, std.string, std.conv, std.container, std.range; pragma(inline, true) T[] Reads(T)() { return readln.split.to!(T[]); } alias reads = Reads!int; pragma(inline, true) void scan(Args...)(ref Args args) { string[] ss = readln.split; foreach (i, ref arg ; args) arg = ss[i].parse!int; } void main() { string s = readln.chomp; bool can = true; if (s=="AAA" || s=="BBB") can = false; writeln(can ? "Yes" : "No"); }
D
import std.stdio, std.conv, std.string, std.algorithm, std.math, std.array, std.container; void main() { long[] hw = readln.chomp.split.to!(long[]); if(hw[0]==1 || hw[1]==1) writeln(1); else writeln((hw[0]*hw[1]+1)/2); }
D
import std.stdio; import std.algorithm; import std.conv; import std.string; void main() { auto n = to!int(chomp(readln())); string[] ws; ws ~= chomp(readln()); bool isYes = true; foreach (i; 1..n) { ws ~= chomp(readln()); if (ws[i-1][$-1] != ws[i][0]) { isYes = false; break; } else if (countUntil(ws[0..$-1], ws[$-1]) != -1) { isYes = false; break; } } if (isYes) writeln("Yes"); else writeln("No"); stdout.flush(); }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); void main() { auto n = lread(); auto t = aryread(); // writeln(t); auto m = lread(); auto px = new long[][](m, 2); foreach (i; 0 .. m) { px[i] = aryread(); } // writeln(px); foreach (i; 0 .. m) { writeln(px[i][1] + (sum(t[0 .. (px[i][0] - 1)])) + (sum(t[px[i][0] .. $]))); } } 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.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; auto cnt = new int[](26); void solve() { auto s = readln.split.map!(to!int); auto N = s[0]; auto K = s[1]; auto S = readln.chomp; int ans = 0; foreach (i; 0..K/2+K%2) { cnt[] = 0; if (K % 2 && i == K / 2) { foreach (j; 0..N/K) { int a = j * K + i; cnt[S[a]-'a'] += 1; ans += 1; } } else { foreach (j; 0..N/K) { int a = j * K + i; int b = (j + 1) * K - i - 1; cnt[S[a]-'a'] += 1; cnt[S[b]-'a'] += 1; ans += 2; } } ans -= cnt.reduce!max; } ans.writeln; } void main() { auto T = readln.chomp.to!int; while (T--) { solve(); } }
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 a = readln.chomp.split.to!(int[]); writeln((a[0]*3+a[1])/2); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; long P = 10^^9 + 7; void main() { auto N = readln.chomp.to!int; auto S = readln.chomp.to!(char[]); long[26] as; foreach (c; S) { if (as[c-97] == 0) { as[c-97] = 2; } else { ++as[c-97]; } } long r = 1; foreach (a; as) if (a) { r = (r * a) % P; } writeln(r-1); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto nmxy = readln.chomp.split.to!(int[]); auto x = max(readln.chomp.split.to!(int[]).reduce!(max), nmxy[2]); auto y = min(readln.chomp.split.to!(int[]).reduce!(min), nmxy[3]); if (x < y) { writeln("No War"); } else { writeln("War"); } }
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 readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n; readV(n); int[] a; readA(n, a); writeln(a.reduce!max - a.reduce!min); }
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 main() { int n, k; scan(n, k); auto a = readln.split.to!(int[]); foreach (i; k .. n) { yes(a[i] > a[i - k]); } } void scan(T...)(ref T args) { auto line = readln.split; // @suppress(dscanner.suspicious.unmodified) 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.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 xs = readints; int a = xs[0], b = xs[1]; int ans = max(a + b, a - b, a * b); writeln(ans); }
D
import std.stdio, std.string, std.array, std.conv; int selectionSort(int n, int[] x) { int cnt = 0; foreach (i; 0 .. n-1) { int minj = i; foreach (j; i .. n) { if (x[j] < x[minj]) minj = j; } if (i != minj) { int t = x[i]; x[i] = x[minj]; x[minj] = t; if (i != minj) ++cnt; } } return cnt; } void main() { int n = readln.chomp.to!int; int[] a = readln.chomp.split.to!(int[]); int times = selectionSort(n, a); foreach (i, x; a) { x.write; if (i != n - 1) " ".write; else writeln; } times.writeln; }
D
import std.array; import std.range; import std.algorithm; import std.conv; import std.stdio; import std.string; import std.traits; import std.ascii; class Solver { private int[char] hash; private bool firstUpdate = true; public this() { foreach(c; lowercase) { hash[c] = 0; } } public void updateState(string str) { if(firstUpdate) { foreach(c; str) { hash[c]++; } } else { int[char] tempHash; foreach(c; lowercase) { tempHash[c] = 0; } foreach(c; str) { tempHash[c]++; } foreach(c; lowercase) { hash[c] = min(tempHash[c], hash[c]); } } firstUpdate = false; } public string ans() { string ans; foreach(c; lowercase) { ans ~= c.repeat().take(hash[c]).array; } return ans; } } void main(string[] args) { auto a = new Solver(); int n = readln.chomp.to!int; foreach(i; iota(n)) { a.updateState(readln.chomp); } a.ans.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 s = sread(); long[dchar] count; foreach (e; s) { count[e]++; } long remove; if ('0' in count && '1' in count) remove = min(count['0'], count['1']); writeln(remove * 2); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; int readint() { return readln.chomp.to!int; } int[] readints() { return readln.split.map!(to!int).array; } bool isParallel(Vec2 a, Vec2 b, Vec2 c, Vec2 d) { auto ab = b - a; auto cd = d - c; return ab.cross(cd) == 0; } bool isOrthogonal(Vec2 a, Vec2 b, Vec2 c, Vec2 d) { auto ab = b - a; auto cd = d - c; return ab.dot(cd) == 0; } int calc(Vec2 a, Vec2 b, Vec2 c, Vec2 d) { if (isParallel(a, b, c, d)) return 2; if (isOrthogonal(a, b, c, d)) return 1; return 0; } void main() { int n = readint(); for (int i = 0; i < n; i++) { auto ps = readints(); auto a = Vec2(ps[0], ps[1]); auto b = Vec2(ps[2], ps[3]); auto c = Vec2(ps[4], ps[5]); auto d = Vec2(ps[6], ps[7]); writeln(calc(a, b, c, d)); } } struct Vec2 { immutable double x; immutable double y; this(double x, double y) { this.x = x; this.y = y; } Vec2 opNeg() { return Vec2(-this.x, -this.y); } Vec2 opAdd(Vec2 other) { return Vec2(this.x + other.x, this.y + other.y); } Vec2 opSub(Vec2 other) { return Vec2(this.x - other.x, this.y - other.y); } Vec2 opMul(double d) { return Vec2(this.x * d, this.y * d); } double dot(Vec2 other) { return this.x * other.x + this.y * other.y; } double cross(Vec2 other) { return this.x * other.y - other.x * this.y; } double mag() { return sqrt(magSq()); } double magSq() { return this.x * this.x + this.y * this.y; } Vec2 normalized() { auto m = mag(); if (m != 0 && m != 1) return Vec2(this.x / m, this.y / m); return this; } static double distance(Vec2 a, Vec2 b) { return (a - b).mag(); } }
D
import std.stdio; import std.string; import std.conv; void main(){ int[string] diction; int max; string longest=""; foreach( c ; readln().chomp().split() ){ diction[c]++; if( max < diction[c] ){ max = diction[c]; } if( longest.length < c.length ){ longest = c; } } foreach( c,i ; diction ){ if( max == diction[c] ){ write( c," " ); } } writeln( longest ); }
D
import std.stdio; import std.algorithm; import std.math; import std.conv; import std.string; T readNum(T)(){ return readStr.to!T; } T[] readNums(T)(){ return readStr.split.to!(T[]); } string readStr(){ return readln.chomp; } void main(){ auto md = readNums!int; int ret; foreach(m; 1 .. md[0]+1){ foreach(d; 1 .. md[1]+1){ if(d % 10 >= 2 && d / 10 >= 2 && (d % 10) * (d / 10) == m){ ret++; } } } writeln(ret); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto a = RD; auto b = RD; auto n = RD; auto m = RD; if (a+b < n+m) continue; if (min(a, b) >= m) ans[ti] = true; } foreach (e; ans) writeln(e ? "Yes" : "No"); stdout.flush; debug readln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void main() { auto N = readln.chomp.to!int; auto ss = new int[][](N, N); auto memo = new bool[][](N, N); foreach (i; 0..N) foreach (j; 0..N) { ss[i][j] = min(i, N-i-1, j, N-j-1); memo[i][j] = true; } void recalc(int i, int j) { auto d = ss[i][j] + (memo[i][j] ? 1 : 0); static foreach (s; [[1,0], [0,1], [-1,0], [0,-1]]) {{ auto ii = i+s[0]; auto jj = j+s[1]; if (!(ii < 0 || ii >= N || jj < 0 || jj >= N) && ss[ii][jj] > d) { ss[ii][jj] = d; recalc(ii, jj); } }} } int r; foreach (p; readln.split.to!(int[])) { auto i = (p-1)/N; auto j = (p-1)%N; r += ss[i][j]; memo[i][j] = false; recalc(i, j); } writeln(r); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N, M, L; scan(N, M, L); auto G1 = new long[][](N, N); foreach (i; 0 .. N) G1[i][] = long.max >> 2; foreach (i; 0 .. N) G1[i][i] = 0; foreach (_; 0 .. M) { long a, b, c; scan(a, b, c); a--, b--; G1[a][b] = G1[b][a] = c; } foreach (k; 0 .. N) foreach (i; 0 .. N) foreach (j; 0 .. N) if (G1[i][k] + G1[k][j] < G1[i][j]) G1[i][j] = G1[i][k] + G1[k][j]; auto G2 = new long[][](N, N); foreach (i; 0 .. N) G2[i][] = long.max >> 2; foreach (i; 0 .. N) G2[i][i] = 0; foreach (i; 0 .. N) foreach (j; 0 .. N) if (G1[i][j] <= L) { G2[i][j] = min(G2[i][j], 1); } foreach (k; 0 .. N) foreach (i; 0 .. N) foreach (j; 0 .. N) if (G2[i][k] + G2[k][j] < G2[i][j]) G2[i][j] = G2[i][k] + G2[k][j]; long Q = lread(); foreach (_; 0 .. Q) { long s, t; scan(s, t); s--, t--; writeln(G2[s][t] == long.max >> 2 ? -1 : G2[s][t] - 1); } }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto c = readln.chomp; writeln("aeiou".canFind(c) ? "vowel" : "consonant"); }
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)); } void main() { try { for (; ; ) { const numCases = readInt(); foreach (_; 0 .. numCases) { const N = readInt(); auto A = new long[N]; foreach (i; 0 .. N) { A[i] = readLong(); } bool ans = true; foreach (i; 0 .. N) { bool ok; for (long x = 2; x <= 2 + i && x <= 30; ++x) { ok = ok || (A[i] % x != 0); } ans = ans && ok; } writeln(ans ? "YES" : "NO"); } } } catch (EOFException e) { } }
D
import std.stdio,std.conv,std.algorithm,std.string;alias L=long;L M=998244353;L[]X;L[L]C;L h(L m,L[]U){L f;foreach(u;U[0..m])f=f*2%M+u;foreach(i,u;U)if(i/m&1^U[i%m]^u)return f+u;return f+1;}void main(){L n=readln.chomp.to!L,b=n*2;foreach(c;readln.chomp)X~=c-'0';for(L d=n|1;d>2;d-=2)if(n%d<1){L c=h(n/d,X);foreach(e,q;C)e%d||(c+=M-q);C[d]=c%M;}n=h(n,X)*b;foreach(d,c;C)n+=M-c*(b-b/d)%M;writeln(n%M);}
D
import std.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; bool calc(string[] a, string[] b) { int n = cast(int) a.length; int m = cast(int) b.length; for (int i = 0; i < n; i++) { if (i + m > n) break; for (int j = 0; j < n; j++) { if (j + m > n) break; bool ok = true; for (int r = 0; r < m; r++) { for (int c = 0; c < m; c++) { if (a[i + r][j + c] != b[r][c]) { ok = false; } } } if (ok) return true; } } return false; } void main() { auto nm = readints; int n = nm[0], m = nm[1]; string[] a; for (int i = 0; i < n; i++) { a ~= read!string; } string[] b; for (int i = 0; i < m; i++) { b ~= read!string; } writeln(calc(a, b) ? "Yes" : "No"); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto n = RD!int; auto m = RD!int; auto sx = RD!int; auto sy = RD!int; int[][] ans; foreach_reverse (y; 1..sy+1) { ans ~= [sx, y]; } foreach (y; sy+1..m+1) { ans ~= [sx, y]; } auto x = sx-1; auto y = m; ans ~= [x, y]; int dir = -1; while (true) { //debug writeln(x, " ", y); y += dir; if (y == 0 || y == m+1) { dir = -dir; y += dir; --x; if (x == 0) break; } ans ~= [x, y]; } x = sx+1; ans ~= [x, y]; while (true) { //debug writeln(x, " ", y); y += dir; if (y == 0 || y == m+1) { dir = -dir; y += dir; ++x; if (x == n+1) break; } ans ~= [x, y]; } foreach (e; ans) { writeln(e[0], " ", e[1]); } stdout.flush; debug readln; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto rd = readln.split.to!(int[]), n = rd[0], a = rd[1]; auto x = readln.split.to!(int[]), xs = x.sum; auto dp = new long[][][](n+1, n+1, xs+1); dp[0][0][0] = 1; foreach (i; 0..n) foreach (j; 0..n+1) foreach (k; 0..xs+1) { dp[i+1][j][k] = dp[i][j][k]; if (j >= 1 && k >= x[i]) dp[i+1][j][k] += dp[i][j-1][k-x[i]]; } auto ans = 0L; foreach (i; 1..n+1) { if (i*a > xs) break; ans += dp[n][i][i*a]; } writeln(ans); }
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; int calc(int k, string s) { // 0, 1 でグループ化して個数を記録する int[] g; // 先頭要素が 0 なら、先頭に 0 個の 1 があると見なす if (s.front == '0') g ~= 0; int lo = 0; while (lo < s.length) { int hi = lo; while (hi < s.length && s[lo] == s[hi]) hi++; g ~= hi - lo; lo = hi; } // 累積和 auto sum = new int[g.length]; sum[0] = g[0]; for (int i = 1; i < g.length; i++) { sum[i] = g[i] + sum[i - 1]; } int ans = 0; // 0, 1, 0, 1, ... のパターンで並んでいるので // 2 個単位で k 個まで取り出す(2 * k + 1 個とる) for (int i = 0; i < sum.length; i += 2) { int hi = min(i + 2 * k, cast(int)sum.length - 1); int a = sum[hi] - (i-1 >= 0 ? sum[i-1] : 0); ans = max(ans, a); } return ans; } void main() { auto nk = readints; int k = nk[1]; string s = read!string; writeln(calc(k, s)); }
D
void main() { int n = readln.chomp.to!int; string s = readln.chomp; int cnt = s[1..$].count('E').to!int; int result = cnt; foreach (i; 1 .. n) { if (s[i-1] == 'W') ++cnt; if (s[i] == 'E') --cnt; result = min(result, cnt); } result.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, std.container, std.range; long floorSum(long n, long m, long a, long b) { long ans; if (m <= a) { ans += (n - 1) * n * (a / m) / 2; a %= m; } if (m <= b) { ans += n * (b / m); b %= m; } long y_max = (a * n + b) / m, x_max = (y_max * m - b); if (y_max == 0) return ans; ans += (n - (x_max + a - 1) / a) * y_max; ans += floorSum(y_max, a, m, (a - x_max % a) % a); return ans; } void main() { auto T = readln.chomp.to!int; while (T--) { auto nmab = readln.split.to!(long[]); writeln(floorSum(nmab[0], nmab[1], nmab[2], nmab[3])); } }
D
void main() { long k = rdElem; long total; foreach (i; 1 .. k+1) { foreach (j; 1 .. k+1) { long g = gcd(i, j); foreach (l; 1 .. k+1) { total += gcd(g, l); } } } total.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.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, 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 LIM = 10^^5; void main() { try { for (; ; ) { const N = readLong(); const P = readLong(); int ans = LIM; foreach (k; 0 .. LIM) { const m = N - k * P; if (m >= 0) { if (popcnt(m) <= k && k <= m) { chmin(ans, k); } } } writeln((ans >= LIM) ? -1 : ans); } } catch (EOFException 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; 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(int n) { for (int i = 0; i * 4 <= n; i++) { for (int j = 0; j * 7 <= n; j++) { if (n == i * 4 + j * 7) { return true; } } } return false; } void main() { int n = readint; writeln(calc(n) ? "Yes" : "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; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(string s) { int n = cast(int)s.length; auto es = new int[n]; int e = 0; for (int i = 0; i < n; i++) { if (s[i] == 'E') e++; es[i] = e; } // p 番目までの W の累積和 int ws(int p) { return p + 1 - es[p]; } int ans = int.max; for (int i = 0; i < n; i++) { // i がリーダー // リーダーの左側で W を向いている人数 int leftWs = i > 0 ? ws(i - 1) : 0; // リーダーの右側で E を向いている人数 int rightEs = i == n-1 ? 0 : es[n - 1] - es[i]; ans = min(ans, leftWs + rightEs); } return ans; } void main() { readint; auto s = readln.chomp; auto ans = calc(s); writeln(ans); }
D
import std.stdio: writeln, readln; import std.array: split; void main() { string[] s = readln.split; if (s[0][$-1] == s[1][0] && s[1][$-1] == s[2][0]) writeln("YES"); else writeln("NO"); }
D
import std.stdio; import std.string; import std.conv; void main() { auto n = to!int(readln.chomp!()); writeln(n / 3); // to!(int[])(readln.split(",")).writeln; }
D
void main() { auto N = ri; auto A = rs, B = rs, C = rs; ulong cnt; foreach(i; 0..N) { if(A[i] == B[i] && B[i] == C[i]) continue; if(A[i] == B[i] || B[i] == C[i] || C[i] == A[i]) cnt++; else cnt+=2; } cnt.writeln; } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.conv, std.string, std.array, std.algorithm, std.math; void main() { auto nm = readln.chomp.split(" ").map!(to!int); auto n = nm[0]; auto m = nm[1]; auto stus = new int[2][n]; foreach (i; 0..n) stus[i] = readln.chomp.split(" ").map!(to!int).array; auto cps = new int[2][m]; foreach (i; 0..m) cps[i] = readln.chomp.split(" ").map!(to!int).array; auto rets = new size_t[n]; foreach (i, stu; stus) { auto min_v = int.max; size_t ret; foreach (j, cp; cps) { auto d = abs(stu[0] - cp[0]) + abs(stu[1] - cp[1]); if (d < min_v) { min_v = d; ret = j; } } rets[i] = ret + 1; } rets.each!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; immutable long MOD = 10^^9+7; void main() { auto N = readln.chomp.to!int; auto cnt = new long[](10^^3+1); fill(cnt, 0); foreach (i; 2..N+1) { int j = 2; while (j * j <= i) { while (i % j == 0) { i /= j; cnt[j]++; } j++; } if (i > 1) cnt[i]++; } long ans = 1; foreach (i; 2..N+1) { ans = (ans * (cnt[i]+1)) % MOD; } ans.writeln; }
D
import std.stdio, std.string, std.array, std.conv; bool linearSearch(int n, int key, int[] x) { int i = 0; x ~= key; while (x[i] != key) ++i; return i != n; } void main() { int n = readln.chomp.to!int; int[] s = readln.chomp.split.to!(int[]); int q = readln.chomp.to!int; int[] t = readln.chomp.split.to!(int[]); int cnt = 0; foreach (x; t) { if (linearSearch(n, x, s)) ++cnt; } cnt.writeln; }
D
void main() { long n = rdElem; long k = 1L << n; long[] a = k.rdCol; auto st = SegTree!((x, y) => x == y ? x : abs(x-y), long, 0)(k); foreach (i, x; a) { st.update(i, x); } st.tree[1].writeln; } struct SegTree(alias pred, T, T init) { import core.bitop; alias F = binaryFun!pred; long n; T[] tree; this(long n) { this.n = n == 1 ? 1 : 1L << bsr(n-1) + 2; tree.length = this.n << 1; tree[] = init; } ref T opIndex(long idx) pure nothrow { return tree[idx+this.n]; } void update(long idx, T x) { idx += this.n; this.tree[idx] = x; while (idx >>= 1) { tree[idx] = F(tree[(idx<<1)], tree[(idx<<1)+1]); } } T query(long x, long y) { T l = init, r = init; for (x += this.n, y += this.n; x < y; x >>= 1, y >>= 1) { if (x & 1) l = F(l, tree[x++]); if (y & 1) r = F(tree[--y], r); } return F(l, r); } } enum long mod = 10L^^9 + 7; enum long inf = 1L << 60; enum double eps = 1.0e-9; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.mathspecial; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni; import core.bitop;
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; alias Tuple!(double, "x", double, "y") Point; void main() { double[] input; while ((input = readln().split().map!(to!double)().array()).length != 0) { Point[3] p; foreach (i, j; [0, 2, 4]) { p[i].x = input[j]; p[i].y = input[j + 1]; } Point pp; pp.x = input[6]; pp.y = input[7]; if (cross(p[0], p[1], pp, p[2]) | cross(p[1], p[2], pp, p[0]) | cross(p[0], p[2], pp, p[1])) { writeln("NO"); } else { writeln("YES"); } } } bool cross(Point p1, Point p2, Point p3, Point p4) { auto value = ((p1.x - p2.x) * (p3.y - p1.y) + (p1.y - p2.y) * (p1.x - p3.x)) * ((p1.x - p2.x) * (p4.y - p1.y) + (p1.y - p2.y) * (p1.x - p4.x)); return (value < 0); }
D