code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nd = readln.split.to!(int[]);
auto N = nd[0];
auto D = nd[1];
auto d = D*2+1;
writeln((N+d-1) / d);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
string s = readln.chomp;
writeln("2018" ~ s[4 .. $]);
}
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.range;
import std.array;
import std.conv;
void main() {
int [] dp = new int[101];
dp[] = 0;
long n;
while(!stdin.eof()) {
auto s = readln().replace(",", " ").split();
if(!s.empty()){
int [] newdp;
int offset;
if(n < s.length) {
offset = 1;
} else {
offset = 2;
}
n = s.length;
newdp ~= 0;
for(int i = 0; i < s.length; ++i) {
newdp ~= max(dp[i+offset], dp[i-1+offset])+to!int(s[i]);
}
newdp ~= 0;
dp = newdp;
}
}
writeln(dp[1]);
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
immutable long MOD = 10^^9 + 7;
void main() {
auto S = readln.chomp;
auto T = readln.chomp;
int N = S.length.to!int;
auto ss = new int[][](26);
auto tt = new int[][](26);
foreach (i; 0..N) ss[S[i] - 'a'] ~= i;
foreach (i; 0..N) tt[T[i] - 'a'] ~= i;
ss = ss.filter!(a => !a.empty).array;
tt = tt.filter!(a => !a.empty).array;
ss.sort!"a.front < b.front";
tt.sort!"a.front < b.front";
foreach (i; 0..ss.length) {
if (ss[i] != tt[i]) {
writeln("No");
return;
}
}
writeln("Yes");
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
while (1) {
int n;
scan(n);
if (!n) return;
int ans = 10^^7;
for (int z; z^^3 <= n; z++) {
for (int y; y^^2 <= n - z^^3; y++) {
int x = n - z^^3 - y^^2;
ans = min(ans, x + y + z);
}
}
writeln(ans);
}
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.algorithm;
import std.conv;
import std.stdio;
import std.string;
void main()
{
auto abck = readln.split.map!( to!long );
writeln( solve( abck[ 0 ], abck[ 1 ], abck[ 2 ], abck[ 3 ] ) );
}
long solve( in long a, in long b, in long c, in long k )
{
if( k % 2 == 0 ) return a - b;
else return b - a;
}
unittest
{
assert( solve( 1, 2, 3, 1 ) == 1 );
assert( solve( 2, 3, 2, 0 ) == -1 );
assert( solve( 1000000000, 1000000000, 1000000000, 1000000000000000000 ) == 0 );
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto n = readln.chomp.to!size_t;
auto root = new RootNode!int;
foreach (_; n.iota) {
auto rd = readln.chomp.splitter(' '), cmd = rd.front;
switch (cmd) {
case "insert":
rd.popFront;
root.insert(rd.front.to!int);
break;
case "print":
root.inorderWalk;
root.preorderWalk;
break;
default:
assert(0);
}
}
}
class Node(T)
{
T key;
Node l, r;
this(T key)
{
this.key = key;
}
void insert(T val)
{
if (val < key) {
if (l is null) l = new Node(val);
else l.insert(val);
} else {
if (r is null) r = new Node(val);
else r.insert(val);
}
}
void inorderWalk()
{
if (l !is null) l.inorderWalk;
write(" ", key);
if (r !is null) r.inorderWalk;
}
void preorderWalk()
{
write(" ", key);
if (l !is null) l.preorderWalk;
if (r !is null) r.preorderWalk;
}
}
class RootNode(T)
{
Node!T n;
void insert(T val)
{
if (n is null) n = new Node!T(val);
else n.insert(val);
}
void inorderWalk()
{
if (n !is null) n.inorderWalk;
writeln;
}
void preorderWalk()
{
if (n !is null) n.preorderWalk;
writeln;
}
}
|
D
|
/+ dub.sdl:
name "E"
dependency "dunkelheit" version="1.0.1"
+/
import std.stdio, std.algorithm, std.range, std.conv, std.string;
// import dkh.foundation, dkh.scanner, dkh.container.deque;
int main() {
Scanner sc = new Scanner(stdin);
scope(exit) assert(!sc.hasNext);
string s;
sc.read(s);
string l, r;
while (s.length) {
if (s.length == 1) {
l ~= s;
break;
}
if (s.front == s.back) {
l ~= s.front;
r ~= s.back;
s = s[1..$-1];
continue;
}
if (s[1] == s.back) {
s = s[1..$];
continue;
}
if (s.front == s[$-1]) {
s = s[0..$-1];
continue;
}
s = s[1..$-1];
continue;
}
writeln(l ~ r.dup.reverse);
return 0;
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/foundation.d */
// module dkh.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/container/deque.d */
// module dkh.container.deque;
struct DequePayload(T) {
import core.exception : RangeError;
private T* _data;
private uint start, len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
if (start + i < cap) return _data[start + i];
else return _data[start + i - cap];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import std.algorithm : max;
import std.conv : to;
if (newCap <= cap) return;
T* newData = cast(T*)GC.malloc(newCap * T.sizeof);
foreach (i; 0..length) {
newData[i] = this[i];
}
_data = newData; start = 0; cap = newCap.to!uint;
}
void clear() {
start = len = 0;
}
import std.algorithm : max;
void insertFront(T item) {
if (len == cap) reserve(max(cap * 2, 4));
if (start == 0) start += cap;
start--; len++;
this[0] = item;
}
void insertBack(T item) {
if (len == cap) reserve(max(cap * 2, 4));
len++;
this[len-1] = item;
}
void removeFront() {
assert(!empty, "Deque.removeFront: Deque is empty");
start++; len--;
if (start == cap) start = 0;
}
void removeBack() {
assert(!empty, "Deque.removeBack: Deque is empty");
len--;
}
}
struct Deque(T, bool mayNull = true) {
import core.exception : RangeError;
import std.range : ElementType, isInputRange;
import std.traits : isImplicitlyConvertible;
alias Payload = DequePayload!T;
Payload* _p;
static if (!mayNull) @disable this();
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);
}
}
private this(Payload* p) { _p = p; }
static Deque make() { return Deque(new Payload()); }
private 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 {
assert(!empty, "Deque.opIndex: Deque is empty");
return (*_p)[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void clear() { if (_p) _p.clear(); }
void insertFront(T item) {
if (mayNull && !_p) _p = new Payload();
_p.insertFront(item);
}
void insertBack(T item) {
if (mayNull && !_p) _p = new Payload();
_p.insertBack(item);
}
alias opOpAssign(string op : "~") = insertBack;
alias stableInsertBack = insertBack;
void removeFront() {
assert(!mayNull || _p, "Deque.removeFront: Deque is empty");
_p.removeFront();
}
void removeBack() {
assert(!mayNull || _p, "Deque.removeBack: Deque is empty");
_p.removeBack();
}
alias stableRemoveBack = removeBack;
alias Range = RangeT!(DequePayload!T);
alias ConstRange = RangeT!(const DequePayload!T);
alias ImmutableRange = RangeT!(immutable DequePayload!T);
size_t[2] opSlice(size_t dim : 0)(size_t start, size_t end) const {
assert(start <= end && end <= length);
return [start, end];
}
Range opIndex(size_t[2] rng) { return Range(_p, rng[0], rng[1]); }
ConstRange opIndex(size_t[2] rng) const { return ConstRange(_p, rng[0], rng[1]); }
ImmutableRange opIndex(size_t[2] rng) immutable { return ImmutableRange(_p, rng[0], rng[1]); }
auto opIndex() inout { return this[0..$]; }
static struct RangeT(QualifiedPayload) {
alias A = QualifiedPayload;
import std.traits : CopyTypeQualifiers;
alias E = CopyTypeQualifiers!(A, T);
A *p;
size_t l, r;
@property bool empty() const { return r <= l; }
@property size_t length() const { return r - l; }
alias opDollar = length;
@property auto save() { return this; }
ref inout(E) opIndex(size_t i) inout {
version(assert) if (empty) throw new RangeError();
return (*p)[l+i];
}
@property ref inout(E) front() inout { return this[0]; }
@property ref inout(E) back() inout { return this[$-1]; }
void popFront() {
version(assert) if (empty) throw new RangeError();
l++;
}
void popBack() {
version(assert) if (empty) throw new RangeError();
r--;
}
size_t[2] opSlice(size_t dim : 0)(size_t start, size_t end) const {
assert(start <= end && end <= length);
return [start, end];
}
auto opIndex(size_t[2] rng) { return RangeT(p, l+rng[0], l+rng[1]); }
auto opIndex(size_t[2] rng) const { return RangeT!(const A)(p, l+rng[0], l+rng[1]); }
auto opIndex(size_t[2] rng) immutable { return RangeT!(immutable A)(p, l+rng[0], l+rng[1]); }
auto opIndex() inout { return this[0..$]; }
}
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
private File f;
this(File f) {
this.f = f;
}
private char[512] lineBuf;
private 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) {
assert(succW());
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
void read(Args...)(auto ref Args args) {
import std.exception : enforce;
static if (args.length != 0) {
enforce(readSingle(args[0]));
read(args[1..$]);
}
}
bool hasNext() {
return succ();
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;
void main() {
auto T = readln.chomp.to!int;
while (T--) {
auto s = readln.split.map!(to!long);
auto l = s[0];
auto r = s[1];
writeln(l, " ", l * 2);
}
}
|
D
|
// import chie template :) {{{
static if (__VERSION__ < 2090) {
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons, std.format,
std.bitmanip, std.numeric;
} else {
import std;
}
// }}}
// nep.scanner {{{
class Scanner {
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) {
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next() {
if (idx < str.length) {
return str[idx++];
}
char[] s;
while (s.length == 0) {
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(size_t len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
void scan(T...)(ref T args) {
foreach (ref arg; args) {
arg = next!(typeof(arg));
}
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType)) {
str ~= s.to!(char[]).strip.split;
}
}
// }}}
// alias {{{
alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less);
alias MinHeap(T) = Heap!(T, "a > b");
// }}}
// memo {{{
/*
- ある値が見つかるかどうか
<https://dlang.org/phobos/std_algorithm_searching.html#canFind>
canFind(r, value); -> bool
- 条件に一致するやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#filter>
// 2で割り切れるやつ
filter!"a % 2 == 0"(r); -> Range
- 合計
<https://dlang.org/phobos/std_algorithm_iteration.html#sum>
sum(r);
- 累積和
<https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold>
// 今の要素に前の要素を足すタイプの一般的な累積和
// 累積和のrangeが帰ってくる(破壊的変更は行われない)
cumulativeFold!"a + b"(r, 0); -> Range
- rangeをarrayにしたいとき
array(r); -> Array
- 各要素に同じ処理をする
<https://dlang.org/phobos/std_algorithm_iteration.html#map>
// 各要素を2乗
map!"a * a"(r) -> Range
- ユニークなやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#uniq>
uniq(r) -> Range
- 順列を列挙する
<https://dlang.org/phobos/std_algorithm_iteration.html#permutations>
permutation(r) -> Range
- ある値で埋める
<https://dlang.org/phobos/std_algorithm_mutation.html#fill>
fill(r, val); -> void
- バイナリヒープ
<https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap>
// 昇順にするならこう(デフォは降順)
BinaryHeap!(Array!T, "a > b") heap;
heap.insert(val);
heap.front;
heap.removeFront();
- 浮動小数点の少数部の桁数設定
// 12桁
writefln("%.12f", val);
- 浮動小数点の誤差を考慮した比較
<https://dlang.org/phobos/std_math.html#.approxEqual>
approxEqual(1.0, 1.0099); -> true
- 小数点切り上げ
<https://dlang.org/phobos/std_math.html#.ceil>
ceil(123.4); -> 124
- 小数点切り捨て
<https://dlang.org/phobos/std_math.html#.floor>
floor(123.4) -> 123
- 小数点四捨五入
<https://dlang.org/phobos/std_math.html#.round>
round(4.5) -> 5
round(5.4) -> 5
*/
// }}}
void main() {
auto cin = new Scanner;
string s;
cin.scan(s);
int c = s[$ - 1] - '0';
if (c == 2 || c == 4 || c == 5 || c == 7 || c == 9) {
writeln("hon");
} else if (c == 0 || c == 1 || c == 6 || c == 8) {
writeln("pon");
} else {
writeln("bon");
}
}
|
D
|
void main()
{
long n = rdElem;
long f(long x)
{
return x * (x + 1) / 2;
}
long l, r = n;
while (r - l > 1)
{
long m = (l + r) / 2;
long p = f(m);
if (p < n) l = m;
else r = m;
}
long total = f(r);
long diff = total - n;
foreach (i; 1 .. r+1)
{
if (i != diff) i.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 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.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.math;
void main() {
string input = readln();
if(input[0] == input[2]) "Yes".writeln;
else "No".writeln;
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto n = readln.chomp.to!size_t;
auto root = new RootNode!int;
foreach (_; n.iota) {
auto rd = readln.chomp.splitter(' '), cmd = rd.front;
switch (cmd) {
case "insert":
rd.popFront;
root.insert(rd.front.to!int);
break;
case "find":
rd.popFront;
writeln(root.find(rd.front.to!int) ? "yes" : "no");
break;
case "print":
root.inorderWalk;
root.preorderWalk;
break;
default:
assert(0);
}
}
}
class Node(T)
{
T key;
Node l, r;
this(T key)
{
this.key = key;
}
void insert(T val)
{
if (val < key) {
if (l is null) l = new Node(val);
else l.insert(val);
} else {
if (r is null) r = new Node(val);
else r.insert(val);
}
}
bool find(T val)
{
if (key == val) {
return true;
} else if (val < key) {
if (l is null) return false;
else return l.find(val);
} else {
if (r is null) return false;
else return r.find(val);
}
}
void inorderWalk()
{
if (l !is null) l.inorderWalk;
write(" ", key);
if (r !is null) r.inorderWalk;
}
void preorderWalk()
{
write(" ", key);
if (l !is null) l.preorderWalk;
if (r !is null) r.preorderWalk;
}
}
class RootNode(T)
{
Node!T n;
void insert(T val)
{
if (n is null) n = new Node!T(val);
else n.insert(val);
}
bool find(T val)
{
if (n is null) return false;
else return n.find(val);
}
void inorderWalk()
{
if (n !is null) n.inorderWalk;
writeln;
}
void preorderWalk()
{
if (n !is null) n.preorderWalk;
writeln;
}
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
int calc(int[][] g) {
int n = cast(int)g.length;
auto ind = new int[n];
auto gg = new int[][](n, n);
int[][] q;
for (int i = 0; i < n; i++) {
// i 番目の対戦相手
int index = ind[i]++;
if (index == n) continue;
int t = g[i][index]; // 相手
int a = min(i, t);
int b = max(i, t);
gg[a][b]++;
if (gg[a][b] == 2) {
q ~= [a, b];
}
}
int match = 0;
bool update = true;
int date = 0;
auto used = new bool[n];
while (update) {
used[] = false;
update = false;
int[][] qq;
while (q.length > 0) {
auto x = q[$-1]; q.popBack();
int a = x[0];
int b = x[1];
if (!used[a] && !used[b]) {
used[a] = used[b] = true;
match++;
// writeln("match ", a, " ", b);
update = true;
int aIndex = ind[a]++;
if (aIndex < n-1) { // 次の対戦相手
int t = g[a][aIndex];
int mi = min(a, t);
int ma = max(a, t);
gg[mi][ma]++;
if (gg[mi][ma] == 2) {
qq ~= [mi, ma];
}
}
int bIndex = ind[b]++;
if (bIndex < n-1) {
int t = g[b][bIndex];
int mi = min(b, t);
int ma = max(b, t);
gg[mi][ma]++;
if (gg[mi][ma] == 2) {
qq ~= [mi, ma];
}
}
}
}
for (int i = 0; i < qq.length; i++) q ~= qq[i];
if (update) date++;
}
if (match == n * (n - 1) / 2) return date;
return -1;
}
void main() {
int n; scan(n);
int[][] g;
for (int i = 0; i < n; i++) {
g ~= readints.map!(e => e - 1).array;
}
writeln(calc(g));
}
|
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);
}
enum MOD = (10 ^^ 9) + 7;
void main()
{
long A, B, C;
scan(A, B, C);
solve(A, B, C).writeln();
}
long solve(long A, long B, long C)
{
if (C <= (A + B))
{
return B + C;
}
else if (C - 1 == A + B)
{
return B + C;
}
else
{
return (A + B) * 2 - A + 1;
}
}
|
D
|
import std.functional,
std.algorithm,
std.bigint,
std.string,
std.traits,
std.array,
std.range,
std.stdio,
std.conv;
void main() {
int[] ABCD = readln.chomp.split.to!(int[]);
int A = ABCD[0],
B = ABCD[1],
C = ABCD[2],
D = ABCD[3];
int L = A + B,
R = C + D;
if (L > R) {
writeln("Left");
} else if (L == R) {
writeln("Balanced");
} else {
writeln("Right");
}
}
|
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 o; readV(o);
string e; readV(e);
foreach (i; 0..e.length)
write(o[i], e[i]);
if (o.length > e.length)
write(o[$-1]);
writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void inputLine(A...)(ref A a) {
string[] line = readln.chomp.split;
if (a.length != line.length) {
throw new Exception("not equal number of input elements and number of variable elements.");
}
foreach (int i, ref e; a) {
e = line[i].to!(A[i]);
// e = line[i].to!(typeof(e));
}
}
bool[100010] p;
void calcPrime() {
p[] = true;
p[0] = p[1] = false;
for (int i = 4; i < 100010; i++) {
for (int j = 2; j * j <= i; j++) {
if (p[j]) {
p[i] &= i % j ? true : false;
}
}
}
}
void main() {
int Q = readln.chomp.to!(int);
calcPrime();
int[100010] hoge;
for (int i = 2; i < 100010; i++) {
hoge[i] = hoge[i - 1] + (p[i] & p[(i + 1) / 2]);
}
for (int i = 0; i < Q; i++) {
int l, r;
inputLine(l, r);
writeln(hoge[r] - hoge[l - 1]);
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto lr = readln.split.to!(long[]);
auto L = lr[0];
auto R = min(L+2020, lr[1]+1);
long x = long.max;
foreach (i; L..R) {
foreach (j; i+1..R) {
x = min(x, i*j%2019);
}
}
writeln(x);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nq = readln.split.to!(int[]);
auto N = nq[0];
auto Q = nq[1];
auto S = readln.chomp;
auto cs = new int[](N);
foreach (i; 1..N) {
cs[i] += cs[i-1];
if (S[i-1] == 'A' && S[i] == 'C') cs[i] += 1;
}
foreach (_; 0..Q) {
auto lr = readln.split.to!(int[]);
writeln(cs[lr[1]-1] - cs[lr[0]-1]);
}
}
|
D
|
import std.stdio;
import std.range;
import std.array;
import std.algorithm;
import std.string;
import std.conv;
void main(){
immutable train_01 = readln.chomp.to!int;
immutable train_02 = readln.chomp.to!int;
immutable bus_01 = readln.chomp.to!int;
immutable bus_02 = readln.chomp.to!int;
immutable answer = min(train_01, train_02) + min(bus_01, bus_02);
writeln(answer);
}
|
D
|
import std.stdio,
std.string,
std.conv;
void main() {
int N = readln.chomp.to!(int);
string[] S;
bool[] tate;
for (int i = 0; i < 2; i++) {
S ~= readln.chomp;
}
for (int i = 0; i < N; i++) {
if (S[0][i] == S[1][i]) {
tate ~= true;
}
else {
tate ~= false;
i++;
}
}
long ans = (tate[0] ? 3 : 6);
for (int i = 1; i < tate.length; i++) {
long kake;
if (tate[i]) {
kake = (tate[i - 1] ? 2 : 1);
}
else {
kake = (tate[i - 1] ? 2 : 3);
}
ans = (ans * kake) % 1000000007;
}
ans.writeln;
}
|
D
|
import std.stdio;
import std.algorithm;
import std.math;
import std.conv;
import std.string;
T readNum(T)(){
return readStr.to!T;
}
T[] readNums(T)(){
return readStr.split.to!(T[]);
}
string readStr(){
return readln.chomp;
}
void main(){
auto nmx = readNums!int;
auto ca = new int[][](nmx[0], nmx[1]+1);
foreach(i; 0 .. nmx[0]){
ca[i] = readNums!int;
}
int ret = -1;
foreach(i; 0.. 2^^nmx[0]){
int[] t = new int[](nmx[1]+1);
foreach(j; 0 .. nmx[0]){
if(i & (1<<j)){
t[] += ca[j][];
}
bool ok = true;
foreach(e; t[1 .. $]){
if(e < nmx[2]){
ok = false;
break;
}
}
if(ok && (ret == -1 || t[0] < ret)){
ret = t[0];
}
}
}
writeln(ret);
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
void main()
{
string s = chomp(readln());
long sec = to!long(s);
string hh = to!string(sec / 3600);
string mm = to!string((sec % 3600) / 60);
string ss = to!string(sec % 3600 % 60);
write(hh);
write(":");
write(mm);
write(":");
writeln(ss);
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
static import std.ascii;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
auto A = sread();
long N = A.length;
auto cnt = new long[]('z' - 'a' + 1);
foreach (c; A)
cnt[c - 'a']++;
writeln(N * (N - 1) / 2 - cnt.map!"a*(a-1)/2"().sum() + 1);
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
while (1) {
int a,L;
scan(a,L);
if (!L) return;
solve(a,L);
}
}
void solve(int a, int L) {
auto as = new int[](21);
as[0] = a;
foreach (i ; 1 .. 21) {
as[i] = calmax(as[i-1], L) - calmin(as[i-1], L);
foreach (j ; 0 .. i) {
if (as[i] == as[j]) {
writeln(j, " ", as[i], " ", i-j);
return;
}
}
}
}
int calmax(int x, int L) {
auto cnt = new int[](10);
while (x > 0) {
cnt[x % 10]++;
x /= 10;
}
cnt[0] += max(0, L - cnt.sum);
int res;
foreach_reverse (i ; 0 .. 10) {
foreach (j ; 0 .. cnt[i]) {
res *= 10;
res += i;
}
}
debug {
writeln(res);
}
return res;
}
int calmin(int x, int L) {
auto cnt = new int[](10);
while (x > 0) {
cnt[x % 10]++;
x /= 10;
}
cnt[0] += max(0, L - cnt.sum);
int res;
foreach (i ; 0 .. 10) {
foreach (j ; 0 .. cnt[i]) {
res *= 10;
res += i;
}
}
debug {
writeln(res);
}
return res;
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.algorithm;
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 abc = readints;
int a = abc[0], b = abc[1], c = abc[2];
int m = max(a, b, c);
if (m % 2 != (a + b + c) % 2) m++;
int ans = (3 * m - (a + b + c)) / 2;
writeln(ans);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
struct Dsu
{
public:
this(int n)
{
_n = n, parent_or_size = new int[](n);
parent_or_size[] = -1;
}
int merge(int a, int b)
{
assert(0 <= a && a < _n);
assert(0 <= b && b < _n);
int x = leader(a), y = leader(b);
if (x == y)
return x;
if (-parent_or_size[x] < -parent_or_size[y])
{
auto tmp = x;
x = y;
y = tmp;
}
parent_or_size[x] += parent_or_size[y];
parent_or_size[y] = x;
return x;
}
bool same(int a, int b)
{
assert(0 <= a && a < _n);
assert(0 <= b && b < _n);
return leader(a) == leader(b);
}
int leader(int a)
{
assert(0 <= a && a < _n);
if (parent_or_size[a] < 0)
return a;
return parent_or_size[a] = leader(parent_or_size[a]);
}
int size(int a)
{
assert(0 <= a && a < _n);
return -parent_or_size[leader(a)];
}
int[][] groups()
{
auto leader_buf = new int[](_n), group_size = new int[](_n);
foreach (i; 0 .. _n)
{
leader_buf[i] = leader(i);
group_size[leader_buf[i]]++;
}
auto result = new int[][](_n);
foreach (i; 0 .. _n)
result[i].reserve(group_size[i]);
foreach (i; 0 .. _n)
result[leader_buf[i]] ~= i;
int[][] filtered;
foreach (r; result)
if (r.length != 0)
filtered ~= r;
return filtered;
}
private:
int _n;
int[] parent_or_size;
}
void main()
{
auto nq = readln.split.to!(int[]);
auto N = nq[0];
auto Q = nq[1];
auto dsu = Dsu(N);
while (Q--) {
auto tuv = readln.split.to!(int[]);
auto t = tuv[0];
auto u = tuv[1];
auto v = tuv[2];
if (t == 0) {
dsu.merge(u, v);
} else {
writeln(dsu.same(u, v) ? 1 : 0);
}
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
void main()
{
auto N = readln.chomp.to!int;
auto K = readln.chomp.to!int;
int sum_t;
foreach (x; readln.split.to!(int[])) {
auto a = x * 2;
auto b = abs(x - K) * 2;
sum_t += min(a, b);
}
writeln(sum_t);
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
auto n=readln.chomp.to!(int);
foreach(b; 0..(1<<3)){
auto tmp=n, sum=0;
foreach(i; 0..3){
if((b>>i)&1) sum+=tmp%10;
else sum-=tmp%10;
tmp/=10;
}
sum+=tmp;
if(sum==7){
char[] s;
foreach(i; 0..3){
s~=(n%10)+'0';
s~=(b>>i)&1 ? '+' : '-';
n/=10;
}
s~=n+'0';
reverse(s);
writeln(s~"=7"); return;
}
}
assert(false);
}
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 A = RDA;
auto N2 = N / 2;
auto dp = new long[long][](N+2);
dp[0][0] = 0;
foreach (i; 0..N)
{
auto remain = N - (i+1);
auto cnt = (remain+1) / 2;
auto base = N2 - cnt;
foreach (key; dp[i].keys)
{
if (key >= base)
{
auto v = dp[i+1].get(key, long.min);
dp[i+1][key] = max(v, dp[i][key]);
}
auto v = dp[i+2].get(key+1, long.min);
dp[i+2][key+1] = max(v, dp[i][key]+A[i]);
}
}
debug writeln(dp);
long ans = long.min;
foreach (key; dp[N].keys)
{
if (key == N2)
ans.chmax(dp[N][key]);
}
foreach (key; dp[N+1].keys)
{
if (key == N2)
ans.chmax(dp[N+1][key]);
}
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
void main()
{
long k, s;
rdVals(k, s);
long cnt;
foreach (x; 0 .. k+1)
{
foreach (y; 0 .. k+1)
{
long z = s - x - y;
if (0 <= z && z <= k) ++cnt;
}
}
cnt.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.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
void main(){
string[] val = inln!string();
writeln( (val[0]=="H")^(val[1]=="H")?'D':'H');
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range;
const long mod = 10^^9+7;
// 1要素のみの入力
T inelm(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] inln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split())ln ~= elm.to!T();
return ln;
}
|
D
|
import std.stdio;
import std.algorithm;
import std.range;
import std.conv;
void main() {
string[] input = split(readln());
long n = to!long(input[0]);
long m = to!long(input[1]);
if (n > m) swap(n,m);
long ans = 0;
if (n == 1) {
ans = max(0, m-2);
if (m == 1) ans = 1;
} else {
ans = (n-2)*(m-2);
}
writeln(ans);
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
int n;
scan(n);
int ans;
foreach (i ; 0 .. n) {
int li, ri;
scan(li, ri);
ans += ri - li + 1;
}
writeln(ans);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
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[string] d;
foreach (_; 0 .. lread())
{
auto s = sread();
if (s !in d)
{
d[s] = 0;
}
d[s]++;
}
foreach (_; 0 .. lread())
{
auto s = sread();
if (s !in d)
{
d[s] = 0;
}
d[s]--;
}
d.values.reduce!max().max(0).writeln();
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
auto ss = reads!(string).map!(toUpper);
writeln(ss[0][0], ss[1][0], ss[2][0]);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
int main()
{
while (true) {
string[] str = readln().chomp().split();
int n = str[0].to!int;
int m = str[1].to!int;
if (m == n && n == 0) break;
int[1001] ans;
int c = n - 1;
for (int i = 0; i < n; i++) ans[i] = i + 1;
while (n > 1) {
c = (c + m) % n;
for (int i = c; i < n - 1; i++) ans[i] = ans[i + 1];
n--;
c--;
}
writeln(ans[0]);
}
return 0;
}
|
D
|
import std.stdio,
std.string,
std.conv,
std.typecons,
std.algorithm;
int abs(int n) {
return n >= 0 ? n : -n;
}
void main() {
int[] buf = readln.chomp.split.to!(int[]);
int H = buf[0], W = buf[1], D = buf[2];
alias Tuple!(int, "x", int, "y") Grid;
Grid[int] A;
for (int i = 1; i <= H; i++) {
int[] m_line = readln.chomp.split.to!(int[]);
for (int j = 0; j < m_line.length; j++) {
A[m_line[j]] = Grid(i, j + 1);
}
}
long[300 * 300 + 1] d;
for (int i = D + 1; i <= H * W; i++) {
d[i] += d[i - D] + abs(A[i].x - A[i - D].x) + abs(A[i].y - A[i - D].y);
}
int Q = readln.chomp.to!(int);
for (int i = 0; i < Q; i++) {
buf = readln.chomp.split.to!(int[]);
int L = buf[0], R = buf[1];
writeln(d[R] - d[L]);
}
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
auto n = readln.chomp.to!(char[]);
auto dp = new long[][][][](n.length + 1, 2, 1000, 2);
dp[0][0][0][0] = 1;
foreach (i; 0 .. (n.length)) {
foreach (l; 0 .. 2) {
foreach (k; 0 .. 1000) {
foreach (ok; 0 .. 2) {
auto x = k / 100, y = (k / 10) % 10, z = k % 10;
auto d = l ? 9 : (n[i] - '0');
for (int digit = 0; digit <= d; digit++) {
auto less = l || (digit < d);
auto okok = ok || (x == 5 && y == 1 && digit == 3);
dp[i + 1][less][y * 100 + z * 10 + digit][okok] += dp[i][l][k][ok];
}
}
}
}
}
long s = 0;
foreach (j; 0 .. 2) {
foreach (k; 0 .. 1000) {
s += dp[n.length][j][k][1];
}
}
writeln(s);
}
void rd(T...)(ref T x) {
import std.stdio : readln;
import std.string : split;
import std.conv : to;
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.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
immutable long inf = 3L * 10L^^18;
void main() {
int n, m;
scan(n, m);
auto uf = UnionFind(n);
foreach (i ; 0 .. m) {
int ai, bi;
scan(ai, bi);
ai--, bi--;
uf.unite(ai, bi);
}
long a = uf.count_node(0), b = uf.count_node(1);
long c = n - a - b;
debug {
writeln(a, " ", b);
}
long ans = a * (a - 1) / 2 + b * (b - 1) / 2 + c * (c - 1) / 2 + max(a, b) * c - m;
writeln(ans);
}
struct UnionFind {
private {
int N;
int[] p;
int[] rank;
int[] num_v;
}
this (int n) {
N = n;
p = iota(N).array;
rank = new int[](N);
num_v = new int[](N);
foreach (i ; 0 .. N) {
num_v[i] = 1;
}
}
int find_root(int x) {
if (p[x] != x) {
p[x] = find_root(p[x]);
}
return p[x];
}
bool same(int x, int y) {
return find_root(x) == find_root(y);
}
void unite(int x, int y) {
int u = find_root(x), v = find_root(y);
if (u == v) return;
if (rank[u] < rank[v]) {
p[u] = v;
num_v[v] += num_v[u];
num_v[u] = 0;
}
else {
p[v] = u;
num_v[u] += num_v[v];
num_v[v] = 0;
if (rank[u] == rank[v]) {
rank[u]++;
}
}
}
int count_node(int x) {
return num_v[find_root(x)];
}
}
void solve(int n, int v, int w) {
if (n == 1) {
writeln(min(v, w));
return;
}
auto va = new int[](0);
va ~= v;
while (v > 1) {
if (v % n <= 1) {
v /= n;
}
else {
v = (v / n) + 1;
}
va ~= v;
}
va.reverse();
auto wa = new int[](0);
wa ~= w;
while (w > 1) {
if (w % n <= 1) {
w /= n;
}
else {
w = (w / n) + 1;
}
wa ~= w;
}
wa.reverse();
debug {
writeln(va);
writeln(wa);
}
int ans = 0;
while (!va.empty) {
while (!wa.empty) {
if (va.front == wa.front) {
ans = max(ans, va.front);
va.popFront;
wa.popFront;
break;
}
else if (va.front < wa.front) {
va.popFront;
break;
}
else {
wa.popFront;
}
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int x, k;
rd(x); rd(k);
auto r=readln.split.to!(int[]);
int q; rd(q);
int u=x, l=0;
int j=0, p=0;
int d=0, e=-1;
while(q--){
int t, a; rd(t, a);
while(j<k && r[j]<=t){
int dd=(r[j]-p)*e;
p=r[j++]; e*=-1;
u=min(x, max(0, u+dd));
l=max(0, min(x, l+dd));
d+=dd;
}
int y=a+d;
y=min(u, max(l, y));
y+=(t-p)*e;
writeln(min(x, max(0, y)));
}
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.container;
import std.bigint;
void main()
{
readln.chomp.toUpper.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) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (ti; 0..t)
{
auto l = RD;
auto r = RD;
auto a = r+1;
ans[ti] = l*2 >= a;
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush;
debug readln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.math;
void main() {
string[] inputs = split(readln());
//int r = to!int(inputs[0]);
int g = to!int(inputs[1]);
int b = to!int(inputs[2]);
if((10*g + b) % 4 == 0) "YES".writeln;
else "NO".writeln;
}
|
D
|
import std.stdio;
import std.conv;
import std.array;
void main()
{
int N = readln.split[0].to!int;
int[] H = readln.split.to!(int[]);
int max = 0;
int ans = 0;
foreach (h; H){
if (h >= max){
ans++;
max = h;
}
}
writeln(ans);
}
|
D
|
import std;
void main(){
int n,m;
scanf("%d %d\n", &n, &m);
auto hs = readln.chomp.split.to!(int[]);
auto as = new int[m];
auto bs = new int[m];
auto mem = new bool[n];
foreach(ref x; mem) {
x = true;
}
foreach(i; 0..m) {
int a,b;
scanf("%d %d\n", &a, &b);
if (hs[a-1] <= hs[b-1]) {
mem[a-1] &= false;
}
if (hs[b-1] <= hs[a-1]) {
mem[b-1] &= false;
}
}
mem.count!"a".writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nm = readln.split.to!(int[]);
auto N = nm[0];
auto M = nm[1];
int[] as, bs;
int t = 1, cnt;
foreach_reverse (i; 1..M+1) if (i%2 == 1) {
as ~= t++;
++cnt;
}
t += cnt-1;
foreach (i; 1..M+1) if (i%2 == 1) {
bs ~= t--;
}
t += cnt+1;
cnt = 0;
foreach_reverse (i; 1..M+1) if (i%2 == 0) {
as ~= t++;
++cnt;
}
t += cnt;
foreach (i; 1..M+1) if (i%2 == 0) {
bs ~= t--;
}
foreach (i, a; as) {
auto b = bs[i];
writeln(a, " ", b);
}
}
|
D
|
import std.stdio, std.algorithm, std.string, std.ascii;
void main()
{
char[] s = readln.strip.dup;
if (s[0] != 'A') {
writeln("WA");
return;
}
auto p = s.indexOf('C');
if (p == -1 || p <= 1 || p > s.length-2) {
writeln("WA");
return;
}
s[0] = 'a';
s[p] = 'a';
if (s.all!(isLower)) {
writeln("AC");
}
else {
writeln("WA");
}
}
|
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);
}
enum MOD = (10 ^^ 9) + 7;
void main()
{
long W, a, b;
scan(W, a, b);
solve(W, a, b).writeln();
}
long solve(long W, long a, long b)
{
if (a + W < b)
{
return b - (a + W);
}
if (b + W < a)
{
return a - (b + W);
}
return 0;
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.range;
import std.array;
import std.conv;
import std.complex;
import std.math;
bool is_uruu(int n) {
return n%400 == 0 || (n%100 != 0 && n%4 == 0);
}
void main() {
bool f = false;
while(true) {
int a, b;
auto s = readln().strip().split();
a = to!int(s[0]);
b = to!int(s[1]);
if((a|b) == 0)
break;
if(f){
writeln();
}else{
f = true;
}
int c;
foreach(int n; a..b+1) {
if(is_uruu(n)){
writeln(n);
++c;
}
}
if(c == 0)
writeln("NA");
}
}
|
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;
void main() {
immutable int MAX = 5010;
auto N = readln.chomp.to!int;
auto A = readln.split.map!(to!int).array;
auto L = new int[](MAX);
auto R = new int[](MAX);
fill(L, MAX);
fill(R, -1);
foreach (i; 0..N) {
L[A[i]] = min(L[A[i]], i);
R[A[i]] = max(R[A[i]], i);
}
auto mem = new int[](N);
fill(mem, -1);
int dp(int i) {
if (i >= N) return 0;
if (mem[i] >= 0) return mem[i];
if (L[A[i]] != i)
return dp(i + 1);
int tar = R[A[i]];
int pos = i + 1;
int score = A[i];
while (pos < N && pos <= tar) {
if (L[A[pos]] < i) {
score = 0;
break;
}
else if (L[A[pos]] == pos) {
score ^= A[pos];
}
tar = max(tar, R[A[pos]]);
pos += 1;
}
return mem[i] = max(dp(i+1), score + dp(tar+1));
}
dp(0).writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void get(Args...)(ref Args args)
{
import std.traits, std.meta, std.typecons;
static if (Args.length == 1) {
alias Arg = Args[0];
static if (isArray!Arg) {
args[0] = readln.split.to!Arg;
} else static if (isTuple!Arg) {
auto input = readln.split;
static foreach (i; 0..Fields!Arg.length) {
args[0][i] = input[i].to!(Fields!Arg[i]);
}
} else {
args[0] = readln.chomp.to!Arg;
}
} else {
auto input = readln.split;
assert(input.length == Args.length);
static foreach (i; 0..Args.length) {
args[i] = input[i].to!(Args[i]);
}
}
}
void get_lines(Args...)(size_t N, ref Args args)
{
import std.traits, std.range;
static foreach (i; 0..Args.length) {
static assert(isArray!(Args[i]));
args[i].length = N;
}
foreach (i; 0..N) {
static if (Args.length == 1) {
get(args[0][i]);
} else {
auto input = readln.split;
static foreach (j; 0..Args.length) {
args[j][i] = input[j].to!(ElementType!(Args[j]));
}
}
}
}
void main()
{
int N; get(N);
long[] AS; get(AS);
foreach (i; 1..N) AS[i] += AS[i-1];
int i, j = 1, k = 2;
long p(int i) { return AS[i]; }
long q(int i) { return AS[j] - AS[i]; }
long r(int k) { return AS[k] - AS[j]; }
long s(int k) { return AS[$-1] - AS[k]; }
auto res = long.max;
while (j < N-1) {
while (i < j-1 && abs(p(i) - q(i)) > abs(p(i+1) - q(i+1))) ++i;
while (k < N-2 && abs(r(k) - s(k)) > abs(r(k+1) - s(k+1))) ++k;
res = min(res, max(p(i), q(i), r(k), s(k)) - min(p(i), q(i), r(k), s(k)));
++j;
}
writeln(res);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void main()
{
auto hwk = readln.split.to!(int[]);
auto H = hwk[0];
auto W = hwk[1];
auto K = hwk[2];
auto MAP = new char[][](H, W);
auto MEMO = new bool[][](H, W);
int sx, sy;
foreach (i; 0..H) foreach (j, c; readln.chomp.to!(char[])) {
if (c == 'S') {
sx = j.to!int;
sy = i;
MEMO[i][j] = true;
}
MAP[i][j] = c;
}
auto ss = [[sx, sy]];
foreach (_; 0..K) {
int[][] nss;
foreach (s; ss) {
auto x = s[0];
auto y = s[1];
static foreach (d; [[1,0], [0,1], [-1,0], [0,-1]]) {{
auto xx = x+d[0];
auto yy = y+d[1];
if (0 <= xx && xx < W && 0 <= yy && yy < H && MAP[yy][xx] == '.' && !MEMO[yy][xx]) {
MEMO[yy][xx] = true;
nss ~= [xx, yy];
if (xx == 0 || xx == W-1 || yy == 0 || yy == H-1) {
writeln(1);
return;
}
}
}}
}
ss = nss;
}
auto r = int.max;
foreach (i; 0..H) foreach (j; 0..W) if (MEMO[i][j]) {
r = min(r, min((i-1+K)/K, (j-1+K)/K, (H-i-2+K)/K, (W-j-2+K)/K));
}
writeln(r+1);
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
void main()
{
auto x = lread();
if (x < 1200)
{
writeln("ABC");
return;
}
writeln("ARC");
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
|
D
|
import std.stdio;
import std.string;
import std.algorithm;
import std.array;
import std.ascii;
import std.conv;
// エンコード化
string decrypt(string from, uint alpha, uint beta) {
string chars;
foreach(c; from) {
chars ~= isAlpha(c) ? ((c - "a"[0]) * alpha + beta) % 26 + "a"[0] : c;
}
return chars;
}
void main() {
int times = readln.chomp.to!(int);
foreach(t; 0..times) {
string line = readln.chomp;
string[] keys = line.split(" ").filter!(x => x.length == 4).array;
foreach(a; [0, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25]) {
foreach(b; 0..26) {
foreach(k; keys) {
string res = decrypt(k, a, b);
if (res == "that" || res == "this") {
decrypt(line, a, b).writeln;
goto end;
}
}
}
}
end:;
}
}
|
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;
}
long calc(int n, int s) {
long rec(int p, int sum, int to, long[][][] memo) {
if (to > 0 && memo[p][to][sum] >= 0)
return memo[p][to][sum];
if (p == n) {
return sum == s ? 1 : 0;
}
long ans = 0;
for (int i = to + 1; i <= 100; i++) {
if (sum + i > s)
break;
ans += rec(p + 1, sum + i, i, memo);
}
if (to > 0)
memo[p][to][sum] = ans;
return ans;
}
auto memo = new long[][][](n + 1, 101, 1001);
for (int i = 0; i < n + 1; i++) {
for (int j = 0; j < 101; j++) {
for (int k = 0; k < 1001; k++)
memo[i][j][k] = -1L;
}
}
return rec(0, 0, -1, memo);
}
void main() {
string t;
while ((t = readln.chomp).length > 0) {
auto ns = t.split.map!(to!int).array;
int n = ns[0], s = ns[1];
if (n == 0 && s == 0)
break;
writeln(calc(n, s));
}
}
|
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, "p", long, "dist");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
long sx, sy, tx, ty;
scan(sx, sy, tx, ty);
foreach (_; iota(tx - sx))
write('R');
foreach (_; iota(ty - sy))
write('U');
foreach (_; iota(tx - sx))
write('L');
foreach (_; iota(ty - sy))
write('D');
write('D');
foreach (_; iota(tx - sx + 1))
write('R');
foreach (_; iota(ty - sy + 1))
write('U');
write('L');
write('U');
foreach (_; iota(tx - sx + 1))
write('L');
foreach (_; iota(ty - sy + 1))
write('D');
writeln('R');
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
module c;
import std.conv, std.stdio;
import std.algorithm, std.array, std.string, std.range;
void main()
{
auto buf = readln.chomp.split.to!(long[]);
auto n = buf[0], k = buf[1];
n = n % k;
n.min(k-n).writeln;
}
|
D
|
// import chie template :) {{{
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons;
// }}}
// nep.scanner {{{
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string : chomp;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.chomp.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
}
// }}}
void main()
{
auto cin = new Scanner;
string s;
cin.scan(s);
int ans;
foreach (c; s)
{
ans += c - '0';
}
writeln(max(ans, s[0] - '0' - 1 + 9.repeat.take(s.length - 1).sum));
}
|
D
|
// import chie template :) {{{
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons, std.format;
// }}}
// nep.scanner {{{
class Scanner {
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) {
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next() {
if (idx < str.length) {
return str[idx++];
}
char[] s;
while (s.length == 0) {
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(size_t len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
void scan()() {
}
void scan(T, S...)(ref T x, ref S args) {
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType)) {
str ~= s.to!(char[]).strip.split;
}
}
// }}}
void main() {
auto cin = new Scanner;
int N = cin.next!int;
long[] A = cin.nextArray!long(N);
long[long] chie;
foreach (i; A) {
chie[i]++;
}
long ret;
foreach (e; chie.byKeyValue) {
ret += e.value * (e.value - 1) / 2;
}
foreach (i; A) {
auto n = chie[i];
writeln(ret - n * (n - 1) / 2 + (n - 1) * (n - 2) / 2);
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
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 deque = Deque!int(400_000);
int q = readint;
for (int i = 0; i < q; i++) {
auto xs = readints;
switch (xs[0]) {
case 0:
if (xs[1] == 0) deque.insertFront(xs[2]);
else deque.insertBack(xs[2]);
break;
case 1:
writeln(deque[xs[1]]);
break;
case 2:
if (xs[1] == 0) deque.removeFront();
else deque.removeBack();
break;
default:
break;
}
}
}
struct Deque(T) {
private T[] _buffer;
private int _front;
private int _back;
this(int size) {
_buffer = new int[size * 3];
_front = _back = (size * 3) / 2;
}
bool empty() @property { return _front == _back; }
int length() @property { return _back - _front; }
void insertFront(T value) {
if (empty) {
_buffer[_front] = value;
_back++;
return;
}
_buffer[--_front] = value;
}
void insertBack(T value) {
if (empty) {
_buffer[_front] = value;
_back++;
return;
}
_buffer[_back++] = value;
}
void removeFront() {
assert(!empty);
++_front;
}
void removeBack() {
assert(!empty);
--_back;
}
T opIndex(int index) {
return _buffer[_front + index];
}
}
|
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;
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 M = RD;
auto s = new long[][](M);
foreach (i; 0..M)
{
auto tmp = RDR.ARR(-1L);
s[i] = tmp[1..$];
}
auto p = RDR.ARR;
long ans;
foreach (i; 0..2^^N)
{
bool f()
{
foreach (j; 0..M)
{
long cnt;
foreach (e; s[j])
{
auto bit = 1LU << e;
if (i & bit)
++cnt;
}
if (cnt % 2 != p[j]) return false;
}
return true;
}
if (f())
{
++ans;
}
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math;
void main()
{
auto N = readln.chomp.to!int;
auto AS = readln.split.to!(long[]);
long[] BS;
BS.length = N + 1;
foreach (i, a; AS) {
BS[i+1] = BS[i] + a;
}
long[long] memo;
foreach (b; BS) {
if (b in memo) {
++memo[b];
} else {
memo[b] = 1;
}
}
long r;
foreach (_, n; memo) {
r += n * (n-1) / 2;
}
writeln(r);
}
|
D
|
import std.stdio, std.array, std.string, std.conv;
void main() {
int n = to!int(chomp(readln()));
string[] a = split(readln());
for (int i = n - 1; i >= 0; --i) {
write(a[i]);
write(i ? " ": "\n");
}
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.array;
import std.conv;
import std.range;
void solve(int[] table) {
auto answer =
['A': [1, 11, 10],
'B': [10, 20, 30],
'C': [1, 2, 3],
'D': [10, 9, 19],
'E': [1, 11, 12],
'F': [10, 11, 21],
'G': [1, 10, 9],
];
foreach(i; 1..9) {
foreach(j; 1..9) {
int curr = j + i * 10;
if (table[curr] == 1) {
foreach(char c, list; answer) {
bool ans = true;
foreach(x; list) if ((x + curr) < table.length && table[x + curr] != 1) ans = false;
if (ans) {writeln(c); break;}
}
goto end;
}
}
}
end:;
}
void main() {
while(true) {
int[] table;
table ~= repeat(0).take(10).array;
foreach(i; 0..8) {
table ~= [0] ~ readln().chomp.array.map!((x) => x.to!(int) - '0').array ~ [0];
}
table ~= repeat(0).take(10).array;
solve(table);
readln();
if(stdin.eof()) break;
}
}
|
D
|
import std.stdio;import std.conv;import std.algorithm;import std.array;
void main()
{
auto it = readln().split().map!(to!int);
writeln(it[0]*it[1]," ",(it[0]+it[1])*2);
}
|
D
|
import std.stdio, std.conv, std.string, std.algorithm,
std.math, std.array, std.container, std.typecons;
void main() {
int[] abc = readln.chomp.split.to!(int[]);
if(abc[0]!=abc[1]&&abc[1]==abc[2] ||
abc[0]!=abc[1]&&abc[0]==abc[2] ||
abc[0]==abc[1]&&abc[1]!=abc[2]) writeln("Yes");
else writeln("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() {
int n;
scan(n);
auto s = readln.chomp.to!string;
auto pf = new int[](n + 1);
foreach (i ; 1 .. n + 1) {
pf[i] = pf[i-1] | (1 << (s[i-1] - 'a'));
}
auto sf = new int[](n + 1);
foreach_reverse (i ; 0 .. n) {
sf[i] = sf[i+1] | (1 << (s[i] - 'a'));
}
int ans;
foreach (i ; 0 .. n) {
ans = max(ans, popcnt(pf[i] & sf[i]));
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
// Your code here!
import std.stdio,std.conv,std.string,std.algorithm,std.array;
void main(){
auto s=readln().chomp().split().map!(to!int);
int a=s[0],b=s[1];
if(a>b)
writeln(a-1);
else
writeln(a);
}
|
D
|
import std.stdio;
import std.algorithm;
import std.math;
import std.conv;
import std.string;
T readNum(T)(){
return readStr.to!T;
}
T[] readNums(T)(){
return readStr.split.to!(T[]);
}
string readStr(){
return readln.chomp;
}
void main(){
auto abck = readNums!int;
int qty, ret;
if(abck[0] > 0 && qty != abck[3]){
ret += min(abck[3] - qty, abck[0]);
qty += min(abck[3] - qty, abck[0]);
}
if(abck[1] > 0 && qty != abck[3]){
qty += min(abck[3] - qty, abck[1]);
}
if(abck[2] > 0 && qty != abck[3]){
ret -= min(abck[3] - qty, abck[2]);
qty += min(abck[3] - qty, abck[2]);
}
writeln(ret);
}
|
D
|
import std.stdio, std.string, std.array, std.algorithm, std.conv, std.typecons, std.numeric, std.math;
void main()
{
auto n = readln.chomp.to!int;
auto DP = new long[](n+1);
DP[0] = 1;
DP[1] = 1;
foreach (i; 2..n+1) {
DP[i] = DP[i-1] + DP[i-2];
}
writeln(DP[n]);
}
|
D
|
//dlang template---{{{
import std.stdio;
import std.conv;
import std.string;
import std.array;
import std.algorithm;
import std.typecons;
import std.math;
import std.range;
// MIT-License https://github.com/kurokoji/nephele
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType))
{
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType))
{
str ~= s.to!(char[]).strip.split;
}
}
//Digit count---{{{
int DigitNum(int num) {
int digit = 0;
while (num != 0) {
num /= 10;
digit++;
}
return digit;
}
//}}}
//}}}
void main() {
Scanner sc = new Scanner;
string S;
string trueStr = "keyence";
sc.scan(S);
foreach (i; 0 .. trueStr.length) {
if (S[0 .. i] ~ S[$ - (trueStr.length - i) .. $] == trueStr) {
writeln("YES");
return;
}
}
writeln("NO");
}
|
D
|
import std.functional,
std.algorithm,
std.typecons,
std.bigint,
std.string,
std.traits,
std.array,
std.range,
std.stdio,
std.conv,
std.math;
T read_num(T)() { return readln.chomp.to!T; }
T min(T)(T a, T b) { return a < b ? a : b; }
void main() {
long N = read_num!long;
long A = read_num!long;
long B = read_num!long;
long C = read_num!long;
long D = read_num!long;
long E = read_num!long;
long x = min(A, min(B, min(C, min(D, E))));
long ans = 4 + N / x;
ans += !!(N % x);
writeln(ans);
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dunkelheit" version="1.0.1"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
int main() {
Scanner sc = new Scanner(stdin);
scope(exit) assert(!sc.hasNext);
int n, k;
sc.read(n, k);
writeln(n - k + 1);
return 0;
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/foundation.d */
// module dkh.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
private File f;
this(File f) {
this.f = f;
}
private char[512] lineBuf;
private 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) {
assert(succW());
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
void read(Args...)(auto ref Args args) {
import std.exception : enforce;
static if (args.length != 0) {
enforce(readSingle(args[0]));
read(args[1..$]);
}
}
bool hasNext() {
return succ();
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
|
D
|
import std.stdio, std.math, std.algorithm, std.array, std.string, std.conv, std.container, std.range;
T Read(T)() { return readln.chomp.to!(T); }
T[] Reads(T)() { return readln.split.to!(T[]); }
alias read = Read!(int);
alias reads = Reads!(int);
void main() {
int n = read();
int[] h = reads();
auto dp = new int[n];
dp[0] = 0; dp[1] = abs(h[1] - h[0]);
foreach (i; 2..n)
dp[i] = min(abs(h[i] - h[i-1]) + dp[i-1]
, abs(h[i] - h[i-2]) + dp[i-2]);
writeln(dp[n-1]);
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
static import std.ascii;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
auto H = aryread();
long ans;
long i;
while (i < N)
{
if (H[i] == 0)
{
i++;
continue;
}
long j = i;
while (j < N && H[j] != 0)
j++;
long c = H[i .. j].reduce!min();
ans += c;
H[i .. j][] -= c;
}
writeln(ans);
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
long a, b, x; readV(a, b, x);
writeln(b/x+1 - (a == 0 ? 0 : (a-1)/x+1));
}
|
D
|
void main()
{
int n = readln.chomp.to!int;
int t, x, y;
bool ok = true;
foreach (i; 0 .. n)
{
int[] tmp = readln.split.to!(int[]);
int ti = tmp[0], xi = tmp[1], yi = tmp[2];
int len = abs(xi - x) + abs(yi - y);
if (ti - t < len)
{
ok = false;
}
else if ((ti - t) % 2 != len % 2)
{
ok = false;
}
t = ti, x = xi, y = yi;
}
writeln(ok ? "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
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto k = readln.chomp.to!long, n = 50;
auto kd = k/n, km = k%n;
auto a = new long[](n);
a[] = n-1+kd;
foreach (i; 0..km) {
a[] -= 1;
a[i] += n+1;
}
writeln(n);
foreach (i, ai; a)
write(ai, i < n-1 ? " " : "");
writeln;
}
|
D
|
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container;
import std.numeric, std.math;
import core.bitop;
string my_readln() { return chomp(readln()); }
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 tokens = my_readln.split;
auto N = tokens[0].to!long;
auto a = my_readln.split.to!(long[]);
long ans;
foreach (e; a)
{
while (e % 2 == 0)
{
++ans;
e /= 2;
}
}
writeln(ans);
stdout.flush();
}
|
D
|
import std.stdio,
std.string,
std.conv,
std.algorithm;
long abs(long n) {
if (n < 0) {
return -n;
}
return n;
}
void main() {
int N = readln.chomp.to!(int);
long[] A = readln.chomp.split.to!(long[]);
if (N == 2) {
writeln(abs(A[1]) * 2);
writeln(abs(A[0]) * 2);
return;
}
long ans;
long a;
for (int i = 1; i < N; i++) {
ans += abs(a - A[i]);
a = A[i];
}
ans += abs(a);
writeln(ans);
for (int i = 1; i < N - 1; i++) {
// ここで前との差分をとって表示したい
a = 0;
if (i - 2 >= 0) {
a = A[i - 2];
}
ans -= abs(a - A[i]) + abs(A[i] - A[i + 1]);
ans += abs(a - A[i - 1]) + abs(A[i - 1] - A[i + 1]);
writeln(ans);
}
ans -= abs(A[N - 3] - A[N - 1]) + abs(A[N - 1]);
ans += abs(A[N - 3] - A[N - 2]) + abs(A[N - 2]);
writeln(ans);
}
|
D
|
import std.stdio;
import std.conv;
import std.algorithm;
import std.range;
import std.string;
import std.math;
void main() {
readln;
auto s = readln.chomp.split.map!(to!int);
readln;
auto t = readln.chomp.split.map!(to!int);
int cnt = 0;
foreach (e; t) {
if (!s.find(e).empty) cnt++;
}
cnt.writeln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void 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 main()
{
auto N = RD;
auto P = RDA;
auto Q = RDA;
bool[] set1, set2;
foreach (i; 0..N)
{
set1 ~= true;
set2 ~= true;
}
long a = 1;
long b = 1;
foreach (i; 0..N)
{
long cnt = 1;
foreach (j; 1..N-i)
{
cnt *= j;
}
long x, y;
foreach (j; 0..P[i])
{
if (set1[j])
++x;
}
foreach (j; 0..Q[i])
{
if (set2[j])
++y;
}
set1[P[i]-1] = false;
set2[Q[i]-1] = false;
a += (x-1) * cnt;
b += (y-1) * cnt;
}
debug writeln(a, ":", b);
writeln(abs(a-b));
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto X = readln.chomp.to!int;
auto ns = new bool[](100004);
foreach (i; 2..100004) {
if (ns[i]) continue;
if (i >= X) {
writeln(i);
return;
}
auto j = i*2;
while (j < 100004) {
ns[j] = true;
j += i;
}
}
}
|
D
|
import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto s = readln.strip;
auto balance = abs (s.count ('0').to !(int) -
s.count ('1').to !(int));
auto moves = (s.length - balance) / 2;
writeln ((moves & 1) ? "DA" : "NET");
}
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
const tmp = readln.split.to!(int[]);
writeln(tmp[0] + tmp[1]/2);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.stdio;
import std.string;
import std.range;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() {
auto s = readln.chomp;
auto t = readln.chomp;
if ((s ~ s).indexOf(t) != -1)
writeln("Yes");
else
writeln("No");
}
|
D
|
module app;
import core.bitop;
import std.algorithm;
import std.array;
import std.bigint;
import std.conv;
import std.stdio;
import std.string;
void main()
{
Input input = void;
parseInput(input, stdin);
auto result = main2(&input);
writeln(result);
}
auto main2(Input* input)
{
foreach (i, c; input.s)
{
if (i & 1)
{
if (c == 'L' || c == 'U' || c == 'D')
{
}
else
return "No";
}
else
{
if (c == 'R' || c == 'U' || c == 'D')
{
}
else
return "No";
}
}
return "Yes";
}
unittest // example1
{
string example =
`RUDLUDR`;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
assert(result == "Yes");
}
unittest // example2
{
string example =
`UUUUUUUUUUUUUUU`;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
assert(result == "Yes");
}
unittest // example3
{
string example =
`DULL`;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
assert(result == "No");
}
struct Input
{
string s;
}
void parseInput(T)(out Input input, T file)
{
with (file) with (input)
{
s = readln().strip();
}
}
void parseExample(out Input input, string example)
{
struct Adapter
{
import std.string;
string[] _lines;
this(string input) { _lines = input.splitLines(); }
string readln()
{
auto line = _lines[0];
_lines = _lines[1..$];
return line;
}
}
Adapter a = Adapter(example);
parseInput(input, a);
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
int l = read.to!int;
long infty = 1L<<32;
long[] depths = [1];
long ans;
foreach(_; 0 .. l){
string cmd = read;
if(cmd == "for"){
long val = read.to!long;
long d = depths[$ - 1] * val;
if(d >= infty) d = infty;
depths ~= d;
}
else if(cmd == "add"){
long d = depths[$ - 1];
ans += d;
if(ans >= infty){
"OVERFLOW!!!".writeln;
return;
}
}
else if(cmd == "end"){
depths.length -= 1;
}
}
ans.writeln;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons;
import std.math, std.numeric;
immutable long mod = 10L^^9 + 7;
void main() {
int n; scan(n);
auto fact = new long[](n + 1);
auto rfact = new long[](n + 1);
fact[0] = 1;
foreach (i ; 1 .. n + 1) {
fact[i] = (fact[i-1] * i) % mod;
}
rfact[n] = powmod(fact[n], mod - 2, mod);
foreach_reverse (i ; 0 .. n) {
rfact[i] = (rfact[i+1] * (i+1)) % mod;
}
debug {
writeln(fact);
writeln(rfact);
}
long binom(int n, int k) {
return fact[n] * rfact[k] % mod * rfact[n-k] % mod;
}
long ans;
long pre;
foreach (k ; 0 .. n) {
int b = n - 2 - (k - 1);
if (b < 0 || b > k - 1) continue;
long v = binom(k - 1, b) * fact[k] % mod * fact[n - 1 - k] % mod;
debug {
writeln(k, " ", v);
}
ans += k * (v - pre + mod) % mod;
ans %= mod;
pre = v;
}
writeln(ans);
}
long powmod(long x, long y, long mod) {
return y > 0 ? powmod(x, y>>1, mod)^^2 % mod * x^^(y&1) % mod : 1L;
}
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
|
// 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
void main()
{
long H, W;
scan(H, W);
auto S = new string[](H);
foreach (i; 0 .. H)
S[i] = sread();
foreach (h; 0 .. H)
foreach (w; 0 .. W)
if (S[h][w] == '#')
{
if (0 < h && S[h - 1][w] == '#')
continue;
if (h + 1 < H && S[h + 1][w] == '#')
continue;
if (0 < w && S[h][w - 1] == '#')
continue;
if (w + 1 < W && S[h][w + 1] == '#')
continue;
writeln("No");
return;
}
writeln("Yes");
}
|
D
|
import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm, std.array, std.range, std.container;
void main() {
auto N = readln.split[0].to!ulong;
ulong[] p;
N.iota.each!(x => p ~= readln.split[0].to!ulong);
auto sum = p.sum, max_price = p.minPos!("a > b")[0];
(p.sum - max_price/2).writeln;
}
|
D
|
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
enum E = 26;
void main() {
/*
debug {
enum LIM = 8;
auto dp = new bool[][LIM + 1];
dp[1] = [true, false];
foreach (n; 2 .. LIM + 1) {
dp[n] = new bool[1 << n];
foreach (m; 1 .. n) {
foreach (p; 0 .. 1 << m) foreach (q; 0 .. 1 << (n - m)) {
if (dp[m][p] && dp[n - m][q]) {
dp[n][(((1 << m) - 1) ^ p) | (q << m)] = true;
}
}
}
foreach (p; 0 .. 1 << n) {
if (dp[n][p]) {
foreach (i; 0 .. n) {
write((p >> i) & 1);
}
writeln;
}
}
assert(dp[n].count(true) == 1 << (n - 2));
}
}
//*/
try {
for (; ; ) {
const N = readInt();
const T = readLong();
const S = readToken();
auto freq = new long[E];
foreach (i; 0 .. N - 2) {
++freq[S[i] - 'a'];
}
long tar = T;
tar -= -(1L << (S[N - 2] - 'a'));
tar -= +(1L << (S[N - 1] - 'a'));
foreach (e; 0 .. E) {
tar -= freq[e] * (1L << e);
}
long mn = tar, mx = tar;
foreach (e; 0 .. E) {
if (mn & 1) ++mn;
if (mx & 1) --mx;
if (mn > mx) {
break;
}
mn /= 2;
mx /= 2;
mx += freq[e];
debug {
writeln(e, ": ", mn, " ", mx);
}
}
writeln((mn <= 0 && 0 <= mx) ? "Yes" : "No");
}
} catch (EOFException e) {
}
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;
void main() {
auto N = readln.chomp.to!int;
auto G = new Tuple!(int, int)[][](N);
foreach (i; 0..N-1) {
auto s = readln.split.map!(to!int);
G[s[0]-1] ~= tuple(s[1]-1, i);
G[s[1]-1] ~= tuple(s[0]-1, i);
}
auto ans = new int[](N-1);
void dfs(int n, int p, int pc) {
int c = 0;
foreach (e; G[n]) {
int m = e[0];
int idx = e[1];
if (m == p) continue;
if (c == pc) c += 1;
ans[idx] = c + 1;
dfs(m, n, c);
c += 1;
}
}
dfs(0, -1, -1);
G.map!(g => g.length).reduce!max.writeln;
ans.each!writeln;
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;
immutable long MOD = 998244353;
void main() {
auto N = readln.chomp.to!int;
auto S = N.iota.map!(_ => readln.chomp).array;
int ans = 0;
int[] dr = [0, -1, -1, 1, 1];
int[] dc = [0, -1, 1, -1, 1];
foreach (r; 1..N-1) {
foreach (c; 1..N-1) {
int ok = 1;
foreach (k; 0..5) {
int nr = r + dr[k];
int nc = c + dc[k];
if (S[nr][nc] == '.') {
ok = 0;
break;
}
}
ans += ok;
}
}
ans.writeln;
}
|
D
|
import std.stdio, std.string, std.conv, std.array, std.algorithm;
void main() {
auto a = readln.split.map!(to!int);
if (a[4] <= a[2] && a[2] <= a[0] - a[4] && a[4] <= a[3] && a[3] <= a[1] - a[4]) writeln("Yes");
else
writeln("No");
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
auto tmp = readln.split.to!(int[]);
auto M = tmp[0], N = tmp[1], X = tmp[2];
auto a = readln.split.to!(int[]);
auto left = a.filter!(x => x < X).walkLength;
auto right = a.filter!(x => x > X).walkLength;
writeln(min(left, right));
}
|
D
|
import std.stdio, std.string, std.algorithm, std.range;
void main() {
auto abc = readln.chomp.split.sort.uniq.array;
abc.length.writeln;
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto n = readln.chomp;
if (((n[0] == n[1]) && (n[1] == n[2])) || ((n[1] == n[2]) && (n[2] == n[3]))) {
writeln("Yes");
} else {
writeln("No");
}
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.