code
stringlengths 4
1.01M
| language
stringclasses 2
values |
---|---|
import std.stdio;
import std.string;
void main() {
auto ar = readln.chomp.split(" ");
auto a = ar[0];
auto b = ar[1];
auto c = ar[2];
if (a == b && b == c) {
writeln("No");
return;
}
if (a != b && a != c && b != c) {
writeln("No");
return;
}
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)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto T = RD!int;
auto ans = new char[][](T);
foreach (ti; 0..T)
{
auto n = RD!int;
auto s = RD!string;
ans[ti].length = n;
int pos;
foreach (i; 0..n)
{
if (s[i] == '?') continue;
pos = i;
break;
}
foreach (i; pos..n)
{
if (s[i] != '?')
ans[ti][i] = s[i];
else if (i == 0)
{
ans[ti][i] = 'R';
}
else
{
if (ans[ti][i-1] == 'R')
ans[ti][i] = 'B';
else
ans[ti][i] = 'R';
}
}
foreach_reverse (i; 0..pos)
{
if (s[i] != '?')
ans[ti][i] = s[i];
else
{
if (ans[ti][i+1] == 'R')
ans[ti][i] = 'B';
else
ans[ti][i] = 'R';
}
}
}
foreach (e; ans)
writeln(e);
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.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto A = RD;
auto B = RD;
auto C = RD;
auto K = RD;
auto c1 = min(A, K);
K -= c1;
auto ans = c1;
auto c2 = min(B, K);
K -= c2;
auto c3 = min(C, K);
ans -= c3;
writeln(ans);
stdout.flush;
debug readln;
} | D |
import std.stdio, std.string, std.conv;
import std.array, std.algorithm, std.range;
void main()
{
foreach(s;stdin.byLine())
{
int[] a;
foreach(c;s) a~=c-'0';
foreach_reverse(i;1..a.length)
foreach(j;0..i) a[j]=(a[j]+a[j+1])%10;
writeln(a[0]);
}
} | D |
import std.stdio, std.string, std.conv, std.range, std.algorithm;
void main() {
auto s = readln.chomp;
char[] line;
foreach (c; s) {
if (c == 'B' && line.length > 0) {
line = line.dropBackOne;
} else if (c == '0' || c == '1') {
line ~= c;
}
}
line.writeln;
}
| D |
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
immutable int mod = 998_244_353;
void main ()
{
string s;
while ((s = readln.strip ()) != "")
{
auto t = readln.strip;
auto n = s.length.to !(int);
auto m = t.length.to !(int);
auto f = new int [] [] (n + 1, n + 1);
foreach (i; 0..n + 1)
{
f[i][i] = 1;
}
foreach (len; 0..n)
{
auto letter = s[len];
foreach (start; 0..n + 1 - len)
{
auto finish = start + len;
auto cur = f[start][finish];
if (start > 0 &&
(start - 1 >= m || letter == t[start - 1]))
{
f[start - 1][finish] += cur;
if (f[start - 1][finish] >= mod)
{
f[start - 1][finish] -= mod;
}
}
if (finish < n &&
(finish >= m || letter == t[finish]))
{
f[start][finish + 1] += cur;
if (f[start][finish + 1] >= mod)
{
f[start][finish + 1] -= mod;
}
}
}
}
writeln (sum (f[0][m..$], 0L) % mod);
}
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto N = RD;
auto X = RD-1;
auto Y = RD-1;
auto d = Y - X - 1;
foreach (i; 1..N)
{
long ans;
foreach (j; 0..N-1)
{
if (abs(X-j) < i)
{
auto remain = i - abs(X-j) - 1;
auto p1 = j+i;
auto p2 = Y-remain;
auto p3 = Y+remain;
if (remain == 0)
{
if (p2 > p1)
ans += 2;
else if (p1 < N)
++ans;
}
else
{
if (p1 >= p3)
{
if (p1 < N)
++ans;
}
else
{
if (p3 < N)
++ans;
if (p2 > p1)
ans += 2;
else if (p2 == p1)
++ans;
}
}
}
else if (j+i < N)
++ans;
debug writeln("i:", i, " j:", j, " ans:", ans);
}
writeln(ans);
}
stdout.flush;
debug readln;
}
| D |
module main;
import core.stdc.stdio;
int main(string[] argv)
{
int n,l,tmp;
scanf("%d", &n);
int [] a = new int[n+5];
for(int i = 0; i<n; i++)
scanf("%d", &a[i]);
for(int i=0;i<n;i++)
{
if (i==0||a[i]==a[i-1])
l = i + 1;
else break;
}
tmp = l;
for(int i=0; i<n; i++)
{
if (i==0||a[i]!=a[i-1])
{
if (tmp!=l)
{
printf("NO");
return 0;
}
tmp=1;
}
else tmp++;
}
if (tmp==l)
printf("YES");
else
printf("NO");
return 0;
} | D |
void main() {
int[] tmp = readln.split.to!(int[]);
int a = tmp[0], b = tmp[1], c = tmp[2];
writeln(a + b >= c ? "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 |
/* 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
20
---+/
/+---test
2
---+/
/+---test
99992
---+/
void main(string[] args) {
const X = readln.chomp.to!long;
auto th = new long[](200000);
th[] = true;
th[0..2] = false;
foreach (i; 2..200000) {
for (long j = i*2; j < th.length; j += i) {
th[j] = false;
}
}
(X + th[X..$].countUntil(true)).writeln;
}
| D |
module main;
import core.stdc.stdio;
import std.algorithm;
int main(string[] argv)
{
int n, ans = 0;
scanf("%d", &n);
int[] t = new int[n];
for (int i = 0; i < n; ++i) {
int h, m;
scanf("%d:%d", &h, &m);
t[i] = 60*h+m;
}
if (n == 1) {
puts("23:59");
return 0;
}
t.sort();
for (int i = 1; i < n; ++i) {
int v = t[i]-t[i-1]-1;
if (v > ans) ans = v;
}
int tmp = 60*24+t[0]-t[n-1]-1;
if (tmp > ans) ans = tmp;
printf("%02d:%02d\n", ans/60, ans%60);
return 0;
} | D |
import core.bitop;
import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.format;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void main()
{
int n = readln.chomp.to!int;
int[int] cnt;
foreach (a; readln.chomp.split.map!(to!int)) {
cnt[a - 1]++;
cnt[a]++;
cnt[a + 1]++;
}
int ans = 0;
foreach (k, v; cnt) {
ans = max(ans, v);
}
ans.writeln;
}
| D |
import std.algorithm, std.array, std.container, std.range, std.bitmanip;
import std.numeric, std.math, std.bigint, std.random, core.bitop;
import std.string, std.ascii, std.regex, std.conv, std.stdio, std.typecons;
void main()
{
auto s = readln.chomp;
auto p = readln.chomp;
foreach (i; 0..s.length)
if ((s[i..$] ~ s[0..i]).indexOf(p) != -1) {
writeln("Yes");
return;
}
writeln("No");
} | D |
import std.stdio, std.string, std.conv, std.algorithm;
void main() {
string s = readln.chomp;
string odd, even;
foreach (i, x; s) {
if (i % 2) even ~= x;
else odd ~= x;
}
writeln(odd.count('L') || even.count('R') ? "No" : "Yes");
} | D |
import std.stdio, std.string, std.conv, std.range, std.algorithm, std.functional;
void main() {
readln.split.to!(int[]).pipe!(input => writeln(min(input[0] * input[1], input[2])));
}
| D |
import std.stdio;
import std.array;
import std.conv;
void main() {
string[] input=split(readln());
int N=to!int(input[0]);
int A=to!int(input[1]);
int B=to!int(input[2]);
int ans=0;
for(int i=1;i<=N;i++) {
int sum=0;
int j=i;
while(j>0) sum+=j%10,j/=10;
if(A<=sum&&sum<=B) ans+=i;
}
writeln(ans);
}
| 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(){
auto ab=readln.split.map!(to!int).map!"a==1?999:a".array;
if(ab[0]==ab[1]) writeln("Draw");
else if(ab[0]<ab[1]) writeln("Bob");
else writeln("Alice");
} | D |
void main() {
int n = readln.chomp.to!int;
int[] p = new int[n];
foreach (i; 0 .. n) {
p[i] = readln.chomp.to!int;
}
writeln(p.sum - p.reduce!max / 2);
}
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.string, std.math, std.algorithm;
void main(){
auto ip = readln.split.to!(int[]);
if(ip[0] + ip[1] < 24) (ip[0] + ip[1]).writeln;
else (ip[0] + ip[1] - 24).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 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; }
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 edges = new long[2][][](N);
foreach (i; 0..N-1)
{
auto a = RD-1;
auto b = RD-1;
edges[a] ~= [b, i];
edges[b] ~= [a, i];
}
auto ans = new long[](N-1);
long[3][] open = [[0, -1, 0]];
while (!open.empty)
{
auto n = open.front; open.popFront;
auto u = n[0];
auto last = n[1];
auto used = n[2];
long c = 1;
foreach (e; edges[u])
{
auto v = e[0];
auto i = e[1];
if (v == last) continue;
if (c == used) ++c;
ans[i] = c;
open ~= [v, u, c];
++c;
}
}
long x;
foreach (e; ans)
x = max(x, e);
writeln(x);
foreach (e; ans)
writeln(e);
stdout.flush();
debug readln();
} | D |
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container, std.typecons;
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; }
bool INSIDE(T)(T x, T b, T e) { return x >= b && x < e; }
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 M = RD!long;
auto cnt = new long[](M+1);
foreach (i; 0..N)
{
auto K = RD!long;
foreach (j; 0..K)
{
auto A = RD!long;
++cnt[A];
}
}
long ans;
foreach (e; cnt)
{
if (e == N) ++ans;
}
writeln(ans);
stdout.flush();
} | 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 A, B;
scan(A, B);
writeln(A * B);
}
| D |
import std.stdio;
import std.conv;
import std.algorithm;
import std.range;
import std.string;
void main() {
foreach (char[] line; stdin.lines) {
if (line == "-\n") break;
char[] s = line.chomp;
int i = readln.chomp.to!int;
for (int x = 0; x < i; x++) {
int n = readln.chomp.to!int;
s = s[n..$] ~ s[0..n];
}
s.writeln;
}
} | D |
import std.stdio, std.string, std.conv;
void main() {
auto x = readln.chomp.to!ulong;
auto ans = x / 11;
x -= ans * 11;
ans *= 2;
if (0 < x && x <= 6) ans += 1;
else if (x > 6) ans += 2;
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;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
string calc(string s, string t) {
bool match(int start) {
for (int i = 0; i < t.length; i++) {
if (s[start + i] == '?')
continue;
if (s[start + i] != t[i])
return false;
}
return true;
}
for (int i = cast(int)(s.length - t.length); i >= 0; i--) {
if (match(i)) {
auto cs = new char[s.length];
for (int j = 0; j < cs.length; j++) {
cs[j] = s[j] == '?' ? 'a' : s[j];
}
for (int j = 0; j < t.length; j++) {
cs[i + j] = t[j];
}
return to!string(cs);
}
}
return "UNRESTORABLE";
}
void main() {
string s = readln.chomp;
string t = readln.chomp;
auto ans = calc(s, t);
writeln(ans);
}
| D |
import std.stdio;
import std.string;
import std.conv;
void main() {
int n;
scanf("%d\n", &n);
auto s = readln.chomp;
auto cache = '\n';
size_t cnt;
foreach(c; s) {
if (c != cache) {
cache = c;
++cnt;
}
}
cnt.write;
}
| D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
import std.range;
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.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 a = scanElem;
(a*a-scanElem).writeln;
}
| D |
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int t;
scan(t);
while (t--) {
int a, b, c;
scan(a, b, c);
auto ans = solve(a, b, c);
writeln(ans);
}
}
int solve(int a, int b, int c) {
int res;
foreach (comb ; 0 .. 1 << (1 << 3)) {
auto x = new int[](3);
foreach (i ; 0 .. 1 << 3) if (comb & 1 << i) {
foreach (j ; 0 .. 3) if (i & 1 << j) {
x[j]++;
}
}
if (x[0] <= a && x[1] <= b && x[2] <= c) {
chmax(res, comb.popcnt);
}
}
res--;
return res;
}
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 |
/* 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
11
---+/
/+---test
136
---+/
/+---test
100000
---+/
void main(string[] args) {
const N = readln.chomp.to!long;
const M = N.log10.to!long;
long ans;
if (M%2 == 0) ans += N - 10.pow(M) + 1;
foreach (i; 0..M) {
if (i%2 == 0) {
ans += 10.pow(i+1) - 10.pow(i);
}
}
ans.writeln;
}
| D |
void main() {
problem();
}
void problem() {
auto a = scan;
long solve() {
long ans;
long count;
foreach(c; a) {
if (c == 'R') {
count++;
if (ans < count) ans = count;
} else {
count = 0;
}
}
return ans;
}
solve().writeln;
}
// ----------------------------------------------
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
// -----------------------------------------------
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto H = RD;
auto W = RD;
auto A = new string[](H);
foreach (i; 0..H)
{
A[i] = RD!string;
}
bool check(long y, long x)
{
long cnt_p, cnt_n;
if (y == 0 && x == 0) cnt_p = 1;
if (y == H-1 && x == W-1) cnt_n = 1;
if (y != 0)
{
if (A[y-1][x] == '#') ++cnt_p;
}
if (x != 0)
{
if (A[y][x-1] == '#') ++cnt_p;
}
if (y != H-1)
{
if (A[y+1][x] == '#') ++cnt_n;
}
if (x != W-1)
{
if (A[y][x+1] == '#') ++cnt_n;
}
return cnt_p == 1 && cnt_n == 1;
}
bool ans = true;
foreach (y; 0..H)
{
foreach (x; 0..W)
{
if (A[y][x] == '#')
{
auto r = check(y, x);
if (!r)
ans = false;
}
}
}
writeln(ans ? "Possible" : "Impossible");
stdout.flush();
debug readln();
}
| D |
import std.algorithm;
import std.array;
import std.bigint;
import std.bitmanip;
import std.conv;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; }
T[] readToArray(T)() {
return readln.split.to!(T[]);
}
void readInto(T...)(ref T ts) {
auto ss = readln.split;
foreach(ref t; ts) {
t = ss.front.to!(typeof(t));
ss.popFront;
}
}
// 冪乗をmod取りつつ計算
pure ulong modPow(ulong a, ulong n, ulong m) {
ulong r = 1;
while (n > 0) {
if(n % 2 != 0) r = r * a % m;
a = a * a % m;
n /= 2;
}
return r;
}
// フェルマーの小定理から乗法逆元を計算
// 定理の要請により法は素数
pure ulong modInv(ulong a, ulong m) {
return modPow(a, m-2, m);
}
// mod取りつつ組み合わせを計算
// modInvを使っているので法は素数
pure ulong modComb(ulong n, ulong r, ulong m) {
if (n == r) return 1;
if (r == 0) return 1;
if (n < r) return 0;
ulong up = 1;
ulong down = 1;
for(ulong i = n-r+1; i <= n; i++) {
up *= i;
up %= m;
}
for(ulong i = 2; i <= r; i++) {
down *= i;
down %= m;
}
return up*modInv(down, m) % m;
}
immutable ulong MOD = 1000000007;
void main() {
ulong n, k;
readInto(n, k);
for (ulong i = 1; i <= k; i++) {
writeln(modComb(n-k+1, i, MOD)*modComb(k-1, i-1, MOD) % MOD);
}
}
| D |
import std.stdio, std.algorithm, std.range, std.conv;
void main(){
iota(1, 10).map!(a => iota(1, 10).map!(b => text(a, "x", b, "=", a*b)).join("\n")).join("\n").writeln;
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math;
void main()
{
auto ab = readln.split.to!(int[]);
writeln(ab[0] + ab[1] == 15 ? "+" : ab[0] * ab[1] == 15 ? "*" : "x");
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new char[][][](t);
foreach (ti; 0..t)
{
auto h = RD!int;
auto w = RD!int;
ans[ti].length = h;
foreach (i; 0..h)
{
ans[ti][i].length = w;
foreach (j; 0..w)
{
ans[ti][i][j] = '0';
}
}
foreach (i; 0..w)
{
if (i % 2 == 0)
{
ans[ti][0][i] = '1';
ans[ti][h-1][i] = '1';
}
}
foreach (i; 2..h-2)
{
if (i % 2 == 0)
{
ans[ti][i][0] = '1';
ans[ti][i][w-1] = '1';
}
}
}
foreach (e; ans)
{
foreach (ee; e)
writeln(ee);
}
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 X = readln.chomp.to!long;
long r;
r += X/500 * 1000;
X -= X/500 * 500;
r += X/5 * 5;
writeln(r);
} | D |
//aoj10018
import std.stdio;
import std.ascii;
void main()
{
char c;
dchar c1;
while(1)
{
scanf("%c", &c);
if(c == '\n') break;
if(isLower(c) != 0) c1 = toUpper(c);
else if(isUpper(c) != 0) c1 = toLower(c);
else c1 = c;
write(c1);
}
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;
long powmod(long a, long x, long m) {
long ret = 1;
while (x) {
if (x % 2) ret = ret * a % m;
a = a * a % m;
x /= 2;
}
return ret;
}
void main() {
immutable long MOD = 10^^9 + 7;
auto S = readln.chomp;
auto N = S.length.to!int;
long a = 0;
long b = 0;
long ans = 0;
char prev = 'x';
foreach (i; 0..N) {
if (S[i] == 'a' && prev == 'b') {
ans += b * (powmod(2, a, MOD) - 1) % MOD;
ans = (ans + MOD) % MOD;
a++;
b = 0;
}
else if (S[i] == 'a') {
a++;
}
else {
b++;
}
prev = S[i];
}
ans += b * (powmod(2, a, MOD) - 1) % MOD;
ans = (ans + MOD) % MOD;
ans.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;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T read(T)(){ return read.to!T; }
T[] read(T)(long n){ return n.iota.map!(_ => read!T).array; }
T[][] read(T)(long m, long n){ return m.iota.map!(_ => read!T(n)).array; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
/*
If you find s_i > s_(i + 1), swap them.
Othewise, NO.
*/
void solve(){
int n = read!int;
string s = readln.chomp;
int i1;
bool f = 0;
foreach(i; 0 .. n - 1){
if(s[i] > s[i + 1]){
i1 = i;
f = 1;
break;
}
}
if(f == 0) writeln("NO");
else writeln("YES"), writeln(i1 + 1, " ", i1 + 2);
}
| 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 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; }
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 q = RD!int;
auto ans = new bool[](q);
foreach (i; 0..q)
{
auto a = RD;
auto b = RD;
auto n = RD;
auto S = RD;
auto x = S / n;
S -= min(x, a) * n;
ans[i] = S <= b;
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush();
debug readln();
} | D |
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.container, std.math;
immutable int mod = 10^^9 + 7;
int x1, y1, x2, y2;
int x, y;
void main() {
readVariables(x1, y1, x2, y2);
readVariables(x, y);
if ((x2 - x1) % x || (y2 - y1) % y) {
writeln("NO");
return;
}
int k = abs(x2 - x1) / x;
int h = abs(y2 - y1) / y;
if ((k & 1) == (h & 1)) {
writeln("YES");
}
else {
writeln("NO");
}
}
void readVariables(T...)(ref T args) {
string[] 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, std.typecons, std.numeric;
void main()
{
writeln(readln.chomp.to!int^^2);
} | D |
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
import std.concurrency;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;
void main() {
string str = readln.chomp;
foreach(i; 0..str.length-1) {
if (str[i..i+2] == "AC") {
"Yes".writeln;
return;
}
}
"No".writeln;
}
// ----------------------------------------------
void scanln(Args...)(ref Args args) {
foreach(i, ref v; args) {
"%d".readf(&v);
(i==args.length-1 ? "\n" : " ").readf;
}
// ("%d".repeat(args.length).join(" ") ~ "\n").readf(args);
}
void times(alias fun)(int n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
return reduce!fun(tuple(seed), r);
}
}
}
}
// cumulativeFold was added in D 2.072.0
static if (__VERSION__ < 2072) {
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
}
// minElement/maxElement was added in D 2.072.0
static if (__VERSION__ < 2072) {
auto minElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto minimum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b < minimum) {
element = a;
minimum = b;
}
}
return element;
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto maximum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b > maximum) {
element = a;
maximum = b;
}
}
return element;
}
}
| D |
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int N;
scan(N);
auto a = readln.split.to!(int[]);
auto b = readln.split.to!(int[]);
long x, y;
foreach (i ; 0 .. N) {
if (a[i] >= b[i]) {
x += a[i] - b[i];
}
else {
y += (b[i] - a[i]) / 2;
}
}
yes(x <= y);
}
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 chie template :) {{{
static if (__VERSION__ < 2090) {
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons, std.format,
std.bitmanip, std.numeric;
} else {
import std;
}
// }}}
// nep.scanner {{{
class Scanner {
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) {
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next() {
if (idx < str.length) {
return str[idx++];
}
char[] s;
while (s.length == 0) {
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(size_t len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
void scan(T...)(ref T args) {
foreach (ref arg; args) {
arg = next!(typeof(arg));
}
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType)) {
str ~= s.to!(char[]).strip.split;
}
}
// }}}
// alias {{{
alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less);
alias MinHeap(T) = Heap!(T, "a > b");
// }}}
// memo {{{
/*
- ある値が見つかるかどうか
<https://dlang.org/phobos/std_algorithm_searching.html#canFind>
canFind(r, value); -> bool
- 条件に一致するやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#filter>
// 2で割り切れるやつ
filter!"a % 2 == 0"(r); -> Range
- 合計
<https://dlang.org/phobos/std_algorithm_iteration.html#sum>
sum(r);
- 累積和
<https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold>
// 今の要素に前の要素を足すタイプの一般的な累積和
// 累積和のrangeが帰ってくる(破壊的変更は行われない)
cumulativeFold!"a + b"(r, 0); -> Range
- rangeをarrayにしたいとき
array(r); -> Array
- 各要素に同じ処理をする
<https://dlang.org/phobos/std_algorithm_iteration.html#map>
// 各要素を2乗
map!"a * a"(r) -> Range
- ユニークなやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#uniq>
uniq(r) -> Range
- 順列を列挙する
<https://dlang.org/phobos/std_algorithm_iteration.html#permutations>
permutation(r) -> Range
- ある値で埋める
<https://dlang.org/phobos/std_algorithm_mutation.html#fill>
fill(r, val); -> void
- バイナリヒープ
<https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap>
// 昇順にするならこう(デフォは降順)
BinaryHeap!(Array!T, "a > b") heap;
heap.insert(val);
heap.front;
heap.removeFront();
- 浮動小数点の少数部の桁数設定
// 12桁
writefln("%.12f", val);
- 浮動小数点の誤差を考慮した比較
<https://dlang.org/phobos/std_math.html#.approxEqual>
approxEqual(1.0, 1.0099); -> true
- 小数点切り上げ
<https://dlang.org/phobos/std_math.html#.ceil>
ceil(123.4); -> 124
- 小数点切り捨て
<https://dlang.org/phobos/std_math.html#.floor>
floor(123.4) -> 123
- 小数点四捨五入
<https://dlang.org/phobos/std_math.html#.round>
round(4.5) -> 5
round(5.4) -> 5
*/
// }}}
void main() {
auto cin = new Scanner;
int[] x = cin.nextArray!int(5);
foreach (i; 1 .. 6) {
if (i != x[i - 1]) {
writeln(i);
break;
}
}
}
| D |
import std.stdio, std.conv, std.string, std.algorithm, std.math, std.array;
void main() {
string s = readln.chomp;
if(s[0]==s[1] && s[1]==s[2]) writeln("No");
else writeln("Yes");
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
string[100] WS;
bool[string] MEMO;
void main()
{
auto N = readln.chomp.to!int;
foreach (i; 0..N) {
WS[i] = readln.chomp;
}
char c = WS[0][0];
foreach (w; WS[0..N]) {
if (c != w[0] || w in MEMO) {
writeln("No");
return;
}
MEMO[w] = true;
c = w[$-1];
}
writeln("Yes");
} | D |
import std.stdio; // readln
import std.array; // split
import std.conv; // to
import std.typecons;
import std.range;
import std.algorithm;
void main(){
string s = readln();
int cnt = 0;
foreach (elem; s) {
if(elem == '1') cnt++;
}
writeln(cnt);
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new int[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto k = RD!int;
if (n % 2)
{
auto loop = n / 2;
auto x = (k-1) / loop;
ans[ti] = (k-1+x) % n + 1;
}
else
{
ans[ti] = (k-1) % n + 1;
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
} | 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;
void main()
{
while (1) {
auto x = readln.chomp.to!int;
if (!x) break;
int cnt;
while (x != 1) {
if (x % 2) x = x * 3 + 1;
else x /= 2;
cnt++;
}
cnt.writeln;
}
} | D |
import std.stdio;
import std.string;
import std.conv;
void main(){
int i = 1;
while(1){
int a = readln().chomp().to!int();
if(a == 0)
break;
writeln("Case ", i, ": ", a);
i++;
}
} | 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[]);
writeln(max(ab[0] + ab[1], ab[0] - ab[1], ab[0] * ab[1]));
} | D |
import std.stdio, std.math;
void main(){
int n, m;
immutable mod = cast(long)(1e9 + 7);
scanf("%d%d", &n, &m);
auto Cs = new int[][](m,2), idxToC = new int[][](n+1, 2);
auto appear = new int[](n+1);
foreach(i; 0..m){
scanf("%d%d", &Cs[i][0], &Cs[i][1]);
foreach(j; 0..2){
int x = abs(Cs[i][j]);
idxToC[x][++appear[x]-1] = i;
}
}
int getAnother(int x, int c){
return idxToC[x][idxToC[x][0] == c ? 1 : 0];
}
long ans = 1;
auto visited = new bool[](m);
//single
foreach(i; 0..m)if(!visited[i]){
if( abs(Cs[i][0]) == abs(Cs[i][1]) ){
visited[i] = true;
ans = ans * (Cs[i][0] == Cs[i][1] ? 1 : 2) % mod;
}
}
void calcDp(int len, long[][] dp, bool[] same){
foreach(i; 0..len){
dp[i+1][0] = dp[i][same[i] ? 1 : 0];
dp[i+1][1] = (dp[i][0] + dp[i][1]) % mod;
}
}
//line
foreach(i; 1..n+1)if(appear[i] == 1){
int idx = idxToC[i][0], x = i, y = Cs[idx][abs(Cs[idx][0]) == x ? 1 : 0];
if(visited[idx]) continue;
visited[idx] = true;
bool[] same;
for(;appear[abs(y)] == 2;){
int nextIdx = getAnother(abs(y), idx);
int which = abs(Cs[nextIdx][0]) == abs(y) ? 0 : 1;
int nx = Cs[nextIdx][which], ny = Cs[nextIdx][which^1];
same ~= y == nx;
visited[nextIdx] = true;
x = nx; y = ny; idx = nextIdx;
}
auto dp = new long[][](same.length+1, 2);
dp[0][0] = 1;
dp[0][1] = 2;
calcDp(cast(int)same.length, dp, same);
ans = ans * (dp[$-1][0] + dp[$-1][1]) % mod;
}
//cycle
foreach(i; 0..m)if(!visited[i]){
int idx = i, x = Cs[i][0], y = Cs[i][1];
bool[] same;
visited[idx] = true;
for(;;){
int nextIdx = getAnother(abs(y), idx);
int which = abs(Cs[nextIdx][0]) == abs(y) ? 0 : 1;
int nx = Cs[nextIdx][which], ny = Cs[nextIdx][which^1];
same ~= y == nx;
if(visited[nextIdx]){
break;
}
visited[nextIdx] = true;
x = nx; y = ny; idx = nextIdx;
}
long tmp = 0;
auto dp = new long[][](same.length, 2);
dp[0][0] = dp[0][1] = 1;
calcDp(cast(int)same.length-1, dp, same);
tmp += dp[$-1][same[$-1] ? 1 : 0];
dp[0][0] = 0; dp[0][1] = 1;
calcDp(cast(int)same.length-1, dp, same);
tmp += dp[$-1][same[$-1] ? 0 : 1];
ans = ans * tmp % mod;
}
writeln(ans);
} | D |
//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.typecons;
class InputReader {
private:
ubyte[] p;
ubyte[] buffer;
size_t cur;
public:
this () {
buffer = uninitializedArray!(ubyte[])(16<<20);
p = stdin.rawRead (buffer);
}
final ubyte skipByte (ubyte lo) {
while (true) {
auto a = p[cur .. $];
auto r = a.find! (c => c >= lo);
if (!r.empty) {
cur += a.length - r.length;
return p[cur++];
}
p = stdin.rawRead (buffer);
cur = 0;
if (p.empty) return 0;
}
}
final ubyte nextByte () {
if (cur < p.length) {
return p[cur++];
}
p = stdin.rawRead (buffer);
if (p.empty) return 0;
cur = 1;
return p[0];
}
template next(T) if (isSigned!T) {
final T next () {
T res;
ubyte b = skipByte (45);
if (b == 45) {
while (true) {
b = nextByte ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 - (b - 48);
}
} else {
res = b - 48;
while (true) {
b = nextByte ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 + (b - 48);
}
}
}
}
template next(T) if (isUnsigned!T) {
final T next () {
T res = skipByte (48) - 48;
while (true) {
ubyte b = nextByte ();
if (b < 48 || b >= 58) {
break;
}
res = res * 10 + (b - 48);
}
return res;
}
}
final T[] nextA(T) (int n) {
auto a = uninitializedArray!(T[]) (n);
foreach (i; 0 .. n) {
a[i] = next!T;
}
return a;
}
}
void main() {
auto r = new InputReader;
immutable n = r.next!int;
immutable m = r.next!int;
immutable ta = r.next!long;
immutable tb = r.next!long;
immutable k = r.next!int;
auto a = r.nextA!long(n).map!(t => t + ta).array;
auto b = r.nextA!long(m);
auto e = b.assumeSorted;
long test () {
if (a.length <= k || b.length <= k) {
return -1L;
}
long res = long.min;
foreach (i; 0 .. k + 1) {
int d = k - i;
auto r = e.upperBound (a[i] - 1);
if (r.length <= d) {
return -1;
}
res = max (res, r.drop(d).front);
}
return res + tb;
}
writeln (test ());
}
| 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 L = RD;
long ans, best = long.max;
foreach (i; 0..N)
{
if (abs(L+i) < abs(best))
best = L+i;
ans += L+i;
}
writeln(ans - best);
stdout.flush();
debug readln();
} | D |
// import chie template :) {{{
static if (__VERSION__ < 2090) {
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons, std.format,
std.bitmanip, std.numeric;
} else {
import std;
}
// }}}
// nep.scanner {{{
class Scanner {
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) {
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next() {
if (idx < str.length) {
return str[idx++];
}
char[] s;
while (s.length == 0) {
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(size_t len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
void scan(T...)(ref T args) {
foreach (ref arg; args) {
arg = next!(typeof(arg));
}
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType)) {
str ~= s.to!(char[]).strip.split;
}
}
// }}}
// alias {{{
alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less);
alias MinHeap(T) = Heap!(T, "a > b");
// }}}
// memo {{{
/*
- ある値が見つかるかどうか
<https://dlang.org/phobos/std_algorithm_searching.html#canFind>
canFind(r, value); -> bool
- 条件に一致するやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#filter>
// 2で割り切れるやつ
filter!"a % 2 == 0"(r); -> Range
- 合計
<https://dlang.org/phobos/std_algorithm_iteration.html#sum>
sum(r);
- 累積和
<https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold>
// 今の要素に前の要素を足すタイプの一般的な累積和
// 累積和のrangeが帰ってくる(破壊的変更は行われない)
cumulativeFold!"a + b"(r, 0); -> Range
- rangeをarrayにしたいとき
array(r); -> Array
- 各要素に同じ処理をする
<https://dlang.org/phobos/std_algorithm_iteration.html#map>
// 各要素を2乗
map!"a * a"(r) -> Range
- ユニークなやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#uniq>
uniq(r) -> Range
- 順列を列挙する
<https://dlang.org/phobos/std_algorithm_iteration.html#permutations>
permutation(r) -> Range
- ある値で埋める
<https://dlang.org/phobos/std_algorithm_mutation.html#fill>
fill(r, val); -> void
- バイナリヒープ
<https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap>
// 昇順にするならこう(デフォは降順)
BinaryHeap!(Array!T, "a > b") heap;
heap.insert(val);
heap.front;
heap.removeFront();
- 浮動小数点の少数部の桁数設定
// 12桁
writefln("%.12f", val);
- 浮動小数点の誤差を考慮した比較
<https://dlang.org/phobos/std_math.html#.approxEqual>
approxEqual(1.0, 1.0099); -> true
- 小数点切り上げ
<https://dlang.org/phobos/std_math.html#.ceil>
ceil(123.4); -> 124
- 小数点切り捨て
<https://dlang.org/phobos/std_math.html#.floor>
floor(123.4) -> 123
- 小数点四捨五入
<https://dlang.org/phobos/std_math.html#.round>
round(4.5) -> 5
round(5.4) -> 5
*/
// }}}
void main() {
auto cin = new Scanner;
string s;
cin.scan(s);
if (s == "ABC") writeln("ARC");
else writeln("ABC");
}
| D |
import std.array, std.conv, std.stdio, std.string;
void main()
{
auto buf = readln.chomp.split.to!(int[]);
((buf[0] * 500 >= buf[1]) ? "Yes" : "No").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;
void main() {
auto S = readln.chomp;
auto N = S.length.to!int;
auto next = new int[][](26, N);
foreach (i; 0..26) fill(next[i], -1);
foreach (c; 0..26) {
for (int i = N - 1; i >= 0; i--) {
if (S[i] - 'a' == c) {
next[c][i] = i + 1;
} else if (i < N - 1) {
next[c][i] = next[c][i+1];
}
}
}
auto mem = new Tuple!(int, int)[](N);
fill(mem, tuple(1 << 29, 1 << 29));
Tuple!(int, int) dfs(int n) {
if (n == -1) return tuple(0, -1);
if (n >= N) return tuple(1, -1);
if (mem[n][0] != 1 << 29) return mem[n];
auto ret = tuple(1 << 29, 1 << 29);
foreach (i; 0..26) {
auto t = dfs(next[i][n]);
if (t[0] < ret[0]) ret = tuple(t[0], i);
}
mem[n] = tuple(ret[0] + 1, ret[1]);
return mem[n];
}
dfs(0);
string ans = "";
for (int i = 0; i != -1;) {
if (mem[i][1] == -1) break;
char c = (mem[i][1].to!char + 'a').to!char;
ans ~= c;
i = next[c - 'a'][i];
}
ans.writeln;
}
| D |
import std;
auto input()
{
return readln().chomp();
}
alias sread = () => readln.chomp();
alias aryread(T = long) = () => readln.split.to!(T[]);
void main()
{
long n;
scan(n);
if (n % 2 == 0)
{
writeln(n / 2);
}
else
{
writeln(n / 2 + 1);
}
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
auto binary_search(long key, long[] l)
{
long left = 0; //lの左端
long right = l.length - 1; //lの右端
while (right >= left)
{
long mid = left + (right - left) / 2; //区間の真ん中
if (l[mid] == key)
{
return mid;
}
else if (l[mid] > key)
{
right = mid - 1;
}
else if (l[mid] < key)
{
left = mid + 1;
}
}
return -1;
}
// auto a = [1, 14, 32, 51, 51, 51, 243, 419, 750, 910];
// auto a = aryread();
// writeln(a);
// long len_a = a.length;
// writeln(len_a);
// long ans;
// ans = binary_search(51, a);
// writeln(ans);
| D |
import std.stdio;
import std.algorithm;
import std.conv;
import std.string;
void main() {
int N = readln.chomp.to!int;
auto cards = readln.chomp.split;
auto cards2 = cards.dup;
string b = cards.bubbleSort.join(" ");
b.writeln;
"Stable".writeln;
string s = cards2.selectionSort.join(" ");
s.writeln;
writeln(s == b ? "Stable" : "Not stable");
}
string[] bubbleSort(string[] cc) {
string[] c = cc;
auto n = c.length;
for (int i = 0; i < n - 1; i++) {
for (int j = n.to!int - 1; j > i; j--) {
if (c[j][1] < c[j - 1][1]) {
swap(c[j], c[j - 1]);
}
}
}
return c;
}
string[] selectionSort(string[] cc) {
string[] c = cc;
auto n = c.length;
for (int i = 0; i < n - 1; i++) {
int minv = c[i][1];
int minj = i;
for (int j = i + 1; j < n; j++) {
if (minv > c[j][1]) {
minv = c[j][1];
minj = j;
}
}
swap(c[i], c[minj]);
}
return c;
} | D |
void main() {
string s = readln.chomp;
int n = s.to!int;
int m;
foreach (x; s) {
m += x -'0';
}
writeln(n % m == 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.uni; | D |
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container;
import std.numeric, std.math;
import core.bitop;
string RD() { return chomp(readln()); }
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 tokens = RD.split;
auto N = tokens[0].to!long;
auto S = RD;
auto e = new long[](N+1);
auto w = new long[](N+1);
foreach (i, c; S)
{
w[i+1] = w[i] + (c == 'W' ? 1 : 0);
}
foreach_reverse (i, c; S)
{
e[i] = e[i+1] + (c == 'E' ? 1 : 0);
}
long ans = long.max;
foreach (i; 0..N)
{
ans = min(ans, w[i] + e[i+1]);
}
writeln(ans);
stdout.flush();
} | D |
void main()
{
string s = rdStr;
long len = s.length;
foreach (i; 0 .. len)
{
'x'.write;
}
writeln;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void ipElems(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 |
import std.string;
import std.stdio;
import std.algorithm;
void main() {
auto s = readln.chomp.dup;
auto t = readln.chomp.dup;
if (t.length < s.length) {
swap(s, t);
}
auto dp0 = new char[][s.length + 1];
auto dp1 = new char[][s.length + 1];
foreach (ref e;dp0) {
e = new char[0];
}
dp1[0] = new char[0];
foreach (te;t) {
foreach (size_t i, se;s) {
if (te == se) {
dp1[i + 1] = dp0[i];
dp1[i + 1] ~= te;
} else if (dp0[i + 1].length >= dp1[i].length) {
dp1[i + 1] = dp0[i + 1];
} else {
dp1[i + 1] = dp1[i];
}
}
swap(dp0, dp1);
}
writeln(dp0[s.length]);
}
| D |
void main() {
auto S = rs;
string s;
foreach(i; 1..6) {
s ~= "hi";
if(S == s) {
writeln("Yes");
return;
}
}
writeln("No");
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
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.conv, std.algorithm;
void main(string[] args) {
auto x = readln().chomp;
foreach(int i, char c; x){
if(i%2==0){c.write;}
}
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;
// dfmt on
void main()
{
long a = lread();
writeln(a + a * a + a * a * a);
}
| D |
import std.algorithm, std.conv, std.range, std.stdio, std.string;
const days = [0,31,28,31,30,31,30,31,31,30,31,30,31];
void main()
{
auto rd = readln.split.to!(int[]), x = rd[0], y = rd[1];
writeln(days[x] == days[y] ? "Yes" : "No");
}
| D |
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;
import std.datetime;
void main()
{
while (1) {
auto x = readln.chomp.split.map!(to!int);
if (x[0] == 0 && x[1] == 0) break;
foreach (i; 0..x[0]) {
foreach (j; 0..x[1]) write("#");
writeln("");
}
writeln("");
}
} | D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.math;
void main() {
(readln.chomp.to!int / 3).writeln;
} | D |
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;
void main() {
immutable long MAX = 2*10^^6+1;
immutable long MOD = 10^^9+7;
auto modinv = new long[](MAX);
modinv[0] = modinv[1] = 1;
foreach(i; 2..MAX) {
modinv[i] = modinv[MOD % i] * (MOD - MOD / i) % MOD;
}
auto f_mod = new long[](MAX);
auto f_modinv = new long[](MAX);
f_mod[0] = f_mod[1] = 1;
f_modinv[0] = f_modinv[1] = 1;
foreach(i; 2..MAX) {
f_mod[i] = (i * f_mod[i-1]) % MOD;
f_modinv[i] = (modinv[i] * f_modinv[i-1]) % MOD;
}
long nck(long n, long k) {
return f_mod[n] * f_modinv[n-k] % MOD * f_modinv[k] % MOD;
}
int H, W, A, B;
scanf("%d %d %d %d", &H, &W, &A, &B);
long ans = 0;
foreach (i; 0..H-A) {
ans = (ans + nck(B-1+i, i) * nck(W-B+H-i-2, H-i-1) % MOD) % MOD;
}
writeln( ans >= 0 ? ans : ans+MOD);
}
| 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 |
// 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 H, W;
scan(H, W);
auto S = new string[](H);
foreach (i; 0 .. H)
S[i] = sread();
auto R = new long[][](H, W);
auto C = new long[][](H, W);
foreach (w; 0 .. W)
{
long h = 0;
while (h < H)
{
long len = 0;
while (h + len < H && S[h + len][w] == '.')
len++;
foreach (i; h .. h + len)
C[i][w] = len;
h += max(1, len);
}
}
foreach (h; 0 .. H)
{
long w = 0;
while (w < W)
{
long len = 0;
while (w + len < W && S[h][w + len] == '.')
len++;
R[h][w .. w + len] = len;
w += max(1, len);
}
}
long ans = 0;
foreach (h; 0 .. H)
foreach (w; 0 .. W)
ans = ans.max(R[h][w] + C[h][w] - 1);
writeln(ans);
}
| D |
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto t = readln.split;
auto N = t[0].to!int;
auto L = t[1].to!long;
auto S = N.iota.map!(_ => readln.chomp).array;
auto tr = new Trie(L);
foreach (s; S) {
tr.add_string(s);
}
writeln(tr.search ? "Alice" : "Bob");
}
class Node {
int d;
Node[2] children;
this(int d) {
this.d = d;
}
}
class Trie {
Node root;
long L;
this(long L) {
root = new Node(0);
this.L = L;
}
void add_string(string s) {
Node cur = root;
foreach (c; s) {
if (cur.children[c-'0'] is null) {
cur.children[c-'0'] = new Node(cur.d+1);
}
cur = cur.children[c-'0'];
}
}
long search() {
return search(root);
}
long search(Node n) {
long l = L - n.d;
long i = 1;
while (l != 0 && l % (i * 2) == 0) i *= 2;
if (n.children[0] is null && n.children[1] is null) {
return 0;
} else if (n.children[0] is null) {
return search(n.children[1]) ^ i;
} else if (n.children[1] is null) {
return search(n.children[0]) ^ i;
} else {
return search(n.children[0]) ^ search(n.children[1]);
}
}
}
| D |
import std.stdio,std.array,std.conv,std.string;
void main(){
int n=readln().chomp().to!int;
string k=to!string(n);
int m=0;
for(int i=0;i<k.length;i++){
m+=to!int(k[i])-48;
}
if(n%m==0) writeln("Yes");
else writeln("No");
} | D |
import std.stdio;
import std.string;
import std.conv;
void main()
{
int[] a = readln.chomp.split.to!(int[]);
if (a[0] <= 8 && a[1] <= 8) {
writeln("Yay!");
}
else {
writeln(":(");
}
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string;
enum DREAM = "dream";
enum DREAM_CNT = DREAM.length;
enum DREAMER = "dreamer";
enum DREAMER_CNT = DREAMER.length;
enum ERASE = "erase";
enum ERASE_CNT = ERASE.length;
enum ERASER = "eraser";
enum ERASER_CNT = ERASER.length;
void main()
{
auto s = readln.chomp;
while (!s.empty) {
if (s.endsWith(DREAMER)) {
s = s[0..$-DREAMER_CNT];
} else if (s.endsWith(ERASER)) {
s = s[0..$-ERASER_CNT];
} else if (s.endsWith(DREAM)) {
s = s[0..$-DREAM_CNT];
} else if (s.endsWith(ERASE)) {
s = s[0..$-ERASE_CNT];
} else {
writeln("NO");
return;
}
}
writeln("YES");
return;
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 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;
writeln(A <= 9 && B <= 9 ? A*B : -1);
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 abc = readln.split.to!(int[]);
auto d = abc[0] + abc[1] + abc[2];
writeln(d + max(abc[0], abc[1], abc[2]) * 9);
} | D |
import std.stdio;
import std.conv;
import std.string;
void main() {
auto n = readln.chomp.to!(int);
auto ans = 0;
if (n / 100 == 9)
ans += 100;
else
ans += 900;
if (n % 100 >= 90)
ans += 10;
else
ans += 90;
if (n % 10 == 9)
ans += 1;
else
ans += 9;
ans.writeln;
}
| D |
void main() {
string b = readln.chomp;
b == "A" && "T".writeln;
b == "T" && "A".writeln;
b == "C" && "G".writeln;
b == "G" && "C".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, 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(){
string s = readln.chomp;
long ans;
string old = "", now = "";
foreach(c; s){
now ~= c;
if(now != old){
ans += 1;
old = "" ~ now;
now = "";
}
log("old:", old, "now:", now, "ans:", ans);
}
ans.writeln;
}
/*
long n = read.to!long;
long m = read.to!long;
long k = read.to!long;
long a = read.to!long;
long b = read.to!long;
long x = read.to!long;
long y = read.to!long;
long z = read.to!long;
long[] as;
foreach(i; 0 .. n) as ~= read.to!long;
long[] bs;
foreach(i; 0 .. n) bs ~= read.to!long;
long[] as;
foreach(j; 0 .. m) as ~= read.to!long;
long[] bs;
foreach(j; 0 .. m) bs ~= read.to!long;
struct S{
long a, b;
}
S[] xs;
foreach(i; 0 .. n) xs ~= S(read.to!long, read.to!long);
*/
| D |
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;
void main() {
auto N = readln.chomp.to!int;
auto S = readln.chomp;
auto dp = new bool[][][][](N+1, 11, 11, 11);
foreach (i; 0..N) {
dp[i + 1] = dp[i].dup;
foreach (j; 0..11) {
foreach (k; 0..11) {
if (dp[i][10][j][k]) {
dp[i + 1][j][k][S[i] - '0'] = true;
}
}
}
dp[i + 1][10][10][S[i] - '0'] = true;
}
int ans = 0;
foreach (i; 0..10) foreach (j; 0..10) foreach (k; 0..10) ans += dp[N][i][j][k];
ans.writeln;
} | D |
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "l ", long, "r");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
auto n = lread();
auto a = new long[](0);
while(abs(n) > 0)
{
if(n == 1)
{
a ~= 1;
break;
}
if(abs(n % (-2)) > 0)
{
a ~= 1;
n -= 1;
}
else
{
a~= 0;
}
n /= (-2);
}
foreach_reverse (e; a)
{
e.write();
}
if(a.empty())
write(0);
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, 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); }
import std.bigint, std.functional;
void main() {
auto I = readln.split.to!(long[]);
auto A = I[0];
auto B = I[1];
auto C = I[2];
bool solve() {
auto x = C - A - B;
if (x < 0) return false;
return 4*A*B < x*x;
}
writeln(solve() ? "Yes" : "No");
}
| D |
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto a = readln.chomp;
auto b = readln.chomp;
auto s = max(a.length, b.length);
auto a2 = a.rightJustify(s, '0');
auto b2 = b.rightJustify(s, '0');
if (a2 == b2)
writeln("EQUAL");
else if (a2 < b2)
writeln("LESS");
else
writeln("GREATER");
}
| D |
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
auto n = lread();
long ans = long.max;
// foreach (i; 1 .. (n + 1))
// {
// foreach (j; 1 .. (n + 1))
// {
// if (i * j == n)
// {
// ans = min(ans, ((i - 1) + (j - 1)));
// }
// }
// }
// writeln(ans);
foreach (i; 1 .. n)
{
if (n % i == 0)
{
ans = min(ans, ((i - 1) + (n / i) - 1));
// writeln(ans);
}
if (i > (n / i))
{
// writeln(i, n / i);
break;
}
}
writeln(ans);
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
| D |
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.stdio;
import std.string;
import std.range;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() {
auto xs = readints();
int a = xs[0], b = xs[1], c = xs[2], d = xs[3];
int ans = 0;
for (int i = 0; i <= 100; i++) {
if (a <= i && i <= b && c <= i && i <= d) {
ans++;
}
}
writeln(max(0, ans - 1));
}
| D |
import std.stdio, std.math, std.algorithm, std.array, std.string, std.conv, std.container, std.range;
pragma(inline, true) T[] Reads(T)() { return readln.split.to!(T[]); }
alias reads = Reads!int;
pragma(inline, true) void scan(Args...)(ref Args args) {
string[] ss = readln.split;
foreach (i, ref arg ; args) arg = ss[i].parse!int;
}
void main() {
int n; scan(n);
writeln(n ^^ 3);
}
| D |
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math;
import core.stdc.stdio, core.bitop;
// foreach, foreach_reverse, writeln
void main() {
int n, MOD;
scanf("%d%d", &n, &MOD);
long[][] c = new long[][](n+1,n+2);
c[0][0] = 1;
foreach (i; 0..n) {
foreach (j; 0..i+1) {
(c[i+1][j] += c[i][j]) %= MOD;
(c[i+1][j+1] += c[i][j]) %= MOD;
}
}
long[][] dp = new long[][](n+1,n+1);
dp[0][0] = 1;
foreach (i; 0..n) {
foreach (j; 0..n+1) {
(dp[i+1][j] += dp[i][j]) %= MOD;
(dp[i+1][j] += dp[i][j]*j) %= MOD;
if (j != n) (dp[i+1][j+1] += dp[i][j]) %= MOD;
}
}
//long pow(long x, long times, long MOD) {
// long ret = 1;
// while (times) {
// if (times&1) (ret *= x) %= MOD;
// times >>= 1;
// (x *= x) %= MOD;
// }
// return ret;
//}
//long powpow(long x, long y, long MOD) {
// foreach (i; 0..y) {
// (x *= x) %= MOD;
// }
// return x;
//}
long[] p2 = new long[n+1];
p2[0] = 1;
foreach (i; 0..n) p2[i+1] = p2[i]*2%MOD;
long[] pp2 = new long[n+1];
pp2[0] = 2;
foreach (i; 0..n) pp2[i+1] = pp2[i]*pp2[i]%MOD;
long ans = 0;
foreach (i; 0..n+1) {
long cur = 0;
long two = 1;
foreach (j; 0..i+1) {
long a = dp[i][j]*c[n][i]%MOD;
//long x = powpow(2,n-i,MOD)*pow(pow(2,n-i,MOD),j,MOD)%MOD;
long x = pp2[n-i]*two%MOD;
(cur += a*x) %= MOD;
(two *= p2[n-i]) %= MOD;
}
if (i%2 == 0) {
ans += cur;
} else {
ans += MOD-cur;
}
ans %= MOD;
}
writeln(ans);
} | D |
void main() {
int k = readln.chomp.to!int;
writeln(k / 2 * ((k + 1) / 2));
}
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 |
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
import std.math;
int n, d; rd(n, d);
auto t=new long[](n);
auto f=new long[](n);
foreach(i; 0..n) rd(t[i], f[i]);
long sm=0, pos=1, tm=0, m=0;
foreach(i; 0..n){
if(tm+(pos-1)+(f[i]-1)<=t[i]){
sm+=m*(pos-1);
m=1;
}else if(tm+abs(pos-f[i])<=t[i] && m<d){
// sm+=m*abs(pos-f[i]);
sm+=m*abs(t[i]-tm);
m++;
}else{
writeln(-1);
return;
}
pos=f[i]; tm=t[i];
}
writeln(sm+m*(pos-1));
}
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.array;
import std.string;
import std.algorithm;
void main() {
string S;
S = chomp(readln());
string res = "";
if(S[0] == '9' || S[1] == '9') {
res = "Yes";
}
else{
res = "No";
}
writeln(res);
}
| 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;
bool isPalindrome(string s) {
int n = cast(int)s.length;
for (int i = 0; i < n/2; i++) {
if (s[i] != s[$-i-1]) return false;
}
return true;
}
bool calc(string s) {
int n = cast(int)s.length;
if (!isPalindrome(s)) return false;
if (!isPalindrome(s[0..(n-1)/2])) return false;
if (!isPalindrome(s[(n+3)/2-1..$])) return false;
return true;
}
void main() {
string s; scan(s);
writeln(calc(s) ? "Yes" : "No");
}
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
| D |
void main() {
int n = readln.chomp.to!int;
int[] a = new int[n];
foreach (i; 0 .. n) {
a[i] = readln.chomp.to!int;
}
bool[] light = new bool[n];
light[0] = true;
int now = a[0];
int cnt;
while (!light[1]) {
if (light[now-1]) {
cnt = -1;
break;
}
light[now-1] = true;
now = a[now-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;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
void main(){
auto a=readln.chomp.to!int;
auto b=readln.chomp.to!int;
auto h=readln.chomp.to!int;
writeln((a+b)*h/2);
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
auto P = 10L^^9+7;
long[100][5][5][5] MEMO;
void main()
{
auto N = readln.chomp.to!int;
foreach (ref xx; MEMO) foreach (ref yy; xx) foreach (ref zz; yy) foreach (ref memo; zz) memo = -1;
long solve(int i, int x, int y, int z) {
if (i == N) return 1;
if (MEMO[z][y][x][i] != -1) return MEMO[z][y][x][i];
long r = (solve(i+1, y, z, 1) + solve(i+1, y, z, 4)) % P;
if ( !(
(y == 1 && z == 3) ||
(x == 1 && z == 3) ||
(y == 3 && z == 1) ||
(x == 1 && y == 3)) ) r = (r + solve(i+1, y, z, 2)) % P;
if ( !(y == 1 && z == 2) ) r = (r + solve(i+1, y, z, 3)) % P;
return MEMO[z][y][x][i] = r;
}
writeln(solve(0, 0, 0, 0));
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void get(Args...)(ref Args args)
{
import std.traits, std.meta, std.typecons;
static if (Args.length == 1) {
alias Arg = Args[0];
static if (isArray!Arg) {
static if (isSomeChar!(ElementType!Arg)) {
args[0] = readln.chomp.to!Arg;
} else {
args[0] = readln.split.to!Arg;
}
} else static if (isTuple!Arg) {
auto input = readln.split;
static foreach (i; 0..Fields!Arg.length) {
args[0][i] = input[i].to!(Fields!Arg[i]);
}
} else {
args[0] = readln.chomp.to!Arg;
}
} else {
auto input = readln.split;
assert(input.length == Args.length);
static foreach (i; 0..Args.length) {
args[i] = input[i].to!(Args[i]);
}
}
}
void get_lines(Args...)(size_t N, ref Args args)
{
import std.traits, std.range;
static foreach (i; 0..Args.length) {
static assert(isArray!(Args[i]));
args[i].length = N;
}
foreach (i; 0..N) {
static if (Args.length == 1) {
get(args[0][i]);
} else {
auto input = readln.split;
static foreach (j; 0..Args.length) {
args[j][i] = input[j].to!(ElementType!(Args[j]));
}
}
}
}
void main()
{
int T; get(T);
while (T--) {
char[] s; get(s);
if (s.length % 2 == 1 || s[0] == ')' || s[$-1] == '(') {
writeln("NO");
} else {
writeln("YES");
}
}
} | D |
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
import std.numeric;
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; readV(n);
long[] t; readC(n, t);
writeln(t.reduce!((a, b) => a/gcd(a,b)*b));
}
| D |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.