code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int n;
rd(n);
auto a = new char[][](n);
foreach (i; 0 .. n) {
a[i] = readln.chomp.to!(char[]);
}
if (a[0][0] == '.' || a[0][n - 1] == '.' || a[n - 1][0] == '.' || a[n - 1][n - 1] == '.') {
writeln("NO");
return;
}
for (int i = 1; i + 1 < n; i++) {
for (int j = 1; j + 1 < n; j++) {
if (a[i][j] == '.') {
if (a[i - 1][j] == '#' || a[i][j - 1] == '#' || a[i + 1][j] == '#' || a[i][j + 1] == '#') {
continue;
}
a[i][j] = a[i - 1][j] = a[i][j - 1] = a[i + 1][j] = a[i][j + 1] = '#';
}
}
}
foreach (i; 0 .. n) {
foreach (j; 0 .. n) {
if (a[i][j] == '.') {
writeln("NO");
return;
}
}
}
writeln("YES");
}
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
|
void main() {
int[] tmp = readln.split.to!(int[]);
int h = tmp[0], w = tmp[1];
string[] s = new string[h];
foreach (i; 0 .. h) {
s[i] = readln.chomp;
}
foreach (i; 0 .. 2*h) {
s[i/2].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.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;
void main() {
int N;
scan(N);
long ans;
foreach (i ; 1 .. N + 1) {
if (i % 3 != 0 && i % 5 != 0) {
ans += i;
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
auto line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
void main() {
int[] tmp = readln.split.to!(int[]);
int a = tmp[0], b = tmp[1];
max(a+b, a-b, a*b).writeln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto N = RD;
auto X = RD!string;
long pc;
foreach (i; 0..N)
{
if (X[i] == '1')
++pc;
}
debug writeln("pc:", pc);
if (pc == 0)
{
foreach (i; 0..N)
{
writeln(1);
}
}
else if (pc == 1)
{
foreach (i; 0..N)
{
if (X[i] == '1')
writeln(0);
else if (i == N-1)
writeln(2);
else
writeln(1);
}
}
else
{
auto to = new long[](N);
foreach (ulong i; 1..N+1)
{
auto c = popcnt(i);
to[i-1] = i % c;
}
auto rem = new long[][](N);
long x = 1, y = 1;
foreach (i; 0..N)
{
x %= pc-1;
y %= pc+1;
rem[i] = [x, y];
x *= 2;
y *= 2;
}
debug writeln(rem);
long z1, z2, _z;
foreach (i; 0..N)
{
if (X[i] == '1')
{
z1 += rem[N-i-1][0];
z2 += rem[N-i-1][1];
z1 %= pc-1;
z2 %= pc+1;
debug _z += 2^^(N-i-1);
}
}
foreach (i; 0..N)
{
long zz, _zz, _zzz;
if (X[i] == '1')
{
zz = (z1+pc-1) - rem[N-i-1][0];
//debug writeln("zz:", zz);
zz %= (pc-1);
debug _zz = _z - 2^^(N-i-1);
debug _zzz = (_zz % pc-1);
}
else
{
zz = z2 + rem[N-i-1][1];
//debug writeln("zz:", zz);
zz %= (pc+1);
debug _zz = _z + 2^^(N-i-1);
debug _zzz = (_zz % pc+1);
}
//debug writeln("_zz:", _zz, " _zzz:", _zzz);
//debug writeln("zz:", zz);
long ans = 1;
while (zz != 0)
{
zz = to[zz-1];
//debug writeln("zz:", zz);
++ans;
}
writeln(ans);
}
}
stdout.flush;
debug readln;
}
|
D
|
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.complex;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
auto readInts() {
return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
return readInts()[0];
}
auto readLongs() {
return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
return readLongs()[0];
}
void readlnTo(T...)(ref T t) {
auto s = readln().split();
assert(s.length == t.length);
foreach(ref ti; t) {
ti = s[0].to!(typeof(ti));
s = s[1..$];
}
}
const real eps = 1e-10;
void main(){
long n, x;
readlnTo(n, x);
auto a = readLongs();
long ans = long.max;
auto b = a.dup();
for(uint i; i < n; ++i) {
long tmp;
for(uint j; j < n; ++j) {
b[j] = min(b[j], a[(j+i)%n]);
}
for(uint j; j < n; ++j) {
tmp += b[j];
}
tmp += x*i;
ans = min(ans, tmp);
}
writeln(ans);
}
|
D
|
import std;
void main() {
int N; long M;
scan(N, M);
auto a = readln.split.to!(long[]).map!(x => x % M).array;
long[long] cnt;
cnt[0] = 1;
long rem;
long ans;
foreach (i; 0 .. N) {
rem = (rem + a[i]) % M;
if (rem in cnt) ans += cnt[rem];
cnt[rem]++;
}
writeln(ans);
}
void scan(T...)(ref T args) {
auto line = readln.split; // @suppress(dscanner.suspicious.unmodified)
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
|
void main(){
string[] str = readln().chomp().split();
if( str[0] > str[1] )writeln(">");
else if( str[0] < str[1] )writeln("<");
else writeln("=");
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int h, w;
scan(h, w);
int a, b;
scan(a, b);
auto ans = h * w - ((h / a * a) * (w / b * b));
writeln(ans);
}
int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new int[][](n, 0);
foreach (i; 0 .. m) {
int u, v;
scan(u, v);
if (is1indexed) {
u--, v--;
}
adj[u] ~= v;
if (isUndirected) {
adj[v] ~= u;
}
}
return adj;
}
alias Edge = Tuple!(int, "to", int, "cost");
Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new Edge[][](n, 0);
foreach (i; 0 .. m) {
int u, v, c;
scan(u, v, c);
if (is1indexed) {
u--, v--;
}
adj[u] ~= Edge(v, c);
if (isUndirected) {
adj[v] ~= Edge(u, c);
}
}
return adj;
}
void yes(bool b) {
writeln(b ? "Yes" : "No");
}
void YES(bool b) {
writeln(b ? "YES" : "NO");
}
T[] readArr(T)() {
return readln.split.to!(T[]);
}
T[] readArrByLines(T)(int n) {
return iota(n).map!(i => readln.chomp.to!T).array;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "begin", long, "end");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
long n = lread();
auto x = aryread();
long ans = INF;
foreach (i; iota(100))
{
ans = min(x.calc(i), ans);
}
ans.writeln();
}
long calc(T)(T a, long t)
{
long ret;
foreach (e; a)
{
ret += (e - t) ^^ 2;
}
return ret;
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (ti; 0..t)
{
auto abcd = RDA;
auto x = RD;
auto y = RD;
auto x1 = RD;
auto y1 = RD;
auto x2 = RD;
auto y2 = RD;
bool check(long pos, long l, long r, long dl, long dr)
{
auto d = dr - dl;
if (!inside(pos+d, l, r+1)) return false;
if (dl != 0 || dr != 0)
{
if (pos == l && pos == r) return false;
}
return true;
}
ans[ti] = check(x, x1, x2, abcd[0], abcd[1]) && check(y, y1, y2, abcd[2], abcd[3]);
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nr = readln.split.to!(int[]);
auto N = nr[0];
auto R = nr[1];
if (N >= 10) {
writeln(R);
} else {
writeln(R + 100 * (10-N));
}
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n; readV(n);
auto dp1 = new int[](n+1), dp2 = new int[](n+1);
dp1[] = n;
foreach (i; 0..6) dp1[i] = i;
foreach (i; 1..8) {
dp2[] = dp1[];
auto m = 6^^i;
foreach (j; 0..n+1)
foreach (k; 0..6)
if (j >= m*k)
dp1[j] = min(dp1[j], dp2[j-m*k]+k);
}
foreach (i; 1..7) {
dp2[] = dp1[];
auto m = 9^^i;
foreach (j; 0..n+1)
foreach (k; 0..9)
if (j >= m*k)
dp1[j] = min(dp1[j], dp2[j-m*k]+k);
}
writeln(dp1[n]);
}
|
D
|
void main()
{
long h = rdElem;
long cnt = 1;
long result;
while (h)
{
h >>= 1;
result += cnt;
cnt <<= 1;
}
result.writeln;
}
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 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
|
void main() {
int[] tmp = readln.split.to!(int[]);
int n = tmp[0], m = tmp[1], x = tmp[2], y = tmp[3];
int[] a = readln.split.to!(int[]);
int[] b = readln.split.to!(int[]);
a ~= x;
b ~= y;
writeln(a.reduce!max < b.reduce!min ? "No War" : "War");
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.container;
import std.typecons;
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main()
{
for (int i = 1; ; i++) {
int a = to!(int)(chomp(readln()));
if (a != 0) {
writeln("Case ", i, ": ", a);
} else {
break;
}
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto N = RD;
auto K = RD;
auto A = RDA;
foreach (i; 0..N-K)
{
writeln(A[i] < A[i+K] ? "Yes" : "No");
}
stdout.flush;
debug readln;
}
|
D
|
import std;
void main() {
int n;
string s;
scan(n);
scan(s);
auto w = new int[](n + 1);
auto b = new int[](n + 1);
foreach (i; 1 .. n + 1) {
b[i] = b[i-1] + (s[i-1]=='#');
}
foreach_reverse (i; 0 .. n) {
w[i] = w[i+1] + (s[i]=='.');
}
auto ans = 1_000_000_000;
foreach (i; 0 .. n + 1) {
chmin(ans, b[i] + w[i]);
}
writeln(ans);
}
long[] factor(int x) {
long[] res;
for (int i = 2; i*i <= x; i++) {
while (x % i == 0) {
x /= i;
res ~= i;
}
}
if (x > 1) res ~= x;
return res;
}
void scan(T...)(ref T args) {
auto line = readln.split; // @suppress(dscanner.suspicious.unmodified)
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args)
if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args)
if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
|
D
|
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math, std.container;
import core.stdc.stdio;
// foreach, foreach_reverse, writeln
void main() {
int n;
scanf("%d\n", &n);
int[][] g = new int[][n];
int[] degree = new int[n];
foreach (i; 0..n-1) {
int x, y;
scanf("%d %d\n", &x, &y);
--x; --y;
g[x] ~= y;
g[y] ~= x;
degree[x]++;
degree[y]++;
}
string c = readln().chomp;
bool[] live = new bool[n];
live[] = true;
alias Queue = DList!int;
auto q = new Queue();
foreach (i; 0..n) {
if (degree[i] == 1) {
q.insertBack(i);
}
}
int L = n;
while (!q.empty) {
int v = q.front();
q.removeFront();
if (c[v] == 'W') continue;
live[v] = false; L--;
foreach (u; g[v]) {
degree[u]--;
if (degree[u] == 1) {
q.insertBack(u);
}
}
}
if (L == 0) {
writeln(0);
return;
}
int base = L*2;
int[] score = new int[n];
foreach (v; 0..n) {
if (!live[v]) continue;
score[v] = (degree[v] + (c[v]=='B'?0:1)) % 2;
base += score[v];
}
int best = 0;
int dfs(int v, int parent=-1) {
int ret = score[v];
foreach (u; g[v]) {
if (u == parent) continue;
if (!live[u]) continue;
int s = dfs(u,v);
best = max(best, ret+s);
ret = max(ret, s+score[v]);
}
return ret;
}
foreach (v; 0..n) {
if (!live[v]) continue;
dfs(v);
break;
}
writeln(base-best*2-2);
}
|
D
|
void main() {
problem();
}
void problem() {
auto a = scan!long;
long solve() {
return a == 0 ? 1 : 0;
}
solve().writeln;
}
// ----------------------------------------------
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
// -----------------------------------------------
|
D
|
import std.stdio, 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 x = RD!int;
auto y = RD!int;
auto z = RD!int;
string ans;
if (abs(x - y) > z || z == 0)
{
ans = x == y ? "0" : x > y ? "+" : "-";
}
else
{
ans = "?";
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
alias MP = Tuple!(int, "i", int, "p");
int[11] PS, CS;
void main()
{
auto dg = readln.split.to!(int[]);
auto D = dg[0];
auto G = dg[1] / 100;
foreach (i; 1..D+1) {
auto pc = readln.split.to!(int[]);
PS[i] = pc[0];
CS[i] = pc[1] / 100;
}
int[3][11][1001] memo;
int solve(int i, int c, int d) {
if (i == 0 || i == D+1 || c <= 0) return 0;
if (memo[c][i][d+1]) return memo[c][i][d+1];
auto r1 = solve(i + d, c, d);
int r2;
if (c >= PS[i]) {
r2 = PS[i] * i + CS[i] + solve(i + d, c - PS[i], d);
} else {
r2 = c * i;
}
return memo[c][i][d+1] = max(r1, r2);
}
foreach (i; 1..1001) {
if (solve(1, i, 1) >= G || solve(D, i, -1) >= G) {
writeln(i);
return;
}
}
}
|
D
|
//dlang template---{{{
import std.stdio;
import std.conv;
import std.string;
import std.array;
import std.algorithm;
import std.typecons;
import std.math;
import std.range;
// MIT-License https://github.com/kurokoji/nephele
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType))
{
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType))
{
str ~= s.to!(char[]).strip.split;
}
}
//Digit count---{{{
int DigitNum(int num) {
int digit = 0;
while (num != 0) {
num /= 10;
digit++;
}
return digit;
}
//}}}
//}}}
void main() {
Scanner sc = new Scanner;
int n;
sc.scan(n);
if (n == 25)
writeln("Christmas");
else if (n == 24)
writeln("Christmas Eve");
else if (n == 23)
writeln("Christmas Eve Eve");
else if (n == 22)
writeln("Christmas Eve Eve Eve");
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n; readV(n);
long[] a; readA(n, a);
auto as = a.cumulativeSum;
auto dp = new long[][](n, n+1);
long f(int i, int j)
{
if (i+1 == j) return 0;
if (dp[i][j]) return dp[i][j];
auto r = 10L^^18;
foreach (k; i+1..j)
r = min(r, f(i, k) + f(k, j) + as[i..j]);
return dp[i][j] = r;
}
writeln(f(0, n));
}
class CumulativeSum(T)
{
size_t n;
T[] s;
this(T[] a)
{
n = a.length;
s = new T[](n+1);
s[0] = T(0);
foreach (i; 0..n) s[i+1] = s[i] + a[i];
}
T opSlice(size_t l, size_t r) { return s[r]-s[l]; }
size_t opDollar() { return n; }
}
auto cumulativeSum(T)(T[] a) { return new CumulativeSum!T(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, core.stdc.string;
void main() {
auto N = readln.chomp.to!int;
auto G = new Tuple!(int, int)[][](N);
foreach (i; 0..N-1) {
auto s = readln.split.map!(to!int);
G[s[0]-1] ~= tuple(s[1]-1, s[2] & 1);
G[s[1]-1] ~= tuple(s[0]-1, s[2] & 1);
}
auto ans = new int[](N);
ans[] = 2;
void dfs(int n, int c) {
if (ans[n] != 2) return;
ans[n] = c;
foreach (to; G[n]) {
if (ans[to[0]] != 2) continue;
dfs(to[0], c^to[1]);
}
}
dfs(0, 0);
ans.each!writeln;
}
|
D
|
/+ dub.sdl:
name "D"
dependency "dunkelheit" version=">=0.9.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
// import dkh.functional;
string s;
memoCont!calcBase calc;
int calcBase(int l, int r, int k) {
if (l == r) return 1;
if (l > r) return 0;
int ans = max(calc(l+1, r, k), calc(l, r-1, k));
if (s[l] == s[r]) ans = max(ans, 2+calc(l+1, r-1, k));
if (k) ans = max(ans, 2 + calc(l+1, r-1, k-1));
return ans;
}
int main() {
Scanner sc = new Scanner(stdin);
scope(exit) assert(!sc.hasNext);
sc.read(s);
int k;
sc.read(k);
int n = s.length.to!int;
calc.init([[-1, n], [-1, n], [0, k]]);
writeln(calc(0, n-1, k));
return 0;
}
/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
void read(Args...)(auto ref Args args) {
import std.exception;
static if (args.length != 0) {
enforce(readSingle(args[0]));
read(args[1..$]);
}
}
bool hasNext() {
return succ();
}
}
/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/functional.d */
// module dkh.functional;
struct memoCont(alias pred) {
import std.traits : ReturnType, ParameterTypeTuple, isIntegral;
import std.meta : allSatisfy;
alias R = ReturnType!pred;
alias Args = ParameterTypeTuple!pred;
static assert (allSatisfy!(isIntegral, Args));
static immutable N = Args.length;
private int[2][N] rng;
int[N] len;
R[] dp;
bool[] used;
void init(in int[2][N] rng) {
import std.algorithm : reduce, map;
import std.range : array;
this.rng = rng;
len = rng[].map!(a => a[1]-a[0]+1).array;
auto sz = reduce!"a*b"(1, len);
dp = new R[sz];
used = new bool[sz];
}
R opCall(Args args) {
import core.exception : RangeError;
size_t idx, base = 1;
foreach (i, v; args) {
version(assert) {
if (v < rng[i][0] || rng[i][1] < v) {
throw new RangeError;
}
}
assert(rng[i][0] <= v && v <= rng[i][1]);
idx += base*(v - rng[i][0]);
base *= len[i];
}
if (used[idx]) return dp[idx];
used[idx] = true;
auto r = pred(args);
dp[idx] = r;
return r;
}
}
/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/foundation.d */
// module dkh.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
int n;
scan(n);
auto a = readln.split.to!(int[]);
int ans = 100;
foreach (ai ; a) {
int cnt;
while (ai % 2 == 0) {
ai /= 2;
cnt++;
}
ans = min(ans, cnt);
}
writeln(ans);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.string, std.array, std.conv;
void main() {
int n = readln.chomp.to!int;
int[] h = readln.split.to!(int[]);
bool ok = true;
int cnt = 1;
foreach (i; 1 .. n) {
if (h[i] > h[i-1]) {
++cnt;
} else if (h[i] < h[i-1]) {
if (h[i-1] - h[i] > cnt) ok = false;
cnt = 0;
}
}
writeln(ok ? "Yes" : "No");
}
|
D
|
import std.stdio, std.string, std.algorithm, std.range, std.conv;
void main()
{
auto N = readln.chomp.to!int;
if(N%2==1) {
writeln(N*2);
} else {
writeln(N);
}
}
|
D
|
import std.stdio, std.string, std.conv;
void main() {
writeln(readln.chomp.to!int ^^ 3);
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.ascii;
void main()
{
auto a = readln.chomp.split.to!(int[]);
if (a[0] <= a[2] && a[1] >= a[2]) {
writeln("Yes");
} else {
writeln("No");
}
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void main() {
int a, b, c; scan(a, b, c);
int k; scan(k);
int ans = a + b + c + max(a, b, c) * (2^^k - 1);
writeln(ans);
}
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 core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
writeln((N + 1) / 2);
}
|
D
|
import std.stdio, std.string, std.array, std.conv;
struct Dice6 {
int[string] dice6;
int[] _dice6 = new int[6];
this(int[] _dice6) {
dice6["top"] = _dice6[0];
dice6["front"] = _dice6[1];
dice6["right"] = _dice6[2];
dice6["left"] = _dice6[3];
dice6["back"] = _dice6[4];
dice6["bottom"] = _dice6[5];
}
void roll(char c) {
int tmp = dice6["top"];
if (c == 'E') {
dice6["top"] = dice6["left"];
dice6["left"] = dice6["bottom"];
dice6["bottom"] = dice6["right"];
dice6["right"] = tmp;
} else if (c == 'N') {
dice6["top"] = dice6["front"];
dice6["front"] = dice6["bottom"];
dice6["bottom"] = dice6["back"];
dice6["back"] = tmp;
} else if (c == 'S') {
dice6["top"] = dice6["back"];
dice6["back"] = dice6["bottom"];
dice6["bottom"] = dice6["front"];
dice6["front"] = tmp;
} else {
dice6["top"] = dice6["right"];
dice6["right"] = dice6["bottom"];
dice6["bottom"] = dice6["left"];
dice6["left"] = tmp;
}
}
void clockwise() {
int tmp = dice6["front"];
dice6["front"] = dice6["right"];
dice6["right"] = dice6["back"];
dice6["back"] = dice6["left"];
dice6["left"] = tmp;
}
int surface(string dir) {
int result;
if (dir == "top") {
result = dice6["top"];
} else if (dir == "front") {
result = dice6["front"];
} else if (dir == "right") {
result = dice6["right"];
} else if (dir == "left") {
result = dice6["left"];
} else if (dir == "back") {
result = dice6["back"];
} else {
result = dice6["bottom"];
}
return result;
}
}
bool areIdentical(Dice6 x, Dice6 y) {
string[] dirs = ["top", "front", "right", "left", "back", "bottom"];
foreach (dir; dirs) {
if (x.surface(dir) != y.surface(dir)) return false;
}
return true;
}
void main() {
int[] _dice1 = readln.chomp.split.to!(int[]);
Dice6 dice1 = Dice6(_dice1);
int[] _dice2 = readln.chomp.split.to!(int[]);
Dice6 dice2 = Dice6(_dice2);
bool check = false;
foreach (i; 0 .. 6) {
foreach (j; 0 .. 4) {
if (areIdentical(dice1, dice2)) check = true;
dice2.clockwise;
}
i % 2 == 0 ? dice2.roll('E') : dice2.roll('N');
}
writeln(check ? "Yes" : "No");
}
|
D
|
/* imports all std modules {{{*/
import
std.algorithm,
std.array,
std.ascii,
std.base64,
std.bigint,
std.bitmanip,
std.compiler,
std.complex,
std.concurrency,
std.container,
std.conv,
std.csv,
std.datetime,
std.demangle,
std.encoding,
std.exception,
std.file,
std.format,
std.functional,
std.getopt,
std.json,
std.math,
std.mathspecial,
std.meta,
std.mmfile,
std.net.curl,
std.net.isemail,
std.numeric,
std.parallelism,
std.path,
std.process,
std.random,
std.range,
std.regex,
std.signals,
std.socket,
std.stdint,
std.stdio,
std.string,
std.system,
std.traits,
std.typecons,
std.uni,
std.uri,
std.utf,
std.uuid,
std.variant,
std.zip,
std.zlib;
/*}}}*/
/+---test
3
2 2 4
---+/
/+---test
5
3 8 7 5 5
---+/
/+---test
3
1000000000 1000000000 0
---+/
void main(string[] args) {
readln;
const A = readln.split.map!(to!long).array;
const N = A.length;
auto M = new long[N];
foreach (i, v; A) {
M[0] += v * (2 - (i%2)*4);
}
M[0] /= 2;
foreach (i; 1..N) {
M[i] = A[i-1]*2 - M[i-1];
}
foreach (i, v; M) {
if (i != 0) " ".write;
v.write;
}
writeln;
}
|
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;
}
}
void main() {
int[char] a;
const string s = readln.chomp;
foreach(c; s) {
a[c]++;
}
if (a.length == 2 && a[s[0]] == 2) {
writeln("Yes");
} else {
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 main()
{
int a, b; readV(a, b);
if (a < b) swap(a, b);
writeln(a*2 - (a > b ? 1 : 0));
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int N, K;
scan(N, K);
auto a = readln.split.to!(int[]);
auto am = new int[](N + 1);
am[0] = 0;
foreach (i ; 1 .. N + 1) {
am[i] = max(am[i - 1], a[i - 1]);
}
long ans = infl;
foreach (mask ; 0 .. 1 << N) {
if (mask.popcnt < K) continue;
int pre;
long cost;
foreach (i ; 0 .. N) if ((mask >> i) & 1) {
if (max(pre, am[i]) >= a[i]) {
cost += max(pre, am[i]) - a[i] + 1;
pre = max(pre, am[i]) + 1;
}
else {
pre = a[i];
}
}
debug {
writefln("%08b", mask);
writeln(cost);
}
chmin(ans, cost);
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.stdio, std.conv, std.string, std.algorithm,
std.math, std.array, std.container, std.typecons;
import std.numeric;
void main() {
int k = readln.chomp.to!int;
long sum = 0;
for(int i=0; i<k; i++)
for(int j=0; j<k; j++)
for(int l=0; l<k; l++) {
sum += gcd(i+1, gcd(j+1, l+1));
}
writeln(sum);
}
|
D
|
import std.stdio;
import std.algorithm;
import std.math;
import std.string;
import std.conv;
import std.range;
void main() {
while (true) {
int n = readln.chomp.to!int;
if (n == 0) break;
int[] ice = new int[10];
foreach (i; 0..n) {
ice[readln.chomp.to!int]++;
}
foreach (e; ice) {
if (e == 0) {
writeln("-");
} else {
foreach (i; 0..e) {
write("*");
}
writeln;
}
}
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
/// [..)
struct SegTree(alias _fun, alias def, T)
if (is(typeof(def) : T))
{
import std.functional : binaryFun;
alias fun = binaryFun!_fun;
///
this(size_t n, T[] ts) {
this.n = 1;
while (this.n < n) this.n *= 2;
this.tree.length = this.n * 2 - 1;
foreach (ref e; this.tree) e = def;
foreach (i, e; ts) this.put(i, e);
}
///
void put(size_t i, T e) {
i += this.n - 1;
this.tree[i] = e;
while (i > 0) {
i = (i-1) / 2;
this.tree[i] = fun(this.tree[i*2+1], this.tree[i*2+2]);
}
}
///
T query(size_t a, size_t b) {
T impl(size_t i, size_t l, size_t r) {
if (r <= a || b <= l) return def;
if (a <= l && r <= b) return this.tree[i];
return fun(
impl(i*2+1, l, (l+r)/2),
impl(i*2+2, (l+r)/2, r)
);
}
return impl(0, 0, this.n);
}
private:
size_t n;
T[] tree;
}
///
SegTree!(f, def, T) seg_tree(alias f, alias def, T)(size_t n, T[] arr = [])
{
return SegTree!(f, def, T)(n, arr);
}
///
SegTree!(f, def, T) seg_tree(alias f, alias def, T)(T[] arr)
{
return SegTree!(f, def, T)(arr.length, arr);
}
void main()
{
auto nk = readln.split.to!(int[]);
auto N = nk[0];
auto K = nk[1];
auto ps = readln.split.to!(int[]);
int last = -1, cnt;
auto bs = new bool[](N);
bool has_nc;
foreach (i, p; ps) {
if (p > last) {
++cnt;
if (cnt >= K) {
bs[i-K+1] = true;
has_nc = true;
}
} else {
cnt = 1;
}
last = p;
}
auto ps_min = seg_tree!("a < b ? a : b", int.max)(ps.dup);
auto ps_max = seg_tree!("a > b ? a : b", -1)(ps.dup);
int r = bs[0] ? 0 : 1;
foreach (i; 0..N-K) {
if (
(ps_min.query(i, i+K) != ps[i] || ps_max.query(i+1, i+K+1) != ps[i+K]) &&
bs[i+1] == false
) ++r;
}
writeln(r + (has_nc ? 1 : 0));
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
long h, w;
scan(h, w);
auto a = new string[](h);
foreach (i; 0 .. h)
{
a[i] = sread();
}
// writeln(a);
bool[] row = new bool[](h);
bool[] col = new bool[](w);
foreach (y; 0 .. h)
{
// writeln(a[y]);
foreach (x; 0 .. w)
{
if (a[y][x] == '.')
{
continue;
}
else
{
row[y] = true;
break;
}
}
}
// writeln(row);
foreach (x; 0 .. w)
{
foreach (y; 0 .. h)
{
if (a[y][x] == '.')
{
continue;
}
else
{
col[x] = true;
break;
}
}
}
// writeln(col);
foreach (y; 0 .. h)
{
foreach (x; 0 .. w)
{
if (row[y] && col[x])
{
write(a[y][x]);
}
}
if (row[y])
{
writeln();
}
}
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
|
D
|
import 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, std.datetime;
void main() {
auto N = readln.chomp.to!int;
auto S = readln.chomp;
auto W = new int[](N+1);
auto E = new int[](N+1);
foreach (i; 0..N) W[i+1] = W[i] + (S[i] == 'W');
foreach (i; 0..N) E[i+1] = E[i] + (S[i] == 'E');
int ans = 1 << 29;
foreach (i; 0..N) {
int tmp = 0;
if (i > 0) {
tmp += W[i];
}
if (i < N-1) {
tmp += E[N] - E[i+1];
}
ans = min(ans, tmp);
}
ans.writeln;
}
|
D
|
//dlang template---{{{
import std.stdio;
import std.conv;
import std.string;
import std.array;
import std.algorithm;
import std.typecons;
import std.math;
import std.range;
// MIT-License https://github.com/kurokoji/nephele
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType))
{
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType))
{
str ~= s.to!(char[]).strip.split;
}
}
//Digit count---{{{
int DigitNum(int num) {
int digit = 0;
while (num != 0) {
num /= 10;
digit++;
}
return digit;
}
//}}}
//}}}
void main() {
Scanner sc = new Scanner;
int N, i;
sc.scan(N, i);
writeln(N - i + 1);
}
|
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;
void main() {
int a, b, c, d;
scan(a, b, c, d);
foreach (i; 0 .. 1000) {
if (a <= 0 || c <= 0) break;
if (i % 2 == 0) {
c -= b;
}
else {
a -= d;
}
}
yes(c <= 0);
}
void scan(T...)(ref T args) {
auto line = readln.split; // @suppress(dscanner.suspicious.unmodified)
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args)
if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args)
if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
|
D
|
import std.stdio, std.conv, std.string, std.algorithm,
std.math, std.array, std.container, std.typecons;
void main() {
string s = readln.chomp;
if(s[0]=='7' || s[1]=='7' || s[2]=='7') writeln("Yes");
else writeln("No");
}
|
D
|
import std.stdio, std.conv, std.string;
void main() {
int N, K;
N = readln().chomp().to!(int);
K = readln().chomp().to!(int);
int res = 1;
foreach(int i; 0..N) {
if(res + K < res*2) {
res += K;
}
else {
res *= 2;
}
}
writeln(res);
}
|
D
|
import std.stdio;
import std.string;
import std.format;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.concurrency;
import std.traits;
import std.uni;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;
enum long INF = long.max/5;
enum long MOD = 10L^^9+7;
void main() {
long s = readln.split.to!(long[]).sum;
if (s >= 22) {
writeln("bust");
} else {
writeln("win");
}
}
// ----------------------------------------------
void times(alias fun)(long n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(long n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
// `(x+y-1)/y` will only work for positive numbers ...
T t = x / y;
if (y > 0 && t * y < x) t++;
if (y < 0 && t * y > x) t++;
return t;
}
T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
T t = x / y;
if (y > 0 && t * y > x) t--;
if (y < 0 && t * y < x) t--;
return t;
}
ref T ch(alias fun, T, S...)(ref T lhs, S rhs) {
return lhs = fun(lhs, rhs);
}
unittest {
long x = 1000;
x.ch!min(2000);
assert(x == 1000);
x.ch!min(3, 2, 1);
assert(x == 1);
x.ch!max(100).ch!min(1000); // clamp
assert(x == 100);
x.ch!max(0).ch!min(10); // clamp
assert(x == 10);
}
mixin template Constructor() {
import std.traits : FieldNameTuple;
this(Args...)(Args args) {
// static foreach(i, v; args) {
foreach(i, v; args) {
mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v;
}
}
}
void scanln(Args...)(auto ref Args args) {
enum sep = " ";
enum n = Args.length;
enum fmt = n.rep!(()=>"%s").join(sep) ~ "\n";
static if (__VERSION__ >= 2071) {
readf!fmt(args);
} else {
enum argsTemp = n.iota.map!(
i => "&args[%d]".format(i)
).join(", ");
mixin(
"readf(fmt, " ~ argsTemp ~ ");"
);
}
}
// 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);
}
}
}
}
// popcnt with ulongs was added in D 2.071.0
static if (__VERSION__ < 2071) {
ulong popcnt(ulong x) {
x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L);
x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L);
x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL);
x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL);
x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL);
x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL);
return x;
}
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
import std.array;
int n; long c; rd(n, c);
auto x=new long[](n), v=new long[](n);
foreach(i; 0..n) rd(x[i], v[i]);
// import std.exception;
// enforce(n<=100);
x=0L~x~c;
v=0L~v~0L;
long mx=0, inf=2e18.to!(long);
mx=max(mx, fun(n, x, v));
x=x.map!(e=>(c-e)).array;
reverse(x); reverse(v);
mx=max(mx, fun(n, x, v));
long tot=reduce!"a+b"(0L, v);
for(int i=0; i<=n; i++){
long cl=x[i], cr=c-x[i+1];
long cost=cl+cr+min(cl, cr);
mx=max(mx, tot-cost);
}
writeln(mx);
}
long fun(int n, long[] d, long[] v){ // 0->l->0->r (l<r) distance : l*2+r
import std.algorithm;
auto best=new long[](n+2); // [i, $)
fill(best, -2_000_000_000_000_000_000);
best[n+1]=0;
for(auto r=n, s=0L; r>0; r--){
s+=v[r];
best[r]=max(best[r+1], s-(d[$-1]-d[r]));
}
long ret=0;
for(auto l=0, s=0L; l<=n; l++){
s+=v[l];
ret=max(ret, (s-d[l]*2)+best[l+1]);
}
return ret;
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x) e=l[i].to!(typeof(e));
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main()
{
auto X = readln.split.to!(int[]),A = X[0],B = X[1];
if((A + B) % 2 == 0)
{
writeln((A + B) / 2);
}
else
{
writeln((A + B) / 2 + 1);
}}
|
D
|
// Vicfred
// https://atcoder.jp/contests/abc047/tasks/arc063_a
// greedy
import std.stdio;
import std.string;
void main() {
string s = readln.strip;
long n = s.length;
long count = 0;
for(long i = 0; i < s.length-1; i++) {
if(s[i] != s[i + 1]) count += 1;
}
count.writeln;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
int n;
scan(n);
int ans;
int ls, rs;
ans = query(0);
if (ans == 2) return;
ls = ans;
ans = query(n-1);
if (ans == 2) return;
rs = ans;
int left = 0, right = n - 1;
while (true) {
int mid = (left + right) / 2;
ans = query(mid);
if (ans == 2) return;
int b = ((mid - left + 1) & 1) ^ (ls ^ ans);
if (!b) {
right = mid;
rs = ans;
}
else {
left = mid;
ls = ans;
}
}
}
int query(int i) {
writeln(i);
stdout.flush();
string s = readln.chomp;
if (s == "Male") {
return 0;
}
else if (s == "Female") {
return 1;
}
else {
return 2;
}
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.conv;
import std.stdio;
import std.string;
void main()
{
auto ab = readln.split.to!( int[] );
writeln( solve( ab[ 0 ], ab[ 1 ] ) );
}
auto solve( in int a, in int b )
{
return ( a <= 8 && b <= 8 ) ? "Yay!" : ":(" ;
}
unittest
{
assert( solve( 5, 4 ) == "Yay!" );
assert( solve( 8, 8 ) == "Yay!" );
assert( solve( 11, 4 ) == ":(" );
}
|
D
|
void main(){
int a, b, x;
scanf("%d %d %d", &a, &b, &x);
writeln(a+b>=x&&a<=x?"YES":"NO");
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range;
const long mod = 10^^9+7;
// 1要素のみの入力
T inelm(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] inln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split())ln ~= elm.to!T();
return ln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto ab = readln.split.to!(int[]);
writeln(ab[0] < 10 && ab[1] < 10 ? ab[0] * ab[1] : -1);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void main()
{
auto M = readln.chomp.to!int;
long r, x = -1;
while (M--) {
auto dc = readln.split.to!(long[]);
auto d = dc[0];
auto c = dc[1];
if (x == -1) {
--c;
x = d;
}
while (c) {
if (c%2 == 1) {
++r;
x += d;
if (x > 9) {
++r;
x = x/10 + x%10;
}
}
r += c/2;
d *= 2;
if (d > 9) {
r += c/2;
d = d/10 + d%10;
}
c /= 2;
}
}
writeln(r);
}
|
D
|
void main()
{
long d, n;
rdVals(d, n);
writeln(n == 100 ? (n + 1) * 100 ^^ d : n * 100 ^^ d);
}
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 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;
ulong MOD = 1_000_000_007;
ulong INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias t = Tuple!(string, "name", long, "y");
void main()
{
auto n = lread();
auto s = sread();
auto first = new long[](10);
auto second = new long[](100);
auto third = new long[](1_000);
foreach (i; iota(n))
{
long idx1 = s[i] - '0';
if (first[idx1])
continue;
foreach (j; iota(i + 1, n))
{
long idx2 = idx1 * 10 + s[j] - '0';
if (second[idx2])
continue;
foreach (k; iota(j + 1, n))
{
long idx3 = idx2 * 10 + s[k] - '0';
if (third[idx3])
continue;
third[idx3] = 1;
}
second[idx2] = 1;
}
first[idx1] = 1;
}
third.sum.writeln();
}
auto getdisit(T)(T n)
{
T i = 0;
while (abs(n) >= 1)
{
n /= 10;
i++;
}
return i;
}
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;
}
}
|
D
|
void main() {
// ((A, B) => A > B ? "GREATER" : A < B ? "LESS" : "EQUAL")(BigInt(rs), BigInt(rs)).writeln;
auto A = rs, B = rs;
if(A.length == B.length) {
if(A[0] > B[0]) writeln("GREATER");
else if(A[0] == B[0]) writeln("EQUAL");
else writeln("LESS");
} else {
if(A.length > B.length) writeln("GREATER");
else writeln("LESS");
}
}
// ===================================
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
void main()
{
import std.stdio;
(readln>"2019/05"?"TBD":"Heisei").writeln;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm, std.array, std.range, std.container;
import std.math;
void main() {
auto N = readln.split[0].to!int;
if (N == 1) { writeln("Hello World"); }
else if (N == 2) {
auto A = readln.split[0].to!int;
auto B = readln.split[0].to!int;
writeln(A+B);
}
}
|
D
|
void main()
{
string n = readln.chomp;
int total;
foreach (d; n)
{
total += d - '0';
}
if (total == 1) total *= 10;
total.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.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
int n = readint;
writeln((n * (n + 1)) / 2);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container;
void main()
{
auto N = readln.chomp.to!int;
auto T = new int[][N];
foreach (_; 1..N) {
auto ab = readln.split.to!(int[]);
auto a = ab[0]-1;
auto b = ab[1]-1;
T[a] ~= b;
T[b] ~= a;
}
int p1, max_l;
void run1(int i, int p, int l) {
if (max_l < l) {
max_l = l;
p1 = i;
}
foreach (j; T[i]) if (j != p) run1(j, i, l+1);
}
run1(0, -1, 0);
int run2(int i, int p, int l) {
int ll = l;
foreach (j; T[i]) if (j != p) ll = max(ll, run2(j, i, l+1));
return ll;
}
auto len = run2(p1, -1, 1);
writeln((len-2)%3 == 0 ? "Second" : "First");
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
static import std.ascii;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long H = lread();
long W = lread();
long N = lread();
long m = max(H, W);
writeln((N + m - 1) / m);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.container;
import std.array;
import std.math;
import std.range;
import std.typecons;
import std.ascii;
import std.format;
void main()
{
int[101] imos;
auto d = readln.chomp.split.map!(to!int);
imos[d[0]]++;
imos[d[1]]--;
imos[d[2]]++;
imos[d[3]]--;
int cnt = 0;
int ite = 0;
for (int i = 0; i <= 100; ++i) {
ite += imos[i];
if (ite == 2) {
++cnt;
}
}
writeln = cnt;
}
|
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;
int calc(int n, int s) {
int ans = 0;
foreach (e; combinations!int(iota(10).array, n)) {
if (e.sum == s)
ans++;
}
return ans;
}
void main() {
while (true) {
auto xs = readints;
int n = xs[0], s = xs[1];
if (n == 0 && s == 0) break;
writeln(calc(n, s));
}
}
struct combinations(T) {
private T[] _source;
private int _n;
private T[][] _q;
private int _state;
this(T[] source, int n) {
_source = source.dup;
_n = n;
_state = 0;
pushNext();
}
@property T[] front() {
return _q[0];
}
void popFront() {
_q = _q[1..$];
pushNext();
}
bool empty() {
return _q.length == 0;
}
private void pushNext() {
int len = cast(int)_source.length;
while (_state < (1 << len)) {
int[] buf;
for (int j = 0; j < len; j++) {
if (_state & (1 << j)) {
buf ~= _source[j];
if (buf.length > _n) break;
}
}
_state++;
if (buf.length == _n) {
_q ~= buf;
return;
}
}
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nm = readln.split.to!(long[]);
auto N = nm[0];
auto M = nm[1];
auto as = readln.split.to!(long[]);
auto x = as[0]/2;
int k;
while (x % 2 == 0) {
++k;
x /= 2;
}
x = as[0]/2;
foreach (a; as[1..$]) {
a /= 2;
x = x * a / gcd(x, a);
if (x <= 0) {
writeln(0);
return;
}
int kk;
while (a%2 == 0) {
++kk;
a /= 2;
}
if (k != kk) {
writeln(0);
return;
}
}
writeln((M/x+1)/2);
}
|
D
|
import std.stdio, std.ascii;
void main() {
auto d = stdin.readln.dup;
foreach (c; d) {
if (c.isLower) c -= 32;
else if (c.isUpper) c+= 32;
c.write;
}
}
|
D
|
import std.stdio;
import std.string, std.conv, std.array, std.algorithm;
import std.uni, std.math, std.container;
import core.bitop, std.datetime;
void main(){
auto s = readln.chomp;
auto n = s.length.to!int;
if(n == 2){
if(s[0] == s[1]){
writeln(1, " ", 2);
return;
} else {
writeln(-1, " ", -1);
return;
}
}
foreach(i ; 0 .. n - 2){
if(s[i] == s[i + 1]){
writeln(i+1, " ", i + 2);
return;
} else if(s[i + 1] == s[i + 2]){
writeln(i+2, " ", i+3);
return;
} else if(s[i] == s[i + 2]){
writeln(i+1, " ", i+3);
return;
}
}
writeln(-1, " ", -1);
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
int n;
scan(n);
writeln(24 + 24 - n);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.conv, std.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 A = RD;
auto B = RD;
writeln(lcm(A, B));
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.conv, std.string, std.range, std.array, std.algorithm;
import std.bigint;
bool[1000001] p;
void main()
{
int[10002] sum;
p[2 .. 1000001] = true;
for (int i = 2; i <= 1000000; i++){
if (p[i]){
for (int j = i * 2; j <= 1000000; j += i){
p[j] = false;
}
}
}
for (int i = 1, j = 0; j <= 10000; i++){
if (p[i]){
sum[j + 1] = sum[j] + i;
j++;
}
}
while (true){
int n = readln().chomp().to!int;
if (n == 0) break;
writeln(sum[n]);
}
}
|
D
|
import std.stdio, std.string, std.array, std.algorithm, std.conv, std.typecons, std.numeric, std.math;
struct LazySegTree(alias opt, alias opu, alias _add, alias mul, alias E, alias F, T, U)
if (is(typeof(E) : T))
{
import std.functional : binaryFun;
alias OPT = binaryFun!opt;
alias OPU = binaryFun!opu;
alias ADD = binaryFun!_add;
alias MUL = binaryFun!mul;
///
this(size_t n, T[] ts) {
this.n = 1;
while (this.n < n) this.n *= 2;
this.tree.length = this.n * 2 - 1;
foreach (ref e; this.tree) e = E;
foreach (i, e; ts) this.replace(i, e);
this.ltree.length = this.n * 2 - 1;
foreach (ref f; this.ltree) f = F;
}
///
void replace(size_t i, T e) {
i += this.n - 1;
this.tree[i] = e;
while (i > 0) {
i = (i-1) / 2;
this.tree[i] = OPT(this.tree[i*2+1], this.tree[i*2+2]);
}
}
///
void update(size_t i, T e) {
replace(i, OPT(e, tree[i + this.n - 1]));
}
void update(size_t a, size_t b, U e) {
void impl(size_t i, size_t l, size_t r) {
eval(i, l, r);
if (r <= a || b <= l) return;
if (a <= l && r <= b) {
import std.conv;
ltree[i] = OPU(ltree[i], e);
eval(i, l, r);
return;
}
impl(i*2+1, l, (l+r)/2);
impl(i*2+2, (l+r)/2, r);
tree[i] = OPT(tree[i*2+1], tree[i*2+2]);
}
impl(0, 0, this.n);
}
///
T query(size_t a, size_t b) {
T impl(size_t i, size_t l, size_t r) {
eval(i, l, r);
if (r <= a || b <= l) return E;
if (a <= l && r <= b) return this.tree[i];
return OPT(
impl(i*2+1, l, (l+r)/2),
impl(i*2+2, (l+r)/2, r)
);
}
return impl(0, 0, this.n);
}
private:
size_t n;
T[] tree;
U[] ltree;
void eval(size_t i, size_t l, size_t r) {
if (ltree[i] == F) return;
import std.conv;
tree[i] = ADD(tree[i], MUL(ltree[i], (r-l).to!U));
if (i < this.n - 1) {
ltree[2*i+1] = OPU(ltree[2*i+1], ltree[i]);
ltree[2*i+2] = OPU(ltree[2*i+2], ltree[i]);
}
ltree[i] = F;
}
}
///
auto lazy_seg_tree(alias opt, alias opu, alias add, alias mul, alias E, alias F, T, U)(size_t n, T[] arr = [])
{
return LazySegTree!(opt, opu, add, mul, E, F, T, T)(n, arr);
}
///
auto lazy_seg_tree(alias opt, alias opu, alias add, alias mul, alias E, alias F, T, U)(T[] arr)
{
return LazySegTree!(opt, opu, add, mul, E, F, T, T)(arr.length, arr);
}
void main()
{
auto nq = readln.split.to!(int[]);
auto N = nq[0];
auto Q = nq[1];
auto segt = lazy_seg_tree!(
"a < b ? a : b",
(_, b) => b,
(_, b) => b,
(a, _) => a,
(1L<<31)-1, 1L<<31,
long, long
)(N);
foreach (_; 0..Q) {
auto q = readln.split.to!(int[]);
if (q[0] == 0) {
segt.update(q[1], q[2]+1, q[3]);
} else {
writeln(segt.query(q[1], q[2]+1));
}
}
}
|
D
|
import std.stdio;
import std.array;
import std.conv;
import std.algorithm;
void main()
{
while ( true ) {
string[] line = split( readln() );
int n = to!int( line[ 0 ] );
int q = to!int( line[ 1 ] );
if ( n == 0 && q == 0 ) break;
int[ 101 ] date;
foreach ( i; 0 .. n ) {
line = split( readln() );
int m = to!int( line[ 0 ] );
foreach ( j; 0 .. m ) {
date[ to!int( line[ j + 1 ] ) ]++;
}
}
int mx = reduce!(max)( 0, date );
if ( mx >= q ) {
foreach ( i; 0 .. 100 ) {
if ( date[ i ] == mx ) {
writeln( i );
break;
}
}
} else {
writeln( 0 );
}
}
}
|
D
|
import std.stdio,std.conv, std.algorithm, std.container,std.array,std.range,std.string,std.typecons;
const dx = [1,0,-1,0];
const dy = [0,1,0,-1];
void read(T...)(auto ref T args){
auto line = readln().split();
foreach(i,ref arg; args) arg = line[i].to!(typeof(arg));
}
int sum(int n){return n * (n + 1)/ 2;}
void main(){
int m;m.read;
for(;m>0;m--){
string cars;cars.read;
bool[string] exist;
foreach(i;1..cars.length){
auto f = (string a) =>[a,a.dup.reverse.idup];
foreach(c1;f(cars[0..i])) {
foreach(c2;f(cars[i..$])){
exist[c1 ~ c2] = true;
exist[c2 ~ c1] = true;
}
}
}
exist.length.writeln;
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD!string;
writeln(N[0] == '9' || N[1] == '9' ? "Yes" : "No");
stdout.flush();
debug readln();
}
|
D
|
void main() {
long n = readln.chomp.to!int;
long mod = 10 ^^ 9 + 7;
long power = 1;
foreach (i; 1 .. n+1) {
power = power * i % mod;
}
power.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.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto Q = readln.split.to!(long[]);
auto N = readln.chomp.to!long * 100;
long[] A = [25, 50, 100, 200];
long r = long.max;
auto ss = [0,1,2,3];
do {
auto a = ss[0];
auto b = ss[1];
auto c = ss[2];
auto d = ss[3];
auto n = N;
long rr;
rr += n/A[a]*Q[a];
n %= A[a];
rr += n/A[b]*Q[b];
n %= A[b];
rr += n/A[c]*Q[c];
n %= A[c];
rr += n/A[d]*Q[d];
r = min(r, rr);
} while (nextPermutation(ss));
writeln(r);
}
|
D
|
import std.stdio;
import std.array;
import std.conv;
import std.math;
import std.algorithm;
void main()
{
string[] input = split(readln());
int w = to!int(input[0]), h = to!int(input[1]), n = to!int(input[2]), ans = 0;
input = split(readln());
int x = to!int(input[0]), y = to!int(input[1]);
for(auto i = 0; i < n - 1; i++)
{
input = split(readln());
int tx = to!int(input[0]), ty = to!int(input[1]);
if(tx - x < 0 && ty - y < 0 || tx - x > 0 && ty - y > 0)
ans += max(abs(tx - x), abs(ty - y));
else
ans += abs(tx - x) + abs(ty - y);
x = tx;
y = ty;
}
writeln(ans);
}
|
D
|
/+ dub.sdl:
name "F"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.array;
immutable long MD = 10^^9 + 7;
int main() {
auto sc = new Scanner(stdin);
int q;
sc.read(q);
long[2][] base = [[1L, 1L].fixed];
while (base.back[1] < 10L^^18) {
auto u = base.back;
base ~= [u[1], u[0]+u[1]];
}
foreach (i; 0..q) {
long x, y;
sc.read(x, y);
if (x > y) swap(x, y);
if (x == 1 && y == 1) {
//corner case
writeln("1 1");
continue;
}
int ans = (){
foreach (int i, u; base) {
if (x < u[0] || y < u[1]) {
return i-1;
}
}
assert(false);
}();
long cnt(long x, long y) {
long last(long a, long b) {
if (x < b) return 0;
if (y < a+b) return 0;
return ((y-a)/b) % MD;
}
long sm = last(base[ans-1][0], base[ans-1][1]);
long[2] freq = [0L, 1L];
foreach_reverse (ph; 0..ans-1) {
//use to ph
long bb = base[ph][1];
long na = base[ans-1][0] + freq[0]*bb;
long nb = base[ans-1][1] + freq[1]*bb;
sm += last(na, nb); sm %= MD;
freq = [freq[1], freq[0]+freq[1]];
}
return sm;
}
long sm = cnt(x, y);
if (base[ans-1][1] <= x) sm += cnt(x, x);
if (ans == 1) sm += min(x, y);
sm %= MD;
writeln(ans, " ", sm);
}
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
static if (__VERSION__ <= 2070) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
version (X86) static if (__VERSION__ < 2071) {
import core.bitop : bsf, bsr, popcnt;
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
if (f.eof) return false;
line = lineBuf[];
f.readln(line);
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else {
auto buf = line.split.map!(to!E).array;
static if (isStaticArray!T) {
assert(buf.length == T.length);
}
x = buf;
line.length = 0;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/array.d */
// module dcomp.array;
T[N] fixed(T, size_t N)(T[N] a) {return a;}
struct FastAppender(A) {
import std.algorithm : max;
import std.range.primitives : ElementEncodingType;
import core.stdc.string : memcpy;
private alias T = ElementEncodingType!A;
private T* _data;
private size_t len, cap;
@property size_t length() {return len;}
void reserve(size_t nlen) {
import core.memory : GC;
if (nlen <= cap) return;
void* nx = GC.malloc(nlen * T.sizeof);
cap = nlen;
if (len) memcpy(nx, _data, len * T.sizeof);
_data = cast(T*)(nx);
}
void opOpAssign(string op : "~")(T item) {
if (len == cap) {
reserve(max(4, cap*2));
}
_data[len++] = item;
}
void clear() {
len = 0;
}
T[] data() {
return (_data) ? _data[0..len] : null;
}
}
|
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()
{
readln;
auto a = readln.chomp.split.to!(int[]);
int[int] cnt;
int sum;
foreach (e; a) {
cnt[e]++;
}
foreach (k, v; cnt) {
if (k != v) {
if (v-k < 0) {
sum += v;
} else {
sum += min(v, v-k);
}
}
}
sum.writeln;
}
|
D
|
import std.stdio, std.string, std.conv, std.range, std.array, std.algorithm;
import std.uni, std.math, std.container, std.typecons, std.typetuple;
import core.bitop, std.datetime;
immutable long mod = 10^^9 + 7;
immutable int inf = mod;
void main(){
int N, W;
readVars(N, W);
auto v = new int[](N);
auto w = new int[](N);
foreach(i ; 0 .. N){
readVars(v[i], w[i]);
}
auto dp = new int[](W + 1);
foreach(u ; 1 .. W + 1){
foreach(i ; 0 .. N){
if(u - w[i] >= 0){
dp[u] = max(dp[u], dp[u - w[i]] + v[i]);
}
}
}
writeln(dp[W]);
}
void readVars(T...)(auto ref T args){
auto line = readln.split;
foreach(ref arg ; args){
arg = line.front.to!(typeof(arg));
line.popFront;
}
if(!line.empty){
throw new Exception("args num < input num");
}
}
|
D
|
import std.stdio;
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;
foreach (i; 1..8) {
auto p = 2 ^^ i;
if (p > n) {
writeln(2 ^^ (i - 1));
break;
}
}
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "l ", long, "r");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
auto s = sread();
writeln(s[0 .. $ - "FESTIVAL".length]);
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() {
int n = readint;
for (int i = 0; i < n; i++) {
auto s = readln.chomp;
auto t = s.replace("Hoshino", "Hoshina");
writeln(t);
}
}
|
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 w = readln.chomp.split.to!(int[]);
int s1, s2;
auto d = int.max;
s2 = w.sum;
foreach (i; 0..n) {
s1 += w[i];
s2 -= w[i];
d = min(d, abs(s1 - s2));
}
d.writeln;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.format;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void main()
{
int n = readln.chomp.to!int;
int[] p = readln.chomp.split.map!(to!int).array;
int cnt = 0;
for (int i = 0; i < n - 1; ++i) {
if (p[i] == i + 1) {
swap(p[i], p[i + 1]);
++cnt;
}
}
if (p[$ - 1] == n) {
swap(p[$ - 1], p[$ - 2]);
++cnt;
}
cnt.writeln;
}
|
D
|
import std.stdio;
import std.array;
import std.algorithm;
import std.string;
import std.conv;
import std.format;
long[][] map;
long n;
long hai(int i, int j) {
long a = 0;
if (j < n-1) {
a = max(a, hai(i, j+1) + map[i][j]);
}
if (i == 0) {
a = max(a, hai(i+1, j)+map[i][j]);
}
return a;
}
void main()
{
n = readln.chomp.to!long;
long[] a = readln.chomp.split(" ").to!(long[]);
long[] b = readln.chomp.split(" ").to!(long[]);
map ~= a;
map ~= b;
writeln(hai(0, 0) + map[1][n-1] );
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
long n, m;
scan(n, m);
writeln((n - 1) * (m - 1));
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
// dfmt on
void main()
{
long N = lread();
auto A = aryread!ulong();
long[long] D;
foreach (a; A)
{
if (a !in D)
D[a] = 0;
D[a]++;
}
if (0 in D && D[0] == N)
{
writeln("Yes");
return;
}
if (0 in D && D.keys.length == 2 && D[0] == N / 3)
{
writeln("Yes");
return;
}
if (D.keys.length == 3 && (D.keys[0] ^ D.keys[1] ^ D.keys[2]) == 0
&& D[D.keys[0]] == N / 3 && D[D.keys[1]] == N / 3 && D[D.keys[2]] == N / 3)
{
writeln("Yes");
return;
}
writeln("No");
}
|
D
|
/+ dub.sdl:
name "F"
dependency "dunkelheit" version=">=0.9.0"
+/
import std.stdio, std.algorithm, std.range, std.conv, std.typecons;
// import dkh.foundation, dkh.scanner, dkh.container.pairingheap;
alias P = Tuple!(long, "d", int, "p");
alias Q = PairingHeap!(P, "a.d>b.d");
int main() {
Scanner sc = new Scanner(stdin);
int n, m;
sc.read(n, m);
int[][] g = new int[][n];
long[] lw = new long[n], us = new long[n];
foreach (i; 0..n) {
sc.read(lw[i], us[i]); lw[i] -= us[i];
}
long off = us.sum;
foreach (i; 0..m) {
int a, b;
sc.read(a, b); a--; b--;
g[a] ~= b; g[b] ~= a;
}
Q[] gx = new Q[n];
foreach (i; 0..n) {
foreach (d; g[i]) {
gx[i].insert(P(lw[d], d));
}
}
int[] ig = new int[n];
int[][] gi = new int[][n];
foreach (i; 0..n) {
ig[i] = i; gi[i] = [i];
}
auto uf_same = (int a, int b) => ig[a] == ig[b];
void uf_merge(int a, int b) {
if (uf_same(a, b)) return;
int x = ig[a], y = ig[b];
if (gi[x].length < gi[y].length) swap(x, y);
foreach (d; gi[y]) ig[d] = x;
gi[x] ~= gi[y]; gi[y] = [];
us[x] += us[y];
us[y] = 0;
gx[x].meld(gx[y]);
}
long ans = 0;
Q que;
foreach (i; 0..n) {
que.insert(P(lw[i], i));
}
bool[] vis = new bool[n];
void on(int p) {
vis[p] = true;
foreach (d; g[p]) {
if (vis[d]) uf_merge(p, d);
}
int x = ig[p];
while (gx[x].length) {
auto tp = gx[x].front();
if (vis[tp.p]) {
gx[x].removeFront();
continue;
}
if (tp.d <= ans+us[x]) {
on(tp.p);
} else {
que.insert(P(tp.d - us[x], tp.p));
}
break;
}
}
while (que.length) {
P tp = que.front; que.removeFront();
int p = tp.p;
if (vis[p]) continue;
ans = max(ans, tp.d);
on(p);
}
writeln(ans+off);
return 0;
}
/* IMPORT /mnt/c/Users/yosupo/Programs/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /mnt/c/Users/yosupo/Programs/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
void read(bool enforceEOF = false, T, Args...)(ref T x, auto ref Args args) {
import std.exception;
enforce(readSingle(x));
static if (args.length == 0) {
enforce(enforceEOF == false || !succ());
} else {
read!enforceEOF(args);
}
}
void read(bool enforceEOF = false, Args...)(auto ref Args args) {
import std.exception;
static if (args.length == 0) {
enforce(enforceEOF == false || !succ());
} else {
enforce(readSingle(args[0]));
read!enforceEOF(args);
}
}
}
/* IMPORT /mnt/c/Users/yosupo/Programs/dunkelheit/source/dkh/container/pairingheap.d */
// module dkh.container.pairingheap;
private NP meldPairingHeapNode(alias less, NP)(NP x, NP y) {
import std.algorithm : swap;
if (!x) return y;
if (!y) return x;
if (less(x._item, y._item)) swap(x, y);
y.next = x.head;
x.head = y;
return x;
}
struct PairingHeap(T, alias less = "a < b") {
import std.functional : binaryFun;
private alias _less = binaryFun!less;
private alias NP = Node*;
private static struct Node {
T _item;
NP head, next;
this(T item) {
_item = item;
}
}
private struct Payload {
import std.algorithm : swap;
private NP node;
private uint len;
void insert(T item) {
len++;
node = meldPairingHeapNode!_less(node, new Node(item));
}
inout(T) front() inout { return node._item; }
void removeFront() {
len--;
NP s = node.head;
NP t;
while (s) {
NP first, second;
first = s; s = s.next; first.next = null;
if (s) {
second = s; s = s.next; second.next = null;
}
auto v = meldPairingHeapNode!_less(first, second);
v.next = t;
t = v;
}
node = null;
while (t) {
NP first = t; t = t.next; first.next = null;
node = meldPairingHeapNode!_less(first, node);
}
}
void meld(Payload* r) {
len += r.len; r.len = 0;
node = meldPairingHeapNode!_less(node, r.node);
r.node = null;
}
}
private Payload* _p;
@property bool empty() const { return !_p || _p.len == 0; }
@property size_t length() const { return (!_p) ? 0 : _p.len; }
void insert(T item) {
if (!_p) _p = new Payload();
_p.insert(item);
}
inout(T) front() inout {
assert(!empty, "PairingHeap.front: heap is empty");
return _p.front;
}
void removeFront() {
assert(!empty, "PairingHeap.removeFront: heap is empty");
_p.removeFront;
}
void meld(PairingHeap r) { _p.meld(r._p); }
}
/* IMPORT /mnt/c/Users/yosupo/Programs/dunkelheit/source/dkh/foundation.d */
// module dkh.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
|
D
|
import std.stdio, std.range, std.algorithm, std.conv, std.string, std.math, std.typecons;
void main() {
auto N = readln.chomp.to!int;
foreach (i; 0..N) {
auto a = readln.split.to!(int[]).map!(a => a * a).array;
writeln((a[0] + a[1] == a[2]
|| a[1] + a[2] == a[0]
|| a[2] + a[0] == a[1]) ? "YES" : "NO");
}
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.array;
void main()
{
ulong D = 1000000007;
string S = readln.chomp;
ulong[] b_mod = new ulong[13];
b_mod[0] = 1;
ulong[] c_mod;
foreach (char c; S){
c_mod = new ulong[13];
for (int i = 0; i < 13; i++){
if (c != '?'){
int c_n = c - '0';
int m = (i * 10 + c_n) % 13;
c_mod[m] += b_mod[i];
c_mod[m] %= D;
} else {
for (int j = 0; j < 10; j++){
int m = (i * 10 + j) % 13;
c_mod[m] += b_mod[i];
c_mod[m] %= D;
}
}
}
for (int i = 0; i < 13; i++) b_mod[i] = c_mod[i];
}
writeln(c_mod[5]);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.math;
import std.conv;
void main() {
auto n = (readln.chomp.to!int % 500);
auto A = readln.chomp.to!int;
if(n > A) "No".writeln;
else "Yes".writeln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }
long mod = 10^^9 + 7;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto A = RD;
auto B = RD;
auto C = RD;
writeln(A == B && B == C ? "Yes" : "No");
stdout.flush();
debug readln();
}
|
D
|
unittest
{
assert( [ "6", "ooxoox" ].parse.expand.solve == "SSSWWS" );
assert( [ "3", "oox" ].parse.expand.solve == "-1" );
assert( [ "10", "oxooxoxoox" ].parse.expand.solve == "SSWWSSSWWS" );
}
import std.conv;
import std.range;
import std.stdio;
import std.typecons;
void main()
{
stdin.byLineCopy.parse.expand.solve.writeln;
}
auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
input.popFront;
auto s = input.front;
return tuple( s );
}
auto solve( string s )
{
// 最初の二匹を仮定して整合性が取れるか判定
foreach( a; [ "SS", "SW", "WW", "WS" ] )
{
auto ans = solve2( s, a );
if( ans ) return ans.idup;
}
return "-1";
}
auto solve2( string s, string a )
{
s ~= s[ 0 ];
auto anss = new char[ s.length + 1 ];
anss[ 0 .. 2 ] = a;
for( auto i = 1L; i < s.length; i++ )
{
final switch( anss[ i - 1 .. i + 1 ] )
{
case "SS": anss[ i + 1 ] = ( s[ i ] == 'o' ) ? 'S' : 'W'; break;
case "SW": anss[ i + 1 ] = ( s[ i ] == 'o' ) ? 'W' : 'S'; break;
case "WW": anss[ i + 1 ] = ( s[ i ] == 'o' ) ? 'S' : 'W'; break;
case "WS": anss[ i + 1 ] = ( s[ i ] == 'o' ) ? 'W' : 'S'; break;
}
}
return ( anss[ 0 .. 2 ] == anss[ $ - 2 .. $ ] ) ? anss[ 0 .. $ - 2 ] : null;
}
|
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()
{
auto S = sread();
auto T = sread();
auto f = new char[](100);
auto g = new char[](100);
foreach (i; 0 .. S.length)
{
if (f[S[i] - 'a'] == char.init && g[T[i] - 'a'] == char.init)
{
f[S[i] - 'a'] = T[i];
g[T[i] - 'a'] = S[i];
continue;
}
if (f[S[i] - 'a'] != T[i] || g[T[i] - 'a'] != S[i])
{
writeln("No");
return;
}
}
writeln("Yes");
}
|
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()
{
auto S = sread();
bool eval(string s)
{
return s.length % 2 == 0 && s[0 .. $ / 2] == s[$ / 2 .. $];
}
foreach_reverse (i; 0 .. S.length)
{
if (eval(S[0 .. i]))
{
writeln(i);
return;
}
}
}
|
D
|
/+ dub.sdl:
name "B"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner, dcomp.array;
import std.container.rbtree;
// import dcomp.algorithm;
int main() {
auto sc = new Scanner(stdin);
int n;
int[] a;
sc.read(n, a);
int mi = a.minimum;
int ma = a.maximum;
if (ma - mi >= 2) {
writeln("No");
return 0;
}
if (ma - mi == 0) {
if (mi <= n/2 || mi == n-1) {
writeln("Yes");
} else {
writeln("No");
}
return 0;
}
int mic = a.count(mi).to!int;
int mac = n-mic;
if (mac == 1) {
writeln("No");
return 0;
}
if (mic + 1 <= ma && ma <= mic + mac/2) {
writeln("Yes");
} else {
writeln("No");
}
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
static if (__VERSION__ <= 2070) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
version (X86) static if (__VERSION__ < 2071) {
import core.bitop : bsf, bsr, popcnt;
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
if (f.eof) return false;
line = lineBuf[];
f.readln(line);
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else {
auto buf = line.split.map!(to!E).array;
static if (isStaticArray!T) {
assert(buf.length == T.length);
}
x = buf;
line.length = 0;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/algorithm.d */
// module dcomp.algorithm;
import std.range.primitives;
import std.traits : isFloatingPoint, isIntegral;
T binSearch(alias pred, T)(T l, T r) if (isIntegral!T) {
while (r-l > 1) {
T md = (l+r)/2;
if (!pred(md)) l = md;
else r = md;
}
return r;
}
T binSearch(alias pred, T)(T l, T r, int cnt = 60) if (isFloatingPoint!T) {
foreach (i; 0..cnt) {
T md = (l+r)/2;
if (!pred(md)) l = md;
else r = md;
}
return r;
}
E minimum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed)
if (isInputRange!Range && !isInfinite!Range) {
import std.algorithm, std.functional;
return reduce!((a, b) => binaryFun!pred(a, b) ? a : b)(seed, range);
}
ElementType!Range minimum(alias pred = "a < b", Range)(Range range) {
assert(!range.empty, "range must not empty");
auto e = range.front; range.popFront;
return minimum!pred(range, e);
}
E maximum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed)
if (isInputRange!Range && !isInfinite!Range) {
import std.algorithm, std.functional;
return reduce!((a, b) => binaryFun!pred(a, b) ? b : a)(seed, range);
}
ElementType!Range maximum(alias pred = "a < b", Range)(Range range) {
assert(!range.empty, "range must not empty");
auto e = range.front; range.popFront;
return maximum!pred(range, e);
}
Rotator!Range rotator(Range)(Range r) {
return Rotator!Range(r);
}
struct Rotator(Range)
if (isForwardRange!Range && hasLength!Range) {
size_t cnt;
Range start, now;
this(Range r) {
cnt = 0;
start = r.save;
now = r.save;
}
this(this) {
start = start.save;
now = now.save;
}
@property bool empty() {
return now.empty;
}
@property auto front() {
assert(!now.empty);
import std.range : take, chain;
return chain(now, start.take(cnt));
}
@property Rotator!Range save() {
return this;
}
void popFront() {
cnt++;
now.popFront;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/array.d */
// module dcomp.array;
T[N] fixed(T, size_t N)(T[N] a) {return a;}
struct FastAppender(A) {
import std.algorithm : max;
import std.range.primitives : ElementEncodingType;
import core.stdc.string : memcpy;
private alias T = ElementEncodingType!A;
private T* _data;
private size_t len, cap;
@property size_t length() {return len;}
void reserve(size_t nlen) {
import core.memory : GC;
if (nlen <= cap) return;
void* nx = GC.malloc(nlen * T.sizeof);
cap = nlen;
if (len) memcpy(nx, _data, len * T.sizeof);
_data = cast(T*)(nx);
}
void opOpAssign(string op : "~")(T item) {
if (len == cap) {
reserve(max(4, cap*2));
}
_data[len++] = item;
}
void clear() {
len = 0;
}
T[] data() {
return (_data) ? _data[0..len] : null;
}
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.