code
stringlengths 4
1.01M
| language
stringclasses 2
values |
---|---|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
readln;
long r;
foreach (a; readln.split.to!(long[])) r += a-1;
writeln(r);
} | D |
import std.stdio;
import std.conv;
import std.string;
void main()
{
int[] input = readln.split.to!(int[]);
int k = input[0];
int s = input[1];
long ans = 0;
foreach_reverse (i; 0..k + 1) {
foreach_reverse (j; 0..k + 1) {
auto tmp = s - i - j;
if (tmp <= k && tmp >= 0){
ans++;
}
}
}
writeln(ans);
}
| 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 A = RD;
auto B = RD;
auto C = RD;
auto D = RD;
long ans;
foreach (z; C..D+1)
{
auto len = A + B;
auto d = len - z - 1;
auto tmp = (B-A+1)*(C-B+1);
debug writeln("tmp:", tmp);
if (d < 0)
{
d = abs(d);
auto len2 = A+C;
auto d2 = len2 - z - 1;
if (d2 < 0)
{
d2 = abs(d2);
auto h = min(B-A+1, d2-1);
tmp -= (C-B+1) * h;
debug writeln("tmp:", tmp);
d = (C-B+1);
tmp -= d * (d+1) / 2;
auto BA = B-A+1 - h;
auto d3 = max(0, d-BA);
tmp += d3 * (d3+1) / 2;
}
else
{
tmp -= d * (d+1) / 2;
auto BA = B-A+1;
auto d3 = max(0, d-BA);
tmp += d3 * (d3+1) / 2;
}
debug writeln("tmp:", tmp);
}
ans += max(0, tmp);
debug writeln("z:", z, " ans:", tmp);
}
writeln(ans);
stdout.flush;
debug readln;
} | D |
// import chie template :) {{{
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons;
// }}}
// nep.scanner {{{
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string : chomp;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.chomp.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
}
// }}}
void main()
{
auto cin = new Scanner;
int n;
cin.scan(n);
int sum;
foreach (i; 0 .. n)
{
sum += abs(cin.next!int - cin.next!int) + 1;
}
writeln(sum);
}
| D |
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
import std.range;
auto s=readln.chomp.to!(char[]);
int[char] cnt;
foreach(c; s){
if(!(c in cnt)) cnt[c]=1;
}
auto mi=s.length;
foreach(tg; cnt.keys){
int i=-1, j;
int r=0;
while(j<s.length){
if(s[j]==tg){
r=max(r, j-i-1);
i=j;
}
j++;
}
mi=min(mi, max(r, s.length-i-1));
}
writeln(mi);
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
int n;
foreach (c; readln.chomp) if (c == '2') ++n;
writeln(n);
} | D |
import std.stdio, std.array, std.conv, std.string;
void main() {
while (true) {
string[] input = split(readln());
if (input[1] == "?") break;
writeln(calc(input));
}
}
int calc(string[] s) {
int a = to!int(s[0]), b = to!int(s[2]);
string op = s[1];
if (op == "+") return a + b;
if (op == "-") return a - b;
if (op == "*") return a * b;
if (op == "/") return a / b;
return -1;
} | D |
void main()
{
auto io = new IO();
auto N = io.line!size_t()[0];
auto input = io.line!size_t();
size_t pair = 0;
foreach( i,v ; input ){
if( i<v-1 && i==input[v-1]-1 ){
pair++;
}
}
writeln(pair);
}
import std.stdio,std.string,std.conv;
class IO
{
string str( size_t lines = 1 )
{
return readln().chomp();
}
T[] line( T = real , string sp = " " )( size_t lines = 1 )
{
T[] ret;
foreach( i ; 0..lines )
ret ~= readln().chomp().split(sp).convert!T();
return ret;
}
T[][] rect( T = real , string sp = " " )( size_t lines = 1 )
{
T[][] ret = new T[][](lines);
foreach( i ; 0..lines )
ret[i] = readln().chomp().split(sp).convert!T();
return ret;
}
}
R[] convert( R , T )( T[] args ) pure
{
R[] ret = new R[](args.length);
foreach( i ; 0..args.length )
ret[i] = args[i].to!R();
return ret;
}
T sum( T )( in T[] args ) pure nothrow
{
T ret = 0;
foreach( elm ; args )
ret += elm;
return ret;
}
| D |
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.format;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
long disit_sum(long a)
{
long sum,disit_num;
foreach(disit;0 .. 9)
{
disit_num = a / pow(10, disit);
sum += disit_num % 10;
}
return sum;
}
void main()
{
long n = lread();
string s = sread();
foreach(e;s)
{
if(e == 'Y')
{
writeln("Four");
return;
}
}
writeln("Three");
} | D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
void main(){
auto A=readln.chomp;
writeln(A.count('1'));
} | D |
import std.stdio;
import std.array;
import std.conv;
import std.string;
import std.algorithm;
void main(){
int sScore = 0; int tScore = 0;
string[] sInput = readln().split();
string[] tInput = readln().split();
foreach(string s; sInput){
sScore += s.to!int();
}
foreach(string s; tInput){
tScore += s.to!int();
}
if(sScore > tScore){
writeln(sScore);
}else{
writeln(tScore);
}
} | D |
import std.stdio, std.array, std.conv, std.string, std.algorithm, std.math;
void main() {
auto n = readln.chomp.to!int;
auto a = readln.chomp.split;
for (int i = n - 1; i >= 0; i--) {
write(a[i]);
if (i != 0) { write(" "); }
}
writeln;
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void main()
{
auto N = readln.chomp;
auto DP = new int[][](N.length+1, 2);
DP[0][0] = 0;
DP[0][1] = 1;
foreach (i; 0..N.length) {
auto n = (N[i] - '0').to!int;
DP[i+1][0] = min(DP[i][0] + n, DP[i][1] + 10-n);
DP[i+1][1] = min(n == 9 ? int.max : (DP[i][0] + n+1), DP[i][1] + 10-n-1);
}
writeln(DP[N.length][0]);
} | D |
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math, std.container, std.typecons;
import core.stdc.stdio;
// foreach, foreach_reverse, writeln
class BIT {
int[] data;
this(int n) {
data = new int[n+1];
}
BIT dup() {
BIT ret = new BIT(to!int(data.length));
ret.data = data.dup;
return ret;
}
void add(int i, int x=1) {
i++;
while (i < data.length) {
data[i] += x;
i += i&-i;
}
}
int sum(int i) {
int ret = 0;
i++;
while (i) {
ret += data[i];
i -= i&-i;
}
return ret;
}
}
void main() {
int n;
scanf("%d", &n);
int[] a = new int[n];
foreach (i; 0..n) scanf("%d", &a[i]);
long L = long(n+1)*n/2;
long M = (L+1)/2;
int l = 0, u = (1<<30)-10;
long count(int[] b) {
int s = n;
long ret = 0;
BIT c = new BIT(n*2+1);
foreach (i; 0..n) {
c.add(s);
s += b[i];
ret += c.sum(s);
}
//writeln(b); writeln(ret);
return ret;
}
while (l+1 < u) {
int m = (l+u)/2;
int[] b = new int[n];
foreach (i; 0..n) {
b[i] = a[i]<m ? -1 : 1;
}
if (count(b) < M) u = m; else l = m;
}
writeln(l);
} | D |
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
void main() {
char a;
scan(a);
writeln('a' <= a && a <= 'z' ? 'a' : 'A');
}
void scan(T...)(ref T args) {
auto line = readln.split; // @suppress(dscanner.suspicious.unmodified)
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.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
auto s = readln.chomp;
writeln(700 + s.count('o') * 100);
}
| D |
import std.stdio, std.string, std.conv, std.range, std.algorithm;
void main() {
auto w = readln.chomp;
int[char] char_count;
auto beautiful = true;
foreach (c; w) {
char_count[c]++;
}
if (char_count.values.all!"a % 2 == 0") {
"Yes".writeln;
} else {
"No".writeln;
}
}
| D |
// import chie template :) {{{
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons, std.format;
// }}}
// 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()() {
}
void scan(T, S...)(ref T x, ref S args) {
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType)) {
str ~= s.to!(char[]).strip.split;
}
}
// }}}
void main() {
auto cin = new Scanner;
int a, b, t;
cin.scan(a, b, t);
int res;
for (int i = a; i <= t; i += a) {
res += b;
}
writeln(res);
}
| D |
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
void main()
{
long[] n_and_k;
long n;
long k;
n_and_k = readln.split.to!(long[]);
n = n_and_k[0];
k = n_and_k[1];
writeln(min(n % k, k - n % k));
}
| D |
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
void main()
{
auto n = sread();
long sum_n;
foreach (i; 0 .. n.length)
{
// writeln(n[i] - '0');
sum_n += (n[i] - '0');
}
// writeln('s', sum_n);
// writeln(n.length);
auto nn = n.to!long();
// writeln(nn);
auto tmp = new long[](n.length);
tmp[0] = nn / (10 ^^ ((n.length) - 1)) - 1;
foreach (i; 1 .. (n.length))
{
tmp[i] = 9;
}
// writeln(tmp);
writeln(max(tmp.sum(), sum_n));
}
auto func(long n)
{
long sum_x;
while (n > 0)
{
sum_x += n % 10;
n /= 10;
}
return sum_x;
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
| D |
import std.stdio;
import std.string;
import std.math;
import std.conv;
import std.algorithm;
import std.bigint;
void main(){
while(true){
long n = to!long(chomp(readln()));
if(n == 0) break;
long[] a;
a ~= 0;
a ~= to!long(chomp(readln()));
for(long i=1;i<n;i++){
a ~= a[i] + to!long(chomp(readln()));
}
long res = a[1];
for(long i=0;i<=n;i++){
for(long j=i+1;j<=n;j++){
res = max( a[j] - a[i] , res);
}
}
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;
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[] as;
foreach(i; 0 .. n) as ~= read.to!long;
long old_a = 0;
long ans = 0;
foreach_reverse(a; as){
if(a >= old_a){
ans += a;
}
else if(a == old_a - 1){
ans += 0;
}
else{
ans = -1;
break;
}
debug writeln("a:", a, " old_a:", old_a, " ans:", ans);
old_a = a;
}
if(old_a > 0) ans = -1;
ans.writeln;
} | D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf = 10L^^15;
void main() {
int n, w;
scan(n, w);
auto ws = new int[](n);
auto v = new int[](n);
int vs;
foreach (i ; 0 .. n) {
scan(ws[i], v[i]);
vs += v[i];
}
auto dp = new long[](vs + 1);
dp[] = inf;
dp[0] = 0;
foreach (i ; 0 .. n) {
foreach_reverse (j ; 0 .. vs - v[i] + 1) {
dp[j + v[i]] = min(dp[j + v[i]], dp[j] + ws[i]);
}
}
foreach_reverse (i ; 0 .. vs + 1) {
if (dp[i] <= w) {
writeln(i);
return;
}
}
}
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 |
/* imports all std modules {{{*/
import
std.algorithm,
std.array,
std.ascii,
std.base64,
std.bigint,
std.bitmanip,
std.compiler,
std.complex,
std.concurrency,
std.container,
std.conv,
std.csv,
std.datetime,
std.demangle,
std.encoding,
std.exception,
std.file,
std.format,
std.functional,
std.getopt,
std.json,
std.math,
std.mathspecial,
std.meta,
std.mmfile,
std.net.curl,
std.net.isemail,
std.numeric,
std.parallelism,
std.path,
std.process,
std.random,
std.range,
std.regex,
std.signals,
std.socket,
std.stdint,
std.stdio,
std.string,
std.system,
std.traits,
std.typecons,
std.uni,
std.uri,
std.utf,
std.uuid,
std.variant,
std.zip,
std.zlib;
/*}}}*/
/+---test
3
1
4
3
---+/
/+---test
2
5
5
---+/
void main(string[] args) {
const N = readln.chomp.to!long;
struct Pair { long i, v; }
Pair[2] M;
foreach (i; 0..N) {
const v = readln.chomp.to!long;
if (M[0].v <= v) {
swap(M[0], M[1]);
M[0] = Pair(i, v);
} else if (M[1].v <= v) {
M[1] = Pair(i, v);
}
}
auto ans = new long[N];
foreach (i; 0..N) {
const p = M[0].i != i? M[0]: M[1];
p.v.writeln;
}
}
| 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()
{
int x, a, b; readV(x, a, b);
if (a >= b)
writeln("delicious");
else if (x+a >= b)
writeln("safe");
else
writeln("dangerous");
}
| D |
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd = readln.split.to!(int[]), a = rd[0], b = rd[1], c = rd[2];
writeln(b-a == c-b ? "YES" : "NO");
}
| D |
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto s = readln.chomp;
auto c = new char[](s.length);
int index;
foreach (i; 0..s.length) {
if (s[i] == 'B') {
if (index != 0) {
index--;
}
} else {
c[index] = s[i];
index++;
}
}
foreach (i; 0..index) {
c[i].write;
}
writeln("");
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (ti; 0..t)
{
auto a = RD!int;
auto b = RD!int;
auto c = RD!int;
auto tot = a + b + c;
if (tot % 9) continue;
ans[ti] = min(a, b, c) >= tot / 9;
}
foreach (e; ans)
{
writeln(e ? "YES" : "NO");
}
stdout.flush;
debug readln;
} | D |
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
long bignum = 1_000_000_007;
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
auto manhattan(long x1, long y1, long x2, long y2)
{
return abs(y2 - y1) + abs(x2 - x1);
}
void main()
{
auto n = lread();
long t_prev, x_prev, y_prev;
bool travel = true;
foreach (_; 0 .. n)
{
long t, x, y;
scan(t, x, y);
auto limit = t - t_prev;
auto m_dist = manhattan(x_prev, y_prev, x, y);
limit -= m_dist;
if(limit < 0 || limit % 2)
travel = false;
t_prev = t;
x_prev = x;
y_prev = y;
}
if(travel)
writeln("Yes");
else
writeln("No");
} | D |
import std.stdio, std.algorithm;
void main() {
auto p = new long[2001];
foreach (i; 0 .. 1001) foreach (j; 0 .. 1001)
p[i + j]++;
while (true) {
if (stdin.eof) break;
int N; scanf("%d\n", &N);
long ans;
foreach (i; 0 .. min(N, 2000) + 1) if (0 <= N - i && N - i <= 2000)
ans += p[i] * p[N - i];
writeln(ans);
}
} | 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;
int k, a, b;
cin.scan(k, a, b);
int t = a / k + cast(int)(a % k != 0);
if (a <= k * t && k * t <= b) {
writeln("OK");
} else {
writeln("NG");
}
}
| D |
module app;
import core.bitop;
import std.algorithm;
import std.array;
import std.bigint;
import std.conv;
import std.stdio;
import std.string;
struct Input
{
int a;
int b;
}
void parseInput(T)(out Input input, T file)
{
with (file) with (input)
{
a = readln().strip().to!int;
b = readln().strip().to!int;
}
}
struct Output
{
}
auto main2(Input* input)
{
return 6 - input.a - input.b;
}
void parseExample(out Input input, string example)
{
struct Adapter
{
string[] _lines;
this(string input) { _lines = input.splitLines(); }
string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; }
}
parseInput(input, Adapter(example));
}
void main()
{
Input input = void;
parseInput(input, stdin);
auto result = main2(&input);
writeln(result);
}
| 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 h, w; scan(h, w);
if (h == 1 && w == 1) {
writeln("No");
return;
}
auto ban = new char[][](h, w);
iota(h).each!(i => ban[i] = readln.chomp.dup);
int[] di = [0, 1, 0, -1];
int[] dj = [1, 0, -1, 0];
foreach (i ; 0 .. h) {
foreach (j ; 0 .. w) {
if (ban[i][j] == '.') continue;
int cnt;
foreach (k ; 0 .. 4) {
int ni = i + di[k];
int nj = j + dj[k];
if (ni < 0 || ni >= h) continue;
if (nj < 0 || nj >= w) continue;
if (ban[ni][nj] == '#') cnt++;
}
if (cnt == 0) {
debug {
writefln("(%d, %d)", i, j);
}
writeln("No");
return;
}
}
}
writeln("Yes");
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
| D |
import std.stdio;
import std.conv;
import std.string;
void main()
{
string[] buf;
int w,h;
buf = split(readln());
w = to!(int)(buf[0]);
h = to!(int)(buf[1]);
writeln(w*h,' ',2*w+2*h);
} | D |
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.math;
void main(){
int n = readln.chomp.to!int;
int[] tmp = readln.chomp.split.map!(to!int).array;
int t = tmp[0], a = tmp[1];
int[] hs = readln.chomp.split.map!(to!int).array;
real f(int x){
return abs(a.to!real - (t.to!real - x.to!real * 0.006));
}
int i0 = 0;
real min = f(hs[0]);
for(int i = 0; i < n; i ++){
if(f(hs[i]) < min){
min = f(hs[i]);
i0 = i;
}
}
writeln(i0 + 1);
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nhw = readln.split.to!(int[]);
auto N = nhw[0];
auto H = nhw[1];
auto W = nhw[2];
int r;
foreach (_; 0..N) {
auto ab = readln.split.to!(int[]);
if (ab[0] >= H && ab[1] >= W) ++r;
}
writeln(r);
} | D |
import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;
void main()
{
auto s = readln.chomp;
auto t = readln.chomp;
auto input = new int[][] (300);
foreach (i, e; s) {
input[e] ~= (i+1).to!int;
}
debug { input.writeln; }
auto pos = input.map!assumeSorted;
long ans = 0;
int cpos = 0;
foreach (e; t) {
if (pos[e].empty()) {
writeln(-1);
return;
}
auto r = pos[e].upperBound(cpos);
if (r.empty()) {
ans += s.length - cpos;
cpos = 0;
r = pos[e].upperBound(0);
}
auto nxt = r[0];
ans += nxt - cpos;
debug { writeln(e, ' ', ans, ' ', nxt); }
cpos = nxt;
}
ans.writeln;
} | D |
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int h, w, k;
rd(h, w, k);
const int mod = 10 ^^ 9 + 7;
auto dp = new int[](w);
dp[0] = 1;
foreach (_; 0 .. h) {
auto nex = new int[](w);
foreach (bit; 0 .. (1 << (w - 1))) {
if (bit & (bit >> 1))
continue;
foreach (i; 0 .. w) {
if (i - 1 >= 0 && bit & (1 << (i - 1))) {
(nex[i - 1] += dp[i]) %= mod;
} else if (i < w - 1 && bit & (1 << i)) {
(nex[i + 1] += dp[i]) %= mod;
} else {
(nex[i] += dp[i]) %= mod;
}
}
}
dp.swap(nex);
}
writeln(dp[k - 1]);
}
void rd(T...)(ref T x) {
import std.stdio : readln;
import std.string : split;
import std.conv : to;
auto l = readln.split;
assert(l.length == x.length);
foreach (i, ref e; x)
e = l[i].to!(typeof(e));
}
| D |
import std.stdio;
import std.math;
void main()
{
int n, m;
int[] a, b;
scanf("%d %d", &n, &m);
a.length = n;
b.length = m;
foreach (i; 0 .. n) scanf("%d", &a[i]);
foreach (i; 0 .. m) scanf("%d", &b[i]);
int result;
while (b.length) {
if (a.length == 0) break;
if (b[0] < a[0]) {
b = b[1..$];
} else {
a = a[1..$];
b = b[1..$];
}
}
writeln(a.length);
} | D |
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
void main() {
string input = readln.chomp;
switch(input) {
case "1 0 0": case "0 1 0": case "0 0 0": {
writeln("Close");
} break;
default: {
writeln("Open");
} break;
}
} | D |
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.numeric;
void main()
{
auto x = readln.chomp.to!long;
auto t = x % 11;
auto res = (x / 11) * 2;
if (t > 6) {
res += 2;
} else if (t > 0) {
res += 1;
}
res.writeln;
}
| D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int N;
string S;
scan(N);
scan(S);
int ans;
foreach (l ; 0 .. N) {
auto z = Zalg(S[l .. $]);
debug {
writeln(z);
}
foreach (j ; 0 .. N - l) {
ans = max(ans, min(j, z[j]));
}
}
writeln(ans);
}
int[] Zalg(string S) {
int N = S.length.to!int;
auto z = new int[](N);
z[0] = N;
for (int i = 1, j = 0; i < N; ) {
while (i + j < N && S[j] == S[i + j]) j++;
z[i] = j;
if (j == 0) {
i++;
continue;
}
int k = 1;
while (i + k < N && k + z[k] < j) {
z[i + k] = z[k];
k++;
}
i += k;
j -= k;
}
return z;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
| D |
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.format;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
enum MOD = (10 ^^ 9) + 7;
void main()
{
auto S = sread();
auto T = sread();
foreach_reverse (i; 0 .. S.length - T.length + 1)
{
bool b = true;
// writeln(S[i .. i + T.length]);
foreach (j, c; S[i .. i + T.length])
{
// writeln(c, T[j]);
b = b && (c == T[j] || c == '?');
}
if (b)
{
char[] ans = S.dup;
ans[i .. i + T.length][] = T;
foreach (ref c; ans)
if (c == '?')
{
c = 'a';
}
writeln(ans);
return;
}
}
writeln("UNRESTORABLE");
}
| D |
import std.conv, std.stdio, std.algorithm, std.string, std.range, std.math;
void main() {
readln;
const S = readln.chomp;
S.uniq.count.writeln;
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
bool[1000001] MEMO;
void main()
{
auto s = readln.chomp.to!int;
int i;
for (;;) {
++i;
if (MEMO[s]) {
writeln(i);
return;
}
MEMO[s] = true;
s = s % 2 == 0 ? s / 2 : s * 3 + 1;
}
} | D |
import std.stdio, std.string, std.conv, std.bigint, std.typecons, std.algorithm, std.array, std.math, std.range;
void main() {
auto N = readln.chomp.to!int;
auto A = readln.chomp.to!int;
writeln(N^^2 - A);
}
| D |
void main()
{
dchar[] s = rdDchar;
dchar[] t = rdDchar;
long len = s.length;
foreach (i; 0 .. len)
{
if (s == t)
{
"Yes".writeln;
return;
}
s = s[$-1] ~ s[0..$-1];
}
"No".writeln;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni; | D |
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int n;
rd(n);
auto a = readln.split.to!(int[]);
long ans = 0;
for (int i = 0, j = 0, s = 0; i < n; s ^= a[i++]) {
while (j < n && ((s & a[j]) == 0)) {
s ^= a[j++];
ans += (j - i);
}
}
writeln(ans);
}
void rd(T...)(ref T x) {
import std.stdio : readln;
import std.string : split;
import std.conv : to;
auto l = readln.split;
assert(l.length == x.length);
foreach (i, ref e; x)
e = l[i].to!(typeof(e));
}
| D |
import std.stdio;
void main(){
for(int x=1;x<10;x++){for(int y=1;y<10;y++){
writeln(x,"x",y,"=",x*y);
}}
} | D |
import std.stdio;
import std.string;
import std.algorithm;
void main() {
foreach (input; stdin.byLine) {
// 勝敗を判定
string result = judge(input.chomp);
// 結果を出力
result.writeln;
}
}
string judge(char[] banmen) {
// 勝ち筋
auto combs = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]];
// それぞれの配置を取得
auto areas = ["o":getArea(banmen, 'o'), "x":getArea(banmen, 'x')];
// 各々の配置が勝ち筋に沿っていれば、その記号を返す
foreach(key; areas.keys) {
foreach(comb; combs) {
if(canFind(areas[key], comb[0]) &&
canFind(areas[key], comb[1]) &&
canFind(areas[key], comb[2])) return key;
}
}
// どちらも勝ち筋に沿っていない場合はドローの記号を返す
return "d";
}
int[] getArea(char[] banmen, char side) {
int[] area = [];
foreach (i; 0..9) {
if(banmen[i] == side) { area ~= i; }
}
return area;
} | D |
import std.stdio;
import std.algorithm;
import std.string;
import std.conv;
import std.math;
void main(){
while(true){
auto s1 = readln();
if(stdin.eof()) break;
auto s = split(s1);
int a = to!int(s[0]);
int b = to!int(s[1]);
int oth = to!int(s[2]);
int[11] cards;
cards = 0;
cards[a]--;
cards[b]--;
cards[oth]--;
int r = 20 - a - b;
int ans = 0;
int ini = min(10,r);
for(int i=ini;i>=0;i--){
if(cards[i]==0) ans++;
}
if(ans>=5) writeln("YES");
else writeln("NO");
}
} | D |
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
string A, op, B;
scan(A, op, B);
long a = A.to!long();
long b = B.to!long();
if (op == "-")
{
writeln(a - b);
}
else
{
writeln(a + b);
}
}
| D |
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n; readV(n);
string s; readV(s);
auto x = 0, m = 0;
foreach (si; s) {
x += si == 'I' ? +1 : -1;
m = max(m, x);
}
writeln(m);
}
| D |
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void main()
{
int n; readV(n);
writeln(n.nsqrt^^2);
}
pure T nsqrt(T)(T n)
{
import std.algorithm, std.conv, std.range, core.bitop;
if (n <= 1) return n;
T m = 1 << (n.bsr/2+1);
return iota(1, m).map!"a * a".assumeSorted!"a <= b".lowerBound(n).length.to!T;
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void main()
{
auto N = readln.chomp.to!long;
long r;
foreach (a; 1..N) {
auto b = N/a;
r += b - (N%a == 0 ? 1 : 0);
}
writeln(r);
} | D |
import std.stdio, std.string, std.conv, std.algorithm;
import std.math;
void main(){
writeln("Odd");
}
| 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; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
int n = read.to!int;
char[] cs = readln.chomp.to!(char[]);
char[] ans = "0000000000".to!(char[]);
foreach(c; cs){
if(c == 'L'){
foreach(i; 0 .. 10) if(ans[i] == '0'){
ans[i] = '1';
break;
}
}
else if(c == 'R'){
foreach_reverse(i; 0 .. 10) if(ans[i] == '0'){
ans[i] = '1';
break;
}
}
else{
int i = (c - '0').to!int;
ans[i] = '0';
}
}
ans.writeln;
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new int[](t);
foreach (ti; 0..t)
{
auto a = RD!int;
auto b = RD!int;
auto x = a & b;
ans[ti] = (a ^ x) + (b ^ x);
}
foreach (e; ans)
{
writeln(e);
}
stdout.flush;
debug readln;
}
| D |
import std.stdio, std.bigint, std.string;
void main() {
int n;
scanf("%d", &n);
int[] a = new int[n];
for(int i = 0; i<n; i++) {
scanf("%d", &a[i]);
}
int [] b= new int[n];
for (int i =0; i < n; ++i) {
b[i] = -1;
}
b[0] = a[0];
int c = 1;
int i = 1;
while (i < n) {
c +=1;
b[c-1] = a[i];
while (c > 1 && b[c-1] == b[c-2]) {
b[c-2] = b[c-2] +1;
c-=1;
}
i += 1;
}
writeln(c);
for (int j = 0; j < c; ++j) {
write(b[j]);
write(" ");
}
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new int[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto s = RD!string;
long cnt;
foreach (c; s)
{
if (c == '0')
++cnt;
}
if (cnt % 2)
ans[ti] = cnt == 1 ? -1 : 1;
else
ans[ti] = -1;
}
foreach (e; ans)
{
writeln(e == 1 ? "ALICE" : e == -1 ? "BOB" : "DRAW");
}
stdout.flush;
debug readln;
} | D |
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
int n;
scan(n);
int ans;
int set = (1<<26) - 1;
bool safe;
foreach (i ; 0 .. n-1) {
char e;
string s;
scan(e, s);
if (safe && e != '.') {
ans++;
continue;
}
int t;
if (e == '!') {
foreach (ch ; s) {
t |= (1<<(ch - 'a'));
}
}
else if (e == '.') {
t = (1<<26) - 1;
foreach (ch ; s) {
t &= ~(1<<(ch - 'a'));
}
}
else {
t = ~(1<<(s[0] - 'a'));
}
set &= t;
debug {
writefln("%b",set);
}
if (set.popcnt == 1) {
safe = 1;
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
} | D |
import std.stdio, std.conv, std.string;
void main() {
int n,m,k;
scanf("%d %d %d", &n, &m, &k);
foreach (i;0..(m+1)) {
foreach (j;0..(n+1)) {
auto a = n*i + (m-2*i)*j;
if (a == k) { writeln("Yes"); return; }
}
}
writeln("No");
}
| D |
import std.stdio; // ??\????????????????????????
import std.string; // chomp????????????????????????(?????????????????????)
import std.conv; // to????????????????????????
import std.array; // split?????????????????????
import std.algorithm; // map?????????????????????
void main() {
uint i = 1;
while (true) {
int x = readln.chomp.to!int;
if (x == 0) {
break;
}
writeln("Case ", i, ": ", x);
i++;
}
} | D |
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
immutable long MOD = 10^^9 + 7;
void main() {
auto S = readln.chomp;
auto N = S.length.to!int;
auto dp = new long[][](N+1, 4); // Aの数, ABの数, ABCの数
dp[0][0] = 1;
foreach (i; 0..N) {
foreach (j; 0..4) {
dp[i+1][j] = S[i] == '?' ? dp[i][j] * 3 % MOD : dp[i][j];
}
if (S[i] == 'A' || S[i] == '?') {
(dp[i+1][1] += dp[i][0]) %= MOD;
}
if (S[i] == 'B' || S[i] == '?') {
(dp[i+1][2] += dp[i][1]) %= MOD;
}
if (S[i] == 'C' || S[i] == '?') {
(dp[i+1][3] += dp[i][2]) %= MOD;
}
}
dp[N][3].writeln;
}
| D |
void main(){
int[] atox;
int cnt;
foreach(i; 0..4)atox ~= _scan();
for(int an; an<=atox[0]; an++){
for(int bn; bn<=atox[1]; bn++){
for(int cn; cn<=atox[2]; cn++){
if(500*an+100*bn+50*cn==atox[3])cnt++;
}
}
}
cnt.writeln();
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
| D |
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
long n = lread();
writeln((n < 1000) ? "ABC" : "ABD");
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto S = RD!string;
writeln(S[0] == S[1] && S[1] == S[2] ? "No" : "Yes");
stdout.flush;
debug readln;
} | D |
import core.stdc.stdio;
import std.algorithm;
char[][] mapdata;
bool[] visited;
int[3][] queue;
int w,h,n;
int len(int[2] p1,int[2] p2){
visited[] = false;
queue[0]=[p1[0],p1[1],0];
visited[p1[0]*w+p1[1]] = true;
int[] dx = [0,-1,0,1];
int[] dy = [-1,0,1,0];
int qc=1;
for(int i=0;i<w*h;i++){
int y=queue[i][0];
int x=queue[i][1];
int s=queue[i][2];
if(y==p2[0]&&x==p2[1])
return s;
for(int k=0;k<4;k++){
int ny = y+dy[k];
int nx = x+dx[k];
if(0<=ny&&ny<h&&0<=nx&&nx<w&&mapdata[ny][nx]!='X'&&!visited[ny*w+nx]){
visited[ny*w+nx]=true;
queue[qc++] = [ny,nx,s+1];
}
}
}
return 114514;
}
void main(){
scanf("%d%d%d",&h,&w,&n);
int[2][] poses = new int[2][n+1];
mapdata = new char[][h];
visited = new bool[w*h];
queue = new int[3][w*h];
for(int i=0;i<h;i++){
mapdata[i] = new char[w+1];
scanf("%s",mapdata[i].ptr);
foreach(int j,c;mapdata[i]){
if(c=='S')
poses[0] = [i,j];
else if('1'<=c&&c<='9')
poses[c-'0'] = [i,j];
}
}
int ans=0;
for(int i=0;i<n;i++)
ans += len(poses[i],poses[i+1]);
printf("%d\n",ans);
} | 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 = new string[](3);
foreach (i; 0..3)
S[i] = RD!string;
size_t pos;
while (true)
{
if (S[pos].empty) break;
auto c = S[pos].front; S[pos].popFront;
if (c == 'a')
pos = 0;
else if (c == 'b')
pos = 1;
else
pos = 2;
}
writeln(pos == 0 ? "A" : pos == 1 ? "B" : "C");
stdout.flush();
debug readln();
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
enum cs = [6,36,216,1296,7776,46656,279936,9,81,729,6561,59049,531441];
void main()
{
auto N = readln.chomp.to!int;
auto DP = new int[][](cs.length, N+1);
foreach (ref dp; DP) dp[] = -1;
int solve(int i, int n) {
if (i == cs.length) return n;
if (DP[i][n] == -1) {
if (n >= cs[i]) {
DP[i][n] = min(solve(i+1, n), solve(i, n-cs[i]) + 1);
} else {
DP[i][n] = solve(i+1, n);
}
}
return DP[i][n];
}
writeln(solve(0, N));
} | D |
import std.stdio;
import std.string;
import std.array;
import std.range;
import std.algorithm;
import std.conv;
void main(string[] args) {
auto input = readln().chomp.split.map!(to!int);
max(input[0]+input[1],input[0]-input[1],input[0]*input[1]).writeln;
} | D |
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int N, T;
scan(N, T);
auto a = new int[](N);
auto b = new int[](N);
foreach (i ; 0 .. N) {
scan(a[i], b[i]);
}
auto dpf = new long[][](N + 1, T);
foreach (i ; 1 .. N + 1) {
foreach (j ; 0 .. T) {
dpf[i][j] = dpf[i - 1][j];
if (j - a[i - 1] >= 0) chmax(dpf[i][j], dpf[i - 1][j - a[i - 1]] + b[i - 1]);
}
}
auto dpb = new long[][](N + 1, T);
foreach_reverse (i ; 0 .. N) {
foreach (j ; 0 .. T) {
dpb[i][j] = dpb[i + 1][j];
if (j - a[i] >= 0) chmax(dpb[i][j], dpb[i + 1][j - a[i]] + b[i]);
}
}
long ans;
foreach (i ; 0 .. N) {
foreach (j ; 0 .. T) {
chmax(ans, dpf[i][j] + dpb[i + 1][T - 1 - j] + b[i]);
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
| D |
import std.stdio,std.string,std.array,std.algorithm,std.range,std.conv;
import std.algorithm:rev=reverse;
void main(){
auto input=readln.chomp.split.to!(int[]);
auto n=input[0];
auto m=input[1];
auto result=n*(n-1)/2;
result+=m*(m-1)/2;
result.writeln;
} | D |
//dlang template---{{{
import std.stdio;
import std.conv;
import std.string;
import std.array;
import std.algorithm;
import std.typecons;
import std.math;
import std.range;
// MIT-License https://github.com/kurokoji/nephele
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType))
{
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType))
{
str ~= s.to!(char[]).strip.split;
}
}
//Digit count---{{{
int DigitNum(int num) {
int digit = 0;
while (num != 0) {
num /= 10;
digit++;
}
return digit;
}
//}}}
//}}}
void main() {
Scanner sc = new Scanner;
string input;
sc.scan(input);
char[] S = new char[input.length];
long len = input.length;
int zeroNum = 0;
int oneNum = 0;
foreach (i; 0 .. len) {
S[i] = to!char(input[0]);
input.popFront;
if (S[i] == '0')
zeroNum++;
else if (S[i] == '1')
oneNum++;
}
if (oneNum == zeroNum) {
writeln(oneNum + zeroNum);
return;
}
writeln((zeroNum + oneNum) - abs(zeroNum - oneNum));
}
| D |
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
void main()
{
// 1*2*3*4*5*6 == 2^^4 * 3^^2 * 5
// 1*
// 2*
// 3*
// 2*2*
// 5*
// 2*3
auto n = lread();
long[long] aa;
foreach (i; 1 .. n + 1)
{
long[long] tmp = factorize(i);
// writeln(tmp);
foreach (key, value; tmp)
{
aa[key] = aa.get(key, 0) + value;
}
}
// writeln(aa);
long ans = 1;
foreach (key, value; aa)
{
ans = ans % (10 ^^ 9 + 7) * (value + 1) % (10 ^^ 9 + 7);
}
writeln(ans);
}
long[long] factorize(long n)
{
long tmp = n;
long[long] aa;
foreach (x; 2 .. n * n + 100)
{
if (tmp < x)
{
if (tmp != 1)
aa[tmp] = aa.get(tmp, 0) + 1;
return aa;
}
while (tmp % x == 0)
{
aa[x] = aa.get(x, 0) + 1;
tmp /= x;
}
}
writeln(tmp);
assert(0);
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
| D |
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
import std.format;
void main()
{
auto D = readln.chomp.to!int;
switch(D)
{
case 22:
writeln("Christmas Eve Eve Eve");
break;
case 23:
writeln("Christmas Eve Eve");
break;
case 24:
writeln("Christmas Eve");
break;
case 25:
writeln("Christmas");
break;
default:
}
} | D |
import std.algorithm, std.array, std.container, std.range, std.bitmanip;
import std.numeric, std.math, std.bigint, std.random, core.bitop;
import std.string, std.conv, std.stdio, std.typecons;
void main()
{
while (true) {
auto rd = readln.split.map!(to!int);
auto h = rd[0], w = rd[1];
if (h == 0 && w == 0) break;
foreach (i; 0..h) {
foreach (j; 0..w)
write(i == 0 || i == h - 1 || j == 0 || j == w - 1 ? '#' : '.');
writeln;
}
writeln;
}
} | D |
// Vicfred
// https://atcoder.jp/contests/abc136/tasks/abc136_c
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;
void main() {
const long n = readln.chomp.to!long;
long[] h = readln.split.map!(to!long).array;
long maxima = h[0] - 1;
foreach(square; h) {
if(square > maxima)
square -= 1;
if(square < maxima) {
"No".writeln;
return;
}
maxima = square;
}
"Yes".writeln;
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
writeln(N%2 == 0 ? N : N*2);
} | D |
import std.stdio;
import std.conv;
import std.string;
void main() {
int x = readln.chomp.to!int;
writeln(x * x * x);
} | D |
module app;
import core.bitop;
import std.algorithm;
import std.array;
import std.bigint;
import std.conv;
import std.stdio;
import std.string;
import std.traits;
struct Input
{
int n;
}
void parseInput(T)(out Input input, T file)
{
with (file) with (input)
{
n = readln().strip().to!int;
}
}
auto main2(Input* input)
{
with (input)
{
return (n + 1) / 2;
}
}
alias retType = ReturnType!main2;
static if (!is(retType == void))
{
unittest { writeln("begin unittest"); }
auto _placeholder_ = ReturnType!main2.init;
unittest // example1
{
string example =
`5`;
if (example.empty) return;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
printResult(result);
assert(result == 3);
}
unittest // example2
{
string example =
`2`;
if (example.empty) return;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
printResult(result);
assert(result == 1);
}
unittest // example3
{
string example =
`100`;
if (example.empty) return;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
printResult(result);
assert(result == 50);
}
unittest { writeln("end unittest"); }
void parseExample(out Input input, string example)
{
struct Adapter
{
string[] _lines;
this(string input) { _lines = input.splitLines(); }
string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; }
}
parseInput(input, Adapter(example));
}
}
void printResult(T)(T result)
{
static if (isFloatingPoint!T) writefln("%f", result);
else writeln(result);
}
void main()
{
Input input = void;
parseInput(input, stdin);
alias retType = ReturnType!main2;
static if (is(retType == void))
main2(&input);
else
{
auto result = main2(&input);
printResult(result);
}
}
| D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
int cnt;
void InsertionSort(int[] arr, int n, int g){
for(int i = g; i < n; ++i){
int v = arr[i];
int j = i - g;
while (j >= 0 && arr[j] > v){
arr[j+g] = arr[j];
j -= g;
++cnt;
}
arr[j+g] = v;
}
}
void ShellSort(int[] arr, int n){
cnt = 0;
int m;
int[] g;
for(int i = 1; ; ){
g ~= i;
i = 3*i + 1;
if(i > n) break;
}
reverse(g);
m = g.length.to!int();
//writeln("m : ",m);
writeln(m);
for(int i = 0; i < m; ++i){
//write("g[", i ,"] : ", g[i]);
write(g[i]);
if(i == m-1) writeln();
else write(" ");
}
for(int i = 0; i < m; ++i){
InsertionSort(arr, n, g[i]);
}
}
int main(string[] argv)
{
auto n = readln.chomp.to!int();
int[] arr;
for(int i = 0; i < n; ++i){
arr ~= readln.chomp.to!int();
}
ShellSort(arr, n);
//writeln("cnt : ", cnt);
writeln(cnt);
foreach(e; arr){
writeln(e);
}
return 0;
} | D |
import std.stdio;
import std.algorithm;
import std.string;
import std.conv;
import std.math;
void main(){
auto s = chomp(readln());
char[] ss;
for(int i=0;i<s.length;i++){
ss ~= s[i];
}
for(int i=0;i<s.length-4;i++){
if(ss[i]=='a'&&ss[i+1]=='p'&&ss[i+2]=='p'&&ss[i+3]=='l'&&ss[i+4]=='e'){
ss[i] = 'p';
ss[i+1] = 'e';
ss[i+2] = 'a';
ss[i+3] = 'c';
ss[i+4] = 'h';
}else if(ss[i]=='p'&&ss[i+1]=='e'&&ss[i+2]=='a'&&ss[i+3]=='c'&&ss[i+4]=='h'){
ss[i] = 'a';
ss[i+1] = 'p';
ss[i+2] = 'p';
ss[i+3] = 'l';
ss[i+4] = 'e';
}
}
for(int i=0;i<s.length;i++)
write(ss[i]);
writeln();
} | D |
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
auto S = sread();
foreach (i; 0 .. S.length)
if (i % 2 == 0)
write(S[i]);
writeln();
}
| D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
void main() {
int n;
scan(n);
foreach (i ; 0 .. 100) {
foreach (j ; 0 .. 100) {
if (4*i + 7*j == n) {
writeln("Yes");
return;
}
}
}
writeln("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.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 H = RD;
auto W = RD;
auto a = new string[](H);
foreach (i; 0..H)
a[i] = RD!string;
auto ws = new bool[](W);
auto hs = new bool[](H);
foreach (y; 0..H)
{
foreach (x; 0..W)
{
if (a[y][x] == '#')
{
ws[x] = 1;
hs[y] = 1;
}
}
}
foreach (y; 0..H)
{
bool writed;
foreach (x; 0..W)
{
if (ws[x] == 1 && hs[y] == 1)
{
write(a[y][x]);
writed = true;
}
}
if (writed) writeln();
}
stdout.flush();
debug readln();
}
| D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main()
{
int n = readln.chomp.to!int;
string s = readln.chomp;
int leftW = 0;
int rightE = 0;
foreach (c; s) {
if (c == 'E') {
++rightE;
}
}
int ans = int.max;
foreach (c; s) {
if (c == 'E') {
--rightE;
}
ans = min(ans, leftW + rightE);
if (c == 'W') {
++leftW;
}
}
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 = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
bool ans;
foreach (i; 1..10)
{
foreach (j; 1..10)
{
if (i*j == N)
ans = true;
}
}
writeln(ans ? "Yes" : "No");
stdout.flush();
debug 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, b, c; readV(a, b, c);
writeln(a[$-1] == b[0] && b[$-1] == c[0] ? "YES" : "NO");
}
| 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.ascii;
import std.concurrency;
import std.traits;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;
const long INF = long.max/3;
const long MOD = 10L^^9+7;
void main() {
writeln(readln.chomp.to!int.floor(3));
}
// ----------------------------------------------
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 str = [staticMap!(getFormat, Args)].join(" ") ~ "\n";
// readf!str(args);
mixin("str.readf(" ~ Args.length.iota.map!(i => "&args[%d]".format(i)).join(", ") ~ ");");
}
void times(alias fun)(int n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
T ceil(T)(T x, T y) if (__traits(isIntegral, T)) {
// `(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 (__traits(isIntegral, T)) {
T t = x / y;
if (t * y > x) t--;
return t;
}
// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
return reduce!fun(tuple(seed), r);
}
}
}
}
// cumulativeFold was added in D 2.072.0
static if (__VERSION__ < 2072) {
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
}
// minElement/maxElement was added in D 2.072.0
static if (__VERSION__ < 2072) {
auto minElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto minimum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b < minimum) {
element = a;
minimum = b;
}
}
return element;
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto maximum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b > maximum) {
element = a;
maximum = b;
}
}
return element;
}
}
| D |
void main(){
auto ABCD = readLine!long();
foreach( i ; 0..102 ){
ABCD[2] -= ABCD[1];
if( ABCD[2] <= 0 ){ writeln("Yes");return; }
ABCD[0] -= ABCD[3];
if ( ABCD[0] <= 0 ){ writeln("No");return; }
}
}
import std.stdio, std.string, std.conv;
import std.math, std.algorithm, std.array;
import std.regex;
T[] readLine( T = size_t )( string sp = " " ){
T[] ol;
foreach( string elm ; readln().chomp().split(sp) ){
ol ~= elm.to!T();
}
return ol;
} | D |
// Vicfred
// https://atcoder.jp/contests/abc162/tasks/abc162_a
// implementation
import std.stdio;
void main() {
string n = readln;
foreach(ch; n) {
if(ch == '7') {
"Yes".writeln;
return;
}
}
"No".writeln;
}
| D |
import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void log(A...)(A arg) { stderr.writeln(arg); }
int size(T)(in T s) { return cast(int)s.length; }
void main() {
auto s = readln.chomp;
long n = s.size;
long ans = 0;
foreach (i, c; s) {
if (i < (n + 1) / 2) { // choose 'g'
ans += (c == 'g' ? 0 : -1);
} else {
ans += (c == 'g' ? 1 : 0);
}
}
writeln(ans);
}
| D |
void main() {
int n = readln.chomp.to!int;
int k = readln.chomp.to!int;
int m = k.log2.ceil.to!int;
int ans = n - m > 0 ? 2 ^^ m + (n - m) * k : 2 ^^ n;
ans.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;
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, std.algorithm, std.range, std.conv, std.string, std.math;
import core.stdc.stdio;
// foreach, foreach_reverse, writeln
void main() {
int a, b, c, x, y;
scanf("%d%d%d%d%d", &a, &b, &c, &x, &y);
a = min(a,c*2);
b = min(b,c*2);
int ans = a*x + b*y;
ans -= max(0, a+b-c*2) * min(x,y);
writeln(ans);
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto ab = readln.split.to!(int[]);
auto A = ab[0];
auto B = ab[1];
int t = 1, r;
while (t < B) {
++r;
t = t - 1 + A;
}
writeln(r);
} | D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
void main()
{
string S = readln.chomp;
long n;
char prev = '#';
foreach(c;S)
{
if(c != prev)
{
prev = c;
n++;
}
}
writeln(n-1);
} | D |
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int a, b;
scan(a, b);
foreach (x ; 0 .. 100000) {
auto p8 = x * 8 / 100;
auto p10 = x * 10 / 100;
if (p8 == a && p10 == b) {
writeln(x);
return;
}
}
writeln(-1);
}
void scan(T...)(ref T args) {
auto line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
| D |
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
auto s = read!string;
auto t = read!string;
auto rs = s.dup;
rs.reverse();
writeln(rs == t ? "YES" : "NO");
}
| D |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.