code
stringlengths 4
1.01M
| language
stringclasses 2
values |
---|---|
import std.stdio;
import std.algorithm;
import std.conv;
import std.string;
void main()
{
auto n = to!int(chomp(readln()));
string[] ws;
ws ~= chomp(readln());
bool isYes = true;
foreach (i; 1..n)
{
ws ~= chomp(readln());
if (ws[i-1][$-1] != ws[i][0])
{
isYes = false;
break;
}
else if (countUntil(ws[0..$-1], ws[$-1]) != -1)
{
isYes = false;
break;
}
}
if (isYes)
writeln("Yes");
else
writeln("No");
stdout.flush();
} | D |
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.ascii;
void main()
{
auto a = readln.chomp.split.to!(int[]);
writeln((a[0]*3+a[1])/2);
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
long P = 10^^9 + 7;
void main()
{
auto N = readln.chomp.to!int;
auto S = readln.chomp.to!(char[]);
long[26] as;
foreach (c; S) {
if (as[c-97] == 0) {
as[c-97] = 2;
} else {
++as[c-97];
}
}
long r = 1;
foreach (a; as) if (a) {
r = (r * a) % P;
}
writeln(r-1);
} | D |
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n; readV(n);
int[] a; readA(n, a);
writeln(a.reduce!max - a.reduce!min);
}
| D |
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
void main() {
int n, k;
scan(n, k);
auto a = readln.split.to!(int[]);
foreach (i; k .. n) {
yes(a[i] > a[i - k]);
}
}
void scan(T...)(ref T args) {
auto line = readln.split; // @suppress(dscanner.suspicious.unmodified)
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args)
if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args)
if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
| D |
import std.array;
import std.range;
import std.algorithm;
import std.conv;
import std.stdio;
import std.string;
import std.traits;
import std.ascii;
class Solver {
private int[char] hash;
private bool firstUpdate = true;
public this() {
foreach(c; lowercase) {
hash[c] = 0;
}
}
public void updateState(string str) {
if(firstUpdate) {
foreach(c; str) {
hash[c]++;
}
} else {
int[char] tempHash;
foreach(c; lowercase) {
tempHash[c] = 0;
}
foreach(c; str) {
tempHash[c]++;
}
foreach(c; lowercase) {
hash[c] = min(tempHash[c], hash[c]);
}
}
firstUpdate = false;
}
public string ans() {
string ans;
foreach(c; lowercase) {
ans ~= c.repeat().take(hash[c]).array;
}
return ans;
}
}
void main(string[] args) {
auto a = new Solver();
int n = readln.chomp.to!int;
foreach(i; iota(n)) {
a.updateState(readln.chomp);
}
a.ans.writeln;
} | D |
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
ulong bignum = 1_000_000_007;
alias Pair = Tuple!(long, "number", long, "times");
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();
long[dchar] count;
foreach (e; s)
{
count[e]++;
}
long remove;
if ('0' in count && '1' in count)
remove = min(count['0'], count['1']);
writeln(remove * 2);
} | D |
import std.stdio;
import std.string;
import std.conv;
void main(){
int[string] diction;
int max;
string longest="";
foreach( c ; readln().chomp().split() ){
diction[c]++;
if( max < diction[c] ){
max = diction[c];
}
if( longest.length < c.length ){
longest = c;
}
}
foreach( c,i ; diction ){
if( max == diction[c] ){
write( c," " );
}
}
writeln( longest );
} | 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 a = RD;
auto b = RD;
auto n = RD;
auto m = RD;
if (a+b < n+m) continue;
if (min(a, b) >= m)
ans[ti] = true;
}
foreach (e; ans)
writeln(e ? "Yes" : "No");
stdout.flush;
debug readln;
}
| D |
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto c = readln.chomp;
writeln("aeiou".canFind(c) ? "vowel" : "consonant");
}
| D |
import std.stdio,std.conv,std.algorithm,std.string;alias L=long;L M=998244353;L[]X;L[L]C;L h(L m,L[]U){L f;foreach(u;U[0..m])f=f*2%M+u;foreach(i,u;U)if(i/m&1^U[i%m]^u)return f+u;return f+1;}void main(){L n=readln.chomp.to!L,b=n*2;foreach(c;readln.chomp)X~=c-'0';for(L d=n|1;d>2;d-=2)if(n%d<1){L c=h(n/d,X);foreach(e,q;C)e%d||(c+=M-q);C[d]=c%M;}n=h(n,X)*b;foreach(d,c;C)n+=M-c*(b-b/d)%M;writeln(n%M);}
| D |
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
bool calc(string[] a, string[] b) {
int n = cast(int) a.length;
int m = cast(int) b.length;
for (int i = 0; i < n; i++) {
if (i + m > n) break;
for (int j = 0; j < n; j++) {
if (j + m > n) break;
bool ok = true;
for (int r = 0; r < m; r++) {
for (int c = 0; c < m; c++) {
if (a[i + r][j + c] != b[r][c]) {
ok = false;
}
}
}
if (ok) return true;
}
}
return false;
}
void main() {
auto nm = readints;
int n = nm[0], m = nm[1];
string[] a;
for (int i = 0; i < n; i++) {
a ~= read!string;
}
string[] b;
for (int i = 0; i < m; i++) {
b ~= read!string;
}
writeln(calc(a, b) ? "Yes" : "No");
}
| D |
void main()
{
int n = readln.chomp.to!int;
string s = readln.chomp;
int cnt = s[1..$].count('E').to!int;
int result = cnt;
foreach (i; 1 .. n)
{
if (s[i-1] == 'W') ++cnt;
if (s[i] == 'E') --cnt;
result = min(result, cnt);
}
result.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 |
void main()
{
long k = rdElem;
long total;
foreach (i; 1 .. k+1)
{
foreach (j; 1 .. k+1)
{
long g = gcd(i, j);
foreach (l; 1 .. k+1)
{
total += gcd(g, l);
}
}
}
total.writeln;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
enum double eps = 1.0e-9;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop; | D |
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
bool calc(int n) {
for (int i = 0; i * 4 <= n; i++) {
for (int j = 0; j * 7 <= n; j++) {
if (n == i * 4 + j * 7) {
return true;
}
}
}
return false;
}
void main() {
int n = readint;
writeln(calc(n) ? "Yes" : "No");
}
| D |
import std.stdio: writeln, readln;
import std.array: split;
void main() {
string[] s = readln.split;
if (s[0][$-1] == s[1][0] && s[1][$-1] == s[2][0])
writeln("YES");
else
writeln("NO");
}
| D |
import std.stdio;
import std.string;
import std.conv;
void main() {
auto n = to!int(readln.chomp!());
writeln(n / 3);
// to!(int[])(readln.split(",")).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, std.bitmanip;
immutable long MOD = 10^^9+7;
void main() {
auto N = readln.chomp.to!int;
auto cnt = new long[](10^^3+1);
fill(cnt, 0);
foreach (i; 2..N+1) {
int j = 2;
while (j * j <= i) {
while (i % j == 0) {
i /= j;
cnt[j]++;
}
j++;
}
if (i > 1)
cnt[i]++;
}
long ans = 1;
foreach (i; 2..N+1) {
ans = (ans * (cnt[i]+1)) % MOD;
}
ans.writeln;
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math;
void main()
{
auto ip = readln.split.to!(int[]);
auto A = ip[0];
auto B = ip[1];
auto C = ip[2];
auto X = ip[3];
auto Y = ip[4];
writeln(min(
X * A + Y * B,
X * 2 * C + max(Y - X, 0) * B,
max(X - Y, 0) * A + Y * C * 2,
max(X, Y) * C * 2
));
} | D |
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void main()
{
int n; readV(n);
writeln(n%calc(n) == 0 ? "Yes" : "No");
}
auto calc(int n)
{
int r = 0;
for (; n > 0; n /= 10) r += n%10;
return r;
}
| D |
import std.stdio, std.string, std.conv;
void main(){
auto AB = ri!size_t();
solve( AB[0] , AB[1] ).writeln();
return;
}
auto solve( size_t A , size_t B ){
if ( (B-1)%(A-1)==0 ){
return (B-1)/(A-1);
} else {
return (B-1)/(A-1)+1;
}
}
T[] ri( T = size_t )(){
T[] ol;
foreach( string elm ; readln().chomp().split() ){
ol ~= elm.to!T();
}
return ol;
}
| D |
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
void 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;
}
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int N, M, Q;
scan(N, M, Q);
auto lr = new int[][](N, N);
foreach (i ; 0 .. M) {
int li, ri;
scan(li, ri);
li--, ri--;
lr[li][ri]++;
}
auto cu = new int[][](N + 1, N + 1);
foreach (i ; 1 .. N + 1) {
foreach (j ; 1 .. N + 1) {
cu[i][j] += cu[i-1][j] + cu[i][j-1] - cu[i-1][j-1] + lr[i-1][j-1];
}
}
foreach (_ ; 0 .. Q) {
int pi, qi;
scan(pi, qi);
auto ans = cu[N][qi] - cu[pi - 1][qi];
writeln(ans);
}
}
| D |
void main() {
dchar[] c1 = readln.chomp.to!(dchar[]);
dchar[] c2 = readln.chomp.to!(dchar[]);
reverse(c2);
writeln(c1 == c2 ? "YES" : "NO");
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni; | D |
import std.algorithm;
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 calc(string s, string t) {
ulong start = 0;
ptrdiff_t p;
while ((p = s.indexOf(t, start)) != -1) {
writeln(p);
start = p + 1;
}
}
void main() {
auto s = readln.chomp;
auto t = readln.chomp;
calc(s, t);
} | D |
void main()
{
long n = rdElem;
long[] factor = new long[n+1];
foreach (i; 2 .. n+1)
{
long num = i;
for (long j = 2; j * j <= n; ++j)
{
while (num % j == 0)
{
num /= j;
++factor[j];
}
}
if (num > 1) ++factor[num];
}
long[] list = [2, 4, 14, 24, 74];
long[long] cnt;
foreach (x; list) cnt[x] = 0;
foreach (x; factor)
{
foreach (y; list)
{
if (x >= y) ++cnt[y];
}
}
long result;
result += cnt[74];
result += cnt[24] * (cnt[2] - 1);
result += cnt[14] * (cnt[4] - 1);
result += ((cnt[4] * (cnt[4] - 1)) >> 1) * (cnt[2] - 2);
result.writeln;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
enum double eps = 1.0e-9;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop; | D |
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int a, b, c;
rd(a, b, c);
writeln(max(a * 10 + b + c, b * 10 + c + a, c * 10 + a + b));
}
void rd(T...)(ref T x) {
import std.stdio, std.string, std.conv;
auto l = readln.split;
assert(l.length == x.length);
foreach (i, ref e; x)
e = l[i].to!(typeof(e));
}
| D |
import std.stdio, std.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 D = RD;
auto N = RD;
writeln(N == 100 ? (N+1) * 100^^D : N * 100^^D);
stdout.flush();
debug readln();
}
| D |
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
string s = read!string;
string t = s.translate(['A': 'T', 'T': 'A', 'C': 'G', 'G': 'C']);
writeln(t);
}
| D |
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array, std.functional;
void main() {
auto N = readln.split[0].to!ulong;
((N*(N-1))/2).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;
bool check(long a, long b, long c) {
return (c - b - a) >= 0 && 4*a*b < (c - b - a)^^2;
}
void main() {
long a, b, c;
scan(a, b, c);
yes(check(a, b, c));
}
void scan(T...)(ref T args) {
auto line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
| D |
import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.numeric;
void main()
{
auto rd = readln.split.map!(to!int), x = rd[0], y = rd[1];
writeln(gcd(x, y));
} | 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 solve() {
auto N = readln.chomp.to!int;
auto S = readln.chomp;
if (N == 1) {
writeln("NO");
return;
}
if (N == 2) {
if (S[0] >= S[1]) {
writeln("NO");
return;
}
}
writeln("YES");
writeln(2);
writeln(S[0], " ", S[1..$]);
}
void main() {
auto T = readln.chomp.to!int;
while (T--) solve;
}
| 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 X = readln.chomp.to!int;
writeln(8 - (X-400)/200);
} | D |
import std.stdio, std.conv, std.string, std.bigint;
import std.math, std.random, std.datetime;
import std.array, std.range, std.algorithm, std.container, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
/*
グリッド
経路数
剰余
x[i][j] = x[i - 1][j] + x[i][j - 1] % mod
*/
const long mod = 1_000_000_007;
void main(){
int h = read.to!int;
int w = read.to!int;
char[][] cells;
foreach(i; 0 .. h) cells ~= readln.chomp.to!(char[]);
long[] xs;
foreach(i; 0 .. h){
foreach(j; 0 .. w){
if(i == 0){
if(j == 0) xs ~= 1;
else xs ~= 0;
}
if(cells[i][j] == '#'){
xs[j] = 0;
}
else if(j > 0){
xs[j] += xs[j - 1];
xs[j] %= mod;
}
}
}
xs[w - 1].writeln;
}
| D |
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto n = readln.chomp.to!size_t;
auto r = 100_000;
foreach (_; n.iota)
r = next(r);
writeln(r);
}
int next(int r)
{
r = r + r / 20;
if (r % 1000 > 0)
return (r / 1000 + 1) * 1000;
else
return r / 1000 * 1000;
} | D |
import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm;
import std.math;
void main(){
int n = readln.chomp.to!int;
auto ls = readln.chomp.split.map!(to!long);
long s = 0;
foreach(l; ls){
s += l;
}
long m = 0;
foreach(l; ls){
m = max(m, l);
}
long res = abs(2*m-s)+1;
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.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto S = RD!string;
long[3] cnt;
foreach (c; S)
{
++cnt[c-'a'];
}
writeln(cnt[0] == 1 && cnt[1] == 1 && cnt[2] == 1 ? "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, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N, K;
scan(N, K);
long R, S, P;
scan(R, S, P);
auto T = sread();
auto dp = new long[][](3, N);
long pts(long x, long y)
{
if (x == 0)
return T[y] == 's' ? R : 0;
if (x == 1)
return T[y] == 'p' ? S : 0;
if (x == 2)
return T[y] == 'r' ? P : 0;
return long.min;
}
foreach (i; 0 .. N)
{
if (i < K)
{
dp[0][i] = pts(0, i);
dp[1][i] = pts(1, i);
dp[2][i] = pts(2, i);
}
else
{
dp[0][i] = max(dp[1][i - K], dp[2][i - K]) + pts(0, i);
dp[1][i] = max(dp[0][i - K], dp[2][i - K]) + pts(1, i);
dp[2][i] = max(dp[0][i - K], dp[1][i - K]) + pts(2, i);
}
}
long ans;
foreach (i; N - K .. N)
{
ans += max(dp[0][i], dp[1][i], dp[2][i]);
}
writeln(ans);
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto a = RD;
auto b = RD;
auto ans = sgn(a) * sgn(b);
if (ans == 1 && sgn(a) == -1 && (abs(a - b) + 1) % 2 == 0)
a = 1;
writeln((ans == 1 && sgn(a) == 1) ? "Positive" : ans == 1 && sgn(a) == -1 ? "Negative" : "Zero");
stdout.flush();
debug readln();
}
| 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 Q = readln.chomp.to!int;
while (Q--) {
int N = readln.chomp.to!int;
int L = 2;
int U = 7;
int i = 1;
while (U < N) {
L += 2;
U += 7;
i += 1;
}
writeln(i);
}
}
| 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 main()
{
auto a = lread();
writeln(a/2 * (a/2 + a%2));
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nk = readln.split.to!(int[]);
readln;
auto N = nk[0];
auto K = nk[1];
int c;
while (N > 1) {
++c;
N -= K-1;
}
writeln(c);
} | D |
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math;
import core.stdc.stdio;
// foreach, foreach_reverse, writeln
void main() {
int n;
scanf("%d", &n);
int[] a = new int[n+2];
foreach (i; 0..n) {
scanf("%d", &a[i+1]);
}
int ans = 0;
foreach (i; 0..n+1) {
ans += abs(a[i+1]-a[i]);
}
foreach (i; 1..n+1) {
int diff = 0;
diff += abs(a[i]-a[i-1]);
diff += abs(a[i+1]-a[i]);
diff -= abs(a[i+1]-a[i-1]);
writeln(ans-diff);
}
} | D |
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
enum MOD = (10 ^^ 9) + 7;
void main()
{
long N;
scan(N);
long ans = 1;
foreach (i; 0 .. N)
{
ans *= i + 1;
ans %= MOD;
}
writeln(ans);
}
| D |
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long Q, H, S, D;
scan(Q, H, S, D);
long N = lread();
long A = Q * 4;
long B = H * 2;
long ans;
if (min(A * 2, B * 2, S * 2, D) == D)
{
ans = D * (N / 2);
ans += min(A, B, S) * (N % 2);
}
else
{
ans = min(A, B, S) * N;
}
writeln(ans);
}
| D |
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container;
import std.numeric, std.math;
import core.bitop;
T RD(T)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; }
string RDR() { return readln.chomp; }
long mod = pow(10, 9) + 7;
long moda(long x, long y) { return (x + y) % mod; }
long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; }
long modm(long x, long y) { return (x * y) % mod; }
void main()
{
auto A = RD!ulong;
auto B = RD!ulong;
auto C = RD!ulong;
auto X = RD!ulong;
auto Y = RD!ulong;
ulong ans;
auto xy = min(X, Y);
ans += min(C * xy * 2, (A + B) * xy);
ulong cost;
if (X > Y) cost = A;
else cost = B;
auto q = max(X, Y) - min(X, Y);
ans += min(C * q * 2, cost * q);
writeln(ans);
stdout.flush();
} | D |
import std.stdio;
import std.string;
import std.range;
import std.conv;
void main()
{
auto S = readln.chomp.to!(char[]);
S[3] = '8';
S.writeln;
//if(S[4] == '7');
//writeln(S[0],S[1],S[2],8,S[4],S[5],S[6],S[7],S[8],S[9],S[10]);
} | 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 str=readln().chomp;
writeln(str.length/2-str.count!(s=>s=='p'));
} | 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 C = RD;
writeln(A == B ? C : B == C ? A : B);
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;
import std.datetime;
void main()
{
auto path = new bool[][]('I'+1, 'I'+1);
foreach (i; 'A'..'I'+1) {
if ((i - 'A') % 3) path[i][i-1] = 1;
if ((i - 'A') % 3 != 2) path[i][i+1] = 1;
if ((i - 'C') > 0) path[i][i-3] = 1;
if ((i - 'G') < 0) path[i][i+3] = 1;
}
foreach (line; stdin.byLine) {
auto str = line.chomp;
char pre = str[0];
bool f;
foreach (c; str[1..$]) {
if (!path[pre][c]) {
f = 1;
break;
}
pre = c;
}
if (!f) str.writeln;
}
} | D |
void main()
{
long[] tmp = readln.split.to!(long[]);
long a = tmp[0], b = tmp[1], c = tmp[2], k = tmp[3];
writeln(k & 1 ? b - a : a - b);
}
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;
int main()
{
while (true) {
int n = readln().chomp().to!int();
if (n == 0) break;
//writeln(dfs(0,0,0,n));
int p = 9999999;
for (int i = 0; i <= 25 && i * 200 <= n; i++) {
for (int j = 0; j <= 17 && i * 200 + j * 300 <= n; j++) {
for (int k = 0; k <= 10 && i * 200 + j * 300 + k * 500 <= n; k++) {
if (i * 200 + j * 300 + k * 500 == n) {
int t = i * 380 + j * 550 + k * 850;
t -= (i / 5) * 380;
t -= (j / 4) * 330;
t -= (k / 3) * 306;
p = min(p, t);
}
}
}
}
writeln(p);
}
return 0;
} | 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!int;
const int a = r.next!uint;
const int b = r.next!uint;
const int c = r.next!uint;
const int d = r.next!uint;
const u1 = n * (a - b), v1 = n * (a + b);
const u2 = c - d, v2 = c + d;
const u = max (u1, u2), v = min (v1, v2);
writeln ((u <= v) ? "Yes" : "No");
}
}
| D |
// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
int n;
string s, t;
void main() {
scan(n);
scan(s);
scan(t);
int ans = n;
while (!s.empty) {
if (s == t) {
writeln(ans);
return;
}
s.popFront();
t.popBack();
ans++;
}
writeln(ans);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
} | D |
import std.stdio;
int n;
short[] a;
ulong sumW() {
ulong sum;
for (size_t i = 0; i < 2 * n; i += 2)
sum += a[i];
return sum;
}
size_t j, k, t;
short maxH() {
short max;
for (size_t i = 1; i < 2 * n; i += 2)
if ((a[i] > max) && (i != j)) {
max = a[i];
k = i;
}
if (t < 1)
j = k;
++t;
return max;
}
void main() {
scanf("%d", &n);
short w, h;
foreach (i; 0 .. n) {
scanf("%hd%hd", &w, &h);
a ~= w, a ~= h;
}
ulong sum = sumW();
short max1 = maxH();
short max2 = maxH();
for (size_t i = 0; i < 2 * n; i += 2)
if (i != j - 1)
write((sum - a[i]) * max1, ' ');
else
write((sum - a[i]) * max2, ' ');
putchar('\n');
} | 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 a, b, c; readV(a, b, c);
writeln(a+b == c || b+c == a || c+a == b ? "Yes" : "No");
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto s = RD!string;
writeln(s.replace(",", " "));
stdout.flush();
debug readln();
} | D |
void main() {
int[] tmp = readln.split.to!(int[]);
int l = tmp[0] + tmp[1], r = tmp[2] + tmp[3];
if (l > r) {
"Left".writeln;
} else if (l == r) {
"Balanced".writeln;
} else {
"Right".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.range;
import std.conv;
void main()
{
auto ip = readln.split.to!(int[]), a = ip[0], b = ip[1];
if(a*b%2 == 1){
writeln("Odd");
}else{
writeln("Even");
}
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
void main()
{
auto N = readln.chomp.to!int;
int cnt;
foreach (n; 1..N+1) {
if (n % 2 == 0) continue;
int y;
foreach (x; 1..n+1) {
if (n % x == 0) ++y;
}
if (y == 8) ++cnt;
}
writeln(cnt);
} | D |
import std.stdio, std.conv, std.array, std.string;
import std.algorithm;
import std.container;
void main() {
auto a = readln.chomp.to!int;
auto s = readln.chomp;
writeln(a >= 3200 ? s : "red");
}
| D |
void main()
{
long n = readln.chomp.to!long;
long f = long.max;
for (long i = 1; i * i <= n; ++i)
{
if (n % i == 0)
{
long a = i.log10.to!long + 1;
long b = log10(n/i).to!long + 1;
f = min(f, max(a, b));
}
}
f.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.algorithm;
import std.array;
import std.bigint;
import std.bitmanip;
import std.conv;
import std.math;
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取りつつ計算
@nogc @safe 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;
}
// フェルマーの小定理から乗法逆元を計算
// 定理の要請により法は素数
@nogc @safe pure ulong modInv(ulong a, ulong m) {
return modPow(a, m-2, m);
}
// mod取りつつ順列を計算
@nogc @safe pure ulong modPerm(ulong n, ulong k, ulong m) {
if (n < k) return 0;
ulong r = 1;
for (ulong i = n-k+1; i <= n; i++) {
r *= i;
r %= m;
}
return r;
}
// mod取りつつ順列を計算
@nogc @safe pure ulong modFact(ulong n, ulong m) {
return modPerm(n, n, m);
}
// mod取りつつ組み合わせを計算
// modInvを使っているので法は素数
@nogc @safe pure ulong modComb(ulong n, ulong r, ulong m) {
return modPerm(n, r, m)*modInv(modFact(r, m), m) % m;
}
immutable ulong MOD = 1000000007;
// [min, max] に n の倍数がいくつあるか
ulong countMuls(ulong min, ulong max, ulong n) {
return max/n - (min-1)/n;
}
ulong mod_2019(ulong l, ulong r) {
if (l == 0) {
return 0;
}
const ulong mul_3 = countMuls(l, r, 3);
const ulong mul_673 = countMuls(l, r, 673);
if (mul_3 != 0 && mul_673 != 0) return 0;
if (mul_3 == 0) {
// r - l == 1
return (l * r % 2019);
}
// mul_3 != 0 && mul_673 == 0
return naive(l, r);
}
ulong naive(ulong l, ulong r) {
ulong min_mod = 2019;
for(ulong i = l; i < r; i++) {
for(ulong j = i + 1; j <= r; j++) {
const ulong tmp = (cast(BigInt)i * cast(BigInt)j) % 2019;
if (tmp < min_mod) {
min_mod = tmp;
}
}
}
return min_mod;
}
void main() {
ulong l, r;
readInto(l, r);
writeln(mod_2019(l, r));
}
| D |
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
import std.math;
auto s=readln.chomp.to!(char[]);
char[] t;
foreach(c; s)if(c!='x') t~=c;
bool ok=true;
foreach(i; 0..(t.length/2)) ok&=(t[i]==t[$-i-1]);
if(!ok){writeln(-1); return;}
int cnt=0;
int[] y;
foreach(c; s){
if(c=='x') cnt++;
else y~=cnt, cnt=0;
}
y~=cnt;
int sum=0;
foreach(i; 0..(y.length/2)) sum+=(y[i]-y[$-i-1]).abs;
writeln(sum);
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, 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; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }
long mod = 10^^9 + 7;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto Q = RD;
auto S = RD!string;
auto LR = new long[2][](Q);
foreach (i; 0..Q)
{
LR[i][0] = RD-1;
LR[i][1] = RD;
}
auto dp = new long[](N+1);
char last = S[0];
foreach (i; 1..N)
{
if ([last, S[i]] == "AC")
{
++dp[i+1];
}
last = S[i];
}
foreach (i; 0..N)
{
dp[i+1] += dp[i];
}
foreach (i; 0..Q)
{
auto l = LR[i][0];
auto r = LR[i][1];
auto ans = dp[r] - dp[l];
if (l != 0)
if (S[l] == 'C' && S[l-1] == 'A')
--ans;
writeln(ans);
}
debug writeln(dp);
stdout.flush();
debug readln();
} | D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
int a, b, c, d;
scanf("%d %d %d %d ", &a, &b, &c, &d);
writeln(min(a,b) + min(c,d));
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
| D |
void main()
{
long n = rdElem;
string[] s = 2.rdCol!string;
dchar[] vh = new dchar[n];
long result = 1L;
long pos;
while (pos < n)
{
if (s[0][pos] == s[1][pos])
{
vh[pos] = 'v';
if (pos > 0)
{
if (vh[pos-1] == 'v') result *= 2;
}
else result *= 3;
++pos;
}
else
{
vh[pos] = 'h', vh[pos+1] = 'h';
if (pos > 0)
{
if (vh[pos-1] == 'v') result *= 2;
else result *= 3;
}
else result *= 6;
pos += 2;
}
result %= mod;
}
result.writeln;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni; | D |
import std.stdio;
import core.stdc.stdio;
import std.algorithm;
int n,m;
int[][] map;
int ans=0;
void DFS(int y,int x,int count){
map[y][x] = 0;
int[] dx=[0,-1,0,1];
int[] dy=[-1,0,1,0];
for(int k=0;k<4;k++){
int ny = y+dy[k];
int nx = x+dx[k];
if(0<=ny&&ny<n&&0<=nx&&nx<m&&map[ny][nx] == 1)
DFS(ny,nx,count+1);
}
map[y][x] = 1;
ans = max(count,ans);
}
void main(){
while(1){
ans = 0;
scanf("%d%d",&m,&n);
if(m==0&&n==0)
break;
map = new int[][n];
for(int i=0;i<n;i++){
map[i] = new int[m];
for(int j=0;j<m;j++){
scanf("%d",&map[i][j]);
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(map[i][j] == 1)
DFS(i,j,1);
}
}
printf("%d\n",ans);
}
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto m = RD!int;
if (n == 1 || m == 1)
ans[ti] = true;
else if (n == 2 && m == 2)
ans[ti] = true;
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush;
debug readln;
} | D |
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n; rd(n);
auto a=new int[](n);
foreach(i; 0..n) rd(a[i]);
int[int] cnt;
foreach(e; a){
if(e in cnt) cnt.remove(e);
else cnt[e]=1;
}
writeln(cnt.length);
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
} | D |
import std.stdio, std.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.split.map!(to!long);
auto H = s[0];
auto W = s[1];
if (H % 3 == 0 || W % 3 == 0) {
writeln(0);
return;
}
long ans = min(H, W);
foreach (i; 1..W) {
long area1 = H * i;
long area2 = H / 2 * (W - i);
long area3 = (H / 2 + H % 2) * (W - i);
long tmp = max(max(abs(area1 - area2), abs(area2 - area3)), abs(area3 - area1));
ans = min(ans, tmp);
}
foreach (i; 1..H) {
long area1 = W * i;
long area2 = W / 2 * (H - i);
long area3 = (W / 2 + W % 2) * (H - i);
long tmp = max(max(abs(area1 - area2), abs(area2 - area3)), abs(area3 - area1));
ans = min(ans, tmp);
}
ans.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);}}
enum MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std;
// dfmt on
void main()
{
auto T = sread();
long N = T.length;
auto ans = new char[](N);
foreach (i; 0 .. N)
{
ans[i] = T[i];
if (T[i] == '?')
{
if (0 < i && ans[i - 1] == 'P')
{
ans[i] = 'D';
continue;
}
if (i < N - 1 && T[i + 1] == 'D')
{
ans[i] = 'P';
continue;
}
if (i < N - 1 && T[i + 1] == '?')
{
ans[i] = 'P';
continue;
}
ans[i] = 'D';
}
}
writeln(ans);
}
| D |
import std.stdio;void main(){writeln(cast(char)(readln[0]+1));} | D |
import core.stdc.stdio;
import std.traits;
import std.algorithm;
import std.typecons;
import std.stdio;
int[][] wall;
bool[][][] pass;
int[][][][] dist;
int[][][] buf;
int[] dx=[0,-1,0,1];
int[] dy=[-1,0,1,0];
static immutable int inf=114514;
int w,h;
void Clear(T,U)(ref T t,U u){
static if(isArray!T)
foreach(ref d;t)
d.Clear(u);
else
t=u;
}
void SetPass(){
pass.Clear(false);
foreach(i;0..h)
foreach(j;0..w){
if(i>0)
pass[i][j][0]=wall[i*2-1][j]==0;
if(j>0)
pass[i][j][1]=wall[i*2][j-1]==0;
if(i<h-1)
pass[i][j][2]=wall[i*2+1][j]==0;
if(j<w-1)
pass[i][j][3]=wall[i*2][j]==0;
}
}
bool inside(int x,int y){
return 0<=x&&x<w&&0<=y&&y<h;
}
int[] qx,qy,qd;
void BFS(int[][] dst){
SetPass;
dst.Clear(inf);
qx[0]=w-1;
qy[0]=h-1;
qd[0]=0;
dst[h-1][w-1]=0;
int q=1;
int i=0;
while(i<q){
int x=qx[i],y=qy[i],d=qd[i];
i++;
foreach(k;0..4){
int nx=x+dx[k],ny=y+dy[k];
if(pass[y][x][k]&&inside(nx,ny)&&dst[ny][nx]==inf){
qx[q]=nx;
qy[q]=ny;
qd[q++]=d+1;
dst[ny][nx]=d+1;
}
}
}
}
bool Solve(){
scanf("%d%d",&h,&w);
if(w==0&&h==0)
return false;
foreach(i;0..2*h-1)
foreach(j;0..w-1+(i&1))
scanf("%d",&wall[i][j]);
BFS(dist[0][0]);
if(dist[0][0][0][0]==inf){
printf("-1\n");
return true;
}
foreach(i;0..2*h-1)
foreach(j;0..w-1+(i&1))
if(wall[i][j]==0){
wall[i][j]=1;
BFS(dist[i][j]);
wall[i][j]=0;
if(dist[i][j][0][0]==inf){
printf("-1\n");
return true;
}
}
SetPass;
int[][] now=buf[0];
now.Clear(inf);
int[][] next=buf[1];
int ans=inf;
now[0][0]=0;
foreach(k;0..h*w){
next.Clear(inf);
foreach(i;0..h)
foreach(j;0..w){
if(pass[i][j][0])
next[i][j]=min(next[i][j],max(now[i-1][j],dist[i*2-1][j][i-1][j]+k));
if(pass[i][j][1])
next[i][j]=min(next[i][j],max(now[i][j-1],dist[i*2][j-1][i][j-1]+k));
if(pass[i][j][2])
next[i][j]=min(next[i][j],max(now[i+1][j],dist[i*2+1][j][i+1][j]+k));
if(pass[i][j][3])
next[i][j]=min(next[i][j],max(now[i][j+1],dist[i*2][j][i][j+1]+k));
}
swap(now,next);
ans=min(ans,max(k+1,now[h-1][w-1]));
}
printf("%d\n",ans);
return true;
}
void main(){
wall=new int[][](60,30);
pass=new bool[][][](30,30,4);
dist=new int[][][][](60,30,30,30);
buf=new int[][][](2,30,30);
qx=new int[1000];
qy=new int[1000];
qd=new int[1000];
while(Solve){}
} | D |
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
import std.range;
auto str=readln.chomp.to!(char[]);
int[char] cnt;
foreach(c; str){
if(!(c in cnt)) cnt[c]=1;
}
bool g(char[] s){
char tg=s[0];
bool ok=true;
foreach(c; s) ok&=(c==tg);
return ok;
}
int f(char c, char[] s){
int ret=0;
while(!g(s)){
foreach(i; 0..(s.length-1)){
if(s[i+1]==c) s[i]=c;
}
/* if(s.back!=c) */ s.popBack;
ret++;
}
return ret;
}
int mi=100;
foreach(c, v; cnt){
mi=min(mi, f(c, str.dup));
}
writeln(mi);
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
} | D |
import std.stdio;
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;
void main()
{
while (1) {
auto x = readln.chomp.split.map!(to!int);
if (x[0] == 0) break;
int res;
foreach (i; 1..x[2]) {
foreach (j; i..x[2]) {
auto pl = i * (100+x[0]) / 100 + j * (100+x[0]) / 100;
if (pl == x[2]) {
auto npl = i * (100+x[1]) / 100 + j * (100+x[1]) / 100;
res = max(res, npl);
}
}
}
res.writeln;
}
} | 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 = lread();
auto C = new long[](N - 1);
auto S = new long[](N - 1);
auto F = new long[](N - 1);
foreach (i; 0 .. N - 1)
scan(C[i], S[i], F[i]);
// writeln(C);
foreach (i; 0 .. N - 1)
{
long t = S[i];
t += C[i];
foreach (j; i + 1 .. N - 1)
{
// write(t, " ");
if (t < S[j])
t = S[j];
if (t % F[j] != 0)
t += F[j] - (t % F[j]);
t += C[j];
}
writeln(t);
}
writeln(0);
}
| D |
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
static import std.ascii;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
auto S = sread();
long state;
foreach (c; S)
{
if (state == 0 && c == 'C')
state = 1;
if (state == 1 && c == 'F')
state = 2;
}
writeln(state == 2 ? "Yes" : "No");
}
| D |
void main(){
auto N = readLine!long()[0];
long[string] dict;
foreach( i ; 0..N ){
dict[ readln().chomp() ] = 1;
}
writeln(dict.length);
}
import std.stdio, std.string, std.conv;
import std.math, std.algorithm, std.array;
import std.regex;
T[] readLine( T = size_t )( string sp = " " ){
T[] ol;
foreach( string elm ; readln().chomp().split(sp) ){
ol ~= elm.to!T();
}
return ol;
} | D |
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
readln.chomp.count('1').writeln;
}
| D |
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void main(string[] args){ args ~= ["", ""]; string cmd = args[1]; if(cmd == "-debug") DEBUG = 1;
if(cmd == "-gen") gen; else if(cmd == "-jury") jury; else solve; }
void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ std.stdio.write(t, " "), print(a); }
string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void gen(){}
void jury(){}
void solve(){
int n = scan!int;
int cnt;
string ans = "No";
foreach(_; 0 .. n){
int a = scan!int, b = scan!int;
if(a == b) cnt += 1;
else cnt = 0;
if(cnt >= 3) ans = "Yes";
}
ans.print;
} | D |
import std.stdio, std.conv, std.string, std.algorithm,
std.math, std.array, std.container, std.typecons;
bool solve() {
readln;
int[] a = readln.chomp.split.to!(int[]);
for(int i=0; i<a.length; i++) {
if(a[i]%2==0) {
if(a[i]%3==0 || a[i]%5==0) continue;
else return false;
}
}
return true;
}
void main() {
if(solve) writeln("APPROVED");
else writeln("DENIED");
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto S = RD!string;
auto cnt = new long[](26);
foreach (c; S)
++cnt[c-'a'];
cnt.sort!"a > b"();
writeln(cnt[0] <= 1 ? "yes" : "no");
stdout.flush();
debug readln();
} | 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 K = s[1];
auto S = readln.chomp;
int ans = 0;
for (char c = 'a'; c <= 'z'; ++c) {
int tmp = 0;
int v = 0;
foreach (i; 0..N) {
if (S[i] == c) {
tmp += 1;
if (tmp == K) {
v += 1;
tmp = 0;
}
} else {
tmp = 0;
}
}
ans = max(ans, v);
}
ans.writeln;
}
| D |
import std.stdio, std.string, std.array, std.conv;
struct Stack {
int[] x;
void push(int y) {
x ~= y;
}
int pop() {
int result = x[$-1];
--x.length;
return result;
}
}
void main() {
Stack s;
string[] c = readln.chomp.split.to!(string[]);
int a, b;
foreach (x; c) {
if (x == "+") {
a = s.pop;
b = s.pop;
s.push(b+a);
} else if (x == "-") {
a = s.pop;
b = s.pop;
s.push(b-a);
} else if (x == "*") {
a = s.pop;
b = s.pop;
s.push(b*a);
} else {
s.push(x.to!int);
}
}
s.pop.writeln;
}
| D |
/+ dub.sdl:
name "C"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
int main() {
Scanner sc = new Scanner(stdin);
long n;
sc.read(n);
foreach (i; 1..3501) {
foreach (j; 1..3501) {
long z = 4*i*j - n*(i+j);
if (z <= 0) continue;
long u = n * i * j;
if (u % z) continue;
writeln(i, " ", j, " ", u/z);
return 0;
}
}
return 0;
}
/* IMPORT /Users/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 /Users/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;
}
}
/*
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)
*/
| D |
import std.stdio;
import std.array;
import std.conv;
import std.string;
import std.algorithm;
void main() {
auto z = map!(to!int)(split(chomp(readln)));
string exp = "==";
if (z[0]<z[1]) exp = "<";
else if (z[0]>z[1]) exp = ">";
writeln("a ",exp," b");
} | D |
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int x; readV(x);
writeln(x < 1200 ? "ABC" : "ARC");
}
| D |
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int N, W;
scan(N, W);
auto w = new int[](N);
auto v = new int[](N);
iota(N).each!(i => scan(v[i], w[i]));
auto dp = new int[](W + 1);
foreach (i ; 0 .. N) {
foreach_reverse (j ; w[i] .. W + 1) {
chmax(dp[j], dp[j - w[i]] + v[i]);
}
}
auto ans = dp[W];
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
| D |
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.ascii;
void main()
{
auto a = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51];
auto k = readln.chomp.to!int;
a[k-1].writeln;
}
| D |
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii;
import std.typecons, std.functional;
import std.algorithm, std.container;
void main()
{
string S = readln.strip;
long res= long.max;
foreach(n; [0, 1])
{
long count;
foreach(i, c; S)
{
if(c == ((i % 2 == n) ? '1' : '0'))
{
count++;
}
}
res = min(count, res);
}
writeln(res);
}
class UnionFind{
UnionFind parent = null;
void merge(UnionFind a)
{
if(same(a)) return;
a.root.parent = this.root;
}
UnionFind root()
{
if(parent is null)return this;
return parent = parent.root;
}
bool same(UnionFind a)
{
return this.root == a.root;
}
}
void scanValues(TList...)(ref TList list)
{
auto lit = readln.splitter;
foreach (ref e; list)
{
e = lit.fornt.to!(typeof(e));
lit.popFront;
}
}
T[] scanArray(T = long)()
{
return readln.split.to!(long[]);
}
void scanStructs(T)(ref T[] t, size_t n)
{
t.length = n;
foreach (ref e; t)
{
auto line = readln.split;
foreach (i, ref v; e.tupleof)
{
v = line[i].to!(typeof(v));
}
}
}
T scanElem(T = long)()
{
char[] res;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
res ~= cast(char) c;
c = getchar;
}
return res.strip.to!T;
}
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
{
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
struct Factor
{
long n;
long c;
}
Factor[] factors(long n)
{
Factor[] res;
for (long i = 2; i ^^ 2 <= n; i++)
{
if (n % i != 0)
continue;
int c;
while (n % i == 0)
{
n = n / i;
c++;
}
res ~= Factor(i, c);
}
if (n != 1)
res ~= Factor(n, 1);
return res;
}
long[] primes(long n)
{
if(n<2)return [];
auto table = new long[n+1];
long[] res;
for(int i = 2;i<=n;i++)
{
if(table[i]==-1) continue;
for(int a = i;a<table.length;a+=i)
{
table[a] = -1;
}
res ~= i;
}
return res;
}
bool isPrime(long n)
{
if (n <= 1)
return false;
if (n == 2)
return true;
if (n % 2 == 0)
return false;
for (long i = 3; i ^^ 2 <= n; i += 2)
if (n % i == 0)
return false;
return true;
} | D |
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
static import std.ascii;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long a, b, c;
scan(a, b, c);
writeln((b - a == c - b) ? "YES" : "NO");
}
| D |
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto w = readln.chomp;
auto r = 0;
for (;;) {
auto t = readln.chomp;
if (t == "END_OF_TEXT") break;
auto ti = t.split;
r += ti.count!(a => icmp(a, w) == 0);
}
writeln(r);
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto ab = readln.split.to!(int[]);
auto A = ab[0];
auto B = ab[1];
auto C = new char[][](100, 100);
foreach (i; 0..50) C[i][] = '#';
foreach (i; 50..100) C[i][] = '.';
foreach (i; 0..A-1) {
auto h = i/50 * 2;
auto w = i%50 * 2;
C[h][w] = '.';
}
foreach (i; 0..B-1) {
auto h = 99 - i/50 * 2;
auto w = i%50 * 2;
C[h][w] = '#';
}
writeln("100 100");
foreach (c; C) writeln(c);
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
void main()
{
auto ab = readln.split.to!(int[]);
writeln(ab[0] - 1 + (ab[1] >= ab[0] ? 1 : 0));
} | 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 k, a, b;
scan(k, a, b);
if(b - a <= 2 || a >= k)
{
writeln(k + 1);
}
else
{
k -= a - 1;
writeln(a + (k / 2) * (b - a) + (k % 2));
}
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
| D |
import 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()
{
max(scanElem*scanElem,scanElem*scanElem).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;
// 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 r, d, x;
scan(r, d, x);
foreach (_; 0 .. 10)
{
x = r * x - d;
writeln(x);
}
}
| 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;
long ans = b / x - (a == 0 ? -1 : (a-1) / x);
writeln(ans);
stdout.flush();
debug readln();
} | 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 = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "a", long, "b");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
auto n = lread();
bool check = true;
long pre_t, pre_x, pre_y;
foreach (_; iota(n))
{
long t, x, y;
scan(t, x, y);
long dist = abs(x - pre_x) + abs(y - pre_y);
if (t - pre_t < dist)
check = false;
if ((dist - (t - pre_t)) % 2)
check = false;
pre_t = t;
pre_x = x;
pre_y = y;
}
if (check)
writeln("Yes");
else
writeln("No");
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
| D |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.