code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
struct BITree(alias _fun, alias E, T)
if (is(typeof(E) : T))
{
import std.functional : binaryFun;
alias OP = binaryFun!_fun;
///
this(size_t n, T[] ts) {
this.n = n;
this.tree.length = n+1;
foreach (ref e; this.tree) e = E;
foreach (i, t; ts) this.update(i, t);
}
void update(size_t i, T e) {
i += 1;
while (i <= this.n) {
this.tree[i] = OP(this.tree[i], e);
i += i & -i;
}
}
///
T query(size_t i) {
auto r = E;
while (i > 0) {
r = OP(r, this.tree[i]);
i -= i & -i;
}
return r;
}
private:
size_t n;
T[] tree;
}
auto bitree(alias fun, alias init, T)(size_t n, T[] ts = [])
{
return BITree!(fun, init, T)(n, ts);
}
///
auto bitree(alias fun, alias init, T)(T[] ts)
{
return BITree!(fun, init, T)(ts.length, ts);
}
auto sum_bitree(T)(size_t n, T[] ts = [])
{
return bitree!("a + b", 0, T)(n, ts);
}
auto sum_bitree(T)(T[] ts)
{
return sum_bitree!T(ts.length, ts);
}
void main()
{
for (;;) {
auto ql = readln.split.to!(int[]);
auto Q = ql[0];
auto L = ql[1];
if (Q == 0 && L == 0) break;
int[] xs;
size_t[int] xi;
auto bit = sum_bitree!int(Q);
foreach (_; 0..Q) {
auto q = readln.split.to!(int[]);
if (q[0] == 0) {
auto x = q[1];
auto i = xs.length;
xi[x] = i;
xs ~= x;
bit.update(i, 1);
if (bit.query(Q) > L) {
if (bit.query(1) == 1) {
bit.update(0, -1);
} else {
size_t l = 1, r = Q;
while (l+1 < r) {
auto m = (l+r)/2;
if (bit.query(m) > 0) {
r = m;
} else {
l = m;
}
}
bit.update(l, -1);
}
}
}
if (q[0] == 1) {
auto i = q[1];
if (bit.query(1) == i) {
bit.update(0, -1);
} else if (bit.query(Q) >= i) {
size_t l = 1, r = Q;
while (l+1 < r) {
auto m = (l+r)/2;
if (bit.query(m) < i) {
l = m;
} else {
r = m;
}
}
bit.update(l, -1);
}
}
if (q[0] == 2) {
auto i = q[1];
if (bit.query(Q) >= i) {
int x;
if (bit.query(1) == i) {
x = xs[0];
} else {
size_t l = 1, r = Q;
while (l+1 < r) {
auto m = (l+r)/2;
if (bit.query(m) < i) {
l = m;
} else {
r = m;
}
}
x = xs[l];
}
writeln(x);
}
}
if (q[0] == 3) {
bit.update(xi[q[1]], -1);
}
}
writeln("end");
}
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
const MOD = 10^^9 + 7;
long calc(long[] a) {
long[61] zero, one;
foreach (e; a) {
for (int i = 0; i <= 60; i++) {
if (e & (1L << i)) one[i]++;
else zero[i]++;
}
}
long ans = 0;
for (int i = 0; i <= 60; i++) {
// 0 と 1 のペア数
long cnt = zero[i] * one[i] % MOD;
ans += ((1L << i) % MOD) * cnt % MOD;
ans %= MOD;
}
return ans;
}
void main() {
read;
auto a = reads!long;
writeln(calc(a));
}
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T=string)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readints = reads!int;
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random;
import std.typecons, std.functional, std.traits,std.concurrency;
import std.algorithm, std.container;
import core.bitop, core.time, core.memory;
import std.bitmanip;
import std.regex;
enum INF = long.max/3;
enum MOD = 10L^^9+7;
//辞書順順列はiota(1,N),nextPermituionを使う
void end(T)(T v)
if(isIntegral!T||isSomeString!T||isSomeChar!T)
{
import core.stdc.stdlib;
writeln(v);
exit(0);
}
T[] scanArray(T = long)()
{
static char[] scanBuf;
readln(scanBuf);
return scanBuf.split.to!(T[]);
}
dchar scanChar()
{
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
return cast(dchar)c;
}
T scanElem(T = long)()
{
import core.stdc.stdlib;
static auto scanBuf = appender!(char[])([]);
scanBuf.clear;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
scanBuf ~= cast(char) c;
c = getchar;
}
return scanBuf.data.to!T;
}
dchar[] scanString(){
return scanElem!(dchar[]);
}
void main()
{
auto a=scanString;
auto b=scanString;
auto c=scanString;
write(dchar(a[0]-'a'+'A'));
write(dchar(b[0]-'a'+'A'));
write(dchar(c[0]-'a'+'A'));
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 = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "x", long, "y", long, "cost");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
auto s = sread();
long cnt = INF;
if (s.allEqual())
{
writeln(0);
return;
}
foreach (c; "abcdefghijklmnopqrstuvwxyz")
{
cnt = min(s.needchange(c), cnt);
}
cnt.writeln();
}
long needchange(string str, char c)
{
if (str.length == 1)
return 1;
auto newstr = new char[](str.length - 1);
foreach (i; iota(str.length - 1))
{
newstr[i] = (str[i] == c || str[i + 1] == c) ? c : str[i];
}
if (newstr.allEqual())
return 1;
else
return 1 + needchange(newstr.to!string, c);
}
bool allEqual(T)(T s)
{
bool ret = true;
foreach (i; iota(s.length - 1))
ret &= s[i] == s[i + 1];
return ret;
}
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 core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.format;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
long a,b;
scan(a,b);
if(a*b%2)
{
writeln("Yes");
}
else
{
writeln("No");
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.algorithm, std.array;
import std.math;
auto solve(string s_)
{
auto NA = s_.split.map!(to!int)();
immutable N=NA[0], A=NA[1];
immutable xs=readln.split.map!(to!int).array();
immutable M=N*A+1;
auto dp = new long[][](N+1,M); dp[0][0]=1;
foreach(int k,x;xs)
{
foreach_reverse(j;0..k+1)
foreach(i;0..max(0,M-x))
dp[j+1][i+x]+=dp[j][i];
}
long c=0;
foreach(i;1..N+1) c+=dp[i][i*A];
return c;
}
void main(){ for(string s; (s=readln.chomp()).length;) writeln(solve(s)); }
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.algorithm;
import std.range;
import std.functional;
import std.math;
import core.bitop;
void main()
{
long H, W, D;
{
auto input = readln.chomp.split(' ').map!(to!long);
H = input[0];
W = input[1];
D = input[2];
}
long[2][] table = new long[2][](H * W + 1);
foreach (h; 0 .. H)
{
auto input = readln.chomp.split(' ').map!(to!long).array;
foreach (w, n; input)
{
table[n] = [h, w];
}
}
long[] cost = new long[](H * W + 1);
foreach (i; D + 1 .. H * W + 1)
{
cost[i] = cost[i - D] + abs(table[i][0] - table[i - D][0]) + abs(
table[i][1] - table[i - D][1]);
}
auto Q = readln.chomp.to!long;
foreach (_; 0 .. Q)
{
long L, R;
{
auto input = readln.chomp.split(' ').map!(to!long);
L = input[0];
R = input[1];
}
writeln(cost[R] - cost[L]);
}
}
|
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() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto M = s[1];
auto uf = new UnionFind(N);
auto edges = new int[][](N);
foreach (i; 0..M) {
s = readln.split.map!(to!int);
uf.unite(s[0]-1, s[1]-1);
edges[s[0]-1] ~= s[1]-1;
edges[s[1]-1] ~= s[0]-1;
}
long g1, g2, g3;
foreach (i; 0..N) {
if (uf.find(i) == uf.find(0)) {
g1 += edges[i].length.to!int;
} else if (uf.find(i) == uf.find(1)) {
g2 += edges[i].length.to!int;
} else {
g3 += edges[i].length.to!int;
}
}
g1 /= 2;
g2 /= 2;
g3 /= 2;
long s1 = -uf.table[uf.find(0)];
long s2 = -uf.table[uf.find(1)];
long s3 = N - s1 - s2;
if (s1 < s2) {
swap(s1, s2);
swap(g1, g2);
}
long hoge = s1 + s3;
long ans = hoge * (hoge-1) / 2 - g1 - g3 + s2 * (s2-1) / 2 - g2;
ans.writeln;
}
class UnionFind {
int N;
int[] table;
this(int n) {
N = n;
table = new int[](N);
fill(table, -1);
}
int find(int x) {
return table[x] < 0 ? x : (table[x] = find(table[x]));
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return;
if (table[x] > table[y]) swap(x, y);
table[x] += table[y];
table[y] = x;
}
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
static import std.ascii;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long H, N;
scan(H, N);
auto A = aryread();
writeln(A.sum() < H ? "No" : "Yes");
}
|
D
|
void main() {
auto S = rs;
auto w = ri;
string res;
foreach(i; iota(0, S.length, w)) {
res ~= S[i];
}
res.writeln;
}
// ===================================
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
|
import std;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N, M;
scan(N, M);
auto A = aryread();
long ans = N - A.sum();
writeln(max(-1, ans));
}
|
D
|
import std.stdio,
std.string,
std.conv,
std.algorithm;
void main() {
int N = readln.chomp.to!(int);
int[] buf = readln.chomp.split.to!(int[]);
int D = buf[0], X = buf[1];
int[] A;
for (int i = 0; i < N; i++) {
A ~= readln.chomp.to!(int);
}
int ans;
for (int i = 0; i < N; i++) {
for (int j = 0; j * A[i] + 1 <= D; j++) {
ans++;
}
}
writeln(ans + X);
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto rgb = readln.chomp.split.map!(to!int);
writeln((rgb[0]*100+rgb[1]*10+rgb[2])%4?"NO":"YES");
}
|
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;
long ans;
foreach (c; s)
{
if (c == '1')
++ans;
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
import std.conv, std.functional, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.range, 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)); }
int N;
string S;
void main() {
try {
for (; ; ) {
N = readInt();
S = readToken();
auto sum = new int[N + 1];
foreach (i; 0 .. N) {
sum[i + 1] = sum[i] + ((S[i] == '#') ? 1 : 0);
}
// ...###
int ans = N + 1;
foreach (i; 0 .. N + 1) {
int tmp;
tmp += sum[i];
tmp += (N - i) - (sum[N] - sum[i]);
chmin(ans, tmp);
}
writeln(ans);
}
} catch (EOFException e) {
}
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
immutable long MOD = 10^^9 + 7;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto M = s[1];
auto S = readln.chomp;
auto LR = M.iota.map!(_ => readln.split.map!(a => a.to!int - 1).array).array;
auto zeros = new long[](N+1);
foreach (i; 0..N) {
zeros[i+1] = zeros[i] + (S[i] == '0');
}
auto dp = new long[][](N+1, N+1);
dp[0][0] = 1;
int rmax = 0;
int q = 0;
foreach (i; 0..N) {
while (q < M && LR[q][0] <= i) {
rmax = max(LR[q][1], rmax);
q += 1;
}
rmax = max(rmax, i);
long zero = zeros[rmax+1];
long one = rmax + 1 - zero;
foreach (j; 0..i+1) {
long rest_zero = zero - j;
long rest_one = one - (i - j);
if (rest_zero < 0 || rest_one < 0) {
continue;
}
if (rest_zero > 0) {
dp[i+1][j+1] += dp[i][j];
dp[i+1][j+1] %= MOD;
}
if (rest_one > 0) {
dp[i+1][j] += dp[i][j];
dp[i+1][j] %= MOD;
}
}
}
long ans = 0;
foreach (a; dp[N]) {
ans += a;
ans %= MOD;
}
ans.writeln;
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto n = readln.chomp.to!size_t;
auto rci = iota(n).map!(_ => readln.split.to!(size_t[])).array;
auto memo = new size_t[][](n, n + 1);
memo.each!(a => a[] = size_t.max);
size_t dp(size_t s, size_t t)
{
if (memo[s][t] < size_t.max)
return memo[s][t];
if (t - s <= 1)
return 0;
auto minP = size_t.max;
foreach (i; s+1..t) {
auto p = dp(s, i) + dp(i, t) + rci[s][0] * rci[t-1][1] * rci[i][0];
minP = min(minP, p);
}
return memo[s][t] = minP;
}
writeln(dp(0, n));
}
|
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() {
auto N = readln.chomp.map!(i => (i - '0').to!int).retro.array;
N ~= 0;
auto L = N.length.to!int;
auto dp = new int[][](L + 1, 2);
fillAll(dp, inf);
dp[0][0] = 0;
foreach (i ; 0 .. L) {
foreach (j ; 0 .. 2) {
foreach (d ; 0 .. 10) {
int x = N[i] + j;
if (d < x) {
chmin(dp[i + 1][1], dp[i][j] + d + 10 + d - x);
}
else {
chmin(dp[i + 1][0], dp[i][j] + d + d - x);
}
}
}
}
auto ans = dp[L][0];
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.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
bool calc(int[] ds, int[] ts) {
int[int] map;
foreach (d; ds)
map[d]++;
foreach (t; ts) {
int x = map.get(t, 0);
if (x > 0) {
map[t]--;
}
else {
return false;
}
}
return true;
}
void main() {
readint;
auto ds = readints;
readint;
auto ts = readints;
writeln(calc(ds, ts) ? "YES" : "NO");
}
|
D
|
void main()
{
long[] tmp = rdRow;
long n = tmp[0], k = tmp[1];
long[] a = rdRow;
long g = a[0];
long amax = a[0];
foreach (x; a)
{
if (x == k)
{
"POSSIBLE".writeln;
return;
}
g = gcd(g, x);
amax = max(amax, x);
}
bool ok = k <= amax;
if (ok) ok = (amax - k) % g == 0;
writeln(ok ? "POSSIBLE" : "IMPOSSIBLE");
}
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.conv, std.string, std.algorithm;
void main()
{
auto s = readln.strip;
writeln(700 + s.count('o') * 100);
}
|
D
|
import std.stdio;
import std.regex;
void main(){
auto io = new IO();
auto w = io.line!string()[0];
foreach( a ; alphaB ){
if( w.count(a) % 2 != 0 ){
writeln( "No" );
return;
}
}
writeln("Yes");
return;
}
import std.stdio,std.conv,std.string;
import std.algorithm,std.array,std.math;
immutable PRIME = 1_000_000_007;
immutable alphaB = "abcdefghijklmnopqrstuvwxyz";
immutable alphaU = "ABCDRFGHIJKLMNOPQRSTUVWXYZ";
class IO
{
T[] line( T = size_t , string token = " " )( size_t m = 1 ){
T[] arr = [];
foreach( i ; 0..m ){
arr ~= this.read!T();
}
return arr;
}
T[][] rect( T = size_t , string token = " " )( size_t m = 1 ){
T[][] arr = new T[][](m);
foreach( i ; 0..m ){
arr[i] = this.read!T(token);
}
return arr;
}
private T[] read( T = size_t )( string token = " " ){
T[] arr;
foreach( elm ; readln().chomp().split(token) ){
arr ~= elm.to!T();
}
return arr;
}
void swap( T )( ref T x , ref T y ){
x = x^y;
y = x^y;
x = x^y;
}
}
// Array compute
// T -> T[]
pure T[] step( T = size_t )( in T begin , in T end , in T step = 1 ){
T[] ret = new T[]((end-begin)/step);
size_t i;
for( T n = begin ; n < end ; n += step ){
ret[i] = n;
i+=1;
}
return ret;
}
unittest{
assert( step(0,10,2) == [0,2,4,6,8] );
}
// T[] -> T[]
pure T[] fill( T )( T[] args , in T filling ){
foreach( ref arg ; args ){
arg = filling;
}
return args;
}
T[] map( T )( T[] args , T function(T) f ){
foreach( ref arg ; args ){
arg = f(arg);
}
return args;
}
unittest{
assert( [true,false,true,false].fill(true) == [true,true,true,true] );
assert( [1,-2,3,-4].map!int(x=>x*2) == [2,-4,6,-8] );
}
// operation
// T[] -> T number
pure T sum( T )( in T[] args ){
T ret = 0;
foreach( i ; 0..args.length ){
ret += args[i];
}
return ret;
}
pure real multi( in real[] args ){
real ret = 1.0;
foreach( i ; 0..args.length ){
ret *= args[i];
}
return ret;
}
pure T ave(T)( in T[] args ){
return args.sum()/args.length;
}
// Search
// T[] -> any
pure bool any( T )( in T[] args , in T target ){
foreach( arg ; args ){
if( arg == target ){return true;}
}
return false;
}
pure bool all( T )( in T[] args , in T cond ){
foreach( arg ; args ){
if( arg != cond ){return false;}
}
return true;
}
pure bool have( T )( in T[] args , in T[] target ... ){
bool[] found = new bool[](target.length);
foreach( arg ; args ){
foreach( i,t ; target ){
if( arg == t ){
found[i] = true;
}
}
}
foreach( f ; found ){
if( f == false ){return false;}
}
return true;
}
pure size_t count( T )( in T[] args , in T target ){
size_t c = 0;
foreach( arg ; args ){
c += ( arg == target ) ? 1 : 0 ;
}
return c;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
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 A = RD;
auto B = RD;
auto T = RD;
writeln((T / A) * B);
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.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;
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 X = RD!int;
auto Y = RD!int;
auto s = RD!string;
long ans;
foreach_reverse (i; 0..N)
{
if (N-1-i == X) break;
auto bit = N-1-i == Y ? '1' : '0';
if (s[i] != bit)
++ans;
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
void main()
{
string n = readln.chomp;
long total;
foreach (x; n)
{
total += x - '0';
}
max(total, (n[0] - '1') + 9 * (n.length - 1)).writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto S = readln.chomp;
int a, b;
foreach (i, c; S) {
if ((i%2 == 0 && c == '1') || (i%2 == 1 && c == '0')) ++a;
if ((i%2 == 0 && c == '0') || (i%2 == 1 && c == '1')) ++b;
}
writeln(min(a, b));
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
ulong MOD = 1_000_000_007;
ulong INF = 1_000_000_000_000;
alias Mame = Tuple!(long, "point", long, "number");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
long n, k, q;
scan(n, k, q);
auto point = new long[](n);
foreach (i; iota(q))
{
auto a = lread();
point[a - 1]++;
}
foreach (i; iota(n))
{
auto lost = (q - point[i]);
if (lost >= k)
writeln("No");
else
writeln("Yes");
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
long P = 10^^9 + 7;
int[2*10^^5+1] CS;
long[2*10^^5+1] MEMO;
void main()
{
auto N = readln.chomp.to!int;
foreach (i; 0..N) {
CS[i] = readln.chomp.to!int;
}
int[2*10^^5+1] acc, right;
foreach_reverse(ii, c; CS[0..N]) {
int i = ii.to!int;
if (!acc[c]) {
acc[c] = i;
} else {
right[i] = acc[c];
acc[c] = i;
}
}
foreach_reverse (i; 0..N) {
if (i == N-1) {
MEMO[i] = 1;
} else {
long r = MEMO[i+1];
if (right[i] && right[i] != i+1) r = (r + MEMO[right[i]]) % P;
MEMO[i] = r;
}
}
writeln(MEMO[0]);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
int n, m; scan(n, m);
int[][] ms;
for (int i = 0; i < m; i++) {
auto ss = readints;
ms ~= ss[1..$].map!(e => e - 1).array;
}
auto ps = readints;
int ans = 0;
for (int i = 0; i < (1 << n); i++) {
auto state = new bool[n];
for (int j = 0; j < n; j++) {
if (i & (1 << j)) state[j] = true;
}
bool ok = true;
for (int j = 0; j < m; j++) {
int p = 0;
foreach (k; ms[j]) {
if (state[k]) p++;
}
if (p % 2 != ps[j]) {
ok = false;
break;
}
}
if (ok) ans++;
}
writeln(ans);
}
|
D
|
import std.stdio, std.string, std.array, std.conv;
void main() {
int n = readln.chomp.to!int;
int[] a = readln.chomp.split.to!(int[]);
foreach_reverse (i; 0 .. n) {
a[i].write;
if (i != 0) " ".write;
else writeln;
}
}
|
D
|
import std.stdio;
import std.string;
import std.algorithm;
import std.conv;
void main(){
int[] a;
a = new int[](10);
foreach(i; 0..10){
a[i] = readln().chomp().to!int();
}
sort!("a > b")(a);
foreach(i; 0..3){
writeln(a[i]);
}
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
void main()
{
string s = chomp(readln()); // ??????????????????
int x = to!int(s.split(" ")[0]);
int y = to!int(s.split(" ")[1]);
write(x*y);
write(" ");
writeln(2*x+2*y);
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "damage", long, "cost");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
long a, b, c;
scan(a, b, c);
if ((a == b && a != c) || (a == c && a != b) || (b == c && a != b))
{
writeln("Yes");
}
else
{
writeln("No");
}
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
long n = lread();
string s = sread();
long max_same = 0;
foreach (i, e; s)
{
long[] lhs_alf = new long[26];
long[] rhs_alf = new long[26];
string lhs_str = s[0 .. i];
string rhs_str = s[i .. $];
input_to_array(lhs_str, lhs_alf);
input_to_array(rhs_str, rhs_alf);
long same = count_same(lhs_alf, rhs_alf);
max_same = max(same, max_same);
}
writeln(max_same);
}
void input_to_array(string s, long[] a)
{
long key;
foreach (i, e; s)
{
key = e - 'a';
a[key] = 1;
}
}
long count_same(long[] a, long[] b)
{
long count;
foreach (e; 0 .. a.length)
{
if(a[e] && b[e])
{
count++;
}
}
return count;
}
|
D
|
import std.stdio;
import std.array;
import std.string;
import std.conv;
int main()
{
import std.stdio;
auto input = split(chomp(readln()));
int a = to!int(input[0]);
int b = to!int(input[1]);
if (a <= b)
writeln(a);
else
writeln(a - 1);
return 0;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.algorithm, std.array;
auto solve(string s_)
{
auto NK = s_.split.map!(to!int)();
immutable N=NK[0], K=NK[1];
auto as = readln.split.map!(x=>x.to!int()-1).array();
auto g = new int[][N];
foreach(int i,v;as) if(i!=0) g[v]~=i;
int n=as[0]==0?0:1;
int dfs(int v, int k)
{
int m=K-1;
foreach(i;g[v]) m=min(m,dfs(i,k+1));
if(m==0 && v!=0)
{
if(as[v]!=0) ++n;
m=K;
}
return m-1;
}
dfs(0,0);
return n;
}
void main(){ for(string s; (s=readln.chomp()).length;) writeln(solve(s)); }
|
D
|
void main() {
problem();
}
void problem() {
auto A = scan!int;
auto B = scan!int;
auto C = scan!int;
auto K = scan!int;
int solve() {
int answer;
if (K <= A) return K;
if (K <= A+B) return A;
auto nokori = K - A - B;
return A - nokori;
}
solve().writeln;
}
// ----------------------------------------------
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
// -----------------------------------------------
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii;
import std.typecons, std.functional;
import std.algorithm, std.container;
void main()
{
auto N = scanElem;
foreach(i;0..N)
{
if(scanElem%2==1){
writeln("first");
return;
}
}
writeln("second");
}
class UnionFind{
UnionFind parent = null;
void merge(UnionFind a)
{
if(same(a)) return;
a.root.parent = this.root;
}
UnionFind root()
{
if(parent is null)return this;
return parent = parent.root;
}
bool same(UnionFind a)
{
return this.root == a.root;
}
}
void scanValues(TList...)(ref TList list)
{
auto lit = readln.splitter;
foreach (ref e; list)
{
e = lit.fornt.to!(typeof(e));
lit.popFront;
}
}
T[] scanArray(T = long)()
{
return readln.split.to!(long[]);
}
void scanStructs(T)(ref T[] t, size_t n)
{
t.length = n;
foreach (ref e; t)
{
auto line = readln.split;
foreach (i, ref v; e.tupleof)
{
v = line[i].to!(typeof(v));
}
}
}
T scanElem(T = long)()
{
char[] res;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
res ~= cast(char) c;
c = getchar;
}
return res.strip.to!T;
}
template fold(fun...) if (fun.length >= 1)
{
auto fold(R, S...)(R r, S seed)
{
static if (S.length < 2)
{
return reduce!fun(seed, r);
}
else
{
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
struct Factor
{
long n;
long c;
}
Factor[] factors(long n)
{
Factor[] res;
for (long i = 2; i ^^ 2 <= n; i++)
{
if (n % i != 0)
continue;
int c;
while (n % i == 0)
{
n = n / i;
c++;
}
res ~= Factor(i, c);
}
if (n != 1)
res ~= Factor(n, 1);
return res;
}
long[] primes(long n)
{
if(n<2)return [];
auto table = new long[n+1];
long[] res;
for(int i = 2;i<=n;i++)
{
if(table[i]==-1) continue;
for(int a = i;a<table.length;a+=i)
{
table[a] = -1;
}
res ~= i;
}
return res;
}
bool isPrime(long n)
{
if (n <= 1)
return false;
if (n == 2)
return true;
if (n % 2 == 0)
return false;
for (long i = 3; i ^^ 2 <= n; i += 2)
if (n % i == 0)
return false;
return true;
}
|
D
|
import std.stdio, 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 t = RD!int;
auto ans = new long[](t);
foreach (i; 0..t)
{
auto n = RD!int;
auto a = RDA;
auto b = new long[](n+1);
b[n] = long.max;
foreach_reverse (j; 0..n)
{
b[j] = min(b[j+1], a[j]);
}
long cnt;
foreach (j; 0..n)
{
if (a[j] > b[j+1])
++cnt;
}
ans[i] = cnt;
}
foreach (e; ans)
writeln(e);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm, std.array, std.range, std.container;
import std.math;
void main() {
auto N = readln.split[0].to!long;
auto A = readln.split[0].to!long;
auto B = readln.split[0].to!long;
auto C = readln.split[0].to!long;
auto D = readln.split[0].to!long;
auto E = readln.split[0].to!long;
auto M = min(A,B,C,D,E);
writeln(N%M == 0 ? N/M+4 : N/M+5);
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
auto S = sread();
char[100] ans;
long i;
foreach (c; S)
{
if (c == 'B')
{
i = max(0, i - 1);
continue;
}
ans[i++] = c;
}
writeln(ans[0 .. i]);
}
|
D
|
module app;
import core.bitop;
import std.algorithm;
import std.array;
import std.bigint;
import std.conv;
import std.stdio;
import std.string;
struct Input
{
int n;
string s, t;
}
void parseInput(T)(out Input input, T file)
{
with (file) with (input)
{
n = readln().strip().to!int;
auto array = readln().strip().split();
s = array[0];
t = array[1];
}
}
auto main2(Input* input)
{
foreach (i; 0..input.n)
{
write(input.s[i]);
write(input.t[i]);
}
}
void main()
{
Input input = void;
parseInput(input, stdin);
main2(&input);
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto s = readln.split.map!(to!int);
auto n = s[0];
auto q = s[1];
auto st = new SegmentTree!(int, (a,b)=>a+b, 0)(n);
while (q--) {
s = readln.split.map!(to!int);
auto t = s[0];
auto x = s[1];
auto y = s[2];
if (t == 0) {
st.add(x, y);
} else {
st.query(x, y).writeln;
}
}
}
class SegmentTree(T, alias op, T e) {
T[] table;
int size;
int offset;
this(int n) {
size = 1;
while (size <= n) size <<= 1;
size <<= 1;
table = new T[](size);
fill(table, e);
offset = size / 2;
}
void assign(int pos, T val) {
pos += offset;
table[pos] = val;
while (pos > 1) {
pos /= 2;
table[pos] = op(table[pos*2], table[pos*2+1]);
}
}
void add(int pos, T val) {
pos += offset;
table[pos] += val;
while (pos > 1) {
pos /= 2;
table[pos] = op(table[pos*2], table[pos*2+1]);
}
}
T query(int l, int r) {
return query(l, r, 1, 0, offset-1);
}
T query(int l, int r, int i, int a, int b) {
if (b < l || r < a) {
return e;
} else if (l <= a && b <= r) {
return table[i];
} else {
return op(query(l, r, i*2, a, (a+b)/2), query(l, r, i*2+1, (a+b)/2+1, b));
}
}
}
|
D
|
// 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 += ModInt(rhs).value;
if (val >= modulus) {
val -= modulus;
}
} else if (op == "-") {
auto r = ModInt(rhs);
if (val < r.value) {
val += modulus;
}
val -= r.value;
} else if (op == "*") {
val = val * ModInt(rhs).value % 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 = cin.next!int;
int[] h = cin.nextArray!int(n);
int[][] dp = new int[][](2, n + 2);
h ~= [0, 0];
dp[0] = int.max.repeat.take(n + 2).array;
dp[1] = int.max.repeat.take(n + 2).array;
dp[0][0] = dp[1][0] = 0;
foreach (i; 0 .. n - 1) {
dp[0][i + 1] = min(dp[1][i], dp[0][i]) + abs(h[i + 1] - h[i]);
dp[1][i + 2] = min(dp[1][i], dp[0][i]) + abs(h[i + 2] - h[i]);
}
writeln(min(dp[0][n - 1], dp[1][n - 1]));
}
|
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 AS = readln.split.to!(int[]);
AS = [0] ~ AS ~ 0;
int s;
foreach (i; 0..N+1) s += abs(AS[i] - AS[i+1]);
foreach (i; 1..N+1) {
writeln(s - abs(AS[i-1] - AS[i]) - abs(AS[i] - AS[i+1]) + abs(AS[i-1] - AS[i+1]));
}
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
// dfmt on
void main()
{
long N, A, B;
scan(N, A, B);
auto S = sread();
long cnt;
long b;
foreach (i, c; S)
{
switch (c)
{
case 'a':
if (cnt < A + B)
{
cnt++;
writeln("Yes");
}
else
writeln("No");
break;
case 'b':
if (cnt < A + B && b < B)
{
b++;
cnt++;
writeln("Yes");
}
else
writeln("No");
break;
case 'c':
writeln("No");
break;
default:
break;
}
}
}
|
D
|
import std.stdio, std.conv, std.string, std.math, std.regex, std.range, std.ascii, std.algorithm;
void main(){
auto ip = readln.split.to!(int[]), A=ip[0], B=ip[1];
if(A<6) writeln(0);
else if(A<13) writeln(B/2);
else writeln(B);
}
|
D
|
import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;
import std.container;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
class Node{
bool[Node] adjnodeset;
long id;
long value1 = -1;
long value2 = -1;
this(long id){
this.id = id;
}
void add(Node nd){
this.adjnodeset[nd] = 1;
nd.adjnodeset[this] = 1;
}
void extend1(){
debug writeln("(", id, ")");
foreach(nd; adjnodeset.keys){
if(nd.value1 < 0){
nd.value1 = this.value1 + 1;
nd.extend1();
}
}
}
void extend2(){
debug writeln("(", id, ")");
foreach(nd; adjnodeset.keys){
if(nd.value2 < 0){
nd.value2 = this.value2 + 1;
nd.extend2();
}
}
}
string dump(){
return id.to!string ~ "[" ~ value1.to!string ~ " " ~ value2.to!string ~ "]";
}
}
bool f;
void main(){
long n = read.to!long;
Node[long] nodes;
foreach(i; 0 .. n) nodes[i + 1] = new Node(i + 1);
foreach(i; 0 .. n - 1){
long a = read.to!long;
long b = read.to!long;
nodes[a].add(nodes[b]);
}
nodes[1].value1 = 0, nodes[1].extend1();
nodes[n].value2 = 0, nodes[n].extend2();
long count;
foreach(nd; nodes) if(nd.value1 <= nd.value2) count += 1;
string ans;
if(count > n / 2) ans = "Fennec";
else ans = "Snuke";
ans.writeln;
debug nodes.values.map!(x => x.dump).join(" ").writeln;
}
|
D
|
void main() {
string[] s = readln.split;
writeln(s[0][0], s[1][0], s[2][0]);
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
long N, A, B;
scan(N, A, B);
writeln(min(N * A, B));
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
int a, b;
scan(a, b);
if (a == 1) a = 14;
if (b == 1) b = 14;
if (a < b) {
writeln("Bob");
}
else if (a > b ) {
writeln("Alice");
}
else {
writeln("Draw");
}
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import core.bitop, 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 H, W;
scan(H, W);
auto S = new string[](H);
foreach (i; 0 .. H)
S[i] = sread();
foreach (h; 0 .. H)
foreach (w; 0 .. W)
if (S[h][w] == '#')
{
if (0 < h && S[h - 1][w] == '#')
continue;
if (h + 1 < H && S[h + 1][w] == '#')
continue;
if (0 < w && S[h][w - 1] == '#')
continue;
if (w + 1 < W && S[h][w + 1] == '#')
continue;
writeln("No");
return;
}
writeln("Yes");
}
|
D
|
// import 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;
// }}}
// tbh.scanner {{{
class Scanner {
import std.stdio;
import std.conv : to;
import std.array : split;
import std.string : chomp;
private File file;
private dchar[][] str;
private uint idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
private dchar[] next() {
if (idx < str.length) {
return str[idx++];
}
dchar[] s;
while (s.length == 0) {
s = file.readln.chomp.to!(dchar[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(uint len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
}
// }}}
void main() {
auto cin = new Scanner;
long n = cin.next!long, m = cin.next!long;
if (n == 1 && m == 1) {
writeln(1);
} else if (min(n, m) == 1) {
writeln(n * m - 2);
} else {
writeln(n * m - n * 2 - m * 2 + 4);
}
}
|
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;
int generate()
{
Mt19937 gen;
string str = __DATE__ ~ __TIME__;
uint hash;
foreach (c; str)
{
hash = (hash * 9) + c;
}
gen.seed(hash);
return uniform(1, 1_000_000_000 + 1, gen);
}
enum SUBMIT = true;
bool test(long n)
{
static if (SUBMIT)
{
return query(n);
}
else
{
enum N = generate();
pragma(msg, N);
enum strN = N.to!string;
string strn = n.to!string;
return (n <= N && strn <= strN) || (n > N && strn >= strN);
}
}
bool query(T)(T n)
{
writeln("? ", n);
stdout.flush;
auto ans = readln;
return ans[0] == 'Y';
}
void main()
{
long l = 1;
long zero = 1;
for (; zero.test && zero <= 10_000_000_000L; zero *= 10, ++l)
{
}
--l;
zero /= 10;
long l2 = 1;
for (long z = 2;;)
{
if (z.test)
{
break;
}
z *= 10;
++l2;
}
if (l == 11)
{
long ans = 1;
foreach (i; 1 .. l2)
{
ans *= 10;
}
writeln("! ", ans);
stdout.flush;
return;
}
long left = zero;
long right = zero * 10;
while (right - left > 1)
{
long mid = (left + right) / 2;
bool res = test(10 * mid);
if (res)
{
right = mid;
}
else
{
left = mid;
}
}
writeln("! ", right);
stdout.flush;
}
|
D
|
void main()
{
long n, m;
rdVals(n, m);
Bridge[] b = m.rdCol!Bridge;
foreach (i; 0 .. m)
{
b[i].from -= 1, b[i].to -= 1;
}
long cnt;
foreach (i; 0 .. m)
{
auto tree = new UnionFind(n);
foreach (j; 0 .. m)
{
if (j == i) continue;
tree.unite(b[j].from, b[j].to);
}
if (!tree.same(b[i].from, b[i].to)) ++cnt;
}
cnt.writeln;
}
struct Bridge
{
long from, to;
}
struct UnionFind
{
long[] par;
this(long n)
{
par = new long[n];
foreach (i; 0 .. n)
{
par[i] = i;
}
}
long root(long x)
{
if (par[x] == x) return x;
else return par[x] = root(par[x]);
}
bool same(long x, long y)
{
return root(x) == root(y);
}
void unite(long x, long y)
{
x = root(x), y = root(y);
if (x == y) return;
par[x] = y;
}
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.string;
import std.stdio;
import std.algorithm;
import std.conv;
import std.typecons;
void main() {
long[Tuple!(immutable(char), size_t)] dp;
long[char] checks;
auto s = readln.chomp;
auto t = readln.chomp;
foreach(c; s) {
checks[c] = 0;
}
bool check = true;
foreach(c; t) {
if (c !in checks) {
check = false;
}
}
if (check) {
size_t i;
foreach(c; t) {
auto key = tuple(c, i%s.length);
if (key in dp) {
i += dp[key];
}
else {
size_t j;
for(;j < s.length; ++j) {
auto p = (i + j) % s.length;
if (s[p] == c) break;
}
i += j+1;
dp[key] = j+1;
}
}
write(i);
}
else {
write(-1);
}
}
|
D
|
void main()
{
long n, k, l;
rdVals(n, k, l);
auto road = UnionFind(n);
foreach (i; 0 .. k)
{
long p, q;
rdVals(p, q);
--p, --q;
road.unite(p, q);
}
auto rail = UnionFind(n);
foreach (i; 0 .. l)
{
long r, s;
rdVals(r, s);
--r, --s;
rail.unite(r, s);
}
long[Pair] cnt;
foreach (i; 0 .. n)
{
++cnt[Pair(road.root(i), rail.root(i))];
}
foreach (i; 0 .. n)
{
cnt[Pair(road.root(i), rail.root(i))].write;
if (i == n - 1) writeln;
else " ".write;
}
}
struct Pair
{
long a, b;
}
struct UnionFind
{
long[] par, cnt;
this(long n)
{
par.length = n, cnt.length = n;
foreach (i; 0 .. n)
{
par[i] = i;
}
cnt[] = 1L;
}
long root(long x)
{
if (par[x] == x) return x;
else return par[x] = root(par[x]);
}
bool same(long x, long y)
{
return root(x) == root(y);
}
void unite(long x, long y)
{
x = root(x), y = root(y);
if (x == y) return;
if (cnt[x] > cnt[y]) swap(x, y);
cnt[y] += cnt[x];
par[x] = y;
}
long size(long x)
{
return cnt[root(x)];
}
}
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
|
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop;
// dfmt on
void main()
{
long X, Y;
scan(X, Y);
long ans;
while (X <= Y)
ans++, X += X;
writeln(ans);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
auto a = readints;
int ab = a[0], bc = a[1];
int area = ab * bc / 2;
writeln(area);
}
|
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() {
auto N = readln.chomp.to!int;
auto X = new int[][](10, 2);
foreach (i; 0..10) X[i][0] = 0;
foreach (i; 0..10) X[i][1] = 1;
foreach (i; 0..N) {
auto s = readln.split;
auto op = s[0];
auto n = s[1].to!int;
foreach (j; 0..10) {
bool b = (n & (1 << j)) != 0;
if (op[0] == '|') {
X[j][0] |= b;
X[j][1] |= b;
} else if (op[0] == '&') {
X[j][0] &= b;
X[j][1] &= b;
} else {
X[j][0] ^= b;
X[j][1] ^= b;
}
}
}
int opor = 0;
int opand = 0;
int opxor = 0;
foreach (i; 0..10) {
if (X[i][0] == 0 && X[i][1] == 0) {
} else if (X[i][0] == 0 && X[i][1] == 1) {
opand |= (1 << i);
} else if (X[i][0] == 1 && X[i][1] == 0) {
opand |= (1 << i);
opxor |= (1 << i);
} else {
opor |= (1 << i);
opand |= (1 << i);
}
}
writeln(3);
writeln("| ", opor);
writeln("& ", opand);
writeln("^ ", opxor);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math;
bool solve(int s, int g, int[] ns) {
auto N = ns.length;
auto DP = new bool[][](N+1, 8000*2+1);
void sv(int i, int n) {
if (!DP[i][n]) {
DP[i][n] = true;
if (i == N) return;
sv(i+1, n+ns[i]);
sv(i+1, n-ns[i]);
}
}
sv(0, s+8000);
return DP[N][g+8000];
}
void main()
{
int[] xs, ys;
int n, t;
auto s = readln.chomp;
foreach (c; s) {
if (c == 'T') {
if (n != 0) {
if (t%2 == 0) {
xs ~= n;
} else {
ys ~= n;
}
n = 0;
}
++t;
} else {
++n;
}
}
if (n != 0) {
if (t%2 == 0) {
xs ~= n;
} else {
ys ~= n;
}
}
int x;
if (s[0] != 'T') {
x = xs[0];
xs = xs[1..$];
}
auto xy = readln.split.to!(int[]);
auto gx = xy[0];
auto gy = xy[1];
writeln(solve(x, gx, xs) && solve(0, gy, ys) ? "Yes" : "No");
}
|
D
|
void main()
{
dchar[] s = readln.chomp.to!(dchar[]);
dchar[] t = s.dup;
reverse(t);
long cnt;
foreach (i; 0 .. s.length/2)
{
if (s[i] != t[i]) ++cnt;
}
cnt.writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.string, std.conv;
void main()
{
while(true)
{
int n=readln.chomp.to!int;
if(!n)break;
int num;
for(int i=1; i<n; ++i)
{
int sum;
for(int j=i; j<n; ++j)
{
sum+=j;
if(sum == n) ++num;
else if(sum > n) break;
}
}
num.writeln;
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
void main() {
int sum = 0;
foreach(int i; 0..5) {
sum += max(40, readln.chomp.to!int);
}
writeln(sum/5);
}
|
D
|
import std;
void main(){
int n, k;
int[] as;
scanf("%d %d\n", &n, &k);
auto mem = new bool[n];
foreach(_i; 0..k) {
int d;
scanf("%d", &d);
foreach (_j; 0..d) {
int a;
scanf("%d", &a);
mem[a-1] |= true;
}
}
mem.count!"!a".writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
void main()
{
auto S_ = readln.chomp.to!(wchar[]);
auto T = readln.chomp.to!(wchar[]);
wchar[] S;
foreach_reverse (i; 0..S_.length - T.length + 1) {
auto s = S_.dup;
foreach_reverse (j; 0..T.length) {
if (S_[i+j] == '?')
s[i+j] = T[j];
else if (S_[i+j] != T[j])
goto loop;
}
S = s;
break;
loop:
}
if (S) {
foreach (ref c; S) if (c == '?') c = 'a';
writeln(S);
} else {
writeln("UNRESTORABLE");
}
}
|
D
|
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.complex;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
auto readInts() {
return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
return readInts()[0];
}
auto readLongs() {
return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
return readLongs()[0];
}
void readlnTo(T...)(ref T t) {
auto s = readln().split();
assert(s.length == t.length);
foreach(ref ti; t) {
ti = s[0].to!(typeof(ti));
s = s[1..$];
}
}
const real eps = 1e-10;
const long p = 1_000_000_000 + 7;
void main(){
auto s = readln();
writeln(s.length >= 4 && s[0..4] == "YAKI" ? "Yes" : "No");
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
auto S = readln.chomp;
if (N%2 == 1) {
writeln("No");
return;
}
foreach (i; 0..N/2) {
if (S[i] != S[N/2+i]) {
writeln("No");
return;
}
}
writeln("Yes");
}
|
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, k; readV(n, k);
int[] d; readA(k, d);
auto b = new bool[](10);
foreach (di; d) b[di] = true;
auto isOK(int n)
{
while (n > 0) {
if (b[n%10]) return false;
n /= 10;
}
return true;
}
foreach (i; n..100000)
if (isOK(i)) {
writeln(i);
return;
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main(){
while(true){
auto s = readln();
if(stdin.eof()) break;
real w = to!real(chomp(s));
if(w <= 48.0) writeln("light fly");
else if(w <= 51.0) writeln("fly");
else if(w <= 54.0) writeln("bantam");
else if(w <= 57.0) writeln("feather");
else if(w <= 60.0) writeln("light");
else if(w <= 64.0) writeln("light welter");
else if(w <= 69.0) writeln("welter");
else if(w <= 75.0) writeln("light middle");
else if(w <= 81.0) writeln("middle");
else if(w <= 91.0) writeln("light heavy");
else writeln("heavy");
}
}
|
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 n; readV(n);
int a; readV(a);
writeln(n*n-a);
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
const N = readln.chomp.to!long;
iota(1,N+1).filter!(x => x < 10 || (100 <= x && x < 1000) || (10000 <= x && x < 100000)).walkLength.writeln;
}
|
D
|
import std.stdio, std.math, std.conv, std.array, std.string, std.algorithm;
void main() {
uint c = to!uint(readln().chomp);
for (uint i = 0; i < c; ++i) {
double[] dx = map!(to!double)(readln().chomp.split).array;
double[] ap = dx[0..2];
double[] bp = dx[3..5];
double ar = dx[2];
double br = dx[5];
double d = dist(ap, bp);
if (d < (ar-br)^^2) {
if (br < ar) {
writeln(2);
} else {
writeln(-2);
}
} else if (d - (ar+br)^^2 < 1.0e-10) {
writeln(1);
} else {
writeln(0);
}
}
}
double dist(double[] ax, double[] bx) {
return (ax[0]-bx[0])^^2 + (ax[1]-bx[1])^^2;
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
import std.typecons;
import std.bigint;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.container.deque;
int main() {
auto sc = new Scanner(stdin);
int n, m, k;
sc.read(n, m, k); m++;
int[] a = new int[n];
a.each!((ref x) => sc.read(x));
long[] dp = a.map!(to!long).array;
long[] ndp = new long[n];
auto deq = Deque!(int, false).make();
auto p = deq.p;
foreach (int ph; 2..k+1) {
ndp[] = -(10L^^18);
deq.clear();
foreach (int i; 0..n) {
if (deq.length && deq[0] == i-m) {
p.removeFront();
}
if (deq.length) {
ndp[i] = dp[deq[0]] + 1L * ph * a[i];
}
while (deq.length && dp[(*p)[p.length-1]] <= dp[i]) {
p.removeBack();
}
p.insertBack(i);
}
swap(dp, ndp);
}
writeln(dp.fold!max);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
//fold(for old compiler)
static if (__VERSION__ <= 2070) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
unittest {
import std.stdio;
auto l = [1, 2, 3, 4, 5];
assert(l.fold!"a+b"(10) == 25);
}
}
version (X86) static if (__VERSION__ < 2071) {
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/deque.d */
// module dcomp.container.deque;
struct Deque(T, bool hasNull = true) {
import core.exception : RangeError;
import core.memory : GC;
import std.range : ElementType, isInputRange;
import std.traits : isImplicitlyConvertible;
struct Payload {
T *d;
size_t st, length, cap;
@property bool empty() const { return length == 0; }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {
version(assert) if (length <= i) throw new RangeError();
return d[(st+i >= cap) ? (st+i-cap) : st+i];
}
private void expand() {
import std.algorithm : max;
assert(length == cap);
auto nc = max(size_t(4), 2*cap);
T* nd = cast(T*)GC.malloc(nc * T.sizeof);
foreach (i; 0..length) {
nd[i] = this[i];
}
d = nd; st = 0; cap = nc;
}
void clear() {
st = length = 0;
}
void insertFront(T v) {
if (length == cap) expand();
if (st == 0) st += cap;
st--; length++;
this[0] = v;
}
void insertBack(T v) {
if (length == cap) expand();
length++;
this[length-1] = v;
}
void removeFront() {
assert(!empty, "Deque.removeFront: Deque is empty");
st++; length--;
if (st == cap) st = 0;
}
void removeBack() {
assert(!empty, "Deque.removeBack: Deque is empty");
length--;
}
}
struct RangeT(A) {
alias T = typeof(*(A.p));
alias E = typeof(A.p.d[0]);
T *p;
size_t a, b;
@property bool empty() const { return b <= a; }
@property size_t length() const { return b-a; }
@property RangeT save() { return RangeT(p, a, b); }
@property RangeT!(const A) save() const {
return typeof(return)(p, a, b);
}
alias opDollar = length;
@property ref inout(E) front() inout { return (*p)[a]; }
@property ref inout(E) back() inout { return (*p)[b-1]; }
void popFront() {
version(assert) if (empty) throw new RangeError();
a++;
}
void popBack() {
version(assert) if (empty) throw new RangeError();
b--;
}
ref inout(E) opIndex(size_t i) inout { return (*p)[i]; }
RangeT opSlice() { return this.save; }
RangeT opSlice(size_t i, size_t j) {
version(assert) if (i > j || a + j > b) throw new RangeError();
return typeof(return)(p, a+i, a+j);
}
RangeT!(const A) opSlice() const { return this.save; }
RangeT!(const A) opSlice(size_t i, size_t j) const {
version(assert) if (i > j || a + j > b) throw new RangeError();
return typeof(return)(p, a+i, a+j);
}
}
alias Range = RangeT!Deque;
alias ConstRange = RangeT!(const Deque);
alias ImmutableRange = RangeT!(immutable Deque);
Payload *p;
private void I() { if (hasNull && !p) p = new Payload(); }
private void C() const { version(assert) if (!p) throw new RangeError(); }
//some value
this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) {I;
p = new Payload();
foreach (v; values) {
insertBack(v);
}
}
//range
this(Range)(Range r)
if (isInputRange!Range &&
isImplicitlyConvertible!(ElementType!Range, T) &&
!is(Range == T[])) {I;
p = new Payload();
foreach (v; r) {
insertBack(v);
}
}
static Deque make() {
Deque que;
que.p = new Payload();
return que;
}
@property private bool hasPayload() const { return (!hasNull || p); }
@property bool empty() const { return (!hasPayload || p.empty); }
@property size_t length() const { return (hasPayload ? p.length : 0); }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; }
ref inout(T) front() inout {C; return (*p)[0]; }
ref inout(T) back() inout {C; return (*p)[$-1]; }
void clear() { if (p) p.clear(); }
void insertFront(T v) {I; p.insertFront(v); }
void insertBack(T v) {I; p.insertBack(v); }
void removeFront() {C; p.removeFront(); }
void removeBack() {C; p.removeBack(); }
Range opSlice() {I; return Range(p, 0, length); }
}
unittest {
import std.algorithm : equal;
import std.range.primitives : isRandomAccessRange;
import std.container.util : make;
auto q = make!(Deque!int);
assert(isRandomAccessRange!(typeof(q[])));
//insert,remove
assert(equal(q[], new int[](0)));
q.insertBack(1);
assert(equal(q[], [1]));
q.insertBack(2);
assert(equal(q[], [1, 2]));
q.insertFront(3);
assert(equal(q[], [3, 1, 2]) && q.front == 3);
q.removeFront;
assert(equal(q[], [1, 2]) && q.length == 2);
q.insertBack(4);
assert(equal(q[], [1, 2, 4]) && q.front == 1 && q.back == 4 && q[$-1] == 4);
q.insertFront(5);
assert(equal(q[], [5, 1, 2, 4]));
//range
assert(equal(q[][1..3], [1, 2]));
assert(equal(q[][][][], q[]));
//const range
const auto rng = q[];
assert(rng.front == 5 && rng.back == 4);
//reference type
auto q2 = q;
q2.insertBack(6);
q2.insertFront(7);
assert(equal(q[], q2[]) && q.length == q2.length);
//construct with make
auto a = make!(Deque!int)(1, 2, 3);
auto b = make!(Deque!int)([1, 2, 3]);
assert(equal(a[], b[]));
}
unittest {
import std.algorithm : equal;
import std.range.primitives : isRandomAccessRange;
import std.container.util : make;
auto q = make!(Deque!int);
q.clear();
assert(equal(q[], new int[0]));
foreach (i; 0..100) {
q.insertBack(1);
q.insertBack(2);
q.insertBack(3);
q.insertBack(4);
q.insertBack(5);
assert(equal(q[], [1,2,3,4,5]));
q.clear();
assert(equal(q[], new int[0]));
}
}
unittest {
Deque!int a;
Deque!int b;
a.insertFront(2);
assert(b.length == 0);
}
unittest {
import std.algorithm : equal;
import std.range : iota;
Deque!int a;
foreach (i; 0..100) {
a.insertBack(i);
}
assert(equal(a[], iota(100)));
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
if (f.eof) return false;
line = lineBuf[];
f.readln(line);
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
//string or char[10] etc
//todo optimize
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else {
auto buf = line.split.map!(to!E).array;
static if (isStaticArray!T) {
//static
assert(buf.length == T.length);
}
x = buf;
line.length = 0;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
fout.writeln("1 2 3");
fout.writeln("ab cde");
fout.writeln("1.0 1.0 2.0");
fout.close;
Scanner sc = new Scanner(File(fileName, "r"));
int a;
int[2] b;
char[2] c;
string d;
double e;
double[] f;
sc.read(a, b, c, d, e, f);
assert(a == 1);
assert(equal(b[], [2, 3]));
assert(equal(c[], "ab"));
assert(equal(d, "cde"));
assert(e == 1.0);
assert(equal(f, [1.0, 2.0]));
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File, writeln;
import std.datetime;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
foreach (i; 0..1_000_000) {
fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2);
}
fout.close;
writeln("Scanner Speed Test(3*1,000,000 int)");
StopWatch sw;
sw.start;
Scanner sc = new Scanner(File(fileName, "r"));
foreach (i; 0..500_000) {
int a, b, c;
sc.read(a, b, c);
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
foreach (i; 500_000..700_000) {
int[3] d;
sc.read(d);
int a = d[0], b = d[1], c = d[2];
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
foreach (i; 700_000..1_000_000) {
int[] d;
sc.read(d);
assert(d.length == 3);
int a = d[0], b = d[1], c = d[2];
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
writeln(sw.peek.msecs, "ms");
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;
immutable long MOD = 10^^9 + 7;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto K = s[1];
auto Comb = new Combination(100000);
foreach (i; 1..K+1) {
long x = Comb.comb(N-K+1, i);
long y = Comb.comb(K-1, i -1);
writeln(x * y % MOD);
}
}
class Combination {
long[] modinv;
long[] f_mod;
long[] f_modinv;
this(int size) {
modinv = new long[](size);
modinv[0] = modinv[1] = 1;
foreach(i; 2..size) {
modinv[i] = modinv[MOD.to!int % i] * (MOD - MOD / i) % MOD;
}
f_mod = new long[](size);
f_modinv = new long[](size);
f_mod[0] = f_mod[1] = 1;
f_modinv[0] = f_modinv[1] = 1;
foreach(i; 2..size) {
f_mod[i] = (i * f_mod[i-1]) % MOD;
f_modinv[i] = (modinv[i] * f_modinv[i-1]) % MOD;
}
}
long comb(long n, long k) {
if (n < k) return 0;
return f_mod[n.to!int] * f_modinv[(n-k).to!int] % MOD * f_modinv[k.to!int] % MOD;
}
}
long powmod(long a, long x, long m) {
long ret = 1;
while (x) {
if (x % 2) ret = ret * a % m;
a = a * a % m;
x /= 2;
}
return ret;
}
|
D
|
/+ dub.sdl:
name "B"
dependency "dunkelheit" version=">=0.9.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
int[] solve(int n) {
if (n == 3) return [2, 5, 63];
int[] l = [2, 4];
n -= 2;
foreach (i; 0..2500) {
if (n < 2) break;
l ~= 3 + i*12;
l ~= 9 + i*12;
n -= 2;
}
if (n % 3 == 2) {
l = l[0..$-2];
n += 2;
}
int s = 6;
while (n >= 3) {
l ~= s; s += 2;
l ~= s; s += 2;
l ~= s; s += 2;
n -= 3;
}
if (n == 1) {
l ~= s;
}
return l;
}
int main() {
Scanner sc = new Scanner(stdin);
scope(exit) assert(!sc.hasNext);
int n;
sc.read(n);
writeln(solve(n).map!(to!string).join(" "));
return 0;
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */
// module dkh.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
void read(Args...)(auto ref Args args) {
import std.exception;
static if (args.length != 0) {
enforce(readSingle(args[0]));
read(args[1..$]);
}
}
bool hasNext() {
return succ();
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
|
D
|
import std.stdio, std.conv, std.string, std.algorithm, std.range;
void main() {
auto N = readln.split[0].to!int;
auto A = new int[N];
auto B = readln.split.to!(int[]);
A[0] = B[0];
A[$-1] = B[$-1];
for(auto i = 1; i < A.length - 1; i++) {
A[i] = min(B[i-1], B[i]);
}
A.reduce!"a+b"().writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto S = readln.chomp.to!(char[]);
auto T = readln.chomp.to!(char[]);
int r;
foreach (i; 0..3) if (S[i] == T[i]) ++r;
writeln(r);
}
|
D
|
void main()
{
long n = rdElem;
string[] s = new string[n];
long[] t = new long[n];
foreach (i; 0 .. n)
{
string[] tmp = rdRow!string;
s[i] = tmp[0];
t[i] = tmp[1].to!long;
}
string x = rdStr;
long result = long.min;
foreach (i; 0 .. n)
{
result += t[i];
if (s[i] == x) result = 0;
}
result.writeln;
}
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
|
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop;
// dfmt on
void main()
{
long R, G, B, N;
scan(R, G, B, N);
long ans;
foreach (r; 0 .. N / R + 1)
{
long M = N - r * R;
foreach (g; 0 .. M / G + 1)
{
if ((M - g * G) % B == 0)
ans++;
}
}
writeln(ans);
}
|
D
|
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container;
import std.numeric, std.math;
import core.bitop;
T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
long mod = pow(10, 9) + 7;
long moda(long x, long y) { return (x + y) % mod; }
long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; }
long modm(long x, long y) { return (x * y) % mod; }
void main()
{
auto N = RD!ulong;
auto K = RD!ulong;
auto A = RDR.split.to!(ulong[]);
ulong[64] bitCnt;
foreach (e; A)
{
foreach (i; 0..64)
{
if (e & (cast(ulong)1 << i)) ++bitCnt[i];
}
}
ulong x;
ulong ans;
foreach_reverse (i; 0..64)
{
if (bitCnt[i] > N / 2) continue;
ulong bit = cast(ulong)1 << i;
if (x + bit <= K) x += bit;
}
foreach (e; A)
{
ans += e ^ x;
}
writeln(ans);
stdout.flush();
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
import std.array;
int n; rd(n);
long[int] freq;
auto y=new int[](n);
auto ng=new bool[](n);
foreach(i; 0..n){
auto s=readln.chomp.to!(char[]);
char[] st;
foreach(c; s){
if(c=='('){
st~='(';
}else{
if(st.length>0){
if(st.back=='(') st=st[0..($-1)];
else st~=')';
}else{
st~=')';
}
}
}
int x=0;
foreach(c; st){
if(c=='(') x++;
else x--;
}
if(st.length>=2 && (st[0]==')' && st[$-1]=='(')){ng[i]=true; continue;}
y[i]=x;
if(x in freq) freq[x]++;
else freq[x]=1;
hell:;
}
long tot=0;
foreach(i; 0..n)if(ng[i]==false){
if(y[i]>=0 && (-y[i] in freq)){
tot+=freq[-y[i]];
}
// writeln(tot);
}
writeln(tot);
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x) e=l[i].to!(typeof(e));
}
|
D
|
import std.stdio;
import std.algorithm;
void main()
{
int n;
scanf("%d", &n);
bool[] used = new bool[n];
char[] gender = new char[n];
int[] from = new int[n];
int[] to = new int[n];
for (int i = 0; i < n; i++) {
char[10] s;
scanf("%s%d%d", s.ptr, &from[i], &to[i]);
gender[i] = s[0];
}
int res = 0;
for (int i = 1; i <= 366; i++) {
char[2] genders = ['F', 'M'];
int[2] found = [0, 0];
for (int j = 0; j < 2; j++) {
for (int k = 0; k < n; k++) {
if (!used[k] && gender[k] == genders[j] && from[k] <= i && to[k] >= i) {
found[j]++;
}
}
}
res = max(res, min(found[0], found[1]));
}
printf("%d\n", 2 * res);
}
|
D
|
// Cheese-Cracker: cheese-cracker.github.io
import std.stdio, std.conv, std.functional, std.string, std.algorithm;
import std.container, std.range, std.typecons, std.numeric, std.math, std.random;
alias ll = long;
alias rbt = redBlackTree;
alias tup = Tuple!(long, long);
static string[] inp;
T rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}
T[] rdarr(T = long)(T fix = 0){ auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
void show(A...)(A a) { debug{ foreach(t; a){ write(t, "| "); } writeln; } }
/*******************************It's a Me, Mario!*******************************************/
void play(){
int n, q;
n = rd!int;
q = rd!int;
ll[] arr = rdarr;
if(n == 1){ writeln(arr[0]); return;}
ll[] res;
bool side = (arr[1] > arr[0]), nside;
if(!side){ res ~= arr[0]; }
foreach(i; 1..n){
if(arr[i] > arr[i-1]){
nside = 1;
}else{
nside = 0;
}
if(nside != side){
res ~= arr[i-1];
side = nside;
}
}
if(side){ res ~= arr[n-1]; }
/* writeln(res); */
ll pow = 0;
foreach(i; 0..res.length){
if(i % 2 == 0){
pow += res[i];
}else if(i != res.length-1){
pow -= res[i];
}
}
writeln(pow);
}
int main(){
long t = 1;
t = rd; // Toggle!
while(t--) play(); // Let's play!
stdout.flush;
return 0;
}
|
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 t = RD!int;
auto ans = new bool[](t);
foreach (i; 0..t)
{
auto a = RD!int;
auto b = RD!int;
auto x = min(a, b);
auto y = cast(long)max(a, b);
auto arr = new bool[](x);
foreach (j; 1..10^^5)
{
auto z = (y*j) % x;
arr[cast(int)z] = true;
}
bool ok = true;
foreach (j; 0..arr.length)
{
if (arr[j] == false)
{
ok = false;
break;
}
}
ans[i] = ok;
}
foreach (e; ans)
{
writeln(e ? "Finite" : "Infinite");
}
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto T = readln.chomp.to!int;
foreach (_; 0..T) {
auto ad = readln.split.to!(long[]);
auto A = ad[0];
auto B = ad[1];
auto C = ad[2];
auto D = ad[3];
if (A <= B) {
writeln(B);
continue;
}
auto e = C - D;
if (e <= 0) {
writeln(-1);
continue;
}
A -= B;
auto f = (A + e - 1) / e;
writeln(B + C * f);
}
}
|
D
|
module _;
void main() {
import std.stdio;
import std.string, std.conv;
int n = readln.strip.to!int;
auto s = readln.strip;
bool check(int limit, ref string co, bool doco) {
int r = 0, b = 0;
foreach(ch; s) {
if (ch == '(') {
if (r < limit) {
r++;
if (doco) co ~= '0';
} else if (b < limit) {
b++;
if (doco) co ~='1';
} else {
return false;
}
} else {
if (r > 0) {
r--;
if (doco) co ~= '0';
} else if (b > 0) {
b--;
if (doco) co ~='1';
} else {
// impossible
return false;
}
}
}
return true;
}
string ans = "";
int l = 0, r = n;
while (l < r-1) {
int mid = (l+r)/2;
if (check(mid, ans, false)) {
r = mid;
} else {
l = mid;
}
}
check(r, ans, true);
writeln(ans);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.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 T = RD!int;
foreach (i; 0..T)
{
auto n = RD!int;
auto s = RD!int;
auto t = RD!int;
auto x = min(s, t);
writeln(n - x + 1);
}
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.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 m = RD!int;
auto A = new int[][](n, m);
auto B = new int[][](n, m);
foreach (i; 0..n)
{
A[i] = RDA!int;
}
long[2][] ans;
foreach (y; 0..n-1)
{
foreach (x; 0..m-1)
{
if (A[y][x] == 1)
{
if (A[y+1][x] == 1 && A[y][x+1] == 1 && A[y+1][x+1] == 1)
{
ans ~= [y+1, x+1];
B[y][x] = 1;
B[y+1][x] = 1;
B[y][x+1] = 1;
B[y+1][x+1] = 1;
}
}
}
}
bool ok = true;
foreach (y; 0..n)
{
foreach (x; 0..m)
{
if (A[y][x] != B[y][x])
{
ok = false;
}
}
}
if (ok)
{
writeln(ans.length);
foreach (e; ans)
writeln(e[0], " ",e[1]);
}
else
{
writeln(-1);
}
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(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto k = RD!int;
auto s = RD!string;
if (k == 0)
ans[ti] = true;
else if (n <= k*2)
continue;
else
{
int l, r = n-1;
int cnt;
while (l < r)
{
if (s[l] != s[r])
break;
++l; --r;
++cnt;
}
ans[ti] = cnt >= k;
}
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush;
debug readln;
}
|
D
|
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.complex;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
auto readInts() {
return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
return readInts()[0];
}
auto readLongs() {
return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
return readLongs()[0];
}
void readlnTo(T...)(ref T t) {
auto s = readln().split();
assert(s.length == t.length);
foreach(ref ti; t) {
ti = s[0].to!(typeof(ti));
s = s[1..$];
}
}
const real eps = 1e-10;
const long p = 1_000_000_000 + 7;
void main(){
long n, a, b, c, d;
readlnTo(n, a, b, c, d);
foreach(i; iota(n)) {
auto pmax = d * i;
auto pmin = c * i;
auto mmax = d * (n-i-1);
auto mmin = c * (n-i-1);
auto diff = b-a;
if(pmin - mmax <= diff && diff <= pmax - mmin) {
writeln("YES");
return;
}
}
writeln("NO");
}
|
D
|
import std;
long calc(long n) {
long sum(long n) { return n * (n + 1) / 2; }
return sum(n) - 3 * sum(n/3) - 5 * sum(n/5) + 15 * sum(n/15);
}
void main() {
int n; scan(n);
writeln(calc(n));
}
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 = "%.15f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto M = RD;
auto K = RD;
bool ans;
foreach (i; 0..N+1)
{
foreach (j; 0..M+1)
{
auto x = i * M + j * N - (i * j * 2);
if (x == K)
ans = true;
}
}
writeln(ans ? "Yes" : "No");
stdout.flush();
debug readln();
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}}
void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}}
void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}}
void main()
{
string a, b; readV(a, b);
auto n = (a ~ b).to!int;
writeln(n.nsqrt^^2 == n ? "Yes" : "No");
}
pure T nsqrt(T)(T n)
{
import std.algorithm, std.conv, std.range, core.bitop;
if (n <= 1) return n;
T m = 1 << (n.bsr/2+1);
return iota(1, m).map!"a * a".assumeSorted!"a <= b".lowerBound(n).length.to!T;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto N = RD;
auto K = RD;
auto A = RDA(-1);
auto list = new long[][](65, N);
foreach (i; 0..N)
{
list[0][i] = A[i];
}
foreach (i; 1..65)
{
foreach (j; 0..N)
{
auto mid = list[i-1][j];
list[i][j] = list[i-1][mid];
}
}
debug writeln(list);
long pos;
foreach (i; 0..64)
{
auto bit = 1L << i;
if (K & bit)
{
pos = list[i][pos];
debug writeln("pos:", pos);
}
}
writeln(pos+1);
stdout.flush;
debug readln;
}
|
D
|
void main()
{
string s = rdStr;
long len = s.length;
long[][] dp = new long[][](len+1, 4);
dp[0][0] = 1;
foreach (i, x; s)
{
long c = (x == '?' ? 3 : 1);
foreach (j; 0 .. 4)
{
dp[i+1][j] = (dp[i+1][j] + c * dp[i][j]) % mod;
if (j == 3) continue;
if (x == '?' || j == x - 'A')
{
dp[i+1][j+1] = (dp[i+1][j+1] + dp[i][j]) % mod;
}
}
}
dp[len][3].writeln;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
enum double eps = 1.0e-9;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
import std.stdio, std.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 = 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 r = RD;
writeln((r^^2)*3);
stdout.flush();
debug readln();
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner, dcomp.numeric.primitive, dcomp.algorithm;
int main() {
auto sc = new Scanner(stdin);
int n;
int[] a;
sc.read(n, a);
int[int] m;
foreach(d; a) {
m[d]++;
m[d-1]++;
m[d+1]++;
}
writeln(m.values.maximum);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
static if (__VERSION__ <= 2070) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
version (X86) static if (__VERSION__ < 2071) {
import core.bitop : bsf, bsr, popcnt;
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
if (f.eof) return false;
line = lineBuf[];
f.readln(line);
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else {
auto buf = line.split.map!(to!E).array;
static if (isStaticArray!T) {
assert(buf.length == T.length);
}
x = buf;
line.length = 0;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/algorithm.d */
// module dcomp.algorithm;
import std.range.primitives;
import std.traits : isFloatingPoint, isIntegral;
T binSearch(alias pred, T)(T l, T r) if (isIntegral!T) {
while (r-l > 1) {
T md = (l+r)/2;
if (!pred(md)) l = md;
else r = md;
}
return r;
}
T binSearch(alias pred, T)(T l, T r, int cnt = 60) if (isFloatingPoint!T) {
foreach (i; 0..cnt) {
T md = (l+r)/2;
if (!pred(md)) l = md;
else r = md;
}
return r;
}
E minimum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed)
if (isInputRange!Range && !isInfinite!Range) {
import std.algorithm, std.functional;
return reduce!((a, b) => binaryFun!pred(a, b) ? a : b)(seed, range);
}
ElementType!Range minimum(alias pred = "a < b", Range)(Range range) {
assert(!range.empty, "range must not empty");
auto e = range.front; range.popFront;
return minimum!pred(range, e);
}
E maximum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed)
if (isInputRange!Range && !isInfinite!Range) {
import std.algorithm, std.functional;
return reduce!((a, b) => binaryFun!pred(a, b) ? b : a)(seed, range);
}
ElementType!Range maximum(alias pred = "a < b", Range)(Range range) {
assert(!range.empty, "range must not empty");
auto e = range.front; range.popFront;
return maximum!pred(range, e);
}
Rotator!Range rotator(Range)(Range r) {
return Rotator!Range(r);
}
struct Rotator(Range)
if (isForwardRange!Range && hasLength!Range) {
size_t cnt;
Range start, now;
this(Range r) {
cnt = 0;
start = r.save;
now = r.save;
}
this(this) {
start = start.save;
now = now.save;
}
@property bool empty() {
return now.empty;
}
@property auto front() {
assert(!now.empty);
import std.range : take, chain;
return chain(now, start.take(cnt));
}
@property Rotator!Range save() {
return this;
}
void popFront() {
cnt++;
now.popFront;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/numeric/primitive.d */
// module dcomp.numeric.primitive;
import std.traits;
import std.bigint;
T pow(T, U)(T x, U n) if (!isFloatingPoint!T && (isIntegral!U || is(U == BigInt))) {
return pow(x, n, T(1));
}
T pow(T, U)(T x, U n, T e) if (isIntegral!U || is(U == BigInt)) {
while (n) {
if (n & 1) e *= x;
x *= x;
n /= 2;
}
return e;
}
T lcm(T)(in T a, in T b) {
import std.numeric : gcd;
return a / gcd(a,b) * b;
}
T[3] extGcd(T)(in T a, in T b)
if (!isIntegral!T || isSigned!T)
{
if (b==0) {
return [T(1), T(0), a];
} else {
auto e = extGcd(b, a%b);
return [e[1], e[0]-a/b*e[1], e[2]];
}
}
|
D
|
import std.stdio,std.string,std.conv,std.algorithm;
import std.algorithm:rev=reverse;
int main(){
auto s=readln.chomp.dup;
foreach(t;s[1..3]){
if(t!=s[0]){
writeln("Yes");
return 0;
}
}
writeln("No");
return 0;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
enum MOD = (10 ^^ 9) + 7;
void main()
{
readln();
auto T = aryread();
long s = T.sum();
long M;
scan(M);
foreach (_; 0 .. M)
{
long P, X;
scan(P, X);
writeln(s - T[P - 1] + X);
}
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.