code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int a;
rd(a);
int digit_sum(int x) {
int ret = 0;
while (x) {
ret += x % 10;
x /= 10;
}
return ret;
}
for (int n = a;; n++) {
if (digit_sum(n) % 4 == 0) {
writeln(n);
break;
}
}
}
void rd(T...)(ref T x) {
import std.stdio : readln;
import std.string : split;
import std.conv : to;
auto l = readln.split;
assert(l.length == x.length);
foreach (i, ref e; x)
e = l[i].to!(typeof(e));
}
|
D
|
import std.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()
{
auto rd = readln.split.map!(to!int);
auto a = rd[0], b = rd[1];
writeln(a * b, " ", (a + b) * 2);
}
|
D
|
import std.stdio;
import std.string;
import std.array;
import std.range;
import std.algorithm;
import std.conv;
void main(string[] args) {
readln();
string input = readln().chomp;
int[2][char] range;
foreach(int i, char c; input) {
if(c in range){
range[c][1] = i;
}else{
range[c] = [i,0];
}
}
int[100] res;
foreach(char c, int[2] a; range){
for(int i = a[0]; i < a[1]; i++){
res[i]++;
}
}
res.reduce!max.writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
import std.range;
import std.math;
void main(){
auto z=readln.split.to!(int[]),a=z[0],b=z[1];
if(a<=b)writeln(a);
else writeln(a-1);
}
|
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 t = RD!int;
auto ans = new bool[](t);
foreach (i; 0..t)
{
auto x = RD;
auto y = RD;
auto d = x - y;
if (d == 1)
ans[i] = false;
else
ans[i] = true;
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush();
debug readln();
}
|
D
|
import std;
enum inf(T)()if(__traits(isArithmetic,T)){return T.max/4;}
T scan(T=long)(){return readln.chomp.to!T;}
void scan(T...)(ref T args){auto input=readln.chomp.split;foreach(i,t;T)args[i]=input[i].to!t;}
T[] scanarr(T=long)(){return readln.chomp.split.to!(T[]);}
//END OF TEMPLATE
void main(){
long a,b,c,k;
scan(a,b,c,k);
if(k<=a){
k.writeln;
}else if(k<=a+b){
a.writeln;
}else{
(a-(k-a-b)).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[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
auto A = lreads(N);
writeln(A.any!"(a&1)!=0" ? "first" : "second");
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
bool calc(string s) {
if (s.startsWith("keyence")) return true;
if (s.startsWith("k") && s.endsWith("eyence")) return true;
if (s.startsWith("ke") && s.endsWith("yence")) return true;
if (s.startsWith("key") && s.endsWith("ence")) return true;
if (s.startsWith("keye") && s.endsWith("nce")) return true;
if (s.startsWith("keyen") && s.endsWith("ce")) return true;
if (s.startsWith("keyenc") && s.endsWith("e")) return true;
if (s.endsWith("keyence")) return true;
return false;
}
void main() {
string s = read!string;
writeln(calc(s) ? "YES" : "NO");
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
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[] arr;
arr ~= RD;
arr ~= RD;
arr ~= RD;
arr ~= RD;
arr ~= RD;
long ans;
long cnt;
foreach (i; 0..5)
{
if (i != 0)
{
arr[i] = min(arr[i], arr[i-1]);
}
if (i == 4)
{
cnt += N / arr[i];
cnt += N % arr[i] != 0 ? 1 : 0;
}
}
writeln(cnt + 4);
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;
void main()
{
auto b = iota(42, 133, 10);
auto ptr = new int[](10);
auto str = readln.chomp;
int pos;
foreach (c; str) {
int cur;
foreach (e; b) {
if (c <= e) break;
cur++;
}
while (cur < pos) {
write("<");
pos--;
}
while (cur > pos) {
write(">");
pos++;
}
while (ptr[pos] < c) {
write("+");
ptr[pos]++;
}
while (ptr[pos] > c) {
write("-");
ptr[pos]--;
}
write(".");
}
}
|
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;
string ans;
foreach (c; S)
{
if (c == 'B')
{
if (ans.length != 0)
ans.popBack;
}
else
ans ~= c;
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
int n;
scan(n);
auto adj = new int[][](n, 0);
foreach (i ; 0 .. n-1) {
int ai,bi;
scan(ai,bi);
adj[ai] ~= bi;
adj[bi] ~= ai;
}
int dfs(int v, int p) {
if (adj[v].length == 1) {
return 0;
}
int res, dmax;
bool zero;
foreach (u ; adj[v]) {
if (u == p) continue;
int d = dfs(u, v);
if (!d) {
zero = 1;
d++;
}
res += d;
}
if (zero) res--;
return res;
}
int ans = 1;
foreach (i ; 0 .. n) {
if (adj[i].length > 2) {
ans = dfs(i, -1);
break;
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array, std.functional;
void main() {
auto N = readln.split[0].to!int;
auto h = readln.split.to!(int[]);
h ~= 0;
auto res = 0;
while(h.reduce!max > 0) {
foreach(i; 0..N) {
if(h[i] > 0) {
h[i]--;
if(h[i+1] == 0) res++;
}
}
}
res.writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
void main()
{
readln;
auto AS = readln.split.to!(int[]);
long ret;
foreach (a; AS) {
while (!(a&1)) {
a >>= 1;
++ret;
}
}
writeln(ret);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nxy = readln.split.to!(int[]);
auto N = nxy[0];
auto X = nxy[1]-1;
auto Y = nxy[2]-1;
auto G = new int[][](N, N);
auto memo = new int[](N);
void run(int i, int p, int n) {
if (memo[i] < n) return;
if (memo[i] < N) G[i][memo[i]] -= 1;
memo[i] = n;
G[i][n] += 1;
if (i > 0 && p != i-1) run(i-1, i, n+1);
if (i < N-1 && p != i+1) run(i+1, i, n+1);
if (i == X && p != Y) run(Y, i, n+1);
if (i == Y && p != X) run(X, i, n+1);
}
foreach (i; 0..N) {
memo[] = int.max;
run(i, -1, 0);
}
auto rr = new int[](N);
foreach (i; 0..N) {
foreach (j; 1..N) {
rr[j] += G[i][j];
}
}
foreach (r; rr[1..$]) writeln(r/2);
}
|
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 p = readln.chomp.split.to!(int[]);
auto q = readln.chomp.split.to!(int[]);
int[][] ar;
auto f = new int[](n);
f[] = false;
void calc(int[] a) {
if (a.length == n) {
ar ~= a;
return;
}
foreach (i; 0..n) {
if (f[i]) continue;
f[i] = true;
calc(a ~ (i + 1));
f[i] = false;
}
}
int[] tmp;
calc(tmp);
int pr, qr;
foreach (int i; 0..ar.length.to!int) {
if (p == ar[i]) {
pr = i + 1;
}
if (q == ar[i]) {
qr = i + 1;
}
}
abs(pr - qr).writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
void main(){
auto ip = readln.split, s = ip[1];
writeln("A", s[0], "C");
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto xy = readln.split.to!(int[]);
auto X = xy[0];
auto Y = xy[1];
writeln(X + Y / 2);
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto s = readln.chomp;
auto p = readln.chomp;
foreach (i; 0..s.length)
if ((s[i..$] ~ s[0..i]).canFind(p)) {
writeln("Yes");
return;
}
writeln("No");
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
bool calc(string s) {
for (int i = 0; i < s.length; i++) {
if (i % 2 == 0) {
if (s[i] == 'L') return false;
}
else if (s[i] == 'R') return false;
}
return true;
}
void main() {
string s = read!string;
writeln(calc(s) ? "Yes" : "No");
}
|
D
|
void main(){
int k,a,b;
k = _scan();
scanf("%d %d", &a, &b);
for(int i=a; i<=b; i++){
if( i%k==0 ){
writeln("OK");
return;
}
}
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.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
import std.concurrency;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;
void main() {
int[] as = readln.chomp.map!(a => a=='1'?1:0).array;
if (as[0]==1) {
foreach(i, a; as) {
as[i] = a==1?0:1;
}
}
int N = as.length.to!int;
bool f(int c) {
int[] bs = as.dup;
int k = 0;
foreach(i; 0..c) {
if ((bs[i]+k)%2==1 && i+c<=N) {
k++;
}
if ((bs[i]+k)%2==0) {
} else {
return false;
}
}
return true;
}
int l = 0;
int r = N+1;
while(r-l>1) {
int c = (r + l)/2;
if (f(c)) {
l = c;
} else {
r = c;
}
}
l.writeln;
}
// ----------------------------------------------
void scanln(Args...)(ref Args args) {
foreach(i, ref v; args) {
"%d".readf(&v);
(i==args.length-1 ? "\n" : " ").readf;
}
// ("%d".repeat(args.length).join(" ") ~ "\n").readf(args);
}
void times(alias fun)(int n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
return reduce!fun(tuple(seed), r);
}
}
}
}
// cumulativeFold was added in D 2.072.0
static if (__VERSION__ < 2072) {
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
}
// minElement/maxElement was added in D 2.072.0
static if (__VERSION__ < 2072) {
auto minElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto minimum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b < minimum) {
element = a;
minimum = b;
}
}
return element;
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto maximum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b > maximum) {
element = a;
maximum = b;
}
}
return element;
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
void main() {
int n;
scan(n);
int pmax = 0;
int ps = 0;
foreach (i ; 0 .. n) {
int pi;
scan(pi);
ps += pi;
pmax = max(pmax, pi);
}
ps -= pmax / 2;
writeln(ps);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.conv, std.array;
import std.math;
void main(){
double r = readln.split[0].to!double;
r = 2 * PI * r;
printf("%lf\n",r);
}
|
D
|
import std.stdio, std.string, std.conv, std.range, std.array, std.algorithm;
import std.uni, std.math, std.container, std.typecons, std.typetuple;
import core.bitop, std.datetime;
immutable long mod = 10^^9 + 7;
alias Pair = Tuple!(int, "l", int, "x");
void main(){
int N, M;
int li, ri, xi;
readVars(N, M);
auto cond = new Pair[][](N + 1);
foreach(i ; 0 .. M){
readVars(li, ri, xi);
cond[ri] ~= Pair(li, xi);
}
//writeln(cond);
auto dp = new int[][][](N + 1, N + 1, N + 1);
dp[0][0][0] = 1;
int k, v, ans;
foreach(r ; 0 .. N + 1){
foreach(g ; 0 .. N + 1){
foreach(b ; 0 .. N + 1){
if (!dp[r][g][b]) {
continue;
}
k = max(r, g, b);
bool flag;
foreach(c ; cond[k]){
v = (c.l <= r) + (c.l <= g) + (c.l <= b);
if (v != c.x) {
flag = true;
break;
}
}
if (flag) {
dp[r][g][b] = 0;
continue;
}
if (k == N) {
(ans += dp[r][g][b]) %= mod;
continue;
}
dp[k + 1][g][b] = (dp[k + 1][g][b] + dp[r][g][b]) % mod;
dp[r][k + 1][b] = (dp[r][k + 1][b] + dp[r][g][b]) % mod;
dp[r][g][k + 1] = (dp[r][g][k + 1] + dp[r][g][b]) % mod;
}
}
}
writeln(ans);
}
void readVars(T...)(auto ref T args){
auto line = readln.split;
foreach(ref arg ; args){
arg = line.front.to!(typeof(arg));
line.popFront;
}
if(!line.empty){
throw new Exception("args num < input num");
}
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
const tmp = readln.split.to!(int[]);
const N = tmp[0], M = tmp[1];
auto res = new int[M];
foreach (i; 0..N) {
auto input = readln.split.to!(int[]);
input[1..$].each!(a => res[a-1]++);
}
res.count!(r => r == N).writeln;
}
|
D
|
void main() {
dchar[] n = readln.chomp.to!(dchar[]);
auto g = n.group.array;
writeln(g.any!(t => t[1] >= 3) ? "Yes" : "No");
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
int sum, tmp;
foreach (i; 0..5) {
auto o = readln.chomp.to!int;
if (o % 10 == 0) {
sum += o;
} else {
auto t = 10 - o % 10;
tmp = max(t, tmp);
sum += o + t;
}
}
writeln(sum - tmp);
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
void main()
{
long a, b;
scan(a, b);
writeln(a * b);
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons, std.functional;
enum N = 1_000_000_007;
void main() {
const tmp = readln.split.to!(int[]);
const H = tmp[0], W = tmp[1], K = tmp[2];
ulong _countCase(int x) {
if (x <= 0) return 1;
return memoize!_countCase(x-1) + memoize!_countCase(x-2);
}
alias countCase = memoize!_countCase;
ulong func(int current, int cnt) {
if (cnt == H) return current == K ? 1 : 0;
ulong res = 0;
res += memoize!func(current, cnt+1) * countCase(current-2) * countCase(W-current-1);
if (1 < current) res += memoize!func(current-1, cnt+1) * countCase(current-3) * countCase(W-current-1);
if (current < W) res += memoize!func(current+1, cnt+1) * countCase(current-2) * countCase(W-current-2);
return res % N;
}
writeln(func(1, 0));
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
auto S = new string[](N);
foreach (i; 0 .. N)
S[i] = sread();
long ans;
outer: foreach (i; 0 .. N)
{
foreach (j; 0 .. N)
foreach (k; 0 .. N)
{
if (S[(j + i) % N][k] != S[(k + i) % N][j])
{
// writefln("%s %s %s %s %s", i, j, k, S[(j + i) % N][k], S[k][(j + i) % N]);
continue outer;
}
}
ans += N;
}
writeln(ans);
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n; rd(n);
auto a=new int[][](2, n);
a[0]=readln.split.to!(int[]);
a[1]=readln.split.to!(int[]);
int mx=0;
foreach(i; 0..n){
int sub=0;
for(auto j=0; j<=i; j++) sub+=a[0][j];
for(auto j=i; j<n; j++) sub+=a[1][j];
mx=max(mx, sub);
}
writeln(mx);
}
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.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
void main() {
auto S = readln.chomp;
auto L = S.length;
int distinct_count;
foreach(i; 0..L/2) {
if (S[i] != S[L-i-1]) distinct_count++;
}
writeln(distinct_count);
}
|
D
|
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
void main() {
long[][] matrix;
bool[long] numbers;
foreach(i; 0..3) {
auto line = readln.split.to!(long[]);
matrix ~= line;
foreach(l; line) numbers[l] = false;
}
foreach(i; 0..readln.chomp.to!long) {
auto n = readln.chomp.to!long;
numbers[n] = true;
}
bool solve() {
bool[][] comb;
foreach(i; 0..3) {
comb ~= [numbers[matrix[0][i]], numbers[matrix[1][i]], numbers[matrix[2][i]]];
comb ~= [numbers[matrix[i][0]], numbers[matrix[i][1]], numbers[matrix[i][2]]];
}
comb ~= [numbers[matrix[0][0]], numbers[matrix[1][1]], numbers[matrix[2][2]]];
comb ~= [numbers[matrix[0][2]], numbers[matrix[1][1]], numbers[matrix[2][0]]];
foreach(c; comb) {
if (c.all!(x => x == true)) return true;
}
return false;
}
writeln(solve() ? "Yes" : "No");
}
|
D
|
void main() {
auto S = rs;
if(S[2] == S[3] && S[4] == S[5]) writeln("Yes");
else writeln("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.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n; readV(n);
auto g = GraphW!(int, long)(n);
foreach (_; 0..n-1) {
int a, b, c; readV(a, b, c); --a; --b;
g.addEdgeB(a, b, c);
}
int q, k; readV(q, k); --k;
auto d = new long[](n), vst = new bool[](n);
struct VD { int v; long d; }
auto qu = DList!int(k);
vst[k] = true;
while (!qu.empty) {
auto v = qu.front; qu.removeFront();
foreach (e; g[v].filter!(e => !vst[e.dst])) {
d[e.dst] = d[v] + e.wt;
vst[e.dst] = true;
qu.insertBack(e.dst);
}
}
foreach (_; 0..q) {
int x, y; readV(x, y); --x; --y;
writeln(d[x]+d[y]);
}
}
struct GraphW(N = int, W = int, W i = 10^^9)
{
alias Node = N, Wt = W, inf = i;
struct Edge { Node src, dst; Wt wt; alias cap = wt; }
Node n;
Edge[][] g;
alias g this;
this(Node n) { this.n = n; g = new Edge[][](n); }
void addEdge(Node u, Node v, Wt w) { g[u] ~= Edge(u, v, w); }
void addEdgeB(Node u, Node v, Wt w) { g[u] ~= Edge(u, v, w); g[v] ~= Edge(v, u, w); }
}
|
D
|
import std.stdio,std.array;
void main(){
string[] n = split(readln());
int sevenCount = 0;
for(int i=0;i<3;i++){
if("7" == n[i]) sevenCount++;
}
int fiveCount =0;
for(int i=0;i<3;i++){
if("5" == n[i]) fiveCount++;
}
if(fiveCount == 2 && sevenCount == 1){
writeln("YES");
}else{
writeln("NO");
}
}
|
D
|
void main() {
auto N = ri;
auto S = rs;
auto K = ri;
auto c = S[K-1];
foreach(v; S) {
if(v == c) c.write;
else '*'.write;
}
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
|
void main() {
int n = readln.chomp.to!int;
string s = readln.chomp;
string t1 = s[0..n/2], t2 = s[n/2..$];
writeln(n % 2 == 0 && t1 == t2 ? "Yes" : "No");
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio,std.string,std.conv;
int main()
{
string s;
while((s = readln.chomp).length != 0)
{
double d = s.to!double;
if(91.00 < d) "heavy".writeln;
if(81.00 < d && d <= 91.00) "light heavy".writeln;
if(75.00 < d && d <= 81.00) "middle".writeln;
if(69.00 < d && d <= 75.00) "light middle".writeln;
if(64.00 < d && d <= 69.00) "welter".writeln;
if(60.00 < d && d <= 64.00) "light welter".writeln;
if(57.00 < d && d <= 60.00) "light".writeln;
if(54.00 < d && d <= 57.00) "feather".writeln;
if(51.00 < d && d <= 54.00) "bantam".writeln;
if(48.00 < d && d <= 51.00) "fly".writeln;
if(d <= 48.00) "light fly".writeln;
}
return 0;
}
|
D
|
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.typecons, std.range, std.container, std.math, std.algorithm.searching, std.functional,std.mathspecial;
void main(){
auto n=readln().chomp.to!int;
auto k=readln().chomp.to!int;
auto x=readln().chomp.to!int;
auto y=readln().chomp.to!int;
writeln(min(n,k)*x+max(0,n-k)*y);
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
// dfmt on
void main()
{
long H, W;
scan(H, W);
long[2][] black;
auto A = new bool[][](H, W);
foreach (i; 0 .. H)
{
auto s = sread();
foreach (j, c; s)
if (c == '#')
{
black ~= [i, j];
A[i][j] = true;
}
}
long cnt;
while (true)
{
long[2][] tmp;
foreach (b; black)
{
long y = b[0];
long x = b[1];
if (0 < y && !A[y - 1][x])
{
A[y - 1][x] = true;
tmp ~= [y - 1, x];
}
if (0 < x && !A[y][x - 1])
{
A[y][x - 1] = true;
tmp ~= [y, x - 1];
}
if (y + 1 < H && !A[y + 1][x])
{
A[y + 1][x] = true;
tmp ~= [y + 1, x];
}
if (x + 1 < W && !A[y][x + 1])
{
A[y][x + 1] = true;
tmp ~= [y, x + 1];
}
}
if (tmp.empty)
{
writeln(cnt);
return;
}
else
{
cnt++;
black = tmp;
}
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
iota(2).map!(i => readln.chomp.to!int).reduce!((a, b) => a^^2 - b).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);
}
}
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
auto S = readln.chomp;
auto T = readln.chomp;
foreach (i; 0..S.length) {
if (S[i..$] ~ S[0..i] == T) {
writeln("Yes");
return;
}
}
writeln("No");
}
|
D
|
import std.stdio;
import std.math;
import std.conv;
void main(){
int n;
scanf("%d", &n);
int digit = log10(n.to!float).floor.to!int;
if (digit == 0) {
write(n);
}
else if (digit == 1) {
write(9);
}
else if (digit == 2) {
write(9 + (n-99));
}
else if (digit == 3) {
write(9 + 900);
}
else if (digit == 4) {
write(9 + 900 + (n-9999));
}
else {
write(9 + 900 + 90000);
}
}
|
D
|
import std.stdio;
import std.string;
import std.algorithm;
import std.conv;
void main()
{
int A, B, r, K;
auto input = readln.split.to!(int[]);
A = input[0];
B = input[1];
K = input[2];
while(A%B)
{
r = A%B;
A = B;
B = r;
}
int[] ans = new int[100];
ans[0] = B;
int j = 1;
for(int i = 2; i <= B; i++) {
if(B%i == 0)
{
ans[j] = B / i;
j++;
}
}
writeln(ans[K-1]);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
// 0, 1, 2, ..., n の xor を求める
long f(long n) {
if (n <= 0) return 0;
long ans = 0;
for (long i = n / 4 * 4; i <= n; i++) {
ans ^= i;
}
return ans;
}
long calc(long a, long b) {
return f(a - 1) ^ f(b);
}
void main() {
auto ab = reads!long;
long a = ab[0], b = ab[1];
auto ans = calc(a, b);
writeln(ans);
}
|
D
|
import std.stdio;
void main() {
foreach (i; 1..10) {
foreach (j; 1..10) {
write(i); write("x"); write(j); write("="); writeln(i*j);
}
}
}
|
D
|
import std.stdio;
void main(){
foreach(x;1..10)foreach(y;1..10)writeln(x,"x",y,"=",x*y);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.array;
import std.algorithm;
immutable MAX_SCORE = 20;
immutable MAX_CARDS = 10;
void main() {
string[] input;
while ((input = readln.chomp.split(" ")).length != 0) {
auto cards = new bool[](10); //未出現カード
auto openedCards = input.map!(to!int); //0,1:自分の手札, 2:相手の公開札
auto deck = MAX_CARDS - (openedCards.length); //山札の残り枚数
auto myScore = openedCards[0] + openedCards[1]; //現在の手札の合計
auto count = 0;
cards.fill(true);
foreach (i; openedCards) {
cards[i - 1] = false;
}
foreach (i; 0..(MAX_SCORE - myScore)) {
if (i >= 10) break;
if (cards[i]) count++;
}
if ((count.to!double / deck) >= 0.50) {
writeln("YES");
} else {
writeln("NO");
}
}
}
|
D
|
import std.stdio, std.conv, std.array, std.string;
import std.algorithm;
import std.container;
void main() {
auto S = readln.chomp;
auto T = readln.chomp;
int correct_count = 0;
for(int i=0; i<S.length; i++) {
if (S[i] == T[i]) correct_count++;
}
writeln(correct_count);
}
|
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, std.datetime;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long R, G, B, N;
scan(R, G, B, N);
long ans;
foreach (r; 0 .. N / R + 1)
{
foreach (g; 0 .. (N - r * R) / G + 1)
{
if ((N - r * R - g * G) % B == 0)
{
// writefln("%s %s %s", r, g, (N - r * R - g * G) / B);
ans++;
}
}
}
writeln(ans);
}
|
D
|
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container;
import std.numeric, std.math;
import core.bitop;
T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
long mod = pow(10, 9) + 7;
long moda(long x, long y) { return (x + y) % mod; }
long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; }
long modm(long x, long y) { return (x * y) % mod; }
void main()
{
auto N = RD!ulong;
auto K = RD!ulong;
auto A = RDR.split.to!(ulong[]);
ulong[64] bitCnt;
foreach (e; A)
{
foreach (i; 0..64)
{
if (e & (cast(ulong)1 << i)) ++bitCnt[i];
}
}
auto bitQ = new ulong[](64);
fill(bitQ, ulong.max);
foreach (ulong i; 0..64)
{
if (bitCnt[i] > N / 2) continue;
bitQ[i] = ((N - bitCnt[i]) - bitCnt[i]) * (cast(ulong)1 << i);
}
//stderr.writeln(bitQ);
ulong x;
ulong ans;
foreach_reverse (ulong i, e; bitQ)
{
if (bitCnt[i] > N / 2) continue;
ulong bit = cast(ulong)1 << i;
//stderr.writeln(x, " a ", i , " ", bit);
x += bit;
if (x > K)
{
ulong best = ulong.max, bestPos;
/*foreach (j, ee; bitQ[i..$])
{
if (ee < best)
{
best = ee;
bestPos = i + j;
}
}*/
foreach_reverse (ulong j; i..64)
{
if (x & (cast(ulong)1 << j))
{
if (bitQ[j] < best)
{
best = bitQ[j];
bestPos = j;
}
}
}
x -= cast(ulong)1 << bestPos;
//stderr.writeln(x, " ", i, " ", bestPos);
}
else
{
//stderr.writeln("else");
}
}
foreach (e; A)
{
ans += e ^ x;
}
writeln(ans);
stdout.flush();
}
|
D
|
// Vicfred
// https://atcoder.jp/contests/abc152/tasks/abc152_c
// greedy
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;
void main() {
int n = readln.chomp.to!int;
int[] p = readln.split.map!(to!int).array;
int minima = 1<<30;
int answer = 0;
foreach(item; p) {
if(item <= minima) {
minima = item;
answer += 1;
}
}
answer.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 M = s[0].to!long;
auto N = s[1];
auto P = new int[](N);
long x;
int[] Q;
int debug_cnt = 0;
debug {
x = readln.chomp.to!long;
Q = N.iota.map!(_ => readln.chomp.to!int).array;
}
int ask(long y) {
writeln(y);
stdout.flush;
debug {
if (x < y) return -1 * Q[debug_cnt];
else if (x > y) return 1 * Q[debug_cnt];
else return 0;
++debug_cnt;
}
return readln.chomp.to!int;
}
foreach (i; 0..N) {
P[i] = ask(1);
if (P[i] == 0)
return;
}
long hi = M+1;
long lo = 0;
int cnt = 0;
while (hi - lo > 1) {
long mid = (hi + lo) / 2;
int ret = ask(mid) * P[cnt%N];
if (ret == 1) {
lo = mid;
} else if (ret == -1) {
hi = mid;
} else {
return;
}
++cnt;
}
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
immutable long MOD = 10^^9+7;
void main() {
auto N = readln.chomp.to!int;
auto S = N.iota.map!(_ => readln.chomp).array;
auto cnts = new int[](N);
auto mns = new int[](N);
int[int] mn;
foreach (i, s; S.enumerate) {
int cnt = 0;
int minv = 0;
foreach (c; s) {
cnt += c == '(' ? 1 : -1;
minv = min(minv, cnt);
}
cnts[i] = cnt;
mns[i] = minv;
if (minv == cnts[i]) mn[minv] += 1;
}
long ans = 0;
foreach (i; 0..N) {
if (mns[i] < 0) continue;
if (-cnts[i] !in mn) continue;
ans += mn[-cnts[i]];
}
ans.writeln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
T binarySearch(alias pred, T)(T ok, T ng)
{
while (abs(ok-ng) > 1)
{
auto mid = (ok+ng)/2;
if (unaryFun!pred(mid)) ok = mid;
else ng = mid;
}
return ok;
}
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (ti; 0..t)
{
auto d = RD;
auto k = RD;
bool f(long x)
{
return ((x*k)^^2)*2 <= d^^2;
}
auto r = binarySearch!(f)(0, d/k+1);
debug writeln("r:", r);
if (r == 0)
ans[ti] = true;
else if ((r*k)^^2 + ((r+1)*k)^^2 <= d^^2)
{
ans[ti] = true;
}
}
foreach (e; ans)
{
writeln(e ? "Ashish" : "Utkarsh");
}
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(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 long[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto q = RD!int;
auto a = RDA;
auto dp = new long[][](n+1, 2);
foreach (i; 0..n)
{
dp[i+1][0].chmax(dp[i][0]);
dp[i+1][1].chmax(dp[i][1]);
dp[i+1][0].chmax(dp[i][1] - a[i]);
dp[i+1][1].chmax(dp[i][0] + a[i]);
}
ans[ti] = max(dp[n][0], dp[n][1]);
}
foreach (e; ans)
{
writeln(e);
}
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(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 b = RDA;
bool[long] set;
foreach (i; 0..n)
{
if (set.get(b[i], false))
{
ans[ti] = true;
break;
}
set[b[i]] = true;
}
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush;
debug readln;
}
|
D
|
//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.random;
void main() {
immutable n = readln.strip.to!int;
auto s = readln.take(n).array;
int[] st;
auto x = new int[n];
auto ans = new char[n];
foreach (i; 0 .. n) {
if (s[i] == '(') {
st ~= i;
ans[i] = (st.length & 1) ? '0' : '1';
} else {
int j = st.back;
st.popBack ();
x[i] = j;
x[j] = i;
ans[i] = ans[j];
}
}
writeln (ans);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto T = readln.chomp.to!int;
foreach (_t; 0..T) {
auto N = readln.chomp.to!int;
auto ss = readln.split.to!(int[]);
auto DP = new int[](N+1);
foreach_reverse (i; 1..N+1) {
int x = i * 2, n = 1;
while (x <= N) {
if (ss[x-1] > ss[i-1]) n = max(n, DP[x] + 1);
x += i;
}
DP[i] = n;
}
int r;
foreach (ref e; DP) r = max(r, e);
writeln(r);
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(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 n = RD!int;
writeln(4 + n * 3);
writeln("0 0");
writeln("0 1");
writeln("1 0");
writeln("1 1");
foreach (i; 0..n)
{
writeln(i+2, " ", i+2);
writeln(i+1, " ", i+2);
writeln(i+2, " ", i+1);
}
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
string f0 = "What are you doing at the end of the world? Are you busy? Will you save us?";
string str1 = "What are you doing while sending \"";
string str2 = "\"? Are you busy? Will you send \"";
string str3 = "\"?";
long[] flen;
unittest {
assert(str1.length == 34);
}
void main() {
int q;
scan(q);
int n;
long k;
auto ans = new char[](q);
flen = new long[](53 + 1);
flen[0] = 75;
foreach (i ; 1 .. 54) {
flen[i] = 68 + 2*flen[i-1];
}
debug {
writefln("%(%s\n%)", flen);
}
foreach (i ; 0 .. q) {
scan(n, k);
bool b;
while (n > 53) {
n--;
k -= 34;
if (k <= 0) {
k += 34;
b = 1;
break;
}
}
if (b) {
ans[i] = str1[(k-1).to!uint];
}
else {
ans[i] = ask(n, k);
}
}
writeln(ans);
}
char ask(int n, long k) {
if (n == 0) {
if (k > f0.length) {
return '.';
}
else {
return f0[(k - 1).to!uint];
}
}
else {
int d1 = str1.length.to!int;
int d2 = str2.length.to!int;
int d3 = str3.length.to!int;
if (1 <= k && k <= d1) {
return str1[(k-1).to!uint];
}
else if (d1 < k && k <= d1 + flen[n-1]) {
return ask(n-1, k-d1);
}
else if (d1 + flen[n-1] < k && k <= d1 + d2 + flen[n-1]) {
return str2[(k - (d1 + flen[n-1])).to!uint - 1];
}
else if (d1 + d2 + flen[n-1] < k && k <= d1 + d2 + 2*flen[n-1]) {
return ask(n-1, k-(d1 + d2 +flen[n-1]));
}
else if (d1 + d2 + 2*flen[n-1] < k && k <= d1 + d2 + d3 + 2*flen[n-1]){
return str3[(k - (d1 + d2 + 2*flen[n-1])).to!uint - 1];
}
else {
return '.';
}
}
}
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.algorithm;
void main()
{
long x1, x2, y1, y2;
scanf("%lld%lld%lld%lld", &x1, &y1, &x2, &y2);
long dx = (x2 - x1) / 2;
long dy = (y2 - y1) / 2;
printf("%lld\n", (dx + 1) * (dy + 1) + dx * dy);
}
|
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(); } bool DEBUG = 0;
void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); }
string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
foreach(_; 0 .. scan!int){
long d = scan!long, m = scan!long;
long u = 1;
long ans = 0;
long tmp = 1;
int i = 0;
d += 1;
while(u <= d){
tmp *= u / 2 + 1, tmp %= m;
ans += tmp * u, ans %= m;
i += 1;
u *= 2;
}
ans += m - tmp * (u - d) % m, ans %= m;
tmp *= u / 2 + 1, tmp %= m;
ans.print;
}
}
|
D
|
module main;
import core.stdc.stdio;
int main(string[] argv)
{
int n;
scanf("%d", &n);
if(n%2==1){
printf("7");
}else{
printf("1");
}
for(int i = 0; i<n/2-1; i++)
printf("1");
return 0;
}
|
D
|
import std.stdio;
void main()
{
int n;
int res = 0;
char[200 + 1] s;
scanf("%d%s", &n, &s[0]);
for (int i = 0; i < n; i++) {
int vertical = 0;
int horizontal = 0;
for (int j = i; j < n; j++) {
if (s[j] == 'U')
vertical--;
if (s[j] == 'D')
vertical++;
if (s[j] == 'L')
horizontal--;
if (s[j] == 'R')
horizontal++;
if (vertical == 0 && horizontal == 0)
res++;
}
}
printf("%d\n", res);
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
int[] dist, prev;
void main() {
int N, K;
scan(N, K);
auto a = readln.split.to!(int[]);
a[] -= 1;
a[] %= K;
auto as = new int[](N + 1);
foreach (i ; 1 .. N + 1) {
as[i] = (as[i - 1] + a[i - 1]) % K;
}
debug {
writeln(as);
}
long ans;
int[int] c;
foreach (i ; 0 .. N + 1) {
if (i >= K) c[as[i - K]]--;
if (as[i] in c) ans += c[as[i]];
c[as[i]]++;
debug {
writeln(c);
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.stdio, std.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 s = readln.split.map!(to!long);
auto N = s[0];
auto A = s[1];
auto B = s[2];
auto C = s[3];
auto D = s[4];
long lb = A - (N - 1) * D;
long ub = A - (N - 1) * C;
foreach (i; 0..N) {
if (lb <= B && B <= ub) {
"YES".writeln;
return;
}
lb += C + D;
ub += C + D;
//writeln(i, " " , lb, " ", ub);
}
"NO".writeln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto N = RD;
long ans;
foreach (i; 1..N+1)
{
if (i % 5 && i % 3)
ans += i;
}
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
double norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }
double dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }
//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;
auto m = RD;
if (n == 1 && m == 1)
ans[ti] = 0;
else if (n == 1 || m == 1)
ans[ti] = 1;
else
ans[ti] = 2;
}
foreach (e; ans)
{
writeln(e);
}
stdout.flush;
debug readln;
}
|
D
|
import std.stdio;
long div = 1_000_000_007;
void main() {
long n, m;
scanf("%ld %ld\n", &n, &m);
auto dp = new long[n+2];
dp[0] = 1;
auto check = new bool[1_0000];
long before = -1;
foreach(_; 0..m) {
long a;
scanf("%ld\n", &a);
assert(before < a);
before = a;
check[a] = true;
}
foreach(i; 0..n) {
if (check[i]) continue;
dp[i+1] = (dp[i+1] + dp[i]) % div;
dp[i+2] = (dp[i+2] + dp[i]) % div;
}
dp[n].write;
}
|
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 X = RD;
long ans;
foreach (i; 0..A+1)
{
foreach (j; 0..B+1)
{
foreach (k; 0..C+1)
{
if (i * 500 + j * 100 + k * 50 == X)
++ans;
}
}
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
void main() {
auto I = readln.split.to!(long[]);
auto W = I[0];
auto H = I[1];
long solve() {
if (W == 1 || H == 1) return 1;
long ans;
ans += (1 + (W - 1)/2) * (1 + (H - 1)/2);
ans += (W / 2) * (H / 2);
return ans;
}
solve().writeln();
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;
void main() {
auto K = readln.chomp.to!long;
int N = 50;
auto A = new long[](N);
auto X = K / 50;
auto Y = K % 50;
foreach (i; 0..N) {
A[i] = 49 + X;
}
foreach (i; 0..Y) {
A[i] += N;
foreach (j; 0..N) if (j != i) A[j] -= 1;
}
N.writeln;
A.map!(a => a.to!string).join(" ").writeln;
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
int n, r; scan(n, r);
int ans = n >= 10
? r
: r + 100 * (10 - n);
writeln(ans);
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
import std.typecons;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.array;
// import dcomp.barrettint;
int[] primeList(int n) {
bool[] used = new bool[n+1];
FastAppender!(int[]) ans;
foreach (i; 2..n+1) {
if (used[i]) continue;
ans ~= i;
foreach (j; iota(i, n+1, i)) {
used[j] = true;
}
}
return ans.data;
}
int[long] fastFactor(ulong x, in BarrettULong[] primes) {
int[long] mp;
foreach (p; primes) {
while (x%p == 0) {
mp[p.value]++;
x=x/p;
}
}
if (x) mp[x]++;
return mp;
}
long mul(long x, long y) {
if (10L^^12 / x < y) return 10L^^12;
return x*y;
}
int main() {
auto sc = new Scanner(stdin);
// auto sc = new Scanner(File("01.txt", "r"));
auto pr = primeList(100_100);
auto bpr = pr.map!(x => BarrettULong(x)).array;
int n;
sc.read(n);
int[2][long] g;
bool one = false;
foreach (i; 0..n) {
long s;
sc.read(s);
// writeln(fastFactor(s, bpr));
long d0 = 1, d1 = 1;
foreach (p, c; fastFactor(s, bpr)) {
c %= 3;
if (c == 1) {
d0 = mul(d0, p);
d1 = mul(d1, p);
d1 = mul(d1, p);
} else if (c == 2) {
d0 = mul(d0, p);
d0 = mul(d0, p);
d1 = mul(d1, p);
}
}
if (d0 == 1) {
one = true;
continue;
}
if (d0 < d1) {
if (d0 in g) {
g[d0][0]++;
} else {
g[d0] = [1, 0];
}
} else {
if (d1 in g) {
g[d1][1]++;
} else {
g[d1] = [0, 1];
}
}
}
int ans = 0;
if (one) ans++;
foreach (u; g) {
ans += max(u[0], u[1]);
}
writeln(ans);
return 0;
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/array.d */
// module dcomp.array;
T[N] fixed(T, int N)(T[N] a) {return a;}
//this is not reference type!(please attention to copy)
struct FastAppender(A) {
import std.algorithm : max;
import std.range.primitives : ElementEncodingType;
import core.stdc.string : memcpy;
private alias T = ElementEncodingType!A;
private T* _data;
private size_t len, cap;
@property size_t length() {return len;}
void reserve(size_t nlen) {
import core.memory : GC;
if (nlen <= cap) return;
void* nx = GC.malloc(nlen * T.sizeof);
cap = nlen;
if (len) memcpy(nx, _data, len * T.sizeof);
_data = cast(T*)(nx);
}
void opOpAssign(string op : "~")(T item) {
if (len == cap) {
reserve(max(4, cap*2));
}
_data[len++] = item;
}
void clear() {
len = 0;
}
T[] data() {
return (_data) ? _data[0..len] : null;
}
}
unittest {
import std.stdio, std.algorithm;
auto u = FastAppender!(int[])();
u ~= 4; u ~= 5;
assert(equal(u.data, [4, 5]));
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
if (f.eof) return false;
line = lineBuf[];
f.readln(line);
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
//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");
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/barrettint.d */
// module dcomp.barrettint;
// import dcomp.int128;
struct BarrettULong {
ulong value, m;
int lg;
this(ulong value) {
import core.bitop : bsr;
this.value = value;
if (value <= 1) return;
lg = value.bsr;
if (1UL<<lg != value) lg++;
m = div128([0UL, (2UL<<(lg-1))-value], value)+1;
}
ulong opBinaryRight(string op:"/")(ulong x) const {
assert(value != 0);
if (value == 1) return x;
ulong r;
r = mul128(m, x)[1];
r = (r + ((x-r)>>1)) >> (lg-1);
return r;
}
ulong opBinaryRight(string op:"%")(ulong x) const {
return x - x/this*value;
}
}
unittest {
ulong[] up;
ulong[] down;
up ~= 0;
foreach (ulong i; 1..1000) {
up ~= i;
up ~= -i;
}
foreach (ulong i; 1..1000) {
down ~= i;
down ~= -i;
}
foreach (ulong i; down) {
auto b = BarrettULong(i);
foreach (ulong j; up) {
assert(j/b == j/i);
assert(j%b == j%i);
}
}
}
/* IMPORT /Users/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);
}
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/ldc/inline.d */
// module dcomp.ldc.inline;
version(LDC) {
pragma(LDC_inline_ir)
R inlineIR(string s, R, P...)(P);
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/int128.d */
// module dcomp.int128;
version(LDC) {
// import dcomp.ldc.inline;
}
ulong[2] mul128(ulong a, ulong b) {
ulong[2] res;
version(LDC) {
ulong upper, lower;
inlineIR!(`
%r0 = zext i64 %0 to i128
%r1 = zext i64 %1 to i128
%r2 = mul i128 %r1, %r0
%r3 = trunc i128 %r2 to i64
%r4 = lshr i128 %r2, 64
%r5 = trunc i128 %r4 to i64
store i64 %r3, i64* %2
store i64 %r5, i64* %3`, void)(a, b, &lower, &upper);
return [lower, upper];
} else version(D_InlineAsm_X86_64) {
ulong upper, lower;
asm {
mov RAX, a;
mul b;
mov lower, RAX;
mov upper, RDX;
}
return [lower, upper];
} else {
pragma(msg, "Not Supported");
}
}
ulong div128(ulong[2] a, ulong b) {
version(LDC) {
return inlineIR!(`
%r0 = zext i64 %0 to i128
%r1 = zext i64 %1 to i128
%r2 = shl i128 %r1, 64
%r3 = add i128 %r0, %r2
%r4 = zext i64 %2 to i128
%r5 = udiv i128 %r3, %r4
%r6 = trunc i128 %r5 to i64
ret i64 %r6`,ulong)(a[0], a[1], b);
} else version(D_InlineAsm_X86_64) {
ulong upper = a[1], lower = a[0];
ulong res;
asm {
mov RDX, upper;
mov RAX, lower;
div b;
mov res, RAX;
}
return res;
} else {
pragma(msg, "Not Supported");
}
}
|
D
|
//dlang template---{{{
import std.stdio;
import std.conv;
import std.string;
import std.array;
import std.algorithm;
import std.typecons;
import std.math;
import std.range;
// MIT-License https://github.com/kurokoji/nephele
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType))
{
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType))
{
str ~= s.to!(char[]).strip.split;
}
}
//}}}
void main() {
Scanner sc = new Scanner;
int A, P;
sc.scan(A, P);
P += (A * 3);
writeln(P / 2);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
void main() {
int n;
scan(n);
int ans = n;
foreach (a ; 0 .. n + 1) {
int tmp;
int b = n - a;
while (a > 0) {
tmp += a % 6;
a /= 6;
}
while (b > 0) {
tmp += b % 9;
b /= 9;
}
ans = min(ans, tmp);
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
void main() {
int n = readln.chomp.to!int;
int[] a = readln.split.to!(int[]);
writeln(a.reduce!max - a.reduce!min);
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.uni;
|
D
|
import core.stdc.stdio;
import std.algorithm;
void main(){
int n,m;
scanf("%d%d",&n,&m);
int[] p = new int[m];
foreach(ref v;p)
scanf("%d",&v);
int[] a=new int[n-1],b=new int[n-1],c=new int[n-1];
foreach(i;0..n-1)
scanf("%d%d%d",&a[i],&b[i],&c[i]);
int[] sum=new int[n-1];
foreach(i;0..m-1){
int s=p[i]-1,t=p[i+1]-1;
if(s>t)
swap(s,t);
sum[s]++;
if(t<n-1)
sum[t]--;
}
foreach(i;1..n-1)
sum[i]+=sum[i-1];
long ans;
foreach(i;0..n-1)
ans+=min(cast(long)a[i]*sum[i],cast(long)b[i]*sum[i]+c[i]);
printf("%lld\n",ans);
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.algorithm;
void main()
{
auto n = map!(to!int)(readln.strip.split);
while(n[0]!=-1 || n[1] != -1 || n[2] != -1) {
string grade;
int m=n[0],f=n[1],r=n[2],score=0;
score = m+f;
if(m==-1 || f ==-1)
{
grade = "F";
}
else if(score >= 80)
{
grade = "A";
}
else if(score < 80 && score >= 65)
{
grade = "B";
}
else if(score < 65 && score >= 50)
{
grade = "C";
}
else if(score < 50 && score >= 30)
{
if(r>=50)
grade = "C";
else
grade = "D";
}
else
grade = "F";
writeln(grade);
n = map!(to!int)(readln.strip.split);
}
}
|
D
|
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.range;
void main() {
string s = readln.strip;
long cnt = 0;
char[] last = new char[](10000);
long p = -1;
for (long i = 0; i < s.length; i++) {
if (i+1 < s.length && s[i] != s[i+1]) {
cnt += 2;
i++;
} else {
if (p >= 0 && last[p] != s[i]) {
cnt += 2;
p--;
} else {
p++;
last[p] = s[i];
}
}
}
writeln(cnt);
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.format;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
enum MOD = (10 ^^ 9) + 7;
void main()
{
long N = lread();
long[long] D;
foreach (n; 1 .. N + 1)
{
long i = 2;
while (n != 1)
{
// writeln(n);
if (n < i * i)
{
D[n]++;
n = 1;
}
while (n % i == 0)
{
n /= i;
D[i]++;
}
i++;
}
}
long ans = 1;
// writeln(D);
foreach (v; D.values)
{
ans = ans * (v + 1) % MOD;
}
writeln(ans);
}
|
D
|
import std.stdio;
import std.conv;
import std.array;
void main()
{
int N = readln.split[0].to!int;
int[] V = readln.split.to!(int[]);
int[] C = readln.split.to!(int[]);
int ans = 0;
foreach (i; 0..N){
if (V[i] > C[i]) ans += V[i] - C[i];
}
writeln(ans);
}
|
D
|
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container;
import std.numeric, std.math;
import core.bitop;
string my_readln() { return chomp(readln()); }
long mod = pow(10, 9) + 7;
long moda(long x, long y) { return (x + y) % mod; }
long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; }
long modm(long x, long y) { return (x * y) % mod; }
void main()
{
auto tokens = my_readln.split;
auto D = tokens[0].to!long;
auto G = tokens[1].to!long;
ulong[] p, c;
foreach (i; 0..D)
{
auto tokens2 = my_readln.split;
p ~= tokens2[0].to!ulong;
c ~= tokens2[1].to!ulong;
}
long f(ulong[] used, long score, long cnt)
{
if (score <= 0) return cnt;
long r = long.max;
foreach (i, e; used)
{
if (e == 1) continue;
auto x = score / ((i + 1) * 100) + (score % ((i + 1) * 100) == 0 ? 0 : 1);
if (x <= p[i])
{
r = min(r, cnt + x);
}
else
{
auto nUsed = used.dup;
nUsed[i] = 1;
r = min(r, f(nUsed, score - ((i + 1) * 100 * p[i] + c[i]), cnt + p[i]));
}
}
return r;
}
writeln(f(new ulong[](D), G, 0));
stdout.flush();
}
|
D
|
/+ dub.sdl:
name "D"
dependency "dunkelheit" version=">=0.9.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
int n;
int[][] g;
long[] mp = new long[110];
void dfs(int p, int b, int ndps) {
int ch = 0;
foreach (d; g[p]) {
if (d == b) continue;
ch++;
dfs(d, p, ndps+1);
}
mp[ndps] = max(mp[ndps], ch);
}
long calc(int r) {
mp[] = 1;
dfs(r, -1, 1);
// writeln("ONE : ", mp);
return mp.reduce!"a*b";
}
long calc2(int r1, int r2) {
mp[] = 1;
mp[0] = 2;
dfs(r1, r2, 1);
dfs(r2, r1, 1);
// writeln("MID : ", mp);
return mp.reduce!"a*b";
}
int main() {
Scanner sc = new Scanner(stdin);
sc.read(n);
g = new int[][](n);
foreach (i; 0..n-1) {
int a, b;
sc.read(a, b); a--; b--;
g[a] ~= b; g[b] ~= a;
}
int dfs(int p, int b, int ndps) {
int ans = ndps;
foreach (d; g[p]) {
if (d == b) continue;
ans = max(ans, dfs(d, p, ndps+1));
}
return ans;
}
int deg = 0;
foreach (i; 0..n) {
deg = max(deg, dfs(i, -1, 1));
}
// writeln(deg);
int col = (deg+1)/2;
long ans = 1L << 62;
foreach (i; 0..n) {
if (dfs(i, -1, 1) > col) continue;
// / writeln("CENT ", i);
ans = min(ans, calc(i));
}
foreach (i; 0..n) {
foreach (j; g[i]) {
if (dfs(i, j, 1) > col || dfs(j, i, 1) > col) continue;
// writeln("CENT ", i, " ", j);
ans = min(ans, calc2(i, j));
}
}
writeln(col, " ", ans);
return 0;
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
void read(bool enforceEOF = false, T, Args...)(ref T x, auto ref Args args) {
import std.exception;
enforce(readSingle(x));
static if (args.length == 0) {
enforce(enforceEOF == false || !succ());
} else {
read!enforceEOF(args);
}
}
void read(bool enforceEOF = false, Args...)(auto ref Args args) {
import std.exception;
static if (args.length == 0) {
enforce(enforceEOF == false || !succ());
} else {
enforce(readSingle(args[0]));
read!enforceEOF(args);
}
}
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/foundation.d */
// module dkh.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
|
D
|
import std.stdio, std.math, std.algorithm, std.array, std.string, std.conv, std.container, std.range;
T Read(T)() { return readln.chomp.to!(T); }
T[] Reads(T)() { return readln.split.to!(T[]); }
alias read = Read!(int);
alias reads = Reads!(int);
void main() {
int d = read();
if (d == 25)
writeln("Christmas");
else if (d == 24)
writeln("Christmas Eve");
else if (d == 23)
writeln("Christmas Eve Eve");
else if (d == 22)
writeln("Christmas Eve Eve Eve");
}
|
D
|
import std.stdio, std.array, std.conv;
void main() {
while (true) {
string[] input = split(readln());
int H = to!int(input[0]);
int W = to!int(input[1]);
if (H == 0 && W == 0) return;
for (int i = 0; i < H; ++i) {
for (int j= 0; j < W; ++j) {
write(is_side(i, j, H, W) ? "#" : ".");
}
writeln("");
}
writeln("");
}
}
bool is_side(int y, int x, int H, int W) {
return (y == 0 || x == 0 || y == (H - 1) || x == (W - 1));
}
|
D
|
import std.stdio, std.conv, std.string;
import std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
int DEBUG_LEVEL = 0;
void print()(){ writeln(""); }
void print(T, A ...)(T t, lazy A a){ write(t), print(a); }
void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); }
void main(string[] args){
if(args.length > 1 && args[1] == "-debug"){
if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int;
else DEBUG_LEVEL = 1;
}
int n = read.to!int;
Node[] nodes;
foreach(i; 0 .. n) nodes ~= new Node(i);
Edge[] edges;
foreach(i; 0 .. n - 1){
int u = read.to!int - 1;
int v = read.to!int - 1;
long w = read.to!long;
edges ~= new Edge(nodes[u], nodes[v], w);
}
nodes[0].setValue(1);
foreach(ed; edges) ed.paint();
foreach(nd; nodes) nd.value.writeln;
}
class Edge{
Node node1;
Node node2;
long value;
this(Node node1, Node node2, long value){
if(node1.id < node2.id) this.node1 = node1, this.node2 = node2;
else this.node1 = node2, this.node2 = node1;
node1.edges ~= this, node2.edges ~= this;
this.value = value;
}
void paint(){
if(node1.isVisited && ! node2.isVisited){
node2.setValue((node1.value + this.value) % 2);
foreach(ed; node2.edges) ed.paint();
}
else if(! node1.isVisited && node2.isVisited){
node1.setValue((node2.value + this.value) % 2);
foreach(ed; node1.edges) ed.paint();
}
else if(! node1.isVisited && node2.isVisited){
foreach(ed; node1.edges) ed.paint();
foreach(ed; node2.edges) ed.paint();
}
}
}
// 木 辺の重み edge 再帰
class Node{
int id;
int value = -1;
bool isVisited = 0;
Edge[] edges;
this(int id){
this.id = id;
}
void setValue(int v){
value = v % 2;
this.isVisited = 1;
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto N = RD;
auto P = RDA(-1);
auto cnt = new long[][](N, N);
foreach (i; 0..N)
{
auto b = N-i-1;
foreach (j; 0..N)
{
auto r = N-j-1;
cnt[i][j] = min(i, j, b, r);
}
}
auto use = new long[][](N, N);
foreach (i; 0..N)
{
use[i][] = 1;
}
void visit(long y, long x)
{
auto cost = cnt[y][x] + use[y][x];
if (y != 0)
{
if (cost < cnt[y-1][x])
{
cnt[y-1][x] = cost;
visit(y-1, x);
}
}
if (y != N-1)
{
if (cost < cnt[y+1][x])
{
cnt[y+1][x] = cost;
visit(y+1, x);
}
}
if (x != 0)
{
if (cost < cnt[y][x-1])
{
cnt[y][x-1] = cost;
visit(y, x-1);
}
}
if (x != N-1)
{
if (cost < cnt[y][x+1])
{
cnt[y][x+1] = cost;
visit(y, x+1);
}
}
}
debug writeln(cnt);
long ans;
foreach (e; P)
{
auto y = e / N;
auto x = e % N;
visit(y, x);
ans += cnt[y][x];
use[y][x] = 0;
visit(y, x);
}
writeln(ans);
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, core.stdc.string, core.stdc.stdlib;
void main() {
immutable long INF = 1L << 59;
auto s = readln.split.map!(to!int);
auto N = s[0];
auto M = s[1];
auto L = s[2].to!long;
auto G = new long[][](N, N);
foreach (i; 0..N) foreach (j; 0..N) G[i][j] = i == j ? 0 : INF;
foreach (_; 0..M) {
s = readln.split.map!(to!int);
auto a = s[0] - 1;
auto b = s[1] - 1;
auto c = s[2].to!long;
G[a][b] = c;
G[b][a] = c;
}
auto dist1 = warshall_floyd!(long, INF)(G);
auto dist2 = new long[][](N, N);
foreach (i; 0..N) foreach (j; 0..N) dist2[i][j] = i == j ? 0 : INF;
foreach (i; 0..N) foreach (j; 0..N) if (dist1[i][j] <= L) dist2[i][j] = 1;
auto dist3 = warshall_floyd!(long, INF)(dist2);
auto Q = readln.chomp.to!int;
while (Q--) {
s = readln.split.map!(to!int);
auto a = s[0] - 1;
auto b = s[1] - 1;
auto ans = dist3[a][b];
writeln(ans >= INF ? -1 : ans - 1);
}
}
T[][] warshall_floyd(T, T inf)(const T[][] graph) {
import std.conv : to;
int n = graph.length.to!int;
auto dist = new T[][](n, n);
foreach (i; 0..n) foreach (j; 0..n) dist[i][j] = graph[i][j];
foreach (i; 0..n)
foreach (j; 0..n)
foreach (k; 0..n)
if (dist[j][i] != inf && dist[i][k] != inf)
dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k]);
return dist;
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
auto N = readln.chomp.to!int;
auto a = readln.split.to!(int[]);
int cnt = 0;
while (a.all!"a%2==0") {
a[] /= 2;
cnt++;
}
writeln(cnt);
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
void main()
{
auto n = lread();
auto h = aryread();
long cnt;
foreach (i; 0 .. n - 1)
{
if (h[i] < h[i + 1])
{
h[i + 1] -= 1;
}
// writeln(h);
}
foreach (i; 0 .. n - 1)
{
if (h[i] > h[i + 1])
{
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;
}
}
|
D
|
void main() {
long n = readln.chomp.to!long;
writeln(2 * n / gcd(2, n));
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.uni;
|
D
|
void main() {
writeln(ri^^3);
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.conv;
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;
import std.string;
import std.format;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.concurrency;
import std.traits;
import std.uni;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;
enum long INF = long.max/3;
enum long MOD = 10L^^9+7;
void main() {
string str = readln.chomp;
auto po = (iota('a', 'z').array ~ "z").find!(c => !str.canFind(c));
if (po.empty) {
int[] as = str.map!(c => c.to!int).array;
if (as.nextPermutation) {
string bs = as.map!(c => c.to!char).array.to!string;
foreach(i; 0..bs.length) {
if (bs[i] != str[i]) {
bs[0..i+1].writeln;
return;
}
}
} else {
writeln(-1);
}
} else {
writeln(str, po.front);
}
}
// ----------------------------------------------
void scanln(Args...)(auto ref Args args) {
import std.meta;
template getFormat(T) {
static if (isIntegral!T) {
enum getFormat = "%d";
} else static if (isFloatingPoint!T) {
enum getFormat = "%g";
} else static if (isSomeString!T || isSomeChar!T) {
enum getFormat = "%s";
} else {
static assert(false);
}
}
enum string str = [staticMap!(getFormat, Args)].join(" ") ~ "\n";
// readf!str(args);
mixin("str.readf(" ~ Args.length.iota.map!(i => "&args[%d]".format(i)).join(", ") ~ ");");
}
void times(alias fun)(long n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(long n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
T ceil(T)(T x, T y) if (__traits(isIntegral, T)) {
// `(x+y-1)/y` will only work for positive numbers ...
T t = x / y;
if (t * y < x) t++;
return t;
}
T floor(T)(T x, T y) if (__traits(isIntegral, T)) {
T t = x / y;
if (t * y > x) t--;
return t;
}
// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
return reduce!fun(tuple(seed), r);
}
}
}
}
// cumulativeFold was added in D 2.072.0
static if (__VERSION__ < 2072) {
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
}
// minElement/maxElement was added in D 2.072.0
static if (__VERSION__ < 2072) {
auto minElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto minimum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b < minimum) {
element = a;
minimum = b;
}
}
return element;
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto maximum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b > maximum) {
element = a;
maximum = b;
}
}
return element;
}
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.range;
import std.array;
import std.conv;
import std.complex;
import std.math;
import std.ascii;
import std.bigint;
import std.container;
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];
}
const real eps = 1e-10;
void main(){
auto n = readInt();
const string[char] cmap = [
'1': ".,!? ",
'2': "abc",
'3': "def",
'4': "ghi",
'5': "jkl",
'6': "mno",
'7': "pqrs",
'8': "tuv",
'9': "wxyz"
];
for(int i = 0; i < n; ++i) {
string ans;
string s = readln().strip();
while(!s.empty) {
while(!s.empty && s[0] == '0') {
s = s.drop(1);
}
if(s.empty) {
break;
}
char c = s[0];
int count;
while(!s.empty && s[0] == c) {
s = s.drop(1);
++count;
}
ans = ans ~ cmap[c][(count-1) % cmap[c].length];
}
writeln(ans);
}
}
|
D
|
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop;
// dfmt on
void main()
{
long N, M;
scan(N, M);
long ac = N - M;
writeln((2 ^^ M) * (ac * 100 + 1900 * M));
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
void main()
{
auto nk = readln.split.to!(int[]);
writeln(nk[0] % nk[1] ? 1 : 0);
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.