code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.stdio,std.array,std.conv,std.algorithm;
void main(){
auto a=readln().split().map!(to!int);
writeln(a[0]-(a[0]>a[1]));
}
|
D
|
import std.stdio;
import std.string;
import std.format;
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.concurrency;
import std.traits;
import std.uni;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;
enum long INF = long.max/3;
enum long MOD = 10L^^9+7;
void main() {
long N;
scanln(N);
long[] as = N.rep!(() => readln.chomp.to!long);
if (as.all!"a%2==0") {
"second".writeln;
} else {
"first".writeln;
}
}
// ----------------------------------------------
void times(alias fun)(long n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(long n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
// `(x+y-1)/y` will only work for positive numbers ...
T t = x / y;
if (t * y < x) t++;
return t;
}
T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
T t = x / y;
if (t * y > x) t--;
return t;
}
ref T ch(alias fun, T, S...)(ref T lhs, S rhs) {
return lhs = fun(lhs, rhs);
}
unittest {
long x = 1000;
x.ch!min(2000);
assert(x == 1000);
x.ch!min(3, 2, 1);
assert(x == 1);
x.ch!max(100).ch!min(1000); // clamp
assert(x == 100);
x.ch!max(0).ch!min(10); // clamp
assert(x == 10);
}
mixin template Constructor() {
import std.traits : FieldNameTuple;
this(Args...)(Args args) {
// static foreach(i, v; args) {
foreach(i, v; args) {
mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v;
}
}
}
void scanln(Args...)(auto ref Args args) {
import std.meta;
template getFormat(T) {
static if (isIntegral!T) {
enum getFormat = "%d";
} else static if (isFloatingPoint!T) {
enum getFormat = "%g";
} else static if (isSomeString!T || isSomeChar!T) {
enum getFormat = "%s";
} else {
static assert(false);
}
}
enum string fmt = [staticMap!(getFormat, Args)].join(" ");
string[] inputs = readln.chomp.split;
foreach(i, ref v; args) {
v = inputs[i].to!(Args[i]);
}
}
// 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) {
private auto extremum(alias map, alias selector = "a < b", Range)(Range r)
if (isInputRange!Range && !isInfinite!Range &&
is(typeof(unaryFun!map(ElementType!(Range).init))))
in
{
assert(!r.empty, "r is an empty range");
}
body
{
alias Element = ElementType!Range;
Unqual!Element seed = r.front;
r.popFront();
return extremum!(map, selector)(r, seed);
}
private auto extremum(alias map, alias selector = "a < b", Range,
RangeElementType = ElementType!Range)
(Range r, RangeElementType seedElement)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void) &&
is(typeof(unaryFun!map(ElementType!(Range).init))))
{
alias mapFun = unaryFun!map;
alias selectorFun = binaryFun!selector;
alias Element = ElementType!Range;
alias CommonElement = CommonType!(Element, RangeElementType);
Unqual!CommonElement extremeElement = seedElement;
alias MapType = Unqual!(typeof(mapFun(CommonElement.init)));
MapType extremeElementMapped = mapFun(extremeElement);
// direct access via a random access range is faster
static if (isRandomAccessRange!Range)
{
foreach (const i; 0 .. r.length)
{
MapType mapElement = mapFun(r[i]);
if (selectorFun(mapElement, extremeElementMapped))
{
extremeElement = r[i];
extremeElementMapped = mapElement;
}
}
}
else
{
while (!r.empty)
{
MapType mapElement = mapFun(r.front);
if (selectorFun(mapElement, extremeElementMapped))
{
extremeElement = r.front;
extremeElementMapped = mapElement;
}
r.popFront();
}
}
return extremeElement;
}
private auto extremum(alias selector = "a < b", Range)(Range r)
if (isInputRange!Range && !isInfinite!Range &&
!is(typeof(unaryFun!selector(ElementType!(Range).init))))
{
alias Element = ElementType!Range;
Unqual!Element seed = r.front;
r.popFront();
return extremum!selector(r, seed);
}
private auto extremum(alias selector = "a < b", Range,
RangeElementType = ElementType!Range)
(Range r, RangeElementType seedElement)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void) &&
!is(typeof(unaryFun!selector(ElementType!(Range).init))))
{
alias Element = ElementType!Range;
alias CommonElement = CommonType!(Element, RangeElementType);
Unqual!CommonElement extremeElement = seedElement;
alias selectorFun = binaryFun!selector;
// direct access via a random access range is faster
static if (isRandomAccessRange!Range)
{
foreach (const i; 0 .. r.length)
{
if (selectorFun(r[i], extremeElement))
{
extremeElement = r[i];
}
}
}
else
{
while (!r.empty)
{
if (selectorFun(r.front, extremeElement))
{
extremeElement = r.front;
}
r.popFront();
}
}
return extremeElement;
}
auto minElement(Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum(r);
}
auto minElement(alias map, Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!map(r, seed);
}
auto minElement(Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum(r, seed);
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum!(map, "a > b")(r);
}
auto maxElement(Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum!`a > b`(r);
}
auto maxElement(alias map, Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!(map, "a > b")(r, seed);
}
auto maxElement(Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!`a > b`(r, seed);
}
}
// popcnt with ulongs was added in D 2.071.0
static if (__VERSION__ < 2071) {
ulong popcnt(ulong x) {
x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L);
x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L);
x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL);
x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL);
x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL);
x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL);
return x;
}
}
|
D
|
import std.stdio;
import std.string;
import std.algorithm;
import std.array;
import std.conv;
int solve(int[] list) {
while (list.length > 1) {
int[] res = new int[](list.length - 1);
for(int i = 0; i < list.length - 1; i++) {
res[i] = (list[i] + list[i + 1]) % 10;
}
list = res;
}
return list[0];
}
void main() {
string line;
while ((line = readln.chomp).length != 0) {
line.array.map!((x) => x.to!(int) - '0').array.solve.writeln;
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.conv;
void main() {
auto s = readln.chomp;
string res;
foreach(i; s) {
switch(i) {
case '0', '1':
res ~= i;
break;
case 'B':
if(res.length != 0) res.popBack();
break;
default: break;
}
}
res.writeln;
}
// ===================================
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 (isBasicType!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
int ri() {
return readAs!int;
}
|
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 n = readln.strip.to!int;
auto s = readln.strip.splitter;
auto a = s.front; s.popFront;
auto b = s.front;
foreach (i; 0 .. n) {
write (a[i], b[i]);
}
writeln;
}
|
D
|
import std.stdio,
std.string,
std.conv,
std.algorithm;
int abs(int x) {
return (x >= 0) ? x : -x;
}
void main() {
int N = readln.chomp.to!(int);
int[][] a;
for (int i = 0; i < N; i++) {
a ~= readln.chomp.split.to!(int[]);
}
bool f = true;
int t = 0, x = 0, y = 0;
for (int i = 0; i < N; i++) {
int dt = abs(a[i][0] - x), dx = a[i][1] - x, dy = a[i][2] - y;
int d = abs(dx) + abs(dy);
f = d <= dt && (d - dt) % 2 == 0;
if (!f) {
break;
}
}
writeln(f ? "Yes" : "No");
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
void main() {
immutable n = readln.strip.to!long;
ulong res;
void check (long x) {
--x;
if (n / x == n % x) res += x;
}
for (int i = 2; i.to!long * i <= n; ++i) {
check (i);
long j = n / i;
if (j != i) {
check (n / i);
}
}
if (n > 1) check (n);
writeln (res);
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.array;
import std.functional;
import std.conv;
import std.math;
void main(){
while(true){
auto s = readln();
if(stdin.eof()) break;
int n = to!int(s.chomp());
int[4001] arr;
for(int i=0;i<=1000;i++){
for(int j=0;j<=1000;j++){
arr[i+j]++;
}
}
int ans;
for(int i=0;i<=n;i++){
ans += arr[i] * arr[n-i];
}
writeln(ans);
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() {
auto xt = readints;
int x = xt[0], t = xt[1];
int ans = max(0, x - t);
writeln(ans);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.math;
void main() {
((ushort[] ip) => ip[0] + ip[1] >= ip[2] ? "Yes" : "No")(readln.split.to!(ushort[])).writeln;
}
|
D
|
void main() {
int[] tmp = readln.split.to!(int[]);
int a = tmp[0], b = tmp[1];
writeln(a >= 13 ? b : a >= 6 ? b / 2 : 0);
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.typecons;
|
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(){
ulong n,k;
scan(n,k);
ulong sum;
foreach(l;k..n+2){
sum=(sum+l*(n-l+1)+1)%(10^^9+7);
}
sum.writeln;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
long x, a, b;
scan(x, a, b);
auto z = abs(x - a) - abs(x - b);
z = z / abs(z);
auto ary = ["A", "", "B"];
writeln(ary[z + 1]);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
bool f(int n) {
return 1 <= n && n <= 12;
}
string calc(string s) {
auto a = (10 * (s[0] - '0')) + (s[1] - '0');
auto b = (10 * (s[2] - '0')) + (s[3] - '0');
if (f(a) && f(b)) return "AMBIGUOUS";
if (f(a)) return "MMYY";
if (f(b)) return "YYMM";
return "NA";
}
void main() {
string s = read!string;
writeln(calc(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;
long N, cnt;
bool ask(long n) {
writeln("? ", n);
stdout.flush;
cnt += 1;
debug {
string sn = n.to!string;
string sN = N.to!string;
return (n <= N && sn <= sN) || (n > N && sn > sN);
} else {
return readln.chomp == "Y";
}
}
void main() {
debug {N = readln.chomp.to!long;}
long x = 10;
for (; x <= 10^^9 && ask(x); x *= 10) {}
if (x == 10L^^10) { // N = 1, 10, 100, ...
x = 2;
for (; x <= 2 * 10L^^9 && !ask(x); x *= 10) {}
writeln("! ", x / 2);
} else {
long k = x / 10;
x = 0;
for (; k > 0; k /= 10) {
long hi = 10;
long lo = 0;
while (hi - lo > 1) {
long mid = (hi + lo) / 2;
if (ask((x * 10 + mid) * 10L^^9)) hi = mid;
else lo = mid;
}
x = x * 10 + lo;
}
writeln("! ", x + 1);
}
debug {cnt.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 a, b, c; readV(a, b, c);
auto dp = new int[][][](60, 60, 60), inf = 10^^9;
int calc(int a, int b, int c)
{
if (a >= 60 || b >= 60 || c >= 60) return inf;
if (a == b && b == c) return 0;
if (dp[a][b][c]) return dp[a][b][c];
return dp[a][b][c] = min(calc(a+1, b+1, c),
calc(a+1, b, c+1),
calc(a, b+1, c+1),
calc(a+2, b, c),
calc(a, b+2, c),
calc(a, b, c+2)) + 1;
}
writeln(calc(a, b, c));
}
|
D
|
import std.algorithm,
std.string,
std.range,
std.stdio,
std.conv,
std.math;
long f(long b, long n) {
return (n < b)
? n
: f(b, n/b) + n % b;
}
long solve(long N, long S) {
if (N < S) {
return -1;
}
if (N == S) {
return N + 1;
}
foreach (i; 2.iota(sqrt(N.to!float) + 1)) {
auto j = i.to!long;
if (f(j, N) == S) {
return j;
}
}
long ans = long.max;
foreach (i; 1.iota(sqrt(N.to!float) + 1)) {
auto j = i.to!long;
long b = (N - S) / j + 1;
if (b == 1) continue;
if (f(b, N) == S) ans = min(ans, b);
}
if (ans < long.max) {
return ans;
} else {
return -1;
}
}
void main() {
long N = readln.chomp.to!long,
S = readln.chomp.to!long;
writeln(solve(N, S));
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
size_t[][26] D;
void main()
{
auto s = readln.chomp.to!(char[]);
auto t = readln.chomp.to!(char[]);
foreach (i, c; s) {
D[c - 'a'] ~= i;
}
ulong cnt;
size_t p;
bool first = true;
foreach (c; t) {
auto d = D[c - 'a'];
if (d.empty) {
writeln(-1);
return;
}
if (first) {
first = false;
if (d[0] == 0) {
continue;
}
}
if (d[$-1] <= p) {
p = d[0];
++cnt;
continue;
} else if (d[0] > p) {
p = d[0];
continue;
}
size_t l, r = d.length-1;
while (l+1 < r) {
auto m = (l+r)/2;
if (d[m] <= p) {
l = m;
} else {
r = m;
}
}
p = d[r];
}
writeln(s.length * cnt + p + 1);
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
long x, y;
scan(x, y);
int ans = 1;
while (2*x <= y) {
x *= 2;
ans++;
}
writeln(ans);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;
void main()
{
auto s = readln.chomp;
int ans = 0;
for (int i = 0, j = s.length.to!int - 1; i < j; ++i, --j) {
if (s[i] != s[j]) { ++ans; }
}
ans.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;
import std.ascii;
void main()
{
auto s = readln.chomp;
if (s[2] == s[3] && s[4] == s[5]) {
writeln("Yes");
} else {
writeln("No");
}
}
|
D
|
// unihernandez22
// https://atcoder.jp/contests/abc076/tasks/abc076_c
// string manipulation
import std.stdio;
import std.string;
void main() {
string s = readln.chomp;
string t = readln.chomp;
long idx = -1;
bool matched;
for(long i = (s.count-t.count); i >= 0; i--) {
if (s[i] == '?' || s[i] == t[0]) {
matched = true;
foreach(j; 0..t.count) {
if (s[i+j] != '?' && s[i+j] != t[j]) {
matched = false;
break;
}
}
if (matched) {
idx = i;
break;
}
}
}
if (idx == -1) {
writeln("UNRESTORABLE");
return;
}
foreach(i; 0..s.count) {
if (s[i] == '?' && (i < idx || i >= t.count+idx))
write("a");
else if (s[i] == '?')
write(t[i-idx]);
else
write(s[i]);
} writeln;
}
|
D
|
import std.stdio;
import std.ascii;
void main() {
int sum = 0;
foreach (string input; stdin.lines) {
auto num = 0;
foreach (c; input) {
if (c.isDigit) {
num = (num * 10) + (c - '0');
} else {
sum += num;
num = 0;
}
}
}
writeln(sum);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main() {
string[] inputs = split(readln());
int a, b;
foreach(v; inputs) {
switch(v) {
case "5":
a++;
break;
case "7":
b++;
break;
default:
break;
}
}
if(a == 2 && b == 1) "YES".writeln;
else "NO".writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void main()
{
auto N = readln.chomp.to!int;
auto cs = readln.chomp.to!(char[]);
int c;
int i, j = N-1;
while (i < j) {
while (i < N && cs[i] != 'W') ++i;
while (j >= 0 && cs[j] != 'R') --j;
if (i < j && j < N) {
++c;
++i;
--j;
}
}
writeln(c);
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
writeln((lread() - lread()) % lread());
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
long n, m;
rd(n, m);
long ans = 1;
for (long k = 1; k * k <= m; k++) {
if (m % k == 0) {
if (n * k <= m) {
ans = max(ans, k);
}
if (n * (m / k) <= m) {
ans = max(ans, m / k);
}
}
}
writeln(ans);
}
void rd(T...)(ref T x) {
import std.stdio : readln;
import std.string : split;
import std.conv : to;
auto l = readln.split;
assert(l.length == x.length);
foreach (i, ref e; x)
e = l[i].to!(typeof(e));
}
|
D
|
import std.stdio;
import std.string;
void main()
{
char[] s = readln.chomp.dup;
char[] t = readln.chomp.dup;
string ans = "No";
foreach (i; 0..s.length){
s = s[$ - 1] ~ s[0..$ - 1];
if (s == t){
ans = "Yes";
break;
}
}
writeln(ans);
}
|
D
|
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.typecons, std.range, std.container, std.math, std.algorithm.searching, std.functional,std.mathspecial;
void main(){
writeln(totalSum(readln.chomp));
}
long totalSum(string str){
long total=0;
foreach(i,s;str){
auto num=[s].to!long;
auto headPattern=2^^i;
foreach(k;0..str.length-i){
auto tailLength=str.length-i-1-k;
auto tailPattern=tailLength==0?1:2^^(tailLength-1);
total+=num*10^^k*headPattern*tailPattern;
}
}
return total;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto abk = readln.split.to!(int[]);
auto A = abk[0];
auto B = abk[1];
auto K = abk[2];
foreach_reverse (k; 1..101) {
if (A % k == 0 && B % k == 0 && --K == 0) {
writeln(k);
return;
}
}
}
|
D
|
import std.stdio;
int main() {
for(int i=1; i<=9; i++) {
for(int j=1; j<=9; j++) {
writeln(i, "x", j, "=", i*j);
}
}
return 0;
}
|
D
|
import std.stdio, std.algorithm, std.range, std.conv;
void main(){
iota(1, 10).map!(a => iota(1, 10).map!(b => text(a, "x", b, "=", a*b)).join("\n")).join("\n").writeln;
}
|
D
|
import std.stdio, std.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 X = RD;
auto K = RD;
auto D = RD;
long ans;
if (abs(X) / D >= K)
{
ans = abs(X) - D * K;
}
else
{
auto cnt = abs(X) / D;
auto y = abs(X) - D * cnt;
auto rem = K - cnt;
if (rem % 2)
ans = abs(y - D);
else
ans = y;
}
writeln(ans);
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;
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 N = lread();
auto S = new string[](N);
auto T = new long[](N);
foreach (i; 0 .. N)
scan(S[i], T[i]);
auto X = sread();
long s;
while (S[s] != X)
s++;
long ans;
foreach (i; s + 1 .. N)
ans += T[i];
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 x, y;
{
int[] tmp = readln.chomp.split.map!(to!int).array;
x = tmp[0], y = tmp[1];
}
int ans1, ans2, ans3, ans4;
if(y >= x) ans1 = y - x; else ans1 = int.max;
if(y >= -x) ans2 = 1 + (y - -x); else ans2 = int.max;
if(-y >= x) ans3 = 1 + (-y - x); else ans3 = int.max;
if(-y >= -x) ans4 = 2 + (-y - -x); else ans4 = int.max;
int ans = min(ans1, ans2, ans3, ans4);
ans.writeln;
}
|
D
|
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std;
// dfmt on
void main()
{
long R, G, B, N;
scan(R, G, B, N);
long ans;
foreach (r; 0 .. N*2)
foreach (g; 0 .. N*2)
{
long remain = N - R * r - G * g;
if (remain < 0)
continue;
if (remain % B == 0)
ans++;
}
writeln(ans);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (ti; 0..t)
{
auto a = RD!string;
foreach (i; 0..2^^3)
{
auto arr = new int[](3);
arr[0] = i & 0b100;
arr[1] = i & 0b010;
arr[2] = i & 0b001;
long cnt;
bool ok = true;
foreach (c; a)
{
auto num = c - 'A';
if (arr[num])
++cnt;
else
--cnt;
if (cnt < 0)
{
ok = false;
break;
}
}
if (!ok) continue;
if (cnt == 0)
{
ans[ti] = true;
break;
}
}
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush;
debug readln;
}
|
D
|
// xxxxx
import std.random;
import std.stdio;
void main ()
{
rndGen.seed (unpredictableSeed);
writeln (uniform (0, 2) ? "Even" : "Odd");
}
|
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 K = RD!int;
int a, b;
for (int i = 5; i*i <= K; ++i)
{
if (K % i == 0)
{
if (K / i >= 5)
{
a = i;
b = K / i;
break;
}
}
}
if (a == 0)
writeln(-1);
else
{
auto str = "aiueo";
foreach (i; 0..a)
{
foreach (j; 0..b)
{
write(str[((i%5)+j)%5]);
}
}
writeln();
}
stdout.flush();
debug readln();
}
|
D
|
/+ dub.sdl:
name "B"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.modint;
int main() {
auto sc = new Scanner(stdin);
int n; long a, b, c, d;
sc.read(n, a, b, c, d); n--;
foreach (i; 0..n+1) {
//up i, down n-i
long ma = a + d*i - c*(n-i);
long mi = a + c*i - d*(n-i);
if (mi <= b && b <= ma) {
writeln("YES");
return 0;
}
}
writeln("NO");
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/modint.d */
// module dcomp.modint;
// import dcomp.numeric.primitive;
struct ModInt(uint MD) if (MD < int.max) {
import std.conv : to;
uint v;
this(int v) {this(long(v));}
this(long v) {this.v = (v%MD+MD)%MD;}
static auto normS(uint x) {return (x<MD)?x:x-MD;}
static auto make(uint x) {ModInt m; m.v = x; return m;}
auto opBinary(string op:"+")(ModInt r) const {return make(normS(v+r.v));}
auto opBinary(string op:"-")(ModInt r) const {return make(normS(v+MD-r.v));}
auto opBinary(string op:"*")(ModInt r) const {return make((long(v)*r.v%MD).to!uint);}
auto opBinary(string op:"/")(ModInt r) const {return this*inv(r);}
auto opOpAssign(string op)(ModInt r) {return mixin ("this=this"~op~"r");}
static ModInt inv(ModInt x) {return ModInt(extGcd!int(x.v, MD)[0]);}
string toString() {return v.to!string;}
}
struct DModInt(string name) {
import std.conv : to;
static uint MD;
uint v;
this(int v) {this(long(v));}
this(long v) {this.v = ((v%MD+MD)%MD).to!uint;}
static auto normS(uint x) {return (x<MD)?x:x-MD;}
static auto make(uint x) {DModInt m; m.MD = MD; m.v = x; return m;}
auto opBinary(string op:"+")(DModInt r) const {return make(normS(v+r.v));}
auto opBinary(string op:"-")(DModInt r) const {return make(normS(v+MD-r.v));}
auto opBinary(string op:"*")(DModInt r) const {return make((long(v)*r.v%MD).to!uint);}
auto opBinary(string op:"/")(DModInt r) const {return this*inv(r);}
auto opOpAssign(string op)(DModInt r) {return mixin ("this=this"~op~"r");}
static DModInt inv(DModInt x) {
return DModInt(extGcd!int(x.v, MD)[0]);
}
string toString() {return v.to!string;}
}
template isModInt(T) {
const isModInt =
is(T : ModInt!MD, uint MD) || is(S : DModInt!S, string s);
}
T[] factTable(T)(size_t length) if (isModInt!T) {
import std.range : take, recurrence;
import std.array : array;
return T(1).recurrence!((a, n) => a[n-1]*T(n)).take(length).array;
}
T[] invFactTable(T)(size_t length) if (isModInt!T) {
import std.algorithm : map, reduce;
import std.range : take, recurrence, iota;
import std.array : array;
auto res = new T[length];
res[$-1] = T(1) / iota(1, length).map!T.reduce!"a*b";
foreach_reverse (i, v; res[0..$-1]) {
res[i] = res[i+1] * T(i+1);
}
return res;
}
T[] invTable(T)(size_t length) if (isModInt!T) {
auto f = factTable!T(length);
auto invf = invFactTable!T(length);
auto res = new T[length];
foreach (i; 1..length) {
res[i] = invf[i] * f[i-1];
}
return res;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
static if (__VERSION__ <= 2070) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
version (X86) static if (__VERSION__ < 2071) {
import core.bitop : bsf, bsr, popcnt;
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* 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;
}
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 /home/yosupo/Program/dcomp/source/dcomp/numeric/primitive.d */
// module dcomp.numeric.primitive;
import std.traits;
import std.bigint;
T pow(T, U)(T x, U n) if (!isFloatingPoint!T && (isIntegral!U || is(U == BigInt))) {
return pow(x, n, T(1));
}
T pow(T, U)(T x, U n, T e) if (isIntegral!U || is(U == BigInt)) {
while (n) {
if (n & 1) e *= x;
x *= x;
n /= 2;
}
return e;
}
T lcm(T)(in T a, in T b) {
import std.numeric : gcd;
return a / gcd(a,b) * b;
}
T[3] extGcd(T)(in T a, in T b)
if (!isIntegral!T || isSigned!T)
{
if (b==0) {
return [T(1), T(0), a];
} else {
auto e = extGcd(b, a%b);
return [e[1], e[0]-a/b*e[1], e[2]];
}
}
|
D
|
// Vicfred
// https://atcoder.jp/contests/abc162/tasks/abc162_b
// simulation
import std.conv;
import std.stdio;
import std.string;
void main() {
int n = readln.chomp.to!int;
long sum = 0;
foreach(i; 1..n+1) {
if(i%3 != 0 && i%5 != 0)
sum += i;
}
sum.writeln;
}
|
D
|
void main()
{
long n, m, k;
rdVals(n, m, k);
foreach (i; 0 .. n+1)
{
foreach (j; 0 .. m+1)
{
long black = i * m + j * n -2 * i * j;
if (black == k)
{
"Yes".writeln;
return;
}
}
}
"No".writeln;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio;
import std.string;
import std.format;
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.concurrency;
import std.traits;
import std.uni;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;
enum long INF = long.max/3;
enum long MOD = 10L^^9+7;
void main() {
long x = readln.split.reduce!"a~b".to!long;
foreach(i; 0..x) {
if (i*i == x) {
"Yes".writeln;
return;
}
}
"No".writeln;
}
// ----------------------------------------------
void times(alias fun)(long n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(long n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
// `(x+y-1)/y` will only work for positive numbers ...
T t = x / y;
if (t * y < x) t++;
return t;
}
T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
T t = x / y;
if (t * y > x) t--;
return t;
}
ref T ch(alias fun, T, S...)(ref T lhs, S rhs) {
return lhs = fun(lhs, rhs);
}
unittest {
long x = 1000;
x.ch!min(2000);
assert(x == 1000);
x.ch!min(3, 2, 1);
assert(x == 1);
x.ch!max(100).ch!min(1000); // clamp
assert(x == 100);
x.ch!max(0).ch!min(10); // clamp
assert(x == 10);
}
mixin template Constructor() {
import std.traits : FieldNameTuple;
this(Args...)(Args args) {
// static foreach(i, v; args) {
foreach(i, v; args) {
mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v;
}
}
}
void scanln(Args...)(auto ref Args args) {
import std.meta;
template getFormat(T) {
static if (isIntegral!T) {
enum getFormat = "%d";
} else static if (isFloatingPoint!T) {
enum getFormat = "%g";
} else static if (isSomeString!T || isSomeChar!T) {
enum getFormat = "%s";
} else {
static assert(false);
}
}
enum string fmt = [staticMap!(getFormat, Args)].join(" ");
string[] inputs = readln.chomp.split;
foreach(i, ref v; args) {
v = inputs[i].to!(Args[i]);
}
}
// 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) {
private template RebindableOrUnqual(T)
{
static if (is(T == class) || is(T == interface) || isDynamicArray!T || isAssociativeArray!T)
alias RebindableOrUnqual = Rebindable!T;
else
alias RebindableOrUnqual = Unqual!T;
}
private auto extremum(alias map, alias selector = "a < b", Range)(Range r)
if (isInputRange!Range && !isInfinite!Range &&
is(typeof(unaryFun!map(ElementType!(Range).init))))
in
{
assert(!r.empty, "r is an empty range");
}
body
{
alias Element = ElementType!Range;
RebindableOrUnqual!Element seed = r.front;
r.popFront();
return extremum!(map, selector)(r, seed);
}
private auto extremum(alias map, alias selector = "a < b", Range,
RangeElementType = ElementType!Range)
(Range r, RangeElementType seedElement)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void) &&
is(typeof(unaryFun!map(ElementType!(Range).init))))
{
alias mapFun = unaryFun!map;
alias selectorFun = binaryFun!selector;
alias Element = ElementType!Range;
alias CommonElement = CommonType!(Element, RangeElementType);
RebindableOrUnqual!CommonElement extremeElement = seedElement;
// if we only have one statement in the loop, it can be optimized a lot better
static if (__traits(isSame, map, a => a))
{
// direct access via a random access range is faster
static if (isRandomAccessRange!Range)
{
foreach (const i; 0 .. r.length)
{
if (selectorFun(r[i], extremeElement))
{
extremeElement = r[i];
}
}
}
else
{
while (!r.empty)
{
if (selectorFun(r.front, extremeElement))
{
extremeElement = r.front;
}
r.popFront();
}
}
}
else
{
alias MapType = Unqual!(typeof(mapFun(CommonElement.init)));
MapType extremeElementMapped = mapFun(extremeElement);
// direct access via a random access range is faster
static if (isRandomAccessRange!Range)
{
foreach (const i; 0 .. r.length)
{
MapType mapElement = mapFun(r[i]);
if (selectorFun(mapElement, extremeElementMapped))
{
extremeElement = r[i];
extremeElementMapped = mapElement;
}
}
}
else
{
while (!r.empty)
{
MapType mapElement = mapFun(r.front);
if (selectorFun(mapElement, extremeElementMapped))
{
extremeElement = r.front;
extremeElementMapped = mapElement;
}
r.popFront();
}
}
}
return extremeElement;
}
private auto extremum(alias selector = "a < b", Range)(Range r)
if (isInputRange!Range && !isInfinite!Range &&
!is(typeof(unaryFun!selector(ElementType!(Range).init))))
{
return extremum!(a => a, selector)(r);
}
// if we only have one statement in the loop it can be optimized a lot better
private auto extremum(alias selector = "a < b", Range,
RangeElementType = ElementType!Range)
(Range r, RangeElementType seedElement)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void) &&
!is(typeof(unaryFun!selector(ElementType!(Range).init))))
{
return extremum!(a => a, selector)(r, seedElement);
}
auto minElement(alias map = (a => a), Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum!map(r);
}
auto minElement(alias map = (a => a), Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!map(r, seed);
}
auto maxElement(alias map = (a => a), Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum!(map, "a > b")(r);
}
auto maxElement(alias map = (a => a), Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!(map, "a > b")(r, seed);
}
}
// popcnt with ulongs was added in D 2.071.0
static if (__VERSION__ < 2071) {
ulong popcnt(ulong x) {
x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L);
x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L);
x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL);
x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL);
x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL);
x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL);
return x;
}
}
|
D
|
import std.stdio;
int[][int] graph;
bool[int] used;
void dependent(int n) {
if (!used[n]) {
foreach(i; graph[n]) {
dependent(i);
}
writeln(n);
used[n] = true;
}
}
void main() {
int m, n;
scanf("%d\n%d\n", &m, &n);
foreach(i; 1..(m + 1)) {graph[i] = []; used[i] = false;}
foreach(i; 0..n) {
int to, from;
scanf("%d %d", &from, &to);
graph[to] ~= from;
}
foreach(i; 1..m) dependent(i);
}
|
D
|
import std.stdio, std.string, std.conv;
void main() {
int[] tmp = readln.split.to!(int[]);
int x = tmp[0], a = tmp[1];
writeln(x < a ? 0 : 10);
}
|
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 = 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 main()
{
auto n = RD!int;
auto s = RD!string;
long cnt;
bool ans = true;
foreach (i; 0..n)
{
if (s[i] == '(')
++cnt;
else
--cnt;
if (cnt < -1)
{
ans = false;
break;
}
}
writeln(ans && cnt == 0 ? "Yes" : "No");
stdout.flush();
debug readln();
}
|
D
|
import std.stdio;
void main(){
auto s=readln();
char x=s[0];
char y=s[2];
if(x<y) writeln("<");
else if(x>y) writeln(">");
else writeln("=");
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.container;
import std.array;
import std.math;
import std.range;
void main()
{
int m = readln.chomp.split.back.to!int;
string s = readln.chomp;
int l = 0, r = 1;
// bool[string] set;
bool[ulong[2]] set;
auto hasher = new RollingHash(s);
foreach (i; 0 .. m)
{
string v = readln;
auto f = function(char c, ref int w) {
if (c == '+')
{
++w;
}
else
{
--w;
}
};
if (v[0] == 'L')
{
f(v[1], l);
}
else
{
f(v[1], r);
}
set[hasher.substr(l, r)] = true;
// set[s[l..r]] = true;
}
writeln = set.length;
}
class RollingHash
{
enum ulong MOD1 = 1_000_000_007;
enum ulong MOD2 = 1_000_000_009;
enum ulong B1 = 1_009;
enum ulong B2 = 1_007;
ulong[] b1s, b2s;
ulong[] array1;
ulong[] array2;
this(string s)
{
array1 ~= 0;
array2 ~= 0;
b1s ~= 1;
b2s ~= 1;
foreach (c; s)
{
array1 ~= ((array1.back + ulong(c)) * B1) % MOD1;
array2 ~= ((array2.back + ulong(c)) * B2) % MOD2;
b1s ~= (b1s.back * B1) % MOD1;
b2s ~= (b2s.back * B2) % MOD2;
}
}
ulong[2] substr(uint l, uint r)
{
ulong h1 = (array1[r] - ((b1s[r - l] * array1[l]) % MOD1) + MOD1) % MOD1;
ulong h2 = (array2[r] - ((b2s[r - l] * array2[l]) % MOD2) + MOD2) % MOD2;
return [h1, h2];
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void main()
{
auto H = RD;
long ans, cnt = 1;
while (H != 0)
{
ans += cnt;
H /= 2;
cnt *= 2;
}
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
import std.algorithm;
import std.array;
import std.bigint;
import std.bitmanip;
import std.conv;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; }
T[] readToArray(T)() {
return readln.split.to!(T[]);
}
void readInto(T...)(ref T ts) {
auto ss = readln.split;
foreach(ref t; ts) {
t = ss.front.to!(typeof(t));
ss.popFront;
}
}
// 冪乗をmod取りつつ計算
@nogc @safe pure ulong modPow(ulong a, ulong n, ulong m) {
ulong r = 1;
while (n > 0) {
if(n % 2 != 0) r = r * a % m;
a = a * a % m;
n /= 2;
}
return r;
}
// フェルマーの小定理から乗法逆元を計算
// 定理の要請により法は素数
@nogc @safe pure ulong modInv(ulong a, ulong m) {
return modPow(a, m-2, m);
}
// mod取りつつ順列を計算
@nogc @safe pure ulong modPerm(ulong n, ulong k, ulong m) {
if (n < k) return 0;
ulong r = 1;
for (ulong i = n-k+1; i <= n; i++) {
r *= i;
r %= m;
}
return r;
}
// mod取りつつ順列を計算
@nogc @safe pure ulong modFact(ulong n, ulong m) {
return modPerm(n, n, m);
}
// mod取りつつ組み合わせを計算
// modInvを使っているので法は素数
@nogc @safe pure ulong modComb(ulong n, ulong r, ulong m) {
return modPerm(n, r, m)*modInv(modFact(r, m), m) % m;
}
immutable ulong MOD = 1000000007;
void main() {
ulong n, a, b;
readInto(n, a, b);
writeln(n*a < b ? n*a : b);
}
|
D
|
import std.stdio, std.conv, std.string, std.bigint;
import std.math, std.random, std.datetime;
import std.array, std.range, std.algorithm, std.container, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
/*
有向グラフ
トポロジカルソートみたいなもの
※トポロジカルソートみたいなものは先日の日経予選にもあり
ソース、シンクを考えると、題意のパスはどれかのソースからどれかのシンクに至っている
入次数を調べることでソースたちを見つける
他の点にもすべて入次数を持たせておく
ソースから幅優先探索
ソースの値は0とする
自分の子たちに自分の値+1をつける
ただしすでに値がある場合は、改善される場合だけ上書き
いずれにしてもこれらをされた子は入次数を減らす
入次数が残り0になった子はその子から続きの探索をする
*/
class Node{
Node[] kids;
int parentcount;
int value;
int id;
this(int id){
this.id = id;
}
}
void main(){
int n = read.to!int;
int m = read.to!int;
Node[] nodes;
foreach(i; 0 .. n) nodes ~= new Node(i);
foreach(j; 0 .. m){
int x = read.to!int - 1;
int y = read.to!int - 1;
nodes[x].kids ~= nodes[y];
nodes[y].parentcount += 1;
}
Node[] queue;
int iq;
foreach(nd; nodes) if(nd.parentcount == 0){
nd.value = 0;
queue ~= nd;
}
while(iq < queue.length){
Node nd = queue[iq];
foreach(kid; nd.kids){
kid.value = nd.value + 1;
kid.parentcount -= 1;
if(kid.parentcount == 0) queue ~= kid;
}
iq ++;
}
int ans = 0;
foreach(nd; nodes) if(ans < nd.value) ans = nd.value;
ans.writeln;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
enum MOD = (10 ^^ 9) + 7;
void main()
{
string s = sread();
long[2] cnt;
foreach (c; s)
{
cnt[c / '1']++;
}
writeln(min(cnt[0], cnt[1]) * 2);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.array, std.algorithm, std.range;
void main()
{
int[string] c;
string mw; c[mw]=0;
string ml;
foreach(w;readln().split())
{
if(w in c){ if(++c[w]>c[mw]) mw=w; }
else c[w]=1;
if(ml.length < w.length) ml=w;
}
writeln(mw," ",ml);
}
|
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);
auto a = new char[][][](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto k = RD!int;
a[ti] = new char[][](n, n);
ans[ti] = k % n == 0 ? 0 : 2;
foreach (i; 0..n)
{
foreach (j; 0..n)
a[ti][i][j] = '0';
}
(){
foreach (i; 0..n)
{
foreach (j; 0..n)
{
if (k == 0) return;
a[ti][j][(i+j)%n] = '1';
--k;
}
}}();
}
foreach (ti, e; ans)
{
writeln(e);
foreach (ee; a[ti])
writeln(ee);
}
stdout.flush;
debug readln;
}
|
D
|
import std.stdio;
import std.string;
import std.algorithm;
import std.conv;
void main() {
int N = readln.chomp.to!int;
auto A = readln.chomp.split.map!(to!int);
int ans = (1e9).to!int;
foreach(a; A) {
int tmp;
while (a % 2 == 0 && a > 0) {
tmp++;
a /= 2;
}
ans = min(ans, tmp);
}
ans.writeln;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
void main() {
int n;
scan(n);
writeln(n & 1 ? n * 2 : n);
}
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, 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() {
long n;
long s;
scan(n);
scan(s);
for (long b = 2; b*b <= n; b++) {
if (dsum(b, n) == s) {
writeln(b);
return;
}
}
long ans = inf6;
for (long p = 1; p*p < n; p++) {
auto q = s - p;
if ((n - q) % p == 0) {
long b = (n - q) / p;
if (b > 1 && dsum(b, n) == s) {
ans = min(ans, b);
}
}
}
if (ans < inf6) {
writeln(ans);
return;
}
if (n == s) {
writeln(n + 1);
return;
}
writeln(-1);
}
long dsum(long b, long n) {
return n < b ? n : dsum(b, n / b) + (n % b);
}
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.algorithm;
import std.range;
import std.string;
import std.math;
import std.format;
void main() {
foreach (string line; stdin.lines) {
int n = line.chomp.to!int;
int cnt = 0;
for (int a = 0; a <= 9; a++) {
for (int b = 0; b <= 9; b++) {
for (int c = 0; c <= 9; c++) {
for (int d = 0; d <= 9; d++) {
if (a + b + c + d == n) {
cnt++;
}
}
}
}
}
cnt.writeln;
}
}
|
D
|
void main()
{
string s = readln.chomp;
long cnt;
long times;
foreach (i, c; s)
{
if (c == 'W')
{
times += i - cnt;
++cnt;
}
}
times.writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
// import chie template :) {{{
static if (__VERSION__ < 2090) {
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons, std.format,
std.bitmanip, std.numeric;
} else {
import std;
}
// }}}
// nep.scanner {{{
class Scanner {
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) {
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next() {
if (idx < str.length) {
return str[idx++];
}
char[] s;
while (s.length == 0) {
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(size_t len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
void scan(T...)(ref T args) {
foreach (ref arg; args) {
arg = next!(typeof(arg));
}
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType)) {
str ~= s.to!(char[]).strip.split;
}
}
// }}}
// ModInt {{{
struct ModInt(ulong modulus) {
import std.traits : isIntegral, isBoolean;
import std.exception : enforce;
private {
ulong val;
}
this(const ulong n) {
val = n % modulus;
}
this(const ModInt n) {
val = n.value;
}
@property {
ref inout(ulong) value() inout {
return val;
}
}
T opCast(T)() {
static if (isIntegral!T) {
return cast(T)(val);
} else if (isBoolean!T) {
return val != 0;
} else {
enforce(false, "cannot cast from " ~ this.stringof ~ " to " ~ T.stringof ~ ".");
}
}
ModInt opAssign(const ulong n) {
val = n % modulus;
return this;
}
ModInt opOpAssign(string op)(ModInt rhs) {
static if (op == "+") {
val += rhs.value;
if (val >= modulus) {
val -= modulus;
}
} else if (op == "-") {
if (val < rhs.value) {
val += modulus;
}
val -= rhs.value;
} else if (op == "*") {
val = val * rhs.value % modulus;
} else if (op == "/") {
this *= rhs.inv;
} else if (op == "^^") {
ModInt res = 1;
ModInt t = this;
while (rhs.value > 0) {
if (rhs.value % 2 != 0) {
res *= t;
}
t *= t;
rhs /= 2;
}
this = res;
} else {
enforce(false, op ~ "= is not implemented.");
}
return this;
}
ModInt opOpAssign(string op)(ulong rhs) {
static if (op == "+") {
val += ModInt(rhs).value;
if (val >= modulus) {
val -= modulus;
}
} else if (op == "-") {
auto r = ModInt(rhs);
if (val < r.value) {
val += modulus;
}
val -= r.value;
} else if (op == "*") {
val = val * ModInt(rhs).value % modulus;
} else if (op == "/") {
this *= ModInt(rhs).inv;
} else if (op == "^^") {
ModInt res = 1;
ModInt t = this;
while (rhs > 0) {
if (rhs % 2 != 0) {
res *= t;
}
t *= t;
rhs /= 2;
}
this = res;
} else {
enforce(false, op ~ "= is not implemented.");
}
return this;
}
ModInt opUnary(string op)() {
static if (op == "++") {
this += 1;
} else if (op == "--") {
this -= 1;
} else {
enforce(false, op ~ " is not implemented.");
}
return this;
}
ModInt opBinary(string op)(const ulong rhs) const {
mixin("return ModInt(this) " ~ op ~ "= rhs;");
}
ModInt opBinary(string op)(ref const ModInt rhs) const {
mixin("return ModInt(this) " ~ op ~ "= rhs;");
}
ModInt opBinary(string op)(const ModInt rhs) const {
mixin("return ModInt(this) " ~ op ~ "= rhs;");
}
ModInt opBinaryRight(string op)(const ulong lhs) const {
mixin("return ModInt(this) " ~ op ~ "= lhs;");
}
long opCmp(ref const ModInt rhs) const {
return cast(long)value - cast(long)rhs.value;
}
bool opEquals(const ulong rhs) const {
return value == ModInt(rhs).value;
}
ModInt inv() const {
ModInt ret = this;
ret ^^= modulus - 2;
return ret;
}
string toString() const {
import std.format : format;
return format("%s", val);
}
}
// }}}
// alias {{{
alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less);
alias MinHeap(T) = Heap!(T, "a > b");
// }}}
// memo {{{
/*
- ある値が見つかるかどうか
<https://dlang.org/phobos/std_algorithm_searching.html#canFind>
canFind(r, value); -> bool
- 条件に一致するやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#filter>
// 2で割り切れるやつ
filter!"a % 2 == 0"(r); -> Range
- 合計
<https://dlang.org/phobos/std_algorithm_iteration.html#sum>
sum(r);
- 累積和
<https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold>
// 今の要素に前の要素を足すタイプの一般的な累積和
// 累積和のrangeが帰ってくる(破壊的変更は行われない)
cumulativeFold!"a + b"(r, 0); -> Range
- rangeをarrayにしたいとき
array(r); -> Array
- 各要素に同じ処理をする
<https://dlang.org/phobos/std_algorithm_iteration.html#map>
// 各要素を2乗
map!"a * a"(r) -> Range
- ユニークなやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#uniq>
uniq(r) -> Range
- 順列を列挙する
<https://dlang.org/phobos/std_algorithm_iteration.html#permutations>
permutation(r) -> Range
- ある値で埋める
<https://dlang.org/phobos/std_algorithm_mutation.html#fill>
fill(r, val); -> void
- バイナリヒープ
<https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap>
// 昇順にするならこう(デフォは降順)
BinaryHeap!(Array!T, "a > b") heap;
heap.insert(val);
heap.front;
heap.removeFront();
- 浮動小数点の少数部の桁数設定
// 12桁
writefln("%.12f", val);
- 浮動小数点の誤差を考慮した比較
<https://dlang.org/phobos/std_math.html#.approxEqual>
approxEqual(1.0, 1.0099); -> true
- 小数点切り上げ
<https://dlang.org/phobos/std_math.html#.ceil>
ceil(123.4); -> 124
- 小数点切り捨て
<https://dlang.org/phobos/std_math.html#.floor>
floor(123.4) -> 123
- 小数点四捨五入
<https://dlang.org/phobos/std_math.html#.round>
round(4.5) -> 5
round(5.4) -> 5
*/
// }}}
void main() {
auto cin = new Scanner;
int s, w;
cin.scan(s, w);
if (w >= s) {
writeln("unsafe");
} else {
writeln("safe");
}
}
|
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()
{
long N = lread();
char[] S = sread().dup;
long Q = lread();
auto bit = new BinaryIndexedTree[]('z' - 'a' + 1);
foreach (i; 0 .. bit.length)
bit[i] = BinaryIndexedTree(N);
foreach (i; 0 .. N)
{
bit[S[i] - 'a'].add(i + 1, 1);
}
outer: foreach (_; 0 .. Q)
{
auto input = readln.split();
long a = input[1].to!long();
if (input[0] == "1")
{
char b = input[2][0];
if (S[a - 1] == b)
continue outer;
bit[S[a - 1] - 'a'].add(a, -1);
bit[b - 'a'].add(a, 1);
S[a - 1] = cast(char) b;
}
if (input[0] == "2")
{
long b = input[2].to!long();
long ans;
foreach (c; 0 .. bit.length)
ans += (bit[c].sum(b) - bit[c].sum(a - 1) != 0);
writeln(ans);
}
}
}
/// BIT (1-indexed)
alias BinaryIndexedTree = BinaryIndexedTreeImpl!long;
struct BinaryIndexedTreeImpl(T)
{
T[] _data;
this(long n)
{
_data = new T[](n + 1);
}
T sum(long i)
{
T ans;
for (; 0 < i; i -= (i & -i))
ans += _data[i];
return ans;
}
void add(long i, T x)
{
for (; i < _data.length; i += (i & -i))
_data[i] += x;
}
}
|
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;
char[] str;
void main() {
int N = readln.chomp.to!int;
str = readln.chomp.to!(char[]);
int cnt = 0;
foreach(i; 0..N) {
char[] s = readln.chomp.to!(char[]);
if (rec(0, 0, s, [])) cnt++;
}
cnt.writeln;
}
bool rec(int depth, int index, char[] s, int[] ary) {
if (ary.length == str.length) {
return true;
} else if (depth<s.length){
if (s[depth] == str[index]) {
if (ary.length<2 || depth-ary[$-1]==ary[$-1]-ary[$-2]) {
int[] _ary = ary.dup;
if (rec(depth+1, index+1, s, _ary~depth)) return true;
}
}
if (rec(depth+1, index, s, ary)) return true;
}
return false;
}
|
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 s = readln.chomp;
auto k = "CODEFESTIVAL2016";
int cnt;
foreach (i; 0..s.length) {
if (s[i] != k[i]) {
cnt++;
}
}
cnt.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;
import std.numeric;
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;
}
}
}
int dist (int x, int k) {
x = (x - 1) % k;
return min (x, k - x);
}
void main() {
auto r = new InputReader;
immutable n = r.next!uint;
immutable k = r.next!uint;
immutable a = r.next!uint;
immutable b = r.next!uint;
long rmin = long.max, rmax = long.min;
ulong nk = n.to!ulong * k;
foreach (s; 1 .. k + 1) {
if (dist (s, k) != a) {
continue;
}
debug stderr.writeln ("s = ", s);
foreach (l; 1 .. k + 1) {
immutable sl = l + s;
if (dist (sl, k) != b) {
continue;
}
debug stderr.writeln ("l = ", l);
ulong m = l;
while (m <= nk) {
long z = nk / gcd (nk, m);
rmin = min (rmin, z);
rmax = max (rmax, z);
m += k;
}
}
}
writeln (rmin, ' ', rmax);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto ab = readln.split.to!(int[]);
auto A = ab[0];
auto B = ab[1];
int r;
foreach (_; 0..2) {
if (A > B) {
r += A;
--A;
} else {
r += B;
--B;
}
}
writeln(r);
}
|
D
|
void main()
{
string s = rdStr;
bool ok = true;
foreach (i, x; s)
{
if (i & 1)
{
if (x != 'i') ok = false;
}
else
{
if (x != 'h') ok = false;
}
}
if (s.length & 1) ok = false;
writeln(ok ? "Yes" : "No");
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.conv, std.stdio, std.algorithm, std.string, std.range;
void main() {
const N = readln.chomp.to!int;
foreach (x; 1..10) foreach (y; x..10) {
if (x*y == N) {
"Yes".writeln; return;
}
}
"No".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 n; readV(n);
writeln(n/3);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
auto A = readln.chomp.to!(wchar[]);
auto B = readln.chomp.to!(wchar[]);
auto C = readln.chomp.to!(wchar[]);
int r;
foreach (i; 0..N) {
auto a = A[i];
auto b = B[i];
auto c = C[i];
if (a == b && b == c) {
} else if (a == b || b == c || c == a) {
++r;
} else {
r += 2;
}
}
writeln(r);
}
|
D
|
void main(){
int n = _scan(); // 操作を行う回数
int k = _scan(); // 足す値
int ans = 1;
foreach(elm; 0..n){
if(ans*2 < ans+k)ans *=2;
else ans +=k;
}
writeln(ans);
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.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;
void times(alias pred)(int n) {
foreach(i; 0..n) pred();
}
auto rep(alias pred, T = typeof(pred()))(int n) {
T[] res = new T[n];
foreach(ref e; res) e = pred();
return res;
}
void main() {
int N = readln.chomp.to!int;
long[] ary = new long[2^^(N+1)];
foreach(i; 0..2^^N) {
ary[2^^N-1+i] = readln.chomp.to!long;
}
for(int i=N-1; i>=0; i--) {
foreach(j; 2^^i-1..2^^(i+1)-1) {
ary[j] = f(ary[j*2+1], ary[j*2+2]);
}
}
ary.front.writeln;
}
long f(long x, long y) {
if (x==y) return x;
return abs(x-y);
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.7.4"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.geo.primitive;
bool solve() {
alias P = Point2D!double;
P[] pol = new P[3];
P p;
foreach (i; 0..3) {
double x, y;
if (sc.read(x, y) != 2) return false;
pol[i] = P(x, y);
}
double x, y;
sc.read(x, y);
p = P(x, y);
if (contains(pol, p) == 2) {
writeln("YES");
} else {
writeln("NO");
}
return true;
}
int main() {
EPS!double = 1e-10;
while (solve()) {}
return 0;
}
Scanner sc;
static this() {
sc = new Scanner(stdin);
}
/* IMPORT /Users/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 /Users/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;
}
}
/* IMPORT /Users/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 /Users/yosupo/Program/dcomp/source/dcomp/geo/primitive.d */
// module dcomp.geo.primitive;
import std.traits;
template EPS(R) {
R EPS;
}
int sgn(R)(R a) {
if (a < -EPS!R) return -1;
if (a > EPS!R) return 1;
return 0;
}
struct Point2D(T) {
T[2] d;
this(T x, T y) {this.d = [x, y];}
this(T[2] d) {this.d = d;}
@property ref inout(T) x() inout {return d[0];}
@property ref inout(T) y() inout {return d[1];}
ref inout(T) opIndex(size_t i) inout {return d[i];}
auto opBinary(string op:"+")(Point2D r) const {return Point2D(x+r.x, y+r.y);}
auto opBinary(string op:"-")(Point2D r) const {return Point2D(x-r.x, y-r.y);}
static if (isFloatingPoint!T) {
T abs() {
import std.math : sqrt;
return (x*x+y*y).sqrt;
}
T arg() {
import std.math : atan2;
return atan2(y, x);
}
}
}
bool near(T)(Point2D!T a, Point2D!T b) {
return !sgn((a-b).abs);
}
T dot(T)(in Point2D!T l, in Point2D!T r) {
return l[0]*l[0] + l[1]*r[1];
}
T cross(T)(in Point2D!T l, in Point2D!T r) {
return l[0]*r[1] - l[1]*r[0];
}
int ccw(R)(Point2D!R a, Point2D!R b, Point2D!R c) {
assert(!near(a, b));
if (near(a, c) || near(b, c)) return 0;
int s = sgn(cross(b-a, c-a));
if (s) return s;
if (dot(b-a, c-a) < 0) return 2;
if (dot(a-b, c-b) < 0) return -2;
return 0;
}
int contains(R)(Point2D!R[] pol, Point2D!R p) {
import std.algorithm : swap;
int res = -1;
foreach (i; 0..pol.length) {
auto a = pol[i] - p, b = pol[(i+1)<pol.length?i+1:0] - p;
if (ccw(a, b, Point2D!R(0, 0)) == 0) return 1;
if (a.y > b.y) swap(a, b);
if (a.y <= 0 && 0 < b.y) {
if (cross(a, b) < 0) res *= -1;
}
}
return res+1;
}
int argcmp(T)(Point2D!T l, Point2D!T r) if (isIntegral!T) {
int sgn(Point2D!T p) {
if (p[1] < 0) return -1;
if (p[1] > 0) return 1;
if (p[0] < 0) return 2;
return 0;
}
int lsgn = sgn(l);
int rsgn = sgn(r);
if (lsgn < rsgn) return -1;
if (lsgn > rsgn) return 1;
T x = cross(l, r);
if (x > 0) return -1;
if (x < 0) return 1;
return 0;
}
/*
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.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 args = readln.chomp.split.map!(to!int);
int a = args[0];
int b = args[1];
int c = args[2];
int x = args[3];
int y = args[4];
int z = x + y;
int ans = int.max;
for (int i = 0; i <= z * 2; ++i) {
int m = i / 2;
int rx = x - m;
int ry = y - m;
int v = i * c + rx.max(0) * a + ry.max(0) * b;
ans = min(ans, v);
}
ans.writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
void main() {
auto n = readln.chomp;
int sum;
for (int i = 0; i < n.length; ++i) {
sum += (n[i] - '0');
}
if (n.to!int % sum == 0) {
"Yes".writeln;
}
else {
"No".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 n, m, nq; readV(n, m, nq);
auto t = new int[][](n+2, n+2);
foreach (_; 0..m) {
int l, r; readV(l, r);
++t[1][r];
--t[l+1][r];
--t[1][n+1];
++t[l+1][n+1];
}
foreach (j; 0..n+2)
foreach (i; 0..n+1)
t[i+1][j] += t[i][j];
foreach (i; 0..n+2)
foreach (j; 0..n+1)
t[i][j+1] += t[i][j];
foreach (_; 0..nq) {
int p, q; readV(p, q);
writeln(t[p][q]);
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto A = RD;
auto B = RD;
long ans = -1;
foreach (i; 1..10^^5)
{
if (cast(long)(i*0.08) == A && cast(long)(i*0.1) == B)
{
ans = i;
break;
}
}
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
auto c = new string[](2);
c[0] = readln.chomp;
c[1] = readln.chomp;
writeln(equal(c[0], c[1].retro) ? "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
|
void main() {
int[] tmp = readln.split.to!(int[]);
int a = tmp[0], b = tmp[1];
writeln(a < 9 && b < 9 ? "Yay!" : ":(");
}
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.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void main()
{
int a, b, x; readV(a, b, x);
writeln(a <= x && x <= a+b ? "YES" : "NO");
}
|
D
|
import core.stdc.stdio;
import std.container;
import std.algorithm;
import std.typecons;
import std.stdio;
void main(){
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
alias Tuple!(int,"cost",int,"to") ct;
ct[][] graph = new ct[][n];
alias Tuple!(int,"a",int,"b",int,"l") edge;
edge[] es = new edge[m];
foreach(ref e;es){
scanf("%d%d%d",&e.a,&e.b,&e.l);
graph[--e.a]~=ct(e.l,--e.b);
graph[e.b]~=ct(e.l,e.a);
}
auto pq = BinaryHeap!(ct[],"a>b")(new ct[n+m],0);
int[] nc = new int[n];
nc[] = 1145141919;
foreach(i;0..k){
int a;
scanf("%d",&a);
nc[--a]=0;
pq.insert(ct(0,a));
}
while(!pq.empty){
auto t=pq.front;
pq.popFront;
with(t){
if(cost>nc[to])
continue;
foreach(c;graph[to]){
if(nc[c.to]>cost+c.cost){
nc[c.to]=cost+c.cost;
pq.insert(ct(cost+c.cost,c.to));
}
}
}
}
int ans;
foreach(e;es)
ans = max(ans,e.l+nc[e.a]+nc[e.b]);
printf("%d\n",(ans+1)/2);
}
|
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;
immutable int INF = 1 << 29;
void main() {
auto N = readln.chomp.to!int;
auto A = readln.split.map!(to!long).array;
long m = 0;
foreach (i; 0..N) {
if (A[i] > m) {
writeln(i + 1);
return;
}
if (A[i] == m) {
m += 1;
}
}
writeln(-1);
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Node = Tuple!(long, "p", long, "dist");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
auto n = lread();
long lim = 101; // 入力の制限より大きい最小の素数
auto ary = new long[](lim);
foreach (a; iota(1, n))
{
auto p_factor = prime_factorization(a + 1, lim);
ary[] += p_factor[];
}
ary[] += 1;
// ary.take(n).writeln();
long gt_5, gt_3, gt_15, gt_25, gt_75;
foreach (e; ary)
{
if (e >= 3)
gt_3++;
if (e >= 5)
gt_5++;
if (e >= 15)
gt_15++;
if (e >= 25)
gt_25++;
if (e >= 75)
gt_75++;
}
long ans;
ans += (gt_5 * (gt_5 - 1) / 2) * (gt_3 - 2);
ans += (gt_15) * (gt_5 - 1);
ans += (gt_25) * (gt_3 - 1);
ans += (gt_75);
ans.writeln();
}
auto prime_factorization(long a, long lim)
{
auto p_factor = new long[](lim);
foreach (i; iota(1, a))
{
while (a % (i + 1) == 0)
{
a /= (i + 1);
p_factor[i]++;
}
if (a == 0)
break;
}
return p_factor;
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.stdio, std.conv, std.string, std.math, std.regex, std.range, std.ascii, std.algorithm;
void main(){
auto ip = readln.chomp;
switch(ip){
case "A":
writeln("T");
break;
case "T":
writeln("A");
break;
case "C":
writeln("G");
break;
case "G":
writeln("C");
break;
default:
}
}
|
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();
long bignum = 1_000_000_007;
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
auto c = new long[](0);
foreach (_; 0 .. 3)
{
auto tmp = aryread();
c ~= tmp;
}
bool check = true;
long b1, b2, b3, a2, a3;
b1 = c[0];
b2 = c[1];
b3 = c[2];
if(!((c[3] - b1) == (c[4] - b2) && (c[3] - b1) == (c[5] - b3)))
check = false;
if(!((c[6] - b1) == (c[7] - b2) && (c[6] - b1) == (c[8] - b3)))
check = false;
if(check)
writeln("Yes");
else
writeln("No");
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!long;
writeln(N * (N-1) / 2);
}
|
D
|
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container;
import std.numeric, std.math;
import core.bitop;
string my_readln() { return chomp(readln()); }
long mod = pow(10, 9) + 7;
long moda(long x, long y) { return (x + y) % mod; }
long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; }
long modm(long x, long y) { return (x * y) % mod; }
void main()
{
auto N = my_readln.to!long;
ulong[32] d;
foreach (i; 0..32)
{
auto x = N % 2;
//stderr.writeln(N, " ", x);
N /= 2;
long sign;
if (i % 2 == 0) sign = 1;
else sign = -1;
if (sign * sgn(x) >= 0)
{
d[i] += abs(x);
}
else
{
++d[i+1];
++d[i];
}
while (d[i] > 1)
{
++d[i+2];
++d[i+1];
d[i] -= 2;
}
}
string ans;
bool hasStart = false;
foreach_reverse (e; d)
{
if (hasStart)
{
ans ~= e.to!string;
}
else if (e == 1)
{
ans ~= e.to!string;
hasStart = true;
}
}
if (!hasStart) ans ~= "0";
writeln(ans);
stdout.flush();
}
|
D
|
import std.stdio;
import std.algorithm;
import std.array;
import std.conv;
void main(){
string[] input = readln().split();
int a = input[0].to!int(), b = input[1].to!int();
while(b != 0){
a = a % b;
swap(a, b);
}
a.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.string;
void solve() {
auto s = readln.split.map!(to!long);
auto K = s[0];
auto X = s[1];
long ans;
if (X == 9) {
ans = 9 * K;
} else {
ans = 9 * (K - 1) + X;
}
ans.writeln;
}
void main() {
auto T = readln.chomp.to!int;
while (T--) solve;
}
|
D
|
import std.stdio;
import std.algorithm;
import std.array;
import std.conv;
import std.datetime;
import std.numeric;
import std.math;
import std.string;
string my_readln() { return chomp(readln()); }
void main()
{
auto tokens = my_readln().split();
auto H = tokens[0].to!ulong;
auto W = tokens[1].to!ulong;
string[] a;
foreach (i; 0..H)
{
a ~= my_readln();
}
long[1010][1010] memo;
foreach (ref e; memo) foreach (ref ee; e) ee = -1;
ulong mod = pow(10, 9) + 7;
ulong search(long i, long j)
{
if (i == W || j == H) return 0;
if (memo[i][j] != -1) return memo[i][j];
if (a[j][i] == '#') return 0;
if (i == W - 1 && j == H - 1) return 1;
ulong r;
r = (r + search(i+1, j)) % mod;
r = (r + search(i, j+1)) % mod;
//stderr.writeln(i, " ", j, " ", r);
memo[i][j] = r;
return r;
}
ulong ans = search(0, 0);
writeln(ans);
stdout.flush();
}
|
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; }
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 X = RD;
auto remain = X % 100;
auto cnt = X / 100;
writeln(cnt*5 >= remain ? 1 : 0);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.container;
import std.array;
import std.math;
import std.range;
import std.typecons;
import std.ascii;
import std.format;
void main()
{
auto a = readln.chomp.split.map!(to!int);
auto h = a[0];
auto w = a[1];
char[][] map = new char[][](h + 2, w + 2);
foreach (ref arr; map) {
arr[] = '.';
}
foreach (i; 0..h) {
auto s = readln;
foreach (j; 0..w) {
map[i + 1][j + 1] = s[j];
}
}
for (int i = 1; i <= h; ++i) {
for (int j = 1; j <= w; ++j) {
if (map[i][j] == '#') {
write = '#';
continue;
}
int cnt = 0;
for (int dx = -1; dx <= 1; ++dx) {
for (int dy = -1; dy <= 1; ++dy) {
cnt += map[i + dy][j + dx] == '#';
}
}
cnt.write;
}
writeln;
}
}
|
D
|
import std.stdio, std.string, std.conv, std.math;
void main() {
int n = readln.chomp.to!int;
long debt = 100000;
foreach(i;0..n) {
debt += debt * 0.05;
debt = (((debt / 1000.0).ceil) * 1000).to!long;
}
debt.writeln;
}
|
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 k = readln.chomp.to!long;
writeln((k/2) * (k - (k/2)));
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
static import std.ascii;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N, A, B;
scan(N, A, B);
if (N == 1)
{
writeln(A == B ? 1 : 0);
return;
}
if (B < A)
{
writeln(0);
return;
}
writeln((B * (N - 1) + A) - (A * (N - 1) + B) + 1);
}
|
D
|
void main() {
writeln(readln.chomp.count('9') ? "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 core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "x", long, "y");
alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less);
void main()
{
auto s = sread();
// s[0 .. ($ - 1) / 2].writeln();
// s[($ + 1) / 2 .. $].writeln();
if (s.isreverse() && s[0 .. ($ - 1) / 2].isreverse() && s[($ + 1) / 2 .. $].isreverse())
writeln("Yes");
else
writeln("No");
}
bool isreverse(string t)
{
bool ret = true;
foreach (i; iota(t.length / 2))
{
ret &= (t[i] == t[$ - 1 - i]);
}
return ret;
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.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 n = read.to!int;
int[] as;
foreach(i; 0 .. n) as ~= read.to!int;
int f = 0;
int last = -10;
foreach(a; as){
if(last < a){
if(f == 1){
"NO".writeln;
return;
}
}
if(last > a){
if(f == 0) f = 1;
}
last = a;
}
"YES".writeln;
return;
}
|
D
|
import std.stdio, std.array, std.algorithm, std.string, std.conv, std.math;
void main() {
int n = readln.chomp.to!int;
int[int] chie;
auto b = map!(x => x.to!int)(readln.chomp.split);
int res = 0;
foreach (x; b) {
++chie[x];
}
foreach (x, cnt; chie) {
if (cnt >= x) {
res += min(cnt - x, cnt);
} else {
res += cnt;
}
}
writeln(res);
}
|
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 n = read.to!int;
int k = read.to!int;
if(n % 2 == k % 2) ((n + k) / 2).writeln;
else "IMPOSSIBLE".writeln;
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "a", long, "b");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
auto s = sread();
s[0 .. $ - "FESTIVAL".length].writeln();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.