code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.stdio,std.conv,std.string,std.algorithm,std.array;
void main(){
auto s=readln().chomp().split().map!(to!int);
int a=s[0],b=s[1];
writeln(max(a+b,a-b,a*b));
}
|
D
|
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math;
import core.stdc.stdio;
// foreach, foreach_reverse, writeln
void main() {
int n;
scanf("%d\n", &n);
int[][] g = new int[][n];
int[] degree = new int[n];
foreach (i; 0..n-1) {
int x, y;
scanf("%d %d\n", &x, &y);
--x; --y;
g[x] ~= y;
g[y] ~= x;
degree[x]++;
degree[y]++;
}
string c = readln().chomp;
bool[] live = new bool[n];
live[] = true;
int[] queue; int cursor = 0;
foreach (i; 0..n) {
if (degree[i] == 1) {
queue ~= i;
}
}
int L = n;
while (cursor < queue.length) {
int v = queue[cursor]; cursor++;
if (c[v] == 'W') continue;
live[v] = false; L--;
foreach (u; g[v]) {
degree[u]--;
if (degree[u] == 1) {
queue ~= u;
}
}
}
if (L == 0) {
writeln(0);
return;
}
int base = L*2;
int[] score = new int[n];
foreach (v; 0..n) {
if (!live[v]) continue;
score[v] = (degree[v] + (c[v]=='B'?0:1)) % 2;
base += score[v];
}
int best = 0;
int dfs(int v, int parent=-1) {
int ret = score[v];
foreach (u; g[v]) {
if (u == parent) continue;
if (!live[u]) continue;
int s = dfs(u,v);
best = max(best, ret+s);
ret = max(ret, s+score[v]);
}
return ret;
}
foreach (v; 0..n) {
if (!live[v]) continue;
dfs(v);
break;
}
writeln(base-best*2-2);
}
|
D
|
void main()
{
long n, m;
rdVals(n, m);
long result = n * (n - 1) / 2 + m * (m - 1) / 2;
result.writeln;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
import std.stdio, std.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;
auto p = new long[](N);
foreach (i; 0..N)
p[i] = RD;
auto pos = new long[](N);
foreach (i; 0..N)
{
pos[p[i]-1] = i;
}
long ans;
long streak;
long last;
foreach (i; 0..N)
{
if (pos[i] >= last)
++streak;
else
{
ans.chmax(streak);
streak = 1;
}
last = pos[i];
}
ans.chmax(streak);
writeln(N-ans);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.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;
auto v = RDR.ARR;
auto c = RDR.ARR;
long ans;
foreach (i; 0..N)
{
ans += max(0, v[i] - c[i]);
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int n, g;
rd(n, g);
struct P {
int p, c;
}
auto ps = new P[](n);
foreach (i; 0 .. n)
rd(ps[i].p, ps[i].c);
int mn = 1000000000;
foreach (bit; 0 .. (1 << n)) {
int s = 0, cnt = 0;
foreach (i; 0 .. n) {
if (bit & (1 << i)) {
s += ps[i].p * (i + 1) * 100 + ps[i].c;
cnt += ps[i].p;
}
}
if (s >= g) {
mn = min(mn, cnt);
continue;
}
foreach_reverse (i; 0 .. n) {
if ((bit & (1 << i)) == 0) {
if (s + ps[i].p * (i + 1) * 100 >= g) {
mn = min(mn, cnt + ((g - s) + (i + 1) * 100 - 1) / ((i + 1) * 100));
}
break;
}
}
}
writeln(mn);
}
void rd(T...)(ref T x) {
import std.stdio : readln;
import std.string : split;
import std.conv : to;
auto l = readln.split;
assert(l.length == x.length);
foreach (i, ref e; x)
e = l[i].to!(typeof(e));
}
|
D
|
import std.stdio, std.conv, std.string, std.range, std.algorithm;
void main() {
int d = readln.chomp.to!int;
("Christmas " ~ repeat("Eve", 25 - d).join(" ")).writeln;
}
|
D
|
import std.algorithm;
import std.array;
import std.ascii;
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 calc(int a, int b, string s) {
if (s[a] != '-') return false;
for (int i = 0; i < s.length; i++) {
if (i == a) continue;
if (!isDigit(s[i])) return false;
}
return true;
}
void main() {
auto ab = readints;
int a = ab[0], b = ab[1];
auto s = read!string;
writeln(calc(a, b, s) ? "Yes" : "No");
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new long[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
ans[ti] = n / 10;
if (n % 10 == 9)
++ans[ti];
}
foreach (e; ans)
{
writeln(e);
}
stdout.flush;
debug readln;
}
|
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 H = aryread();
foreach_reverse (i; 0 .. N - 1)
{
if (H[i + 1] < H[i])
{
H[i]--;
}
if (H[i + 1] < H[i])
{
writeln("No");
return;
}
}
writeln("Yes");
}
|
D
|
import std.stdio, std.algorithm, std.string, std.conv, std.array, std.range, std.math;
int read() { return readln.chomp.to!int; }
void main() {
int n = read();
writeln(n&1 ? 2*n : n);
}
|
D
|
import core.thread;
import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;
// Input
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { tokens = readln.split; if (stdin.eof) throw new EOFException; } auto token = tokens[0]; tokens.popFront; return token; }
int readInt() { return to!int(readToken); }
long readLong() { return to!long(readToken); }
real readReal() { return to!real(readToken); }
// chmin/chmax
void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }
// Pair
struct Pair(S, T) {
S x; T y;
int opCmp( const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
int opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
string toString() const { return "(" ~ to!string(x) ~ ", " ~ to!string(y) ~ ")"; }
}
auto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }
// Array
int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = as.length; for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a < val); }); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }
T[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }
long hp0, atk0, def0;
long hp1, atk1, def1;
long hpC, atkC, defC;
void main(string[] args) {
try {
for (; ; ) {
hp0 = readLong; atk0 = readLong; def0 = readLong;
hp1 = readLong; atk1 = readLong; def1 = readLong;
hpC = readLong; atkC = readLong; defC = readLong;
long ans = long.max;
foreach (x; 0 .. 1005L) foreach (y; 0 .. 1005L) {
const decr0 = max(0, atk1 - (def0 + y));
const decr1 = max(0, (atk0 + x) - def1);
if (decr1 > 0) {
const turns = (hp1 + decr1 - 1) / decr1;
const needed = 1 + decr0 * turns;
long tmp;
tmp += hpC * max(needed - hp0, 0);
tmp += atkC * x;
tmp += defC * y;
chmin(ans, tmp);
}
}
writeln(ans);
}
} catch (EOFException) {}
}
|
D
|
import std.stdio;
import std.algorithm;
import std.conv;
import std.array;
import std.string;
void main() {
int n = readln.chomp.to!int;
auto L = readln.chomp.split.map!(to!int).array;
int[] is_alive = new int[n];
for (int i = n - 1; i > 0; i--) {
if (L[i] == 0) continue;
int idx = max(0, i - L[i]);
is_alive[idx] += 1;
is_alive[i] -= 1;
}
int ans = 0;
for (int i = 1; i < n; ++i) is_alive[i] += is_alive[i - 1];
for (int i = 0; i < n; ++i) if (is_alive[i] == 0) 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; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto H = RD;
auto W = RD;
auto K = RD;
auto x1 = RD-1;
auto y1 = RD-1;
auto x2 = RD-1;
auto y2 = RD-1;
auto c = new string[](H);
foreach (i; 0..H)
{
c[i] = RD!string;
}
long[][] open_l = [[x1, y1]], open_r = [[x1, y1]], open_t = [[x1, y1]], open_b = [[x1, y1]];
auto used = new bool[][](H, W);
used[x1][y1] = true;
long ans = -1;
long cnt = 1;
(){
while (true)
{
long[][] next_l, next_r, next_t, next_b;
while (!open_l.empty)
{
auto nd = open_l.front; open_l.popFront;
auto x = nd[0];
auto y = nd[1];
foreach (i; 1..K+1)
{
auto nx = x;
auto ny = y-i;
if (!inside(ny, 0, W)) break;
if (c[nx][ny] == '@') break;
if (used[nx][ny]) continue;
used[nx][ny] = true;
if (nx == x2 && ny == y2)
{
ans = cnt;
return;
}
if (i == K)
{
next_l ~= [nx, ny];
}
next_t ~= [nx, ny];
next_b ~= [nx, ny];
}
}
while (!open_r.empty)
{
auto nd = open_r.front; open_r.popFront;
auto x = nd[0];
auto y = nd[1];
foreach (i; 1..K+1)
{
auto nx = x;
auto ny = y+i;
if (!inside(ny, 0, W)) break;
if (c[nx][ny] == '@') break;
if (used[nx][ny]) continue;
used[nx][ny] = true;
if (nx == x2 && ny == y2)
{
ans = cnt;
return;
}
if (i == K)
{
next_r ~= [nx, ny];
}
next_t ~= [nx, ny];
next_b ~= [nx, ny];
}
}
while (!open_t.empty)
{
auto nd = open_t.front; open_t.popFront;
auto x = nd[0];
auto y = nd[1];
foreach (i; 1..K+1)
{
auto nx = x-i;
auto ny = y;
if (!inside(nx, 0, H)) break;
if (c[nx][ny] == '@') break;
if (used[nx][ny]) continue;
used[nx][ny] = true;
if (nx == x2 && ny == y2)
{
ans = cnt;
return;
}
if (i == K)
{
next_t ~= [nx, ny];
}
next_l ~= [nx, ny];
next_r ~= [nx, ny];
}
}
while (!open_b.empty)
{
auto nd = open_b.front; open_b.popFront;
auto x = nd[0];
auto y = nd[1];
foreach (i; 1..K+1)
{
auto nx = x+i;
auto ny = y;
if (!inside(nx, 0, H)) break;
if (c[nx][ny] == '@') break;
if (used[nx][ny]) continue;
used[nx][ny] = true;
if (nx == x2 && ny == y2)
{
ans = cnt;
return;
}
if (i == K)
{
next_b ~= [nx, ny];
}
next_l ~= [nx, ny];
next_r ~= [nx, ny];
}
}
++cnt;
if (next_l.empty && next_r.empty && next_t.empty && next_b.empty)
{
return;
}
open_l = next_l;
open_r = next_r;
open_t = next_t;
open_b = next_b;
}}();
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
void times(alias fun)(int n) {
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
// fold was added in D 2.071.0.
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);
}
}
}
void main() {
long n = readln.chomp.to!long;
long s = readln.chomp.to!long;
if (n == s) {
(n+1).writeln;
return;
}
long t = n.to!double.sqrt.to!long+2;
foreach(b; 2..t) {
if (check(b, n, s)) {
b.writeln;
return;
}
}
foreach(p; iota(1, t).retro) {
long b = (n-s)/p + 1;
if (check(b, n, s)) {
b.writeln;
return;
}
}
(-1).writeln;
}
bool check(long b, long n, long s) {
long f(long b, long n) {
return n%b + (n<b ? 0 : f(b, n/b));
}
return b>1 && f(b, n) == s;
}
|
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 S = readln.chomp;
auto N = S.length.to!int;
long ans = 0;
long w = 0;
foreach_reverse (i; 0..N) {
if (S[i] == 'W') {
w += 1;
} else {
ans += w;
}
}
ans.writeln;
}
|
D
|
void main() {
int[] tmp = readln.split.to!(int[]);
int a = tmp[0], b = tmp[1], c = tmp[2];
writeln(c % gcd(a, b) == 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.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;
import core.bitop;
const int segSize = 1 << 20;
int[segSize * 2] seg;
// a_i を v に上書き
void update(int ind, int v) {
ind += segSize;
seg[ind] = v;
while (true) {
ind /= 2; // 上にのぼる
if (ind == 0) break;
// 下の子の最小値で更新
seg[ind] = seg[ind * 2] | seg[ind * 2 + 1];
}
}
// [l, r) の最小値を取得
int find(int l, int r) {
l += segSize;
r += segSize;
int ans = seg[l];
while (l < r) {
if (l % 2 == 1) {
ans |= seg[l];
l++;
}
l /= 2;
if (r % 2 == 1) {
ans |= seg[r - 1];
r--;
}
r /= 2;
}
return popcnt(ans);
}
void main() {
int n; scan(n);
string s = read;
for (int i = 0; i < s.length; i++) update(i + 1, 1 << (s[i] - 'a'));
int q; scan(q);
foreach (_; 0..q) {
int a, b; string t; scan(a, b, t);
if (a == 1) { // udpate
update(b, 1 << (t[0] - 'a'));
}
if (a == 2) { // get
int p = t.to!int;
writeln(find(b, p + 1));
}
}
}
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T=string)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readints = reads!int;
|
D
|
import std.stdio;
import std.algorithm;
import std.math;
import std.conv;
import std.string;
T readNum(T)(){
return readStr.to!T;
}
T[] readNums(T)(){
return readStr.split.to!(T[]);
}
string readStr(){
return readln.chomp;
}
void main(){
auto s = readStr;
int[char] dic;
foreach(c; s){
dic[c]++;
}
foreach(i; dic.byValue){
if(i != 2){
writeln("No");
return;
}
}
writeln("Yes");
}
|
D
|
void main()
{
string s = readln.chomp;
string t = "CODEFESTIVAL2016";
int cnt;
foreach (i; 0 .. 16)
{
if (s[i] != t[i]) ++cnt;
}
cnt.writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.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; }
T[] ARR(T)(in string str) { return str.split.to!(T[]); }
long mod = pow(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; }
struct UnionFind
{
void init(long n) { par = new long[](n); foreach (i; 0..n) par[i] = i; }
long root(long i) { return par[i] == i ? i : (par[i] = root(par[i])); }
bool same(long i, long j) { return root(i) == root(j); }
void unite(long i, long j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; }
long[] par;
}
void main()
{
auto N = RD!long;
auto C = RD!long;
auto x = new long[](N);
auto v = new long[](N);
foreach (i; 0..N)
{
x[i] = RD!long;
v[i] = RD!long;
}
auto dp = new long[2][2][](N+1);
foreach (i; 0..N)
{
dp[i+1][0][0] = dp[i][0][0] + v[i];
dp[i+1][0][1] = max(dp[i][0][1], dp[i+1][0][0] - x[i] * 2);
}
foreach_reverse (i; 0..N)
{
dp[i][1][0] = dp[i+1][1][0] + v[i];
dp[i][1][1] = max(dp[i+1][1][1], dp[i][1][0] - (C - x[i]) * 2);
}
long ans;
foreach (i; 0..N)
{
auto cal1 = dp[i+1][0][0] - x[i];
auto cal2 = dp[i+1][1][1];
ans = max(ans, cal1 + cal2);
}
foreach_reverse (i; 0..N)
{
auto cal1 = dp[i][1][0] - (C - x[i]);
auto cal2 = dp[i][0][1];
ans = max(ans, cal1 + cal2);
}
writeln(ans);
stdout.flush();
}
|
D
|
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container;
import std.numeric, std.math;
string my_readln() { return chomp(readln()); }
long mod = pow(10, 9) + 7;
long moda(long x, long y) { return (x + y) % mod; }
long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; }
long modm(long x, long y) { return (x * y) % mod; }
void main()
{
//auto tokens = my_readln().split();
//auto N = tokens[0].to!uint;
//auto K = tokens[1].to!uint;
auto s = my_readln;
auto t = my_readln;
auto dp = new uint[][](3005, 3005);
foreach (i, cs; s)
{
foreach (j, ct; t)
{
dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1]);
if (cs == ct)
{
dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j] + 1);
}
//stderr.writeln(i, ":", j, " ", dp[i+1][j+1]);
}
}
string ans;
long i = s.length - 1, j = t.length - 1;
while (i >= 0 && j >= 0)
{
if (s[i] == t[j])
{
ans = s[i] ~ ans;
--i; --j;
}
else if (dp[i+1][j] > dp[i][j+1]) --j;
else --i;
}
writeln(ans);
stdout.flush();
}
|
D
|
import std.stdio;
import std.string;
import std.algorithm;
import std.conv;
import std.array;
import std.math;
import std.range;
void main()
{
foreach(line; stdin.byLine)
{
auto s = line.chomp;
if(s == "0") return;
writeln(reduce!"a+b"(0, s.map!"a-'0'"));
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.math;
void main() {
string[] inputs = split(readln());
int A = to!int(inputs[0]);
int B = to!int(inputs[1]);
int result = A + B;
if(result < 10) result.writeln;
else "error".writeln;
}
|
D
|
import std.algorithm, std.array, std.container, std.range, std.bitmanip;
import std.numeric, std.math, std.bigint, std.random, core.bitop;
import std.string, std.conv, std.stdio, std.typecons;
void main()
{
while (true) {
auto rd = readln.split.map!(to!int);
auto h = rd[0], w = rd[1];
if (h == 0 && w == 0) break;
foreach (_1; 0..h) {
foreach (_2; 0..w)
write('#');
writeln;
}
writeln;
}
}
|
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() {
int n = readint;
writeln(n / 3);
}
|
D
|
#!/usr/bin/env rdmd
import std.stdio, std.string, std.conv;
import std.algorithm, std.array;
void main()
{
for(string S; (S=readln().chomp()).length; )
{
long[3] h;
foreach(c;S)
if(c=='B') ++h[0];
else if(c=='S') ++h[1];
else ++h[2];
auto n = array(readln().split().map!(to!long)());
auto p = array(readln().split().map!(to!long)());
auto r = readln().chomp().to!long();
long lo=0, hi=1000000000000000;
while(lo+1<hi)
{
auto m = (lo+hi)/2;
long c = 0;
foreach(i;0..3)
c += p[i]*max(0,m*h[i]-n[i]);
if(c<=r) lo=m;
else hi=m;
}
writeln(lo);
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
long P = 10^^9+7;
long[10^^5*2+50] F, RF;
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;
}
void main()
{
init();
auto hwab = readln.split.to!(long[]);
auto H = hwab[0];
auto W = hwab[1];
auto A = hwab[2];
auto B = hwab[3];
long r;
auto free_h = H - A;
auto free_w = W - B;
foreach (y; 1..free_h+1) {
auto m = ((F[B + y - 2] * RF[B-1])%P * RF[y-1])%P;
auto o = ((F[free_w + (H-y) - 1] * RF[free_w-1])%P * RF[H-y])%P;
r = (r + (m * o)%P)%P;
}
writeln(r);
}
|
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;
immutable long mod = 10^^9 + 7;
void main() {
int n;
scan(n);
auto s = iota(n + 1).array;
long ans = 1;
foreach (p ; 2 .. n + 1) {
if (s[p] > 1) {
int cnt = 1;
for (int q = p; q < n + 1; q += p) {
while (s[q] % p == 0) {
cnt++;
s[q] /= p;
}
}
(ans *= cnt) %= 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, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (ti; 0..t)
{
auto n = RD;
bool dfs(long x, bool turn)
{
if (x == 1) return !turn;
if (x == 2 || x % 2) return turn;
bool res = !turn;
for (long i = 3; i*i <= x; ++i)
{
if (x % i) continue;
if (i % 2)
res |= dfs(x/i, !turn) == turn;
if ((x / i) % 2)
res |= dfs(i, !turn) == turn;
}
return res;
}
ans[ti] = dfs(n, true);
}
foreach (e; ans)
{
writeln(e ? "Ashishgup" : "FastestFinger");
}
stdout.flush;
debug readln;
}
|
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() {
readint();
auto ss = readints();
bool[int] map;
foreach (s; ss)
map[s] = true;
readint();
auto ts = readints();
auto ans = ts.count!(e => (e in map) != null);
writeln(ans);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto hw = readln.split.to!(int[]);
auto H = hw[0];
auto W = hw[1];
auto us = new int[][](H, W);
auto ds = new int[][](H, W);
auto ls = new int[][](H, W);
auto rs = new int[][](H, W);
foreach (i; 0..H) {
foreach (j, c; readln.chomp) if (c == '#') {
us[i][j] = -1;
ds[i][j] = -1;
ls[i][j] = -1;
rs[i][j] = -1;
}
}
foreach (i; 0..H) {
foreach (j; 0..W) {
if (i+1 < H && ds[i+1][j] != -1) ds[i+1][j] = ds[i][j] + 1;
auto ii = H-i-1;
if (ii-1 >= 0 && us[ii-1][j] != -1) us[ii-1][j] = us[ii][j] + 1;
if (j+1 < W && rs[i][j+1] != -1) rs[i][j+1] = rs[i][j] + 1;
auto jj = W-j-1;
if (jj-1 >= 0 && ls[i][jj-1] != -1) ls[i][jj-1] = ls[i][jj] + 1;
}
}
int r;
foreach (i; 0..H) foreach (j; 0..W) if (us[i][j] != -1) r = max(r, us[i][j] + ds[i][j] + ls[i][j] + rs[i][j] + 1);
writeln(r);
}
|
D
|
import std.stdio : writeln;
void main() {
int a,b;
scan(a,b);
writeln((a+b+1)/2);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.array : split;
import std.conv : to;
import std.range.primitives;
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto q = RD!int;
auto ans = new long[](q);
foreach (i; 0..q)
{
auto n = RD!int;
auto k = RD!int;
auto s = RD!string;
auto dp = new long[3][](n+1);
auto str = "RGB";
long cnt = k;
foreach (j; 0..3)
{
foreach (l; 0..n)
{
dp[l+1][j] = dp[l][j];
if (s[l] != str[(j+l)%3])
++dp[l+1][j];
if (l+1 >= k)
{
cnt = min(cnt, dp[l+1][j] - dp[l+1-k][j]);
}
}
}
ans[i] = cnt;
}
foreach (e; ans)
{
writeln(e);
}
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
int x, y, z;
scan(x, y, z);
int ans = (x - z) / (y + z);
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
|
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
import std.typecons;
import std.bigint;
// import dcomp.foundation, dcomp.scanner;
int main() {
auto sc = new Scanner(stdin);
long n, m;
sc.read(n, m);
long[][] a = new long[][](n, m);
foreach (i; 0..n) {
sc.read(a[i]);
if (i && a[i-1][0] > a[i][0]) {
writeln(-1);
return 0;
}
}
if (m == 1) {
foreach (i; 1..n) {
if (a[i-1][0] == a[i][0]) {
writeln(-1);
return 0;
}
}
writeln(0);
return 0;
}
// m >= 2
immutable INF = 10L^^9 + 10L^^7;
long[] suc(long[] x, long c) {
long[] y = x.dup;
long la = m;
foreach (ph; 0..c) {
foreach (i; 1..la) {
y[i] += y[i-1];
if (y[i] >= INF) {
y[i] = INF;
la = i;
break;
}
}
if (la <= 2) break;
}
y[1] = min(INF, x[0] * c + x[1]);
return y;
}
long[] co = new long[n];
foreach (i; 1..n) {
if (a[i-1][0] < a[i][0]) {
continue;
}
// a[i-1][0] == a[i][0]
long b2 = a[i-1][1] + a[i-1][0] * co[i-1];
if (b2 < a[i][1]) {
continue;
}
long c = b2 - a[i][1];
if (c % a[i][0]) {
co[i] = c / a[i][0] + 1;
continue;
}
co[i] = c / a[i][0];
long mi = min(co[i-1], co[i]);
long[] x1 = suc(a[i-1], co[i-1]-mi);
long[] x2 = suc(a[i], co[i]-mi);
if (cmp(x1, x2) != -1) {
co[i]++;
}
}
writeln(co.sum);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
//fold(for old compiler)
static if (__VERSION__ <= 2070) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
unittest {
import std.stdio;
auto l = [1, 2, 3, 4, 5];
assert(l.fold!"a+b"(10) == 25);
}
}
version (X86) static if (__VERSION__ < 2071) {
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
if (f.eof) return false;
line = lineBuf[];
f.readln(line);
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
//string or char[10] etc
//todo optimize
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else {
auto buf = line.split.map!(to!E).array;
static if (isStaticArray!T) {
//static
assert(buf.length == T.length);
}
x = buf;
line.length = 0;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
fout.writeln("1 2 3");
fout.writeln("ab cde");
fout.writeln("1.0 1.0 2.0");
fout.close;
Scanner sc = new Scanner(File(fileName, "r"));
int a;
int[2] b;
char[2] c;
string d;
double e;
double[] f;
sc.read(a, b, c, d, e, f);
assert(a == 1);
assert(equal(b[], [2, 3]));
assert(equal(c[], "ab"));
assert(equal(d, "cde"));
assert(e == 1.0);
assert(equal(f, [1.0, 2.0]));
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File, writeln;
import std.datetime;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
foreach (i; 0..1_000_000) {
fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2);
}
fout.close;
writeln("Scanner Speed Test(3*1,000,000 int)");
StopWatch sw;
sw.start;
Scanner sc = new Scanner(File(fileName, "r"));
foreach (i; 0..500_000) {
int a, b, c;
sc.read(a, b, c);
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
foreach (i; 500_000..700_000) {
int[3] d;
sc.read(d);
int a = d[0], b = d[1], c = d[2];
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
foreach (i; 700_000..1_000_000) {
int[] d;
sc.read(d);
assert(d.length == 3);
int a = d[0], b = d[1], c = d[2];
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
writeln(sw.peek.msecs, "ms");
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
writeln(0);
stdout.flush();
auto f = readln.chomp;
if (f == "Vacant") return;
int l = 0, r = N;
for (;;) {
auto m = (l+r)/2;
writeln(m);
stdout.flush();
auto x = readln.chomp;
if (x == "Vacant") {
return;
} else if (m%2 == 0 && x == f || m%2 == 1 && x != f) {
l = m;
} else {
r = m;
}
}
}
|
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 calc(int a, int b) {
for (int i = 0; i < (1 << 16); i++) {
bool[] xs;
for (int j = 0; j < 16; j++) {
xs ~= (i & (1 << j)) == 0;
}
xs ~= xs[0];
bool ok = true;
int x = 0;
int y = 0;
for (int j = 0; j < xs.length - 1; j++) {
if (xs[j]) x++; else y++;
if (xs[j] == xs[j + 1]) {
ok = false;
break;
}
}
if (ok) {
if (x >= a && y >= b) return true;
if (x >= b && y >= a) return true;
}
}
return false;
}
void main() {
auto ab = readints;
int a = ab[0], b = ab[1];
auto ans = calc(a, b);
writeln(ans ? "Yay!" : ":(");
}
|
D
|
void main()
{
dchar[] s = readln.chomp.to!(dchar[]);
reverse(s);
string[] words = ["dream", "dreamer", "erase", "eraser"];
dchar[][] rev = new dchar[][](4);
foreach (i, x; words)
{
rev[i] = x.to!(dchar[]);
reverse(rev[i]);
}
long index;
bool ok = true;
while (index < s.length)
{
bool check;
foreach (x; rev)
{
long len = x.length;
if (index + len > s.length) continue;
if (s[index..index+len] == x)
{
check = true;
index += len;
break;
}
}
if (!check)
{
ok = false;
break;
}
}
writeln(ok ? "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.conv, std.string, std.range, std.algorithm, std.array,
std.functional, std.container, std.typecons;
void main() {
auto input = readln.split.to!(int[]);
if(input.all!"1 <= a && a <= 9"()) {
writeln(input[0] * input[1]);
} else {
writeln(-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() {
string s = read!string;
foreach (c; s) {
switch (c) {
case '1': write('9'); break;
case '9': write('1'); break;
default: write(c); break;
}
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main() {
auto N = split(readln())[0].to!int();
auto L = split(readln()).map!(x => to!int(x));
auto sum = reduce!("a + b")(L);
foreach (i; 0 .. N) {
if (sum-L[i] <= L[i] ) { writeln("No"); return; }
}
writeln("Yes");
}
|
D
|
unittest
{
assert( [ "36", "24" ].parse.expand.solve == "GREATER" );
assert( [ "850", "3777" ].parse.expand.solve == "LESS" );
assert( [ "9720246", "22516266" ].parse.expand.solve == "LESS" );
assert( [ "123456789012345678901234567890", "234567890123456789012345678901" ].parse.expand.solve == "LESS" );
assert( [ "123", "123" ].parse.expand.solve == "EQUAL" ); // 追加ケース
}
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 a = input.front.strip;
input.popFront;
auto b = input.front.strip;
return tuple( a, b );
}
auto solve( string a, string b )
{
if( a.length < b.length ) return "LESS";
if( b.length < a.length ) return "GREATER";
if( a < b ) return "LESS";
if( b < a ) return "GREATER";
return "EQUAL";
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main(){
if(readln.chomp.to!int == 1){
writeln("Hello World");}
else{
auto a = readln.chomp.to!int;
auto b = readln.chomp.to!int;
writeln(a+b);}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
auto AS = readln.split.to!(int[]);
int cnt, last, d;
foreach (a; AS) {
if (last) {
if (d == 0) {
if (last > a) {
d = -1;
} else if (last < a) {
d = 1;
}
} else if ((d == -1 && a > last) || (d == 1 && a < last)) {
++cnt;
d = 0;
}
}
last = a;
}
writeln(cnt+1);
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto n = readln.chomp.to!int;
writeln(n*(n+1)/2);
}
|
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 ab = readln.split.to!(int[]);
auto A = ab[0];
auto B = ab[1];
auto PS = readln.split.to!(int[]);
int a, b, c;
foreach (p; PS) {
if (p <= A) {
++a;
} else if (p <= B) {
++b;
} else {
++c;
}
}
writeln(min(a, b, c));
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
auto A = new long[](N);
auto B = new long[](N);
foreach (i; 0 .. N)
scan(A[i], B[i]);
long ans;
foreach_reverse (i; 0 .. N)
{
ans += (B[i] - ((A[i] + ans) % B[i])) % B[i];
}
writeln(ans);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.math;
import std.range;
void main() {
auto ip = readln.split.to!(int[]), A = ip[0], B = ip[1], C = ip[2], D = ip[3];
if(B <= C || D <= A) 0.writeln;
else if(A <= C) (B < D ? B - C : D - C).writeln;
else (B < D ? B - A : D - A).writeln;
}
|
D
|
void main(){
int n = _scan();
(n^^3).writeln();
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main(){
auto z=readln.split.to!(int[]),a=z[0],b=z[1],c=z[2];
if(a+b<=a+c&&a+b<=b+c)writeln(a+b);
else if(a+c<=a+b&&a+c<=b+c)writeln(a+c);
else if(b+c<=a+b&&b+c<=a+c)writeln(b+c);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf = 10^^6;
void main() {
int r;
scan(r);
writeln(r < 1200 ? "ABC" : r < 2800 ? "ARC" : "AGC");
}
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;
long[2001] a,b;
long n,x,y,ans;
void main()
{
scanf(" %d",&n);
for(int i=0;i<n;++i)
{
scanf(" %d %d",&x,&y);
++a[to!uint(x+y)];
++b[to!uint(1000+x-y)];
}
for(int i=0;i<=2000;++i)
{
if(a[i]>1) ans+=a[i]*(a[i]-1)/2;
if(b[i]>1) ans+=b[i]*(b[i]-1)/2;
}
writeln(ans);
}
|
D
|
void main()
{
long x = rdElem;
long result = ceil(0.5 * (-1 + sqrt(1.0 + 8.0 * x))).to!long;
result.writeln;
}
T rdElem(T = long)()
{
//import std.stdio : readln;
//import std.string : chomp;
//import std.conv : to;
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
dchar[] rdDchar()
{
//import std.conv : to;
return rdStr.to!(dchar[]);
}
T[] rdRow(T = long)()
{
//import std.stdio : readln;
//import std.array : split;
//import std.conv : to;
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
//import std.range : iota;
//import std.algorithm : map;
//import std.array : array;
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
//import std.range : iota;
//import std.algorithm : map;
//import std.array : array;
return iota(col).map!(x => rdRow!T).array;
}
void wrMat(T = long)(T[][] mat)
{
//import std.stdio : write, writeln;
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int q;
rd(q);
int l = 0, r = 0; // [l, r)
auto pos = new int[](2 * 10 ^^ 5 + 1);
while (q--) {
char t;
int x;
rd(t, x);
if (t == 'L') {
pos[x] = --l;
} else if (t == 'R') {
pos[x] = r++;
} else {
writeln(min(pos[x] - l, r - pos[x] - 1));
}
}
}
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;
void main(){
auto cin = new Cin();
auto ab = cin.line!long();
if( ab[0] <= 0 && 0 <= ab[1] ){
writeln("Zero");
} else if( ab[1]<0 && (ab[1]-ab[0])%2==0 ){
writeln("Negative");
} else {
writeln("Positive");
}
//sum.writeln();
}
auto solve(){
}
unittest{
}
import std.stdio,std.conv,std.string;
import std.algorithm,std.array;
class Cin
{
T[] line( T = size_t , string token = " " )( size_t m = 1 ){
T[] arr = [];
foreach( i ; 0..m ){
arr ~= this.read!T();
}
return arr;
}
T[][] rect( T = size_t , string token = " " )( size_t m = 1 ){
T[][] arr = new T[][](m);
foreach( i ; 0..m ){
arr[i] = this.read!T(token);
}
return arr;
}
private T[] read( T = size_t )( string token = " " ){
T[] arr;
foreach( elm ; readln().chomp().split(token) ){
arr ~= elm.to!T();
}
return arr;
}
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
while (true) {
int N, M;
scan(N, M);
if (N == 0 && M == 0) return;
auto c = iota(M).map!(i => readln.chomp.to!int).array;
auto x = iota(N).map!(i => readln.chomp.to!int).array;
auto ans = solve(N, M, c, x);
writeln(ans);
}
}
int solve(int N, int M, int[] c, int[] x) {
auto dp = new int[][](N + 1, 256);
fillAll(dp, inf);
dp[0][128] = 0;
foreach (i ; 0 .. N) {
foreach (j ; 0 .. 256) {
foreach (k ; 0 .. M) {
int y = clamp(j + c[k], 0, 255);
chmin(dp[i + 1][y], dp[i][j] + (y - x[i])^^2);
}
}
}
debug {
//writefln("%(%s\n%)", dp);
}
int ans = int.max;
foreach (j ; 0 .. 256) {
chmin(ans, dp[N][j]);
}
return ans;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import core.bitop, 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[] 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, U, V;
scan(N, U, V);
U--, V--;
auto G = new long[][](N);
foreach (_; 0 .. N - 1)
{
long A, B;
scan(A, B);
A--, B--;
G[A] ~= B;
G[B] ~= A;
}
if (G[U].length == 1 && G[U][0] == V)
{
writeln(0);
return;
}
void bfs1(ref long[] leave, ref long[] dist, long v)
{
DList!long S;
S.insertBack(v);
dist[] = -1;
dist[v] = 0;
leave[] = true;
while (!S.empty)
{
long u = S.front;
S.removeFront;
foreach (w; G[u])
if (dist[w] == -1)
{
dist[w] = dist[u] + 1;
S.insertBack(w);
leave[u] = false;
}
}
}
auto du = new long[](N);
auto l = new long[](N);
auto dv = new long[](N);
bfs1(l, du, U);
bfs1(l, dv, V);
long e = V;
foreach (w; 0 .. N)
if (du[w] < dv[w] && dv[e] < dv[w] && l[w])
e = w;
// du.writeln;
// dv.writeln;
// writeln(e, " ", du[e], " ", dv[e]);
writeln(dv[e] - 1);
}
|
D
|
module app;
import core.bitop;
import std.algorithm;
import std.array;
import std.bigint;
import std.conv;
import std.stdio;
import std.string;
struct Input
{
ulong a, b;
}
void parseInput(T)(out Input input, T file)
{
with (file) with (input)
{
auto array = readln().strip().split();
a = array[0].to!ulong;
b = array[1].to!ulong;
}
}
struct Output
{
}
auto main2(Input* input)
{
return lcm(input.a, input.b);
}
unittest { writeln("begin unittest"); }
unittest // example1
{
string example =
`2 3`;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
writeln(result);
assert(result == 6);
}
unittest // example2
{
string example =
`123 456`;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
writeln(result);
assert(result == 18696);
}
unittest // example3
{
string example =
`100000 99999`;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
writeln(result);
assert(result == 9999900000);
}
unittest { writeln("end unittest"); }
void parseExample(out Input input, string example)
{
struct Adapter
{
string[] _lines;
this(string input) { _lines = input.splitLines(); }
string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; }
}
parseInput(input, Adapter(example));
}
void main()
{
Input input = void;
parseInput(input, stdin);
auto result = main2(&input);
writeln(result);
}
T lcm(T)(T i1, T i2)
{
return i1 / gcm(i1, i2) * i2;
}
T gcm(T)(T i1, T i2)
{
T large = i1 < i2 ? i2 : i1;
T small = i1 < i2 ? i1 : i2;
while (small != 0)
{
T q = large % small;
large = small;
small = q;
}
return large;
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;
immutable long MOD = 10^^9 + 7;
void main() {
auto N = readln.chomp.to!int;
auto S = readln.chomp;
auto dp = new long[][](N, N);
dp[0][] = 1;
foreach (i; 1..N) {
if (S[i-1] == '>') {
long s = dp[i-1][N-i];
foreach_reverse (j; 0..N-i) {
(dp[i][j] += s) %= MOD;
(s += dp[i-1][j]) %= MOD;
}
} else {
long s = dp[i-1][0];
foreach (j; 0..N-i) {
(dp[i][j] += s) %= MOD;
(s += dp[i-1][j+1]) %= MOD;
}
}
}
long ans = 0;
foreach (j; 0..N)
(ans += dp[N-1][j]) %= MOD;
ans.writeln;
}
|
D
|
import std.stdio, std.conv, std.string, std.math, std.regex, std.range, std.ascii, std.algorithm;
void main(){
auto N = readln.split.to!(int[]), a=N[0], b=N[1], c=N[2], d=N[3];
if(abs(c-a)<=d||(abs(b-a)<=d&&abs(c-b)<=d)) writeln("Yes");
else writeln("No");
}
|
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()
{
auto s = sread();
bool b = s[0] == '9' || s[1] == '9';
writeln(b ? "Yes" : "No");
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
ulong MOD = 1_000_000_007;
ulong INF = 1_000_000_000_000;
alias Loc = Tuple!(long, "x", long, "y", long, "h");
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;
}
}
auto charcount(string s)
{
char current = s[0];
auto ret = new long[](1);
foreach (e; s)
{
if (e != current)
{
ret ~= [0];
current = e;
}
ret[$ - 1]++;
}
return ret;
}
void main()
{
long n, k;
scan(n, k);
auto remainder = new long[](k);
foreach (i; iota(1, n + 1))
remainder[i % k]++;
if (k % 2)
{
writeln(remainder[0] ^^ 3);
}
else
{
auto result = remainder[0] ^^ 3;
result += remainder[k / 2] ^^ 3;
result.writeln();
}
}
|
D
|
void main() {
problem();
}
void problem() {
auto a = scan!long;
auto b = scan!long;
auto c = scan!long;
auto d = scan!long;
long solve() {
if ((b <= 0 && c >= 0) || (a >= 0 && d <= 0)) {
return -1 * min(abs(a), abs(b)) * min(abs(c), abs(d));
}
return max(a*c, b*d);
}
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;
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()
{
immutable mod = 1_000_000_000 + 7;
auto n = readln.chomp.to!int;
long res = 1;
foreach (i; 1..n+1) {
res *= i;
res %= mod;
}
res.writeln;
}
|
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()
{
long q, h, s, d;
scan(q, h, s, d);
// writeln(q, ' ', h, ' ', s, ' ', d);
auto n = lread();
// writeln(n, "L買いたい");
long x = 4 * q; //0.25で作った1
long y = 2 * h; //0.5で作った1
// writeln("1: ", s);
// writeln("0.25で作った1: ", x);
// writeln("0.5で作った1: ", y);
// writeln("2: ", d);
// writeln("0.25で作った2: ", x * 2);
// writeln("0.5で作った2: ", y * 2);
long min_1 = min(s, x, y);
long min_2 = min(d, x * 2, y * 2, min_1 * 2);
// writeln(min_1);
// writeln(min_2);
if (n == 1)
{
writeln(min_1);
return;
}
if (n % 2 != 0)
{
writeln(min((n / 2) * min_2 + min_1, min_1 * n));
return;
}
else
{
writeln(min(min_1 * n, min_2 * (n / 2)));
return;
}
}
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.array;
import std.math;
void main()
{
auto reader = readln.split;
int A = reader[0].to!int;
int B = reader[1].to!int;
int diff = abs(A - B);
if (diff % 2 == 0){
writeln((A + B) / 2);
} else {
writeln("IMPOSSIBLE");
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.container;
import std.array;
import std.math;
import std.range;
import std.typecons;
import std.ascii;
import std.format;
void main()
{
int n = readln.chomp.to!int;
long[] t = new long[](n);
foreach (ref v; t) {
v = readln.chomp.to!long;
}
long ite = 1;
foreach (v; t) {
ite = lcm(ite, v);
}
writeln = ite;
}
long lcm(long a, long b)
{
return a / gcd(a, b) * b;
}
long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
|
D
|
module AOJ_Volume0101;
import std.stdio,std.string,std.conv,std.array;
int main()
{
int n = readln.chomp.to!int;
while(n--)
{
string s = readln.chomp;
replace(s,"Hoshino","Hoshina").writeln;
}
return 0;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void main()
{
auto N = RD;
auto K = RD;
long ans = 1;
while (N >= K)
{
++ans;
N /= K;
}
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.conv, std.string, std.math, std.regex, std.range, std.ascii, std.algorithm;
void main(){
auto N = readln.chomp.to!long;
auto W = readln.split.to!(long[]);
long count = W.sum;
foreach(i; 0..N){
long x = abs(W.sum - 2*((W[0..i]).sum));
if(x < count) count=x;
}
writeln(count);
}
|
D
|
import std;
alias S = Tuple!(int, "a", int, "b", int, "c", int, "d");
int calc(int n, int m, S[] s) {
int ans = 0;
void rec(int last, int p, int[] buf) {
if (p == buf.length) {
int t = 0;
foreach (e; s) {
int x = buf[e.b - 1] - buf[e.a - 1];
if (x == e.c) t += e.d;
}
ans = max(ans, t);
return;
}
for (int i = last; i <= m; i++) {
buf[p] = i;
rec(i, p + 1, buf);
}
}
auto buf = new int[n];
rec(1, 0, buf);
return ans;
}
void main() {
int n, m, q; scan(n, m, q);
S[] s;
foreach (_; 0..q) {
int a, b, c, d; scan(a, b, c, d);
s ~= S(a, b, c, d);
}
writeln(calc(n, m, s));
}
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto hw = readln.split.to!(int[]);
auto H = hw[0];
auto W = hw[1];
int[][] cs;
foreach (_; 0..H) cs ~= readln.split.to!(int[]);
int[] xs1, ys1, xs2, ys2;
foreach (i; 0..H) {
foreach (j; 0..W) {
if (cs[i][j] % 2 == 1) {
xs1 ~= j+1;
ys1 ~= i+1;
if (j < W-1) {
cs[i][j+1] += 1;
xs2 ~= j+2;
ys2 ~= i+1;
} else if (i < H-1) {
cs[i+1][j] += 1;
xs2 ~= j+1;
ys2 ~= i+2;
}
}
}
}
writeln(xs2.length);
foreach (i; 0..xs2.length) {
writeln(ys1[i], " ", xs1[i], " ", ys2[i], " ", xs2[i]);
}
}
|
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 edges = new long[][](N);
foreach (i; 0..N)
{
auto S = RD!string;
foreach (j, c; S)
{
if (c == '1')
{
edges[i] ~= j;
}
}
}
/*while (true){
auto N = 5;
auto S = new char[][](N, N);
foreach (i; 0..N)
{
auto num = uniform(0, N-1);
if (i != 0)
{
auto pos = uniform(0, i);
S[i][pos] = '1';
S[pos][i] = '1';
}
foreach (j; 0..num)
{
auto pos = i;
while (pos == i)
{
pos = uniform(0, N);
}
S[i][pos] = '1';
S[pos][i] = '1';
}
}
auto edges = new long[][](N);
foreach (i; 0..N)
{
foreach (j, c; S[i])
{
if (c == '1')
{
edges[i] ~= j;
}
}
}
foreach (i; 0..N)
{
foreach (to; edges[i])
writeln(i, " ", to);
}
long _ans = -1;
foreach (i1; 0..N)
{
foreach (i2; 0..N)
{
foreach (i3; 0..N)
{
foreach (i4; 0..N)
{
foreach (i5; 0..N)
{
auto list = [i1, i2, i3, i4, i5];
auto set = new bool[](N);
foreach (e; list)
set[e] = true;
long cnt;
foreach (e; set)
if (e) ++cnt;
if (cnt <= _ans) continue;
auto close = new bool[](N);
bool dfs(long from, long par)
{
if (close[from]) return true;
close[from] = true;
foreach (to; edges[from])
{
if (to == par) continue;
if (list[to] == list[from]-1 || list[to] == list[from]+1)
{
auto r = dfs(to, from);
if (!r) return false;
}
else
return false;
}
return true;
}
auto r = dfs(0, 0);
if (r)
{
_ans = cnt;
debug writeln(list);
}
}
}
}
}
}
debug writeln("ans:", _ans);*/
long bfs(long pos)
{
long[2][] open = [[pos, -1]];
auto dist = new long[](N);
dist[] = -1;
dist[pos] = 0;
while (!open.empty)
{
auto n = open.front; open.popFront;
auto from = n[0];
auto par = n[1];
foreach (to; edges[from])
{
if (to == par) continue;
if (dist[to] != -1)
{
auto diff = dist[from]+1 - dist[to];
if (diff != 0 && diff != 2)
return -1;
continue;
}
dist[to] = dist[from] + 1;
open ~= [to, from];
}
}
long p = dist.MIN_POS!"a > b"();
debug writeln("dist:", dist);
return dist[p]+1;
}
long ans = -1;
foreach (i; 0..N)
{
auto r = bfs(i);
debug writeln(r);
/*if (r == -1)
{
ans = -1;
break;
}*/
ans = max(ans, r);
}
writeln(ans);
//if (_ans != ans) break;
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n, d;
scan(n, d);
auto c = 2*d + 1;
auto ans = (n + c - 1) / c;
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, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
string s; readV(s);
writeln(s.length/2-s.count('p'));
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
auto s = readln.chomp.to!(char[]);
auto t = readln.chomp.to!(char[]);
char[char] map;
foreach (i; 0 .. s.length) {
if (s[i] in map) {
if (t[i] != map[s[i]]) {
writeln("No");
return;
}
} else {
map[s[i]] = t[i];
}
}
char[char] map2;
foreach (i; 0 .. t.length) {
if (t[i] in map2) {
if (s[i] != map2[t[i]]) {
writeln("No");
return;
}
} else {
map2[t[i]] = s[i];
}
}
writeln("Yes");
}
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 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 A, B, C;
scan(A, B);
long a, b;
scan(a, b);
writeln((A * B) - (a * B) - (A * b) + (a * b));
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string;
void main()
{
auto ss = readln.chomp;
while (ss) {
ss = ss[0..$-2];
auto l = ss.length;
if (ss[0..l/2] == ss[l/2..$]) {
writeln(l);
return;
}
}
}
|
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, std.bitmanip;
void main() {
auto S = readln.chomp;
auto T = readln.chomp;
auto N = S.length.to!int;
auto M = T.length.to!int;
auto A = readln.split.map!(to!int).array;
auto B = new int[](N);
foreach (i; 0..N) {
B[A[i]-1] = i;
}
int hi = N;
int lo = M - 1;
while (hi - lo > 1) {
int mid = (hi + lo) / 2;
int cnt = 0;
int del = N - mid;
foreach (i; 0..N) {
if (B[i] < del)
continue;
if (S[i] == T[cnt])
cnt += 1;
if (cnt >= M)
break;
}
if (cnt >= M)
hi = mid;
else
lo = mid;
}
(N-hi).writeln;
}
|
D
|
void main()
{
int[] tmp = readln.split.to!(int[]);
int m1 = tmp[0], d1 = tmp[1];
tmp = readln.split.to!(int[]);
int m2 = tmp[0], d2 = tmp[1];
writeln(m1 != m2 && d1 > d2 ? 1 : 0);
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.conv, std.array, std.string;
import std.algorithm;
import std.container;
import std.range;
import core.stdc.stdlib;
import std.math;
void main() {
auto S = readln.chomp;
writeln(S == "Sunny" ? "Cloudy" : S == "Cloudy" ? "Rainy" : "Sunny");
}
|
D
|
import std;
void main() {
int n; scan(n);
int m = n % 10;
if (m == 3) writeln("bon");
else if ([0, 1, 6, 8].canFind(m)) writeln("pon");
else writeln("hon");
}
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T=string)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readints = reads!int;
|
D
|
// 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;
// dfmt on
void main()
{
long K = lread();
auto S = sread();
if (S.length <= K)
{
writeln(S);
return;
}
writeln(S[0 .. K], "...");
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto T = readln.chomp.to!int;
foreach (_t; 0..T) {
auto nn = readln.split.to!(int[]);
auto N = nn[0];
auto A = nn[1];
auto B = nn[2];
auto C = nn[3];
auto D = nn[4];
if ((A-B)*N > (C+D) || (A+B)*N < (C-D)) {
writeln("No");
} else {
writeln("Yes");
}
}
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
void main()
{
while(1){
auto n = readln.chomp.to!int;
if(n==0) break;
int s=1;
int max=50000001;
for(int i=2;i*i<n;++i){
if(n%i==0){
if(i<max){
s+=(i+n/i);
max = n/i;
if(i==max)
s -= i;
}else break;
}else if(i>max) break;
}
if(s<n || n==1) writeln("deficient number");
else if(s==n) writeln("perfect number");
else writeln("abundant number");
}
}
|
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 A, B, C;
scan(A, B, C);
if (B < A)
swap(A, B);
bool b = A < C && C < B;
writeln(b ? "Yes" : "No");
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.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 k = RD!int;
auto s = new string[](n);
foreach (i; 0..n)
s[i] = RD!string;
auto toNum = ['S':0, 'E':1, 'T':2];
int[long] set;
foreach (i; 0..n)
{
long key;
foreach (j; 0..k)
{
auto num = toNum[s[i][j]];
key += num * (3^^j);
}
++set[key];
debug writeln("i:", i, " key:", key);
}
debug writeln(set);
long ans;
foreach (i; 0..n)
{
foreach (j; i+1..n)
{
long key;
foreach (l; 0..k)
{
if (s[i][l] == s[j][l])
key += toNum[s[i][l]] * (3^^l);
else
{
auto num = 3 - (toNum[s[i][l]] + toNum[s[j][l]]);
key += num * (3^^l);
}
}
ans += set.get(key, 0);
debug writeln("i:", i, " j:", j, " ans:", ans);
}
}
writeln(ans/3);
stdout.flush;
debug readln;
}
|
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;
int[] as;
int[3] ns;
foreach (c; readln.chomp) {
auto a = c - '1';
++ns[a];
as ~= a;
}
foreach (ref a; as) {
if (ns[1] == 0) {
a /= 2;
} else if (a == 2) {
a = 0;
}
}
// Lucas' theorem
auto ls = new bool[](N);
ls[0] = true;
loop: foreach (x; 1..N) {
static foreach (i; 0..20) if (((N-1)&(1<<i)) < (x&(1<<i))) continue loop;
ls[x] = true;
}
int x;
foreach (i, a; as) if (ls[i]) x ^= a;
writeln(x == 1 ? ns[1] > 0 ? 1 : 2 : 0);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math;
void main()
{
auto ab = readln.split.to!(int[]);
writeln((ab[0]&1) == 1 && (ab[1]&1) == 1 ? "Odd" : "Even");
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto O = RD!string;
auto E = RD!string;
string ans;
foreach (i; 0..E.length)
{
ans ~= [O[i]] ~ [E[i]];
}
if (O.length > E.length)
ans ~= O[$-1];
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.string, std.conv;
import std.algorithm, std.array, std.range;
auto solve(string s_){
immutable K = s_.chomp.to!int();
if(K<13) return K.to!string();
string a;
void add(int i){ a~=(i+'0').to!char(); }
immutable M=18;
auto dp = new int[10][M];
dp[1][]=1;
auto t=9;
foreach(i;2..M) foreach(j;0..10){
auto n=dp[i-1][j];
if(j>0) n+=dp[i-1][j-1];
if(j<9) n+=dp[i-1][j+1];
dp[i][j]=n;
if(j==0) continue;
if(t+n<K){
t+=n;
continue;
}
a.reserve(i);
add(j);
auto p=j;
foreach_reverse(m;1..i){
foreach(k;max(0,p-1)..min(10,p+2)){
if(t+dp[m][k]>=K){
add(k);
p=k;
break;
}
t+=dp[m][k];
}
}
return a;
}
return a;
}
void main(){ for(string s; (s=readln.chomp()).length;) writeln(solve(s)); }
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
immutable int n = 2019;
void divmod(int x, ref int a, ref int b) {
a = x / n;
b = x % n;
}
void main() {
int[] tmp = readln.split.to!(int[]);
int ld, lm, rd, rm;
divmod(tmp[0], ld, lm);
divmod(tmp[1], rd, rm);
if (ld != rd) {
0.writeln;
} else {
int minv = n;
foreach (i; lm .. rm) {
foreach (j; i+1 .. rm+1) {
minv = min(minv, i * j % n);
}
}
minv.writeln;
}
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdlib;
void main() {
auto N = readln.chomp.to!int;
auto S = readln.chomp;
auto R = new int[](N+1);
auto G = new int[](N+1);
auto B = new int[](N+1);
foreach (i; 0..N) R[i+1] += R[i] + (S[i]=='R');
foreach (i; 0..N) G[i+1] += G[i] + (S[i]=='G');
foreach (i; 0..N) B[i+1] += B[i] + (S[i]=='B');
long ans = 0;
foreach (i; 0..N) foreach (k; i+2..N) if (S[i] != S[k]) {
int par = (k - i + 1) % 2;
int mid = (k + i) / 2;
if ((S[i] == 'R' && S[k] == 'G') || (S[i] == 'G' && S[k] == 'R')) {
ans += B[k+1] - B[i];
if (par) ans -= S[mid] == 'B';
} else if ((S[i] == 'R' && S[k] == 'B') || (S[i] == 'B' && S[k] == 'R')) {
ans += G[k+1] - G[i];
if (par) ans -= S[mid] == 'G';
} else if ((S[i] == 'B' && S[k] == 'G') || (S[i] == 'G' && S[k] == 'B')) {
ans += R[k+1] - R[i];
if (par) ans -= S[mid] == 'R';
}
}
ans.writeln;
}
|
D
|
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.complex;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
auto readInts() {
return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
return readInts()[0];
}
auto readLongs() {
return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
return readLongs()[0];
}
void readlnTo(T...)(ref T t) {
auto s = readln().split();
assert(s.length == t.length);
foreach(ref ti; t) {
ti = s[0].to!(typeof(ti));
s = s[1..$];
}
}
const real eps = 1e-10;
void main(){
int k, t;
readlnTo(k,t);
auto a = readInts();
auto m = reduce!(max)(0, a);
writeln(max(0, m-1-(a.sum-m)));
}
|
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()
{
writeln(min(lread(), lread()) + min(lread(), lread()));
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.range, std.array, std.typecons;
void main() {
auto s = readln.strip;
if (s[0] == s[1] || s[1] == s[2] || s[2] == s[3]) {
writeln("Bad");
} else {
writeln("Good");
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nx = readln.split.to!(int[]);
auto N = nx[0];
auto x = nx[1];
if (x == 1 || x == N*2-1) {
writeln("No");
return;
}
writeln("Yes");
int[] line;
bool[] memo;
line.length = N*2-1;
memo.length = N*2;
line[N-1] = x;
memo[x] = true;
line[N-2] = 1;
memo[1] = true;
line[N] = N*2-1;
memo[N*2-1] = true;
if (N > 2) {
int i = N*2-2;
while (memo[i]) --i;
line[N-3] = i;
memo[i] = true;
int j = 2;
while (memo[j]) ++j;
line[N+1] = j;
memo[j] = true;
}
x = 3;
foreach (ref n; line) if (n == 0) {
while (memo[x]) ++x;
n = x;
memo[x++] = true;
}
foreach (n; line) writeln(n);
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
auto n = lread();
auto a = aryread();
// writeln(a);
auto ans = new long[](n);
foreach (i; 0 .. (n - 1))
{
if (a[i] == a[i + 1])
{
ans[i + 1] = 0;
}
else if (a[i] > a[i + 1])
{
ans[i + 1] = a[i] - a[i + 1];
a[i + 1] = a[i];
}
}
// writeln(ans);
sum(ans).writeln();
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
|
D
|
import std.stdio, std.conv, std.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;
auto S = RD!string;
long ans;
foreach (i; 0..N-2)
{
if (S[i..i+3] == "ABC")
++ans;
}
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.