code
stringlengths 4
1.01M
| language
stringclasses 2
values |
---|---|
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;
long[] as, bs;
foreach(i; 0 .. n) as ~= read.to!long;
foreach(i; 0 .. n) bs ~= read.to!long;
long[] xs, ys;
foreach(i; 0 .. n){
if(i == 0) xs ~= as[0], ys ~= bs[0];
else{
xs ~= max(ys[i - 1] + as[i], xs[i - 1]);
ys ~= max(xs[i - 1] + bs[i], ys[i - 1]);
}
}
long ans = max(xs[n - 1], ys[n - 1]);
ans.writeln;
}
| D |
import std.stdio, std.conv, std.algorithm, std.string;
alias L = long;
L M = 998244353;
L[] X, D;
L q(L m, L[] U){
foreach(i, u; U) if(i / m & 1 ^ U[i % m] ^ u) return u;
return 1;
}
L t(L[] U){
L f;
foreach(u; U) f = (f * 2 + u) % M;
return f;
}
void main(){
L n = readln.chomp.to!L, b = n * 2;
foreach(c; readln.chomp) X ~= c - '0';
foreach_reverse(d; 3 .. n + 1) if(d % 2 && !(n % d)) D ~= d;
L[L] C;
foreach(d; D){
C[d] = t(X[0 .. n / d]) + q(n / d, X);
foreach(e; D) if(e > d && !(e % d)) C[d] += M - C[e];
C[d] %= M;
}
n = (t(X) + 1) * b % M;
foreach(d; D) n += M - C[d] * (b - b / d) % M;
writeln(n % M);
}
| D |
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}}
void main()
{
int n, m; readV(n, m);
string[] a; readC(n, a);
string[] b; readC(m, b);
foreach (y; 0..n-m+1)
foreach (x; 0..n-m+1)
if (a[y..y+m].map!(ai => ai[x..x+m]).array == b) {
writeln("Yes");
return;
}
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; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto t = RD!int;
auto ans = new int[](t);
foreach (ti; 0..t)
{
auto a = RD!int;
auto b = RD!int;
auto c = RD!int;
auto r = RD!int;
if (a > b)
swap(a, b);
auto x = c - r;
auto y = c + r;
auto len = b - a;
if (x < a)
ans[ti] = max(len - max(0, y - a), 0);
else if (y >= b)
ans[ti] = max(len - max(0, b - x), 0);
else
ans[ti] = max(len - (y - x), 0);
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
| D |
void main(){
string s = _scan!string();
switch(s){
case "SUN": writeln(7); break;
case "MON": writeln(6); break;
case "TUE": writeln(5); break;
case "WED": writeln(4); break;
case "THU": writeln(3); break;
case "FRI": writeln(2); break;
case "SAT": writeln(1); break;
default: break;
}
}
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.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 .. 10)
{
disit_num = a / pow(10, disit);
sum += disit_num % 10;
}
return sum;
}
void main()
{
long s = lread();
long sum = disit_sum(s);
(s % sum?"No":"Yes").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 n; readV(n);
string s; readV(s);
auto a = new int[](n);
foreach (i; 0..n) a[i] = s[i] == 'E';
auto b = cumulativeSum(a);
auto m = n;
foreach (i; 0..n)
m = min(m, (i-b[0..i])+b[i+1..$]);
writeln(m);
}
class CumulativeSum(T)
{
size_t n;
T[] s;
this(T[] a)
{
n = a.length;
s = new T[](n+1);
s[0] = T(0);
foreach (i; 0..n) s[i+1] = s[i] + a[i];
}
T opSlice(size_t l, size_t r) { return s[r]-s[l]; }
size_t opDollar() { return n; }
}
auto cumulativeSum(T)(T[] a) { return new CumulativeSum!T(a); }
| D |
import std.stdio : readln, writeln, writefln;
import std.array : array, split;
import std.conv : to;
import std.range.primitives;
import std.range : enumerate, iota, repeat, retro, assumeSorted;
import std.algorithm.searching : all, any, canFind, count, countUntil;
import std.algorithm.comparison : max, min, clamp;
import std.algorithm.iteration : each, group, filter, map, reduce, permutations, sum, uniq;
import std.algorithm.sorting : sort;
import std.algorithm.mutation : reverse, swap;
void main() {
int n, y;
scan(n, y);
foreach (i ; 0 .. n + 1) {
foreach (j ; 0 .. n + 1 - i) {
int k = n - i - j;
if (10000*i + 5000*j + 1000*k == y) {
writeln(i, ' ', j, ' ', k);
return;
}
}
}
writeln(-1, ' ', -1, ' ', -1);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.array : split;
import std.conv : to;
import std.range.primitives;
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
} | D |
import std.stdio, std.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;
void main() {
auto N = readln.chomp.to!int;
auto A = readln.split.map!(x => x.to!int-1).array;
auto B = new int[](N);
foreach (a; A) B[a] += 1;
B.each!writeln;
} | D |
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop;
// dfmt on
void main()
{
long N = lread();
auto S = sread();
auto E = new long[](N + 1);
auto W = new long[](N + 1);
foreach (i; 0 .. N)
{
E[i + 1] = E[i] + (S[i] == 'E');
W[i + 1] = W[i] + (S[i] == 'W');
}
long ans = long.max;
foreach (i; 0 .. N)
{
long cnt = W[i] + E[$ - 1] - E[i + 1];
ans = ans.min(cnt);
}
writeln(ans);
}
| D |
void main() {
string[] tmp = readln.split;
string a = tmp[0], b = tmp[1], c = tmp[2];
writeln(a[$-1] == b[0] && b[$-1] == c[0] ? "YES" : "NO");
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni; | D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
import std.math;
void main() {
int n;
scan(n);
writeln(n/3);
}
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 |
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n, k; rd(n, k);
auto x=new long[](n), y=new long[](n);
foreach(i; 0..n) rd(x[i], y[i]);
long mn=5_000_000_000_000_000_000;
foreach(i1; 0..n)foreach(i2; (i1+1)..n){
foreach(j1; 0..n)foreach(j2; (j1+1)..n){
auto x1=x[i1], x2=x[i2];
auto y1=y[j1], y2=y[j2];
if(x1>x2) swap(x1, x2);
if(y1>y2) swap(y1, y2);
if((x2-x1)*(y2-y1)>=mn) continue;
int cnt=0;
foreach(t; 0..n){
if(x1<=x[t] && x[t]<=x2 && y1<=y[t] && y[t]<=y2) cnt++;
}
if(cnt>=k) mn=min(mn, (x2-x1)*(y2-y1));
}
}
writeln(mn);
}
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;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto n = readln.chomp.to!int;
int sum;
auto tmp = n;
while (tmp != 0) {
auto x = tmp % 10;
sum += x;
tmp /= 10;
}
if (n % sum) {
writeln("No");
} else {
writeln("Yes");
}
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
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 N = RD;
auto A = RD;
auto B = RD;
auto ans = N / (A+B) * A;
ans += min(A, N % (A+B));
writeln(ans);
stdout.flush;
debug readln;
} | D |
import std.stdio;
import std.algorithm;
const int BORING = 15;
const int MATCH = 90;
void main()
{
int n;
int last = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
if ((x - 1) - last >= BORING) {
printf("%d\n", last + BORING);
return;
}
last = x;
}
printf("%d\n", min(last + BORING, MATCH));
}
| 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() {
string s;
scan(s);
int ans;
foreach (i ; 1 .. s.length) {
if (s[i] != s[i - 1]) ans++;
}
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,std.string,std.conv,std.algorithm;
import std.algorithm:rev=reverse;
int main(){
auto input=readln.chomp.split;
auto a=input[0].to!int;
auto b=input[1].to!int;
auto at=(a*13).to!int;
auto bt=(b*10).to!int;
foreach(y;bt..at+1){
if((y*0.08).to!int==a&&(y*0.1).to!int==b){
y.writeln;
return 0;
}
}
"-1".writeln;
return 0;
} | D |
import std.stdio;
import std.algorithm;
import std.array;
import std.conv;
import std.string;
void main() {
string str = readln.chomp;
string[4] ss = ["dream", "erase", "eraser", "dreamer"];
// array.lengthの返り値の型はulongなので
// intに代入しようとすると(dmd64 v2.070.1では)CEになる
int p = str.length.to!int;
while (p > 0) {
bool flag = false;
if (str[p - 1] == 'r') {
for (int i = 2; i < 4; ++i) {
if (p < ss[i].length.to!int) {
break;
}
if (ss[i] == str[(p - ss[i].length.to!int)..p]) {
p -= ss[i].length.to!int;
flag = true;
break;
}
}
}
else if (str[p - 1] == 'e' && p >= 5) {
if (str[(p - ss[1].length.to!int)..p] == ss[1]) {
flag = true;
p -= ss[1].length.to!int;
}
}
else if (str[p - 1] == 'm' && p >= 5) {
if (str[(p - ss[0].length.to!int)..p] == ss[0]) {
flag = true;
p -= ss[0].length.to!int;
}
}
if (!flag) {
writeln("NO");
return;
}
}
writeln("YES");
return;
} | D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n, a, b;
scan(n, a, b);
auto mx = min(a, b);
auto mn = max(0, a + b - n);
writeln(mx, " ", mn);
}
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.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
enum P = 10L^^9+7;
long[2][10^^5+10] DP;
void main()
{
auto L = readln.chomp.to!(char[]);
DP[0][1] = 2;
DP[0][0] = 1;
foreach (i; 1..L.length) {
if (L[i] == '0') {
DP[i][1] = DP[i-1][1];
DP[i][0] = (DP[i-1][0] * 3) % P;
} else {
DP[i][1] = (DP[i-1][1] * 2) % P;
DP[i][0] = (DP[i-1][1] + DP[i-1][0] * 3) % P;
}
}
writeln((DP[L.length-1][0] + DP[L.length-1][1]) % P);
} | D |
void main() {
long n = readln.chomp.to!long;
string s = readln.chomp;
long cnt;
foreach (i; 0 .. 1000)
{
dchar[] a =[
(i / 100 + '0').to!dchar,
(i % 100 / 10 + '0').to!dchar,
(i % 10 + '0').to!dchar
];
bool ok;
long index;
foreach (x; s)
{
if (x == a[index])
{
++index;
}
if (index == 3)
{
++cnt;
break;
}
}
}
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;
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 a = readln.chomp.to!BigInt;
auto b = readln.chomp.to!BigInt;
if (a > b) {
writeln("GREATER");
} else if (b > a) {
writeln("LESS");
} else {
writeln("EQUAL");
}
}
| D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
string s;
scan(s);
string a = "2019/04/30";
writeln(s <= a ? "Heisei" : "TBD");
}
int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new int[][](n, 0);
foreach (i ; 0 .. m) {
int u, v;
scan(u, v);
if (is1indexed) {
u--, v--;
}
adj[u] ~= v;
if (isUndirected) {
adj[v] ~= u;
}
}
return adj;
}
alias Edge = Tuple!(int, "to", int, "cost");
Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new Edge[][](n, 0);
foreach (i ; 0 .. m) {
int u, v, c;
scan(u, v, c);
if (is1indexed) {
u--, v--;
}
adj[u] ~= Edge(v, c);
if (isUndirected) {
adj[v] ~= Edge(u, c);
}
}
return adj;
}
void yes(bool b) {writeln(b ? "Yes" : "No");}
void YES(bool b) {writeln(b ? "YES" : "NO");}
T[] readArr(T)() {return readln.split.to!(T[]);}
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;
immutable hurui = (int n){
bool[] table = new bool[](n + 1);
table[] = true;
table[1] = false;
for(int i = 2; i * i <= n; ++i)
{
for(int j = i + i; j <= n; j += i)
{
table[j] = false;
}
}
return table;
}(60000);
void main()
{
while(true)
{
string line = readln.chomp;
if(line is null)break;
auto num = line.chomp.to!int;
for(int i = num - 1; i >= 0; --i)
{
if(hurui[i])
{
write(i, " ");
break;
}
}
for(int i = num + 1; i <= 60000; ++i)
{
if(hurui[i])
{
i.writeln;
break;
}
}
}
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.bigint;
int count_ds(int i)
{
int r;
while (i) {
r += i%10;
i /= 10;
}
return r;
}
void main()
{
auto N = readln.chomp.to!int;
int r = int.max;
foreach (i; 1..N/2+1) {
r = min(r, count_ds(i) + count_ds(N-i));
}
writeln(r);
} | D |
import std.stdio, std.conv, std.string;
void main(){
(n => n * (n + 1) / 2)(r).writeln;
}
int r() { return readln.chomp.to!int; } | D |
import std.stdio, std.string, std.conv, std.math;
void main() {
writeln(readln.chomp.to!int ^^ 3);
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.typecons;
import std.numeric, std.math;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }
long mod = 10^^9 + 7;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto M = RD;
auto diff = M - N*2;
if (diff > 0)
{
N += diff / 4;
M -= diff / 2;
}
writeln(min(N, M/2));
stdout.flush();
} | D |
import std.stdio, std.string, std.uni;
void main() {
string str = readln.chomp;
foreach (x; str) {
if (x.isUpper) {
x.toLower.write;
} else if (x.isLower) {
x.toUpper.write;
} else {
x.write;
}
}
writeln;
}
| 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;
bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void main(string[] args){ args ~= ["", ""]; string cmd = args[1]; if(cmd == "-debug") DEBUG = 1;
if(cmd == "-gen") gen; else if(cmd == "-jury") jury; else solve; }
void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ std.stdio.write(t, " "), print(a); }
string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void gen(){}
void jury(){}
void solve(){
string s = scan;
if(s[$ - 1] == 's') s ~= 'e';
(s ~ 's').print;
} | 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 long[](t);
foreach (ti; 0..t)
{
auto x = RD!int;
auto y = RD!int;
ans[ti] += min(x.abs, y.abs) * 2;
auto d = max(x.abs, y.abs) - min(x.abs, y.abs);
if (d == 0) continue;
ans[ti] += (d-1) * 2 + 1;
}
foreach (e; ans)
{
writeln(e);
}
stdout.flush;
debug readln;
}
| 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;
int K;
sc.scan(K);
if (K % 2 == 0) {
writeln((K * K / 2) / 2);
} else {
writeln( (K - (K / 2)) * ((K - (K / 2)) - 1));
}
}
| D |
import std.stdio, std.conv, std.array, std.range, std.algorithm,
std.string, std.math;
void main(){
auto tmp = readln.chomp.split.to!(int[]);
auto a = tmp[0], b = tmp[1];
char[100][100] field;
foreach(i; 0 .. 50) foreach(j; 0 .. 100) field[i][j] = '.';
foreach(i; 50 .. 100) foreach(j; 0 .. 100) field[i][j] = '#';
foreach(k; 0 .. a - 1){
field[51 + (k / 50) * 2][(k % 50) * 2] = '.';
}
foreach(k; 0 .. b - 1){
field[(k / 50) * 2][(k % 50) * 2] = '#';
}
writeln("100 100");
foreach(i; 0 .. 100) field[i].to!string.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;
void main() {
int[] NK = readln.chomp.split.to!(int[]);
int N = NK[0],
K = NK[1];
int[] A = readln.chomp.split.to!(int[]);
int ans = 1;
int cnt = K;
ans += (N - K) / (K - 1);
if ((N - K) % (K - 1) != 0) {
ans++;
}
writeln(ans);
}
| D |
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random;
import std.typecons, std.functional, std.traits,std.concurrency;
import std.algorithm, std.container;
import core.bitop, core.time, core.memory;
import std.bitmanip;
import std.regex;
enum INF = long.max/3;
enum MOD = 10L^^9+7;
//辞書順順列はiota(1,N),nextPermituionを使う
void end(T)(T v)
if(isIntegral!T||isSomeString!T||isSomeChar!T)
{
import core.stdc.stdlib;
writeln(v);
exit(0);
}
T[] scanArray(T = long)()
{
static char[] scanBuf;
readln(scanBuf);
return scanBuf.split.to!(T[]);
}
dchar scanChar()
{
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
return cast(dchar)c;
}
T scanElem(T = long)()
{
import core.stdc.stdlib;
static auto scanBuf = appender!(char[])([]);
scanBuf.clear;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
scanBuf ~= cast(char) c;
c = getchar;
}
return scanBuf.data.to!T;
}
dchar[] scanString(){
return scanElem!(dchar[]);
}
void main()
{
auto b = scanString.count!"a=='9'"!=0;
if(b)end("Yes");end("No");
}
| D |
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int n, t;
rd(n, t);
int mn = 10 ^^ 9;
foreach (_; 0 .. n) {
int c, tm;
rd(c, tm);
if (tm <= t) {
mn = min(mn, c);
}
}
if (mn < 10 ^^ 9) {
writeln(mn);
} else {
writeln("TLE");
}
}
void rd(T...)(ref T x) {
import std.stdio : readln;
import std.string : split;
import std.conv : to;
auto l = readln.split;
assert(l.length == x.length);
foreach (i, ref e; x)
e = l[i].to!(typeof(e));
}
| D |
import std.stdio,std.conv,std.string;
void main()
{
auto a = readint();
auto b = readint();
auto h = readint();
writeln(((a+b)*h)/2);
}
int readint() {
return to!(int)(chomp(readln()));
}
| D |
import std.stdio, std.string, std.conv, std.array, std.algorithm;
import std.uni, std.range, std.math, std.container, std.datetime;
import core.bitop, std.typetuple, std.typecons;
immutable long MOD = 1_000_000_007;
alias tie = TypeTuple;
alias triplet = Tuple!(int, int, int);
void main(){
long m, n;
readVars(m, n);
auto ans = modpow(m, n, MOD);
writeln(ans);
}
int gcd(int x, int y){
return y ? gcd(y, x % y) : x;
}
long modpow(long x, long y, long mod){
return y ? ((modpow(x, y>>1, mod)^^2) % mod * x^^(y&1)) % mod : 1;
}
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){
throw new Exception("args num < input num");
}
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math;
void main()
{
readln;
int[int] as;
foreach (a; readln.split.to!(int[])) as[a] += 1;
int r;
foreach (a, n; as) {
if (n >= a) {
r += n - a;
} else {
r += n;
}
}
writeln(r);
} | 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.algorithm, std.conv, std.range, std.stdio, std.string;
import std.numeric; // gcd, fft
void main()
{
auto n = readln.chomp.to!size_t;
auto t = new long[](n);
foreach (i; 0..n) t[i] = readln.chomp.to!long;
writeln(t.reduce!lcm);
}
auto lcm(long a, long b) { return a / gcd(a, b) * b; }
| D |
void main() {
int[] a = new int[5];
foreach (i; 0 .. 5) {
a[i] = readln.chomp.to!int;
}
int k = readln.chomp.to!int;
writeln(a[$-1] - a[0] > k ? ":(" : "Yay!");
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.container;
import std.typecons; | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto D = RD;
auto x = (D * 2 + 1);
auto ans = N / x;
writeln(ans + (N % x != 0 ? 1 : 0));
stdout.flush();
debug readln();
}
| D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.container;
import std.array;
import std.math;
import std.range;
import std.typecons;
import std.ascii;
bool check(string s)
{
int index = 0;
foreach (c; s) {
if (c=='(') {
++index;
} else {
if (index == 0) {
return false;
}
--index;
}
}
return index == 0;
}
void main()
{
int n = readln.chomp.to!int;
string s = readln.chomp;
string sub, ans;
int index = 0;
foreach (c; s) {
final switch(c) {
case '(':
ans ~= sub;
ans ~= '(';
sub = "";
++index;
break;
case ')':
if (index == 0) {
sub = sub ~ ")";
ans = "(" ~ ans;
} else {
sub = sub ~ ")";
--index;
}
break;
}
}
//()())))((()(()))(((((
foreach (i; 0..index) {
sub ~= ")";
}
ans ~= sub;
assert(ans.check);
writeln = ans;
}
| D |
import std.stdio, std.string, std.conv;
void main()
{
auto S = readln.chomp.to!(dchar[]);
S[3] = '8';
writeln(S);
} | D |
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int a, b, c; rd(a, b, c);
int[int] cnt;
cnt[a]=1;
if(b in cnt) writeln(c);
else if(c in cnt) writeln(b);
else writeln(a);
}
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 |
void main() {
string s = readln.chomp[0..$-2];
ulong len = s.length / 2;
while (s[0..len] != s[len..$]) {
s = s[0..$-2];
--len;
}
s.length.writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni; | D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n, k;
scan(n, k);
auto s = readln.chomp.to!(char[]);
s[k - 1] = (s[k - 1] - 'A' + 'a').to!char;
writeln(s);
}
int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new int[][](n, 0);
foreach (i; 0 .. m) {
int u, v;
scan(u, v);
if (is1indexed) {
u--, v--;
}
adj[u] ~= v;
if (isUndirected) {
adj[v] ~= u;
}
}
return adj;
}
alias Edge = Tuple!(int, "to", int, "cost");
Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new Edge[][](n, 0);
foreach (i; 0 .. m) {
int u, v, c;
scan(u, v, c);
if (is1indexed) {
u--, v--;
}
adj[u] ~= Edge(v, c);
if (isUndirected) {
adj[v] ~= Edge(u, c);
}
}
return adj;
}
void yes(bool b) {
writeln(b ? "Yes" : "No");
}
void YES(bool b) {
writeln(b ? "YES" : "NO");
}
T[] readArr(T)() {
return readln.split.to!(T[]);
}
T[] readArrByLines(T)(int n) {
return iota(n).map!(i => readln.chomp.to!T).array;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
| D |
import std.stdio;
void main() {
int cnt, max;
foreach(c;readln) {
if (c == 'A' || c == 'C' || c == 'T' || c == 'G') {
++cnt;
if (cnt > max) max = cnt;
}
else {
cnt = 0;
}
}
max.write;
}
| D |
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container;
import std.numeric, std.math;
import core.bitop;
T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T)(in string str) { return str.split.to!(T[]); }
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; }
long mod = pow(10, 9) + 7;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
struct UnionFind
{
void init(long n) { par = new long[](n); foreach (i; 0..n) par[i] = i; }
long root(long i) { return par[i] == i ? i : (par[i] = root(par[i])); }
bool same(long i, long j) { return root(i) == root(j); }
void unite(long i, long j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; }
long[] par;
}
void main()
{
auto N = RD!long;
auto K = RD!long;
writeln(N / 2 + (N % 2) >= K ? "YES" : "NO");
stdout.flush();
} | D |
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.ascii;
void main()
{
auto s = readln.chomp.array;
if (s.length == 3) {
s.reverse();
}
s.writeln;
}
| D |
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() { return readln.chomp.to!int; }
int[] readints() { return readln.split.to!(int[]); }
void calc(int n, int a, int b) {
while (true) {
if (a + 1 < b) { // to right
a++;
}
else if (a > 1) { // to left
a--;
}
else {
writeln("Borys");
break;
}
if (a < b - 1) { // to left
b--;
}
else if (b + 1 < n) { // to right
b++;
}
else {
writeln("Alice");
break;
}
}
}
void main() {
auto nab = readints;
int n = nab[0], a = nab[1], b = nab[2];
calc(n, a, b);
} | D |
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
int n = readint;
int h = readint;
int w = readint;
int ans = (n - h + 1) * (n - w + 1);
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.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 AB = RD + RD;
auto CD = RD + RD;
writeln(AB == CD ? "Balanced" : AB > CD ? "Left" : "Right");
stdout.flush();
debug readln();
}
| D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
import std.range;
void main(){
auto Z=readln.split.to!(int[]),a=Z[0],b=Z[1];
if((a*b)%2==0)writeln("Even");
else writeln("Odd");
} | D |
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = 10 ^^ 5 + 1;
auto P = new bool[](N);
foreach (i; 2 .. N)
P[i] = f(i);
auto PP = new bool[](N);
foreach (i; 3 .. N)
PP[i] = ((i & 1) != 0) && P[i] && P[(i + 1) / 2];
auto S = new long[](N + 1);
foreach (i; 0 .. N)
S[i + 1] = S[i] + PP[i];
long Q = lread();
foreach (_; 0 .. Q)
{
long l, r;
scan(l, r);
writeln(S[r + 1] - S[l - 1]);
}
}
bool f(long x)
{
foreach (i; 2 .. x)
{
if (x < i * i)
return true;
if (x % i == 0)
return false;
}
return true;
}
| 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];
writeln(B % A == 0 ? A + B : B - A);
} | 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 K = readln.chomp;
auto D = readln.chomp.to!int;
auto N = K.length.to!int;
auto dp = new long[][][](N+1, D, 2);
dp[0][0][0] = 1;
foreach (i; 0..N) {
foreach (j; 0..D) {
foreach (k; 0..2) {
int d = 10;
if (!k) d = K[i] - '0' + 1;
foreach (x; 0..d) {
(dp[i+1][(j+x)%D][k||x<K[i]-'0'] += dp[i][j][k]) %= MOD;
}
}
}
}
long ans = (dp[N][0][0] + dp[N][0][1] - 1) % MOD;
ans = (ans + MOD) % MOD;
ans.writeln;
}
| D |
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
int n, w;
scan(n, w);
auto dp = new int[](w + 1);
foreach (i ; 1 .. n + 1) {
int val, wei, m;
scan(val, wei, m);
for (int k; m > 0; k++) {
int take = min(m, 1<<k);
m -= take;
foreach_reverse (j ; take * wei .. w + 1) {
dp[j] = max(dp[j], dp[j - take * wei] + val * take);
}
}
}
writeln(dp[w]);
}
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.conv, std.stdio;
import std.algorithm, std.array, std.range, std.string;
void main()
{
immutable n = readln.chomp.to!size_t, s = readln.chomp;
auto count = ['R': long(0), 'G': 0, 'B': 0];
foreach (l; s)
count[l] += 1;
auto answer = count['R'] * count['G'] * count['B'];
foreach (i; 0..s.length)
foreach (k; i+2..s.length)
{
if ((i ^ k) & 1) continue;
immutable j = (i + k) >> 1;
if (s[i] != s[j] && s[j] != s[k] && s[k] != s[i])
answer -= 1;
}
answer.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 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
string s;
scan(s);
writeln(s[0] == s[1] || s[1] == s[2] || s[2] == s[3] ? "Bad" : "Good");
}
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 |
void main()
{
string x = readln.chomp;
auto g = x.group.array;
long slen;
long len = x.length;
foreach (t; g)
{
if (t[0] == 'S')
{
slen += t[1].to!long;
}
else
{
long lmin = min(slen, t[1].to!long);
len -= 2 * lmin;
slen -= lmin;
}
}
len.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.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/5;
enum long MOD = 10L^^9+7;
void main() {
string s = readln.chomp;
string t = readln.chomp;
long K = 2000000;
string x = "";
while(x.length < K) {
x ~= t;
}
string y = "";
while(y.length <= x.length) {
y ~= s;
}
long l = 0;
long r = K.floor(t.length) + 1;
while(r - l > 1) {
long c = (l + r)/2;
string z = x[0..t.length*c];
if (searchPattern(y, z).empty) {
r = c;
} else {
l = c;
}
}
if (l == K.floor(t.length)) {
writeln(-1);
} else {
writeln(l);
}
}
/*
KMP法
@return:
out[0] == -1
out[i] == max { ys.length | ys is a prefix of xs[0..i] and a suffix of xs[0..i] }
計算量
- 全体: O(n)
- 各ステップ: 最悪 O(log n)
*/
long[] solveKmp(T)(T[] xs) {
long n = xs.length;
long[] kmp = new long[n+1];
long[] mp = new long[n+1];
long j = -1;
kmp[0] = mp[0] = j;
foreach(i; 0..n) {
while(j >= 0 && xs[i] != xs[j]) {
j = kmp[j];
}
kmp[i+1] = mp[i+1] = ++j;
if (i+1 < n && xs[i+1] == xs[j]) {
kmp[i+1] = kmp[j];
}
}
return mp;
}
/*
textからpatternに一致する開始インデックスを列挙する
- O(n + m)
*/
long[] searchPattern(T)(T[] text, T[] pattern) {
long n = text.length;
long m = pattern.length;
assert(m <= n);
long[] indices = [];
long[] kmp = solveKmp!T(pattern);
long i = 0; // index for `text`
long j = 0; // index for `pattern`
while(i < n) {
while(j >= 0 && text[i] != pattern[j]) {
j = kmp[j];
}
j++;
i++;
if (j == m) {
assert(i >= m);
indices ~= i-m;
j = kmp[j-1];
i--;
}
}
return indices;
}
// ----------------------------------------------
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 (y > 0 && t * y < x) t++;
if (y < 0 && t * y > x) t++;
return t;
}
T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
T t = x / y;
if (y > 0 && t * y > x) t--;
if (y < 0 && 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) {
enum sep = " ";
enum n = Args.length;
enum fmt = n.rep!(()=>"%s").join(sep);
string line = readln.chomp;
static if (__VERSION__ >= 2074) {
line.formattedRead!fmt(args);
} else {
enum argsTemp = n.iota.map!(
i => "&args[%d]".format(i)
).join(", ");
mixin(
"line.formattedRead(fmt, " ~ argsTemp ~ ");"
);
}
}
// 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);
}
}
}
}
// popcnt with ulongs was added in D 2.071.0
static if (__VERSION__ < 2071) {
ulong popcnt(ulong x) {
x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L);
x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L);
x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL);
x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL);
x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL);
x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL);
return x;
}
}
| D |
import core.stdc.stdio;
import std.typecons;
import std.stdio;
import std.algorithm;
import std.math;
void main(){
int v,e;
scanf("%d%d",&v,&e);
int[][] graph = new int[][v];
foreach(i;0..e){
int a,b;
scanf("%d%d",&a,&b);
graph[--a]~=--b;
graph[b]~=a;
}
int[] d=new int[v];
int[] s=new int[v];
int[] t=new int[v];
void dfs(int i,int b,ref int e,ref int f){
if(d[i])
return;
d[i]=b;
e++;
foreach(c;graph[i])
dfs(c,3-b,f,e);
}
foreach(i;0..v)
dfs(i,1,s[i],t[i]);
foreach(i;0..v)
foreach(c;graph[i])
if(d[i]==d[c]){
printf("-1\n");
return;
}
int g;
int[int] h;
foreach(i;0..v)
if(s[i]||t[i]){
g+=min(s[i],t[i]);
int w=abs(s[i]-t[i]);
if(w in h)
h[w]++;
else
h[w]=1;
}
int[] dp=new int[v+1];
static immutable int inf=1145141919;
dp[]=inf;
dp[0]=0;
foreach(q;h.byKeyValue){
foreach(ref x;dp)
if(x<inf)x=0;
foreach(i;q.key..v+1)
if(dp[i-q.key]<q.value)
dp[i]=min(dp[i],dp[i-q.key]+1);
}
// writeln(g," ",dp);
long ans=0;
foreach(i,x;dp)
if(x<inf)
ans=max(ans,cast(long)(g+i)*(v-g-i));
printf("%lld\n",ans-e);
} | D |
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.7.4"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
int main() {
Scanner sc = new Scanner(stdin);
string s;
sc.read(s);
string t = "AKIHABARA";
int cnt = 0;
foreach (c; s) {
while (true) {
if (cnt == t.length.to!int || (t[cnt] != c && t[cnt] != 'A')) {
writeln("NO");
return 0;
}
if (t[cnt] == c) break;
cnt++;
}
cnt++;
}
if (cnt < t.length.to!int - 1) {
writeln("NO");
return 0;
}
writeln("YES");
return 0;
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
// import dcomp.array;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
FastAppender!(E[]) buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/array.d */
// module dcomp.array;
T[N] fixed(T, size_t N)(T[N] a) {return a;}
struct FastAppender(A, size_t MIN = 4) {
import std.algorithm : max;
import std.conv;
import std.range.primitives : ElementEncodingType;
import core.stdc.string : memcpy;
private alias T = ElementEncodingType!A;
private T* _data;
private uint len, cap;
@property size_t length() const {return len;}
bool empty() const { return len == 0; }
void reserve(size_t nlen) {
import core.memory : GC;
if (nlen <= cap) return;
void* nx = GC.malloc(nlen * T.sizeof);
cap = nlen.to!uint;
if (len) memcpy(nx, _data, len * T.sizeof);
_data = cast(T*)(nx);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void opOpAssign(string op : "~")(T item) {
if (len == cap) {
reserve(max(MIN, cap*2));
}
_data[len++] = item;
}
void insertBack(T item) {
this ~= item;
}
void removeBack() {
len--;
}
void clear() {
len = 0;
}
ref inout(T) back() inout { assert(len); return _data[len-1]; }
ref inout(T) opIndex(size_t i) inout { return _data[i]; }
T[] data() {
return (_data) ? _data[0..len] : null;
}
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
import core.bitop : popcnt;
static if (!__traits(compiles, popcnt(ulong.max))) {
public import core.bitop : popcnt;
int popcnt(ulong v) {
return popcnt(cast(uint)(v)) + popcnt(cast(uint)(v>>32));
}
}
bool poppar(ulong v) {
v^=v>>1;
v^=v>>2;
v&=0x1111111111111111UL;
v*=0x1111111111111111UL;
return ((v>>60) & 1) != 0;
}
/*
This source code generated by dcomp and include dcomp's source code.
dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp)
dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt)
*/
| D |
import std.stdio;import std.conv;import std.algorithm;import std.array;
auto raia(){ return readln().split().map!(to!int); } //read as a int[]
static const int mint =60;
static const int hour = 60*mint;
void main()
{
auto it = raia();
writeln( it[0] /hour,":",it[0]%hour/mint,":",it[0]%mint);
} | D |
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container;
import std.numeric, std.math;
import core.bitop;
T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
long mod = pow(10, 9) + 7;
long moda(long x, long y) { return (x + y) % mod; }
long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; }
long modm(long x, long y) { return (x * y) % mod; }
void main()
{
auto N = RD!long;
long[long] cnt;
foreach (i; 0..N)
{
++cnt[RD!long];
}
long ans;
foreach (key; cnt.keys)
{
if (cnt[key] % 2 == 1) ++ans;
}
writeln(ans);
stdout.flush();
} | D |
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
auto s=readln.chomp.to!(char[]);
bool[char] v;
v['a']=true;
v['i']=true;
v['u']=true;
v['e']=true;
v['o']=true;
int n=s.length.to!(int);
foreach(i; 0..n){
if((s[i] in v)==null){
if(s[i]=='n'){
//
}else{
if(i+1<n){
if((s[i+1] in v)==null){
writeln("NO");
return;
}
}else{
writeln("NO");
return;
}
}
}else{
//
}
}
writeln("YES");
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x) e=l[i].to!(typeof(e));
} | D |
import 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;
void main() {
auto S = readln.chomp;
auto N = S.length.to!int;
auto X = new int[](N+1);
int[][int] Y;
X[0] = 0;
Y[0] = [0];
foreach (i; 0..N) {
int c = S[i] - 'a';
X[i+1] = X[i] ^ (1 << c);
Y[X[i+1]] ~= i+1;
}
auto dp = new int[](N+1);
dp.fill(1 << 29);
dp[0] = 0;
int[int] used;
foreach (i; 0..N) {
foreach (j; 0..26) {
int next = X[i] ^ (1 << j);
if (next in Y && (!(next in used) || used[next] > dp[i])) {
used[next] = dp[i];
foreach (m; Y[next]) {
dp[m] = min(dp[m], dp[i]+1);
}
}
}
if (X[i] in Y && (!(X[i] in used) || used[X[i]] > dp[i])) {
used[X[i]] = dp[i];
foreach (m; Y[X[i]]) {
dp[m] = min(dp[m], dp[i]+1);
}
}
}
dp[N].writeln;
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
bool n, w, s, e;
foreach (c; readln.chomp) switch (c) {
case 'N':
n = true;
break;
case 'W':
w = true;
break;
case 'S':
s = true;
break;
case 'E':
e = true;
break;
default:
}
writeln((n==s)&&(w==e) ? "Yes" : "No");
} | 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 readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readM(T)(size_t r,size_t c,ref T[][]t){t=new T[][](r);foreach(ref v;t)readA(c,v);}
void main()
{
int n; readV(n);
int[][] a; readM(n, 3, a);
auto dp = new int[][](n, 3);
dp[0] = a[0];
foreach (i; 1..n)
foreach (j; 0..3)
foreach (k; 0..3)
if (j != k)
dp[i][j] = max(dp[i][j], dp[i-1][k] + a[i][j]);
writeln(dp[$-1].reduce!max);
}
| D |
import std.stdio, std.array, std.conv, std.string, std.algorithm, std.range;
void main() {
foreach (string line; lines(stdin)) {
auto d = line.chomp.to!int;
int ans = 0;
for (int i = d; i < 600; i += d) {
ans += i * i * d;
}
ans.writeln;
}
} | D |
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int n;
rd(n);
int s = 0;
foreach (_; 0 .. n) {
int l, r;
rd(l, r);
s += (r - l + 1);
}
writeln(s);
}
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.string;
import std.conv;
import std.algorithm;
import std.math;
import std.regex;
import std.range;
void main() {
char[] a;
foreach(i; 0..3) {
a ~= readln.chomp[i];
}
a.writeln;
}
| D |
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd = readln.split.to!(int[]), n = rd[0], m = rd[1];
auto a = m * 1900 + (n-m) * 100;
writeln(a * 2^^m);
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto s = readln.chomp;
int i;
foreach (c; s) {
if (i == 0 && c == 'C') ++i;
if (i == 1 && c == 'F') ++i;
}
writeln(i == 2 ? "Yes" : "No");
} | D |
import std.stdio;
void main()
{
int i;
for(i=0;i<1000;i++)
{
writeln("Hello World");
}
} | D |
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(i;0..n){t[i]=r.front.to!(ElementType!T);r.popFront;}}
void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t)v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}
void main()
{
int n; readV(n);
int d, x; readV(d, x);
int[] a; readM(n, a);
writeln(a.map!(ai => (d+ai-1)/ai).sum + x);
}
| D |
void main() {
rs.count!"a == '1'".writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
| D |
void main(){
int n = _scan();
writeln("ABC", n);
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
| D |
import std.algorithm;
import std.range;
import std.stdio;
void main(){
readln();
readln().split().retro().join(" ").writeln();
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto x = RD;
writeln(x < 1200 ? "ABC" : "ARC");
stdout.flush();
debug readln();
} | 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!(long[]);
auto a = ab[0];
auto b = ab[1];
if (a > b) swap(a, b);
long r;
while (b) {
r *= 10;
r += a;
--b;
}
writeln(r);
} | 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()
{
long N, M;
scan(N, M);
auto T = new bool[long][](N);
foreach (_; 0 .. M)
{
long a, b;
scan(a, b);
T[a - 1][b - 1] = T[b - 1][a - 1] = true;
}
foreach (i; 0 .. N)
if (0 in T[i] && N - 1 in T[i])
{
writeln("POSSIBLE");
return;
}
writeln("IMPOSSIBLE");
}
| D |
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
int n, k, q; scan(n, k, q);
auto t = new int[n];
for (int i = 0; i < q; i++) {
int a = readint - 1;
t[a]++;
}
for (int i = 0; i < n; i++) {
int point = k - (q - t[i]);
writeln(point > 0 ? "Yes" : "No");
}
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container;
void main()
{
auto N = readln.chomp.to!int;
auto YS = new long[](N+1);
YS[] = 1;
YS[0] = 0;
foreach (i; 2..N+1) {
long k = 1;
while (i*k <= N) {
YS[i*k] += 1;
k += 1;
}
}
long r;
foreach (i; 1..N+1) r += YS[i] * i;
writeln(r);
} | D |
import core.stdc.stdio;
import std.math;
import std.algorithm;
import std.typecons;
import std.stdio;
void main(){
int n;
scanf("%d",&n);
long[] x=new long[n],y=new long[n];
foreach(i;0..n)
scanf("%lld%lld",&x[i],&y[i]);
alias Tuple!(long,"x",long,"y",int,"k") p;
p[] ps = new p[n-1];
foreach(i;0..n){
int c;
int u1;
int u2;
foreach(j;0..n)
if(j<i){
if(y[j]>=y[i]){
ps[c++]=p(x[j]-x[i],y[j]-y[i],0);
u1++;
}else
ps[c++]=p(x[i]-x[j],y[i]-y[j],1);
}else if(j>i){
if(y[j]>=y[i]){
ps[c++]=p(x[j]-x[i],y[j]-y[i],2);
u2++;
}else
ps[c++]=p(x[i]-x[j],y[i]-y[j],3);
}
sort!((a,b)=>a.x*b.y>a.y*b.x)(ps);
if((u1==0||u1==i)&&(u2==0||u2==(n-i-1)))
continue;
bool ok=false;
foreach(v;ps){
if(v.k==0)
u1--;
else if(v.k==1)
u1++;
else if(v.k==2)
u2--;
else if(v.k==3)
u2++;
else
assert(false);
if((u1==0||u1==i)&&(u2==0||u2==(n-i-1))){
ok=true;
break;
}
}
if(!ok){
printf("Impossible\n");
return;
}
}
printf("Possible\n");
} | D |
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int q;
rd(q);
bool enough(long k, long n, long a, long b, long m) {
return a * m + b * (n - m) < k;
}
while (q--) {
long k, n, a, b;
rd(k, n, a, b);
if (b * n >= k) {
writeln(-1);
continue;
}
long ok = 0, ng = n + 1;
while (ng - ok > 1) {
auto m = (ok + ng) / 2;
if (enough(k, n, a, b, m)) {
ok = m;
} else {
ng = m;
}
}
writeln(ok);
}
}
void rd(T...)(ref T x) {
import std.stdio : readln;
import std.string : split;
import std.conv : to;
auto l = readln.split;
assert(l.length == x.length);
foreach (i, ref e; x)
e = l[i].to!(typeof(e));
}
| D |
void main() {
int[] tmp = readln.split.to!(int[]);
int a = tmp[0], b = tmp[1], c = tmp[2];
writeln(b - a == c - b ? "YES" : "NO");
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni; | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string;
void main()
{
auto S = readln.chomp.to!(dchar[]);
auto N = S.length;
size_t i = 0, j = S.length - 1;
auto a = S[0], b = S[0], c = S[$-1], d = S[$-1];
int x, y;
foreach (s; S) {
++(s == '1' ? x : y);
}
if (!x || !y) {
writeln(N);
return;
}
while (i <= j) {
if (i + 1 < N - j) {
a = S[i];
while (a == b) b = S[++i];
} else {
d = S[j];
while (c == d) c = S[--j];
}
}
writeln(i > N - j - 1 ? i : N - j - 1);
} | D |
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
auto S = sread();
auto C = new uint[](N);
foreach (i; 0 .. N)
{
C[i] = (1 << (S[i] - 'a'));
}
long ans = -1;
foreach (i; 1 .. N)
{
uint a = (C[0 .. i + 1] ~ 0).reduce!"a|b"();
uint b = (C[i + 1 .. $] ~ 0).reduce!"a|b"();
// writefln("%030b %030b %030b", a, b, a & b);
ans = ans.max(popcnt(a & b));
}
writeln(ans);
}
| D |
void main()
{
long n = rdElem;
long[] a = 5.rdCol;
long amin = a.reduce!min;
long m = (n + amin - 1) / amin;
writeln(m + 4);
}
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()
{
long x = rdElem;
long limit = 200000;
auto sieve = new Prime(limit);
foreach (i; x .. limit)
{
if (sieve.isPrime(i))
{
i.writeln;
return;
}
}
}
struct Prime
{
bool[] flags;
this(long limit)
{
flags = makeSieve(limit);
}
bool[] makeSieve(long limit)
{
long len = (limit - 1) / 2;
bool[] result = new bool[len];
result[] = true;
foreach (i; 0 .. len)
{
if (!result[i]) continue;
long prime = 3 + 2 * i;
foreach (j; iota(prime+i, len, prime))
{
result[j] = false;
}
}
return result;
}
bool isPrime(long number)
in
{
assert((number - 1) / 2 <= flags.length);
}
body
{
if (number < 2)
return false;
else if (number == 2)
return true;
else if (number % 2 == 0)
return false;
else
return flags[(number-3)/2];
}
}
T rdElem(T = long)()
{
//import std.stdio : readln;
//import std.string : chomp;
//import std.conv : to;
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
dchar[] rdDchar()
{
//import std.conv : to;
return rdStr.to!(dchar[]);
}
T[] rdRow(T = long)()
{
//import std.stdio : readln;
//import std.array : split;
//import std.conv : to;
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
//import std.range : iota;
//import std.algorithm : map;
//import std.array : array;
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
//import std.range : iota;
//import std.algorithm : map;
//import std.array : array;
return iota(col).map!(x => rdRow!T).array;
}
void wrMat(T = long)(T[][] mat)
{
//import std.stdio : write, writeln;
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni; | D |
import std.stdio, std.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];
if (A >= 13) {
writeln(B);
} else if (A >= 6) {
writeln(B/2);
} else {
writeln(0);
}
} | D |
module main;
import core.stdc.stdio;
int main(string[] arv) {
int n;
scanf("%d", &n);
char [] s = new char[n];
for(int i = 0; i < n; i++) scanf(" %c", &s[i]);
for(int i = 0; i < n; i++) {
if(s[i] != 'a' && s[i] != 'e' && s[i] != 'i' && s[i] != 'o' && s[i] != 'u' && s[i] != 'y')
printf("%c", s[i]);
else {
int count = 1;
for(int j = i + 1; j < n && s[i] == s[j]; j++) count++;
if((s[i] == 'e' || s[i] == 'o') && count == 2)
printf("%c%c", s[i], s[i]);
else
printf("%c", s[i]);
while(s[i] == s[i+1]) i++;
}
}
printf("\n");
return 0;
}
| D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
void main(){
int n = readln.chomp.to!int;
int[] a = readln().split().map!(to!int).array;
int[] x = [0,0,0];
foreach(i; 0..n-1){
if(a[i] == a[i+1]){
x ~= i+1;
}
}
x ~= n;
int ans = 0;
foreach(i; 0..x.length-3){
ans = max(ans, x[i+3] - x[i]);
}
writeln(ans);
} | 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.container;
import std.random;
void main() {
auto a = readln.chomp.split.map!(to!int);
auto w = a[0];
auto h = a[1];
auto x = a[2];
auto y = a[3];
auto r = a[4];
if (x-r>=0 && x+r<=w && y-r>=0 && y+r<=h) writeln("Yes");
else writeln("No");
} | 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[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N, M;
scan(N, M);
writeln((N == M) ? "Yes" : "No");
}
| D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons;
import std.math, std.numeric;
void main() {
int n; scan(n);
char[][] ban = new char[][](n, n);
iota(n).each!(i => ban[i] = readln.chomp.to!(char[]));
long ans;
foreach (i ; 0 .. n) {
if (isSym(n, ban, 0, i)) {
ans += n;
}
}
writeln(ans);
}
bool isSym(int n, char[][] ban, int a, int b) {
auto cop = new char[][](n, n);
foreach (i ; 0 .. n) {
foreach (j ; 0 .. n) {
cop[i][j] = ban[(i + a) % n][(j + b) % n];
}
}
debug {
writefln("(a, b) = (%d, %d)", a, b);
writefln("%-(%s\n%)", cop);
}
foreach (i ; 0 .. n) {
foreach (j ; 0 .. n) {
if (cop[i][j] != cop[j][i]) {
return false;
}
}
}
return true;
}
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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.