code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto M = s[1];
auto edges = new int[][](N);
Tuple!(int, int)[] hoge;
foreach (_; 0..M) {
s = readln.split.map!(to!int);
edges[s[0]-1] ~= s[1]-1;
edges[s[1]-1] ~= s[0]-1;
hoge ~= tuple(s[0]-1, s[1]-1);
}
auto used = new bool[](N);
void dfs(int n, int x, int y) {
if (used[n]) return;
used[n] = true;
foreach (m; edges[n]) {
if (x == n && y == m) continue;
if (x == m && y == n) continue;
if (!used[m]) dfs(m, x, y);
}
}
int ans = 0;
foreach (i; 0..M) {
used.fill(false);
dfs(0, hoge[i][0], hoge[i][1]);
if (used.sum != N) ans += 1;
}
ans.writeln;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
alias route = Tuple!(long, "From", long, "To");
long bignum = 1_000_000_007;
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
long n, x, d, count = 1;
scan(n, x);
auto l = aryread();
foreach (e; l)
{
if(d + e > x)
break;
d += e;
count++;
}
writeln(count);
}
|
D
|
void main()
{
long[] tmp = readln.split.to!(long[]);
long n = tmp[0], x = tmp[1];
long[] a = readln.split.to!(long[]);
long cnt;
foreach (i; 1 .. n)
{
long diff = a[i] + a[i-1] - x;
if (diff > 0)
{
a[i] = max(0, a[i]-diff);
cnt += diff;
}
}
cnt.writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void 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 main()
{
auto N = RD;
auto M = RD;
writeln(N == M ? "Yes" : "No");
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
long[100][8] DP;
long P = 1_000_000_007;
void main()
{
foreach (ref h; DP) foreach (ref e; h) e = -1;
auto hwk = readln.split.to!(int[]);
auto H = hwk[0];
auto W = hwk[1];
auto K = hwk[2];
long[] BS;
BS.length = W;
foreach (s; 0..2^^(W-1)) {
bool prev, ok = true;
foreach (i; 0..W-1) {
auto c = (s & (1<<i)) > 0;
if (prev && c) {
ok = false;
break;
}
prev = c;
}
if (!ok) continue;
++BS[0];
foreach (i; 0..W-1) {
if (s & (1<<i)) ++BS[i+1];
}
}
long solve(int w, int h) {
if (h == H) return w == K-1 ? 1 : 0;
if (DP[w][h] != -1) return DP[w][h];
auto r = solve(w, h+1) * (BS[0] - (w > 0 ? BS[w] : 0) - (w < W-1 ? BS[w+1] : 0));
if (w > 0) r += solve(w-1, h+1) * BS[w];
if (w < W-1) r += solve(w+1, h+1) * BS[w+1];
return DP[w][h] = r % P;
}
writeln(solve(0, 0));
}
|
D
|
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math, std.container, std.typecons;
import core.stdc.stdio;
// foreach, foreach_reverse, writeln
void main() {
int n;
scanf("%d", &n);
int[] v = new int[n];
foreach (i; 0..n) {
scanf("%d", &v[i]);
}
int ans = 0;
int memkey = -1;
int memdiff = 0;
foreach (p; 0..2) {
int[int] counter;
for (int i = p; i < n; i += 2) {
if (!(v[i] in counter)) {
counter[v[i]] = 0;
}
counter[v[i]]++;
}
int first = 0, second = 0, firstkey = -1;
foreach (key, val; counter) {
if (second < val) {
second = val;
if (first < val) {
swap(first, second);
firstkey = key;
}
}
}
ans += first;
if (p == 0) {
memkey = firstkey;
memdiff = first-second;
} else if (memkey == firstkey) {
int diff = first-second;
ans -= min(memdiff, diff);
}
}
writeln(n-ans);
}
|
D
|
import std.stdio, std.string, std.array, std.conv;
void main() {
int[] tmp = readln.chomp.split.to!(int[]);
int a = tmp[0], b = tmp[1], c = tmp[2];
int cnt = 0;
foreach (i; a .. b+1) {
if (c % i == 0) ++cnt;
}
cnt.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;
const long MOD = 10^^9 + 7;
void main() {
auto N = readln.chomp.to!int;
auto A = 2.iota.map!(_ => readln.split.map!(to!long).array).array;
foreach (j; 0..N-1) {
A[0][j+1] += A[0][j];
}
for (int j = N-1; j > 0; --j) {
A[1][j-1] += A[1][j];
}
long ans = 0;
foreach (i; 0..N) {
ans = max(ans, A[0][i] + A[1][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 = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto A = new long[][](3);
foreach (i; 0..3)
A[i] = RDA;
auto N = RD;
bool[long] set;
foreach (i; 0..N)
{
auto b = RD;
set[b] = true;
}
bool ans;
foreach (i; 0..3)
{
if (set.get(A[i][0], false) && set.get(A[i][1], false) && set.get(A[i][2], false))
ans = true;
if (set.get(A[0][i], false) && set.get(A[1][i], false) && set.get(A[2][i], false))
ans = true;
}
if (set.get(A[0][0], false) && set.get(A[1][1], false) && set.get(A[2][2], false))
ans = true;
if (set.get(A[0][2], false) && set.get(A[1][1], false) && set.get(A[2][0], false))
ans = true;
writeln(ans ? "Yes" : "No");
stdout.flush;
debug readln;
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format, 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()
{
auto S = sread();
writeln(S[2] == S[3] && S[4] == S[5] ? "Yes" : "No");
}
|
D
|
void main() {
long x = readln.chomp.to!long;
long result = x / 11 * 2;
long r = x % 11;
if (r > 0) ++result;
if (r > 6) ++result;
result.writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
void main()
{
dchar[] s = readln.chomp.to!(dchar[]);
dchar[] t = readln.chomp.to!(dchar[]);
long last = t.length - 1;
bool ok;
foreach_reverse (i, x; s)
{
if (x == t[last] || x == '?')
{
bool check = true;
if (i.to!long - last >= 0)
{
foreach (j; 0 .. last)
{
if (s[i-last+j] != t[j] && s[i-last+j] != '?')
{
check = false;
break;
}
}
}
else check = false;
if (check)
{
ok = true;
foreach (j; 0 .. last+1)
{
s[i-last+j] = t[j];
}
foreach (j; 0 .. s.length)
{
if (s[j] == '?') s[j] = 'a';
}
break;
}
}
}
if (ok) s.writeln;
else "UNRESTORABLE".writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import 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 R = RD;
auto G = RD;
writeln((G * 2) - R);
stdout.flush();
debug readln();
}
|
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;
alias Key = Tuple!(int, "cost", int, "mask");
int calc(int num, Key[] keys) {
int n = cast(int)keys.length;
auto dp = new int[][](n, 1 << num);
for (int i = 0; i < dp.length; i++) dp[i][] = int.max;
dp[0][keys[0].mask] = keys[0].cost;
for (int i = 1; i < n; i++) {
int cost = keys[i].cost;
int mask = keys[i].mask;
dp[i][mask] = cost;
for (int j = 0; j < (1 << num); j++) {
if (dp[i-1][j] == int.max) continue;
dp[i][j] = min(dp[i][j], dp[i-1][j]);
dp[i][j | mask] = min(dp[i][j | mask], dp[i-1][j] + cost);
}
}
int ans = dp[n-1][(1 << num) - 1];
return ans == int.max ? -1 : ans;
}
void main() {
int n, m; scan(n, m);
Key[] keys;
foreach (_; 0..m) {
int a, b; scan(a, b);
auto c = readints;
int mask = 0;
foreach (e; c) mask |= (1 << (e-1));
keys ~= Key(a, mask);
}
writeln(calc(n, keys));
}
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;
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto s = readln.chomp;
if (s.length < 4) writeln("No");
else if (s[0..4] != "YAKI") writeln("No");
else writeln("Yes");
}
|
D
|
void main()
{
long n = rdElem;
dchar[] s = rdDchar;
s.uniq.array.length.writeln;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.typecons, std.range, std.container, std.math, std.algorithm.searching, std.functional;
void main(){
auto nk=readln().split.array;
auto n=nk[0].to!int;
string[] ds=readln().split.array;
bool isSafe(int cur){
string str=cur.to!string;
return ds.all!(d=>!str.canFind(d));
}
foreach(current;n..100000){
if(isSafe(current)){
writeln(current);
return;
}
}
}
|
D
|
import std.stdio, std.string, std.conv;
void main()
{
while(true)
{
auto line = readln;
if(!line)break;
auto f = line.chomp.to!real;
f.classify.writeln;
}
}
string classify(real w)
{
if(w <= 48) return "light fly";
if(w <= 51) return "fly";
if(w <= 54) return "bantam";
if(w <= 57) return "feather";
if(w <= 60) return "light";
if(w <= 64) return "light welter";
if(w <= 69) return "welter";
if(w <= 75) return "light middle";
if(w <= 81) return "middle";
if(w <= 91) return "light heavy";
return "heavy";
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto n = readln.chomp.to!int;
auto 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
|
void main() {
auto N = ri, A = ri;
writeln(N*N-A);
}
// ===================================
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.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;
import std.datetime;
void main()
{
while (1) {
auto x = readln.chomp.split.map!(to!int);
if (x[0] == -1) break;
(Date(x[3], x[4], x[5]) - Date(x[0], x[1], x[2])).total!"days".writeln;
}
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto n = readln.chomp.to!int;
auto m = n;
int cnt;
int k = 1;
while (n) {
auto t = n / 10;
if (t) {
cnt += 10 ^^ k - 10 ^^ (k - 1);
} else {
cnt += m - 10 ^^ (k - 1) + 1;
}
k += 2;
n /= 100;
}
cnt.writeln;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.array, std.algorithm, std.range;
void main()
{
for(int t=1; ; ++t)
{
immutable W = readln().chomp().to!int();
if(!W) break;
auto dp = new int[W+1];
foreach(_;0..readln().chomp().to!int())
{
auto m = readln().chomp().split(",").map!(to!int);
immutable v=m[0], w=m[1];
foreach_reverse(i;w..W+1)
dp[i]=max(dp[i],dp[i-w]+v);
}
int mv=0,mw=0;
foreach(int w,v;dp)
if(mv<v) mv=v,mw=w;
writeln("Case ",t,":\n",mv,"\n",mw);
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.array, std.algorithm, std.range;
import std.math;
void main()
{
foreach(_;0..readln().chomp().to!int())
{
auto m = readln().split().map!(to!real);
immutable xa=m[0], ya=m[1], ra=m[2], xb=m[3], yb=m[4], rb=m[5];
immutable d = hypot(xa-xb,ya-yb);
if(ra>rb+d) writeln(2);
else if(ra+d<rb) writeln(-2);
else if(ra+rb<d) writeln(0);
else writeln(1);
}
}
|
D
|
import std.stdio;
import std.ascii;
import std.string;
import std.range;
import std.algorithm;
import core.stdc.stdio;
int main()
{
int k = readInt!int;
char[] s = readString.dup.reverse;
int n = cast(int)s.length;
int q = readInt!int;
long[] pos = new long[](n + 1);
void calculate(int v)
{
if (v * 2 > s.length)
{
if (s[v - 1] == '0' || s[v - 1] == '1') pos[v] = 1;
else pos[v] = 2;
return;
}
int left = 2 * v;
int right = 2 * v + 1;
calculate(left);
calculate(right);
if (s[v - 1] == '1')
{
pos[v] = pos[left];
}
else if (s[v - 1] == '0')
{
pos[v] = pos[right];
}
else
{
pos[v] = pos[right] + pos[left];
}
}
calculate(1);
foreach(qi; 0 .. q)
{
int p = n - readInt!int + 1;
string ch = readString;
s[p - 1] = ch[0];
while (p >= 1)
{
if (p * 2 >= n)
{
if (s[p - 1] == '0' || s[p - 1] == '1') pos[p] = 1;
else pos[p] = 2;
goto done;
}
if (s[p - 1] == '1')
{
pos[p] = pos[2 * p];
}
else if (s[p - 1] == '0')
{
pos[p] = pos[2 * p + 1];
}
else
{
pos[p] = pos[2 * p] + pos[2 * p + 1];
}
done:
p /= 2;
}
pos[1].writeln;
}
return 0;
}
/* INPUT ROUTINES */
int currChar;
static this()
{
currChar = getchar();
}
char topChar()
{
return cast(char) currChar;
}
void popChar()
{
currChar = getchar();
}
auto readInt(T)()
{
T num = 0;
int sgn = 0;
while (topChar.isWhite) popChar;
while (topChar == '-' || topChar == '+') { sgn ^= (topChar == '-'); popChar; }
while (topChar.isDigit) { num *= 10; num += (topChar - '0'); popChar; }
if (sgn) return -num; return num;
}
string readString()
{
string res = "";
while (topChar.isWhite) popChar;
while (!topChar.isWhite) { res ~= topChar; popChar; }
return res;
}
/**MODULAR SYSTEM*/
struct Z(immutable long m)
{
long rep;
this(long num)
{
rep = num;
}
Z!m opBinary(string operator)(Z!m rhs)
{
static if (operator == "+")
{
long result = rhs.rep + this.rep;
if (result >= m) result -= m;
return Z!m(result);
}
else static if (operator == "-")
{
long result = this.rep - rhs.rep;
if (result < 0) result += m;
return Z!m(result);
}
else static if (operator == "*")
{
long result = this.rep * rhs.rep;
if (result >= m) result %= m;
return Z!m(result);
} else static assert(text("Operator ", operator, " not supported"));
}
Z!m opBinary(string operator)(long exponent) if (operator == "^^")
{
assert(exponent >= 0);
Z!m base = this;
Z!m result = 1;
while (exponent)
{
if (exponent & 1)
result = result * base;
base = base * base;
exponent >>= 1;
}
return result;
}
invariant
{
assert(rep >= 0 && rep < m);
}
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
import std.typecons;
import std.bigint;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.container.deque;
int main() {
auto sc = new Scanner(stdin);
int n, m, k;
sc.read(n, m, k); m++;
int[] a = new int[n];
a.each!((ref x) => sc.read(x));
long[] dp = a.map!(to!long).array;
long[] ndp = new long[n];
auto deq = Deque!(int, false).make();
foreach (int ph; 2..k+1) {
ndp[] = -(10L^^18);
deq.clear();
foreach (int i; 0..n) {
if (deq.length && deq[0] == i-m) {
deq.removeFront();
}
if (deq.length) {
ndp[i] = dp[deq[0]] + 1L * ph * a[i];
}
while (deq.length && dp[deq.back] <= dp[i]) {
deq.removeBack();
}
deq.insertBack(i);
}
swap(dp, ndp);
}
writeln(dp.fold!max);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
//fold(for old compiler)
static if (__VERSION__ <= 2070) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
unittest {
import std.stdio;
auto l = [1, 2, 3, 4, 5];
assert(l.fold!"a+b"(10) == 25);
}
}
version (X86) static if (__VERSION__ < 2071) {
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/deque.d */
// module dcomp.container.deque;
struct Deque(T, bool mayNull = true) {
import core.exception : RangeError;
import core.memory : GC;
import std.range : ElementType, isInputRange;
import std.traits : isImplicitlyConvertible;
struct Payload {
T *d;
size_t st, length, cap;
@property bool empty() const { return length == 0; }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {
version(assert) if (length <= i) throw new RangeError();
return d[(st+i >= cap) ? (st+i-cap) : st+i];
}
private void expand() {
import std.algorithm : max;
assert(length == cap);
auto nc = max(size_t(4), 2*cap);
T* nd = cast(T*)GC.malloc(nc * T.sizeof);
foreach (i; 0..length) {
nd[i] = this[i];
}
d = nd; st = 0; cap = nc;
}
void clear() {
st = length = 0;
}
void insertFront(T v) {
if (length == cap) expand();
if (st == 0) st += cap;
st--; length++;
this[0] = v;
}
void insertBack(T v) {
if (length == cap) expand();
length++;
this[length-1] = v;
}
void removeFront() {
assert(!empty, "Deque.removeFront: Deque is empty");
st++; length--;
if (st == cap) st = 0;
}
void removeBack() {
assert(!empty, "Deque.removeBack: Deque is empty");
length--;
}
}
struct RangeT(A) {
alias T = typeof(*(A.p));
alias E = typeof(A.p.d[0]);
T *p;
size_t a, b;
@property bool empty() const { return b <= a; }
@property size_t length() const { return b-a; }
@property RangeT save() { return RangeT(p, a, b); }
@property RangeT!(const A) save() const {
return typeof(return)(p, a, b);
}
alias opDollar = length;
@property ref inout(E) front() inout { return (*p)[a]; }
@property ref inout(E) back() inout { return (*p)[b-1]; }
void popFront() {
version(assert) if (empty) throw new RangeError();
a++;
}
void popBack() {
version(assert) if (empty) throw new RangeError();
b--;
}
ref inout(E) opIndex(size_t i) inout { return (*p)[i]; }
RangeT opSlice() { return this.save; }
RangeT opSlice(size_t i, size_t j) {
version(assert) if (i > j || a + j > b) throw new RangeError();
return typeof(return)(p, a+i, a+j);
}
RangeT!(const A) opSlice() const { return this.save; }
RangeT!(const A) opSlice(size_t i, size_t j) const {
version(assert) if (i > j || a + j > b) throw new RangeError();
return typeof(return)(p, a+i, a+j);
}
}
alias Range = RangeT!Deque;
alias ConstRange = RangeT!(const Deque);
alias ImmutableRange = RangeT!(immutable Deque);
Payload* p;
private void I() { if (mayNull && !p) p = new Payload(); }
private void C() const {
version(assert) if (mayNull && !p) throw new RangeError();
}
static if (!mayNull) {
@disable this();
}
//some value
private this(Payload* p) {
this.p = p;
}
this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) {I;
p = new Payload();
foreach (v; values) {
insertBack(v);
}
}
//range
this(Range)(Range r)
if (isInputRange!Range &&
isImplicitlyConvertible!(ElementType!Range, T) &&
!is(Range == T[])) {I;
p = new Payload();
foreach (v; r) {
insertBack(v);
}
}
static Deque make() { return Deque(new Payload()); }
@property bool havePayload() const { return (!mayNull || p); }
@property bool empty() const { return (!havePayload || p.empty); }
@property size_t length() const { return (havePayload ? p.length : 0); }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; }
ref inout(T) front() inout {C; return (*p)[0]; }
ref inout(T) back() inout {C; return (*p)[$-1]; }
void clear() { if (p) p.clear(); }
void insertFront(T v) {I; p.insertFront(v); }
void insertBack(T v) {I; p.insertBack(v); }
void removeFront() {C; p.removeFront(); }
void removeBack() {C; p.removeBack(); }
Range opSlice() {I; return Range(p, 0, length); }
}
unittest {
import std.algorithm : equal;
import std.range.primitives : isRandomAccessRange;
import std.container.util : make;
auto q = make!(Deque!int);
assert(isRandomAccessRange!(typeof(q[])));
//insert,remove
assert(equal(q[], new int[](0)));
q.insertBack(1);
assert(equal(q[], [1]));
q.insertBack(2);
assert(equal(q[], [1, 2]));
q.insertFront(3);
assert(equal(q[], [3, 1, 2]) && q.front == 3);
q.removeFront;
assert(equal(q[], [1, 2]) && q.length == 2);
q.insertBack(4);
assert(equal(q[], [1, 2, 4]) && q.front == 1 && q.back == 4 && q[$-1] == 4);
q.insertFront(5);
assert(equal(q[], [5, 1, 2, 4]));
//range
assert(equal(q[][1..3], [1, 2]));
assert(equal(q[][][][], q[]));
//const range
const auto rng = q[];
assert(rng.front == 5 && rng.back == 4);
//reference type
auto q2 = q;
q2.insertBack(6);
q2.insertFront(7);
assert(equal(q[], q2[]) && q.length == q2.length);
//construct with make
auto a = make!(Deque!int)(1, 2, 3);
auto b = make!(Deque!int)([1, 2, 3]);
assert(equal(a[], b[]));
}
unittest {
import std.algorithm : equal;
import std.range.primitives : isRandomAccessRange;
import std.container.util : make;
auto q = make!(Deque!int);
q.clear();
assert(equal(q[], new int[0]));
foreach (i; 0..100) {
q.insertBack(1);
q.insertBack(2);
q.insertBack(3);
q.insertBack(4);
q.insertBack(5);
assert(equal(q[], [1,2,3,4,5]));
q.clear();
assert(equal(q[], new int[0]));
}
}
unittest {
Deque!int a;
Deque!int b;
a.insertFront(2);
assert(b.length == 0);
}
unittest {
import std.algorithm : equal;
import std.range : iota;
Deque!int a;
foreach (i; 0..100) {
a.insertBack(i);
}
assert(equal(a[], iota(100)));
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
if (f.eof) return false;
line = lineBuf[];
f.readln(line);
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
//string or char[10] etc
//todo optimize
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else {
auto buf = line.split.map!(to!E).array;
static if (isStaticArray!T) {
//static
assert(buf.length == T.length);
}
x = buf;
line.length = 0;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
fout.writeln("1 2 3");
fout.writeln("ab cde");
fout.writeln("1.0 1.0 2.0");
fout.close;
Scanner sc = new Scanner(File(fileName, "r"));
int a;
int[2] b;
char[2] c;
string d;
double e;
double[] f;
sc.read(a, b, c, d, e, f);
assert(a == 1);
assert(equal(b[], [2, 3]));
assert(equal(c[], "ab"));
assert(equal(d, "cde"));
assert(e == 1.0);
assert(equal(f, [1.0, 2.0]));
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File, writeln;
import std.datetime;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
foreach (i; 0..1_000_000) {
fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2);
}
fout.close;
writeln("Scanner Speed Test(3*1,000,000 int)");
StopWatch sw;
sw.start;
Scanner sc = new Scanner(File(fileName, "r"));
foreach (i; 0..500_000) {
int a, b, c;
sc.read(a, b, c);
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
foreach (i; 500_000..700_000) {
int[3] d;
sc.read(d);
int a = d[0], b = d[1], c = d[2];
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
foreach (i; 700_000..1_000_000) {
int[] d;
sc.read(d);
assert(d.length == 3);
int a = d[0], b = d[1], c = d[2];
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
writeln(sw.peek.msecs, "ms");
}
|
D
|
import std.algorithm;
import std.array;
import std.bigint;
import std.bitmanip;
import std.conv;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; }
T[] readToArray(T)() {
return readln.split.to!(T[]);
}
void readInto(T...)(ref T ts) {
auto ss = readln.split;
foreach(ref t; ts) {
t = ss.front.to!(typeof(t));
ss.popFront;
}
}
// 冪乗をmod取りつつ計算
ulong modPow(ulong a, ulong n, ulong m) {
ulong r = 1;
while (n > 0) {
if(n % 2 != 0) r = r * a % m;
a = a * a % m;
n /= 2;
}
return r;
}
// フェルマーの小定理から乗法逆元を計算
// 定理の要請により法は素数
ulong modInv(ulong a, ulong m) {
return modPow(a, m-2, m);
}
// mod取りつつ組み合わせを計算
// modInvを使っているので法は素数
ulong modComb(ulong n, ulong r, ulong m) {
if (n == r) return 1;
if (r == 0) return 1;
if (n < r) return 0;
ulong up = 1;
ulong down = 1;
for(ulong i = n-r+1; i <= n; i++) {
up *= i;
up %= m;
}
for(ulong i = 2; i <= r; i++) {
down *= i;
down %= m;
}
return up*modInv(down, m) % m;
}
ulong MOD = 1000000007;
void main() {
ulong n, k;
readInto(n, k);
for (ulong i = 1; i <= k; i++) {
writeln(modComb(n-k+1, i, MOD)*modComb(k-1, i-1, MOD) % MOD);
}
}
|
D
|
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
bool solve(int L, int N, int R, in string S, in string T, char plus, char minus) {
int a = 1, b = L;
foreach_reverse (i; 0 .. N) {
if (T[i] == plus) {
if (a > 1) {
--a;
}
} else if (T[i] == minus) {
if (b < L) {
++b;
}
}
if (S[i] == plus) {
--b;
} else if (S[i] == minus) {
++a;
}
if (a > b) {
return false;
}
}
return (a <= R && R <= b);
}
void main() {
try {
for (; ; ) {
const H = readInt();
const W = readInt();
const N = readInt();
const RX = readInt();
const RY = readInt();
const S = readToken();
const T = readToken();
const resX = solve(H, N, RX, S, T, 'D', 'U');
const resY = solve(W, N, RY, S, T, 'R', 'L');
writeln((resX && resY) ? "YES" : "NO");
}
} catch (EOFException e) {
}
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto a = readln.chomp.split.to!(int[]);
auto x = a[0] + a[1];
auto y = a[0] * a[1];
if (x == 15) writeln("+");
else if (y == 15) writeln("*");
else writeln("x");
}
|
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, std.bitmanip;
bool ok(ref long[] A, ref long[] B, long n) {
long s = n * (n+1) / 2;
if (A.sum % s != 0)
return false;
long m = A.sum / s;
long x = 0;
//writeln(x, " ", m);
foreach (i; 0..n) {
//writeln(m, " ", B[i], " ", m - B[i]);
if ((m + B[i]) % n != 0)
return false;
long d = (m + B[i]) / n;
if (d < 0) return false;
x += d;
}
return x == m;
}
void main() {
auto N = readln.chomp.to!long;
auto A = readln.split.map!(to!long).array;
auto B = new long[](N);
foreach (i; 0..N) {
B[i] = A[i] - A[(i+1)%N];
}
writeln(ok(A, B, N) ? "YES" : "NO");
}
|
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!(long[]);
auto X = xy[0];
auto Y = xy[1];
writeln(abs(X - Y) <= 1 ? "Brown" : "Alice");
}
|
D
|
import std.stdio;
import std.string;
void main(){
string a,s=readln.chomp;
for(int i=0;i<s.length;++i){
if('a'<=s[i]&&s[i]<='z')a~=s[i]-'a'+'A';
else if('A'<=s[i]&&s[i]<='Z')a~=s[i]-'A'+'a';
else a~=s[i];
}
writeln(a);
}
|
D
|
import std.stdio;
import std.algorithm;
import std.range;
import std.array;
import std.string;
import std.conv;
void main(){
auto n = readln.chomp.to!long;
writeln(get_lucas(n));
}
long get_lucas(long n, long pre = 2, long after = 1){
if(n == 1){
return after;
}else if(n == 0){
return 2;
}else{
return get_lucas(n-1, after, pre+after);
}
}
|
D
|
// Try Codeforces
// author: Leonardone @ NEETSDKASU
import std.stdio : readln, writeln;
import std.string : chomp;
import std.array : split;
import std.conv : to;
import std.algorithm : sum;
auto gets() { return readln.chomp; }
auto getNum(T)() { return gets.to!T; }
auto getVals(T)() { return gets.split.to!(T[]); }
void main() {
auto n = getNum!int;
int[6] xs;
for (int i = 0; i < 6; i++) {
xs[i] = n % 10;
n = (n - n % 10) / 10;
}
auto check = () => sum(xs[0..3]) == sum(xs[3..$]);
if (check()) {
writeln(0);
return;
}
foreach (i; 0..6) {
auto backup = xs[i];
foreach (j; 0..10) {
xs[i] = j;
if (check()) {
writeln(1);
return;
}
}
xs[i] = backup;
}
foreach (i; 0..5) {
auto backupA = xs[i];
foreach (a; 0..10) {
xs[i] = a;
foreach (j; i+1..6) {
auto backupB = xs[j];
foreach (b; 0..10) {
xs[j] = b;
if (check()) {
writeln(2);
return;
}
}
xs[j] = backupB;
}
}
xs[i] = backupA;
}
writeln(3);
}
|
D
|
import std;
void main()
{
int t;
scanf("%d", &t);
getchar();
foreach(_; 0..t)
{
int k;
scanf("%d\n", &k);
int[] s = readln.strip.split.to!(int[]);
auto end = countUntil(retro(s), 0);
auto start = countUntil(s, 0);
if (start == -1)
writeln(0);
else
writeln(k - start - end + 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)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new long[](t);
foreach (ti; 0..t)
{
auto _n = RD!(char[]);
_n.reverse;
auto n = _n.idup;
foreach (i; 0..2^^n.length)
{
if (i >> max(0, cast(int)(n.length)-2)) continue;
long cnt = 1;
foreach (j; 0..n.length)
{
long x = n[j] - '0';
if (j >= 2)
if (i & (1L << (j-2)))
--x;
long c;
if (i & (1L << j))
x += 10;
foreach (k; 0..10)
{
if (inside(x - k, 0, 10))
{
++c;
debug writeln("i:", i, " j:", j, " x:", x, " k:", k);
}
}
cnt *= c;
}
if (i == 0)
{
if (cnt != 0)
ans[ti] += cnt-2;
}
else
ans[ti] += cnt;
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.datetime;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto L = s[1].to!long;
auto C = readln.split.map!(to!long).array;
foreach (i; 0..N-1) C[i+1] = min(C[i]*2, C[i+1]);
auto X = L >> (N-1);
auto Y = L - (X << (N-1));
long tmp = X * C[N-1];
long ans = 1L << 60;
for (int i = N-2; i >= 0; --i) {
if (Y == 0) break;
if (Y & (1L << i)) {
for (int j = N-1; j > i; --j) {
ans = min(ans, tmp + C[j]);
}
tmp += C[i];
}
}
ans = min(ans, tmp);
ans.writeln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto a = RD;
auto b = RD;
auto k = RD;
string ans1, ans2;
if (a == 0)
{
if (k == 0)
{
foreach (i; 0..b)
{
ans1 ~= '1';
ans2 ~= '1';
}
}
}
else if (b == 1)
{
if (k == 0)
{
ans1 ~= '1';
ans2 ~= '1';
foreach (i; 0..a)
{
ans1 ~= '0';
ans2 ~= '0';
}
}
}
else if (k == 0)
{
foreach (i; 0..b)
{
ans1 ~= '1';
ans2 ~= '1';
}
foreach (i; 0..a)
{
ans1 ~= '0';
ans2 ~= '0';
}
}
else if (k <= a + b - 2)
{
auto d = k-1;
ans1 ~= "11";
ans2 ~= "10";
auto aa = min(a-1, d);
foreach (i; 0..aa)
{
ans1 ~= '0';
ans2 ~= '0';
}
d -= aa;
foreach (i; 0..d)
{
ans1 ~= '1';
ans2 ~= '1';
}
ans1 ~= '0';
ans2 ~= '1';
auto rem_a = a - 1 - aa;
auto rem_b = b - 2 - d;
foreach (i; 0..rem_a)
{
ans1 ~= '0';
ans2 ~= '0';
}
foreach (i; 0..rem_b)
{
ans1 ~= '1';
ans2 ~= '1';
}
}
if (ans1.empty)
writeln("No");
else
{
writeln("Yes");
writeln(ans1);
writeln(ans2);
}
stdout.flush;
debug readln;
}
|
D
|
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.utf;
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto n = readln.strip.to !(int);
auto s = readln.strip;
alias mirror = len => s[0..len] ~ s[0..len].byChar.retro.array;
int len = 1;
bool first = true;
int [] cand = [1, n];
foreach (i; 0..n - 1)
{
if (s[i + 1] < s[i])
{
cand ~= i + 1;
}
if (s[i + 1] > s[i])
{
cand ~= i + 1;
break;
}
}
debug {writeln (cand);}
cand.map !(mirror).minElement.writeln;
}
}
|
D
|
import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;
import std.numeric;
import std.bigint;
import core.checkedint;
void main()
{
int n = readln.chomp.to!int;
long[] s = readln.chomp.split.map!(to!long).array;
int[] c = readln.chomp.split.map!(to!int).array;
long[] sub = new long[](n);
foreach (i; 0..n) {
int v = int.max;
foreach (j; i + 1 .. n) {
if (s[i] >= s[j]) {
continue;
}
v = min(v, c[j]);
}
sub[i] = v;
}
long ans = int.max;
foreach (i; 0..n) {
foreach (j; i + 1 .. n) {
if (s[i] >= s[j]) {
continue;
}
debug verbose(i, j, sub[j], c[i] + c[j] + sub[j]);
ans = min(ans, c[i] + c[j] + sub[j]);
}
}
if (ans >= int.max) {
writeln = -1;
} else {
ans.writeln;
}
}
void tie(R, Args...)(R arr, ref Args args)
if (isRandomAccessRange!R || isArray!R)
in
{
assert (arr.length == args.length);
}
body
{
foreach (i, ref v; args) {
alias T = typeof(v);
v = arr[i].to!T;
}
}
void verbose(Args...)(in Args args)
{
stderr.write("[");
foreach (i, ref v; args) {
if (i) stderr.write(", ");
stderr.write(v);
}
stderr.writeln("]");
}
|
D
|
import std.stdio, std.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(){
A: foreach(_; 0 .. scan!int){
string s = scan;
int n = s.length.to!int;
int a = 0, b = n - 1;
while(a < b){
log("a:", a, "b:", b, "s[a]:", s[a], "s[b]:", s[b]);
if(s[a] != s[b]) break;
a ++, b --;
}
if(a >= b){
s.writeln;
continue A;
}
log("a:", a, "b:", b);
bool isPalin(int u, int v){ // s[u .. v] is palin.
log("u:", u, "v:", v, "s[u..v]:", s[u .. v]);
int i = u, j = v - 1;
while(i < j){
if(s[i] != s[j]) return 0;
i ++, j --;
}
return 1;
}
foreach_reverse(i; 0 .. b - a + 1){
if(isPalin(a, a + i)){
(s[0 .. a + i] ~ s[b + 1 .. $]).writeln;
continue A;
}
if(isPalin(b - i + 1, b + 1)){
(s[0 .. a] ~ s[b - i + 1 .. $]).writeln;
continue A;
}
}
}
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto K = s[1];
auto S = readln.chomp;
auto st = new int[](N*2);
int p = -1;
int cnt = 0;
int front = 0;
foreach (i; 0..N) {
if (S[i] == '(') {
st[++p] = i;
} else if (cnt < N - K) {
cnt += 2;
p -= 1;
} else {
st[++p] = i;
}
}
foreach (i; 0..p+1) {
write(S[st[i]]);
}
writeln;
}
|
D
|
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto n = readln.strip.to !(int);
auto a = readln.splitter.map !(to !(int)).array;
long res = 0;
foreach (i; 1..n)
{
res += max (0, a[i - 1] - a[i]);
}
writeln (res);
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new int[](t);
foreach (ti; 0..t)
{
auto n = RD!string;
ans[ti] = cast(int)n.length;
}
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; }
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 t = RD!int;
auto ans = new string[](t);
foreach (ti; 0..t)
{
auto s = RD!(char[]);
auto c = RD!string;
auto f = new bool[][](s.length+1, 26);
foreach_reverse (i; 0..s.length)
{
f[i] = f[i+1].dup;
auto p = s[i] - 'A';
f[i][p] = true;
}
(){
foreach (i; 0..s.length)
{
auto p = s[i] - 'A';
foreach (j; 0..p)
{
if (f[i][j])
{
foreach_reverse (k; 0..s.length)
{
auto p2 = s[k] - 'A';
if (p2 == j)
{
swap(s[i], s[k]);
return;
}
}
}
}
}}();
bool ok = true;
size_t last = size_t.max;
auto len = min(s.length, c.length);
foreach (i; 0..len)
{
if (s[i] < c[i])
{
ok = true;
break;
}
else if (s[i] > c[i])
{
ok = false;
break;
}
last = i;
}
if (ok && last == len-1)
{
ok = s.length < c.length;
}
if (ok)
ans[ti] = s.idup;
else
ans[ti] = "---";
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.container, std.math;
immutable int mod = 10^^9 + 7;
long n, s;
void main() {
readVariables(n, s);
long top, btm, mid;
if (n - dsum(n) < s) {
writeln(0);
return;
}
top = n;
while (top - btm > 1) {
mid = btm + (top - btm) / 2;
debug {
writeln(mid, " ", dsum(mid));
}
if (mid - dsum(mid) >= s) {
top = mid;
}
else {
btm = mid;
}
}
long ans = n - top + 1;
writeln(ans);
}
long dsum(long mid) {
return mid > 0 ? dsum(mid / 10) + mid % 10 : 0;
}
void readVariables(T...)(ref T args) {
string[] 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.conv, std.stdio;
import std.string;
void main()
{
auto n = readln.chomp.to!int;
long s;
foreach (i; 1..n+1)
if (i % 3 && i % 5)
s += i;
s.writeln;
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n, m, k; rd(n, m, k);
foreach(i; 0..n+1)foreach(j; 0..m+1){
int s=0;
s+=i*m;
s-=j*i;
s+=j*(n-i);
if(s==k){writeln("Yes"); return;}
}
writeln("No");
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
|
D
|
// import chie template :) {{{
import std.stdio,
std.algorithm,
std.array,
std.string,
std.math,
std.conv,
std.range,
std.container,
std.bigint,
std.ascii;
// }}}
// tbh.scanner {{{
class Scanner {
import std.stdio;
import std.conv : to;
import std.array : split;
import std.string : chomp;
private File file;
private dchar[][] str;
private uint idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
private dchar[] next() {
if (idx < str.length) {
return str[idx++];
}
dchar[] s;
while (s.length == 0) {
s = file.readln.chomp.to!(dchar[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(uint len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
}
// }}}
void main() {
auto cin = new Scanner;
string s = cin.next!string, t = cin.next!string;
int n = (s ~ t).to!int;
foreach (i; 1 .. 1000) {
if (i * i == n) {
writeln("Yes");
return ;
}
}
writeln("No");
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}}
void main()
{
int n; readV(n);
int[] t, a; readC(n, t, a);
auto ta = new long[](n), aa = new long[](n);
ta[0] = t[0]; aa[0] = a[0];
foreach (i; 1..n) {
auto k = max((ta[i-1]+t[i]-1)/t[i], (aa[i-1]+a[i]-1)/a[i]);
ta[i] = k*t[i]; aa[i] = k*a[i];
}
writeln(ta[$-1]+aa[$-1]);
}
|
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;
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;
auto dp = new long[][](S.length+1, 4);
dp[0][0] = 1;
foreach (i, c; S)
{
dp[i+1][] = dp[i][];
if (c == '?')
{
dp[i+1][0].moda(dp[i][0]);
dp[i+1][0].moda(dp[i][0]);
foreach (j; 0..3)
{
dp[i+1][j+1].moda(dp[i][j]);
dp[i+1][j+1].moda(dp[i][j+1]);
dp[i+1][j+1].moda(dp[i][j+1]);
}
}
else
{
auto j = c - 'A';
dp[i+1][j+1].moda(dp[i][j]);
}
}
//stderr.writeln(dp);
writeln(dp[S.length][$-1]);
stdout.flush();
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
foreach (i, x; readln.split.to!(int[])) if (x == 0) {
writeln(i+1);
return;
}
}
|
D
|
import std.functional,
std.algorithm,
std.bigint,
std.string,
std.traits,
std.array,
std.range,
std.stdio,
std.conv;
void main() {
string[] XY = readln.chomp.split.to!(string[]);
string X = XY[0],
Y = XY[1];
if (X < Y) {
writeln("<");
} else if (X > Y) {
writeln(">");
} else {
writeln("=");
}
}
|
D
|
void main() {
auto N = readln.chomp.to!int;
dchar last;
string[] m;
foreach(i; 0..N) {
auto S = readln.chomp;
if(i==0) {
m ~= S;
last = S.back;
continue;
}
if(last == S[0]) {
last = S.back;
} else {
writeln("No");
return;
}
foreach(j; m) {
if(S == j) {
writeln("No");
return;
}
}
m ~= S;
}
writeln("Yes");
}
// ===================================
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
|
// url: http://tenka1-2017-beginner.contest.atcoder.jp/tasks/tenka1_2017_a
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto s = readln.chomp;
writeln(s.count('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;
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 a, p; scan(a, p);
int ans = (p + a * 3) / 2;
writeln(ans);
}
|
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 memo = new long[](N + 1);
memo[] = -1;
memo[0] = 0;
long solve(long n)
{
if (memo[n] != -1)
return memo[n];
long i = 1;
long ans = long.max;
while (i <= n)
{
ans = ans.min(solve(n - i) + 1);
i *= 6;
}
i = 1;
while (i <= n)
{
ans = ans.min(solve(n - i) + 1);
i *= 9;
}
memo[n] = ans;
return ans;
}
solve(N).writeln();
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
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 a, b, c; scan(a, b, c);
int ans = min(b / a, c);
writeln(ans);
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto n = readln.chomp.to!int;
auto g = new int[][](n);
foreach (i; 0..n) {
auto s = readln.split.map!(to!int).array;
auto k = s.front;
foreach (j; 1..k+1) {
g[i] ~= s[j];
g[s[j]] ~= i;
}
}
auto lca = new LowestCommonAncestor(g, 0);
auto q = readln.chomp.to!int;
while (q--) {
auto s = readln.split.map!(to!int);
auto x = s[0];
auto y = s[1];
writeln(lca.lca(x, y));
}
}
class LowestCommonAncestor {
import std.algorithm : swap;
import std.conv : to;
import std.typecons : Tuple, tuple;
import core.bitop : bsr;
int n, root, lgn;
int[][] graph;
int[] depth;
int[][] dp;
this(const int[][] graph, int root) {
n = graph.length.to!int;
this.root = root;
this.graph = new int[][](n);
foreach (i; 0..n) this.graph[i] = graph[i].dup;
lgn = bsr(n) + 3;
depth = new int[](n);
dp = new int[][](lgn, n);
construct;
}
int lca(int a, int b) {
if (depth[a] < depth[b]) swap(a, b);
int diff = depth[a] - depth[b];
foreach_reverse (i; 0..lgn) if (diff & (1<<i)) a = dp[i][a];
if (a == b) return a;
int K = lgn;
while (dp[0][a] != dp[0][b]) {
foreach_reverse (k; 0..lgn) {
if (dp[k][a] != dp[k][b]) {
a = dp[k][a];
b = dp[k][b];
K = k;
}
}
}
return dp[0][a];
}
private void construct() {
auto stack = new Tuple!(int, int, int)[](n+10);
int sp = 0;
stack[0] = tuple(root, -1, 0);
while (sp >= 0) {
auto u = stack[sp][0];
auto p = stack[sp][1];
auto d = stack[sp][2];
sp -= 1;
dp[0][u] = p;
depth[u] = d;
foreach (v; graph[u]) if (v != p) stack[++sp] = tuple(v, u, d+1);
}
foreach (k; 0..lgn-1)
foreach (i; 0..n)
dp[k+1][i] = (dp[k][i] == -1) ? -1 : dp[k][dp[k][i]];
}
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
while (true) {
int n;
scan(n);
if (!n) return;
auto a = readln.split;
if (n&1) {
n--;
a.popBack;
}
int ans;
foreach (i ; 0 .. n/2) {
ans += (a[2*i] == "lu" && a[2*i + 1] == "ru" || a[2*i] == "ru" && a[2*i+1] == "lu"
|| a[2*i] == "ld" && a[2*i+1] == "rd" || a[2*i] == "rd" && a[2*i+1] == "ld");
}
writeln(ans);
}
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
void main() {
problem();
}
void problem() {
auto N = scan!long, M = scan!long;
long solve() {
long answer;
if (N > 0) answer += N*(N-1)/2;
if (M > 0) answer += M*(M-1)/2;
return answer;
}
solve().writeln;
}
// ----------------------------------------------
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
// -----------------------------------------------
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
auto S = readln.chomp;
int x = 0;
int cnt = 0;
while (x+1 < S.length) {
if (S[x] == S[x+1]) {
x++;
} else {
S = S[0..x] ~ S[x+2..$];
cnt += 2;
x = max(x-1, 0);
}
}
writeln(cnt);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto dg = readln.split.to!(long[]);
auto D = dg[0];
auto G = dg[1];
long[] ps, cs;
foreach (_; 0..D) {
auto pc = readln.split.to!(long[]);
ps ~= pc[0];
cs ~= pc[1];
}
long r = long.max;
foreach (x; 0..(1<<D)) {
long g, rr;
foreach (i; 0..D) if (x & (1<<i)) {
rr += ps[i];
g += 100*(i+1)*ps[i] + cs[i];
}
foreach_reverse (i; 0..D) if (!(x & (1<<i))) {
foreach (_; 0..ps[i]) {
if (g >= G) break;
rr += 1;
g += 100*(i+1);
}
}
if (g >= G) {
r = min(r, rr);
}
}
writeln(r);
}
|
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, std.numeric;
void main(){
auto nt=readln.split.map!(to!long).array;
auto ts=readln.split.map!(to!long).array;
auto T=nt[1];
long last=0;
long sum=0;
foreach(t;ts){
sum+=min(T,t-last);
last=t;
}
sum+=T;
writeln(sum);
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
writeln("Christmas", " Eve".repeat(25 - readln.chomp.to!int).join);
}
|
D
|
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
immutable int steps = 10;
int solve (string s, int team)
{
int [2] goals;
foreach (step; 0..steps)
{
auto cur = (s[step] == '0') ? 0 :
(s[step] == '1') ? 1 : (step % 2 == team);
goals[step % 2] += cur;
if (goals[0] + (steps - 1 - step + 0) / 2 < goals[1] ||
goals[1] + (steps - 1 - step + 1) / 2 < goals[0])
{
return step + 1;
}
}
return steps;
}
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto s = readln.strip;
writeln (min (solve (s, 0), solve (s, 1)));
}
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.numeric;
void main()
{
auto n = readln.chomp.to!int;
auto h = readln.chomp.split.to!(int[]);
bool f;
foreach (i; 1..n) {
auto d = h[i] - h[i-1];
if (d == -1) {
h[i]++;
} else if (d < 0) {
f = true;
break;
}
}
if (f) {
writeln("No");
} else {
writeln("Yes");
}
}
|
D
|
import std.conv;
import std.stdio;
import std.string;
void main() {
auto n = readln.chomp.to!int;
(n%2==0 ? n : 2*n).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 size = readln.chomp.to!int;
auto numbers = readln.split.to!(int[]);
void solve() {
int next = 1;
int break_count;
foreach(n; numbers) {
if (next != n) {
break_count++;
continue;
}
next++;
}
writeln(next == 1 ? -1 : break_count);
}
solve();
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.math;
void main() {
auto num = readln().chomp().to!int();
writeln(pow(num, 3));
}
|
D
|
import std.stdio,
std.conv,
std.string;
void main()
{
int a,b,c;
scanf("%d %d %d", &a, &b, &c);
if (a <= c && c <= b) {
writeln("Yes");
}
else {
writeln("No");
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto n = RD!int;
auto k = RD!int;
auto x = cast(int)sqrt(cast(double)k);
long ans;
foreach (i; x..n+2)
{
auto j = cast(long)i;
auto j2 = cast(long)(i-1);
auto y = j * j2 / 2;
if (j * j2 < k) continue;
if (y - k == n - j2)
{
debug writeln(i);
ans = n - j2;
break;
}
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
int count(int n) {
int ans = 0;
for(int a = 0; a < 10; a++)
for(int b = 0; b < 10; b++)
for(int c = 0; c < 10; c++)
for(int d = 0; d < 10; d++)
if(a + b + c + d == n) ans++;
return ans;
}
void main() {
int[] data;
while(true) {
auto input = readln();
if(stdin.eof()) break;
data ~= to!int(chomp(input));
}
foreach(i ; data) {
writeln(count(i));
}
}
|
D
|
import std.stdio,std.string,std.conv;
void main(){
for(;;){
auto rc = readln().chomp();
if( rc == "0" ){ break; }
int sum=0;
foreach( c ; rc ){
sum += c-'0';
}
writeln(sum);
}
}
|
D
|
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
bool solve (int n, int [] a)
{
n = min (n, 30);
a = a[0..n];
remove_loop:
while (!a.empty)
{
foreach_reverse (i; 0..n)
{
if (a[i] % (i + 2) != 0)
{
a = a[0..i] ~ a[i + 1..$];
n -= 1;
continue remove_loop;
}
}
return false;
}
return true;
}
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto n = readln.strip.to !(int);
auto a = readln.splitter.map !(to !(int)).array;
writeln (solve (n, a) ? "YES" : "NO");
}
}
|
D
|
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math, std.container, std.typecons;
import core.stdc.stdio;
// foreach, foreach_reverse, writeln
void main() {
const int MOD = 1_000_000_007;
int n;
scanf("%d", &n);
int[] cb = new int[n+1];
cb[0] = 1;
foreach (i; 2..n+1) {
if (i&1) continue;
cb[i] = long(cb[i-2])*(i-1)%MOD;
}
int[][] e = new int[][](n);
foreach (i; 0..n-1) {
int x, y;
scanf("%d%d", &x, &y);
x--; y--;
e[x] ~= y;
e[y] ~= x;
}
int[][] dfs(int v, int par=-1) {
int[][] dp = new int[][](2,2);
dp[0][1] = 1;
foreach (u; e[v]) {
if (u == par) continue;
auto d = dfs(u,v);
int nv = dp[0].length.to!int;
int nu = d[0].length.to!int;
int[][] p = new int[][](2,nv+nu-1);
swap(dp,p);
foreach (bi; 0..2) foreach (i; 0..nv) {
foreach (bj; 0..2) foreach (j; 0..nu) {
(dp[bi^bj][i+j] += long(p[bi][i])*d[bj][j]%MOD) %= MOD;
}
}
}
foreach (bi; 0..2) foreach (i; 2..dp[0].length) {
if (i&1) continue;
(dp[bi^1][0] += long(dp[bi][i])*cb[i]%MOD) %= MOD;
}
return dp;
}
auto dp = dfs(0);
int ans = (dp[1][0]-dp[0][0]+MOD)%MOD;
writeln(ans);
}
|
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 S, W;
sc.scan(S, W);
if (S <= W) writeln("unsafe");
else writeln("safe");
}
|
D
|
import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;
import std.numeric;
import std.bigint;
import core.checkedint;
import core.bitop;
import std.digest.sha;
void main()
{
int n, k;
readln.chomp.split.tie(n, k);
writeln = n % k != 0 ? 1 : 0;
}
void tie(R, Args...)(R arr, ref Args args)
if (isRandomAccessRange!R || isArray!R)
in
{
assert (arr.length == args.length);
}
body
{
foreach (i, ref v; args) {
alias T = typeof(v);
v = arr[i].to!T;
}
}
void verbose(Args...)(in Args args)
{
stderr.write("[");
foreach (i, ref v; args) {
if (i) stderr.write(", ");
stderr.write(v);
}
stderr.writeln("]");
}
|
D
|
import std.stdio, std.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 File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto S = RD!string;
long ans;
if (S == "SUN")
ans = 7;
else if (S == "MON")
ans = 6;
else if (S == "TUE")
ans = 5;
else if (S == "WED")
ans = 4;
else if (S == "THU")
ans = 3;
else if (S == "FRI")
ans = 2;
else
ans = 1;
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto n = readln.chomp.to!int;
auto m = n;
int cnt;
foreach_reverse(i; 0..10) {
auto e = 10 ^^ i;
auto d = n / e;
n %= e;
cnt += d;
}
writeln(m % cnt ? "No" : "Yes");
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.ascii;
void main()
{
auto s = readln.chomp;
int[char] cnt;
foreach (e; s) {
cnt[e]++;
}
if (cnt.keys.length == 2 && cnt[cnt.keys[0]] == 2) {
writeln("Yes");
} else {
writeln("No");
}
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd = readln.split.to!(int[]), h = rd[0], w = rd[1];
auto n = readln.chomp.to!size_t;
auto a = readln.split.to!(int[]);
auto k = 0;
foreach (i; 0..h) {
auto r = new int[](w), j = 0;
for (;;) {
auto m = min(a[k], w-j);
r[j..j+m][] = k+1;
j += m;
a[k] -= m;
if (a[k] == 0) ++k;
if (j == w) break;
}
if (i % 2 == 0)
foreach (c, ri; r) write(ri, c < w-1 ? " " : "");
else
foreach_reverse (c, ri; r) write(ri, c > 0 ? " " : "");
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 = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void main()
{
auto S = RD!string;
string ans;
foreach (i; 0..S.length)
ans ~= "x";
writeln(ans);
stdout.flush;
debug readln;
}
|
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;
bool check(string s) {
if (s.length % 2) {
return false;
}
while (!s.empty) {
if (s[0 .. 2] != "hi") {
return false;
}
s.popFront();
s.popFront();
}
return true;
}
void main() {
string s;
scan(s);
yes(check(s));
}
void scan(T...)(ref T args) {
auto line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
|
D
|
import std;
int calc(int k) {
int sum = 0;
foreach (a; 1..k+1) {
foreach (b; 1..k+1) {
foreach (c; 1..k+1) {
sum += gcd(a, gcd(b, c));
}
}
}
return sum;
}
void main() {
int k; scan(k);
writeln(calc(k));
}
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;
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.math;
import std.algorithm;
void main(){
auto S = readln.split.to!(int[]);
auto A = S[0];
auto B = S[1];
if(A+B>=10) writeln("error");
else writeln (A+B);
}
|
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(int n) {
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
if (n == i * 4 + j * 7) {
return true;
}
}
}
return false;
}
void main() {
int n = readint;
writeln(calc(n) ? "Yes" : "No");
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii;
import std.typecons, std.functional;
import std.algorithm, std.container;
struct S{
long x=long.min;
long[] pair;
long[] cost;
}
S[] sList;
bool resolve(S s){
if(s.x==long.min)
{
s.x=0;
}
foreach(i; 0..s.pair.length)
{
if(sList[s.pair[i]].x==long.min)
{
sList[s.pair[i]].x = s.x + s.cost[i];
resolve(sList[s.pair[i]]);
}else{
if(sList[s.pair[i]].x != s.x + s.cost[i])
{
return false;
}
}
}
return true;
}
void main()
{
long N = scanElem;
long M = scanElem;
sList.length = N+1;
foreach(i; 0..M)
{
auto l = scanElem;
auto r = scanElem;
auto d = scanElem;
sList[l].pair ~= r;
sList[l].cost ~= d;
sList[r].pair ~= l;
sList[r].cost ~= d*-1;
}
foreach(s; sList)
{
if(!resolve(s))
{
writeln("No");
return;
}
}
writeln("Yes");
}
class UnionFind{
UnionFind parent = null;
void merge(UnionFind a)
{
if(same(a)) return;
a.root.parent = this.root;
}
UnionFind root()
{
if(parent is null)return this;
return parent = parent.root;
}
bool same(UnionFind a)
{
return this.root == a.root;
}
}
void scanValues(TList...)(ref TList list)
{
auto lit = readln.splitter;
foreach (ref e; list)
{
e = lit.fornt.to!(typeof(e));
lit.popFront;
}
}
T[] scanArray(T = long)()
{
return readln.split.to!(long[]);
}
void scanStructs(T)(ref T[] t, size_t n)
{
t.length = n;
foreach (ref e; t)
{
auto line = readln.split;
foreach (i, ref v; e.tupleof)
{
v = line[i].to!(typeof(v));
}
}
}
T scanElem(T = long)()
{
char[] res;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
res ~= cast(char) c;
c = getchar;
}
return res.strip.to!T;
}
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
{
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
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);
}
}
struct Factor
{
long n;
long c;
}
Factor[] factors(long n)
{
Factor[] res;
for (long i = 2; i ^^ 2 <= n; i++)
{
if (n % i != 0)
continue;
int c;
while (n % i == 0)
{
n = n / i;
c++;
}
res ~= Factor(i, c);
}
if (n != 1)
res ~= Factor(n, 1);
return res;
}
long[] primes(long n)
{
if(n<2)return [];
auto table = new long[n+1];
long[] res;
for(int i = 2;i<=n;i++)
{
if(table[i]==-1) continue;
for(int a = i;a<table.length;a+=i)
{
table[a] = -1;
}
res ~= i;
}
return res;
}
bool isPrime(long n)
{
if (n <= 1)
return false;
if (n == 2)
return true;
if (n % 2 == 0)
return false;
for (long i = 3; i ^^ 2 <= n; i += 2)
if (n % i == 0)
return false;
return true;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
import std.concurrency;
void times(alias fun)(int n) {
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
void main() {
int N = readln.chomp.to!int;
int M = 10^^6+1;
long[] dp = new long[M];
int[] ary = N.rep!(() => readln.chomp.to!int);
dp[0] = 1;
foreach(i; 0..N) {
foreach(j; M.iota.retro) {
if (j+ary[i] >= M) continue;
dp[j+ary[i]] += dp[j];
}
}
auto po = dp.enumerate.retro.find!(
a => a.value>0 && a.index%10!=0
);
writeln(po.empty ? 0 : po.front.index);
}
// ----------------------------------------------
// 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
|
void main() {
writeln(readln.chomp.to!int / 3);
}
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;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
void main()
{
long n, m;
scan(n, m);
auto ab = new long[][](n, 2);
auto cd = new long[][](m, 2);
// writeln(ab);
// writeln(cd);
auto ans = new long(100);
auto check_i = new long[](n);
foreach (i; 0 .. n)
{
long a, b;
scan(a, b);
ab[i][0] = a;
ab[i][1] = b;
}
// writeln(ab);
foreach (i; 0 .. m)
{
long c, d;
scan(c, d);
cd[i][0] = c;
cd[i][1] = d;
}
// writeln(cd);
foreach (i; 0 .. n)
{
long ans_x = 10 ^^ 9;
foreach (j; 0 .. m)
{
long tmp;
tmp = abs(ab[i][0] - cd[j][0]) + abs(ab[i][1] - cd[j][1]);
if (tmp < ans_x)
{
check_i[i] = j + 1;
ans_x = tmp;
}
}
}
foreach (i; 0 .. n)
{
writeln(check_i[i]);
}
}
//https://rclone.org/
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.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;
immutable long MOD = 10^^9 + 7;
void main() {
auto N = readln.chomp.to!int;
auto G = new int[][](N);
foreach (i; 0..N-1) {
auto s = readln.split.map!(to!int);
G[s[0]-1] ~= s[1]-1;
G[s[1]-1] ~= s[0]-1;
}
auto sub = new int[](N);
void dfs1(int n, int p) {
sub[n] = 1;
foreach (m ;G[n]) if (p != m) {
dfs1(m, n);
sub[n] += sub[m];
}
}
dfs1(0, -1);
long ans = 0;
void dfs2(int n, int p) {
if (G[n].length > 1) {
long prob = powmod(2, MOD-2, MOD);
long bb = 0;
long b0 = 1;
foreach (m; G[n]) {
long num = m == p ? N - sub[n] : sub[m];
long q = powmod(2, num, MOD);
b0 = b0 * powmod(q, MOD-2, MOD) % MOD;
}
bb = b0;
foreach (m; G[n]) {
long num = m == p ? N - sub[n] : sub[m];
long q = powmod(2, num, MOD);
long b1 = b0 * q % MOD;
q = (q - 1) * powmod(q, MOD-2, MOD) % MOD;
q = (q + MOD) % MOD;
b1 = b1 * q % MOD;
(bb += b1) %= MOD;
}
bb = (1 - bb) % MOD;
bb = (bb + MOD) % MOD;
(ans += prob * bb % MOD ) %= MOD;
}
foreach (m; G[n]) if (m != p) {
dfs2(m, n);
}
}
dfs2(0, -1);
ans.writeln;
}
long powmod(long a, long x, long m) {
long ret = 1;
while (x) {
if (x % 2) ret = ret * a % m;
a = a * a % m;
x /= 2;
}
return ret;
}
|
D
|
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std;
// dfmt on
void main()
{
long N = lread();
long[long] D;
foreach (i; 1 .. N + 1)
{
auto f = factorize(i);
foreach (k; f)
D[k] = D.get(k, 0) + 1;
}
long ans = 1;
foreach (k, v; D)
ans *= v + 1, ans %= MOD;
writeln(ans);
}
/// 素因数分解
T[] factorize(T = long)(T x)
{
assert(0 < x, "x is negative");
T[] result;
while ((x & 1) == 0)
x /= 2, result ~= 2;
for (T i = 3; i * i <= x; i += 2)
while (x % i == 0)
x /= i, result ~= i;
if (x != 1)
result ~= x;
return result;
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
immutable mod = 10^^9 + 7;
immutable inf = 2 * 10^^9;
void main() {
int n, k;
scan(n, k);
auto x = new int[](n), y = new int[](n);
foreach (i ; 0 .. n) {scan(x[i], y[i]);}
long ans = long.max;
bool[] mask = new bool[](n);
void dfs(int xl, int xr, int yb, int yt, int take) {
if (take == k || take == 4) {
int cnt;
foreach (i ; 0 .. n) {
if (xl <= x[i] && x[i] <= xr && yb <= y[i] && y[i] <= yt) cnt++;
}
if (cnt >= k) {
ans = min(ans, 1L * (xr - xl) * (yt - yb));
}
return;
}
foreach (i ; 0 .. n) {
if (!mask[i]) {
mask[i] = 1;
dfs(min(xl, x[i]), max(xr, x[i]), min(yb, y[i]), max(yt, y[i]), take + 1);
mask[i] = 0;
}
}
}
dfs(inf, -inf, inf, -inf, 0);
writeln(ans);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
void main(){
auto F=readln.split.to!(int[]),a=F[0],b=F[1];
if((a+b)%2==0)writeln((a+b)/2);
else writeln((a+b)/2+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;
char calc(string s, long k) {
int p = -1;
for (int i = 0; i < s.length; i++) {
if (s[i] == '1') p = i;
else break;
}
if (k <= p + 1) return '1';
return s[p+1];
}
void main() {
string s = read!string;
long k = read!long;
writeln(calc(s, k));
}
|
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() {
string s;
scan(s);
writeln(s.count('+').to!int - s.count('-').to!int);
}
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() {
import std.stdio, std.string, std.conv, std.algorithm;
int x, y, z;
rd(x, y, z);
writeln((x - z) / (y + z));
}
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
|
// 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);
auto G = new long[][](N);
foreach (_; 0 .. M)
{
long a, b;
scan(a, b);
a--, b--;
G[a] ~= b;
G[b] ~= a;
}
DList!long Q;
auto ans = new long[](N);
ans[] = -1;
ans[0] = 0;
Q.insertBack(0);
while (!Q.empty)
{
auto v = Q.front;
Q.removeFront();
foreach (w; G[v])
{
if (ans[w] == -1)
{
ans[w] = v;
Q.insertBack(w);
}
}
}
bool b = ans.all!"a != -1";
if (!b)
{
writeln("No");
return;
}
writeln("Yes");
foreach (a; ans[1 .. $])
{
writeln(a + 1);
}
}
|
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<=8&&b<=8)writeln("Yay!");
else writeln(":(");
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.