code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
unittest
{
assert( [ "happy,newyear,enjoy" ].parse.expand.solve == "happy newyear enjoy" );
assert( [ "haiku,atcoder,tasks" ].parse.expand.solve == "haiku atcoder tasks" );
assert( [ "abcde,fghihgf,edcba" ].parse.expand.solve == "abcde fghihgf edcba" );
}
import std.conv;
import std.range;
import std.stdio;
import std.typecons;
void main()
{
stdin.byLineCopy.parse.expand.solve.writeln;
}
auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
auto s = input.front;
return tuple( s );
}
auto solve( string s )
{
return s.replace( ",", " " );
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.math;
int f(int x, int y) {
return x - y + 1;
}
void main() {
int n = readln.strip.to!int;
int h = readln.strip.to!int;
int w = readln.strip.to!int;
writeln(f(n, h) * f(n, w));
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
long N = lread();
auto A = aryread();
(A.reduce!max - A.reduce!min).writeln();
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
enum MOD = (10 ^^ 9) + 7;
void main()
{
string[2] s;
s[0] = sread();
s[1] = sread();
foreach (i; 0 .. s[0].length + s[1].length)
write(s[i & 1][i / 2]);
writeln();
}
|
D
|
// import chie template :) {{{
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons;
// }}}
// nep.scanner {{{
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string : chomp;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.chomp.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
}
// }}}
int[] getTable() {
bool[] era = new bool[100010];
era[2 .. $] = true;
for (int i = 0; i * i < era.length; i++)
{
if (!era[i]) continue;
for (int j = i + i; j < era.length; j += i)
{
era[j] = false;
}
}
int[] res = new int[100010];
foreach (i; 0 .. era.length)
{
if (era[i] && era[(i + 1) / 2])
{
res[i] = 1;
}
}
foreach (i; 1 .. res.length) {
res[i] += res[i - 1];
}
return res;
}
void main()
{
static auto imos = getTable;
auto cin = new Scanner;
int q;
cin.scan(q);
foreach (i; 0 .. q) {
int l, r;
cin.scan(l, r);
writeln(imos[r] - imos[l - 1]);
}
}
|
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() {
int n, a, b;
scan(n, a, b);
auto ans = powmod(2, n);
long na = f(n, a);
long nb = f(n, b);
ans -= (na + nb) % mod;
if (ans < 0) ans += mod;
ans--;
if (ans < 0) ans += mod;
writeln(ans);
}
long f(int n, int a) {
long res = 1;
foreach (i ; 0 .. a) {
res *= (n - i);
res %= mod;
res *= powmod(a - i, mod - 2);
res %= mod;
}
return res;
}
long powmod(long x, long y) {
return y > 0 ? powmod(x, y >> 1)^^2 % mod * x^^(y & 1) % mod : 1L;
}
struct ModComb {
int _N;
long[] _fact, _factinv;
long _mod;
this(int n, long mod = 1_000_000_007L) {
_N = n;
_fact = new long[](_N + 1);
_factinv = new long[](_N + 1);
_mod = mod;
_fact[0] = 1;
foreach (i ; 1 .. _N + 1) {
_fact[i] = (_fact[i-1] * i) % mod;
}
_factinv[_N] = _powmod(_fact[_N], mod - 2);
foreach_reverse (i ; 0 .. _N) {
_factinv[i] = (_factinv[i+1] * (i+1)) % mod;
}
}
long c(int n, int k) {
if (k < 0 || k > n) return 0;
return f(n) * finv(n - k) % _mod * finv(k) % _mod;
}
long p(int n, int r) {
if (r < 0 || r > n) return 0;
return f(n) * finv(n - r) % _mod;
}
long f(int n) {
return _fact[n];
}
long finv(int n) {
return _factinv[n];
}
long _powmod(long x, long y) {
return y > 0 ? _powmod(x, y>>1)^^2 % _mod * x^^(y & 1) % _mod : 1;
}
}
unittest {
auto mc = ModComb(1_000_000);
assert(mc.c(5, 2) == 10);
}
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.math, std.algorithm, std.array, std.string, std.conv, std.container, std.range;
T Read(T)() { return readln.chomp.to!(T); }
T[] Reads(T)() { return readln.split.to!(T[]); }
alias read = Read!(int);
alias reads = Reads!(int);
void main() {
string s = readln.chomp;
foreach (i;0..3) if (s[i] == s[i + 1]) {
writeln("Bad");
return;
}
writeln("Good");
}
|
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;
bool ok1(int src, int a, int b, string s) {
if (src + 1 >= s.length) return false;
if (src + 1 == a || src + 1 == b) return false;
if (s[src + 1] == '#') return false;
return true;
}
bool ok2(int src, int a, int b, string s) {
if (src + 2 >= s.length) return false;
if (src + 2 == a || src + 2 == b) return false;
if (s[src + 2] == '#') return false;
return true;
}
bool calc_ba(string s, int n, int a, int b, int c, int d) {
assert(c > d);
// a を先に進ませる
while (a != c || b != d) {
if (a > c || b > d) break;
// a を先に進ませて、b を超えさせる
if (a < b) {
if (ok1(a, a, b, s)) {
a++;
continue;
}
else if (ok2(a, a, b, s)) {
a += 2;
continue;
}
else if (ok1(b, a, b, s)) {
b++;
continue;
}
else if (ok2(b, a, b, s)) {
b += 2;
continue;
}
else break; // a, b いずれも移動できない
}
assert(a > b);
if (a != c) {
if (ok1(a, a, b, s)) {
a++;
continue;
}
else if (ok2(a, a, b, s)) {
a += 2;
continue;
}
else break;
}
if (b != d) {
if (ok1(b, a, b, s)) {
b++;
continue;
}
else if (ok2(b, a, b, s)) {
b += 2;
continue;
}
else break;
}
}
return a == c && b == d;
}
bool calc(string s, int n, int a, int b, int c, int d) {
if (c > d) return calc_ba(s, n, a, b, c, d);
assert(c < d);
while (a != c || b != d) {
if (b != d) {
if (ok1(b, a, b, s)) {
b++;
continue;
}
else if (ok2(b, a, b, s)) {
b += 2;
continue;
}
else break;
}
if (a != c) {
if (ok1(a, a, b, s)) {
a++;
continue;
}
else if (ok2(a, a, b, s)) {
a += 2;
continue;
}
else break;
}
}
return a == c && b == d;
}
void main() {
int n, a, b, c, d; scan(n, a, b, c, d);
string s = read!string;
writeln(calc(s, n, a-1, b-1, c-1, d-1) ? "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)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new string[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto s = RD!string;
auto cnt = new long[](26);
foreach (i; 0..n)
{
auto c = s[i] - 'a';
++cnt[c];
}
foreach (i; 0..26)
{
auto c = cast(char)('a'+i);
foreach (j; 0..cnt[i])
ans[ti] ~= c;
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
alias route = Tuple!(long, "From", long, "To");
long bignum = 1_000_000_007;
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
long x, y;
scan(x, y);
if (x % y)
{
foreach (n; 0 .. y)
{
if(n * x % y)
{
writeln(n * x);
return;
}
}
}
else
{
writeln(-1);
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;
void main ()
{
string a;
while ((a = readln.strip) != "")
{
auto n = a.length.to !(int);
int [char] w;
foreach (c; a)
{
w[c] += 1;
}
auto res = 1 + (n * (n - 1L)) / 2;
foreach (k, v; w)
{
res -= (v * (v - 1L)) / 2;
}
writeln (res);
}
}
|
D
|
import std.functional,
std.algorithm,
std.bigint,
std.string,
std.traits,
std.array,
std.range,
std.stdio,
std.conv;
void main() {
long N = readln.chomp.to!long;
bool[long] A;
foreach (_; 0..N) {
long a = readln.chomp.to!long;
if (a in A) {
A[a] = !A[a];
} else {
A[a] = true;
}
}
long ans;
foreach (k, v; A) {
if (v) { ans++; }
}
writeln(ans);
}
|
D
|
import std.stdio;
import std.conv;
import std.algorithm;
import std.range;
import std.string;
import std.typecons;
import std.math;
import std.random;
import std.range;
void main()
{
auto a = readln().strip.split(" ").map!(to!int);
auto c = 0;
foreach (i; iota(a[0]))
{
auto k = readln.strip.to!int;
if (k == 2)
{
c++;
continue;
}
if (k < 2)
continue;
if (k % 2 == 0)
continue;
for (auto j = 3; j ^^ 2 <= k; j += 2)
{
if (k % j == 0)
goto break_;
}
c++;
break_:
}
writeln(c);
}
|
D
|
import std.stdio, std.string, std.array, std.conv;
struct S {
string name;
int time;
}
struct T {
private:
int n;
S[] s;
public:
this(int n) {
this.s = new S[](n);
}
ulong length() {
return s.length;
}
S front() {
return s[0];
}
void pop() {
s = s[1..$];
}
void push(S x) {
s ~= x;
}
}
void main() {
int[] tmp = readln.chomp.split.to!(int[]);
int n = tmp[0], q = tmp[1];
T t = T(n);
foreach (i; 0 .. n) {
string[] process = readln.chomp.split;
t.tupleof[1][i] = S(process[0], process[1].to!int);
}
int elaps = 0;
while (t.length) {
S s = t.front;
t.pop;
if (s.time > q) {
elaps += q;
t.push(S(s.name, s.time-q));
} else {
elaps += s.time;
writeln(s.name, " ", elaps);
}
}
}
|
D
|
// Cheese-Cracker: cheese-cracker.github.io
void solve(){
auto arr = scanArray;
auto summ = arr[6];
long a = summ - arr[5];
long b = summ - arr[4];
long c = summ - a - b;
writeln(a, " ", b, " ", c);
}
void main(){
long tests = scan; // Toggle!
while(tests--) solve;
}
/************** ***** That's All Folks! ***** **************/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math, std.numeric;
string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
alias ll = long, tup = Tuple!(long, "x", long, "y");
|
D
|
import std.algorithm,
std.string,
std.array,
std.range,
std.stdio,
std.conv;
void main() {
int n = readln.chomp.to!int;
int[] a = readln.chomp.split.to!(int[]);
int m1,
m2,
m4;
bool flag;
foreach (ai; a) {
if (ai % 4 == 0) {
m4++;
} else if (ai % 2 == 0) {
m2++;
} else {
m1++;
}
}
if (m2) {
flag = (m1 <= m4);
} else {
flag = (m1 <= m4 + 1);
}
writeln(flag ? "Yes" : "No");
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto M = s[1];
auto C = readln.split.map!(to!int).array;
auto A = readln.split.map!(to!int).array;
int ans = 0;
for (int i = 0, p = 0; i < N && p < M; ++i) {
if (C[i] <= A[p]) {
ans += 1;
p += 1;
}
}
ans.writeln;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
alias route = Tuple!(long, "From", long, "To");
long bignum = 1_000_000_007;
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
long a, b, c, d;
auto tmp = lread();
a = tmp / 1000;
tmp %= 1000;
b = tmp / 100;
tmp %= 100;
c = tmp / 10;
tmp %= 10;
d = tmp;
if (a + b + c + d == 7)
{
writeln(a, "+", b, "+", c, "+", d, "=7");
return;
}
if (a + b + c - d == 7)
{
writeln(a, "+", b, "+", c, "-", d, "=7");
return;
}
if (a + b - c + d == 7)
{
writeln(a, "+", b, "-", c, "+", d, "=7");
return;
}
if (a + b - c - d == 7)
{
writeln(a, "+", b, "-", c, "-", d, "=7");
return;
}
if (a - b + c + d == 7)
{
writeln(a, "-", b, "+", c, "+", d, "=7");
return;
}
if (a - b + c - d == 7)
{
writeln(a, "-", b, "+", c, "-", d, "=7");
return;
}
if (a - b - c + d == 7)
{
writeln(a, "-", b, "-", c, "+", d, "=7");
return;
}
if (a - b - c - d == 7)
{
writeln(a, "-", b, "-", c, "-", d, "=7");
return;
}
}
|
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;
auto s = RD!string;
long pos;
foreach (i; n/2..n)
{
if (s[i] == '0')
{
pos = i;
break;
}
}
if (pos != 0)
{
ans[ti] = [1, pos+1, 1, pos];
continue;
}
if (s[n/2-1] == '1')
ans[ti] = [n/2, n-1, n/2+1, n];
else
ans[ti] = [n/2+1, n, n/2, n];
}
foreach (e; ans)
{
writeln(e[0], " ", e[1], " ", e[2], " ", e[3]);
}
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() {
int n;
while ((n = readint) > 0) {
int[] ids;
long[int] m;
for (int i = 0; i < n; i++) {
auto xs = readints;
int no = xs[0];
long a = xs[1];
long b = xs[2];
m[no] += a * b;
if (!ids.canFind(no)) {
ids ~= no;
}
}
bool none = true;
foreach (id; ids) {
if (m[id] >= 1_000_000) {
writeln(id);
none = false;
}
}
if (none)
writeln("NA");
}
}
|
D
|
import std.stdio;
import std.string;
import std.range;
import std.conv;
void main()
{
auto ip = readln.split.to!(int[]), a = ip[0], b = ip[1];
writeln(((b-a)*(1+(b-a))/2)-b);
}
|
D
|
void main()
{
dchar c = rdElem!dchar;
writeln((c + 1).to!dchar);
}
T rdElem(T = long)()
{
//import std.stdio : readln;
//import std.string : chomp;
//import std.conv : to;
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
dchar[] rdDchar()
{
//import std.conv : to;
return rdStr.to!(dchar[]);
}
T[] rdRow(T = long)()
{
//import std.stdio : readln;
//import std.array : split;
//import std.conv : to;
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
//import std.range : iota;
//import std.algorithm : map;
//import std.array : array;
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
//import std.range : iota;
//import std.algorithm : map;
//import std.array : array;
return iota(col).map!(x => rdRow!T).array;
}
void wrMat(T = long)(T[][] mat)
{
//import std.stdio : write, writeln;
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto n = RD!int;
auto a = RDR.ARR;
auto toI = [4:0, 8:1, 15:2, 16:3, 23:4, 42:5];
auto cnt = new long[](6);
foreach (i; 0..n)
{
auto pos = toI[cast(int)a[i]];
bool ok = true;
foreach (j; 0..pos)
{
if (cnt[j] <= cnt[pos])
{
ok = false;
break;
}
}
if (ok)
++cnt[pos];
}
long ans;
ans = n - (cnt[$-1]*6);
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.conv, std.string, std.algorithm,
std.math, std.array, std.container, std.typecons;
alias tuple = Tuple!(long,long,long);
void main() {
long n = readln.chomp.to!long;
long[] a = readln.chomp.split.to!(long[]);
long[long] l;
long[long] r;
for(long i=0; i<n; i++) {
l[i+1+a[i]]++;
r[i+1-a[i]]++;
}
long result = 0;
foreach(k;l.byKey) {
result += l[k]*r.get(k, 0);
}
result.writeln;
}
|
D
|
unittest
{
assert( [ "ant", "obe", "rec" ].parse.expand.solve == "abc" );
assert( [ "edu", "cat", "ion" ].parse.expand.solve == "ean" );
}
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 css = new string[ 3 ];
foreach( ref cs; css )
{
cs = input.front.strip;
input.popFront;
}
return tuple( css );
}
auto solve( string[] css )
{
auto s = "";
foreach( i, cs; css )
{
s ~= cs[ i ];
}
return s;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto s = readln.chomp;
auto K = readln.chomp.to!int;
string[] ss;
bool[string] memo;
foreach (i; 0..s.length) {
foreach (j; i+1..min(i+6, s.length+1)) {
if (s[i..j] !in memo) {
ss ~= s[i..j];
memo[s[i..j]] = true;
}
}
}
sort(ss);
writeln(ss[K-1]);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
import std.concurrency;
void main() {
string str = readln.chomp;
auto po = (iota('a', 'z').chain('z'.only)).find!(c => !str.canFind(c)).array;
if (po.empty) {
"None".writeln;
} else {
po.front.writeln;
}
}
// ----------------------------------------------
void times(alias fun)(int n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
// 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) {
auto minElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto minimum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b < minimum) {
element = a;
minimum = b;
}
}
return element;
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto maximum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b > maximum) {
element = a;
maximum = b;
}
}
return element;
}
}
|
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 f1, f2;
foreach (e; s) {
if (!f1) {
if (e == 'C') {
f1 = true;
}
} else {
if (e == 'F') {
f2 = true;
}
}
}
if (f1 && f2) writeln("Yes");
else writeln("No");
}
|
D
|
#!/usr/bin/rdmd
import std.stdio;
immutable hW = "Hello World";
void main()
{
foreach(i;0..1000)
{
writeln(hW);
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
void main()
{
readln;
auto hs = readln.split;
sort(hs);
writeln(hs.uniq.count == 3 ? "Three" : "Four");
}
|
D
|
import std;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
auto X = BigInt(sread());
auto v = BigInt(100);
foreach (i; 0 .. 1_000_000)
{
if (X <= v)
{
writeln(i);
return;
}
v = (v * 101) / 100;
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main()
{
auto input = split( readln() );
int sup = to!int(input[1]), inf = to!int(input[0]);
int i = inf;
int cnt = 0;
while (i <= sup) {
int n1 = i % 10;
int n2 = (i % 100 - n1) / 10;
int n3 = (i % 1000 - 10 * n2 - n1) / 100;
int n4 = (i % 10000 - 100 * n3 - 10 * n2 - n1) / 1000;
int n5 = (i - 1000 * n4 - 100 * n3 - 10 * n2 - n1) / 10000;
if (n1 == n5 && n2 == n4) { cnt += 1; }
i+=1;
}
writeln(cnt);
}
|
D
|
import std.stdio, std.conv, std.string;
void main()
{
auto N = readln.chomp;
writeln("ABC", N);
}
|
D
|
void main() {
problem();
}
void problem() {
auto N = scan!int;
auto D = N.iota.map!(x => [scan!int, scan!int]);
string solve() {
int continuasLevel;
foreach(d; D) {
if (d[0] == d[1]) continuasLevel++;
if (d[0] != d[1]) continuasLevel = 0;
if (continuasLevel == 3) {
return "Yes";
}
}
return "No";
}
solve().writeln;
}
// ----------------------------------------------
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
// -----------------------------------------------
|
D
|
import std.stdio;
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 a = readln.chomp.split.to!(int[]);
auto f = false;
foreach (e; a) {
if (e % 2 == 0) {
if (e % 3 && e % 5) {
f = true;
}
}
}
if (f) {
writeln("DENIED");
} else {
writeln("APPROVED");
}
}
|
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 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
long a, b, c, d;
scan(a, b, c, d);
auto ans = (b - a + 1) - (f(b, c, d) - f(a - 1, c, d));
writeln(ans);
}
long f(long x, long c, long d) {
auto ans = x / c + x / d - x / lcm(c, d);
return ans;
}
long lcm(long c, long d) {
return c / gcd(c, d) * d;
}
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.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]]; }
double norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }
double dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }
//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 a = RD;
auto b = RD;
auto x = RD;
if (a == x || b == x)
{
ans[ti] = true;
continue;
}
if (a < b)
swap(a, b);
while (true)
{
auto rem = a % b;
if (x > a) break;
if ((x - rem) % b == 0)
{
ans[ti] = true;
break;
}
if (rem == 0) break;
a = b;
b = rem;
}
}
foreach (e; ans)
{
writeln(e ? "YES" : "NO");
}
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;
import std.concurrency;
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;
}
void main() {
readln.chomp.map!(a => a.to!int).array.sort().pipe!(str => str.length==str.uniq.array.length).pipe!(a => a ? "yes" : "no").writeln;
}
// ----------------------------------------------
// 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) {
auto minElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto minimum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b < minimum) {
element = a;
minimum = b;
}
}
return element;
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto maximum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b > maximum) {
element = a;
maximum = b;
}
}
return element;
}
}
|
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;
}
}
enum MOD = (10 ^^ 9) + 7;
void main()
{
long N = lread();
long sum;
long mins = long.max;
foreach (_; 0 .. N)
{
long s = lread();
if (s % 10 != 0)
mins = mins.min(s);
sum += s;
}
if (sum % 10 != 0)
{
writeln(sum);
return;
}
if (mins != long.max)
{
writeln(sum - mins);
return;
}
writeln(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; }
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 M = RD;
auto ac = new bool[](N);
auto wa = new long[](N);
foreach (i; 0..M)
{
auto p = RD-1;
auto S = RD!string;
if (ac[p]) continue;
if (S == "WA")
++wa[p];
else
ac[p] = true;
}
long ans0, ans1;
foreach (i; 0..N)
{
if (ac[i])
{
++ans0;
ans1 += wa[i];
}
}
writeln(ans0, " ", ans1);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio,std.string,std.conv,std.array,std.algorithm;
void main(){
for(;;write("\n")){
auto a = readln().chomp().split();
if( a[0]=="0" && a[1]=="0" ){ break; }
foreach(y;0..to!int(a[0])){
foreach(x;0..to!int(a[1])){ write("#"); }
write("\n");
}
}
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int N, K;
scan(N, K);
auto 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.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.ascii;
void main()
{
writeln(700 + readln.chomp.to!(char[]).count!(x=>x=='o') * 100);
}
|
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[64] t;
string s = sread();
foreach (c; s)
{
t[c - 'a']++;
}
writeln(t.reduce!((a, b) => (a & 1) | (b & 1)) == 0 ? "Yes" : "No");
}
|
D
|
module app;
import core.bitop;
import std.algorithm;
import std.array;
import std.bigint;
import std.container.rbtree;
import std.conv;
import std.stdio;
import std.string;
import std.traits;
struct Input
{
int k;
}
void parseInput(T)(out Input input, T file)
{
with (file) with (input)
{
k = readln().strip().to!int;
}
}
auto main2(Input* input)
{
with (input)
{
string s = "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";
auto array = s.split(",").map!(a => a.strip()).array();
return array[k - 1];
}
}
alias retType = ReturnType!main2;
static if (!is(retType == void))
{
unittest { writeln("begin unittest"); }
auto _placeholder_ = ReturnType!main2.init;
unittest // example1
{
string example =
`6`;
if (example.empty) return;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
printResult(result);
assert(result == "2");
}
unittest // example2
{
string example =
`27`;
if (example.empty) return;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
printResult(result);
assert(result == "5");
}
unittest // example3
{
string example =
``;
if (example.empty) return;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
printResult(result);
assert(result == _placeholder_);
}
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 printResult(T)(T result)
{
static if (isFloatingPoint!T) writefln("%f", result);
else writeln(result);
}
void main()
{
Input input = void;
parseInput(input, stdin);
static if (is(retType == void))
main2(&input);
else
{
auto result = main2(&input);
printResult(result);
}
}
|
D
|
// Vicfred
// https://atcoder.jp/contests/abc159/tasks/abc159_a
// math
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;
long gauss(long n) {
return n*(n-1)/2;
}
void main() {
long n = readln.chomp.to!long;
long[] a = readln.split.map!(to!long).array;
long[long] balls;
foreach(ball; a) {
if(ball in balls)
balls[ball] += 1;
else
balls[ball] = 1;
}
long ways = 0;
foreach(i; balls.keys)
ways += gauss(balls[i]);
for(int k = 0; k < n; k++) {
long ans = ways-balls[a[k]]+1;
ans.writeln;
}
}
|
D
|
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, 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 N = readInt();
auto A = new int[N];
foreach (i; 0 .. N) {
A[i] = readInt();
}
auto ans = new int[N];
int cnt;
foreach (i; 0 .. N) {
if (A[i] % 2 == 0) {
ans[i] = A[i] / 2;
} else {
if (cnt++ % 2 == 0) {
ans[i] = (A[i] + 1) / 2;
} else {
ans[i] = (A[i] - 1) / 2;
}
}
}
foreach (i; 0 .. N) {
writeln(ans[i]);
}
}
} catch (EOFException e) {
}
}
|
D
|
void main() {
auto N = ri;
auto A = iota(N).map!(a => ri).array;
if(A[0] > 0) { // むりむりのむり(X[0]は0以外の非負整数になれない)
writeln(-1);
return;
}
foreach_reverse(i; 1..N) {
if(A[i]-A[i-1] > 1) { // むりむりのむり(数列を作る過程で(後ろ)-(前)が2以上にすることはできない)
writeln(-1);
return;
}
}
ulong ans;
foreach_reverse(i; 1..N) {
debug writefln("i: %d", i);
/*
1, 2, 3, ..., n のような列はn回の操作で作れる
ex) 1 2 3 4
0 0 0 0
1 1 0 0 0
2 1 2 0 0
3 1 2 3 0
4 1 2 3 4
こういう、iota(1, n+1)のような列を「いいやつ」とする。
もし A[i] が A[i-1]+1 より小さいなら、A[i]で完成する「いいやつ」を作った後に、
A[i-1]で完成する別の「いいやつ」を作らないといけない。
ex) 1 2 3 3
0 0 0 0
1 0 1 0 0
2 0 1 2 0
3 0 1 2 3 … 1つ目の「いいやつ」が完成
4 1 1 2 3 … 2つ目の「いいやつ」を作り始める
5 1 2 2 3
6 1 2 3 3 … 2つ目の「いいやつ」が完成
こうして見ると、A[i]とA[i-1]が一つの「いいやつ」の中で存在するなら、その差は1であるから、
A[i]を作るための操作回数は1回でいい。
もしA[i]とA[i-1]が一つの「いいやつ」の中で存在しない、すなわちその差が2以上なら、
まずA[i]のための「いいやつ」を作る回数、すなわちA[i]回の操作が必要。
*/
if(A[i] - A[i-1] == 1) ans++;
else ans += A[i];
}
ans.writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
T[] deepcopy(T)(T[] a) {
static if(isArray!T) {
T[] res;
foreach(i; a) {
res ~= deepcopy(i);
}
return res;
} else {
return a.dup;
}
}
ulong[] generate_prime_list(T)(T N) if(isIntegral!T) {
ulong[] prime_list = [2];
bool not_prime = false;
foreach(i; 3..N.to!ulong+1) {
foreach(j; prime_list) {
if(i % j == 0) {
not_prime = true;
break;
}
}
if(!not_prime) prime_list ~= i;
not_prime = false;
}
return prime_list;
}
bool isPrime(ulong n) {
if(n <= 1) return false;
else if(n == 2) return true;
else if(n % 2 == 0) return false;
foreach(i; iota(3, n.to!double.sqrt.ceil+1, 2)) {
if(n % i == 0) return false;
}
return true;
}
class UnionFind(T) {
T[] arr;
this(ulong n) {
arr.length = n+1;
arr[] = -1;
}
T root(T x) {
return arr[x] < 0 ? x : root(arr[x]);
}
bool same(T x, T y) {
return root(x) == root(y);
}
bool unite(T x, T y) {
x = root(x);
y = root(y);
if(x == y) return false;
if(arr[x] > arr[y]) swap(x, y);
arr[x] += arr[y];
arr[y] = x;
return true;
}
T size(T a) {
return -arr[root(a)];
}
}
|
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 readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}}
void main()
{
int n, t; readV(n, t);
int[] w, v; readC(n, w, v);
auto s = v.sum;
auto dp = new long[][](n+1, s+1);
foreach (ref dpi; dp) dpi[] = 10L^^18;
dp[0][0] = 0;
foreach (i; 0..n)
foreach (j; 0..s+1) {
dp[i+1][j] = dp[i][j];
if (j >= v[i])
dp[i+1][j] = min(dp[i+1][j], dp[i][j-v[i]] + w[i]);
}
foreach_reverse (i; 0..s+1)
if (dp[$-1][i] <= t) {
writeln(i);
return;
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto n = RD!int;
long[3][int] list;
foreach (i; 0..n)
{
list[i] = [RD, RD, RD];
}
int[2][] ans;
while (true)
{
auto keys = list.keys;
if (keys.length == 0) break;
auto k1 = keys[0];
int pos;
long best = long.max;
foreach (k2; keys[1..$])
{
auto lhs = list[k1].dup;
auto rhs = list[k2];
lhs[] -= rhs[];
lhs[] *= lhs[];
long d;
foreach (e; lhs)
d += e;
if (d < best)
{
pos = k2;
best = d;
}
}
ans ~= [k1, pos];
list.remove(k1);
list.remove(pos);
}
foreach (e; ans)
writeln(e[0]+1, " ", e[1]+1);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio;
import std.algorithm;
import std.array;
import std.conv;
import std.string;
void main() {
int n = readln.chomp.to!int, ans;
bool[int] mp;
foreach(i; 0..n) {
int d = readln.chomp.to!int;
// keyが連想配列に登録されているかどうか
if ((d in mp) == null) {
ans++;
mp[d] = true;
}
}
ans.writeln;
}
|
D
|
import std.algorithm;
import std.stdio;
import std.string;
import std.conv;
void main()
{
uint[] arr;
foreach(i; 0 .. 10)
arr ~= readln().chomp().to!uint();
arr.sort!"a > b"();
foreach(i; 0 .. 3)
writeln(arr[i]);
}
|
D
|
import std.stdio,std.conv,std.string;
void main(){
auto args = readln().chomp().split();
writeln( to!int(args[0]) * to!int(args[1])," ",(to!int(args[0]) + to!int(args[1])) * 2 );
}
|
D
|
import std.stdio;
import std.string;
import std.range;
import std.conv;
void main()
{
auto ip = readln.split.to!(int[]), a = ip[0], b = ip[1], c = ip[2];
if(b-a==c-b){
writeln("YES");
}else{
writeln("NO");
}
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void main()
{
string s; readV(s);
auto n = s.length.to!int, k = (n+1)/2+1;
for (; k <= n; ++k)
if (s[k-1] != s[n-k] || s[k-1] != s[k-2]) break;
writeln(k-1);
}
|
D
|
/+ dub.sdl:
name "C"
dependency "dcomp" version=">=0.7.4"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
int main() {
Scanner sc = new Scanner(stdin);
int n;
int[] a, b, c;
sc.read(n, a, b, c);
a.sort!"a<b";
b.sort!"a<b";
c.sort!"a<b";
int l = 0, r = 0;
long ans = 0;
foreach (i; 0..n) {
while (l < n && a[l] < b[i]) l++;
while (r < n && c[r] <= b[i]) r++;
ans += long(l) * (n-r);
}
writeln(ans);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
import core.bitop : popcnt;
static if (!__traits(compiles, popcnt(ulong.max))) {
public import core.bitop : popcnt;
int popcnt(ulong v) {
return popcnt(cast(uint)(v)) + popcnt(cast(uint)(v>>32));
}
}
bool poppar(ulong v) {
v^=v>>1;
v^=v>>2;
v&=0x1111111111111111UL;
v*=0x1111111111111111UL;
return ((v>>60) & 1) != 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
// import dcomp.array;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
FastAppender!(E[]) buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/array.d */
// module dcomp.array;
T[N] fixed(T, size_t N)(T[N] a) {return a;}
struct FastAppender(A, size_t MIN = 4) {
import std.algorithm : max;
import std.conv;
import std.range.primitives : ElementEncodingType;
import core.stdc.string : memcpy;
private alias T = ElementEncodingType!A;
private T* _data;
private uint len, cap;
@property size_t length() const {return len;}
bool empty() const { return len == 0; }
void reserve(size_t nlen) {
import core.memory : GC;
if (nlen <= cap) return;
void* nx = GC.malloc(nlen * T.sizeof);
cap = nlen.to!uint;
if (len) memcpy(nx, _data, len * T.sizeof);
_data = cast(T*)(nx);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void opOpAssign(string op : "~")(T item) {
if (len == cap) {
reserve(max(MIN, cap*2));
}
_data[len++] = item;
}
void insertBack(T item) {
this ~= item;
}
void removeBack() {
len--;
}
void clear() {
len = 0;
}
ref inout(T) back() inout { assert(len); return _data[len-1]; }
ref inout(T) opIndex(size_t i) inout { return _data[i]; }
T[] data() {
return (_data) ? _data[0..len] : null;
}
}
/*
This source code generated by dcomp and include dcomp's source code.
dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp)
dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt)
*/
|
D
|
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 K, A, B;
scan(K, A, B);
long a = K + 1;
long b = (max(0, K - (A - 1)) / 2) * max(B - A, 0) + A + (max(0, K - (A - 1)) % 2);
writeln(max(a, b));
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.conv;
import std.math;
void main(){
while(true){
auto s = readln();
if(stdin.eof()) break;
s = chomp(s);
if(s=="")break;
string s1,s2;
s1 = s;
s2 = s;
while(s1.length > 1){
s2 = "";
for(int i=0;i<s1.length-1;i++){
s2 ~= to!char( '0' + (to!int(s1[i]-'0') + to!int(s1[i+1]-'0'))%10);
}
swap(s1,s2);
}
writeln(s1);
}
}
|
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);
auto t = "";
foreach (si; s) {
if (si == 'B') {
if (!t.empty) t = t[0..$-1];
} else {
t ~= si;
}
}
writeln(t);
}
|
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;
enum MAX = 1_000_100;
ulong MOD = 1_000_000_007;
ulong INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias Pair = Tuple!(long, "a", long, "b");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
auto n = lread();
string s, t;
scan(s, t);
foreach(i; iota(n))
write(s[i], t[i]);
writeln();
}
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
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 = 998244353;
void main() {
auto S = readln.chomp;
auto N = S.length.to!int;
long ans = 1;
auto dp = new long[][](2, 2*N+1); // これまで何個赤を使っているか?
dp[0][0] = 1;
int red = 0;
int blue = 0;
int cur = 0;
int tar = 1;
foreach (i; 0..2*N) {
dp[tar][] = 0;
int cand = (i + 1) * 2 - i;
if (i < N) {
red += 2 - (S[i] - '0');
blue += S[i] - '0';
}
foreach (used_red; 0..2*N+1) {
if (dp[cur][used_red] == 0) continue;
int used_blue = i - used_red;
int rest_red = red - used_red;
int rest_blue = blue - used_blue;
if (rest_red > 0) {
(dp[tar][used_red+1] += dp[cur][used_red]) %= MOD;
}
if (rest_blue > 0) {
(dp[tar][used_red] += dp[cur][used_red]) %= MOD;
}
}
swap(cur, tar);
}
dp[cur][red].writeln;
}
|
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, k;
scan(n, k);
int ans = n - k + 1;
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 core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(string, "x", long, "y");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
auto n = lread();
auto h = aryread();
long cnt, left;
while (h.sum > 0)
{
long iter = n, m = h[left];
foreach (i; iota(left, n))
{
if(h[i] == 0)
{
iter = i;
break;
}
m = min(h[i], m);
}
foreach (i; iota(iter - left))
{
// writeln(left + i);
h[i + left] -= m;
}
left = h.left_idx();
// h.writeln();
cnt += m;
}
cnt.writeln();
}
auto left_idx(T)(T ary)
{
long iter;
while (iter < ary.length && ary[iter] == 0)
iter++;
return iter;
}
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.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd = readln.split.to!(long[]), a = rd[0], b = rd[1], x = rd[2];
if (a > 0)
writeln(b/x - (a-1)/x);
else
writeln(b/x + 1);
}
|
D
|
import std.stdio;
import std.algorithm;
import std.array;
import std.conv;
import std.string;
import std.math;
void main() {
int N;
N = readln.chomp.to!int;
auto s = [0, 0, 0];
bool is_reach = true;
for (int i = 0; i < N; ++i) {
auto g = readln.chomp.split.map!(to!int).array;
int t = g[0] - s[0], dist = abs(g[1] - s[1]) + abs(g[2] - s[2]);
if (!(t >= dist && (t - dist) % 2 == 0)) {
is_reach = false;
}
s = g;
}
if (is_reach) {
writeln("Yes");
}
else {
writeln("No");
}
return;
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
void main()
{
int[3] pasta;
int[2] drink;
foreach (i; 0..3) {
pasta[i] = readln.chomp.to!int;
}
foreach (i; 0..2) {
drink[i] = readln.chomp.to!int;
}
writeln(pasta.array.sort[0]+drink.array.sort[0]-50);
}
|
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 X = RD;
auto t = RD;
writeln(max(X - t, 0));
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math;
int[10^^5+2] CNT;
void main()
{
auto N = readln.chomp.to!int;
auto as = readln.split.to!(int[]);
foreach (a; as) {
CNT[a == 0 ? 10^^5+1 : a-1]++;
CNT[a]++;
CNT[a+1]++;
}
int m = 0;
foreach (cnt; CNT) {
m = max(cnt, m);
}
writeln(m);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string;
void main()
{
writeln("A" ~ readln.split[1][0] ~ "C");
}
|
D
|
import std.stdio, std.conv, std.string, std.algorithm.comparison, std.algorithm.searching, std.algorithm.sorting;
void main()
{
int[] input = readln().split.to!(int[]);
int X = input[0];
int Y = input[1];
writeln(X + Y / 2);
}
|
D
|
import std.stdio, std.string, std.algorithm;
void main() {
string s = readln.chomp;
s ~= s;
string p = readln.chomp;
writeln(s.canFind(p) ? "Yes" : "No");
}
|
D
|
import std.stdio, std.string, std.conv;
void main()
{
auto x = readln.split.to!(int[]), n = x[0], a = x[1], b = x[2];
if(a*n < b){
writeln(a*n);
} else {
writeln(b);
}
}
|
D
|
import std.stdio;
import std.algorithm;
import std.array;
import std.conv;
import std.string;
void main() {
int n, a, b;
auto l = readln.chomp.split.map!(to!int);
n = l[0], a = l[1], b = l[2];
long ans; // longである必要はない
foreach(i; 1..(n + 1)) {
int sum, num = i;
while (num > 0) {
sum += (num % 10);
num /= 10;
}
if (a <= sum && sum <= b) {
ans += i;
}
}
ans.writeln;
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto S = readln.chomp;
auto N = S.length.to!int;
if (S.map!(s => s == S[0]).all) {
writeln(N);
return;
}
int ans = 1000000;
foreach (i; 0..N-1) if (S[i] != S[i+1]) ans = min(ans, max(i+1,N-i-1));
ans.writeln;
}
|
D
|
import std.stdio, std.conv, std.string, std.math;
void main(){
auto ip = readln.split.to!(int[]);
if(ip[0] == ip[1]){
"Draw".writeln;
} else if(ip[0] == 1){
"Alice".writeln;
} else if(ip[1] == 1){
"Bob".writeln;
} else if (ip[0] > ip[1]){
"Alice".writeln;
} else {
"Bob".writeln;
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
enum PCNT = 10^^6;
bool[PCNT+1] PS;
void prime_init()
{
PS[] = true;
PS[0] = false;
PS[1] = false;
foreach (i; 2..PCNT+1) {
if (PS[i]) {
auto x = i*2;
while (x <= PCNT) {
PS[x] = false;
x += i;
}
}
}
}
void main()
{
prime_init();
long[] ps;
foreach (i, p; PS) if (p) ps ~= i.to!long;
auto N = readln.chomp.to!int;
auto AS = new int[](10^^6+1);
foreach (a; readln.split.to!(int[])) ++AS[a];
auto F = new bool[](10^^6+1);
int r;
foreach (i, a; AS) {
if (a == 0 || F[i]) continue;
if (a == 1) ++r;
void run(size_t i) {
F[i] = true;
foreach (p; ps) {
if (i*p > 10^^6) break;
if (!F[i*p]) run(i*p);
}
}
run(i);
}
writeln(r);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
import std.range;
void main(){
auto Q=readln.split.to!(int[]),A=Q[0],B=Q[1];
if(A+B>=24)writeln((A+B)-24);
else writeln(A+B);
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=rdsp;foreach(ref j;v.tupleof)pick(r,j);}}
const mod = 10^^9+7;
alias mint = FactorRing!mod;
void main()
{
int n, m; readV(n, m);
struct C { int l, r, x; }
C[] c; readS(m, c);
C[][int] ch;
foreach (ci; c) ch[ci.r] ~= ci;
auto dp = new int[][][](n+1, n+1, n+1);
dp[0][0][0] = 1;
foreach (r; 0..n+1)
foreach (g; 0..n+1)
foreach (b; 0..n+1) {
auto k = max(r, g, b);
if (k in ch) {
foreach (ci; ch[k]) {
if (r == k) {
if ((b >= ci.l) + (g >= ci.l) + 1 != ci.x)
dp[r][g][b] = 0;
} else if (g == k) {
if ((r >= ci.l) + (b >= ci.l) + 1 != ci.x)
dp[r][g][b] = 0;
} else {
if ((r >= ci.l) + (g >= ci.l) + 1 != ci.x)
dp[r][g][b] = 0;
}
}
}
if (k < n) {
(dp[k+1][g][b] += dp[r][g][b]) %= mod;
(dp[r][k+1][b] += dp[r][g][b]) %= mod;
(dp[r][g][k+1] += dp[r][g][b]) %= mod;
}
}
auto ans = 0;
foreach (c1; 0..n)
foreach (c2; 0..n) {
(ans += dp[n][c1][c2]) %= mod;
(ans += dp[c1][n][c2]) %= mod;
(ans += dp[c1][c2][n]) %= mod;
}
writeln(ans);
}
struct FactorRing(int m, bool pos = false)
{
version(BigEndian) union { long vl; struct { int vi2; int vi; } } else union { long vl; int vi; }
alias FR = FactorRing!(m, pos);
@property static init() { return FR(0); }
@property int value() { return vi; }
@property void value(int v) { vi = mod(v); }
alias value this;
this(int v) { vi = v; }
this(int v, bool runMod) { vi = runMod ? mod(v) : v; }
this(long v) { vi = mod(v); }
ref auto opAssign(int v) { vi = v; return this; }
pure auto mod(int v) const { static if (pos) return v%m; else return (v%m+m)%m; }
pure auto mod(long v) const { static if (pos) return cast(int)(v%m); else return cast(int)((v%m+m)%m); }
static if (!pos) pure ref auto opUnary(string op: "-")() { return FR(mod(-vi)); }
static if (m < int.max / 2) {
pure ref auto opBinary(string op)(int r) if (op == "+" || op == "-") { return FR(mod(mixin("vi"~op~"r"))); }
ref auto opOpAssign(string op)(int r) if (op == "+" || op == "-") { vi = mod(mixin("vi"~op~"r")); return this; }
} else {
pure ref auto opBinary(string op)(int r) if (op == "+" || op == "-") { return FR(mod(mixin("vl"~op~"r"))); }
ref auto opOpAssign(string op)(int r) if (op == "+" || op == "-") { vi = mod(mixin("vl"~op~"r")); return this; }
}
pure ref auto opBinary(string op: "*")(int r) { return FR(mod(vl*r)); }
ref auto opOpAssign(string op: "*")(int r) { vi = mod(vl*r); return this; }
pure ref auto opBinary(string op)(ref FR r) if (op == "+" || op == "-" || op == "*") { return opBinary!op(r.vi); }
ref auto opOpAssign(string op)(ref FR r) if (op == "+" || op == "-" || op == "*") { return opOpAssign!op(r.vi); }
}
|
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.numeric;
void main()
{
auto s = readln.chomp.split.to!(int[])[1];
auto x = readln.chomp.split.to!(int[]);
auto res = abs(x[0] - s);
foreach (i; 1..x.length) {
res = gcd(res, abs(x[i] - s));
}
res.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.stdio;
long digit_sum(long b, long n) {
if (n < b)
return n;
else
return digit_sum(b, n/b) + n % b;
}
void main() {
auto N = readln.chomp.to!long;
auto S = readln.chomp.to!long;
if (N == S) {
writeln(N+1);
return;
}
for (long b = 2; b * b <= N; b++) {
if (digit_sum(b, N) == S) {
writeln(b);
return;
}
}
long ans = long.max;
for (long p = 1; p * p <= N; p++) {
if ((p-S+N) % p != 0)
continue;
long b = (p-S+N) / p;
if (b > 1 && digit_sum(b, N) == S)
ans = min(ans, b);
}
writeln(ans == long.max ? -1 : ans);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
void main()
{
auto N = readln.chomp.to!(wchar[]);
writeln(N[1] != N[2] ? "No" : N[0] == N[1] || N[2] == N[3] ? "Yes" : "No");
}
|
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 x, y;
scan(x, y);
long[] tmp;
tmp ~= x;
while (x * 2 <= y)
{
x *= 2;
tmp ~= x;
}
// writeln(tmp);
writeln(tmp.length);
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int y, b, r;
rd(y, b, r);
int mx = 0;
for (int i = 1; i <= y; i++) {
for (int j = 1; j <= b; j++) {
for (int k = 1; k <= r; k++) {
if (i + 1 == j && j + 1 == k) {
mx = max(mx, i + j + k);
}
}
}
}
writeln(mx);
}
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
|
void main() {
problem();
}
void problem() {
auto N = scan!int;
auto A = scan!long(N);
long solve() {
ulong ans;
long sumA;
foreach(a; A) sumA = (sumA + a) % MOD;
foreach(i; 0..N-1) {
sumA -= A[i];
if (sumA < 0) sumA += MOD;
auto add = (A[i] * sumA) % MOD;
ans = (ans + add) % MOD;
}
return ans;
}
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");
const MOD = 7 + 10^^9;
// -----------------------------------------------
|
D
|
import std.stdio;
import std.conv;
import std.string;
void main() {
auto n = readln.chomp.to!(int);
((n / 111 + (n % 111 != 0)) * 111).writeln;
}
|
D
|
import std.stdio,std.conv,std.algorithm,std.array;
int[] raia() { return readln().split().map!(to!int).array; } //read stdin as int[]
void main(){
auto it = raia();
int count;
for(auto i=it[0];i<=it[1];i++)
if(it[2]%i == 0) count++;
writeln(count);
}
|
D
|
// unihernandez22
// https://atcoder.jp/contests/abc157/tasks/abc157_b
// implementation
import std.stdio: writeln, writefln, readln;
import std.array: array, split;
import std.algorithm: map;
import std.string: chomp;
import std.conv: to;
void main() {
int[3][3] bingo;
foreach(byte i; 0..3) {
int[3] input = readln.split.map!(to!int).array;
bingo[i] = input;
}
int n = readln.chomp.to!int;
bool[int] won;
foreach(_; 0..n) {
int x = readln.chomp.to!int;
won[x] = true;
}
// Check rows
foreach(int[3] i; bingo) {
if (won.get(i[0], false) && won.get(i[1], false) && won.get(i[2], false)) {
writeln("Yes");
return;
}
}
// Check columns
foreach(byte i; 0..3) {
if (won.get(bingo[0][i], false) && won.get(bingo[1][i], false) && won.get(bingo[2][i], false)) {
writeln("Yes");
return;
}
}
// Check a diagonal
bool diagonal_a = true;
foreach(byte i; 0..3)
diagonal_a = diagonal_a && won.get(bingo[i][i], false);
// The other diagonal
bool diagonal_b = true;
foreach(byte i; 0..3)
diagonal_b = diagonal_b && won.get(bingo[i][2-i], false);
if (diagonal_a || diagonal_b)
writeln("Yes");
else
writeln("No");
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } bool DEBUG = 0;
void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); }
string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); }
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; }
T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
string s = scan;
if(s[2] == s[3] && s[4] == s[5]) "Yes".print;
else "No".print;
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
long X;
int cnt = 0;
void ans(long x) {
debug {
writeln((x == X) ? "OK ": "NG");
writeln(cnt);
return;
}
writeln("! ", x);
stdout.flush;
}
bool ask(long x, long y) {
debug {
cnt += 1;
return x % X >= y % X;
}
writeln("? ", x, " ", y);
stdout.flush;
return readln.chomp == "x";
}
void solve() {
if (ask(1, 2)) {
if (ask(2, 1)) {
ans(1);
} else {
ans(2);
}
return;
}
long mn = 2;
long mx = 4;
while (true) {
if (ask(mn, mx)) {
break;
} else {
mx *= 2;
mn *= 2;
}
}
while (mx - mn > 1) {
long next_mn = (mx + mn) / 2;
if (ask(next_mn, mx)) {
mn = next_mn;
} else {
mx = next_mn;
}
}
ans (mx);
}
void main() {
debug {
X = readln.chomp.to!int;
solve;
return;
}
while (true) {
auto s = readln.chomp;
if (s == "start") solve;
else break;
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
auto a = readln.split.to!(int[]);
auto ans = a.reduce!max - a.reduce!min;
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
|
//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.random;
final class InputReader {
private:
ubyte[] p, buffer;
bool eof;
bool rawRead () {
if (eof) {
return false;
}
p = stdin.rawRead (buffer);
if (p.empty) {
eof = true;
return false;
}
return true;
}
ubyte nextByte(bool check) () {
static if (check) {
if (p.empty) {
if (!rawRead ()) {
return 0;
}
}
}
auto r = p.front;
p.popFront ();
return r;
}
public:
this () {
buffer = uninitializedArray!(ubyte[])(16<<20);
}
bool seekByte (in ubyte lo) {
while (true) {
p = p.find! (c => c >= lo);
if (!p.empty) {
return false;
}
if (!rawRead ()) {
return true;
}
}
}
template next(T) if (isSigned!T) {
T next () {
if (seekByte (45)) {
return 0;
}
T res;
ubyte b = nextByte!false ();
if (b == 45) {
while (true) {
b = nextByte!true ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 - (b - 48);
}
} else {
res = b - 48;
while (true) {
b = nextByte!true ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 + (b - 48);
}
}
}
}
template next(T) if (isUnsigned!T) {
T next () {
if (seekByte (48)) {
return 0;
}
T res = nextByte!false () - 48;
while (true) {
ubyte b = nextByte!true ();
if (b < 48 || b >= 58) {
break;
}
res = res * 10 + (b - 48);
}
return res;
}
}
T[] nextA(T) (in int n) {
auto a = uninitializedArray!(T[]) (n);
foreach (i; 0 .. n) {
a[i] = next!T;
}
return a;
}
}
void main() {
auto r = new InputReader ();
const n = r.next!uint ();
const m = r.next!uint ();
const p = r.next!uint ();
auto a = r.nextA!uint (n);
auto b = r.nextA!uint (m);
int i, j;
for (i = n - 1; i >= 0 && !(a[i] % p); --i) {}
for (j = m - 1; j >= 0 && !(b[j] % p); --j) {}
writeln (i + j);
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.array;
void main() {
string S, T;
S = readln().chomp();
T = readln().chomp();
string res = S.replace("?","z");
const string ng = "UNRESTORABLE";
bool flg = false;
foreach(int i; 0..(to!(int)(S.length)-to!(int)(T.length)+1)) {
bool f = true;
foreach(int j; 0..to!(int)(T.length)) {
if(S[i+j]!=T[j] && S[i+j]!='?') {
f = false;
break;
}
}
if(f) {
flg = true;
res = min(res, (S[0..i] ~ T ~ S[(i+to!(int)(T.length))..to!int(S.length)]).replace("?","a"));
}
}
if(flg) writeln(res);
else writeln(ng);
}
|
D
|
import std.stdio, std.string, std.conv;
void main()
{
auto r = readln.split.to!(int[]), R = r[0];
auto g = readln.split.to!(int[]), G = g[0];
writeln(2*G-R);
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
const long INF = 1L << 59;
void main() {
auto N = readln.chomp.to!int;
auto dp = new long[](1 << 3);
fill(dp, INF);
dp[0] = 0;
foreach (_; 0..N) {
auto s = readln.split;
auto c = s[0].to!long;
auto S = s[1];
int mask = 0;
if (S.canFind('A')) mask |= 1;
if (S.canFind('B')) mask |= (1 << 1);
if (S.canFind('C')) mask |= (1 << 2);
foreach (nmask; 0..8) {
dp[mask | nmask] = min(dp[mask | nmask], dp[nmask] + c);
}
}
writeln( dp[7] == INF ? -1 : dp[7] );
}
|
D
|
import std.stdio;
import std.algorithm;
import std.math;
import std.conv;
import std.string;
import std.array;
int readInt(){
return readln.chomp.to!int;
}
int[] readInts(){
return readln.chomp.split.to!(int[]);
}
void main(){
int s = readInt(), n;
int[] dic;
dic ~= s;
for(n = 2; ; n++){
if(s % 2 == 0){
s /= 2;
} else {
s = 3 * s + 1;
}
if(dic.find(s).empty){
dic ~= s;
} else break;
}
writeln(n);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main(){
while(true){
auto s = readln();
if(stdin.eof()) break;
real w = to!real(chomp(s));
if(w <= 48.0) writeln("light fly");
else if(w <= 51.0) writeln("fly");
else if(w <= 54.0) writeln("bantam");
else if(w <= 57.0) writeln("feather");
else if(w <= 60.0) writeln("light");
else if(w <= 64.0) writeln("light welter");
else if(w <= 69.0) writeln("welter");
else if(w <= 75.0) writeln("light middle");
else if(w <= 81.0) writeln("middle");
else if(w <= 91.0) writeln("light heavy");
else writeln("heavy");
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main() {
int N = to!int(chomp(readln()));
int K = to!int(chomp(readln()));
int X = to!int(chomp(readln()));
int Y = to!int(chomp(readln()));
int fee;
if(N < K) {
fee = N*X;
fee.writeln;
return;
}
fee += K*X;
if(!N-K < 1) fee += (N-K)*Y;
fee.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.stdio, std.bitmanip;
immutable int MAX = 10^^3+1;
immutable long MOD = 10^^9+7;
auto modinv = new long[](MAX);
auto f_mod = new long[](MAX);
auto f_modinv = new long[](MAX);
long[][] mem;
int N, A, B, C, D;
long powmod(long a, long x, long m) {
long ret = 1;
while (x >= 1) {
if (x % 2 == 1)
ret = ret * a % m;
a = a * a % m;
x /= 2;
}
return ret;
}
long nck(int n, int k) {
if (n < k) return 0;
return f_mod[n] * f_modinv[n-k] % MOD * f_modinv[k] % MOD;
}
long dp(int g, int n) {
if (n == 0) return 1;
if (n < g) return 0;
if (g > B) return 0;
if (mem[g][n] >= 0) return mem[g][n];
mem[g][n] = dp(g+1, n);
foreach (i; C..D+1) {
if (g * i > n) break;
auto cnt =
nck(n, g*i) * f_mod[g*i] % MOD
* powmod(f_modinv[g], i, MOD) % MOD;
cnt = cnt * f_modinv[i] % MOD;
mem[g][n] = (mem[g][n] + dp(g+1, n-g*i)*cnt%MOD) % MOD;
}
return mem[g][n];
}
void main() {
modinv[0] = modinv[1] = 1;
foreach(i; 2..MAX) {
modinv[i] = modinv[MOD % i] * (MOD - MOD / i) % MOD;
}
f_mod[0] = f_mod[1] = 1;
f_modinv[0] = f_modinv[1] = 1;
foreach(i; 2..MAX) {
f_mod[i] = (i * f_mod[i-1]) % MOD;
f_modinv[i] = (modinv[i] * f_modinv[i-1]) % MOD;
}
scanf("%d %d %d %d %d", &N, &A, &B, &C, &D);
mem = new long[][](B+1, N+1);
foreach (i; 0..B+1) fill(mem[i], -1);
dp(A, N).writeln;
}
|
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();
long cnt;
foreach (i; 1 .. n + 1)
{
string s;
s = i.to!string();
if (s.length % 2 != 0)
{
cnt += 1;
}
}
writeln(cnt);
}
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.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
alias L = Tuple!(int, "level", long, "size", long, "patty");
L[51] LS;
void main()
{
LS[0] = L(0, 1, 1);
foreach (i; 0..50) {
auto l = LS[i];
LS[i+1] = L(i+1, l.size*2 + 3, l.patty*2 + 1);
}
long solve(long l, long i) {
if (i <= l) return 0;
if (l == 0) return 1;
if (i <= LS[l-1].size + 1) {
return solve(l-1, i-1);
} else if (i == LS[l-1].size + 2) {
return LS[l-1].patty + 1;
} else {
return solve(l-1, i-2-LS[l-1].size) + 1 + LS[l-1].patty;
}
}
auto nx = readln.split.to!(long[]);
auto N = nx[0];
auto X = nx[1];
writeln(solve(N, X));
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.