code
stringlengths 4
1.01M
| language
stringclasses 2
values |
---|---|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
const dx = [0, 1, 0, -1];
const dy = [1, 0, -1, 0];
int H, W;
bool[55][55] memo;
string[] s;
int bfs(int h, int w) {
if (!(0 <= h && h < H && 0 <= w && w < W)) return 0;
if (s[h][w] == '.') return 0;
if (s[h][w] == '#' && memo[h][w] == true) return 0;
memo[h][w] = true;
int ans;
for (int i = 0; i < 4; i++) {
ans += bfs(h + dy[i], w + dx[i]);
}
return ans + 1;
}
void main() {
int[] buf = readln.chomp.split.to!(int[]);
H = buf[0], W = buf[1];
for (int i = 0; i < H; i++) {
s ~= readln.chomp;
}
bool flg = true;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
flg &= (bfs(i, j) == 1 ? false : true);
}
}
writeln(flg ? "Yes" : "No");
} | D |
import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;
import std.container;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
long binarySearch(long min, long max, bool delegate(long) f){
if(!f(min)) return min - 1; if(f(max)) return max;
long mid; while(max - min > 1){ mid = min + (max - min) / 2; if(f(mid)) min = mid; else max = mid; } return min;
}
bool check(long x){
writeln("? ", x);
stdout.flush;
return (readln.chomp == "Y");
}
void main(){
long[] ms = [1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000];
long ans;
if(check(ms[10])){ // 10...0 の形である
foreach(m; ms){
if(check(m + 1)){
ans = m;
break;
}
}
}
else{
foreach(m; ms){
if(!check(m * 10)){
ans = binarySearch(m, m * 10 - 1, x => !check(x * 10)) + 1;
break;
}
}
}
writeln("! ", ans);
}
| D |
import std.stdio;
import std.algorithm;
import std.conv;
import std.string;
import std.array;
import std.math;
void main(){
auto nums = readln().chomp.split.map!(to!int);
int[] result;
result.length = nums[1];
for(int i; i < nums[0]; i++){
auto input = readln().chomp.split.map!(to!int);
foreach(a; input[1..$]){
result[a-1]++;
}
}
result.count!("a==b")(nums[0]).writeln;
} | D |
void main() {
dchar[] n = readln.chomp.to!(dchar[]);
auto g = n.group.assocArray;
writeln(g.values.any!"a >= 3" ? "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, std.array, std.container, std.range, std.bitmanip;
import std.numeric, std.math, std.bigint, std.random, core.bitop;
import std.string, std.regex, std.conv, std.stdio, std.typecons;
void main()
{
for (;;) {
auto rd = readln.split.map!(to!int);
auto n = rd[0], x = rd[1];
if (n == 0 && x == 0) break;
auto r = 0;
foreach (i; 1..n+1)
foreach (j; i+1..n+1) {
auto k = x - i - j;
if (k > j && k <= n) ++r;
}
writeln(r);
}
} | D |
import std.stdio, std.string, std.array, std.conv, std.algorithm.iteration, std.functional;
long[10^^5+1] AS;
void main()
{
readln;
auto as = readln.split.to!(long[]);
foreach (a; as) ++AS[a];
auto cnt = as.length;
size_t i = 1, j = 10^^5+1;
for (; j > i && i < 10^^5+1 && j > 0;) {
if (AS[i] > 2) {
AS[i] -= 2;
cnt -= 2;
} else if (AS[i] == 2) {
if (AS[j] < 2) {
--j;
} else if (AS[j] > 2) {
AS[j] -= 2;
cnt -= 2;
} else {
--AS[i];
--AS[j];
cnt -= 2;
}
} else {
++i;
}
}
while (AS[i] > 1) {
AS[i] -= 2;
cnt -= 2;
}
while (AS[j] > 1) {
AS[j] -= 2;
cnt -= 2;
}
writeln(cnt);
} | D |
void main() {
problem();
}
void problem() {
auto A = scan!long;
auto B = scan!long;
long solve() {
return A * B;
}
solve().writeln;
}
// ----------------------------------------------
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
// -----------------------------------------------
| D |
import std.stdio, std.string, std.conv;
import std.array, std.algorithm, std.range;
auto solve(immutable int n)
{
auto a = iota(n).map!(_=>readln().chomp()).array();
auto dp = new int[][](n+1,n+1);
foreach(i;0..n)
foreach(j;0..n)
dp[i+1][j+1]=dp[i][j+1]+dp[i+1][j]-dp[i][j]+(a[i][j]!='.'?1:0);
int m = 0;
foreach(i;0..n)
foreach(j;0..n)
{
int lo=0, hi=min(n-i,n-j)+1;
while(lo+1<hi)
{
immutable k = (lo+hi)/2;
if(0==dp[i+k][j+k]-dp[i][j+k]-dp[i+k][j]+dp[i][j])
lo=k;
else
hi=k;
}
m=max(m,lo);
}
return m;
}
void main()
{
for(int n; 0!=(n=readln().chomp().to!int()); )
writeln(solve(n));
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto s = RD!string;
long l, r;
foreach (i; 0..s.length)
{
if (s[i] == 'A')
{
l = i;
break;
}
}
foreach_reverse (i; 0..s.length)
{
if (s[i] == 'Z')
{
r = i;
break;
}
}
writeln(r-l+1);
stdout.flush();
debug readln();
} | D |
import std.stdio, std.string, std.conv, std.range, std.array, std.algorithm;
import std.uni, std.math, std.container, std.typecons, std.typetuple;
import core.bitop, std.datetime;
void main(){
int q = readln.chomp.to!int;
string x, y;
int ans;
foreach(qi ; 0 .. q){
x = readln.chomp;
y = readln.chomp;
ans = computeLCS(x, y);
writeln(ans);
}
}
int computeLCS(string x, string y){
int n = x.length.to!int;
int m = y.length.to!int;
auto dp = new int[][](n + 1, m + 1);
foreach(i ; 1 .. n + 1){
foreach(j ; 1 .. m + 1){
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
if (x[i - 1] == y[j - 1]){
dp[i][j] = dp[i - 1][j - 1] + 1;
}
}
}
return dp[n][m];
}
void readVars(T...)(auto ref T args){
auto line = readln.split;
foreach(ref arg ; args){
arg = line.front.to!(typeof(arg));
line.popFront;
}
if(!line.empty){
writeln(line);
throw new Exception("args num < input num");
}
} | D |
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
immutable long MOD = 10^^9 + 7;
long[] F;
void solve() {
auto s = readln.split.map!(to!long);
auto N = s[0];
auto X = s[1];
writeln(X * 2);
}
void main() {
auto T = readln.chomp.to!int;
while(T--) solve;
} | D |
import std.stdio, std.string, std.conv;
import std.array, std.algorithm, std.range;
void main()
{
iota(1,readln().chomp().to!long()+1).reduce!"a*b"().writeln();
} | D |
void main()
{
import std.algorithm, std.array, std.range, std.stdio, std.string, std.conv;
readln;
auto yum = cast(ubyte[])(readln.chomp);
yum.uniq.walkLength.writeln;
}
| D |
import std.stdio;
import std.string;
import std.conv;
void main(){
int n;
while((n = readln.chomp.to!int) != 0){
string input = readln.chomp;
foreach(Unused; 0..n){
input = input.operate;
}
input.writeln;
}
}
string operate(string input){
string res;
char current = input[0];
int count = 1;
foreach(c; input[1..$]){
if(current == c){
++count;
}else{
res ~= text(count, current);
current = c;
count = 1;
}
}
res ~= text(count, current);
return res;
} | D |
import std.functional,
std.algorithm,
std.container,
std.typetuple,
std.typecons,
std.bigint,
std.string,
std.traits,
std.array,
std.range,
std.stdio,
std.conv,
std.format,
std.math;
void main()
{
auto s = readln.chomp;
auto t = readln.chomp;
int a;
foreach(i; 0..s.length){
if(s == t){
a = 1;
break;
}else{
s ~= s.front;
s.popFront();
}
}
if(a == 1) writeln("Yes");
else writeln("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(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 A = RD;
auto V = RD;
auto B = RD;
auto W = RD;
auto T = RD;
auto d = abs(A-B);
auto v = V - W;
auto ans = v * T >= d;
writeln(ans ? "YES" : "NO");
stdout.flush;
debug readln;
}
| D |
import std.stdio, std.string, std.conv;
import std.array, std.algorithm, std.range;
int[][] J;
static this()
{
foreach(i;0..3) J~=[i*3,i*3+1,i*3+2];
foreach(i;0..3) J~=[i,i+3,i+6];
foreach(i;0..2) J~=[i*2,4,8-i*2];
}
char solve(char[] s)
{
foreach(c;"ox")
foreach(a;J)
if(a.map!(i=>s[i]==c).all!"a"())
return c;
return 'd';
}
void main()
{
foreach(s;stdin.byLine())
writeln(solve(s));
} | 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) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto t = RD!int;
auto ans = new long[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto m = RD!int;
auto k = RD!int;
auto cnt = n / k;
auto x = min(cnt, m);
m -= x;
ans[ti] = x - (m+k-2) / (k-1);
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
} | D |
void main()
{
int[] tmp = readln.split.to!(int[]);
int r = tmp[0], g = tmp[1], b = tmp[2], n = tmp[3];
int cnt;
int x = n / r;
foreach (i; 0 .. x+1)
{
int y = (n - r * i) / g;
foreach (j; 0 .. y+1)
{
if ((n - r * i - g * j) % b == 0)
{
++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.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 long[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto s = RD!string;
int[int] cnt;
cnt[0] = 1;
int x;
foreach (i; 0..n)
{
x += s[i] - '0';
auto y = x-i-1;
ans[ti] += cnt.get(y, 0);
cnt[y] += 1;
debug writeln(ans[ti]);
}
}
foreach (e; ans)
{
writeln(e);
}
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 get(Args...)(ref Args args)
{
import std.traits, std.meta, std.typecons;
static if (Args.length == 1) {
alias Arg = Args[0];
static if (isArray!Arg) {
args[0] = readln.split.to!Arg;
} else static if (isTuple!Arg) {
auto input = readln.split;
static foreach (i; 0..Fields!Arg.length) {
args[0][i] = input[i].to!(Fields!Arg[i]);
}
} else {
args[0] = readln.chomp.to!Arg;
}
} else {
auto input = readln.split;
assert(input.length == Args.length);
static foreach (i; 0..Args.length) {
args[i] = input[i].to!(Args[i]);
}
}
}
void get_lines(Args...)(size_t N, ref Args args)
{
import std.traits, std.range;
static foreach (i; 0..Args.length) {
static assert(isArray!(Args[i]));
args[i].length = N;
}
foreach (i; 0..N) {
static if (Args.length == 1) {
get(args[0][i]);
} else {
auto input = readln.split;
static foreach (j; 0..Args.length) {
args[j][i] = input[j].to!(ElementType!(Args[j]));
}
}
}
}
void solve()
{
int N; get(N);
int[] AS; get(AS);
foreach (i; 1..N) if (AS[i-1] <= AS[i]) {
writeln("YES");
return;
}
writeln("NO");
}
void main()
{
int T; get(T);
foreach (_; 0..T) solve();
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
//long mod = 10^^9 + 7;
long mod = 998244353;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto T = RD!int;
string[] ans;
foreach (i; 0..T)
{
auto N = RD!int;
auto s = RD!string;
bool a;
foreach (j; 0..N)
{
if (j > N - 11) break;
if (s[j] == '8')
{
a = true;
break;
}
}
ans ~= a ? "YES" : "NO";
}
foreach (e; ans)
writeln(e);
stdout.flush();
debug readln();
} | D |
import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm;
bool isPrefixOf(string x, string y){
if (x.length > y.length) return false;
bool res = true;
for(int i;i<x.length;i++){
res = res && x[i] == y[i];
}
return res;
}
void main(){
string x = readln.chomp;
string y = readln.chomp;
int res = 0;
for(int i=0; i < x.length;){
if (isPrefixOf(y,x[i..$])){
res++;
i += y.length;
} else {
i++;
}
}
writeln(res);
} | 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(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T read(T)(){ return read.to!T; }
T[] read(T)(long n){ return n.iota.map!(_ => read!T).array; }
T[][] read(T)(long m, long n){ return m.iota.map!(_ => read!T(n)).array; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
int n = read.to!int;
int m = read.to!int;
int r = read.to!int;
int a = 9999;
foreach(i; 0 .. n) a = min(a, read.to!int);
int b = -1;
foreach(j; 0 .. m) b = max(b, read.to!int);
int x;
if(a > b) x = 0;
else x = r / a;
int ans = (r - x * a) + (x * b);
ans.writeln;
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.bigint;
void main()
{
readln;
auto os = readln.chomp;
int n;
foreach (o; os) {
if (o == '+') {
++n;
} else if (o == '-' && n > 0) {
--n;
}
}
writeln(n);
} | 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 int[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto m = RD!int;
auto r = RD!int;
auto c = RD!int;
ans[ti].chmax(r-1 + c-1);
ans[ti].chmax(n-r + m-c);
ans[ti].chmax(r-1 + m-c);
ans[ti].chmax(n-r + c-1);
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
| D |
void main()
{
string s = readln.chomp;
writeln(s.canFind("AC") ? "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;
immutable mod = 4294967311L;
void main(){
long x, y;
int n, op;
scanf("%d", &n);
long[] inv = new long[1000_001];
inv[1] = 1;
foreach(i; 2..inv.length){
inv[i] = mul(inv[mod%i], mod - mod/i);
}
foreach(_; 0..n){
scanf("%d%lld", &op, &y);
if(op >= 3 && y < 0) x = -x, y = -y;
if(op == 1) x += y;
else if(op == 2) x -= y;
else if(op == 3) x *= y;
else if(op == 4) x = mul(x, inv[cast(int)y]);
x %= mod;
}
if(x < 0) x += mod;
if(x > int.max) x -= mod;
writeln(x);
}
long mul(long x, long y){
return ( ((x*(y>>16)%mod)<<16) + (x*(y&0xffff)) ) % mod;
} | D |
import std.stdio;
long div = 1_000_000_007;
void main() {
long n, m;
scanf("%ld %ld\n", &n, &m);
auto dp = new long[n+2];
dp[0] = 1;
auto check = new bool[1_0000];
foreach(_; 0..m) {
long a;
scanf("%ld\n", &a);
check[a] = true;
}
foreach(i; 0..n) {
if (check[i]) continue;
dp[i+1] = (dp[i+1] + dp[i]) % div;
dp[i+2] = (dp[i+2] + dp[i]) % div;
}
dp[n].write;
}
| D |
import std.stdio,std.conv,std.string,std.algorithm;
void main(){
for( int i;;++i ){
auto arg = readln().chomp().to!int;
if( !arg ) break;
writeln("Case ",i+1,": ",arg );
}
} | D |
import std.stdio;
import std.string;
import std.format;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.concurrency;
import std.traits;
import std.uni;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;
enum long INF = long.max/3;
enum long MOD = 10L^^9+7;
void main() {
long A, B, C, X;
scanln(A);
scanln(B);
scanln(C);
scanln(X);
long[] as = [A, B, C];
long[] bs = [500, 100, 50];
long rec(long depth, long x) {
if (depth == 3) return x == 0;
long res = 0;
foreach(i; 0..as[depth]+1) {
if (i*bs[depth] > x) continue;
res += rec(depth+1, x-i*bs[depth]);
}
return res;
}
rec(0, X).writeln;
}
// ----------------------------------------------
void times(alias fun)(long n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(long n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
// `(x+y-1)/y` will only work for positive numbers ...
T t = x / y;
if (t * y < x) t++;
return t;
}
T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
T t = x / y;
if (t * y > x) t--;
return t;
}
ref T ch(alias fun, T, S...)(ref T lhs, S rhs) {
return lhs = fun(lhs, rhs);
}
unittest {
long x = 1000;
x.ch!min(2000);
assert(x == 1000);
x.ch!min(3, 2, 1);
assert(x == 1);
x.ch!max(100).ch!min(1000); // clamp
assert(x == 100);
x.ch!max(0).ch!min(10); // clamp
assert(x == 10);
}
mixin template Constructor() {
import std.traits : FieldNameTuple;
this(Args...)(Args args) {
// static foreach(i, v; args) {
foreach(i, v; args) {
mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v;
}
}
}
void scanln(Args...)(auto ref Args args) {
import std.meta;
template getFormat(T) {
static if (isIntegral!T) {
enum getFormat = "%d";
} else static if (isFloatingPoint!T) {
enum getFormat = "%g";
} else static if (isSomeString!T || isSomeChar!T) {
enum getFormat = "%s";
} else {
static assert(false);
}
}
enum string fmt = [staticMap!(getFormat, Args)].join(" ");
string[] inputs = readln.chomp.split;
foreach(i, ref v; args) {
v = inputs[i].to!(Args[i]);
}
}
// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
return reduce!fun(tuple(seed), r);
}
}
}
}
// cumulativeFold was added in D 2.072.0
static if (__VERSION__ < 2072) {
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
}
// minElement/maxElement was added in D 2.072.0
static if (__VERSION__ < 2072) {
private template RebindableOrUnqual(T)
{
static if (is(T == class) || is(T == interface) || isDynamicArray!T || isAssociativeArray!T)
alias RebindableOrUnqual = Rebindable!T;
else
alias RebindableOrUnqual = Unqual!T;
}
private auto extremum(alias map, alias selector = "a < b", Range)(Range r)
if (isInputRange!Range && !isInfinite!Range &&
is(typeof(unaryFun!map(ElementType!(Range).init))))
in
{
assert(!r.empty, "r is an empty range");
}
body
{
alias Element = ElementType!Range;
RebindableOrUnqual!Element seed = r.front;
r.popFront();
return extremum!(map, selector)(r, seed);
}
private auto extremum(alias map, alias selector = "a < b", Range,
RangeElementType = ElementType!Range)
(Range r, RangeElementType seedElement)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void) &&
is(typeof(unaryFun!map(ElementType!(Range).init))))
{
alias mapFun = unaryFun!map;
alias selectorFun = binaryFun!selector;
alias Element = ElementType!Range;
alias CommonElement = CommonType!(Element, RangeElementType);
RebindableOrUnqual!CommonElement extremeElement = seedElement;
// if we only have one statement in the loop, it can be optimized a lot better
static if (__traits(isSame, map, a => a))
{
// direct access via a random access range is faster
static if (isRandomAccessRange!Range)
{
foreach (const i; 0 .. r.length)
{
if (selectorFun(r[i], extremeElement))
{
extremeElement = r[i];
}
}
}
else
{
while (!r.empty)
{
if (selectorFun(r.front, extremeElement))
{
extremeElement = r.front;
}
r.popFront();
}
}
}
else
{
alias MapType = Unqual!(typeof(mapFun(CommonElement.init)));
MapType extremeElementMapped = mapFun(extremeElement);
// direct access via a random access range is faster
static if (isRandomAccessRange!Range)
{
foreach (const i; 0 .. r.length)
{
MapType mapElement = mapFun(r[i]);
if (selectorFun(mapElement, extremeElementMapped))
{
extremeElement = r[i];
extremeElementMapped = mapElement;
}
}
}
else
{
while (!r.empty)
{
MapType mapElement = mapFun(r.front);
if (selectorFun(mapElement, extremeElementMapped))
{
extremeElement = r.front;
extremeElementMapped = mapElement;
}
r.popFront();
}
}
}
return extremeElement;
}
private auto extremum(alias selector = "a < b", Range)(Range r)
if (isInputRange!Range && !isInfinite!Range &&
!is(typeof(unaryFun!selector(ElementType!(Range).init))))
{
return extremum!(a => a, selector)(r);
}
// if we only have one statement in the loop it can be optimized a lot better
private auto extremum(alias selector = "a < b", Range,
RangeElementType = ElementType!Range)
(Range r, RangeElementType seedElement)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void) &&
!is(typeof(unaryFun!selector(ElementType!(Range).init))))
{
return extremum!(a => a, selector)(r, seedElement);
}
auto minElement(alias map = (a => a), Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum!map(r);
}
auto minElement(alias map = (a => a), Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!map(r, seed);
}
auto maxElement(alias map = (a => a), Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum!(map, "a > b")(r);
}
auto maxElement(alias map = (a => a), Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!(map, "a > b")(r, seed);
}
}
// popcnt with ulongs was added in D 2.071.0
static if (__VERSION__ < 2071) {
ulong popcnt(ulong x) {
x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L);
x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L);
x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL);
x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL);
x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL);
x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL);
return x;
}
}
| D |
// Your code here!
import std.stdio;
import std.range;
import std.array;
import std.algorithm;
import std.conv;
import std.string;
void main(){
auto input = readln().chomp().to!int;
if(input <= 999){
writeln("ABC");
}else{
writeln("ABD");
}
} | D |
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
import std.numeric;
void main() {
int n, m;
scan(n, m);
auto adj = new int[][](n + m, 0);
foreach (i ; 0 .. n) {
auto line = readln.split.to!(int[]);
foreach (lang ; line[1 .. $]) {
lang += n - 1;
adj[i] ~= lang;
adj[lang] ~= i;
}
}
auto visited = new bool[](n + m);
void dfs(int u) {
visited[u] = 1;
foreach (v ; adj[u]) {
if (!visited[v]) {
dfs(v);
}
}
}
dfs(0);
foreach (i ; 0 .. n) {
if (!visited[i]) {
writeln("NO");
return;
}
}
writeln("YES");
}
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.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 A = readln.split.map!(to!int).array;
int ans = 0;
int tmp = 0;
foreach (i; 0..N) {
tmp = max(tmp, A[i]);
if (i + 1 == tmp) ans += 1, tmp = 0;
}
ans.writeln;
}
| D |
import std.functional,
std.algorithm,
std.container,
std.typetuple,
std.typecons,
std.bigint,
std.string,
std.traits,
std.array,
std.range,
std.stdio,
std.conv;
bool chmin(t)(ref t a, t b) {
if (b < a) {
a = b;
return true;
} else {
return false;
}
}
void main() {
long N = readln.chomp.to!long;
long ans = N;
foreach (long i; 0..N+1) {
long count, t;
t = i;
while (t > 0) {
count += t % 6;
t /= 6;
}
t = N - i;
while (t > 0) {
count += t % 9;
t /= 9;
}
chmin(ans, count);
}
writeln(ans);
}
| D |
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
void main() {
auto H = readln.chomp.to!(ulong);
void solve() {
ulong count;
ulong monsters = 1;
while(true) {
if (H == 0) break;
if (H == 1) {
H = 0;
} else {
H /= 2;
}
count += monsters;
monsters *= 2;
}
writeln(count);
}
solve();
}
| D |
import std.stdio;
import std.range;
import std.array;
import std.algorithm;
import std.string;
import std.conv;
void main(){
auto ab = readln().chomp().split().map!(to!int);
auto a = ab[0];
auto b = ab[1];
solve(a, b).writeln();
}
int solve(int a, int b){
return max(a+b, a-b, a*b);
} | D |
import std;
void main() {
string s = read!string;
writeln(s == "ABC" ? "ARC" : "ABC");
}
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;
import std.algorithm;
import std.conv;
import std.datetime;
import std.numeric;
import std.math;
import std.string;
string my_readln() { return chomp(readln()); }
void main()
{//try{
auto tokens = split(my_readln());
auto D = to!ulong(tokens[0]);
if (D == 25)
writeln("Christmas");
else if (D == 24)
writeln("Christmas Eve");
else if (D == 23)
writeln("Christmas Eve Eve");
else if (D == 22)
writeln("Christmas Eve Eve Eve");
stdout.flush();
/*}catch (Throwable e)
{
writeln(e.toString());
}
readln();*/
} | 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 a; readV(a);
auto p = new int[][](26);
foreach (int i, ai; a) p[ai-'a'] ~= i;
int[] s;
auto c = 0, b = new bool[](26);
foreach_reverse (i, ai; a) {
if (!b[ai-'a']) {
b[ai-'a'] = true;
++c;
if (c == 26) {
s ~= i.to!int;
c = 0;
b[] = false;
}
}
}
s.reverse();
auto k = s.length.to!int;
char[] r;
auto j = -1;
foreach (i; 0..k) {
foreach (d; 0..26) {
auto di = p[d].assumeSorted.upperBound(j).front;
if (di >= s[i]) {
r ~= cast(char)('a'+d);
j = di;
break;
}
}
}
foreach (d; 0..26) {
auto di = p[d].assumeSorted.upperBound(j);
if (di.empty) {
r ~= cast(char)('a'+d);
break;
}
}
writeln(r);
}
| D |
/+ dub.sdl:
name "A"
dependency "dunkelheit" version=">=0.9.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
int main() {
Scanner sc = new Scanner(stdin);
scope(exit) assert(!sc.hasNext);
string s;
sc.read(s);
if (s.length != 26) {
int[26] d;
s.each!(c => d[c-'a']++);
foreach (i; 0..26) {
if (d[i] == 0) {
s ~= i + 'a';
writeln(s);
return 0;
}
}
assert(false);
}
int b = 26;
foreach_reverse (i; 0..25) {
if (s[i] < s[i+1]) {
b = i;
break;
}
}
if (b == 26) {
writeln("-1");
return 0;
}
char[] ss = s.dup;
int z = ss[b] - 'a';
foreach (j; z+1..26) {
if (!s[b+1..$].count(cast(char)(j + 'a'))) continue;
ss[b] = cast(char)(j + 'a');
break;
}
ss = ss[0..b+1];
writeln(ss);
return 0;
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */
// module dkh.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
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 {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(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);
}
}
void read(Args...)(auto ref Args args) {
import std.exception;
static if (args.length != 0) {
enforce(readSingle(args[0]));
read(args[1..$]);
}
}
bool hasNext() {
return succ();
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
| D |
import std.stdio;
import std.string;
void main() {
auto l = readln().chomp();
size_t c;
size_t t;
for (size_t i; i < l.length; ++i) {
if (l[i] == 'W') {
t += i - c;
++c;
}
}
t.write;
}
| D |
import std;
import core.bitop;
ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "index", long, "num");
alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less);
void main()
{
long n, m;
scan(n, m);
auto a = new string[](n);
auto b = new string[](m);
foreach (ref e; a)
e = sread();
foreach (ref e; b)
e = sread();
bool includes(long x, long y)
{
bool ret = true;
foreach (i; iota(m))
{
ret &= (a[x + i][y .. y + m] == b[i]);
}
return ret;
}
foreach (i; iota(n - m + 1))
{
foreach (j; iota(n - m + 1))
{
if (includes(i, j))
{
writeln("Yes");
return;
}
}
}
writeln("No");
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto sw = readln.split.to!(int[]);
writeln(sw[1] >= sw[0] ? "unsafe" : "safe");
} | 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
{
string s;
string t;
}
void parseInput(T)(out Input input, T adapter)
{
with (input)
{
s = adapter.readln();
t = adapter.readln();
}
}
struct Output
{
}
auto main2(Input* input)
{
int minDistance = int.max;
int i = 0;
while (i + input.t.length <= input.s.length)
{
int distance = 0;
for (int j = 0; j < input.t.length; j++)
{
if (input.s[i + j] != input.t[j])
distance++;
}
if (distance < minDistance)
minDistance = distance;
i++;
}
return minDistance;
}
alias retType = ReturnType!main2;
static if (!is(retType == void))
{
unittest { writeln("begin unittest"); }
auto _placeholder_ = ReturnType!main2.init;
unittest // example1
{
string example =
`cabacc
abc`;
if (example.empty) return;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
printResult(result);
assert(result == 1);
}
unittest // example2
{
string example =
`codeforces
atcoder`;
if (example.empty) return;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
printResult(result);
assert(result == 6);
}
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)
{
parseInput(input, new StringAdapter(example));
}
}
class StdinAdapter
{
// readf!"%s"は使えない
// https://issues.dlang.org/show_bug.cgi?id=19820
uint readf(alias format, A...)(auto ref A args) { return std.stdio.readf!format(args); }
string readln() { return std.stdio.readln().strip(); }
}
class StringAdapter
{
import std.format;
string _s;
this(string input) { _s = input; }
uint readf(alias format, A...)(auto ref A args) { return _s.formattedRead!format(args); }
string readln()
{
string ret;
auto i = _s.countUntil("\n");
if (i == -1) { ret = _s; _s = ""; }
else { ret = _s[0..i]; _s = _s[i+1..$]; }
return ret;
}
}
void printResult(T)(T result)
{
static if (isFloatingPoint!T) writefln("%f", result);
else writeln(result);
}
void main()
{
Input input = void;
parseInput(input, new StdinAdapter());
static if (is(retType == void))
main2(&input);
else
{
auto result = main2(&input);
printResult(result);
}
}
| D |
import std.stdio;
import std.conv;
import std.array;
void main()
{
auto reader = readln.split;
int A = reader[0].to!int;
int B = reader[1].to!int;
if (A == B){
writeln(A * 2);
} else {
int max = A;
if (B > A) max = B;
writeln(max + (max - 1));
}
}
| 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 long[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto s = RD!string;
char last = s[0];
int[] a;
int cnt;
foreach (i; 0..n)
{
if (s[i] == last)
++cnt;
else
{
a ~= cnt;
cnt = 1;
last = s[i];
}
}
a ~= cnt;
debug writeln(a);
long c;
foreach (e; a)
{
++c;
auto x = min(e-1, c);
c -= x;
ans[ti] += x;
}
ans[ti] += (c+1) / 2;
}
foreach (ti; 0..t)
{
writeln(ans[ti]);
}
stdout.flush;
debug readln;
}
| D |
import std.stdio;
import std.range;
import std.array;
import std.algorithm;
import std.string;
import std.conv;
void main(){
int input = readln.chomp.to!int;
writeln(input / 3);
} | D |
import std.stdio, std.conv, std.string, std.bigint;
import std.math, std.random, std.datetime;
import std.array, std.range, std.algorithm, std.container, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
void main(){
long n = read.to!long;
string a = readln.chomp;
string b = readln.chomp;
string c = readln.chomp;
long ans;
foreach(i; 0 .. n){
char x = a[i], y = b[i], z = c[i];
if(x == y && y == z) ans += 0;
else if(x == y || y == z || z == x) ans += 1;
else ans += 2;
}
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.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.15f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto s = RD!string;
long cnt;
foreach (c; s)
{
if (c == 'p')
++cnt;
}
auto ans = s.length / 2 - cnt;
writeln(ans);
stdout.flush();
debug readln();
} | 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 W, H, N;
scan(W, H, N);
int l = 0, r = W;
int d = 0, u = H;
foreach (i ; 0 .. N) {
int xi, yi, ai;
scan(xi, yi, ai);
if (ai == 1) {
chmax(l, xi);
}
else if (ai == 2) {
chmin(r, xi);
}
else if (ai == 3) {
chmax(d, yi);
}
else {
chmin(u, yi);
}
debug {
writefln("%s, %s", l, r);
writefln("%s, %s", d, u);
}
}
auto ans = max(0, r - l) * max(0, u - d);
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.algorithm;
import std.range;
import std.conv;
import std.string;
int f(int a, int k) {
if (a < k) return 0;
if (a%k == 0) return a/k;
int c = a/k+1;
int l = a/k*k;
a -= ((a-l-1)/c+1)*c;
return f(a, k);
}
void main() {
int n = to!int(chomp(readln()));
//int m = 100;
//int[] dp = new int[m+1];
//foreach (i; 0..m+1) {
// int[] s = new int[m+1];
// foreach (j; 1..i/n+1) {
// s[dp[i-j]] = 1;
// }
// foreach (j; 0..m) {
// if (s[j] == 1) continue;
// dp[i] = j;
// break;
// }
// writefln("%2d : %2d - %2d", i, dp[i], f(i,n));
//}
//return;
int x = 0;
foreach (i; 0..n) {
string[] input = split(readln());
int a = to!int(input[0]);
int k = to!int(input[1]);
x ^= f(a,k);
}
writeln(x ? "Takahashi" : "Aoki");
} | D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.math;
immutable limitList = 100000;
void main() {
int input;
bool[] listNumbers = new bool[](limitList);
int[] listPrimeNumbers; //List of prime numbers.
listNumbers.fill(true);
listNumbers[0..1] = false;
foreach (i; 2..limitList.to!double.sqrt.to!int) {
if (listNumbers[i]) {
for (int j = i*2; j < limitList; j += i) listNumbers[j] = false;
}
}
while ((input = readln.chomp.to!int) != 0) {
uint count = 0;
foreach (i; 2..(input/2)+1) {
if(listNumbers[i] && listNumbers[input-i]) count++;
}
writeln(count);
}
} | D |
import std.algorithm;
import std.conv;
import std.stdio;
import std.string;
void main()
{
auto abcxy = readln.split.map!( to!int );
writeln( solve( abcxy[ 0 ], abcxy[ 1 ], abcxy[ 2 ], abcxy[ 3 ], abcxy[ 4 ] ) );
}
int solve( in int a, in int b, in int c, in int x, in int y )
{
auto cc = c * 2;
if( a + b < cc ) return ( a * x ) + ( b * y );
if( x < y )
{
if( b < cc ) return ( cc * x ) + ( b * ( y - x ) );
else return ( cc * x ) + ( cc * ( y - x ) );
}
else
{
if( a < cc ) return ( cc * y ) + ( a * ( x - y ) );
else return ( cc * y ) + ( cc * ( x - y ) );
}
}
unittest
{
assert( solve( 1500, 2000, 1600, 3, 2 ) == 7900 );
assert( solve( 1500, 2000, 1900, 3, 2 ) == 8500 );
assert( solve( 1500, 2000, 500, 90000, 100000 ) == 100000000 );
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nab = readln.split.to!(long[]);
auto N = nab[0];
auto A = nab[1];
auto B = nab[2];
if (N <= A) {
writeln(N);
} else {
auto c = N / (A+B);
writeln(A * c + min(A, N % (A+B)));
}
} | 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!(long, "l ", long, "r");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
auto s = sread();
long cnt;
foreach (i; iota(s.length - 1))
{
if(s[i] != s[i + 1])
cnt++;
}
cnt.writeln();
}
bool is_reversable(string s)
{
bool ret = true;
foreach (i; iota(s.length / 2))
{
ret &= (s[i] == s[$ - 1 - i]);
}
return ret;
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
| D |
/+ dub.sdl:
name "A"
dependency "dunkelheit" version=">=0.7.4"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
Scanner sc;
static this() {
sc = new Scanner(stdin);
}
int query(int i) {
writeln(i);
stdout.flush();
string s;
sc.read(s);
if (s == "Vacant") return 0;
if (s == "Male") return -1;
if (s == "Female") return 1;
assert(false);
}
int main() {
int n;
sc.read(n);
int u = query(0);
if (u == 0) return 0;
int lc = u;
int l = 1, r = n;
while (true) {
int md = (l+r)/2;
int w = query(md);
if (w == 0) return 0;
bool od1 = (md-l) % 2 != 0;
bool od2 = (lc != w);
// writeln(od1, " ", od2);
if (od1 == od2) {
r = md;
} else {
l = md+1;
lc = w;
}
}
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */
// module dkh.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
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 {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(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);
}
}
void read(bool enforceEOF = false, T, Args...)(ref T x, auto ref Args args) {
import std.exception;
enforce(readSingle(x));
static if (args.length == 0) {
enforce(enforceEOF == false || !succ());
} else {
read!enforceEOF(args);
}
}
void read(bool enforceEOF = false, Args...)(auto ref Args args) {
import std.exception;
static if (args.length == 0) {
enforce(enforceEOF == false || !succ());
} else {
enforce(readSingle(args[0]));
read!enforceEOF(args);
}
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
| D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
void main() {
int a, b;
scan(a, b);
if (a >= 9 || b >= 9) {
writeln(":(");
}
else {
writeln("Yay!");
}
}
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.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);}
const e = ["dream", "dreamer", "erase", "eraser"];
void main()
{
string s; readV(s);
loop: while (!s.empty) {
foreach (ei; e) {
if (s.endsWith(ei)) {
s = s[0..$-ei.length];
continue loop;
}
}
writeln("NO");
return;
}
writeln("YES");
}
| D |
import std.stdio, std.conv, std.string, std.bigint;
import std.math, std.random, std.datetime;
import std.array, std.range, std.algorithm, std.container, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
void main(){
long n = read.to!long;
long a = read.to!long;
long b = read.to!long;
long x, y;
if(a + b < n) x = 0; else x = (a + b) - n;
y = min(a, b);
writeln(y, " ", x);
}
| D |
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
void main ()
{
string s;
while ((s = readln.strip) != "")
{
auto n = s.length.to !(int);
int res = 0;
foreach (i; 0..n)
{
foreach (j; i + 1..n + 1)
{
auto t = s[i..j];
if (!equal (t, t.retro))
{
res = max (res, t.length.to !(int));
}
}
}
writeln (res);
}
}
| D |
import std.stdio, std.conv, std.string, std.bigint;
import std.math, std.random, std.datetime;
import std.array, std.range, std.algorithm, std.container, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
void main(){
int n = read.to!int;
int[] ls = readln.chomp.split.map!(to!int).array;
int sum;
foreach(l; ls) sum += l;
string ans = "Yes";
foreach(l; ls) if(sum - l <= l) ans = "No";
ans.writeln;
}
| 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!(long, "l ", long, "r");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
auto n = lread();
writeln(n * (n - 1) / 2);
}
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 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 = lread();
long ans = N - 1;
foreach (x; 1 .. N + 1)
{
if (N < x * x)
break;
if (N % x != 0)
continue;
ans = ans.min(x - 1 + (N / x) - 1);
}
writeln(ans);
}
| D |
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
alias sread = () => readln.chomp();
void main()
{
auto s = sread();
if (s == "1")
{
writeln("Hello World");
}
else
{
writeln(lread + lread);
}
}
| D |
import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;
import std.numeric;
import std.bigint;
import core.checkedint;
import core.bitop;
int f(int x)
{
int ans = 0;
while (x) {
ans += x % 10;
x /= 10;
}
return ans;
}
void main()
{
int n = readln.chomp.to!int;
int ans = int.max;
for (int a = 1; a <= n - 1; ++a) {
ans = min(ans, a.f + f(n - a));
}
ans.writeln;
}
void tie(R, Args...)(R arr, ref Args args)
if (isRandomAccessRange!R || isArray!R)
in
{
assert (arr.length == args.length);
}
body
{
foreach (i, ref v; args) {
alias T = typeof(v);
v = arr[i].to!T;
}
}
void verbose(Args...)(in Args args)
{
stderr.write("[");
foreach (i, ref v; args) {
if (i) stderr.write(", ");
stderr.write(v);
}
stderr.writeln("]");
}
| D |
import std.stdio;
void main(){
auto cin = new Cin();
auto N = cin.line()[0];
((N+N*N)/2).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 |
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
import std.typecons;
int n, m; rd(n, m);
auto l=new int[](m), r=new int[](m), d=new int[](m);
foreach(i; 0..m){
rd(l[i], r[i], d[i]);
l[i]--; r[i]--;
}
alias T=Tuple!(int, "to", long, "d");
auto g=new T[][](n), rev=new T[][](n);
foreach(i; 0..m){
g[l[i]]~=T(r[i], d[i]);
rev[r[i]]~=T(l[i], d[i]);
}
auto a=new long[](n), vis=new bool[](n);
long dmin=0, dmax=0;
bool fun(int cur, int pre=-1, long p=0){
// writeln(cur);
if(vis[cur]){
if(a[cur]!=p) return false;
else return true;
}else{
vis[cur]=true;
a[cur]=p;
dmin=min(dmin, p);
dmax=max(dmax, p);
}
bool ret=true;
foreach(e; g[cur]){
if(e.to==pre) continue;
ret&=fun(e.to, cur, p+e.d);
}
foreach(e; rev[cur]){
if(e.to==pre) continue;
ret&=fun(e.to, cur, p-e.d);
}
return ret;
}
foreach(i; 0..n)if(vis[i]==false){
// writeln(i);
dmin=dmax=0;
if(fun(i)==false){writeln("No"); return;}
if(dmax-dmin>1_000_000_000){writeln("No"); return;}
}
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));
}
}
void wr(T...)(T x){
import std.stdio;
foreach(e; x) write(e, " ");
writeln();
} | D |
// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
int a, b, c, d;
scan(a, b, c, d);
auto on = new int[](200);
foreach (i ; a .. b) {
on[i]++;
}
foreach (i ; c .. d) {
on[i]++;
}
writeln(on.count!"a == 2");
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
} | D |
import std.algorithm, std.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()
{
long n, m; readV(n, m);
auto x = min(n, m/2);
auto y = (m-x*2)/4;
writeln(x+y);
}
| D |
import std.stdio;
import std.conv;
import std.string;
import std.algorithm;
import std.range;
import std.functional;
import std.math;
import core.bitop;
void main()
{
auto i = readln.chomp.to!long();
if (i < 1200)
{
"ABC".writeln;
}
else if (i < 2800)
{
"ARC".writeln;
}
else
{
"AGC".writeln;
}
}
| D |
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto x = readln.chomp.to!long;
struct T { long t, y; }
auto t = iota(0, x+1)
.map!(t => T(t, t*(t+1)/2))
.assumeSorted!"a.y < b.y"
.lowerBound(T(0, x)).back.t;
writeln(t+1);
}
| D |
import std.stdio;
import std.string;
import std.range;
import std.conv;
void main()
{
auto S = readln.chomp;
switch(S) {
case "abc", "acb", "bac", "bca", "cab", "cba":
"Yes".writeln;
break;
default:
"No".writeln;
break;
}
} | D |
#!/usr/bin/rdmd
import std.stdio: stdin, lines, writeln;
import std.ascii: newline;
import std.conv: to;
import std.array: split;
void main()
{
int[2] a;
foreach (string line; lines(stdin))
{
if(line == newline) break;
else
{
a = to!(int[])(split(line));
writeln((a[0]+a[1]));
}
}
} | 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 M = RD;
writeln(48-M);
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;
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()
{
long a, b;
scan(a, b);
auto t = gcd(a, b);
writeln(a * b / t);
}
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.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
void main() {
int a, b, c, d;
scan(a, b, c, d);
writeln(abs(a-c) <= d || max(abs(a-b), abs(b-c)) <= d ? "Yes" : "No");
}
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.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
writeln(readln.chomp.canFind('9') ? "Yes" : "No");
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
| D |
import core.bitop, std.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!(long, "l ", long, "r");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
auto s = sread();
// s[0 .. (s.length - 1) / 2].writeln();
// s[(s.length + 3) / 2 - 1 .. $].writeln();
if (s.is_reversable() && s[0 .. (s.length - 1) / 2].is_reversable()
&& s[(s.length + 3) / 2 - 1 .. $].is_reversable())
writeln("Yes");
else
writeln("No");
}
bool is_reversable(string s)
{
bool ret = true;
foreach (i; iota(s.length / 2))
{
ret &= (s[i] == s[$ - 1 - i]);
}
return ret;
}
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.string, std.conv, std.range;
void main() {
auto N = readln.chomp.to!int;
ulong power = 1;
foreach (i; iota(1, N + 1)) {
power *= i;
power %= 1000000007;
}
power.writeln;
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto a = RD;
auto b = RD;
auto h = RD;
writeln(min(a, b) * h + (max(a, b) - min(a, b)) * h / 2);
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; }
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;
long ans = 1;
long best;
foreach (i; 1..N+1)
{
auto x = i;
long cnt;
while (x % 2 == 0)
{
x /= 2;
++cnt;
}
if (cnt > best)
{
best = cnt;
ans = i;
}
}
writeln(ans);
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;
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[] a;
foreach (_; 0..5) a ~= readint;
int k = readint;
// 両端の差が k 以下か調べるだけでよい
bool ans = a[$-1] - a[0] <= k;
writeln(ans ? "Yay!" : ":(");
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nd = readln.split.to!(int[]);
auto N = nd[0];
auto D = nd[1];
auto d = D*2+1;
writeln((N+d-1) / d);
} | D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
string s = readln.chomp;
writeln("2018" ~ s[4 .. $]);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
| D |
import std.stdio;
import std.algorithm;
import std.string;
import std.range;
import std.array;
import std.conv;
void main() {
int [] dp = new int[101];
dp[] = 0;
long n;
while(!stdin.eof()) {
auto s = readln().replace(",", " ").split();
if(!s.empty()){
int [] newdp;
int offset;
if(n < s.length) {
offset = 1;
} else {
offset = 2;
}
n = s.length;
newdp ~= 0;
for(int i = 0; i < s.length; ++i) {
newdp ~= max(dp[i+offset], dp[i-1+offset])+to!int(s[i]);
}
newdp ~= 0;
dp = newdp;
}
}
writeln(dp[1]);
} | D |
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
while (1) {
int n;
scan(n);
if (!n) return;
int ans = 10^^7;
for (int z; z^^3 <= n; z++) {
for (int y; y^^2 <= n - z^^3; y++) {
int x = n - z^^3 - y^^2;
ans = min(ans, x + 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 |
import std.algorithm;
import std.conv;
import std.stdio;
import std.string;
void main()
{
auto abck = readln.split.map!( to!long );
writeln( solve( abck[ 0 ], abck[ 1 ], abck[ 2 ], abck[ 3 ] ) );
}
long solve( in long a, in long b, in long c, in long k )
{
if( k % 2 == 0 ) return a - b;
else return b - a;
}
unittest
{
assert( solve( 1, 2, 3, 1 ) == 1 );
assert( solve( 2, 3, 2, 0 ) == -1 );
assert( solve( 1000000000, 1000000000, 1000000000, 1000000000000000000 ) == 0 );
}
| D |
/+ dub.sdl:
name "E"
dependency "dunkelheit" version="1.0.1"
+/
import std.stdio, std.algorithm, std.range, std.conv, std.string;
// import dkh.foundation, dkh.scanner, dkh.container.deque;
int main() {
Scanner sc = new Scanner(stdin);
scope(exit) assert(!sc.hasNext);
string s;
sc.read(s);
string l, r;
while (s.length) {
if (s.length == 1) {
l ~= s;
break;
}
if (s.front == s.back) {
l ~= s.front;
r ~= s.back;
s = s[1..$-1];
continue;
}
if (s[1] == s.back) {
s = s[1..$];
continue;
}
if (s.front == s[$-1]) {
s = s[0..$-1];
continue;
}
s = s[1..$-1];
continue;
}
writeln(l ~ r.dup.reverse);
return 0;
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/foundation.d */
// module dkh.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/container/deque.d */
// module dkh.container.deque;
struct DequePayload(T) {
import core.exception : RangeError;
private T* _data;
private uint start, len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
if (start + i < cap) return _data[start + i];
else return _data[start + i - cap];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import std.algorithm : max;
import std.conv : to;
if (newCap <= cap) return;
T* newData = cast(T*)GC.malloc(newCap * T.sizeof);
foreach (i; 0..length) {
newData[i] = this[i];
}
_data = newData; start = 0; cap = newCap.to!uint;
}
void clear() {
start = len = 0;
}
import std.algorithm : max;
void insertFront(T item) {
if (len == cap) reserve(max(cap * 2, 4));
if (start == 0) start += cap;
start--; len++;
this[0] = item;
}
void insertBack(T item) {
if (len == cap) reserve(max(cap * 2, 4));
len++;
this[len-1] = item;
}
void removeFront() {
assert(!empty, "Deque.removeFront: Deque is empty");
start++; len--;
if (start == cap) start = 0;
}
void removeBack() {
assert(!empty, "Deque.removeBack: Deque is empty");
len--;
}
}
struct Deque(T, bool mayNull = true) {
import core.exception : RangeError;
import std.range : ElementType, isInputRange;
import std.traits : isImplicitlyConvertible;
alias Payload = DequePayload!T;
Payload* _p;
static if (!mayNull) @disable this();
this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) {
_p = new Payload();
foreach (v; values) {
insertBack(v);
}
}
this(Range)(Range r)
if (isInputRange!Range &&
isImplicitlyConvertible!(ElementType!Range, T) &&
!is(Range == T[])) {
_p = new Payload();
foreach (v; r) {
insertBack(v);
}
}
private this(Payload* p) { _p = p; }
static Deque make() { return Deque(new Payload()); }
private bool havePayload() const { return (!mayNull || _p); }
@property bool empty() const { return (!havePayload || _p.empty); }
@property size_t length() const { return (havePayload ? _p.length : 0); }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {
assert(!empty, "Deque.opIndex: Deque is empty");
return (*_p)[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void clear() { if (_p) _p.clear(); }
void insertFront(T item) {
if (mayNull && !_p) _p = new Payload();
_p.insertFront(item);
}
void insertBack(T item) {
if (mayNull && !_p) _p = new Payload();
_p.insertBack(item);
}
alias opOpAssign(string op : "~") = insertBack;
alias stableInsertBack = insertBack;
void removeFront() {
assert(!mayNull || _p, "Deque.removeFront: Deque is empty");
_p.removeFront();
}
void removeBack() {
assert(!mayNull || _p, "Deque.removeBack: Deque is empty");
_p.removeBack();
}
alias stableRemoveBack = removeBack;
alias Range = RangeT!(DequePayload!T);
alias ConstRange = RangeT!(const DequePayload!T);
alias ImmutableRange = RangeT!(immutable DequePayload!T);
size_t[2] opSlice(size_t dim : 0)(size_t start, size_t end) const {
assert(start <= end && end <= length);
return [start, end];
}
Range opIndex(size_t[2] rng) { return Range(_p, rng[0], rng[1]); }
ConstRange opIndex(size_t[2] rng) const { return ConstRange(_p, rng[0], rng[1]); }
ImmutableRange opIndex(size_t[2] rng) immutable { return ImmutableRange(_p, rng[0], rng[1]); }
auto opIndex() inout { return this[0..$]; }
static struct RangeT(QualifiedPayload) {
alias A = QualifiedPayload;
import std.traits : CopyTypeQualifiers;
alias E = CopyTypeQualifiers!(A, T);
A *p;
size_t l, r;
@property bool empty() const { return r <= l; }
@property size_t length() const { return r - l; }
alias opDollar = length;
@property auto save() { return this; }
ref inout(E) opIndex(size_t i) inout {
version(assert) if (empty) throw new RangeError();
return (*p)[l+i];
}
@property ref inout(E) front() inout { return this[0]; }
@property ref inout(E) back() inout { return this[$-1]; }
void popFront() {
version(assert) if (empty) throw new RangeError();
l++;
}
void popBack() {
version(assert) if (empty) throw new RangeError();
r--;
}
size_t[2] opSlice(size_t dim : 0)(size_t start, size_t end) const {
assert(start <= end && end <= length);
return [start, end];
}
auto opIndex(size_t[2] rng) { return RangeT(p, l+rng[0], l+rng[1]); }
auto opIndex(size_t[2] rng) const { return RangeT!(const A)(p, l+rng[0], l+rng[1]); }
auto opIndex(size_t[2] rng) immutable { return RangeT!(immutable A)(p, l+rng[0], l+rng[1]); }
auto opIndex() inout { return this[0..$]; }
}
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
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;
private File f;
this(File f) {
this.f = f;
}
private char[512] lineBuf;
private 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) {
assert(succW());
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(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);
}
}
void read(Args...)(auto ref Args args) {
import std.exception : enforce;
static if (args.length != 0) {
enforce(readSingle(args[0]));
read(args[1..$]);
}
}
bool hasNext() {
return succ();
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
| D |
// import chie template :) {{{
static if (__VERSION__ < 2090) {
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons, std.format,
std.bitmanip, std.numeric;
} else {
import std;
}
// }}}
// nep.scanner {{{
class Scanner {
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) {
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next() {
if (idx < str.length) {
return str[idx++];
}
char[] s;
while (s.length == 0) {
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(size_t len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
void scan(T...)(ref T args) {
foreach (ref arg; args) {
arg = next!(typeof(arg));
}
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType)) {
str ~= s.to!(char[]).strip.split;
}
}
// }}}
// alias {{{
alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less);
alias MinHeap(T) = Heap!(T, "a > b");
// }}}
// memo {{{
/*
- ある値が見つかるかどうか
<https://dlang.org/phobos/std_algorithm_searching.html#canFind>
canFind(r, value); -> bool
- 条件に一致するやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#filter>
// 2で割り切れるやつ
filter!"a % 2 == 0"(r); -> Range
- 合計
<https://dlang.org/phobos/std_algorithm_iteration.html#sum>
sum(r);
- 累積和
<https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold>
// 今の要素に前の要素を足すタイプの一般的な累積和
// 累積和のrangeが帰ってくる(破壊的変更は行われない)
cumulativeFold!"a + b"(r, 0); -> Range
- rangeをarrayにしたいとき
array(r); -> Array
- 各要素に同じ処理をする
<https://dlang.org/phobos/std_algorithm_iteration.html#map>
// 各要素を2乗
map!"a * a"(r) -> Range
- ユニークなやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#uniq>
uniq(r) -> Range
- 順列を列挙する
<https://dlang.org/phobos/std_algorithm_iteration.html#permutations>
permutation(r) -> Range
- ある値で埋める
<https://dlang.org/phobos/std_algorithm_mutation.html#fill>
fill(r, val); -> void
- バイナリヒープ
<https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap>
// 昇順にするならこう(デフォは降順)
BinaryHeap!(Array!T, "a > b") heap;
heap.insert(val);
heap.front;
heap.removeFront();
- 浮動小数点の少数部の桁数設定
// 12桁
writefln("%.12f", val);
- 浮動小数点の誤差を考慮した比較
<https://dlang.org/phobos/std_math.html#.approxEqual>
approxEqual(1.0, 1.0099); -> true
- 小数点切り上げ
<https://dlang.org/phobos/std_math.html#.ceil>
ceil(123.4); -> 124
- 小数点切り捨て
<https://dlang.org/phobos/std_math.html#.floor>
floor(123.4) -> 123
- 小数点四捨五入
<https://dlang.org/phobos/std_math.html#.round>
round(4.5) -> 5
round(5.4) -> 5
*/
// }}}
void main() {
auto cin = new Scanner;
string s;
cin.scan(s);
int c = s[$ - 1] - '0';
if (c == 2 || c == 4 || c == 5 || c == 7 || c == 9) {
writeln("hon");
} else if (c == 0 || c == 1 || c == 6 || c == 8) {
writeln("pon");
} else {
writeln("bon");
}
} | D |
void main()
{
long n = rdElem;
long f(long x)
{
return x * (x + 1) / 2;
}
long l, r = n;
while (r - l > 1)
{
long m = (l + r) / 2;
long p = f(m);
if (p < n) l = m;
else r = m;
}
long total = f(r);
long diff = total - n;
foreach (i; 1 .. r+1)
{
if (i != diff) i.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 wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni; | D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.math;
void main() {
string input = readln();
if(input[0] == input[2]) "Yes".writeln;
else "No".writeln;
}
| D |
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
enum MOD = (10 ^^ 9) + 7;
void main()
{
long A, B, C;
scan(A, B, C);
solve(A, B, C).writeln();
}
long solve(long A, long B, long C)
{
if (C <= (A + B))
{
return B + C;
}
else if (C - 1 == A + B)
{
return B + C;
}
else
{
return (A + B) * 2 - A + 1;
}
}
| D |
import std.functional,
std.algorithm,
std.bigint,
std.string,
std.traits,
std.array,
std.range,
std.stdio,
std.conv;
void main() {
int[] ABCD = readln.chomp.split.to!(int[]);
int A = ABCD[0],
B = ABCD[1],
C = ABCD[2],
D = ABCD[3];
int L = A + B,
R = C + D;
if (L > R) {
writeln("Left");
} else if (L == R) {
writeln("Balanced");
} else {
writeln("Right");
}
}
| 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 o; readV(o);
string e; readV(e);
foreach (i; 0..e.length)
write(o[i], e[i]);
if (o.length > e.length)
write(o[$-1]);
writeln;
}
| D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void inputLine(A...)(ref A a) {
string[] line = readln.chomp.split;
if (a.length != line.length) {
throw new Exception("not equal number of input elements and number of variable elements.");
}
foreach (int i, ref e; a) {
e = line[i].to!(A[i]);
// e = line[i].to!(typeof(e));
}
}
bool[100010] p;
void calcPrime() {
p[] = true;
p[0] = p[1] = false;
for (int i = 4; i < 100010; i++) {
for (int j = 2; j * j <= i; j++) {
if (p[j]) {
p[i] &= i % j ? true : false;
}
}
}
}
void main() {
int Q = readln.chomp.to!(int);
calcPrime();
int[100010] hoge;
for (int i = 2; i < 100010; i++) {
hoge[i] = hoge[i - 1] + (p[i] & p[(i + 1) / 2]);
}
for (int i = 0; i < Q; i++) {
int l, r;
inputLine(l, r);
writeln(hoge[r] - hoge[l - 1]);
}
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto lr = readln.split.to!(long[]);
auto L = lr[0];
auto R = min(L+2020, lr[1]+1);
long x = long.max;
foreach (i; L..R) {
foreach (j; i+1..R) {
x = min(x, i*j%2019);
}
}
writeln(x);
} | D |
import std.stdio;
import std.range;
import std.array;
import std.algorithm;
import std.string;
import std.conv;
void main(){
immutable train_01 = readln.chomp.to!int;
immutable train_02 = readln.chomp.to!int;
immutable bus_01 = readln.chomp.to!int;
immutable bus_02 = readln.chomp.to!int;
immutable answer = min(train_01, train_02) + min(bus_01, bus_02);
writeln(answer);
} | D |
import std.stdio,
std.string,
std.conv;
void main() {
int N = readln.chomp.to!(int);
string[] S;
bool[] tate;
for (int i = 0; i < 2; i++) {
S ~= readln.chomp;
}
for (int i = 0; i < N; i++) {
if (S[0][i] == S[1][i]) {
tate ~= true;
}
else {
tate ~= false;
i++;
}
}
long ans = (tate[0] ? 3 : 6);
for (int i = 1; i < tate.length; i++) {
long kake;
if (tate[i]) {
kake = (tate[i - 1] ? 2 : 1);
}
else {
kake = (tate[i - 1] ? 2 : 3);
}
ans = (ans * kake) % 1000000007;
}
ans.writeln;
}
| D |
import std.stdio;
import std.conv;
import std.string;
void main()
{
string s = chomp(readln());
long sec = to!long(s);
string hh = to!string(sec / 3600);
string mm = to!string((sec % 3600) / 60);
string ss = to!string(sec % 3600 % 60);
write(hh);
write(":");
write(mm);
write(":");
writeln(ss);
} | D |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.