code
stringlengths 4
1.01M
| language
stringclasses 2
values |
---|---|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto ap = readln.split.to!(int[]);
auto A = ap[0];
auto P = ap[1];
writeln((P + A*3)/2);
} | D |
import std.stdio;
import std.algorithm;
import std.range;
import std.conv;
import std.format;
import std.array;
import std.math;
import std.string;
import std.container;
int[100001] dp;
int solve(int n) {
if (n == 0)
return 0;
if (dp[n] != -1)
return dp[n];
int ret = solve(n - 1) + 1;
for (int d = 6; n - d >= 0; d *= 6)
ret = min(solve(n - d) + 1, ret);
for (int d = 9; n - d >= 0; d *= 9)
ret = min(solve(n - d) + 1, ret);
return dp[n] = ret;
}
void main() {
int N; readlnTo(N);
foreach(i; 0..N+1)
dp[i] = -1;
writeln(solve(N));
}
// helpers
void readlnTo(T...)(ref T t) {
auto s = readln.split;
assert(s.length == t.length);
foreach(ref ti; t) {
ti = s[0].to!(typeof(ti));
s = s[1..$];
}
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto abc = readln.split.to!(int[]);
auto A = abc[0];
auto B = abc[1];
auto C = abc[2];
writeln(min(B / A, C));
} | 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;
}
}
}
bool check (int[][] a, int[][] b, int h, int w) {
auto r = new int[h];
foreach (i; 0 .. h) {
int t;
foreach (j; 0 .. w) {
t += a[i][j] ^ b[i][j];
}
if (t & 1) return false;
}
foreach (j; 0 .. w) {
int t;
foreach (i; 0 .. h) {
t += a[i][j] ^ b[i][j];
}
if (t & 1) return false;
}
return true;
}
void main() {
auto r = new InputReader;
immutable h = r.next!uint;
immutable w = r.next!uint;
auto a = uninitializedArray!(int[][]) (h, w);
auto b = uninitializedArray!(int[][]) (h, w);
foreach (i; 0 .. h) foreach (j; 0 .. w) a[i][j] = r.next!uint;
foreach (i; 0 .. h) foreach (j; 0 .. w) b[i][j] = r.next!uint;
writeln (check (a, b, h, w) ? "Yes" : "No");
}
| D |
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std;
// dfmt on
void main()
{
long N = lread();
auto ans = "";
while (N != 0)
{
ans = cast(char)('a' + (N - 1) % 26) ~ ans;
N--;
N /= 26;
}
writeln(ans);
}
| 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 n = readln.chomp.to!int;
string s = readln.chomp;
ulong mod = 1_000_000_007;
ulong[][] dp; // dp[j][i] := # of ways to make (any) string of length i by pressing j times
foreach(j; 0 .. n + 2){
dp ~= [0];
dp[j].length = n + 2;
foreach(i; 0 .. n + 2){
if(j == 0){
if(i == 0) dp[j][i] = 1;
else dp[j][i] = 0;
}
else{
if(i == 0) dp[j][i] = dp[j - 1][i] + dp[j - 1][i + 1];
else if(i < n - 1)
dp[j][i] = dp[j - 1][i - 1] * 2 + dp[j - 1][i + 1];
else
dp[j][i] = dp[j - 1][i - 1] * 2;
}
dp[j][i] %= mod;
}
}
ulong ans = dp[n][s.length];
foreach(k; s){
if(ans % 2 == 0) ans /= 2;
else ans = (ans + mod) / 2;
}
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.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 n = RD!int;
auto c0 = RD!int;
auto c1 = RD!int;
auto h = RD!int;
auto s = RD!string;
auto c = c0 < c1 ? '1' : '0';
auto d = abs(c0 - c1) - h;
foreach (i; 0..n)
{
if (s[i] == '0')
ans[ti] += c0;
else
ans[ti] += c1;
if (s[i] == c)
{
ans[ti] -= max(0, d);
}
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
| 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;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
void main(){
int n = read.to!int;
int[] ps;
foreach(i; 0 .. n) ps ~= read.to!int - 1;
debug writeln("ps:", ps);
bool[] xs = new bool[](n); // xs[i]: number i has already appeard
bool[] ys = new bool[](n); // ys[i]: number (i + 1) appears later than number i in ps
foreach(p; ps){
xs[p] = 1;
ys[p] = (p < n - 1 && !xs[p + 1]);
}
debug writeln("xs:", xs);
debug writeln("ys:", ys);
int ans = 0;
int temp = 0;
foreach(y; ys){
temp += 1;
if(y == 0){
if(temp > ans) ans = temp;
temp = 0;
}
debug writeln("y:", y, " ans:", ans, " temp:", temp);
}
if(temp > ans) ans = temp;
(n - ans).writeln;
} | D |
import std;
enum inf(T)()if(__traits(isArithmetic,T)){return T.max/4;}
T scan(T=long)(){return readln.chomp.to!T;}
void scan(T...)(ref T args){auto input=readln.chomp.split;foreach(i,t;T)args[i]=input[i].to!t;}T[] scanarr(T=long)(){return readln.chomp.split.to!(T[]);}
alias Queue=DList;auto enq(T)(ref Queue!T q,T e){q.insertBack(e);}T deq(T)(ref Queue!T q){T e=q.front;q.removeFront;return e;}
alias Stack=SList;auto push(T)(ref Stack!T s,T e){s.insert(e);}T pop(T)(ref Stack!T s){T e=s.front;s.removeFront;return e;}
struct UnionFind(T){T[T]u;ulong[T] rank;@property{bool inc(T e){return e in u;}auto size(){return u.keys.length;}auto dup(){T[] child=u.keys;T[] parent=u.values;auto res=UnionFind!T(child);child.each!(e=>res.add(e));size.iota.each!(i=>res.unite(child[i],parent[i]));return res;}}this(T e){e.add;}this(T[] es){es.each!(e=>e.add);}auto add(T a,T b=a){assert(b.inc);u[a]=a;rank[a];if(a!=b)unite(a,b);}auto find(T e){if(u[e]==e)return e;return u[e]=find(u[e]);}auto same(T a,T b){return a.find==b.find;}auto unite(T a,T b){a=a.find;b=b.find;if(a==b)return;if(rank[a]<rank[b])u[a]=b;else{u[b]=a;if(rank[a]==rank[b])rank[a]++;}}}
struct PriorityQueue(T,alias less="a<b"){BinaryHeap!(T[],less) heap;@property{bool empty(){return heap.empty;}auto length(){return heap.length;}auto dup(){return PriorityQueue!(T,less)(array);}T[] array(){T[] res;auto tp=heap.dup;foreach(i;0..length){res~=tp.front;tp.removeFront;}return res;}void push(T e){heap.insert(e);}void push(T[] es){es.each!(e=>heap.insert(e));}}T look(){return heap.front;}T pop(){T tp=look;heap.removeFront;return tp;}this(T e){ heap=heapify!(less,T[])([e]);} this(T[] e){ heap=heapify!(less,T[])(e);}}
//END OF TEMPLATE
void main(){
auto c=scan!string;
(c=="ABC"?"ARC":"ABC").writeln;
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto md = readln.split.to!(int[]);
auto M = md[0];
auto D = md[1];
int r;
foreach (m; 0..M) {
++m;
foreach (d; 21..D) {
++d;
if (d%10 >= 2 && d/10 >= 2 && (d%10)*(d/10) == m) {
++r;
}
}
}
writeln(r);
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto S = readln.chomp.to!(dchar[]);
auto Q = readln.chomp.to!int;
dchar[] as, bs;
int f;
foreach (_; 0..Q) {
auto q = readln;
if (q[0] == '1') {
++f;
} else {
auto c = q[4].to!dchar;
if ((q[2] == '1' && f%2 == 0) || (q[2] == '2' && f%2 == 1)) {
as ~= c;
} else {
bs ~= c;
}
}
}
if (f%2 == 1) {
S.reverse();
bs.reverse();
writeln(bs ~ S ~ as);
} else {
as.reverse();
writeln(as ~ S ~ bs);
}
} | D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
void main(){
auto c=readln.chomp.to!char;
if(c=='a'||c=='i'||c=='u'||c=='e'||c=='o')writeln("vowel");
else writeln("consonant");
} | 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;
long[Tuple!(long, long)] mem;
immutable long MOD = 10^^9 + 7;
long dp(long s, long x) {
Tuple!(long, long) t = tuple(s, x);
if (t in mem)
return mem[t];
if (s == 0)
return 1;
else if (s == 1) {
mem[t] = (dp(s/2, x/2) + dp((s-1)/2, (x-1)/2)) % MOD;
return mem[t];
}
mem[t] = (dp(s/2, x/2) + dp((s-1)/2, (x-1)/2) + dp((s-2)/2, x/2)) % MOD;
return mem[t];
}
void main() {
long N = readln.chomp.to!long;
dp(N, N).writeln;
}
| D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
import std.range;
void main(){
auto Q=readln.split.to!(int[]),A=Q[0],B=Q[1],C=Q[2];
if(C>=A&&C<=B)writeln("Yes");
else writeln("No");
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
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 n = RD!int;
auto k = RD!int;
auto cnt = new long[](k);
foreach (i; 0..n)
{
auto a = RD!int-1;
++cnt[a];
}
long ans;
long used;
foreach (i; 0..k)
{
auto c = cnt[i] / 2;
ans += c * 2;
used += c;
cnt[i] -= c * 2;
}
auto remain = n - used * 2 + n % 2;
ans += remain / 2;
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()
{
writeln((readln.chomp.to!int+1)/2);
} | D |
import std.stdio;
import std.string;
import std.conv;
import std.bigint;
import std.typecons;
import std.algorithm;
import std.array;
void main() {
auto tmp = readln.split.to!(int[]);
auto N = tmp[0];
auto M = tmp[1];
writeln((M * 1900 + (N-M) * 100) * 2^^M);
}
| D |
import std.stdio, std.string, std.conv;
void main(){
string str;
string[] s;
int q1, b, c1, c2, q2, a1, a2, t;
while ((str = readln()) != "0\n") {
s = split(str);
q1 = s[0].to!int;
b = s[1].to!int;
c1 = s[2].to!int;
c2 = s[3].to!int;
q2 = s[4].to!int;
foreach (i; 0..q2+1) {
a1 = q2 - i;
t = b - (a1 * c1);
if (t < 0) continue;
a2 = t / c2;
if (a1 + a2 >= q1) break;
}
if (a1 == 0) writeln("NA");
else writeln(a1, " ", a2);
}
} | D |
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n, k; readV(n, k);
writeln(n%k == 0 ? 0 : 1);
}
| 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 m;
foreach (i; 1..n) {
if (s[i] == 'E') {
m++;
}
}
auto lm = m;
foreach (i; 1..n) {
if (s[i-1] == 'W') {
lm++;
}
if (s[i] == 'E') {
lm--;
}
m = min(m, lm);
}
m.writeln;
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto S = readln.chomp.to!(char[]);
foreach (ref c; S) c = 'x';
writeln(S);
} | 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!int;
long ans = 0;
foreach (i; 1..N+1) foreach (j; 1..N+1) foreach(k; 1..N+1) {
ans += gcd(gcd(i, j), k);
}
ans.writeln;
} | D |
import std.stdio,
std.string,
std.conv,
std.algorithm,
std.range,
std.math;
void main()
{
while(true){
auto ip = readln.split.to!(int[]), h = ip[0], w = ip[1];
if(h == 0 && w == 0) break;
else{
string W;
foreach(j; 0..w){
W ~= "#";
}
foreach(i; 0..h){
W.writeln;
}
writeln;
}
}
}
| D |
import std.algorithm,
std.string,
std.range,
std.stdio,
std.conv;
void chmax(T)(ref T a, ref T b) {
if (a < b) {
a = b;
}
}
void main() {
int N = readln.chomp.to!int;
int[] s = N.iota.map!(_ => readln.chomp.to!int).array;
bool[10010][110] dp;
dp[0][0] = true;
dp[0][s[0]] = true;
foreach (i; 1..N) {
foreach (j; 10010.iota) {
if (dp[i - 1][j]) {
dp[i][j+s[i]] = true;
dp[i][j] = true;
}
}
}
int ans;
foreach (i; 10010.iota) {
if (dp[N-1][i]) {
if (i % 10) {
chmax(ans, i);
}
}
}
writeln(ans);
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
writeln(reduce!"a + b"(0, readln.split.to!(int[])) >= 22 ? "bust" : "win");
} | 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!string;
long cnt;
foreach (c; N)
{
cnt += [c].to!long;
}
writeln(N.to!long % cnt == 0 ? "Yes" : "No");
stdout.flush();
debug readln();
} | D |
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N, A, B;
scan(N, A, B);
long a = A * (N / (A + B));
long b = min((N % (A + B)), A);
writeln(a + b);
}
| D |
void main(){
string s1, s2;
s1 = readln().chomp().dup().reverse();
s2 = readln().chomp();
if(s1 == s2)writeln("YES");
else writeln("NO");
}
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.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 a = RD!int;
auto b = RD!int;
auto s = RD!string;
auto n = cast(int)s.length;
int l = -1, r;
foreach (i; 0..n)
{
if (s[i] == '1')
{
l = i;
break;
}
}
foreach_reverse (i; 0..n)
{
if (s[i] == '1')
{
r = i+1;
break;
}
}
if (l == -1) continue;
ans[ti] = a;
int cnt;
foreach (i; l..r)
{
debug writeln("ans:", ans[ti]);
if (s[i] == '0')
++cnt;
else
{
ans[ti] += min(a, b*cnt);
cnt = 0;
}
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
| D |
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto s = readln.chomp;
auto a = ["dream", "dreamer", "erase", "eraser"];
loop: while (!s.empty) {
foreach (ai; a) {
if (s.length >= ai.length && s[$-ai.length..$] == ai) {
s = s[0..$-ai.length];
continue loop;
}
}
writeln("NO");
return;
}
writeln("YES");
}
| 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 A, B;
scan(A, B);
writeln((A < 10 && B < 10) ? A * B : -1);
}
| D |
/+ dub.sdl:
name "C"
dependency "dunkelheit" version=">=0.9.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
int main() {
Scanner sc = new Scanner(stdin);
int n;
sc.read(n);
int[][] g = new int[][n];
foreach (i; 0..n-1) {
int a, b;
sc.read(a, b); a--; b--;
g[a] ~= b; g[b] ~= a;
}
if (g.count!"a.length>=3" >= 2) {
writeln("No");
return 0;
}
int[] leafs;
void dfs(int p, int b) {
bool haveCh = false;
foreach (d; g[p]) {
if (d == b) continue;
haveCh = true;
dfs(d, p);
}
if (!haveCh) leafs ~= p;
}
foreach (i; 0..n) {
if (i == n-1 || g[i].length >= 3) {
dfs(i, -1);
writeln("Yes");
writeln(leafs.length);
foreach (d; leafs) {
writeln(i+1, " ", d+1);
}
break;
}
}
return 0;
}
/* IMPORT /mnt/c/Users/yosupo/Programs/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /mnt/c/Users/yosupo/Programs/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
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 {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(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);
}
}
void read(Args...)(auto ref Args args) {
import std.exception;
static if (args.length != 0) {
enforce(readSingle(args[0]));
read(args[1..$]);
}
}
bool hasNext() {
return succ();
}
}
/* IMPORT /mnt/c/Users/yosupo/Programs/dunkelheit/source/dkh/foundation.d */
// module dkh.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);
}
}
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
| D |
//dlang template---{{{
import std.stdio;
import std.conv;
import std.string;
import std.array;
import std.algorithm;
import std.typecons;
import std.math;
import std.range;
// MIT-License https://github.com/kurokoji/nephele
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType))
{
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType))
{
str ~= s.to!(char[]).strip.split;
}
}
//Digit count---{{{
int DigitNum(int num) {
int digit = 0;
while (num != 0) {
num /= 10;
digit++;
}
return digit;
}
//}}}
//}}}
void main() {
Scanner sc = new Scanner;
string N;
sc.scan(N);
char[] n = new char[3];
foreach (i; 0 .. 3)
n[i] = to!char(N[i]);
foreach (i; 0 .. 3) {
if (n[i] == '1')
n[i] = '9';
else
n[i] = '1';
}
writeln(n);
}
| D |
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n; readV(n);
int[] l; readA(n, l);
auto ml = l.reduce!max;
writeln(l.sum-ml > ml ? "Yes" : "No");
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
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 S = RD!string;
auto T = S.idup;
long ans = 1;
auto last = [S[0]];
S.popFront;
while (!S.empty)
{
if (last.length == 2)
{
last = [S[0]]; S.popFront;
++ans;
}
else if (S[0] == last[0])
{
if (S.length == 1)
{
break;
}
last = S[0..2];
S = S[2..$];
++ans;
}
else
{
last = [S[0]]; S.popFront;
++ans;
}
}
long ans2 = 1;
last = [T[0], T[1]];
T = T[2..$];
while (!T.empty)
{
if (last.length == 2)
{
last = [T[0]]; T.popFront;
++ans2;
}
else if (T[0] == last[0])
{
if (T.length == 1)
{
break;
}
last = T[0..2];
T = T[2..$];
++ans2;
}
else
{
last = [T[0]]; T.popFront;
++ans2;
}
}
writeln(ans);
stdout.flush();
debug readln();
}
| D |
import std.stdio;
import std.algorithm;
import std.range;
import std.conv;
import std.format;
import std.array;
import std.math;
import std.string;
import std.container;
void main() {
int N; readlnTo(N);
if (N == 1) "Hello World".writeln;
else {
int A, B;
readlnTo(A);
readlnTo(B);
(A + B).writeln;
}
}
// helpers
void readlnTo(T...)(ref T t) {
auto s = readln.split;
assert(s.length == t.length);
foreach(ref ti; t) {
ti = s[0].to!(typeof(ti));
s = s[1..$];
}
}
| 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 |
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 AB = new long[2][](N);
foreach (i; 0..N)
{
AB[i] = [RD, RD];
}
long ans;
foreach_reverse (i; 0..N)
{
auto a = AB[i][0] + ans;
auto b = AB[i][1];
auto x = b * ((a / b) + (a % b != 0 ? 1 : 0));
ans += x - a;
}
writeln(ans);
stdout.flush();
debug readln();
}
| D |
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.stdio;
import std.string;
import std.range;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() {
auto xs = readints();
int a = xs[0], b = xs[1], c = xs[2], d = xs[3];
int start = max(a, c);
int end = min(b, d);
writeln(max(0, end - start));
}
| D |
import std.stdio, std.conv, std.string, std.algorithm, std.range;
void main() {
auto N = readln.split[0].to!int;
(N ^^ 3).writeln;
} | D |
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.format;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
enum MOD = (10 ^^ 9) + 7;
void main()
{
long N, M;
scan(N, M);
if (M <= N * 2)
{
writeln(M / 2);
return;
}
writeln(N + ((M - N * 2) / 4));
}
| D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main() {
string[] inputs = split(readln());
int a = to!int(inputs[0]);
int b = to!int(inputs[1]);
int c = to!int(inputs[2]);
min(a + b, b + c, c + a).writeln;
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
void main()
{
auto R = readln.chomp.to!int;
if (R < 1200) {
writeln("ABC");
} else if (R < 2800) {
writeln("ARC");
} else {
writeln("AGC");
}
} | D |
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
writeln(48 - readln.chomp.to!int);
}
| 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() {
int k;
scan(k);
auto ans = (k / 2) * ((k + 1) / 2);
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
| D |
import std.stdio,std.string;
import std.conv,std.math;
void main(){
int [] n = readln.chomp.split.to!(int[]);
if((abs(n[0]-n[1])<=n[3] &&abs(n[1]-n[2])<=n[3])
||abs(n[0]-n[2])<=n[3]){
writeln("Yes");
}
else{
writeln("No");
}
} | D |
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
string S, T;
scan(S, T);
writeln(T, S);
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string;
int[10^^5] BS;
bool[10^^5] HS;
void main()
{
auto n = readln.chomp.to!int;
foreach (i; 0..n) {
BS[i] = readln.chomp.to!int - 1;
}
int p, cnt;
for (;;) {
++cnt;
HS[p] = true;
p = BS[p];
if (p == 1) {
writeln(cnt);
return;
}
if (HS[p]) {
writeln(-1);
return;
}
}
} | D |
import std.stdio, std.string, std.conv, std.range, std.array, std.algorithm;
import std.uni, std.math, std.container, std.typecons, std.typetuple;
import core.bitop, std.datetime;
immutable long mod = 10^^9 + 7;
immutable long inf = mod;
void main(){
long m, n;
readVars(m, n);
long ans = modpow(m, n, mod);
writeln(ans);
}
T modpow(T)(T x, T y, T mod){
return y ? modpow(x, y>>1, mod)^^2 % mod * x^^(y&1) % mod : 1;
}
void readVars(T...)(auto ref T args){
auto line = readln.split;
foreach(ref arg ; args){
arg = line.front.to!(typeof(arg));
line.popFront;
}
if(!line.empty){
throw new Exception("args num < input num");
}
} | D |
import std.stdio, std.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 a, b, c, x, y;
scan(a, b, c, x, y);
long ans = infl;
foreach (i ; 0 .. max(x, y) + 1) {
long t = 1L * i * 2 * c;
t += 1L * max(0, x - i) * a;
t += 1L * max(0, y - i) * b;
chmin(ans, t);
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
| D |
import std.stdio;
import std.string;
import std.conv;
void main() {
int N = to!int(chomp(readln()));
int x = N * 800;
int y = (N / 15) * 200;
(x - y).writeln;
}
| D |
void main() {
"square1001".writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
| D |
import 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);
}
enum MOD = 10 ^^ 9 + 7;
void main()
{
long a = lread();
long b = lread();
long c = lread();
long d = lread();
long e = lread();
long k = lread();
writeln((k < e - a) ? ":(" : "Yay!");
}
| D |
/+ dub.sdl:
name "E"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.range, std.algorithm, std.conv;
// import dcomp.scanner;
// import dcomp.algorithm;
int n;
long[] d, v;
bool check(long fi) {
v[] = d[];
foreach_reverse(i; 1..n) {
long x = v[i];
if (fi < x) return false;
long u = (fi-x) / 10;
x += u * 10;
v[i-1] -= u;
fi = x;
}
return v[0] <= fi;
}
int main() {
auto sc = new Scanner(stdin);
string s;
sc.read(s);
d = s.map!(x => (x - '0').to!long).array;
n = d.length.to!int;
v = new long[n];
// writeln(d, " ", d.length);
/* foreach (i; 0..100) {
writeln(i, " ", check(i));
}*/
writeln((binSearch!(i => check(i))(-1L, 10L^^15) + 8) / 9);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
string[] buf;
private bool succ() {
while (!buf.length) {
if (f.eof) return false;
buf = f.readln.split;
}
return true;
}
private bool readSingle(T)(ref T x) {
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
//string or char[10] etc
x = buf.front;
buf.popFront;
} else {
static if (isStaticArray!T) {
//static
assert(buf.length == T.length);
}
x = buf.map!(to!E).array;
buf.length = 0;
}
} else {
x = buf.front.to!T;
buf.popFront;
}
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);
}
}
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
fout.writeln("1 2 3");
fout.writeln("ab cde");
fout.writeln("1.0 1.0 2.0");
fout.close;
Scanner sc = new Scanner(File(fileName, "r"));
int a;
int[2] b;
char[2] c;
string d;
double e;
double[] f;
sc.read(a, b, c, d, e, f);
assert(a == 1);
assert(equal(b[], [2, 3]));
assert(equal(c[], "ab"));
assert(equal(d, "cde"));
assert(e == 1.0);
assert(equal(f, [1.0, 2.0]));
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/algorithm.d */
// module dcomp.algorithm;
import std.range.primitives;
import std.traits : isFloatingPoint, isIntegral;
//[0,0,0,...,1,1,1]で、初めて1となる場所を探す。pred(l) == 0, pred(r) == 1と仮定
T binSearch(alias pred, T)(T l, T r) if (isIntegral!T) {
while (r-l > 1) {
T md = (l+r)/2;
if (!pred(md)) l = md;
else r = md;
}
return r;
}
T binSearch(alias pred, T)(T l, T r, int cnt = 60) if (isFloatingPoint!T) {
foreach (i; 0..cnt) {
T md = (l+r)/2;
if (!pred(md)) l = md;
else r = md;
}
return r;
}
Rotator!Range rotator(Range)(Range r) {
return Rotator!Range(r);
}
struct Rotator(Range)
if (isForwardRange!Range && hasLength!Range) {
size_t cnt;
Range start, now;
this(Range r) {
cnt = 0;
start = r.save;
now = r.save;
}
this(this) {
start = start.save;
now = now.save;
}
@property bool empty() {
return now.empty;
}
@property auto front() {
assert(!now.empty);
import std.range : take, chain;
return chain(now, start.take(cnt));
}
@property Rotator!Range save() {
return this;
}
void popFront() {
cnt++;
now.popFront;
}
}
E minimum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed)
if (isInputRange!Range && !isInfinite!Range) {
import std.algorithm, std.functional;
return reduce!((a, b) => binaryFun!pred(a, b) ? a : b)(seed, range);
}
ElementType!Range minimum(alias pred = "a < b", Range)(Range range) {
assert(!range.empty, "range must not empty");
auto e = range.front; range.popFront;
return minimum!pred(range, e);
}
E maximum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed)
if (isInputRange!Range && !isInfinite!Range) {
import std.algorithm, std.functional;
return reduce!((a, b) => binaryFun!pred(a, b) ? b : a)(seed, range);
}
ElementType!Range maximum(alias pred = "a < b", Range)(Range range) {
assert(!range.empty, "range must not empty");
auto e = range.front; range.popFront;
return maximum!pred(range, e);
}
unittest {
assert(minimum([2, 1, 3]) == 1);
assert(minimum!"a > b"([2, 1, 3]) == 3);
assert(minimum([2, 1, 3], -1) == -1);
assert(minimum!"a > b"([2, 1, 3], 100) == 100);
assert(maximum([2, 1, 3]) == 3);
assert(maximum!"a > b"([2, 1, 3]) == 1);
assert(maximum([2, 1, 3], 100) == 100);
assert(maximum!"a > b"([2, 1, 3], -1) == -1);
}
bool[ElementType!Range] toMap(Range)(Range r) {
import std.algorithm : each;
bool[ElementType!Range] res;
r.each!(a => res[a] = true);
return res;
}
| 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;
}
void main() {
int N = readln.chomp.to!int;
int[] as = readln.chomp.map!(a => a=='('?1:-1).cumulativeFold!"a+b"(0).array;
as = [0]~as;
int m = as.reduce!min;
(-m).times!(() => '('.write);
as[] -= m;
foreach(i; 0..as.length-1) {
if (as[i+1]-as[i]>0) {
'('.write;
} else {
')'.write;
}
}
as.back.times!(() => ')'.write);
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;
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;
const long INF = long.max/3;
const long MOD = 10L^^9+7;
void main() {
char[] s = readln.chomp.to!(char[]);
s[3] = '8';
s.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.3"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
int main() {
Scanner sc = new Scanner(stdin);
int a, b, c;
sc.read(a, b, c);
if (a == b) writeln(c);
else if (b == c) writeln(a);
else writeln(b);
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);
}
}
}
}
version (X86) static if (__VERSION__ < 2071) {
import core.bitop : bsf, bsr, popcnt;
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
// 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.algorithm;
import std.array;
import std.conv;
import std.range;
import std.stdio;
import std.string;
enum S { E = 0, A = 1, B = 2 };
void main() {
string s = readln.strip;
immutable n = s.length;
auto z = new dchar[n];
s.copy(z);
debug stderr.writeln(z);
long res;
int i;
int cnt;
S t = S.E;
foreach (c; z) {
debug stderr.writeln ("state: ", t, ", c = ", c, ", cnt = ", cnt);
if (t == S.E) {
if (c == 'A') {
t = S.A;
cnt = 1;
} else if (c == 'B' || c == 'C') {
}
} else if (t == S.A) {
if (c == 'A') {
++cnt;
} else if (c == 'B') {
t = S.B;
} else if (c == 'C') {
t = S.E;
}
} else if (t == S.B) {
if (c == 'A') {
t = S.A;
cnt = 1;
} else if (c == 'B') {
t = S.E;
cnt = 0;
} else if (c == 'C') {
res += cnt;
t = S.A;
}
}
}
writeln (res);
}
| D |
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std;
// dfmt on
/// x^^n % m
long powmod(long x, long n)
{
alias T = long;
T m = 10 ^^ 9 + 7;
if (n < 1)
return 1;
if (n & 1)
{
return x * powmod(x, n - 1) % m;
}
T tmp = powmod(x, n / 2);
return tmp * tmp % m;
}
long invmod(long x)
{
return powmod(x, MOD - 2L);
}
// alias invmod = (long x, long m = 10 ^^ 9 + 7) => powmod(x, m - 2, m);
void main()
{
long N, M;
scan(N, M);
auto memo = new long[](M + 1);
memo[0] = 1;
foreach (i; 1 .. M + 1)
memo[i] = memo[i - 1] * i % MOD;
auto inv = new long[](M + 1);
foreach (i; 0 .. M + 1)
inv[i] = invmod(memo[i]);
long Permutation(long n, long r)
{
return memo[n] * inv[n - r] % MOD;
}
long Combination(long n, long r)
{
return (memo[n] * inv[n - r] % MOD) * inv[r] % MOD;
}
long a = Permutation(M, N);
foreach (i; 1 .. N + 1)
{
long b = Permutation(M - i, N - i);
// dprint(i, a, b, (N - i + 1));
a += MOD;
a += (b * Combination(N, i) % MOD) * ((i & 1) ? -1 : 1);
a %= MOD;
}
writeln(a * Permutation(M, N) % MOD);
}
| D |
import std;
auto input()
{
return readln().chomp();
}
alias sread = () => readln.chomp();
alias aryread(T = long) = () => readln.split.to!(T[]);
void main()
{
long x;
scan(x);
long k = 1;
while ((x * k) % 360 != 0)
{
k++;
}
writeln(k);
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto S = readln.chomp;
auto N = S.length;
long min_n;
auto ns = new long[](N+1);
long x;
foreach (i; 1..N+1) {
if (S[i-1] == '<') {
ns[i] = ++x;
} else {
x = 0;
}
}
x = 0;
foreach_reverse (i; 0..N) {
if (S[i] == '>') {
ns[i] = max(ns[i], ++x);
} else {
x = 0;
}
}
writeln(0L.reduce!"a + b"(ns));
} | D |
void main() {
problem();
}
void problem() {
auto a = scan!long;
long solve() {
auto mod = a % 1000;
return mod == 0 ? 0 : 1000 - mod;
}
solve().writeln;
}
// ----------------------------------------------
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
// -----------------------------------------------
| D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
import std.range;
void main(){
auto A=readln.chomp;
writeln(A.replace(","," "));
} | 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 inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n, h, w;
scan(n);
scan(h);
scan(w);
int ans = (n - h + 1) * (n - w + 1);
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;
}
long powmod(long x, long y, long mod = 1_000_000_007L) {
long res = 1L;
while (y > 0) {
if (y & 1) {
(res *= x) %= mod;
}
(x *= x) %= mod;
y >>= 1;
}
return res;
} | D |
import std.stdio, std.conv, std.array,std.string;
void main()
{
string[] input=readln.split;
int a=input[0].to!int,b=input[1].to!int;
if((a*b)%2==0){
writeln("Even");
}else{
writeln("Odd");
}
} | 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 a = read!string;
auto b = read!string;
for (int i = 0; i < a.length; i++) {
write(a[i]);
if (i < b.length) write(b[i]);
}
writeln();
}
| D |
import std.stdio,std.math,std.string,std.conv,std.typecons,std.format;
import std.algorithm,std.range,std.array;
T[] readarr(T=long)(){return readln.chomp.split.to!(T[]);}
void scan(T...)(ref T args){auto input=readln.chomp.split;foreach(i,t;T)args[i]=input[i].to!t;}
//END OF TEMPLATE
void main(){
ulong k;
k.scan;
k.solve.writeln;
}
struct Queue{
public:
char[][] list;
auto enq(char c){list~=[c];}
auto enq(char[] c){list~=c;}
auto deq(){auto d=list[0];list=list.length==1?[]:list[1..$];return d;}
}
char[] co(char c){
if(c=='0')
return ['0','1'];
if('1'<=c&&c<='8')
return [to!char(c-1),c,to!char(c+1)];
if(c=='9')
return ['8','9'];
assert(0);
}
auto solve(ulong k){
Queue q;
foreach(c;'1'.to!char..('9'+1).to!char)q.enq(c);
foreach(i;0..k-1){
auto tmp=q.deq;
foreach(s;tmp[$-1].co)
q.enq(tmp~s);
}
return q.deq;
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto s = readln.chomp;
size_t i, j = s.length-1;
int c;
while (j > i) {
if (s[i] == s[j]) {
++i; --j;
} else if (s[i] == 'x') {
++i; ++c;
} else if (s[j] == 'x') {
--j; ++c;
} else {
writeln(-1);
return;
}
}
writeln(c);
} | D |
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
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;
void main() {
string s = read!string;
bool good = true;
for (int i = 1; i < s.length; i++) {
if (s[i - 1] == s[i]) {
good = false;
break;
}
}
writeln(good ? "Good" : "Bad");
}
| 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() {
long n, p;
scan(n, p);
long ans = 1;
for (long i = 2; i*i <= p; i++) {
int cnt;
while (p % i == 0) {
p /= i;
cnt++;
}
ans *= i^^(cnt / n);
}
if (n == 1 && p > 1) {
ans *= p;
}
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 |
void main()
{
string s = rdStr;
string t = "AKIHABARA";
string u;
foreach (i, x; s)
{
if (i == 0 && x != 'A') u ~= 'A';
if ((x == 'B' || x == 'R') && u.back != 'A') u ~= 'A';
u ~= x;
if (i == s.length - 1 && x == 'R') u ~= 'A';
}
writeln(t == u ? "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.stdc.stdio;
import std.typecons;
import std.algorithm;
struct Seg{
Seg* l,r;
int c;
}
int Count(Seg* x){
return x is null?0:x.c;
}
Seg* Left(Seg* x){
return x is null?null:x.l;
}
Seg* Right(Seg* x){
return x is null?null:x.r;
}
Seg[] buf;
Seg* newSeg(Seg* l,Seg* r){
static int i;
buf[i]=Seg(l,r,l.Count+r.Count);
return &buf[i++];
}
Seg* Add(Seg* x,int i,int l=0,int r=1<<30){
if(i<l||r<=i)
return x;
if(i==l&&i+1==r){
Seg* t=newSeg(null,null);
t.c=1+x.Count;
return t;
}else
return newSeg(Add(x.Left,i,l,(l+r)/2),Add(x.Right,i,(l+r)/2,r));
}
int Get(Seg* x,int i,int l=0,int r=1<<30){
if(x is null)
return 0;
if(i<=l)
return 0;
if(r<=i)
return x.Count;
return Get(x.Left,i,l,(l+r)/2)+Get(x.Right,i,(l+r)/2,r);
}
int[][] par;
int[] dep;
void LCA_Init(){
foreach(i;1..20)
foreach(j,ref p;par[i])
p=par[i-1][j]>=0?par[i-1][par[i-1][j]]:-1;
}
int LCA(int a,int b){
if(dep[a]>dep[b])
swap(a,b);
int d=dep[b]-dep[a];
foreach(i;0..20)
if((d>>i)&1)
b=par[i][b];
if(a==b)
return a;
foreach_reverse(i;0..20)
if(par[i][a]!=par[i][b])
a=par[i][a],b=par[i][b];
return par[0][a];
}
void main(){
buf = new Seg[6000000];
int n,q;
scanf("%d%d",&n,&q);
int[] x=new int[n];
foreach(ref v;x)
scanf("%d",&v);
int[][] graph=new int[][n];
foreach(i;0..n-1){
int a,b;
scanf("%d%d",&a,&b);
graph[--a]~=--b;
graph[b]~=a;
}
Seg*[] segs=new Seg*[n];
par=new int[][](20,n);
dep=new int[n];
alias Tuple!(int,"i",int,"p",Seg*,"s",int,"d") stack;
stack[] st=new stack[n*2];
st[0]=stack(0,-1,null,0);
int ss=1;
while(ss){
stack cur=st[--ss];
with(cur){
par[0][i]=p;
dep[i]=d;
segs[i]=s.Add(x[i]);
foreach(c;graph[i])
if(c!=p)
st[ss++]=stack(c,i,segs[i],d+1);
}
}
LCA_Init;
foreach(_;0..q){
int v,w,k,l=0,r=1<<30;
scanf("%d%d%d",&v,&w,&k);
int p=LCA(--v,--w);
Seg* a=segs[v],b=segs[w],c=segs[p],d=p?segs[par[0][p]]:null;
while(r-l>1){
int m=(r+l)/2;
if(k<=a.Get(m)+b.Get(m)-c.Get(m)-d.Get(m))
r=m;
else
l=m;
}
printf("%d\n",l);
}
} | 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 inf = 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);
int ma;
int ans;
foreach (i ; 0 .. n) {
int ai, bi;
scan(ai, bi);
if (ai > ma) {
ma = ai;
ans = ai + bi;
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
| D |
import std.stdio;
import std.conv;
import std.algorithm;
import std.string;
import std.file;
int main() {
int[] hs;
string l;
while((l = readln()).length >= 2) printf("%d\n", to!string(to!int(l.split()[0]) + to!int(l.split()[1])).length);
return 0;
} | D |
import std.stdio, std.range, std.conv, std.string;
import std.algorithm.comparison, std.algorithm.iteration, std.algorithm.mutation, std.algorithm.searching, std.algorithm.setops, std.algorithm.sorting;
void main()
{
int[] input = readln().split.to!(int[]);
int N = input[0];
int M = input[1];
int C = input[2];
auto blist = readln().split.to!(int[]);
int count;
foreach(int i; 0..N)
{
auto sList = readln().split.to!(int[]);
long sum = 0;
foreach(int k; 0..M)
{
sum += blist[k]*sList[k];
}
sum += C;
count += sum > 0 ? 1 : 0;
}
writeln(count);
} | D |
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n, m;
rd(n, m);
auto a=readln.split.to!(int[]);
auto b=readln.split.to!(int[]);
int mx=0;
foreach(i; 0..m){
int j=i;
foreach(e; a){
if(j>=m) break;
if(e==b[j]) j++;
}
mx=max(mx, j-i);
}
writeln(mx);
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
} | D |
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
string a, b; readV(a, b);
writeln(a == b ? "H" : "D");
}
| D |
import std;
void main() {
int n;
scanf("%d\n", &n);
auto as = readln.chomp.split.to!(int[]);
long count = 0;
int[long] memj;
foreach(i;0..as.length) {
++memj[i-as[i]];
}
foreach(i;0..as.length) {
if (i+as[i] in memj) {
count += memj[i+as[i]];
}
}
write(count);
}
| D |
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.ascii;
void main()
{
auto a = readln.chomp[0];
auto b = readln.chomp[1];
auto c = readln.chomp[2];
writeln(a,b,c);
}
| D |
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto S = readln.chomp;
auto N = S.length.to!int;
int[] cnt = new int[](26);
foreach (s; S) cnt[s-'a'] += 1;
if (cnt.map!(c => c % 2).sum > 1) {
writeln(-1);
return;
}
int[] cnt2 = new int[](26);
string s1, s2;
foreach(s; S) {
int c = s - 'a';
cnt2[c] += 1;
if (cnt2[c] <= (cnt[c] + 1) / 2) s1 ~= s;
else s2 ~= s;
}
auto number = new int[][](26);
int tmp = 0;
foreach (s; s1) number[s-'a'] ~= tmp++;
int[] cnt3 = new int[](26);
int[] new_s = new int[](N);
foreach (i; 0..N) {
int c = S[N-i-1] - 'a';
if (cnt3[c] + 1 == number[c].length && cnt[c] % 2) new_s[i] = 2*N - 2, cnt3[c]++;
else if (cnt3[c] < number[c].length) new_s[i] = number[c][cnt3[c]++];
else new_s[i] = 2*N-1;
}
auto st = new SegmentTree(2*N+1);
long ans;
foreach (s; new_s) {
ans += st.sum(s+1, 2*N+1);
st.add(s, 1);
}
ans.writeln;
}
class SegmentTree {
long[] table;
int size;
this(int n) {
assert(bsr(n) < 29);
size = 1 << (bsr(n) + 2);
table = new long[](size);
}
void add(int pos, long num) {
return add(pos, num, 0, 0, size/2-1);
}
void add(int pos, long num, int i, int left, int right) {
table[i] += num;
if (left == right)
return;
auto mid = (left + right) / 2;
if (pos <= mid)
add(pos, num, i*2+1, left, mid);
else
add(pos, num, i*2+2, mid+1, right);
}
long sum(int pl, int pr) {
return sum(pl, pr, 0, 0, size/2-1);
}
long sum(int pl, int pr, int i, int left, int right) {
if (pl > right || pr < left)
return 0;
else if (pl <= left && right <= pr)
return table[i];
else
return
sum(pl, pr, i*2+1, left, (left+right)/2) +
sum(pl, pr, i*2+2, (left+right)/2+1, right);
}
} | D |
import std.stdio;
import std.algorithm;
import std.conv;
import std.datetime;
import std.numeric;
import std.math;
import std.string;
string my_readln() { return chomp(readln()); }
void main()
{//try{
auto tokens = split(my_readln());
auto N = to!string(tokens[0]);
ulong ans;
foreach (e; N)
{
if (e == '2') ++ans;
}
writeln(ans);
stdout.flush();
/*}catch (Throwable e)
{
writeln(e.toString());
}
readln();*/
} | D |
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() {
while (true) {
auto ab = readints;
int a = ab[0], b = ab[1];
if (a == 0 && b == 0)
break;
if (a % 2 == 1 && b % 2 == 1) {
writeln("no");
}
else {
writeln("yes");
}
}
} | D |
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
auto s=readln.chomp.to!(char[]);
int w; rd(w);
auto t="".to!(char[]);
int i=0;
while(i<s.length){
t~=s[i];
i+=w;
}
writeln(t);
}
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.algorithm;
import std.math;
import std.string;
import std.conv;
import std.range;
void main() {
int sum = 0;
foreach (i; 0..10) {
sum += readln.chomp.to!int;
}
sum.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) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto N = RD;
bool[string] set;
foreach (i; 0..N)
{
set[RD!string] = true;
}
writeln(set.keys.length);
stdout.flush;
debug readln;
} | D |
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() {
string s;
while ((s = readln.chomp) != null) {
auto ab = s.split.map!(to!int).array;
int a = ab[0], b = ab[1];
writeln(gcd(a, b));
}
} | 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()
{
string s; readV(s);
writeln(s.count('1'));
}
| D |
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random;
import std.typecons, std.functional, std.traits,std.concurrency;
import std.algorithm, std.container;
import core.bitop, core.time, core.memory;
import std.bitmanip;
import std.regex;
enum INF = long.max/3;
enum MOD = 10L^^9+7;
//辞書順順列はiota(1,N),nextPermituionを使う
void end(T)(T v)
if(isIntegral!T||isSomeString!T||isSomeChar!T)
{
import core.stdc.stdlib;
writeln(v);
exit(0);
}
T[] scanArray(T = long)()
{
static char[] scanBuf;
readln(scanBuf);
return scanBuf.split.to!(T[]);
}
dchar scanChar()
{
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
return cast(dchar)c;
}
T scanElem(T = long)()
{
import core.stdc.stdlib;
static auto scanBuf = appender!(char[])([]);
scanBuf.clear;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
scanBuf ~= cast(char) c;
c = getchar;
}
return scanBuf.data.to!T;
}
dchar[] scanString(){
return scanElem!(dchar[]);
}
void main()
{
writeln("ABC"~scanString);
}
| D |
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;
immutable long MOD = 10^^9 + 7;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto L = s[1].to!long;
auto R = s[2].to!long;
long r = R + 2 - R % 3;
long l = L - L % 3;
long x = (r - l + 1) / 3;
auto num = new long[](3);
num[] = x;
if (L % 3 == 1) {
num[0] -= 1;
} else if (L % 3 == 2) {
num[0] -= 1;
num[1] -= 1;
}
if (R % 3 == 0) {
num[1] -= 1;
num[2] -= 1;
} else if (R % 3 == 1) {
num[2] -= 1;
}
auto dp = new long[][](N+1, 3);
dp[0][0] = 1;
foreach (i; 0..N) {
foreach (j; 0..3) {
foreach (k; 0..3) {
(dp[i+1][(j+k)%3] += dp[i][j] * num[k] % MOD) %= MOD;
}
}
}
dp[N][0].writeln;
}
| D |
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdlib;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto K = s[1];
auto C = s[2];
auto S = readln.chomp;
auto hoge = new int[](N+C+10);
foreach_reverse (i; 0..N) {
if (S[i] == 'x') {
hoge[i] = hoge[i + 1];
} else {
hoge[i] = hoge[i + C + 1] + 1;
}
}
int tmp = 0;
int[] ans;
for (int i = 0; i < N; ) {
if (S[i] == 'x') {
i += 1;
continue;
}
if (tmp + hoge[i + 1] < K) ans ~= i + 1;
tmp += 1;
i += C + 1;
}
ans.each!writeln;
}
class SegmentTree(T, alias op, T e) {
T[] table;
int size;
int offset;
this(int n) {
size = 1;
while (size <= n) size <<= 1;
size <<= 1;
table = new T[](size);
fill(table, e);
offset = size / 2;
}
void assign(int pos, T val) {
pos += offset;
table[pos] = val;
while (pos > 1) {
pos /= 2;
table[pos] = op(table[pos*2], table[pos*2+1]);
}
}
void add(int pos, T val) {
pos += offset;
table[pos] += val;
while (pos > 1) {
pos /= 2;
table[pos] = op(table[pos*2], table[pos*2+1]);
}
}
T query(int l, int r) {
return query(l, r, 1, 0, offset-1);
}
T query(int l, int r, int i, int a, int b) {
if (b < l || r < a) {
return e;
} else if (l <= a && b <= r) {
return table[i];
} else {
return op(query(l, r, i*2, a, (a+b)/2), query(l, r, i*2+1, (a+b)/2+1, b));
}
}
}
| D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
writeln(readln.chomp.to!int < 1200 ? "ABC" : "ARC");
}
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.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; }
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 N = RD!long;
auto M = RD!long;
auto cnt = new long[](N);
foreach (i; 0..M)
{
auto a = RD!long;
auto b = RD!long;
if (a == 1) ++cnt[b];
else if (b == N) ++cnt[a];
}
bool ans = false;
foreach (e; cnt)
{
if (e >= 2) ans = true;
}
writeln(ans ? "POSSIBLE" : "IMPOSSIBLE");
stdout.flush();
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string;
void main()
{
auto stxy = readln.split.to!(int[]);
auto xd = stxy[2] - stxy[0];
auto yd = stxy[3] - stxy[1];
string ret;
foreach (_; 0..yd) ret ~= "U";
foreach (_; 0..xd) ret ~= "R";
foreach (_; 0..yd) ret ~= "D";
foreach (_; 0..xd+1) ret ~= "L";
foreach (_; 0..yd+1) ret ~= "U";
foreach (_; 0..xd+1) ret ~= "R";
ret ~= "DR";
foreach (_; 0..yd+1) ret ~= "D";
foreach (_; 0..xd+1) ret ~= "L";
ret ~= "U";
writeln(ret);
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto n = RD!int;
foreach (i; 0..n)
{
auto s = RD!string;
auto cnt = new int[](10);
foreach (c; s)
{
auto x = c - '0';
++cnt[x];
}
int x;
foreach (j; 0..10)
x += cnt[j] * j;
if (cnt[0] == 0 || x % 3)
writeln("cyan");
/*else if (s.length <= 3)
{
bool ok;
if (cnt[6] >= 1)
ok = true;
else if (cnt[1] == 1 && cnt[2]+cnt[8] == 1)
ok = true;
else if (cnt[4] == 1 && cnt[2]+cnt[5]+cnt[8] == 1)
ok = true;
else if (cnt[7] == 1 && cnt[8]+cnt[2] == 1)
ok = true;
writeln(ok ? "red" : "cyan");
}*/
else if (cnt[0] == 1)
{
bool ok;
foreach (j; 1..5)
{
if (cnt[j*2])
ok = true;
}
writeln(ok ? "red" : "cyan");
}
else
{
writeln("red");
}
}
stdout.flush;
debug readln;
} | D |
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N, K;
scan(N, K);
if (N % K == 0)
{
writeln(0);
return;
}
writeln(min(N % K, -(N % K - K)));
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp;
int l = N.length.to!int;
int r, p;
foreach (int i, c; N) {
auto n = c-48;
if (n) {
r = max(r, p + n-1 + (l-i-1)*9);
p += n;
}
}
writeln(max(r, p));
} | 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() {
string s;
scan(s);
bool first = (s.front == s.back) ^ (s.length % 2);
writeln(first ? "First" : "Second");
}
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.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) {
string str = readln.chomp;
if (str == "END OF INPUT") break;
str.split(" ").map!(s => s.length.to!string).reduce!"a~b".writeln;
}
} | D |
import std.stdio;
import std.string;
import std.algorithm.mutation;
void main()
{
readln;
string[] s = readln.split;
s.reverse();
writeln(s.join(" "));
} | D |
import std.stdio, std.string, std.conv;
import std.array, std.algorithm, std.range;
void main()
{
do{
auto m = iota(8).map!(_=>countUntil(readln().chomp(),'1')).array().filter!"a>=0"().array();
char c='Z';
switch(m.length)
{
case 1:
c = 'C';
break;
case 2:
if(m[0]==m[1]) c='A';
else if(m[0]<m[1]) c='E';
else c='G';
break;
case 3:
c = m[0]>m[1]?'D':'F';
break;
case 4:
default:
c = 'B';
break;
}
writeln(c);
}while(readln().length);
} | D |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.