code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.functional,
std.algorithm,
std.typecons,
std.bigint,
std.string,
std.traits,
std.array,
std.range,
std.stdio,
std.conv;
void main() {
int[] NQ = readln.chomp.split.to!(int[]);
int N = NQ[0],
Q = NQ[1];
string s = readln.chomp;
alias LR = Tuple!(int, "l", int, "r");
LR[] lrs;
lrs.length = Q;
int[] dp;
dp.length = N;
foreach (i; 0..Q) {
int[] lr = readln.chomp.split.to!(int[]);
lrs[i] = tuple(lr[0], lr[1]);
}
for (size_t i = 1; i < s.length; i++) {
if (s[i] == 'C' && s[i - 1] == 'A') {
dp[i] = dp[i - 1] + 1;
} else {
dp[i] = dp[i - 1];
}
}
foreach (lr; lrs) {
writeln(dp[lr.r - 1] - dp[lr.l - 1]);
}
}
|
D
|
void main() {
auto A = ri, B = ri, C = ri, D = ri;
(min(A, B) + min(C, D)).writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.conv, std.functional, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
void main() {
try {
for (; ; ) {
auto A = new int[3];
auto B = new int[3];
foreach (i; 0 .. 3) {
A[i] = readInt() - 1;
B[i] = readInt() - 1;
}
auto deg = new int[4];
foreach (i; 0 .. 3) {
++deg[A[i]];
++deg[B[i]];
}
bool ans = true;
foreach (u; 0 .. 4) {
ans = ans && (deg[u] <= 2);
}
writeln(ans ? "YES" : "NO");
}
} catch (EOFException e) {
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
void main() {
int n;
scan(n);
auto a = readln.split.to!(int[]);
auto dp = new long[][](n + 1, n + 1);
fillAll(dp, -1);
// 先手は X - Y の最大化
// 後手は Y - X の最大化
// をすると考えれば先手後手の場合分けが不要になる
long dfs(int l, int r) {
auto v = &dp[l][r];
if (*v != -1) {
return *v;
}
if (r - l == 1) {
return *v = a[l];
}
return *v = max(a[l] - dfs(l + 1, r), a[r-1] - dfs(l, r - 1));
}
auto ans = dfs(0, n);
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
|
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!int;
auto s = readln.chomp;
int cnt;
foreach (i; 0..n) {
if (i+3 > s.length) break;
if (s[i..i+3] == "ABC") {
cnt++;
}
}
cnt.writeln;
}
|
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;
void main() {
long x = readln.chomp.to!long;
long left = 0;
long right = x + 1;
while (right - left > 1) {
long mid = (left + right) / 2;
long five = mid / 2;
long six = mid - five;
if (five * 5 + six * 6 < x) {
left = mid;
} else {
right = mid;
}
}
right.writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto T = readln.chomp.to!int;
auto cs = new long[](25820);
cs[1] = 2;
foreach (i; 2..25820) {
cs[i] = cs[i-1] + (i-1) * 3 + 2;
}
foreach (_; 0..T) {
auto N = readln.chomp.to!int;
int res;
while (N >= 2) {
++res;
if (N >= cs[$-1]) {
N -= cs[$-1];
} else {
int l = 1, r = cs.length.to!int-1;
while (l+1 < r) {
auto m = (l+r)/2;
if (N >= cs[m]) {
l = m;
} else {
r = m;
}
}
N -= cs[l];
}
}
writeln(res);
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
long x, y;
scan(x, y);
if (x < y || x % y) {
writeln(x);
}
else {
writeln(-1);
}
}
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; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void main()
{
auto n = RD!int;
auto a = RDA;
bool[long][32] set;
foreach (i; 0..32)
{
foreach (j; 0..n)
{
auto x = a[j] >> (31 - i);
set[i][x] = true;
}
}
long ans = long.max;
foreach (i; 0..n)
{
long tmp;
foreach (j; 0..32)
{
auto x = a[i] >> (31 - j);
x ^= 1;
if (set[j].get(x, false))
{
tmp += 1L << (31 - j);
}
}
debug writeln(tmp);
ans.chmin(tmp);
}
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[]);
void main()
{
auto n = lread();
auto a = aryread();
long[long] ary;
foreach (i; 0 .. n)
{
ary[a[i]] = ary.get(a[i], 0) + 1;
}
foreach (value; ary)
{
if (value > 1)
{
writeln("NO");
return;
}
}
writeln("YES");
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
|
D
|
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container;
import std.numeric, std.math;
import core.bitop;
T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
long mod = pow(10, 9) + 7;
long moda(long x, long y) { return (x + y) % mod; }
long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; }
long modm(long x, long y) { return (x * y) % mod; }
void main()
{
auto N = RD!long;
auto a = RDR.split.to!(long[]);
long cnt2, cnt4;
foreach (e; a)
{
if (e % 4 == 0) ++cnt4;
else if (e % 2 == 0) ++cnt2;
}
long cnt1 = N - cnt2 - cnt4;
auto ans = cnt1 + cnt4 == N ? cnt4 >= cnt1 - 1 : cnt4 >= cnt1;
writeln(ans ? "Yes" : "No");
stdout.flush();
}
|
D
|
void main()
{
string t = rdStr;
string s;
foreach (x; t)
{
if (x == 'P' || x == 'D') s ~= x;
else s ~= 'D';
}
s.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;
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 x = readln.chomp.to!int;
if (x == 7 || x == 5 || x == 3) {
writeln("YES");
} else {
writeln("NO");
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
import std.container : DList;
int[2][10^^5] GP;
bool[10^^5] MS;
alias S = Tuple!(int, "i", int, "c");
void main()
{
auto K = readln.chomp.to!int;
foreach (i; 1..K) {
GP[i][0] = (i*10)%K;
GP[i][1] = (i+1)%K;
}
auto ss = DList!S(S(1, 1));
for (;;) {
auto h = ss.front;
if (!h.i) break;
ss.removeFront();
if (MS[h.i]) continue;
MS[h.i] = true;
auto n = GP[h.i];
ss.insertFront(S(n[0], h.c));
ss.insertBack(S(n[1], h.c+1));
}
writeln(ss.front.c);
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.format;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void 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()
{
auto inp = sread();
auto A = new long[](4);
foreach (i; 0 .. 4)
A[i] = inp[i] - '0';
// writeln(A);
foreach (x; 0 .. (1 << 3))
{
long sum = A[0];
foreach (i; 0 .. 3)
{
sum += A[i + 1] * ((-1) ^^ ((x & (1 << i)) != 0));
// writeln(A[i + 1] * ((-1) ^^ ((x & (1 << i)) != 0)));
}
// writeln(sum);
if (sum == 7)
{
write(A[0]);
foreach (i; 0 .. 3)
{
write(['+', '-'][((x & (1 << i)) >> i)]);
write(A[i + 1]);
}
writeln("=7");
return;
}
// else
// writeln(sum);
}
}
|
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;
T gcd(T)(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
T lcm(T)(T a, T b) {
return a * (b / gcd(a, b));
}
long calc(int[] a) {
long m = 1;
foreach (x; a) m = lcm(m, x);
int ans = 0;
return a.map!(e => (e - 1) % m).sum;
}
void main() {
readint;
auto a = readints;
writeln(calc(a));
}
|
D
|
import std.stdio, std.string, std.conv;
import std.array, std.algorithm, std.range;
void main()
{
int[] b;
foreach(s;stdin.byLine())
{
immutable n = s.chomp().to!int;
if(n) b~=n;
else writeln(b.back),b.popBack();
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.array, std.algorithm, std.range;
void main()
{
for(int n; 0<(n=readln().chomp().to!int()); )
{
auto a = iota(n).map!(_=>readln().split().array()).array();
long[string] t;
foreach(v;a)
{
immutable s = v[1..$].map!(to!long).reduce!"a*b"();
if(v[0] in t) t[v[0]] += s;
else t[v[0]] = s;
}
bool[string] u;
string[] ids;
foreach(v;a)
if(t[v[0]]>=1000000 && v[0] !in u) ids~=v[0], u[v[0]]=true;
if(ids.length==0) writeln("NA");
else foreach(id;ids) writeln(id);
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto c = readln.chomp[0];
writeln(cast(string)[c+1]);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
auto ab = reads!string;
string a = ab[0], b = ab[1];
writeln(a == b ? "H" : "D");
}
|
D
|
// Vicfred
// https://atcoder.jp/contests/abc166/tasks/abc166_e
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;
void main() {
long n = readln.chomp.to!long;
long[] a = readln.split.map!(to!long).array;
long[long] y;
foreach(idx; 0..n) {
long x = idx+1+a[idx];
if(x in y)
y[x] += 1;
else
y[x] = 1;
}
long answer = 0;
foreach(i; 0..n) {
long x = i+1-a[i];
if(x in y)
answer += y[x];
}
answer.writeln;
}
|
D
|
import std.stdio,
std.string,
std.conv,
std.algorithm;
void main() {
int N = readln.chomp.to!(int);
int[string] s;
for (int i = 0; i < N; i++) {
s[readln.chomp]++;
}
int M = readln.chomp.to!(int);
for (int i = 0; i < M; i++) {
s[readln.chomp]--;
}
int ans = s.values.reduce!(max);
writeln(ans >= 0 ? ans : 0);
}
|
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;
immutable long MOD = 10^^9 + 7;
immutable int M = 62;
void main() {
auto N = readln.chomp.to!int;
auto A = readln.split.map!(to!long).array;
auto B = new long[][](N, M);
foreach (i; 0..N) foreach (j; 0..M) {
B[i][j] = (A[i] >> j) & 1;
}
auto S = new long[](M);
foreach (j; 0..M) foreach (i; 0..N) S[j] += B[i][j];
long ans = 0;
foreach (i; 0..N) {
long cnt = N - i - 1;
long carry = 0;
foreach (j; 0..M) {
S[j] -= B[i][j];
long one = S[j];
long zero = cnt - S[j];
if (B[i][j]) swap(one, zero);
long s = one + carry;
if (s & 1) ans += powmod(2, j, MOD) % MOD;
ans %= MOD;
carry = s >> 1;
}
int j = M;
while (carry) {
if (carry & 1) ans += powmod(2, j, MOD) % MOD;
j += 1;
carry >>= 1;
}
}
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.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;
import std.container;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
void main(){
char[] s = readln.chomp.to!(char[]);
int ans = 10000;
foreach(c; s){
int x = solve(s, c);
if(x < ans) ans = x;
}
bool flag = 1;
foreach(c; s) if(c != s[0]) flag = 0;
if(flag) ans = 0;
ans.writeln;
}
int solve(char[] s, char c){
char[] s1 = s;
int count;
while(1){
bool flag = 1;
char[] s2;
for(int i = 0; i < s1.length - 1; i ++){
if(s1[i] == c || s1[i + 1] == c) s2 ~= c;
else{
flag = 0;
s2 ~= ' ';
}
}
s1 = s2;
count += 1;
if(flag) break;
}
return count;
}
|
D
|
import std.stdio, std.algorithm, std.string, std.conv, std.array;
void main() {
int calc(int base, int rate) {
return base * (100 + rate) / 100;
}
for(;;) {
auto input = readln.chomp.split(" ").map!(to!int).array;
if ( input == [0, 0, 0] ) break;
immutable x = input[0], y = input[1], s = input[2];
int res = 0;
for( int i=1; i<s; ++i ) {
for( int j=1; j<s; ++j ) {
if ( calc(i, x) + calc(j, x) == s ) {
res = max(res, calc(i, y) + calc(j, y));
}
}
}
res.writeln;
}
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto n = readln.chomp.to!int;
auto c = new int[](n-1), s = new int[](n-1), f = new int[](n-1);
foreach (i; 0..n-1) {
auto rd = readln.split.to!(int[]);
c[i] = rd[0];
s[i] = rd[1];
f[i] = rd[2];
}
int calc(int i, int t)
{
if (i == n-1) return t;
auto t2 = max(s[i], (t+f[i]-1)/f[i]*f[i])+c[i];
return calc(i+1, t2);
}
foreach (i; 0..n) writeln(calc(i, 0));
}
|
D
|
void main()
{
long[] tmp = rdRow;
long h = tmp[0], n = tmp[1];
long[] a = rdRow;
writeln(a.sum >= h ? "Yes" : "No");
}
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
// 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, M;
scan(N, M);
auto box = new long[](N);
auto red = new bool[](N);
box[] = 1;
red[0] = true;
foreach (_; 0 .. M)
{
long x, y;
scan(x, y);
x--, y--;
if (red[x])
{
if (box[x] == 1)
red[x] = false;
red[y] = true;
box[x]--;
box[y]++;
}
else
{
box[x]--;
box[y]++;
}
}
red.sum().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);
auto a = readln.split.to!(int[]);
int o, t;
foreach (ai ; a) {
if (ai == 1) o++;
else t++;
}
int ans;
if (t <= o) {
ans += t;
o -= t;
ans += o / 3;
}
else {
ans += o;
}
writeln(ans);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
void main()
{
int n = readln.chomp.to!int;
int[] a = readln.split.to!(int[]);
a[] -= 1;
int cnt;
foreach (i; 0 .. n)
{
if (i == a[a[i]]) ++cnt;
}
writeln(cnt / 2);
}
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.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int H, W;
scan(H, W);
auto s = iota(H).map!(i => readln.chomp).array;
auto dp = new int[][](H, W);
dp[0][0] = s[0][0] == '#';
foreach (i ; 0 .. H) {
foreach (j ; 0 .. W) {
if (i == 0 && j == 0) continue;
dp[i][j] = inf;
if (i - 1 >= 0) {
if (s[i][j] == '#') {
chmin(dp[i][j], dp[i-1][j] + (s[i-1][j] == '.'));
}
else {
chmin(dp[i][j], dp[i-1][j]);
}
}
if (j - 1 >= 0) {
if (s[i][j] == '#') {
chmin(dp[i][j], dp[i][j-1] + (s[i][j-1] == '.'));
}
else {
chmin(dp[i][j], dp[i][j-1]);
}
}
}
}
auto ans = dp[H-1][W-1];
writeln(ans);
}
void scan(T...)(ref T args) {
auto line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
|
D
|
void main() {
problem();
}
void problem() {
const N = scan!long;
void solve() {
bool[string] items;
foreach(_; 0..N) {
const item = scan;
items[item] = true;
}
writeln(items.keys.length);
}
solve();
}
// ----------------------------------------------
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
// -----------------------------------------------
|
D
|
import std.stdio;
import std.conv;
import std.string;
void main()
{
readln();
auto s = readln();
foreach (c; s)
{
if (c == 'Y'){
writeln("Four");
return;
}
}
writeln("Three");
}
|
D
|
import std;
import core.bitop;
// dfmt off
ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
void aryWrite(T = long)(T[] ary){ ary.map!(x => x.text()).join(' ').writeln(); }
alias Pair = Tuple!(long, "H", long, "W", long, "cost");
alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
auto s = sread();
auto k = lread();
auto ans = new char[](s.length);
foreach (i, e; s)
{
auto diff = ('z' - e + 1) % 26;
// diff.writeln();
if (diff <= k)
{
ans[i] = 'a';
k -= diff;
}
else
{
ans[i] = e;
}
}
ans[$ - 1] += k % 26;
ans.writeln();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
auto hs = readln.split.to!(int[]);
auto max_h = hs[0];
int r;
foreach (h; hs) {
if (h >= max_h) {
++r;
max_h = h;
}
}
writeln(r);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math;
void main()
{
auto N = readln.chomp.to!int;
auto S = readln.chomp.to!(wchar[]);
long[immutable(wchar[])][18] omemo;
long solve(int i, wchar[] rs_, wchar[] bs_) {
if (i == N) {
auto rs = rs_.dup;
reverse(rs);
auto bs = bs_.dup;
reverse(bs);
long[long][long] memo;
long dp(int i, int j) {
if (i + j == N) return 1L;
if (i in memo && j in memo[i]) return memo[i][j];
long r1, r2;
if (i < bs.length && S[N+i+j] == bs[i]) r1 = dp(i+1, j);
if (j < rs.length && S[N+i+j] == rs[j]) r2 = dp(i, j+1);
return memo[i][j] = r1 + r2;
}
auto ret = dp(0, 0);
return ret;
} else {
auto key = (rs_ ~ "|" ~ bs_).idup;
if (key in omemo[i]) return omemo[i][key];
return omemo[i][key] = solve(i+1, rs_ ~ S[i], bs_) + solve(i+1, rs_, bs_ ~ S[i]);
}
}
writeln(solve(0, [], []));
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
void main()
{
long a, b, c;
scan(a, b, c);
auto n = lread();
while (a >= b)
{
b *= 2;
n -= 1;
}
// writeln(a, b, c);
// writeln(n);
if (c * 2 ^^ n > b)
{
writeln("Yes");
}
else
{
writeln("No");
}
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
|
D
|
import std.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;
bool f;
if (s[0] != 'A') {
f = true;
}
if (!(s[1] >= 'a' && s[1] <= 'z')) {
f = true;
}
if (!(s[$-1] >= 'a' && s[$-1] <= 'z')) {
f = true;
}
auto c_num = s[2..$-1].count!(x=>x == 'C');
auto s_num = s[2..$-1].count!(x=>x>='a' && x<='z');
if (c_num != 1) {
f = true;
}
if (s_num != s[2..$-1].length.to!int - 1) {
f = true;
}
if (f) {
writeln("WA");
} else {
writeln("AC");
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string;
int[50] CITY;
void main()
{
auto nm = readln.chomp.split(" ").map!(to!int);
auto n = nm[0];
auto m = nm[1];
auto city = CITY[0..n];
foreach (_i; 0..m) {
auto ab = readln.chomp.split(" ").map!(to!int);
++city[ab[0]-1];
++city[ab[1]-1];
}
city.each!(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[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
auto A = aryread();
foreach (a; A)
if (a % 2 == 0)
{
if (a % 3 != 0 && a % 5 != 0)
{
writeln("DENIED");
return;
}
}
writeln("APPROVED");
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "x", long, "y");
alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less);
void main()
{
auto n = lread();
auto status = new long[][](n, n);
foreach (ref e; status)
e[] = -1;
foreach (i; iota(n))
{
auto a = lread();
foreach (_; iota(a))
{
long x, y;
scan(x, y);
status[i][x - 1] = y;
}
}
// status.each!(x => x.writeln());
long ans;
outer: foreach (s; iota(0, 1 << n))
{
auto p = new long[](n);
p[] = -1;
foreach (loc; iota(n))
{
if (s & (1 << loc))
{
bool t = true;
t &= (p[loc] != 0);
p[loc] = 1;
foreach (u; iota(n))
{
if (p[u] == -1)
p[u] = status[loc][u];
else if (status[loc][u] != -1 && p[u] != status[loc][u])
t = false;
}
// writef("%b, %s ", s, loc);
// p.writeln(" ", t);
if (!t)
continue outer;
}
else
{
// writef("%b, %s ", s, loc);
// p[loc].writeln("= p[loc]");
if (p[loc] == 1)
continue outer;
p[loc] = 0;
}
}
ans = max(ans, popcnt(s));
// ans.writeln(" = ans");
}
ans.writeln();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std;
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 S = new long[](N);
foreach (i; 0 .. N)
{
auto s = lread();
S[i] = s;
}
// writeln(S);
if (func(S))
{
writeln(0);
return;
}
long sum_S = sum(S);
// writeln(sum_S);
// S.sort().writeln();
S.sort();
if (sum_S % 10 == 0)
{
sum_S -= func2(S);
}
writeln(sum_S);
}
//全部10の倍数
bool func(long[] S)
{
foreach (i; 0 .. (S.length))
{
if (S[i] % 10 == 0)
{
continue;
}
else
{
return false;
}
}
return true;
}
//sort(S)の中で一番小さいS[i]%10!=0
long func2(long[] S)
{
foreach (i; 0 .. (S.length))
{
if (S[i] % 10 != 0)
{
return S[i];
}
}
return 0;
}
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;
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 s = readln.chomp;
auto cnt = new int[](3);
foreach (e; s) {
cnt[e-'a']++;
}
if (abs(cnt[0]-cnt[1]) < 2 && abs(cnt[1]-cnt[2]) < 2 && abs(cnt[0]-cnt[2]) < 2) {
writeln("YES");
} else {
writeln("NO");
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main()
{
auto line = chomp(readln()).split(" ");
int H = to!int(line[0]),
W = to!int(line[1]);
while(H || W)
{
foreach(int i; 0 .. H)
{
writeln(repeat("#", W));
}
writeln();
line = chomp(readln()).split(" ");
H = to!int(line[0]);
W = to!int(line[1]);
}
}
string repeat(string s, int n)
{
string r;
while(n)
{
if(n % 2)
{
r ~= s;
}
n >>= 1;
s ~= s;
}
return 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;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int N, K;
scan(N, K);
int ans = K;
foreach (i ; 0 .. N - 1) {
ans *= K - 1;
}
writeln(ans);
}
void scan(T...)(ref T args) {
auto line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
|
D
|
import std.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() {
try {
for (; ; ) {
const numCases = readInt();
foreach (caseId; 0 .. numCases) {
const X = readLong();
const q = X / 14, r = X % 14;
const ans = (q >= 1 && 1 <= r && r <= 6);
writeln(ans ? "YES" : "NO");
}
}
} catch (EOFException e) {
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto abt = readln.split.to!(int[]);
auto A = abt[0];
auto B = abt[1];
auto T = abt[2];
writeln(B * (T / A));
}
|
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 N = readln.chomp.to!int;
auto AS = readln.split.to!(int[]);
auto ps = new int[](10^^6+1);
foreach (a; AS) {
for (int i = 2; i^^2 <= a; ++i) {
if (a%i == 0) {
ps[i] += 1;
while (a%i == 0) a /= i;
}
}
if (a != 1) ps[a] += 1;
}
auto pc = true;
foreach (p; ps) {
if (p == N) {
writeln("not coprime");
return;
} else if (p > 1) {
pc = false;
}
}
writeln(pc ? "pairwise coprime" : "setwise coprime");
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto xn = readln.split.to!(int[]);
auto X = xn[0];
auto N = xn[1];
bool[101] ps;
foreach (p; readln.split.to!(int[])) ps[p] = true;
foreach (d; 0..100) {
auto x = X-d;
if (x < 0 || !ps[x]) {
writeln(x);
return;
}
auto y = X+d;
if (y > 100 || !ps[y]) {
writeln(y);
return;
}
}
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
alias Edge = Tuple!(int, "to", int, "idx");
void main() {
int N;
scan(N);
auto adj = new Edge[][](N, 0);
foreach (i ; 0 .. N - 1) {
int ai, bi;
scan(ai, bi);
ai--, bi--;
adj[ai] ~= Edge(bi, i);
adj[bi] ~= Edge(ai, i);
}
int M;
scan(M);
auto useEdge = new ulong[](M);
ulong dfs(int u, int p, int v) {
foreach (e ; adj[u]) if (e.to != p) {
if (e.to == v) {
return 1UL << e.idx;
}
auto b = dfs(e.to, u, v);
if (b != 0) {
return b | (1UL << e.idx);
}
}
return 0;
}
foreach (i ; 0 .. M) {
int ui, vi;
scan(ui, vi);
ui--, vi--;
useEdge[i] = dfs(ui, -1, vi);
}
debug {
writeln(useEdge);
}
long ans;
foreach (s ; 0 .. (1 << M)) {
ulong b;
foreach (i ; 0 .. M) {
if (s & (1 << i)) {
b |= useEdge[i];
}
}
auto p = s.popcnt % 2;
auto t = (-1)^^p * 2L^^(N - 1 - b._popcnt);
ans += t;
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
auto a = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51];
int k;
scan(k);
writeln(a[k - 1]);
}
void scan(T...)(ref T args) {
auto line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
|
D
|
import std.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;
writeln((S[0] == S[$-1] && S.length%2 == 0) || (S[0] != S[$-1] && S.length%2 == 1) ? "First" : "Second");
}
|
D
|
import std.algorithm;
import std.functional;
import std.range;
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;
}
long extgcd(long a, long b, ref long x, ref long y) {
long g = a; x = 1; y = 0;
if (b != 0) {
g = extgcd(b, a % b, y, x);
y -= (a / b) * x;
}
return g;
}
long inverse(long mod, long v) {
long x, y;
if (extgcd(v, mod, x, y) == 1) return (x + mod) % mod;
return 0;
}
const long P = 10000481L;
const long[] mods = [1000000123L, 1000000207L, 1000001137L];
long[][] pow;
static this() {
const int N = cast(int)1e5 + 5;
pow = new long[][](mods.length, N + 1);
foreach (k, mod; mods) {
pow[k][0] = 1L;
foreach (n; 0 .. N) {
pow[k][n + 1] = pow[k][n] * P % mod;
}
}
}
class RollingHash {
long[] H;
long mod;
long P;
int mod_index;
this(long P, long mod, int mod_index, in string s) {
this.mod = mod;
this.mod_index = mod_index;
this.P = P;
auto N = s.length;
this.H = new long[N + 1]; // H[k]: s[0]*P^(n-1) + s[1]*P^(n-2) + ... + s[n-1]*P^0
foreach (int i, c; s) {
H[i + 1] = (H[i] * P + cast(int)(c)) % mod;
}
}
long hash(int l, int r) { // [l, r)
return (H[r] - H[l] * pow[mod_index][r - l] % mod + mod) % mod;
}
}
void main() {
auto s = readln.chomp;
int N = s.size;
int Q = readln.chomp.to!int;
auto P_exp = new bool[long][mods.length];
foreach (k, mod; mods) {
long p = 1;
P_exp[k][p] = true;
foreach (n; 0 .. N + 2) {
p = (p * P) % mod;
P_exp[k][p] = true;
}
}
auto hashers = iota(0, mods.length, 1).map!((k) => new RollingHash(P, mods[k], cast(int)k, s)).array;
auto inv_cache = new long[][](mods.length, 70);
foreach (k, mod; mods) {
foreach (q; -25 .. 25 + 1) {
long y = q < 0 ? q + mod : q;
inv_cache[k][q + 30] = inverse(mod, y);
}
}
foreach (_; 0 .. Q) {
//log(_);
int l, r, t;
scanf("%d %d %d\n", &l, &r, &t);
l--;
auto k = (r - l) / t;
auto m = r - l - t * k;
//log([l, r, t, k, m]);
auto ans = new bool[hashers.length];
foreach (j, ref hasher; hashers) {
auto mod = hasher.mod;
long org = hasher.hash(l, r);
auto inv = delegate long(long x) { return inverse(mod, x); };
long cycle_hash(int z) {
long a = hasher.hash(l + t * z, l + t * z + t);
return (a * (pow[j][t * k] - 1L) % mod * inv(pow[j][t] - 1L) % mod * pow[j][m] % mod
+ hasher.hash(l + t*z, l + t*z + m)) % mod;
}
foreach (z; [0, 1]) {
if (l + t * z + t > r) continue;
auto diff = (org - cycle_hash(z) + mod) % mod;
if (diff == 0) { ans[j] = true; goto next;}
foreach (q; -25 .. 25 + 1) {
if (inv_cache[j][q + 30] * diff % mod in P_exp[j]) {
ans[j] = true;
goto next;
}
}
}
if (! ans[j]) break;
next:;
}
writeln(ans.all ? "Yes" : "No");
}
}
|
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) * 180);
};
|
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 N = RD;
auto A = new long[](N);
foreach (i; 0..N)
A[i] = RD;
auto B = new long[](N+1);
auto C = new long[](N+1);
foreach (i; 0..N)
{
B[i+1] = max(B[i], A[i]);
}
foreach_reverse (i; 0..N)
{
C[i] = max(C[i+1], A[i]);
}
foreach (i; 0..N)
{
writeln(max(B[i], C[i+1]));
}
stdout.flush();
debug readln();
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
int main(string[] argv)
{
immutable MAX_HIGHT = 10000;
immutable INPUT = 10;
int[] hill;
int count = 0;
while(count < 10){
int temp = readln().chomp().to!int();
if(0 <= temp && temp <= MAX_HIGHT){
hill ~= temp;
count++;
}
}
int temp;
for(int i = 0;i < INPUT - 1;i++){
for(int j = INPUT - 1;j > i;j--){
if(hill[j-1] < hill[j]){
temp = hill[j];
hill[j] = hill[j-1];
hill[j-1] = temp;
}
}
}
for(int i = 0;i < 3;i++){
writeln(hill[i]);
}
return 0;
}
|
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;
void main() {
int x, a, b;
scan(x, a, b);
if (b - a <= 0) {
writeln("delicious");
}
else if (b - a <= x) {
writeln("safe");
}
else {
writeln("dangerous");
}
}
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);
}
|
D
|
import std.stdio,std.array,std.conv,std.algorithm;
void main(){
auto a=readln().split().map!(to!int);
writeln(a[0]-(a[0]>a[1]));
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void calc(int[] a) {
int n = a.reduce!max;
// n/2 に最も近い値を選ぶ
int k = a.filter!(e => e != n).reduce!((a, b) => abs(a - n/2) <= abs(b - n/2) ? a : b);
writeln(n, " ", k);
}
void main() {
readint;
auto a = readints;
calc(a);
}
|
D
|
import std.stdio;
import std.string;
import std.format;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.concurrency;
import std.traits;
import std.uni;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;
enum long INF = long.max/3;
enum long MOD = 10L^^9+7;
void main() {
long N;
scanln(N);
long[] as = N.rep!(() => readln.chomp.to!long);
if (as.all!"a%2==0") {
"second".writeln;
} else {
"first".writeln;
}
}
// ----------------------------------------------
void times(alias fun)(long n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(long n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
// `(x+y-1)/y` will only work for positive numbers ...
T t = x / y;
if (t * y < x) t++;
return t;
}
T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
T t = x / y;
if (t * y > x) t--;
return t;
}
ref T ch(alias fun, T, S...)(ref T lhs, S rhs) {
return lhs = fun(lhs, rhs);
}
unittest {
long x = 1000;
x.ch!min(2000);
assert(x == 1000);
x.ch!min(3, 2, 1);
assert(x == 1);
x.ch!max(100).ch!min(1000); // clamp
assert(x == 100);
x.ch!max(0).ch!min(10); // clamp
assert(x == 10);
}
mixin template Constructor() {
import std.traits : FieldNameTuple;
this(Args...)(Args args) {
// static foreach(i, v; args) {
foreach(i, v; args) {
mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v;
}
}
}
void scanln(Args...)(auto ref Args args) {
import std.meta;
template getFormat(T) {
static if (isIntegral!T) {
enum getFormat = "%d";
} else static if (isFloatingPoint!T) {
enum getFormat = "%g";
} else static if (isSomeString!T || isSomeChar!T) {
enum getFormat = "%s";
} else {
static assert(false);
}
}
enum string fmt = [staticMap!(getFormat, Args)].join(" ");
string[] inputs = readln.chomp.split;
foreach(i, ref v; args) {
v = inputs[i].to!(Args[i]);
}
}
// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
return reduce!fun(tuple(seed), r);
}
}
}
}
// cumulativeFold was added in D 2.072.0
static if (__VERSION__ < 2072) {
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
}
// minElement/maxElement was added in D 2.072.0
static if (__VERSION__ < 2072) {
private auto extremum(alias map, alias selector = "a < b", Range)(Range r)
if (isInputRange!Range && !isInfinite!Range &&
is(typeof(unaryFun!map(ElementType!(Range).init))))
in
{
assert(!r.empty, "r is an empty range");
}
body
{
alias Element = ElementType!Range;
Unqual!Element seed = r.front;
r.popFront();
return extremum!(map, selector)(r, seed);
}
private auto extremum(alias map, alias selector = "a < b", Range,
RangeElementType = ElementType!Range)
(Range r, RangeElementType seedElement)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void) &&
is(typeof(unaryFun!map(ElementType!(Range).init))))
{
alias mapFun = unaryFun!map;
alias selectorFun = binaryFun!selector;
alias Element = ElementType!Range;
alias CommonElement = CommonType!(Element, RangeElementType);
Unqual!CommonElement extremeElement = seedElement;
alias MapType = Unqual!(typeof(mapFun(CommonElement.init)));
MapType extremeElementMapped = mapFun(extremeElement);
// direct access via a random access range is faster
static if (isRandomAccessRange!Range)
{
foreach (const i; 0 .. r.length)
{
MapType mapElement = mapFun(r[i]);
if (selectorFun(mapElement, extremeElementMapped))
{
extremeElement = r[i];
extremeElementMapped = mapElement;
}
}
}
else
{
while (!r.empty)
{
MapType mapElement = mapFun(r.front);
if (selectorFun(mapElement, extremeElementMapped))
{
extremeElement = r.front;
extremeElementMapped = mapElement;
}
r.popFront();
}
}
return extremeElement;
}
private auto extremum(alias selector = "a < b", Range)(Range r)
if (isInputRange!Range && !isInfinite!Range &&
!is(typeof(unaryFun!selector(ElementType!(Range).init))))
{
alias Element = ElementType!Range;
Unqual!Element seed = r.front;
r.popFront();
return extremum!selector(r, seed);
}
private auto extremum(alias selector = "a < b", Range,
RangeElementType = ElementType!Range)
(Range r, RangeElementType seedElement)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void) &&
!is(typeof(unaryFun!selector(ElementType!(Range).init))))
{
alias Element = ElementType!Range;
alias CommonElement = CommonType!(Element, RangeElementType);
Unqual!CommonElement extremeElement = seedElement;
alias selectorFun = binaryFun!selector;
// direct access via a random access range is faster
static if (isRandomAccessRange!Range)
{
foreach (const i; 0 .. r.length)
{
if (selectorFun(r[i], extremeElement))
{
extremeElement = r[i];
}
}
}
else
{
while (!r.empty)
{
if (selectorFun(r.front, extremeElement))
{
extremeElement = r.front;
}
r.popFront();
}
}
return extremeElement;
}
auto minElement(Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum(r);
}
auto minElement(alias map, Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!map(r, seed);
}
auto minElement(Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum(r, seed);
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum!(map, "a > b")(r);
}
auto maxElement(Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum!`a > b`(r);
}
auto maxElement(alias map, Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!(map, "a > b")(r, seed);
}
auto maxElement(Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!`a > b`(r, seed);
}
}
// popcnt with ulongs was added in D 2.071.0
static if (__VERSION__ < 2071) {
ulong popcnt(ulong x) {
x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L);
x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L);
x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL);
x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL);
x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL);
x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL);
return x;
}
}
|
D
|
import std.stdio;
import std.string;
import std.algorithm;
import std.array;
import std.conv;
int solve(int[] list) {
while (list.length > 1) {
int[] res = new int[](list.length - 1);
for(int i = 0; i < list.length - 1; i++) {
res[i] = (list[i] + list[i + 1]) % 10;
}
list = res;
}
return list[0];
}
void main() {
string line;
while ((line = readln.chomp).length != 0) {
line.array.map!((x) => x.to!(int) - '0').array.solve.writeln;
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
enum P = 10L^^9+7;
void main()
{
auto N = readln.chomp.to!int;
int[][] RS;
RS.length = N;
foreach (i; 0..N) RS[i] = readln.split.to!(int[]);
auto DP = new long[][](N, 2^^N);
foreach (ref dp; DP) foreach (ref e; dp) e = -1;
long solve(int i, uint s) {
if (i == N) return 1;
if (DP[i][s] == -1) {
long x;
foreach (j; 0..N) {
if ((s & (1<<j)) == 0 && RS[i][j]) x = (x + solve(i+1, s | (1<<j))) % P;
}
DP[i][s] = x;
}
return DP[i][s];
}
writeln(solve(0, 0));
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.conv;
void main() {
auto s = readln.chomp;
string res;
foreach(i; s) {
switch(i) {
case '0', '1':
res ~= i;
break;
case 'B':
if(res.length != 0) res.popBack();
break;
default: break;
}
}
res.writeln;
}
// ===================================
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 (isBasicType!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
int ri() {
return readAs!int;
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
void main() {
auto n = readln.strip.to!int;
auto s = readln.strip.splitter;
auto a = s.front; s.popFront;
auto b = s.front;
foreach (i; 0 .. n) {
write (a[i], b[i]);
}
writeln;
}
|
D
|
import std.stdio, std.conv, std.string;
import std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
int DEBUG_LEVEL = 0;
void print()(){ writeln(""); }
void print(T, A ...)(T t, lazy A a){ write(t), print(a); }
void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); }
void main(string[] args){
if(args.length > 1 && args[1] == "-debug"){
if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int;
else DEBUG_LEVEL = 1;
}
int n = read.to!int;
int k = read.to!int;
int ans = n - k + 1;
ans.writeln;
}
|
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 a = readln.splitter.map !(to !(int)).array;
auto total = sum (a);
bool ok = (total % 9 == 0);
total /= 9;
ok &= (a.minElement >= total);
writeln (ok ? "Yes" : "No");
}
}
|
D
|
void main()
{
int n = readln.chomp.to!int;
int[] h = readln.split.to!(int[]);
int hi;
int[] cnt = [0];
foreach (x; h)
{
if (x > hi)
{
cnt[$-1] = x;
}
else if (x < hi)
{
cnt[$-1] = hi - x;
cnt ~= x;
}
hi = x;
}
cnt.sum.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;
int abs(int x) {
return (x >= 0) ? x : -x;
}
void main() {
int N = readln.chomp.to!(int);
int[][] a;
for (int i = 0; i < N; i++) {
a ~= readln.chomp.split.to!(int[]);
}
bool f = true;
int t = 0, x = 0, y = 0;
for (int i = 0; i < N; i++) {
int dt = abs(a[i][0] - x), dx = a[i][1] - x, dy = a[i][2] - y;
int d = abs(dx) + abs(dy);
f = d <= dt && (d - dt) % 2 == 0;
if (!f) {
break;
}
}
writeln(f ? "Yes" : "No");
}
|
D
|
import std.stdio;
import std.algorithm;
import std.conv;
import std.string;
import std.array;
import std.math;
int factnum(int n){
int i = 0;
while(n%2 == 0){
n /= 2;
i++;
}
return i;
}
void main(string[] args) {
readln();
auto input = readln().chomp.split.map!(to!int);
input.map!factnum.sum.writeln();
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
void main() {
immutable n = readln.strip.to!long;
ulong res;
void check (long x) {
--x;
if (n / x == n % x) res += x;
}
for (int i = 2; i.to!long * i <= n; ++i) {
check (i);
long j = n / i;
if (j != i) {
check (n / i);
}
}
if (n > 1) check (n);
writeln (res);
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.array;
import std.functional;
import std.conv;
import std.math;
void main(){
while(true){
auto s = readln();
if(stdin.eof()) break;
int n = to!int(s.chomp());
int[4001] arr;
for(int i=0;i<=1000;i++){
for(int j=0;j<=1000;j++){
arr[i+j]++;
}
}
int ans;
for(int i=0;i<=n;i++){
ans += arr[i] * arr[n-i];
}
writeln(ans);
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() {
auto xt = readints;
int x = xt[0], t = xt[1];
int ans = max(0, x - t);
writeln(ans);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.math;
void main() {
((ushort[] ip) => ip[0] + ip[1] >= ip[2] ? "Yes" : "No")(readln.split.to!(ushort[])).writeln;
}
|
D
|
void main() {
int[] tmp = readln.split.to!(int[]);
int a = tmp[0], b = tmp[1];
writeln(a >= 13 ? b : a >= 6 ? b / 2 : 0);
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.typecons;
|
D
|
import std;
enum inf(T)()if(__traits(isArithmetic,T)){return T.max/4;}
T scan(T=long)(){return readln.chomp.to!T;}
void scan(T...)(ref T args){auto input=readln.chomp.split;foreach(i,t;T)args[i]=input[i].to!t;}
T[] scanarr(T=long)(){return readln.chomp.split.to!(T[]);}
alias Queue=DList;auto enq(T)(ref Queue!T q,T e){q.insertBack(e);}T deq(T)(ref Queue!T q){T e=q.front;q.removeFront;return e;}
alias Stack=SList;auto push(T)(ref Stack!T s,T e){s.insert(e);}T pop(T)(ref Stack!T s){T e=s.front;s.removeFront;return e;}
struct UnionFind(T){T[T]u;ulong[T] rank;@property{bool inc(T e){return e in u;}auto size(){return u.keys.length;}auto dup(){T[] child=u.keys;T[] parent=u.values;auto res=UnionFind!T(child);child.each!(e=>res.add(e));size.iota.each!(i=>res.unite(child[i],parent[i]));return res;}}this(T e){e.add;}this(T[] es){es.each!(e=>e.add);}auto add(T a,T b=a){assert(b.inc);u[a]=a;rank[a];if(a!=b)unite(a,b);}auto find(T e){if(u[e]==e)return e;return u[e]=find(u[e]);}auto same(T a,T b){return a.find==b.find;}auto unite(T a,T b){a=a.find;b=b.find;if(a==b)return;if(rank[a]<rank[b])u[a]=b;else{u[b]=a;if(rank[a]==rank[b])rank[a]++;}}}
struct PriorityQueue(T,alias less="a<b"){BinaryHeap!(T[],less) heap;@property{bool empty(){return heap.empty;}auto length(){return heap.length;}auto dup(){return PriorityQueue!(T,less)(array);}T[] array(){T[] res;auto tp=heap.dup;foreach(i;0..length){res~=tp.front;tp.removeFront;}return res;}void push(T e){heap.insert(e);}void push(T[] es){es.each!(e=>heap.insert(e));}}T look(){return heap.front;}T pop(){T tp=look;heap.removeFront;return tp;}this(T e){ heap=heapify!(less,T[])([e]);} this(T[] e){ heap=heapify!(less,T[])(e);}}
//END OF TEMPLATE
void main(){
ulong n,k;
scan(n,k);
ulong sum;
foreach(l;k..n+2){
sum=(sum+l*(n-l+1)+1)%(10^^9+7);
}
sum.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);
}
void main()
{
long x, a, b;
scan(x, a, b);
auto z = abs(x - a) - abs(x - b);
z = z / abs(z);
auto ary = ["A", "", "B"];
writeln(ary[z + 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[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N, M;
scan(N, M);
auto P = aryread();
auto uf = UnionFind(N + 1);
foreach (i; 0 .. M)
{
long X, Y;
scan(X, Y);
uf.unite(X, Y);
}
long ans;
foreach (i; 0 .. N)
ans += (uf.find(i + 1) == uf.find(P[i]));
writeln(ans);
}
alias UnionFind = UnionFindImpl!long;
struct UnionFindImpl(T)
{
T[] _rank, _data, _size;
this(long n)
{
_rank = new long[](n);
_size = new long[](n);
_size[] = 1;
_data = iota!T(n).array();
}
T find(T x)
{
if (_data[x] == x)
return x;
T r = _data[x] = this.find(_data[x]);
return r;
}
void unite(T x, T y)
{
T rx = this.find(x), ry = this.find(y);
if (rx == ry)
return;
_size[rx] = _size[ry] = (_size[rx] + _size[ry]);
if (_rank[rx] == _rank[ry])
{
_data[rx] = ry;
_rank[ry]++;
}
else if (_rank[rx] < _rank[ry])
_data[rx] = ry;
else
_data[ry] = rx;
}
T size(T x)
{
return _size[this.find(x)];
}
}
|
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;
bool f(int n) {
return 1 <= n && n <= 12;
}
string calc(string s) {
auto a = (10 * (s[0] - '0')) + (s[1] - '0');
auto b = (10 * (s[2] - '0')) + (s[3] - '0');
if (f(a) && f(b)) return "AMBIGUOUS";
if (f(a)) return "MMYY";
if (f(b)) return "YYMM";
return "NA";
}
void main() {
string s = read!string;
writeln(calc(s));
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
void main()
{
auto hw = readln.split.to!(int[]);
auto H = hw[0];
auto W = hw[1];
wchar[][] PIC;
PIC.length = H;
foreach (ref line; PIC) {
line = readln.chomp.to!(wchar[]);
}
foreach (i, line; PIC) {
foreach (j, p; line) {
if (p == '#') {
wchar[] cs = "....".to!(wchar[]);
if (i > 0) cs[0] = PIC[i-1][j];
if (i < H-1) cs[1] = PIC[i+1][j];
if (j > 0) cs[2] = PIC[i][j-1];
if (j < W-1) cs[3] = PIC[i][j+1];
if (cs == "....") {
writeln("No");
return;
}
}
}
}
writeln("Yes");
}
|
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; }
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 = RDA;
long ans = 1;
long[] s = [0, 0, 0];
foreach (i; 0..N)
{
long cnt;
foreach (j; 0..3)
{
if (s[j] == A[i])
++cnt;
}
foreach (j; 0..3)
{
if (s[j] == A[i])
{
++s[j];
break;
}
}
ans.modm(cnt);
}
writeln(ans);
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;
long N, cnt;
bool ask(long n) {
writeln("? ", n);
stdout.flush;
cnt += 1;
debug {
string sn = n.to!string;
string sN = N.to!string;
return (n <= N && sn <= sN) || (n > N && sn > sN);
} else {
return readln.chomp == "Y";
}
}
void main() {
debug {N = readln.chomp.to!long;}
long x = 10;
for (; x <= 10^^9 && ask(x); x *= 10) {}
if (x == 10L^^10) { // N = 1, 10, 100, ...
x = 2;
for (; x <= 2 * 10L^^9 && !ask(x); x *= 10) {}
writeln("! ", x / 2);
} else {
long k = x / 10;
x = 0;
for (; k > 0; k /= 10) {
long hi = 10;
long lo = 0;
while (hi - lo > 1) {
long mid = (hi + lo) / 2;
if (ask((x * 10 + mid) * 10L^^9)) hi = mid;
else lo = mid;
}
x = x * 10 + lo;
}
writeln("! ", x + 1);
}
debug {cnt.writeln;}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
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 q = RD!int;
auto d = RD!int;
auto a = RDA;
foreach (e; a)
{
if (e >= d*10 || e % d == 0)
{
ans[ti] ~= true;
}
else
{
auto cnt = e / d;
bool ok;
foreach (i; 0..cnt)
{
auto x = e - i * d;
if ((x % 10) == d)
{
ok = true;
break;
}
}
ans[ti] ~= ok;
}
}
}
foreach (e; ans)
{
foreach (ee; e)
writeln(ee ? "YES" : "NO");
}
stdout.flush;
debug readln;
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}}
void main()
{
int n, m; readV(n, m);
int[] a, b; readC(m, a, b);
a[] -= 1; b[] -= 1;
auto r = 0;
foreach (i; 0..m) {
auto uf = UnionFind!int(n);
foreach (j; 0..m)
if (i != j) uf.unite(a[j], b[j]);
if (uf.countForests > 1) ++r;
}
writeln(r);
}
struct UnionFind(T)
{
import std.algorithm, std.range;
T[] p; // parent
const T s; // sentinel
const T n;
T countForests; // number of forests
T[] countNodes; // number of nodes in forests
this(T n)
{
this.n = n;
s = n;
p = new T[](n);
p[] = s;
countForests = n;
countNodes = new T[](n);
countNodes[] = 1;
}
T opIndex(T i)
{
if (p[i] == s) {
return i;
} else {
p[i] = this[p[i]];
return p[i];
}
}
bool unite(T i, T j)
{
auto pi = this[i], pj = this[j];
if (pi != pj) {
p[pj] = pi;
--countForests;
countNodes[pi] += countNodes[pj];
return true;
} else {
return false;
}
}
auto countNodesOf(T i) { return countNodes[this[i]]; }
bool isSame(T i, T j) { return this[i] == this[j]; }
auto groups()
{
auto g = new T[][](n);
foreach (i; 0..n) g[this[i]] ~= i;
return g.filter!(l => !l.empty);
}
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}}
void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}}
void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}}
void main()
{
int a, b, c; readV(a, b, c);
auto dp = new int[][][](60, 60, 60), inf = 10^^9;
int calc(int a, int b, int c)
{
if (a >= 60 || b >= 60 || c >= 60) return inf;
if (a == b && b == c) return 0;
if (dp[a][b][c]) return dp[a][b][c];
return dp[a][b][c] = min(calc(a+1, b+1, c),
calc(a+1, b, c+1),
calc(a, b+1, c+1),
calc(a+2, b, c),
calc(a, b+2, c),
calc(a, b, c+2)) + 1;
}
writeln(calc(a, b, c));
}
|
D
|
import std.algorithm,
std.string,
std.range,
std.stdio,
std.conv,
std.math;
long f(long b, long n) {
return (n < b)
? n
: f(b, n/b) + n % b;
}
long solve(long N, long S) {
if (N < S) {
return -1;
}
if (N == S) {
return N + 1;
}
foreach (i; 2.iota(sqrt(N.to!float) + 1)) {
auto j = i.to!long;
if (f(j, N) == S) {
return j;
}
}
long ans = long.max;
foreach (i; 1.iota(sqrt(N.to!float) + 1)) {
auto j = i.to!long;
long b = (N - S) / j + 1;
if (b == 1) continue;
if (f(b, N) == S) ans = min(ans, b);
}
if (ans < long.max) {
return ans;
} else {
return -1;
}
}
void main() {
long N = readln.chomp.to!long,
S = readln.chomp.to!long;
writeln(solve(N, S));
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
size_t[][26] D;
void main()
{
auto s = readln.chomp.to!(char[]);
auto t = readln.chomp.to!(char[]);
foreach (i, c; s) {
D[c - 'a'] ~= i;
}
ulong cnt;
size_t p;
bool first = true;
foreach (c; t) {
auto d = D[c - 'a'];
if (d.empty) {
writeln(-1);
return;
}
if (first) {
first = false;
if (d[0] == 0) {
continue;
}
}
if (d[$-1] <= p) {
p = d[0];
++cnt;
continue;
} else if (d[0] > p) {
p = d[0];
continue;
}
size_t l, r = d.length-1;
while (l+1 < r) {
auto m = (l+r)/2;
if (d[m] <= p) {
l = m;
} else {
r = m;
}
}
p = d[r];
}
writeln(s.length * cnt + p + 1);
}
|
D
|
void main()
{
long n, w;
rdVals(n, w);
Pair[] pair = n.rdCol!Pair;
long[][] dp = new long[][](n+1, w+1);
foreach (i, p; pair)
{
foreach (j; 0 .. w+1)
{
dp[i+1][j] = dp[i][j];
if (j - p.a < 0) continue;
dp[i+1][j] = max(dp[i+1][j], dp[i][j-p.a]+p.b);
}
}
dp[n][w].writeln;
}
struct Pair
{
long a, b;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
enum double eps = 1.0e-9;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
long x, y;
scan(x, y);
int ans = 1;
while (2*x <= y) {
x *= 2;
ans++;
}
writeln(ans);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
auto abc = readints;
int a = abc[0], b = abc[1], c = abc[2];
double s = (a + b + c) / 2;
double x = sqrt(s * (s - a) * (s - b) * (s - c));
writeln(cast(int) x);
}
|
D
|
import std.stdio, std.algorithm, std.string, std.conv, std.array;
int read_num() {return readln.chomp.to!(int);}
void main() {
while(true) {
int[] line = readln.chomp.split(" ").map!(to!(int)).array;
int times = line[0];
int len = line[1];
int[100000] arr;
if (times == 0 && len == 0) break;
foreach(i; 0..times)
arr[i] = read_num;
int max, sum;
int next = 0;
foreach(x; arr[0..len])
max += x;
sum = max;
foreach(int i, x; arr[len..times]) {
sum += x;
sum -= arr[i];
if (sum > max) max = sum;
}
writeln(max);
}
}
|
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.random;
immutable inf = 10L^^16;
alias Edge = Tuple!(int, "from", int, "to", long, "cap", int, "rev");
void main() {
int n, m;
scan(n, m);
auto adj = new Edge[][](n, 0);
void addEdge(int u, int v, long cap) {
adj[u] ~= Edge(u, v, cap, adj[v].length.to!int);
adj[v] ~= Edge(v, u, 0, adj[u].length.to!int - 1);
}
foreach (i ; 0 .. m) {
int u, v, c;
scan(u, v, c);
addEdge(u, v, c);
}
long ans = EdmondsKarp(adj, 0, n - 1);
writeln(ans);
}
long EdmondsKarp(Edge[][] adj, int s, int t) {
int n = adj.length.to!int;
long F;
while (true) {
auto q = Queue!(int)(n);
q.push(s);
auto pred = new Edge*[](n);
while (!q.empty && !pred[t]) {
auto cur = q.front;
q.pop();
foreach (ref e ; adj[cur]) {
if (!pred[e.to] && e.to != s && e.cap > 0) {
pred[e.to] = &e;
q.push(e.to);
}
}
}
if (pred[t]) {
long df = inf;
for (auto e = pred[t]; e; e = pred[e.from]) {
df = min(df, e.cap);
}
for (auto e = pred[t]; e; e = pred[e.from]) {
e.cap -= df;
adj[e.to][e.rev].cap += df;
}
F += df;
}
else break;
}
return F;
}
struct Queue(T) {
private {
int N, head, tail;
T[] data;
}
this (int n) {
N = n + 1;
data = new T[](N);
}
bool empty() {
return head == tail;
}
bool full() {
return (tail + 1) % N == head;
}
T front() {
return data[head];
}
void push(T x) {
assert(!full);
data[tail++] = x;
tail %= N;
}
void pop() {
assert(!empty);
head++;
head %= N;
}
}
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.string, std.conv;
import std.range, std.algorithm, std.array;
import std.typecons : Tuple;
void main() {
int n;
scan(n);
auto a = readln.split.to!(int[]);
auto b = readln.split.to!(int[]);
int ue = a[0], sita = b.sum();
int ans = ue + sita;
foreach (i ; 1 .. n) {
ue += a[i];
sita -= b[i-1];
ans = max(ans, ue + sita);
}
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
|
import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;
void main()
{
auto s = readln.chomp;
int ans = 0;
for (int i = 0, j = s.length.to!int - 1; i < j; ++i, --j) {
if (s[i] != s[j]) { ++ans; }
}
ans.writeln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto t = RD!int;
auto ans = new int[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto s = RD!int;
auto a = RDA;
auto b = new long[](n+1);
auto c = new long[][](n+1, 2);
foreach (i; 0..n)
{
b[i+1] = b[i] + a[i];
c[i+1] = c[i].dup;
if (a[i] > c[i+1][0])
c[i+1] = [a[i], i+1];
}
long best;
int pos;
foreach (i; 0..n)
{
if (b[i+1] <= s)
{
best = i+1;
pos = 0;
}
else if (b[i+1] - c[i+1][0] <= s)
{
best = i;
pos = cast(int)c[i+1][1];
}
}
ans[ti] = pos;
}
foreach (e; ans)
writeln(e);
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 s = readln.chomp;
if (s[2] == s[3] && s[4] == s[5]) {
writeln("Yes");
} else {
writeln("No");
}
}
|
D
|
// unihernandez22
// https://atcoder.jp/contests/abc076/tasks/abc076_c
// string manipulation
import std.stdio;
import std.string;
void main() {
string s = readln.chomp;
string t = readln.chomp;
long idx = -1;
bool matched;
for(long i = (s.count-t.count); i >= 0; i--) {
if (s[i] == '?' || s[i] == t[0]) {
matched = true;
foreach(j; 0..t.count) {
if (s[i+j] != '?' && s[i+j] != t[j]) {
matched = false;
break;
}
}
if (matched) {
idx = i;
break;
}
}
}
if (idx == -1) {
writeln("UNRESTORABLE");
return;
}
foreach(i; 0..s.count) {
if (s[i] == '?' && (i < idx || i >= t.count+idx))
write("a");
else if (s[i] == '?')
write(t[i-idx]);
else
write(s[i]);
} writeln;
}
|
D
|
import std.stdio;
import std.ascii;
void main() {
int sum = 0;
foreach (string input; stdin.lines) {
auto num = 0;
foreach (c; input) {
if (c.isDigit) {
num = (num * 10) + (c - '0');
} else {
sum += num;
num = 0;
}
}
}
writeln(sum);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main() {
string[] inputs = split(readln());
int a, b;
foreach(v; inputs) {
switch(v) {
case "5":
a++;
break;
case "7":
b++;
break;
default:
break;
}
}
if(a == 2 && b == 1) "YES".writeln;
else "NO".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;
alias Pair = Tuple!(long, "p", int, "r");
void main() {
int n;
scan(n);
auto x = new long[](n + 1);
auto s = new int[](n + 1);
foreach (i ; 1 .. n + 1) {
scan(x[i], s[i]);
}
auto mh = MaxHeap!(Pair)(n);
long d;
long p = s[1];
long ans = p;
mh.push(Pair(s[1], 1));
foreach (i ; 2 .. n + 1) {
p += s[i] - (x[i] - x[i-1]);
mh.push(Pair(p, i));
ans = max(ans, p);
}
foreach (i ; 1 .. n) {
d += x[i + 1] - x[i] - s[i];
while (mh.front.r <= i) {
mh.pop;
}
debug {
writeln(d);
writeln(mh.front);
}
ans = max(ans, mh.front.p + d);
}
writeln(ans);
}
struct MaxHeap(T) {
private {
int N, last;
T[] data;
}
this(int n) {
N = n;
data = new T[](N);
last = 0;
}
bool empty() {
return last == 0;
}
bool full() {
return last == N;
}
T front() {
return data[0];
}
void heapUp(int x) {
while (x > 0) {
if (data[x] > data[(x - 1) / 2]) {
swap(data[x], data[(x - 1) / 2]);
x = (x - 1) / 2;
}
else {
break;
}
}
}
void heapDown(int x) {
while (2*x + 1 < last) {
int ch = 2*x + 1;
if (2*x + 2 < last && data[2*x + 2] > data[2*x + 1]) {
ch = 2*x + 2;
}
if (data[ch] > data[x]) {
swap(data[ch], data[x]);
x = ch;
}
else {
break;
}
}
}
void push(T x) {
assert(!full);
data[last++] = x;
heapUp(last - 1);
}
void pop() {
assert(!empty);
data[0] = data[last - 1];
last--;
heapDown(0);
}
T[] array() {
return data[0 .. last];
}
}
unittest {
auto mh = MaxHeap!(int)(5);
mh.push(3);
mh.push(1);
mh.push(4);
mh.push(5);
mh.push(1);
assert(mh.full);
assert(mh.front == 5);
mh.pop;
mh.pop;
mh.pop;
assert(mh.front == 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
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void main()
{
auto N = readln.chomp.to!int;
auto cs = readln.chomp.to!(char[]);
int c;
int i, j = N-1;
while (i < j) {
while (i < N && cs[i] != 'W') ++i;
while (j >= 0 && cs[j] != 'R') --j;
if (i < j && j < N) {
++c;
++i;
--j;
}
}
writeln(c);
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.