code stringlengths 4 1.01M | language stringclasses 2 values |
|---|---|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void main()
{
auto N = readln.chomp.to!int;
int c;
bool ok;
foreach (_; 0..N) {
auto ab = readln.split;
if (ab[0] == ab[1]) {
++c;
if (c >= 3) ok = true;
} else {
c = 0;
}
}
writeln(ok ? "Yes" : "No");
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
auto S = readln.chomp;
auto Q = readln.chomp.to!int;
auto ks = readln.split.to!(int[]);
auto ms = new long[](N);
auto cs = new long[](N);
size_t[] dis, cis;
long m, c;
foreach (i, s; S) {
if (s == 'M') ++m;
ms[i] = m;
if (s == 'D') {
dis ~= i;
}
if (s == 'C') {
cis ~= i;
c++;
}
cs[i] = c;
}
auto cms = new long[](N);
long last;
foreach (i, s; S) if (s == 'D' || s == 'C') {
if (s == 'C') last += ms[i];
cms[i] = last;
}
foreach (k; ks) {
long res;
size_t ci;
foreach (i; dis) {
while (ci < cis.length && cis[ci] < i) ++ci;
if (ci == cis.length) break;
if (cis[ci] - i >= k) continue;
size_t l = ci, r = cis.length-1;
if (cis[$-1] - i < k) {
l = r;
} else {
while (l+1 < r) {
m = (l+r)/2;
if (cis[m] - i >= k) {
r = m;
} else {
l = m;
}
}
}
auto j = cis[l];
res += cms[j] - cms[i] - ms[i] * (cs[j] - cs[i]);
}
writeln(res);
}
} | D |
void main()
{
long n = rdElem;
long[] a = rdRow;
bool ok = true;
foreach (x; a)
{
if (!(x & 1))
{
if (x % 3 != 0 && x % 5 != 0) ok = false;
}
}
writeln(ok ? "APPROVED" : "DENIED");
}
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.conv, std.range, std.stdio, std.string;
import std.bitmanip; // BitArray
void main()
{
auto n = readln.chomp.to!size_t;
auto s = new int[](n);
foreach (i; 0..n) s[i] = readln.chomp.to!int;
auto t = s.sum;
auto dp = BitArray();
dp.length = t+1;
dp[0] = true;
foreach (i; 0..n+1) {
auto dp2 = dp.dup;
dp2.lshift(s[i]);
dp |= dp2;
}
foreach_reverse (i; 0..t+1)
if (dp[i] && i % 10 != 0) {
writeln(i);
return;
}
writeln(0);
}
auto lshift(ref BitArray ba, size_t n)
{
if (n % 64 == 0) {
if (n > 0) {
ba <<= 1;
ba <<= n-1;
}
} else {
ba <<= n;
}
}
| D |
import std.stdio, std.algorithm, std.array, std.ascii;
int main() {
char a = scanChar();
if('a'<=a && a<='z') {
writeln("a");
} else {
writeln("A");
}
return 0;
}
char scanChar() {
import std.ascii;
int c = ' ';
while(isWhite(c) && c != -1) {
c = getchar;
}
return cast(char)c;
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto K = RD;
writeln(K * (K-1)^^(N-1));
stdout.flush();
debug readln();
} | D |
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
auto s = sread();
long n = s.filter!(c => c == 'x')
.map!(x => 100)
.sum();
writeln(1000 - n);
}
| D |
import std.stdio, std.string, std.conv, std.range, std.algorithm;
void main() {
auto w = readln.chomp;
int[char] char_count;
foreach (c; w) {
char_count[c]++;
}
if (char_count.values.all!"a % 2 == 0") {
"Yes".writeln;
} else {
"No".writeln;
}
} | D |
import std.stdio, std.conv, std.string, std.math, std.regex, std.range, std.ascii, std.algorithm;
void main(){
auto ip = readln.split.to!(int[]), A=ip[0], B=ip[1], T=ip[2];
writeln(T/A*B);
} | D |
// import chie template :) {{{
static if (__VERSION__ < 2090) {
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons, std.format,
std.bitmanip, std.numeric;
} else {
import std;
}
// }}}
// nep.scanner {{{
class Scanner {
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) {
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next() {
if (idx < str.length) {
return str[idx++];
}
char[] s;
while (s.length == 0) {
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(size_t len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
void scan(T...)(ref T args) {
foreach (ref arg; args) {
arg = next!(typeof(arg));
}
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType)) {
str ~= s.to!(char[]).strip.split;
}
}
// }}}
// alias {{{
alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less);
alias MinHeap(T) = Heap!(T, "a > b");
// }}}
// memo {{{
/*
- ある値が見つかるかどうか
<https://dlang.org/phobos/std_algorithm_searching.html#canFind>
canFind(r, value); -> bool
- 条件に一致するやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#filter>
// 2で割り切れるやつ
filter!"a % 2 == 0"(r); -> Range
- 合計
<https://dlang.org/phobos/std_algorithm_iteration.html#sum>
sum(r);
- 累積和
<https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold>
// 今の要素に前の要素を足すタイプの一般的な累積和
// 累積和のrangeが帰ってくる(破壊的変更は行われない)
cumulativeFold!"a + b"(r, 0); -> Range
- rangeをarrayにしたいとき
array(r); -> Array
- 各要素に同じ処理をする
<https://dlang.org/phobos/std_algorithm_iteration.html#map>
// 各要素を2乗
map!"a * a"(r) -> Range
- ユニークなやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#uniq>
uniq(r) -> Range
- 順列を列挙する
<https://dlang.org/phobos/std_algorithm_iteration.html#permutations>
permutation(r) -> Range
- ある値で埋める
<https://dlang.org/phobos/std_algorithm_mutation.html#fill>
fill(r, val); -> void
- バイナリヒープ
<https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap>
// 昇順にするならこう(デフォは降順)
BinaryHeap!(Array!T, "a > b") heap;
heap.insert(val);
heap.front;
heap.removeFront();
- 浮動小数点の少数部の桁数設定
// 12桁
writefln("%.12f", val);
- 浮動小数点の誤差を考慮した比較
<https://dlang.org/phobos/std_math.html#.approxEqual>
approxEqual(1.0, 1.0099); -> true
- 小数点切り上げ
<https://dlang.org/phobos/std_math.html#.ceil>
ceil(123.4); -> 124
- 小数点切り捨て
<https://dlang.org/phobos/std_math.html#.floor>
floor(123.4) -> 123
- 小数点四捨五入
<https://dlang.org/phobos/std_math.html#.round>
round(4.5) -> 5
round(5.4) -> 5
*/
// }}}
void main() {
auto cin = new Scanner;
int x, n;
cin.scan(x, n);
bool[102] c;
foreach (i; 0 .. n) {
c[cin.next!int] = true;
}
int mini;
int len;
foreach (i; x .. 102) {
if (!c[i]) {
mini = i;
len = i - x;
break;
}
}
foreach_reverse (i; 0 .. x + 1) {
if (!c[i]) {
if (len >= x - i) {
mini = i;
}
break;
}
}
writeln(mini);
}
| D |
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
auto n = sread();
// writeln(n);
long sum_n;
foreach (i; 0 .. (n.length))
{
sum_n += n[i] - '0';
}
// writeln(sum_n);
auto tmp = new long[](n.length);
auto long_n = n.to!long();
// writeln(long_n / (10 ^^ ((n.length) - 1)) - 1);
tmp[0] = long_n / (10 ^^ ((n.length) - 1)) - 1;
foreach (i; 1 .. (n.length))
{
tmp[i] = 9;
}
// writeln(tmp);
// writeln(tmp.sum());
writeln(max(sum(tmp), sum_n));
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
| D |
import std.stdio;
import std.conv;
import std.string;
void main(string[] args){
while(1){
int n = to!int(readln.chomp);
if(n == 0) break;
int S = 0;
int Smin = 0;
int Smax = int.min;
int dmax = int.min;
for(int i = 0; i < n; i ++){
S += to!int(readln.chomp);
if(S > Smax) Smax = S;
if(Smax - Smin > dmax) dmax = Smax - Smin;
if(S < Smin) Smin = S, Smax = int.min;
}
writeln(dmax);
}
} | D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
void main() {
int n;
scan(n);
auto a = iota(n).map!(i => readln.chomp.to!int).array;
long ans;
foreach_reverse (i ; 0 .. n) {
if (a[i] > i) {
writeln(-1);
return;
}
if (i == n - 1) {
ans += a[i];
continue;
}
if (a[i] == a[i+1] - 1) {
continue;
}
else if (a[i+1] - a[i] > 1) {
writeln(-1);
return;
}
else {
ans += a[i];
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
| D |
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
auto dp = new long[][](100 + 1, (10 ^^ 5) * 2 + 1);
long N, W;
scan(N, W);
auto w = new long[](100 + 1);
auto v = new long[](100 + 1);
foreach (i; 0 .. N)
scan(w[i], v[i]);
dp[0][] = long.max / 2;
dp[0][0] = 0;
foreach (n; 0 .. N)
{
dp[n + 1][] = long.max / 2;
foreach (val; 0 .. 10 ^^ 5 + 1)
{
dp[n + 1][val].minAssign(dp[n][val]);
// writeln(v[n]);
dp[n + 1][val + v[n]].minAssign(dp[n][val] + w[n]);
}
}
foreach_reverse (i, val; dp[N])
{
if (val <= W)
{
writeln(i);
return;
}
}
}
| D |
import std.stdio;
import std.string;
import std.range;
import std.conv;
void main()
{
auto ip = readln.split.to!(int[]), X = ip[0], A = ip[1], B = ip[2];
if(A >= B) {
writeln("delicious");
} else {
if(B-A > X) {
writeln("dangerous");
} else {
writeln("safe");
}
}
}
| D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm: canFind;
void main() {
string[] inputs = split(readln());
int a = to!int(inputs[0]);
int b = to!int(inputs[1]);
int c = to!int(inputs[2]);
if(b - a == c - b) "YES".writeln;
else "NO".writeln;
}
| D |
import std.stdio,std.string,std.conv;
void main(){
auto n = to!int(readln().chomp());
for( int i=0; i<n ; i++ ){
auto arr = readln().chomp().split();
double[] arr2;
arr2.length = arr.length;
for( int j=0; j<arr.length ; j++ ){
arr2[j] = to!double( arr[j] );
}
if( (arr2[3]-arr2[1])/(arr2[2]-arr2[0]) == (arr2[7]-arr2[5])/(arr2[6]-arr2[4]) ){
writeln("YES");
}else{
writeln("NO");
}
}
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
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 L = RD;
auto R = RD;
auto d = RD;
long ans;
foreach (i; L..R+1)
{
if (i % d == 0)
++ans;
}
writeln(ans);
stdout.flush;
debug readln;
} | D |
import std.algorithm,
std.string,
std.array,
std.stdio,
std.range,
std.conv;
long abs(long x) {
return x * (x < 0 ? -1 : 1);
}
void main() {
long N = readln.chomp.to!long;
long[] a = readln.chomp.split.to!(long[]);
long ans = long.max;
long x, y;
for (size_t i = 0; i < N - 1; i++) {
if (i == 0) {
x = a[i];
y = a[1..$].sum;
} else {
x += a[i];
y -= a[i];
}
long z = abs(x - y);
if (z < ans) {
ans = z;
}
}
writeln(ans);
}
| D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main() {
string[] inputs = split(readln());
int A = to!int(inputs[0]);
int B = to!int(inputs[1]);
int C = to!int(inputs[2]);
int D = to!int(inputs[3]);
max(A*B, C*D).writeln;
}
| D |
import std.stdio;
import std.conv;
import std.string;
import std.math;
import std.random;
import std.range;
import std.algorithm;
import std.functional;
import core.bitop;
import std.bigint;
import std.typecons;
import std.container;
void main()
{
auto input = readln.split.to!(ulong[]);
auto X = input[0], Y = input[1];
int result;
for (ulong cur = X; cur <= Y; cur *= 2)
result++;
result.writeln;
} | D |
import std.stdio, std.string, std.array, std.algorithm, std.conv, std.typecons, std.numeric, std.math;
struct SegTree(alias _fun, alias def, T)
if (is(typeof(def) : T))
{
import std.functional : binaryFun;
alias fun = binaryFun!_fun;
///
this(size_t n, T[] ts) {
this.n = 1;
while (this.n < n) this.n *= 2;
this.tree.length = this.n * 2 - 1;
foreach (ref e; this.tree) e = def;
foreach (i, e; ts) this.put(i, e);
}
///
void put(size_t i, T e) {
i += this.n - 1;
this.tree[i] = e;
while (i > 0) {
i = (i-1) / 2;
this.tree[i] = fun(this.tree[i*2+1], this.tree[i*2+2]);
}
}
///
T query(size_t a, size_t b) {
T impl(size_t i, size_t l, size_t r) {
if (r <= a || b <= l) return def;
if (a <= l && r <= b) return this.tree[i];
return fun(
impl(i*2+1, l, (l+r)/2),
impl(i*2+2, (l+r)/2, r)
);
}
return impl(0, 0, this.n);
}
private:
size_t n;
T[] tree;
}
///
SegTree!(f, def, T) seg_tree(alias f, alias def, T)(size_t n, T[] arr = [])
{
return SegTree!(f, def, T)(n, arr);
}
///
SegTree!(f, def, T) seg_tree(alias f, alias def, T)(T[] arr)
{
return SegTree!(f, def, T)(arr.length, arr);
}
void main()
{
auto nq = readln.split.to!(int[]);
auto N = nq[0];
auto Q = nq[1];
auto segt = seg_tree!("a + b", 0, int)(N+1);
foreach (_; 0..Q) {
auto cxy = readln.split.to!(int[]);
auto c = cxy[0];
auto x = cxy[1];
auto y = cxy[2];
if (c == 0) {
auto o = segt.query(x, x+1);
segt.put(x, o+y);
} else {
writeln(segt.query(x, y+1));
}
}
}
| D |
import std.array;
import std.conv;
import std.math;
import std.algorithm;
import std.string;
import std.stdio;
void main() {
int n = readln.chomp.to!int;
auto steps = readln.chomp.split(' ').map!(to!int).array;
auto dp = new int[n];
dp[1] = abs(steps[0] - steps[1]);
foreach (i;2..n) {
dp[i] = min(
dp[i - 2] + abs(steps[i - 2] - steps[i]),
dp[i - 1] + abs(steps[i - 1] - steps[i]));
}
writeln(dp[n - 1]);
}
| D |
import std.stdio, std.array, std.conv, std.typecons, std.algorithm;
T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; }
void main() {
immutable input = readln.split.to!(ulong[]);
immutable r = input[0], d = input[1], x2k = input[2];
ulong x = x2k;
for(ulong i = 0; i < 10; i++) {
x = r * x - d;
writeln(x);
}
}
| D |
import std.stdio;
import std.string;
import std.conv;
int main()
{
int a = readln().chomp().to!int();
a = a * a * a;
writeln(a);
return 0;
} | 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 HS = readln.split.to!(long[]);
HS ~= 0;
long[] hc;
auto DP = new long[][](N, K+1);
long solve(int i, int k) {
if (i == N) return 0;
if (DP[i][k] == 0) {
long r = long.max;
foreach (d; 1..k+2) {
if (i+d > N) break;
r = min(r, max(0, HS[i] - HS[i+d]) + solve(i+d, k-d+1));
}
DP[i][k] = r;
}
return DP[i][k];
}
long r = long.max;
foreach (i; 0..min(N+1, K+1)) {
r = min(r, solve(i, K-i));
}
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()
{
long n, x, y;
scan(n, x, y);
x--, y--;
auto cost = new long[][](n, n);
foreach (i; iota(n))
{
foreach (j; iota(i + 1, n))
{
cost[i][j] = min(j - i, abs(j - y) + abs(i - x) + 1);
}
}
auto ans = new long[](n);
foreach (i; iota(n))
{
foreach (j; iota(i + 1, n))
{
ans[cost[i][j]]++;
}
}
foreach (e; ans[1 .. $])
{
e.writeln();
}
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
| D |
void main()
{
int[] tmp = readln.split.to!(int[]);
int n = tmp[0], a = tmp[1], b = tmp[2];
string s = readln.chomp;
int pass, oversea;
foreach (c; s)
{
if (c == 'a' && pass < a + b)
{
"Yes".writeln;
++pass;
}
else if (c == 'b' && pass < a + b && oversea < b)
{
"Yes".writeln;
++pass;
++oversea;
}
else
{
"No".writeln;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni; | D |
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto xt = readln.chomp.split.map!(to!int);
writeln(max(xt[0]-xt[1],0));
}
| 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 a = read.to!int;
int b = read.to!int;
if(a >= 13) b.writeln;
else if(a >= 6) (b / 2).writeln;
else 0.writeln;
}
| D |
/+ dub.sdl:
name "D"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv, std.math;
// import dcomp.foundation, dcomp.scanner;
int main() {
auto sc = new Scanner(stdin);
int n;
sc.read(n);
int[][] g = new int[][](n);
foreach (i; 0..n-1) {
int a, b;
sc.read(a, b); a--; b--;
g[a] ~= b; g[b] ~= a;
}
int[] dist;
void dfs(int p, int b, int di) {
dist[p] = di;
foreach (d; g[p]) {
if (d == b) continue;
dfs(d, p, di+1);
}
}
int[] dist0 = new int[n]; dist = dist0;
dfs(0, -1, 0);
int[] dist1 = new int[n]; dist = dist1;
dfs(n-1, -1, 0);
auto s0 = iota(n).filter!(i => dist0[i] <= dist1[i]).count;
auto s1 = n - s0;
if (s0 > s1) {
writeln("Fennec");
} else {
writeln("Snuke");
}
return 0;
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
if (f.eof) return false;
line = lineBuf[];
f.readln(line);
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else {
auto buf = line.split.map!(to!E).array;
static if (isStaticArray!T) {
assert(buf.length == T.length);
}
x = buf;
line.length = 0;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
static if (__VERSION__ <= 2070) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
version (X86) static if (__VERSION__ < 2071) {
import core.bitop : bsf, bsr, popcnt;
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
| D |
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop;
// dfmt on
void main()
{
auto S = aryread!string();
S.map!"a[0]".array().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 N = RD;
auto A = RD;
auto B = RD;
writeln(min(N * A, B));
stdout.flush();
debug readln();
} | D |
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } bool DEBUG = 0;
void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); }
string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
long d = scan!long, t = scan!long, s = scan!long;
if(t * s >= d) "Yes".writeln;
else "No".writeln;
}
| D |
import std.stdio;
import std.string;
import std.conv;
void main() {
for(;;) {
// input
int R, C;
{
auto temp = readln.split;
R = to!int(temp[0]);
C = to!int(temp[1]);
}
if(R == 0 && C == 0) {
break;
}
bool[][] board;
board.length = R;
for(int i=0; i<R; ++i) {
board[i].length = C;
}
for(int i=0; i<R; ++i) {
auto temp = readln.split;
for(int j=0; j<C; ++j) {
board[i][j] = cast(bool) to!int(temp[j]);
}
}
// calc
int ret = 0;
int rowsEnd = 1 << R;
for(int rInt=0; rInt<rowsEnd; ++rInt) {
int val;
for(int i=0; i<C; ++i) {
int temp;
for(int j=0; j<R; ++j) {
bool r = cast(bool) (rInt & (1 << j));
if(r != board[j][i]) {
++temp;
}
}
temp = temp > R - temp ? temp : R - temp;
val += temp;
}
if(ret < val) {
ret = val;
}
}
writeln(ret);
}
} | D |
import std.stdio;
import std.array;
import std.math;
import std.conv;
import std.string;
void main() {
string[] input = split(chomp(readln));
int x = to!int(input[0]);
int y = to!int(input[1]);
writeln((x*y)," ",(x*2 + y*2));
} | D |
import std.stdio;
import std.string;
import std.conv;
void main(){
int[101] counter;
string rc;
int max;
while( 1 ){
rc = readln().chomp();
if( !rc ){ break; }
counter[ to!int( rc ) ]++;
if( max < counter[ to!int( rc ) ] ){
max = counter[ to!int( rc ) ];
}
}
foreach( i,c ; counter ){
if( c == max ){
writeln( i );
}
}
} | D |
/+ dub.sdl:
name "C"
dependency "dcomp" version=">=0.7.3"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.graph.bridge;
int main() {
Scanner sc = new Scanner(stdin);
int n, m;
sc.read(n, m);
struct E {int to;}
E[][] g = new E[][n];
foreach (i; 0..m) {
int a, b;
sc.read(a, b);
a--; b--;
g[a] ~= E(b);
g[b] ~= E(a);
}
auto info = bridge(g);
// writeln(info);
writeln(info.isRoot.count!"a" - 1);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
version (X86) static if (__VERSION__ < 2071) {
import core.bitop : bsf, bsr, popcnt;
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/graph/dfstree.d */
// module dcomp.graph.dfstree;
struct DFSTreeInfo {
int[] low, ord, par, vlis;
int[][] tr;
this(int n) {
low = new int[n];
ord = new int[n];
par = new int[n];
vlis = new int[n];
tr = new int[][](n);
}
}
DFSTreeInfo dfsTree(T)(T g) {
import std.algorithm : min, each, filter;
import std.conv : to;
const int n = g.length.to!int;
auto info = DFSTreeInfo(n);
with(info) {
int co = 0;
bool[] used = new bool[](n);
void dfs(int p, int b) {
used[p] = true;
low[p] = ord[p] = co++;
par[p] = b;
bool rt = true;
foreach (e; g[p]) {
int d = e.to;
if (rt && d == b) {
rt = false;
continue;
}
if (!used[d]) {
dfs(d, p);
low[p] = min(low[p], low[d]);
} else {
low[p] = min(low[p], ord[d]);
}
}
}
foreach (i; 0..n) {
if (used[i]) continue;
dfs(i, -1);
}
par.filter!"a!=-1".each!((i, v) => tr[v] ~= i.to!int);
ord.each!((i, v) => vlis[v] = i.to!int);
}
return info;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
// import dcomp.array;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
FastAppender!(E[]) buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/graph/bridge.d */
// module dcomp.graph.bridge;
// import dcomp.graph.dfstree;
struct BridgeInfo {
bool[] isRoot;
int count;
int[] id, root;
this(int n) {
isRoot = new bool[n];
id = new int[n];
}
}
BridgeInfo bridge(T)(T g) {
return bridge(g, dfsTree(g));
}
BridgeInfo bridge(T)(T g, DFSTreeInfo info) {
import std.conv : to;
int n = g.length.to!int;
auto br = BridgeInfo(n);
with (br) with (info) {
foreach (p; vlis) {
isRoot[p] = (low[p] == ord[p]);
if (isRoot[p]) {
id[p] = count++;
root ~= ((par[p] == -1) ? -1 : id[par[p]]);
} else {
id[p] = id[par[p]];
}
}
}
return br;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/array.d */
// module dcomp.array;
T[N] fixed(T, size_t N)(T[N] a) {return a;}
struct FastAppender(A, size_t MIN = 4) {
import std.algorithm : max;
import std.conv;
import std.range.primitives : ElementEncodingType;
import core.stdc.string : memcpy;
private alias T = ElementEncodingType!A;
private T* _data;
private uint len, cap;
@property size_t length() const {return len;}
bool empty() const { return len == 0; }
void reserve(size_t nlen) {
import core.memory : GC;
if (nlen <= cap) return;
void* nx = GC.malloc(nlen * T.sizeof);
cap = nlen.to!uint;
if (len) memcpy(nx, _data, len * T.sizeof);
_data = cast(T*)(nx);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void opOpAssign(string op : "~")(T item) {
if (len == cap) {
reserve(max(MIN, cap*2));
}
_data[len++] = item;
}
void insertBack(T item) {
this ~= item;
}
void removeBack() {
len--;
}
void clear() {
len = 0;
}
ref inout(T) back() inout { assert(len); return _data[len-1]; }
ref inout(T) opIndex(size_t i) inout { return _data[i]; }
T[] data() {
return (_data) ? _data[0..len] : null;
}
}
/*
This source code generated by dcomp and include dcomp's source code.
dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp)
dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt)
*/
| D |
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto x = readln.chomp.split.to!(int[]);
int cnt;
foreach (i; 0..x[0]) {
auto y = readln.chomp.split.to!(int[]);
if (x[1] <= y[0] && x[2] <= y[1]) {
cnt++;
}
}
cnt.writeln;
}
| D |
import std.stdio;
import std.conv;
import std.array;
import std.algorithm;
struct Item {
int w;
int v;
}
void main()
{
auto buf = readln.split;
int N = buf[0].to!int;
int W = buf[1].to!int;
Item[] items = new Item[N];
foreach (i; 0..N){
buf = readln.split;
items[i].w = buf[0].to!int;
items[i].v = buf[1].to!int;
}
long[] sum_v = new long[W + 1];
foreach (i; 0..N){
int v = items[i].v;
int w = items[i].w;
for (uint j = W; j > 0; j--){
if (j < items[i].w) break;
long sum_i = sum_v[j - w] + v;
if (sum_i > sum_v[j]) sum_v[j] = sum_i;
}
}
writeln(sum_v[W]);
}
| D |
import std.string,
std.stdio,
std.conv;
void main(){
while(true){
string s = readln.chomp;
if (s == "-") break;
int n = readln.chomp.to!int;
ulong sx=0;
foreach(i;0..n){
sx+=readln.chomp.to!int;
}
sx = sx % s.length;
writeln(s[sx..$] ~ s[0..sx]);
}
} | D |
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
int n, m;
scan(n, m);
auto adj = new Edge[][](n, 0);
foreach (i ; 0 .. m) {
int ui, vi, ci;
scan(ui, vi, ci);
adj[ui] ~= Edge(vi, ci, adj[vi].length.to!int);
adj[vi] ~= Edge(ui, 0, adj[ui].length.to!int - 1);
}
int ans = FordFulkerson(adj, 0, n - 1);
writeln(ans);
}
alias Edge = Tuple!(int, "to", int, "cap", int, "rev");
immutable inf = 10^^9 + 7;
int FordFulkerson(Edge[][] adj, int s, int t) {
int n = adj.length.to!int;
auto visited = new bool[](n);
int dfs(int u, int f) {
if (u == t) {return f;}
visited[u] = 1;
foreach (ref e ; adj[u]) {
if (!visited[e.to] && e.cap > 0) {
int d = dfs(e.to, min(f, e.cap));
if (d > 0) {
e.cap -= d;
adj[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
int F;
while (true) {
visited[] = 0;
int f = dfs(0, inf);
if (!f) break;
F += f;
}
return F;
}
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 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()
{
auto S = sread();
long N = S.length;
long X, Y;
scan(X, Y);
auto ls = new long[][](2, 1);
{
long s;
foreach (i; 0 .. N)
{
if (S[i] == 'T')
{
ls[s] ~= 0;
s = s ^ 1;
}
if (S[i] == 'F')
{
ls[s][$ - 1]++;
}
}
}
{
bool[long] D;
D[ls[0][0]] = true;
foreach (j; ls[0][1 .. $])
{
bool[long] D2;
foreach (k; D.byKey)
{
D2[k + j] = D2[k - j] = true;
}
D = D2;
}
if (X !in D)
{
writeln("No");
return;
}
}
{
bool[long] D;
D[0] = true;
foreach (j; ls[1])
{
bool[long] D2;
foreach (k; D.byKey)
{
D2[k + j] = D2[k - j] = true;
}
D = D2;
}
if (Y !in D)
{
writeln("No");
return;
}
}
writeln("Yes");
}
| D |
module app;
import std.stdio;
import std.algorithm;
import std.range;
import std.array;
import std.string;
import std.conv;
void main(string[] args) {
// 一行目の入力を捨てる
readln();
int[][] lines = iota(0,2).map!((Unused) => readln.chop.split.map!(to!int).array).array;
auto candies = iota(0,lines[0].length).map!((x) => sum(lines[0][0..x+1]) + sum(lines[1][x..$]));
int max_score = 0;
candies.each!((num){
if(num > max_score){
max_score = num;
}
});
writeln(max_score);
} | D |
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto s = readln.chomp;
int cnt;
foreach (i; 0..s.length/2) {
if (s[i] != s[$-i-1]) {
cnt++;
}
}
cnt.writeln;
}
| D |
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
void main ()
{
string s;
while ((s = readln.strip) != "")
{
bool have0 = false;
bool have1 = false;
foreach (ref c; s)
{
if (c == '0')
{
writeln (have0 ? 1 : 3, " ", 1);
have0 ^= true;
}
else
{
writeln (1, " ", have1 ? 1 : 3);
have1 ^= true;
}
}
}
}
| D |
void main()
{
long n = rdElem;
string s = rdStr;
long result;
foreach (i; 0 .. n)
{
long[] z_arr = s[i..$].z;
foreach (j; 0 .. n-i)
{
if (j >= z_arr[j]) result = max(result, z_arr[j]);
}
}
result.writeln;
}
long[] z(string s)
{
long len = s.length;
long[] result = new long[len];
result[0] = len;
long i = 1, j;
while (i < len)
{
while (i + j < len && s[j] == s[i+j]) ++j;
result[i] = j;
if (j == 0)
{
++i;
continue;
}
long k = 1;
while (i + k < len && k + result[k] < j)
{
result[i+k] = result[k];
++k;
}
i += k;
j -= k;
}
return result;
}
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 core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
ulong bignum = 1_000_000_007;
alias Pair = Tuple!(long, "number", long, "times");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
auto s = sread();
auto t = sread();
auto dif = max(s.length - t.length, 0);
foreach_reverse (i; 0 .. dif + 1)
{
auto parts = s[i .. t.length + i];
if (parts.is_match(t))
{
ans_write(s, t, i);
return;
}
}
writeln("UNRESTORABLE");
}
bool is_match(T)(T a, T b)
{
foreach (i, e; a)
{
if (!(e == b[i] || e == '?'))
return false;
}
return true;
}
void ans_write(string s, string t, long p)
{
auto s_before = s[0 .. p];
auto s_after = s[p + t.length .. $];
foreach (e; s_before ~ t ~ s_after)
{
if(e == '?')
write('a');
else
e.write();
}
writeln();
}
| D |
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
writeln(-lread() + (2 * lread));
}
| D |
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii;
import std.typecons, std.functional, std.traits;
import std.algorithm, std.container;
void main()
{
long N = scanElem;
string S = readln.strip;
long K = scanElem;
foreach(c; S)
{
if(S[K-1]==c)write(c);else write('*');
}
writeln("");
}
class UnionFind{
UnionFind parent = null;
void merge(UnionFind a)
{
if(same(a)) return;
a.root.parent = this.root;
}
UnionFind root()
{
if(parent is null)return this;
return parent = parent.root;
}
bool same(UnionFind a)
{
return this.root == a.root;
}
}
void scanValues(TList...)(ref TList list)
{
auto lit = readln.splitter;
foreach (ref e; list)
{
e = lit.fornt.to!(typeof(e));
lit.popFront;
}
}
T[] scanArray(T = long)()
{
return readln.split.to!(long[]);
}
void scanStructs(T)(ref T[] t, size_t n)
{
t.length = n;
foreach (ref e; t)
{
auto line = readln.split;
foreach (i, ref v; e.tupleof)
{
v = line[i].to!(typeof(v));
}
}
}
long scanULong(){
long x;
while(true){
const c = getchar;
if(c<'0'||c>'9'){
break;
}
x = x*10+c-'0';
}
return x;
}
T scanElem(T = long)()
{
char[] res;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
res ~= cast(char) c;
c = getchar;
}
return res.strip.to!T;
}
template fold(fun...) if (fun.length >= 1)
{
auto fold(R, S...)(R r, S seed)
{
static if (S.length < 2)
{
return reduce!fun(seed, r);
}
else
{
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
struct Factor
{
long n;
long c;
}
Factor[] factors(long n)
{
Factor[] res;
for (long i = 2; i ^^ 2 <= n; i++)
{
if (n % i != 0)
continue;
int c;
while (n % i == 0)
{
n = n / i;
c++;
}
res ~= Factor(i, c);
}
if (n != 1)
res ~= Factor(n, 1);
return res;
}
long[] primes(long n)
{
if(n<2)return [];
auto table = new long[n+1];
long[] res;
for(int i = 2;i<=n;i++)
{
if(table[i]==-1) continue;
for(int a = i;a<table.length;a+=i)
{
table[a] = -1;
}
res ~= i;
}
return res;
}
bool isPrime(long n)
{
if (n <= 1)
return false;
if (n == 2)
return true;
if (n % 2 == 0)
return false;
for (long i = 3; i ^^ 2 <= n; i += 2)
if (n % i == 0)
return false;
return true;
} | D |
// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
import std.datetime, std.bigint;
void main() {
long n, k;
scan(n, k);
long x = n / k;
writeln(x & 1 ? "YES" : "NO");
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
} | 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() {
readln;
readln.chomp.group.count.writeln;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
| D |
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int s; readV(s);
auto b = new bool[](1000001);
b[s] = true;
foreach (i; 2..1000002) {
s = s%2 == 0 ? s/2 : s*3+1;
if (b[s]) {
writeln(i);
return;
}
b[s] = true;
}
}
| D |
import std.stdio;
import core.stdc.stdio;
import std.algorithm;
import std.math;
import core.stdc.string;
char[] A;
char[] B;
class Arr{
int as,bs,cs,ds;
void Set(int a,int b,int c,int d){
as = a;
bs = b;
cs = c;
ds = d;
data = new int[a*b*c*d];
}
this(){
Set(500,10,2,3);
}
int[] data;
ref int opIndex(int a,int b,int c,int d){
return data[(((a)*bs+b)*cs+c)*ds+d];
}
void Clear(){
data[] = 0;
}
}
Arr now;
Arr next;
static immutable modBase = 10000;
void Minus(char[] d,int idx){
if(d[idx] == 0){
d[idx] = 9;
d.Minus(idx-1);
}else{
d[idx]--;
}
}
int Count(char[] d,int m,bool minus){
int len = cast(int)strlen(d.ptr);
for(int i=0;i<len;i++){
d[i] -= '0';
}
if(minus)
d[0..len].Minus(len-1);
now.Clear;
int md=1;
int res=0;
foreach_reverse(_,c;d[0..len]){
next.Clear;
for(int i=0;i<m;i++){
for(int j=0;j<10;j++){
if(_ == len-1){
if(i==0){
next[j%m,j,j<=c?0:1,2] = 1;
}
}else{
for(int k=0;k<10;k++){
if(j==k) continue;
int t = 0;
int s = 1;
if(j > k) swap(s,t);
if(j<c){
next[(i+md*j)%m,j,0,t] += now[i,k,0,s] + now[i,k,1,s] + now[i,k,0,2] + now[i,k,1,2];
next[(i+md*j)%m,j,0,t] %= modBase;
}else if(j==c){
next[(i+md*j)%m,j,0,t] += now[i,k,0,s] + now[i,k,0,2];
next[(i+md*j)%m,j,1,t] += now[i,k,1,s] + now[i,k,1,2];
next[(i+md*j)%m,j,0,t] %= modBase;
next[(i+md*j)%m,j,1,t] %= modBase;
}else{
next[(i+md*j)%m,j,1,t] += now[i,k,0,s] + now[i,k,1,s] + now[i,k,0,2] + now[i,k,1,2];
next[(i+md*j)%m,j,1,t] %= modBase;
}
}
}
}
}
swap(now,next);
for(int i=1;i<10;i++){
res += now[0,i,0,0] + now[0,i,0,1] + now[0,i,0,2];
if(_>0)
res += now[0,i,1,0] + now[0,i,1,1] + now[0,i,1,2];
res %= modBase;
}
md *= 10;
md %= m;
}
return res;
}
void main(){
A = new char[514];
B = new char[514];
scanf("%s",A.ptr);
scanf("%s",B.ptr);
int m;
scanf("%d",&m);
now = new Arr;
next = new Arr;
int ac = A.Count(m,true);
int bc = B.Count(m,false);
printf("%d\n",(bc+modBase-ac)%modBase);
} | D |
import std.stdio;
import std.string;
import std.conv;
import std.math;
void main() {
int N = to!int(chomp(readln()));
int A = to!int(chomp(readln()));
writeln(pow(N, 2) - A);
}
| D |
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.complex;
import std.container;
import std.conv;
import std.datetime;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
auto readInts() {
return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
return readInts()[0];
}
auto readLongs() {
return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
return readLongs()[0];
}
const real eps = 1e-10;
void main(){
while(true) {
auto l = readInts();
foreach(li; l) {
if(li < 0) {
return;
}
}
auto d1 = Date(l[0], l[1], l[2]);
auto d2 = Date(l[3], l[4], l[5]);
writeln((d2-d1).total!"days");
}
} | D |
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
auto S = sread();
auto T = sread();
foreach (i; 0 .. S.length)
{
if (S[i .. $] ~ S[0 .. i] == T)
{
writeln("Yes");
return;
}
}
writeln("No");
}
| D |
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
const N = readln.chomp.to!long;
const as = readln.split.to!(long[]);
long cnt;
long ss, xs;
long r;
foreach (l; 0..N) {
while (r < N && ss + as[r] == (xs ^ as[r])) {
ss += as[r];
xs ^= as[r];
r++;
}
cnt += r-l;
ss -= as[l];
xs ^= as[l];
}
writeln(cnt);
}
| D |
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int a, b, k; readV(a, b, k);
foreach_reverse (i; 1..101) {
if (a%i == 0 && b%i == 0) {
--k;
if (k == 0) {
writeln(i);
return;
}
}
}
}
| D |
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
import std.typecons;
import std.bigint;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.container.deque;
int main() {
auto sc = new Scanner(stdin);
int n, m, k;
sc.read(n, m, k); m++;
int[] a = new int[n];
a.each!((ref x) => sc.read(x));
long[] dp = a.map!(to!long).array;
long[] ndp = new long[n];
auto deq = Deque!int();
deq.insertBack(1); deq.removeBack();
auto p = deq.p;
foreach (int ph; 2..k+1) {
ndp[] = -(10L^^18);
deq.clear();
foreach (int i; 0..n) {
if (p.length && deq.front == i-m) {
p.removeFront();
}
if (p.length) {
ndp[i] = dp[deq.front] + 1L * ph * a[i];
}
while (p.length && dp[deq.back] <= dp[i]) {
p.removeBack();
}
p.insertBack(i);
}
swap(dp, ndp);
}
writeln(dp.fold!max);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
//fold(for old compiler)
static if (__VERSION__ <= 2070) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
unittest {
import std.stdio;
auto l = [1, 2, 3, 4, 5];
assert(l.fold!"a+b"(10) == 25);
}
}
version (X86) static if (__VERSION__ < 2071) {
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/deque.d */
// module dcomp.container.deque;
struct Deque(T) {
import core.exception : RangeError;
import core.memory : GC;
import std.range : ElementType, isInputRange;
import std.traits : isImplicitlyConvertible;
struct Payload {
T *d;
size_t st, length, cap;
@property bool empty() const { return length == 0; }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {
version(assert) if (length <= i) throw new RangeError();
return d[(st+i >= cap) ? (st+i-cap) : st+i];
}
private void expand() {
import std.algorithm : max;
assert(length == cap);
auto nc = max(size_t(4), 2*cap);
T* nd = cast(T*)GC.malloc(nc * T.sizeof);
foreach (i; 0..length) {
nd[i] = this[i];
}
d = nd; st = 0; cap = nc;
}
void clear() {
st = length = 0;
}
void insertFront(T v) {
if (length == cap) expand();
if (st == 0) st += cap;
st--; length++;
this[0] = v;
}
void insertBack(T v) {
if (length == cap) expand();
length++;
this[length-1] = v;
}
void removeFront() {
assert(!empty, "Deque.removeFront: Deque is empty");
st++; length--;
if (st == cap) st = 0;
}
void removeBack() {
assert(!empty, "Deque.removeBack: Deque is empty");
length--;
}
}
struct RangeT(A) {
alias T = typeof(*(A.p));
alias E = typeof(A.p.d[0]);
T *p;
size_t a, b;
@property bool empty() const { return b <= a; }
@property size_t length() const { return b-a; }
@property RangeT save() { return RangeT(p, a, b); }
@property RangeT!(const A) save() const {
return typeof(return)(p, a, b);
}
alias opDollar = length;
@property ref inout(E) front() inout { return (*p)[a]; }
@property ref inout(E) back() inout { return (*p)[b-1]; }
void popFront() {
version(assert) if (empty) throw new RangeError();
a++;
}
void popBack() {
version(assert) if (empty) throw new RangeError();
b--;
}
ref inout(E) opIndex(size_t i) inout { return (*p)[i]; }
RangeT opSlice() { return this.save; }
RangeT opSlice(size_t i, size_t j) {
version(assert) if (i > j || a + j > b) throw new RangeError();
return typeof(return)(p, a+i, a+j);
}
RangeT!(const A) opSlice() const { return this.save; }
RangeT!(const A) opSlice(size_t i, size_t j) const {
version(assert) if (i > j || a + j > b) throw new RangeError();
return typeof(return)(p, a+i, a+j);
}
}
alias Range = RangeT!Deque;
alias ConstRange = RangeT!(const Deque);
alias ImmutableRange = RangeT!(immutable Deque);
Payload *p;
private void I() { if (!p) p = new Payload(); }
private void C() const {
version(assert) if (!p) throw new RangeError();
}
//some value
this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) {I;
p = new Payload();
foreach (v; values) {
insertBack(v);
}
}
//range
this(Range)(Range r)
if (isInputRange!Range &&
isImplicitlyConvertible!(ElementType!Range, T) &&
!is(Range == T[])) {I;
p = new Payload();
foreach (v; r) {
insertBack(v);
}
}
@property bool empty() const { return (!p || p.empty); }
@property size_t length() const { return (p ? p.length : 0); }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; }
ref inout(T) front() inout {C; return (*p)[0]; }
ref inout(T) back() inout {C; return (*p)[$-1]; }
void clear() { if (p) p.clear(); }
void insertFront(T v) {I; p.insertFront(v); }
void insertBack(T v) {I; p.insertBack(v); }
void removeFront() {C; p.removeFront(); }
void removeBack() {C; p.removeBack(); }
Range opSlice() {I; return Range(p, 0, length); }
}
unittest {
import std.algorithm : equal;
import std.range.primitives : isRandomAccessRange;
import std.container.util : make;
auto q = make!(Deque!int);
assert(isRandomAccessRange!(typeof(q[])));
//insert,remove
assert(equal(q[], new int[](0)));
q.insertBack(1);
assert(equal(q[], [1]));
q.insertBack(2);
assert(equal(q[], [1, 2]));
q.insertFront(3);
assert(equal(q[], [3, 1, 2]) && q.front == 3);
q.removeFront;
assert(equal(q[], [1, 2]) && q.length == 2);
q.insertBack(4);
assert(equal(q[], [1, 2, 4]) && q.front == 1 && q.back == 4 && q[$-1] == 4);
q.insertFront(5);
assert(equal(q[], [5, 1, 2, 4]));
//range
assert(equal(q[][1..3], [1, 2]));
assert(equal(q[][][][], q[]));
//const range
const auto rng = q[];
assert(rng.front == 5 && rng.back == 4);
//reference type
auto q2 = q;
q2.insertBack(6);
q2.insertFront(7);
assert(equal(q[], q2[]) && q.length == q2.length);
//construct with make
auto a = make!(Deque!int)(1, 2, 3);
auto b = make!(Deque!int)([1, 2, 3]);
assert(equal(a[], b[]));
}
unittest {
import std.algorithm : equal;
import std.range.primitives : isRandomAccessRange;
import std.container.util : make;
auto q = make!(Deque!int);
q.clear();
assert(equal(q[], new int[0]));
foreach (i; 0..100) {
q.insertBack(1);
q.insertBack(2);
q.insertBack(3);
q.insertBack(4);
q.insertBack(5);
assert(equal(q[], [1,2,3,4,5]));
q.clear();
assert(equal(q[], new int[0]));
}
}
unittest {
Deque!int a;
Deque!int b;
a.insertFront(2);
assert(b.length == 0);
}
unittest {
import std.algorithm : equal;
import std.range : iota;
Deque!int a;
foreach (i; 0..100) {
a.insertBack(i);
}
assert(equal(a[], iota(100)));
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
if (f.eof) return false;
line = lineBuf[];
f.readln(line);
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
//string or char[10] etc
//todo optimize
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else {
auto buf = line.split.map!(to!E).array;
static if (isStaticArray!T) {
//static
assert(buf.length == T.length);
}
x = buf;
line.length = 0;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
fout.writeln("1 2 3");
fout.writeln("ab cde");
fout.writeln("1.0 1.0 2.0");
fout.close;
Scanner sc = new Scanner(File(fileName, "r"));
int a;
int[2] b;
char[2] c;
string d;
double e;
double[] f;
sc.read(a, b, c, d, e, f);
assert(a == 1);
assert(equal(b[], [2, 3]));
assert(equal(c[], "ab"));
assert(equal(d, "cde"));
assert(e == 1.0);
assert(equal(f, [1.0, 2.0]));
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File, writeln;
import std.datetime;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
foreach (i; 0..1_000_000) {
fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2);
}
fout.close;
writeln("Scanner Speed Test(3*1,000,000 int)");
StopWatch sw;
sw.start;
Scanner sc = new Scanner(File(fileName, "r"));
foreach (i; 0..500_000) {
int a, b, c;
sc.read(a, b, c);
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
foreach (i; 500_000..700_000) {
int[3] d;
sc.read(d);
int a = d[0], b = d[1], c = d[2];
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
foreach (i; 700_000..1_000_000) {
int[] d;
sc.read(d);
assert(d.length == 3);
int a = d[0], b = d[1], c = d[2];
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
writeln(sw.peek.msecs, "ms");
}
| D |
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;
void main()
{
foreach (line; stdin.byLine) {
auto x = line.chomp.array;
auto field = new int[](9);
foreach (i, e; x) {
if (e == 'o') field[i] = 1;
else if (e == 'x') field[i] = -1;
else field[i] = 0;
}
int res;
foreach (i; 0..3) {
auto a = field[i*3] + field[i*3+1] + field[i*3+2];
auto b = field[i] + field[i+3] + field[i+6];
if (a == 3 || b == 3) res = 1;
else if (a == -3 || b == -3) res = -1;
}
auto a = field[0] + field[4] + field[8];
auto b = field[2] + field[4] + field[6];
if (a == 3 || b == 3) res = 1;
else if (a == -3 || b == -3) res = -1;
if (res == 1) writeln("o");
else if (res == -1) writeln("x");
else writeln("d");
}
} | 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.numeric;
void main()
{
auto n = readln.chomp.to!int;
auto b = readln.chomp.split.to!(int[]);
auto a = new int[](n);
a[0] = b[0];
a[$-1] = b[$-1];
foreach (i; 1..n-1) {
a[i] = min(b[i-1], b[i]);
}
a.sum.writeln;
}
| D |
import std.stdio, std.string, std.conv;
void main() {
string s = readln.chomp;
string t = readln.chomp;
int cnt;
foreach (i; 0 .. 3) {
if (s[i] == t[i]) ++cnt;
}
cnt.writeln;
} | D |
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
void main() {
writeln("No");
}
| D |
// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
import std.datetime, std.bigint;
int n, a, b;
int[] x, y;
void main() {
scan(n, a, b);
x = new int[](n);
y = new int[](n);
foreach (i ; 0 .. n) {
scan(x[i], y[i]);
}
bool check(int U, int V, int u, int v) {
return (u <= U && v <= V) || (u <= V && v <= U);
}
int ans;
foreach (i ; 0 .. n) {
foreach (j ; 0 .. n) {
if (i == j) continue;
if (x[i] <= a && y[i] <= b) {
if (check(a - x[i], b, x[j], y[j]) || check(a, b - y[i], x[j], y[j])) {
ans = max(ans, x[i]*y[i] + x[j]*y[j]);
}
}
if (x[i] <= b && y[i] <= a) {
if (check(a - y[i], b, x[j], y[j]) || check(a, b - x[i], x[j], y[j])) {
ans = max(ans, x[i]*y[i] + x[j]*y[j]);
}
}
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
} | D |
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
long n, m, a, b; rd(n, m, a, b);
long x=(((n+m-1)/m)*m-n)*a;
long y=(n-(n/m)*m)*b;
writeln(min(x, y));
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x) e=l[i].to!(typeof(e));
} | D |
import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.algorithm;
import std.math;
import std.typecons;
import std.variant;
void main() {
long tt = readln().chomp.to!long;
foreach (t; 0 .. tt) {
readln();
long[] piles = readln().chomp.split(" ").map!(to!long).array;
bool winner = true;
bool didBreak = false;
foreach (i; piles) {
if (i == 1) {
winner = !winner;
}
else {
didBreak = true;
break;
}
}
if (!didBreak) {
winner = !winner;
}
writeln(winner ? "First" : "Second");
}
}
| 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 x = RD!int;
auto m = RD!int;
int ll = x, rr = x;
foreach (i; 0..m)
{
auto l = RD!int;
auto r = RD!int;
if (l <= rr && r >= ll)
{
ll.chmin(l);
rr.chmax(r);
}
}
ans[ti] = rr-ll+1;
}
foreach (e; ans)
{
writeln(e);
}
stdout.flush;
debug readln;
}
| D |
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
int solve (int n, bool [] a, bool [] b)
{
auto aOnes = a.sum.to !(int);
auto bOnes = b.sum.to !(int);
auto same = n.iota.count !(i => a[i] == b[i]).to !(int);
auto diff = n - same;
int res = int.max;
if (aOnes == bOnes)
{
res = min (res, diff);
}
if (aOnes + bOnes == n + 1)
{
res = min (res, same);
}
if (res == int.max)
{
res = -1;
}
return res;
}
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto n = readln.strip.to !(int);
auto a = readln.strip.map !(q{a == '1'}).array;
auto b = readln.strip.map !(q{a == '1'}).array;
writeln (solve (n, a, b));
}
}
| D |
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;
void main() {
auto N = readln.chomp.to!int;
auto S = readln.chomp;
auto used = new bool[](N);
for (int i = 0, j = 0, t = 0, rest = N; rest > 11; t ^= 1, --rest) {
if (t == 0) {
while (i < N && (used[i] || S[i] == '8')) ++i;
if (i >= N) {
writeln("YES");
return;
}
used[i] = true;
} else {
while (j < N && (used[j] || S[j] != '8')) ++j;
if (j >= N) {
writeln("NO");
return;
}
used[j] = true;
}
}
foreach (i; 0..N) {
if (!used[i]) {
writeln(S[i] == '8' ? "YES" : "NO");
return;
}
}
} | D |
module main;
import std.stdio;
import std.algorithm;
import std.array;
ulong pow(ulong a, ulong b){
if (b == 0)
return 1;
ulong ans = a;
for (ulong i = 2; i<=b; ++i)
ans *= a;
return ans;
}
int main(string[] argv)
{
ulong l;
ulong r;
scanf("%d %d", &l, &r);
ulong ans = 0;
for (ulong i = 0; i <= 32; ++i){
for (ulong j = 0; j <= 21; ++j){
if (pow(2, i) * pow(3, j) >= l && pow(2, i) * pow(3, j) <= r)
++ans;
}
}
printf("%d", ans);
return 0;
} | D |
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
auto s=readln.chomp.to!(char[]);
auto t="abcdefghijklmnopqrstuvwxyz".to!(char[]);
if(s.length<t.length){writeln(-1); return;}
int cnt=0;
for(int i=0, j=0; i<t.length.to!(int); i++, j++){
if(cnt==t.length.to!(int)) break;
while(j<s.length.to!(int)){
if(s[j]<=t[i]){s[j]=t[i]; cnt++; break;}
else j++;
}
}
if(cnt<t.length.to!(int)) writeln(-1);
else writeln(s);
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
} | D |
import std.stdio, std.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;
bool solve() {
auto L = new int[](4);
auto S = new int[](4);
auto R = new int[](4);
auto P = new int[](4);
foreach (i; 0..4) {
auto s = readln.split.map!(to!int);
L[i] = s[0];
S[i] = s[1];
R[i] = s[2];
P[i] = s[3];
}
foreach (i; 0..4) {
if (L[i] && P[i]) return true;
if (S[i] && P[i]) return true;
if (R[i] && P[i]) return true;
}
foreach (i; 0..4) {
if (L[i] && P[(i+3)%4]) return true;
}
foreach (i; 0..4) {
if (S[i] && P[(i+2)%4]) return true;
}
foreach (i; 0..4) {
if (R[i] && P[(i+1)%4]) return true;
}
return false;
}
void main() {
writeln( solve ? "YES" : "NO" );
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.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; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }
long mod = 10^^9 + 7;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto x = RD;
long cnt;
long[] ans;
foreach (i; 0..20)
{
auto y = 1UL << (19 - i);
if (x & y) continue;
x = x ^ ((y << 1) - 1);
ans ~= 20 - i;
++cnt;
if (x == (2^^20)-1) break;
++x;
++cnt;
}
debug writeln(x);
writeln(cnt);
if (cnt != 0)
{
write(ans[0]);
foreach (e; ans[1..$])
write(" ", e);
writeln();
}
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;
void main()
{
auto s = readln.chomp;
auto f = false;
foreach (i; 0..s.length-1) {
if (s[i] == 'A' && s[i+1] == 'C') {
f = true;
break;
}
}
if (f) writeln("Yes");
else writeln("No");
}
| D |
// 提出解
void solve(){
A: foreach(_; 0 .. scan!int){
int n = scan!int;
int[] as = scan!int(n);
int[] xs;
for(int i = 0, j = n - 1; i < j; i ++, j --){
if(as[i] != as[j]){
xs = [as[i], as[j]];
break;
}
}
if(xs.length == 0){
"YES".print;
continue;
}
foreach(x; xs){
bool isOK = 1;
for(int i = 0, j = n - 1; i < j; ){
if(as[i] == x){
i ++;
continue;
}
else if(as[j] == x){
j --;
continue;
}
if(as[i] != as[j]){
isOK = 0;
break;
}
i ++, j --;
}
if(isOK){
"YES".print;
continue A;
}
}
"NO".print;
}
}
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
// 愚直解
void jury(){
}
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
// テストケース
void gen(){
}
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
// テンプレ
import std;
bool DEBUG = 0;
void main(string[] args){
if(args.canFind("-debug")) DEBUG = 1;
if(args.canFind("-gen")) gen; else if(args.canFind("-jury")) jury; else solve;
}
void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void print(){ writeln(""); }
void print(T)(T t){ writeln(t); }
void print(T, A ...)(T t, A a){ stdout.write(t, " "), print(a); }
string unsplit(T)(T xs, string d = " "){ return xs.array.to!(string[]).join(d); }
string scan(){
static string[] ss; while(!ss.length) ss = readln.chomp.split;
string res = ss[0]; ss.popFront; return res;
}
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
T mini(T)(ref T x, T y){ if(x > y) x = y; return x; }
T maxi(T)(ref T x, T y){ if(x < y) x = y; return x; }
T mid(T)(T l, T r, bool delegate(T) f){
T m = (l + r) / 2; (f(m)? l: r) = m; return f(l)? f(r - 1)? r: mid(l, r, f): l;
}
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
// ライブラリ(基本)
class UnionFind{
this(int n){ foreach(i; 0 .. n) R ~= i, K ~= [i]; } int[] R; int[][] K;
void unite(int a, int b){
int ra = R[a], rb = R[b]; if(ra == rb) return;
if(K[ra].length < K[rb].length) unite(b, a);
else foreach(k; K[rb]) R[k] = ra, K[ra] ~= k;
}
int find(int a){ return R[a]; }
int getSize(int a){ return K[R[a]].length.to!int; }
}
class Queue(T){
private T[] xs; private uint i, j;
this(T[] xs = []){ this.xs = xs; j = xs.length.to!uint; }
uint length(){ return j - i; }
bool isEmpty(){ return j == i; }
void enq(T x){ while(j + 1 >= xs.length) xs.length = xs.length * 2 + 1; xs[j ++] = x; }
T deq(){ assert(i < j); return xs[i ++]; }
T peek(){ assert(i < j); return xs[i]; }
void flush(){ i = j; }
alias empty = isEmpty, front = peek, popFront = deq, pop = deq, push = enq, top = peek;
Queue opOpAssign(string op)(T x) if(op == "~"){ enq(x); return this; }
T opIndex(uint li){ assert(i + li < j); return xs[i + li]; }
static Queue!T opCall(T[] xs){ return new Queue!T(xs); }
T[] array(){ return xs[i .. j]; }
override string toString(){ return array.to!string; }
}
class Stack(T){
private T[] xs; private uint j;
this(T[] xs = []){ this.xs = xs; j = xs.length.to!uint; }
uint length(){ return j; }
bool isEmpty(){ return j == 0; }
void push(T x){ while(j + 1 >= xs.length) xs.length = xs.length * 2 + 1; xs[j ++] = x; }
T pop(){ assert(j > 0); return xs[-- j]; }
T peek(){ assert(j > 0); return xs[j - 1]; }
void clear(){ j = 0; }
alias empty = isEmpty, front = peek, popFront = pop, top = peek;
Stack opOpAssign(string op)(T x) if(op == "~"){ push(x); return this; }
T opIndex(uint li){ assert(j > 0 && j - 1 >= li); return xs[j - 1 - li]; }
static Stack!T opCall(T[] xs = []){ return new Stack!T(xs); }
T[] array(){ return xs[0 .. j]; }
override string toString(){ return array.to!string; }
}
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
// ライブラリ(追加)
| D |
import std;
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, "cur", long, "prev");
alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less);
void main()
{
long n, k;
scan(n, k);
auto next = aryread.map!(x => x - 1).array();
long[] tele;
auto reached = new long[](n);
long cur;
while (!reached[cur])
{
tele ~= cur;
reached[cur] = true;
cur = next[cur];
}
auto nloops = tele[0 .. tele.countUntil(cur)], loops = tele[tele.countUntil(cur) .. $];
if (k >= nloops.length)
{
k -= nloops.length;
writeln(loops[k % loops.length] + 1);
}
else
{
writeln(nloops[k] + 1);
}
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
| D |
import std.stdio;
import std.algorithm;
import std.typecons : tuple, Tuple;
import std.container.rbtree;
int n;
long s;
int[] l;
int[] r;
bool can(long m) {
int cnt = 0;
int other = n;
long sum = 0;
for(int i = 0; i < n; ++i) {
if (m < l[i]) {
++cnt;
sum += l[i];
--other;
} else if (r[i] < m) {
sum += l[i];
--other;
}
}
if (cnt + other < (n + 1) / 2) {
return false;
}
int[] ls = new int[other];
int ptr = 0;
for(int i = 0; i < n; ++i) {
if (l[i] <= m && m <= r[i]) {
ls[ptr] = l[i];
++ptr;
}
}
sort(ls);
for(int i = other - 1; i >= 0; --i) {
if (i >= other - ((n+1)/2 - cnt)) {
sum += m;
} else {
sum += ls[i];
}
}
return sum <= s;
}
int main() {
int t;
scanf("%d", &t);
while (t --> 0) {
scanf("%d%lld", &n, &s);
l = new int[n];
r = new int[n];
for(int i = 0; i < n; ++i) {
scanf("%d%d", &l[i], &r[i]);
}
long tl = 0, tr = s + 1;
while (tl+1 < tr) {
long tm = (tl+tr) / 2;
if (can(tm)) {
tl = tm;
} else {
tr = tm;
}
}
printf("%lld\n", tl);
}
return 0;
} | D |
void main()
{
long a, b, c, d;
rdVals(a, b, c, d);
long x = abs(a-c), y = abs(b-d);
long g = gcd(x, y);
long result = x + y - g;
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;
import std.algorithm;
import std.conv;
import std.string;
import std.math;
void main(string[] args)
{
auto n = split(strip(readln));
auto a = to!long(n[0]);
auto b = to!long(n[1]);
long count;
if (a == 1 && b == 1)
{
writeln(0);
return;
}
while(a >= 1 && b >= 1)
{
if (a < b)
{
a++;
b -= 2;
}
else
{
b++;
a -= 2;
}
count++;
// writeln(a, "\t", b);
}
writeln(count);
} | D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.math;
import std.regex;
import std.range;
void main() {
auto A = readln.chomp.to!int, B = readln.chomp.to!int, C = readln.chomp.to!int, X = readln.chomp.to!int;
int count;
foreach(i; 0..A+1) {
foreach(j; 0..B+1) {
foreach(k; 0..C+1) {
if(500*i + 100*j + 50*k == X) count++;
}
}
}
count.writeln;
}
| D |
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int k, n; readV(k, n);
auto ans = new int[](n), s = n;
if (k%2 == 0) {
ans[0] = k/2;
ans[1..$][] = k;
} else {
ans[] = (k+1)/2;
foreach (i; 0..n/2) {
if (ans[s-1] == 1) {
--s;
} else {
ans[s-1]--;
ans[s..n][] = k;
s = n;
}
}
}
foreach (i; 0..s) {
write(ans[i]);
if (i < s-1) write(" ");
}
writeln;
}
| D |
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] 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;
// dfmt on
void main()
{
auto S = sread();
long ans;
foreach (c; S)
ans += c == '1';
writeln(ans);
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void main()
{
auto N = RD;
auto X = RDA(-1);
long ans = long.max;
foreach (i; 0..100)
{
long cnt;
foreach (e; X)
{
cnt += (e-i)^^2;
}
ans.chmin(cnt);
}
writeln(ans);
stdout.flush;
debug readln;
} | 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;
alias Edge = Tuple!(int, "from", int, "to", int, "s");
alias Pair = Tuple!(int, "sgn", int, "val");
enum inf = 2*10^^9 + 3;
void main() {
int n, m;
scan(n, m);
auto adj = new Edge[][](n, 0);
auto es = new Edge[](m);
foreach (i ; 0 .. m) {
int ui, vi, si;
scan(ui, vi, si);
ui--, vi--;
adj[ui] ~= Edge(ui, vi, si);
adj[vi] ~= Edge(vi, ui, si);
es[i] = Edge(ui, vi, si);
}
auto t = new Pair[](n);
t[] = Pair(0, 0);
void dfs(int v, int s, int a) {
t[v] = Pair(s, a);
foreach (e ; adj[v]) {
if (t[e.to].sgn == 0) {
dfs(e.to, s*(-1), e.s - a);
}
}
}
dfs(0, 1, 0);
int x = inf;
long ans = inf;
foreach (i ; 0 .. m) {
int u = es[i].from, v = es[i].to;
int s = es[i].s;
if (t[u].sgn != t[v].sgn) {
if (t[u].val + t[v].val != s) {
writeln(0);
return;
}
}
else if (t[u].sgn == 1) {
int df = s - (t[u].val + t[v].val);
if (df % 2 == 1 || df < 0) {
writeln(0);
return;
}
if (x == inf || x == df) {
x = df;
ans = 1;
}
else {
writeln(0);
return;
}
}
else if (t[u].sgn == -1) {
int df = s - (t[u].val + t[v].val);
if (df % 2 == 1 || df > 0) {
writeln(0);
return;
}
if (x == inf || x == -df) {
x = -df;
ans = 1;
}
else {
writeln(0);
return;
}
}
}
long lo = 1, hi = inf;
foreach (i ; 0 .. n) {
if (t[i].sgn == 1) {
lo = max(lo, -t[i].val + 1);
}
else {
hi = min(hi, t[i].val - 1);
}
}
debug {
writefln("%d %d", lo, hi);
}
ans = min(ans, max(0, hi - lo + 1));
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
| D |
import std.stdio, std.string, std.conv;
void main() {
int[] tmp = readln.split.to!(int[]);
int a = tmp[0], p= tmp[1];
writeln((3 * a + p) / 2);
} | 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!long;
auto S = readln.chomp;
auto cnt = new long[](26);
foreach (i; 0..N) {
cnt[S[i] - 'a'] += 1;
}
long ans = 0;
foreach (i; 0..N) {
long tmp = 1;
foreach (j; 0..26) if (cnt[j] && j != S[i] - 'a') tmp = tmp * (cnt[j] + 1) % MOD;
ans += tmp;
ans %= MOD;
cnt[S[i] - 'a'] -= 1;
}
ans = (ans + MOD) % MOD;
ans.writeln;
}
long powmod(long a, long x, long m) {
long ret = 1;
while (x) {
if (x % 2) ret = ret * a % m;
a = a * a % m;
x /= 2;
}
return ret;
}
| D |
import std.string;
import std.range;
import std.conv;
import std.stdio;
import std.algorithm;
void main()
{
auto N = readln.chomp.to!int;
auto A = readln.split.to!(int[]);
auto m = A.reduce!max;
auto l = A.reduce!min;
writeln(m-l);
} | D |
/+ dub.sdl:
name "F"
dependency "dunkelheit" version=">=0.9.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
int solve(int[][] g, bool[] s) {
int n = g.length.to!int;
foreach (i; 0..n) {
if (g[i].length % 2) s[i] = !s[i];
}
debug {
foreach (v; g) {
writeln(v);
}
writeln(s);
}
int base = 0;
base += 2*(n-1) + s.count(true).to!int;
int ans = 0;
int dfs(int p, int b) {
int[2] buf = [0, 0];
foreach (d; g[p]) {
if (d == b) continue;
int ch = dfs(d, p);
if (ch > buf[0]) swap(ch, buf[0]);
if (ch > buf[1]) swap(ch, buf[1]);
}
if (s[p]) {
if (g[p].length.to!int == 1) {
buf[0]++;
} else {
buf[0] += 2;
}
}
ans = max(ans, buf[0] + buf[1]);
return buf[0];
}
dfs(0, -1);
debug writeln("ANS! ", base, " ", ans);
return base - ans;
}
int main() {
Scanner sc = new Scanner(stdin);
scope(exit) assert(!sc.hasNext);
int n;
sc.read(n);
int[][] g = new int[][n];
foreach (i; 0..n-1) {
int a, b; sc.read(a, b); a--; b--;
g[a] ~= b; g[b] ~= a;
}
string s;
sc.read(s);
int wc = s.count('W').to!int;
if (wc == 0) {
writeln(0);
return 0;
} else if (wc == 1) {
writeln(1);
return 0;
}
int[] wsz = new int[n];
bool[] enable = new bool[n];
void dfs(int p, int b) {
wsz[p] = 0;
int chv = 0;
if (s[p] == 'W') {
wsz[p] = 1;
chv = 2;
}
foreach (d; g[p]) {
if (d == b) continue;
dfs(d, p);
wsz[p] += wsz[d];
if (wsz[d]) chv++;
}
if (wsz[p] != wc) {
chv++;
}
enable[p] = (chv >= 2);
}
dfs(0, -1);
int n2 = enable.count(true).to!int;
int[] newID = new int[n]; int idc = 0;
bool[] s2 = new bool[n2];
foreach (i; 0..n) {
if (enable[i]) {
newID[i] = idc++;
s2[newID[i]] = (s[i] == 'W');
}
}
int[][] ng = new int[][n2];
foreach (i; 0..n) {
if (!enable[i]) continue;
foreach (j; g[i]) {
if (i > j || !enable[j]) continue;
ng[newID[i]] ~= newID[j];
ng[newID[j]] ~= newID[i];
}
}
writeln(solve(ng, s2));
return 0;
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */
// module dkh.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
void read(Args...)(auto ref Args args) {
import std.exception;
static if (args.length != 0) {
enforce(readSingle(args[0]));
read(args[1..$]);
}
}
bool hasNext() {
return succ();
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
| D |
import std.stdio, std.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 N, K;
scan(N, K);
K = min(K, N - 1);
long ans;
auto mc = ModComb(N);
foreach (i ; 0 .. K + 1) {
debug {
//writeln(mc.c(N, i) * mc.c(N - 1, i));
}
ans += mc.c(N, i) * mc.c(N - 1, i) % mod;
ans %= mod;
}
writeln(ans);
}
struct ModComb {
int _N;
long[] _fact, _factinv;
long _mod;
this(int n, long mod = 1_000_000_007L) {
_N = n;
_fact = new long[](_N + 1);
_factinv = new long[](_N + 1);
_mod = mod;
_fact[0] = 1;
foreach (i ; 1 .. _N + 1) {
_fact[i] = (_fact[i-1] * i) % mod;
}
_factinv[_N] = _powmod(_fact[_N], mod - 2);
foreach_reverse (i ; 0 .. _N) {
_factinv[i] = (_factinv[i+1] * (i+1)) % mod;
}
}
long c(int n, int k) {
if (k < 0 || k > n) return 0;
return f(n) * finv(n - k) % _mod * finv(k) % _mod;
}
long p(int n, int r) {
if (r < 0 || r > n) return 0;
return f(n) * finv(n - r) % _mod;
}
long f(int n) {
return _fact[n];
}
long finv(int n) {
return _factinv[n];
}
long _powmod(long x, long y) {
return y > 0 ? _powmod(x, y>>1)^^2 % _mod * x^^(y & 1) % _mod : 1;
}
}
unittest {
auto mc = ModComb(1_000_000);
assert(mc.c(5, 2) == 10);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void main()
{
writeln(readln.chomp == "0" ? 1 : 0);
} | 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, std.datetime;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N, M;
scan(N, M);
writeln(N * (N - 1) / 2 + M * (M - 1) / 2);
}
| D |
void main(){
//auto NM = readLine!long();
if( readStr() == "ABC" ){
writeln("ARC");
} else {
writeln("ABC");
}
}
import std;
string readStr(){
return readln().chomp();
}
T[] readLine( T = long )(){
return readln().split().to!(T[])();
} | D |
import std.stdio,std.string,std.conv,std.array,std.algorithm;
void main(){
string[] arr;
readln();
for(;;){
auto rc = readln().chomp();
if(!rc){ break; }
arr ~= rc;
}
sort( arr );
writeln( arr[0] );
} | D |
import std.stdio;
import std.string;
import std.conv;
import std.range;
import std.array;
import std.algorithm;
import std.typecons;
import std.math;
void main() {
while(true) {
string[] cin;
cin=split(readln());
int n=to!int(cin[0]),a=to!int(cin[1]),b=to!int(cin[2]),c=to!int(cin[3]),x=to!int(cin[4]);
if(!(n||a||b||c||x)) break;
int[] z=new int[n];
cin=split(readln());
for(int i=0; i<n; i++) z[i]=to!int(cin[i]);
int d=0,cnt=0;
for(int i=0; i<=10000; i++) {
if(z[d]==x) d++;
if(d==n) break;
x=(a*x+b)%c;
cnt++;
}
if(d==n) writeln(cnt);
else writeln(-1);
}
}
| D |
// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
import std.exception : enforce;
int n, p;
int[] a;
void main() {
scan(n, p);
a = readln.split.to!(int[]);
auto dp = new long[][](2, 2);
dp[0][0] = 1L;
foreach (i, ai; a) {
i &= 1;
dp[i ^ 1][0] = dp[i][0] + dp[i][ai & 1];
dp[i ^ 1][1] = dp[i][1] + dp[i][ai & 1 ^ 1];
}
writeln(dp[n & 1][p]);
}
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 core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
ulong bignum = 1_000_000_007;
alias SugarWater = Tuple!(long, "swater", long, "sugar");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
auto n = lread();
auto a = aryread();
foreach (i; iota(1, n))
{
if (a[i - 1] <= a[i] - 1)
a[i] -= 1;
}
bool check = true;
foreach (i; iota(1, n))
check &= a[i - 1] <= a[i];
if(check)
writeln("Yes");
else
writeln("No");
} | D |
import std.conv;
import std.stdio;
import std.string;
void main()
{
auto n = readln.strip.to!( int );
writeln( solve( n ) );
}
auto solve( in int n )
{
return n + n * ( n % 2 );
}
| D |
import std.stdio;
import std.array;
import std.conv;
void main(){
string[] input = split(readln());
int a = to!int(input[0]);
writeln(a*a*a);
}
| D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main()
{
auto S = split(readln())[0];
//if (S == "zyxwvutsrqponmlkjihgfedcba") { writeln("-1"); return; }
if (S.length < 26) { writeln(solve1(S)); }
else { writeln(solve2(S)); }
}
string solve1(string S) {
bool[26] list;
foreach (c; S) list[c-'a'] = true;
foreach (i, f; list) {
if (!f) { return S ~ ( to!char('a'+i) ); }
}
return "-1";
}
string solve2(string S) {
int index = 25;
while (index >= 0) {
foreach ( c; S[index] + 1 .. 'z'+1 ) {
if (S[index+1..$].find(to!char(c)).length!=0) return S[0..index] ~ to!char(c);
}
--index;
}
return "-1";
}
| D |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.