code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
void main() {
auto s = readln.strip;
auto cl = s.count!"a == 'x'";
writeln (cl <= 7 ? "YES" : "NO");
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int n;
rd(n);
auto a = readln.split.to!(int[]);
int s = 0;
foreach (i; 0 .. n) {
a[i] *= n;
s += a[i];
}
auto ave = s / n;
int best_idx = 0;
import std.math : abs;
foreach (int i; 1 .. n) {
if (abs(ave - a[best_idx]) > abs(ave - a[i])) {
best_idx = i;
}
}
writeln(best_idx);
}
void rd(T...)(ref T x) {
import std.stdio : readln;
import std.string : split;
import std.conv : to;
auto l = readln.split;
assert(l.length == x.length);
foreach (i, ref e; x)
e = l[i].to!(typeof(e));
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
void main() {
while(true) {
int M = readln.chomp.to!int;
if (M==0) break;
int n = readln.chomp.to!int;
int[] d = new int[n+2];
foreach(i; 0..n) d[i+1] = readln.chomp.to!int;
bool[][] edges = new bool[][](d.length, d.length);
bool[][] edges_reverse = new bool[][](d.length, d.length);
foreach(i; 0..d.length) {
foreach(j; 1..M+1) {
int k = min(d.length-1, i+j).to!int;
k += d[k];
k = max(0, min(d.length-1, k));
edges[i][k] = true;
edges_reverse[k][i] = true;
}
}
bool[] flg1 = new bool[d.length];
flg1[0] = true;
auto list = DList!(int)(0);
while(!list.empty) {
int id = list.front;
list.removeFront;
foreach(i, e; edges[id]) {
if (e && !flg1[i]) {
flg1[i] = true;
list.insertBack(i.to!int);
}
}
}
bool[] flg2 = new bool[d.length];
flg2[$-1] = true;
list.insertBack(d.length.to!int-1);
while(!list.empty) {
int id = list.front;
list.removeFront;
foreach(i, e; edges_reverse[id]) {
if (e && !flg2[i]) {
flg2[i] = true;
list.insertBack(i.to!int);
}
}
}
foreach(i; 0..d.length) {
if (flg1[i] && !flg2[i]) {
writeln("NG");
break;
}
if (i == d.length-1) {
writeln("OK");
}
}
}
}
|
D
|
void main() {
int[] arr;
arr ~= 1;
foreach(i; 2..1001) {
auto a = i;
auto b = a;
auto k = 2;
a *= a;
while(a <= 1000) {
arr ~= a;
k++;
a = b^^k;
}
}
auto X = ri;
while(true) {
if(arr.canFind(X)) {
X.writeln;
return;
}
X--;
}
}
// ===================================
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
T[] deepcopy(T)(T[] a) {
static if(isArray!T) {
T[] res;
foreach(i; a) {
res ~= deepcopy(i);
}
return res;
} else {
return a.dup;
}
}
|
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 m = RD!int;
auto a = RDA!int;
auto cnt = new long[](m);
foreach (e; a)
++cnt[e%m];
foreach (i; 1..(m+1)/2)
{
auto x = cnt[i];
auto y = cnt[m-i];
auto z = min(x, y);
x -= z;
y -= z;
if (z == 0)
ans[ti] += max(x, y);
else
ans[ti] += max(max(x, y), 1);
debug writeln("i:", i, " ans:", ans[ti]);
}
if (cnt[0] != 0)
++ans[ti];
if (m % 2 == 0 && cnt[m/2] != 0)
++ans[ti];
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
alias Pair = Tuple!(int, "a", int, "b");
void main() {
int N;
scan(N);
auto s = readln.chomp.map!(ch => ch - '0').array;
auto dp = new bool[][](1000, 4);
dp[0][0] = 1;
foreach (i ; 0 .. N) {
foreach_reverse (k ; 0 .. 4) {
foreach (j ; 0 .. 1000) {
if (dp[j][k] && k < 3) {
dp[10*j + s[i]][k + 1] |= dp[j][k];
}
}
}
}
int ans;
foreach (j ; 0 .. 1000) {
ans += dp[j][3];
}
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
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
long l, r; rd(l, r);
writeln("YES");
for(auto i=l; i+1<=r; i+=2){
writeln(i, " ", i+1);
}
}
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.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto H = RD;
auto W = RD;
auto K = RD;
auto S = new string[](H);
foreach (i; 0..H)
{
S[i] = RD!string;
}
long ans = long.max;
foreach (i; 0..2^^(H-1))
{
auto n = popcnt(cast(int)i)+1;
auto cnt = new long[][](n, W);
long pos;
foreach (y; 0..H)
{
foreach (x; 0..W)
{
if (S[y][x] == '1')
++cnt[pos][x];
}
auto bit = 1L << y;
if (i & bit)
{
++pos;
}
}
bool ok0 = true;
(){
foreach (j; 0..n)
{
foreach (x; 0..W)
{
if (cnt[j][x] > K)
{
ok0 = false;
return;
}
}
}}();
if (!ok0)
continue;
auto total = new long[](n);
long ans_t;
foreach (j; 0..W)
{
bool ok = true;
foreach (k; 0..n)
{
if (total[k] + cnt[k][j] > K)
{
ok = false;
break;
}
}
if (!ok)
{
++ans_t;
total[] = 0;
}
foreach (k; 0..n)
{
total[k] += cnt[k][j];
}
}
ans.chmin(ans_t + n - 1);
}
writeln(ans);
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)); }
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 n = RD!int;
auto dp = new long[](n);
dp[] = 1;
long tot = 1;
foreach (i; 1..n)
{
dp[i].moda(1);
dp[i].moda(tot);
tot.moda(dp[i]);
int x = (i+1)*2;
while (x <= n)
{
dp[x-1].moda(1);
x += i+1;
}
debug writeln(dp);
}
auto ans = dp[n-1];
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
import std.algorithm;
import std.conv;
import std.stdio;
import std.string;
void main()
{
auto ns = readln.strip;
writeln( solve( ns ) );
}
int solve( in string ns )
{
auto ds = ns.map!( a => a - '0' );
auto r = ds.sum;
if( 1 < r ) return r;
else return solve( ( ns.to!int / 2 ).to!string ) * 2;
}
unittest
{
assert( solve( "15" ) == 6 );
assert( solve( "100000" ) == 10 );
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto n = readln.chomp.to!int;
int sum;
foreach (i; 1..n+1) {
sum += i;
}
sum.writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
long[10^^5] AS, BS;
void main()
{
auto N = readln.chomp.to!int;
foreach (i; 0..N) {
auto ab = readln.split.to!(long[]);
AS[i] = ab[0];
BS[i] = ab[1];
}
long r;
foreach_reverse (i; 0..N) {
AS[i] += r;
r += AS[i] % BS[i] == 0 ? 0 : BS[i] - AS[i] % BS[i];
}
writeln(r);
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd = readln.split.to!(int[]), a = rd[0], b = rd[1], c = rd[2], d = rd[3];
writeln(max(0, min(b, d) - max(a, c)));
}
|
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()
{
writeln(readln.chomp.to!int^^3);
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
ulong MOD = 1_000_000_007;
ulong INF = 1_000_000_000_000;
ulong MIDDLE = 100_000;
alias Pair = Tuple!(long, "flag", long, "num");
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()
{
long n, m, make;
scan(n, m);
if (n <= m / 2)
{
make += n, m -= 2 * n;
make += m / 4;
}
else
{
make += m / 2;
}
make.writeln();
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int r; readV(r);
if (r < 1200)
writeln("ABC");
else if (r < 2800)
writeln("ARC");
else
writeln("AGC");
}
|
D
|
import std.stdio;
import std.conv;
void main()
{
char[] buf;
readln(buf);
for (int i = to!(int)(buf.length); i > 1; i--) {
write(buf[i-2]);
}
write("\n");
}
|
D
|
/*
AOJ 0501 'Data Conversion'
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0501
*/
import std.stdio;
import std.array;
import std.range;
import std.conv;
import std.string;
import std.algorithm;
void main()
{
while(true)
{
int N = readln().chomp().to!int;
if(N == 0) break;
int[char] table;
foreach(_; 0..N)
{
auto line = readln().split().map!("a.to!char");
table[line[0]] = line[1];
}
int M = readln().chomp().to!int;
string ans;
foreach(_; 0..M)
{
char c = readln()[0];
ans ~= (c in table ? table[c] : c);
}
writeln(ans);
}
}
|
D
|
import std.stdio, std.algorithm, std.string, std.conv, std.array;
void main() {
writeln(readln.chomp.map!(to!int).array.sort().map!(to!char).array == "abc" ? "Yes" : "No");
}
|
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;
immutable long MOD = 998244353;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto K = s[1];
s = readln.split.map!(to!int);
auto R = s[0];
auto S = s[1];
auto P = s[2];
auto T = readln.chomp;
int[dchar] score;
score['r'] = P;
score['s'] = R;
score['p'] = S;
auto past = new dchar[](N);
int ans = 0;
foreach (i; 0..N) {
if (i < K) {
ans += score[T[i]];
past[i] = T[i];
} else if (past[i-K] != T[i]) {
ans += score[T[i]];
past[i] = T[i];
} else {
past[i] = '.';
}
}
ans.writeln;
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}}
void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}}
void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}}
void main()
{
int m; readV(m);
writeln(24-m+24);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto S = RD!string;
writeln(S[$-1] == 's' ? S ~ "es" : S ~ "s");
stdout.flush;
debug readln;
}
|
D
|
import std.conv;
import std.stdio;
import std.string;
void main()
{
auto nk = readln.split.to!( int[] );
auto as = readln.split.to!( int[] );
writeln( solve( nk[ 1 ], as ) );
}
auto solve( in int k, in int[] as )
{
auto as_r = as.length - k;
auto k_r = k - 1;
auto c = 1 + as_r / k_r;
auto re = ( as_r % k_r == 0 ) ? 0 : 1;
return c + re;
}
unittest
{
assert( solve( 3, [ 2, 3, 1, 4 ] ) == 2 );
assert( solve( 3, [ 1, 2, 3 ] ) == 1 );
assert( solve( 3, [ 7, 3, 1, 8, 4, 6, 2, 5 ] ) == 4 );
assert( solve( 3, [ 7, 3, 6, 8, 4, 1, 2, 5 ] ) == 4 );
assert( solve( 3, [ 7, 3, 6, 1, 4, 8, 2, 5 ] ) == 4 );
assert( solve( 3, [ 7, 3, 6, 4, 1, 8, 2, 5 ] ) == 4 );
assert( solve( 3, [ 7, 3, 8, 1, 4, 6, 2, 5, 9 ] ) == 4 );
assert( solve( 3, [ 7, 3, 8, 4, 6, 1, 2, 5, 9 ] ) == 4 );
assert( solve( 3, [ 7, 3, 8, 4, 1, 6, 2, 5, 9 ] ) == 4 );
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
int n, k;
int[] a;
scan(n, k);
a = readln.split.to!(int[]);
auto dp1 = new bool[][](n + 1, k);
dp1[0][0] = 1;
foreach (i ; 1 .. n + 1) {
foreach (j ; 0 .. k) {
dp1[i][j] = dp1[i-1][j];
if (j - a[i-1] >= 0) dp1[i][j] |= dp1[i-1][j-a[i-1]];
}
}
auto dp2 = new bool[][](n + 1, k);
dp2[n][0] = 1;
foreach_reverse (i ; 0 .. n) {
foreach (j ; 0 .. k) {
dp2[i][j] = dp2[i+1][j];
if (j - a[i] >= 0) dp2[i][j] |= dp2[i+1][j-a[i]];
}
}
int ans;
foreach (i ; 0 .. n) {
int mv;
for (int l = 0, r = k-1; l < k; l++) {
if (!dp1[i][l]) continue;
for (; r >= 0; r--) {
if (dp2[i+1][r] && l + r < k) {
mv = max(mv, l + r);
break;
}
}
}
if (mv < k - a[i]) ans++;
}
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);
}
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main() {
string input = readln();
char a = input[0];
char b = input[1];
if(a == '9' || b == '9') {
"Yes".writeln;
} else {
"No".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;
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
alias sread = () => readln.chomp();
void main()
{
int[] nmm = aryread!int();
int N = nmm[0];
int MA = nmm[1];
int MB = nmm[2];
int[] A, B, C;
foreach (_; 0 .. N)
{
int[] abc = aryread!int();
A ~= abc[0];
B ~= abc[1];
C ~= abc[2];
}
auto dp = new int[][][](N + 1, N * 11 + 1, N * 11 + 1);
foreach (ref n; dp[0 .. $])
foreach (ref row; n)
row[] = int.max;
dp[0][0][0] = 0;
foreach (n; 0 .. N)
foreach (a; 0 .. N * 10 + 1)
foreach (b; 0 .. N * 10 + 1)
{
if (dp[n][a][b] != int.max)
{
dp[n + 1][a + A[n]][b + B[n]] = min(dp[n + 1][a + A[n]][b + B[n]],
dp[n][a][b] + C[n]);
}
dp[n + 1][a][b] = min(dp[n + 1][a][b], dp[n][a][b]);
}
int ma = MA;
int mb = MB;
int result = int.max;
while (ma < N * 10 + 1 && mb < N * 10 + 1)
{
if (dp[N][ma][mb] != 0)
{
result = result.min(dp[N][ma][mb]);
}
ma += MA;
mb += MB;
}
// writeln(dp[N]);
writeln(result == int.max ? -1 : result);
}
|
D
|
import std.stdio;
void main() {
int n, k, s;
scanf("%d %d %d\n", &n, &k, &s);
if (s == 10^^9) {
foreach(_;0..n-k) {
write(1, " ");
}
foreach(_;0..k) {
write(10^^9, " ");
}
}
else {
foreach(_;0..n-k) {
write(s+1, " ");
}
foreach(_;0..k) {
write(s, " ");
}
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main() {
int a = to!int(chomp(readln()));
int b = to!int(chomp(readln()));
int h = to!int(chomp(readln()));
int surface = (a + b) * h / 2;
surface.writeln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new int[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto s = RD!string;
if (n % 2)
{
bool ok;
foreach (i; 0..n)
{
if (i % 2 == 0)
{
if ((s[i]-'0') % 2)
ok = true;
}
}
ans[ti] = ok ? 1 : 2;
}
else
{
bool ok;
foreach (i; 0..n)
{
if (i % 2)
{
if ((s[i]-'0') % 2 == 0)
ok = true;
}
}
ans[ti] = ok ? 2 : 1;
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
void main(){
import std.stdio, std.conv, std.string, std.algorithm;
long n, a, b, k; rd(n, a, b, k);
const long mod=998244353;
const int M=1_000_00*4;
auto fact=new long[](M);
fact[0]=fact[1]=1;
foreach(i; 2..M) fact[i]=i*fact[i-1]%mod;
auto inv_fact=new long[](M);
long powmod(long a, long x){
if(x==0) return 1;
else if(x==1) return a;
else if(x&1) return a*powmod(a, x-1)%mod;
else return powmod(a*a%mod, x/2);
}
foreach(i; 0..M) inv_fact[i]=powmod(fact[i], mod-2);
long comb(long nn, long rr){
long ret=fact[nn]%mod;
(ret*=inv_fact[rr])%=mod;
(ret*=inv_fact[nn-rr])%=mod;
return ret;
}
long tot=0;
for(long x=0; x<=n; x++)if(k-a*x>=0){
if((k-a*x)%b==0){
long y=(k-a*x)/b;
if(y>n) continue;
(tot+=comb(n, x)*comb(n, y)%mod)%=mod;
}
}
writeln(tot);
}
/*
xマス赤で塗るとする (0<=x<=N)
塗り方:comb(N, x)通り
残りK-A*x点
Bがこれを割るなら
y=(K-A*x)/B マス青で上書きする
y<=Nなら
comb(N, x)*comb(N, y)
*/
/*
(4C2)*2+(4C3)*3+(4C3)*3+(4C4)*4 = 40
x=0 ... 残り5点 -> 無理
x=1 ... 残り4点 -> y=2 -> (4C1)*(4C2)=24
x=2 ... 残り3点 -> 無理
x=3 ... 残り2点 -> y=1 -> (4C3)*(4C1)=16
*/
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 core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
auto T = lreads(N);
T.reduce!lcm().writeln();
}
T lcm(T)(T a, T b)
{
return a / gcd(a, b) * b;
}
|
D
|
void main() {
int n = readln.chomp.to!int;
writeln(800 * n - n / 15 * 200);
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.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.stdlib;
void main() {
auto N = readln.chomp.to!long;
long[] F;
for (long i = 1; i * i <= (N - 1); ++i) {
if ((N - 1) % i == 0) {
F ~= i;
if (i * i != (N - 1)) {
F ~= (N - 1) / i;
}
}
}
F.sort();
long[] G;
for (long i = 1; i * i <= N; ++i) {
if (N % i == 0) {
G ~= i;
if (i * i != N) {
G ~= N / i;
}
}
}
G.sort();
long ans = F.length.to!int - 2;
foreach (g; G) if ((N / g - 1) % g == 0) ans += 1;
for (long i = 2; i * i <= N; ++i) {
for (long x = i * i; x <= N; x *= i) {
if (N % x != 0) continue;
long y = N / x - 1;
if (y % i == 0) ans += 1;
}
}
ans.writeln;
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
static import std.ascii;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long H, A;
scan(H, A);
writeln((H + A - 1) / A);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto a = RDA;
foreach (i; 0..n)
{
bool ok = true;
for (long j = 1; j*j <= a[i]; ++j)
{
if (j*j == a[i])
ok = false;
}
if (ok)
{
ans[ti] = true;
break;
}
}
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush;
debug readln;
}
|
D
|
import core.stdc.stdio;
long gcd(long a,long b){
return b?gcd(b,a%b):a;
}
long lcd(long a,long b){
return a/gcd(a,b)*b;
}
void main(){
while(1){
int n;
scanf("%d",&n);
if(!n)
break;
auto p=new long[n],q=new long[n];
auto r=new int[n],b=new int[n],ic=new int[n];
foreach(i;0..n){
scanf("%lld%lld%d%d",&p[i],&q[i],&r[i],&b[i]);
if(--r[i]>=0)
ic[r[i]]++;
if(--b[i]>=0)
ic[b[i]]++;
}
int root;
foreach(int i,c;ic)
if(!c)
root=i;
long dfs(int i){
if(i<0)
return 1;
long s=dfs(r[i]),t=dfs(b[i]);
//s*p[i]=t*q[i]
long l=lcd(s*p[i],t*q[i]);
s=l/p[i];
t=l/q[i];
return s+t;
}
printf("%lld\n",dfs(root));
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm, std.array, std.range, std.container;
import std.math;
void main() {
auto data = readln.split, H = data[0].to!uint, W = data[1].to!uint;
uint[501][501] coin;
foreach (i; 0 .. H) foreach (j, a; readln.split.to!(uint[])) {
coin[i][j] = a;
}
alias Point = Tuple!(int, "x", int, "y");
Point[501*501] p;
int dy = 1;
uint y;
foreach (x; 0 .. H) {
foreach(i; 0 .. W)
p[x*W + i] = Point(x, y), y += dy;
y -= dy;
dy = -dy;
}
Tuple!(Point, Point)[] points;
foreach (i; 0 .. H*W-1) {
if (coin[p[i].x][p[i].y] % 2 == 1) {
points ~= tuple(p[i], p[i+1]);
coin[p[i].x][p[i].y]--, coin[p[i+1].x][p[i+1].y]++;
}
}
writeln(points.length);
foreach (a; points) {
writeln(a[0].x+1, " ", a[0].y+1, " ", a[1].x+1, " ", a[1].y+1 );
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
long P = 10^^9+7;
long[10^^6+50] F, RF;
long pow(long x, long n) {
long y = 1;
while (n) {
if (n%2 == 1) y = (y * x) % P;
x = x^^2 % P;
n /= 2;
}
return y;
}
long inv(long x)
{
return pow(x, P-2);
}
void init()
{
F[0] = F[1] = 1;
foreach (i, ref x; F[2..$]) x = (F[i+1] * (i+2)) % P;
{
RF[$-1] = 1;
auto x = F[$-1];
auto k = P-2;
while (k) {
if (k%2 == 1) RF[$-1] = (RF[$-1] * x) % P;
x = x^^2 % P;
k /= 2;
}
}
foreach_reverse(i, ref x; RF[0..$-1]) x = (RF[i+1] * (i+1)) % P;
}
long comb(N)(N n, N k)
{
if (k > n) return 0;
auto n_b = F[n]; // n!
auto nk_b = RF[n-k]; // 1 / (n-k)!
auto k_b = RF[k]; // 1 / k!
auto nk_b_k_b = (nk_b * k_b) % P; // 1 / (n-k)!k!
return (n_b * nk_b_k_b) % P; // n! / (n-k)!k!
}
Tuple!(N, N)[] prime_division(N)(N n)
{
Tuple!(N, N)[] res;
foreach (N i; 2..10^^6+1) {
if (n%i == 0) {
N cnt;
while (n%i == 0) {
++cnt;
n /= i;
}
res ~= tuple(i, cnt);
}
}
if (n != cast(N)1) res ~= tuple(n, cast(N)1);
return res;
}
void main()
{
init();
auto nm = readln.split.to!(long[]);
auto N = nm[0];
auto M = nm[1];
auto ps = prime_division(M);
long r = 1;
foreach (p; ps) {
r = r * comb(p[1]+N-1, N-1) % P;
}
writeln(r);
}
|
D
|
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
void main() {
try {
for (; ; ) {
const N = readInt();
auto A = new long[N];
foreach (i; 0 .. N) {
A[i] = readLong();
}
auto s = new long[2];
foreach (i; 0 .. N) {
s[i % 2] += A[i] / 2;
s[1 - i % 2] += A[i] - A[i] / 2;
}
const ans = min(s[0], s[1]);
writeln(ans);
}
} catch (EOFException e) {
}
}
|
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;
long[][] factor(long n) {
if (n == 1) return [[1, 1]];
long[][] res;
if (n % 2 == 0) {
int k = 0;
while (n % 2 == 0) { n /= 2; k++; }
res ~= [2, k];
}
for (long i = 3; i * i <= n; i += 2) {
if (n % i != 0) continue;
int k = 0;
while (n % i == 0) { n /= i; k++; }
res ~= [i, k];
}
if (n > 1) res ~= [n, 1];
return res;
}
long calc(long a, long b) {
long g = gcd(a, b);
if (g == 1) return 1;
return factor(g).length + 1;
}
void main() {
long a, b; scan(a, b);
writeln(calc(a, b));
}
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
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
// dfmt on
void main()
{
long N, D;
scan(N, D);
long a = (2 * D + 1);
writeln((N + a - 1) / a);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string;
void main()
{
readln;
auto s = readln.chomp;
string k;
string kk;
string l;
int p;
foreach (c; s) {
switch (c) {
case '(':
if (!p++) {
kk ~= k;
k = "";
}
k ~= '(';
break;
case ')':
if (p) {
--p;
k = k ~ ')';
} else {
kk = '(' ~ kk;
k ~= ')';
}
break;
default:
}
}
while (p--) k ~= ')';
writeln(kk ~ k);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math;
void main()
{
auto s = readln.chomp.to!(dchar[]);
s[3] = '8';
writeln(s);
}
|
D
|
import std.stdio, std.string, std.array, std.algorithm, std.conv, std.typecons, std.numeric, std.math;
struct UFTree(T)
{
struct Node
{
T parent;
T rank = 1;
}
///
this(T n)
{
nodes.length = n;
sizes.length = n;
foreach (i, ref node; nodes) {
node = Node(i.to!T);
sizes[i] = 1;
}
}
///
bool unite(T a, T b)
{
a = root(a);
b = root(b);
if (a == b) return false;
if (nodes[a].rank < nodes[b].rank) {
sizes[nodes[a].parent] += sizes[nodes[b].parent];
nodes[b].parent = nodes[a].parent;
} else {
sizes[nodes[b].parent] += sizes[nodes[a].parent];
nodes[a].parent = nodes[b].parent;
if (nodes[a].rank == nodes[b].rank) nodes[a].rank += 1;
}
return true;
}
///
bool is_same(T a, T b)
{
return root(a) == root(b);
}
///
T size(T i)
{
return sizes[root(i)];
}
private:
Node[] nodes;
T[] sizes;
T root(T i)
{
if (nodes[i].parent == i) return i;
return nodes[i].parent = root(nodes[i].parent);
}
}
///
UFTree!T uftree(T)(T n)
{
return UFTree!T(n);
}
void main()
{
auto nq = readln.split.to!(int[]);
auto N = nq[0];
auto Q = nq[1];
auto uft = uftree(N);
foreach (_; 0..Q) {
auto cxy = readln.split.to!(int[]);
if (cxy[0] == 0) {
uft.unite(cxy[1], cxy[2]);
} else {
writeln(uft.is_same(cxy[1], cxy[2])+0);
}
}
}
|
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 lim = 24 * 3600 + 1;
void main() {
while (true) {
int n;
scan(n);
if (!n) return;
auto c = new int[](lim);
foreach (i ; 0 .. n) {
string s, t;
scan(s, t);
auto l = toSec(s);
auto r = toSec(t);
c[l]++;
c[r]--;
}
foreach (i ; 1 .. lim) {
c[i] += c[i-1];
}
auto ans = c.reduce!max;
writeln(ans);
}
}
int toSec(string hms) {
int res;
res += hms[0 .. 2].to!int * 3600;
res += hms[3 .. 5].to!int * 60;
res += hms[6 .. 8].to!int;
return res;
}
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;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
void main()
{
auto s = sread();
// writeln(s);
// writeln(s[0 .. (s.length) / 2]);
// writeln(s[(s.length) / 2 .. $]);
foreach (i; 2 .. (s.length))
{
// writeln(s[0 .. $ - i]);
if (func(s[0 .. $ - i]))
{
writeln(s[0 .. $ - i].length);
return;
}
}
}
auto func(string s)
{
if (s[0 .. (s.length) / 2] == s[(s.length) / 2 .. $])
{
return true;
}
else
{
return false;
}
}
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.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.functional;
void main() {
int n = readln.chomp.to!int;
int[] a = readln.split.to!(int[]);
int k = a.reduce!min, m = a.reduce!max;
if (m - k > 1) {
writeln("No");
return;
}
if (k == m) {
writeln(k == n - 1 || 2*k <= n ? "Yes" : "No");
}
else {
int p = a.count(k).to!int;
writeln(p <= k && 2*(k + 1 - p) <= n - p ? "Yes" : "No");
}
}
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);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto K = RD;
auto s = RD!string;
string ans = s[0..K-1];
if (s[K-1] == 'A')
ans ~= "a";
else if (s[K-1] == 'B')
ans ~= "b";
else
ans ~= "c";
ans ~= s[K..$];
writeln(ans);
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 tt = readln.split.to!(long[]);
auto T1 = tt[0];
auto T2 = tt[1];
auto aa = readln.split.to!(long[]);
auto A1 = aa[0];
auto A2 = aa[1];
auto bb = readln.split.to!(long[]);
auto B1 = bb[0];
auto B2 = bb[1];
if (B1 < A1) {
swap(A1, B1);
swap(A2, B2);
}
auto x = (A1 - B1) * T1;
auto y = (A2 - B2) * T2;
if (x+y < 0) {
writeln(0);
} else if (x+y == 0) {
writeln("infinity");
} else {
auto d = x+y;
writeln(-x/d * 2 + (-x%d == 0 ? 0 : 1));
}
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
long calc(int n, int[] a, int[] b) {
long ans = 0;
long x = 0;
for (int i = 0; i < n; i++) {
ans += min(a[i], b[i]);
int d = b[i] - a[i];
if (d > 0) {
ans += min(a[i+1], d);
a[i+1] = max(0, a[i+1] - d);
}
}
return ans;
}
void main() {
int n; scan(n);
auto a = readints;
auto b = readints;
writeln(calc(n, a, b));
}
|
D
|
void main(){
int[] n = _scanln();
foreach(i; 0..n[0]+1 ){
if(n[1] == 2*i + 4*(n[0]-i)){
writeln("Yes");
return;
}
}
writeln("No");
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.array;
void main()
{
int num;
num = chomp(readln()).to!int;
string[][4] card;
for(int i = 0; i < num; ++i){
string[] tmp = split(chomp(readln()));
if(tmp[0] == "S")
card[0] ~= tmp[1];
else if(tmp[0] == "H")
card[1] ~= tmp[1];
else if(tmp[0] == "C")
card[2] ~= tmp[1];
else if(tmp[0] == "D")
card[3] ~= tmp[1];
}
bool[13][4] flag = false;
for(int i = 0; i < 4; ++i){
for(int j = 0; j < card[i].length; ++j){
flag[i][(card[i][j].to!int)-1] = true;
}
}
for(int i = 0; i < 4; ++i){
for(int j = 0; j < 13; ++j){
if(!flag[i][j]){
if(i == 0)
writeln("S ", j+1);
else if(i == 1)
writeln("H ", j+1);
else if(i == 2)
writeln("C ", j+1);
else if(i == 3)
writeln("D ", j+1);
}
}
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
long P = 10^^9+7;
long[10^^5+50] F, RF;
void init()
{
F[0] = F[1] = 1;
foreach (i, ref x; F[2..$]) x = (F[i+1] * (i+2)) % P;
{
RF[$-1] = 1;
auto x = F[$-1];
auto k = P-2;
while (k) {
if (k%2 == 1) RF[$-1] = (RF[$-1] * x) % P;
x = x^^2 % P;
k /= 2;
}
}
foreach_reverse(i, ref x; RF[0..$-1]) x = (RF[i+1] * (i+1)) % P;
}
long comb(N)(N n, N k)
{
auto n_b = F[n]; // n!
auto nk_b = RF[n-k]; // 1 / (n-k)!
auto k_b = RF[k]; // 1 / k!
auto nk_b_k_b = (nk_b * k_b) % P; // 1 / (n-k)!k!
return (n_b * nk_b_k_b) % P; // n! / (n-k)!k!
}
int[10^^5+10] MEMO;;
void main()
{
init();
auto n = readln.chomp.to!int;
auto as = readln.split.to!(int[]);
int l, r;
foreach (int i, a; as) {
if (MEMO[a]) {
l = MEMO[a];
r = i+1;
break;
} else {
MEMO[a] = i + 1;
}
}
foreach (i; 0..n+1) {
++i;
if (i == 1) {
writeln(n);
} else if (i == n+1) {
writeln(1);
} else {
auto res = comb(n+1, i);
if (l-1 + (n+1 - r) >= i - 1) res = (res - comb(l-1 + (n+1 - r), i - 1) + P) % P;
writeln(res);
}
}
}
|
D
|
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math, std.container, std.typecons;
import core.stdc.stdio;
// foreach, foreach_reverse, writeln
void main() {
string s;
s = readln().chomp;
int n = s.length.to!int;
if (s[n-1] == '1') {
writeln(-1);
return;
}
if (s[0] == '0') {
writeln(-1);
return;
}
foreach (i; 0..n-1) {
if (s[i] != s[n-2-i]) {
writeln(-1);
return;
}
}
int v = 2, front = 2;
writeln(1,' ',2);
foreach (i; 1..n-1) {
v++;
writeln(front,' ',v);
if (s[i] == '1') {
front = v;
}
}
}
|
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.random;
import std.datetime.systime : Clock;
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;
auto a = r.nextA!int (n);
int res;
foreach (i; 1 .. n) {
auto x = a[i - 1], y = a[i];
if (x == 2 && y == 3 || x == 3 && y == 2) {
writeln ("Infinite");
return;
}
if (x == 1 && y == 2) {
res += 3;
if (i >= 2 && a[i - 2] == 3) --res;
} else if (x == 1 && y == 3) {
res += 4;
} else if (x == 2 && y == 1) {
res += 3;
} else if (x == 3 && y == 1) {
res += 4;
}
/*
else if (x == 3 && y == 2) {
res += 4;
}
*/
debug stderr.writeln (x, " ", y, " ", res);
}
writeln ("Finite");
writeln (res);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nab = readln.split.to!(int[]);
auto A = nab[1];
auto B = nab[2];
writeln(A%2 == B%2 ? "Alice" : "Borys");
}
|
D
|
import std.algorithm,
std.string,
std.array,
std.range,
std.stdio,
std.conv;
void main() {
string[] n = readln.chomp.split("");
if (n[0] == n[2]) {
writeln("Yes");
} else {
writeln("No");
}
}
|
D
|
import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;
import std.numeric;
void main()
{
int n, m;
readln.chomp.split.tie(n, m);
char[][] board = new char[][](n);
foreach (i; 0..n) {
board[i] = readln.chomp.dup;
}
bool yes = true;
enum OFS = [
[0, 1],
[1, 0],
[-1, 0],
[0, -1],
[1, 1],
[1, -1],
[-1, 1],
[-1, -1]
];
foreach (y; 0..n) {
foreach (x; 0..m) {
if (board[y][x] == '*') {
continue;
}
int v;
if (board[y][x] == '.') {
v = 0;
} else {
v = board[y][x] - '0';
}
int cnt = 0;
foreach (d; OFS) {
int nx = x + d[0];
int ny = y + d[1];
if (nx < 0 || m <= nx
|| ny < 0 || n <= ny) {
continue;
}
if (board[ny][nx] == '*') {
++cnt;
}
}
yes &= v == cnt;
}
}
writeln = yes ? "YES" : "NO";
}
void tie(R, Args...)(R arr, ref Args args)
if (isRandomAccessRange!R || isArray!R)
in
{
assert (arr.length == args.length);
}
body
{
foreach (i, ref v; args) {
alias T = typeof(v);
v = arr[i].to!T;
}
}
void verbose(Args...)(in Args args)
{
stderr.write("[");
foreach (i, ref v; args) {
if (i) stderr.write(", ");
stderr.write(v);
}
stderr.writeln("]");
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii;
import std.typecons, std.functional, std.traits;
import std.algorithm, std.container;
void main()
{
long A = scanElem;
long B = scanElem;
long C = scanElem;
if(A>B)swap(A,B);
if(A<C&&C<B){
writeln("Yes");
}else{
writeln("No");
}
}
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));
}
}
}
long scanULong(){
long x;
while(true){
const c = getchar;
if(c<'0'||c>'9'){
break;
}
x = x*10+c-'0';
}
return x;
}
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);
}
}
}
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);
}
}
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 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; }
void main(){
long a = read.to!long;
long b = read.to!long;
long c = read.to!long;
long ans;
if(c > a + b + 1) ans = b + (a + b + 1);
else ans = b + c;
ans.writeln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
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 p = RDR.ARR;
long ans;
foreach (i; 1..n-1)
{
if (p[i] < p[i-1] && p[i] > p[i+1])
++ans;
else if (p[i] > p[i-1] && p[i] < p[i+1])
++ans;
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
long A, B;
scan(A, B);
bool b = (A * B) % 2 == 0;
writeln(!b ? "Odd" : "Even");
}
|
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 o = sread();
auto e = sread();
auto s = (o.length) + (e.length);
foreach (i; 0 .. s)
{
if (i % 2 == 0)
{
write(o[i / 2]);
}
else
{
write(e[i / 2]);
}
}
}
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.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 s = RD!string;
writeln(a >= 3200 ? s : "red");
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
enum lim = 2 * 10^^5;
void main() {
auto s = eratos(lim);
auto t = new bool[](lim);
foreach (i ; 3 .. lim) {
if (s[i] && s[(i + 1) / 2]) {
t[i] = true;
}
}
auto ts = new int[](lim + 1);
foreach (i ; 1 .. lim + 1) {
ts[i] = ts[i - 1] + t[i - 1];
}
int q;
scan(q);
foreach (i ; 0 .. q) {
int li, ri;
scan(li, ri);
ri++;
int ans = ts[ri] - ts[li];
writeln(ans);
}
}
bool[] eratos(int n) {
auto s = new bool[](n);
s[] = true;
s[0] = s[1] = false;
for (int p = 2; p*p < n; p++) {
if (s[p]) {
for (int d = p*p; d < n; d += p) {
s[d] = false;
}
}
}
return s;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.container;
import std.bigint;
import std.math;
void main()
{
auto n = readln.chomp.to!int;
foreach (i; 1..n+1) {
auto x = i;
if (x % 3 == 0) {
write(" ", i);
continue;
}
while (x) {
if (x % 10 == 3) {
write(" ", i);
break;
}
x /= 10;
}
}
writeln("");
}
|
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;
const MOD = 1000000000 + 7;
class FermetCalculator {
long[] factrial; //階乗を保持
long[] inverse; //逆元を保持
this(long size) {
factrial = new long[size + 1];
inverse = new long[size + 1];
factrial[0] = 1;
inverse[0] = 1;
for (long i = 1; i <= size; i++) {
factrial[i] = (factrial[i - 1] * i) % MOD; //階乗を求める
inverse[i] = pow(factrial[i], MOD - 2) % MOD; // フェルマーの小定理で逆元を求める
}
}
long combine(long n, long k) {
return factrial[n] * inverse[k] % MOD * inverse[n - k] % MOD;
}
long combine_naive(long n, long k) {
auto bunshi = reduce!((x, i) => (x * (n - i + 1)) % MOD)(1L, iota(1, k+1));
auto bumbo = reduce!((x, i) => (x * i) % MOD)(1L, iota(1, k+1));
return bunshi * pow(bumbo, MOD - 2) % MOD;
}
long pow(long x, long n) { //x^n 計算量O(logn)
long ans = 1;
while (n > 0) {
if ((n & 1) == 1) {
ans = ans * x % MOD;
}
x = x * x % MOD; //一周する度にx, x^2, x^4, x^8となる
n >>= 1; //桁をずらす n = n >> 1
}
return ans;
}
}
void main() {
auto INPUT = readln.split.to!(long[]);
auto N = INPUT[0];
auto A = INPUT[1];
auto B = INPUT[2];
auto fermetCalculator = new FermetCalculator(1);
ulong solve() {
auto answer = fermetCalculator.pow(2, N) - 1;
auto a = fermetCalculator.combine_naive(N, A);
auto b = fermetCalculator.combine_naive(N, B);
answer = answer >= a ? answer - a : MOD - a + answer;
answer = answer >= b ? answer - b : MOD - b + answer;
return answer;
}
solve().writeln;
}
|
D
|
import std.stdio,std.string,std.conv,std.array;
void main(){
int[4][] data;
for(int i=0;;i++){
auto rd = readln().chomp();
if(rd){
data.length = i+1;
auto abcd = split(rd);
data[i][0] = to!int(abcd[0]);
data[i][1] = to!int(abcd[1]);
data[i][2] = to!int(abcd[2]);
data[i][3] = to!int(abcd[3]);
}else{
break;
}
}
for( int i=0;i<data.length;i+=2 ){
int hit=0;
if( data[i][0] == data[i+1][0] ){ hit+=1; }
if( data[i][1] == data[i+1][1] ){ hit+=1; }
if( data[i][2] == data[i+1][2] ){ hit+=1; }
if( data[i][3] == data[i+1][3] ){ hit+=1; }
write( hit , " " );
int blow=0;
if( data[i][0] == data[i+1][1] ){ blow+=1; }
if( data[i][0] == data[i+1][2] ){ blow+=1; }
if( data[i][0] == data[i+1][3] ){ blow+=1; }
if( data[i][1] == data[i+1][0] ){ blow+=1; }
if( data[i][1] == data[i+1][2] ){ blow+=1; }
if( data[i][1] == data[i+1][3] ){ blow+=1; }
if( data[i][2] == data[i+1][0] ){ blow+=1; }
if( data[i][2] == data[i+1][1] ){ blow+=1; }
if( data[i][2] == data[i+1][3] ){ blow+=1; }
if( data[i][3] == data[i+1][0] ){ blow+=1; }
if( data[i][3] == data[i+1][1] ){ blow+=1; }
if( data[i][3] == data[i+1][2] ){ blow+=1; }
writeln( blow );
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
int[] xs, ys;
foreach (_; 0..N) {
auto rc = readln.split.to!(int[]);
xs ~= rc[1];
ys ~= rc[0];
}
xs.length = 8;
ys.length = 8;
bool solve(int p, int n) {
if (n == 8) return true;
if (p == 64) return false;
auto x = p%8;
auto y = p/8;
foreach (i; 0..n) {
if (x == xs[i] || y == ys[i] || x+ys[i] == y+xs[i] || x+y == xs[i]+ys[i]) goto ng;
}
xs[n] = x;
ys[n] = y;
if (solve(p+1, n+1)) return true;
ng:
return solve(p+1, n);
}
solve(0, N);
auto res = new char[][](8, 8);
foreach (ref r; res) r[] = '.';
foreach (i; 0..8) res[ys[i]][xs[i]] = 'Q';
foreach (r; res) writeln(r);
}
|
D
|
void main(){
string s = readln().chomp();
char tmpc;
foreach(ch; s){
if(tmpc == ch){
writeln("Bad");
return;
}
tmpc = ch;
}
writeln("Good");
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
|
D
|
import std.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!long;
long a = 0;
long b = 0;
foreach (i; 0..10^^6) {
b += 1;
a += b;
if (a >= N) {
b.writeln;
return;
}
}
}
|
D
|
void main()
{
long[] tmp = rdRow;
long n = tmp[0];
long a = tmp[1], b = tmp[2], c = tmp[3], d = tmp[4];
string s = rdStr;
bool ok = !s[a..max(c, d)].canFind("##");
if (c > d) ok &= s[b-2..d+1].canFind("...");
writeln(ok ? "Yes" : "No");
}
T rdElem(T = long)()
{
//import std.stdio : readln;
//import std.string : chomp;
//import std.conv : to;
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
dchar[] rdDchar()
{
//import std.conv : to;
return rdStr.to!(dchar[]);
}
T[] rdRow(T = long)()
{
//import std.stdio : readln;
//import std.array : split;
//import std.conv : to;
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
//import std.range : iota;
//import std.algorithm : map;
//import std.array : array;
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
//import std.range : iota;
//import std.algorithm : map;
//import std.array : array;
return iota(col).map!(x => rdRow!T).array;
}
void wrMat(T = long)(T[][] mat)
{
//import std.stdio : write, writeln;
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import 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();
bool[long] D;
foreach (_; 0 .. N)
{
long A = lread();
D[A] = (A in D) ? !D[A] : true;
}
D.values.sum().writeln();
}
|
D
|
import std.stdio, std.algorithm, core.bitop;
void main(){
int A, B, K, ans;
scanf("%d%d%d", &A, &B, &K);
foreach(bit; 0..1<<10)if(popcnt(bit) <= K){
ans = max(ans, test(A, B, bit));
}
writeln(ans);
}
int test(int A, int B, int bit){
int carry = 0, ten = 1, C = 0;
for(;A;A/=10, B/=10, ten*=10, bit>>=1){
int x = A%10, y = B%10;
if(x - carry >= y){
C += ten * (x - carry - y);
carry = 0;
}
else{
C += ten * (x - carry + 10 - y);
carry = (bit&1) ? 0 : 1;
}
}
return C;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm, std.array, std.range, std.container;
import std.math;
void main() {
auto data = readln.split;
auto N = data[0].to!int, M = data[1].to!int, C = data[2].to!int;
auto B = readln.split.to!(int[]);
int ans = 0;
foreach (i; 0 .. N) {
auto row = readln.split.to!(int[]);
M.iota.each!(i => row[i] *= B[i]);
if (row.sum + C > 0) { ans++; }
}
writeln(ans);
}
|
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()
{
int N, Q;
scan(N, Q);
auto uf = dsu(N);
foreach (_; 0 .. Q)
{
int t, u, v;
scan(t, u, v);
if (t == 0)
{
uf.merge(u, v);
}
else
{
writeln(uf.same(u, v) ? 1 : 0);
}
}
}
struct dsu
{
public:
this(int n)
{
_n = n, parent_or_size = new int[](n);
parent_or_size[] = -1;
}
int merge(int a, int b)
{
assert(0 <= a && a < _n);
assert(0 <= b && b < _n);
int x = leader(a), y = leader(b);
if (x == y)
return x;
if (-parent_or_size[x] < -parent_or_size[y])
{
auto tmp = x;
x = y;
y = tmp;
}
parent_or_size[x] += parent_or_size[y];
parent_or_size[y] = x;
return x;
}
bool same(int a, int b)
{
assert(0 <= a && a < _n);
assert(0 <= b && b < _n);
return leader(a) == leader(b);
}
int leader(int a)
{
assert(0 <= a && a < _n);
if (parent_or_size[a] < 0)
return a;
return parent_or_size[a] = leader(parent_or_size[a]);
}
int size(int a)
{
assert(0 <= a && a < _n);
return -parent_or_size[leader(a)];
}
int[][] groups()
{
auto leader_buf = new int[](_n), group_size = new int[](_n);
foreach (i; 0 .. _n)
{
leader_buf[i] = leader(i);
group_size[leader_buf[i]]++;
}
auto result = new int[][](_n);
foreach (i; 0 .. _n)
result[i].reserve(group_size[i]);
foreach (i; 0 .. _n)
result[leader_buf[i]] ~= i;
int[][] filtered;
foreach (r; result)
if (r.length != 0)
filtered ~= r;
return filtered;
}
private:
int _n;
int[] parent_or_size;
}
|
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;
long calc(int h, int w) {
long _calc(long h, long w) {
long ans = long.max;
for (int i = 1; i < w; i++) {
long a = h * i;
// h を分割
long h1 = h / 2;
long h2 = h - h1;
long b = h1 * (w - i);
long c = h2 * (w - i);
ans = min(ans, max(a, b, c) - min(a, b, c));
}
for (int i = 1; i < w - 1; i++) {
long a = h * i;
// w を分割
long w1 = (w - i) / 2;
long w2 = (w - i) - w1;
long b = h * w1;
long c = h * w2;
ans = min(ans, max(a, b, c) - min(a, b, c));
}
return ans;
}
return min(_calc(h, w), _calc(w, h));
}
void main() {
auto hw = readints;
int h = hw[0], w = hw[1];
writeln(calc(h, w));
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (ti; 0..t)
{
auto a = RD!string;
auto b = RD!string;
auto c = RD!string;
bool ok = true;
foreach (i; 0..a.length)
{
if (a[i] == c[i] || b[i] == c[i]) continue;
ok = false;
break;
}
ans[ti] = ok;
}
foreach (e; ans)
{
writeln(e ? "YES" : "NO");
}
stdout.flush;
debug readln;
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int k, s; readV(k, s);
auto ans = 0;
foreach (x; 0..k+1)
foreach (y; 0..k+1) {
auto z = s-x-y;
if (z >= 0 && z <= k) ++ans;
}
writeln(ans);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
int[] ss, ls, ps;
foreach (_; 0..N) {
auto slp = readln.split.to!(int[]);
ss ~= slp[0];
ls ~= slp[1];
ps ~= slp[2];
}
auto DP = new int[][](N+1, 393+1);
foreach (ref dp; DP) dp[] = int.min;
DP[0][0] = 0;
foreach (i; 0..N) {
foreach (f; 0..393+1) {
DP[i+1][f] = max(DP[i+1][f], DP[i][f]);
foreach (d; ss[i]..ls[i]+1) {
if (f+d <= 393) DP[i][f+d] = max(DP[i][f+d], DP[i][f] + ps[i]);
}
}
}
foreach (i; 1..N+1) {
foreach (f; 0..393+1) {
DP[i][f] = max(DP[i][f], DP[i-1][f]);
}
}
auto M = readln.chomp.to!int;
int[] res;
foreach (_; 0..M) {
auto m = DP[N][readln.chomp.to!int];
if (m <= 0) {
writeln(-1);
return;
}
res ~= m;
}
foreach (r; res) writeln(r);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new long[](t);
foreach (ti; 0..t)
{
auto s = RD!(char[]);
auto n = s.length;
auto ss = new long[](n);
foreach (i; 0..n)
ss[i] = s[i];
long x = 1000;
foreach (i; 0..n)
{
auto j1 = i+1;
auto j2 = i+2;
if (j1 < n)
{
if (ss[i] == ss[j1])
{
ss[j1] = x;
++x;
++ans[ti];
}
}
if (j2 < n)
{
if (ss[i] == ss[j2])
{
ss[j2] = x;
++x;
++ans[ti];
}
}
}
}
foreach (e; ans)
{
writeln(e);
}
stdout.flush;
debug readln;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
char A, B;
scan(A, B);
writeln((A == B) ? "H" : "D");
}
|
D
|
import std;
import core.bitop;
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, "x", long, "c");
alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less);
void main()
{
auto n = lread();
auto a = aryread();
long[long] R;
foreach (i; iota(n))
{
if ((i - a[i]) in R)
R[(i - a[i])]++;
else
R[(i - a[i])] = 1;
}
long cnt;
foreach (i; iota(n))
{
cnt += (i + a[i]) in R ? R[i + a[i]] : 0;
}
cnt.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
|
void main() {
string s1 = readln.chomp;
string s2 = readln.chomp;
string s3 = readln.chomp;
writeln(s1[0], s2[1], s3[2]);
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.algorithm;
import std.array;
import std.range;
import std.conv;
import std.stdio;
import std.string;
import std.typecons;
alias FairyPos = Tuple!(int,int);
void main(){
auto blue_num = readln.chomp.to!int;
string[] blues;
foreach(i; iota(0, blue_num)) {
blues ~= readln.chomp;
}
auto red_num = readln.chomp.to!int;
string[] reds;
foreach(i; iota(0, red_num)) {
reds ~= readln.chomp;
}
auto pluss = get_num_by_string(blues);
// writeln(pluss);
auto minus = get_num_by_string(reds);
// writeln(minus);
int[string] scores;
foreach(str, num; pluss) {
int minus_score = str in minus ? minus[str] : 0;
scores[str] = pluss[str] - minus_score;
}
int max_num;
foreach(score; scores) {
max_num = score > max_num ? score : max_num;
}
max_num.writeln;
}
int[string] get_num_by_string(string[] raw){
int[string] result;
raw.each!((str){
if(str in result){
result[str]++;
}else{
result[str] = 1;
}
});
return result;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
enum P = 10L^^9+7;
void main()
{
auto N = readln.chomp.to!long;
auto AS = readln.split.to!(ulong[]);
long[61][] CS;
CS.length = N;
foreach_reverse (i; 0..N) {
foreach (j; 0..61) {
if (i != N-1) CS[i][j] = CS[i+1][j];
if (AS[i] & (1L<<j)) ++CS[i][j];
}
}
long r;
foreach (i; 0..N-1) {
auto a = AS[i];
foreach (j; 0..61) {
long x;
if (a & (1L<<j)) {
x = (((1L<<j) % P) * (N - i - 1 - CS[i+1][j])) % P;
} else {
x = (((1L<<j) % P) * CS[i+1][j]) % P;
}
r = (r + x) % P;
}
}
writeln(r);
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, core.bitop;
int r, c;
int[] sb;
void main() {
string s = readln.chomp;
int n = s.length.to!int;
bool[] e = new bool[](26);
foreach (ch ; s) {
e[ch - 'a'] = 1;
}
if (e.count(1) == 1) {
writeln(0);
return;
}
int ans = 1000;
foreach (char ch; 'a' .. 'z' + 1) {
if (!e[ch - 'a']) continue;
auto t = s.dup;
int cnt;
foreach (i ; 0 .. n - 1) {
cnt++;
foreach (j ; 0 .. n - 1 - i) {
if (t[j + 1] == ch) {
t[j] = ch;
}
}
bool flag = true;
foreach (j ; 0 .. n - 1 - i) {
if (t[j] != ch) {
flag = false;
break;
}
}
debug {
stderr.writeln(t[0 .. n - 1 - i]);
}
if (flag) break;
}
ans = min(ans, cnt);
}
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);
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
int q = read.to!int;
foreach(t; 0 .. q){
long a = read.to!long;
long b = read.to!long;
if(a == b) (a * 2 - 2).writeln;
else{
long r = sqrt((a * b - 1).to!real).to!long;
if(r * (r + 1) < a * b) (r * 2 - 1).writeln;
else (r * 2 - 2).writeln;
}
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
auto ab = readints;
int a = ab[0], b = ab[1];
bool found = iota(1, 3).map!(c => a * b * c).any!(e => e % 2 == 1);
writeln(found ? "Yes" : "No");
}
|
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()
{
string s; readV(s);
auto b = new bool[](26);
foreach (c; s) b[c-'a'] = true;
foreach (i; 0..26)
if (!b[i]) {
writeln(dchar(i+'a'));
return;
}
writeln("None");
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
enum L = 2001;
void main() {
int n, k;
scan(n, k);
auto a = readln.split.to!(int[]);
int I;
foreach (i ; 1 .. n) {
foreach (j ; 0 .. i) {
if (a[j] > a[i]) {
I++;
}
}
}
long ans = 1L * I * k % mod;
auto c = new int[](L);
foreach (i ; 0 .. n) {
c[a[i]]++;
}
auto cc = new int[](L + 1);
foreach (i ; 1 .. L + 1) {
cc[i] = cc[i - 1] + c[i - 1];
}
long p;
foreach (i ; 0 .. L) {
p += c[i] * cc[i] % mod;
p %= mod;
}
ans += p * (1L * k * (k - 1) / 2 % mod) % mod;
ans %= mod;
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
|
void main()
{
string s = readln.chomp;
long n = s.countUntil('C');
long m = -1;
if (n != -1)
{
m = s[n+1..$].countUntil('F');
}
writeln(m != -1 ? "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
|
void main() {
problem();
}
void problem() {
const S = scan;
auto N = S.length;
long solve() {
if (N < 4) return 0;
const P = 2019;
long ans;
auto cs = new long[](P);
cs[0] = 1;
long x, t = 1;
foreach_reverse (c; S) {
x = ((c-'0').to!long * t + x) % P;
t = (t * 10) % P;
ans += cs[x];
++cs[x];
}
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");
import std.bigint, std.functional;
const MOD = 100_000_000_7;
// -----------------------------------------------
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
void main() {
int n;
scan(n);
auto a = iota(n).map!(i => readln.chomp.to!int).array;
foreach (s ; 0 .. 1<<n) {
int deg;
foreach (i ; 0 .. n) {
if (s & (1 << i)) {
deg += a[i];
deg %= 360;
}
else {
deg -= a[i];
deg %= 360;
}
}
if (deg == 0) {
writeln("YES");
return;
}
}
writeln("NO");
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
/+ dub.sdl:
name "D"
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);
int n; long k;
sc.read(n, k);
long[] a = new long[n];
long[] b = new long[n];
foreach (i; 0..n) {
sc.read(a[i], b[i]);
}
long ans = 0;
void query(long z) {
long sm = 0;
foreach (i; 0..n) {
if (!(~z & a[i])) sm += b[i];
}
// writeln(z, " ", sm);
ans = max(ans, sm);
}
query(k);
foreach (i; 0..40) {
if (!(k & (1L<<i))) continue;
query(k ^ (1L<<i) | ((1L<<i)-1));
}
writeln(ans);
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.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.numeric;
void main()
{
auto n = readln.chomp.to!int;
bool[string] cnt;
foreach (_; 0..n) {
auto s = readln.chomp;
cnt[s] = true;
}
cnt.keys.length.writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
int main() {
int[string] hash;
int n = to!int(readln.chomp);
string[] s = readln.chomp.split;
foreach (ref c; s) {
hash[c]++;
}
writeln(hash.length == 3 ? "Three" : "Four");
return (0);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
import core.bitop;
alias H = Tuple!(int, "r", string, "s");
void main()
{
auto N = readln.chomp.to!int;
auto S = readln.chomp;
long[H] ls, rs;
foreach (b; 0..(1<<N)) {
auto ll = new char[](N);
auto rr = new char[](N);
int j, k = N-1, l, m = N-1;
foreach (i; 0..N) {
if (b & (1<<i)) {
ll[j++] = S[i];
rr[m--] = S[2*N-i-1];
} else {
ll[k--] = S[i];
rr[l++] = S[2*N-i-1];
}
}
auto r = popcnt(b);
++ls[H(r, ll.to!string)];
++rs[H(r, rr.to!string)];
}
long r;
foreach (k, v; rs) {
auto l = H(N - k.r, k.s);
if (l in ls) r += v * ls[l];
}
writeln(r);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math;
void main()
{
writeln(readln.to!(wchar[]).count!(c => c == '1'));
}
|
D
|
import std.stdio, std.array, std.conv, std.typecons, std.algorithm;
T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; }
void main() {
immutable ip = readln.split.to!(ulong[]);
immutable n = ip[0], m = ip[1];
auto ls = new ulong[m];
auto rs = new ulong[m];
for(ulong i = 0; i < m; i++) {
immutable ip1 = readln.split.to!(ulong[]);
immutable l = ip1[0], r = ip1[1];
ls[i] = l;
rs[i] = r;
}
ulong max_l = ls[0], min_r = rs[0];
ulong invalid = false;
for (ulong i = 1; i < m; i++) {
if (ls[i] > max_l) max_l = ls[i];
if (rs[i] < min_r) min_r = rs[i];
if (max_l > min_r) {invalid = true; break;}
}
writeln(invalid ? 0 : min_r-max_l+1);
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.