code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
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;
void times(alias fun)(int n) {
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
void main() {
int N = readln.chomp.to!int;
int[] as = readln.chomp.map!(a => a=='('?1:-1).cumulativeFold!"a+b"(0).array;
as = [0]~as;
int m = as.reduce!min;
(-m).times!(() => '('.write);
as[] -= m;
foreach(i; 0..as.length-1) {
if (as[i+1]-as[i]>0) {
'('.write;
} else {
')'.write;
}
}
as.back.times!(() => ')'.write);
writeln;
}
// ----------------------------------------------
// 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.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;
const long INF = long.max/3;
const long MOD = 10L^^9+7;
void main() {
char[] s = readln.chomp.to!(char[]);
s[3] = '8';
s.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); }
void main()
{
auto t = RD!int;
auto ans = new int[][][](t);
foreach (ti; 0..t)
{
auto n = RD!int;
ans[ti].length = n;
foreach (i; 0..n)
ans[ti][i].length = n;
if (n % 2 == 0)
{
foreach (i; 0..n)
{
foreach (j; 0..2)
{
auto x = (i/2) * 2 + j;
ans[ti][i][x] = 1;
}
}
}
else if (n % 3 == 2 || n % 3 == 0)
{
foreach (i; 0..n)
{
foreach (j; 0..3)
{
auto x = (i/3) * 3 + j;
if (x >= n) break;
ans[ti][i][x] = 1;
}
}
}
else
{
foreach (i; 0..n-4)
{
foreach (j; 0..3)
{
auto x = (i/3) * 3 + j;
if (x >= n) break;
ans[ti][i][x] = 1;
}
}
foreach (i; n-4..n)
{
foreach (j; 0..2)
{
auto x = ((i-(n-4))/2) * 2 + (n-4) + j;
ans[ti][i][x] = 1;
}
}
}
}
foreach (e; ans)
{
foreach (i; 0..e.length)
{
write(e[i][0]);
foreach (j; 1..e.length)
{
write(" ", e[i][j]);
}
writeln;
}
}
stdout.flush;
debug readln;
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.7.3"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
int main() {
Scanner sc = new Scanner(stdin);
int a, b, c;
sc.read(a, b, c);
if (a == b) writeln(c);
else if (b == c) writeln(a);
else writeln(b);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
version (X86) static if (__VERSION__ < 2071) {
import core.bitop : bsf, bsr, popcnt;
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
// import dcomp.array;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
FastAppender!(E[]) buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/array.d */
// module dcomp.array;
T[N] fixed(T, size_t N)(T[N] a) {return a;}
struct FastAppender(A, size_t MIN = 4) {
import std.algorithm : max;
import std.conv;
import std.range.primitives : ElementEncodingType;
import core.stdc.string : memcpy;
private alias T = ElementEncodingType!A;
private T* _data;
private uint len, cap;
@property size_t length() const {return len;}
bool empty() const { return len == 0; }
void reserve(size_t nlen) {
import core.memory : GC;
if (nlen <= cap) return;
void* nx = GC.malloc(nlen * T.sizeof);
cap = nlen.to!uint;
if (len) memcpy(nx, _data, len * T.sizeof);
_data = cast(T*)(nx);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void opOpAssign(string op : "~")(T item) {
if (len == cap) {
reserve(max(MIN, cap*2));
}
_data[len++] = item;
}
void insertBack(T item) {
this ~= item;
}
void removeBack() {
len--;
}
void clear() {
len = 0;
}
ref inout(T) back() inout { assert(len); return _data[len-1]; }
ref inout(T) opIndex(size_t i) inout { return _data[i]; }
T[] data() {
return (_data) ? _data[0..len] : null;
}
}
/*
This source code generated by dcomp and include dcomp's source code.
dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp)
dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt)
*/
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.range;
import std.stdio;
import std.string;
enum S { E = 0, A = 1, B = 2 };
void main() {
string s = readln.strip;
immutable n = s.length;
auto z = new dchar[n];
s.copy(z);
debug stderr.writeln(z);
long res;
int i;
int cnt;
S t = S.E;
foreach (c; z) {
debug stderr.writeln ("state: ", t, ", c = ", c, ", cnt = ", cnt);
if (t == S.E) {
if (c == 'A') {
t = S.A;
cnt = 1;
} else if (c == 'B' || c == 'C') {
}
} else if (t == S.A) {
if (c == 'A') {
++cnt;
} else if (c == 'B') {
t = S.B;
} else if (c == 'C') {
t = S.E;
}
} else if (t == S.B) {
if (c == 'A') {
t = S.A;
cnt = 1;
} else if (c == 'B') {
t = S.E;
cnt = 0;
} else if (c == 'C') {
res += cnt;
t = S.A;
}
}
}
writeln (res);
}
|
D
|
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop;
// dfmt on
void main()
{
long N = lread();
auto A = aryread();
long[long] cnt;
foreach (a; A)
{
cnt[a] = cnt.get(a, 0) + 1;
}
if (!A.all!(a => a < N))
{
writeln("No");
return;
}
switch (cnt.keys.length)
{
case 1:
writeln((A[0] * 2 <= N || N - 1 == A[0]) ? "Yes" : "No");
return;
case 2:
long a = cnt.keys[0];
long b = cnt.keys[1];
if (b < a)
swap(a, b);
if (b - a != 1)
{
writeln("No");
return;
}
dprint(N - cnt[a], b - cnt[a]);
writeln(cnt[a] < b && 2 * (b - cnt[a]) <= cnt[b] ? "Yes" : "No");
return;
default:
writeln("No");
return;
}
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.numeric;
void main()
{
auto n = readln.chomp.to!int;
auto a = readln.chomp.split.to!(int[]);
auto b = readln.chomp.split.to!(int[]);
long cnt;
foreach (i; 0..n) {
auto t = min(a[i], b[i]);
a[i] -= t;
b[i] -= t;
auto t2 = min(a[i+1], b[i]);
a[i+1] -= t2;
cnt += t + t2;
}
cnt.writeln;
}
|
D
|
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std;
// dfmt on
/// x^^n % m
long powmod(long x, long n)
{
alias T = long;
T m = 10 ^^ 9 + 7;
if (n < 1)
return 1;
if (n & 1)
{
return x * powmod(x, n - 1) % m;
}
T tmp = powmod(x, n / 2);
return tmp * tmp % m;
}
long invmod(long x)
{
return powmod(x, MOD - 2L);
}
// alias invmod = (long x, long m = 10 ^^ 9 + 7) => powmod(x, m - 2, m);
void main()
{
long N, M;
scan(N, M);
auto memo = new long[](M + 1);
memo[0] = 1;
foreach (i; 1 .. M + 1)
memo[i] = memo[i - 1] * i % MOD;
auto inv = new long[](M + 1);
foreach (i; 0 .. M + 1)
inv[i] = invmod(memo[i]);
long Permutation(long n, long r)
{
return memo[n] * inv[n - r] % MOD;
}
long Combination(long n, long r)
{
return (memo[n] * inv[n - r] % MOD) * inv[r] % MOD;
}
long a = Permutation(M, N);
foreach (i; 1 .. N + 1)
{
long b = Permutation(M - i, N - i);
// dprint(i, a, b, (N - i + 1));
a += MOD;
a += (b * Combination(N, i) % MOD) * ((i & 1) ? -1 : 1);
a %= MOD;
}
writeln(a * Permutation(M, N) % MOD);
}
|
D
|
import std;
auto input()
{
return readln().chomp();
}
alias sread = () => readln.chomp();
alias aryread(T = long) = () => readln.split.to!(T[]);
void main()
{
long x;
scan(x);
long k = 1;
while ((x * k) % 360 != 0)
{
k++;
}
writeln(k);
}
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.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto S = readln.chomp;
auto N = S.length;
long min_n;
auto ns = new long[](N+1);
long x;
foreach (i; 1..N+1) {
if (S[i-1] == '<') {
ns[i] = ++x;
} else {
x = 0;
}
}
x = 0;
foreach_reverse (i; 0..N) {
if (S[i] == '>') {
ns[i] = max(ns[i], ++x);
} else {
x = 0;
}
}
writeln(0L.reduce!"a + b"(ns));
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "begin", long, "end");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
long a, b, m;
scan(a, b, m);
auto refrigator = aryread();
auto microwave = aryread();
auto value = new long[](0);
foreach(_; iota(m))
{
long x, y, c;
scan(x, y, c);
value ~= (refrigator[x - 1] + microwave[y - 1] - c);
}
value ~= refrigator.reduce!(min) + microwave.reduce!(min);
writeln(value.reduce!(min));
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
void main() {
problem();
}
void problem() {
auto a = scan!long;
long solve() {
auto mod = a % 1000;
return mod == 0 ? 0 : 1000 - mod;
}
solve().writeln;
}
// ----------------------------------------------
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
// -----------------------------------------------
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
import std.range;
void main(){
auto A=readln.chomp;
writeln(A.replace(","," "));
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n, h, w;
scan(n);
scan(h);
scan(w);
int ans = (n - h + 1) * (n - w + 1);
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
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;
}
long powmod(long x, long y, long mod = 1_000_000_007L) {
long res = 1L;
while (y > 0) {
if (y & 1) {
(res *= x) %= mod;
}
(x *= x) %= mod;
y >>= 1;
}
return res;
}
|
D
|
import std.stdio, std.conv, std.array,std.string;
void main()
{
string[] input=readln.split;
int a=input[0].to!int,b=input[1].to!int;
if((a*b)%2==0){
writeln("Even");
}else{
writeln("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;
void main() {
auto a = read!string;
auto b = read!string;
for (int i = 0; i < a.length; i++) {
write(a[i]);
if (i < b.length) write(b[i]);
}
writeln();
}
|
D
|
import std.stdio,std.math,std.string,std.conv,std.typecons,std.format;
import std.algorithm,std.range,std.array;
T[] readarr(T=long)(){return readln.chomp.split.to!(T[]);}
void scan(T...)(ref T args){auto input=readln.chomp.split;foreach(i,t;T)args[i]=input[i].to!t;}
//END OF TEMPLATE
void main(){
ulong k;
k.scan;
k.solve.writeln;
}
struct Queue{
public:
char[][] list;
auto enq(char c){list~=[c];}
auto enq(char[] c){list~=c;}
auto deq(){auto d=list[0];list=list.length==1?[]:list[1..$];return d;}
}
char[] co(char c){
if(c=='0')
return ['0','1'];
if('1'<=c&&c<='8')
return [to!char(c-1),c,to!char(c+1)];
if(c=='9')
return ['8','9'];
assert(0);
}
auto solve(ulong k){
Queue q;
foreach(c;'1'.to!char..('9'+1).to!char)q.enq(c);
foreach(i;0..k-1){
auto tmp=q.deq;
foreach(s;tmp[$-1].co)
q.enq(tmp~s);
}
return q.deq;
}
|
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[] sieve(int n) {
assert(n > 1);
auto t = new bool[n + 1];
int[] primes = [2];
for (int i = 3; i < t.length; i += 2) {
if (!t[i]) {
primes ~= i;
for (int j = i + i; j < t.length; j += i)
t[j] = true;
}
}
return primes;
}
void main() {
int n = 10 ^^ 5;
auto primes = sieve(n);
int[int] d;
foreach (p; primes) d[p] = true;
bool isLike(int n) {
if (n % 2 == 0) return false;
if (n in d && (n + 1) / 2 in d) {
return true;
}
return false;
}
auto t = new int[n + 1];
for (int i = 3; i <= n; i++) {
t[i] = t[i - 1] + (isLike(i) ? 1 : 0);
}
int q = readint;
for (int i = 0; i < q; i++) {
auto lr = readints;
int l = lr[0];
int r = lr[1];
writeln(t[r] - t[l - 1]);
}
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
const MOD = 2019;
// 1ms(AC)
// [L, R] を全探索するが、余り 0 が見つかったら探索を打ち切る
// R-L >= 2019 のとき、余り 0 となるような値が必ず存在する => 探索範囲の上限は 2019^2 個まで
long calc1(long L, long R) {
long ans = int.max;
for (long i = L; i <= R; i++) {
for (long j = i + 1; j <= R; j++) {
ans = min(ans, i * j % MOD);
if (ans == 0) goto END;
}
}
END:
return ans;
}
// i * j mod 2019
// = (i mod 2019) * (j mod 2019) mod 2019
// なので [0, 2019) の範囲を調べるだけでよい
long calc2(long L, long R) {
long ans = int.max;
int[int] i_used;
foreach (i; L..R+1) {
if (i_used[i % MOD]++) break;
int[int] j_used;
foreach (j; i+1..R+1) {
if (j_used[j % MOD]++) break;
ans = min(ans, i * j % MOD);
}
}
return ans;
}
void main() {
int L, R; scan(L, R);
writeln(calc2(L, R));
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto s = readln.chomp;
size_t i, j = s.length-1;
int c;
while (j > i) {
if (s[i] == s[j]) {
++i; --j;
} else if (s[i] == 'x') {
++i; ++c;
} else if (s[j] == 'x') {
--j; ++c;
} else {
writeln(-1);
return;
}
}
writeln(c);
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
string s = read!string;
bool good = true;
for (int i = 1; i < s.length; i++) {
if (s[i - 1] == s[i]) {
good = false;
break;
}
}
writeln(good ? "Good" : "Bad");
}
|
D
|
import std.stdio, std.range, std.random, std.conv, std.string, std.math;
import std.algorithm.comparison, std.algorithm.iteration, std.algorithm.mutation, std.algorithm.searching, std.algorithm.sorting;
void main()
{
int[5] l;
foreach(i; 0..3) {
int[] input = readln().strip.split().to!(int[]);
l[input[0]]++;
l[input[1]]++;
}
writeln(
(
l[1]<3 &&
l[2]<3 &&
l[3]<3 &&
l[4]<3) ? "YES" : "NO");
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
void main() {
long n, p;
scan(n, p);
long ans = 1;
for (long i = 2; i*i <= p; i++) {
int cnt;
while (p % i == 0) {
p /= i;
cnt++;
}
ans *= i^^(cnt / n);
}
if (n == 1 && p > 1) {
ans *= p;
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
void main() {
try {
for (; ; ) {
const numCases = readInt();
foreach (caseId; 0 .. numCases) {
const N = readInt();
const A = readToken();
auto freq = new int[0x100];
foreach (i; 0 .. N) {
++freq[A[i]];
}
string ans;
foreach (c; 0 .. 0x100) {
foreach (_; 0 .. freq[c]) {
ans ~= cast(char)(c);
}
}
writeln(ans);
}
}
} catch (EOFException e) {
}
}
|
D
|
void main()
{
string s = rdStr;
string t = "AKIHABARA";
string u;
foreach (i, x; s)
{
if (i == 0 && x != 'A') u ~= 'A';
if ((x == 'B' || x == 'R') && u.back != 'A') u ~= 'A';
u ~= x;
if (i == s.length - 1 && x == 'R') u ~= 'A';
}
writeln(t == u ? "YES" : "NO");
}
T rdElem(T = long)()
{
//import std.stdio : readln;
//import std.string : chomp;
//import std.conv : to;
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
dchar[] rdDchar()
{
//import std.conv : to;
return rdStr.to!(dchar[]);
}
T[] rdRow(T = long)()
{
//import std.stdio : readln;
//import std.array : split;
//import std.conv : to;
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
//import std.range : iota;
//import std.algorithm : map;
//import std.array : array;
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
//import std.range : iota;
//import std.algorithm : map;
//import std.array : array;
return iota(col).map!(x => rdRow!T).array;
}
void wrMat(T = long)(T[][] mat)
{
//import std.stdio : write, writeln;
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.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import core.stdc.stdio;
import std.typecons;
import std.algorithm;
struct Seg{
Seg* l,r;
int c;
}
int Count(Seg* x){
return x is null?0:x.c;
}
Seg* Left(Seg* x){
return x is null?null:x.l;
}
Seg* Right(Seg* x){
return x is null?null:x.r;
}
Seg[] buf;
Seg* newSeg(Seg* l,Seg* r){
static int i;
buf[i]=Seg(l,r,l.Count+r.Count);
return &buf[i++];
}
Seg* Add(Seg* x,int i,int l=0,int r=1<<30){
if(i<l||r<=i)
return x;
if(i==l&&i+1==r){
Seg* t=newSeg(null,null);
t.c=1+x.Count;
return t;
}else
return newSeg(Add(x.Left,i,l,(l+r)/2),Add(x.Right,i,(l+r)/2,r));
}
int Get(Seg* x,int i,int l=0,int r=1<<30){
if(x is null)
return 0;
if(i<=l)
return 0;
if(r<=i)
return x.Count;
return Get(x.Left,i,l,(l+r)/2)+Get(x.Right,i,(l+r)/2,r);
}
int[][] par;
int[] dep;
void LCA_Init(){
foreach(i;1..20)
foreach(j,ref p;par[i])
p=par[i-1][j]>=0?par[i-1][par[i-1][j]]:-1;
}
int LCA(int a,int b){
if(dep[a]>dep[b])
swap(a,b);
int d=dep[b]-dep[a];
foreach(i;0..20)
if((d>>i)&1)
b=par[i][b];
if(a==b)
return a;
foreach_reverse(i;0..20)
if(par[i][a]!=par[i][b])
a=par[i][a],b=par[i][b];
return par[0][a];
}
void main(){
buf = new Seg[6000000];
int n,q;
scanf("%d%d",&n,&q);
int[] x=new int[n];
foreach(ref v;x)
scanf("%d",&v);
int[][] graph=new int[][n];
foreach(i;0..n-1){
int a,b;
scanf("%d%d",&a,&b);
graph[--a]~=--b;
graph[b]~=a;
}
Seg*[] segs=new Seg*[n];
par=new int[][](20,n);
dep=new int[n];
alias Tuple!(int,"i",int,"p",Seg*,"s",int,"d") stack;
stack[] st=new stack[n*2];
st[0]=stack(0,-1,null,0);
int ss=1;
while(ss){
stack cur=st[--ss];
with(cur){
par[0][i]=p;
dep[i]=d;
segs[i]=s.Add(x[i]);
foreach(c;graph[i])
if(c!=p)
st[ss++]=stack(c,i,segs[i],d+1);
}
}
LCA_Init;
foreach(_;0..q){
int v,w,k,l=0,r=1<<30;
scanf("%d%d%d",&v,&w,&k);
int p=LCA(--v,--w);
Seg* a=segs[v],b=segs[w],c=segs[p],d=p?segs[par[0][p]]:null;
while(r-l>1){
int m=(r+l)/2;
if(k<=a.Get(m)+b.Get(m)-c.Get(m)-d.Get(m))
r=m;
else
l=m;
}
printf("%d\n",l);
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n;
scan(n);
int ma;
int ans;
foreach (i ; 0 .. n) {
int ai, bi;
scan(ai, bi);
if (ai > ma) {
ma = ai;
ans = ai + bi;
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.stdio;
import std.conv;
import std.algorithm;
import std.string;
import std.file;
int main() {
int[] hs;
string l;
while((l = readln()).length >= 2) printf("%d\n", to!string(to!int(l.split()[0]) + to!int(l.split()[1])).length);
return 0;
}
|
D
|
import std.stdio, std.range, std.conv, std.string;
import std.algorithm.comparison, std.algorithm.iteration, std.algorithm.mutation, std.algorithm.searching, std.algorithm.setops, std.algorithm.sorting;
void main()
{
int[] input = readln().split.to!(int[]);
int N = input[0];
int M = input[1];
int C = input[2];
auto blist = readln().split.to!(int[]);
int count;
foreach(int i; 0..N)
{
auto sList = readln().split.to!(int[]);
long sum = 0;
foreach(int k; 0..M)
{
sum += blist[k]*sList[k];
}
sum += C;
count += sum > 0 ? 1 : 0;
}
writeln(count);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
long calc(int h, int w) {
long _calc(long h, long w) {
long ans = long.max;
for (int i = 1; i < w; i++) {
long a = h * i;
long hTop = h / 2;
long hBtm = h - hTop;
long b = hTop * (w - i);
long c = hBtm * (w - i);
ans = min(ans, max(a, b, c) - min(a, b, c));
}
for (int i = 1; i < w - 1; i++) {
long a = h * i;
long w2 = w - i;
long wLeft = w2 / 2;
long wRight = w2 - wLeft;
long b = h * wLeft;
long c = h * wRight;
ans = min(ans, max(a, b, c) - min(a, b, c));
}
return ans;
}
return min(_calc(h, w), _calc(w, h));
}
void main() {
auto hw = readints;
int h = hw[0], w = hw[1];
writeln(calc(h, w));
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n, m;
rd(n, m);
auto a=readln.split.to!(int[]);
auto b=readln.split.to!(int[]);
int mx=0;
foreach(i; 0..m){
int j=i;
foreach(e; a){
if(j>=m) break;
if(e==b[j]) j++;
}
mx=max(mx, j-i);
}
writeln(mx);
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
ulong MOD = 1_000_000_007;
ulong INF = 1_000_000_000_000;
alias Score = Tuple!(long, "p", long, "c");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
auto n = lread();
auto a = aryread();
long f;
foreach(e; a)
{
f += e - 1;
}
f.writeln();
}
|
D
|
void main() {
int[] tmp = readln.split.to!(int[]);
int a = tmp[0], b = tmp[1];
int diff = b - a;
writeln(iota(1, diff+1).sum - b);
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.uni;
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
string a, b; readV(a, b);
writeln(a == b ? "H" : "D");
}
|
D
|
import std;
void main() {
int n;
scanf("%d\n", &n);
auto as = readln.chomp.split.to!(int[]);
long count = 0;
int[long] memj;
foreach(i;0..as.length) {
++memj[i-as[i]];
}
foreach(i;0..as.length) {
if (i+as[i] in memj) {
count += memj[i+as[i]];
}
}
write(count);
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.ascii;
void main()
{
auto a = readln.chomp[0];
auto b = readln.chomp[1];
auto c = readln.chomp[2];
writeln(a,b,c);
}
|
D
|
// Code By H~$~C
module main;
import core.stdc.stdlib, core.stdc.string;
import std.stdio, std.array, std.string, std.math, std.uni;
import std.algorithm, std.range, std.random, std.container;
import std.conv, std.typecons, std.functional;
immutable Maxn = 100005;
int n, q;
Tuple!(int, int, int)[][Maxn] g;
int[Maxn] col, wd, dist;
int indx;
int[Maxn] dep, par, sz, son, top, ind, rind;
void dfs1(int u, int fa, int depth) {
par[u] = fa, sz[u] = 1;
dep[u] = depth;
foreach(pr; g[u]) {
int v = pr[0];
if (v == fa) continue;
dist[v] = dist[u] + pr[2];
dfs1(v, u, depth + 1);
sz[u] += sz[v];
if (sz[v] > sz[son[u]]) son[u] = v;
col[v] = pr[1];
wd[v] = pr[2];
}
}
void dfs2(int u, int topv) {
if (top[u]) assert(0);
top[u] = topv;
ind[u] = ++indx;
rind[indx] = u;
if (son[u]) dfs2(son[u], topv);
foreach(pr; g[u]) {
int v = pr[0];
if (v != par[u] && v != son[u])
dfs2(v, v);
}
}
// segment tree
struct node {
this()() {
cnt = 0;
sum = 0;
}
this()(in node x) { *this = x; }
int cnt, sum, ls, rs;
void apply(int l, int r, int c, int s) {
cnt = c, sum = s;
}
}
void pullup(ref node a, in node b, in node c) {
a.cnt = b.cnt + c.cnt;
a.sum = b.sum + c.sum;
}
node[] tr;
struct segment_tree {
int N, root;
private void pushup(int p) {
pullup(tr[p], tr[tr[p].ls], tr[tr[p].rs]);
}
private void modify_(T...)(ref int p, int l, int r, int L, int R, T args) {
if (!p) p = cast(int)tr.length, tr.length++;
if (L == l && r == R) return tr[p].apply(l, r, args);
int mid = (l + r) >> 1;
if (R <= mid) modify_(tr[p].ls, l, mid, L, R, args);
else if (L > mid) modify_(tr[p].rs, mid + 1, r, L, R, args);
else {
modify_(tr[p].ls, l, mid, L, mid, args);
modify_(tr[p].rs, mid + 1, r, mid + 1, R, args);
}
pushup(p);
}
private node query_(int p, int l, int r, int L, int R) {
if (!p) return node.init;
if (L == l && r == R) return tr[p];
int mid = (l + r) >> 1;
if (R <= mid) return query_(tr[p].ls, l, mid, L, R);
if (L > mid) return query_(tr[p].rs, mid + 1, r, L, R);
node left = query_(tr[p].ls, l, mid, L, mid);
node right = query_(tr[p].rs, mid + 1, r, mid + 1, R);
node ans; pullup(ans, left, right); return ans;
}
public void build(T...)(int len, T args) {
N = len;
root = 0;
}
public void modify(T...)(int L, int R, T args) {
if (L > R) return ;
modify_(root, 1, N, L, R, args);
}
public node query(int L, int R) {
if (L > R) return node();
return query_(root, 1, N, L, R);
}
}
segment_tree[Maxn] sgt;
int jump_chain(int u, int v, int c, int d) {
int U, V; U = u, V = v;
node res;
while (top[u] != top[v]) {
if (dep[top[u]] > dep[top[v]]) swap(u, v);
pullup(res, res, sgt[c].query(ind[top[v]], ind[v]));
v = par[top[v]];
}
if (dep[u] > dep[v]) swap(u, v);
pullup(res, res, sgt[c].query(ind[u] + 1, ind[v]));
return dist[U] + dist[V] - dist[u] * 2 - res.sum + res.cnt * d;
}
void solve(in int testcase) {
n.read, q.read;
foreach(i; 1 .. n) {
int u, v, d, c;
u.read, v.read, c.read, d.read;
g[u] ~= tuple(v, c, d);
g[v] ~= tuple(u, c, d);
}
dfs1(1, 0, 1);
dfs2(1, 1);
tr.length = 1;
foreach(i; 1 .. n) sgt[i].build(n);
foreach(i; 2 .. n + 1) {
sgt[col[i]].modify(ind[i], ind[i], 1, wd[i]);
}
while (q--) {
int u, v, c, d;
c.read, d.read, u.read, v.read;
writeln(jump_chain(u, v, c, d));
}
}
// ************************************************************
T reader(T)() { return readToken().to!T; }
void read(T)(ref T t) { t = reader!T; }
T[] readarray(T)() { return readln.splitter.map!(to!T).array; }
void chmax(T)(ref T a, T b) { a = max(a, b); }
void chmin(T)(ref T a, T b) { a = min(a, b); }
alias less = (a, b) => a < b;
alias greater = (a, b) => a > b;
T min_element(T)(in T[] a) { return a.element!less; }
T max_element(T)(in T[] a) { return a.element!greater; }
alias unique = uniq;
// constant or some variables
immutable int inf = 0x3f3f3f3f;
immutable long lnf = 0x3f3f3f3f3f3f3f3f;
immutable typeof(null) NULL = null;
string[] _tokens;
// functions
T element(alias pred, T)(in T[] a) {
int pos = 0;
for (int i = 1; i < cast(int)(a.length); ++i)
if (binaryFun!pred(a[i], a[pos])) pos = i;
return a[pos];
}
string readToken() {
for (; _tokens.empty; ) {
if (stdin.eof) return "";
_tokens = readln.split;
}
auto token = _tokens.front;
_tokens.popFront;
return token;
}
int binary_bearch(alias pred, T)(in T[] as) {
int l = -1, h = cast(int)(as.length), mid;
for (; l + 1 < h; ) {
mid = (l + h) >> 1;
(unaryFun!pred(as[mid]) ? h : l) = mid;
}
return h;
}
int lower_bound(alias pred = less, T)(in T[] as, T val) {
return as.binary_bearch!(a => a == val || pred(val, a));
}
int upper_bound(alias pred = less, T)(in T[] as, T val) {
return as.binary_bearch!(a => pred(val, a));
}
void main(string[] args) {
int tests = 1;
// tests.read();
foreach(test; 1 .. tests + 1) solve(test);
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto S = readln.chomp;
auto N = S.length.to!int;
int[] cnt = new int[](26);
foreach (s; S) cnt[s-'a'] += 1;
if (cnt.map!(c => c % 2).sum > 1) {
writeln(-1);
return;
}
int[] cnt2 = new int[](26);
string s1, s2;
foreach(s; S) {
int c = s - 'a';
cnt2[c] += 1;
if (cnt2[c] <= (cnt[c] + 1) / 2) s1 ~= s;
else s2 ~= s;
}
auto number = new int[][](26);
int tmp = 0;
foreach (s; s1) number[s-'a'] ~= tmp++;
int[] cnt3 = new int[](26);
int[] new_s = new int[](N);
foreach (i; 0..N) {
int c = S[N-i-1] - 'a';
if (cnt3[c] + 1 == number[c].length && cnt[c] % 2) new_s[i] = 2*N - 2, cnt3[c]++;
else if (cnt3[c] < number[c].length) new_s[i] = number[c][cnt3[c]++];
else new_s[i] = 2*N-1;
}
auto st = new SegmentTree(2*N+1);
long ans;
foreach (s; new_s) {
ans += st.sum(s+1, 2*N+1);
st.add(s, 1);
}
ans.writeln;
}
class SegmentTree {
long[] table;
int size;
this(int n) {
assert(bsr(n) < 29);
size = 1 << (bsr(n) + 2);
table = new long[](size);
}
void add(int pos, long num) {
return add(pos, num, 0, 0, size/2-1);
}
void add(int pos, long num, int i, int left, int right) {
table[i] += num;
if (left == right)
return;
auto mid = (left + right) / 2;
if (pos <= mid)
add(pos, num, i*2+1, left, mid);
else
add(pos, num, i*2+2, mid+1, right);
}
long sum(int pl, int pr) {
return sum(pl, pr, 0, 0, size/2-1);
}
long sum(int pl, int pr, int i, int left, int right) {
if (pl > right || pr < left)
return 0;
else if (pl <= left && right <= pr)
return table[i];
else
return
sum(pl, pr, i*2+1, left, (left+right)/2) +
sum(pl, pr, i*2+2, (left+right)/2+1, right);
}
}
|
D
|
import std.stdio;
import std.algorithm;
import std.conv;
import std.datetime;
import std.numeric;
import std.math;
import std.string;
string my_readln() { return chomp(readln()); }
void main()
{//try{
auto tokens = split(my_readln());
auto N = to!string(tokens[0]);
ulong ans;
foreach (e; N)
{
if (e == '2') ++ans;
}
writeln(ans);
stdout.flush();
/*}catch (Throwable e)
{
writeln(e.toString());
}
readln();*/
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() {
while (true) {
auto ab = readints;
int a = ab[0], b = ab[1];
if (a == 0 && b == 0)
break;
if (a % 2 == 1 && b % 2 == 1) {
writeln("no");
}
else {
writeln("yes");
}
}
}
|
D
|
void main()
{
long i, o, t, j, l, s, z;
rdVals(i, o, t, j, l, s, z);
long result = o;
if (i && j && l && (i & 1) + (j & 1) + (l & 1) >= 2)
{
result += 3;
--i, --j, --l;
}
result += 2 * (i / 2 + j / 2 + l / 2);
result.writeln;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
auto s=readln.chomp.to!(char[]);
int w; rd(w);
auto t="".to!(char[]);
int i=0;
while(i<s.length){
t~=s[i];
i+=w;
}
writeln(t);
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x) e=l[i].to!(typeof(e));
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto N = RD;
auto M = RD;
auto A = RDA;
long cnt = A.sum;
writeln(cnt > N ? -1 : N - cnt);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.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 t = RD!int;
auto ans = new long[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto a = RDA!int(-1);
int cnt;
foreach (i; 0..n*2)
cnt += (a[i] == 0 ? -1 : 1);
auto b = new int[](n+1);
foreach (i; 0..n)
{
b[i+1] = b[i] + (a[n-i-1] == 0 ? -1 : 1);
}
auto c = new int[](n+1);
foreach (i; 0..n)
{
c[i+1] = c[i] + (a[n+i] == 0 ? -1 : 1);
}
int[int] set;
foreach (i; 0..n+1)
{
auto v = set.get(b[i], int.max);
set[b[i]] = min(v, i);
}
int tmp = int.max;
foreach (i; 0..n+1)
{
auto remain = cnt - c[i];
auto v = set.get(remain, -1);
if (v != -1)
tmp.chmin(i + v);
debug writeln("cnt:", cnt, " c[i]:", c[i]);
debug writeln(i, ":", tmp);
}
ans[ti] = tmp;
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio;
import std.algorithm;
import std.math;
import std.string;
import std.conv;
import std.range;
void main() {
int sum = 0;
foreach (i; 0..10) {
sum += readln.chomp.to!int;
}
sum.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; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto N = RD;
bool[string] set;
foreach (i; 0..N)
{
set[RD!string] = true;
}
writeln(set.keys.length);
stdout.flush;
debug readln;
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() {
string s;
while ((s = readln.chomp) != null) {
auto ab = s.split.map!(to!int).array;
int a = ab[0], b = ab[1];
writeln(gcd(a, b));
}
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}}
void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}}
void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}}
void main()
{
string s; readV(s);
writeln(s.count('1'));
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random;
import std.typecons, std.functional, std.traits,std.concurrency;
import std.algorithm, std.container;
import core.bitop, core.time, core.memory;
import std.bitmanip;
import std.regex;
enum INF = long.max/3;
enum MOD = 10L^^9+7;
//辞書順順列はiota(1,N),nextPermituionを使う
void end(T)(T v)
if(isIntegral!T||isSomeString!T||isSomeChar!T)
{
import core.stdc.stdlib;
writeln(v);
exit(0);
}
T[] scanArray(T = long)()
{
static char[] scanBuf;
readln(scanBuf);
return scanBuf.split.to!(T[]);
}
dchar scanChar()
{
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
return cast(dchar)c;
}
T scanElem(T = long)()
{
import core.stdc.stdlib;
static auto scanBuf = appender!(char[])([]);
scanBuf.clear;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
scanBuf ~= cast(char) c;
c = getchar;
}
return scanBuf.data.to!T;
}
dchar[] scanString(){
return scanElem!(dchar[]);
}
void main()
{
writeln("ABC"~scanString);
}
|
D
|
import std.stdio;
import std.string;
void main() {
const string m = "kstnhm";
const string ch = "aiueo";
const string dir = "TLURD";
auto str = readln;
bool flag = false;
foreach (int i, c; str) {
if (!(i % 2)) {
auto a = c - '0';
if (a == 0) flag = true;
else if (2 <= a && a <= 7) {
write(m[a-2]);
} else if (a == 9) {
write('r');
} else if (a == 8) {
write('y');
}
} else {
auto a = dir.indexOf(c);
if (flag) {
if (a == 0) {
write("wa");
} else if (a == 2) {
write("nn");
} else {
write("wo");
}
flag = false;
} else {
write(ch[a]);
}
}
}
writeln;
}
|
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 a = readln.split.to!(int[]);
bool ok = true;
foreach (i ; 0 .. N) {
if (a[i] % 2 == 0 && a[i] % 3 != 0 && a[i] % 5 != 0) {
ok = false;
}
}
writeln(ok ? "APPROVED": "DENIED");
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;
immutable long MOD = 10^^9 + 7;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto L = s[1].to!long;
auto R = s[2].to!long;
long r = R + 2 - R % 3;
long l = L - L % 3;
long x = (r - l + 1) / 3;
auto num = new long[](3);
num[] = x;
if (L % 3 == 1) {
num[0] -= 1;
} else if (L % 3 == 2) {
num[0] -= 1;
num[1] -= 1;
}
if (R % 3 == 0) {
num[1] -= 1;
num[2] -= 1;
} else if (R % 3 == 1) {
num[2] -= 1;
}
auto dp = new long[][](N+1, 3);
dp[0][0] = 1;
foreach (i; 0..N) {
foreach (j; 0..3) {
foreach (k; 0..3) {
(dp[i+1][(j+k)%3] += dp[i][j] * num[k] % MOD) %= MOD;
}
}
}
dp[N][0].writeln;
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdlib;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto K = s[1];
auto C = s[2];
auto S = readln.chomp;
auto hoge = new int[](N+C+10);
foreach_reverse (i; 0..N) {
if (S[i] == 'x') {
hoge[i] = hoge[i + 1];
} else {
hoge[i] = hoge[i + C + 1] + 1;
}
}
int tmp = 0;
int[] ans;
for (int i = 0; i < N; ) {
if (S[i] == 'x') {
i += 1;
continue;
}
if (tmp + hoge[i + 1] < K) ans ~= i + 1;
tmp += 1;
i += C + 1;
}
ans.each!writeln;
}
class SegmentTree(T, alias op, T e) {
T[] table;
int size;
int offset;
this(int n) {
size = 1;
while (size <= n) size <<= 1;
size <<= 1;
table = new T[](size);
fill(table, e);
offset = size / 2;
}
void assign(int pos, T val) {
pos += offset;
table[pos] = val;
while (pos > 1) {
pos /= 2;
table[pos] = op(table[pos*2], table[pos*2+1]);
}
}
void add(int pos, T val) {
pos += offset;
table[pos] += val;
while (pos > 1) {
pos /= 2;
table[pos] = op(table[pos*2], table[pos*2+1]);
}
}
T query(int l, int r) {
return query(l, r, 1, 0, offset-1);
}
T query(int l, int r, int i, int a, int b) {
if (b < l || r < a) {
return e;
} else if (l <= a && b <= r) {
return table[i];
} else {
return op(query(l, r, i*2, a, (a+b)/2), query(l, r, i*2+1, (a+b)/2+1, b));
}
}
}
|
D
|
module AOJ_Volume0093;
import std.stdio,std.string,std.conv;
int main()
{
bool first = true;
while(true)
{
string[] s = readln.chomp.split(" ");
int a = s[0].to!int;
int b = s[1].to!int;
if(a == 0 && b == 0) break;
if(first)
{
first = false;
}
else
{
writeln;
}
bool flag = true;
foreach(i;a..b+1)
{
if(i%400 == 0)
{
flag = false;
i.writeln;
}
else if(i%100 != 0 && i%4 == 0)
{
flag = false;
i.writeln;
}
}
if(flag) "NA".writeln;
}
return 0;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
writeln(readln.chomp.to!int < 1200 ? "ABC" : "ARC");
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.conv;
import std.array;
import std.typecons;
import std.container;
void main() {
auto inp = readln.split.map!(to!int);
int N = inp[0], R = inp[1], L = inp[2];
alias Tuple!(int, "point", int, "time") Team;
Team[] datas = new Team[N];
foreach (i; 0..N) datas[i] = tuple(0, 0);
int t = 0;
alias Tuple!(int, "id", int, "point") MaxInfo;
MaxInfo maxInfo = tuple(0,0);
foreach (i; 0..R) {
auto input = readln.split.map!(to!int).array;
input[0]--;
datas[input[0]].point += input[2];
auto t2 = input[1];
datas[maxInfo.id].time += t2 - t;
if (datas[input[0]].point > maxInfo.point || (datas[input[0]].point == maxInfo.point && input[0] < maxInfo.id)) {
maxInfo = tuple(input[0], datas[input[0]].point);
} else if (maxInfo.id == input[0]) {
int max = 0, id = 0;
foreach (j; 0..N) {
if (datas[j].point > max) {
max = datas[j].point;
id = j;
}
}
maxInfo = tuple(id, max);
}
t = t2;
}
datas[maxInfo.id].time += L - t;
int max = 0;
int maxId = 0;
foreach(i; 0..N) {
if (datas[i].time > max) {
max = datas[i].time;
maxId = i;
}
}
writeln(maxId+1);
}
|
D
|
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container;
import std.numeric, std.math;
import core.bitop;
T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
long mod = pow(10, 9) + 7;
long moda(long x, long y) { return (x + y) % mod; }
long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; }
long modm(long x, long y) { return (x * y) % mod; }
void main()
{
auto N = RD!long;
auto M = RD!long;
auto cnt = new long[](N);
foreach (i; 0..M)
{
auto a = RD!long;
auto b = RD!long;
if (a == 1) ++cnt[b];
else if (b == N) ++cnt[a];
}
bool ans = false;
foreach (e; cnt)
{
if (e >= 2) ans = true;
}
writeln(ans ? "POSSIBLE" : "IMPOSSIBLE");
stdout.flush();
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string;
void main()
{
auto stxy = readln.split.to!(int[]);
auto xd = stxy[2] - stxy[0];
auto yd = stxy[3] - stxy[1];
string ret;
foreach (_; 0..yd) ret ~= "U";
foreach (_; 0..xd) ret ~= "R";
foreach (_; 0..yd) ret ~= "D";
foreach (_; 0..xd+1) ret ~= "L";
foreach (_; 0..yd+1) ret ~= "U";
foreach (_; 0..xd+1) ret ~= "R";
ret ~= "DR";
foreach (_; 0..yd+1) ret ~= "D";
foreach (_; 0..xd+1) ret ~= "L";
ret ~= "U";
writeln(ret);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
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!int;
foreach (i; 0..n)
{
auto s = RD!string;
auto cnt = new int[](10);
foreach (c; s)
{
auto x = c - '0';
++cnt[x];
}
int x;
foreach (j; 0..10)
x += cnt[j] * j;
if (cnt[0] == 0 || x % 3)
writeln("cyan");
/*else if (s.length <= 3)
{
bool ok;
if (cnt[6] >= 1)
ok = true;
else if (cnt[1] == 1 && cnt[2]+cnt[8] == 1)
ok = true;
else if (cnt[4] == 1 && cnt[2]+cnt[5]+cnt[8] == 1)
ok = true;
else if (cnt[7] == 1 && cnt[8]+cnt[2] == 1)
ok = true;
writeln(ok ? "red" : "cyan");
}*/
else if (cnt[0] == 1)
{
bool ok;
foreach (j; 1..5)
{
if (cnt[j*2])
ok = true;
}
writeln(ok ? "red" : "cyan");
}
else
{
writeln("red");
}
}
stdout.flush;
debug readln;
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N, K;
scan(N, K);
if (N % K == 0)
{
writeln(0);
return;
}
writeln(min(N % K, -(N % K - K)));
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp;
int l = N.length.to!int;
int r, p;
foreach (int i, c; N) {
auto n = c-48;
if (n) {
r = max(r, p + n-1 + (l-i-1)*9);
p += n;
}
}
writeln(max(r, p));
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.array;
import std.algorithm;
void main() {
auto n = readln.chomp.to!int;
auto a = readln.chomp.split(" ").map!(to!long).array;
long[long] table;
foreach (e;a) {
++table[e];
}
table.rehash;
long patterns = 0;
foreach (value;table.values) {
patterns += value * (value - 1) / 2;
}
foreach (e;a) {
auto v = table[e];
if (v < 2) {
writeln(patterns);
} else {
writeln(patterns - (v * (v - 1) / 2) + ((v - 1) * (v - 2) / 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;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
string s;
scan(s);
bool first = (s.front == s.back) ^ (s.length % 2);
writeln(first ? "First" : "Second");
}
void scan(T...)(ref T args) {
auto line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
|
D
|
import std.stdio;
import std.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;
void main() {
while(true) {
string str = readln.chomp;
if (str == "END OF INPUT") break;
str.split(" ").map!(s => s.length.to!string).reduce!"a~b".writeln;
}
}
|
D
|
import std.algorithm, std.array, std.bitmanip, std.conv, std.stdio, std.string,std.typecons;
T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; }
void main() {
immutable ip = readln.split.to!(ulong[]);
immutable n = ip[0], m = ip[1];
auto sw_vec = new BitArray[m];
for (int i = 0; i < m; i++) {
immutable ip1 = readln.split.to!(ulong[]);
sw_vec[i] = BitArray(new bool[n]);
for (int j = 0; j < ip1[0]; j++) {
sw_vec[i][ip1[j+1]-1] = true;
}
}
immutable res_vec = readln.split.to!(ulong[]);
auto check_vec = BitArray(new bool[n]);
const auto all_set = {
auto b = BitArray(new bool[n]);
foreach (ref v ; b) {
v = !v;
}
return b;
}();
ulong valid = 0;
while(check_vec != all_set) {
for(int i = 0; i < m; i++) {
ulong cnt = (sw_vec[i] & check_vec).bitsSet.count % 2;
if (cnt != res_vec[i]) goto LEND;
}
valid++;
LEND:;
for (int i = 0; i < n; i++) {
if (check_vec[i] == true) check_vec[i] = false;
else { check_vec[i] = true; break; }
}
}
for(int i = 0; i < m; i++) {
ulong cnt = (sw_vec[i] & check_vec).bitsSet.count % 2;
if (cnt != res_vec[i]) goto FINAL;
}
valid++;
FINAL:
writeln(valid);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto A = RDR.ARR;
auto B = new long[](N+1);
foreach (i; 0..N)
{
B[i+1] = B[i] + A[i];
}
long[long] set;
foreach (i; 0..N+1)
{
++set[B[i]];
}
long ans;
foreach (i; 0..N+1)
{
ans += set[B[i]] - 1;
}
writeln(ans/2);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio;
import std.string;
import std.algorithm.mutation;
void main()
{
readln;
string[] s = readln.split;
s.reverse();
writeln(s.join(" "));
}
|
D
|
import std.stdio, std.string, std.conv;
import std.array, std.algorithm, std.range;
void main()
{
do{
auto m = iota(8).map!(_=>countUntil(readln().chomp(),'1')).array().filter!"a>=0"().array();
char c='Z';
switch(m.length)
{
case 1:
c = 'C';
break;
case 2:
if(m[0]==m[1]) c='A';
else if(m[0]<m[1]) c='E';
else c='G';
break;
case 3:
c = m[0]>m[1]?'D':'F';
break;
case 4:
default:
c = 'B';
break;
}
writeln(c);
}while(readln().length);
}
|
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;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
// dfmt on
void main()
{
long N = lread();
auto A = new long[](N);
foreach (i; 0 .. N)
A[i] = lread();
auto B = A.dup;
B.sort();
long pm = B[$ - 2];
long cm = long.min;
long mi = -1;
foreach (i; 0 .. N)
if (cm < A[i])
{
cm = A[i];
mi = i;
}
foreach (i; 0 .. N)
writeln((i == mi) ? pm : cm);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new long[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto m = RD!int;
auto a = new long[][](n);
foreach (i; 0..n)
{
a[i] = RDA;
}
long x, y;
foreach (i; 0..n)
{
bool ok = true;
foreach (j; 0..m)
{
if (a[i][j] == 1)
{
ok = false;
break;
}
}
if (ok)
++x;
}
foreach (i; 0..m)
{
bool ok = true;
foreach (j; 0..n)
{
if (a[j][i] == 1)
{
ok = false;
break;
}
}
if (ok)
++y;
}
ans[ti] = min(x, y) % 2;
}
foreach (e; ans)
writeln(e ? "Ashish" : "Vivek");
stdout.flush;
debug readln;
}
|
D
|
import std.container;
import std.range;
import std.algorithm;
import std.array;
import std.string;
import std.conv;
import std.stdio;
import std.container;
void main() {
auto n = readln.chomp.to!int;
auto l = readln.chomp.split(' ').map!(to!int).array;
long count;
foreach (i;0..n - 2) {
foreach (j;i + 1..n - 1) {
foreach (k;j + 1..n) {
auto large = max(l[i], l[j], l[k]);
auto small = min(l[i], l[j], l[k]);
auto middle = cast(int)(cast(long)l[i] + l[j] + l[k] - large - small);
if (large != middle && middle != small && large < small + middle) {
++count;
}
}
}
}
count.writeln;
}
|
D
|
import std.stdio;
import std.string;
void main() {
auto ar = readln.chomp.split(" ");
auto a = ar[0];
auto b = ar[1];
auto c = ar[2];
if (a == b && b == c) {
writeln("No");
return;
}
if (a != b && a != c && b != c) {
writeln("No");
return;
}
writeln("Yes");
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto T = RD!int;
auto ans = new char[][](T);
foreach (ti; 0..T)
{
auto n = RD!int;
auto s = RD!string;
ans[ti].length = n;
int pos;
foreach (i; 0..n)
{
if (s[i] == '?') continue;
pos = i;
break;
}
foreach (i; pos..n)
{
if (s[i] != '?')
ans[ti][i] = s[i];
else if (i == 0)
{
ans[ti][i] = 'R';
}
else
{
if (ans[ti][i-1] == 'R')
ans[ti][i] = 'B';
else
ans[ti][i] = 'R';
}
}
foreach_reverse (i; 0..pos)
{
if (s[i] != '?')
ans[ti][i] = s[i];
else
{
if (ans[ti][i+1] == 'R')
ans[ti][i] = 'B';
else
ans[ti][i] = 'R';
}
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto A = RD;
auto B = RD;
auto C = RD;
auto K = RD;
auto c1 = min(A, K);
K -= c1;
auto ans = c1;
auto c2 = min(B, K);
K -= c2;
auto c3 = min(C, K);
ans -= c3;
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.array, std.algorithm, std.range;
void main()
{
foreach(s;stdin.byLine())
{
int[] a;
foreach(c;s) a~=c-'0';
foreach_reverse(i;1..a.length)
foreach(j;0..i) a[j]=(a[j]+a[j+1])%10;
writeln(a[0]);
}
}
|
D
|
import std.stdio, std.string, std.conv, std.range, std.algorithm;
void main() {
auto s = readln.chomp;
char[] line;
foreach (c; s) {
if (c == 'B' && line.length > 0) {
line = line.dropBackOne;
} else if (c == '0' || c == '1') {
line ~= c;
}
}
line.writeln;
}
|
D
|
void main() {
auto N = ri;
auto ip = readAs!(string[]), S = ip[0], T = ip[1];
foreach(i; 0..N) {
write(S[i]);
write(T[i]);
}
writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.stdio;
import std.string;
import std.conv,std.array;
void main(){
//auto i = to!int( readln().chomp() );
Map map = new Map();
while(1){
auto cs = readln().chomp().split(",");
if( !cs ){ break; }
map.drop( to!int(cs[0]), to!int(cs[1]), to!int(cs[2]) );
}
writeln(map.not);
writeln(map.max);
}
class Map{
int[10][10] map;
int not = 10*10;
int max = 0;
void drop( int x, int y, int w ){
drop(x,y);
drop(x,y+1);
drop(x+1,y);
drop(x,y-1);
drop(x-1,y);
if( w<2 ){ return; }
drop(x+1,y+1);
drop(x+1,y-1);
drop(x-1,y+1);
drop(x-1,y-1);
if( w<3 ){ return; }
drop(x+2,y);
drop(x-2,y);
drop(x,y+2);
drop(x,y-2);
}
void drop( int x,int y ){
if( x<0 || y<0 || 9<x || 9<y ){ return; }
map[x][y]+=1;
if( map[x][y] == 1 ){ not--; }
if( max < map[x][y] ){ max = map[x][y]; }
}
}
|
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 n = readln.split.to!(int[]);
writeln(n[1], " ", n[2], " ", n[2]);
}
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
long[10 ^^ 5] dp;
dp[] = long.max;
dp[0] = 0;
long N = lread();
auto h = aryread();
foreach (i; 0 .. N - 2)
{
dp[i + 1].minAssign(dp[i] + abs(h[i] - h[i + 1]));
dp[i + 2].minAssign(dp[i] + abs(h[i] - h[i + 2]));
}
dp[N - 1].minAssign(dp[N - 2] + abs(h[N - 2] - h[N - 1]));
dp[N - 1].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;
immutable long MOD = 10^^9 + 7;
void main() {
auto N = readln.chomp.to!int;
auto ans = new int[](N);
int tmp = 1<<29;
int x = 1;
for (int i = 1; i <= N; ++i) {
if (i + N/i + (N%i>0) < tmp) {
tmp = i + N/i + (N%i>0);
x = i;
}
}
for (int i = 1, p = 0; p < N; i += x) {
for (int j = min(N, i + x - 1); j >= i && p < N; --j, ++p) {
ans[p] = j;
}
}
ans.map!(to!string).join(" ").writeln;
}
|
D
|
void main()
{
long n = rdElem;
Point[] p = n.rdCol!Point;
Point q = Point(0, 0, 0);
foreach (x; p)
{
Point diff = x - q;
if (diff.t < diff.len || (diff.t & 1) != (diff.len & 1))
{
"No".writeln;
return;
}
q = x;
}
"Yes".writeln;
}
struct Point
{
long t, x, y;
Point opBinary(string op)(Point that)
if ((op == "+" || op == "-" || op == "*"))
{
return Point(this.t-that.t, this.x-that.x, this.y-that.y);
}
long len()
{
return this.x.abs + this.y.abs;
}
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
immutable int mod = 998_244_353;
void main ()
{
string s;
while ((s = readln.strip ()) != "")
{
auto t = readln.strip;
auto n = s.length.to !(int);
auto m = t.length.to !(int);
auto f = new int [] [] (n + 1, n + 1);
foreach (i; 0..n + 1)
{
f[i][i] = 1;
}
foreach (len; 0..n)
{
auto letter = s[len];
foreach (start; 0..n + 1 - len)
{
auto finish = start + len;
auto cur = f[start][finish];
if (start > 0 &&
(start - 1 >= m || letter == t[start - 1]))
{
f[start - 1][finish] += cur;
if (f[start - 1][finish] >= mod)
{
f[start - 1][finish] -= mod;
}
}
if (finish < n &&
(finish >= m || letter == t[finish]))
{
f[start][finish + 1] += cur;
if (f[start][finish + 1] >= mod)
{
f[start][finish + 1] -= mod;
}
}
}
}
writeln (sum (f[0][m..$], 0L) % mod);
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto N = RD;
auto X = RD-1;
auto Y = RD-1;
auto d = Y - X - 1;
foreach (i; 1..N)
{
long ans;
foreach (j; 0..N-1)
{
if (abs(X-j) < i)
{
auto remain = i - abs(X-j) - 1;
auto p1 = j+i;
auto p2 = Y-remain;
auto p3 = Y+remain;
if (remain == 0)
{
if (p2 > p1)
ans += 2;
else if (p1 < N)
++ans;
}
else
{
if (p1 >= p3)
{
if (p1 < N)
++ans;
}
else
{
if (p3 < N)
++ans;
if (p2 > p1)
ans += 2;
else if (p2 == p1)
++ans;
}
}
}
else if (j+i < N)
++ans;
debug writeln("i:", i, " j:", j, " ans:", ans);
}
writeln(ans);
}
stdout.flush;
debug readln;
}
|
D
|
module main;
import core.stdc.stdio;
int main(string[] argv)
{
int n,l,tmp;
scanf("%d", &n);
int [] a = new int[n+5];
for(int i = 0; i<n; i++)
scanf("%d", &a[i]);
for(int i=0;i<n;i++)
{
if (i==0||a[i]==a[i-1])
l = i + 1;
else break;
}
tmp = l;
for(int i=0; i<n; i++)
{
if (i==0||a[i]!=a[i-1])
{
if (tmp!=l)
{
printf("NO");
return 0;
}
tmp=1;
}
else tmp++;
}
if (tmp==l)
printf("YES");
else
printf("NO");
return 0;
}
|
D
|
void main() {
int[] tmp = readln.split.to!(int[]);
int a = tmp[0], b = tmp[1], c = tmp[2];
writeln(a + b >= c ? "Yes" : "No");
}
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
|
/* 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
20
---+/
/+---test
2
---+/
/+---test
99992
---+/
void main(string[] args) {
const X = readln.chomp.to!long;
auto th = new long[](200000);
th[] = true;
th[0..2] = false;
foreach (i; 2..200000) {
for (long j = i*2; j < th.length; j += i) {
th[j] = false;
}
}
(X + th[X..$].countUntil(true)).writeln;
}
|
D
|
module main;
import core.stdc.stdio;
import std.algorithm;
int main(string[] argv)
{
int n, ans = 0;
scanf("%d", &n);
int[] t = new int[n];
for (int i = 0; i < n; ++i) {
int h, m;
scanf("%d:%d", &h, &m);
t[i] = 60*h+m;
}
if (n == 1) {
puts("23:59");
return 0;
}
t.sort();
for (int i = 1; i < n; ++i) {
int v = t[i]-t[i-1]-1;
if (v > ans) ans = v;
}
int tmp = 60*24+t[0]-t[n-1]-1;
if (tmp > ans) ans = tmp;
printf("%02d:%02d\n", ans/60, ans%60);
return 0;
}
|
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;
int[int] cnt;
foreach (a; readln.chomp.split.map!(to!int)) {
cnt[a - 1]++;
cnt[a]++;
cnt[a + 1]++;
}
int ans = 0;
foreach (k, v; cnt) {
ans = max(ans, v);
}
ans.writeln;
}
|
D
|
import std.algorithm, std.array, std.container, std.range, std.bitmanip;
import std.numeric, std.math, std.bigint, std.random, core.bitop;
import std.string, std.ascii, std.regex, std.conv, std.stdio, std.typecons;
void main()
{
auto s = readln.chomp;
auto p = readln.chomp;
foreach (i; 0..s.length)
if ((s[i..$] ~ s[0..i]).indexOf(p) != -1) {
writeln("Yes");
return;
}
writeln("No");
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
void main() {
string s = readln.chomp;
string odd, even;
foreach (i, x; s) {
if (i % 2) even ~= x;
else odd ~= x;
}
writeln(odd.count('L') || even.count('R') ? "No" : "Yes");
}
|
D
|
import std.stdio, std.string, std.conv, std.range, std.algorithm, std.functional;
void main() {
readln.split.to!(int[]).pipe!(input => writeln(min(input[0] * input[1], input[2])));
}
|
D
|
import std.stdio;
import std.array;
import std.conv;
void main() {
string[] input=split(readln());
int N=to!int(input[0]);
int A=to!int(input[1]);
int B=to!int(input[2]);
int ans=0;
for(int i=1;i<=N;i++) {
int sum=0;
int j=i;
while(j>0) sum+=j%10,j/=10;
if(A<=sum&&sum<=B) ans+=i;
}
writeln(ans);
}
|
D
|
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.typecons, std.range, std.container, std.math, std.algorithm.searching, std.functional,std.mathspecial;
void main(){
auto ab=readln.split.map!(to!int).map!"a==1?999:a".array;
if(ab[0]==ab[1]) writeln("Draw");
else if(ab[0]<ab[1]) writeln("Bob");
else writeln("Alice");
}
|
D
|
/+ dub.sdl:
name "E"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.container.deque;
int main() {
auto sc = new Scanner(stdin);
int n, m;
int[] a;
sc.read(n, m, a); a[] -= 1;
long[] f0 = new long[2*m+1];
long[] f1 = new long[2*m+1];
long sm = 0;
foreach (i; 1..n) {
//a[i-1] -> a[i]
int l = a[i-1], r = a[i];
if (l == r) continue;
if (r < l) r += m;
sm += r-l;
f1[l+1]++;
f1[r+1]--;
f0[l+1] -= l+1;
f0[r+1] += l+1;
}
foreach (i; 0..2*m) {
f0[i+1] += f0[i];
f1[i+1] += f1[i];
}
long ma = 0;
foreach (i; 0..m) {
long x = f0[i] + f0[i+m] + i*f1[i] + (i+m)*f1[i+m];
ma = max(ma, x);
}
writeln(sm-ma);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
static if (__VERSION__ <= 2070) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
version (X86) static if (__VERSION__ < 2071) {
import core.bitop : bsf, bsr, popcnt;
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/deque.d */
// module dcomp.container.deque;
struct Deque(T, bool mayNull = true) {
import core.exception : RangeError;
import core.memory : GC;
import std.range : ElementType, isInputRange;
import std.traits : isImplicitlyConvertible;
struct Payload {
T *d;
size_t st, length, cap;
@property bool empty() const { return length == 0; }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {
version(assert) if (length <= i) throw new RangeError();
return d[(st+i >= cap) ? (st+i-cap) : st+i];
}
private void expand() {
import std.algorithm : max;
assert(length == cap);
auto nc = max(size_t(4), 2*cap);
T* nd = cast(T*)GC.malloc(nc * T.sizeof);
foreach (i; 0..length) {
nd[i] = this[i];
}
d = nd; st = 0; cap = nc;
}
void clear() {
st = length = 0;
}
void insertFront(T v) {
if (length == cap) expand();
if (st == 0) st += cap;
st--; length++;
this[0] = v;
}
void insertBack(T v) {
if (length == cap) expand();
length++;
this[length-1] = v;
}
void removeFront() {
assert(!empty, "Deque.removeFront: Deque is empty");
st++; length--;
if (st == cap) st = 0;
}
void removeBack() {
assert(!empty, "Deque.removeBack: Deque is empty");
length--;
}
}
struct RangeT(A) {
alias T = typeof(*(A.p));
alias E = typeof(A.p.d[0]);
T *p;
size_t a, b;
@property bool empty() const { return b <= a; }
@property size_t length() const { return b-a; }
@property RangeT save() { return RangeT(p, a, b); }
@property RangeT!(const A) save() const {
return typeof(return)(p, a, b);
}
alias opDollar = length;
@property ref inout(E) front() inout { return (*p)[a]; }
@property ref inout(E) back() inout { return (*p)[b-1]; }
void popFront() {
version(assert) if (empty) throw new RangeError();
a++;
}
void popBack() {
version(assert) if (empty) throw new RangeError();
b--;
}
ref inout(E) opIndex(size_t i) inout { return (*p)[i]; }
RangeT opSlice() { return this.save; }
RangeT opSlice(size_t i, size_t j) {
version(assert) if (i > j || a + j > b) throw new RangeError();
return typeof(return)(p, a+i, a+j);
}
RangeT!(const A) opSlice() const { return this.save; }
RangeT!(const A) opSlice(size_t i, size_t j) const {
version(assert) if (i > j || a + j > b) throw new RangeError();
return typeof(return)(p, a+i, a+j);
}
}
alias Range = RangeT!Deque;
alias ConstRange = RangeT!(const Deque);
alias ImmutableRange = RangeT!(immutable Deque);
Payload* p;
private void I() { if (mayNull && !p) p = new Payload(); }
private void C() const {
version(assert) if (mayNull && !p) throw new RangeError();
}
static if (!mayNull) {
@disable this();
}
private this(Payload* p) {
this.p = p;
}
this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) {
p = new Payload();
foreach (v; values) {
insertBack(v);
}
}
this(Range)(Range r)
if (isInputRange!Range &&
isImplicitlyConvertible!(ElementType!Range, T) &&
!is(Range == T[])) {
p = new Payload();
foreach (v; r) {
insertBack(v);
}
}
static Deque make() { return Deque(new Payload()); }
@property bool havePayload() const { return (!mayNull || p); }
@property bool empty() const { return (!havePayload || p.empty); }
@property size_t length() const { return (havePayload ? p.length : 0); }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; }
ref inout(T) front() inout {C; return (*p)[0]; }
ref inout(T) back() inout {C; return (*p)[$-1]; }
void clear() { if (p) p.clear(); }
void insertFront(T v) {I; p.insertFront(v); }
void insertBack(T v) {I; p.insertBack(v); }
void removeFront() {C; p.removeFront(); }
void removeBack() {C; p.removeBack(); }
Range opSlice() {I; return Range(p, 0, length); }
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
if (f.eof) return false;
line = lineBuf[];
f.readln(line);
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else {
auto buf = line.split.map!(to!E).array;
static if (isStaticArray!T) {
assert(buf.length == T.length);
}
x = buf;
line.length = 0;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
|
D
|
void main() {
int n = readln.chomp.to!int;
int[] p = new int[n];
foreach (i; 0 .. n) {
p[i] = readln.chomp.to!int;
}
writeln(p.sum - p.reduce!max / 2);
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.container;
import std.typecons;
|
D
|
import std.stdio, std.conv, std.string, std.math, std.algorithm;
void main(){
auto ip = readln.split.to!(int[]);
if(ip[0] + ip[1] < 24) (ip[0] + ip[1]).writeln;
else (ip[0] + ip[1] - 24).writeln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static 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; }
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 edges = new long[2][][](N);
foreach (i; 0..N-1)
{
auto a = RD-1;
auto b = RD-1;
edges[a] ~= [b, i];
edges[b] ~= [a, i];
}
auto ans = new long[](N-1);
long[3][] open = [[0, -1, 0]];
while (!open.empty)
{
auto n = open.front; open.popFront;
auto u = n[0];
auto last = n[1];
auto used = n[2];
long c = 1;
foreach (e; edges[u])
{
auto v = e[0];
auto i = e[1];
if (v == last) continue;
if (c == used) ++c;
ans[i] = c;
open ~= [v, u, c];
++c;
}
}
long x;
foreach (e; ans)
x = max(x, e);
writeln(x);
foreach (e; ans)
writeln(e);
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 nx = readints;
int x = nx[1];
auto xs = readints;
auto ans = xs.map!(e => abs(x - e)).reduce!((a, b) => gcd(a, b));
writeln(ans);
}
T gcd(T)(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
ulong MOD = 1_000_000_007;
ulong INF = 1_000_000_000_000;
alias Side = Tuple!(long, "from", long, "to");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
long n, m;
long[][] graph;
void main()
{
scan(n, m);
auto g = new long[][](n, 0);
auto sides = new Side[](m);
foreach (i; iota(m))
{
long a, b;
scan(a, b);
g[a - 1] ~= [b - 1];
g[b - 1] ~= [a - 1];
sides[i] = Side(a - 1, b - 1);
}
graph = g;
long cnt;
foreach (e; sides)
{
auto visited = new long[](n);
visited[0] = 1;
dfs(0, 0, e, visited);
// writeln();
// visited.writeln();
if(visited.sum != n)
cnt++;
}
cnt.writeln();
}
void dfs(long vertex, long neighbor, Side invail, long[] visited)
{
if (neighbor >= graph[vertex].length)
return;
auto next = graph[vertex][neighbor];
if (!visited[next])
{
// writef("vertex = %s next = %s ,", vertex + 1, next + 1);
if(Side(vertex, next) == invail || Side(next, vertex) == invail)
{
dfs(vertex, neighbor + 1, invail, visited);
}
else
{
visited[next] = 1;
dfs(next, 0, invail, visited);
}
dfs(vertex, neighbor + 1, invail, visited);
}
else
{
dfs(vertex, neighbor + 1, invail, visited);
}
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.