code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
/* imports all std modules {{{*/
import
std.algorithm,
std.array,
std.ascii,
std.base64,
std.bigint,
std.bitmanip,
std.compiler,
std.complex,
std.concurrency,
std.container,
std.conv,
std.csv,
std.datetime,
std.demangle,
std.encoding,
std.exception,
std.file,
std.format,
std.functional,
std.getopt,
std.json,
std.math,
std.mathspecial,
std.meta,
std.mmfile,
std.net.curl,
std.net.isemail,
std.numeric,
std.parallelism,
std.path,
std.process,
std.random,
std.range,
std.regex,
std.signals,
std.socket,
std.stdint,
std.stdio,
std.string,
std.system,
std.traits,
std.typecons,
std.uni,
std.uri,
std.utf,
std.uuid,
std.variant,
std.zip,
std.zlib;
/*}}}*/
/+---test
2
3 5 2
4 5
---+/
/+---test
3
5 6 3 8
5 100 8
---+/
/+---test
2
100 1 1
1 100
---+/
void main(string[] args) {
readln;
auto A = readln.split.map!(to!long).array;
auto B = readln.split.map!(to!long).array;
size_t ans;
foreach (i, b; B) {
auto n1 = min(b, A[i]);
b -= n1;
auto n2 = min(b, A[i+1]);
A[i+1] -= n2;
ans += n1 + n2;
}
ans.writeln;
}
|
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; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto a = RD!int;
auto b = RD!int;
auto c = RD!int;
auto d = RD!int;
auto e = RD!int;
auto f = RD!int;
long ans1, ans2;
{
auto aa = a;
auto bb = b;
auto cc = c;
auto dd = d;
auto ad = min(aa, dd);
ans1 += ad * e;
aa -= ad;
dd -= ad;
auto bcd = min(bb, cc, dd);
ans1 += bcd * f;
}
{
auto aa = a;
auto bb = b;
auto cc = c;
auto dd = d;
auto bcd = min(bb, cc, dd);
ans2 += bcd * f;
bb -= bcd;
cc -= bcd;
dd -= bcd;
auto ad = min(aa, dd);
ans2 += ad * e;
}
writeln(max(ans1, ans2));
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;
void main() {
auto T = readln.chomp.to!int;
while (T--) {
auto X = readln.chomp.to!long;
auto Y = 180L;
auto G = gcd(X, Y);
X /= G;
Y /= G;
if (Y - X == 1) {
Y *= 2;
}
writeln(Y);
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp;
switch (N[$-1]) {
case '3': writeln("bon"); return;
case '0':
case '1':
case '6':
case '8': writeln("pon"); return;
default: writeln("hon"); return;
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto T = readln.chomp.to!int;
foreach (_t; 0..T) {
auto nk = readln.split.to!(int[]);
auto N = nk[0];
auto K = nk[1];
auto as = readln.split.to!(int[]);
int p;
foreach (i; 0..K) {
if (i != K-1 && i > 0 && i < N-1 && as[i] > as[i-1] && as[i] > as[i+1]) ++p;
}
int max_p = p, min_l = 1;
foreach (i; 1..N-K+1) {
auto j = i+K-1;
if (as[j-1] > as[j-2] && as[j-1] > as[j]) ++p;
if (as[i] > as[i-1] && as[i] > as[i+1]) --p;
if (p > max_p) {
max_p = p;
min_l = i+1;
}
}
writeln(max_p+1, " ", min_l);
}
}
|
D
|
import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;
void main(){
int n = readln.chomp.to!int;
int[] ts, as;
foreach(i; 0 .. n){
int[] tmp = readln.chomp.split.map!(to!int).array;
ts ~= tmp[0], as ~= tmp[1];
}
long t, a;
t = 1, a = 1;
long ad, td;
long d;
foreach(i; 0 .. n){
if(t % ts[i] == 0) td = t / ts[i];
else td = t / ts[i] + 1;
if(a % as[i] == 0) ad = a / as[i];
else ad = a / as[i] + 1;
if(td < ad) d = ad; else d = td;
t = ts[i] * d, a = as[i] * d;
debug writeln(ts[i], ",", as[i], "; ", t, ",", a);
}
(t + a).writeln;
}
|
D
|
import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;
import std.numeric;
void main()
{
int n, k;
readln.chomp.split.tie(n, k);
if (k == 0) {
writeln = cast(long) n * n;
return;
}
long cnt = 0;
for (int b = 1; b <= n; ++b) {
int p = n / b;
int r = n % b;
cnt += p * max(0, b - k);
cnt += max(0, r - k + 1);
}
cnt.writeln;
}
void tie(R, Args...)(R arr, ref Args args)
if (isRandomAccessRange!R || isArray!R)
in
{
assert (arr.length == args.length);
}
body
{
foreach (i, ref v; args) {
alias T = typeof(v);
v = arr[i].to!T;
}
}
void verbose(Args...)(in Args args)
{
stderr.write("[");
foreach (i, ref v; args) {
if (i) stderr.write(", ");
stderr.write(v);
}
stderr.writeln("]");
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
const tmp = readln.split.to!(long[]);
writeln(tmp[0] < tmp[2] && tmp[2] < tmp[1] || tmp[0] > tmp[2] && tmp[2] > tmp[1] ? "Yes" : "No");
}
|
D
|
import std.stdio, std.conv, std.string;
void main() {
int N = readln().chomp().to!(int);
long [256] cnt;
foreach(i; 0..256) cnt[i] = 0;
foreach(i; 0..N) {
string S;
S = readln();
cnt[S[0]]++;
}
long res = 0;
string m = "MARCH";
foreach(i; 0..5) {
foreach(j; (i+1)..5) {
foreach(k; (j+1)..5) {
res += cnt[m[i]] * cnt[m[j]] * cnt[m[k]];
}
}
}
writeln(res);
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
int calc(int[] a) {
int ans = 0;
for (int i = 2; i < a.length; i++) {
int mi = min(a[i-2], a[i]);
int ma = max(a[i-2], a[i]);
if (mi < a[i-1] && a[i-1] < ma) ans++;
}
return ans;
}
void main() {
readint;
auto a = readints;
writeln(calc(a));
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long A, B;
scan(A, B);
writeln(A * B % 2 == 0 ? "Even" : "Odd");
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
int calc(int n) {
int ans = 0;
for (int i = 1; i <= n; i += 2) {
int x = 0;
for (int j = 1; j <= i; j++) {
if (i % j == 0) x++;
}
if (x == 8) ans++;
}
return ans;
}
void main() {
int n = readint;
writeln(calc(n));
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto T = readln.chomp.to!int;
foreach (_; 0..T) {
auto S = readln.chomp;
int max_l, l;
foreach (c; S) {
if (c == 'L') {
l += 1;
max_l = max(l, max_l);
} else {
l = 0;
}
}
writeln(max_l + 1);
}
}
|
D
|
module app;
import core.bitop;
import std.algorithm;
import std.array;
import std.bigint;
import std.conv;
import std.stdio;
import std.string;
import std.traits;
struct Input
{
ulong n;
int a, b;
}
void parseInput(T)(out Input input, T file)
{
with (file) with (input)
{
auto ar = readln().strip().split().map!(to!int).array();
n = ar[0].to!int;
a = ar[1].to!int;
b = ar[2].to!int;
}
}
alias ModLong = Mod!(long, 10 ^^ 9 + 7);
auto main2(Input* input)
{
with (input)
{
ModLong sum = 2;
sum ^^= n;
sum -= combinationMod(n, a);
sum -= combinationMod(n, b);
sum -= 1;
return sum.value;
}
}
auto combinationMod(ulong n, ulong r)
{
ModLong numerator = 1;
foreach (i; (n-r+1)..(n+1))
numerator *= i;
ModLong denominator = 1;
foreach (i; 1..(r+1))
denominator *= i;
denominator ^^= (10^^9+7-2);
return numerator * denominator;
}
alias retType = ReturnType!main2;
static if (!is(retType == void))
{
unittest { writeln("begin unittest"); }
auto _placeholder_ = ReturnType!main2.init;
unittest // example1
{
string example =
`4 1 3`;
if (example.empty) return;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
printResult(result);
assert(result == 7);
}
unittest // example2
{
string example =
`1000000000 141421 173205`;
if (example.empty) return;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
printResult(result);
assert(result == 34076506);
}
unittest { writeln("end unittest"); }
void parseExample(out Input input, string example)
{
struct Adapter
{
string[] _lines;
this(string input) { _lines = input.splitLines(); }
string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; }
}
parseInput(input, Adapter(example));
}
}
void printResult(T)(T result)
{
static if (isFloatingPoint!T) writefln("%f", result);
else writeln(result);
}
void main()
{
Input input = void;
parseInput(input, stdin);
alias retType = ReturnType!main2;
static if (is(retType == void))
main2(&input);
else
{
auto result = main2(&input);
printResult(result);
}
}
struct Mod(T, T m)
if ((is(T : int) || is(T : long)) && m > 0 && m ^^ 2 <= T.max)
{
private T _value;
invariant { assert(_value >= 0); }
this(T t) { opAssign(t); }
void opAssign(T t) { _value = t % m; }
void opAssign(typeof(this) mod) { opAssign(mod._value); }
typeof(this) opBinary(string op)(T t)
{
static if (op == "*")
{
typeof(this) result;
result = _value * t;
return result;
}
else static if (op == "^^")
{
if (t <= 0)
assert(0);
else if (t == 1)
return this;
else if (t % 2 == 1)
return this * (this ^^ (t - 1));
else
{
auto temp = this ^^ (t / 2);
return temp * temp;
}
}
else static if (op == "-")
{
auto temp = _value - t % m;
if (temp < 0)
temp += m;
typeof(this) result = temp;
return result;
}
else static assert(0);
}
typeof(this) opBinary(string op)(typeof(this) mod) { return opBinary!op(mod._value); }
void opOpAssign(string op)(T t) { this = this.opBinary!op(t); }
void opOpAssign(string op)(typeof(this) mod) { opOpAssign!op(mod._value); }
bool opEquals()(auto ref const T t) const { return _value == t; }
bool opEquals()(auto ref const typeof(this) mod) const { return opEquals(mod._value); }
T value() const { return _value; }
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.typecons;
import std.numeric, std.math;
import core.bitop;
string FMT_F = "%.10f";
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 N = RD;
long ans = long.max;
long i;
while (i^^2 <= N)
{
++i;
if (N % i != 0) continue;
auto x = max(N / i, i);
//stderr.writeln(i, " ", x);
long cnt;
while (x != 0)
{
++cnt;
x /= 10;
}
ans = min(ans, cnt);
}
writeln(ans);
stdout.flush();
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
import std.concurrency;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;
void main() {
char[] str = readln.chomp.map!(to!char).array;
char[] s = new char[str.length];
int[] cnt = new int[str.length];
int index = 0;
foreach(c; str) {
if (c=='x') {
cnt[index]++;
} else {
s[index] = c;
index++;
}
}
s.length = index;
cnt.length = index+1;
int N = s.length.to!int;
foreach(i; 0..N/2) {
if (s[i] != s[N-i-1]) {
(-1).writeln;
return;
}
}
long ans = 0;
foreach(i; 0..(N+1)/2) {
ans += abs(cnt[i] - cnt[N-i]);
}
ans.writeln;
}
// ----------------------------------------------
void scanln(Args...)(ref Args args) {
foreach(i, ref v; args) {
"%d".readf(&v);
(i==args.length-1 ? "\n" : " ").readf;
}
// ("%d".repeat(args.length).join(" ") ~ "\n").readf(args);
}
void times(alias fun)(int n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
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 {
return reduce!fun(tuple(seed), r);
}
}
}
}
// cumulativeFold was added in D 2.072.0
static if (__VERSION__ < 2072) {
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);
}
}
}
// minElement/maxElement was added in D 2.072.0
static if (__VERSION__ < 2072) {
auto minElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto minimum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b < minimum) {
element = a;
minimum = b;
}
}
return element;
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto maximum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b > maximum) {
element = a;
maximum = b;
}
}
return element;
}
}
|
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); }
struct SegTree(T, E)
{
alias T delegate(T, E) G;
alias T delegate(E, E) H;
int n;
G g; H h;
T d1; E d0;
T[] dat; E[] laz;
this(int _n, G _g, H _h, T _d1, E _d0, T[] v = [])
{
g = _g; h = _h;
d1 = _d1; d0 = _d0;
init(_n);
if (_n == cast(int)v.length) build(_n, v);
}
void init(int _n)
{
n = 1;
while (n < _n) n *= 2;
dat.length = n; dat[] = d1;
laz.length = 2*n-1; laz[] = d0;
}
void build(int _n, T[] v)
{
foreach (i; 0.._n) dat[i] = v[i];
}
void update(int a, int b, E x, int k, int l, int r)
{
if (r <= a || b <= l) return;
if (a <= l && r <= b)
{
laz[k] = h(laz[k], x);
return;
}
update(a, b, x, k*2+1, l, (l+r)/2);
update(a, b, x, k*2+2, (l+r)/2, r);
}
void update(int a, int b, E x)
{
update(a, b, x, 0, 0, n);
}
T query(int k)
{
T c = dat[k];
k += n-1;
E x = laz[k];
while (k > 0)
{
k = (k-1)/2;
x = h(x,laz[k]);
}
return g(c,x);
}
}
void main()
{
auto N = RD!int;
auto Q = RD;
long f(long a, long b)
{
return min(a, b);
}
auto st1 = new SegTree!(long, long)(N, &f, &f, N-1, N-1);
auto st2 = new SegTree!(long, long)(N, &f, &f, N-1, N-1);
st1.update(0, N-1, N-1);
st2.update(0, N-1, N-1);
long cnt;
foreach (i; 0..Q)
{
auto num = RD;
auto x = RD!int-1;
if (num == 1)
{
auto y = st1.query(x);
debug writeln("i:", " y:", y);
cnt += y-1;
st2.update(0, cast(int)y, x);
}
else
{
auto y = st2.query(x);
debug writeln("i:", " y:", y);
cnt += y-1;
st1.update(0, cast(int)y, x);
}
}
debug writeln("cnt:", cnt);
long ans = (cast(long)(N-2))^^2 - cnt;
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.format;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void main()
{
int n = readln.chomp.to!int;
char[][] field = new char[][](4, n + 2);
foreach (ref line; field)
{
line[] = '*';
}
int[char] cnt;
foreach (i, c; readln.chomp)
{
field[1][i + 1] = c;
cnt[c]++;
}
foreach (i, c; readln.chomp)
{
field[2][i + 1] = c;
cnt[c]++;
}
bool[char] done;
long sum = 1;
enum MOD = 1_000_000_007L;
bool x;
int ofs;
if (field[1][1] == field[2][1]) {
sum *= 3;
x = true;
ofs = 2;
} else {
sum *= 6;
x = false;
ofs = 3;
}
for (int c = ofs; c <= n;)
{
long t;
bool nx = field[1][c] == field[2][c];
final switch (x << 1 | nx) {
case 0:
sum *= 3;
break;
case 1:
sum *= 1;
break;
case 2:
sum *= 2;
break;
case 3:
sum *= 2;
break;
}
if (nx) {
++c;
} else {
c += 2;
}
x = nx;
sum %= MOD;
}
sum.writeln;
}
|
D
|
/* imports all std modules {{{*/
import
std.algorithm,
std.array,
std.ascii,
std.base64,
std.bigint,
std.bitmanip,
std.compiler,
std.complex,
std.concurrency,
std.container,
std.conv,
std.csv,
std.datetime,
std.demangle,
std.encoding,
std.exception,
std.file,
std.format,
std.functional,
std.getopt,
std.json,
std.math,
std.mathspecial,
std.meta,
std.mmfile,
std.net.curl,
std.net.isemail,
std.numeric,
std.parallelism,
std.path,
std.process,
std.random,
std.range,
std.regex,
std.signals,
std.socket,
std.stdint,
std.stdio,
std.string,
std.system,
std.traits,
std.typecons,
std.uni,
std.uri,
std.utf,
std.uuid,
std.variant,
std.zip,
std.zlib;
/*}}}*/
/* libnum {{{*/
void amax(N, Args...)(ref N x, Args y) {
x = x.max(y);
}
void amin(N, Args...)(ref N x, Args y) {
x = x.min(y);
}
auto maxE(R)(R r) if (isInputRange!R) {
ElementType!R x = r.front;
r.popFront();
foreach (y; r) x.amax(y);
return x;
}
auto minE(R)(R r) if (isInputRange!R) {
ElementType!R x = r.front;
r.popFront();
foreach (y; r) x.amin(y);
return x;
}
/*}}}*/
/+---test
TSTTSS
---+/
/+---test
SSTTST
---+/
/+---test
TSSTTTSS
---+/
void main(string[] args) {
const S = readln.chomp;
long rem, dep;
foreach (c; S) {
if (c == 'S') {
++dep;
}
if (c == 'T' && dep > 0) {
--dep;
++rem;
}
}
(S.length - rem*2).writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void main()
{
auto N = readln.chomp.to!int;
long s, last;
foreach (a; readln.split.to!(long[])) {
if (a >= last) {
last = a;
} else {
s += last - a;
}
}
writeln(s);
}
|
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 bs = readln.split.to!(int[]);
int[] solve(int[] as) {
if (as.empty) {
return [];
} else {
foreach_reverse (i, a; as) {
if (a == i+1) return solve(as[0..i] ~ as[i+1..$]) ~ a;
}
return [-1];
}
}
auto rs = solve(bs);
if (rs[0] == -1) {
writeln("-1");
} else {
foreach (r; rs) writeln(r);
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void main()
{
auto nm = readln.split.to!(int[]);
auto N = nm[0];
auto M = nm[1];
auto xs = new long[int][](M);
foreach (x; readln.split.to!(int[])) {
auto p = x % M;
++xs[p][x];
}
auto ps = new long[](M);
auto rs = new long[](M);
foreach (i, xx; xs) {
foreach (_, v; xx) {
ps[i] += v/2*2;
rs[i] += v%2;
}
}
long res = ps[0]/2 + rs[0]/2;
foreach (i; 1..M) {
auto j = M-i;
if (i >= j) {
if (i == j) res += ps[i]/2 + rs[i]/2;
break;
}
auto r = min(rs[i], rs[j]);
res += r;
rs[i] -= r;
rs[j] -= r;
if (rs[i] > 0) {
auto p = min(rs[i], ps[j]);
res += p;
ps[j] -= p;
} else if (rs[j] > 0) {
auto p = min(ps[i], rs[j]);
res += p;
ps[i] -= p;
}
res += ps[i]/2 + ps[j]/2;
}
writeln(res);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
import std.concurrency;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;
void main() {
auto as = DList!char("AKIHABARA".map!(to!char));
auto bs = DList!char(readln.chomp.map!(to!char));
while(true) {
if (bs.empty) {
as[].all!"a=='A'".pipe!(a => a?"YES":"NO").writeln;
return;
} else if (as.empty) {
"NO".writeln;
return;
}
char a = as.front;
char b = bs.front;
if (a == b) {
as.removeFront;
bs.removeFront;
} else if (a=='A') {
as.removeFront;
} else {
"NO".writeln;
return;
}
}
}
// ----------------------------------------------
void scanln(Args...)(ref Args args) {
foreach(i, ref v; args) {
"%d".readf(&v);
(i==args.length-1 ? "\n" : " ").readf;
}
// ("%d".repeat(args.length).join(" ") ~ "\n").readf(args);
}
void times(alias fun)(int n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
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 {
return reduce!fun(tuple(seed), r);
}
}
}
}
// cumulativeFold was added in D 2.072.0
static if (__VERSION__ < 2072) {
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);
}
}
}
// minElement/maxElement was added in D 2.072.0
static if (__VERSION__ < 2072) {
auto minElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto minimum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b < minimum) {
element = a;
minimum = b;
}
}
return element;
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto maximum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b > maximum) {
element = a;
maximum = b;
}
}
return element;
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.container;
import std.bigint;
import std.math;
void main()
{
auto s = readln.chomp.to!int;
writeln(s/3600, ":", s%3600/60, ":", s%60);
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto n = readln.chomp.to!size_t;
bool[string] di;
foreach (_; n.iota) {
auto rd = readln.chomp.splitter(' ');
auto cmd = rd.front; rd.popFront;
auto str = rd.front;
switch (cmd) {
case "insert":
di[str] = true;
break;
case "find":
writeln(str in di ? "yes" : "no");
break;
default:
assert(0);
}
}
}
|
D
|
import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;
int[int[]] memo;
int dfs(int[] a, int depth = 0)
{
if (a.reduce!max >= 100) {
return 1 << 28;
}
if (a in memo) {
return memo[a];
}
if (a[0] == a[1] && a[0] == a[2]) {
return depth;
}
int ans = int.max;
for (int i = 0; i < 3; ++i) {
for (int j = i + 1; j < 3; ++j) {
int[] b = a.dup;
b[i]++;
b[j]++;
ans = min(ans, dfs(b, depth + 1));
}
}
for (int i = 0; i < 3; ++i) {
int[] b = a.dup;
b[i] += 2;
ans = min(ans, dfs(b, depth + 1));
}
memo[a.idup] = ans;
return ans;
}
void main()
{
auto arr = readln.chomp.split.map!(to!int).array;
dfs(arr).writeln;
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.range;
import std.array;
import std.conv;
import std.complex;
import std.math;
import std.ascii;
import std.bigint;
import std.container;
import std.typecons;
auto readInts() {
return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
return readInts()[0];
}
auto readLongs() {
return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
return readLongs()[0];
}
void readlnTo(T...)(ref T t) {
auto s = readln().split();
assert(s.length == t.length);
foreach(ref ti; t) {
ti = s[0].to!(typeof(ti));
s = s[1..$];
}
}
const real eps = 1e-10;
void main(){
long h, w, a, b;
readlnTo(h, w, a, b);
long p = 1000000000L+7;
long [long[]] memo;
long pow_mod(long n, long k) {
if(k == 0) {
return 1;
}
auto tmp = pow_mod(n, k/2);
tmp = (tmp*tmp)%p;
if(k%2 == 1) {
tmp = (tmp*n)%p;
}
return tmp;
}
long c(long n, long k) {
if(([n, k] in memo) !is null) {
return memo[[n,k]];
}
long ans;
if(k == 0) {
ans = 1;
} else {
ans = (((c(n, k-1)*(n-k+1))%p)*pow_mod(k, p-2))%p;
}
memo[[n,k].idup] = ans;
return ans;
}
for(int i; i <= h-a-1+b; ++i) {
c(h-a-1+b, i);
}
for(int i; i <= a+w-b-1; ++i) {
c(a+w-b-1, i);
}
long ans;
for(int i = 1; i <= min(h-a, w-b); ++i) {
ans += c(h-a-1+b, h-a-i)*c(a-b+w-1, a-1+i);
ans %= p;
}
writeln(ans);
}
|
D
|
import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.format;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void main()
{
int n = readln.chomp.to!int;
int k = readln.chomp.to!int;
int sum = 0;
foreach (x; readln.chomp.split.map!(to!int)) {
sum += min(abs(x - k), x) * 2;
}
sum.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 readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}
const inf = 10^^9;
void main()
{
int n; readV(n);
int[] p; readA(n-1, p); p[] -= 1;
int[] x; readA(n, x);
auto c = new int[][](n);
foreach (i; 1..n) c[p[i-1]] ~= i;
auto nc = new int[](n);
foreach (i; 0..n) nc[i] = c[i].length.to!int;
auto y = new int[](n);
foreach_reverse (i; 0..n) {
auto dp = new int[][](nc[i]+1, x[i]+1);
foreach (ref dpi; dp) dpi[] = inf;
dp[0][0] = 0;
foreach (j; 0..nc[i]) {
auto xj = x[c[i][j]], yj = y[c[i][j]];
foreach (k; 0..x[i]+1) {
if (k >= xj) dp[j+1][k] = min(dp[j+1][k], dp[j][k-xj]+yj);
if (k >= yj) dp[j+1][k] = min(dp[j+1][k], dp[j][k-yj]+xj);
}
}
y[i] = dp[$-1].reduce!min;
if (y[i] >= inf) {
writeln("IMPOSSIBLE");
return;
}
}
writeln("POSSIBLE");
}
|
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;
int color (int[] c1, int[] c2) {
return (c1[0] - c2[0])^^2 + (c1[1] - c2[1])^^2 + (c1[2] - c2[2])^^2;
}
void main()
{
while (1) {
if (!readln.chomp.to!int) break;
auto x = readln.chomp.split.map!(to!int).array;
auto num = 32;
int turn;
bool ply;
while (num > 0) {
if (ply) {
num -= x[turn++%x.length];
} else {
num -= (num - 1) % 5;
}
if (num < 0) num = 0;
num.writeln;
ply = !ply;
}
}
}
|
D
|
import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;
void main(){
string s = readln.chomp;
bool[char] flag;
foreach(c; s) flag[c] = 1;
string ans = "Yes";
if('N' in flag && !('S' in flag)) ans = "No";
if('S' in flag && !('N' in flag)) ans = "No";
if('E' in flag && !('W' in flag)) ans = "No";
if('W' in flag && !('E' in flag)) ans = "No";
ans.writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.array;
void main()
{
int N = readln.chomp.to!int;
int[3][] price = new int[3][](N);
foreach (i; 0..N) price[i] = readln.split.to!(int[]);
int[3][] max_sum = new int[3][](N);
max_sum[0] = price[0];
foreach (i; 1..N){
foreach (j; 0..3){
int max_p = 0;
foreach (k; 0..3){
if (k == j) continue;
if (max_sum[i - 1][k] > max_p) max_p = max_sum[i - 1][k];
}
max_sum[i][j] = max_p + price[i][j];
}
}
int max = max_sum[$ - 1][0];
foreach (i; 1..3){
if (max_sum[$ - 1][i] > max) max = max_sum[$ - 1][i];
}
writeln(max);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.typecons;
void main() {
auto S = readln;
int[char] count;
foreach (c; S) {
if (c != '\n')
count[c]++;
}
int[] data;
int oddCount = 0;
foreach(int co; count) {
if (co % 2 == 1) oddCount++;
else data ~= co / 2;
}
if (oddCount > 1) {
writeln("0");
return;
}
ulong evenCount = S.length - oddCount;
if (evenCount == 0) {
writeln("1");
return;
}
//evenCount / 2???????????????????????????????????????)
auto a = evenCount / 2;
//data??????????????????????????§?????§?¨???????????????????
auto combi(ulong a, ulong b) {
auto u = 1, d = 1;
foreach (i; 0..b) {
u *= a;
a--;
}
foreach (j; 0..b) {
d *= b - j;
}
return u / d;
}
ulong result = 1;
foreach (int co; data) {
result *= combi(a, co);
a -= co;
}
writeln(result);
}
|
D
|
void main() {
int[] tmp = readln.split.to!(int[]);
int k = tmp[0], s = tmp[1];
int cnt;
foreach (x; 0 .. k+1) {
foreach (y; 0 .. k+1) {
int z = s - x - y;
if (0 <= z && z <= k) ++cnt;
}
}
cnt.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
|
/+ dub.sdl:
name "B"
dependency "dcomp" version=">=0.4.0"
+/
import std.stdio, std.algorithm, std.conv, std.range;
import std.container;
// import dcomp.scanner;
int main() {
auto sc = new Scanner(stdin);
int n;
long a, b;
long[] x;
sc.read(n, a, b, x);
long ans = 0;
writeln(iota(0, n-1).map!(i => min(b, a * (x[i+1]-x[i]))).sum);
return 0;
}
/* 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;
}
string[] buf;
private bool succ() {
while (!buf.length) {
if (f.eof) return false;
buf = f.readln.split;
}
return true;
}
private bool readSingle(T)(ref T x) {
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
//string or char[10] etc
x = buf.front;
buf.popFront;
} else {
static if (isStaticArray!T) {
//static
assert(buf.length == T.length);
}
x = buf.map!(to!E).array;
buf.length = 0;
}
} else {
x = buf.front.to!T;
buf.popFront;
}
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]));
}
|
D
|
void main(){
string[] val = inln!string();
bool a = (val[0]=="H")?true:false;
bool b = (val[1]=="H")?true:false;
writeln(( (a^b)==false)?'H':'D');
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range;
const long mod = 10^^9+7;
// 1要素のみの入力
T inelm(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] inln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split())ln ~= elm.to!T();
return ln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main()
{
auto N = readln.chomp.to!int;
int ans = 0;
foreach(_; 0..N){
auto l = readln.chomp.split.map!(to!int);
ans += l[1] - l[0] + 1;
}
writeln(ans);
}
|
D
|
void main() {
auto N = ri;
int[string] count;
foreach(_; 0..N) {
count[rs]++;
}
auto M = ri;
foreach(_; 0..M) {
count[rs]--;
}
count.values.reduce!max.max(0).writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
const long mod = 1_000_000_007;
void main(){
int n = readln.chomp.to!int;
long[] as = readln.chomp.split.to!(long[]);
long m = 1;
long ans;
A: while(1){
long x;
foreach(a; as) if(a % 2 > 0) x += 1;
x *= (n - x), x %= mod;
x *= m, x %= mod;
ans += x, ans %= mod;
m *= 2, m %= mod;
foreach(ref a; as) a /= 2;
foreach(a; as) if(a > 0) continue A;
break;
}
ans.writeln;
}
|
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()
{
auto S = readln.strip;
char c='a';
long res=long.max;
while(isASCII(c))
{
res = min(res, S.split(c).map!"a.length".fold!max);
c++;
}
writeln(res);
}
class UnionFind{
UnionFind parent = null;
void merge(UnionFind a)
{
if(same(a)) return;
a.root.parent = this.root;
}
UnionFind root()
{
if(parent is null)return this;
return parent = parent.root;
}
bool same(UnionFind a)
{
return this.root == a.root;
}
}
void scanValues(TList...)(ref TList list)
{
auto lit = readln.splitter;
foreach (ref e; list)
{
e = lit.fornt.to!(typeof(e));
lit.popFront;
}
}
T[] scanArray(T = long)()
{
return readln.split.to!(long[]);
}
void scanStructs(T)(ref T[] t, size_t n)
{
t.length = n;
foreach (ref e; t)
{
auto line = readln.split;
foreach (i, ref v; e.tupleof)
{
v = line[i].to!(typeof(v));
}
}
}
long scanULong(){
long x;
while(true){
const c = getchar;
if(c<'0'||c>'9'){
break;
}
x = x*10+c-'0';
}
return x;
}
T scanElem(T = long)()
{
char[] res;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
res ~= cast(char) c;
c = getchar;
}
return res.strip.to!T;
}
template fold(fun...) if (fun.length >= 1)
{
auto fold(R, S...)(R r, S seed)
{
static if (S.length < 2)
{
return reduce!fun(seed, r);
}
else
{
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
struct Factor
{
long n;
long c;
}
Factor[] factors(long n)
{
Factor[] res;
for (long i = 2; i ^^ 2 <= n; i++)
{
if (n % i != 0)
continue;
int c;
while (n % i == 0)
{
n = n / i;
c++;
}
res ~= Factor(i, c);
}
if (n != 1)
res ~= Factor(n, 1);
return res;
}
long[] primes(long n)
{
if(n<2)return [];
auto table = new long[n+1];
long[] res;
for(int i = 2;i<=n;i++)
{
if(table[i]==-1) continue;
for(int a = i;a<table.length;a+=i)
{
table[a] = -1;
}
res ~= i;
}
return res;
}
bool isPrime(long n)
{
if (n <= 1)
return false;
if (n == 2)
return true;
if (n % 2 == 0)
return false;
for (long i = 3; i ^^ 2 <= n; i += 2)
if (n % i == 0)
return false;
return true;
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.functional;
import std.array;
import std.conv;
import std.math;
import std.typecons;
import std.regex;
import std.range;
const int INF = 100000000;
int[21][21] map;
int n;
int ans;
bool check(int x,int u){
int count;
for(int i=1;i<=n;i++)
if(map[x][i] > 0 && (u&(1<<i))==0) count++;
if(count == 1) return true;
else return false;
}
int saiki(int pos,int u,int v,int us){
us |= 1<<pos;
if(v > ans) return INF;
int res = INF;
int count;
bool flg = true;
for(int i=1;i<=n;i++){
if(map[pos][i] > 0 && (u&(1<<i)) == 0 ){
if(check(i,u)){
u |= 1<<i;
flg = false;
}
}
}
for(int i=1;i<=n;i++){
if( (u&(1<<i)) != 0 ) count++;
}
if(count == n-1) {
ans = min(ans,v);
return v;
}
for(int i=1;i<=n;i++){
if( (u&(1<<i)) == 0 && map[pos][i] > 0){
if(flg && (us&(1<<i))!=0 ) continue;
res = min(res,saiki(i,u,v+map[pos][i],us) );
}
}
return res;
}
void main(){
while(true){
n = readln().chomp().to!int;
ans = INF;
map = new int[21][21];
if(n==0) break;
for(int i=0;i<n-1;i++){
auto s = readln().split().to!(int[]);
map[s[0]][s[1]] = s[2];
map[s[1]][s[0]] = s[2];
}
writeln(saiki(1,0,0,0));
}
}
|
D
|
import std.stdio, std.range, std.conv, std.string, std.array, std.functional, std.math;
import std.algorithm.comparison, std.algorithm.iteration, std.algorithm.mutation, std.algorithm.searching, std.algorithm.setops, std.algorithm.sorting;
import std.container.binaryheap;
import std.typecons;
void main()
{
const input = readln().strip.split.to!(long[]);
foreach(e; 1..4)
{
if(input[0] * input[1] * e % 2 == 1)
{
writeln("Yes");
return;
}
}
writeln("No");
}
|
D
|
import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;
void main(){
int n, a;
{
int[] cs = readln.chomp.split.map!(to!int).array;
n = cs[0], a = cs[1];
}
int[] xs = readln.chomp.split.map!(to!int).array;
int[] ys = xs.map!(x => x - a).array;
long[5001] count;
count[2500] = 1;
foreach(y; ys){
long[5001] tmpcount;
foreach(i; 0 .. 5000){
tmpcount[i] += count[i];
if(i + y >= 0 && i + y < 5001) tmpcount[i + y] += count[i];
}
foreach(i; 0 .. 5001) count[i] = tmpcount[i];
}
(count[2500] - 1).writeln;
}
|
D
|
import std.stdio, std.algorithm, std.string, std.string, std.conv, std.array, std.range, std.math;
int readint() { return readln.chomp.to!int; }
int[] readints() { return readln.split.to!(int[]); }
void main() {
auto hwd = readints();
int h = hwd[0], w = hwd[1], d = hwd[2];
int[] px = new int[h*w+5];
int[] py = new int[h*w+5];
foreach(y; 0..h) {
int[] a = readints();
foreach(int x, int v; a) {
px[v] = x;
py[v] = y;
}
}
int[] ds = new int[h*w+5];
foreach_reverse(i; 0..h*w-d+1) {
ds[i] = ds[i+d] + abs(px[i] - px[i+d]) + abs(py[i] - py[i+d]);
}
int q = readint();
while(q--) {
int[] lr = readints();
writeln(ds[lr[0]] - ds[lr[1]]);
}
string _ = readln(); // dbg
}
|
D
|
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
enum MO = 10L^^9 + 7;
void main() {
try {
for (; ; ) {
const N = readInt();
const K = readLong();
auto A = new int[N];
foreach (i; 0 .. N) {
A[i] = readInt();
}
long ans;
long inside;
foreach (i; 0 .. N) foreach (j; i + 1 .. N) {
if (A[i] > A[j]) {
++inside;
}
}
ans += inside * K;
ans %= MO;
foreach (i; 0 .. N) {
long cnt;
foreach (j; 0 .. N) {
if (A[j] < A[i]) {
++cnt;
}
}
ans += cnt * ((K * (K - 1) / 2) % MO);
ans %= MO;
}
writeln(ans);
}
} catch (EOFException e) {
}
}
|
D
|
import std;
int calc(string s) {
int[2019] a;
a[0] = 1;
int ans = 0;
int p = 1;
int now = 0;
foreach_reverse (c; s) {
now = (now + p * (c - '0')) % 2019;
p = p * 10 % 2019;
ans += a[now];
a[now]++;
}
return ans;
}
void main() {
string s = read!string;
writeln(calc(s));
}
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
|
D
|
void main()
{
long[] tmp = readln.split.to!(long[]);
long n = tmp[0], m = tmp[1];
bool[] boxes = new bool[n];
boxes[0] = true;
long[] balls = new long[n];
balls[] = 1;
foreach (i; 0 .. m)
{
tmp = readln.split.to!(long[]);
long x = tmp[0] - 1, y = tmp[1] - 1;
--balls[x], ++balls[y];
if (boxes[x]) boxes[y] = true;
if (balls[x] < 1) boxes[x] = false;
}
boxes.count(true).writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm, std.array, std.range, std.container;
import std.math;
void main() {
auto N = readln.split[0].to!int;
bool[string] S;
foreach (i; 0 .. N) {
S[readln.split[0]] = true;
}
writeln(S.keys.length);
}
|
D
|
import std.stdio,std.algorithm;void main(){readln;writeln(readln.canFind("Y")?"Four":"Three");}
|
D
|
import std.stdio;
import std.math;
import std.conv;
import std.string;
import std.algorithm;
import std.array;
void main() {
while(true) {
string line = readln.chomp;
if (stdin.eof) break;
uint[] data = line.split(" ").map!(to!(uint)).array;
uint a = data[0];
uint b = data[1];
uint n = data[2];
uint sum = 0;
a %= b;
for(uint i = 0; i < n; i++) {
a *= 10;
sum += a / b;
a %= b;
if (a == 0) break;
}
writeln(sum);
}
}
|
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.split.map!(to!int);
foreach_reverse (i; 1..max(x[0], x[1])+1) {
if (x[0] % i == 0 && x[1] % i == 0) {
i.writeln;
break;
}
}
}
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
immutable long MOD = 10^^9 + 7;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto M = s[1];
auto S = readln.chomp;
auto LR = M.iota.map!(_ => readln.split.map!(a => a.to!int - 1).array).array;
auto zeros = new long[](N+1);
foreach (i; 0..N) {
zeros[i+1] = zeros[i] + (S[i] == '0');
}
auto dp = new long[][](N+1, N+1);
dp[0][0] = 1;
int rmax = 0;
int q = 0;
foreach (i; 0..N) {
while (q < M && LR[q][0] <= i) {
rmax = max(LR[q][1], rmax);
q += 1;
}
rmax = max(rmax, i);
long zero = zeros[rmax+1];
long one = rmax + 1 - zero;
foreach (j; 0..i+1) {
long rest_zero = zero - j;
long rest_one = one - (i - j);
if (rest_zero < 0 || rest_one < 0) {
continue;
}
if (rest_zero > 0) {
dp[i+1][j+1] += dp[i][j];
dp[i+1][j+1] %= MOD;
}
if (rest_one > 0) {
dp[i+1][j] += dp[i][j];
dp[i+1][j+1] %= MOD;
}
}
}
long ans = 0;
foreach (a; dp[N]) {
ans += a;
ans %= MOD;
}
ans.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 w, a, b; readV(w, a, b);
if (a > b) swap(a, b);
writeln(max(0, b-(a+w)));
}
|
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()
{
auto n = readln.chomp.to!int;
long sum;
foreach (i; 0..n) {
sum += readln.chomp.to!long;
}
writeln(sum / n);
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
alias T=long;
const inf=1e9.to!(T);
struct Edge{int to; T cost;}
int n; rd(n);
auto g=new Edge[][](n);
foreach(i; 0..n){
auto args=readln.split.to!(int[]);
foreach(j; 0..args[1]){
g[i]~=Edge(args[j*2+2], args[j*2+3]);
}
}
T[] dijkstra(Edge[][] g, int s){
auto d=new T[](n);
fill(d, inf);
struct P{int v; T d;}
import std.container;
// auto Q=new RedBlackTree!(P, "a.d<b.d", true);
auto Q=new MyBinaryHeap!(P, "a.d<b.d");
Q.insert(P(s, 0)); d[s]=0;
while(Q.empty==false){
auto cur=Q.front; Q.removeFront;
foreach(e; g[cur.v]){
if(d[e.to]<=d[cur.v]+e.cost) continue;
d[e.to]=d[cur.v]+e.cost;
Q.insert(P(e.to, d[e.to]));
}
}
return d;
}
auto d=dijkstra(g, 0);
foreach(i; 0..n) writeln(i, " ", d[i]);
}
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));
}
import std.functional : binaryFun;
class MyBinaryHeap(T, alias less = "a<b") if(is(typeof(binaryFun!less(T.init, T.init))))
{
import std.exception : enforce;
import std.algorithm : each, swap;
import std.functional : binaryFun;
private T[] dat;
private alias comp = binaryFun!(less);
this()
{
}
this(T[] arr)
{
arr.each!(x => insert(x));
}
void insert(T x)
{
size_t i = dat.length;
dat ~= x;
while (i > 0)
{
size_t p = (i - 1) / 2;
if (comp(dat[i], dat[p]))
{
swap(dat[i], dat[p]);
i = p;
}
else
{
break;
}
}
}
T front()
{
enforce(dat.length > 0, "Cannot call front on an empty heap."); // https://github.com/dlang/phobos/blob/master/std/container/binaryheap.d#L248
return dat[0];
}
void removeFront()
{
enforce(dat.length > 0, "Cannot call removeFront on an empty heap."); // https://github.com/dlang/phobos/blob/master/std/container/binaryheap.d#L316
swap(dat[0], dat[$ - 1]);
dat = dat[0 .. ($ - 1)];
size_t len = dat.length;
size_t i = 0;
while (i * 2 + 1 < len)
{
if (i * 2 + 2 < len)
{
size_t j = comp(dat[i * 2 + 1], dat[i * 2 + 2]) ? i * 2 + 1 : i * 2 + 2; // 左右で強いほうの子と比べる
if (comp(dat[j], dat[i]))
{
swap(dat[j], dat[i]);
i = j;
}
else
{
break;
}
}
else
{ // 左の子しかない
if (comp(dat[i * 2 + 1], dat[i]))
{
swap(dat[i * 2 + 1], dat[i]);
i = i * 2 + 1;
}
else
{
break;
}
}
}
}
bool empty(){
return dat.length==0;
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
void main(){
auto x=readln.chomp.to!int;
if(x<1200)writeln("ABC");
else writeln("ARC");
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd1 = readln.split.to!(size_t[]), n = rd1[0], m = rd1[1];
auto c = new int[](n);
foreach (_; 0..m) {
auto rd2 = readln.splitter;
auto a = rd2.front.to!size_t-1; rd2.popFront();
auto b = rd2.front.to!size_t-1;
if (a > b) swap(a, b);
if (a == 0) ++c[b];
if (b == n-1) ++c[a];
}
writeln(c.canFind!"a>1" ? "POSSIBLE" : "IMPOSSIBLE");
}
|
D
|
import std.stdio, std.string, std.array, std.conv, std.algorithm;
void main() {
int[] tmp = readln.chomp.split.to!(int[]);
int[] h = readln.chomp.split.to!(int[]);
h.count!("a >= b")(tmp[1]).writeln;
}
|
D
|
void main()
{
long n, k;
rdVals(n, k);
long a = n % k;
min(a, k-a).writeln;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
enum double eps = 1.0e-9;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
import std.stdio, std.algorithm, std.string, std.conv, std.array, std.range, std.math, core.stdc.stdio;
int readint() { return readln.chomp.to!int; }
int[] readints() { return readln.split.to!(int[]); }
int f(string n) {
int k = n.length.to!int;
int r = (n[0] - '0') + (k - 1) * 9;
if (count(n[1..$], '9') != k - 1) r--;
return r;
}
void main() {
writeln(readln.strip.f);
auto _ = readln(); // dbg
}
|
D
|
// Vicfred
// https://atcoder.jp/contests/abc043/tasks/arc059_a
// brute force
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;
void main() {
int n = readln.chomp.to!int;
int[] a = readln.split.map!(to!int).array;
long minima = long.max;
for(int i = -100; i <= 100; i++) {
long cost = 0;
foreach(item; a)
cost += (item - i)^^2;
minima = min(minima, cost);
}
minima.writeln;
}
|
D
|
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void main ()
{
string s;
while ((s = readln.strip) != "")
{
int res1 = 0;
int res2 = 0;
foreach (i, c; s)
{
res1 += (c != ('0' ^ (i & 1)));
res2 += (c != ('1' ^ (i & 1)));
}
writeln (min (res1, res2));
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.15f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto K = RD;
auto Q = RD;
auto cnt = new long[](N);
foreach (i; 0..Q)
{
auto A = RD-1;
++cnt[A];
}
foreach (i; 0..N)
{
if (K - Q + cnt[i] > 0)
writeln("Yes");
else
writeln("No");
}
stdout.flush();
debug readln();
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dunkelheit" version=">=0.9.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
int main() {
Scanner sc = new Scanner(stdin);
scope(exit) assert(!sc.hasNext);
int[long] mp;
int n;
sc.read(n);
long[] a;
sc.read(a);
long sm = 0;
mp[sm] = 1;
long ans = 0;
foreach (i; 0..n) {
sm += a[i];
if (sm in mp) ans += mp[sm];
if (sm in mp) {
mp[sm] = mp[sm] + 1;
} else {
mp[sm] = 1;
}
}
writeln(ans);
return 0;
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */
// module dkh.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
void read(Args...)(auto ref Args args) {
import std.exception;
static if (args.length != 0) {
enforce(readSingle(args[0]));
read(args[1..$]);
}
}
bool hasNext() {
return succ();
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
int n = readint;
writeln(180 * (n - 2));
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
void scan(T...)(ref T args) {
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;
}
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int N, M;
scan(N, M);
auto cost = new int[][](N, N);
fillAll(cost, inf);
foreach (i ; 0 .. M) {
int u, v, d;
scan(u, v, d);
cost[u][v] = d;
}
auto dp = new int[][](1 << N, N);
fillAll(dp, inf);
foreach (i ; 1 .. N) {
dp[1 << i][i] = cost[0][i];
}
foreach (mask ; 0 .. 1 << N) {
foreach (u ; 0 .. N) if (mask & 1 << u) {
auto sub = mask ^ 1 << u;
foreach (v ; 0 .. N) if (sub & 1 << v) {
chmin(dp[mask][u], dp[sub][v] + cost[v][u]);
}
}
}
auto ans = dp[(1 << N) - 1][0];
if (ans == inf) ans = -1;
writeln(ans);
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
void main() {
int[] d;
foreach (string l; lines(stdin)) d ~= l.chomp.to!int;
d.sort!"a > b"[0..3].each!writeln;
}
|
D
|
import std.stdio, std.string, std.conv, std.array, std.algorithm;
void main() {
auto a = readln.split.map!(to!int).array;
writeln(a[0]*a[1], " ",(a[0]+a[1])*2);
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n; readV(n);
int[] a; readA(n, a);
auto mi = a.reduce!min, ma = a.reduce!max;
auto t = (n.to!long*(n+1)/2+1)/2;
auto calc(int x)
{
auto b = a.map!(ai => ai >= x ? +1 : -1).array;
auto s = new int[](n+1);
foreach (i; 0..n) s[i+1] = s[i] + b[i];
auto ft = new FenwickTree!int(n*2+1), ans = 0L;
foreach (i; 0..n+1) {
ans += ft[0..s[i]+n+1];
++ft[s[i]+n];
}
return ans < t;
}
auto bs = iota(mi, ma+1).map!(x => tuple(x, calc(x))).assumeSorted!"a[1]<b[1]";
writeln(bs.lowerBound(tuple(0, true)).back[0]);
}
class FenwickTree(T)
{
const size_t n;
T[] buf;
this(size_t n)
{
this.n = n;
this.buf = new T[](n+1);
}
void opIndexOpAssign(string op)(T val, size_t i) if (op == "+" || op == "-")
{
++i;
for (; i <= n; i += i & -i) mixin("buf[i] " ~ op ~ "= val;");
}
void opIndexUnary(string op)(size_t i) if (op == "++" || op == "--")
{
++i;
for (; i <= n; i += i & -i) mixin("buf[i]" ~ op ~ ";");
}
pure T opSlice(size_t r, size_t l) { return get(l) - get(r); }
pure T opIndex(size_t i) { return opSlice(i, i+1); }
pure size_t opDollar() { return n; }
private:
pure T get(size_t i)
{
auto s = T(0);
for (; i > 0; i -= i & -i) s += buf[i];
return s;
}
}
|
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 A = RD;
auto B = RD;
auto C = RD;
auto ans = (A == B && A != C) || (A == C && A != B) || (B == C && A != B);
writeln(ans ? "Yes" : "No");
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void main()
{
auto N = readln.chomp.to!int;
int c;
foreach (i, a; readln.split.to!(int[])) if (i%2 == 0 && a%2 == 1) ++c;
writeln(c);
}
|
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 S = RD!string;
writeln(S[0..3]);
stdout.flush;
debug readln;
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
auto ab = readints;
int a = ab[0], b = ab[1];
writeln(a <= b ? a : a - 1);
}
|
D
|
void main() {
auto N = ri;
writeln((N-1)/2);
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.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() {
debug {
bool[long] app;
foreach (i; 0 .. 100) {
for (int a = 0; ; ++a) {
if (a !in app) {
for (int b = 0; ; ++b) {
const c = a ^ b;
if ((b !in app) && (c !in app)) {
writefln("%3d %3d %3d %10b %10b %10b", a, b, c, a, b, c);
app[a] = app[b] = app[c] = true;
goto found;
}
}
}
}
found:
}
}
try {
for (; ; ) {
const numCases = readInt();
foreach (caseId; 0 .. numCases) {
const N = readLong() - 1;
long n = N / 3;
for (int k = 0; ; ++k) {
// a \in [2^(2k), 2^(2k+1))
if (n < 1L << (2 * k)) {
debug {
writefln("k = %s, n = %s", k, n);
}
long a = 1L << (2 * k);
a += n;
long b = 1L << (2 * k + 1);
foreach (i; 0 .. k) {
b |= [0L, 2L, 3L, 1L][cast(int)((a >> (2 * i)) & 3)] << (2 * i);
}
const c = a ^ b;
debug {
writefln("%064b", a);
writefln("%064b", b);
writefln("%064b", c);
}
writeln([a, b, c][cast(int)(N % 3)]);
break;
} else {
n -= 1L << (2 * k);
}
}
}
}
} catch (EOFException e) {
}
}
|
D
|
import std.stdio, std.string, std.conv;
void main() {
int n = readln.chomp.to!int;
int[] fib = new int[n+1];
fib[0] = fib[1] = 1;
foreach (i; 2 .. n+1) {
fib[i] = fib[i-1] + fib[i-2];
}
fib[n].writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.format;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.concurrency;
import std.traits;
import std.uni;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;
enum long INF = long.max/3;
enum long MOD = 10L^^9+7;
void main() {
string str = readln.chomp;
string a = "keyence";
foreach(i; 0..8) {
if (str[0..i] == a[0..i] && str[$-7+i..$] == a[$-7+i..$]) {
"YES".writeln;
return;
}
}
"NO".writeln;
}
// ----------------------------------------------
void times(alias fun)(long n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(long n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
// `(x+y-1)/y` will only work for positive numbers ...
T t = x / y;
if (t * y < x) t++;
return t;
}
T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
T t = x / y;
if (t * y > x) t--;
return t;
}
ref T ch(alias fun, T, S...)(ref T lhs, S rhs) {
return lhs = fun(lhs, rhs);
}
unittest {
long x = 1000;
x.ch!min(2000);
assert(x == 1000);
x.ch!min(3, 2, 1);
assert(x == 1);
x.ch!max(100).ch!min(1000); // clamp
assert(x == 100);
x.ch!max(0).ch!min(10); // clamp
assert(x == 10);
}
mixin template Constructor() {
import std.traits : FieldNameTuple;
this(Args...)(Args args) {
// static foreach(i, v; args) {
foreach(i, v; args) {
mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v;
}
}
}
void scanln(Args...)(auto ref Args args) {
import std.meta;
template getFormat(T) {
static if (isIntegral!T) {
enum getFormat = "%d";
} else static if (isFloatingPoint!T) {
enum getFormat = "%g";
} else static if (isSomeString!T || isSomeChar!T) {
enum getFormat = "%s";
} else {
static assert(false);
}
}
enum string fmt = [staticMap!(getFormat, Args)].join(" ");
string[] inputs = readln.chomp.split;
foreach(i, ref v; args) {
v = inputs[i].to!(Args[i]);
}
}
// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
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 {
return reduce!fun(tuple(seed), r);
}
}
}
}
// cumulativeFold was added in D 2.072.0
static if (__VERSION__ < 2072) {
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);
}
}
}
// minElement/maxElement was added in D 2.072.0
static if (__VERSION__ < 2072) {
private auto extremum(alias map, alias selector = "a < b", Range)(Range r)
if (isInputRange!Range && !isInfinite!Range &&
is(typeof(unaryFun!map(ElementType!(Range).init))))
in
{
assert(!r.empty, "r is an empty range");
}
body
{
alias Element = ElementType!Range;
Unqual!Element seed = r.front;
r.popFront();
return extremum!(map, selector)(r, seed);
}
private auto extremum(alias map, alias selector = "a < b", Range,
RangeElementType = ElementType!Range)
(Range r, RangeElementType seedElement)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void) &&
is(typeof(unaryFun!map(ElementType!(Range).init))))
{
alias mapFun = unaryFun!map;
alias selectorFun = binaryFun!selector;
alias Element = ElementType!Range;
alias CommonElement = CommonType!(Element, RangeElementType);
Unqual!CommonElement extremeElement = seedElement;
alias MapType = Unqual!(typeof(mapFun(CommonElement.init)));
MapType extremeElementMapped = mapFun(extremeElement);
// direct access via a random access range is faster
static if (isRandomAccessRange!Range)
{
foreach (const i; 0 .. r.length)
{
MapType mapElement = mapFun(r[i]);
if (selectorFun(mapElement, extremeElementMapped))
{
extremeElement = r[i];
extremeElementMapped = mapElement;
}
}
}
else
{
while (!r.empty)
{
MapType mapElement = mapFun(r.front);
if (selectorFun(mapElement, extremeElementMapped))
{
extremeElement = r.front;
extremeElementMapped = mapElement;
}
r.popFront();
}
}
return extremeElement;
}
private auto extremum(alias selector = "a < b", Range)(Range r)
if (isInputRange!Range && !isInfinite!Range &&
!is(typeof(unaryFun!selector(ElementType!(Range).init))))
{
alias Element = ElementType!Range;
Unqual!Element seed = r.front;
r.popFront();
return extremum!selector(r, seed);
}
private auto extremum(alias selector = "a < b", Range,
RangeElementType = ElementType!Range)
(Range r, RangeElementType seedElement)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void) &&
!is(typeof(unaryFun!selector(ElementType!(Range).init))))
{
alias Element = ElementType!Range;
alias CommonElement = CommonType!(Element, RangeElementType);
Unqual!CommonElement extremeElement = seedElement;
alias selectorFun = binaryFun!selector;
// direct access via a random access range is faster
static if (isRandomAccessRange!Range)
{
foreach (const i; 0 .. r.length)
{
if (selectorFun(r[i], extremeElement))
{
extremeElement = r[i];
}
}
}
else
{
while (!r.empty)
{
if (selectorFun(r.front, extremeElement))
{
extremeElement = r.front;
}
r.popFront();
}
}
return extremeElement;
}
auto minElement(Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum(r);
}
auto minElement(alias map, Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!map(r, seed);
}
auto minElement(Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum(r, seed);
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum!(map, "a > b")(r);
}
auto maxElement(Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum!`a > b`(r);
}
auto maxElement(alias map, Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!(map, "a > b")(r, seed);
}
auto maxElement(Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!`a > b`(r, seed);
}
}
// popcnt with ulongs was added in D 2.071.0
static if (__VERSION__ < 2071) {
ulong popcnt(ulong x) {
x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L);
x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L);
x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL);
x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL);
x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL);
x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL);
return x;
}
}
|
D
|
import std.stdio;
import std.conv;
import std.array;
void main()
{
long N = readln.split[0].to!long;
long[] T = new long[5];
foreach (i; 0..5){
T[i] = readln.split[0].to!long;
}
long minT = T[0];
foreach (t; T){
if (t < minT) minT = t;
}
long ans = N / minT;
if (N % minT) ans++;
ans += 4;
writeln(ans);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
struct Matrix(size_t height, size_t width)
{
long M;
long[width][height] arr;
this(long[width][height] arr, long M = 0) {
this.arr = arr;
this.M = M;
}
pure nothrow @nogc
Matrix!(height, rhs_width) opBinary(string op, size_t rhs_width)(Matrix!(width, rhs_width) rhs) {
static if (op == "*") {
long[rhs_width][height] res;
foreach (y; 0..height) {
foreach (x; 0..rhs_width) {
foreach (i; 0..width) {
auto s = this.arr[y][i] * rhs.arr[i][x];
if (this.M) s %= this.M;
res[y][x] += s;
if (this.M) res[y][x] %= this.M;
}
}
}
return Matrix!(height, rhs_width)(res, this.M);
} else {
static assert(0, "Operator "~op~" not implemented");
}
}
pure nothrow @nogc
Matrix!(height, width) opBinary(string op)(long n) {
static if (op == "^^" && height == width) {
long[width][height] rr;
foreach (i; 0..width) rr[i][i] = 1;
auto r = Matrix!(height, width)(rr, M);
auto x = this;
while (n) {
if (n%2 == 1) r = r * x;
x = x * x;
n /= 2;
}
return r;
} else {
static assert(0, "Operator "~op~" not implemented");
}
}
}
Matrix!(h, w) matrix(size_t h, size_t w)(long[w][h] arr, long M = 0)
{
return Matrix!(h, w)(arr, M);
}
void main()
{
auto labm = readln.split.to!(long[]);
auto L = labm[0];
auto A = labm[1];
auto B = labm[2];
auto M = labm[3];
long[18] CS;
if (A < 10) CS[0] = (10-A-1)/B+1;
foreach (i; 1..18) {
auto dm = 10L^^(i+1)-1-A;
if (dm < 0) continue;
auto last = B*(L-1);
CS[i] = ((dm > last) ? last : dm)/B+1;
foreach (j; 0..i) CS[i] -= CS[j];
if (dm >= last) break;
}
long[3][1] xss = [[0, A, 1]];
auto xs = matrix(xss, M);
foreach (i, d; CS) {
long[3][3] cm = [
[(10L^^(i+1)%M).to!long, 0, 0],
[1L, 1, 0],
[0L, B, 1]
];
auto m = matrix(cm, M);
m = m^^d;
xs = xs * m;
}
writeln(xs.arr[0][0]);
}
|
D
|
import std.stdio;
import std.algorithm;
import std.math;
import std.conv;
import std.string;
auto readNum(T)(){
return readln.chomp.to!T;
}
auto readNums(T)(){
return readln.chomp.split.to!(T[]);
}
void main(){
auto r = readNums!long;
foreach(i; 0 .. 10){
r[2] = r[0] * r[2] - r[1];
writeln(r[2]);
}
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "a", long, "b");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
auto n = lread();
long check = true;
long c_t, c_x, c_y;
foreach (_; iota(n))
{
long t, x, y;
scan(t, x, y);
auto manh = abs(c_x - x) + abs(c_y - y);
long t_dif = t - c_t;
check = check && (t_dif >= manh) && ((t_dif - manh) % 2 == 0);
c_t = t;
c_x = x;
c_y = y;
}
if (check)
writeln("Yes");
else
writeln("No");
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.functional,
std.algorithm,
std.container,
std.typetuple,
std.typecons,
std.bigint,
std.string,
std.traits,
std.array,
std.range,
std.stdio,
std.conv;
void main() {
ulong N = readln.chomp.to!ulong;
ulong[] A = readln.chomp.split.to!(ulong[]);
ulong ans;
foreach (a; A) {
while (a % 2 == 0) {
a /= 2;
ans++;
}
}
writeln(ans);
}
|
D
|
/+ dub.sdl:
name "D"
dependency "dcomp" version=">=0.7.4"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
int n;
int[] a;
int[] hist;
void query(int k) {
hist ~= k;
foreach (i; k..n) {
swap(a[i], a[i-k]);
}
}
int main() {
Scanner sc = new Scanner(stdin);
sc.read(n);
sc.read(a);
foreach (s; 1..n) {
while (true) {
while (a[$-1] != s) query(1);
int pos = -1;
foreach (i; 0..n) {
if (a[i] == 0) {
pos = i;
break;
}
}
// writeln(s, " -> ", pos, " : ", a);
if (pos+s+1 == n) break;
query(n-1);
}
}
while (a[0] != 0) query(1);
writeln(hist.length);
writeln(hist.map!(to!string).join("\n"));
return 0;
}
/* IMPORT /Users/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 /Users/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;
}
}
/* IMPORT /Users/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);
}
}
}
}
import core.bitop : popcnt;
static if (!__traits(compiles, popcnt(ulong.max))) {
public import core.bitop : popcnt;
int popcnt(ulong v) {
return popcnt(cast(uint)(v)) + popcnt(cast(uint)(v>>32));
}
}
bool poppar(ulong v) {
v^=v>>1;
v^=v>>2;
v&=0x1111111111111111UL;
v*=0x1111111111111111UL;
return ((v>>60) & 1) != 0;
}
/*
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, 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;
scan(N);
auto primes = eratos(2 * 10^^5);
foreach (pi ; primes) {
if (pi >= N) {
writeln(pi);
return;
}
}
}
int[] eratos(int N) {
auto s = new bool[](N);
s[] = true;
s[0] = s[1] = false;
for (int p = 2; p*p < N; p++) {
if (s[p]) for (int d = p*p; d < N; d += p) {
s[d] = false;
}
}
return iota(N).filter!(i => s[i]).array;
}
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;
}
}
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;
import std.algorithm;
import std.math;
import std.conv;
import std.string;
auto readNum(T)(){
return readln.chomp.to!T;
}
auto readNums(T)(){
return readln.chomp.split.to!(T[]);
}
void main(){
auto ab = readNums!int;
if(ab[0] <= 5){
writeln(0);
} else if(ab[0] <= 12){
writeln(ab[1] / 2);
} else {
writeln(ab[1]);
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto A = RD!string;
auto B = RD!string;
auto C = RD!string;
writeln(A[0], B[0], C[0]);
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;
enum inf = int.max;
void main() {
int n, q;
scan(n, q);
auto st = SegmentTree!(int, min, inf)(n);
foreach (_ ; 0 .. q) {
int com, x, y;
scan(com, x, y);
if (com == 0) {
st.assign(x, y);
}
else {
st.query(x, y + 1).writeln;
}
}
}
struct SegmentTree(T, alias op, T initValue = 0) {
import std.functional : binaryFun;
alias f = binaryFun!(op);
T[] _data;
int _treeSize, _lineSize = 1;
// 配列の要素数 n だけ与えて初期化
this(int n) {
while (_lineSize < n) {
_lineSize *= 2;
}
_treeSize = 2*_lineSize - 1;
_data = new T[](_treeSize);
_data[] = initValue;
}
// 配列 a を与えて初期化
this(T[] a) {
auto n = a.length;
while (_lineSize < n) {
_lineSize *= 2;
}
_treeSize = 2*_lineSize - 1;
_data = new T[](_treeSize);
_data[] = initValue;
_data[_lineSize - 1 .. _lineSize - 1 + n] = a[];
foreach_reverse (i ; 0 .. _lineSize - 1) {
_data[i] = f(_data[2*i + 1], _data[2*i + 2]);
}
}
// a[i] = x
void assign(int i, int x) {
i += _lineSize - 1;
_data[i] = x;
while (i > 0) {
i = (i - 1) / 2;
_data[i] = f(_data[2*i + 1], _data[2*i + 2]);
}
}
// a[i] = f(a[i], x)
void act(int i, int x) {
i += _lineSize - 1;
_data[i] = f(_data[i], x);
while (i > 0) {
i = (i - 1) / 2;
_data[i] = f(_data[2*i + 1], _data[2*i + 2]);
}
}
alias add = act;
// 要求区間 [a, b) に対して、対象区間 [l, r) の値を返す
T query(int a, int b, int k = 0, int l = 0, int r = -1) {
if (r == -1) {
r = _lineSize;
}
if (a >= b) { // 要求区間が空である場合
return initValue;
}
if (b <= l || r <= a) { // 要求区間が対象区間外の場合
return initValue;
}
if (a <= l && r <= b) { // 要求区間が対象区間に含まれている場合
return _data[k];
}
// どちらでもない場合は対象区間を2つに分けて再帰する
auto leftValue = query(a, b, 2*k + 1, l, l + (r - l) / 2);
auto rightValue = query(a, b, 2*k + 2, l + (r - l) / 2, r);
return f(leftValue, rightValue);
}
}
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.conv, std.stdio;
import std.algorithm, std.array, std.range, std.string;
void main()
{
(6 - readln.chomp.to!int - readln.chomp.to!int).writeln;
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip, std.regex;
void main() {
int[] dr = [1, -1, 0, 0];
int[] dc = [0, 0, 1, -1];
auto s = readln.split.map!(to!int);
auto H = s[0];
auto W = s[1];
auto A = H.iota.map!(_ => readln.chomp).array;
foreach (i; 0..H) {
foreach (j; 0..W) {
if (A[i][j] != '#') continue;
bool ok = false;
foreach (k; 0..4) {
int di = i + dr[k];
int dj = j + dc[k];
if (di < 0 || di >= H || dj < 0 || dj >= W) continue;
if (A[di][dj] == '#') ok = true;
}
if (!ok) {
writeln("No");
return;
}
}
}
writeln("Yes");
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.array;
void main(){
string[] sqlen = readln().chomp().split();
int a = sqlen[0].to!int(); int b = sqlen[1].to!int();
writeln(a*b, " ", (a+b)*2);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nm = readln.split.to!(long[]);
auto N = nm[0];
auto M = nm[1];
writeln(N == 1 && M == 1 ? 1 : N == 1 ? max(0, M-2) : M == 1 ? max(0, N-2) : (N-2) * (M-2));
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main() {
auto a = readln.chomp.split.map!(to!int);
int w = a[0], h = a[1], x = a[2], y = a[3], r = a[4];
if (x - r >= 0 && x + r <= w && y - r >= 0 && y + r <= h) {
writeln("Yes");
} else {
writeln("No");
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nx = readln.split.to!(long[]);
auto N = nx[0];
auto X = nx[1];
auto xs = readln.split.to!(long[]);
auto r = abs(xs[0] - X);
foreach (x; xs[1..$]) {
r = gcd(r, abs(x - X));
}
writeln(r);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
auto nmx = readints;
int n = nmx[0], m = nmx[1], x = nmx[2];
auto a = readints;
auto xs = new int[n + 2];
for (int i = 0; i < a.length; i++) {
xs[a[i]] = 1;
}
int s = xs[0..x].sum;
int t = xs[x+1..$].sum;
writeln(min(s, t));
}
|
D
|
import std.stdio;
import core.stdc.stdio;
import std.algorithm;
struct state{
int x;
int y;
int z;
int r;
}
void main(){
while(1){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a==0&&b==0&&c==0)
break;
int m = a+b+c;
int n;
scanf("%d",&n);
int[] dec = new int[m];
dec[] = 2;
state[] sts = new state[n];
for(int i=0;i<n;i++){
state s;
scanf("%d%d%d%d",&s.x,&s.y,&s.z,&s.r);
s.x--;
s.y--;
s.z--;
if(s.r==1){
dec[s.x] = 1;
dec[s.y] = 1;
dec[s.z] = 1;
}
sts[i] = s;
}
for(int _=0;_<m;_++){
foreach(st;sts){
if(st.r==0){
if(dec[st.x]==1&&dec[st.y]==1)
dec[st.z]=0;
else if(dec[st.y]==1&&dec[st.z]==1)
dec[st.x]=0;
else if(dec[st.z]==1&&dec[st.x]==1)
dec[st.y]=0;
}
}
}
foreach(d;dec)
printf("%d\n",d);
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
int[30] CNT;
void main()
{
auto nm = readln.split.to!(int[]);
auto N = nm[0];
auto M = nm[1];
foreach (_; 0..N) {
auto as = readln.split.to!(int[]);
foreach (n; as[1..$]) {
++CNT[n-1];
}
}
int cnt;
foreach (c; CNT) if (c == N) ++cnt;
writeln(cnt);
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
void main()
{
auto n = sread();
// writeln(n);
if ((n[0] == n[1]) && (n[0] == n[2]))
{
writeln("Yes");
return;
}
if ((n[1] == n[2]) && (n[1] == n[3]))
{
writeln("Yes");
return;
}
writeln("No");
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
|
D
|
import std.stdio, std.string, std.array, std.conv;
void main() {
while (true) {
int[] tmp = readln.chomp.split.to!(int[]);
int n = tmp[0], x = tmp[1];
int cnt = 0;
if (n == 0 && x == 0) break;
foreach (i; 1 .. n+1) {
foreach (j; 1 .. i) {
foreach (k; 1 .. j) {
if (i + j + k == x && i != j && j != k && k != i) ++cnt;
}
}
}
cnt.writeln;
}
}
|
D
|
void main() {
auto arr = 5.iota.map!(i => ri).array;
ulong res = ulong.max;
do {
ulong tmp;
foreach(i, v; arr) {
if(i == 4) {
tmp += v;
} else {
tmp = ceil((tmp + v) / 10.).to!ulong * 10;
}
}
res = min(res, tmp);
} while(nextPermutation(arr));
res.writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std;
void main() {
int a, b; scan(a, b);
writeln(a * b);
}
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T=string)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readints = reads!int;
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.