code
stringlengths 4
1.01M
| language
stringclasses 2
values |
---|---|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n; readV(n);
int[] a; readA(n, a);
auto c = new int[](9);
foreach (ai; a) {
if (ai >= 3200) c[8]++;
else c[ai/400]++;
}
auto b = c[0..8].count!"a>0";
write(b == 0 ? 1 : b, " ", b+c[8]);
}
| D |
void main()
{
auto io = new IO();
auto input = io.str();
writeln(input[0..4]," ",input[4..$]);
}
import std.stdio,std.string,std.conv;
class IO
{
string str( size_t lines = 1 )
{
return readln().chomp();
}
T[] line( T = real , string sp = " " )( size_t lines = 1 )
{
T[] ret;
foreach( i ; 0..lines )
ret ~= readln().chomp().split(sp).convert!T();
return ret;
}
T[][] rect( T = real , string sp = " " )( size_t lines = 1 )
{
T[][] ret = new T[][](lines);
foreach( i ; 0..lines )
ret[i] = readln().chomp().split(sp).convert!T();
return ret;
}
}
R[] convert( R , T )( T[] args ) pure
{
R[] ret = new R[](args.length);
foreach( i ; 0..args.length )
ret[i] = args[i].to!R();
return ret;
}
T sum( T )( in T[] args ) pure nothrow
{
T ret = 0;
foreach( elm ; args )
ret += elm;
return ret;
}
| D |
import std.stdio, std.string, std.algorithm, std.range;
void main() {
auto S = readln.chomp;
auto s = 0, e = S.length-1;
while (S[s] != 'A') {
s++;
}
while (S[e] != 'Z') {
e--;
}
writeln(e - s + 1);
} | D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
void main() {
int t;
scan(t);
while (t--) {
long ai, bi, ci, di;
scan(ai, bi, ci, di);
writeln(solve(ai, bi, ci, di) ? "Yes" : "No");
}
}
bool solve(long a, long b, long c, long d) {
if (a < b) {
return false;
}
if (b > d) {
return false;
}
if (c >= b) {
return true;
}
// a >= b, d >= b, b > c
auto g = gcd(b, d);
auto f = b - g + (a % g);
if (f > c) {
return false;
}
else {
return true;
}
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double[] rotate(double[] vec, double rad) { return [cos(rad) * vec[0] - sin(rad) * vec[1], sin(rad) * vec[0] + cos(rad) * vec[1]]; }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (ti; 0..t)
{
auto n = RD;
auto m = RD;
auto k = RD;
long cnt = n-1;
cnt += n * (m-1);
ans[ti] = cnt == k;
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
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 = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto A = RD;
auto B = RD;
writeln(max(0, A-B*2));
stdout.flush();
debug readln();
} | D |
import std.stdio;
import std.string;
import std.array; // split
import std.conv; // to
void main()
{
string s1 = chomp(readln());
string s2 = chomp(readln());
int a = to!int(s1); // 第0要素を整数に変換
int b = to!int(s2); // 第1要素を整数に変換
a *= a;
writeln(a-b); // 表示
} | D |
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;
void main()
{
int n = readln.chomp.to!int;
auto cash = readln.split.map!(to!int);
auto time = new int[](n);
foreach (i; 0..n) {
auto inp = readln.split.map!(to!int);
foreach (j; 0..cash[i]) {
time[i] += inp[j] * 5;
time[i] += 15;
}
}
time.reduce!(min).writeln;
} | D |
import std.stdio;
import std.algorithm;
import std.conv;
import std.datetime;
import std.numeric;
import std.math;
import std.string;
string my_readln() { return chomp(readln()); }
void main()
{//try{
auto tokens = split(my_readln());
auto N = to!ulong(tokens[0]);
auto X = to!ulong(tokens[1]);
ulong[2][] memo;
memo ~= [1, 1];
foreach (i; 0..N+1)
{
memo ~= [memo[i][0] * 2 + 3, memo[i][1] * 2 + 1];
}
ulong f(ulong lv, ref ulong remain)
{
if (remain == 0) return 0;
if (lv == 0) return 1;
ulong r;
--remain;
if (remain < memo[lv-1][0])
r += f(lv-1, remain);
else
{
r += memo[lv-1][1];
remain -= memo[lv-1][0];
}
if (remain == 0) return r;
--remain;
++r;
if (remain == 0) return r;
if (remain < memo[lv-1][0])
r += f(lv-1, remain);
else
{
r += memo[lv-1][1];
remain -= memo[lv-1][0];
}
if (remain == 0) return r;
--remain;
return r;
}
auto result = f(N, X);
writeln(result);
stdout.flush();
/*}catch (Throwable e)
{
writeln(e.toString());
}
readln();*/
} | D |
import std.algorithm;
import std.array;
import std.bigint;
import std.bitmanip;
import std.conv;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; }
T[] readToArray(T)() {
return readln.split.to!(T[]);
}
void readInto(T...)(ref T ts) {
auto ss = readln.split;
foreach(ref t; ts) {
t = ss.front.to!(typeof(t));
ss.popFront;
}
}
// 冪乗をmod取りつつ計算
pure ulong modPow(ulong a, ulong n, ulong m) {
ulong r = 1;
while (n > 0) {
if(n % 2 != 0) r = r * a % m;
a = a * a % m;
n /= 2;
}
return r;
}
// フェルマーの小定理から乗法逆元を計算
// 定理の要請により法は素数
pure ulong modInv(ulong a, ulong m) {
return modPow(a, m-2, m);
}
// mod取りつつ順列を計算
pure ulong modPerm(ulong n, ulong k, ulong m) {
if (n < k) return 0;
ulong r = 1;
for (ulong i = n-k+1; i <= n; i++) {
r *= i;
r %= m;
}
return r;
}
// mod取りつつ順列を計算
pure ulong modFact(ulong n, ulong m) {
return modPerm(n, n, m);
}
// mod取りつつ組み合わせを計算
// modInvを使っているので法は素数
pure ulong modComb(ulong n, ulong r, ulong m) {
return modPerm(n, r, m)*modInv(modFact(r, m), m) % m;
}
immutable ulong MOD = 1000000007;
void main() {
ulong n, k;
readInto(n, k);
for (ulong i = 1; i <= k; i++) {
writeln(modComb(n-k+1, i, MOD)*modComb(k-1, i-1, MOD) % MOD);
}
}
| D |
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.numeric;
import std.stdio;
import std.string;
void main()
{
long[] s = readln().chomp().split(" ").map!(to!(long)).array;
long a = s[0];
long b = s[1];
if (a + b == 15)
{
writeln("+");
return;
}
if (a * b == 15)
{
writeln("*");
return;
}
writeln("x");
}
| D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm.searching;
void main(){
auto l = readln.chomp;
auto x = l.to!long;
long u = 0;
while(x >= 500)
{
u += 1000;
x -= 500;
}
while(x >= 5)
{
u+= 5;
x -= 5;
}
u.writeln;
} | D |
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto n = readln.chomp.to!size_t;
auto s = readln.chomp;
auto x = 0, m = 0;
foreach (si; s) {
x += si.predSwitch('I', +1, 'D', -1);
m = max(m, x);
}
writeln(m);
}
| D |
import std.stdio;
import std.string;
import std.conv;
import std.bigint;
import std.typecons;
import std.algorithm;
import std.array;
import std.math;
void main() {
auto N = readln.chomp.to!real;
writeln(cast(ulong)(floor(sqrt(N)) ^^2));
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new string[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto s = RD!string;
foreach (c; s)
{
if (c == 'U')
ans[ti] ~= "D";
else if (c == 'L')
ans[ti] ~= "L";
else if (c == 'R')
ans[ti] ~= "R";
else
ans[ti] ~= "U";
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
} | D |
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
import std.ascii;
void main() {
int n;
scan(n);
auto adj = new int[][](n, 0);
foreach (i ; 1 .. n) {
int pi;
scan(pi);
adj[pi-1] ~= i;
}
bool ans = true;
void dfs(int v, int p) {
if (adj[v].empty) return;
int lc;
foreach (u ; adj[v]) {
if (u == p) continue;
if (adj[u].empty) {
lc++;
}
else {
dfs(u, v);
}
}
if (lc < 3) ans = false;
return;
}
dfs(0,0);
writeln(ans ? "Yes" : "No");
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto k = RD!int;
auto s = RD!(char[]);
bool ok = true;
foreach (i; 0..n-k)
{
if (s[i] == s[i+k]) continue;
if (s[i] == '?')
s[i] = s[i+k];
else if (s[i+k] == '?')
s[i+k] = s[i];
else
{
ok = false;
break;
}
}
if (!ok) continue;
long cnt0, cnt1;
foreach (i; 0..k)
{
if (s[i] == '0')
++cnt0;
else if (s[i] == '1')
++cnt1;
}
debug writeln("cnt0:", cnt0, " cnt1:", cnt1);
auto k2 = k/2;
foreach (i; 0..n-k)
{
if (cnt0 > k2 || cnt1 > k2)
{
ok = false;
break;
}
if (s[i] == '0')
--cnt0;
else if (s[i] == '1')
--cnt1;
if (s[i+k] == '0')
++cnt0;
else if (s[i+k] == '1')
++cnt1;
debug writeln("i:", i, " cnt0:", cnt0, " cnt1:", cnt1);
}
if (cnt0 > k2 || cnt1 > k2)
ok = false;
ans[ti] = ok;
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
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 = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void main()
{
auto n = RD!int;
auto xy = new int[][](n);
foreach (i; 0..n)
{
xy[i] = [RD!int, RD!int];
}
if (n % 2 == 1)
{
writeln("NO");
}
else
{
int[][] vec;
foreach (i; 1..n/2+1)
{
int[] t = xy[i].dup;
t[] -= xy[i-1][];
vec ~= t;
}
bool ok = true;
foreach (i; n/2..n)
{
int[] t = xy[(i+1)%n].dup;
t[] -= xy[i][];
t = [-t[0], -t[1]];
if ((vec[i-n/2][0] != t[0]) || (vec[i-n/2][1] != t[1]))
{
ok = false;
break;
}
}
writeln(ok ? "YES" : "NO");
}
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 string[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
ans[ti] = "9";
long x = 8;
foreach (i; 1..n)
{
ans[ti] ~= cast(char)('0'+x);
x = (x+1) % 10;
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
} | D |
//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
class InputReader {
private:
ubyte[] p;
ubyte[] buffer;
size_t cur;
public:
this () {
buffer = uninitializedArray!(ubyte[])(16<<20);
p = stdin.rawRead (buffer);
}
final ubyte skipByte (ubyte lo) {
while (true) {
auto a = p[cur .. $];
auto r = a.find! (c => c >= lo);
if (!r.empty) {
cur += a.length - r.length;
return p[cur++];
}
p = stdin.rawRead (buffer);
cur = 0;
if (p.empty) return 0;
}
}
final ubyte nextByte () {
if (cur < p.length) {
return p[cur++];
}
p = stdin.rawRead (buffer);
if (p.empty) return 0;
cur = 1;
return p[0];
}
template next(T) if (isSigned!T) {
final T next () {
T res;
ubyte b = skipByte (45);
if (b == 45) {
while (true) {
b = nextByte ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 - (b - 48);
}
} else {
res = b - 48;
while (true) {
b = nextByte ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 + (b - 48);
}
}
}
}
template next(T) if (isUnsigned!T) {
final T next () {
T res = skipByte (48) - 48;
while (true) {
ubyte b = nextByte ();
if (b < 48 || b >= 58) {
break;
}
res = res * 10 + (b - 48);
}
return res;
}
}
}
void main() {
auto r = new InputReader;
int n = r.next!int, m = r.next!int;
auto f (int k) {
auto c = new int[2];
foreach (i; 0 .. k) {
++c[1 & r.next!int];
}
return c;
}
auto a = f (n), b = f (m);
writeln (min (a[0], b[1]) + min (a[1], b[0]));
}
| D |
void main()
{
long n = rdElem;
long total;
foreach (i; 1 .. n+1)
{
if (i % 3 && i % 5) total += i;
}
total.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;
immutable mod = 4294967311L;
void main(){
long x, y;
int n, op;
scanf("%d", &n);
foreach(i; 0..n){
scanf("%d%lld", &op, &y);
if(op == 1) x += y;
else if(op == 2) x -= y;
else if(op == 3) x *= y;
else if(op == 4) x = mul(x, inv(y));
x %= mod;
}
if(x < 0) x += mod;
if(x > int.max) x -= mod;
writeln(x);
}
long inv(long y){
long ret = 1;
for(long e=mod-2; e; e>>=1, y=mul(y,y))if(e&1){
ret = mul(ret, y);
}
return ret;
}
long mul(long x, long y){
if(y < 0){
y = -y; x = -x;
}
return ( ((x*(y>>16)%mod)<<16) + (x*(y&0xffff)) ) % mod;
} | 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;
}
int calc(int[] xs) {
int[int] d;
for (int i = 0; i < xs.length; i++) {
int x = xs[i];
d[x - 1]++;
d[x]++;
d[x + 1]++;
}
int ans = 0;
foreach (k, v; d) {
ans = max(ans, v);
}
return ans;
}
void main() {
readint;
auto xs = readints;
writeln(calc(xs));
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
writeln(N > 999 ? "ABD" : "ABC");
stdout.flush();
debug readln();
}
| D |
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.ascii;
void main()
{
writeln(readln.chomp.uniq.array.length==1?"No":"Yes");
}
| D |
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;
void main() {
auto K = readln.chomp.to!int;
writeln("3 3");
auto IK = 0b100000000000000000 + K;
auto IO = 0b100000000000000000;
auto OK = K;
auto ans =
[[IK, IO, 0],
[OK, IK, OK],
[0, 0, OK]];
foreach (a; ans) {
foreach (b; a) write(b, " ");
writeln;
}
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
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 P = RD;
writeln((A * 3 + P) / 2);
stdout.flush();
debug readln();
} | D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main() {
foreach (string line; stdin.lines) {
int ans = 1;
int n = line.chomp.to!int;
for (int i = 1; i <= n; i++) {
ans += i;
}
ans.writeln;
}
} | D |
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto N = readln.chomp.to!int;
auto S = readln.chomp;
auto A = new int[](N+1);
foreach (i; 0..N) A[i+1] = A[i] + (S[i] == '#');
int ans = 1 << 29;
foreach (i; 0..N+1) {
int left_black = A[i];
int right_white = N - i - (A[N] - A[i]);
ans = min(ans, left_black + right_white);
}
ans.writeln;
} | D |
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii;
import std.typecons, std.functional, std.traits;
import std.algorithm, std.container;
import core.stdc.stdlib;
void main()
{
long N = scanElem;
long[] list;
list.length=N;
foreach(i;0..N)
{
list[scanElem-1] = i;
}
long res;
long p;
long k;
foreach(n; list)
{
if(p>n)
{
k=0;
}
k++;
res = max(res, k);
p=n;
}
writeln(N-res);
}
class UnionFind{
UnionFind parent = null;
void merge(UnionFind a)
{
if(same(a)) return;
a.root.parent = this.root;
}
UnionFind root()
{
if(parent is null)return this;
return parent = parent.root;
}
bool same(UnionFind a)
{
return this.root == a.root;
}
}
void scanValues(TList...)(ref TList list)
{
auto lit = readln.splitter;
foreach (ref e; list)
{
e = lit.fornt.to!(typeof(e));
lit.popFront;
}
}
T[] scanArray(T = long)()
{
return readln.split.to!(long[]);
}
void scanStructs(T)(ref T[] t, size_t n)
{
t.length = n;
foreach (ref e; t)
{
auto line = readln.split;
foreach (i, ref v; e.tupleof)
{
v = line[i].to!(typeof(v));
}
}
}
long scanULong(){
long x;
while(true){
const c = getchar;
if(c<'0'||c>'9'){
break;
}
x = x*10+c-'0';
}
return x;
}
T scanElem(T = long)()
{
char[] res;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
res ~= cast(char) c;
c = getchar;
}
return res.strip.to!T;
}
template fold(fun...) if (fun.length >= 1)
{
auto fold(R, S...)(R r, S seed)
{
static if (S.length < 2)
{
return reduce!fun(seed, r);
}
else
{
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
struct Factor
{
long n;
long c;
}
Factor[] factors(long n)
{
Factor[] res;
for (long i = 2; i ^^ 2 <= n; i++)
{
if (n % i != 0)
continue;
int c;
while (n % i == 0)
{
n = n / i;
c++;
}
res ~= Factor(i, c);
}
if (n != 1)
res ~= Factor(n, 1);
return res;
}
long[] primes(long n)
{
if(n<2)return [];
auto table = new long[n+1];
long[] res;
for(int i = 2;i<=n;i++)
{
if(table[i]==-1) continue;
for(int a = i;a<table.length;a+=i)
{
table[a] = -1;
}
res ~= i;
}
return res;
}
bool isPrime(long n)
{
if (n <= 1)
return false;
if (n == 2)
return true;
if (n % 2 == 0)
return false;
for (long i = 3; i ^^ 2 <= n; i += 2)
if (n % i == 0)
return false;
return true;
} | D |
void main(){
string s = readln().chomp();
if(s=="ABC")writeln("ARC");
else writeln("ABC");
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
| D |
void main()
{
int[] tmp = readln.split.to!(int[]);
int month = tmp[0], day = tmp[1];
int cnt;
foreach (m; 2 .. month+1)
{
foreach (d; 21 .. day+1)
{
int div = d / 10, rem = d % 10;
if (rem >= 2 && m == div * rem)
{
++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, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
while (true) {
int n;
scan(n);
if (n == 0) return;
auto map = new int[][](21, 21);
foreach (i ; 0 .. n) {
int xi, yi;
scan(xi, yi);
map[xi][yi]++;
}
int m;
scan(m);
int hsk;
int px = 10, py = 10;
foreach (i ; 0 .. m) {
char di;
int li;
scan(di, li);
foreach (j ; 0 .. li) {
if (di == 'N') {
py++;
}
else if (di == 'S') {
py--;
}
else if (di == 'E') {
px++;
}
else {
px--;
}
hsk += map[px][py];
map[px][py] = 0;
}
}
debug {
writeln(hsk);
}
writeln(hsk == n ? "Yes" : "No");
}
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
} | D |
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd = readln.split.to!(int[]), a = rd[0], b = rd[1];
auto s = readln.chomp;
foreach (i, c; s) {
if (i == a) {
if (c != '-') {
writeln("No");
return;
}
} else {
if (c < '0' || c > '9') {
writeln("No");
return;
}
}
}
writeln("Yes");
}
| D |
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.ascii;
void main()
{
auto a = ['a', 'i', 'u', 'e', 'o'];
auto c = readln.chomp.to!char;
if (a.count(c)) {
writeln("vowel");
} else {
writeln("consonant");
}
}
| D |
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
int n, q;
scan(n, q);
while (q--) {
int vi, wi;
scan(vi, wi);
solve(n, vi, wi);
}
}
void solve(int n, int v, int w) {
if (n == 1) {
writeln(min(v, w));
return;
}
auto va = new int[](0);
va ~= v;
while (v > 1) {
if (v % n <= 1) {
v /= n;
}
else {
v = (v / n) + 1;
}
va ~= v;
}
va.reverse();
auto wa = new int[](0);
wa ~= w;
while (w > 1) {
if (w % n <= 1) {
w /= n;
}
else {
w = (w / n) + 1;
}
wa ~= w;
}
wa.reverse();
debug {
writeln(va);
writeln(wa);
}
int ans = 0;
while (!va.empty) {
while (!wa.empty) {
if (va.front == wa.front) {
ans = max(ans, va.front);
va.popFront;
wa.popFront;
break;
}
else if (va.front < wa.front) {
va.popFront;
break;
}
else {
wa.popFront;
}
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
} | D |
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.ascii;
void main()
{
auto n = readln.chomp.to!int;
writeln(n/2+(n%2>0));
}
| D |
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
auto N = readln.chomp.to!long;
auto n = N;
auto S = 0;
while (n > 0) {
S += n % 10;
n /= 10;
}
writeln(N % S == 0 ? "Yes" : "No");
}
| D |
import std.stdio;
import std.conv;
import std.algorithm;
import std.string;
import std.file;
import std.math;
int main() {
string l;
while((l = readln()).length >= 2){
int a, b, aorg, borg, r;
a = aorg = to!int(l.split()[0]), b = borg = to!int(l.split()[1]);
while(b > 0){
a = a - (a / b) * b;
r = a, a = b, b = r;
}
printf("%d %d\n", a, aorg / a * borg);
}
return 0;
} | D |
import std.string;
import std.stdio;
import std.algorithm;
void main() {
auto s = readln.chomp.dup;
auto t = readln.chomp.dup;
if (s.length == 0 || t.length == 0) {
writeln();
return;
}
auto dp = new int[][t.length + 1];
foreach (ref e;dp) {
e = new int[s.length + 1];
}
foreach (size_t i, te;t) {
foreach (size_t j, se;s) {
if (te == se) {
dp[i + 1][j + 1] = max(dp[i][j] + 1, dp[i + 1][j], dp[i][j + 1]);
} else {
dp[i + 1][j + 1] = max(dp[i][j], dp[i + 1][j], dp[i][j + 1]);
}
}
}
auto result = new char[dp[t.length][s.length]];
auto y = t.length, x = s.length;
for (;;) {
auto num = dp[y][x];
while (y >= 1 && dp[y - 1][x] == num) {
--y;
}
while (x >= 1 && dp[y][x - 1] == num) {
--x;
}
if (y == 0 || x == 0) break;
result[num - 1] = s[x - 1];
--x;
--y;
}
writeln(result);
}
| 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 q = RD!int;
auto ans = new bool[](q);
foreach (i; 0..q)
{
auto n = RD!int;
auto a = RD!string;
auto b = RD!string;
int pos;
ans[i] = true;
foreach (j; 0..n)
{
if (pos == 0)
{
if ([a[j]].to!int >= 3)
{
if ([b[j]].to!int <= 2)
{
ans[i] = false;
break;
}
pos = 1;
}
}
else
{
if ([b[j]].to!int >= 3)
{
if ([a[j]].to!int <= 2)
{
ans[i] = false;
break;
}
pos = 0;
}
}
}
if (pos == 0)
ans[i] = false;
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush();
debug readln();
} | 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 K = lread();
long ans;
foreach (i; 1 .. K + 1)
foreach (j; 1 .. K + 1)
foreach (k; 1 .. K + 1)
{
ans += gcd(i, j).gcd(k);
}
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 s = RD!string;
foreach (i, c; s)
{
if (i % 2 == 0)
write(c);
}
writeln();
stdout.flush();
debug readln();
} | D |
import std.stdio;
import std.algorithm;
import std.array;
import std.conv;
import std.string;
import std.uni;
void main()
{
while (true)
{
auto a = readln;
if (a == "0\n") break;
writeln(a.strip.map!(x => x - '0').sum);
}
}
| D |
import std.stdio,
std.string,
std.conv;
void main(){
string s, w = readln.chomp;
while(true){
string tmp = readln.chomp;
if (tmp == "END_OF_TEXT") break;
s ~= tmp.toLower ~ " ";
}
int c = 0;
foreach(x; s.split){
if (x == w) c++;
}
writeln(c);
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto L = readln.chomp.to!int;
if (L <= 60) {
writeln(2, " ", L);
foreach (l; 0..L) {
writeln("1 2 ", l);
}
return;
}
int[] AS, BS, CS;
int k, l = 1;
while (l*2 <= L) {
++k;
l *= 2;
}
int x = 1;
foreach (_; 0..k) {
l /= 2;
AS ~= [x, x];
BS ~= [x+1, x+1];
CS ~= [l, 0];
x += 1;
}
auto d = 2^^k;
auto r = L - d;
while (r) {
if (r == 1) {
AS ~= 1;
BS ~= k+1;
CS ~= L-1;
break;
}
l = 2;
foreach_reverse (y; 1..k+1) {
if (l*2 > r) {
AS ~= 1;
BS ~= y;
CS ~= d;
r -= l;
d += l;
break;
}
l *= 2;
}
}
writeln(k+1, " ", AS.length);
foreach (i; 0..AS.length) writeln(AS[i], " ", BS[i], " ", CS[i]);
} | D |
import std.stdio, std.string, std.conv;
void main()
{
auto input = readln.split.to!(int[]);
auto N = input[0];
auto i = input[1];
writeln(N-i+1);
} | D |
//dlang template---{{{
import std.stdio;
import std.conv;
import std.string;
import std.array;
import std.algorithm;
import std.typecons;
import std.math;
import std.range;
// MIT-License https://github.com/kurokoji/nephele
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType))
{
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType))
{
str ~= s.to!(char[]).strip.split;
}
}
//}}}
void main() {
Scanner sc = new Scanner;
int A, B, C, D;
sc.scan(A, B, C, D);
while (1) {
C -= B;
if (C < 1) {
writeln("Yes");
return;
}
A -= D;
if (A < 1){
writeln("No");
return;
}
}
}
| D |
import std.stdio;
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.string;
void main()
{
int n, k;
n = readln.chomp.to!int;
k = readln.chomp.to!int;
int res = 1;
foreach (int i; 0..n) {
res = min(res * 2, res + k);
}
writeln(res);
}
| D |
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int n, k;
rd(n, k);
auto x = new long[](n), y = new long[](n);
foreach (i; 0 .. n)
rd(x[i], y[i]);
long mn = 9 * 10L ^^ 18;
foreach (a; 0 .. n) {
foreach (b; 0 .. n) {
foreach (c; 0 .. n) {
foreach (d; 0 .. n) {
auto left = x[a], right = x[b], top = y[c], bottom = y[d];
int count = 0;
foreach (i; 0 .. n) {
if (left <= x[i] && x[i] <= right && bottom <= y[i] && y[i] <= top) {
count++;
}
}
if (count >= k) {
mn = min(mn, (right - left) * (top - bottom));
}
}
}
}
}
writeln(mn);
}
void rd(T...)(ref T x) {
import std.stdio : readln;
import std.string : split;
import std.conv : to;
auto l = readln.split;
assert(l.length == x.length);
foreach (i, ref e; x)
e = l[i].to!(typeof(e));
}
| D |
// import chie template :) {{{
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons, std.format;
// }}}
// nep.scanner {{{
class Scanner {
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) {
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next() {
if (idx < str.length) {
return str[idx++];
}
char[] s;
while (s.length == 0) {
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(size_t len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
void scan()() {
}
void scan(T, S...)(ref T x, ref S args) {
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType)) {
str ~= s.to!(char[]).strip.split;
}
}
// }}}
void main() {
auto cin = new Scanner;
long N, A, B;
cin.scan(N, A, B);
writeln(N / (A + B) * A + (N % (A + B) < A ? N % (A + B) : A));
}
| 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 s = readln.chomp;
(s.count('+').to!int - s.count('-').to!int).writeln;
}
| D |
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
import std.exception;
int n; rd(n);
void q(int i){
writeln(i); stdout.flush();
}
const M=0, F=1, V=-1;
int read(){
auto t=readln.chomp.to!(char[]);
if(t[0]=='M') return M;
else if(t[0]=='F') return F;
else return V;
}
int l=0, r=n-1;
q(l); auto ls=read();
if(ls==V) return;
q(r); auto rs=read();
if(rs==V) return;
void fun(int l, int ls, int r, int rs){
if((r-l+1)&1){ // odd
enforce(ls!=rs);
auto m=(r+l)/2;
q(m); auto ms=read();
if(ms==V) return;
if(ls==ms){
if((m-l+1)&1) fun(m, ms, r, rs);
else fun(l, ls, m, ms);
}else{
if((m-l+1)&1) fun(l, ls, m, ms);
else fun(m, ms, r, rs);
}
}else{
enforce(ls==rs);
auto m=(r+l)/2;
q(m); auto ms=read();
if(ms==V) return;
if(ls==ms){
if((m-l+1)&1) fun(m, ms, r, rs);
else fun(l, ls, m, ms);
}else{
if((m-l+1)&1) fun(l, ls, m, ms);
else fun(m, ms, r, rs);
}
}
}
fun(l, ls, r, rs);
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
} | D |
import std.stdio,std.conv,std.string;
void main(){
auto AB = readLine!int();
if ( AB[0] <= 8 && AB[1] <= 8 ){
writeln("Yay!");
}else{
writeln(":(");
}
}
T[] readLine(T)(){
T[] ret;
foreach( elm ; readln().split() ){
ret ~= elm.to!int();
}
return ret;
} | D |
import std.stdio;
import std.algorithm;
import std.range;
import std.conv;
import std.format;
import std.array;
import std.math;
import std.string;
import std.container;
string[] C = ["dreamer", "eraser", "dream", "erase"];
bool judge(string sub_str) {
if (sub_str.length == 0)
return true;
bool ok = false;
foreach(c; C) {
if (sub_str.length < c.length)
continue;
if (sub_str[0..c.length] == c)
ok = ok || judge(sub_str[c.length..$]);
}
return ok;
}
void main() {
string S; readlnTo(S);
writeln(judge(S) ? "YES" : "NO");
}
// helpers
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..$];
}
}
| D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n, a, b;
scan(n, a, b);
int mx = min(a, b);
int mn = max(0, a + b - n);
writeln(mx, " ", mn);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
| D |
import std.stdio;
import std.algorithm;
import std.range;
import std.string;
import std.conv;
// 素因数分解
uint[] prime_factor(uint n) {
uint[] res = [];
for(uint i = 2; i * i <= n; i++) {
if (n % i == 0) {
do {
res ~= i;
n = n / i;
} while (n % i == 0);
}
}
if (n > 1) res ~= n;
return res;
}
void main() {
// 5 と 2 の約数を調べる
while(true) {
uint n = readln.chomp.to!(uint);
uint a = 0, b = 0;
if (n == 0) break;
foreach(numbers; iota(0, n + 1).map!(prime_factor)) {
foreach(number; numbers) {
if (number == 2) a++;
else if (number == 5) b++;
}
}
min(a, b).writeln;
}
} | D |
import std.stdio,
std.string,
std.range,
std.algorithm;
void main(){
int n, x, cnt;
while (true){
scanf("%d %d", &n, &x);
if (n == 0 && x == 0) break;
cnt = 0;
n+=1;
foreach (i; 1..n){
foreach (j; i..n){
foreach (k; j..n){
if (i == j || i == k || j == k) continue;
if (i+j+k == x) cnt++;
}
}
}
writeln(cnt);
}
} | D |
import std.stdio;
import std.algorithm;
import std.math;
import std.conv;
import std.string;
T readNum(T)(){
return readStr.to!T;
}
T[] readNums(T)(){
return readStr.split.to!(T[]);
}
string readStr(){
return readln.chomp;
}
void main(){
string s = readStr;
int l = s.length.to!int;
if(s.count("o") + 15 - l >= 8){
writeln("YES");
} else {
writeln("NO");
}
}
| D |
void main(){
string n = readln().chomp();
char[] ans;
foreach(elm; n){
ans ~= (elm=='9')? '1': '9';
}
ans.writeln();
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
| D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main() {
string[] input = new string[](2);
bool[] cup = new bool[](3);
cup.fill(false);
cup[0] = true;
while ((input = readln.chomp.split(",")).length != 0) {
swap(cup[(input[0][0] - 'A').to!int], cup[(input[1][0] - 'A').to!int]);
}
foreach (i; 0..cup.length) {
if (cup[i]) {
writeln((i + 'A').to!char);
break;
}
}
} | D |
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
auto s = sread();
long i = 1;
string previous; //前の文字
previous = s[0 .. 1];
// writeln(previous);
long cnt = 1;
// writeln(s);
while (i < (s.length))
{
// writeln(s[i .. i + 1]);
if (previous == s[i .. i + 1])
{
if (i + 2 <= s.length)
{
previous = s[i .. i + 2];
i += 2;
cnt += 1;
}
else
{
break;
}
}
else
{
if (i + 1 <= s.length)
{
previous = s[i .. i + 1];
i += 1;
cnt += 1;
}
else
{
break;
}
}
// writeln("i:", i);
// writeln("pre:", previous);
}
writeln(cnt);
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
| D |
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
void main() {
auto size = readln.chomp.to!int;
auto lucky = cast(byte[])readln.chomp;
lucky[] -= '0';
void solve() {
int count;
bool[byte] ava;
foreach(a; lucky) ava[a] = true;
foreach(x; ava.keys) foreach(y; ava.keys) foreach(z; ava.keys) {
if (auto after_x = lucky.findSplit([x])) {
if (auto after_y = after_x[2].findSplit([y])) {
if (after_y[2].canFind(z)) {
count++;
}
}
}
}
writeln(count);
}
solve();
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto A = RD!string;
auto B = RD!string;
int ans;
if (A.length == B.length)
{
foreach (i; 0..A.length)
{
auto x = A[i].to!int;
auto y = B[i].to!int;
if (x == y) continue;
ans = x < y ? -1 : 1;
break;
}
}
else
ans = A.length == B.length ? 0 : A.length < B.length ? -1 : 1;
writeln(ans == -1 ? "LESS" : ans == 0 ? "EQUAL" : "GREATER");
stdout.flush();
debug readln();
} | D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
alias Point = Tuple!(int, "x", int, "y", int, "c");
void main() {
int n;
scan(n);
int ans = 10^^9;
foreach (a ; 1 .. n) {
ans = min(ans, s(a) + s(n - a));
}
writeln(ans);
}
int s(int x) {
return x > 0 ? s(x/10) + (x % 10) : 0;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
| D |
import std.stdio, std.conv, std.string;
void main(){
auto ip = readln.chomp.to!int;
int res;
for(int i = 0; i <= ip; i++){
res += i;
}
res.writeln;
} | D |
import std.stdio;
import std.algorithm;
import std.array;
import std.conv;
import std.datetime;
import std.numeric;
import std.math;
import std.string;
string my_readln() { return chomp(readln()); }
void main()
{//try{
auto N = my_readln().to!ulong();
auto tokens = split(my_readln());
auto A = tokens[0].to!ulong();
auto B = tokens[1].to!ulong();
ulong[3] cnt;
foreach (token; split(my_readln()))
{
auto p = token.to!ulong();
if (p <= A) ++cnt[0];
else if (p <= B) ++cnt[1];
else ++cnt[2];
}
writeln(min(min(cnt[0], cnt[1]), cnt[2]));
stdout.flush();
/*}catch (Throwable e)
{
writeln(e.toString());
}
readln();*/
} | D |
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;
void main() {
auto N = readln.chomp.to!int;
auto G = new int[][](N);
foreach (_; 0..N-1) {
auto s = readln.split.map!(to!int);
G[s[0]-1] ~= s[1]-1;
G[s[1]-1] ~= s[0]-1;
}
if (N == 1) {
writeln("First");
return;
} else if (N == 2) {
writeln("Second");
return;
}
Tuple!(int, int) dfs(int n, int p) {
auto ret = tuple(0, n);
foreach (m; G[n]) {
if (m == p) continue;
auto t = dfs(m, n);
t[0] += 1;
ret = max(ret, t);
}
return ret;
}
auto d = dfs(dfs(0, -1)[1], -1)[0];
int g = (d - 1) % 3;
writeln(g > 0 ? "First" : "Second");
}
| D |
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int a, b, c, d; readV(a, b, c, d);
auto n = 101, e = new int[](n);
e[a]++; e[b]--; e[c]++; e[d]--;
foreach (i; 1..n) e[i] += e[i-1];
writeln(e.filter!"a==2".count);
}
| 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 a, b, c;
scan(a, b, c);
(a + b).min(b + c).min(a + c).writeln();
}
| D |
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii;
import std.typecons, std.functional, std.traits;
import std.algorithm, std.container;
import core.stdc.stdlib;
void main()
{
long R = scanElem;
if(R<1200){
writeln("ABC");
return;
}
if(R<2800){
writeln("ARC");
return;
}
writeln("AGC");
return;
}
class UnionFind{
UnionFind parent = null;
void merge(UnionFind a)
{
if(same(a)) return;
a.root.parent = this.root;
}
UnionFind root()
{
if(parent is null)return this;
return parent = parent.root;
}
bool same(UnionFind a)
{
return this.root == a.root;
}
}
void scanValues(TList...)(ref TList list)
{
auto lit = readln.splitter;
foreach (ref e; list)
{
e = lit.fornt.to!(typeof(e));
lit.popFront;
}
}
T[] scanArray(T = long)()
{
return readln.split.to!(long[]);
}
void scanStructs(T)(ref T[] t, size_t n)
{
t.length = n;
foreach (ref e; t)
{
auto line = readln.split;
foreach (i, ref v; e.tupleof)
{
v = line[i].to!(typeof(v));
}
}
}
long scanULong(){
long x;
while(true){
const c = getchar;
if(c<'0'||c>'9'){
break;
}
x = x*10+c-'0';
}
return x;
}
T scanElem(T = long)()
{
char[] res;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
res ~= cast(char) c;
c = getchar;
}
return res.strip.to!T;
}
template fold(fun...) if (fun.length >= 1)
{
auto fold(R, S...)(R r, S seed)
{
static if (S.length < 2)
{
return reduce!fun(seed, r);
}
else
{
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
struct Factor
{
long n;
long c;
}
Factor[] factors(long n)
{
Factor[] res;
for (long i = 2; i ^^ 2 <= n; i++)
{
if (n % i != 0)
continue;
int c;
while (n % i == 0)
{
n = n / i;
c++;
}
res ~= Factor(i, c);
}
if (n != 1)
res ~= Factor(n, 1);
return res;
}
long[] primes(long n)
{
if(n<2)return [];
auto table = new long[n+1];
long[] res;
for(int i = 2;i<=n;i++)
{
if(table[i]==-1) continue;
for(int a = i;a<table.length;a+=i)
{
table[a] = -1;
}
res ~= i;
}
return res;
}
bool isPrime(long n)
{
if (n <= 1)
return false;
if (n == 2)
return true;
if (n % 2 == 0)
return false;
for (long i = 3; i ^^ 2 <= n; i += 2)
if (n % i == 0)
return false;
return true;
} | D |
import std.stdio,std.conv,std.string,std.algorithm;
void main(){
int m;
auto sm=readln().split();
m=to!int(sm[0]);
writeln(48-m);
} | 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 n = RD!int;
auto dp = new long[](n+1);
dp[0] = 1;
dp[1] = 1;
foreach (i; 2..n)
{
dp[i] = dp[i-1];
dp[i].moda(dp[i-2]);
}
auto ans = dp[n-1];
long m = 2;
m.modpow(n);
ans.modd(m);
writeln(ans);
stdout.flush;
debug readln;
}
| D |
import std.stdio, std.algorithm, std.string, std.conv, std.array;
void main()
{
foreach(unused; 0 .. readln.chomp.to!int)
{
int left = 0, right = -1;
bool flag = true;
foreach(n; readln.split.map!(to!int))
{
if(n > left && n > right)
{
if(left > right) left = n;
else right = n;
}
else if(n > left) left = n;
else if(n > right) right = n;
else
{
flag = false;
break;
}
}
(flag ? "YES" : "NO").writeln;
}
} | D |
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto n = readln.chomp.to!(char[]).array.count('9');
writeln(n?"Yes":"No");
}
| D |
import std.conv, std.stdio;
import std.algorithm, std.array, std.string, std.range;
void main()
{
auto s = readln.chomp.to!dstring;
if (s[0..$/2].isPalindrome && (s[0..$/2] == s[$/2+1..$]))
"Yes".writeln;
else
"No".writeln;
}
auto isPalindrome(T)(in T[] a)
{
return a.equal(a.retro);
}
| D |
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto a = readln.chomp.to!int;
auto b = readln.chomp.to!int;
auto h = readln.chomp.to!int;
writeln((a + b) * h / 2);
}
| D |
void main()
{
long n = readln.chomp.to!long;
long[] t = new long[n];
foreach (i; 0 .. n)
{
t[i] = readln.chomp.to!long;
}
long time = t[0];
foreach (i; 1 .. n)
{
long g = gcd(time, t[i]);
time = time / g * t[i];
}
time.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.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
writeln(N*800 - (N/15)*200);
stdout.flush();
debug readln();
} | D |
// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
int n, m;
void main() {
scan(n, m);
writeln((n - 1) * (m - 1));
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
} | D |
void main() {
auto a = ri, b = ri, c = ri, d = ri, e = ri, k = ri;
if(e - a > k) writeln(":(");
else writeln("Yay!");
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
| D |
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n; rd(n);
auto s=readln.chomp.to!(char[]);
int f=0;
int k=0;
foreach(i; 0..n){
if(s[i]=='('){
k++;
}else{
if(k==0){
f++;
}else{
k--;
}
}
}
foreach(_; 0..f) write("(");
write(s);
foreach(_; 0..k) write(")");
writeln();
}
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.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto s = readln.chomp;
writeln("2018", s[4..$]);
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto K = RD;
auto seven = 7 % K;
auto ten = 10 % K;
auto used = new bool[](K);
long x = seven;
long ans = 1;
while (true)
{
if (x == 0) break;
used[x] = true;
x = (x * ten + seven) % K;
if (used[x])
{
ans = -1;
break;
}
++ans;
}
writeln(ans);
stdout.flush;
debug readln;
} | D |
import std.stdio;
import std.string;
import std.conv;
void main() {
string[] inputs = split(readln());
int A = to!int(inputs[0]);
int B = to!int(inputs[1]);
int C = to!int(inputs[2]);
if(A == B) writeln(C);
else if(A == C) writeln(B);
else writeln(A);
}
| 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 A = new long[](N);
foreach (i; 0..N)
{
A[i] = RD;
}
long ans;
foreach (i; 0..N)
{
if (i != 0)
{
if (A[i-1] != 0 && A[i] != 0)
{
++ans;
--A[i-1];
--A[i];
}
}
auto x = A[i] / 2;
ans += x;
A[i] -= x * 2;
debug writeln(ans);
}
writeln(ans);
stdout.flush();
debug readln();
} | D |
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
auto n = lread();
auto a = aryread();
auto b = aryread();
// writeln(a);
// writeln(b);
long cnt;
long tmp;
long tmptmp;
foreach (i; 0 .. n)
{
tmp = min(a[i], b[i]);
cnt += tmp;
a[i] -= tmp;
b[i] -= tmp;
// writeln("a:", a);
// writeln("b:", b);
// writeln(cnt);
tmptmp = min(a[i + 1], b[i]);
cnt += tmptmp;
a[i + 1] -= tmptmp;
b[i] -= tmptmp;
// writeln("a:", a);
// writeln("b:", b);
}
writeln(cnt);
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
| D |
void main()
{
long n = rdElem;
n %= 10;
if (n == 3)
{
"bon".writeln;
}
else if (n == 0 || n == 1 || n == 6 || n == 8)
{
"pon".writeln;
}
else
{
"hon".writeln;
}
}
enum long mod = 10L^^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.array, std.conv;
void main() {
while (true) {
string[] input = split(readln());
int H = to!int(input[0]);
int W = to!int(input[1]);
if (H == 0 && W == 0) break;
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
write(is_dots(i, j) ? "." : "#");
}
writeln("");
}
writeln("");
}
}
bool is_dots(int y, int x) {
return (((y % 2) + x) % 2 == 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 memo = new long[][](10, 10);
foreach (n; 1..N+1) {
auto y = n%10;
int x;
while (n) {
x = n;
n /= 10;
}
++memo[x][y];
}
long r;
foreach (i; 1..10) {
foreach (j; 1..10) {
r += memo[i][j] * memo[j][i];
}
}
writeln(r);
} | 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;
int[] Z_algorithm(string s) {
auto A = new int[](s.length);
A[0] = s.length.to!int;
for (int i = 1, j = 0; i < s.length; ) {
while (i + j < s.length && s[j] == s[i+j]) ++j;
A[i] = j;
if (j == 0) { ++i; continue; }
int k = 1;
while (i + k < s.length && k + A[k] < j) A[i+k] = A[k], ++k;
i += k; j -= k;
}
return A;
}
void main() {
auto S = readln.chomp;
auto N = S.length.to!int;
if (all(S.map!(s => s == S[0]))) {
writeln(N);
writeln(1);
return;
}
string T;
foreach (i; 0..N) T ~= S[N - i - 1];
auto z1 = Z_algorithm(S);
auto z2 = Z_algorithm(T);
auto ng1 = new bool[](N + 1);
auto ng2 = new bool[](N + 1);
for (int i = 0; i < N; i++) {
if (i > 0 && i - z1[i] <= 0) {
int len = min(i, z1[i]);
int end = min(i + z1[i], N) + 1;
for (int j = i + len; j < end && !ng1[j]; j += len)
ng1[j] = true;
}
}
for (int i = 0; i < N; i++) {
if (i > 0 && i - z2[i] <= 0) {
int len = min(i, z2[i]);
int end = min(i + z2[i], N) + 1;
for (int j = i + len; j < end && !ng2[j]; j += len)
ng2[j] = true;
}
}
ng2.reverse;
auto ng = (N + 1).iota.map!(i => ng1[i] || ng2[i]);
if (!ng[0]) {
writeln(1);
writeln(1);
} else {
writeln(2);
writeln(N + 1 - ng.sum);
}
debug {
z1.writeln;
z2.writeln;
ng1.map!(x => x ? 1: 0).writeln;
ng2.map!(x => x ? 1: 0).writeln;
}
}
| D |
import std.stdio, std.conv, std.string, std.array, std.algorithm, std.regex;
void main(){
auto ip = readln.split.to!(int[]), a = ip[0], b = ip[1], c = ip[2];
if(a == b + c || b == a + c || c == a + b){
writeln("Yes");
} else {
writeln("No");
}
} | D |
void main()
{
long n = rdElem;
string march = "MARCH";
long[] list = new long[5];
foreach (i; 0 .. n)
{
string s = rdStr;
foreach (j, m; march)
{
if (m == s[0]) ++list[j];
}
}
long result;
foreach (i; 0 .. 3)
{
foreach (j; i+1 .. 4)
{
foreach (k; j+1 .. 5)
{
result += list[i] * list[j] * list[k];
}
}
}
result.writeln;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop; | D |
import std.stdio, std.string, std.algorithm, std.conv, std.range;
void main() {
foreach (string line; lines(stdin)) {
if (line == "0 0\n")
break;
auto inputArray = line.chomp.split.map!(to!int);
auto h = inputArray[0];
auto w = inputArray[1];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if ((i + j) % 2 == 0) {
write("#");
} else {
write(".");
}
}
writeln;
}
writeln;
}
} | D |
//dlang template---{{{
import std.stdio;
import std.conv;
import std.string;
import std.array;
import std.algorithm;
import std.typecons;
import std.math;
import std.range;
// MIT-License https://github.com/kurokoji/nephele
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType))
{
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType))
{
str ~= s.to!(char[]).strip.split;
}
}
//Digit count---{{{
int DigitNum(int num) {
int digit = 0;
while (num != 0) {
num /= 10;
digit++;
}
return digit;
}
//}}}
//}}}
void main() {
Scanner sc = new Scanner;
int N, H, W;
sc.scan(N, H, W);
writeln(((N - H) + 1) * ((N - W) + 1));
}
| 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 A, B, C, D;
scan(A, B, C, D);
long a = ((A + B) - (C + D));
long b = (a == 0) ? 0 : (a / abs(a));
auto ary = ["Right", "Balanced", "Left"];
writeln(ary[b + 1]);
}
| D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int a;
string s;
scan(a);
scan(s);
writeln(a >= 3200 ? s : "red");
}
int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new int[][](n, 0);
foreach (i; 0 .. m) {
int u, v;
scan(u, v);
if (is1indexed) {
u--, v--;
}
adj[u] ~= v;
if (isUndirected) {
adj[v] ~= u;
}
}
return adj;
}
alias Edge = Tuple!(int, "to", int, "cost");
Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new Edge[][](n, 0);
foreach (i; 0 .. m) {
int u, v, c;
scan(u, v, c);
if (is1indexed) {
u--, v--;
}
adj[u] ~= Edge(v, c);
if (isUndirected) {
adj[v] ~= Edge(u, c);
}
}
return adj;
}
void yes(bool b) {
writeln(b ? "Yes" : "No");
}
void YES(bool b) {
writeln(b ? "YES" : "NO");
}
T[] readArr(T)() {
return readln.split.to!(T[]);
}
T[] readArrByLines(T)(int n) {
return iota(n).map!(i => readln.chomp.to!T).array;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
| D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int a, b;
scan(a, b);
int ans;
if (b % a == 0) {
ans = a + b;
}
else {
ans = b - a;
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
| D |
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
import std.array;
auto expr=readln.chomp.to!(char[]);
int res; rd(res);
auto n=expr.length;
int[] a; a~=(expr[0]-'0');
for(auto i=1; i<n; i+=2){
if(expr[i]=='+') a~=(expr[i+1]-'0');
else{
auto bk=a.back; a.popBack;
a~=(bk*(expr[i+1]-'0'));
}
}
auto res1=sum(a);
int res2=(expr[0]-'0');
for(auto i=1; i<n; i+=2){
if(expr[i]=='+') res2+=(expr[i+1]-'0');
else res2*=(expr[i+1]-'0');
}
if(res1==res2 && res1==res) writeln("U");
else if(res1==res) writeln("M");
else if(res2==res) writeln("L");
else writeln("I");
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
void wr(T...)(T x){
import std.stdio;
foreach(e; x) write(e, " ");
writeln();
} | D |
import std.algorithm;
import std.ascii;
import std.array;
import std.container;
import std.conv;
import std.numeric;
import std.stdio;
import std.string;
import std.typecons;
void log(A...)(A arg) {
stderr.writeln(arg);
}
int size(T)(in T s) {
return cast(int)s.length;
}
const int INF = 10000;
int op(char c, int a, int b) {
if (c == '+') return a + b;
if (c == '-') return a - b;
assert(false);
}
int next_op_index(in string s, int i) {
for (int j = i; j < s.size; j++) {
if (s[j] == '+' || s[j] == '-') return j;
}
throw new Exception("");
}
int prev_op_index(in string s, int i) {
for (int j = i; j >= 0; j--) {
if (s[j] == '+' || s[j] == '-') return j;
}
throw new Exception("");
}
void main() {
string s = readln.chomp;
auto t_index = new int[s.size]; t_index[] = -1;
int cur = 0;
foreach (i, c; s) {
if (c == '(' || c == ')') continue;
t_index[i] = cur++;
}
auto buf = new char[cur];
foreach (i, c; s) {
if (c == '(' || c == ')') continue;
buf[t_index[i]] = c;
}
string t = buf.idup;
int N = t.size;
auto open_banned = new bool[N];
auto close_banned = new bool[N];
foreach (int i, c; s) {
try {
if (c == '(') close_banned[t_index[next_op_index(s, i)]] = true;
else if (c == ')') open_banned[t_index[prev_op_index(s, i)]] = true;
} catch (Exception e) {}
}
auto dp_min = new int[][](N + 1, N + 1);
auto dp_max = new int[][](N + 1, N + 1);
foreach (ref a; dp_max) a[] = -INF;
foreach (ref a; dp_min) a[] = INF;
foreach (i, c; t) {
if (c.isDigit) dp_max[i][i + 1] = dp_min[i][i + 1] = cast(int)(c - '0');
}
for (int k = 2; k <= N; k++) {
for (int i = 0; i + k <= N; i++) {
for (int j = i + 1; j < i + k; j++) {
if (! (t[j] == '+' || t[j] == '-')) continue;
int lcount = cast(int)t[i .. j].count!isDigit;
int rcount = cast(int)t[j .. i + k].count!isDigit;
if (lcount >= 2 && close_banned[j]) continue;
if (rcount >= 2 && open_banned[j]) continue;
switch (t[j]) {
case '+':
dp_max[i][i + k] = max(dp_max[i][i + k], dp_max[i][j] + dp_max[j + 1][i + k]);
dp_min[i][i + k] = min(dp_min[i][i + k], dp_min[i][j] + dp_min[j+ 1][i + k]);
break;
case '-':
dp_max[i][i + k] = max(dp_max[i][i + k], dp_max[i][j] - dp_min[j + 1][i + k]);
dp_min[i][i + k] = min(dp_min[i][i + k], dp_min[i][j] - dp_max[j + 1][i + k]);
break;
default:
break;
}
}
}
}
/*
foreach (L; dp_max) log(L);
log();
foreach (L; dp_min) log(L);
*/
writeln(dp_max[0][t.size]);
} | D |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.