code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.stdio, std.algorithm, std.array, std.conv, std.string;
bool[3000] leap_table;
void init_leap_table() {
foreach(i, ref x; leap_table) {
if (((i % 4) == 0) && (((i % 100) != 0) || ((i % 400) == 0))) x = true;
}
}
void main() {
init_leap_table;
bool first = true;
while(true) {
int[] date = readln.chomp.split(" ").map!(to!(int)).array;
bool ans = false;
if (date[0] == 0 && date[1] == 0) break;
if (!first) writeln;
foreach(d; date[0]..(date[1] + 1))
if (leap_table[d]) {writeln(d); ans = true;}
if (!ans) writeln("NA");
first = false;
}
}
|
D
|
void main() {
auto N = rs;
auto K = ri;
while(N.length < 100) N = '0' ~ N;
auto S = '0'.repeat(100).array;
ulong res;
foreach(i; 0..100) {
for(S[i] = '1'; S[i] <= '9'; S[i]++) {
if(K == 1 && S <= N) res++;
foreach(j; 0..i) {
for(S[j] = '1'; S[j] <= '9'; S[j]++) {
if(K == 2 && S <= N) res++;
foreach(k; 0..j) {
for(S[k] = '1'; S[k] <= '9'; S[k]++) {
if(K == 3 && S <= N) res++;
}
S[k] = '0';
}
}
S[j] = '0';
}
}
S[i] = '0';
}
res.writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;
void main() {
auto N = readln.chomp.to!int;
auto A = readln.split.map!(to!long).array;
auto B = new long[](N+1);
foreach (i; 0..N) B[i+1] = B[i] + A[i];
int a = 0;
int b = 2;
long sa1 = A[0];
long sa2 = A[1];
long sb1 = A[2];
long sb2 = B[N] - B[3];
long ans = 1L << 59;
foreach (i; 2..N-1) {
while (a < i-1 && sa1 + A[a+1] <= sa2 - A[a+1]) sa1 += A[a+1], sa2 -= A[a+1], a += 1;
while (b < N-1 && sb1 + A[b+1] <= sb2 - A[b+1]) sb1 += A[b+1], sb2 -= A[b+1], b += 1;
Tuple!(long, long)[] x = [tuple(sa1, sa2)];
Tuple!(long, long)[] y = [tuple(sb1, sb2)];
if (a < i - 1) x ~= tuple(sa1 + A[a+1], sa2 - A[a+1]);
if (b < N - 1) y ~= tuple(sb1 + A[b+1], sb2 - A[b+1]);
foreach (u; x) foreach (v; y) ans = min(ans, max(u[0], u[1], v[0], v[1]) - min(u[0], u[1], v[0], v[1]));
sa2 += A[i];
sb1 -= A[i];
if (b == i) {
b = i + 1;
sb1 = A[i+1];
sb2 = B[N] - B[i+2];
}
}
ans.writeln;
}
|
D
|
void main()
{
long n = rdElem;
if (n == 1)
{
0.writeln;
return;
}
long[long] cnt;
for (long i = 2; i * i <= n; ++i)
{
while (!(n % i))
{
++cnt[i];
n /= i;
}
}
if (n > 1) ++cnt[n];
long m = cnt.byValue.reduce!max;
long[] list;
list ~= 1;
foreach (i; 2 .. m+1)
{
list ~= list[$-1] + i;
if (list[$-1] > m) break;
}
long result;
foreach (x; cnt.byValue)
{
long num;
foreach (y; list)
{
if (x >= y) ++num;
}
result += num;
}
result.writeln;
}
enum long mod = 10L^^9 + 7;
enum long inf = 1L << 60;
enum double eps = 1.0e-9;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
import std.stdio, std.conv, std.string;
void main() {
(readln.chomp.to!int < 1200 ? "ABC" : "ARC").writeln;
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
auto w = sread();
// writeln(w);
long[char] ary;
foreach (i; 0 .. (w.length))
{
ary[w[i]] = ary.get(w[i], 0) + 1;
}
// writeln(ary);
foreach (value; ary)
{
// writeln(value);
if (value % 2 == 0)
{
continue;
}
else
{
writeln("No");
return;
}
}
writeln("Yes");
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "begin", long, "end");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
long n, m;
scan(n, m);
if(n < m)
swap(n, m);
foreach (_; iota(n))
{
write(m);
}
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
|
/+ dub.sdl:
name "A"
dependency "dunkelheit" version=">=0.9.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
int main() {
Scanner sc = new Scanner(stdin);
scope(exit) assert(!sc.hasNext);
string s;
sc.read(s);
int n = s.length.to!int;
int ans = 0;
int sm = 0;
foreach (int i, c; s) {
sm += c - '0';
if (c != '0') ans = max(ans, sm - 1 + (n-1-i)*9);
}
ans = max(ans, sm);
writeln(ans);
return 0;
}
/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/foundation.d */
// module dkh.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
void read(Args...)(auto ref Args args) {
import std.exception;
static if (args.length != 0) {
enforce(readSingle(args[0]));
read(args[1..$]);
}
}
bool hasNext() {
return succ();
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
|
D
|
import std.stdio, std.conv, std.string, std.algorithm, std.math, std.array, std.container;
int n;
void solve(int max_i, string prefix) {
if(prefix.length == n) {
writeln(prefix);
return;
}
for(int i=0; i<max_i+1; i++) solve(max(i+1, max_i), prefix~('a'+i).to!char);
}
void main() {
n = readln.chomp.to!int;
solve(0, "");
}
|
D
|
import std.stdio;
import std.string;
import std.algorithm;
import std.conv;
void main() {
auto N = readln.chomp.to!int;
auto A = readln.split.to!(int[]);
auto q = readln.chomp.to!int;
auto m = readln.split.to!(int[]);
bool solve(int i, int j) {
if(j < 0) return false;
if(j == 0) return true;
if(i == N) return false;
return solve(i+1, j) || solve(i+1, j - A[i]);
}
m.map!(i => solve(0, i) ? "yes" : "no").each!writeln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 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 ans = new long[](n);
int cnt;
foreach (i; 0..n)
{
auto a = RD!int;
if (a % 2 == 0)
ans[i] = a/2;
else if (cnt == 0)
{
ans[i] = a/2;
if (a < 0)
--ans[i];
++cnt;
}
else
{
ans[i] = a/2;
if (a > 0)
++ans[i];
--cnt;
}
}
foreach (e; ans)
writeln(e);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto N = readln.chomp.to!int;
auto A = readln.split.map!(to!long).array;
auto B = new long[](N+1);
foreach (i; 0..N) B[i+1] = B[i] + A[i];
long[long] cnt;
long ans = 0;
foreach_reverse(i; 0..N) {
long x = B[i];
cnt[B[i+1]] += 1;
if (x in cnt) ans += cnt[x];
}
ans.writeln;
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
// dfmt on
void main()
{
long N = lread();
writeln((N - 2) * 180);
}
|
D
|
module aoj;
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
int main(){
int[] array = new int[10];
for(int i = 0; i < 10; i++) {
array[i] = to!int(chomp(readln()));
}
sort!((int e1, int e2){ return e1 > e2; })(array);
for(int i = 0; i < 3; i++) {
writeln(array[i]);
}
return 0;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new long[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto a = RDA;
long cnt;
foreach (e; a)
{
if (e % 2)
++cnt;
}
ans[ti] = cnt == n;
}
foreach (e; ans)
writeln(e ? "Yes" : "No");
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
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 string[][](t);
foreach (ti; 0..t)
{
auto a = RD;
auto b = RD;
auto c = RD;
ans[ti].length = 2;
if (a >= b)
{
ans[ti][0] ~= '1';
foreach (i; 0..a-1)
ans[ti][0] ~= '0';
if (b == c)
{
ans[ti][1] ~= '1';
foreach (i; 0..b-1)
ans[ti][1] ~= '0';
}
else
{
ans[ti][1] ~= '1';
foreach (i; 1..b)
{
if (i == b-c)
ans[ti][1] ~= '1';
else
ans[ti][1] ~= '0';
}
}
}
else
{
ans[ti][1] ~= '1';
foreach (i; 0..b-1)
ans[ti][1] ~= '0';
if (a == c)
{
ans[ti][0] ~= '1';
foreach (i; 0..a-1)
ans[ti][0] ~= '0';
}
else
{
ans[ti][0] ~= '1';
foreach (i; 1..a)
{
if (i == a-c)
ans[ti][0] ~= '1';
else
ans[ti][0] ~= '0';
}
}
}
debug writeln("gcd:", gcd(ans[ti][0].to!long, ans[ti][1].to!long));
}
foreach (e; ans)
writeln(e[0], " ", e[1]);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.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 q = RD!int;
auto ans = new long[](q);
foreach (i; 0..q)
{
auto n = RD;
auto m = RD;
long[] a = [0];
while ((a.back + m) % 10 != 0)
{
a ~= (a.back + m) % 10;
}
a.popFront;
a ~= 0;
auto x = a.sum;
auto y = n / m;
long z1, z2;
if (y != 0)
{
z1 = y / a.length;
z2 = y % a.length;
}
ans[i] = z1 * x + a[0..cast(int)z2].sum;
}
foreach (e; ans)
writeln(e);
stdout.flush();
debug readln();
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
long bignum = 1_000_000_007;
auto dp = new long[](100_100);
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 n = lread();
auto a = aryread();
long sunuke, rascoon = a.sum;
auto accum = long.max;
foreach (i, e; a)
{
sunuke += e;
rascoon -= e;
if (i < n - 1)
accum = min(abs(sunuke - rascoon), accum);
}
accum.writeln();
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto A = RD;
auto B = RD;
auto C = RD;
auto D = RD;
writeln(max(A*B, C*D));
stdout.flush();
debug readln();
}
|
D
|
import std.stdio;
void main(){
auto cin = new Cin();
auto S = cin.rect!(char[],"")()[0];
//writeln(S);
char[] c;
foreach( s ; S ){
switch( s ){
case "0":
c ~= "0";
break;
case "1":
c ~= "1";
break;
case "B":
c.length = 0<c.length ? c.length-1 : 0 ;
break;
default:break;
}
}
(c).writeln();
}
auto solve(){
}
unittest{
}
import std.stdio,std.conv,std.string;
import std.algorithm,std.array;
class Cin
{
T[] line( T = size_t , string token = " " )( size_t m = 1 ){
T[] arr = [];
foreach( i ; 0..m ){
arr ~= this.read!T();
}
return arr;
}
T[][] rect( T = size_t , string token = " " )( size_t m = 1 ){
T[][] arr = new T[][](m);
foreach( i ; 0..m ){
arr[i] = this.read!T(token);
}
return arr;
}
private T[] read( T = size_t )( string token = " " ){
T[] arr;
foreach( elm ; readln().chomp().split(token) ){
arr ~= elm.to!T();
}
return arr;
}
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;
void main() {
auto N = readln.chomp.to!int;
auto G = new int[][](N);
foreach (i; 0..N-1) {
auto s = readln.split.map!(to!int);
G[s[0]] ~= s[1];
G[s[1]] ~= s[0];
}
int root = -1;
foreach (i; 0..N) if (G[i].length >= 3) root = i;
if (root == -1) {
writeln(1);
return;
}
int dfs(int n, int p) {
int ret = 0;
int cnt = 0;
int children = G[n].length.to!int - (p != -1);
foreach (m; G[n]) {
if (m == p) continue;
int v = dfs(m, n);
ret += v;
cnt += v > 0;
}
if (cnt < children - 1) ret += children - 1 - cnt;
return ret;
}
dfs(root, -1).writeln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto K = RD;
writeln(N - K + 1);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.numeric;
void main()
{
auto n = readln.chomp.to!int;
auto h = readln.chomp.split.to!(int[]);
int res;
foreach (i; 0..n) {
while (h[i]) {
int j = i + 1;
while (j < n && h[j]) {
j++;
}
h[i..j] -= 1;
res++;
}
}
res.writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.array;
import std.range;
import std.algorithm;
import std.conv;
import std.typecons;
long byTwoCount(long a){
long result = 0;
while(a % 2 == 0){
result++;
a /= 2;
}
return result;
}
long solve(long[] as){
return as.map!(byTwoCount).sum();
}
void main(){
readln();
auto as = readln().chomp().split().map!(to!long).array();
solve(as).writeln();
}
|
D
|
import std.conv, std.stdio;
import std.algorithm, std.array, std.string, std.range;
void main()
{
auto
buf = readln.chomp.split.to!(int[]),
n = buf[0], x = buf[1] - 1, y = buf[2] - 1,
d = new int[][](n, n);
foreach (i; 0..n)
foreach (j; i..n)
d[i][j] = j - i;
int[int] c;
foreach (i; 1..n)
c[i] = n - i;
c[d[x][y]] -= 1;
d[x][y] = 1;
c[1] += 1;
foreach (i; 0..n)
foreach (j; i..n)
{
c[d[i][j]] -= 1;
d[i][j] = d[i][j].min(d[i.min(x)][i.max(x)] + 1 + d[j.min(y)][j.max(y)]);
c[d[i][j]] += 1;
}
foreach (i; 1..n)
c[i].writeln;
}
|
D
|
import std.stdio, std.string, std.conv, std.bigint, std.typecons, std.algorithm, std.array, std.math, std.range, std.functional;
void main() {
readln.split.to!(long[]).pipe!(tmp => max(tmp[0] - tmp[1], 0)).writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
import std.range;
void main(){
auto A=readln.split.to!(int[]),a=A[0],b=A[1],c=A[2];
if(a+b>=c)writeln("Yes");
else writeln("No");
}
|
D
|
void main(){
long x, a, b;
scanf("%ld %ld %ld", &x, &a, &b);
if(abs(x-a)>abs(x-b))writeln("B");
else writeln("A");
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range;
const long mod = 10^^9+7;
// 1要素のみの入力
T inelm(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] inln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split())ln ~= elm.to!T();
return ln;
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
auto xy = readints;
int x = xy[0], y = xy[1];
writeln(x + y / 2);
}
|
D
|
void main() {
auto s = rs;
auto p = rs;
writeln((s ~ s).canFind(p) ? "Yes" : "No");
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto S = readln.chomp.to!(char[]);
foreach (i, c; S) {
if (i%2 == 0) {
if (c == 'L') {
writeln("No");
return;
}
} else {
if (c == 'R') {
writeln("No");
return;
}
}
}
writeln("Yes");
}
|
D
|
void main(){
int k,a,b;
k = _scan();
scanf("%d %d", &a, &b);
if( a<= b/k*k ) writeln("OK");
else writeln("NG");
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
|
D
|
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.complex;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
auto readInts() {
return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
return readInts()[0];
}
auto readLongs() {
return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
return readLongs()[0];
}
void readlnTo(T...)(ref T t) {
auto s = readln().split();
assert(s.length == t.length);
foreach(ref ti; t) {
ti = s[0].to!(typeof(ti));
s = s[1..$];
}
}
const real eps = 1e-10;
const long p = 1_000_000_000 + 7;
//n!
long P(long n) {
if(n == 0) {
return 1;
}
return n * P(n-1) % p;
}
void main(){
long n, m;
readlnTo(n, m);
if(abs(n-m) > 1) {
writeln(0);
return;
}
writeln((P(n) * P(m) % p) * (n == m ? 2: 1) % p);
}
|
D
|
/* imports all std modules {{{*/
import
std.algorithm,
std.array,
std.ascii,
std.base64,
std.bigint,
std.bitmanip,
std.compiler,
std.complex,
std.concurrency,
std.container,
std.conv,
std.csv,
std.datetime,
std.demangle,
std.encoding,
std.exception,
std.file,
std.format,
std.functional,
std.getopt,
std.json,
std.math,
std.mathspecial,
std.meta,
std.mmfile,
std.net.curl,
std.net.isemail,
std.numeric,
std.parallelism,
std.path,
std.process,
std.random,
std.range,
std.regex,
std.signals,
std.socket,
std.stdint,
std.stdio,
std.string,
std.system,
std.traits,
std.typecons,
std.uni,
std.uri,
std.utf,
std.uuid,
std.variant,
std.zip,
std.zlib;
/*}}}*/
/+---test
1905
---+/
/+---test
0112
---+/
/+---test
1700
---+/
void main(string[] args) {
const S = readln.chomp;
const l = S[0..2].to!int;
const r = S[2..4].to!int;
const yymm = 1 <= r && r <= 12;
const mmyy = 1 <= l && l <= 12;
if (yymm && !mmyy) {
"YYMM".writeln;
} else if (!yymm && mmyy) {
"MMYY".writeln;
} else if (yymm && mmyy) {
"AMBIGUOUS".writeln;
} else {
"NA".writeln;
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto hw = readln.split.to!(int[]);
auto H = hw[0];
auto W = hw[1];
char[][] P;
P.length = H;
foreach (i; 0..H) P[i] = readln.chomp.to!(char[]);
foreach (i; 0..H) {
foreach (j; 0..W) {
if (P[i][j] == '.') continue;
bool ok;
foreach (d; [[1,0], [0,1], [-1,0], [0,-1]]) {
auto x = j+d[0];
auto y = i+d[1];
if (x >= 0 && x < W && y >= 0 && y < H && P[y][x] == '#') {
ok = true;
break;
}
}
if (!ok) {
writeln("No");
return;
}
}
}
writeln("Yes");
}
|
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 readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}}
void main()
{
int n; readV(n);
int[] p; readC(n, p);
writeln(p.sum - p.reduce!max/2);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
int[51][51] GF;
int[51] NS;
void main()
{
auto nm = readln.split.to!(int[]);
auto N = nm[0];
auto M = nm[1];
foreach (i; 1..M+1) {
auto ab = readln.split.to!(int[]);
auto A = ab[0];
auto B = ab[1];
GF[A][B] = i;
GF[B][A] = i;
}
int cnt;
foreach (i; 1..M+1) {
void solve(int p) {
if (NS[p] == i) return;
NS[p] = i;
foreach (j, n; GF[p][1..N+1]) if (n && n != i) solve((j+1).to!int);
}
solve(1);
foreach (n; NS[1..N+1]) if (n != i) {
++cnt;
break;
}
}
writeln(cnt);
}
|
D
|
import std.stdio, std.algorithm, std.string, std.conv, std.array, std.range, std.math;
int[] readints() { return readln.split.to!(int[]); }
void main() {
auto a = readints();
auto q = new Queue!(int[]);
q.push(a ~ 0);
int ans = 0;
while (1) {
auto t = q.front();
if (t[0] == t[1] && t[1] == t[2]) {
ans = t[3];
break;
}
q.pop();
t[3] += 1;
// +2
auto tt = t.dup; // 参照になってしまうから dup が必要
tt[t[0..3]._minIndex] += 2;
q.push(tt);
if (t[0..3]._maxElement - t[0..3]._minElement > 3) continue;
// +1
q.push([t[0] + 1, t[1] + 1, t[2] + 0, t[3]]);
q.push([t[0] + 1, t[1] + 0, t[2] + 1, t[3]]);
q.push([t[0] + 0, t[1] + 1, t[2] + 1, t[3]]);
}
writeln(ans);
}
int _minIndex(T) (T[] a){
int mini = 1145141919;
int r = -1;
foreach (i, v; a) if (v < mini) {
r = i.to!int;
mini = v;
}
return r;
}
int _maxElement(int[] a) {
int m = -1;
foreach(i; a) if(m<i) m = i;
return m;
}
int _minElement(int[] a) {
int m = 1145141919;
foreach(i; a) if(m>i) m = i;
return m;
}
class Queue(T) {
T[] queue;
this() { }
T front(){
return queue[0];
}
void pop(){
queue = queue[1..$];
}
void push(T x){
queue ~= x;
}
int size(){
return queue.length.to!int;
}
}
|
D
|
import std.stdio,std.string,std.conv;
int main()
{
string s;
while((s = readln.chomp).length != 0)
{
auto _s = s.split(" ");
int a = _s[0].to!int;
int b = _s[1].to!int;
int c = _s[2].to!int;
int n = a * 10000 + b * 100 + c;
if(n < 18680908)
{
writeln("pre-meiji");
}
else if(18680908 <= n && n < 19120730)
{
writeln("meiji ",a - 1867," ",b," ",c);
}
else if(19120730 <= n && n < 19261225)
{
writeln("taisho ",a - 1911," ",b," ",c);
}
else if(19261225 <= n && n < 19890108)
{
writeln("showa ",a - 1925," ",b," ",c);
}
else if(19890108 <= n)
{
writeln("heisei ",a - 1988," ",b," ",c);
}
}
return 0;
}
|
D
|
import std.stdio;
import std.algorithm;
import std.range;
import std.array;
import std.string;
void main(){
auto num_str = readln.chop;
auto check_tri = (string target){
return target[0] == target[1] && target[1] == target[2];
};
auto answer = check_tri(num_str[0..3]) || check_tri(num_str[1..4]) ? "Yes" : "No";
writeln(answer);
}
|
D
|
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop;
// dfmt on
void main()
{
auto S = sread();
auto T = sread();
auto b = new long[][](26);
foreach (i, c; S)
b[c - 'a'] ~= i;
foreach (c; T)
if (b[c - 'a'].empty)
{
writeln(-1);
return;
}
long ptr;
long ans;
foreach (j, c; T)
{
long k = () {
long ok = 0;
long ng = b[c - 'a'].length;
while (1 < ng - ok)
{
long m = (ok + ng) / 2;
if (b[c - 'a'][m] <= ptr)
ok = m;
else
ng = m;
}
return ok;
}();
long m = long.max;
foreach (i; [
b[c - 'a'][k], b[c - 'a'][(k + 1) % $], b[c - 'a'][($ + k - 1) % $]
])
{
if (ptr == i)
m = m.min((j != 0 && T[j - 1] == c) ? S.length : 0);
if (ptr < i)
m = m.min(i - ptr);
if (i < ptr)
m = m.min(S.length + i - ptr);
}
ptr += m;
ptr %= S.length;
ans += m;
}
writeln(ans + 1);
}
|
D
|
void main() {
auto A = ri, B = ri, C = ri, D = ri, E = ri;
auto arr = [A, B, C, D, E];
int res = int.max;
do {
int tmp;
foreach(i, v; arr) {
if(i == 4) tmp += v;
else tmp = (tmp + v + 9) / 10 * 10;
}
res = min(res, tmp);
} while(nextPermutation(arr));
res.writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n; readV(n);
int[] a; readA(n, a);
auto c = new int[](10^^5+1);
foreach (ai; a) c[ai]++;
auto c1 = 0, c2 = 0;
foreach (ci; c)
if (ci%2 == 1) ++c1;
else if (ci > 0) ++c2;
writeln(c1+c2-c2%2);
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto n = readln.chomp.to!size_t;
auto a = readln.split.to!(int[]);
auto c = new int[](9);
foreach (ai; a) ++c[min(8, ai/400)];
auto r = c[0..8].count!"a>0";
if (r == 0)
writeln(1, " ", c[8]);
else
writeln(r, " ", r + c[8]);
}
|
D
|
void main()
{
string s = readln.chomp;
writeln(s[0..4], " ", s[4..$]);
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio;
import std.range;
import std.string;
import std.algorithm;
import std.conv;
void main() {
writeln(readln.find("A").retro.find("Z").source.length);
}
|
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 ps = readln.split.to!(int[]);
int d;
foreach (i, p; ps) {
if (i.to!int+1 != p) ++d;
}
writeln(d <= 2 ? "YES" : "NO");
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.functional;
import std.array;
import std.conv;
import std.math;
import std.typecons;
import std.regex;
import std.range;
void main(){
string list = "abcdefghijklmnopqrstuvwxyz.?! ";
while(true){
string s = readln();
if(stdin.eof()) break;
s = s.chomp();
if(s.length % 2 == 1){
writeln("NA");
continue;
}
string ans = "";
for(int i=0;i<s.length/2;i++){
int c = to!int(s[2*i]-'0') - 1;
int r = to!int(s[2*i+1]-'0') - 1;
if(5<c || 4<r || c < 0 || r < 0){
ans = "NA";
break;
}
ans ~= list[5*c + r];
}
writeln(ans);
}
}
|
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;
/*
A = 1, B = 2, C = 100, D = 100みたいなケースもある
*/
void main() {
int t;
scan(t);
while (t--) {
long ai, bi, ci, di;
scan(ai, bi, ci, di);
writeln(solve(ai, bi, ci, di));
}
}
string solve(long a, long b, long c, long d) {
if (a < b) {
return "No";
}
if (b > c) {
if (b == c + 1) {
if (d >= b) {
return "Yes";
}
else {
return "No";
}
}
if ((a % b) > c) {
return "No";
}
else {
long g = gcd(b, d);
if (d >= b && (c + 1 + g - 1) / g * g >= b) {
return "Yes";
}
else {
return "No";
}
}
}
else {
if (b == 1) {
return "Yes";
}
if (d >= c) {
return "Yes";
}
else {
if (b <= d) {
return "Yes";
}
else {
return "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
|
import std.stdio, std.string, std.conv, std.algorithm;
void main()
{
foreach(_; 0..readln.chomp.to!int)
{
readln;
int up, down;
auto inp=readln.split.map!(to!int);
for(int i; i < inp.length-1; ++i)
{
up=max(inp[i+1]-inp[i],up);
down=min(inp[i+1]-inp[i],down);
}
writeln(up, " ", -down);
}
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
void main() {
int R, G;
R = readln().chomp().to!(int);
G = readln().chomp().to!(int);
writeln(R+(G-R)*2);
}
|
D
|
import std.stdio, std.string, std.conv, std.range, std.algorithm;
void main() {
auto abc = readln.chomp.split.map!(to!int);
if (count(abc, 5) == 2 && count(abc, 7) == 1) {
"YES".writeln;
} else {
"NO".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);}}
alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop;
// dfmt on
void main()
{
long N = lread();
auto S = sread();
auto R = new long[](N + 1);
auto W = new long[](N + 1);
foreach (i; 0 .. N)
{
R[i + 1] = R[i] + (S[i] == 'R');
W[i + 1] = W[i] + (S[i] == 'W');
}
long ans = long.max;
foreach (i; 0 .. N + 1)
{
long r = R[$ - 1] - R[i];
long w = W[i];
long a = min(r, w) + abs(r - w);
ans = ans.min(a);
}
writeln(ans);
}
|
D
|
import std.stdio, std.string, std.conv, std.range, std.algorithm;
void main() {
iota(2, readln.chomp.to!long+1).reduce!"a*b".writeln;
}
|
D
|
// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
immutable mod = 10^^9 + 7;
void main() {
int n;
auto s = new string[](2);
scan(n);
iota(2).each!(i => s[i] = readln.chomp);
if (n == 1) {
writeln(3);
return;
}
int pos = 0;
int joutai;
long ans;
if (s[0][0] == s[1][0]) {
joutai = 0;
ans = 3;
pos = 1;
}
else {
joutai = 1;
ans = 6;
pos = 2;
}
while (pos < n) {
if (pos == n - 1) {
if (joutai == 0) {
(ans *= 2) %= mod;
}
break;
}
if (s[0][pos] == s[1][pos]) {
if (joutai == 0) {
(ans *= 2) %= mod;
}
pos++;
joutai = 0;
}
else {
if (joutai == 0) {
(ans *= 2) %= mod;
}
else {
(ans *= 3) %= mod;
}
pos += 2;
joutai = 1;
}
debug {
stderr.write(ans, " ");
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto n = readln.chomp.to!int;
auto k = readln.chomp.to!int;
auto x = readln.chomp.to!int;
auto y = readln.chomp.to!int;
if (n < k)
writeln(x * n);
else
writeln(x * k + y * (n - k));
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
int n = readint;
int a = readint;
writeln(n * n - a);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto S = RD!string;
auto T = RD!string;
bool ans;
foreach (i; 0..S.length)
{
bool ok = true;
foreach (j; 0..T.length)
{
if (S[(i+j)%S.length] != T[j])
{
ok = false;
break;
}
}
if (ok)
{
ans = ok;
break;
}
}
writeln(ans ? "Yes" : "No");
stdout.flush();
debug readln();
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
ulong bignum = 1_000_000_007;
alias SugarWater = Tuple!(long, "swater", long, "sugar");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
long n = lread(), ans;
if(n >= 0)
ans += min(10 - 1, n - 1 + 1);
if(n >= 100)
ans += min(1000 - 100, n - 100 + 1);
if(n >= 10_000)
ans += min(100_000 - 10_000, n - 10_000 + 1);
ans.writeln();
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
/// return [P の数, 長さ]
long[] burger(long n) {
if (n == 0) {
return [1, 1];
}
auto c = burger(n - 1);
long p = c[0] * 2 + 1;
long len = c[1] * 2 + 3;
return [p, len];
}
long calc(long n, long x, long p) {
// B
x--;
if (x == 0) return p;
// L - 1
auto c = burger(n - 1);
if (x < c[1]) { // たべすぎ
return calc(n - 1, x, p);
}
x -= c[1];
p += c[0];
if (x == 0) return p;
// P
x--;
p++;
if (x == 0) return p;
// L - 1
if (x < c[1]) { // たべすぎ
return calc(n - 1, x, p);
}
x -= c[1];
p += c[0];
if (x == 0) return p;
// B
x--;
if (x == 0) return p;
return p;
}
void main() {
auto nx = reads!long;
long n = nx[0], x = nx[1];
writeln(calc(n, x, 0));
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
import std.typecons;
import std.bigint;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.container.deque;
int main() {
auto sc = new Scanner(stdin);
int n, m, k;
sc.read(n, m, k); m++;
int[] a = new int[n];
a.each!((ref x) => sc.read(x));
long[] dp = a.map!(to!long).array;
long[] ndp = new long[n];
auto deq = Deque!(int, false).make();
auto p = deq.p;
foreach (int ph; 2..k+1) {
ndp[] = -(10L^^18);
deq.clear();
foreach (int i; 0..n) {
if (p.length && (*p)[0] == i-m) {
p.removeFront();
}
if (p.length) {
ndp[i] = dp[(*p)[0]] + 1L * ph * a[i];
}
while (p.length && dp[(*p)[p.length-1]] <= dp[i]) {
p.removeBack();
}
p.insertBack(i);
}
swap(dp, ndp);
}
writeln(dp.fold!max);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
//fold(for old compiler)
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);
}
}
}
unittest {
import std.stdio;
auto l = [1, 2, 3, 4, 5];
assert(l.fold!"a+b"(10) == 25);
}
}
version (X86) static if (__VERSION__ < 2071) {
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/deque.d */
// module dcomp.container.deque;
struct Deque(T, bool hasNull = true) {
import core.exception : RangeError;
import core.memory : GC;
import std.range : ElementType, isInputRange;
import std.traits : isImplicitlyConvertible;
struct Payload {
T *d;
size_t st, length, cap;
@property bool empty() const { return length == 0; }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {
version(assert) if (length <= i) throw new RangeError();
return d[(st+i >= cap) ? (st+i-cap) : st+i];
}
private void expand() {
import std.algorithm : max;
assert(length == cap);
auto nc = max(size_t(4), 2*cap);
T* nd = cast(T*)GC.malloc(nc * T.sizeof);
foreach (i; 0..length) {
nd[i] = this[i];
}
d = nd; st = 0; cap = nc;
}
void clear() {
st = length = 0;
}
void insertFront(T v) {
if (length == cap) expand();
if (st == 0) st += cap;
st--; length++;
this[0] = v;
}
void insertBack(T v) {
if (length == cap) expand();
length++;
this[length-1] = v;
}
void removeFront() {
assert(!empty, "Deque.removeFront: Deque is empty");
st++; length--;
if (st == cap) st = 0;
}
void removeBack() {
assert(!empty, "Deque.removeBack: Deque is empty");
length--;
}
}
struct RangeT(A) {
alias T = typeof(*(A.p));
alias E = typeof(A.p.d[0]);
T *p;
size_t a, b;
@property bool empty() const { return b <= a; }
@property size_t length() const { return b-a; }
@property RangeT save() { return RangeT(p, a, b); }
@property RangeT!(const A) save() const {
return typeof(return)(p, a, b);
}
alias opDollar = length;
@property ref inout(E) front() inout { return (*p)[a]; }
@property ref inout(E) back() inout { return (*p)[b-1]; }
void popFront() {
version(assert) if (empty) throw new RangeError();
a++;
}
void popBack() {
version(assert) if (empty) throw new RangeError();
b--;
}
ref inout(E) opIndex(size_t i) inout { return (*p)[i]; }
RangeT opSlice() { return this.save; }
RangeT opSlice(size_t i, size_t j) {
version(assert) if (i > j || a + j > b) throw new RangeError();
return typeof(return)(p, a+i, a+j);
}
RangeT!(const A) opSlice() const { return this.save; }
RangeT!(const A) opSlice(size_t i, size_t j) const {
version(assert) if (i > j || a + j > b) throw new RangeError();
return typeof(return)(p, a+i, a+j);
}
}
alias Range = RangeT!Deque;
alias ConstRange = RangeT!(const Deque);
alias ImmutableRange = RangeT!(immutable Deque);
Payload *p;
private void I() { if (hasNull && !p) p = new Payload(); }
private void C() const { version(assert) if (!p) throw new RangeError(); }
//some value
this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) {I;
p = new Payload();
foreach (v; values) {
insertBack(v);
}
}
//range
this(Range)(Range r)
if (isInputRange!Range &&
isImplicitlyConvertible!(ElementType!Range, T) &&
!is(Range == T[])) {I;
p = new Payload();
foreach (v; r) {
insertBack(v);
}
}
static Deque make() {
Deque que;
que.p = new Payload();
return que;
}
@property private bool hasPayload() const { return (!hasNull || p); }
@property bool empty() const { return (!hasPayload || p.empty); }
@property size_t length() const { return (hasPayload ? p.length : 0); }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; }
ref inout(T) front() inout {C; return (*p)[0]; }
ref inout(T) back() inout {C; return (*p)[$-1]; }
void clear() { if (p) p.clear(); }
void insertFront(T v) {I; p.insertFront(v); }
void insertBack(T v) {I; p.insertBack(v); }
void removeFront() {C; p.removeFront(); }
void removeBack() {C; p.removeBack(); }
Range opSlice() {I; return Range(p, 0, length); }
}
unittest {
import std.algorithm : equal;
import std.range.primitives : isRandomAccessRange;
import std.container.util : make;
auto q = make!(Deque!int);
assert(isRandomAccessRange!(typeof(q[])));
//insert,remove
assert(equal(q[], new int[](0)));
q.insertBack(1);
assert(equal(q[], [1]));
q.insertBack(2);
assert(equal(q[], [1, 2]));
q.insertFront(3);
assert(equal(q[], [3, 1, 2]) && q.front == 3);
q.removeFront;
assert(equal(q[], [1, 2]) && q.length == 2);
q.insertBack(4);
assert(equal(q[], [1, 2, 4]) && q.front == 1 && q.back == 4 && q[$-1] == 4);
q.insertFront(5);
assert(equal(q[], [5, 1, 2, 4]));
//range
assert(equal(q[][1..3], [1, 2]));
assert(equal(q[][][][], q[]));
//const range
const auto rng = q[];
assert(rng.front == 5 && rng.back == 4);
//reference type
auto q2 = q;
q2.insertBack(6);
q2.insertFront(7);
assert(equal(q[], q2[]) && q.length == q2.length);
//construct with make
auto a = make!(Deque!int)(1, 2, 3);
auto b = make!(Deque!int)([1, 2, 3]);
assert(equal(a[], b[]));
}
unittest {
import std.algorithm : equal;
import std.range.primitives : isRandomAccessRange;
import std.container.util : make;
auto q = make!(Deque!int);
q.clear();
assert(equal(q[], new int[0]));
foreach (i; 0..100) {
q.insertBack(1);
q.insertBack(2);
q.insertBack(3);
q.insertBack(4);
q.insertBack(5);
assert(equal(q[], [1,2,3,4,5]));
q.clear();
assert(equal(q[], new int[0]));
}
}
unittest {
Deque!int a;
Deque!int b;
a.insertFront(2);
assert(b.length == 0);
}
unittest {
import std.algorithm : equal;
import std.range : iota;
Deque!int a;
foreach (i; 0..100) {
a.insertBack(i);
}
assert(equal(a[], iota(100)));
}
/* 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) {
//string or char[10] etc
//todo optimize
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) {
//static
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);
}
}
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
fout.writeln("1 2 3");
fout.writeln("ab cde");
fout.writeln("1.0 1.0 2.0");
fout.close;
Scanner sc = new Scanner(File(fileName, "r"));
int a;
int[2] b;
char[2] c;
string d;
double e;
double[] f;
sc.read(a, b, c, d, e, f);
assert(a == 1);
assert(equal(b[], [2, 3]));
assert(equal(c[], "ab"));
assert(equal(d, "cde"));
assert(e == 1.0);
assert(equal(f, [1.0, 2.0]));
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File, writeln;
import std.datetime;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
foreach (i; 0..1_000_000) {
fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2);
}
fout.close;
writeln("Scanner Speed Test(3*1,000,000 int)");
StopWatch sw;
sw.start;
Scanner sc = new Scanner(File(fileName, "r"));
foreach (i; 0..500_000) {
int a, b, c;
sc.read(a, b, c);
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
foreach (i; 500_000..700_000) {
int[3] d;
sc.read(d);
int a = d[0], b = d[1], c = d[2];
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
foreach (i; 700_000..1_000_000) {
int[] d;
sc.read(d);
assert(d.length == 3);
int a = d[0], b = d[1], c = d[2];
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
writeln(sw.peek.msecs, "ms");
}
|
D
|
import std.stdio;
int main(){
for(int i=1;i<10;i++)for(int j=1;j<10;j++)printf("%dx%d=%d\n",i,j,i*j);
return 0;
}
|
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;
if (N == 3) {
writeln("2 5 63");
return;
}
int[] ths;
foreach (i; 1..30001) {
if (i%2 != 0 && i%3 == 0) ths ~= i;
}
int[] res;
size_t t;
while (t < ths.length && N > 3) {
res ~= ths[t++];
res ~= ths[t++];
N -= 2;
}
int x = 1, s;
foreach (_; 0..N) {
res ~= 2*x;
s += x;
++x;
}
if (s%3 != 0) {
res ~= 2*x;
s += x;
switch (s%3) {
case 1:
res = res[0..t] ~ res[t+1..$];
break;
case 2:
res = res[0..t+1] ~ res[t+2..$];
break;
default:
res = res[0..t+2] ~ res[t+3..$];
}
}
writeln(res.to!(string[]).join(" "));
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
string s, t;
scan(s);
scan(t);
iota(3).map!(i => s[i] == t[i]).sum().writeln;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
void main() {
try {
for (; ; ) {
const N = readInt();
auto S = new string[N];
auto T = new long[N];
foreach (i; 0 .. N) {
S[i] = readToken();
T[i] = readLong();
}
const X = readToken();
long ans;
foreach (i; 0 .. N) {
if (S[i] == X) {
foreach (j; i + 1 .. N) {
ans += T[j];
}
break;
}
}
writeln(ans);
}
} catch (EOFException e) {
}
}
|
D
|
import std.stdio, std.string, std.conv;
long [100] L;
long foo(int n) {
if(L[n]==0) {
L[n]= foo(n-1) + foo(n-2);
}
return L[n];
}
void main() {
int N = readln().chomp().to!(int);
L[0]=2;L[1]=1;
writeln(foo(N));
}
|
D
|
ulong[] A;
ulong cost(ulong i, ulong j) {
switch(j) {
case 0, 4: // never visit there
return A[i];
case 1, 3: // even(x > 0)
return A[i] == 0 ? 2 : A[i] % 2;
case 2: // odd
return (A[i] + 1) % 2;
default: return 0;
}
}
// dp[i][j]
// i: point
// j: current mode(details on editorial)
// allowed to move any times
// just need to take care of the parity of A[i]
void main() {
auto L = ri;
A = L.iota.map!(i => readAs!ulong).array;
auto dp = new ulong[][](L+1, 5);
foreach(ref v; dp) v[] = INF;
dp[0][0] = 0;
foreach(i; 0..L) foreach(j; 0..5) foreach(k; j..5) {
dp[i+1][k] = min(dp[i+1][k], dp[i][j] + cost(i, k));
}
dp[L].reduce!min.writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
const long INF = 1UL << 60;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
import std.datetime, std.bigint;
int n, m;
char[][] flag;
void main() {
scan(n, m);
if (n % 3 && m % 3) {
writeln("NO");
return;
}
flag = new char[][](n, m);
iota(n).each!(i => flag[i][] = readln.chomp);
debug {
writefln("%(%(%s %)\n%)", flag);
}
int r, c;
if (n % 3 == 0) {
r = n / 3;
auto rgb = new int[](3);
bool ok = true;
foreach (k ; 0 .. 3) {
char ch = flag[k * r][0];
if (ch == 'R') rgb[0]++;
else if (ch == 'G') rgb[1]++;
else rgb[2]++;
foreach (i ; k * r .. k * r + r) {
foreach (j ; 0 .. m) {
if (flag[i][j] != ch) ok = false;
}
}
}
if (rgb == [1, 1, 1] && ok) {
writeln("YES");
return;
}
}
if (m % 3 == 0) {
c = m / 3;
auto rgb = new int[](3);
bool ok = true;
foreach (k ; 0 .. 3) {
char ch = flag[0][k * c];
if (ch == 'R') rgb[0]++;
else if (ch == 'G') rgb[1]++;
else rgb[2]++;
foreach (j ; k * c .. k * c + c) {
foreach (i ; 0 .. n) {
if (flag[i][j] != ch) ok = false;
}
}
if (rgb == [1, 1, 1] && ok) {
writeln("YES");
return;
}
}
}
writeln("NO");
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main() {
int n = readln.chomp.to!int;
int price = 0;
int[][] apList;
for (int i = 0; i < n; i++) {
auto ap = readln.chomp.split.map!(to!int);
int a = ap[0];
int p = ap[1];
apList ~= [a, p];
}
for (int i = 1; i < n; i++) {
if (apList[i - 1][1] < apList[i][1]) {
apList[i][1] = apList[i - 1][1];
}
}
int answer = 0;
foreach (e; apList) {
answer += e[0] * e[1];
}
writeln(answer);
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
long n, m; rd(n); rd(m);
auto k=2L, p=1L;
while(k<=m) k*=2, p++;
if(p<=n){writeln(m); return;}
auto npow2=1L;
while(n--) npow2*=2;
writeln(m%npow2);
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
void wr(T...)(T x){
import std.stdio;
foreach(e; x) write(e, " ");
writeln();
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto a = RD!int-1;
auto b = RD!int-1;
auto da = RD!int;
auto db = RD!int;
auto edges = new int[][](n);
foreach (i; 0..n-1)
{
auto u = RD!int-1;
auto v = RD!int-1;
edges[u] ~= v;
edges[v] ~= u;
}
if (db <= da*2)
{
ans[ti] = true;
continue;
}
bool dfs1(int pos, int last, int depth)
{
if (pos == b) return true;
if (depth == da) return false;
foreach (v; edges[pos])
{
if (v == last) continue;
auto res = dfs1(v, pos, depth+1);
if (res) return true;
}
return false;
}
auto otk = dfs1(a, -1, 0);
if (otk)
{
ans[ti] = true;
continue;
}
int[] dfs2(int pos, int last, int depth)
{
int[] res = [depth, pos];
foreach (v; edges[pos])
{
if (v == last) continue;
auto r = dfs2(v, pos, depth+1);
if (r[0] > res[0])
{
res = r;
}
}
return res;
}
auto r = dfs2(0, -1, 0);
auto r2 = dfs2(r[1], -1, 0);
ans[ti] = r2[0] <= da*2;
}
foreach (e; ans)
writeln(e ? "Alice" : "Bob");
stdout.flush;
debug readln;
}
|
D
|
import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;
import std.numeric;
import std.bigint;
import core.checkedint;
void main()
{
string[string] dic = [
"purple" : "Power",
"green" : "Time",
"blue" : "Space",
"orange" : "Soul",
"red" : "Reality",
"yellow" : "Mind"
];
int m = readln.chomp.to!int;
foreach (i; 0..m) {
string s = readln.chomp;
dic.remove(s);
}
dic.length.writeln;
foreach (k, v; dic) {
v.writeln;
}
}
void tie(R, Args...)(R arr, ref Args args)
if (isRandomAccessRange!R || isArray!R)
in
{
assert (arr.length == args.length);
}
body
{
foreach (i, ref v; args) {
alias T = typeof(v);
v = arr[i].to!T;
}
}
void verbose(Args...)(in Args args)
{
stderr.write("[");
foreach (i, ref v; args) {
if (i) stderr.write(", ");
stderr.write(v);
}
stderr.writeln("]");
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto M = s[1];
auto S = readln.chomp;
auto T = readln.chomp;
if (S.canFind('*')) {
if (N > M + 1) {
writeln("NO");
return;
}
bool ok = true;
for (int i = 0; S[i] != '*'; ++i) {
if (S[i] != T[i]) {
ok = false;
break;
}
}
for (int i = 0; S[N-i-1] != '*'; ++i) {
if (S[N-i-1] != T[M-i-1]) {
ok = false;
break;
}
}
writeln(ok ? "YES" : "NO");
} else {
writeln( S == T ? "YES" : "NO" );
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new int[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto k = RD!int;
if (k >= n)
{
ans[ti] = k - n;
}
else
{
n -= k;
ans[ti] = n % 2;
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
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;
import std.container;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
void main(){
long n = read.to!long;
long a = read.to!long;
long b = read.to!long;
long c = read.to!long;
long d = read.to!long;
long h = abs(b - a);
long m = -(n - 1) * d;
long r = (h - m) % (c + d);
long x = (n - 1) * (d - c);
long q = (h - m - x + (c + d - 1)) / (c + d);
string ans = (q <= n - 1 && r <= x)? "YES": "NO";
debug "h m r x q".writeln;
debug [h, m, r, x, q].writeln;
ans.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 = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "x", long, "y", long, "cost");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
auto n = lread();
long sum;
foreach(i; iota(n))
{
if((i + 1) % 3 && (i + 1) % 5)
sum += i + 1;
}
sum.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
|
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()
{
string a, b;
scan(a, b);
long n = (a ~ b).to!long();
foreach (i; 1 .. 400)
if (i * i == n)
{
writeln("Yes");
return;
}
writeln("No");
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
bool[100000] prime;
void initPrime() {
prime[0] = false;
prime[1] = false;
for (int i = 3; i < 100000; i++) {
for (int j = 2; j * j <= i; j++) {
if (i % j == 0) {
prime[i] = false;
}
}
}
}
void main() {
prime[] = true;
initPrime();
int N = readln.chomp.to!(int);
int[] ans;
for (int i = 3; i <= 55555 && ans.length < N; i++) {
if (prime[i] && i % 5 == 1) ans ~= i;
}
for (int i = 0; i < N; i++) {
if (i != 0) write(" ");
write(ans[i]);
}
writeln();
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.typecons;
import std.numeric, std.math;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }
long mod = 10^^9 + 7;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
long cnt_t, cnt_a;
foreach (i; 0..N)
{
auto T = RD;
auto A = RD;
long pt = 1, pa = 1;
if (T < cnt_t)
pt = cnt_t / T + (cnt_t % T != 0 ? 1 : 0);
if (A < cnt_a)
pa = cnt_a / A + (cnt_a % A != 0 ? 1 : 0);
auto p = max(pt, pa);
cnt_t = T * p;
cnt_a = A * p;
//stderr.writeln(i, ":", cnt_t, " ", cnt_a);
}
writeln(cnt_t + cnt_a);
stdout.flush();
//readln();
}
|
D
|
void main()
{
long n, k;
rdVals(n, k);
long[] a = rdRow;
Edge[long] list;
long pos;
long l;
long rem = k;
if (k <= n)
{
foreach (i; 0 .. k)
{
pos = a[pos] - 1;
}
}
else
{
foreach (i; 0 .. k)
{
if (pos in list)
{
rem -= list[pos].cost;
l = i - list[pos].cost;
break;
}
else
{
list[pos] = Edge(a[pos]-1, i);
pos = a[pos] - 1;
}
}
rem %= l;
foreach (i; 0 .. rem)
{
pos = list[pos].to;
}
}
pos += 1;
pos.writeln;
}
struct Edge
{
long to, cost;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
enum double eps = 1.0e-9;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
import std.algorithm, std.array, std.container, std.range, std.bitmanip;
import std.numeric, std.math, std.bigint, std.random, core.bitop;
import std.string, std.conv, std.stdio, std.typecons;
void main()
{
int x;
for (auto i = 1; ; ++i) {
x = readln.chomp.to!int;
if (x == 0) break;
writeln("Case ", i, ": ", x);
}
}
|
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;
void main()
{
auto s = readln.chomp;
auto abc = "ABC";
auto mod = 10 ^^ 9 + 7;
long[][4] dp;
foreach (ref row; dp)
row = new long[](s.length + 1);
dp[3][$ - 1] = 1;
foreach_reverse (i; 0 .. s.length)
if (s[i] == '?')
dp[3][i] = dp[3][i + 1] * 3 % mod;
else
dp[3][i] = dp[3][i + 1];
foreach_reverse (i; 0 .. s.length)
foreach (j; 0 .. 3)
{
if (s[i] == '?')
dp[j][i] = dp[j][i + 1] * 3 % mod;
else
dp[j][i] = dp[j][i + 1];
if (s[i] == '?' || s[i] == abc[j])
dp[j][i] = (dp[j][i] + dp[j + 1][i + 1]) % mod;
}
// foreach (r; dp)
// writeln(r);
dp[0][0].writeln;
}
|
D
|
import std.stdio;
import std.array;
import std.conv;
import std.algorithm;
struct gom{
int x = -1;
int depth=-1;
this(int a,int b){
x = a;
depth = b;
}
}
gom[][5000] kugi;
void main(){
string[] fi = readln().split();
int n = to!int(fi[0]);
int m = to!int(fi[1]);
for(int i = 0;i < m;++i){
string[] input = readln().split();
int a = to!int(input[0]);
int b = to!int(input[1]);
int c = to!int(input[2]);
kugi[a-1] ~= gom(b-1,c);
}
int res = 0;
int[] now = new int[5001];
int[] next = new int[5001];
now[] = -1;
next[] = -1;
for(int i = 0;i < n;++i){
foreach(gom k;kugi[i]){
now[k.x] = max(now[k.x],k.depth);
}
foreach(int x,int k;now[0..i+1]){
if(k >= 0){
++res;
}
next[x] = max(next[x],k-1);
next[x+1] = max(next[x+1],k-1);
}
int[] tmp = now;
now = next;
next = tmp;
next[] = -1;
}
writeln(res);
}
|
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.to!(int[]); }
bool calc(long[] as, long[] bs) {
auto asum = as.sum;
auto bsum = bs.sum;
if (asum > bsum) return false;
long r = 0;
for (int i = 0; i < as.length; i++) {
r += max(0, as[i] - bs[i]);
}
if (asum + 2 * r > bsum + r) return false;
for (int i = 0; i < as.length; i++) {
long x = bs[i] - as[i];
if (x > 0) {
r -= x / 2;
}
}
return r <= 0;
}
void main() {
readint;
auto as = readln.split.to!(long[]);
auto bs = readln.split.to!(long[]);
writeln(calc(as, bs) ? "Yes" : "No");
}
|
D
|
// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
immutable mod = 10^^9 + 7;
immutable inf = 10^^9 + 7;
void main() {
int n;
int[] a;
scan(n);
a = readln.split.to!(int[]);
auto cnt = new int[](200000);
foreach (ai ; a) {
cnt[ai]++;
}
int ans;
foreach (i ; 0 .. 100000) {
int b = cnt[i] + cnt[i + 1] + cnt[i + 2];
ans = max(ans, b);
}
writeln(ans);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.conv, std.string, std.math, std.regex, std.range, std.ascii, std.algorithm;
void main(){
auto N = readln.chomp.to!int;
if(N <= 999) writeln("ABC");
else writeln("ABD");
}
|
D
|
import std.stdio;
void main() {
auto s = readln;
auto r = (s[0] == s[1] && s[0] == s[2]) ? "No" : "Yes";
writeln(r);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
long P = 10^^9 + 7;
void main()
{
auto nk = readln.split.to!(int[]);
auto N = nk[0];
auto K = nk[1];
auto as = readln.split.to!(int[]);
long[][] DP;
DP.length = N;
foreach_reverse (i, ref dp; DP) {
dp.length = K+1;
long[] cs;
if (i+1 != N) {
cs.length = K+1;
cs[0] = DP[i+1][0];
foreach (k; 1..K+1) cs[k] = (DP[i+1][k] + cs[k-1]);
}
foreach (j; 0..K+1) {
DP[i][j] = i+1 == N ? (j <= as[i] ? 1 : 0) : (j <= as[i] ? cs[j] : cs[j] - cs[j - as[i] - 1]) % P;
}
}
writeln(DP[0][K]);
}
|
D
|
// Vicfred
// https://atcoder.jp/contests/abc045/tasks/abc045_b
import std.array;
import std.stdio;
import std.string;
import std.range.primitives;
void main() {
string a = readln.strip;
string b = readln.strip;
string c = readln.strip;
char[] A;
foreach(ch; a)
A ~= ch;
char[] B;
foreach(ch; b)
B ~= ch;
char[] C;
foreach(ch; c)
C ~= ch;
char[][char] deck;
deck['a'] = A;
deck['b'] = B;
deck['c'] = C;
char current = 'a';
while(deck[current].length > 0) {
char temp = deck[current][0];
deck[current].popFront;
current = temp;
}
if(current == 'a')
"A".writeln;
if(current == 'b')
"B".writeln;
if(current == 'c')
"C".writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main() {
int[] buf = readln.chomp.split.to!(int[]);
int a = buf[0], p = buf[1];
writeln((a * 3 + p) / 2);
}
|
D
|
import std.stdio;
import std.array;
import std.conv;
import std.algorithm;
import std.math;
void main() {
string s;
while( (s=readln()).length != 0 ){
string[] input = split(s);
int a = to!int(input[0]);
int x= 0;
for(int i=0;i<10;i++){
if(a%2==1){
if(x!=0) write(" ");
write(pow(2,i));
x++;
}
a/=2;
}
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;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n; string s;
scan(n);
scan(s);
auto cnt = new int[](26);
foreach (ch ; s) {
cnt[ch - 'a']++;
}
long ans = 1;
foreach (i ; 0 .. 26) {
ans *= cnt[i] + 1;
ans %= mod;
}
ans += mod - 1;
ans %= mod;
writeln(ans);
}
int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new int[][](n, 0);
foreach (i; 0 .. m) {
int u, v;
scan(u, v);
if (is1indexed) {
u--, v--;
}
adj[u] ~= v;
if (isUndirected) {
adj[v] ~= u;
}
}
return adj;
}
alias Edge = Tuple!(int, "to", int, "cost");
Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new Edge[][](n, 0);
foreach (i; 0 .. m) {
int u, v, c;
scan(u, v, c);
if (is1indexed) {
u--, v--;
}
adj[u] ~= Edge(v, c);
if (isUndirected) {
adj[v] ~= Edge(u, c);
}
}
return adj;
}
void yes(bool b) {
writeln(b ? "Yes" : "No");
}
void YES(bool b) {
writeln(b ? "YES" : "NO");
}
T[] readArr(T)() {
return readln.split.to!(T[]);
}
T[] readArrByLines(T)(int n) {
return iota(n).map!(i => readln.chomp.to!T).array;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.stdio;
import std.string;
void main()
{
string input;
int count = 0;
for (int i = 1; ; i++) {
input = chomp(readln());
if (input == "0") break;
writeln("Case ", i, ": ", input);
}
}
|
D
|
import std.stdio;
import std.string;
import std.algorithm;
import std.range;
import std.conv;
import std.math;
void main() {
int n;
scanf("%d\n", &n);
auto arr = readln.split.to!(int[]);
int max = int.min;
int min = int.max;
foreach(x; arr) {
max = x > max ? x : max;
min = x < min ? x : min;
}
writeln(max-min);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.array;
void main()
{
ulong N = readln.split[0].to!ulong;
string S = readln.chomp;
S = "." ~ S ~ "#";
int switch_i = 0;
while (S[switch_i] == '.') switch_i++;
int cnt = 0;
foreach (i; switch_i..(N + 2)) if (S[i] == '.') cnt++;
int ans = cnt;
int cnt0 = 0;
int cnt1 = 0;
foreach (i; switch_i..(N + 1)){
if (S[i] == '.') cnt0++;
else cnt1++;
if (S[i] == '.' && S[i + 1] == '#'){
cnt = cnt + cnt1 - cnt0;
if (cnt < ans) ans = cnt;
cnt0 = 0;
cnt1 = 0;
}
}
writeln(ans);
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.math;
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);
int[] a; readA(n, a);
auto mi = a.reduce!min, ma = a.reduce!max;
auto b = mi.abs > ma.abs ? mi : ma, i = a.countUntil(b);
if (b == 0) {
writeln(0);
} else if (b > 0) {
writeln(n*2-2);
writeln(i+1, " ", 2);
writeln(i+1, " ", 2);
foreach (j; 2..n) {
writeln(j, " ", j+1);
writeln(j, " ", j+1);
}
} else {
writeln(n*2-2);
writeln(i+1, " ", n-1);
writeln(i+1, " ", n-1);
foreach_reverse (j; 0..n-2) {
writeln(j+2, " ", j+1);
writeln(j+2, " ", j+1);
}
}
}
|
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; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
const long infty = 1_000_000_000_000_000_100;
void solve(){
int n = read.to!int;
long[] as;
foreach(i; 0 .. n) as ~= read.to!long;
long calc(long x, long y, int l, int r){
if(l + 1 == r) return 0;
long res = infty;
foreach(i; l + 1 .. r){
long temp = calc(x, x + y, l, i) + (x + y) * as[i] + calc(x + y, y, i, r);
res = min(res, temp);
}
return res;
}
long ans = as[0] + calc(1, 1, 0, n - 1) + as[n - 1];
ans.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;
long MOD = 10^^9 + 7;
void main() {
auto N = readln.chomp;
auto K = readln.chomp.to!int;
auto dp = new int[](1001);
dp[1] = 1;
for (int i = 2; i <= 1000; ++i) {
dp[i] = dp[i.popcnt] + 1;
}
auto dp2 = new long[][][](N.length+1, N.length+1, 2);
dp2[0][0][0] = 1;
foreach (i; 0..N.length) {
foreach (j; 0..N.length+1) {
foreach (k; 0..2) {
int digit = (k || N[i] == '1') ? 2 : 1;
foreach (d; 0..digit) {
if (j+d <= N.length)
(dp2[i+1][j+d][k||(d < N[i]-'0')] += dp2[i][j][k]) %= MOD;
}
}
}
}
long ans = 0;
foreach (i; 1..N.length+1) {
if (dp[i] == K) {
(ans += dp2[N.length][i][0]) %= MOD;
(ans += dp2[N.length][i][1]) %= MOD;
}
}
if (K == 1) {
ans -= 1;
ans %= MOD;
} else if (K == 0) {
ans = 1;
}
ans.writeln;
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int n;
rd(n);
auto a = readln.split.to!(int[]);
if (reduce!"a+b"(a) > 0) {
writeln("HARD");
} else {
writeln("EASY");
}
}
void rd(T...)(ref T x) {
import std.stdio, std.string, std.conv;
auto l = readln.split;
assert(l.length == x.length);
foreach (i, ref e; x)
e = l[i].to!(typeof(e));
}
|
D
|
import std.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;
// 差の絶対値
@nogc @safe pure T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; }
// 切り上げ除算
@nogc @safe pure T divCeil(T)(const ref T a, const ref T b) {
return (a+b-1)/b;
}
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 m, d;
readInto(m, d);
ulong cnt = 0;
for(ulong i = 1; i <= m; i++) {
for (ulong j = 1; j <= d; j++) {
ulong d1 = j%10;
ulong d2 = j/10;
if (d1 >= 2 && d2 >= 2 && d1*d2 == i) {
cnt++;
}
}
}
writeln(cnt);
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.