code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std;
auto input()
{
return readln().chomp();
}
alias sread = () => readln.chomp();
alias aryread(T = long) = () => readln.split.to!(T[]);
void main()
{
long n;
scan(n);
auto T = new long[](n);
foreach (i; 0 .. n)
{
long t;
scan(t);
T[i] = t;
}
// writeln(T);
long ans_gcd;
ans_gcd = T[0];
foreach (i; 0 .. n)
{
ans_gcd = gcd(ans_gcd, T[i]);
}
// writeln(ans_gcd);
long ans_lcm;
ans_lcm = T[0];
foreach (i; 0 .. n)
{
ans_lcm = ans_lcm * (T[i] / gcd(ans_lcm, T[i]));
}
writeln(ans_lcm);
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
auto binary_search(long key, long[] l)
{
long left = 0; //lの左端
long right = l.length - 1; //lの右端
while (right >= left)
{
long mid = left + (right - left) / 2; //区間の真ん中
if (l[mid] == key)
{
return mid;
}
else if (l[mid] > key)
{
right = mid - 1;
}
else if (l[mid] < key)
{
left = mid + 1;
}
}
return -1;
}
// auto a = [1, 14, 32, 51, 51, 51, 243, 419, 750, 910];
// auto a = aryread();
// writeln(a);
// long len_a = a.length;
// writeln(len_a);
// long ans;
// ans = binary_search(51, a);
// writeln(ans);
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
void main()
{
auto n = lread();
writeln(800 * n - (n / 15) * 200);
}
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.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;
import std.random;
void main() {
auto x = readln.chomp.split.map!(to!int);
if (x[0] < x[1] && x[1] < x[2]) writeln("Yes");
else writeln("No");
}
|
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;
}
}
// }}}
// ModInt {{{
struct ModInt(ulong modulus) {
import std.traits : isIntegral, isBoolean;
import std.exception : enforce;
private {
ulong val;
}
this(const ulong n) {
val = n % modulus;
}
this(const ModInt n) {
val = n.value;
}
@property {
ref inout(ulong) value() inout {
return val;
}
}
T opCast(T)() {
static if (isIntegral!T) {
return cast(T)(val);
} else if (isBoolean!T) {
return val != 0;
} else {
enforce(false, "cannot cast from " ~ this.stringof ~ " to " ~ T.stringof ~ ".");
}
}
ModInt opAssign(const ulong n) {
val = n % modulus;
return this;
}
ModInt opOpAssign(string op)(ModInt rhs) {
static if (op == "+") {
val += rhs.value;
if (val >= modulus) {
val -= modulus;
}
} else if (op == "-") {
if (val < rhs.value) {
val += modulus;
}
val -= rhs.value;
} else if (op == "*") {
val = val * rhs.value % modulus;
} else if (op == "/") {
this *= rhs.inv;
} else if (op == "^^") {
ModInt res = 1;
ModInt t = this;
while (rhs.value > 0) {
if (rhs.value % 2 != 0) {
res *= t;
}
t *= t;
rhs /= 2;
}
this = res;
} else {
enforce(false, op ~ "= is not implemented.");
}
return this;
}
ModInt opOpAssign(string op)(ulong rhs) {
static if (op == "+") {
val += rhs;
if (val >= modulus) {
val -= modulus;
}
} else if (op == "-") {
if (val < rhs) {
val += modulus;
}
val -= rhs;
} else if (op == "*") {
val = val * rhs % modulus;
} else if (op == "/") {
this *= ModInt(rhs).inv;
} else if (op == "^^") {
ModInt res = 1;
ModInt t = this;
while (rhs > 0) {
if (rhs % 2 != 0) {
res *= t;
}
t *= t;
rhs /= 2;
}
this = res;
} else {
enforce(false, op ~ "= is not implemented.");
}
return this;
}
ModInt opUnary(string op)() {
static if (op == "++") {
this += 1;
} else if (op == "--") {
this -= 1;
} else {
enforce(false, op ~ " is not implemented.");
}
return this;
}
ModInt opBinary(string op)(const ulong rhs) const {
mixin("return ModInt(this) " ~ op ~ "= rhs;");
}
ModInt opBinary(string op)(ref const ModInt rhs) const {
mixin("return ModInt(this) " ~ op ~ "= rhs;");
}
ModInt opBinary(string op)(const ModInt rhs) const {
mixin("return ModInt(this) " ~ op ~ "= rhs;");
}
ModInt opBinaryRight(string op)(const ulong lhs) const {
mixin("return ModInt(this) " ~ op ~ "= lhs;");
}
long opCmp(ref const ModInt rhs) const {
return cast(long)value - cast(long)rhs.value;
}
bool opEquals(const ulong rhs) const {
return value == ModInt(rhs).value;
}
ModInt inv() const {
ModInt ret = this;
ret ^^= modulus - 2;
return ret;
}
string toString() const {
import std.format : format;
return format("%s", val);
}
}
// }}}
// alias {{{
alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less);
alias MinHeap(T) = Heap!(T, "a > b");
// }}}
// memo {{{
/*
- ある値が見つかるかどうか
<https://dlang.org/phobos/std_algorithm_searching.html#canFind>
canFind(r, value); -> bool
- 条件に一致するやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#filter>
// 2で割り切れるやつ
filter!"a % 2 == 0"(r); -> Range
- 合計
<https://dlang.org/phobos/std_algorithm_iteration.html#sum>
sum(r);
- 累積和
<https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold>
// 今の要素に前の要素を足すタイプの一般的な累積和
// 累積和のrangeが帰ってくる(破壊的変更は行われない)
cumulativeFold!"a + b"(r, 0); -> Range
- rangeをarrayにしたいとき
array(r); -> Array
- 各要素に同じ処理をする
<https://dlang.org/phobos/std_algorithm_iteration.html#map>
// 各要素を2乗
map!"a * a"(r) -> Range
- ユニークなやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#uniq>
uniq(r) -> Range
- 順列を列挙する
<https://dlang.org/phobos/std_algorithm_iteration.html#permutations>
permutation(r) -> Range
- ある値で埋める
<https://dlang.org/phobos/std_algorithm_mutation.html#fill>
fill(r, val); -> void
- バイナリヒープ
<https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap>
// 昇順にするならこう(デフォは降順)
BinaryHeap!(Array!T, "a > b") heap;
heap.insert(val);
heap.front;
heap.removeFront();
- 浮動小数点の少数部の桁数設定
// 12桁
writefln("%.12f", val);
- 浮動小数点の誤差を考慮した比較
<https://dlang.org/phobos/std_math.html#.approxEqual>
approxEqual(1.0, 1.0099); -> true
- 小数点切り上げ
<https://dlang.org/phobos/std_math.html#.ceil>
ceil(123.4); -> 124
- 小数点切り捨て
<https://dlang.org/phobos/std_math.html#.floor>
floor(123.4) -> 123
- 小数点四捨五入
<https://dlang.org/phobos/std_math.html#.round>
round(4.5) -> 5
round(5.4) -> 5
*/
// }}}
void main() {
auto cin = new Scanner;
int n, m;
cin.scan(n, m);
writeln((n - 1) * (m - 1));
}
|
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
??2??5
---+/
/+---test
?44
---+/
/+---test
7?4
---+/
/+---test
?6?42???8??2??06243????9??3???7258??5??7???????774????4?1??17???9?5?70???76???
---+/
void main(string[] args) {
const S = readln.chomp;
auto dp = new long[13][S.length+1];
dp[0][0] = 1;
foreach (i; 1..S.length+1) {
const long x = S[i-1] == '?'? -1: S[i-1]-'0';
foreach (j; 0..10) {
if (x != -1 && x != j) continue;
foreach (ki; 0..13) {
dp[i][(ki*10 + j)%13] += dp[i-1][ki];
}
}
foreach (j; 0..13) dp[i][j] %= 1000000007;
}
dp[$-1][5].writeln;
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.conv;
import std.array;
void main() {
auto Q = readln.chomp.to!int;
foreach (i; 0..Q) {
auto input = readln.split.map!(to!int).array;
//??????CAN?????°????±??????????CAN?????°??????min(C, A, N)????????????
int can = min(input[0], input[1], input[2]);
input[] -= can;
//?¬????CCA?????°????±??????????CCA?????°??????
int cca;
if (input[0] > input[1] * 2) cca = input[1];
else cca = input[0] / 2;
input[0] -= cca * 2;
int ccc = input[0] / 3;
writeln(can + cca + ccc);
}
}
|
D
|
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math, std.container, std.typecons;
import core.stdc.stdio;
// foreach, foreach_reverse, writeln
void main() {
int n;
scanf("%d", &n);
int[] X = new int[n];
int[] Y = new int[n];
long INF = 1L<<40;
int parity = 0;
foreach (i; 0..n) {
scanf("%d%d", &X[i], &Y[i]);
int p = (X[i]+Y[i]+INF+1)%2;
if (i == 0) parity = p;
else if (parity != p) {
writeln(-1);
return;
}
}
int m = 32;
writeln(m+parity);
foreach_reverse (i; 0..m) {
writeln(1L<<i);
}
if (parity) {
writeln(1);
}
foreach (_i; 0..n) {
long x = X[_i], y = Y[_i];
if (parity) x++;
string ans;
foreach_reverse (i; 0..m) {
long l = 1L<<i;
if (abs(x) < abs(y)) {
if (y < 0) {
ans ~= 'D';
y += l;
} else {
ans ~= 'U';
y -= l;
}
} else {
if (x < 0) {
ans ~= 'L';
x += l;
} else {
ans ~= 'R';
x -= l;
}
}
}
if (parity) ans ~= 'L';
writeln(ans);
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
void main()
{
auto abc = readln.chomp.split.to!(int[]);
if(abc[1] == abc[2])
abc[0].writeln;
else if(abc[0] == abc[2])
abc[1].writeln;
else
abc[2].writeln;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons;
import std.math, std.numeric;
void main() {
int n, q; scan(n, q);
auto uf = UnionFind(n);
foreach (_ ; 0 .. q) {
int com, x, y; scan(com, x, y);
if (com == 0) {
uf.merge(x, y);
}
else {
writeln(uf.same(x, y) ? 1 : 0);
}
}
}
struct UnionFind {
private {
int _size;
int[] _parent;
}
this(int N)
in {
assert(N > 0);
}
body {
_size = N;
_parent = new int[](_size);
foreach (i ; 0 .. _size) {
_parent[i] = i;
}
}
int findRoot(int x)
in {
assert(0 <= x && x < _size);
}
body {
if (_parent[x] != x) {
_parent[x] = findRoot(_parent[x]);
}
return _parent[x];
}
bool same(int x, int y)
in {
assert(0 <= x && x < _size);
assert(0 <= y && y < _size);
}
body {
return findRoot(x) == findRoot(y);
}
void merge(int x, int y)
in {
assert(0 <= x && x < _size);
assert(0 <= y && y < _size);
}
body {
int u = findRoot(x);
int v = findRoot(y);
if (u == v) return;
_parent[v] = u;
}
}
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.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;
import std.datetime;
void main()
{
while (1) {
auto n = readln.chomp.to!int;
if (!n) break;
auto time = new int[](60 * 60 * 24);
foreach (_; 0..n) {
auto x = readln.chomp.split;
auto s = x[0].split(":").map!(to!int);
auto a = s[0]*60*60+s[1]*60+s[2];
auto e = x[1].split(":").map!(to!int);
auto b = e[0]*60*60+e[1]*60+e[2];
time[a..b] += 1;
}
time.reduce!(max).writeln;
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto s = RD!(char[]);
long ans;
long streak;
foreach (i; 0..s.length-1)
{
if (s[i] == 'A')
{
++streak;
}
else if (s[i] == 'B')
{
if (s[i+1] == 'C')
{
ans += streak;
s[i+1] = 'A';
--streak;
}
else
{
streak = 0;
}
}
else
{
streak = 0;
}
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio;
import std.algorithm;
import std.math;
import std.conv;
import std.string;
int readInt(){
return readln.chomp.to!int;
}
int[] readInts(){
return readln.chomp.split.to!(int[]);
}
void main(){
int[] nk = readInts();
string s = readln.chomp;
writeln(s[0 .. nk[1]-1], s[nk[1]-1].toLower, s[nk[1] .. $]);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nk = readln.split.to!(int[]);
auto N = nk[0];
auto K = nk[1];
writeln((N+1) / 2 >= K ? "YES" : "NO");
}
|
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() {
long T1, T2;
long A1, A2;
long B1, B2;
scan(T1, T2);
scan(A1, A2);
scan(B1, B2);
if (T1*A1 + T2*A2 < T1*B1 + T2*B2) {
swap(A1, B1);
swap(A2, B2);
}
// T1*A1 + T2*A2 >= T1*B1 + T2*B2
auto d = T1*A1 + T2*A2 - (T1*B1 + T2*B2);
auto v = T1*B1 - T1*A1;
if (d == 0) {
writeln("infinity");
return;
}
if (v < 0) {
writeln(0);
return;
}
auto ans = 2 * (v / d + 1) - 1;
if (v % d == 0) ans--;
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, std.string, std.conv;
import std.algorithm;
void main()
{
auto buf = readln.chomp.split;
immutable n = buf[0].to!size_t;
auto target = buf[1..$].to!(int[]);
int[] current;
foreach (i; 0..n)
current ~= readln.chomp.to!int;
solve(n, target, current).writeln;
}
int solve(in size_t n, in int[] target, in int[] current)
{
auto ret = int.max;
outer:
foreach (p; 0..1<<(n*2))
{
auto sums = new int[3], counts = new int[3];
auto _p = p;
foreach (i; 0..n)
{
auto r = _p & 3;
_p >>= 2;
if (r < 3)
{
sums[r] += current[i];
counts[r] += 1;
}
}
int cs;
foreach (r; 0..3)
{
if (!sums[r])
continue outer;
cs += (counts[r] - 1) * 10;
if (sums[r] > target[r])
cs += sums[r] - target[r];
else
cs += target[r] - sums[r];
}
ret = ret.min(cs);
}
return ret;
}
|
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 long MAX = 2*10^^5+1;
immutable long MOD = 10^^9+7;
auto modinv = new long[](MAX);
modinv[0] = modinv[1] = 1;
foreach(i; 2..MAX) {
modinv[i.to!int] = modinv[MOD % i.to!int] * (MOD - MOD / i) % MOD;
}
auto f_mod = new long[](MAX);
auto f_modinv = new long[](MAX);
f_mod[0] = f_mod[1] = 1;
f_modinv[0] = f_modinv[1] = 1;
foreach(i; 2..MAX) {
f_mod[i.to!int] = (i * f_mod[i.to!int-1]) % MOD;
f_modinv[i.to!int] = (modinv[i.to!int] * f_modinv[i.to!int-1]) % MOD;
}
long nck(int n, int k) {
if (n < k) return 0;
if (k == 0) return 1;
return f_mod[n] * f_modinv[n-k] % MOD * f_modinv[k] % MOD;
}
auto N = readln.chomp.to!int;
auto A = readln.split.map!(to!int).array;
int l, r;
auto B = new int[](N+1);
B.fill(-1);
foreach (i; 0..N+1) {
if (B[A[i]] != -1) {
l = B[A[i]];
r = i;
break;
} else {
B[A[i]] = i;
}
}
foreach (i; 1..N+2) {
long a = nck(N+1, i);
long b = nck(l + N - r, i - 1);
writeln(((a - b) % MOD + MOD) % MOD);
}
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
for (;;) {
auto rd = readln.split.map!(to!int), h = rd[0], w = rd[1];
if (h == 0 && w == 0) break;
foreach (i; 0..h) {
foreach (j; 0..w)
write((i + j) % 2 == 0 ? '#' : '.');
writeln;
}
writeln;
}
}
|
D
|
// Vicfred
// https://atcoder.jp/contests/abc152/tasks/abc152_d
// math, combinatorics
import std.conv;
import std.stdio;
import std.string;
void main() {
int n = readln.chomp.to!int;
long[10][10] digits;
// count the numbers starting in i and ending in j
for(int i = 0; i <= n; i++)
digits[i.to!string[0] - '0'][i.to!string[$-1] - '0'] += 1L;
long answer = 0L;
for(int i = 1; i <= 9; ++i)
for(int j = 0; j <= 9; ++j)
answer += digits[i][j]*digits[j][i];
answer.writeln;
}
|
D
|
void main() {
auto N = ri;
auto s = rs, t = rs;
if(s==t) writeln(N);
else {
auto tmp = s;
foreach_reverse(i; 0..N+1) {
tmp = s ~ t[i..$];
if(tmp.endsWith(t)) {
writeln(tmp.length);
break;
}
}
}
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.conv;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dunkelheit" version=">=0.9.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
int main() {
Scanner sc = new Scanner(stdin);
scope(exit) sc.read!true;
int n, a, b;
sc.read(n, a, b);
import std.math;
if (abs(a-b) % 2 == 0) {
writeln("Alice");
} else {
writeln("Borys");
}
return 0;
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */
// module dkh.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
void read(bool enforceEOF = false, T, Args...)(ref T x, auto ref Args args) {
import std.exception;
enforce(readSingle(x));
static if (args.length == 0) {
enforce(enforceEOF == false || !succ());
} else {
read!enforceEOF(args);
}
}
void read(bool enforceEOF = false, Args...)(auto ref Args args) {
import std.exception;
static if (args.length == 0) {
enforce(enforceEOF == false || !succ());
} else {
enforce(readSingle(args[0]));
read!enforceEOF(args);
}
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
|
D
|
import std.stdio, std.conv, std.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 k = read.to!int;
int x = read.to!int;
int[] ans;
foreach(i; x - k + 1 .. x + k) ans ~= i;
ans.map!(to!string).join(" ").writeln;
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "next", long, "cost");
alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less);
void main()
{
auto n = lread();
long ok = 10001, ng = -1;
while (abs(ok - ng) > 1)
{
auto mid = (ok + ng) / 2;
if (mid * 1000 >= n)
ok = mid;
else
ng = mid;
}
writeln(1000 * ok - n);
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int N, p;
scan(N, p);
string s;
scan(s);
auto a = s.map!(ch => ch - '0').array;
long ans;
if (p == 2) {
foreach (i ; 0 .. N) {
if (a[i] % 2 == 0) {
ans += i + 1;
}
}
}
else if (p == 5) {
foreach (i ; 0 .. N) {
if (a[i] == 0 || a[i] == 5) {
ans += i + 1;
}
}
}
else {
auto r = new int[](N + 1);
foreach (i ; 1 .. N + 1) {
r[i] = r[i - 1] * 10 % p + a[i - 1] % p;
r[i] %= p;
}
auto rt = powmod(10, p - 2, p);
foreach (i ; 0 .. N + 1) {
r[i] = r[i] * powmod(rt, i, p) % p;
}
auto cnt = new long[](p);
foreach (i ; 0 .. N + 1) {
ans += cnt[r[i]];
cnt[r[i]]++;
debug {
writeln(cnt);
}
}
}
writeln(ans);
}
long powmod(long x, long y, long p) {
return y > 0 ? powmod(x, y >> 1, p)^^2 % p * x^^(y & 1) % p : 1L;
}
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;
}
|
D
|
import std.stdio;
import std.conv;
import std.array;
import std.string;
void main()
{
while (1) {
string[] input = split(readln());
int h = to!(int)(input[0]);
int w = to!(int)(input[1]);
if (h == 0 && w == 0) break;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if ((i + j) % 2 == 0) {
write("#");
} else {
write(".");
}
}
write("\n");
}
write("\n");
}
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import core.bitop;
void main ()
{
int n;
while (scanf (" %d", &n) > 0)
{
int m = (n + 31) / 32;
auto a1 = new uint [] [] (n, m);
auto a2 = new uint [] [] (n, m);
foreach (i; 0..n)
{
foreach (j; 0..n)
{
int c;
scanf (" %d", &c);
if (c)
{
a1[i][j >> 5] |= 1u << (j & 31);
a2[j][i >> 5] |= 1u << (i & 31);
}
}
}
auto b = new bool [n];
uint [] [] a;
void recur (int v)
{
b[v] = true;
foreach (u; 0..n)
{
if (!b[u] && (a[v][u >> 5] & (1u << (u & 31))))
{
recur (u);
}
}
}
bool ok = true;
b[] = false;
a = a1;
recur (0);
ok &= all !("a == true") (b[]);
b[] = false;
a = a2;
recur (0);
ok &= all !("a == true") (b[]);
writeln (ok ? "YES" : "NO");
}
}
|
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;
void main() {
long[] ans = new long[31];
ans[0] = 1;
foreach(i; 1..31) {
ans[i] += ans[i-1];
if (i>1) ans[i] += ans[i-2];
if (i>2) ans[i] += ans[i-3];
}
while(true) {
int n = readln.chomp.to!int;
if (n==0) break;
writeln(((ans[n]+1)/10+1)/365+1);
}
}
|
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.chomp.to!int;
auto as = readln.split.to!(int[]);
auto xs = new int[](5001);
auto ls = new int[](5001);
foreach (i; 1..N+1) {
auto a = as[i-1];
if (xs[a]) {
if (xs[a] > 1) goto ok;
if (ls[a] != i-1) goto ok;
}
++xs[a];
ls[a] = i;
}
writeln("NO");
continue;
ok:
writeln("YES");
}
}
|
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 inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n, q;
scan(n, q);
auto s = readln.chomp;
auto c = new int[](n + 1);
foreach (i ; 2 .. n + 1) {
debug {writeln(s[i - 2 .. i]);}
c[i] = s[i - 2 .. i] == "AC";
}
foreach (i ; 1 .. n + 1) {
c[i] += c[i - 1];
}
debug {
writeln(c);
}
foreach (i ; 0 .. q) {
int li, ri;
scan(li, ri);
int ans = c[ri] - c[li];
writeln(ans);
}
}
int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new int[][](n, 0);
foreach (i; 0 .. m) {
int u, v;
scan(u, v);
if (is1indexed) {
u--, v--;
}
adj[u] ~= v;
if (isUndirected) {
adj[v] ~= u;
}
}
return adj;
}
alias Edge = Tuple!(int, "to", int, "cost");
Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new Edge[][](n, 0);
foreach (i; 0 .. m) {
int u, v, c;
scan(u, v, c);
if (is1indexed) {
u--, v--;
}
adj[u] ~= Edge(v, c);
if (isUndirected) {
adj[v] ~= Edge(u, c);
}
}
return adj;
}
void yes(bool b) {
writeln(b ? "Yes" : "No");
}
void YES(bool b) {
writeln(b ? "YES" : "NO");
}
T[] readArr(T)() {
return readln.split.to!(T[]);
}
T[] readArrByLines(T)(int n) {
return iota(n).map!(i => readln.chomp.to!T).array;
}
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.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;
import std.datetime;
void main()
{
auto n = readln.chomp.to!int;
bool[string] ids;
foreach (i; 0..n) {
auto x = readln.chomp;
ids[x] = 1;
}
auto m = readln.chomp.to!int;
bool s;
foreach (i; 0..m) {
auto x = readln.chomp;
if (ids.get(x, 0)) {
if (s) writeln("Closed by ", x);
else writeln("Opened by ", x);
s = !s;
} else {
writeln("Unknown ", x);
}
}
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
auto s = iota(3).map!(i => readln.chomp).array;
auto n = iota(3).map!(i => s[i].length.to!int).array;
// s[x] の左から k 番目に s[y] が重なることは出来るか
bool check(int x, int y, int k) {
if (k >= n[x]) {
return true;
}
foreach (i ; 0 .. n[y]) {
if (k + i >= n[x]) break;
if (s[x][k + i] == '?' || s[y][i] == '?') continue;
if (s[x][k + i] != s[y][i]) {
return false;
}
}
return true;
}
auto ok = new bool[][][](3, 3, 2001);
foreach (i ; 0 .. 3) {
foreach (j ; 0 .. 3) {
if (i == j) continue;
foreach (k ; 0 .. 2001) {
ok[i][j][k] = check(i, j, k);
}
}
}
int ans = inf;
foreach (lb ; -4000 .. 4001) {
foreach (lc ; -4000 .. 4001) {
bool valid = true;
if (abs(lb) >= 2000) {
}
else if (lb < 0) {
valid &= ok[1][0][-lb];
}
else {
valid &= ok[0][1][lb];
}
if (abs(lc) >= 2000) {
}
else if (lc < 0) {
valid &= ok[2][0][-lc];
}
else {
valid &= ok[0][2][lc];
}
if (abs(lb - lc) >= 2000) {
}
else if (lb < lc) {
valid &= ok[1][2][lc - lb];
}
else {
valid &= ok[2][1][lb - lc];
}
if (valid) {
int l = min(lb, lc, 0);
int r = max(lb + n[1], lc + n[2], n[0]);
chmin(ans, r - l);
}
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
auto line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
string t = read!string;
int len = 0;
int s = 0;
foreach (c; t) {
switch (c) {
case 'S':
s++;
break;
case 'T':
if (s > 0) s--;
else {
// ペアになれなかった T はそのまま残る(後ろの文字とペアになることはない)
len++;
}
break;
default:
break;
}
}
writeln(len + s);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto n = RD!int;
auto m = RD!int;
auto k = RD!int;
auto a = RDA;
auto b = RDA;
int[int] cnt1, cnt2;
{
int streak;
foreach (i; 0..n)
{
if (a[i] == 0)
{
++cnt1[streak];
streak = 0;
}
else
++streak;
}
++cnt1[streak];
}
{
int streak;
foreach (i; 0..m)
{
if (b[i] == 0)
{
++cnt2[streak];
streak = 0;
}
else
++streak;
}
++cnt2[streak];
}
debug writeln(cnt1);
debug writeln(cnt2);
long ans;
foreach (key1; cnt1.keys)
{
foreach (i; 1..k+1)
{
if (i > key1) break;
if (k % i) continue;
auto remain = k / i;
auto c1 = cnt1[key1];
auto cc1 = key1 - i + 1;
foreach (key2; cnt2.keys)
{
if (key2 < remain) continue;
auto cc2 = key2 - remain + 1;
ans += c1 * cnt2[key2] * cc1 * cc2;
}
}
}
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto a = RDA;
auto used = new bool[](n);
bool ok = true;
foreach (i; 0..n)
{
auto p = (i+a[i]+n);
p += (abs(p/n)+1)*n;
p %= n;
if (used[cast(int)p])
{
ok = false;
break;
}
used[cast(int)p] = true;
}
ans[ti] = ok;
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush;
debug readln;
}
|
D
|
void main() {
auto N = ri;
foreach(k; 1..50001) {
if(floor(k * 1.08) == N) {
writeln(k);
return;
}
}
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, std.string, std.conv;
void main() {
int s = readln.chomp.to!int;
int h = s / 3600;
s %= 3600;
int m = s / 60;
s %= 60;
writeln(h, ":", m, ":", s);
}
|
D
|
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array, std.functional, std.container;
import std.typecons;
void main() {
auto S = readln.chomp;
auto N = S.length;
Tuple!(int, int)[] arr;
int i = 0;
while(i < N) {
int r,l;
while(i < N && S[i]=='R') { i++; r++; }
while(i < N && S[i] == 'L') { i++; l++; }
arr ~= tuple(r,l);
}
foreach(a; arr) {
int r = a[0], l = a[1];
foreach(j; 0..(r-1)) {
write("0 ");
}
write((r+1) / 2 + l/ 2, " ");
write(r / 2 + (l+1)/2, " ");
foreach(j; 0..(l-1)) {
write("0 ");
}
}
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
import core.bitop;
int n; rd(n);
int k=1;
while(k*2<=n) k*=2;
writeln(max(popcnt(n), popcnt(k-1)));
}
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.typecons;
import std.numeric, std.math;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }
long mod = 10^^9 + 7;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto M = RD;
auto C = RD;
auto B = RDR.ARR;
auto A = new long[][](N);
foreach (i; 0..N)
{
A[i] = RDR.ARR;
}
long ans;
foreach (ref e; A)
{
e[] *= B[];
if (e.sum + C > 0) ++ans;
}
writeln(ans);
stdout.flush();
}
|
D
|
void main()
{
long x = rdElem;
Pair[] list;
foreach (i; 0L .. 3000L)
{
list ~= Pair(i, i ^^ 5);
if (i) list ~= Pair(-i, -i ^^ 5);
}
foreach (a; list)
{
foreach (b; list)
{
if (a.b - b.b == x)
{
writeln(a.a, " ", b.a);
return;
}
}
}
}
struct Pair
{
long a, b;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
enum double eps = 1.0e-9;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n; readV(n);
int[] a; readA(n, a);
auto n1 = 0, n2 = 0, n3 = 0;
foreach (ai; a) {
if (ai%4 == 0) ++n1;
else if (ai%2 == 0) ++n2;
else ++n3;
}
writeln(n2 == 0 && n3 <= n1+1 || n2 > 0 && n3 <= n1 ? "Yes" : "No");
}
|
D
|
void main() {
auto t = rs.to!(dchar[]);
t.map!(v => v == '?' ? 'D' : v).array.writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
long rl() {
return readAs!long;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
void main(){
int[] ngs = [7, 5, 3];
int x = _scan();
writeln(ngs.count(x)==1? "YES": "NO");
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
|
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!(ulong[]);
ulong ans;
foreach (e; a) ans += e - 1;
writeln(ans);
stdout.flush();
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.algorithm;
long maximalModulus(long left, long right) {
if (right / 2 + 1 >= left) {
return right % (right / 2 + 1);
}
else {
long max = -1;
long i = 1;
long x = 1;
while (x > max + 1) {
x = ((right - 1) / i) + 1;
debug{writeln("x=", x, "right&x=", right%x, "max=", max);}
if (right%x > max) {
max = right%x;
}
if (x < left) {
return right % left;
}
++i;
}
return max;
}
}
// void find_mod(long left, long right)
// {
// long max_current = 0;
// long max_delimeter = 0;
// for (long a = right; a >= left; a--)
// {
// for (long b = max(a - 1, a / 2 + 1); b >= left ; b--)
// {
// auto current = a % b;
// max_current = max(max_current, current);
// if (max_current == b - 1)
// {
// writeln(max_current);
// debug{writeln("A",a," B",b);}
// return;
// }
// }
// }
// writeln(max_current);
// return;
// }
void main()
{
int n;
long l, r;
scanf("%d", &n);
getchar();
for (int k = 0; k < n; k++){
scanf("%ld %ld", &l, &r);
getchar();
// find_mod(l, r);
writeln(maximalModulus(l, r));
}
}
|
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;
void main() {
int N = readln.chomp.to!int;
long[] as = readln.split.to!(long[]);
as.take(N-1).cumulativeFold!(
(a, b) => [a.front+b, a.back-b]
)([0L, as.sum]).map!(
a => abs(a.front - a.back)
).reduce!min.writeln;
}
// ----------------------------------------------
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
|
unittest
{
assert( [ "a" ].parse.expand.solve == 'b' );
assert( [ "y" ].parse.expand.solve == 'z' );
}
import std.conv;
import std.range;
import std.stdio;
import std.typecons;
void main()
{
stdin.byLineCopy.parse.expand.solve.writeln;
}
auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
auto c = input.front.to!( dchar );
return tuple( c );
}
auto solve( dchar c )
{
return cast( dchar )( c + 1 );
}
|
D
|
void main() {
auto ip = readln.split, a = ip[0], b = ip[1];
if(a == b) "H".writeln;
else "D".writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
void main() {
problem();
}
void problem() {
const N = scan!int;
const A = scan!long(N);
void solve2() {
auto B = N.iota.map!(i => A[i] + i).array;
int[long] C;
foreach(i; 0..N) {
C[i - A[i]]++;
}
long answer;
foreach(b; B) {
if (b in C) answer += C[b];
}
writeln(answer);
}
// solve();
solve2();
}
// ----------------------------------------------
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;
void main()
{
foreach (i; 0 .. 3)
{
auto tmp = readln.chop();
tmp[i].write();
}
writeln();
}
|
D
|
import std.stdio;
import std.array;
import std.algorithm;
import std.string;
import std.container;
import std.typecons;
int size(in string s) {
return cast(int)s.length;
}
void main() {
string s = readln.chomp;
immutable X = "ABC";
bool check(in string s) {
foreach (i, c; s) {
if (s[i] != X[i]) return false;
}
return true;
}
bool f(in string s) {
if (s.size < 3) return false;
if (s.size == 3) {
return check(s);
}
if (s.find("ABC").empty) return false;
bool r = false;
foreach (c; "ABC") {
string t = "" ~ c;
if (!s.replace("ABC", "").find(t).empty) continue;
r |= f(s.replace("ABC", t));
}
return r;
}
writeln(f(s) ? "Yes" : "No");
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto S = RD!string;
auto cnt = new bool[](26);
foreach (c; S)
{
cnt[c-'a'] = true;
}
string ans;
foreach (i, e; cnt)
{
if (e == false)
{
ans = [cast(char)('a'+i)];
break;
}
}
writeln(ans == "" ? "None" : ans);
stdout.flush();
debug readln();
}
|
D
|
void main()
{
int n = readln.chomp.to!int;
int[] c = new int[n-1], s = new int[n-1], f = new int[n-1];
foreach (i; 0 .. n-1)
{
int[] tmp = readln.split.to!(int[]);
c[i] = tmp[0], s[i] = tmp[1], f[i] = tmp[2];
}
int[] time = new int[n];
foreach (i; 0 .. n-1)
{
foreach (j; i .. n-1)
{
if (time[i] < s[j])
{
time[i] = s[j];
}
else
{
int m = (time[i] - s[j] + f[j] - 1) / f[j];
time[i] = s[j] + f[j] * m;
}
time[i] += c[j];
}
}
foreach (x; time)
{
x.writeln;
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int n;
rd(n);
auto a = readln.split.to!(long[]);
long s1 = 0, s2 = 0;
foreach (i; 1 .. n) {
if (i & 1) {
s1 += a[i];
} else {
s2 += a[i];
}
}
int ans = 0;
if (s1 == s2) {
ans++;
}
foreach (i; 1 .. n) {
if (i & 1) {
s1 -= a[i];
s1 += a[i - 1];
} else {
s2 -= a[i];
s2 += a[i - 1];
}
if (s1 == s2) {
ans++;
}
}
writeln(ans);
}
// 1, 2, 3, 4, 5
// ., 2, 3, 4, 5
// 1, ., 3, 4, 5
// 1, 2, ., 4, 5
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;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
auto s = sread();
bool[] tmp = [false, false];
foreach (i; 0 .. s.length)
{
foreach (j; 0 .. s.length)
{
if (s[i] == 'C' && s[j] == 'F')
{
if (i < j)
{
writeln("Yes");
return;
}
}
}
}
writeln("No");
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
|
D
|
import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
@safe
long solve (const string s) {
enum m = 2019;
auto c = new int[m];
auto d = new int[m];
long res;
foreach (ch; s) {
c[0] += 1;
const int k = ch.to!int - 48;
d[] = 0;
int v = k;
foreach (j; 0 .. m) {
d[v] += c[j];
v += 10;
if (v >= m) v -= m;
}
swap (c, d);
res += c[0];
}
return res;
}
void main() {
auto s = readln.stripRight;
writeln (solve (s));
}
|
D
|
import std.stdio; // readln
import std.array; // split
import std.conv; // to
import std.typecons;
import std.range;
import std.algorithm;
void main(){
string[] s = split(readln());
int n = to!int(s[0]);
s = split(readln());
int[] a = new int[](n);
for(int i = 0; i < n; i++){
a[i] = to!int(s[i]);
a[i]--;
}
int cnt = 0;
for(int i = 0; i < n; i++){
if(i == a[a[i]]) cnt++;
}
writeln(cnt / 2);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto H = RD;
auto W = RD;
auto s = new string[](H);
foreach (i; 0..H)
{
s[i] = RD!string;
}
long[][] open = [[0, 0]];
auto dist = new long[][](H, W);
foreach (i; 0..H)
dist[i][] = long.max;
dist[0][0] = s[0][0] == '#' ? 1 : 0;
while (!open.empty)
{
auto nd = open.front; open.popFront;
auto y = nd[0];
auto x = nd[1];
if (y != H-1)
{
auto d = dist[y][x];
auto ny = y+1;
auto nx = x;
if (s[y][x] == '.' && s[ny][nx] == '#')
++d;
if (d < dist[ny][nx])
{
dist[ny][nx] = d;
open ~= [ny, nx];
}
}
if (x != W-1)
{
auto d = dist[y][x];
auto ny = y;
auto nx = x+1;
if (s[y][x] == '.' && s[ny][nx] == '#')
++d;
if (d < dist[ny][nx])
{
dist[ny][nx] = d;
open ~= [ny, nx];
}
}
}
writeln(dist[H-1][W-1]);
stdout.flush;
debug readln;
}
|
D
|
import std;
void main() {
int n; scan(n);
int ans = 0;
bool[string] d;
foreach (_; 0..n) {
string s = read!string;
d[s] = true;
}
writeln(d.keys.length);
}
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
|
D
|
import std.stdio, 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;
bool[char] set;
foreach (i; 0..N)
set[RD!char] = true;
writeln(set.keys.length == 3 ? "Three" : "Four");
stdout.flush();
debug readln();
}
|
D
|
// Cheese-Cracker: cheese-cracker.github.io
void solve(){
long n = scan;
auto a = scanArray!int;
auto b = scanArray!int;
auto freqa = new ll[301];
auto freqb = new ll[301];
auto cache = new ll[301];
for(int i = 0; i < n; ++i){
++freqa[a[i] + 100];
++freqb[b[i] + 100];
}
for(int i = 0; i <= 200; ++i){
ll take = min(freqb[i], cache[i]);
cache[i] -= take;
ll left = freqb[i] - take;
freqa[i] -= left;
if(freqa[i] < 0){
writeln("NO");
return;
}
cache[i+1] += freqa[i];
freqa[i] = 0;
}
writeln("YES");
}
void main(){
long tests = scan; // Toggle!
while(tests--) solve;
}
/************** ***** That's All Folks! ***** **************/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math;
string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
alias ll = long, tup = Tuple!(long, "x", long, "y");
|
D
|
void main() {
string[] tmp = readln.split;
int a = tmp[0].to!int, b = tmp[1].to!int;
string p = tmp[0];
int cnt;
foreach (i; a .. b+1) {
char[] q = p.dup;
reverse(q);
if (p == q) ++cnt;
p = p.succ;
}
cnt.writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
// 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);
writeln(s.count('1'));
}
|
D
|
import std.algorithm;
import std.stdio;
import std.string;
import std.conv;
import std.range;
void main(){
2.iota.map!(a => readln.split.map!(to!int).reduce!q{a + b}).reduce!max.writeln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto M = RD;
auto edges = new long[][](N);
foreach (i; 0..M)
{
auto a = RD-1;
auto b = RD-1;
edges[a] ~= b;
edges[b] ~= a;
}
foreach (i; 0..N)
writeln(edges[i].length);
stdout.flush();
debug readln();
}
|
D
|
void main(){
int n = _scan();
int[] a = _scanln();
bool approve = true;
foreach(elm; a){
if(elm%2 == 0){
if( elm%3==0 || elm%5==0 )continue;
else{
approve = false;
break;
}
}
}
writeln(approve? "APPROVED": "DENIED");
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto s = readln.chomp;
auto c = new int[](26);
foreach (si; s) ++c[si-'a'];
writeln(c.all!"a<2" ? "yes" : "no");
}
|
D
|
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
void main() {
auto N = readln.chomp;
void solve() {
ulong answer;
bool nextKetaUp;
auto keta = N.length;
foreach(i; 0..keta) {
auto num = N[$-1-i..$-i].to!(int) + (nextKetaUp ? 1 : 0);
if (num == 10) continue;
nextKetaUp = false;
if (i < keta-1) {
auto next = N[$-2-i..$-1-i].to!(int);
if (num >= 5 && next >= 5) {
answer += 10 - num;
nextKetaUp = true;
continue;
}
}
if (num >= 6) {
answer += 10 - num;
nextKetaUp = true;
continue;
}
answer += num;
}
if (nextKetaUp) answer++;
writeln(answer);
}
solve();
}
|
D
|
void main()
{
string[] tmp = rdRow!string;
long n = tmp[0].to!long, m = tmp[1].to!long;
long[] p = new long[m];
string[] s = new string[m];
foreach (i; 0 .. m)
{
tmp = rdRow!string;
p[i] = tmp[0].to!long;
s[i] = tmp[1];
}
bool[] actof = new bool[n+1];
long[] wacnt = new long[n+1];
foreach_reverse (i; 0 .. m)
{
if (s[i] == "AC")
{
actof[p[i]] = true;
wacnt[p[i]] = 0;
}
else
{
++wacnt[p[i]];
}
}
long ac, wa;
foreach (i; 1 .. n+1)
{
if (actof[i])
{
++ac;
wa += wacnt[i];
}
}
writeln(ac, " ", wa);
}
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 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() {
auto N = readln.chomp.map!(ch => ch - '0').array;
auto L = N.length.to!int;
debug {
writeln(N);
}
int K;
scan(K);
auto dp = new long[][][](L + 1, 2, K + 2);
dp[0][0][0] = 1;
foreach (i ; 0 .. L) {
foreach (j ; 0 .. 2) {
foreach (x ; 0 .. K + 2) {
foreach (d ; 0 .. (j ? 9 : N[i]) + 1) {
if (x == K + 1) {
dp[i + 1][j | (d < N[i])][x] += dp[i][j][x];
}
else {
dp[i + 1][j | (d < N[i])][x + (d != 0)] += dp[i][j][x];
}
}
}
}
}
auto ans = dp[L][0][K] + dp[L][1][K];
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 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.random;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void main()
{
int k = readln.chomp.to!int;
int[] done = new int[](k);
done[] = int.max;
done[1] = 1;
int[] queue = [1];
while (queue.length)
{
int node = queue.front;
queue = queue[1 .. $];
int next10 = (node * 10) % k;
if (done[node] < done[next10])
{
done[next10] = done[node];
queue = next10 ~ queue;
}
int next1 = (node + 1) % k;
if (done[node] + 1 < done[next1]) {
done[next1] = done[node] + 1;
queue ~= next1;
}
}
done[0].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);}}
enum MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std;
// dfmt on
void main()
{
long N = lread();
auto f = factorize(N);
long ans;
foreach (key, val; f)
{
long cnt = 1;
while ((cnt + 2) * (cnt + 1) / 2 <= val)
cnt++;
ans += cnt;
}
writeln(ans);
}
/// 素因数分解
long[long] factorize(long x)
{
assert(0 < x, "x is negative");
long[long] ps;
while ((x & 1) == 0)
{
x /= 2;
ps[2] = (2 in ps) ? ps[2] + 1 : 1;
}
for (long i = 3; i * i <= x; i += 2)
while (x % i == 0)
{
x /= i;
ps[i] = (i in ps) ? ps[i] + 1 : 1;
}
if (x != 1)
ps[x] = (x in ps) ? ps[x] + 1 : 1;
return ps;
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto x = readln.chomp.to!int;
writeln(x < 1200 ? "ABC" : "ARC");
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int N, K;
scan(N, K);
readln.split.to!(int[]).filter!(h => h >= K).count.writeln;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.stdio, std.algorithm, std.string;
void main()
{
readln();
auto s = readln().strip();
bool ok = true;
ok &= !canFind(s, "000");
ok &= !canFind(s, "11");
ok &= !s.startsWith("00");
ok &= !s.endsWith("00");
ok &= s != "0";
writeln(ok ? "Yes" : "No");
}
|
D
|
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
void main() {
debug {
foreach (m; 1 .. 7) {
auto cs = new char[][](m, 1 << m);
foreach (x; 0 .. m) {
cs[x][] = '.';
}
foreach (y; 1 .. 1 << m) {
cs[m - 1 - bsf(y)][y] = "01"[popcnt((1 << m) - y) & 1 ^ 1];
}
foreach (x; 0 .. m) {
writeln(cs[x]);
}
writeln();
}
}
try {
for (; ; ) {
const N = readInt();
if (N == 1) {
writeln(1);
continue;
}
for (int m = 1; ; ++m) {
if (1 << (m - 1) <= N && N < 1 << m) {
auto f = new int[1 << m];
foreach (y; 1 .. 1 << m) {
f[y] = popcnt((1 << m) - y) & 1 ^ 1 ^ (N & 1);
}
int n = (1 << (m - 1)) - 1;
for (int y = 1; y < (1 << m) - 1; y += 2) {
if (f[y - 1] != f[y] && f[y] != f[y + 1]) {
++n;
}
}
debug {
writeln("m = ", m, ", top = ", N & 1);
writeln(" f = ", f);
writeln(" n = ", n);
}
writeln((N == n) ? 1 : 0);
break;
}
}
}
} catch (EOFException e) {
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm, std.array, std.range, std.container;
import std.math;
void main() {
auto S = readln.split[0];
int ans1, ans2;
foreach (i; 0 .. S.length) {
if (i % 2 == 0) {
S[i] == '0' ? ans1++ : ans2++;
}
else {
S[i] == '0' ? ans2++ : ans1++;
}
}
writeln(min(ans1, ans2));
}
|
D
|
import std.stdio;
import std.conv;
import std.algorithm;
import std.range;
import std.string;
import std.math;
import std.format;
void main() {
string[] q;
foreach (string line; stdin.lines) {
if (line == "quit\n") break;
string op, clr;
auto s = line.chomp.split;
if (s.length == 1) {
op = s[0];
} else {
op = s[0];
clr = s[1];
}
switch (op) {
case "push":
q ~= clr;
break;
case "pop":
q.back.writeln;
q.popBack;
default:
}
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto n = RD!int;
auto a = RDA;
auto b = RDA;
int ans;
bool[long] set;
while (true)
{
if (b.empty)
{
break;
}
if (set.get(a.front, false))
{
a.popFront;
}
else if (a.front != b.front)
{
if (set.get(a.front, false) == false)
{
++ans;
set[b.front] = true;
}
b.popFront;
}
else
{
a.popFront;
set[b.front] = true;
b.popFront;
}
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
void main() {
problem();
}
void problem() {
auto N = scan!ulong;
ulong solve() {
// Sum_{k=1..n} k/2 * floor(n/k) * floor(1 + n/k)
ulong ans;
foreach(k; 1..N+1) {
ulong x = k * cast(ulong)floor(cast(real)N/k) * cast(ulong)floor(1 + cast(real)N/k);
ans += x / 2;
}
return ans;
}
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 Queue = Tuple!(long, "from", long, "to");
// -----------------------------------------------
|
D
|
import std.stdio;
import std.string;
import std.algorithm;
import std.conv;
import std.array;
import std.math;
import std.range;
void main()
{
auto w = readln.chomp.toLower;
string[] ss;
while(1){
auto line = readln.chomp;
if(line == "END_OF_TEXT") break;
ss ~= line.toLower.split;
}
writeln(ss.count(w));
}
|
D
|
import std.stdio,
std.string,
std.conv,
std.algorithm;
void main() {
int[] buf = readln.chomp.split.to!(int[]);
int a = buf[0] - 1, b = buf[1] - 1;
int K = 50;
char[100][100] m;
for (int i = 0; i < K; i++) {
for (int j = 0; j < K * 2; j++) {
m[i][j] = '#';
}
}
for (int i = K; i < K * 2; i++) {
for (int j = 0; j < K * 2; j++) {
m[i][j] = '.';
}
}
for (int i = 1; i < K - 1; i += 2) {
if (a <= 0) break;
for (int j = 1; j < K * 2; j += 2) {
if (a <= 0) break;
m[i][j] = '.';
a--;
}
}
for (int i = K + 1; i < K * 2; i += 2) {
if (b <= 0) break;
for (int j = 1; j < K * 2; j += 2) {
if (b <= 0) break;
m[i][j] = '#';
b--;
}
}
writeln("100 100");
foreach(line; m) {
writeln(line);
}
}
|
D
|
void main() {
auto N = ri;
ulong cnt;
foreach(i; 0..N) {
if(rs == "E869120") cnt++;
}
cnt.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;
import std.range;
import std.array;
import std.algorithm;
void main() {
string input;
while ((input = readln.chomp).length != 0) {
while (input.length > 1) {
string output;
for (int i = 0; i < input.length-1; i++) {
output ~= (((input[i] - '0')+(input[i+1] - '0'))%10).to!string;
}
input = output;
}
writeln(input);
}
}
|
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 = lread();
auto A = aryread();
auto sum = new long[](N + 1);
foreach (i; 0 .. N)
{
sum[i + 1] = sum[i] + A[i];
}
long ans = long.max;
foreach (i; 1 .. N)
{
long tmp = abs(sum[i] - (sum[$ - 1] - sum[i]));
ans = ans.min(tmp);
}
writeln(ans);
}
|
D
|
void main(){
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
writeln(max(a*b, c*d));
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range;
const long mod = 10^^9+7;
// 1要素のみの入力
T inelm(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] inln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split())ln ~= elm.to!T();
return ln;
}
|
D
|
import std.stdio;
import std.string, std.conv, std.array, std.algorithm;
import std.uni, std.math, std.container;
import core.bitop, std.datetime;
void main(){
auto s = readln.chomp;
auto stack = new Stack!(char)(s.length.to!int);
foreach(c;s){
if(c == 'B'){
if(!stack.isEmpty) stack.pop();
} else{
stack.push(c);
}
}
stack.print;
}
class Stack(T){
private T[] stack;
private int top, size;
this(int N){
stack = new T[](N + 1);
size = N + 1;
}
bool isEmpty(){
return top == 0;
}
bool isFull(){
return top >= size;
}
void push(T stuff){
++top;
if(isFull) throw new Exception("Stack is Full.");
stack[top] = stuff;
}
auto pop(){
if(isEmpty) throw new Exception("Stack is Empty.");
auto ret = stack[top];
--top;
return ret;
}
auto print(){
foreach(i ; 1 .. top + 1){
write(stack[i]);
}
writeln;
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
struct P {int first; int second;}
int main()
{
while (true) {
string[] str = readln().chomp().split();
int h = str[0].to!int();
int w = str[1].to!int();
if (h == 0 && w == 0) break;
string t;
char[101][101] f;
for (int i = 0; i < h; i++) {
t = readln.chomp;
for (int j = 0; j < w; j++) {
f[i][j] = t[j].to!char;
}
}
P[char] m; P p;
p.first = 0; p.second = 1; m['>'] = p;
p.first = 0; p.second = -1; m['<'] = p;
p.first = -1; p.second = 0; m['^'] = p;
p.first = 1; p.second = 0; m['v'] = p;
p.first = p.second = 0; m['.'] = m['*'] = p;
int x = 0, y = 0;
while (true) {
int nx = x, ny = y;
if (f[nx][ny] == '*') {
writeln("LOOP");
break;
}
//writeln(m[f[nx][ny]].first, " ", m[f[nx][ny]].second);
if (0 <= nx + m[f[nx][ny]].first && nx + m[f[nx][ny]].first < h && 0 <= ny + m[f[nx][ny]].second && ny + m[f[nx][ny]].second < w) {
nx = nx + m[f[nx][ny]].first;
ny = ny + m[f[nx][ny]].second;
}
f[x][y] = '*';
if (x == nx && y == ny || f[nx][ny] == '.') {
writeln(ny, " ", nx);
break;
}
x = nx; y = ny;
}
}
return 0;
}
|
D
|
//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
final class InputReader {
private:
ubyte[] p, buffer;
bool eof;
bool rawRead () {
if (eof) {
return false;
}
p = stdin.rawRead (buffer);
if (p.empty) {
eof = true;
return false;
}
return true;
}
ubyte nextByte(bool check) () {
static if (check) {
if (p.empty) {
if (!rawRead ()) {
return 0;
}
}
}
auto r = p.front;
p.popFront ();
return r;
}
public:
this () {
buffer = uninitializedArray!(ubyte[])(16<<20);
}
bool seekByte (in ubyte lo) {
while (true) {
p = p.find! (c => c >= lo);
if (!p.empty) {
return false;
}
if (!rawRead ()) {
return true;
}
}
}
template next(T) if (isSigned!T) {
T next () {
if (seekByte (45)) {
return 0;
}
T res;
ubyte b = nextByte!false ();
if (b == 45) {
while (true) {
b = nextByte!true ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 - (b - 48);
}
} else {
res = b - 48;
while (true) {
b = nextByte!true ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 + (b - 48);
}
}
}
}
template next(T) if (isUnsigned!T) {
T next () {
if (seekByte (48)) {
return 0;
}
T res = nextByte!false () - 48;
while (true) {
ubyte b = nextByte!true ();
if (b < 48 || b >= 58) {
break;
}
res = res * 10 + (b - 48);
}
return res;
}
}
T[] nextA(T) (in int n) {
auto a = uninitializedArray!(T[]) (n);
foreach (i; 0 .. n) {
a[i] = next!T;
}
return a;
}
}
void main() {
auto r = new InputReader ();
immutable nt = r.next!uint ();
foreach (tid; 0 .. nt) {
auto a = r.nextA!uint (4);
writeln (a[1], ' ', a[2], ' ', a[2]);
}
}
|
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 xnm = readln.split.to!(int[]);
auto X = xnm[0];
auto N = xnm[1];
auto M = xnm[2];
while (N > 0 && X/2 >= 10) {
X = X/2 + 10;
--N;
}
writeln(X <= M*10 ? "YES" : "NO");
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
auto hs = readln.split.to!(int[]);
int solve(int[] lr) {
if (lr.empty) return 0;
auto m = int.max;
foreach (n; lr) m = min(m, n);
int r = m;
size_t i;
foreach (j, ref n; lr) {
n -= m;
if (!n) {
r += solve(lr[i..j]);
i = j+1;
}
}
return r + (lr[$-1] == 0 ? 0 : solve(lr[i..$]));
}
writeln(solve(hs));
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main()
{
int n = readln.chomp.to!(int);
long[] a = readln.chomp.split.to!(long[]);
int ans = 0;
for (int i = 0; i < a.length; i++) {
while (true) {
if (!(a[i] & 1)) {
ans += 1;
a[i] /= 2;
}
else {
break;
}
}
}
writeln(ans);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.array, std.algorithm, std.range;
import std.math;
void main()
{
immutable long N = 1000;
foreach(n;stdin.byLine().map!(to!long))
iota(max(0,n-2*N),min(2*N,n)+1).map!(i=>(N-abs(N-i)+1)*(N-abs(N-(n-i))+1)).reduce!"a+b"().writeln();
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, 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 main()
{
int x, t; readV(x, t);
writeln(max(0, x-t));
}
|
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;
void main(){
auto ip = readln.split.to!(int[]);
if(ip[0] + ip[1] >= ip[2]){
writeln("Yes");
} else {
writeln("No");
}
}
|
D
|
import std.stdio;
void main() {
long l, r;
scanf("%ld%ld", &l, &r);
auto ans = 0;
for (long a2=1; a2<=r; a2*=2) {
for (long a3=1; a3<=r/a2; a3*=3) {
if (a2 * a3 >= l) ans++;
}
}
writeln(ans);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
import std.math;
int x, a, b;
scan(x, a, b);
writeln(abs(x-a) < abs(x-b) ? "A" : "B");
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
void main() {
int x, y;
scan(x, y);
writeln(x + y / 2);
}
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
|
void main(){
auto K = readLine!()()[0];
auto AB = readLine!()();
foreach( i ; 1..1000 ){
if( AB[0] <= K*i && K*i <= AB[1] ){
writeln("OK");
return;
}
if( K*i > 1001 ){break;}
}
writeln("NG");
return;
}
import std.stdio, std.string, std.conv;
import std.math, std.algorithm, std.array;
import std.regex;
T[] readLine( T = size_t )( string sp = " " ){
T[] ol;
foreach( string elm ; readln().chomp().split(sp) ){
ol ~= elm.to!T();
}
return ol;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math;
struct UFTree(T)
{
struct Node
{
T parent;
T rank = 1;
}
this(T n)
{
nodes.length = n;
foreach (i, ref node; nodes) node = Node(i.to!T);
}
void unite(T a, T b)
{
a = root(a);
b = root(b);
if (a == b) return;
if (nodes[a].rank < nodes[b].rank) {
nodes[b].parent = nodes[a].parent;
} else {
nodes[a].parent = nodes[b].parent;
if (nodes[a].rank == nodes[b].rank) nodes[a].rank += 1;
}
}
bool is_same(T a, T b)
{
return root(a) == root(b);
}
private:
Node[] nodes;
T root(T i)
{
if (nodes[i].parent == i) return i;
return nodes[i].parent = root(nodes[i].parent);
}
}
void main()
{
auto nm = readln.split.to!(int[]);
auto N = nm[0];
auto M = nm[1];
auto PS = readln.split.to!(int[]);
auto uft = UFTree!int(N+1);
foreach (_; 0..M) {
auto xy = readln.split.to!(int[]);
uft.unite(xy[0], xy[1]);
}
int res;
foreach (int i, p; PS) {
if (uft.is_same(i+1, p)) ++res;
}
writeln(res);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main() {
auto l = readln.chomp;
auto fst = l[0..2].to!int;
auto snd = l[2..4].to!int;
auto fM = fst > 0 && fst <= 12;
auto sM = snd > 0 && snd <= 12;
if (fM && sM)
write("AMBIGUOUS");
else if (sM)
write("YYMM");
else if (fM)
write("MMYY");
else
write("NA");
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.