code
stringlengths
4
1.01M
language
stringclasses
2 values
void main() { dchar[] s = rdDchar; s.sort!"a < b"; writeln(s.uniq.array.length == 2 ? "Yes" : "No"); } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
import std.algorithm, 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 readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}} void main() { int n; readV(n); int[] t; readA(n, t); int m; readV(m); int[] p, x; readC(m, p, x); p[] -= 1; auto s = t.sum; foreach (i; 0..m) writeln(s - t[p[i]] + x[i]); }
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() { int n, m; scan(n, m); auto adj = new int[][](n + m, 0); foreach (i ; 0 .. n) { auto line = readln.split.to!(int[]); foreach (lang ; line[1 .. $]) { lang += n - 1; adj[i] ~= lang; adj[lang] ~= i; } } auto visited = new bool[](n + m); void dfs(int u) { visited[u] = 1; foreach (v ; adj[u]) { if (!visited[v]) { dfs(v); } } } dfs(0); foreach (i ; 0 .. n) { if (!visited[i]) { writeln("NO"); return; } } writeln("YES"); } 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
module app; import core.bitop; import std.algorithm; import std.array; import std.bigint; import std.conv; import std.stdio; import std.string; import std.traits; struct Input { int n, r; } void parseInput(T)(out Input input, T file) { with (file) with (input) { auto ar = readln().strip().split().map!(to!int).array(); n = ar[0].to!int; r = ar[1].to!int; } } auto main2(Input* input) { with (input) { if (n >= 10) return r; else return r + 100 * (10 - n); } } alias retType = ReturnType!main2; static if (!is(retType == void)) { unittest { writeln("begin unittest"); } auto _placeholder_ = ReturnType!main2.init; unittest // example1 { string example = `2 2919`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == 3719); } unittest // example2 { string example = `22 3051`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == 3051); } unittest // example3 { string example = ``; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == _placeholder_); } unittest { writeln("end unittest"); } void parseExample(out Input input, string example) { struct Adapter { string[] _lines; this(string input) { _lines = input.splitLines(); } string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; } } parseInput(input, Adapter(example)); } } void printResult(T)(T result) { static if (isFloatingPoint!T) writefln("%f", result); else writeln(result); } void main() { Input input = void; parseInput(input, stdin); alias retType = ReturnType!main2; static if (is(retType == void)) main2(&input); else { auto result = main2(&input); printResult(result); } }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string; immutable long MOD = 10^^9 + 7; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!int).array; int ans = 0; int tmp = 0; foreach (i; 0..N) { tmp = max(tmp, A[i]); if (i + 1 == tmp) ans += 1, tmp = 0; } ans.writeln; }
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; bool chmin(t)(ref t a, t b) { if (b < a) { a = b; return true; } else { return false; } } void main() { long N = readln.chomp.to!long; long ans = N; foreach (long i; 0..N+1) { long count, t; t = i; while (t > 0) { count += t % 6; t /= 6; } t = N - i; while (t > 0) { count += t % 9; t /= 9; } chmin(ans, count); } writeln(ans); }
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 H = readln.chomp.to!(ulong); void solve() { ulong count; ulong monsters = 1; while(true) { if (H == 0) break; if (H == 1) { H = 0; } else { H /= 2; } count += monsters; monsters *= 2; } writeln(count); } solve(); }
D
import std.algorithm; import std.conv; import std.stdio; import std.string; void main() { auto n = readln.strip.to!( int ); auto as = readln.split.to!( int[] ); writeln( solve( as ) ); } auto solve( in int[] as ) { auto mi = as.reduce!( min ); auto ma = as.reduce!( max ); return ma - mi; }
D
void main() { auto N = rl; dchar[] res; while(N != 0) { N--; res ~= ('a' + N % 26); N /= 26; } res.reverse.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; } long rl() { return readAs!long; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio; import std.range; import std.array; import std.algorithm; import std.string; import std.conv; void main(){ auto ab = readln().chomp().split().map!(to!int); auto a = ab[0]; auto b = ab[1]; solve(a, b).writeln(); } int solve(int a, int b){ return max(a+b, a-b, a*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; int calc(string s) { int[] ww = new int[s.length]; // [0, i] を全て白にするコスト int[] bb = new int[s.length]; // [i, $] を全て黒にするコスト int cost = 0; for (int i = 0; i < s.length; i++) { if (s[i] == '#') cost++; ww[i] = cost; } cost = 0; for (int i = cast(int)s.length-1; i >= 0; i--) { if (s[i] == '.') cost++; bb[i] = cost; } int ans = min(ww[$-1], bb[0]); for (int i = 0; i < s.length; i++) { // [0, i] を白、[i+1, $-1] を黒にするコスト int c = ww[i] + (i + 1 < s.length ? bb[i + 1] : 0); ans = min(ans, c); } return ans; } void main() { readint; string s = read!string; int ans = calc(s); writeln(ans); }
D
void main() { int[] xy = readln.split.to!(int[]); int money; foreach (i; 0 .. 2) { if (xy[i] == 1) { money += 300000; } else if (xy[i] == 2) { money += 200000; } else if (xy[i] == 3) { money += 100000; } } if (xy.all!"a == 1") money += 400000; money.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.algorithm; import std.array; import std.container; 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, L; scan(N, L); // N 個のリングのおいしさは、L, L + 1, L + 2, ..., L + N - 1 int t = iota(L, L + N).reduce!((a, b) => abs(a) <= abs(b) ? a : b); int ans = iota(L, L + N).sum - t; writeln(ans); }
D
void main() { auto N = ri; auto P = iota(N).map!(a => ri).array; auto dp = new int[](N+1); int res = int.max; foreach(i; 0..N) { debug writeln(dp); dp[P[i]] = dp[P[i] - 1] + 1; res = min(res, N - dp[P[i]]); } res.writeln; } // =================================== import std.stdio; import std.string; 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; } T[] deepcopy(T)(T[] a) { static if(isArray!T) { T[] res; foreach(i; a) { res ~= deepcopy(i); } return res; } else { return a.dup; } } ulong[] generate_prime_list(T)(T N) if(isIntegral!T) { ulong[] prime_list = [2]; bool not_prime = false; foreach(i; 3..N.to!ulong+1) { foreach(j; prime_list) { if(i % j == 0) { not_prime = true; break; } } if(!not_prime) prime_list ~= i; not_prime = false; } return prime_list; } bool isPrime(ulong n) { if(n <= 1) return false; else if(n == 2) return true; else if(n % 2 == 0) return false; foreach(i; iota(3, n.to!double.sqrt.ceil+1, 2)) { if(n % i == 0) return false; } return true; } class UnionFind(T) { T[] arr; this(ulong n) { arr.length = n+1; arr[] = -1; } T root(T x) { return arr[x] < 0 ? x : root(arr[x]); } bool same(T x, T y) { return root(x) == root(y); } bool unite(T x, T y) { x = root(x); y = root(y); if(x == y) return false; if(arr[x] > arr[y]) swap(x, y); arr[x] += arr[y]; arr[y] = x; return true; } T size(T a) { return -arr[root(a)]; } }
D
import std; void main() { string s = read!string; writeln(s == "ABC" ? "ARC" : "ABC"); } 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.ascii; import std.conv; import std.string; import std.algorithm; import std.range; import std.functional; import std.math; import core.bitop; void main() { auto dg = readln.chomp.split.map!(to!long); long d = dg[0], g = dg[1]; long[] p = new long[](d), c = new long[](d); foreach (i; 0 .. d) { auto pc = readln.chomp.split.map!(to!long); p[i] = pc[0]; c[i] = pc[1]; } g /= 100; c[] /= 100; long[] tmp = new long[](0); solve(p, c, tmp.dup, tmp.dup, iota(0, cast(long) p.length).array, g).writeln; } long solve(long[] p, long[] c, long[] use, long[] notuse, long[] a, long g) { if (a.empty) { long score, cnt; foreach (long i; use) { score += (i + 1) * p[i] + c[i]; cnt += p[i]; } if (g <= score) { return cnt; } else if (!notuse.empty) { if ((g - score) / (notuse.reduce!max() + 1) <= p[notuse.reduce!max()]) { return max(cnt + ((g - score) / (notuse.reduce!max() + 1)), 1); } else { return long.max; } } else { return long.max; } } else { long y = solve(p, c, use ~ a[0], notuse, a[1 .. $], g); long n = solve(p, c, use, notuse ~ a[0], a[1 .. $], g); return min(y, n); } }
D
import std.stdio; import std.algorithm; import std.conv; import std.datetime; import std.numeric; import std.math; import std.string; string my_readln() { return chomp(readln()); } void main() {//try{ auto tokens = split(my_readln()); auto D = to!ulong(tokens[0]); if (D == 25) writeln("Christmas"); else if (D == 24) writeln("Christmas Eve"); else if (D == 23) writeln("Christmas Eve Eve"); else if (D == 22) writeln("Christmas Eve Eve Eve"); stdout.flush(); /*}catch (Throwable e) { writeln(e.toString()); } readln();*/ }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.random; struct ImplicitKeyTreapNode(Value, Extra=void) { enum has_extra = !(is (Extra == void)); ImplicitKeyTreapNode!(Value, Extra)* left, right; Value value; static if (has_extra) Extra extra; int y; int sz; this (Value _value) { value = _value; y = uniform!int; sz = 1; } } final class ImplicitKeyTreap(Value, Extra=void, alias relax_op="", alias push_op="", alias update_op="") { /* enum has_push = isCallable!push_op; enum has_relax = isCallable!relax_op; enum has_update = isCallable!update_op; */ enum has_push = true; enum has_relax = true; enum has_update = true; static assert (has_push == has_update); alias Node = ImplicitKeyTreapNode!(Value, Extra); Node *root; private static int _size (const Node *t) { return t ? t.sz : 0; } private static void _relax_inc (Node *t) { ++t.sz; static if (has_relax) relax_op (t); } private static void _relax_dec (Node *t) { --t.sz; static if (has_relax) relax_op (t); } private static void _relax (Node *t) { t.sz = 1; if (t.left) t.sz += t.left.sz; if (t.right) t.sz += t.right.sz; static if (has_relax) relax_op (t); } private static void _split (Node *t, ref Node *l, ref Node *r, int key) { if (!t) { l = r = null; return; } static if (has_push) push_op (t); int ls; if (t.left) ls = t.left.sz; if (key <= ls) { _split (t.left, l, t.left, key); r = t; } else { _split (t.right, t.right, r, key - ls - 1); l = t; } _relax (t); } private static void _build (ref Node *t, Value[] v) { if (v.length == 1) { t = new Node (v[0]); static if (has_relax) relax_op (t); } else { immutable m = v.length >> 1; Node* tl, tr; _build (tl, v[0 .. m]); _build (tr, v[m .. $]); _merge (t, tl, tr); } } private static void _insert (ref Node *t, Node *p, int pos) { if (!t) { static if (has_relax) relax_op (p); t = p; return; } else if (p.y >= t.y) { _split (t, p.left, p.right, pos); t = p; _relax (t); } else { static if (has_push) if (t) push_op (t); int ls; if (t.left) ls = t.left.sz; if (pos <= ls) { _insert (t.left, p, pos); _relax_inc (t); } else { _insert (t.right, p, pos - ls - 1); _relax_inc (t); } } } private static void _merge (ref Node *t, Node *l, Node *r) { if (!l) { static if (has_push) if (r) push_op (r); t = r; } else if (!r) { static if (has_push) push_op (l); t = l; } else { static if (has_push) { push_op (l); push_op (r); } if (l.y > r.y) { _merge (l.right, l.right, r); _relax (l); t = l; } else { _merge (r.left, l, r.left); _relax (r); t = r; } } } private static _replace (Node *t, int pos, Value value) { static if (has_relax) { Node*[128] path = void; int n; } while (true) { static if (has_push) push_op (t); static if (has_relax) { path[n++] = t; } int ls; if (t.left) ls = t.left.sz; if (pos == ls) { t.value = value; break; } if (pos < ls) { t = t.left; } else { t = t.right; pos -= ls + 1; } } static if (has_relax) { foreach_reverse (i; 0 .. n) { relax_op (path[i]); } } } private static inout(Node*) _get (inout(Node*) t, int pos) in { assert (pos >= 0); assert (pos < _size (t)); assert (t); } body { if (t.left) { if (pos < t.left.sz) { return _get (t.left, pos); } pos -= t.left.sz; } if (!pos) { return t; } return _get (t.right, pos - 1); } private static void _remove (ref Node *t, int pos) in { assert (t); assert (pos >= 0); assert (pos < _size (t)); } body { static if (has_push) push_op (t); int ls; if (t.left) ls = t.left.sz; if (pos == ls) { _merge (t, t.left, t.right); } else if (pos < ls) { _remove (t.left, pos); assert (t); _relax_dec (t); } else { _remove (t.right, pos - ls - 1); assert (t); _relax_dec (t); } } void insert (int pos, Value value) { _insert (root, new Node (value), pos); } void remove (int pos) { _remove (root, pos); } void replace (int pos, Value value) { _replace (root, pos, value); } Value get (int pos) const in { assert (pos < size ()); } body { return _get (root, pos).value; } int size () const { return _size (root); } Node*[] getNodes () { Node*[] a; a.reserve (_size (root)); void rec (Node *t) { if (t.left) rec (t.left); a ~= t; if (t.right) rec (t.right); } rec (root); return a; } Value[] getValues () { Value[] a; a.reserve (_size (root)); void rec (Node *t) { static if (has_push) push_op (t); if (t.left) rec (t.left); a ~= t.value; if (t.right) rec (t.right); } if (root) rec (root); return a; } static if (has_relax) { private Node[256] nodes; private int cur; private void persistent_split (Node *t, ref Node *l, ref Node *r, int key) { if (!t) { l = r = null; return; } int ls; if (t.left) ls = t.left.sz; Node *q = &nodes[cur++]; q.value = t.value; q.y = t.y; if (key <= ls) { q.right = t.right; persistent_split (t.left, l, q.left, key); r = q; } else { q.left = t.left; persistent_split (t.right, q.right, r, key - ls - 1); l = q; } _relax (q); } Extra reduce (int l, int r) { cur = 0; Node *t1, t2, t3; persistent_split (root, t1, t2, l); persistent_split (t2, t2, t3, r - l); return t2.extra; } } static if (has_update) { void update (int l, int r) { if (!l) { Node *t1, t2; _split (root, t1, t2, r); update_op (t1); _merge (root, t1, t2); } else { Node *t1, t2, t3; _split (root, t1, t2, l); _split (t2, t2, t3, r - l); update_op (t2); _merge (t2, t2, t3); _merge (root, t1, t2); } } } this (Value[] v) { _build (root, v); } } struct E { //ImplicitKeyTreapNode!(char, E) *parent; bool rev; } void relax (ImplicitKeyTreapNode!(char, E) *t) { //if (t.left) t.left.extra.parent = t; //if (t.right) t.right.extra.parent = t; } void push (ImplicitKeyTreapNode!(char, E) *t) { if (t.extra.rev) { t.extra.rev = false; swap (t.left, t.right); if (t.left) t.left.extra.rev = !t.left.extra.rev; if (t.right) t.right.extra.rev = !t.right.extra.rev; } } void update (ImplicitKeyTreapNode!(char, E) *t) { t.extra.rev = !t.extra.rev; } void main() { rndGen.seed (1); auto s = readln.stripRight; auto z = readln.stripRight; const int nt = z.to!int; auto tree = new ImplicitKeyTreap!(char, E, relax, push, update) (s.dup); int n = s.length.to!int; foreach (tid; 0 .. nt) { z = readln.stripRight; auto w = z.splitter; const int t = w.front.to!int; w.popFront (); if (t == 1) { //tree.update (0, n); update (tree.root); } else { int f = w.front.to!int; w.popFront (); char c = w.front[0]; tree.insert (f == 1 ? 0 : n, c); ++n; } debug { auto ans = tree.getValues (); stderr.writeln (ans); } } /* auto w = tree.reduce (0, n); foreach (i; 0 .. n) { write (tree.get (i)); } writeln; */ auto ans = tree.getValues (); writeln (ans); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto H = RDA; bool ans = true; foreach_reverse (i; 1..N) { if (H[i-1] <= H[i]) continue; if (H[i-1]-1 <= H[i]) { --H[i-1]; continue; } else { ans = false; break; } } writeln(ans ? "Yes" : "No"); stdout.flush(); debug readln(); }
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 a; readV(a); auto p = new int[][](26); foreach (int i, ai; a) p[ai-'a'] ~= i; int[] s; auto c = 0, b = new bool[](26); foreach_reverse (i, ai; a) { if (!b[ai-'a']) { b[ai-'a'] = true; ++c; if (c == 26) { s ~= i.to!int; c = 0; b[] = false; } } } s.reverse(); auto k = s.length.to!int; char[] r; auto j = -1; foreach (i; 0..k) { foreach (d; 0..26) { auto di = p[d].assumeSorted.upperBound(j).front; if (di >= s[i]) { r ~= cast(char)('a'+d); j = di; break; } } } foreach (d; 0..26) { auto di = p[d].assumeSorted.upperBound(j); if (di.empty) { r ~= cast(char)('a'+d); break; } } writeln(r); }
D
/+ dub.sdl: name "A" dependency "dunkelheit" version=">=0.9.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; int main() { Scanner sc = new Scanner(stdin); scope(exit) assert(!sc.hasNext); string s; sc.read(s); if (s.length != 26) { int[26] d; s.each!(c => d[c-'a']++); foreach (i; 0..26) { if (d[i] == 0) { s ~= i + 'a'; writeln(s); return 0; } } assert(false); } int b = 26; foreach_reverse (i; 0..25) { if (s[i] < s[i+1]) { b = i; break; } } if (b == 26) { writeln("-1"); return 0; } char[] ss = s.dup; int z = ss[b] - 'a'; foreach (j; z+1..26) { if (!s[b+1..$].count(cast(char)(j + 'a'))) continue; ss[b] = cast(char)(j + 'a'); break; } ss = ss[0..b+1]; writeln(ss); 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
void main() { long[] tmp = readln.split.to!(long[]); long h = tmp[0], w = tmp[1]; long[][] c = new long[][](10); foreach (i; 0 .. 10) { c[i] = readln.split.to!(long[]); } foreach (i; 0 .. 10) { foreach (j; 0 .. 10) { foreach (k; 0 .. 10) { c[j][k] = min(c[j][k], c[j][i]+c[i][k]); } } } long[10] cnts; foreach (i; 0 .. h) { long[] a = readln.split.to!(long[]); foreach (x; a) { if (x == -1) continue; ++cnts[x]; } } long mp; foreach (i; 0 .. 10) { mp += cnts[i] * c[i][1]; } mp.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.string; void main() { auto l = readln().chomp(); size_t c; size_t t; for (size_t i; i < l.length; ++i) { if (l[i] == 'W') { t += i - c; ++c; } } t.write; }
D
import std; import core.bitop; ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "index", long, "num"); alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less); void main() { long n, m; scan(n, m); auto a = new string[](n); auto b = new string[](m); foreach (ref e; a) e = sread(); foreach (ref e; b) e = sread(); bool includes(long x, long y) { bool ret = true; foreach (i; iota(m)) { ret &= (a[x + i][y .. y + m] == b[i]); } return ret; } foreach (i; iota(n - m + 1)) { foreach (j; iota(n - m + 1)) { if (includes(i, j)) { writeln("Yes"); return; } } } writeln("No"); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto sw = readln.split.to!(int[]); writeln(sw[1] >= sw[0] ? "unsafe" : "safe"); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto t = RD!int; auto ans = new bool[](t); foreach (ti; 0..t) { auto n = RD!int; auto a = RDA; auto b = RDA; bool add, sub; ans[ti] = true; foreach (i; 0..n) { if (a[i] > b[i]) { if (!sub) { ans[ti] = false; break; } } else if (a[i] < b[i]) { if (!add) { ans[ti] = false; break; } } if (a[i] > 0) add = true; else if (a[i] < 0) sub = 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; enum P = 10L^^9+7; long[][10^^5] T; long[10^^5] D; bool[10^^5] M; void main() { auto nk = readln.split.to!(long[]); auto N = nk[0]; auto K = nk[1]; foreach (_; 0..N-1) { auto ab = readln.split.to!(long[]); auto a = ab[0]-1; auto b = ab[1]-1; T[a] ~= b; T[b] ~= a; } long r = 1; long[] ss = [0]; M[0] = true; while (!ss.empty) { auto i = ss[0]; ss = ss[1..$]; auto ir = K - D[i]; foreach (j; T[i]) { if (!M[j]) { ss ~= j; M[j] = true; } ir -= D[j]; ++D[j]; } r = (r * ir) % P; } writeln(r); }
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); } T binarySearch(alias pred, T)(T ok, T ng) { while (abs(ok-ng) > 1) { auto mid = (ok+ng)/2; if (unaryFun!pred(mid)) ok = mid; else ng = mid; } return ok; } void main() { auto N = RD; auto M = RD; auto K = RD; auto A = RDA; auto B = RDA; auto c = new long[](M+1); foreach (i; 0..M) c[i+1] = c[i] + B[i]; long ans; long tot; foreach (i; 0..N+1) { auto remain = K - tot; if (remain < 0) continue; bool f(long x) { return c[x] <= remain; } auto r = binarySearch!(f)(0, M+1); ans.chmax(i+r); if (i == N) continue; tot += A[i]; } writeln(ans); stdout.flush; debug readln; }
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 s = readln.chomp.array; auto k = readln.chomp.to!long; if (s.uniq.array.length == 1) { writeln(s.length * k /2); } else { long tmp; long cnt = 1; foreach (i; 1..s.length) { if (s[i-1] == s[i]) { cnt++; } else { tmp += cnt / 2; cnt = 1; } } tmp += cnt / 2; tmp *= k; if (s[0] != s[$-1]) { tmp.writeln; } else { long a=1, b=1; foreach (i; 1..s.length) { if (s[i-1] == s[i]) a++; else break; } foreach_reverse (i; 0..s.length-1) { if (s[i+1] == s[i]) b++; else break; } tmp -= (k-1) * (a/2 + b/2 - (a+b)/2); tmp.writeln; } } }
D
module app; import core.bitop; import std.algorithm; import std.array; import std.bigint; import std.container.rbtree; import std.conv; import std.stdio; import std.string; import std.traits; struct Input { string s; string t; } void parseInput(T)(out Input input, T adapter) { with (input) { s = adapter.readln(); t = adapter.readln(); } } struct Output { } auto main2(Input* input) { int minDistance = int.max; int i = 0; while (i + input.t.length <= input.s.length) { int distance = 0; for (int j = 0; j < input.t.length; j++) { if (input.s[i + j] != input.t[j]) distance++; } if (distance < minDistance) minDistance = distance; i++; } return minDistance; } alias retType = ReturnType!main2; static if (!is(retType == void)) { unittest { writeln("begin unittest"); } auto _placeholder_ = ReturnType!main2.init; unittest // example1 { string example = `cabacc abc`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == 1); } unittest // example2 { string example = `codeforces atcoder`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == 6); } unittest // example3 { string example = ``; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == _placeholder_); } unittest { writeln("end unittest"); } void parseExample(out Input input, string example) { parseInput(input, new StringAdapter(example)); } } class StdinAdapter { // readf!"%s"は使えない // https://issues.dlang.org/show_bug.cgi?id=19820 uint readf(alias format, A...)(auto ref A args) { return std.stdio.readf!format(args); } string readln() { return std.stdio.readln().strip(); } } class StringAdapter { import std.format; string _s; this(string input) { _s = input; } uint readf(alias format, A...)(auto ref A args) { return _s.formattedRead!format(args); } string readln() { string ret; auto i = _s.countUntil("\n"); if (i == -1) { ret = _s; _s = ""; } else { ret = _s[0..i]; _s = _s[i+1..$]; } return ret; } } void printResult(T)(T result) { static if (isFloatingPoint!T) writefln("%f", result); else writeln(result); } void main() { Input input = void; parseInput(input, new StdinAdapter()); static if (is(retType == void)) main2(&input); else { auto result = main2(&input); printResult(result); } }
D
import std.stdio; import std.conv; import std.array; void main() { auto reader = readln.split; int A = reader[0].to!int; int B = reader[1].to!int; if (A == B){ writeln(A * 2); } else { int max = A; if (B > A) max = B; writeln(max + (max - 1)); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; enum P = 10L^^9+7; long pow(long x, long n) { long y = 1; while (n) { if (n%2 == 1) y = (y * x) % P; x = x^^2 % P; n /= 2; } return y; } long inv(long x) { return pow(x, P-2); } Tuple!(N, N)[] prime_division(N)(N n) { Tuple!(N, N)[] res; foreach (N i; 2..10^^3+1) { if (n%i == 0) { N cnt; while (n%i == 0) { ++cnt; n /= i; } res ~= tuple(i, cnt); } } if (n != cast(N)1) res ~= tuple(n, cast(N)1); return res; } void main() { auto N = readln.chomp.to!int; auto AS = readln.split.to!(long[]); long[long] memo; long[long][] aps; foreach (a; AS) { auto ps = prime_division(a); foreach (p; ps) { if (p[0] in memo) { memo[p[0]] = max(memo[p[0]], p[1]); } else { memo[p[0]] = p[1]; } } } long x = 1; foreach (n, k; memo) (x *= pow(n, k)) %= P; long r; foreach (a; AS) (r += x * inv(a) % P) %= P; writeln(r); }
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; long calc(int K, int[] a) { long ans = long.max; int n = cast(int) a.length; auto buf = new int[n]; auto mask = new bool[n]; for (int i = 1; i < (1 << n); i++) { // a[0] は必ず見えてしまうのでビットを立てる if (!(i & (1 << 0))) continue; mask[] = false; for (int j = 0; j < n; j++) { if (i & (1 << j)) { mask[j] = true; } } if (mask.count(true) != K) continue; buf[] = a[]; if (K > 1) { for (int j = 0; j < n; j++) { for (int k = j + 1; k < n; k++) { if (mask[j] && mask[k]) { // j と k の間にある壁は j より低くする int h = buf[j]; for (int m = j; m < k; m++) { h = max(h, buf[m]); } buf[j] = h; buf[k] = max(h + 1, buf[k]); break; } } } } // ラストのビットの壁を一番高くする(それより後ろの壁は見えなくする) int h = 0; for (int j = n - 1; j >= 0; j--) { h = max(h, buf[j]); if (mask[j]) { buf[j] = h; break; } } long cost = 0; for (int j = 0; j < n; j++) { cost += buf[j] - a[j]; } ans = min(ans, cost); } return ans; } void main() { auto nk = readints; int k = nk[1]; auto a = readints; writeln(calc(k, a)); }
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, int y) { for (int a = 0; a <= 2000; a++) { int x = y - 1000 * n - 9000 * a; if (x < 0) return false; if (x % 4000 == 0) { int b = x / 4000; int c = n - a - b; if (c >= 0) { writeln(a, " ", b, " ", c); return true; } } } return false; } void main() { auto ny = readints; int n = ny[0], y = ny[1]; bool found = calc(n, y); if (!found) writeln("-1 -1 -1"); }
D
unittest { assert( [ "atcoder" ].parse.expand.solve == "acdr" ); assert( [ "aaaa" ].parse.expand.solve == "aa" ); assert( [ "z" ].parse.expand.solve == "z" ); assert( [ "fukuokayamaguchi" ].parse.expand.solve == "fkoaaauh" ); } import std.conv; import std.range; import std.stdio; import std.string; import std.typecons; void main() { stdin.byLineCopy.parse.expand.solve.writeln; } auto parse( Range )( Range input ) if( isInputRange!Range && is( ElementType!Range == string ) ) { auto s = input.front.strip; return tuple( s ); } auto solve( string s ) { auto t = ""; for( auto i = 0; i < s.length; i += 2 ) { t ~= s[ i ]; } return t; }
D
void main() { long n = rdElem; long[] a = rdRow; if (n == 0) { writeln(a[0] == 1 ? 1 : -1); return; } if (a[0]) { writeln(-1); return; } long[] v = new long[n+1]; v[n] = a[n]; foreach_reverse (i; 0 .. n) { v[i] = v[i+1] + a[i]; } long u = 1; long result = u; foreach (i; 1 .. n+1) { u <<= 1; u = min(u, v[i]); result += u; u -= a[i]; if (u < 0) { writeln(-1); return; } } result.writeln; } 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, 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 s = RD!string; char last = s[0]; int[] a; int cnt; foreach (i; 0..n) { if (s[i] == last) ++cnt; else { a ~= cnt; cnt = 1; last = s[i]; } } a ~= cnt; debug writeln(a); long c; foreach (e; a) { ++c; auto x = min(e-1, c); c -= x; ans[ti] += x; } ans[ti] += (c+1) / 2; } foreach (ti; 0..t) { writeln(ans[ti]); } stdout.flush; debug readln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto T = readln.chomp.to!int; foreach (_; 0..T) { auto nk = readln.split.to!(int[]); auto N = nk[0]; auto K = nk[1]; auto as = readln.split.to!(int[]); auto ns = new int[]((K*2+2).to!size_t); foreach (i; 0..N/2) { auto x = as[i.to!size_t]; auto y = as[(N-i-1).to!size_t]; if (x > y) swap(x, y); auto p = x+y; ns[0] += 2; ns[(x+1).to!size_t] -= 1; ns[p.to!size_t] -= 1; ns[(p+1).to!size_t] += 1; ns[(y+K+1).to!size_t] += 1; } foreach (i; 0..ns.length-1) { ns[i+1] += ns[i]; } int min_n = int.max; foreach (i; 0..ns.length) { min_n = min(min_n, ns[i]); } writeln(min_n); } }
D
import std.stdio; import std.range; import std.array; import std.algorithm; import std.string; import std.conv; void main(){ int input = readln.chomp.to!int; writeln(input / 3); }
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } void main(){ long n = read.to!long; string a = readln.chomp; string b = readln.chomp; string c = readln.chomp; long ans; foreach(i; 0 .. n){ char x = a[i], y = b[i], z = c[i]; if(x == y && y == z) ans += 0; else if(x == y || y == z || z == x) ans += 1; else ans += 2; } ans.writeln; }
D
import std.stdio, std.conv, std.string; import std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } int DEBUG_LEVEL = 0; void print()(){ writeln(""); } void print(T, A ...)(T t, lazy A a){ write(t), print(a); } void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); } void main(string[] args){ if(args.length > 1 && args[1] == "-debug"){ if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int; else DEBUG_LEVEL = 1; } int n = read.to!int; int m = read.to!int; int ans; if(m == 0) ans = 1; else if(m * 2 <= n) ans = m; else if(m < n) ans = n - m; else ans = 0; ans.writeln; }
D
/+ dub.sdl: name "D" dependency "dunkelheit" version=">=0.9.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; long[2][] aDiv(long a, long dist, long cnt) { if (a == cnt) return [[1, cnt]]; long[2][] al; long af = (a-cnt) / (dist-1); al ~= [dist, af]; a -= dist*af; cnt -= af; if (cnt == 0) return al; al ~= [a - cnt + 1, 1]; a -= (a-cnt+1); cnt -= 1; al ~= [1, a]; return al; } long[2][] bDiv(long b, long dist, long cnt) { auto u = aDiv(b, dist, cnt); u.reverse; return u; } string calc(long a, long b, long c, long d) { long l = 0, r = max(a, b); long amin, amax, bmin, bmax; void fill(long md) { amin = (a+md-1)/md; amax = a; bmin = (b+md-1)/md; bmax = b; } while(r-l > 1) { long md = (l+r)/2; fill(md); bool f = true; if (amax < bmin-1) f = false; if (bmax < amin-1) f = false; if (!f) { l = md; } else { r = md; } } fill(r); // writeln("FIND ", r, " ", amin, " ", amax, " ", bmin, " ", bmax); long[2][] al, bl; if (bmin == amax+1) { //bbbabbbabbbabbb al = [0, 1] ~ aDiv(a, r, amax); bl = bDiv(b, r, bmin); } else if (amin == bmax+1) { al = aDiv(a, r, amin); bl = bDiv(b, r, bmax) ~ [0, 1]; } else { amin = bmin = max(amin, bmin); amax = bmax = min(amax, bmax); long z = r; l = amin-1, r = bmax; while (r-l > 1) { long md = (l+r)/2; long af = (a-md) / (z-1); long bf = md - (b-md + z-2) / (z-1); if (af - bf >= 2) { // if (af > bf) { l = md; } else if (af - bf == 1) { if ((a - md) % (z-1) == 0) { r = md; } else { l = md; } } else { r = md; } } // writeln("result ", l, " ", r); al = aDiv(a, z, r); bl = bDiv(b, z, r); } // writeln(al, " ", bl); long m = al.map!"a[1]".sum; long cnt(long[2][] li, long u) { long sm = 0; foreach (d; li) { if (u <= d[1]) return sm + d[0]*u; sm += d[0]*d[1]; u -= d[1]; } return u; } long[2][] skip(long[2][] _li, long u) { long[2][] li = _li; while (u) { if (li[0][1] == 0) { li = li[1..$]; continue; } if (u <= li[0][1]) { li[0][1] -= u; break; } u -= li[0][1]; li[0][1] = 0; } return li; } l = 0, r = m; while (r-l > 1) { long md = (l+r)/2; long z = cnt(al, md) + cnt(bl, md); if (z <= c) { l = md; } else { r = md; } } long z = cnt(al, l) + cnt(bl, l); c -= z; d -= z; al = skip(al, l); bl = skip(bl, l); string s; void add(char ch) { if (c <= 0 && 0 < d) s ~= ch; c--; d--; } void addS(long A, long B) { if (c > 0) { long u = min(c, A); c -= u; d -= u; A -= u; } foreach (i; 0..A) { add('A'); if (d <= 0) break; } if (c > 0) { long u = min(c, B); c -= u; d -= u; B -= u; } foreach (i; 0..B) { add('B'); if (d <= 0) break; } } while (0 < d) { while (al[0][1] == 0) al = al[1..$]; while (bl[0][1] == 0) bl = bl[1..$]; addS(al[0][0], bl[0][0]); al[0][1]--; bl[0][1]--; } return s; } int main() { Scanner sc = new Scanner(stdin); scope(exit) sc.read!true; int q; sc.read(q); foreach (_; 0..q) { long a, b, c, d; sc.read(a, b, c, d); c--; writeln(calc(a, b, c, d)); } return 0; } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */ // module dkh.scanner; // import dkh.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(bool enforceEOF = false, T, Args...)(ref T x, auto ref Args args) { import std.exception; enforce(readSingle(x)); static if (args.length == 0) { enforce(enforceEOF == false || !succ()); } else { read!enforceEOF(args); } } void read(bool enforceEOF = false, Args...)(auto ref Args args) { import std.exception; static if (args.length == 0) { enforce(enforceEOF == false || !succ()); } else { enforce(readSingle(args[0])); read!enforceEOF(args); } } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std.stdio, std.conv, std.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 = "%.15f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto s = RD!string; long cnt; foreach (c; s) { if (c == 'p') ++cnt; } auto ans = s.length / 2 - cnt; writeln(ans); stdout.flush(); debug 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; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { int W, H, N; scan(W, H, N); int l = 0, r = W; int d = 0, u = H; foreach (i ; 0 .. N) { int xi, yi, ai; scan(xi, yi, ai); if (ai == 1) { chmax(l, xi); } else if (ai == 2) { chmin(r, xi); } else if (ai == 3) { chmax(d, yi); } else { chmin(u, yi); } debug { writefln("%s, %s", l, r); writefln("%s, %s", d, u); } } auto ans = max(0, r - l) * max(0, u - d); 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; import std.conv; import std.algorithm; import std.array; import std.string; import std.uni; import std.math; void main() { foreach(_; stdin.byLineCopy()) { writeln("NO"); stdout.flush(); } }
D
import std.stdio; import std.algorithm; import std.range; import std.conv; import std.string; int f(int a, int k) { if (a < k) return 0; if (a%k == 0) return a/k; int c = a/k+1; int l = a/k*k; a -= ((a-l-1)/c+1)*c; return f(a, k); } void main() { int n = to!int(chomp(readln())); //int m = 100; //int[] dp = new int[m+1]; //foreach (i; 0..m+1) { // int[] s = new int[m+1]; // foreach (j; 1..i/n+1) { // s[dp[i-j]] = 1; // } // foreach (j; 0..m) { // if (s[j] == 1) continue; // dp[i] = j; // break; // } // writefln("%2d : %2d - %2d", i, dp[i], f(i,n)); //} //return; int x = 0; foreach (i; 0..n) { string[] input = split(readln()); int a = to!int(input[0]); int k = to!int(input[1]); x ^= f(a,k); } writeln(x ? "Takahashi" : "Aoki"); }
D
import std.stdio, std.conv, std.algorithm, std.array; void main(){ while(true){ auto line = readln(); if(line == null){ break; } auto strs = line.split(); if(strs.length < 8){ break; } auto input = strs.map!(to!real)(); auto p1 = input[0] + input[1] * 1.0i, p2 = input[2] + input[3] * 1.0i, p3 = input[4] + input[5] * 1.0i, p = input[6] + input[7] * 1.0i; if((dot(p1, p2, p) && dot(p2, p3, p) && dot(p3, p1, p)) || (!dot(p1, p2, p) && !dot(p2, p3, p) && !dot(p3, p1, p))){ writeln("YES"); }else{ writeln("NO"); } } } bool dot(creal base, creal src, creal target){ real a = (src - base).re, b = (src - base).im, c = (target - base).re, d = (target - base).im; return a * d - b * c > 0; }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.math; immutable limitList = 100000; void main() { int input; bool[] listNumbers = new bool[](limitList); int[] listPrimeNumbers; //List of prime numbers. listNumbers.fill(true); listNumbers[0..1] = false; foreach (i; 2..limitList.to!double.sqrt.to!int) { if (listNumbers[i]) { for (int j = i*2; j < limitList; j += i) listNumbers[j] = false; } } while ((input = readln.chomp.to!int) != 0) { uint count = 0; foreach (i; 2..(input/2)+1) { if(listNumbers[i] && listNumbers[input-i]) count++; } writeln(count); } }
D
import std.algorithm; import std.conv; import std.stdio; import std.string; void main() { auto abcxy = readln.split.map!( to!int ); writeln( solve( abcxy[ 0 ], abcxy[ 1 ], abcxy[ 2 ], abcxy[ 3 ], abcxy[ 4 ] ) ); } int solve( in int a, in int b, in int c, in int x, in int y ) { auto cc = c * 2; if( a + b < cc ) return ( a * x ) + ( b * y ); if( x < y ) { if( b < cc ) return ( cc * x ) + ( b * ( y - x ) ); else return ( cc * x ) + ( cc * ( y - x ) ); } else { if( a < cc ) return ( cc * y ) + ( a * ( x - y ) ); else return ( cc * y ) + ( cc * ( x - y ) ); } } unittest { assert( solve( 1500, 2000, 1600, 3, 2 ) == 7900 ); assert( solve( 1500, 2000, 1900, 3, 2 ) == 8500 ); assert( solve( 1500, 2000, 500, 90000, 100000 ) == 100000000 ); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nab = readln.split.to!(long[]); auto N = nab[0]; auto A = nab[1]; auto B = nab[2]; if (N <= A) { writeln(N); } else { auto c = N / (A+B); writeln(A * c + min(A, N % (A+B))); } }
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)); } bool solve(const(long) A, const(long) B) { if (!(A <= B)) { return false; } int[] xs, ys; foreach (e; 0 .. bsr(A)) if ((A >> e) & 1) xs ~= e; foreach (e; 0 .. bsr(B)) if ((B >> e) & 1) ys ~= e; const xsLen = cast(int)(xs.length); const ysLen = cast(int)(ys.length); if (!(xsLen >= ysLen)) { return false; } foreach (i; 0 .. ysLen) { if (!(xs[i] <= ys[i])) { return false; } } return true; } void main() { debug { enum V = 1 << 8; auto d = new bool[][](V, V); foreach (u; 1 .. V) { d[u][u] = true; } foreach (u; 1 .. V) { foreach (v; 1 .. V - u) { if ((u & v) == v) { d[u][u + v] = true; } } } foreach (w; 1 .. V) foreach (u; 1 .. V) if (d[u][w]) foreach (v; 1 .. V) if (d[w][v]) { d[u][v] = true; } foreach (u; 1 .. V) foreach (v; 1 .. V) { const res = solve(u, v); assert(d[u][v] == res, format("%s %s: %s %s", u, v, d[u][v], res)); } } try { for (; ; ) { const numCases = readInt(); foreach (caseId; 0 .. numCases) { const A = readLong(); const B = readLong(); const ans = solve(A, B); writeln(ans ? "YES" : "NO"); } } } catch (EOFException e) { } }
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() { auto s = sread(); long cnt; foreach (i; iota(s.length - 1)) { if(s[i] != s[i + 1]) cnt++; } cnt.writeln(); } bool is_reversable(string s) { bool ret = true; foreach (i; iota(s.length / 2)) { ret &= (s[i] == s[$ - 1 - i]); } return ret; } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
/+ dub.sdl: name "A" dependency "dunkelheit" version=">=0.7.4" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dkh.foundation, dkh.scanner; Scanner sc; static this() { sc = new Scanner(stdin); } int query(int i) { writeln(i); stdout.flush(); string s; sc.read(s); if (s == "Vacant") return 0; if (s == "Male") return -1; if (s == "Female") return 1; assert(false); } int main() { int n; sc.read(n); int u = query(0); if (u == 0) return 0; int lc = u; int l = 1, r = n; while (true) { int md = (l+r)/2; int w = query(md); if (w == 0) return 0; bool od1 = (md-l) % 2 != 0; bool od2 = (lc != w); // writeln(od1, " ", od2); if (od1 == od2) { r = md; } else { l = md+1; lc = w; } } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */ // module dkh.container.stackpayload; struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) { import core.exception : RangeError; private T* _data; private uint len, cap; @property bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } ref inout(T) opIndex(size_t i) inout { version(assert) if (len <= i) throw new RangeError(); return _data[i]; } ref inout(T) front() inout { return this[0]; } ref inout(T) back() inout { return this[$-1]; } void reserve(size_t newCap) { import core.memory : GC; import core.stdc.string : memcpy; import std.conv : to; if (newCap <= cap) return; void* newData = GC.malloc(newCap * T.sizeof); cap = newCap.to!uint; if (len) memcpy(newData, _data, len * T.sizeof); _data = cast(T*)(newData); } void free() { import core.memory : GC; GC.free(_data); } void clear() { len = 0; } void insertBack(T item) { import std.algorithm : max; if (len == cap) reserve(max(cap * 2, MINCAP)); _data[len++] = item; } alias opOpAssign(string op : "~") = insertBack; void removeBack() { assert(!empty, "StackPayload.removeBack: Stack is empty"); len--; } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */ // module dkh.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } /* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */ // module dkh.scanner; // import dkh.container.stackpayload; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { StackPayload!E buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int unsafeRead(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } void read(bool enforceEOF = false, T, Args...)(ref T x, auto ref Args args) { import std.exception; enforce(readSingle(x)); static if (args.length == 0) { enforce(enforceEOF == false || !succ()); } else { read!enforceEOF(args); } } void read(bool enforceEOF = false, Args...)(auto ref Args args) { import std.exception; static if (args.length == 0) { enforce(enforceEOF == false || !succ()); } else { enforce(readSingle(args[0])); read!enforceEOF(args); } } } /* This source code generated by dunkelheit and include dunkelheit's source code. dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit) dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt) */
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; void main() { int a, b; scan(a, b); if (a >= 9 || b >= 9) { writeln(":("); } else { writeln("Yay!"); } } 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);} const e = ["dream", "dreamer", "erase", "eraser"]; void main() { string s; readV(s); loop: while (!s.empty) { foreach (ei; e) { if (s.endsWith(ei)) { s = s[0..$-ei.length]; continue loop; } } writeln("NO"); return; } writeln("YES"); }
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } void main(){ long n = read.to!long; long a = read.to!long; long b = read.to!long; long x, y; if(a + b < n) x = 0; else x = (a + b) - n; y = min(a, b); writeln(y, " ", x); }
D
/+ dub.sdl: name "C" 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[2][] v = new int[2][n]; foreach (i; 0..n) { int x; sc.read(x); v[i] = [x, i]; } v.sort!"a[0]<b[0]"; int[] res = new int[n]; foreach (i; 0..n/2) { res[v[i][1]] = v[n/2][0]; } foreach (i; n/2..n) { res[v[i][1]] = v[n/2-1][0]; } foreach (d; res) writeln(d); 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.algorithm; import std.conv; import std.range; import std.stdio; import std.string; void main () { string s; while ((s = readln.strip) != "") { auto n = s.length.to !(int); int res = 0; foreach (i; 0..n) { foreach (j; i + 1..n + 1) { auto t = s[i..j]; if (!equal (t, t.retro)) { res = max (res, t.length.to !(int)); } } } writeln (res); } }
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; struct List { List* next; List* prev; long deta; this(long deta) { this.deta = deta; } } void initList(List* head) { head.next = head.prev = head; } void addbefore(List* head, List* add) { add.next = head; add.prev = head.prev; head.prev.next = add; head.prev = add; } void addafter(List* tail, List* add) { add.next = tail.next; add.prev = tail; tail.next.prev = add; tail.next = add; } void printfirst(List* head, List* tail) { while (head != tail) { head.deta.write(" "); head = head.next; } head.deta.writeln(); } void printlast(List* tail, List* head) { while (tail != head) { tail.deta.write(" "); tail = tail.prev; } tail.deta.writeln(); } void main() { auto n = readln.chomp.to!(long); auto a = readln.split.to!(long[]); auto head = new List; auto tail = new List; bool flag; foreach (i, e; a) { if(i == 0) { auto elem = new List(e); initList(elem); head = tail = elem; continue; } if(flag) { auto add = new List(e); addbefore(head, add); head = head.prev; } else { auto add = new List(e); addafter(tail, add); tail = tail.next; } flag = !flag; } if(flag) printlast(tail, head); else printfirst(head, tail); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; enum MP = [ [0,0], [9,10], [9,11], [3,4], [9,13], [9,14], [3,5], [9,16], [9,17], [0,0] ]; void main() { auto M = readln.chomp.to!int; long[] ds; long r; foreach (_; 0..M) { auto dc = readln.split.to!(long[]); auto d = dc[0]; auto c = dc[1]; if (d == 0) { r += c-1; c = 1; } else if (d == 9) { r += (c-1)*2; c = 1; } else { void calc(long m, long p) { if (c%m == 0) { r += (c/m-1)*p; c = m; } else { r += c/m*p; c %= m; } } calc(MP[d][0], MP[d][1]); } while (c--) ds ~= d; } while (ds.length > 1) { ++r; auto c = ds[0]+ds[1]; if (c >= 10) { ds[0] = 1; ds[1] = c%10; } else { ds[1] = c; ds = ds[1..$]; } } writeln(r); }
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } void main(){ int n = read.to!int; int[] ls = readln.chomp.split.map!(to!int).array; int sum; foreach(l; ls) sum += l; string ans = "Yes"; foreach(l; ls) if(sum - l <= l) ans = "No"; ans.writeln; }
D
import std.algorithm; import std.bigint; import std.concurrency; import std.container; import std.conv; import std.functional; import std.format; import std.math; import std.meta; import std.numeric; import std.random; import std.range; import std.stdio; import std.string; import std.traits; import std.typecons; auto solve(string b) { return [ "A": "T", "T": "A", "C": "G", "G": "C" ][b]; } void main() { auto input = stdin.byLine.map!split.joiner; string b; b = input.front.to!string; input.popFront; static if (is(ReturnType!solve == void)) solve(b); else solve(b).writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto a = RDA; auto s = a.sum; if (s % 2 == 1) writeln("NO"); else { bool ans; foreach (i; 0..2^^4) { long cnt; foreach (j; 0..4) { auto bit = 1L << j; if (i & bit) cnt += a[j]; } if (s == cnt*2) { ans = true; break; } } writeln(ans ? "YES" : "NO"); } stdout.flush(); debug readln(); }
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() { auto n = lread(); writeln(n * (n - 1) / 2); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.stdio, std.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 == 0) { writeln(0); return; } auto ans = f(N).stripLeft('0'); writeln(ans); } string f(int N) { if (N == 0) { return "0"; } int b = (N % 2 + 2) % 2; if (b) N -= 1; return f(N / (-2)) ~ b.to!string; } 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.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); long ans = N - 1; foreach (x; 1 .. N + 1) { if (N < x * x) break; if (N % x != 0) continue; ans = ans.min(x - 1 + (N / x) - 1); } writeln(ans); }
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; T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } alias sread = () => readln.chomp(); void main() { auto s = sread(); if (s == "1") { writeln("Hello World"); } else { writeln(lread + lread); } }
D
import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } void main(){ int n = read.to!int; long[] as = readln.split.map!(to!long).array; int count = 0; int trend = 0; bool hasOld = 0; long old = 0; for(int i = 0; i < n; i ++){ debug writeln(i, " ", as[i], " ", old, " ", trend, " ", count); if(!hasOld){ count += 1; debug "a".writeln; } else{ if(trend != 0){ if(trend > 0 && old <= as[i] || trend < 0 && old >= as[i]){ debug "b".writeln; } else{ trend = 0; count += 1; debug "c".writeln; } } else{ if(old < as[i]) trend = 1; else if(old > as[i]) trend = -1; debug "d".writeln; } } old = as[i]; hasOld = 1; debug writeln(i, " ", as[i], " ", old, " ", trend, " ", count); debug "-".writeln; } count.writeln; }
D
import std.stdio; import std.range; import std.array; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.container; import std.typecons; import std.random; import std.csv; import std.regex; import std.math; import core.time; import std.ascii; import std.digest.sha; import std.outbuffer; import std.numeric; import std.bigint; import core.checkedint; import core.bitop; int f(int x) { int ans = 0; while (x) { ans += x % 10; x /= 10; } return ans; } void main() { int n = readln.chomp.to!int; int ans = int.max; for (int a = 1; a <= n - 1; ++a) { ans = min(ans, a.f + f(n - a)); } ans.writeln; } void tie(R, Args...)(R arr, ref Args args) if (isRandomAccessRange!R || isArray!R) in { assert (arr.length == args.length); } body { foreach (i, ref v; args) { alias T = typeof(v); v = arr[i].to!T; } } void verbose(Args...)(in Args args) { stderr.write("["); foreach (i, ref v; args) { if (i) stderr.write(", "); stderr.write(v); } stderr.writeln("]"); }
D
import std.stdio; void main(){ auto cin = new Cin(); auto N = cin.line()[0]; ((N+N*N)/2).writeln(); } auto solve(){ } unittest{ } 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 = size_t , string token = " " )( size_t m = 1 ){ T[][] arr = new T[][](m); foreach( i ; 0..m ){ arr[i] = this.read!T(token); } return arr; } private T[] read( T = size_t )( string token = " " ){ T[] arr; foreach( elm ; readln().chomp().split(token) ){ arr ~= elm.to!T(); } return arr; } }
D
void main() { auto X = ri; int now; if(X % 105 == 0) { writeln(1); return; } auto tmp = X % 10; if(tmp % 5 == tmp || tmp == 5) now += 100 + tmp; else now += 200 + tmp; debug now.writeln; tmp = X % 100 / 10 * 10; now += tmp / 5 * 100; debug now.writeln; if(now > X) { writeln(0); } else writeln(1); } // =================================== 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
void main(){ import std.stdio, std.string, std.conv, std.algorithm; import std.typecons; int n, m; rd(n, m); auto l=new int[](m), r=new int[](m), d=new int[](m); foreach(i; 0..m){ rd(l[i], r[i], d[i]); l[i]--; r[i]--; } alias T=Tuple!(int, "to", long, "d"); auto g=new T[][](n), rev=new T[][](n); foreach(i; 0..m){ g[l[i]]~=T(r[i], d[i]); rev[r[i]]~=T(l[i], d[i]); } auto a=new long[](n), vis=new bool[](n); long dmin=0, dmax=0; bool fun(int cur, int pre=-1, long p=0){ // writeln(cur); if(vis[cur]){ if(a[cur]!=p) return false; else return true; }else{ vis[cur]=true; a[cur]=p; dmin=min(dmin, p); dmax=max(dmax, p); } bool ret=true; foreach(e; g[cur]){ if(e.to==pre) continue; ret&=fun(e.to, cur, p+e.d); } foreach(e; rev[cur]){ if(e.to==pre) continue; ret&=fun(e.to, cur, p-e.d); } return ret; } foreach(i; 0..n)if(vis[i]==false){ // writeln(i); dmin=dmax=0; if(fun(i)==false){writeln("No"); return;} if(dmax-dmin>1_000_000_000){writeln("No"); return;} } writeln("Yes"); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } } void wr(T...)(T x){ import std.stdio; foreach(e; x) write(e, " "); writeln(); }
D
import std.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; long calc(int[] a, int[] b) { long sum = 0; for (int i = cast(int)a.length - 1; i >= 0; i--) { long ai = sum + a[i]; /* if (ai > b[i]) { long m = ai % b[i]; if (m > 0) { sum += b[i] - m; } } else { sum += b[i] - ai; } */ long x = b[i] - ai % b[i]; if (x < b[i]) sum += x; } return sum; } void main() { int n = readint; int[] a, b; for (int i = 0; i < n; i++) { auto ab = readints; a ~= ab[0]; b ~= ab[1]; } writeln(calc(a, b)); }
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; void main() { int a, b, c, d; scan(a, b, c, d); auto on = new int[](200); foreach (i ; a .. b) { on[i]++; } foreach (i ; c .. d) { on[i]++; } writeln(on.count!"a == 2"); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.algorithm, 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() { long n, m; readV(n, m); auto x = min(n, m/2); auto y = (m-x*2)/4; writeln(x+y); }
D
import std.stdio; import std.conv; import std.string; import std.algorithm; import std.range; import std.functional; import std.math; import core.bitop; void main() { auto i = readln.chomp.to!long(); if (i < 1200) { "ABC".writeln; } else if (i < 2800) { "ARC".writeln; } else { "AGC".writeln; } }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto x = readln.chomp.to!long; struct T { long t, y; } auto t = iota(0, x+1) .map!(t => T(t, t*(t+1)/2)) .assumeSorted!"a.y < b.y" .lowerBound(T(0, x)).back.t; writeln(t+1); }
D
import std.stdio; import std.string; import std.range; import std.conv; void main() { auto S = readln.chomp; switch(S) { case "abc", "acb", "bac", "bca", "cab", "cba": "Yes".writeln; break; default: "No".writeln; break; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nk = readln.split.to!(int[]); auto N = nk[0]; auto K = nk[1]; auto rsp = readln.split.to!(long[]); auto r = rsp[0]; auto s = rsp[1]; auto p = rsp[2]; auto T = readln.chomp; long add(int i) { switch (T[i]) { case 'r': return p; case 's': return r; case 'p': return s; default: } return 0; } long res; foreach (i; 0..K) { auto j = i; bool added; while (j < N) { if (j == i) { res += add(j); added = true; } else if (T[j-K] != T[j]) { res += add(j); added = true; } else if (!added) { res += add(j); added = true; } else { added = false; } j += K; } } writeln(res); }
D
#!/usr/bin/rdmd import std.stdio: stdin, lines, writeln; import std.ascii: newline; import std.conv: to; import std.array: split; void main() { int[2] a; foreach (string line; lines(stdin)) { if(line == newline) break; else { a = to!(int[])(split(line)); writeln((a[0]+a[1])); } } }
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 M = RD; writeln(48-M); stdout.flush(); debug readln(); }
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; long calc(int t, int[] ts) { long time = 0; long to = ts[0] + t; for (int i = 1; i < ts.length; i++) { if (ts[i] <= to) { time += ts[i] - ts[i-1]; } else { time += to - ts[i-1]; } to = ts[i] + t; } time += to - ts[$-1]; return time; } void main() { auto nt = readints; int t = nt[1]; auto ts = readints; writeln(calc(t, ts)); }
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; enum MAX = 1_000_100; ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias Pair = Tuple!(long, "a", long, "b"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long a, b; scan(a, b); auto t = gcd(a, b); writeln(a * b / t); } 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; } }
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 a, b, c, d; scan(a, b, c, d); writeln(abs(a-c) <= d || max(abs(a-b), abs(b-c)) <= d ? "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); } } }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array; void main() { writeln(readln.chomp.canFind('9') ? "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); } } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; long P = 10^^9+7; long[10^^5+50] F, RF; long pow(long x, long n) { long y = 1; while (n) { if (n%2 == 1) y = (y * x) % P; x = x^^2 % P; n /= 2; } return y; } long inv(long x) { return pow(x, P-2); } void init() { F[0] = F[1] = 1; foreach (i, ref x; F[2..$]) x = (F[i+1] * (i+2)) % P; { RF[$-1] = 1; auto x = F[$-1]; auto k = P-2; while (k) { if (k%2 == 1) RF[$-1] = (RF[$-1] * x) % P; x = x^^2 % P; k /= 2; } } foreach_reverse(i, ref x; RF[0..$-1]) x = (RF[i+1] * (i+1)) % P; } long comb(N)(N n, N k) { if (k > n) return 0; auto n_b = F[n]; // n! auto nk_b = RF[n-k]; // 1 / (n-k)! auto k_b = RF[k]; // 1 / k! auto nk_b_k_b = (nk_b * k_b) % P; // 1 / (n-k)!k! return (n_b * nk_b_k_b) % P; // n! / (n-k)!k! } void main() { init(); auto N = readln.chomp.to!long; auto xs = readln.split.to!(long[]); long r, x; foreach (i; 1..N) { auto d = xs[i] - xs[i-1]; (x += F[N-1] * inv(i)) %= P; (r += x * d) %= P; } writeln(r); }
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() { auto s = sread(); // s[0 .. (s.length - 1) / 2].writeln(); // s[(s.length + 3) / 2 - 1 .. $].writeln(); if (s.is_reversable() && s[0 .. (s.length - 1) / 2].is_reversable() && s[(s.length + 3) / 2 - 1 .. $].is_reversable()) writeln("Yes"); else writeln("No"); } bool is_reversable(string s) { bool ret = true; foreach (i; iota(s.length / 2)) { ret &= (s[i] == s[$ - 1 - i]); } return ret; } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.stdio, std.string, std.conv, std.range; void main() { auto N = readln.chomp.to!int; ulong power = 1; foreach (i; iota(1, N + 1)) { power *= i; power %= 1000000007; } power.writeln; }
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 qhsd = RDR.ARR; auto N = RD * 4; long[] cp = [qhsd[0]*8, qhsd[1]*4, qhsd[2]*2, qhsd[3]]; long ans; foreach_reverse (i; 0..4) { auto pos = MIN_POS(cp[0..i+1]); auto q = N / (2^^i); ans += q * ((2^^i) / (2^^pos)) * qhsd[pos]; N = N % (2^^i); } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto a = RD; auto b = RD; auto h = RD; writeln(min(a, b) * h + (max(a, b) - min(a, b)) * h / 2); 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 solve() { auto nx = readln.split.to!(int[]); auto N = nx[0]; auto X = nx[1]; int sum_a, infected; bool all_same = true; foreach (a; readln.split.to!(int[])) { if (a != X) all_same = false; if (a == X) ++infected; sum_a += a; } if (all_same) { writeln(0); } else if (X*N == sum_a) { writeln(1); } else if (infected == 0) { writeln(2); } else { writeln(1); } } void main() { auto T = readln.chomp.to!int; foreach (_; 0..T) { solve(); } }
D
import std.conv, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex, std.typecons; import core.bitop, core.thread; 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[0]; tokens.popFront; return token; } int readInt() { return readToken().to!int; } long readLong() { return readToken().to!long; } real readReal() { return readToken().to!real; } void chmin(T)(ref T t, in T f) { if (t > f) t = f; } void chmax(T)(ref T t, in T f) { if (t < f) t = f; } int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; } int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); } int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); } int N, K; int[] A; void main() { try { for (; ; ) { N = readInt(); K = readInt(); A = new int[N]; foreach (i; 0 .. N) { A[i] = readInt(); } auto dp = new bool[K + 1]; foreach (x; 0 .. K + 1) { foreach (i; 0 .. N) { if (x >= A[i] && !dp[x - A[i]]) { dp[x] = true; } } } writeln(dp[K] ? "First" : "Second"); } } catch (EOFException e) { } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; long ans = 1; long best; foreach (i; 1..N+1) { auto x = i; long cnt; while (x % 2 == 0) { x /= 2; ++cnt; } if (cnt > best) { best = cnt; ans = i; } } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N =RD; auto W = RDR.ARR; auto a = new long[](N+1); foreach (i; 0..N) { a[i+1] = a[i] + W[i]; } long ans = long.max; foreach (i; 0..N) { ans = min(ans, abs(a[N] - a[i] - a[i])); } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii; import std.typecons, std.functional; import std.algorithm, std.container; void main() { long N = scanElem; long[] list = scanArray; long count; long res; foreach(i; 0..list.length) { if(list[i]==i+1) count++; if(list[i]!=i+1){ res+=(count+1)/2; count=0; } } res+=(count+1)/2; writeln(res); } class UnionFind{ UnionFind parent = null; void merge(UnionFind a) { if(same(a)) return; a.root.parent = this.root; } UnionFind root() { if(parent is null)return this; return parent = parent.root; } bool same(UnionFind a) { return this.root == a.root; } } void scanValues(TList...)(ref TList list) { auto lit = readln.splitter; foreach (ref e; list) { e = lit.fornt.to!(typeof(e)); lit.popFront; } } T[] scanArray(T = long)() { return readln.split.to!(long[]); } void scanStructs(T)(ref T[] t, size_t n) { t.length = n; foreach (ref e; t) { auto line = readln.split; foreach (i, ref v; e.tupleof) { v = line[i].to!(typeof(v)); } } } 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 chie template :) {{{ import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons; // }}} // nep.scanner {{{ class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string : chomp; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.chomp.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 main() { auto cin = new Scanner; int n; cin.scan(n); auto ar = new long[][](2, n); long res = -1; ar[0] = cin.nextArray!long(n); ar[1] = cin.nextArray!long(n); foreach (i; 0 .. n) { res = max(res, ar[0][0 .. i + 1].sum + ar[1][i .. n].sum); } writeln(res); }
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; int[int] fac(int n) { assert(n > 1); int[int] ps; while (n % 2 == 0) { ps[2]++; n /= 2; } for (int i = 3; i * i <= n; i += 2) { while (n % i == 0) { ps[i]++; n /= i; } } if (n > 1) ps[n]++; return ps; } long calc(int n, int m) { if (m == 1) return 1; // if (n == 1) return 1; // import std.exception; // enforce(n > 1); long ans = 1; foreach (v; fac(m).values) { // v 個の素因数と n - 1 個の仕切りの並べ方 ans = (ans * nck(v + n - 1, n - 1)) % MOD; } return ans; } void main() { initFactTable(210000); int n, m; scan(n, m); writeln(calc(n, m)); } long[] fact; // 階乗 long[] factInv; // 階乗の逆元 void initFactTable(int n) { fact = new long[n + 1]; fact[0] = 1; for (int i = 1; i <= n; i++) fact[i] = i * fact[i - 1] % MOD; factInv = new long[n + 1]; factInv[n] = modPow(fact[n], MOD - 2, MOD); for (int i = n - 1; i >= 0; i--) factInv[i] = factInv[i + 1] * (i + 1) % MOD; } 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 nck(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return (fact[n] * factInv[k] % MOD) * factInv[n - k] % MOD; }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { int K, N; scan(K, N); auto a = readln.split.to!(int[]); long ans = infl; foreach (i ; 0 .. N) { long v; if (i < N - 1) { v = K - (a[i + 1] - a[i]); } else { v = K - (a[0] - a[N - 1] + K); } chmin(ans, v); } 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.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { int[] a; foreach (_; 0..5) a ~= readint; int k = readint; // 両端の差が k 以下か調べるだけでよい bool ans = a[$-1] - a[0] <= k; writeln(ans ? "Yay!" : ":("); }
D
import std.stdio, std.string, std.array, std.conv; long solve(long x, long y) { import std.numeric; long d = gcd(x, y); long[] factors = [1]; while (d % 2 == 0) { d /= 2; if (factors[$-1] != 2) factors ~= 2; } for (long i = 3; i * i <= d; i += 2) { while (d % i == 0) { d /= i; if (factors[$-1] != i) factors ~= i; } } if (d != 1) factors ~= d; return factors.length; } void main() { long[] tmp = readln.chomp.split.to!(long[]); long a = tmp[0], b = tmp[1]; solve(a, b).writeln; }
D