code
stringlengths 4
1.01M
| language
stringclasses 2
values |
---|---|
import std.array;
import std.range;
import std.algorithm;
import std.conv;
import std.stdio;
import std.string;
void main(string[] args) {
string s = readln.chomp;
string[] words = ["dream", "dreamer", "erase", "eraser"];
bool ans = false;
loop: while (true) {
if (s.length <= 0) {
ans = true;
break loop;
} else {
foreach (word; words) {
if (s.endsWith(word)) {
s = s[0..($-word.length)];
continue loop;
}
}
break loop;
}
}
(ans ? "YES" : "NO").writeln;
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto n = readln.chomp.to!int;
auto s = readln.chomp;
int r, b;
foreach (i; 0..n) {
if (s[i] == 'R') r++;
else b++;
}
if (r > b) writeln("Yes");
else writeln("No");
}
|
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 inputs = readln.chomp.split.map!(to!int);
auto a = inputs[0];
auto b = inputs[1];
auto c = inputs[2];
string answer;
if(c <= a + b){
answer = "Yes";
}else{
answer = "No";
}
writeln(answer);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n;
scan(n);
auto adj = new int[][](n, 0);
foreach (i ; 0 .. n - 1) {
int ai, bi;
scan(ai, bi);
ai--, bi--;
adj[ai] ~= bi;
adj[bi] ~= ai;
}
auto c = new int[](n);
auto d = new int[](n);
auto p = new int[](n);
void dfs(int v, int par) {
p[v] = par;
c[v] = 1;
foreach (u ; adj[v]) {
if (u == par) continue;
d[u] = d[v] + 1;
dfs(u, v);
c[v] += c[u];
}
}
dfs(0, 0);
int u = n - 1;
foreach (i ; 0 .. (d[n - 1] + 1) / 2 - 1) {
u = p[u];
}
debug {
writeln(c);
writeln(d);
writeln(p);
writeln(u);
}
auto ans = 2*c[u] >= n ? "Snuke" : "Fennec";
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, 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;
alias Edge = Tuple!(int, "to", int, "idx");
void main() {
int N;
scan(N);
auto ans = new int[](N - 1);
ans[] = -1;
auto adj = new Edge[][](N, 0);
foreach (i ; 0 .. N - 1) {
int ai, bi;
scan(ai, bi);
ai--, bi--;
adj[ai] ~= Edge(bi, i);
adj[bi] ~= Edge(ai, i);
}
int K = 0;
void dfs(int v, int p, int pc) {
int col = 1;
foreach (e ; adj[v]) if (e.to != p) {
if (col == pc) col++;
ans[e.idx] = col;
K = max(K, col);
dfs(e.to, v, col);
col++;
}
}
dfs(0, -1, -1);
writeln(K);
ans.each!(ansi => writeln(ansi));
}
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.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;
void main()
{
auto _ = readln.chomp.split.map!(to!long);
long n = _.front;
long m = _.back;
/+
enum ofs = [
[-1, -1], [0, -1], [1, -1],
[-1, 0], [0, 0], [1, 0],
[-1, 1], [0, 1], [1, 1]
];
bool[][] field = new bool[][](n, m);
auto f = (int x, int y) {
return x < 0 || m <= x
|| y < 0 || n <= y;
};
foreach (i; 0..n) {
foreach (j; 0..m) {
foreach (d; ofs) {
int nx = cast(int)j + d[0];
int ny = cast(int)i + d[1];
if (f(nx, ny)) {
continue;
}
field[ny][nx] = !field[ny][nx];
}
}
}
foreach (i; 0..n) {
foreach (j; 0..m) {
if (field[i][j]) {
"o".write;
} else {
"x".write;
}
}
writeln;
}
+/
if (n == 1 && m == 1) {
1.writeln;
} else if (min(n, m) == 1) {
writeln(max(0, max(n, m) - 2));
} else {
writeln = n * m - 2 * n - 2 * m + 4;
}
}
|
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;
int[] di = [1, 1, -1, -1];
int[] dj = [1, -1, 1, -1];
void main() {
int n;
scan(n);
auto m = iota(n).map!(i => readln.chomp).array;
int ans;
foreach (i ; 1 .. n - 1) {
foreach (j ; 1 .. n - 1) {
if (m[i][j] != 'X') continue;
bool ok = true;
foreach (k ; 0 .. 4) {
int ni = i + di[k];
int nj = j + dj[k];
if (m[ni][nj] != 'X') {
ok = false;
break;
}
}
ans += ok;
}
}
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.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()
{
bool[string] c;
auto a = readln.chomp.split;
foreach (e; a) {
c[e] = true;
}
c.keys.length.writeln;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
writeln(readln.chomp.group.canFind!"a[1] >= 3" ? "Yes" : "No");
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
/* imports all std modules {{{*/
import
std.algorithm,
std.array,
std.ascii,
std.base64,
std.bigint,
std.bitmanip,
std.compiler,
std.complex,
std.concurrency,
std.container,
std.conv,
std.csv,
std.datetime,
std.demangle,
std.encoding,
std.exception,
std.file,
std.format,
std.functional,
std.getopt,
std.json,
std.math,
std.mathspecial,
std.meta,
std.mmfile,
std.net.curl,
std.net.isemail,
std.numeric,
std.parallelism,
std.path,
std.process,
std.random,
std.range,
std.regex,
std.signals,
std.socket,
std.stdint,
std.stdio,
std.string,
std.system,
std.traits,
std.typecons,
std.uni,
std.uri,
std.utf,
std.uuid,
std.variant,
std.zip,
std.zlib;
/*}}}*/
/+---test
contest
son
---+/
/+:---test
contest
programming
---+/
/+:---test
contest
sentence
---+/
void main(string[] args) {
const S = readln.chomp;
const T = readln.chomp;
const Sn = S.length;
long[][] chpos;
chpos.length = 'z'-'a';
foreach (i, c; S) {
chpos[c-'a'] ~= i;
}
long rotation;
long offset;
foreach (i, c; T) {
auto pos = chpos[c-'a'].assumeSorted;
if (pos.length == 0) {
"-1".writeln;
return;
}
auto upper_pos = pos.upperBound(offset-1);
if (upper_pos.length > 0) {
offset = upper_pos[0]+1;
} else {
offset = pos[0]+1;
++rotation;
}
}
(rotation*Sn + offset).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()
{
long a, b;
scan(a, b);
writeln(a * b);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
import std.concurrency;
void times(alias fun)(int n) {
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
long rec(int depth, long D, long repunit) {
if (repunit == 0) return D==0 ? 1 : 0;
long d = D%10;
long a = depth==0?1:0;
return (10-d.abs-a) * rec(depth+1, (D-repunit*d)/10, repunit/100);
}
void main() {
long D = readln.chomp.to!long;
if (D%9 != 0) {
0.writeln;
return;
}
D /= 9;
long ans = 0;
long repunit = 1;
foreach(i; 2..18+1) {
long a = i%2==1 ? 10:1;
ans += a * rec(0, D, repunit);
repunit = repunit*10 + 1;
}
ans.writeln;
}
// ----------------------------------------------
// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
return reduce!fun(tuple(seed), r);
}
}
}
}
// cumulativeFold was added in D 2.072.0
static if (__VERSION__ < 2072) {
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
}
// minElement/maxElement was added in D 2.072.0
static if (__VERSION__ < 2072) {
auto minElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto minimum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b < minimum) {
element = a;
minimum = b;
}
}
return element;
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto maximum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b > maximum) {
element = a;
maximum = b;
}
}
return element;
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string;
wchar[600][600] BD1, BD2;
void main()
{
auto N = readln.chomp.to!int;
foreach (i; 0..N) {
foreach (j, c; readln.chomp) {
BD1[i][j] = BD1[i][N+j] = BD1[N+i][j] = BD1[N+i][N+j]= c;
BD2[j][i] = BD2[j][N+i] = BD2[N+j][i] = BD2[N+j][N+i] = c;
}
}
int cnt;
foreach (a; 0..N) {
foreach (b; 0..N) {
foreach (i; 0..N) {
if (BD1[a+i][b..N+b] != BD2[b+i][a..N+a]) goto bad;
}
++cnt;
bad:
}
}
writeln(cnt);
}
|
D
|
import std.stdio; // ??\????????????????????????
import std.string; // chomp????????????????????????(?????????????????????)
import std.conv; // to????????????????????????
import std.array; // split?????????????????????
import std.algorithm; // map?????????????????????
void main() {
auto input = readln.split.map!(to!int);
int a = input[0];
int b = input[1];
int c = input[2];
int cnt = 0;
foreach (i; a .. (b + 1)) {
if (c % i == 0) {
cnt++;
}
}
writeln(cnt);
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
enum MAX = 1_000_100;
ulong MOD = 1_000_000_007;
ulong INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias Pass = Tuple!(long, "x", long, "y");
void main()
{
auto s = sread();
long l = s.length / 2, cnt;
foreach(i; iota(l))
{
if(s[i] != s[$ - i - 1])
cnt++;
}
cnt.writeln();
}
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;
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto S = readln.chomp;
writeln(S[2] == S[3] && S[4] == S[5] ? "Yes" : "No");
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
import std.concurrency;
void times(alias fun)(int n) {
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
// fold was added in D 2.071.0.
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
return reduce!fun(tuple(seed), r);
}
}
}
void main() {
readln.chomp.to!long.pipe!"(a-1)/11*2+((a+10)%11<6?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;
enum mod = 1_000_000_007L;
void main() {
int q;
scan(q);
while (q--) {
string s, t;
scan(s);
scan(t);
auto ans = solve(s, t);
writeln(ans);
}
}
int solve(string s, string t) {
auto n = s.length.to!int;
auto m = t.length.to!int;
auto dp = new int[][](n + 1, m + 1);
foreach (i ; 1 .. n + 1) {
foreach (j ; 1 .. m + 1) {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
if (s[i - 1] == t[j - 1]) chmax(dp[i][j], dp[i - 1][j - 1] + 1);
}
}
auto res = dp[n][m];
return res;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container;
import std.numeric, std.math;
import core.bitop;
T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
void ERR(T)(in T x) { stderr.writeln(x); }
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 S = RD!string;
auto T = RD!string;
ulong pos = ulong.max;
foreach (i; 0..S.length-T.length+1)
{
bool ng = false;
foreach (j, c; T)
{
if (S[i+j] != c && S[i+j] != '?')
{
ng = true;
break;
}
}
if (!ng)
{
pos = i;
}
}
if (pos == ulong.max) writeln("UNRESTORABLE");
else
{
auto ans = S.dup;
foreach (i; 0..T.length)
{
ans[pos+i] = T[i];
}
ans = ans.replace("?", "a");
writeln(ans);
}
stdout.flush();
}
|
D
|
import std.stdio, std.array, std.conv, std.string, std.range, std.algorithm;
void main() {
long n = readln.strip.to!(long);
string s = readln.strip;
long k = readln.strip.to!(long);
writeln(cast(dchar[])(s.map!(c => (c == s[k-1]) ? c : '*').array));
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto S = readln.chomp;
writeln(S.length > 3 && S[0..4] == "YAKI" ? "Yes" : "No");
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto n = readln.chomp.to!int;
auto s = readln.chomp;
if (n % 2 == 0 && s[0..n/2] == s[n/2..n]) {
writeln("Yes");
} else {
writeln("No");
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math;
void main()
{
auto X = readln.chomp.to!int;
auto A = readln.chomp.to!int;
auto B = readln.chomp.to!int;
writeln((X - A) % B);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
int n, k, x, y;
scan(n);
scan(k);
scan(x);
scan(y);
writeln(x*(min(k, n)) + y*(max(0, n - k)));
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.string, std.conv;
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;
void main() {
string s, t;
scan(s);
scan(t);
auto n = s.length;
bool ok;
foreach (i ; 0 .. n) {
auto sr = s[i .. $] ~ s[0 .. i];
if (sr == t) ok = true;
}
writeln(ok ? "Yes" : "No");
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
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, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;
void main(){
int h, w;
{
int[] tmp = readln.chomp.split.map!(to!int).array;
h = tmp[0], w = tmp[1];
}
int count;
for(int i = 0; i < h; i ++){
string s = readln.chomp;
foreach(c; s) if(c == '#') count += 1;
}
string ans;
if(count == h + w - 1) ans = "Possible";
else ans = "Impossible";
ans.writeln;
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int a, b; rd(a, b);
if(a+b==15){
writeln("+");
}else if(a*b==15){
writeln("*");
}else{
writeln("x");
}
}
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;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
import std.concurrency;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;
void main() {
recurrence!"a[n-1]+a[n-2]"(2L, 1L).take(readln.chomp.to!int + 1).array.back.writeln;
}
// ----------------------------------------------
void scanln(Args...)(ref Args args) {
foreach(i, ref v; args) {
"%d".readf(&v);
(i==args.length-1 ? "\n" : " ").readf;
}
// ("%d".repeat(args.length).join(" ") ~ "\n").readf(args);
}
void times(alias fun)(int n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
return reduce!fun(tuple(seed), r);
}
}
}
}
// cumulativeFold was added in D 2.072.0
static if (__VERSION__ < 2072) {
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
}
// minElement/maxElement was added in D 2.072.0
static if (__VERSION__ < 2072) {
auto minElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto minimum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b < minimum) {
element = a;
minimum = b;
}
}
return element;
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto maximum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b > maximum) {
element = a;
maximum = b;
}
}
return element;
}
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.7.4"
+/
import std.stdio, std.algorithm, std.range, std.conv;
import std.numeric : gcd;
// import dcomp.foundation, dcomp.scanner;
int main() {
Scanner sc = new Scanner(stdin);
int n;
sc.read(n);
long[] a;
sc.read(a);
int zc = a.count(1).to!int;
if (zc) {
writeln(n - zc);
return 0;
}
int ans = 10000;
foreach (l; 0..n) {
long g = 0;
foreach (r; l+1..n+1) {
g = gcd(g, a[r-1]);
if (g == 1) {
ans = min(ans, 2*(r-l-1) + l + (n-r));
}
}
}
if (ans == 10000) ans = -1;
writeln(ans);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
import core.bitop : popcnt;
static if (!__traits(compiles, popcnt(ulong.max))) {
public import core.bitop : popcnt;
int popcnt(ulong v) {
return popcnt(cast(uint)(v)) + popcnt(cast(uint)(v>>32));
}
}
bool poppar(ulong v) {
v^=v>>1;
v^=v>>2;
v&=0x1111111111111111UL;
v*=0x1111111111111111UL;
return ((v>>60) & 1) != 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
// import dcomp.array;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
FastAppender!(E[]) buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/array.d */
// module dcomp.array;
T[N] fixed(T, size_t N)(T[N] a) {return a;}
struct FastAppender(A, size_t MIN = 4) {
import std.algorithm : max;
import std.conv;
import std.range.primitives : ElementEncodingType;
import core.stdc.string : memcpy;
private alias T = ElementEncodingType!A;
private T* _data;
private uint len, cap;
@property size_t length() const {return len;}
bool empty() const { return len == 0; }
void reserve(size_t nlen) {
import core.memory : GC;
if (nlen <= cap) return;
void* nx = GC.malloc(nlen * T.sizeof);
cap = nlen.to!uint;
if (len) memcpy(nx, _data, len * T.sizeof);
_data = cast(T*)(nx);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void opOpAssign(string op : "~")(T item) {
if (len == cap) {
reserve(max(MIN, cap*2));
}
_data[len++] = item;
}
void insertBack(T item) {
this ~= item;
}
void removeBack() {
len--;
}
void clear() {
len = 0;
}
ref inout(T) back() inout { assert(len); return _data[len-1]; }
ref inout(T) opIndex(size_t i) inout { return _data[i]; }
T[] data() {
return (_data) ? _data[0..len] : null;
}
}
/*
This source code generated by dcomp and include dcomp's source code.
dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp)
dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt)
*/
|
D
|
import std.stdio;
import std.algorithm;
import std.conv;
import std.range;
import std.string;
import std.regex;
void main() {
int n, x;
int ans = 1;
int a=0, b=0;
int c=0, d=-1;
scanf("%d", &n );
for( int i = 1; i <= n; i ++ )
{
scanf( "%d", &x );
if( d == -1 )
{
d = x;
}
if( d == x )
{
c ++;
}
else
{
if( x == 1 )
{
if( a == 0 )
a = c;
if( a != c )
ans = 0;
}
else
{
if( b == 0 )
b = c;
if( b != c )
ans = 0;
}
d = x;
c = 1;
}
}
if( d == 0 )
{
if( a == 0 )
a = c;
if( a != c )
ans = 0;
}
else
{
if( b == 0 )
b = c;
if( b != c )
ans = 0;
}
if( a == 0 )
a = b;
if( b == 0 )
b = a;
if( a != b )
ans = 0;
if( ans == 1 )
printf( "YES" );
else
printf( "NO" );
}
|
D
|
//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
final class InputReader {
private:
ubyte[] p, buffer;
bool eof;
bool rawRead () {
if (eof) {
return false;
}
p = stdin.rawRead (buffer);
if (p.empty) {
eof = true;
return false;
}
return true;
}
ubyte nextByte(bool check) () {
static if (check) {
if (p.empty) {
if (!rawRead ()) {
return 0;
}
}
}
auto r = p.front;
p.popFront ();
return r;
}
public:
this () {
buffer = uninitializedArray!(ubyte[])(16<<20);
}
bool seekByte (in ubyte lo) {
while (true) {
p = p.find! (c => c >= lo);
if (!p.empty) {
return false;
}
if (!rawRead ()) {
return true;
}
}
}
template next(T) if (isSigned!T) {
T next () {
if (seekByte (45)) {
return 0;
}
T res;
ubyte b = nextByte!false ();
if (b == 45) {
while (true) {
b = nextByte!true ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 - (b - 48);
}
} else {
res = b - 48;
while (true) {
b = nextByte!true ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 + (b - 48);
}
}
}
}
template next(T) if (isUnsigned!T) {
T next () {
if (seekByte (48)) {
return 0;
}
T res = nextByte!false () - 48;
while (true) {
ubyte b = nextByte!true ();
if (b < 48 || b >= 58) {
break;
}
res = res * 10 + (b - 48);
}
return res;
}
}
T[] nextA(T) (in int n) {
auto a = uninitializedArray!(T[]) (n);
foreach (i; 0 .. n) {
a[i] = next!T;
}
return a;
}
}
void main() {
auto r = new InputReader ();
immutable nt = r.next!uint ();
foreach (tid; 0 .. nt) {
const n = r.next!uint;
int lp, lc, lf;
bool ok = true;
foreach (i; 0 .. n) {
const p = r.next!int;
const c = r.next!int;
const f = p - c;
if (p < lp || c < lc || f < lf) {
ok = false;
}
lp = p;
lc = c;
lf = f;
}
writeln (ok ? "YES" : "NO");
}
}
|
D
|
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
string solve (int n, int [] a)
{
foreach_reverse (b; 0..30)
{
auto z = a.count !(x => (x & (1 << b)) > 0);
if (z & 1)
{
if (z % 2 == n % 2 && z % 4 == 3)
{
return "LOSE";
}
return "WIN";
}
}
return "DRAW";
}
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto n = readln.strip.to !(int);
auto a = readln.splitter.map !(to !(int)).array;
writeln (solve (n, a));
}
}
|
D
|
import std.stdio;
import std.conv;
import std.algorithm;
import std.range;
import std.string;
import std.math;
import std.format;
void main() {
string[][] q;
int n = readln.chomp.to!int;
q.length = n;
foreach (string line; stdin.lines) {
if (line == "quit\n") break;
auto s = line.chomp.split;
switch (s[0]) {
case "push":
q[s[1].to!ulong-1] ~= s[2];
break;
case "move":
q[s[2].to!ulong-1] ~= q[s[1].to!ulong-1].back;
q[s[1].to!ulong-1].popBack;
break;
case "pop":
q[s[1].to!ulong-1].back.writeln;
q[s[1].to!ulong-1].popBack;
break;
default:
}
}
}
|
D
|
void main(){
long n = _scan!long();
long cnt;
foreach(i; 1..n+1){
if(i%3==0 || i%5==0)continue;
else cnt += i;
}
cnt.writeln();
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.regex;
// 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.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!string;
auto B = RD!string;
auto ab = (A ~ B).to!long;
bool ok;
for (int i = 1; i*i <= ab; ++i)
{
if (i * i == ab)
ok = true;
}
writeln(ok ? "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;
long MOD = 10^^9 + 7;
void main() {
auto N = readln.chomp.to!int;
int px = 0, py = 0, pt = 0;
foreach (_; 0..N) {
auto s = readln.split.map!(to!int);
auto t = s[0];
auto x = s[1];
auto y = s[2];
auto dt = t - pt;
auto dx = abs(x - px);
auto dy = abs(y - py);
if (dt < dx + dy) {
writeln("No");
return;
}
if ((dt - dx - dy) % 2 != 0) {
writeln("No");
return;
}
pt = t;
px = x;
py = y;
}
writeln("Yes");
}
|
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 a = read.to!int;
int b = read.to!int;
max(a + b, a - b, a * b).writeln;
}
|
D
|
import std.stdio, std.math, std.algorithm, std.array, std.string, std.conv, std.container, std.range;
pragma(inline, true) T[] Reads(T)() { return readln.split.to!(T[]); }
alias reads = Reads!int;
pragma(inline, true) void scan(Args...)(ref Args args) {
string[] ss = readln.split;
foreach (i, ref arg ; args) arg = ss[i].parse!int;
}
void main() {
int r; scan(r);
writeln(3*r^^2);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto xa = readln.split.to!(int[]);
writeln(xa[0] < xa[1] ? 0 : 10);
}
|
D
|
// Vicfred
// https://atcoder.jp/contests/abc158/tasks/abc158_a
// implementation
import std.stdio;
import std.string;
void main() {
string s = readln.chomp;
if(s.count('A') > 0 && s.count('B') > 0)
"Yes".writeln;
else
"No".writeln;
}
|
D
|
import std.stdio, std.algorithm, std.range, std.conv, std.string;
import core.stdc.stdio;
// foreach, foreach_reverse, writeln
void main() {
int n;
scanf("%d", &n);
int[] a = new int[n];
int[] b = new int[n];
foreach (i; 0..n) scanf("%d", &a[i]);
foreach (i; 0..n) scanf("%d", &b[i]);
int ans = 0;
foreach (k; 0..29) {
int mask = (1<<k)-1;
int s = 0;
if (n&1) {
foreach (x; a) s ^= x>>k&1;
foreach (x; b) s ^= x>>k&1;
}
int[] c = new int[n];
c[] = b[]&mask;
sort(c);
auto sorted = assumeSorted(c);
foreach (x; a) {
x &= mask;
int l = n - to!int(sorted.lowerBound((1<<k)-x).length);
s ^= l&1;
}
ans |= s<<k;
}
writeln(ans);
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int a, p; readV(a, p);
writeln((a*3+p)/2);
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
auto n = lread();
string ans = "";
// writeln(ans);
// writeln(cast(char)('a' + 25));
// writeln(cast(char)('a' + n % 26));
while (n > 0)
{
n -= 1;
ans ~= cast(char)('a' + n % 26);
n /= 26;
}
// writeln(ans);
foreach_reverse (e; 0 .. (ans.length))
{
write(ans[e]);
}
writeln();
}
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
|
void main() {
int[] tmp = readln.split.to!(int[]);
int a = tmp[0], b = tmp[1];
writeln(max(a+b, a-b, 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.uni;
|
D
|
import std.stdio, std.string, std.array, std.conv;
void main() {
foreach (i; 0 .. 50) {
int[] tmp = readln.chomp.split.to!(int[]);
int m = tmp[0], f = tmp[1], r = tmp[2];
if (m == -1 && f == -1 && r == -1) break;
if (m == -1 || f == -1) {
"F".writeln;
} else if (m + f >= 80) {
"A".writeln;
} else if (m + f >= 65) {
"B".writeln;
} else if (m + f >= 50) {
"C".writeln;
} else if (m + f >= 30) {
if (r >= 50) {
"C".writeln;
} else {
"D".writeln;
}
} else {
"F".writeln;
}
}
}
|
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;
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;
auto a = r.next!int;
auto b = r.next!int;
auto c = r.next!int;
long s;
int k = min (a, b);
//ab
a -= k;
s += k;
b -= k;
s += k;
s += 2 * c;
if (a || b) s++;
writeln (s);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nm = readln.split.to!(int[]);
auto N = nm[0];
auto M = nm[1];
writeln(N * (N-1) / 2 + M * (M-1) / 2);
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n; rd(n);
auto a=new long[](n+1);
for(int i=1; i<=n; i++){
int q=i;
for(int j=2; j<=i; j++){
while(q>1 && q%j==0){
q/=j;
a[j]++;
}
}
}
// writeln(a);
const long mod=1_000_000_000+7;
auto ret=reduce!((r, e)=>(r*(e+1)%mod))(1L, a);
writeln(ret);
}
void rd(Type...)(ref Type 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.string, std.range, std.algorithm, std.math, std.typecons, std.conv;
void main() {
auto inp = readln.split.to!(long[]);
auto N = inp[0], T = inp[1];
auto t = readln.split.to!(long[]);
long res = 0;
foreach (i; 0..N-1) {
res += min(T, t[i+1] - t[i]);
}
res += T;
writeln(res);
}
|
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 d; readV(d);
write("Christmas");
foreach (_; 0..25-d) write(" Eve");
writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
import std.format;
void main()
{
auto ab = readln.split.to!(int[]), A = ab[0], B = ab[1];
auto S = readln.chomp;
if(count(S, '-') == 1 && S[A] == '-')
{
writeln("Yes");
}
else
{
writeln("No");
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex;
void main(){
auto n=readln.chomp.to!int;
if(n%2==0)writeln(n);
else writeln(2*n);
}
|
D
|
import std.stdio;
int main()
{
int n = 0;
scanf("%d", &n);
writeln(n*n*n);
return 0;
}
|
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];
writeln(c >= a && c <= b ? "Yes" : "No");
}
|
D
|
import std.stdio,
std.array,
std.conv,
std.string,
std.algorithm;
void main(){
string s;
for(;;){
s=readln();
if(stdin.eof()) break;
int num = to!int(chomp(s));
int a,b,c,d,result=0;
for(a = 0;a < 10;++a){
for(b = 0;b < 10;++b){
for(c = 0;c < 10;++c){
for(d = 0;d < 10; ++d){
if(a+b+c+d == num) ++result;
}
}
}
}
writeln(result);
}
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.ascii;
void main()
{
auto s = readln.chomp;
long cnt, res;
foreach (i; 0..s.length) {
if (s[i] == 'B') {
cnt++;
} else {
res += cnt;
}
}
res.writeln;
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.bigint;
int main() {
auto sc = new Scanner(stdin);
long aa, bb, cc;
sc.read(aa, bb, cc);
alias Uint = uintN!1;
auto a = Uint(aa), b = Uint(bb), c = Uint(cc);
int co = 0;
while (co < 2^^15) {
if (a%2 || b%2 || c%2) break;
co++;
auto na = (b+c)/2;
auto nb = (a+c)/2;
auto nc = (a+b)/2;
a = na; b = nb; c = nc;
}
if (co == 2^^15) co = -1;
writeln(co);
return 0;
}
/* 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) {
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/ldc/inline.d */
// module dcomp.ldc.inline;
version(LDC) {
pragma(LDC_inline_ir)
R inlineIR(string s, R, P...)(P);
}
/* 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/int128.d */
// module dcomp.int128;
version(LDC) {
// import dcomp.ldc.inline;
}
version(LDC) version(X86_64) {
version = LDC_IR;
}
ulong[2] mul128(ulong a, ulong b) {
ulong[2] res;
version(LDC_IR) {
ulong upper, lower;
inlineIR!(`
%r0 = zext i64 %0 to i128
%r1 = zext i64 %1 to i128
%r2 = mul i128 %r1, %r0
%r3 = trunc i128 %r2 to i64
%r4 = lshr i128 %r2, 64
%r5 = trunc i128 %r4 to i64
store i64 %r3, i64* %2
store i64 %r5, i64* %3`, void)(a, b, &lower, &upper);
return [lower, upper];
} else version(D_InlineAsm_X86_64) {
ulong upper, lower;
asm {
mov RAX, a;
mul b;
mov lower, RAX;
mov upper, RDX;
}
return [lower, upper];
} else {
ulong B = 2UL^^32;
ulong[2] a2 = [a % B, a / B];
ulong[2] b2 = [b % B, b / B];
ulong[4] c;
foreach (i; 0..2) {
foreach (j; 0..2) {
c[i+j] += a2[i] * b2[j] % B;
c[i+j+1] += a2[i] * b2[j] / B;
}
}
foreach (i; 0..3) {
c[i+1] += c[i] / B;
c[i] %= B;
}
return [c[0] + c[1] * B, c[2] + c[3] * B];
}
}
ulong div128(ulong[2] a, ulong b) {
version(LDC_IR) {
return inlineIR!(`
%r0 = zext i64 %0 to i128
%r1 = zext i64 %1 to i128
%r2 = shl i128 %r1, 64
%r3 = add i128 %r0, %r2
%r4 = zext i64 %2 to i128
%r5 = udiv i128 %r3, %r4
%r6 = trunc i128 %r5 to i64
ret i64 %r6`,ulong)(a[0], a[1], b);
} else version(D_InlineAsm_X86_64) {
ulong upper = a[1], lower = a[0];
ulong res;
asm {
mov RDX, upper;
mov RAX, lower;
div b;
mov res, RAX;
}
return res;
} else {
import std.bigint, std.conv;
return (((BigInt(a[1]) << 64) + BigInt(a[0])) / BigInt(b)).to!ulong;
}
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/bigint.d */
// module dcomp.bigint;
import core.checkedint;
// import dcomp.int128, dcomp.foundation;
void addMultiWord(in ulong[] l, in ulong[] r, ulong[] res) {
auto N = res.length;
bool of = false;
foreach (i; 0..N) {
bool nof;
res[i] = addu(
(i < l.length) ? l[i] : 0UL,
(i < r.length) ? r[i] : 0UL, nof);
if (of) {
res[i]++;
nof |= (res[i] == 0);
}
of = nof;
}
}
void subMultiWord(in ulong[] l, in ulong[] r, ulong[] res) {
auto N = res.length;
bool of = false;
foreach (i; 0..N) {
bool nof;
res[i] = subu(
(i < l.length) ? l[i] : 0UL,
(i < r.length) ? r[i] : 0UL, nof);
if (of) {
res[i]--;
nof |= (res[i] == ulong.max);
}
of = nof;
}
}
void mulMultiWord(in ulong[] l, in ulong r, ulong[] res) {
auto N = res.length;
ulong ca;
foreach (i; 0..N) {
auto u = mul128((i < l.length) ? l[i] : 0UL, r);
bool of;
res[i] = addu(u[0], ca, of);
if (of) u[1]++;
ca = u[1];
}
}
void shiftLeftMultiWord(in ulong[] l, int n, ulong[] res) {
size_t N = res.length;
int ws = n / 64;
int bs = n % 64;
import std.stdio;
foreach_reverse (ptrdiff_t i; 0..N) {
ulong b = (0 <= i-ws && i-ws < l.length) ? l[i-ws] : 0UL;
if (bs == 0) res[i] = b;
else {
ulong a = (0 <= i-ws-1 && i-ws-1 < l.length) ? l[i-ws-1] : 0UL;
res[i] = (b << bs) | (a >> (64-bs));
}
}
}
int cmpMultiWord(in ulong[] l, in ulong[] r) {
import std.algorithm : max;
auto N = max(l.length, r.length);
foreach_reverse (i; 0..N) {
auto ld = (i < l.length) ? l[i] : 0UL;
auto rd = (i < r.length) ? r[i] : 0UL;
if (ld < rd) return -1;
if (ld > rd) return 1;
}
return 0;
}
struct uintN(int N) if (N >= 1) {
import core.checkedint;
ulong[N] d;
this(ulong x) { d[0] = x; }
this(string s) {
foreach (c; s) {
this *= 10;
this += uintN(c-'0');
}
}
string toString() {
import std.algorithm : reverse;
char[] s;
if (!this) return "0";
while (this) {
s ~= cast(char)('0' + (this % uintN(10))[0]);
this /= uintN(10);
}
reverse(s);
return s.idup;
}
ref inout(ulong) opIndex(int idx) inout { return d[idx]; }
T opCast(T: bool)() {
import std.algorithm, std.range;
return d[].find!"a!=0".empty == false;
}
uintN opUnary(string op)() const if (op == "~") {
uintN res;
foreach (i; 0..N) {
res[i] = ~d[i];
}
return res;
}
uintN opBinary(string op)(in uintN r) const
if (op == "&" || op == "|" || op == "^") {
uintN res;
foreach (i; 0..N) {
res[i] = mixin("d[i]" ~ op ~ "r.d[i]");
}
return res;
}
uintN opBinary(string op : "<<")(int n) const {
if (N * 64 <= n) return uintN(0);
uintN res;
int ws = n / 64;
int bs = n % 64;
if (bs == 0) {
res.d[ws..N][] = d[0..N-ws][];
return res;
}
foreach_reverse (i; 1..N-ws) {
res[i+ws] = (d[i] << bs) | (d[i-1] >> (64-bs));
}
res[ws] = (d[0] << bs);
return res;
}
uintN opBinary(string op : ">>")(int n) const {
if (N * 64 <= n) return uintN(0);
uintN res;
int ws = n / 64;
int bs = n % 64;
if (bs == 0) {
res.d[0..N-ws][] = d[ws..N][];
return res;
}
foreach_reverse (i; 0..N-ws-1) {
res[i] = (d[i+ws+1] >> (64-bs)) | (d[i+ws] << bs);
}
res[dim-ws-1] = (d[N-1] << bs);
return res;
}
int opCmp(in uintN r) const {
return cmpMultiWord(d, r.d);
}
uintN opUnary(string op)() if (op == "++") {
uintN res;
bool of = true;
foreach (i; 0..N) {
if (of) {
d[i]++;
if (d[i]) of = false;
}
}
return res;
}
uintN opUnary(string op)() if (op == "--") {
return this -= uintN(1);
}
uintN opUnary(string op)() const if (op=="+" || op=="-") {
if (op == "+") return this;
if (op == "-") {
return ++(~this);
}
}
uintN opBinary(string op : "+")(in uintN r) const {
uintN res;
addMultiWord(d, r.d, res.d);
return res;
}
uintN opBinary(string op : "-")(in uintN r) const {
uintN res;
subMultiWord(d, r.d, res.d);
return res;
}
uintN opBinary(string op : "*")(in uintN r) const {
uintN res;
foreach (s; 0..N) {
foreach (i; 0..s+1) {
int j = s-i;
auto u = mul128(d[i], r[j]);
bool of;
res[s] = addu(res[s], u[0], of);
if (s+1 < N) {
if (of) {
res[s+1]++;
of = (res[s+1] == 0);
}
res[s+1] = addu(res[s+1], u[1], of);
if (s+2 < N && of) res[s+2]++;
}
}
}
return res;
}
uintN opBinary(string op : "*")(in ulong r) const {
uintN res;
mulMultiWord(d, r, res.d);
return res;
}
uintN opBinary(string op : "/")(in uintN rr) const {
import core.bitop;
int up = -1, shift;
foreach_reverse(i; 0..N) {
if (rr[i]) {
up = i;
shift = 63 - bsr(rr[i]);
break;
}
}
assert(up != -1);
ulong[N+1] l;
l[0..N] = d[0..N];
import std.stdio;
shiftLeftMultiWord(l, shift, l);
auto r = (rr << shift);
uintN res;
foreach_reverse (i; 0..N-up) {
ulong pred = (r[up] == ulong.max) ? l[i+up+1] : div128([l[i+up], l[i+up+1]], r[up]+1);
res[i] = pred;
ulong[N+1] buf;
mulMultiWord(r.d[], pred, buf);
subMultiWord(l[i..i+up+2], buf[], l[i..i+up+2]);
int co = 0;
while (cmpMultiWord(l[i..i+up+2], r.d[]) != -1) {
co++;
res[i]++;
subMultiWord(l[i..i+up+2], r.d[], l[i..i+up+2]);
}
assert(co <= 2);
}
return res;
}
uintN opBinary(string op : "/")(in ulong r) const {
return this / uintN(r);
}
uintN opBinary(string op : "%", T)(in T r) const {
return this - this/r*r;
}
auto opOpAssign(string op, T)(in T r) {
return mixin("this=this" ~ op ~ "r");
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
/// [..)
struct SegTree(alias _fun, alias def, T)
if (is(typeof(def) : T))
{
import std.functional : binaryFun;
alias fun = binaryFun!_fun;
///
this(size_t n, T[] ts) {
this.n = 1;
while (this.n < n) this.n *= 2;
this.tree.length = this.n * 2 - 1;
foreach (ref e; this.tree) e = def;
foreach (i, e; ts) this.put(i, e);
}
///
void put(size_t i, T e) {
i += this.n - 1;
this.tree[i] = e;
while (i > 0) {
i = (i-1) / 2;
this.tree[i] = fun(this.tree[i*2+1], this.tree[i*2+2]);
}
}
///
T query(size_t a, size_t b) {
T impl(size_t i, size_t l, size_t r) {
if (r <= a || b <= l) return def;
if (a <= l && r <= b) return this.tree[i];
return fun(
impl(i*2+1, l, (l+r)/2),
impl(i*2+2, (l+r)/2, r)
);
}
return impl(0, 0, this.n);
}
private:
size_t n;
T[] tree;
}
///
SegTree!(f, def, T) seg_tree(alias f, alias def, T)(size_t n, T[] arr = [])
{
return SegTree!(f, def, T)(n, arr);
}
///
SegTree!(f, def, T) seg_tree(alias f, alias def, T)(T[] arr)
{
return SegTree!(f, def, T)(arr.length, arr);
}
void main()
{
auto N = readln.chomp.to!int;
auto S = readln.chomp.to!(char[]);
SegTree!("a + b", 0, int)[] ss;
ss.length = 26;
foreach (ref s; ss) s = SegTree!("a + b", 0, int)(N+1, []);
foreach (i, c; S) {
ss[c-'a'].put(i, 1);
}
auto Q = readln.chomp.to!int;
foreach (_; 0..Q) {
auto q = readln.split;
if (q[0] == "1") {
auto i = q[1].to!int-1;
auto c = q[2][0];
ss[S[i]-'a'].put(i, 0);
S[i] = c;
ss[c-'a'].put(i, 1);
} else {
auto l = q[1].to!int-1;
auto r = q[2].to!int;
int ret;
foreach (i, ref s; ss) {
ret += min(s.query(l, r), 1);
}
writeln(ret);
}
}
}
|
D
|
/* imports all std modules {{{*/
import
std.algorithm,
std.array,
std.ascii,
std.base64,
std.bigint,
std.bitmanip,
std.compiler,
std.complex,
std.concurrency,
std.container,
std.conv,
std.csv,
std.datetime,
std.demangle,
std.encoding,
std.exception,
std.file,
std.format,
std.functional,
std.getopt,
std.json,
std.math,
std.mathspecial,
std.meta,
std.mmfile,
std.net.curl,
std.net.isemail,
std.numeric,
std.parallelism,
std.path,
std.process,
std.random,
std.range,
std.regex,
std.signals,
std.socket,
std.stdint,
std.stdio,
std.string,
std.system,
std.traits,
std.typecons,
std.uni,
std.uri,
std.utf,
std.uuid,
std.variant,
std.zip,
std.zlib;
/*}}}*/
/+---test
issii
2
---+/
/+---test
qq
81
---+/
/+---test
cooooooooonteeeeeeeeeest
999993333
---+/
void main(string[] args) {
auto S = readln.chomp.to!string;
const K = readln.chomp.to!long;
long ans;
auto l = S.countUntil!(x => x != S[0]);
auto r = S.retro.countUntil!(x => x != S[$-1]);
if (l < 0 || r < 0) {
(S.length*K/2).writeln;
return;
}
ans += l/2;
ans += r/2;
if (S[0] == S[$-1]) {
ans += (l+r)/2 * (K-1);
} else {
ans += (l/2 + r/2) * (K-1);
}
r = S.length - r;
long n = 1;
foreach (i; l..r+1) {
if (S[i-1] == S[i]) {
++n;
} else {
ans += n/2 * K;
n = 1;
}
}
ans.writeln;
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
void main()
{
auto s = sread();
auto s_len = s.length;
// writeln(s_len);
foreach (i; 0 .. s_len)
{
write('x');
}
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
|
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; readV(a, b);
if (a+b >= 10) writeln("error");
else writeln(a+b);
}
|
D
|
void main() {
int[] xy = readln.split.to!(int[]);
int x = xy[0], y = xy[1];
bool ok = true;
int[] d30 = [4, 6, 9, 11];
if (xy.any!"a == 2") {
ok = false;
} else if (d30.canFind(x) != d30.canFind(y)) {
ok = false;
}
writeln(ok ? "Yes" : "No");
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio;
import std.ascii;
import std.conv;
import std.string;
import std.algorithm;
import std.range;
import std.functional;
import std.math;
import core.bitop;
import std.numeric;
void main()
{
auto hw = readln.split.to!(long[]);
long h = hw[0];
long w = hw[1];
char[][] table;
foreach (i; 0 .. h)
table ~= readln.chomp.to!(char[]);
bool[size_t] rows;
bool[size_t] cols;
foreach (i, r; table)
if (r.all!(c => c == '.'))
{
rows[i] = true;
}
foreach (i; 0 .. w)
{
bool flag = true;
foreach (j; 0 .. h)
flag = flag && table[j][i] == '.';
if (flag)
{
cols[i] = true;
}
}
foreach (r; 0 .. h)
{
foreach (c; 0 .. w)
if (!(r in rows || c in cols))
{
write(table[r][c]);
}
if (!(r in rows))
{
writeln;
}
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
int n, m;
scan(n, m);
auto uf = WeightedUnionFind!(int)(n + 1);
bool ok = 1;
foreach (i ; 0 .. m) {
int l, r, d;
scan(l, r, d);
if (uf.same(l, r)) {
if (uf.diff(l, r) != d) {
ok = 0;
}
}
else {
uf.merge(l, r, d);
}
}
writeln(ok ? "Yes" : "No");
}
struct WeightedUnionFind(T) {
private {
int[] _parent, _rank;
T[] _diff;
}
this(int N) {
_parent = new int[](N);
_rank = new int[](N);
_diff = new T[](N);
foreach (i ; 0 .. N) {
_parent[i] = i;
}
}
int findRoot(int x) {
if (_parent[x] != x) {
int r = findRoot(_parent[x]);
_diff[x] += _diff[_parent[x]];
_parent[x] = findRoot(r);
}
return _parent[x];
}
bool same(int x, int y) {
return findRoot(x) == findRoot(y);
}
T weight(int x) {
findRoot(x);
return _diff[x];
}
// x と y が異なるグループに属すならinf(十分大きい値)を返す
T diff(int x, int y) {
return same(x, y) ? weight(y) - weight(x) : T.max;
}
import std.algorithm : swap;
// weight(y) = weight(x) + w
bool merge(int x, int y, T w) {
w += weight(x) - weight(y);
int u = findRoot(x);
int v = findRoot(y);
// x と y が既に同じグループであるなら距離の更新はしない
if (u == v) return false;
if (_rank[u] < _rank[v]) {
swap(u, v);
w = -w;
}
if (_rank[u] == _rank[v]) {
_rank[u]++;
}
_parent[v] = u;
_diff[v] = w;
return true;
}
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
void main() {
problem();
}
void problem() {
auto N = scan;
void solve() {
writeln(N.canFind('7') ? "Yes" : "No");
}
solve();
}
// ----------------------------------------------
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); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
// -----------------------------------------------
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto n = readln.chomp.to!int;
auto k = readln.chomp.to!int;
auto ans = int.max;
foreach (i; 0..1<<n) {
auto r = 1;
foreach (j; 0..n)
if (i.bitTest(j)) r *= 2;
else r += k;
ans = min(ans, r);
}
writeln(ans);
}
pragma(inline) {
pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; }
pure T bitSet(T)(T n, size_t i) { return n | (T(1) << i); }
pure T bitReset(T)(T n, size_t i) { return n & ~(T(1) << i); }
pure T bitComp(T)(T n, size_t i) { return n ^ (T(1) << i); }
import core.bitop;
pure int bsf(T)(T n) { return core.bitop.bsf(ulong(n)); }
pure int bsr(T)(T n) { return core.bitop.bsr(ulong(n)); }
pure int popcnt(T)(T n) { return core.bitop.popcnt(ulong(n)); }
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
auto s = readln.chomp.to!(char[]);
long k;
rd(k);
int j = -1;
for (int i = 0; i < s.length; i++) {
if (s[i] > '1') {
j = i;
break;
}
}
if (j < 0) {
writeln(1);
} else {
if (k <= j) {
writeln(1);
} else {
writeln(s[j]);
}
}
}
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 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()
{
long n = 0;
auto s = sread();
foreach(i;s)
{
if(i == '+')
{
n++;
}
else
{
n--;
}
}
n.writeln();
}
|
D
|
import std.stdio, std.string, std.conv;
void main(){
auto xyz = readln.split.to!(int[]);
auto x = xyz[0], y = xyz[1], z = xyz[2];
writeln((x - z) / (y + z));
}
|
D
|
void main()
{
long n = rdElem;
long l, r = n - 1;
string s;
string t = "Vacant";
l.writeln;
stdout.flush;
s = rdStr;
if (s == t) return;
string f = s;
r.writeln;
stdout.flush;
s = rdStr;
if (s == t) return;
foreach (i; 0 .. 21)
{
long m = (l + r) >> 1;
m.writeln;
stdout.flush;
s = rdStr;
if (s == t) return;
if ((m & 1) == (s == f)) r = m;
else l = m;
}
}
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.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
void main()
{
auto ab = readln.split.to!(int[]);
auto A = ab[0];
auto B = ab[1];
writeln(A <= 8 && B <= 8 ? "Yay!" : ":(");
}
|
D
|
// Cheese-Cracker: cheese-cracker.github.io
void theCode(){
ll a = scan;
ll b = scan;
ll diff = abs(a - b);
ll rem = 0;
if(diff > 0){
rem = min(b % diff, diff - (b % diff));
}
writeln(diff, " ", rem);
}
void main(){
long tests = scan; // Toggle!
while(tests--) theCode();
}
/********* That's All Folks! *********/
import std.stdio, std.random, std.functional, std.container, std.algorithm;
import std.numeric, std.range, std.typecons, std.string, std.math, std.conv;
string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
alias ll = long, tup = Tuple!(long, "x", long, "y");
|
D
|
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;
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)); }
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.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.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.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;
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 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 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 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 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;
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.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
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.