code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.conv;
import std.stdio;
import std.string;
void main(){
int N = readln.chomp.to!int;
writeln(factorial(N));
}
long factorial(long N){
if(N == 1)
return 1;
else
return N * factorial(N-1);
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
// dfmt on
void main()
{
long N = lread();
auto S1 = sread();
auto S2 = sread();
long[] S;
{
long i = 0;
while (i < N)
{
if (S1[i] == S2[i])
{
S ~= 1;
i++;
}
else
{
S ~= 2;
i += 2;
}
}
}
long ans = (S[0] == 1) ? 3 : 6;
foreach (i; 1 .. S.length)
{
if (S[i] == 1)
{
ans *= (S[i - 1] == 1) ? 2 : 1;
}
else
{
ans *= (S[i - 1] == 1) ? 2 : 3;
}
ans %= MOD;
}
writeln(ans);
}
|
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 x = readln.chomp.to!int;
auto a = readln.chomp.to!int;
auto b = readln.chomp.to!int;
writeln((x-a)%b);
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
immutable long MOD = 10^^9 + 7;
void main() {
auto N = readln.chomp.to!int;
auto dp = new long[][](N+1, 2);
dp[0][1] = 1;
auto imos = new long[](3*N);
long INV = powmod(N-1, MOD-2, MOD);
long ans = 0;
foreach (i; 0..N) {
(dp[i+1][0] += dp[i][1] * (N - 1) % MOD) %= MOD; // 1のあとに2〜N
(dp[i+1][1] += dp[i][1]) %= MOD; // 1のあとに1
(dp[N][0] += dp[i][0] * (N - 1) % MOD) %= MOD; // 2〜Nのあとに2〜N (無限ループ)
// 2〜Nのあとに1
(imos[i+2] += dp[i][0] * INV % MOD) %= MOD;
(imos[i+N+1] -= dp[i][0] * INV % MOD) %= MOD;
(imos[i+1] += imos[i]) %= MOD;
(dp[i+1][1] += imos[i+1]) %= MOD;
}
foreach (i; N..3*N-1) {
(imos[i+1] += imos[i]) %= MOD;
(ans += imos[i+1]) %= MOD;
}
ans = (ans + dp[N].sum % MOD + MOD) % MOD;
ans.writeln;
}
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
|
import std.conv, std.stdio, std.algorithm, std.string, std.range, std.math;
void main() {
const input = readln.split.map!(to!int).array;
max(input[0]-input[1]*2, 0).writeln;
}
|
D
|
import std.stdio, std.string, std.array, std.conv;
void main () {
int[] tmp = readln.chomp.split.to!(int[]);
int n = tmp[0], m = tmp[1];
int[][] a = new int[][](n, m);
foreach (i; 0 .. n) {
int[] row = readln.chomp.split.to!(int[]);
foreach (j; 0 .. m) {
a[i][j] = row[j];
}
}
int[] b = new int[m];
foreach (j; 0 .. m) {
b[j] = readln.chomp.to!int;
}
int[] c = new int[n];
foreach (i; 0 .. n) {
foreach (j; 0 .. m) {
c[i] += a[i][j] * b[j];
}
}
foreach (x; c) x.writeln;
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
long n, m;
rd(n, m);
import std.math : sqrt;
for (long k = m; k >= 1; k--) {
if (m >= k * n && (m - k * n) % k == 0) {
writeln(k);
return;
}
}
import std.exception : enforce;
enforce(false);
}
void rd(T...)(ref T x) {
import std.stdio : readln;
import std.string : split;
import std.conv : to;
auto l = readln.split;
assert(l.length == x.length);
foreach (i, ref e; x)
e = l[i].to!(typeof(e));
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.numeric;
import std.stdio;
import std.string;
void main()
{
string s = readln.chomp;
string t = readln.chomp;
for (long diff = 0; diff < s.length; ++diff)
{
char[] n;
n.length = s.length;
for (long i = 0; i < s.length; ++i)
{
long d = i + diff;
if (s.length <= d)
{
d %= s.length;
}
n[i] = s[d];
}
if (n == t)
{
writeln("Yes");
return;
}
}
writeln("No");
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd1 = readln.split.to!(long[]), n = rd1[0], ws = rd1[1];
auto w = new long[](n), v = new long[](n);
foreach (i; 0..n) {
auto rd2 = readln.split.to!(long[]), wi = rd2[0], vi = rd2[1];
w[i] = wi;
v[i] = vi;
}
auto w0 = w[0];
foreach (ref wi; w) wi -= w0;
auto sumW = w.sum, dp = new long[][][](n+1, n+1, sumW+1);
dp[0][0][0] = 1;
foreach (i; 0..n)
foreach (j; 0..n+1)
foreach (k; 0..sumW+1) {
if (j >= 1 && k >= w[i] && dp[i][j-1][k-w[i]])
dp[i+1][j][k] = max(dp[i][j][k], dp[i][j-1][k-w[i]] + v[i]);
else
dp[i+1][j][k] = dp[i][j][k];
}
auto ans = 0L;
foreach (j; 0..n+1)
foreach (k; 0..sumW+1)
if (k + w0 * j <= ws)
ans = max(ans, dp[n][j][k]);
writeln(ans-1);
}
|
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() {
foreach(int i;1..10) foreach(int j; 1..10) {
writeln(i, "x", j, "=", i*j);
}
}
|
D
|
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
immutable int letters = 26;
string solve (string s)
{
int [letters] d;
foreach (ref c; s)
{
d[c - 'a'] += 1;
}
foreach (let; 0..letters)
{
if (d[let] == 1)
{
string res;
res ~= cast (char) (let + 'a');
d[let] -= 1;
foreach (x; 0..letters)
{
foreach (y; 0..d[x])
{
res ~= cast (char) (x + 'a');
}
}
return res;
}
}
foreach (let; 0..letters)
{
if (d[let] > 0)
{
string res;
res ~= cast (char) (let + 'a');
d[let] -= 1;
string [2] add;
foreach (x; 0..letters)
{
foreach (y; 0..d[x])
{
add[x > let] ~= cast (char) (x + 'a');
}
}
if (add[0].length <= add[1].length + 1)
{
while (!add[0].empty || !add[1].empty)
{
foreach (v; 0..2)
{
if (!add[v].empty)
{
res ~= add[v][0];
add[v].popFront ();
}
}
}
return res;
}
if (add[1].empty || add[1].front == add[1].back)
{
res ~= add[1];
res ~= add[0];
return res;
}
auto second = add[1][0];
res ~= second;
add[1].popFront ();
res ~= add[0];
foreach (i, ref c; add[1])
{
if (c != second)
{
res ~= c;
res ~= add[1][0..i];
res ~= add[1][i + 1..$];
return res;
}
}
assert (false);
}
}
assert (false);
}
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto s = readln.strip;
writeln (solve (s));
}
}
|
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 bignum = 1_000_000_007;
alias Pair = Tuple!(long, "number", long, "times");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
auto n = lread();
auto a = aryread();
long odd, multifour, othereven;
foreach (e; a)
{
if (e % 4 == 0)
multifour++;
else if (e % 2 == 0)
othereven++;
else
odd++;
}
// othereven.writeln();
// multifour.writeln();
// odd.writeln();
if (othereven % 2 + odd <= multifour + 1)
if (multifour > 0 || odd == 0)
{
writeln("Yes");
return;
}
writeln("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 BS = readln.split.to!(int[]);
int r;
foreach (i; 0..N) {
if (i == 0) {
r += BS[i];
} else if (i == N-1) {
r += BS[i-1];
} else {
r += min(BS[i], BS[i-1]);
}
}
writeln(r);
}
|
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;
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;
}
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int N;
scan(N);
auto s = readln.chomp;
int x;
int ans;
foreach (ch ; s) {
if (ch == 'I') {
x++;
chmax(ans, x);
}
else {
x--;
chmax(ans, x);
}
}
writeln(ans);
}
|
D
|
import std.stdio;
import core.stdc.stdio;
void main() {
uint n, r = 1;
scanf(`%u`, &n);
uint l = 3;
foreach(i; 0..n) {
uint k;
scanf(`%u`, &k);
uint a = k / 10;
uint b = k % 10;
if(l == a) r++;
l = b;
}
write(r);
}
|
D
|
import std.stdio, std.array, std.conv, std.typecons, std.algorithm;
T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; }
void main() {
immutable i = readln.split.to!(ulong[]);
immutable R = i[0], G = i[1], B = i[2], N = i[3];
ulong cnt = 0;
for(ulong r = 0; r <= N / R; r++) {
const ulong r_val = r * R;
if (r_val > N) break;
else if (r_val == N) cnt++;
else {
for(ulong g = 0; g <= N / G; g++) {
const ulong g_val = r*R + g*G;
if (g_val > N) break;
else if (g_val == N) cnt++;
else {
const ulong rem = (N - g_val) % B;
if (rem == 0) {
cnt++;
}
}
}
}
}
writeln(cnt);
}
|
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;
auto U = RD;
auto R = RD;
auto D = RD;
auto L = RD;
foreach (i; 0..2^^4)
{
auto arr = new int[](4);
arr[0] = i & 1;
arr[1] = (i>>1) & 1;
arr[2] = (i>>2) & 1;
arr[3] = (i>>3) & 1;
debug writeln(arr);
auto u = n - arr[0] - arr[1];
auto r = n - arr[1] - arr[2];
auto d = n - arr[2] - arr[3];
auto l = n - arr[3] - arr[0];
if (inside(U, 2 - arr[0] - arr[1], u+1) && inside(R, 2 - arr[1] - arr[2], r+1) &&
inside(D, 2 - arr[2] - arr[3], d+1) && inside(L, 2 - arr[3] - arr[0], l+1))
{
ans[ti] = true;
break;
}
}
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush;
debug 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, core.stdc.string;
void main() {
auto N = readln.chomp.to!int;
auto S = readln.chomp;
foreach (i; 0..N-1) {
if (S[i] > S[i+1]) {
writeln("YES");
writeln(i+1, " ", i+2);
return;
}
}
writeln("NO");
}
|
D
|
import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;
import std.numeric;
void main()
{
int n, k;
readln.chomp.split.tie(n, k);
if (2 * (n - 2) < k) {
"NO".writeln;
return;
}
"YES".writeln;
char[][] ans = new char[][](4, n);
foreach (i; 0..4) {
foreach (j; 0..n) {
ans[i][j] = '.';
}
}
// vertical
for (int i = 1; i <= 2; ++i) {
if (k < n - 2) {
break;
}
for (int j = 1; j < n - 1; ++j) {
ans[i][j] = '#';
}
k -= n - 2;
}
immutable MID = n / 2;
if (k % 2 == 1) {
ans[2][MID] = '#';
--k;
}
for (int i = 1; k > 0; ++i) {
ans[2][MID - i] = ans[2][MID + i] = '#';
k -= 2;
}
foreach (s; ans) {
foreach (c; s) {
c.write;
}
writeln;
}
}
void tie(R, Args...)(R arr, ref Args args)
if (isRandomAccessRange!R || isArray!R)
in
{
assert (arr.length == args.length);
}
body
{
foreach (i, ref v; args) {
alias T = typeof(v);
v = arr[i].to!T;
}
}
void verbose(Args...)(in Args args)
{
stderr.write("[");
foreach (i, ref v; args) {
if (i) stderr.write(", ");
stderr.write(v);
}
stderr.writeln("]");
}
|
D
|
module _;
void main() {
import std.stdio, std.conv, std.string;
int n =readln.strip.to!int;
int[char] cnt;
foreach(i; 0..n) {
cnt[readln[0]]++;
}
int ans = 0;
foreach(k; cnt.byKey) {
ans += cnt[k]*(cnt[k]-1)/2;
int half = cnt[k]/2;
ans -= half * (cnt[k]-half);
}
writeln(ans);
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;
void main() {
auto N = readln.chomp.to!int;
auto A = readln.split.map!(to!long).array;
auto ans = new long[](N);
long a = 0;
foreach_reverse (i; 0..N) {
long m = (A[i] + a) % N;
long b = ((i - m) % N + N) % N;
ans[i] = b;
a += b;
}
writeln(N+1);
foreach (i; 0..N) {
writeln(1, " ", N-i, " ", ans[N-i-1]);
}
writeln(2, " ", N, " ", N);
}
|
D
|
import std.algorithm;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto n = readln.strip.to !(int);
auto a = readln.splitter.map !(to !(long)).array;
long [] b;
foreach (i; 1..n)
{
b ~= abs (a[i] - a[i - 1]);
}
int res = 1;
int lo = 0;
long cur = 0;
for (int hi = 0; hi < n - 1; hi++)
{
long next = gcd (cur, b[hi]);
if (next == 1)
{
next = 0;
lo = hi + 1;
while (lo > 0)
{
long temp = gcd (next, b[lo - 1]);
if (temp == 1)
{
break;
}
lo -= 1;
next = temp;
}
}
cur = next;
res = max (res, hi - lo + 2);
}
writeln (res);
}
}
|
D
|
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
void main() {
debug {
enum lim = 100;
auto dp = new int[][][][](lim, lim, 2, 2);
foreach (a; 0 .. lim) foreach (b; 0 .. lim) foreach (s; 0 .. 2) foreach (t; 0 .. 2) {
if (a + b == 0) {
dp[a][b][s][t] = s;
} else if (t == 1) {
dp[a][b][s][t] = 0;
if (a > 0 && dp[a - 1][b][s ^ 0][t ^ 1] == 1) dp[a][b][s][t] = 1;
if (b > 0 && dp[a][b - 1][s ^ 1][t ^ 1] == 1) dp[a][b][s][t] = 1;
} else {
dp[a][b][s][t] = 1;
if (a > 0 && dp[a - 1][b][s][t ^ 1] == 0) dp[a][b][s][t] = 0;
if (b > 0 && dp[a][b - 1][s][t ^ 1] == 0) dp[a][b][s][t] = 0;
}
}
foreach (a; 0 .. lim) foreach (b; 0 .. lim) {
if (b % 2 != 0) {
// writeln(a, " ", b, ": ", dp[a][b]);
foreach (s; 0 .. 2) foreach (t; 0 .. 2) {
assert(dp[a][b][s][t] == ((a % 2 == 0) ? (((b >> 1) & 1) ^ s ^ t) : t));
}
}
}
}
try {
for (; ; ) {
const numCases = readInt();
foreach (caseId; 0 .. numCases) {
const N = readInt();
auto A = new long[N];
foreach (i; 0 .. N) {
A[i] = readLong();
}
long sum;
foreach (i; 0 .. N) {
sum ^= A[i];
}
if (sum == 0) {
writeln("DRAW");
} else {
const e = bsr(sum);
auto cnt = new int[2];
foreach (i; 0 .. N) {
++cnt[cast(int)((A[i] >> e) & 1)];
}
debug {
writeln(cnt);
}
const ans = (cnt[0] % 2 == 0) ? (((cnt[1] >> 1) & 1) ^ 1) : 1;
writeln(ans ? "WIN" : "LOSE");
}
}
}
} catch (EOFException e) {
}
}
|
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 N = RD!int;
auto M = RD!int;
auto a = RDR.ARR;
auto b = RDR.ARR;
long cnt_a;
foreach (i; 0..N)
{
if (a[i] % 2 == 1)
++cnt_a;
}
long cnt_b;
foreach (i; 0..M)
{
if (b[i] % 2 == 1)
++cnt_b;
}
long ans;
ans += min(cnt_a, M - cnt_b);
ans += min(cnt_b, N - cnt_a);
writeln(ans);
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()
{
auto r = readln.chomp.to!int;
writeln(r * r);
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.7.3"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
int main() {
Scanner sc = new Scanner(stdin);
string s;
sc.read(s);
if (s.count("AC")) {
writeln("Yes");
} else {
writeln("No");
}
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
// import dcomp.array;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
FastAppender!(E[]) buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/array.d */
// module dcomp.array;
T[N] fixed(T, size_t N)(T[N] a) {return a;}
struct FastAppender(A, size_t MIN = 4) {
import std.algorithm : max;
import std.conv;
import std.range.primitives : ElementEncodingType;
import core.stdc.string : memcpy;
private alias T = ElementEncodingType!A;
private T* _data;
private uint len, cap;
@property size_t length() const {return len;}
bool empty() const { return len == 0; }
void reserve(size_t nlen) {
import core.memory : GC;
if (nlen <= cap) return;
void* nx = GC.malloc(nlen * T.sizeof);
cap = nlen.to!uint;
if (len) memcpy(nx, _data, len * T.sizeof);
_data = cast(T*)(nx);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void opOpAssign(string op : "~")(T item) {
if (len == cap) {
reserve(max(MIN, cap*2));
}
_data[len++] = item;
}
void insertBack(T item) {
this ~= item;
}
void removeBack() {
len--;
}
void clear() {
len = 0;
}
ref inout(T) back() inout { assert(len); return _data[len-1]; }
ref inout(T) opIndex(size_t i) inout { return _data[i]; }
T[] data() {
return (_data) ? _data[0..len] : null;
}
}
/*
This source code generated by dcomp and include dcomp's source code.
dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp)
dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt)
*/
|
D
|
void main()
{
long n = rdElem;
long limit = 55555;
auto sieve = new Prime(limit);
long cnt;
foreach (i; 1.iota(limit+1).stride(5))
{
if (sieve.isPrime(i))
{
i.write;
++cnt;
if (cnt >= n)
{
writeln;
break;
}
else
{
" ".write;
}
}
}
}
struct Prime
{
bool[] flags;
this(long limit)
{
flags = makeSieve(limit);
}
bool[] makeSieve(long limit)
{
long len = (limit - 1) / 2;
bool[] result = new bool[len];
result[] = true;
foreach (i; 0 .. len)
{
if (!result[i]) continue;
long prime = 3 + 2 * i;
foreach (j; iota(prime+i, len, prime))
{
result[j] = false;
}
}
return result;
}
bool isPrime(long number)
in
{
assert((number - 1) / 2 <= flags.length);
}
body
{
if (number < 2)
return false;
else if (number == 2)
return true;
else if (number % 2 == 0)
return false;
else
return flags[(number-3)/2];
}
}
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.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
|
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 isPrime(int n) {
if (n < 2)
return false;
if (n == 2)
return true;
if (n % 2 == 0)
return false;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0)
return false;
}
return true;
}
void main() {
string s;
while ((s = readln.chomp) != null) {
int n = s.to!int;
int ans = 0;
for (int i = 1; i <= n; i++) {
int a = i;
int b = n - i + 1;
if (isPrime(a) && isPrime(b))
ans++;
}
writeln(ans);
}
}
|
D
|
import std.stdio, std.math, std.algorithm, std.array, std.string, std.conv, std.container, std.range;
pragma(inline, true) T[] Reads(T)() { return readln.split.to!(T[]); }
alias reads = Reads!int;
pragma(inline, true) void scan(Args...)(ref Args args) {
string[] ss = readln.split;
foreach (i, ref arg ; args) arg = ss[i].parse!int;
}
void main() {
int a,b; scan(a,b);
writeln(max(a+b, a-b, a*b));
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
long bignum = 1_000_000_007;
auto dp = new long[](100_100);
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, m;
scan(n, m);
auto denger = new long[](m);
foreach (ref e; denger)
{
e = lread();
}
long[] tmp = [0];
auto need = step(denger ~ tmp, n);
need.writeln();
}
long step(long[] denger, long n)
{
ulong j;
if (denger[j] != 1)
dp[0] = 1;
else
j++;
if (denger[j] != 2)
dp[1] = dp[0] + 1;
else
j++;
foreach (i; 2 .. n)
{
if (i == denger[j] - 1)
{
dp[i] = 0;
j++;
}
else
{
dp[i] = (dp[i - 1] + dp[i - 2]) % bignum;
}
}
return dp[n - 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;
import std.container;
alias sread = () => readln.chomp();
ulong bignum = 1_000_000_007;
alias Pair = Tuple!(long, "number", long, "times");
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();
auto all_have = new long['z' - 'a' + 1];
foreach (i; 0 .. n)
{
auto s = sread();
auto now = new long['z' - 'a' + 1];
foreach (e; s)
{
if (!i)
all_have[e - 'a']++;
now[e - 'a']++;
}
foreach (e; 'a' .. 'z' + 1)
{
all_have[e - 'a'] = min(now[e - 'a'], all_have[e - 'a']);
}
}
foreach (i, e; all_have)
{
if(e)
{
long j;
while(j++ < e)
write(cast(char)('a' + i));
}
}
writeln();
}
|
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;
void main(){
int n;
long x;
{
long[] tmp = readln.chomp.split.map!(to!long).array;
n = cast(int) tmp[0];
x = tmp[1];
}
long[] as = readln.chomp.split.map!(to!long).array;
long[][] qs;
qs.length = n;
for(int i = 0; i < n; i ++){
for(int k = 0; k < n; k ++){
int j = i - k;
if(j < 0) j += n;
long u = as[j];
if(qs[i].length && qs[i][$ - 1] < u) u = qs[i][$ - 1];
qs[i] ~= u;
}
}
long ans = long.max;
for(int k = 0; k < n; k ++){
long a = x * k;
foreach(q; qs){
a += q[k];
}
ans = min(ans, a);
}
ans.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 a = readln.chomp.to!int;
auto b = readln.chomp.to!int;
auto c = readln.chomp.to!int;
auto x = readln.chomp.to!int;
int cnt;
foreach (i; 0..a+1) {
foreach (j; 0..b+1) {
foreach (k; 0..c+1) {
if (i*500+j*100+k*50 == x) {
cnt++;
}
}
}
}
cnt.writeln;
}
|
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 = scanChar;
auto B = scanChar;
if(A<B)end("<");
if(A>B)end(">");
end("=");
}
|
D
|
void main()
{
long n, r;
rdVals(n, r);
writeln(n >= 10 ? r : r + 100 * (10 - n));
}
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.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, c;
scan(a, b, c);
int ans = max(0, c - (a - b));
writeln(ans);
}
int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new int[][](n, 0);
foreach (i; 0 .. m) {
int u, v;
scan(u, v);
if (is1indexed) {
u--, v--;
}
adj[u] ~= v;
if (isUndirected) {
adj[v] ~= u;
}
}
return adj;
}
alias Edge = Tuple!(int, "to", int, "cost");
Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new Edge[][](n, 0);
foreach (i; 0 .. m) {
int u, v, c;
scan(u, v, c);
if (is1indexed) {
u--, v--;
}
adj[u] ~= Edge(v, c);
if (isUndirected) {
adj[v] ~= Edge(u, c);
}
}
return adj;
}
void yes(bool b) {
writeln(b ? "Yes" : "No");
}
void YES(bool b) {
writeln(b ? "YES" : "NO");
}
T[] readArr(T)() {
return readln.split.to!(T[]);
}
T[] readArrByLines(T)(int n) {
return iota(n).map!(i => readln.chomp.to!T).array;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.algorithm;
void main()
{
import std.stdio, std.conv, std.string;
readln.chomp.to!int.solve.writeln;
}
int solve(int x)
{
auto result = int.max;
foreach (i; 0..x+1)
result = min(result, simulate(x, i));
return result;
}
int simulate(int x, int i)
{
return i.withdraw(6) + (x - i).withdraw(9);
}
int withdraw(int x, int radix)
{
int result;
while (x)
{
result += x % radix;
x /= radix;
}
return result;
}
|
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 = lread();
long cnt;
long ans = 1;
while (1 < H)
{
H /= 2;
cnt++;
ans += 2 ^^ cnt;
}
writeln(ans);
}
|
D
|
void main() {
problem();
}
void problem() {
auto N = scan.map!(x => x - '0').array;
bool solve() {
ulong sumDecimal;
foreach(n; N) sumDecimal += n;
return sumDecimal % 9 == 0;
}
writeln(solve() ? "Yes" : "No");
}
// ----------------------------------------------
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.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!long;
char[] res;
while (N) {
auto n = N % 26;
if (n == 0) {
n = 25;
N -= 1;
} else {
n -= 1;
}
res ~= 'a' + n.to!char;
N /= 26;
}
res.reverse();
writeln(res);
}
|
D
|
import std.stdio, std.conv, std.string, std.algorithm,
std.math, std.array, std.container, std.typecons;
void main() {
if(readln.chomp=="ABC") writeln("ARC");
else writeln("ABC");
}
|
D
|
import std.algorithm, std.array, std.container, std.range;
import std.string, std.uni, std.regex;
import std.stdio, std.conv, std.typecons;
void main()
{
for (;;) {
auto s = readln.chomp;
if (s == "-") break;
auto m = readln.chomp.to!size_t;
auto hi = iota(m).map!(_ => readln.chomp.to!int);
foreach (h; hi)
s = s[h..$] ~ s[0..h];
writeln(s);
}
}
|
D
|
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop;
// dfmt on
void main()
{
long N = lread();
auto A = aryread();
long ans = long.max;
foreach (a; A)
{
long cnt;
long t = a;
while (t % 2 == 0)
cnt++, t /= 2;
ans = ans.min(cnt);
}
writeln(ans);
}
|
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()
{
char c;
scan(c);
auto s = "aeiou";
foreach(cc;s)
{
if (c == cc)
{
writeln("vowel");
return;
}
}
writeln("consonant");
}
|
D
|
import std.stdio, std.algorithm, std.range, std.conv, std.string;
import core.stdc.stdio;
// foreach, foreach_reverse, writeln
void main() {
int n;
scanf("%d", &n);
int[] x = new int[n*2];
int[] c = new int[n*2];
foreach (color; 0..2) {
foreach (i; 0..n) {
int a, b;
scanf("%d%d", &a, &b);
x[b] = a; c[b] = color;
}
}
int ans = 0;
int[] set = new int[n*2];
foreach (y; 0..n*2) {
if (c[y] == 0) {
set[x[y]] = 1;
} else {
foreach_reverse (i; 0..x[y]) {
if (set[i]) {
ans++;
set[i] = 0;
break;
}
}
}
}
writeln(ans);
}
|
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;
void main()
{
while (1) {
auto n = readln.chomp.to!int;
if (!n) break;
auto f = new bool[][](n, n);
auto a = new int[][](n, n);
auto b = new int[][](n, n);
foreach (i; 0..n) {
a[i] = readln.chomp.split.map!(to!int).array;
auto m = a[i].reduce!(min);
foreach (j; 0..n) {
b[j][i] = a[i][j];
if (a[i][j] == m) {
f[i][j] = 1;
}
}
}
int res;
foreach (j; 0..n) {
auto m = b[j].reduce!(max);
foreach (i; 0..n) {
if (b[j][i] == m && f[i][j]) {
res = m;
}
}
}
res.writeln;
}
}
|
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;
scan(n);
foreach (i ; 0 .. n) {
string s;
scan(s);
solve(s);
}
}
void solve(string s) {
auto a = [".,!? ", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"];
auto ss = s.group;
string res;
foreach (si ; ss) {
if (si[0] == '0') continue;
int d = si[0] - '0' - 1;
res ~= a[d][(si[1]-1) % a[d].length];
}
writeln(res);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
import std.range : iota;
int h, w;
rd(h, w);
const int n = 10;
auto c = new int[][](n, n);
iota(0, n).each!((i) => (c[i] = readln.split.to!(int[])));
foreach (_; 0 .. n)
foreach (i; 0 .. n)
foreach (j; 0 .. n)
c[i][j] = min(c[i][j], c[i][_] + c[_][j]);
int cost = 0;
foreach (_; 0 .. h) {
auto a = readln.split.to!(int[]);
cost += reduce!((res, val) => res + (val < 0 ? 0 : c[val][1]))(0, a);
}
writeln(cost);
}
void rd(T...)(ref T x) {
import std.stdio : readln;
import std.string : split;
import std.conv : to;
auto l = readln.split;
assert(l.length == x.length);
foreach (i, ref e; x)
e = l[i].to!(typeof(e));
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
void main() {
int n = readln.chomp.to!int;
if (n % 2) (n/2 +1).writeln;
else (n/2).writeln;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
enum MOD = (10 ^^ 9) + 7;
void main()
{
long N, M;
scan(N, M);
auto A = new string[](N);
auto B = new string[](M);
foreach (i; 0 .. N)
scan(A[i]);
foreach (i; 0 .. M)
scan(B[i]);
foreach (i; 0 .. N - M + 1)
foreach (j; 0 .. N - M + 1)
{
bool b = true;
foreach (k; 0 .. M)
b = b && (A[i + k][j .. j + M] == B[k]);
if (b)
{
writeln("Yes");
return;
}
}
writeln("No");
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void main()
{
int N = readln.chomp.to!int;
long[] xs, ys;
foreach (_; 0..N) {
auto xy = readln.split.to!(long[]);
xs ~= xy[0];
ys ~= xy[1];
}
long max_a = long.min, min_a = long.max, max_b = long.min, min_b = long.max;
foreach (i; 0..N) {
auto a = xs[i] - ys[i];
max_a = max(max_a, a);
min_a = min(min_a, a);
auto b = xs[i] + ys[i];
max_b = max(max_b, b);
min_b = min(min_b, b);
}
writeln(max(max_a - min_a, max_b - min_b));
}
|
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, a; readV(n, a);
int[] x; readA(n, x);
auto s = x.sum, dp = new long[][][](n+1, n+1, s+1);
dp[0][0][0] = 1;
foreach (i; 0..n)
foreach (j; 0..n+1)
foreach (k; 0..s+1) {
dp[i+1][j][k] = dp[i][j][k];
if (j >= 1 && k >= x[i])
dp[i+1][j][k] += dp[i][j-1][k-x[i]];
}
auto r = 0L;
foreach (j; 1..n+1) {
if (j*a > s) break;
r += dp[n][j][j*a];
}
writeln(r);
}
|
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;
long m, b;
void main() {
scan(m, b);
long ans = -1;
foreach (long a ; 0L .. m * b + 1) {
long ya = (m*b - a) / m;
long v = a * (a + 1) / 2L * (ya + 1L) + ya * (ya + 1L) / 2L * (a + 1L);
debug {
writefln("a: %s, v: %s", a, v);
}
ans = max(ans, v);
}
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, 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;
auto es = new int[](N);
auto ws = new int[](N);
foreach (i; 0..N) {
if (i > 0) ws[i] += ws[i-1];
if (S[i] == 'W') ws[i] += 1;
}
foreach_reverse (i; 0..N) {
if (i < N-1) es[i] += es[i+1];
if (S[i] == 'E') es[i] += 1;
}
int r = N;
foreach (i; 0..N) {
int rr;
if (i > 0) rr += ws[i-1];
if (i < N-1) rr += es[i+1];
r = min(r, rr);
}
writeln(r);
}
|
D
|
import std.stdio;
import std.array;
import std.conv;
int gcd(int x, int y) {
if(x < y) {
int t = x;
x = y;
y = t;
}
if(y == 0) {
return x;
}
return gcd(y, x % y);
}
void main() {
int a, b;
while(true) {
string[] input = split(readln());
if(input.length == 0) break;
a = to!int(input[0]);
b = to!int(input[1]);
int gcd_ab = gcd(a, b);
int lcm_ab = a / gcd_ab * b;
writeln(gcd_ab, " ", lcm_ab);
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto ns = readln.split;
foreach (a; ns) {
foreach (b; ns) {
foreach (c; ns) {
foreach (d; ns) {
if (a~b~c~d == "1974") {
writeln("YES");
return;
}
}
}
}
}
writeln("NO");
return;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void main()
{
auto S = readln.chomp;
auto T = readln.chomp;
int r = 1000;
foreach (i; 0..S.length - T.length + 1) {
int rr;
foreach (j, t; T) if (S[i+j] != t) ++rr;
r = min(r, rr);
}
writeln(r);
}
|
D
|
void main() {
problem();
}
void problem() {
auto a = scan!long;
string solve() {
return a >= 30 ? "Yes" : "No";
}
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.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; }
struct SegTree(T)
{
T delegate(const(T), const(T)) f;
T[] data;
T init;
this(T[] _data, T delegate(const(T), const(T)) _f, T _init)
{
f = _f; init = _init;
size_t n = 1; while (n < _data.length) n *= 2; data.length = n*2-1;
foreach (i; 0.._data.length) data[i+n-1] = _data[i];
foreach (i; _data.length..n) data[i+n-1] = _init;
foreach_reverse (i; 0..n-1) data[i] = f(data[i*2+1], data[i*2+2]);
}
T query(size_t l, size_t r) const
{
size_t n = (data.length+1) / 2; l += n-1; r += n-1; T res = f(init, data[l]);
while (l < r)
{
if ((l & 1) == 0) res = f(res, data[l]);
if ((r & 1) == 0) res = f(res, data[r-1]);
l = l/2; r = (r-1)/2;
}
return res;
}
void update(size_t i, T v)
{
i += (data.length+1) / 2 - 1; data[i] = v;
while (i != 0) { i = (i-1)/2; data[i] = f(data[i*2+1], data[i*2+2]); }
}
}
void main()
{
auto s = RD!string;
auto a = new int[][](s.length, 26);
foreach (i, c; s)
{
auto pos = c-'a';
a[i][pos] = 1;
}
auto ini = new int[](26);
auto st = SegTree!(int[])(a, (const(int[]) _a, const(int[]) _b){ auto c = _a.dup; c[] += _b[]; return c; }, ini);
auto q = RD!int;
long[] ans;
foreach (i; 0..q)
{
auto num = RD!int;
if (num == 1)
{
auto pos = RD!int-1;
auto c = RD!string;
auto tmp = new int[](26);
tmp[c[0]-'a'] = 1;
st.update(pos, tmp);
}
else
{
auto l = RD!int-1;
auto r = RD!int;
auto tmp = st.query(l, r);
long cnt;
foreach (e; tmp)
if (e >= 1) ++cnt;
ans ~= cnt;
}
}
foreach (e; ans)
writeln(e);
stdout.flush();
debug readln();
}
|
D
|
import core.bitop;
import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.format;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void main()
{
foreach (i, c; readln.chomp) {
if (i % 2 == 1) {
continue;
}
c.write;
}
writeln;
}
|
D
|
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop;
// dfmt on
void main()
{
long H, W;
scan(H, W);
string[] A = generate(sread).take(H).array;
auto h = new bool[](H);
auto w = new bool[](W);
foreach (y; 0 .. H)
{
if (A[y].all!"a == '.'")
h[y] = true;
}
foreach (x; 0 .. W)
{
if (iota(H).all!(a => A[a][x] == '.'))
w[x] = true;
}
foreach (y; 0 .. H)
{
if (h[y])
continue;
foreach (x; 0 .. W)
{
if (w[x])
continue;
write(A[y][x]);
}
writeln();
}
}
|
D
|
import std.algorithm;
import std.conv;
import std.exception;
import std.stdio;
import std.string;
import std.typecons;
struct Queue(T){
T[900] content;
size_t head;
size_t tail;
T front(){
enforce(head != tail);
return content[head];
}
void popFront(){
enforce(head != tail);
++head;
}
void pushBack(T value){
content[tail] = value;
++tail;
}
void clear(){
head = 0;
tail = 0;
}
size_t length(){
return tail - head;
}
}
void main(){
bool[900][900] M;
Queue!int queue;
int[900] d;
while(true){
auto input = readln.split;
if(input[0] == "0" && input[1] == "0"){
break;
}
int w = input[0].to!int;
int h = input[1].to!int;
foreach(i; 0..h){
{
auto line = readln.split;
foreach(j; 0..w - 1){
if(line[j] == "0"){
M[i * w + j][i * w + j + 1] = true;
M[i * w + j + 1][i * w + j] = true;
}else{
M[i * w + j][i * w + j + 1] = false;
M[i * w + j + 1][i * w + j] = false;
}
}
}
{
if(i == h - 1){
break;
}
auto line = readln.split;
foreach(j; 0..w){
if(line[j] == "0"){
M[i * w + j][(i + 1) * w + j] = true;
M[(i + 1) * w + j][i * w + j] = true;
}else{
M[i * w + j][(i + 1) * w + j] = false;
M[(i + 1) * w + j][i * w + j] = false;
}
}
}
}
d[1..w * h] = 0;
d[0] = 1;
queue.clear;
queue.pushBack(0);
while(queue.length){
int v = queue.front;
queue.popFront;
if(v - w >= 0 && M[v][v - w] && !d[v - w]){
d[v - w] = d[v] + 1;
queue.pushBack(v - w);
}
if(v % w + 1 < w && M[v][v + 1] && !d[v + 1]){
d[v + 1] = d[v] + 1;
queue.pushBack(v + 1);
}
if(v + w < w * h && M[v][v + w] && !d[v + w]){
d[v + w] = d[v] + 1;
queue.pushBack(v + w);
}
if(v % w - 1 >= 0 && M[v][v - 1] && !d[v - 1]){
d[v - 1] = d[v] + 1;
queue.pushBack(v - 1);
}
}
d[w * h - 1].writeln;
}
}
|
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;
ulong MOD = 1_000_000_007;
ulong INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
void main()
{
auto n = lread();
bool check;
foreach (i; iota(1, 10))
check |= !(n % i) && (n / i < 10);
if (check)
writeln("Yes");
else
writeln("No");
}
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;
}
}
|
D
|
unittest
{
assert( [ "rng gorilla apple" ].parse.expand.solve == "YES" );
assert( [ "yakiniku unagi sushi" ].parse.expand.solve == "NO" );
assert( [ "a a a" ].parse.expand.solve == "YES" );
assert( [ "aaaaaaaaab aaaaaaaaaa aaaaaaaaab" ].parse.expand.solve == "NO" );
}
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void main()
{
stdin.byLineCopy.parse.expand.solve.writeln;
}
auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
auto abc = input.front.split;
return tuple( abc[ 0 ], abc[ 1 ], abc[ 2 ] );
}
auto solve( string a, string b, string c )
{
return ( a[ $ - 1 ] == b[ 0 ] && b[ $ - 1 ] == c[ 0 ] ) ? "YES" : "NO";
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
void main()
{
writeln(readln.chomp.to!int / 3);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
void main()
{
string n;
n = readln;
if (
n[0..1] == "7"
|| n[1..2] == "7"
|| n[2..3] == "7"
) {
writeln("Yes");
} else {
writeln("No");
}
}
|
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;
void main(){
string s = readln.chomp;
int n = s.length.to!int;
int ans = 0;
foreach(i; 0 .. n){
if(i % 2){ // 自分はp
if(s[i] == 'p') ans += 0; // pp
else ans += 1; // pg
}
else{ // 自分はg
if(s[i] == 'p') ans += -1; // gp
else ans += 0; // gg
}
}
ans.writeln;
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd1 = readln.split, w = rd1[0].to!int, h = rd1[1].to!int, n = rd1[2].to!size_t;
auto x1 = 0, x2 = w, y1 = 0, y2 = h;
foreach (_; 0..n) {
auto rd2 = readln.split.to!(int[]), x = rd2[0], y = rd2[1], a = rd2[2];
switch (a) {
case 1: x1 = max(x1, x); break;
case 2: x2 = min(x2, x); break;
case 3: y1 = max(y1, y); break;
case 4: y2 = min(y2, y); break;
default: assert(0);
}
}
writeln(x1 >= x2 || y1 >= y2 ? 0 : (x2-x1) * (y2-y1));
}
|
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;
import std.datetime, std.bigint;
immutable long mod = 10^^9 + 7;
int n;
int[] t, a;
void main() {
scan(n);
t = readln.split.to!(int[]);
a = readln.split.to!(int[]);
auto L = new int[](n);
auto H = new int[](n);
L[0] = H[0] = t[0];
foreach (i ; 1 .. n) {
if (t[i - 1] < t[i]) {
L[i] = H[i] = t[i];
}
else {
L[i] = 1;
H[i] = t[i];
}
}
if (!(L[n - 1] <= a[n - 1] && a[n - 1] <= H[n - 1])) {
writeln(0);
return;
}
L[n - 1] = H[n - 1] = a[n - 1];
foreach_reverse (i ; 0 .. n - 1) {
if (a[i] > a[i + 1]) {
if (L[i] <= a[i] && a[i] <= H[i]) {
L[i] = H[i] = a[i];
}
else {
writeln(0);
return;
}
}
else {
H[i] = min(H[i], a[i]);
}
}
long ans = 1;
foreach (i ; 0 .. n) {
(ans *= (H[i] - L[i] + 1)) %= mod;
}
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.algorithm;
import std.string;
import std.functional;
import std.array;
import std.conv;
import std.math;
import std.typecons;
import std.regex;
import std.range;
void main(){
string n = readln();
while(true){
char[5][8] s;
for(int i=0;i<5;i++){
s[0][i] = '*';
s[1][i] = ' ';
s[2][i] = '=';
s[3][i] = ' ';
s[4][i] = '*';
s[5][i] = '*';
s[6][i] = '*';
s[7][i] = '*';
}
n = n.chomp();
int si = to!int(n.length);
for(int i=si-1;i>=0;i--){
int m = to!int(n[i]-'0');
if(m >= 5){
s[0][5-si+i] = ' ';
s[1][5-si+i] = '*';
m -= 5;
}
s[3][5-si+i] = '*';
s[4][5-si+i] = '*';
s[5][5-si+i] = '*';
s[6][5-si+i] = '*';
s[7][5-si+i] = '*';
s[3+m][5-si+i] = ' ';
}
foreach(i;s){
foreach(j;i) write(j);
writeln();
}
n = readln();
if(stdin.eof()) break;
writeln();
}
}
|
D
|
void main()
{
long n = readln.chomp.to!long;
long[] a = readln.split.to!(long[]);
writeln(a.count!"a % 2 == 1" % 2 == 0 ? "YES" : "NO");
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.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!long);
auto A = s[0];
auto B = s[1];
auto C = s[2];
auto X = s[3];
auto Y = s[4];
long ans = 1L << 59;
for (long ab = 0; ab <= 2 * max(X, Y); ab += 2) {
long a = max(0, X - ab/2);
long b = max(0, Y - ab/2);
ans = min(ans, a * A + b * B + ab * C);
}
ans.writeln;
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n; rd(n);
int sum=0;
for(int div=1; n/div>0; div*=10){
sum+=(n/div)%10;
}
assert(sum>0);
if(n%sum==0) writeln("Yes");
else writeln("No");
}
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) stderr.write(e, " ");
stderr.writeln;
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
auto F = new uint[](N);
auto P = new long[][](N);
foreach (i; 0 .. N)
{
auto tmp = aryread!uint();
foreach (j; 0 .. 10)
F[i] = F[i] | (tmp[j] << j);
}
foreach (i; 0 .. N)
P[i] = aryread();
long eval(uint state)
{
long ret;
foreach (i; 0 .. N)
{
ret += P[i][popcnt(state & F[i])];
}
return ret;
}
long ans = long.min;
foreach (uint s; 1 .. (1 << 10))
{
ans = ans.max(eval(s));
}
writeln(ans);
}
|
D
|
import std.stdio, std.string, std.array, std.conv;
void main() {
int[] tmp = readln.chomp.split.to!(int[]);
int a = tmp[0] - 1, b = tmp[1] - 1;
writeln((a + b - 1) / a);
}
|
D
|
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto n = readln.strip.to !(int);
auto s = readln.splitter.map !(to !(int)).array;
s ~= 0;
long res = 0;
auto p = iota (n + 1).array;
for (int i = 0; i < n; i++)
{
int add = max (0, s[i] - n + i);
res += add;
s[i] = max (0, s[i] - add);
while (s[i] > 1)
{
for (int j = i; j < n; j += s[j] + 1)
{
if (s[j] <= 1)
{
p[j] = p[j + 1];
}
j = p[j];
s[j] = max (0, s[j] - 1);
}
res += 1;
}
}
writeln (res);
}
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N, M, Q;
scan(N, M, Q);
auto T = new long[][](N + 1, N + 1);
foreach (_; 0 .. M)
{
long L, R;
scan(L, R);
T[L][R]++;
}
foreach (i; 0 .. N)
T[i + 1][] += T[i][];
foreach (i; 0 .. N)
foreach (j; 0 .. N)
T[i + 1][j + 1] += T[i + 1][j];
foreach (_; 0 .. Q)
{
long p, q;
scan(p, q);
writeln(T[q][q] - T[q][p - 1] - T[p - 1][q] + T[p - 1][p - 1]);
}
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random;
import std.typecons, std.functional, std.traits,std.concurrency;
import std.algorithm, std.container;
import core.bitop, core.time, core.memory;
import std.bitmanip;
import std.regex;
enum INF = long.max/3;
enum MOD = 10L^^9+7;
//辞書順順列はiota(1,N),nextPermituionを使う
void end(T)(T v)
if(isIntegral!T||isSomeString!T||isSomeChar!T)
{
import core.stdc.stdlib;
writeln(v);
exit(0);
}
T[] scanArray(T = long)()
{
static char[] scanBuf;
readln(scanBuf);
return scanBuf.split.to!(T[]);
}
dchar scanChar()
{
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
return cast(dchar)c;
}
T scanElem(T = long)()
{
import core.stdc.stdlib;
static auto scanBuf = appender!(char[])([]);
scanBuf.clear;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
scanBuf ~= cast(char) c;
c = getchar;
}
return scanBuf.data.to!T;
}
dchar[] scanString(){
return scanElem!(dchar[]);
}
void main()
{
auto S1 = scanString;
auto S2 = scanString;
foreach(i;0..3)if(S1[i]!=S2[2-i])end("NO");end("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, std.bitmanip;
immutable long MOD = 10^^9 + 7;
long[] F;
long[] G;
void main() {
auto N = readln.chomp.to!int;
auto A = readln.split.map!(to!long).array;
auto B = new long[](N+1);
foreach (i; 0..N) B[i+1] = (B[i] + A[i]) % MOD;
F = new long[](N+1);
G = new long[](N+1);
F[0] = G[0] = 1;
foreach (i; 1..N+1) F[i] = F[i-1] * i % MOD;
foreach (i; 1..N+1) G[i] = powmod(F[i], MOD-2, MOD);
long ans = 0;
long tmp = 0;
foreach (i; 1..N-1) tmp = (tmp + A[i]) % MOD;
int left = 1;
int right = N-2;
auto mem = new long[](N+2);
int p = 1;
foreach (len; 1..N+1) {
mem[len] = tmp;
if (len == N) {
ans += B[N] * F[N] % MOD;
ans %= MOD;
} else if (len == N - 1) {
ans += B[N-1] * F[N-1] % MOD;
ans += (B[N] - B[1]) * F[N-1] % MOD;
ans %= MOD;
} else {
auto comb1 = comb(N, len+1);
auto tmptmp = comb1 * F[len] % MOD * F[N-len-1] % MOD;
auto tmp1 = tmptmp * B[len] % MOD;
auto tmp2 = tmptmp * ((B[N] - B[N-len]) % MOD) % MOD;
auto tmp3 = comb(N, len+2) * 2 % MOD * F[len] % MOD * F[N-len-2] % MOD * tmp % MOD;
ans += (tmp1 + tmp2 + tmp3) % MOD;
ans %= MOD;
}
left += 1;
right -= 1;
if (left <= right) {
tmp += B[right+1] - B[left];
tmp %= MOD;
if (left != right) p += 1;
} else if (p >= 0) {
tmp = mem[p];
p -= 1;
}
}
ans = (ans % MOD + MOD) % MOD;
ans.writeln;
}
long comb(long n, long r) {
return F[n] * G[n-r] % MOD * G[r] % MOD;
}
long powmod(long a, long x, long m) {
long ret = 1;
while (x) {
if (x % 2) ret = a * ret % m;
a = a * a % m;
x /= 2;
}
return ret;
}
|
D
|
void main() {
int[] tmp = readln.split.to!(int[]);
int a = tmp[0], b = tmp[1], x = tmp[2];
writeln(a <= x && x <= a + b ? "YES" : "NO");
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
int[] e;
e.length = N+1;
foreach (i; 2..N+1) {
auto cur = i;
foreach (j; 2..i+1) {
while (cur % j == 0) {
++e[j];
cur /= j;
}
}
}
int num(int m) {
int cnt;
foreach (x; e) {
if (x >= m-1) ++cnt;
}
return cnt;
}
writeln(
num(75) +
num(25) * (num(3) - 1) +
num(15) * (num(5) - 1) +
num(5) * (num(5) - 1) * (num(3) - 2) / 2
);
}
|
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, std.datetime;
bool is_p(string s) {
int n = s.length.to!int;
foreach (i; 0..n/2) if (s[i] != s[n-i-1]) return false;
return true;
}
void main() {
auto S = readln.chomp;
auto N = S.length.to!int;
foreach_reverse (i; 1..N+1) {
foreach (j; 0..N-i+1) {
if (!is_p(S[j..j+i])) {
writeln(i);
return;
}
}
}
writeln(0);
}
|
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;
char b;
sc.scan(b);
if (b == 'A') {
writeln("T");
return;
} else if (b == 'T') {
writeln("A");
return;
} else if (b == 'C'){
writeln("G");
return;
} else if (b == 'G') {
writeln("C");
return;
}
}
|
D
|
import std.conv;
import std.stdio;
import std.string;
void main()
{
auto nk = readln.split.to!( int[] );
auto as = readln.split.to!( int[] );
writeln( solve( nk[ 1 ], as ) );
}
auto solve( in int k, in int[] as )
{
auto as_r = as.length - k;
auto k_r = k - 1;
auto c = 1 + as_r / k_r;
auto re = ( as_r % k_r == 0 ) ? 0 : 1;
return c + re;
}
unittest
{
assert( solve( 3, [ 2, 3, 1, 4 ] ) == 2 );
assert( solve( 3, [ 1, 2, 3 ] ) == 1 );
assert( solve( 3, [ 7, 3, 1, 8, 4, 6, 2, 5 ] ) == 4 );
assert( solve( 3, [ 7, 3, 6, 8, 4, 1, 2, 5 ] ) == 4 );
assert( solve( 3, [ 7, 3, 6, 1, 4, 8, 2, 5 ] ) == 4 );
assert( solve( 3, [ 7, 3, 6, 4, 1, 8, 2, 5 ] ) == 4 );
assert( solve( 3, [ 7, 3, 8, 1, 4, 6, 2, 5, 9 ] ) == 4 );
assert( solve( 3, [ 7, 3, 8, 4, 6, 1, 2, 5, 9 ] ) == 4 );
assert( solve( 3, [ 7, 3, 8, 4, 1, 6, 2, 5, 9 ] ) == 4 );
}
|
D
|
/* imports all std modules {{{*/
import
std.algorithm,
std.array,
std.ascii,
std.base64,
std.bigint,
std.bitmanip,
std.compiler,
std.complex,
std.concurrency,
std.container,
std.conv,
std.csv,
std.datetime,
std.demangle,
std.encoding,
std.exception,
std.file,
std.format,
std.functional,
std.getopt,
std.json,
std.math,
std.mathspecial,
std.meta,
std.mmfile,
std.net.curl,
std.net.isemail,
std.numeric,
std.parallelism,
std.path,
std.process,
std.random,
std.range,
std.regex,
std.signals,
std.socket,
std.stdint,
std.stdio,
std.string,
std.system,
std.traits,
std.typecons,
std.uni,
std.uri,
std.utf,
std.uuid,
std.variant,
std.zip,
std.zlib;
/*}}}*/
/+---test
13
---+/
void main(string[] args) {
const N = readln.chomp.to!long;
(N*(N-1)/2).writeln;
}
|
D
|
import std.stdio;
import std.stdio;
import std.algorithm;
import std.range;
import std.functional;
import std.conv;
import std.string;
import std.math;
import core.bitop;
void main() {
int tt = readln().chomp.to!int;
foreach (t; 0 .. tt) {
auto arr = readln().chomp.split(" ").map!(to!ulong);
ulong a = arr[0];
ulong b = arr[1];
if (b < a) {
ulong tm = a;
a = b;
b = tm;
}
if (b > a) {
if (b % a != 0) {
writeln(-1);
continue;
}
ulong diff = b / a;
ulong high = bsr(diff);
if ((1UL << high) == diff) {
writeln(
high / 3 + (high % 3) / 2 + (high % 3) % 2
);
}
else {
writeln(-1);
}
}
else {
writeln(0);
}
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.math;
int main()
{
double x=0.0,y=0.0,angle=90;
while(true)
{
string[] s = readln.chomp.split(",");
if(s[0] == "0" && s[1] == "0") break;
int value = s[0].to!int;
int value_2 = s[1].to!int;
x += cos(angle*PI/180)*value;
y += sin(angle*PI/180)*value;
angle -= value_2;
}
writeln(cast(int)x);
writeln(cast(int)y);
return 0;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
long N;
scan(N);
long ans = inf6;
for (long i = 1; i*i <= N; i++) {
if (N % i == 0) {
ans = min(ans, i + N / i - 2);
}
}
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
|
void main() {
problem();
}
void problem() {
auto X = scan!long;
long solve() {
return 8 - (X - 400) / 200;
}
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;
void main() {
int x;
scanf("%d", &x);
if ((x % 100 + 4) / 5 <= x / 100) {
1.write;
}
else {
0.write;
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main() {
while(true) {
auto NM = readln().split();
int N = to!int(NM[0]);
int M = to!int(NM[1]);
if(N == 0)
break;
const int INF = 200*1000*1000*2;
int[200][200] ldist;
int[200][200] sdist;
for(int i; i < N; ++i){
for(int j; j < N; ++j) {
if(i == j) {
ldist[i][j] = sdist[i][j] = 0;
} else {
ldist[i][j] = sdist[i][j] = INF;
}
}
}
for(int i; i < M; ++i) {
auto s = readln().split();
int x = to!int(s[0])-1;
int y = to!int(s[1])-1;
int t = to!int(s[2]);
auto sl = s[3];
if(sl == "L") {
ldist[x][y] = ldist[y][x] = min(ldist[x][y], t);
} else {
sdist[x][y] = sdist[y][x] = min(sdist[x][y], t);
}
}
for(int k; k < N; ++k){
for(int i; i < N; ++i){
for(int j; j < N; ++j){
ldist[i][j] = min(ldist[i][k] + ldist[k][j], ldist[i][j]);
sdist[i][j] = min(sdist[i][k] + sdist[k][j], sdist[i][j]);
}
}
}
int R = to!int(readln().chomp());
auto sz = readln().chomp().split();
int[1000] z;
for(int i; i < R; ++i){
z[i] = to!int(sz[i])-1;
}
long[200][1000] dp;
for(int i; i < R; ++i){
for(int j; j < N; ++j){
dp[i][j] = INF;
}
}
dp[0][z[0]] = 0;
for(int i = 1; i < R; ++i){
for(int j = 0; j < N; ++j){
for(int k = 0; k < N; ++k){
if(j == k) {
dp[i][j] = min(dp[i][j], dp[i-1][j] + ldist[z[i-1]][z[i]]);
} else {
dp[i][j] = min(dp[i][j], dp[i-1][k] + ldist[z[i-1]][k] + sdist[k][j] + ldist[j][z[i]]);
}
}
}
}
long ans = dp[R-1][0];
for(int i; i < N; ++i) {
ans = min(ans, dp[R-1][i]);
}
writeln(ans);
}
}
|
D
|
import std.stdio,
std.array,
std.conv,
std.string,
std.algorithm;
void main(){
int kane = 100000;
string s;
for(;;){
s=readln();
if(stdin.eof()) break;
int weeks = to!int(chomp(s));
for(int i = 0;i < weeks; ++i){
kane = kane + cast(int)(kane/100)*5;
if(kane > cast(int)(kane/1000)*1000){
kane = cast(int)(kane/1000)*1000 + 1000;
}
}
writeln(kane);
}
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
int n; scan(n);
writeln(n * n * n);
}
|
D
|
void main()
{
long n, m;
rdVals(n, m);
long[] tree = new long[n];
foreach (i; 0 .. m)
{
long a, b;
rdVals(a, b);
--a, --b;
++tree[a], ++tree[b];
}
writeln(tree.all!"a % 2 == 0" ? "YES" : "NO");
}
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.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
long P = 10^^9+7;
long[10^^5+50] F, RF;
long pow(long x, long n) {
long y = 1;
while (n) {
if (n%2 == 1) y = (y * x) % P;
x = x^^2 % P;
n /= 2;
}
return y;
}
long inv(long x)
{
return pow(x, P-2);
}
void init()
{
F[0] = F[1] = 1;
foreach (i, ref x; F[2..$]) x = (F[i+1] * (i+2)) % P;
{
RF[$-1] = 1;
auto x = F[$-1];
auto k = P-2;
while (k) {
if (k%2 == 1) RF[$-1] = (RF[$-1] * x) % P;
x = x^^2 % P;
k /= 2;
}
}
foreach_reverse(i, ref x; RF[0..$-1]) x = (RF[i+1] * (i+1)) % P;
}
long comb(N)(N n, N k)
{
if (k > n) return 0;
auto n_b = F[n]; // n!
auto nk_b = RF[n-k]; // 1 / (n-k)!
auto k_b = RF[k]; // 1 / k!
auto nk_b_k_b = (nk_b * k_b) % P; // 1 / (n-k)!k!
return (n_b * nk_b_k_b) % P; // n! / (n-k)!k!
}
void main()
{
init();
auto N = readln.chomp.to!long;
auto ii = new long[](N+1);
ii[] = -1;
long l, r;
foreach (i, a; readln.split.to!(long[])) {
if (ii[a] == -1) {
ii[a] = i;
} else {
l = ii[a];
r = i;
}
}
foreach (k; 1..N+2) {
long c;
(c += comb(N-1, k)) %= P;
if (k == 1) {
writeln(c+1);
continue;
}
(c += comb(N-1, k-2)) %= P;
(c += comb(l + N-r, k-1)) %= P;
long d = comb(N-1, k-1);
(d += P - comb(l + N-r, k-1)) %= P;
(d *= 2) %= P;
(c += d) %= P;
writeln(c);
}
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int x; rd(x);
auto ushi="ai1333".to!(char[]);
while(x){
ushi~='3';
x-=100;
}
writeln(ushi);
}
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;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
int calc(int n, int s) {
int ans = 0;
foreach (e; combinations(iota(10).array, n)) {
if (e.sum == s)
ans++;
}
return ans;
}
void main() {
while (true) {
auto xs = readints;
int n = xs[0], s = xs[1];
if (n == 0 && s == 0) break;
writeln(calc(n, s));
}
}
Combinations!T combinations(T)(T[] xs, int n) {
return Combinations!T(xs, n);
}
struct Combinations(T) {
private T[] _source;
private int _n;
private T[][] _q;
private int _state;
this(T[] source, int n) {
_source = source.dup;
_n = n;
_state = 0;
pushNext();
}
@property T[] front() {
return _q[0];
}
void popFront() {
_q = _q[1..$];
pushNext();
}
bool empty() {
return _q.length == 0;
}
private void pushNext() {
int len = cast(int)_source.length;
while (_state < (1 << len)) {
int[] buf;
for (int j = 0; j < len; j++) {
if (_state & (1 << j)) {
buf ~= _source[j];
if (buf.length > _n) break;
}
}
_state++;
if (buf.length == _n) {
_q ~= buf;
return;
}
}
}
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
long n, m;
rd(n, m);
auto s = readln.chomp.to!(char[]);
auto t = readln.chomp.to!(char[]);
import std.numeric : gcd;
auto l = n * m / gcd(n, m);
char[long] ch;
foreach (i; 0 .. n) {
auto j = i * (l / n);
ch[j] = s[i];
}
foreach (i; 0 .. m) {
auto j = i * (l / m);
if (j in ch) {
if (ch[j] != t[i]) {
writeln(-1);
return;
}
}
}
writeln(l);
}
void rd(T...)(ref T x) {
import std.stdio : readln;
import std.string : split;
import std.conv : to;
auto l = readln.split;
assert(l.length == x.length);
foreach (i, ref e; x)
e = l[i].to!(typeof(e));
}
|
D
|
import std.stdio, std.array, std.conv, std.algorithm, std.string, std.numeric, std.range;
void main() {
int n;
while((n=readln.strip.to!int)!=0){
char[char] con;
foreach(i; 0..n){
auto r = readln.strip.split(' ');
con[r[0][0]] = r[1][0];
}
n = readln.strip.to!int;
foreach(i; 0..n){
auto c = readln[0];
if(c in con){
write(con[c]);
}else{
write(c);
}
}
writeln();
}
}
|
D
|
import std.algorithm, std.stdio, std.string;
void main() {
auto s = readln.chomp;
(cast(ubyte[])s).sort;
writeln(s == "abc" ? "Yes" : "No");
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.