code
stringlengths 4
1.01M
| language
stringclasses 2
values |
---|---|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD!string;
long ans;
foreach (i; 0..N.length)
{
auto a = [N[i]].to!long;
if (i == N.length - 1)
{
ans += a;
break;
}
auto b = [N[i+1]].to!long;
if (a - 1 + 9 >= a + b)
{
ans += a - 1;
ans += (N.length - 1 - i) * 9;
break;
}
else
{
ans += a;
}
}
writeln(ans);
stdout.flush();
debug readln();
}
| D |
/+ dub.sdl:
name "D"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.algorithm;
import std.typecons;
int main() {
auto sc = new Scanner(stdin);
int n;
sc.read(n);
alias Edge = Tuple!(int, "to", long, "dist");
Edge[][] g = new Edge[][](n);
foreach (i; 0..n-1) {
int a, b; long c;
sc.read(a, b, c); a--; b--;
g[a] ~= Edge(b, c);
g[b] ~= Edge(a, c);
}
long sm, ex = -1;
long[] sz = new long[n];
void dfs(int p, int b, long bd) {
long ma = 0;
sz[p] = 1;
foreach (e; g[p]) {
if (e.to == b) continue;
dfs(e.to, p, e.dist);
ma = max(ma, sz[e.to]);
sz[p] += sz[e.to];
}
long X = sz[p], Y = n-sz[p];
debug writeln(p, " ", b, " ", bd, " ", X, " ", Y);
if (ex == -1) {
if (X == Y) {
ex = bd;
} else if (ma <= n/2 && Y <= n/2) {
//mid point
ex = g[p].map!(e => e.dist).minimum;
}
}
if (X > Y) swap(X, Y);
sm += 2*X*bd;
}
dfs(0, -1, 0);
writeln(sm - ex);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
static if (__VERSION__ <= 2070) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
version (X86) static if (__VERSION__ < 2071) {
import core.bitop : bsf, bsr, popcnt;
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
if (f.eof) return false;
line = lineBuf[];
f.readln(line);
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else {
auto buf = line.split.map!(to!E).array;
static if (isStaticArray!T) {
assert(buf.length == T.length);
}
x = buf;
line.length = 0;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/algorithm.d */
// module dcomp.algorithm;
import std.range.primitives;
import std.traits : isFloatingPoint, isIntegral;
T binSearch(alias pred, T)(T l, T r) if (isIntegral!T) {
while (r-l > 1) {
T md = (l+r)/2;
if (!pred(md)) l = md;
else r = md;
}
return r;
}
T binSearch(alias pred, T)(T l, T r, int cnt = 60) if (isFloatingPoint!T) {
foreach (i; 0..cnt) {
T md = (l+r)/2;
if (!pred(md)) l = md;
else r = md;
}
return r;
}
E minimum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed)
if (isInputRange!Range && !isInfinite!Range) {
import std.algorithm, std.functional;
return reduce!((a, b) => binaryFun!pred(a, b) ? a : b)(seed, range);
}
ElementType!Range minimum(alias pred = "a < b", Range)(Range range) {
assert(!range.empty, "range must not empty");
auto e = range.front; range.popFront;
return minimum!pred(range, e);
}
E maximum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed)
if (isInputRange!Range && !isInfinite!Range) {
import std.algorithm, std.functional;
return reduce!((a, b) => binaryFun!pred(a, b) ? b : a)(seed, range);
}
ElementType!Range maximum(alias pred = "a < b", Range)(Range range) {
assert(!range.empty, "range must not empty");
auto e = range.front; range.popFront;
return maximum!pred(range, e);
}
Rotator!Range rotator(Range)(Range r) {
return Rotator!Range(r);
}
struct Rotator(Range)
if (isForwardRange!Range && hasLength!Range) {
size_t cnt;
Range start, now;
this(Range r) {
cnt = 0;
start = r.save;
now = r.save;
}
this(this) {
start = start.save;
now = now.save;
}
@property bool empty() {
return now.empty;
}
@property auto front() {
assert(!now.empty);
import std.range : take, chain;
return chain(now, start.take(cnt));
}
@property Rotator!Range save() {
return this;
}
void popFront() {
cnt++;
now.popFront;
}
}
| D |
void main() {
dchar[] s = readln.chomp.to!(dchar[]);
int cnt;
foreach (i; 1 .. s.length) {
if (s[i] == s[i-1]) {
if (s[i] == '1') s[i] = '0';
else s[i] = '1';
++cnt;
}
}
cnt.writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni; | D |
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array, std.functional;
void main() {
auto input = readln.split.to!(int[]);
auto N = input[0], K = input[1], Q = input[2];
auto A = new int[Q];
foreach(i; 0..Q) {
A[i] = readln.split[0].to!int - 1;
}
auto ps = new int[N];
ps[] = K;
foreach(a; A) {
ps[a]++;
}
foreach(ref p; ps) {
p-=Q;
}
foreach(p; ps) {
if(p > 0) {
writeln("Yes");
} else {
writeln("No");
}
}
}
| D |
void main()
{
writeln(180 * (readln.chomp.to!int - 2));
}
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;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
void main()
{
long n;
scan(n);
auto a = aryread();
long cnt;
foreach (i; 0 .. n)
{
if ((i % 2 == 0) && (a[i] % 2 != 0))
cnt += 1;
}
writeln(cnt);
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
| D |
void main() {
int[] tmp = readln.split.to!(int[]);
int a = tmp[0], b = tmp[1];
writeln(a <= b ? a : a - 1);
}
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.uni; | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(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 n = RD!int;
auto k = RD!int;
string pat = "a";
foreach (i; 1..k)
{
pat ~= 'a';
foreach (_; 0..2)
pat ~= cast(char)(i+'a');
}
foreach (i; 1..k)
{
foreach (j; i+1..k)
{
pat ~= cast(char)(i+'a');
pat ~= cast(char)(j+'a');
}
}
string ans;
while (ans.length < n)
{
ans ~= pat;
}
ans.length = n;
writeln(ans);
stdout.flush;
debug readln;
} | D |
import std.stdio,
std.string,
std.conv;
R delegate(Args) Z(R, Args...)(R delegate(R delegate(Args), Args) f){
return (Args args) => f(Z(f), args);
}
void main() {
(N =>
(Fibonacci =>
Fibonacci(1, 1, N)
)(Z((ulong delegate(ulong, ulong, ulong)solver, ulong n1, ulong n2, ulong N) =>
0 < N ? solver(n1 + n2, n1, N - 1) : n2)
))(readln.chomp.to!ulong).writeln;
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void main()
{
auto n = RD!int;
auto m = RD!int;
string[] s, t;
foreach (i; 0..n)
s ~= RD!string;
foreach (i; 0..m)
t ~= RD!string;
auto q = RD!int;
auto ans = new string[](q);
foreach (qi; 0..q)
{
auto y = RD!int-1;
ans[qi] = s[y%n] ~ t[y%m];
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
| 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(ref v;t){v=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 readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}}
void main()
{
long x, y; readV(x, y);
auto a = [x];
while (a[$-1]*2 <= y) a ~= a[$-1]*2;
writeln(a.length);
}
| 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 r, d, x; readV(r, d, x);
foreach (i; 0..10) {
x = r*x-d;
writeln(x);
}
}
| D |
import std.stdio;
import std.math;
import std.conv;
import std.string;
int solve(int x) {
return pow(x,3);
}
void main() {
writeln(solve(to!int(chomp(readln))));
} | D |
void main() {
long s = readln.chomp.to!long;
long x1 = 1L;
long x2 = 10L ^^ 9;
long y1 = (s + x2 - 1) / x2;
long y2 = x2 * y1 - s;
writeln(0, " ", 0, " ", x1, " ", y1, " ", x2, " ", y2);
}
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;
void main(string[] args) {
int done = 0;
string[6] data;
for (int i = 0; i < 6; i++)
{
data[i] = std.stdio.readln();
// data[i] = std.string.stripRight(data[i]);
}
int[][] cost = [
[ 3, 3, 0, 4, 4, 0, 3, 3 ],
[ 3, 3, 0, 4, 4, 0, 3, 3 ],
[ 2, 2, 0, 3, 3, 0, 2, 2 ],
[ 2, 2, 0, 3, 3, 0, 2, 2 ],
[ 1, 1, 0, 2, 2, 0, 1, 1 ],
[ 1, 1, 0, 2, 2, 0, 1, 1 ]
];
int ansi = 0;
int ansj = 0;
int value = -1;
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 8; j++)
{
if (data[i][j] == '.' && value < cost[i][j]) {
value = cost[i][j];
ansi = i;
ansj = j;
}
}
}
for (int i = 0; i < 6; i++)
{
if (i == ansi) {
std.stdio.write(data[i][0..ansj]);
std.stdio.write('P');
std.stdio.writeln(data[i][ansj+1..8]);
} else {
std.stdio.write(data[i]);
}
}
} | D |
import std.stdio, std.conv, std.string, std.math, std.regex, std.range, std.ascii, std.algorithm;
void main(){
auto ip = readln.split.to!(int[]), A=ip[0], B=ip[1];
if(A<6) writeln(0);
else if(A<13) writeln(B/2);
else writeln(B);
} | D |
module main;
import core.stdc.stdio ;
int main()
{
int n,l,r,q,k=0,j=0,ans=0,low,mid,i;
scanf("%d", &q);
int [] a = new int[1000005];
for(i=0;i<q;i++)
{
scanf("%d:%d",&l,&r);
k=l*60+r;
a[k]++;
a[k+1440]++;
}
for(i=0;i<2879;i++){
if(a[i]==0) j++;
else {
if(ans<j) ans=j;
j=0;
}
}
if(ans<j) ans=j;
int hh=ans/60;
int mm=ans%60;
if(hh<10) printf("0%d:",hh);
else printf("%d:",hh);
if(mm<10) printf("0%d",mm);
else printf("%d",mm);
return 0;
} | D |
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() {
auto xab = readints();
int x = xab[0], a = xab[1], b = xab[2];
if (abs(x - a) < abs(x - b))
writeln("A");
else
writeln("B");
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto A = readln.chomp.to!int;
auto B = readln.chomp.to!int;
if (A + B == 5) {
writeln(1);
} else if (A + B == 4) {
writeln(2);
} else if (A + B == 3) {
writeln(3);
}
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto S = RD!string;
auto a = S[0..2].to!long;
auto b = S[2..$].to!long;
bool am, bm;
if (a != 0 && a <= 12)
{
am = true;
}
if (b != 0 && b <= 12)
{
bm = true;
}
if (am && bm)
{
writeln("AMBIGUOUS");
}
else if (am)
{
writeln("MMYY");
}
else if (bm)
{
writeln("YYMM");
}
else
{
writeln("NA");
}
stdout.flush();
debug readln();
} | D |
import std.stdio;
import std.algorithm;
import std.conv;
import std.datetime;
import std.numeric;
import std.math;
import std.string;
string my_readln() { return chomp(readln()); }
void main()
{
auto N = to!ulong(my_readln());
auto tokens = split(my_readln());
auto T = to!long(tokens[0]);
auto A = to!long(tokens[1]);
double d = double.max;
ulong pos;
foreach (i, token; split(my_readln()))
{
auto x = to!long(token);
auto diff = abs(T - x * 0.006 - A);
if (diff < d)
{
d = diff;
pos = i + 1;
}
}
writeln(pos);
stdout.flush();
} | D |
import std.stdio, std.string, std.conv;
import std.algorithm;
void main()
{
for(string t; (t=readln().chomp()).length;)
{
immutable N = t.length;
auto a = new int[26][N+1];
int s=-1, e=-1;
void solve()
{
foreach(int i,c;t)
{
immutable int n = c-'a';
a[i+1]=a[i];
a[i+1][n]++;
foreach(int j;max(0,i-2)..i) if(a[i+1][n]-a[j][n]>(i+1-j)/2)
{
s=j+1;
e=i+1;
return;
}
}
}
solve();
writeln(s,' ',e);
}
} | D |
import std.stdio, std.string, std.conv, std.algorithm, std.array;
struct P { int x, y; }
void main()
{
int[102][102] m;
auto f = readln.split.map!(to!int);
int W = f[0],
H = f[1];
foreach(i; 0 .. H)
{
foreach(j, n; readln.split.map!(to!int).array)
{
m[i + 1][j + 1] = n;
}
}
W += 2;
H += 2;
P[] getRin(P p)
{
P[] res;
if(p.x > 0) res ~= P(p.x - 1, p.y);
if(p.x < W - 1) res ~= P(p.x + 1, p.y);
if(p.y % 2 == 0)
{
if(p.y > 0)
{
res ~= P(p.x, p.y - 1);
if(p.x > 0) res ~= P(p.x - 1, p.y - 1);
}
if(p.y < H - 1)
{
res ~= P(p.x, p.y + 1);
if(p.x > 0) res ~= P(p.x - 1, p.y + 1);
}
}
else
{
if(p.y > 0)
{
res ~= P(p.x, p.y - 1);
if(p.x < W - 1) res ~= P(p.x + 1, p.y - 1);
}
if(p.y < H - 1)
{
res ~= P(p.x, p.y + 1);
if(p.x < W - 1) res ~= P(p.x + 1, p.y + 1);
}
}
return res;
}
void dfs(P p)
{
if(m[p.y][p.x] == 0)
{
m[p.y][p.x] = 2;
foreach(rin; getRin(p))
{
dfs(rin);
}
}
}
dfs(P(0, 0));
int res;
foreach(y; 1 .. H - 1)
{
foreach(x; 1 .. W - 1)
{
if(m[y][x] == 1) foreach(rin; getRin(P(x, y)))
{
if(m[rin.y][rin.x] == 2) ++res;
}
}
}
res.writeln;
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math;
void main()
{
auto N = readln.chomp.to!int;
wchar[][] MAP;
MAP.length = N;
foreach (ref line; MAP) {
auto cs = readln.chomp.to!(wchar[]);
line = cs ~ cs;
}
int cnt;
foreach (o; 0..N) {
void check() {
foreach (i; 0..N) {
foreach (j; i..N) {
if (MAP[i][o+j] != MAP[j][o+i]) return;
}
}
++cnt;
}
check();
}
writeln(cnt * N);
} | D |
import std.conv;
import std.stdio;
import std.array;
import std.range;
import std.string;
import std.algorithm;
void main()
{
switch(readln().chomp()) {
case "1 0 0" : writeln("Close"); break;
case "0 1 0" : writeln("Close"); break;
case "1 1 0" : writeln("Open"); break;
case "0 0 1" : writeln("Open"); break;
case "0 0 0" : writeln("Close"); break;
default : stderr.writeln("Invalid Input"); break;
}
} | D |
import std.stdio, std.string, std.conv;
import std.array, std.algorithm, std.range;
void main()
{
for(;;)
{
string[] ss;
for(string t; (t=readln().chomp())!="" && t!="0 0"; ss~=t){}
if(ss.length==0) break;
int[] c = new int[3];
foreach(s;ss)
{
auto m = s.split().map!(to!int);
immutable a=m[0], b=m[1];
c.length = max(c.length,max(a,b)+1);
++c[a];
++c[b];
}
writeln(c[1..3].all!"a&1"() && !c[3..$].any!"a&1"()?"OK":"NG");
}
} | D |
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
bool ask (int x, int y)
{
writeln ("?", " ", x, " ", y);
stdout.flush ();
auto s = readln.strip;
if (s == "e")
{
assert (false);
}
return (s == "x");
}
void main ()
{
string s;
while ((s = readln.strip) != "")
{
if (s == "end")
{
break;
}
if (s != "start")
{
assert (false);
}
immutable int start = 0;
int x = start;
int y = x;
int d = 1;
do
{
x = y;
y += d;
d *= 2;
}
while (!ask (x, y));
int lo = x + 1;
int hi = y;
while (lo != hi)
{
auto me = (lo + hi) / 2;
if (ask (x, me))
{
hi = me;
}
else
{
lo = me + 1;
}
}
writeln ("!", " ", lo);
stdout.flush ();
}
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
auto S = readln.chomp;
int min_l, max_l = N/2+1;
while (min_l+1 < max_l) {
auto l = (min_l + max_l) / 2;
int[string] memo;
bool ok;
foreach (i; 0..N-l+1) {
auto k = S[i..i+l];
if (k in memo) {
if (memo[k]+l-1 < i) {
ok = true;
break;
}
} else {
memo[k] = i;
}
}
if (ok) {
min_l = l;
} else {
max_l = l;
}
}
writeln(min_l);
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto S = readln.chomp.to!(char[]);
auto T = readln.chomp.to!(char[]);
auto N = S.length;
auto M = T.length;
foreach_reverse (i; 0..N-M+1) {
foreach (j; 0..M) {
if (S[i+j] != '?' && S[i+j] != T[j]) goto ng;
}
S[i..i+M] = T[];
goto ok;
ng:
}
writeln("UNRESTORABLE");
return;
ok:
foreach (ref c; S) if (c == '?') c = 'a';
writeln(S);
} | D |
import std.stdio;
import std.algorithm;
import std.string;
import std.functional;
import std.array;
import std.conv;
import std.math;
import std.typecons;
import std.regex;
import std.range;
void main(){
while(true){
auto temp = readln();
if(stdin.eof()) break;
auto s = split(temp).to!(int[]);
writeln(s[0]-s[1]);
}
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
readln;
auto S = readln.chomp.to!(char[]);
auto K = readln.chomp.to!int;
auto c = S[K-1];
foreach (ref s; S) if (s != c) s = '*';
writeln(S);
} | D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
void main() {
string str = chomp(readln());
long n = to!long(str);
writeln(factrial(n));
}
long factrial(long n) {
if (n == 0) {
return 1;
} else {
return n * factrial(n-1);
}
} | D |
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
void main() {
string[] input = readln.split;
int N = input[0].to!int;
int T = input[1].to!int;
int H = input[2].to!int;
int L = input[3].to!int;
int[] t = new int[N];
int[] h = new int[N];
foreach(int i; 0..N) {
input = readln.split;
t[i] = input[0].to!int;
h[i] = input[1].to!int;
}
int i=-1;
int x = 0; // 残金
while(true) {
i = (i+1)%N;
if (t[i] > 0) {
t[i]--; T++; x+=10;
} else if (h[i] > 0) {
h[i]--; H++; x+=100;
} else {
break;
}
if (T > L) break;
if (x >= 90) {
bool flg = false;
for (int j = T; j>=0; j--) {
if (x-90-10*j>=0 && (x-90-10*j)%100==0 && (x-90-10*j)/100<=H) {
x=0;
T-=j; H-=(x-90-10*j)/100;
t[i]+=j; h[i]+=(x-90-10*j)/100;
flg = true;
break;
}
}
if (!flg) break;
}
}
writeln(i+1);
} | D |
import std.stdio;
import std.string;
import std.range;
import std.conv;
void main()
{
auto X = readln.chomp.to!int;
auto A = readln.chomp.to!int;
auto B = readln.chomp.to!int;
writeln((X-A)%B);
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nm = readln.split.to!(int[]);
auto N = nm[0];
auto M = nm[1];
auto min_i = 1, max_i = M;
auto mid_i = (min_i + max_i) / 2;
while (max_i - min_i > 1) {
mid_i = (min_i + max_i) / 2;
if (M / mid_i < N) {
max_i = mid_i;
} else {
min_i = mid_i;
}
}
mid_i += 1000;
for (;;) {
if (M % mid_i == 0 && M / mid_i >= N ) {
writeln(mid_i);
break;
}
--mid_i;
}
} | D |
void main() {
/*auto S = rs, T = rs;
foreach(i; 0..S.length) {
string tmp = S[1..$];
tmp ~= S[0];
if(S == T) {
writeln("Yes");
return;
}
S = tmp;
}
writeln("No");*/
auto S = rs, T = rs;
S ~= S;
if(S.canFind(T)) writeln("Yes");
else writeln("No");
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.conv;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
} | D |
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.typecons, std.range, std.container, std.math, std.algorithm.searching, std.functional,std.mathspecial;
void main(){
writeln(totalSum(readln.chomp));
}
long totalSum(string str){
long total=0;
foreach(i,s;str){
auto num=[s].to!long;
auto headPattern=2^^i;
auto tl=str.length-i-1;
auto tailSum=10L^^tl+2L^^(tl-1)*(5L^^tl-1)/4;
total+=num*headPattern*tailSum;
}
return total;
} | D |
module aizu_online_judge;
import std.stdio;
int main(){
for(int i = 1; i <= 9; i++) {
for(int j = 1; j <= 9; j++) {
writeln(i, "x", j, "=", i * j);
}
}
return 0;
} | D |
void main() {
problem();
}
void problem() {
auto X = scan!long;
auto K = scan!long;
auto D = scan!long;
long solve() {
if (X < 0) X = -X;
auto times = X / D;
if (K < times) {
return X - D*K;
}
auto loopTimes = K - times;
if (loopTimes % 2 == 0) {
return X - D*times;
} else {
return (X - D*times - D).abs;
}
}
solve().writeln;
}
// ----------------------------------------------
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
// -----------------------------------------------
| D |
import std.stdio;
import std.string;
void main() {
string s = chomp(readln());
string t = chomp(readln());
int ans = 0;
for (int i=0; i<s.length; i++) {
if (s[i] == t[i]) ans++;
}
writeln(ans);
} | D |
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void main() {
auto n = readln.strip.to!int;
Tuple!(string, int)[] a;
foreach (i; 0 .. n) {
auto s = readln.splitter;
auto z = s.front;
s.popFront ();
a ~= tuple (z, s.front.to!int);
}
auto t = readln.strip;
int res;
foreach_reverse (i; 0 .. n) {
if (a[i][0] == t) break;
res += a[i][1];
}
writeln (res);
}
| D |
import std.stdio;
void main() {
int r,g,b,n;
scanf("%d %d %d %d", &r, &g, &b, &n);
size_t total;
for (int i; i <= n; i += r) {
for (int j = i; j <= n; j += g) {
if ((n - j) % b == 0) ++total;
}
}
total.write;
}
| D |
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.utf;
bool isGood (R) (R s)
{
int balance = 0;
foreach (c; s)
{
balance += c ? +1 : -1;
if (balance < 0)
{
return false;
}
}
return balance == 0;
}
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto s = readln.strip;
bool ok = false;
foreach (i; 0..8)
{
int [char] t;
t['A'] = (i >> 0) & 1;
t['B'] = (i >> 1) & 1;
t['C'] = (i >> 2) & 1;
ok |= isGood (s.byChar.map !(c => t[c]));
}
writeln (ok ? "YES" : "NO");
}
}
| D |
import std.algorithm;
import std.array;
import std.range;
import std.stdio;
import std.string;
string solve (string s, int i, int j, int n)
{
char [13] [2] res;
int loopLength = (j - i + 1) / 2;
while (i > 0)
{
s = s[1..$] ~ s[0];
i -= 1;
j -= 1;
}
debug {writeln (s, " ", i, " ", j);}
int mid = loopLength - 1;
mid = min (mid, 12);
int pos = mid;
res[0][pos] = s[i];
while (pos > 0)
{
pos -= 1;
i += 1;
res[0][pos] = s[i];
}
i += 1;
res[1][pos] = s[i];
while (i < j - 1)
{
pos += 1;
i += 1;
res[1][pos] = s[i];
}
i += 1;
assert (i == j);
pos = mid;
assert (res[0][pos] == s[i]);
while (pos < 12)
{
pos += 1;
i += 1;
res[0][pos] = s[i];
}
i += 1;
if (i < n)
{
res[1][pos] = s[i];
}
while (i + 1 < n)
{
pos -= 1;
i += 1;
res[1][pos] = s[i];
}
return res[].map !(x => x.idup).join ("\n");
}
void main ()
{
string s;
while ((s = readln.strip) != "")
{
int n = cast (int) (s.length);
foreach (i; 0..n)
{
foreach (j; i + 1..n)
{
if (s[i] == s[j])
{
if (j - i == 1)
{
writeln ("Impossible");
}
else
{
writeln (solve (s, i, j, n));
}
}
}
}
}
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(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 s = RD!string;
string[] ans;
ans ~= "R 2";
ans ~= "L 2";
auto len = s.length*2 - 1;
ans ~= "R 2";
len += len - 2;
ans ~= "R " ~ (len-1).to!string;
writeln(ans.length);
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
| D |
//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
final class InputReader {
private:
ubyte[] p, buffer;
bool eof;
bool rawRead () {
if (eof) {
return false;
}
p = stdin.rawRead (buffer);
if (p.empty) {
eof = true;
return false;
}
return true;
}
ubyte nextByte(bool check) () {
static if (check) {
if (p.empty) {
if (!rawRead ()) {
return 0;
}
}
}
auto r = p.front;
p.popFront ();
return r;
}
public:
this () {
buffer = uninitializedArray!(ubyte[])(16<<20);
}
bool seekByte (in ubyte lo) {
while (true) {
p = p.find! (c => c >= lo);
if (!p.empty) {
return false;
}
if (!rawRead ()) {
return true;
}
}
}
template next(T) if (isSigned!T) {
T next () {
if (seekByte (45)) {
return 0;
}
T res;
ubyte b = nextByte!false ();
if (b == 45) {
while (true) {
b = nextByte!true ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 - (b - 48);
}
} else {
res = b - 48;
while (true) {
b = nextByte!true ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 + (b - 48);
}
}
}
}
template next(T) if (isUnsigned!T) {
T next () {
if (seekByte (48)) {
return 0;
}
T res = nextByte!false () - 48;
while (true) {
ubyte b = nextByte!true ();
if (b < 48 || b >= 58) {
break;
}
res = res * 10 + (b - 48);
}
return res;
}
}
T[] nextA(T) (in int n) {
auto a = uninitializedArray!(T[]) (n);
foreach (i; 0 .. n) {
a[i] = next!T;
}
return a;
}
}
void main() {
auto r = new InputReader ();
immutable nt = r.next!uint ();
foreach (tid; 0 .. nt) {
const n = r.next!uint;
const long x = r.next!uint;
auto a = r.nextA!uint(n);
sort (a);
int res;
long s;
debug stderr.writeln (a);
foreach_reverse (z; a) {
s += z;
debug stderr.writefln ("s = %d, res = %d, x = %d", s, res, x);
if (s < x * (res+1)) break;
++res;
}
writeln (res);
}
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto cnt = new long[](26);
foreach (i; 0..n)
{
auto s = RD!string;
foreach (c; s)
{
auto num = c - 'a';
++cnt[num];
}
}
ans[ti] = true;
foreach (i; 0..26)
{
if (cnt[i] % n)
{
ans[ti] = false;
break;
}
}
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush;
debug readln;
}
| D |
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
// dfmt on
void main()
{
long N, M;
scan(N, M);
bool[long] A;
foreach (i; 0 .. M)
A[lread()] = true;
auto dp = new long[](N + 2);
dp[0] = 1;
foreach (i; 0 .. N)
{
if (i + 1 !in A)
dp[i + 1] = (dp[i + 1] + dp[i]) % MOD;
if (i + 2 !in A)
dp[i + 2] = (dp[i + 2] + dp[i]) % MOD;
}
// writeln(dp);
writeln(dp[N]);
}
| D |
import std.stdio, std.algorithm, std.range, std.array, std.conv, std.string, std.math, std.container, std.typecons;
void main() {
auto a = readln.chomp.to!int;
auto b = readln.chomp.to!int;
auto c = readln.chomp.to!int;
auto x = readln.chomp.to!int;
int cnt = 0;
foreach (i; 0..min(x/500,a)+1) {
foreach (j; 0..min((x-500*i)/100,b)+1) {
auto res = x-500*i-100*j;
if (res % 50 == 0 && res/50 <= c) {
cnt++;
}
}
}
writeln(cnt);
}
| 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()
{
char X, Y;
scan(X, Y);
long a = X - Y;
if (a == 0)
{
writeln("=");
return;
}
if (a < 0)
{
writeln("<");
}
else
{
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 A = RD;
auto B = RD;
auto C = RD;
writeln(max(0, C - (A - B)));
stdout.flush();
debug readln();
} | D |
// 10進数から26進数への基数変換と同様に考えれば良い
// 但し, 番号は1から始まるので注意
void main(){
long n = _scan!long();
string ans;
while(n>0){
int rem = (n%26==0)?26:n%26;
ans ~= ( ('a'-1) + rem );
n = (n -1)/26;
}
ans.retro.array.to!string.writeln();
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range, std.array;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
int a, b;
foreach (c; readln.chomp) {
if (c == '0') {
++a;
} else {
++b;
}
}
writeln(min(a, b) * 2);
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nl = readln.split.to!(int[]);
auto N = nl[0];
auto L = nl[1];
int s, e = int.max;
foreach (i; 0..N) {
s += L+i;
if (abs(L+i) < abs(e)) e = L+i;
}
writeln(s - e);
} | D |
import std.stdio, std.conv, std.string, std.algorithm;
void main() {
uint n = readln.chomp.to!uint;
uint[] result;
foreach (i; 0..n) {
uint outNum = 0;
bool[3] fieldPlayer;
uint point = 0;
while (outNum < 3) {
auto str = readln.chomp;
if (str == "HIT") {
if (fieldPlayer[2]) point++;
fieldPlayer[2] = fieldPlayer[1];
fieldPlayer[1] = fieldPlayer[0];
fieldPlayer[0] = true;
} else if (str == "OUT") {
outNum++;
} else if (str == "HOMERUN"){
foreach (j; 0..3) {
if (fieldPlayer[j]) point++;
fieldPlayer[j] = false;
}
point++;
}
}
result ~= point;
}
foreach (r; result) r.writeln;
} | D |
import std;
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}
T[] lreads(T=long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}
void arywrite(T=long)(T[] ary){ary.map!(text).join(' ').writeln;}
void scan(TList...)(ref TList Args){auto line = readln.split;
foreach (i,T;TList){T val=line[i].to!(T);Args[i]=val;}}
void dprint(TList...)(TList Args){debug{auto s=new string[](TList.length);
static foreach(i,a;Args) s[i]=text(a);s.map!(text).join(' ').writeln;}}
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();
writeln(S == "ABC" ? "ARC" : "ABC");
}
| D |
void main() {
int d = readln.chomp.to!int;
string s = "Christmas";
string t = " Eve";
while (d < 25) {
s ~= t;
++d;
}
s.writeln;
}
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;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto n = readln.chomp.to!int;
auto as = readln.chomp.split.to!(int[]);
auto res = int.max;
foreach (a; as) {
int cnt;
while (!(a & 1)) {
a >>= 1;
cnt++;
}
res = min(res, cnt);
}
res.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 = 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!(char[]);
auto Q = RD;
char[] str1, str2;
bool dir = true;
foreach (i; 0..Q)
{
auto T = RD;
if (T == 1)
{
dir = !dir;
}
else
{
auto F = RD;
auto C = RD!string;
if ((dir && F == 1) || (!dir && F == 2))
str1 ~= C;
else
str2 ~= C;
}
}
if (dir)
{
str1.reverse();
writeln(str1 ~ S ~ str2);
}
else
{
str2.reverse();
S.reverse();
writeln(str2 ~ S ~ str1);
}
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 s; readV(s);
writeln("aeiou".canFind(s) ? "vowel" : "consonant");
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
int[26] CS;
void main()
{
auto S = readln.chomp.to!(char[]);
foreach (c; S) ++CS[c-97];
foreach (i, n; CS) {
if (!n) {
writeln(S ~ cast(char)(i+97));
return;
}
}
foreach_reverse (i, c; S) {
while (c < 'z') {
++c;
bool found;
foreach (d; S[0..i]) {
if (d == cast(char)c) {
found = true;
break;
}
}
if (!found) {
writeln(S[0..i] ~ cast(char)c);
return;
}
}
}
writeln("-1");
} | D |
void main(){
int n = _scan();
(n/2 +n%2).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.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);
long t = 1900 * M + 100 * (N - M);
if (M == 0)
{
writeln(t);
return;
}
long p = 1 << M;
writeln(t * p);
// long ans;
// long i = 1;
// while (true)
// {
// long tmp = (i * t + ((p ^^ i) - 1)) / (p ^^ i);
// writeln(tmp);
// if (tmp == 0)
// {
// writeln(ans);
// return;
// }
// ans += tmp;
// i++;
// }
}
| 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);
auto m = n, s = 0;
while (m > 0) {
s += m%10;
m /= 10;
}
writeln(n%s == 0 ? "Yes" : "No");
}
| D |
import std.stdio;
import std.string;
import std.array;
import std.range;
import std.algorithm;
import std.conv;
void main(string[] args) {
readln();
string input = readln().chomp;
int cur = 0;
int min = 0;
int e = 0;
foreach(c; input){
if(c == 'E'){e++; cur--;}
else{cur++;}
if(cur < min){min = cur;}
}
writeln(min+e);
} | 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
ASSA
---+/
/+---test
ASSS
---+/
void main(string[] args) {
const S = readln.chomp.to!string;
size_t[char] x;
foreach (c; S) {
if (c !in x) x[c] = 0;
++x[c];
}
if (x.length == 2 && x.values[0] == x.values[1]) {
"Yes".writeln;
} else {
"No".writeln;
}
}
| D |
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "damage", long, "cost");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
auto s = sread();
foreach (_; iota(s.length))
{
write('x');
}
writeln();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
| D |
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
ulong MOD = 1_000_000_007;
ulong INF = 1_000_000_000_000;
alias Goods = Tuple!(long, "w", long, "v");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
auto s = sread();
auto t = sread();
long m = s.length, n = t.length;
auto lcs = new long[][](m + 1, n + 1);
foreach (i; iota(m))
{
foreach (j; iota(n))
{
if (s[i] == t[j])
lcs[i + 1][j + 1] = lcs[i][j] + 1;
else
lcs[i + 1][j + 1] = max(lcs[i][j + 1], lcs[i + 1][j]);
}
}
lcs.traceback(t, m, n).writeln();
}
auto traceback(T)(T lcs, string s, long m, long n)
{
auto str = new char[](0);
while (m > 0 && n > 0)
{
if(lcs[m][n] == lcs[m][n - 1])
n--;
else if(lcs[m][n] == lcs[m - 1][n])
m--;
else
{
m--, n--;
str ~= s[n];
}
}
str.reverse();
return str;
}
| D |
import std.stdio;
import std.algorithm;
import std.string;
import std.conv;
void main() {
auto N = to!int(readln.chomp);
foreach (i; 0..N) {
auto input = readln.split.map!(to!int);
if (input[2] >= 5 && input[3] >= 2) {
writeln((input[0] * input[2] + input[1] * input[3]) * 4 / 5);
} else if (input[2] >= 5) {
int a = input[0] * input[2] + input[1] * input[3];
int b = (input[0] * input[2] + input[1] * 2) * 4 / 5;
writeln(min(a, b));
} else if (input[3] >= 2) {
int a = input[0] * input[2] + input[1] * input[3];
int b = (input[0] * 5 + input[1] * input[3]) * 4 / 5;
writeln(min(a, b));
} else {
int a = input[0] * input[2] + input[1] * input[3];
int b = (input[0] * 5 + input[1] * 2) * 4 / 5;
writeln(min(a, b));
}
}
} | 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();
if (S.length & 1)
{
writeln("No");
return;
}
foreach (i; 0 .. S.length)
{
if (S[i] != "hi"[i & 1])
{
writeln("No");
return;
}
}
writeln("Yes");
}
| 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.datetime;
void main()
{
auto path = new int[][](5, 5);
auto key = [' ', 'U', 'L', ' ', 'D', ' ', ' ', ' ', 'R'];
auto dx = [0, 0, -1, 0, 0, 0, 0, 0, 1];
auto dy = [0, -1, 0, 0, 1, 0, 0, 0, 0];
foreach (i; 0..5) {
auto x = readln.chomp;
foreach (j, e; x) {
if (e == '1') {
path[i][j] |= 8;
path[i][j+1] |= 2;
}
}
if (i == 4) continue;
auto y = readln.chomp;
foreach (j, e; y) {
if (e == '1') {
path[i][j] |= 4;
path[i+1][j] |= 1;
}
}
}
int x = 1,
y = 0,
dir = 8;
write("R");
while (x != 0 || y != 0) {
auto d = dir << 1;
if (d == 16) d = 1;
if (path[y][x] & d) {
x += dx[d];
y += dy[d];
write(key[d]);
dir = d;
continue;
}
d = dir;
if (path[y][x] & d) {
x += dx[d];
y += dy[d];
write(key[d]);
continue;
}
d = dir >> 1;
if (d == 0) d = 8;
if (path[y][x] & d) {
x += dx[d];
y += dy[d];
write(key[d]);
dir = d;
continue;
}
d = d >> 1;
if (d == 0) d = 8;
x += dx[d];
y += dy[d];
write(key[d]);
dir = d;
}
writeln("");
} | D |
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "l ", long, "r");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
long h, w;
scan(h, w);
auto mat = new string[](h);
foreach (i; iota(h))
{
mat[i] = sread();
}
auto h_skip = new bool[](h), w_skip = new bool[](w);
h_skip[] = true;
w_skip[] = true;
foreach (i; iota(h))
{
foreach (j; iota(w))
{
h_skip[i] &= (mat[i][j] == '.');
w_skip[j] &= (mat[i][j] == '.');
}
}
foreach (i; iota(h))
{
if (h_skip[i])
continue;
foreach (j; iota(w))
{
if (w_skip[j])
continue;
mat[i][j].write();
}
writeln();
}
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
| D |
import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;
void main(){
string s = readln.chomp;
string ans;
if(s.length % 2){
if(s[0] == s[$ - 1]) ans = "Second"; // like "aba"
else ans = "First"; // like "acb"
}
else{
if(s[0] == s[$ - 1]) ans = "First"; // like "acba"
else ans = "Second"; // like "acab"
}
ans.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;
foreach (i; 0..10) {
foreach (j; i..10) {
if (N == i*j) {
writeln("Yes");
return;
}
}
}
writeln("No");
} | D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
auto s = readln.split;
writeln(s[0][$-1] == s[1][0] && s[1][$-1] == s[2][0] ? "YES" : "NO");
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
| D |
import std;
void main() {
auto l = readln();
writeln(l.canFind('7') ? "Yes" : "No");
} | D |
import std.stdio, std.string, std.algorithm, std.functional;
void main() {
readln.chomp.map!(a => a=='p' ? -1:1).sum.pipe!"a/2".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 W = RD;
auto H = RD;
auto N = RD;
auto xya = new long[3][](N);
foreach (i; 0..N)
{
xya[i] = [RD, RD, RD];
}
long l, r = W, t, b = H;
foreach (i; 0..N)
{
if (xya[i][2] == 1)
l = max(l, xya[i][0]);
else if (xya[i][2] == 2)
r = min(r, xya[i][0]);
else if (xya[i][2] == 3)
t = max(t, xya[i][1]);
else
b = min(b, xya[i][1]);
}
writeln(max(r - l, 0) * max(b - t, 0));
stdout.flush();
debug readln();
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto K = RD;
long ans = 1;
foreach (i; 0..N)
{
if (ans <= K)
ans *= 2;
else
ans += K;
}
writeln(ans);
stdout.flush();
debug readln();
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
void main()
{
auto N = readln.chomp.to!int;
auto n = N;
int s;
while (n) {
s += n % 10;
n /= 10;
}
writeln(N % s ? "No" : "Yes");
} | D |
import std.stdio;
import std.conv;
import std.string;
void main() {
string[] input = split(readln());
int a = to!int(input[0])-1;
int b = to!int(input[1]);
int ans = 0;
int tap = 1;
while (tap < b) {
tap += a;
ans++;
}
writeln(ans);
} | 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()
{
dchar[] s; readV(s);
writeln(s.uniq.walkLength-1);
}
| D |
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
void main() {
auto I = readln.split.to!(long[]);
auto A = I[0];
auto B = I[1];
long solve() {
foreach(i; 0..30000) {
auto cp8 = (i * 108) / 100 - i;
auto cp10 = (i * 110) / 100 - i;
if (cp8 == A && cp10 == B) return i;
}
return -1;
}
solve().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 c1 = RD!(char[]);
auto c2 = RD!(char[]);
c2.reverse();
bool ans = true;
foreach (i; 0..c1.length)
{
if (c1[i] != c2[i])
ans = false;
}
writeln(ans ? "YES" : "NO");
stdout.flush();
debug readln();
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto A = RD;
auto B = RD;
auto X = RD;
writeln(A <= X && A + B >= X ? "YES" : "NO");
stdout.flush();
debug readln();
}
| D |
import std.stdio;
import std.conv;
import std.algorithm;
import std.range;
import std.string;
import std.typecons;
void main()
{
auto a = readln().strip;
auto b = readln().strip;
size_t sub = 0;
while (true)
{
sub = a.indexOf(b, sub);
if (sub == cast(size_t)-1)
{
break;
}
else
{
writeln(sub);
sub++;
}
}
}
| D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.math;
void main() {
int x = readln.chomp.to!(int);
int[1001] arr;
for (int i = 1; i <= x; i++) {
for (int j = 2; j <= 100; j++) {
int t = pow(i, j);
if (t <= x) {
arr[i] = max(arr[i], t);
}
else {
break;
}
}
arr[i] = max(arr[i - 1], arr[i]);
}
writeln(arr[x]);
} | D |
import std.algorithm;
import std.stdio;
import std.string;
void main() {
bool[string] cups = ["A": true, "B": false, "C": false];
foreach (string line; stdin.lines) {
string[] cup = line.chomp.split(",");
swap(cups[cup[0]],cups[cup[1]]);
}
foreach (k; cups.keys) {
if (cups[k] == true) {
k.writeln;
break;
}
}
} | D |
/+ dub.sdl:
name "C"
dependency "dcomp" version=">=0.7.4"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
int main() {
Scanner sc = new Scanner(stdin);
int n; int[] a;
sc.read(n);
sc.read(a);
bool[] ok = new bool[1<<24];
ok[1] = true;
foreach (d; a) {
bool[] ok2 = new bool[1<<24];
foreach (i, f; ok) {
if (!f) continue;
int x = d;
if (!(i & (1<<x))) {
ok2[i | (1<<x)] = true;
}
x = (24-d) % 24;
if (!(i & (1<<x))) {
ok2[i | (1<<x)] = true;
}
}
ok = ok2;
}
int ans = 0;
foreach (i, f; ok) {
if (!f) continue;
// writeln("debug ", i);
int buf = 10000;
foreach (x; 0..24) {
foreach (y; x+1..24) {
if (!(i & (1<<x))) continue;
if (!(i & (1<<y))) continue;
buf = min(buf, y-x);
buf = min(buf, (x-y + 24) % 24);
}
}
ans = max(ans, buf);
}
writeln(ans);
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, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
if (N == 1) {
writeln("Hello World");
} else {
auto A = readln.chomp.to!int;
auto B = readln.chomp.to!int;
writeln(A + B);
}
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto S = readln.chomp;
auto len = S.length;
if (len%2 == 1) {
writeln("No");
return;
}
foreach (i; 0..len/2) {
auto c = S[len-i-1];
switch (S[i]) {
case 'b':
if (c != 'd') goto default;
break;
case 'd':
if (c != 'b') goto default;
break;
case 'p':
if (c != 'q') goto default;
break;
case 'q':
if (c != 'p') goto default;
break;
default:
writeln("No");
return;
}
}
writeln("Yes");
} | D |
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
void times(alias fun)(int n) {
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
// fold was added in D 2.071.0.
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
return reduce!fun(tuple(seed), r);
}
}
}
void main() {
readln.chomp.to!int.pipe!(N => N*(N+1)/2).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[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
// dfmt on
void main()
{
long N = lread();
auto A = new long[](N);
auto B = new long[](N);
foreach (i; 0 .. N)
scan(A[i], B[i]);
long ans;
foreach_reverse (i; 0 .. N)
{
long m = (A[i] + ans) % B[i];
if (m == 0)
continue;
long d = B[i] - m;
ans += d;
}
writeln(ans);
}
| D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.math;
long n, m;
void main() {
scan(n, m);
auto cmb = new long[][](n + 1, n + 1);
auto dp = new long[][](n + 1, n + 1);
foreach (i ; 0 .. n + 1) {
cmb[i][0] = cmb[i][i] = 1;
dp[i][0] = dp[i][i] = 1;
}
foreach (i ; 0 .. n + 1) {
foreach (j ; 1 .. i) {
cmb[i][j] = (cmb[i-1][j] + cmb[i-1][j-1]);
if (cmb[i][j] >= m) cmb[i][j] -= m;
dp[i][j] = (dp[i-1][j-1] + (j + 1) * dp[i-1][j] % m);
if (dp[i][j] >= m) dp[i][j] -= m;
}
}
debug {
//writeln(cmb);
//writeln(dp);
}
auto tpm = new long[](n*n + 1);
auto tpm1 = new long[](n*n + 1);
tpm[0] = tpm1[0] = 1;
foreach (i ; 1 .. n*n + 1) {
tpm[i] = tpm[i-1] * 2 % m;
tpm1[i] = tpm1[i-1] * 2 % (m-1);
}
long f(int k) {
long res;
foreach (x ; 0 .. k + 1) {
//res += powmod(2, powmod(2, n - k, m - 1), m) * dp[k][x] % m * powmod(2, (n - k) * x, m) % m;
res += powmod(2, tpm1[n - k], m) * dp[k][x] % m * tpm[(n-k)*x] % m;
if (res >= m) res -= m;
}
return res;
}
long ans;
foreach (int k ; 0 .. n.to!int + 1) {
if (k & 1) {
ans -= cmb[n][k] * f(k) % m;
if (ans < 0) ans += m;
}
else {
ans += cmb[n][k] * f(k) % m;
if (ans >= m) ans -= m;
}
}
writeln(ans);
}
// x^y % mod
long powmod(long x, long y, long mod) {
return y > 0 ? powmod(x, y>>1, mod)^^2 % mod * x^^(y&1) % mod : 1L;
}
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, std.container, std.range;
void main()
{
auto S = readln.chomp;
writeln(S[$-1] == 's' ? S ~ "es" : S ~ "s");
} | D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main() {
int[] buf = readln.chomp.split.to!(int[]);
int n = buf[0], k = buf[1];
int[] arr = readln.chomp.split.to!(int[]);
int ans = 0;
n -= 1;
while (n > 0) {
n -= k - 1;
ans++;
}
writeln(ans);
} | D |
import std.stdio;
import std.string;
import std.conv;
import std.array;
import std.algorithm;
import std.range;
void main(){
auto N=readln.chomp.to!int;
if(N/10==9||N%10==9)writeln("Yes");
else writeln("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 main()
{
long n, k; readV(n, k);
auto x = n/k, y = 0L;
if (k%2 == 0) y = x + (n%k >= k/2 ? 1 : 0);
writeln(x^^3+y^^3);
}
| D |
import std.stdio, std.string, std.array, std.conv;
long modexp(long x, long n) {
if (n == 0) return 1;
long mod = 1_000_000_007;
long result = modexp(x, n/2);
result = result * result % mod;
if (n % 2 == 1) result = result * x % mod;
return result;
}
void main() {
long[] tmp = readln.chomp.split.to!(long[]);
long m = tmp[0], n = tmp[1];
modexp(m, n).writeln;
}
| D |
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto Q = s[1];
auto st = new LazySegmentTree!(long, long, (a,b)=>a+b, (a,b)=>a+b, (a,b)=>a+b, (a,b)=>a*b, 0L, 0L)(N+1);
while (Q--) {
s = readln.split.map!(to!int);
int q = s[0];
int l = s[1];
int r = s[2];
if (q == 0) {
long v = s[3];
st.update(l, r, v);
} else {
st.query(l, r).writeln;
}
}
}
class LazySegmentTree(T, L, alias opTT, alias opTL, alias opLL, alias opPrd, L eL, T eT) {
T[] table;
L[] lazy_;
int n;
int size;
this(int n) {
this.n = n;
size = 1;
while (size <= n) size <<= 1;
size <<= 1;
table = new T[](size);
lazy_ = new T[](size);
table[] = eT;
lazy_[] = eL;
}
void push(int i, int a, int b) {
if (lazy_[i] == eL) return;
table[i] = opTL(table[i], opPrd(lazy_[i], b - a + 1));
if (i * 2 + 1 < size) {
lazy_[i*2] = opLL(lazy_[i*2], lazy_[i]);
lazy_[i*2+1] = opLL(lazy_[i*2+1], lazy_[i]);
}
lazy_[i] = eL;
}
T query(int l, int r) {
if (l > r) return eT;
return query(l, r, 1, 0, n-1);
}
T query(int l, int r, int i, int a, int b) {
if (b < l || r < a) return eT;
push(i, a, b);
if (l <= a && b <= r) {
return table[i];
} else {
return opTT(query(l, r, i*2, a, (a+b)/2), query(l, r, i*2+1, (a+b)/2+1, b));
}
}
void update(int l, int r, L val) {
if (l > r) return;
update(l, r, 1, 0, n-1, val);
}
void update(int l, int r, int i, int a, int b, L val) {
if (b < l || r < a) {
push(i, a, b);
} else if (l <= a && b <= r) {
lazy_[i] = opLL(lazy_[i], val);
push(i, a, b);
} else {
push(i, a, b);
update(l, r, i*2, a, (a+b)/2, val);
update(l, r, i*2+1, (a+b)/2+1, b, val);
table[i] = opTT(table[i*2], table[i*2+1]);
}
}
}
| D |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.