code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdlib;
immutable int INF = 1 << 29;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto M = s[1];
auto H = readln.split.map!(to!int).array;
auto G = new int[][](N);
foreach (i; 0..M) {
s = readln.split.map!(to!int);
auto u = s[0] - 1;
auto v = s[1] - 1;
G[u] ~= v;
G[v] ~= u;
}
int ans = 0;
foreach (i; 0..N) {
if (G[i].map!(j => H[j] < H[i]).all) {
ans += 1;
}
}
ans.writeln;
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.array;
void main() {
string[] input1 = split(readln());
string[] input2 = split(readln());
int S;
int T;
for(int i = 0; i < 4; ++i)
S += to!int(input1[i]);
for(int i = 0; i < 4; ++i)
T += to!int(input2[i]);
if (S > T)
writeln(S);
else
writeln(T);
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
int h = read.to!int;
int w = read.to!int;
bool[][] as;
foreach(i; 0 .. h){
as ~= readln.chomp.to!(char[]).map!(x => x == '*').array;
}
string ans = "YES";
bool f = 0;
int i0, j0;
foreach(i; 1 .. h - 1) foreach(j; 1 .. w - 1){
if(as[i][j] && as[i - 1][j] && as[i + 1][j] && as[i][j - 1] && as[i][j + 1]){
if(f == 0) i0 = i, j0 = j, f = 1;
else ans = "NO";
}
}
if(f == 0) ans = "NO";
if(ans == "NO"){
ans.writeln;
return;
}
int t, b, l, r;
foreach_reverse(i; 0 .. i0) if(!as[i][j0]) break; else t = i;
foreach(i; i0 + 1 .. h) if(!as[i][j0]) break; else b = i;
foreach_reverse(j; 0 .. j0) if(!as[i0][j]) break; else l = j;
foreach(j; j0 + 1 .. w) if(!as[i0][j]) break; else r = j;
foreach(i; 0 .. h) foreach(j; 0 .. w){
if(i != i0 && j != j0 && as[i][j]) ans = "NO";
if(i == i0 && (j < l || j > r) && as[i][j]) ans = "NO";
if(j == j0 && (i < t || i > b) && as[i][j]) ans = "NO";
}
ans.writeln;
}
/*
bool ng = 0;
int[] ls, rs, ts, bs;
foreach(i; 0 .. h){
int f = 0;
int l = -1, r = -1;
foreach(j; 0 .. w){
if(as[i][j]){
if(f == 0) f = 1, l = j;
else if(f == 2) ng = 1;
}
else{
if(f == 1) f = 2, r = j;
}
}
ls ~= l, rs ~= r;
}
foreach(j; 0 .. w){
int f = 0;
int t = -1, b = -1;
foreach(i; 0 .. h){
if(as[i][j]){
if(f == 0) f = 1, t = i;
else if(f == 2) ng = 1;
}
else{
if(f == 1) f = 2, b = i;
}
}
ts ~= t, bs ~= b;
}
*/
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n, m; readV(n, m);
auto g = new bool[][](n, n);
foreach (_; 0..m) {
int a, b; readV(a, b); --a; --b;
g[a][b] = g[b][a] = true;
}
auto ans = 0;
loop: foreach (r; iota(1, n).permutations) {
if (!g[0][r[0]]) continue;
foreach (i; 0..n-2)
if (!g[r[i]][r[i+1]]) continue loop;
++ans;
}
writeln(ans);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.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 W = RD;
auto a = RD;
auto b = RD;
if (a < b)
writeln(max(b-(W+a), 0));
else
writeln(max(a-(W+b), 0));
stdout.flush();
debug readln();
}
|
D
|
#!/usr/bin/env rdmd
import std.stdio, std.string, std.conv;
import std.algorithm, std.array;
import std.math, std.numeric;
import std.range;
void main()
{
for(string S; (S=readln().chomp()).length; )
{
auto m = S.split().map!(to!int)();
auto n = m[0], l=m[1], r=m[2], Ql=m[3], Qr=m[4];
auto w = array(readln().split().map!(to!int)());
auto sl = new int[n+1];
auto sr = new int[n+1];
foreach(i;0..n)
sl[i+1] = sl[i]+w[i]*l,
sr[n-i-1] = sr[n-i]+w[n-i-1]*r;
auto s = int.max;
foreach(i;0..n+1)
s = min(s,sl[i]+max(0,Ql*(i*2-n-1))+sr[i]+max(0,Qr*(n-i*2-1)));
writeln(s);
}
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto N = readln.chomp.to!int;
auto A = readln.split.map!(to!int).array;
int hi = A.reduce!max + 2;
int lo = A.reduce!min - 2;
long seg = (1L * N * (N - 1) / 2 + N);
seg = (seg - 1) / 2 + 1;
while (hi - lo > 1) {
int mid = (hi + lo) / 2;
auto st = new SegmentTree(N*3, N);
auto B = A.map!(a => a >= mid ? 1 : -1).array;
int offset = 0;
long ans = 0;
foreach (i; 0..N) {
offset += B[i];
st.add(B[i] - offset, 1);
ans += st.sum(-offset, N);
}
(ans >= seg ? lo : hi) = mid;
}
lo.writeln;
}
class SegmentTree {
long[] table;
int size;
int hoge;
this(int n, int hoge) {
assert(bsr(n) < 29);
size = 1 << (bsr(n) + 2);
table = new long[](size);
this.hoge = hoge;
}
void add(int pos, long num) {
pos += hoge;
return add(pos, num, 0, 0, size/2-1);
}
void add(int pos, long num, int i, int left, int right) {
table[i] += num;
if (left == right)
return;
auto mid = (left + right) / 2;
if (pos <= mid)
add(pos, num, i*2+1, left, mid);
else
add(pos, num, i*2+2, mid+1, right);
}
long sum(int pl, int pr) {
pl += hoge;
pr += hoge;
return sum(pl, pr, 0, 0, size/2-1);
}
long sum(int pl, int pr, int i, int left, int right) {
if (pl > right || pr < left)
return 0;
else if (pl <= left && right <= pr)
return table[i];
else
return
sum(pl, pr, i*2+1, left, (left+right)/2) +
sum(pl, pr, i*2+2, (left+right)/2+1, right);
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
void main() {
int n;
scan(n);
auto a = readln.split.to!(int[]);
auto pf = new long[](n + 1);
foreach (i ; 1 .. n + 1) {
pf[i] = pf[i-1] + a[i-1];
}
long dif(int k, int btm, int top) {
return abs(pf[top] - pf[k] - (pf[k] - pf[btm]));
}
int l, r;
long ans = 10L^^18;
foreach (i ; 0 .. n) {
if (i < 2 || (n - i) < 2) {
continue;
}
while (dif(l, 0, i) >= dif(l + 1, 0, i)) {
l++;
}
while (r < i || dif(r, i, n) >= dif(r + 1, i, n)) {
r++;
}
long p1 = pf[l] - pf[0];
long p2 = pf[i] - pf[l];
long p3 = pf[r] - pf[i];
long p4 = pf[n] - pf[r];
long mx = max(p1, p2, p3, p4);
long mn = min(p1, p2, p3, p4);
ans = min(ans, mx - mn);
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
auto f = factorize(N);
long ans;
foreach (key, val; f)
{
// writeln(key, " ", val);
long i = 1;
while (i * (i + 1) <= val * 2)
i++;
ans += i - 1;
}
writeln(ans);
}
/// 素因数分解
long[long] factorize(long x)
{
assert(0 < x, "x is negative");
long[long] ps;
while ((x & 1) == 0)
{
x /= 2;
ps[2] = (2 in ps) ? ps[2] + 1 : 1;
}
for (long i = 3; i * i <= x; i += 2)
while (x % i == 0)
{
x /= i;
ps[i] = (i in ps) ? ps[i] + 1 : 1;
}
if (x != 1)
ps[x] = (x in ps) ? ps[x] + 1 : 1;
return ps;
}
|
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 x = readint;
writeln(x < 1200 ? "ABC" : "ARC");
}
|
D
|
import std.stdio, std.string, std.conv;
void main() {
auto S = readln.split.to!(int[]);
auto sx = S[0], sy = S[1], tx = S[2], ty = S[3];
void po(string c, int x) {
foreach (i; 0..x) {
write(c);
}
}
po("R", tx-sx);
po("U", ty-sy);
po("L", tx-sx);
po("D", ty-sy);
po("L", 1);
po("U", ty-sy+1);
po("R", tx-sx+1);
po("D", 1);
po("R", 1);
po("D", ty-sy+1);
po("L", tx-sx+1);
po("U", 1);
writeln;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int N, K;
scan(N, K);
auto S = readln.chomp;
int ans = N - 1;
int t;
foreach (i ; 1 .. N) {
if (S[i] != S[i - 1]) t++;
}
t = max(0, t - 2 * K);
ans -= t;
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.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[] xs) {
int ans = xs.sum;
for (int i = 0; i < xs.length; i++) {
auto buf = xs.dup;
if (buf[i] > 0) {
buf[i]--;
for (int j = i + 1; j < buf.length; j++) {
buf[j] = 9;
}
ans = max(ans, buf.sum);
}
}
return ans;
}
void main() {
auto s = readln.chomp;
auto xs = s.map!(e => cast(int)(e - '0')).array;
auto ans = calc(xs);
writeln(ans);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.typecons;
import std.numeric, std.math;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }
long mod = 10^^9 + 7;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto a = RDR.ARR;
long least = long.max;
foreach (i; 0..201)
{
long x = cast(long)i - 100;
long cost;
foreach (e; a)
{
cost += (e - x)^^2;
}
least = min(least, cost);
}
writeln(least);
stdout.flush();
//readln();
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto ss = readln.chomp;
int res0, res1;
foreach (i, s; ss) {
if (i % 2) {
if (s == '0') {
res0++;
} else {
res1++;
}
} else {
if (s == '0') {
res1++;
} else {
res0++;
}
}
}
min(res0, res1).writeln;
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
import std.typecons, std.regex;
void main() {
int n = read.to!int;
long W = read.to!long;
auto w = new long[](n);
auto v = new int[](n);
foreach(i; 0..n) {
w[i] = read.to!long;
v[i] = read.to!int;
}
auto dp = new long[][](n+1, v.sum+1);
foreach(i; 0..dp.length) fill(dp[i], 1L << 60);
dp[0][0] = 0;
foreach(i; 0..n) {
foreach(j; 0..v.sum+1) {
chmin(dp[i+1][j], dp[i][j]);
if (j+v[i] < dp[0].length) chmin(dp[i+1][j+v[i]], dp[i][j] + w[i]);
}
}
int ret = 0;
foreach(i; 0..v.sum+1) {
if (dp[n][i] <= W) {
ret = i;
}
}
writeln(ret);
}
void chmin(T)(ref T a, T b) {
if (a > b) a = b;
}
string read() {
static string[] ss;
while (!ss.length) ss = readln.chomp.split;
auto res = ss[0];
ss.popFront;
return res;
}
|
D
|
import std.stdio;
import core.stdc.stdio;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
void main() {
int n; scanf("%d",&n);
int[] a; a.length=n;
foreach(i; 0..n) scanf("%d", &a[i]);
a.sort().reverse();
int x, y;
foreach(i, v; a) {
if(i & 1) y += v;
else x += v;
}
writeln(x - y);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new long[](t);
foreach (ti; 0..t)
{
auto s = RD!string;
long cnt = s[0] == '0' ? 1 : 0;
foreach (i; 0..s.length-1)
{
if (s[i] == '1' && s[i+1] == '0')
++cnt;
}
ans[ti] = min(cnt, 2);
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
const int DATA = 10;
int main() {
int max=-1, mid=-1, min=-1;
for(int i=0; i<DATA; i++) {
int d = to!(int)(chomp(readln));
if(max < d) {
min = mid;
mid = max;
max = d;
}
else if(mid < d) {
min = mid;
mid = d;
}
else if(min < d) {
min = d;
}
}
writeln(max, "\n", mid, "\n", min);
return 0;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.math;
void main() {
string[] inputs = split(readln());
int X = to!int(inputs[0]);
int A = to!int(inputs[1]);
int B = to!int(inputs[2]);
if(A - B >= 0) "delicious".writeln;
else if(X >= abs(A - B)) "safe".writeln;
else "dangerous".writeln;
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
bool isSameSign(int a, int b) {
if (a == 0 && b == 0) return true;
if ((a > 0 && b > 0) || (a < 0 && b < 0)) return true;
return false;
}
long calc(int[] a) {
long _calc(int sign) {
long ans = 0;
int s = sign;
int sum = 0;
foreach (x; a) {
sum += x;
// writeln("sum ", sum, " s ", s);
if (!isSameSign(sum, s)) {
int y = sum * -1 + s;
x = y;
// writeln("y = ", y);
ans += abs(x);
sum += y;
}
s *= -1;
}
// writeln(">>> ", ans);
return ans;
}
return min(_calc(1), _calc(-1));
}
void main() {
readint;
auto a = readints;
writeln(calc(a));
}
|
D
|
static immutable MOD = 4;
static immutable MOD_OVERAL = 5;
static immutable MODS = [
[1, 1, 1, 1],
[1, 2, 4, 3],
[1, 3, 4, 2],
[1, 4, 1, 4]
];
uint solve(const string n) {
uint sum = 0;
foreach (ch; n) {
if (ch < '0' || ch > '9') {break;}
sum = ( (sum * 10) + (ch - '0') ) % MOD;
}
sum %= MOD;
uint res = 0;
foreach (curMod; MODS) {
res += curMod[sum];
}
return res % MOD_OVERAL;
}
unittest {
assert(4 == solve("4"));
assert(0 == solve("124356983594583453458888889"));
assert(4 == solve("0"));
assert(0 == solve("5"));
assert(0 == solve("6"));
assert(0 == solve("7"));
assert(4 == solve("8"));
}
int main(string[] argv) {
import std.stdio;
writeln(solve(readln));
return 0;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto kab = readln.split.to!(long[]);
auto K =kab[0];
auto A = kab[1];
auto B = kab[2];
if (A+2 > B) {
writeln(K+1);
return;
} else if (K == 1) {
writeln(2);
return;
} else {
auto K2 = K - A + 1;
if (K2 < 2) {
writeln(K + 1);
return;
}
auto b = B;
K2 -= 2;
b += (K2 / 2) * (B - A);
b += (K2 % 2);
writeln(b);
}
}
|
D
|
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container;
import std.numeric, std.math;
import core.bitop;
T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
long mod = pow(10, 9) + 7;
long moda(long x, long y) { return (x + y) % mod; }
long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; }
long modm(long x, long y) { return (x * y) % mod; }
void main()
{
auto N = RD!long;
auto a = RDR.split.to!(long[]);
auto dp = new long[](N+1);
dp[0] = 0;
foreach (i, e; a)
{
dp[i+1] = dp[i] + e;
}
long best = long.max;
foreach (e; dp[1..$-1])
{
auto x = e;
auto y = dp[N] - e;
if (abs(x - y) < best)
{
best = abs(x - y);
}
}
writeln(best);
stdout.flush();
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
long A, B, C, D;
scan(A, B, C, D);
writeln(max(A * B, C * D));
}
|
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;
int[] di = [1,0,-1,0], dj = [0,1,0,-1];
int ans;
void main() {
while (1) {
int w, h;
scan(w, h);
if (!w) return;
auto bd = iota(h).map!(i => readln.chomp.to!(char[])).array;
int si, sj;
foreach (i ; 0 .. h) {
foreach (j ; 0 .. w) {
if (bd[i][j] == '@') {
si = i, sj = j;
break;
}
}
}
ans = 0;
dfs(h, w, bd, si, sj);
writeln(ans);
}
}
void dfs(int h, int w, char[][] bd, int i, int j) {
ans++;
bd[i][j] = '#';
foreach (k ; 0 .. 4) {
int ni = i + di[k];
int nj = j + dj[k];
if (ni < 0 || ni >= h || nj < 0 || nj >= w) continue;
if (bd[ni][nj] == '.') {
dfs(h, w, bd, ni, nj);
}
}
}
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;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
void main()
{
auto n = lread();
auto h = aryread();
// writeln(h);
// writeln(h.maxElement);
long cnt;
while (true)
{
if (h.maxElement == 0)
{
break;
}
long i;
while (i < n)
{
if (h[i] == 0)
{
i += 1;
}
else
{
cnt += 1;
while ((i < n) && (h[i] > 0))
{
h[i] -= 1;
i += 1;
}
}
}
}
writeln(cnt);
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n; readV(n);
int[] a; readA(n, a);
writeln(a.map!(ai => ai.bsf).sum);
}
pragma(inline) {
pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; }
pure T bitSet(T)(T n, size_t i) { return n | (T(1) << i); }
pure T bitReset(T)(T n, size_t i) { return n & ~(T(1) << i); }
pure T bitComp(T)(T n, size_t i) { return n ^ (T(1) << i); }
pure T bitSet(T)(T n, size_t s, size_t e) { return n | ((T(1) << e) - 1) & ~((T(1) << s) - 1); }
pure T bitReset(T)(T n, size_t s, size_t e) { return n & (~((T(1) << e) - 1) | ((T(1) << s) - 1)); }
pure T bitComp(T)(T n, size_t s, size_t e) { return n ^ ((T(1) << e) - 1) & ~((T(1) << s) - 1); }
import core.bitop;
pure int bsf(T)(T n) { return core.bitop.bsf(ulong(n)); }
pure int bsr(T)(T n) { return core.bitop.bsr(ulong(n)); }
pure int popcnt(T)(T n) { return core.bitop.popcnt(ulong(n)); }
}
|
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;
void main() {
long n;
scan(n);
if (n == 1) {
writeln(0);
return;
}
long ans;
for (long i = 1; i * i <= n; i++) {
if (n % i != 0)
continue;
auto m = n / i - 1;
if (n / m == n % m)
ans += m;
}
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
|
import std.stdio;
import std.array;
import std.string;
import std.conv;
void main()
{
uint count = 0;
auto n = chomp(readln());
auto s = split(readln());
auto q = to!(int)(chomp(readln()));
auto t = split(readln());
foreach (i; t)
foreach (j; s)
if (i == j) {count++; break;}
writeln(count);
}
|
D
|
import std.stdio,std.range,std.algorithm,std.conv,std.array,std.string;
void main()
{
int[1000] field;
int[1000] dice;
while(true)
{
auto input=readln.split;
int N=input[0].to!int;
int M=input[1].to!int;
if(N==0&&M==0)break;
foreach(i; 0..N)
{
field[i]=readln.chomp.to!int;
}
foreach(i; 0..M)
{
dice[i]=readln.chomp.to!int;
}
int i, cur;
while(true)
{
cur+=dice[i++];
if(cur >= N - 1) break;
cur+=field[cur];
if(cur >= N - 1) break;
}
i.writeln;
}
}
|
D
|
ulong[] generate_prime_list(T)(T N) if(isIntegral!T) {
ulong[] prime_list = [2];
bool not_prime = false;
foreach(i; 3..N.to!ulong+1) {
auto p = i.to!float.sqrt.ceil.to!ulong;
foreach(j; prime_list) {
if(j > p) break;
if(i % j == 0) {
not_prime = true;
break;
}
}
if(!not_prime) prime_list ~= i;
not_prime = false;
}
return prime_list;
}
void main() {
auto X = ri;
auto arr = generate_prime_list(100010);
foreach(v; arr) {
if(X > v) continue;
else {
v.writeln;
return;
}
}
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
// Vicfred
// https://atcoder.jp/contests/abc155/tasks/abc155_c
import std.algorithm;
import std.conv;
import std.stdio;
import std.string;
void main() {
int n = readln.chomp.to!int;
int[string] votes;
foreach(_; 0..n) {
string s = readln.chomp;
votes[s] = votes.get(s,0)+1;
}
int maxima = 0;
foreach(item; votes.values()) {
maxima = max(maxima, item);
}
string[] ans;
foreach(item; votes.keys()) {
if(votes[item] == maxima)
ans ~= item;
}
sort(ans);
foreach(item; ans)
item.writeln;
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
void main(string[] args) {
readln();
auto a = readln().chomp.split.map!(to!int);
int[] t;
void f(int i){if(t.length<=i){t.length=i+1;} t[i]++;}
foreach(int i; a){
if(i>0){ f(i-1); }
f(i);
f(i+1);
}
t.reduce!max.writeln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 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;
auto H = RDA;
long ans, cnt;
foreach (i; 1..N)
{
if (H[i] <= H[i-1])
{
++cnt;
}
else
{
ans = max(ans, cnt);
cnt = 0;
}
}
ans = max(ans, cnt);
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random;
import std.typecons, std.functional, std.traits,std.concurrency;
import std.algorithm, std.container;
import core.bitop, core.time, core.memory;
import std.bitmanip;
import std.regex;
enum INF = long.max/3;
enum MOD = 10L^^9+7;
//辞書順順列はiota(1,N),nextPermituionを使う
void end(T)(T v)
if(isIntegral!T||isSomeString!T||isSomeChar!T)
{
import core.stdc.stdlib;
writeln(v);
exit(0);
}
T[] scanArray(T = long)()
{
static char[] scanBuf;
readln(scanBuf);
return scanBuf.split.to!(T[]);
}
dchar scanChar()
{
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
return cast(dchar)c;
}
T scanElem(T = long)()
{
import core.stdc.stdlib;
static auto scanBuf = appender!(char[])([]);
scanBuf.clear;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
scanBuf ~= cast(char) c;
c = getchar;
}
return scanBuf.data.to!T;
}
dchar[] scanString(){
return scanElem!(dchar[]);
}
void main()
{
auto x = scanElem;
if(abs(x-scanElem)<abs(x-scanElem))end("A");end("B");
}
|
D
|
//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
class InputReader {
private:
ubyte[] p;
ubyte[] buffer;
size_t cur;
public:
this () {
buffer = uninitializedArray!(ubyte[])(16<<20);
p = stdin.rawRead (buffer);
}
final ubyte skipByte (ubyte lo) {
while (true) {
auto a = p[cur .. $];
auto r = a.find! (c => c >= lo);
if (!r.empty) {
cur += a.length - r.length;
return p[cur++];
}
p = stdin.rawRead (buffer);
cur = 0;
if (p.empty) return 0;
}
}
final ubyte nextByte () {
if (cur < p.length) {
return p[cur++];
}
p = stdin.rawRead (buffer);
if (p.empty) return 0;
cur = 1;
return p[0];
}
template next(T) if (isSigned!T) {
final T next () {
T res;
ubyte b = skipByte (45);
if (b == 45) {
while (true) {
b = nextByte ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 - (b - 48);
}
} else {
res = b - 48;
while (true) {
b = nextByte ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 + (b - 48);
}
}
}
}
template next(T) if (isUnsigned!T) {
final T next () {
T res = skipByte (48) - 48;
while (true) {
ubyte b = nextByte ();
if (b < 48 || b >= 58) {
break;
}
res = res * 10 + (b - 48);
}
return res;
}
}
final T[] nextA(T) (int n) {
auto a = uninitializedArray!(T[]) (n);
foreach (i; 0 .. n) {
a[i] = next!T;
}
return a;
}
}
void main() {
auto r = new InputReader;
immutable nt = r.next!uint;
foreach (tid; 0 .. nt) {
immutable n = r.next!uint;
immutable k = r.next!uint;
auto a = r.nextA!int (n);
long res = int.max;
int x = int.max;
foreach (i; k .. n) {
int d = a[i] - a[i - k];
if (res > d) {
res = d;
x = (a[i] + a[i - k]) / 2;
}
}
writeln (x);
}
}
|
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 K = RD;
auto A = RD;
auto B = RD;
bool ans;
foreach (i; A..B+1)
{
if (i % K == 0)
{
ans = true;
}
}
writeln(ans ? "OK" : "NG");
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n, m;
scan(n, m);
auto p = readln.split.to!(int[]);
p[] -= 1;
auto uf = UnionFind(n);
foreach (i ; 0 .. m) {
int xi, yi;
scan(xi, yi);
xi--, yi--;
uf.merge(p[xi], p[yi]);
}
int ans;
foreach (i ; 0 .. n) {
ans += uf.same(i, p[i]);
}
writeln(ans);
}
struct UnionFind {
private {
int _size;
int[] _parent;
}
this(int N)
in {
assert(N > 0);
}
body {
_size = N;
_parent = new int[](_size);
foreach (i ; 0 .. _size) {
_parent[i] = i;
}
}
int findRoot(int x)
in {
assert(0 <= x && x < _size);
}
body {
if (_parent[x] != x) {
_parent[x] = findRoot(_parent[x]);
}
return _parent[x];
}
bool same(int x, int y)
in {
assert(0 <= x && x < _size);
assert(0 <= y && y < _size);
}
body {
return findRoot(x) == findRoot(y);
}
void merge(int x, int y)
in {
assert(0 <= x && x < _size);
assert(0 <= y && y < _size);
}
body {
int u = findRoot(x);
int v = findRoot(y);
if (u == v) return;
_parent[v] = u;
}
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }
long mod = 10^^9 + 7;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
long f(long x)
{
if (x < 10) return 9;
long ans = 1;
++x;
while (x % 10 == 0)
{
x /= 10;
}
return ans + f(x);
}
writeln(f(N));
stdout.flush();
debug readln();
}
|
D
|
void main() {
auto S = rs;
auto A = S[0..2].to!int;
auto B = S[2..4].to!int;
bool YYMM = 0 <= A && A <= 99 && 0 < B && B <= 12;
bool MMYY = 0 <= B && B <= 99 && 0 < A && A <= 12;
if(YYMM && MMYY) writeln("AMBIGUOUS");
else if(YYMM) writeln("YYMM");
else if(MMYY) writeln("MMYY");
else writeln("NA");
}
// =======================111=1===========
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.ascii : toLower;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.stdio,std.string,std.conv;
int main()
{
string s;
bool[int] m;
bool[int] m2;
int[int] count;
while((s = readln.chomp).length != 0)
{
string[] _s = s.split(",");
int a = _s[0].to!int;
m[a] = true;
count[a]++;
}
while((s = readln.chomp).length != 0)
{
string[] _s = s.split(",");
int a = _s[0].to!int;
m2[a] = true;
count[a]++;
}
foreach(i;0..1001)
{
bool* p = (i in m);
bool* p2 = (i in m2);
if(p && p2)
{
writeln(i," ",count[i]);
}
}
return 0;
}
|
D
|
import std.algorithm;
import std.conv;
import std.stdio;
import std.string;
void main()
{
auto hw = readln.split.map!( to!byte );
auto s = new string[ hw[ 0 ] ];
foreach( ref sl; s )
{
sl = readln.strip;
}
writeln( solve( hw[ 0 ], hw[ 1 ], s ) );
}
string solve( in byte h, in byte w, in string[] s )
{
bool[][] r;
r.length = h;
foreach( ref rl; r )
{
rl.length = w;
}
foreach( y; 0 .. h )
{
foreach( x; 0 .. w )
{
if( s[ y ][ x ] == '#' )
{
if( y + 1 < h && s[ y + 1 ][ x ] == '#' )
{
r[ y + 1 ][ x ] = true;
r[ y ][ x ] = true;
}
if( x + 1 < w && s[ y ][ x + 1 ] == '#' )
{
r[ y ][ x + 1 ] = true;
r[ y ][ x ] = true;
}
if( r[ y ][ x ] == false ) return "No";
}
}
}
return "Yes";
}
unittest
{
assert( solve( 3, 3, [ ".#.", "###", ".#." ] ) == "Yes" );
assert( solve( 5, 5, [ "#.#.#", ".#.#.", "#.#.#", ".#.#.", "#.#.#" ] ) == "No" );
assert( solve( 11, 11, [ "...#####...", ".##.....##.", "#..##.##..#", "#..##.##..#", "#.........#", "#...###...#", ".#########.", ".#.#.#.#.#.", "##.#.#.#.##", "..##.#.##..", ".##..#..##." ] ) == "Yes" );
assert( solve( 4, 3, [ "##.", ".##", "##.", ".##" ] ) == "Yes" );
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new long[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto W = RD;
auto w = RDA;
int len;
auto tmp = W;
while (tmp)
{
++len;
tmp /= 2;
}
auto cnt = new long[](len);
foreach (i; 0..n)
{
int c;
while (w[i])
{
++c;
w[i] /= 2;
}
++cnt[c-1];
}
debug writeln("cnt:", cnt);
while (true)
{
auto rem = W;
foreach_reverse (i; 0..cnt.length)
{
if (cnt[i] == 0) continue;
auto bit = 1L << i;
if (rem < bit) continue;
auto c = min(rem / bit, cnt[i]);
rem -= bit * c;
debug writeln("rem:", rem, " c:", c);
cnt[i] -= c;
}
if (rem == W) break;
++ans[ti];
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
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;
auto data = readln.split, T = data[0].to!int, A = data[1].to!int;
auto H = readln.split.to!(int[]);
auto diff = N.iota.map!(n => (T.to!real - H[n].to!real*0.006 - A).abs );
ulong result = 0;
N.iota.each!(x => diff[x] < diff[result] ? result = x : 0);
(1+result).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;
alias Pair = Tuple!(int, "l", int, "x");
void main(){
int N, M;
int li, ri, xi;
readVars(N, M);
auto cond = new Pair[][](N + 1);
foreach(i ; 0 .. M){
readVars(li, ri, xi);
cond[ri] ~= Pair(li, xi);
}
//writeln(cond);
auto dp = new int[][][](N + 1, N + 1, N + 1);
dp[0][0][0] = 1;
int k, v;
foreach(r ; 0 .. N + 1){
foreach(g ; 0 .. N + 1){
foreach(b ; 0 .. N + 1){
k = max(r, g, b);
bool flag;
foreach(c ; cond[k]){
v = (c.l <= r) + (c.l <= g) + (c.l <= b);
if (v != c.x) {
flag = true;
break;
}
}
if (flag){
dp[r][g][b] = 0;
continue;
}
if (k + 1 <= N) {
dp[k + 1][g][b] = (dp[k + 1][g][b] + dp[r][g][b]) % mod;
dp[r][k + 1][b] = (dp[r][k + 1][b] + dp[r][g][b]) % mod;
dp[r][g][k + 1] = (dp[r][g][k + 1] + dp[r][g][b]) % mod;
}
}
}
}
int ans;
foreach(r ; 0 .. N + 1){
foreach(g ; 0 .. N + 1){
ans = (ans + dp[r][g][N]) % mod;
}
}
foreach(r ; 0 .. N + 1){
foreach(b ; 0 .. N + 1){
ans = (ans + dp[r][N][b]) % mod;
}
}
foreach(g ; 0 .. N + 1){
foreach(b ; 0 .. N + 1){
ans = (ans + dp[N][g][b]) % mod;
}
}
writeln(ans);
}
void readVars(T...)(auto ref T args){
auto line = readln.split;
foreach(ref arg ; args){
arg = line.front.to!(typeof(arg));
line.popFront;
}
if(!line.empty){
throw new Exception("args num < input num");
}
}
|
D
|
import std.stdio;
import std.ascii;
import std.conv;
import std.string;
import std.algorithm;
import std.range;
import std.functional;
import std.math;
import core.bitop;
import std.numeric;
alias lread = () => readln.chomp.to!long;
alias aryread = () => readln.split.to!(long[]);
void main()
{
auto nx = aryread();
long N = nx[0];
long X = nx[1];
auto x = aryread().array;
x.map!(a => abs(a - X))
.reduce!((a, b) => gcd(a, b))
.writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main() {
string[] inputs = split(readln());
int a = to!int(inputs[0]);
int b = to!int(inputs[1]);
int c = to!int(inputs[2]);
bool[int] flags;
flags[a] = true;
flags[b] = true;
flags[c] = true;
flags.length.writeln;
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.functional;
import std.array;
import std.conv;
import std.math;
import std.typecons;
import std.regex;
import std.range;
int n,s;
int saiki(int n1,int u,int m){
if(n1 > s) return 0;
else if(m==1){
int d = s-n1;
if(d <= 9 && (u & (1<<d))==0) return 1;
else return 0;
}else if(m==2){
if(s-n1>26) return 0;
}else if(m==3){
if(s-n1>50) return 0;
}else if(m==4){
if(s-n1>80) return 0;
}else if(m==5){
if(s-n1>115) return 0;
}
int ans;
for(int i=9;i>=0;i--){
if((u & (1<<i))==0){
ans += saiki(n1+m*i,u|(1<<i),m-1);
}
}
return ans;
}
void main(){
while(true){
auto ss = readln();
if(stdin.eof()) break;
int[] s1 = ss.split().to!(int[]);
n = s1[0];
s = s1[1];
if(n <= 330)
writeln(saiki(0,0,n));
else writeln(0);
}
}
|
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 pickV(R,T...)(ref R r,ref T t){foreach(ref v;t)pick(r,v);}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n, m; readV(n, m);
auto c = new int[](m+1);
foreach (i; 0..n) {
auto rd = rdsp;
int k; pickV(rd, k);
foreach (j; 0..k) {
int a; pickV(rd, a);
++c[a];
}
}
writeln(c.count(n));
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nx = readln.split.to!(int[]);
auto N = nx[0];
auto X = nx[1];
auto LS = readln.split.to!(int[]);
int d, c;
foreach (l; LS) {
++c;
d += l;
if (d > X) break;
}
if (d <= X) ++c;
writeln(c);
}
|
D
|
import std.stdio;
import std.algorithm;
import std.conv;
import std.datetime;
import std.numeric;
import std.math;
import std.string;
string my_readln() { return chomp(readln()); }
void main()
{//try{
auto tokens = split(my_readln());
auto N = to!ulong(tokens[0]);
auto H = to!ulong(tokens[1]);
auto W = to!ulong(tokens[2]);
ulong ans;
foreach (i; 0..N)
{
auto tokens2 = split(my_readln());
auto A = to!ulong(tokens2[0]);
auto B = to!ulong(tokens2[1]);
if (A >= H && B >= W) ++ans;
}
writeln(ans);
stdout.flush();
/*}catch (Throwable e)
{
writeln(e.toString());
}
readln();*/
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd = readln.split, n = rd[0].to!size_t, x = rd[1].to!long;
auto a = readln.split.to!(long[]);
auto ans = 0L;
foreach (i; 1..n) {
auto s = a[i-1] + a[i];
if (s <= x) continue;
auto r = min(s - x, a[i]);
a[i] -= r;
ans += r;
auto t = s - x - r;
if (t > 0) {
ans += t;
a[i-1] -= t;
}
}
writeln(ans);
}
|
D
|
void main(){
int[] ab = _scanln();
(ab[0] * ab[1]).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.algorithm, std.conv, std.array, std.string;
wchar[600][600] BD1, BD2;
void main()
{
auto N = readln.chomp.to!int;
foreach (i; 0..N) {
foreach (j, c; readln.chomp) {
BD1[i][j] = BD1[i][N+j] = BD1[N+i][j] = BD1[N+i][N+j] = c;
BD2[j][i] = BD2[j][N+i] = BD2[N+j][i] = BD2[N+j][N+i] = c;
}
}
int cnt;
foreach (a; 0..N) {
foreach (i; 0..N) {
if (BD1[a+i][0..N] != BD2[i][a..N+a]) goto bad;
}
++cnt;
bad:
}
writeln(cnt * N);
}
|
D
|
void main() {
int n = readln.chomp.to!int;
int[] a = readln.split.to!(int[]);
bool[] colors = new bool[8];
int red;
foreach (x; a) {
if (x < 3200) {
colors[x/400] = true;
} else {
++red;
}
}
int cmin = colors.count(true).to!int;
int cmax = cmin + red;
if (cmin == 0 && red > 0) ++cmin;
writeln(cmin, " ", cmax);
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
auto s = sread();
long n = s.length;
long max_x = long.min;
long min_x = long.max;
foreach (i; 0 .. n)
{
if (s[i] == 'A')
{
// writeln(i);
min_x = min(min_x, i);
}
}
foreach (i; 0 .. n)
{
if (s[i] == 'Z')
{
// writeln(i);
max_x = max(max_x, i);
}
}
// writeln(min_x);
// writeln(max_x);
writeln(max_x - min_x + 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 std.stdio, std.string, std.algorithm, std.array, std.range, std.conv,
std.typecons, std.math, std.container, std.format;
void main(string[] args)
{
auto s = readln.strip();
long cnt = 0;
foreach (i; 0 .. s.length / 2)
{
if (s[i] != s[s.length - i - 1])
{
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;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() {
int n;
while ((n = readint) > 0) {
int ans = 0;
for (int i = 3; i <= n; i += 2) {
if (n % i == 0) {
ans++;
}
}
writeln(ans);
}
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto x = readln.chomp.to!long;
auto ans = x / 11 * 2, mod = x % 11;
if (1 <= mod && mod <= 6) ans += 1;
else if (5 <= mod) ans += 2;
writeln(ans);
}
|
D
|
import std;
enum inf(T)()if(__traits(isArithmetic,T)){return T.max/4;}
T scan(T=long)(){return readln.chomp.to!T;}
void scan(T...)(ref T args){auto input=readln.chomp.split;foreach(i,t;T)args[i]=input[i].to!t;}T[] scanarr(T=long)(){return readln.chomp.split.to!(T[]);}
//END OF TEMPLATE
void main(){
ulong n,k;
scan(n,k);
ulong[ulong] a;
foreach(i;0..k){
scan!int;
scanarr!ulong.each!(as=>a[as]=0);
}
iota(1,n+1).filter!(s=>!(s in a)).array.length.writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main() {
string ans = "UNRESTORABLE";
string s = readln.chomp;
string t = readln.chomp;
if (t.length <= s.length) {
char[] t_ans;
foreach_reverse (i; 0..s.length - t.length + 1) {
bool flag = true;
t_ans = s.dup;
foreach_reverse (j; 0..t.length) {
if (s[i + j] != t[j] && s[i + j] != '?'){
flag = false;
break;
} else {
t_ans[i + j] = t[j];
}
}
if (flag) {
ans = t_ans.to!string.tr("?", "a");
break;
}
}
}
writeln(ans);
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void main()
{
int r; readV(r);
int g; readV(g);
writeln(2*g-r);
}
|
D
|
import std.stdio;
import std.array;
import std.string;
import std.conv;
import std.algorithm;
import std.typecons;
import std.range;
import std.random;
import std.math;
void main() {
int N = readln().chomp.to!int;
int Lsum = 0;
int Rsum = 0;
int[] L = new int[](N);
int[] R = new int[](N);
foreach (i; iota(N)) {
auto input = readln().split.map!(to!int);
Lsum += input[0];
Rsum += input[1];
L[i] = input[0];
R[i] = input[1];
}
int max_b = abs(Lsum-Rsum);
int max_i = 0;
foreach (i; iota(N)) {
if (max_b < abs((Lsum-L[i]+R[i])-(Rsum-R[i]+L[i]))) {
max_b = abs((Lsum-L[i]+R[i])-(Rsum-R[i]+L[i]));
max_i = i+1;
}
}
writeln(max_i);
}
|
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 s;
scan(s);
int ans = 1;
bool[long] e;
long f(long x) {
if (x & 1) {
return 3 * x + 1;
}
else {
return x / 2;
}
}
long a = s;
e[s] = true;
while (true) {
ans++;
a = f(a);
if (a in e) {
writeln(ans);
return;
}
e[a] = true;
}
}
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.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;
}
bool isOnSegment(Vec2 p, Vec2 a, Vec2 b) {
auto ab = b - a;
auto ap = p - a;
if (ab.cross(ap) != 0)
return false;
// ab ??¨ ap ????????????
if (ab.dot(ap) < 0)
return false;
// p ??? ab ?????????????????????
if (ab.magSq() >= ap.magSq())
return true;
return false;
}
bool isIntersectLineSegments(Vec2 a, Vec2 b, Vec2 c, Vec2 d) {
auto s = (b - a).cross(c - a) * (b - a).cross(d - a);
auto t = (d - c).cross(a - c) * (d - c).cross(b - c);
if (s < 0 && t < 0)
return true;
// ???????????????????????????
if (isOnSegment(c, a, b) || isOnSegment(d, a, b) || isOnSegment(a, c, d) || isOnSegment(b, c, d)) {
return true;
}
return false;
}
void main() {
int q = readint();
for (int i = 0; i < q; i++) {
auto xs = readints();
auto a = Vec2(xs[0], xs[1]);
auto b = Vec2(xs[2], xs[3]);
auto c = Vec2(xs[4], xs[5]);
auto d = Vec2(xs[6], xs[7]);
if (isIntersectLineSegments(a, b, c, d))
writeln(1);
else
writeln(0);
}
}
struct Vec2 {
immutable double x;
immutable double y;
this(double x, double y) {
this.x = x;
this.y = y;
}
Vec2 opAdd(Vec2 other) {
return Vec2(this.x + other.x, this.y + other.y);
}
Vec2 opSub(Vec2 other) {
return Vec2(this.x - other.x, this.y - other.y);
}
Vec2 opMul(double d) {
return Vec2(this.x * d, this.y * d);
}
double dot(Vec2 other) {
return this.x * other.x + this.y * other.y;
}
double cross(Vec2 other) {
return this.x * other.y - other.x * this.y;
}
double mag() {
return sqrt(magSq());
}
double magSq() {
return this.x * this.x + this.y * this.y;
}
Vec2 normalize() {
auto m = mag();
if (m != 0 && m != 1)
return Vec2(this.x / m, this.y / m);
return this;
}
static double distance(Vec2 a, Vec2 b) {
return (a - b).mag();
}
}
|
D
|
import std.stdio, std.string, std.conv;
void main()
{
auto n = readln.chomp.to!int;
auto a = readln.chomp.to!int;
writeln(n * n - a);
}
|
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, M;
scan(N, M);
int ans;
for (int i = 1; i*i <= M; i++) {
if (M % i != 0) continue;
if (M / i >= N) chmax(ans, i);
if (M / (M / i) >= N) chmax(ans, M / i);
}
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
|
//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;
string S, T;
sc.scan(S, T);
foreach (i; 0 .. S.length) {
if (S == T) {
writeln("Yes");
return;
}
S ~= S.front;
S.popFront;
}
writeln("No");
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
void main()
{
int[][] dp = new int[][](1001, 10001);
int cases = 1;
while(true)
{
int w = readln.chomp.to!int;
if(!w)break;
int n = readln.chomp.to!int;
int[1001] treValue, treWeit;
foreach(i; 1 .. n + 1)
{
auto f = readln.chomp.split(",").map!(to!int);
treValue[i] = f[0];
treWeit[i] = f[1];
}
foreach(i; 1 .. n + 1)
{
foreach(j; 1 .. w + 1)
{
if(treWeit[i] > j || dp[i - 1][j - treWeit[i]] + treValue[i] <= dp[i - 1][j])
{
dp[i][j] = dp[i - 1][j];
}
else
{
dp[i][j] = dp[i - 1][j - treWeit[i]] + treValue[i];
}
}
}
int maxValue = int.min, minWeit;
foreach(i; 1 .. w + 1)
{
if(dp[n][i] > maxValue)
{
maxValue = dp[n][i];
minWeit = i;
}
}
writeln("Case ", cases, ":");
maxValue.writeln;
minWeit.writeln;
++cases;
}
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
import std.typecons;
import std.bigint;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.container.deque;
/*
int main() {
auto sc = new Scanner(stdin);
int n, m, k;
sc.read(n, m, k); m++;
int[] a = new int[n];
a.each!((ref x) => sc.read(x));
long[] dp = a.map!(to!long).array;
long[] ndp = new long[n];
auto deq = Deque!(int, false).make();
foreach (int ph; 2..k+1) {
ndp[] = -(10L^^18);
deq.clear();
foreach (int i; 0..n) {
if (deq.length && deq.front == i-m) {
deq.removeFront();
}
if (deq.length) {
ndp[i] = dp[deq.front] + 1L * ph * a[i];
}
while (deq.length && dp[deq[deq.length-1]] <= dp[i]) {
deq.removeBack();
}
deq.insertBack(i);
}
swap(dp, ndp);
}
writeln(dp.fold!max);
return 0;
}
*/
int main() {
auto sc = new Scanner(stdin);
int n, m, k;
sc.read(n, m, k); m++;
int[] a = new int[n];
a.each!((ref x) => sc.read(x));
long[] dp = a.map!(to!long).array;
long[] ndp = new long[n];
auto deq = Deque!int();
deq.insertBack(1); deq.removeBack();
auto p = deq.p;
foreach (int ph; 2..k+1) {
ndp[] = -(10L^^18);
deq.clear();
foreach (int i; 0..n) {
if (p.length && (*p)[0] == i-m) {
p.removeFront();
}
if (p.length) {
ndp[i] = dp[(*p)[0]] + 1L * ph * a[i];
}
while (p.length && dp[(*p)[p.length-1]] <= dp[i]) {
p.removeBack();
}
p.insertBack(i);
}
swap(dp, ndp);
}
writeln(dp.fold!max);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
//fold(for old compiler)
static if (__VERSION__ <= 2070) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
unittest {
import std.stdio;
auto l = [1, 2, 3, 4, 5];
assert(l.fold!"a+b"(10) == 25);
}
}
version (X86) static if (__VERSION__ < 2071) {
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/deque.d */
// module dcomp.container.deque;
struct Deque(T, bool hasNull = true) {
import core.exception : RangeError;
import core.memory : GC;
import std.range : ElementType, isInputRange;
import std.traits : isImplicitlyConvertible;
struct Payload {
T *d;
size_t st, length, cap;
@property bool empty() const { return length == 0; }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {
version(assert) if (length <= i) throw new RangeError();
return d[(st+i >= cap) ? (st+i-cap) : st+i];
}
private void expand() {
import std.algorithm : max;
assert(length == cap);
auto nc = max(size_t(4), 2*cap);
T* nd = cast(T*)GC.malloc(nc * T.sizeof);
foreach (i; 0..length) {
nd[i] = this[i];
}
d = nd; st = 0; cap = nc;
}
void clear() {
st = length = 0;
}
void insertFront(T v) {
if (length == cap) expand();
if (st == 0) st += cap;
st--; length++;
this[0] = v;
}
void insertBack(T v) {
if (length == cap) expand();
length++;
this[length-1] = v;
}
void removeFront() {
assert(!empty, "Deque.removeFront: Deque is empty");
st++; length--;
if (st == cap) st = 0;
}
void removeBack() {
assert(!empty, "Deque.removeBack: Deque is empty");
length--;
}
}
struct RangeT(A) {
alias T = typeof(*(A.p));
alias E = typeof(A.p.d[0]);
T *p;
size_t a, b;
@property bool empty() const { return b <= a; }
@property size_t length() const { return b-a; }
@property RangeT save() { return RangeT(p, a, b); }
@property RangeT!(const A) save() const {
return typeof(return)(p, a, b);
}
alias opDollar = length;
@property ref inout(E) front() inout { return (*p)[a]; }
@property ref inout(E) back() inout { return (*p)[b-1]; }
void popFront() {
version(assert) if (empty) throw new RangeError();
a++;
}
void popBack() {
version(assert) if (empty) throw new RangeError();
b--;
}
ref inout(E) opIndex(size_t i) inout { return (*p)[i]; }
RangeT opSlice() { return this.save; }
RangeT opSlice(size_t i, size_t j) {
version(assert) if (i > j || a + j > b) throw new RangeError();
return typeof(return)(p, a+i, a+j);
}
RangeT!(const A) opSlice() const { return this.save; }
RangeT!(const A) opSlice(size_t i, size_t j) const {
version(assert) if (i > j || a + j > b) throw new RangeError();
return typeof(return)(p, a+i, a+j);
}
}
alias Range = RangeT!Deque;
alias ConstRange = RangeT!(const Deque);
alias ImmutableRange = RangeT!(immutable Deque);
Payload *p;
private void I() { if (hasNull && !p) p = new Payload(); }
private void C() const { version(assert) if (!p) throw new RangeError(); }
//some value
this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) {I;
p = new Payload();
foreach (v; values) {
insertBack(v);
}
}
//range
this(Range)(Range r)
if (isInputRange!Range &&
isImplicitlyConvertible!(ElementType!Range, T) &&
!is(Range == T[])) {I;
p = new Payload();
foreach (v; r) {
insertBack(v);
}
}
static Deque make() {
Deque que;
que.p = new Payload();
return que;
}
@property private bool hasPayload() const { return (!hasNull || p); }
@property bool empty() const { return (!hasPayload || p.empty); }
@property size_t length() const { return (hasPayload ? p.length : 0); }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; }
ref inout(T) front() inout {C; return (*p)[0]; }
ref inout(T) back() inout {C; return (*p)[$-1]; }
void clear() { if (p) p.clear(); }
void insertFront(T v) {I; p.insertFront(v); }
void insertBack(T v) {I; p.insertBack(v); }
void removeFront() {C; p.removeFront(); }
void removeBack() {C; p.removeBack(); }
Range opSlice() {I; return Range(p, 0, length); }
}
unittest {
import std.algorithm : equal;
import std.range.primitives : isRandomAccessRange;
import std.container.util : make;
auto q = make!(Deque!int);
assert(isRandomAccessRange!(typeof(q[])));
//insert,remove
assert(equal(q[], new int[](0)));
q.insertBack(1);
assert(equal(q[], [1]));
q.insertBack(2);
assert(equal(q[], [1, 2]));
q.insertFront(3);
assert(equal(q[], [3, 1, 2]) && q.front == 3);
q.removeFront;
assert(equal(q[], [1, 2]) && q.length == 2);
q.insertBack(4);
assert(equal(q[], [1, 2, 4]) && q.front == 1 && q.back == 4 && q[$-1] == 4);
q.insertFront(5);
assert(equal(q[], [5, 1, 2, 4]));
//range
assert(equal(q[][1..3], [1, 2]));
assert(equal(q[][][][], q[]));
//const range
const auto rng = q[];
assert(rng.front == 5 && rng.back == 4);
//reference type
auto q2 = q;
q2.insertBack(6);
q2.insertFront(7);
assert(equal(q[], q2[]) && q.length == q2.length);
//construct with make
auto a = make!(Deque!int)(1, 2, 3);
auto b = make!(Deque!int)([1, 2, 3]);
assert(equal(a[], b[]));
}
unittest {
import std.algorithm : equal;
import std.range.primitives : isRandomAccessRange;
import std.container.util : make;
auto q = make!(Deque!int);
q.clear();
assert(equal(q[], new int[0]));
foreach (i; 0..100) {
q.insertBack(1);
q.insertBack(2);
q.insertBack(3);
q.insertBack(4);
q.insertBack(5);
assert(equal(q[], [1,2,3,4,5]));
q.clear();
assert(equal(q[], new int[0]));
}
}
unittest {
Deque!int a;
Deque!int b;
a.insertFront(2);
assert(b.length == 0);
}
unittest {
import std.algorithm : equal;
import std.range : iota;
Deque!int a;
foreach (i; 0..100) {
a.insertBack(i);
}
assert(equal(a[], iota(100)));
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
if (f.eof) return false;
line = lineBuf[];
f.readln(line);
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
//string or char[10] etc
//todo optimize
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else {
auto buf = line.split.map!(to!E).array;
static if (isStaticArray!T) {
//static
assert(buf.length == T.length);
}
x = buf;
line.length = 0;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
fout.writeln("1 2 3");
fout.writeln("ab cde");
fout.writeln("1.0 1.0 2.0");
fout.close;
Scanner sc = new Scanner(File(fileName, "r"));
int a;
int[2] b;
char[2] c;
string d;
double e;
double[] f;
sc.read(a, b, c, d, e, f);
assert(a == 1);
assert(equal(b[], [2, 3]));
assert(equal(c[], "ab"));
assert(equal(d, "cde"));
assert(e == 1.0);
assert(equal(f, [1.0, 2.0]));
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File, writeln;
import std.datetime;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
foreach (i; 0..1_000_000) {
fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2);
}
fout.close;
writeln("Scanner Speed Test(3*1,000,000 int)");
StopWatch sw;
sw.start;
Scanner sc = new Scanner(File(fileName, "r"));
foreach (i; 0..500_000) {
int a, b, c;
sc.read(a, b, c);
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
foreach (i; 500_000..700_000) {
int[3] d;
sc.read(d);
int a = d[0], b = d[1], c = d[2];
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
foreach (i; 700_000..1_000_000) {
int[] d;
sc.read(d);
assert(d.length == 3);
int a = d[0], b = d[1], c = d[2];
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
writeln(sw.peek.msecs, "ms");
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.functional;
import std.array;
import std.conv;
import std.math;
import std.typecons;
import std.regex;
import std.range;
void main(){
while(true){
int n = readln().chomp().to!int;
if(n==0)break;
int m = readln().chomp().to!int;
int p = readln().chomp().to!int;
int d = readln().chomp().to!int;
string[] s;
int[11] h;
for(int i=0;i<11;i++) h[i] = i;
for(int i=0;i<d;i++){
s ~= readln().chomp();
for(int j=0;j<n-1;j++){
if(s[i][j] == '1'){
swap(h[j+1],h[j+2]);
}
}
}
bool flg = false;
if(h[p] == m) {
writeln(0);
flg = true;
}
else{
for(int i=0;i<d;i++){
for(int j=0;j<n-1;j++){
if(j<n-2 && s[i][j+1] == '1' ) continue;
if(j>0 && s[i][j-1] == '1' ) continue;
int[11] h1;
for(int i1=0;i1<11;i1++) h1[i1] = i1;
for(int k=0;k<d;k++){
for(int l=0;l<n-1;l++){
if(s[k][l] == '1' ||(k==i&&l==j)){
swap(h1[l+1],h1[l+2]);
}
}
}
if(h1[p]==m){
flg = true;
writeln(i+1," ",j+1);
break;
}
}
if(flg) break;
}
}
if(!flg) writeln(1);
}
}
|
D
|
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
void main(){
iota(1, 10).map!(a => iota(1, 10).map!(b => text(a, "x", b, "=", a * b)).join("\n")).join("\n").writeln;
}
|
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 5
---+/
/+:---test
2
3
---+/
/+---test
6
0 153 10 10 23
---+/
void main(string[] args) {
readln;
const B = readln.split.map!(to!long).array;
if (B.length == 1) {
(B[0]*2).writeln;
return;
}
long ans = B[0] + B[$-1];
foreach (i; 1..B.length) {
ans += min(B[i-1], B[i]);
}
ans.writeln;
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.ascii;
void main()
{
auto s = readln.chomp;
auto t = readln.chomp;
int cnt;
foreach (i; 0..3) {
if (s[i] == t[i]) {
cnt++;
}
}
cnt.writeln;
}
|
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 luca = new long[](100);
luca[0] = 2, luca[1] = 1;
foreach (i ; 2 .. 100) {
luca[i] = luca[i - 1] + luca[i - 2];
}
writeln(luca[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);
}
}
}
struct Queue(T) {
private {
int N, head, tail;
T[] data;
}
this(int n) {
N = n + 1;
data = new T[](N);
}
bool empty() {
return head == tail;
}
bool full() {
return (tail + 1) % N == head;
}
T front() {
return data[head];
}
void push(T x) {
assert(!full);
data[tail++] = x;
tail %= N;
}
void pop() {
assert(!empty);
head = (head + 1) % N;
}
void clear() {
head = tail = 0;
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
long[10^^5*2] AS;
long[10^^5*2][5] MEMO;
void main()
{
auto L = readln.chomp.to!int;
foreach (i; 0..L) {
AS[i] = readln.chomp.to!long;
}
foreach (ref m; MEMO) m[] = -1;
long solve(int i, int t) {
if (i == L) return 0;
if (MEMO[t][i] != -1) return MEMO[t][i];
return MEMO[t][i] = min(
t > 0 ? long.max : AS[i] + solve(i+1, 0),
t > 1 ? long.max : (AS[i] == 0 ? 2 : AS[i] % 2) + solve(i+1, 1),
t > 2 ? long.max : (AS[i] == 0 ? 1 : (AS[i]+1) % 2) + solve(i+1, 2),
t > 3 ? long.max : (AS[i] == 0 ? 2 : AS[i] % 2) + solve(i+1, 3),
AS[i] + solve(i+1, 4)
);
}
writeln(solve(0, 0));
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
struct UnionFind
{
void init(int n) { par = new int[](n); foreach (i; 0..n) par[i] = i; cnt = new int[](n); fill(cnt, 1); }
int root(int i) { return par[i] == i ? i : (par[i] = root(par[i])); }
bool same(int i, int j) { return root(i) == root(j); }
void unite(int i, int j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; cnt[j] += cnt[i]; }
int size(int i) { return cnt[root(i)]; }
int[] par, cnt;
}
void main()
{
auto n = RD!int;
UnionFind uf;
uf.init(26);
auto used = new bool[](26);
foreach (i; 0..n)
{
auto s = RD!string;
foreach (j; 0..s.length)
{
auto u = s[j]-'a';
used[u] = true;
foreach (k; j+1..s.length)
{
auto v = s[k]-'a';
used[v] = true;
uf.unite(u, v);
}
}
}
bool[int] set;
foreach (i; 0..26)
{
if (!used[i]) continue;
auto p = uf.root(i);
set[p] = true;
}
writeln(set.keys.length);
stdout.flush;
debug readln;
}
|
D
|
module cf1167a;
import std.stdio;
import std.string : indexOf, strip;
import std.conv : to;
void main() {
int n;
char[] buf;
readln(buf);
n = buf.strip.to!int;
debug writeln(n);
foreach(i; 0..n) {
int x;
readln(buf);
x = buf.strip.to!int;
readln(buf);
immutable eight = buf.indexOf('8');
debug writeln(eight, " ", x);
if (eight >= 0 && eight + 11 <= x) {
writeln("YES");
}else {
writeln("NO");
}
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.typecons;
int cvt (dchar c) {
return (c.to!int - 97);
}
void main() {
immutable n = readln.strip.to!int;
string s = readln.strip;
auto idx = new int[][26];
foreach (i; 0 .. n) {
int o = cvt (s[i]);
idx[o] ~= i + 1;
}
immutable m = readln.strip.to!int;
debug stderr.writeln (m);
foreach (qid; 0 .. m) {
string t = readln.strip;
int[26] c;
foreach (ch; t) {
++c[cvt (ch)];
}
int res;
foreach (i; 0 .. 26) {
if (c[i] > 0) {
res = max (res, idx[i][c[i]-1]);
}
}
writeln (res);
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void main()
{
auto t = RD!int;
auto ans = new string[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto digit = n / 2;
//if (digit <= 9)
{
string str;
if (n % 2 == 0)
str ~= "1";
else
str ~= "7";
foreach (i; 1..digit)
str ~= "1";
ans[ti] = str;
}
/*else
{
auto arr = [9, 9, 8, 2, 4, 4, 3, 5, 3];
auto cnt = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6];
auto remain = n - 18;
bool isLimit = true;
foreach (i; 0..9)
{
auto lim = isLimit ? arr[i] : 9;
long num;
foreach_reverse (j; 0..lim+1)
{
if (remain >= cnt[j] - 2)
{
num = j;
remain -= cnt[j] - 2;
break;
}
}
if (num < lim)
isLimit = false;
ans[ti] += num * 10^^(8-i);
}
}*/
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto n = RD!int;
auto k = RD!int;
auto p = RDA!int;
long ans0;
int[] pos;
foreach (i; 0..n)
{
if (p[i] > n-k)
{
pos ~= i;
ans0 += p[i];
}
}
long ans = 1;
foreach (i; 1..pos.length)
{
auto d = pos[i] - pos[i-1];
ans.modm(d);
}
writeln(ans0, " ", ans);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto M = s[1];
auto Q = s[2];
auto S = readln.chomp;
auto T = readln.chomp;
auto A = new int[](N+1);
foreach (i; 0..N-M+1) {
bool ok = true;
foreach (j; i..i+M) {
if (S[j] != T[j-i]) {
ok = false;
break;
}
}
if (ok) A[i+1] += 1;
}
foreach (i; 0..N) A[i+1] += A[i];
while (Q--) {
s = readln.split.map!(to!int);
int l = s[0] - 1;
int r = s[1] - M;
if (l > r)
writeln(0);
else
writeln(A[r+1] - A[l]);
}
}
|
D
|
import std.stdio, std.conv, std.string;
import std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
int DEBUG_LEVEL = 0;
void print()(){ writeln(""); }
void print(T, A ...)(T t, lazy A a){ write(t), print(a); }
void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); }
const long mod = 1_000_000_007;
void main(string[] args){
if(args.length > 1 && args[1] == "-debug"){
if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int;
else DEBUG_LEVEL = 1;
}
int n = read.to!int;
char[] cs = readln.chomp.to!(char[]);
if(n % 2 > 0){ // this matters only for n = 1
writeln(":(");
return;
}
if(cs[0] == ')' || cs[$ - 1] == '('){
writeln(":(");
return;
}
auto cntleft = cs.count!(x => x == '(');
auto cntright = cs.count!(x => x == ')');
if( cntleft > n / 2 || cntright > n / 2){
writeln(":(");
return;
}
auto lackleft = n / 2 - cntleft;
auto lackright = n / 2 - cntright;
foreach(i; 0 .. n){
if(cs[i] == '?') lackleft -= 1, cs[i] = '(';
if(lackleft == 0) break;
}
foreach_reverse(i; 0 .. n){
if(cs[i] == '?') lackright -= 1, cs[i] = ')';
if(lackright == 0) break;
}
int depth = 0;
foreach(i; 0 .. n){
if(cs[i] == '(') depth += 1;
else depth -= 1;
if(i < n - 1 && depth == 0){
writeln(":(");
return;
}
}
cs.to!string.writeln;
}
/*
- Fail if n is odd
- Fail if the leftmost is )
- Fail if the rightmost is (
- Fail if there are too many (s
- Fail if there are too many )s
~ Otherwise, try this:
- Fill with ( from the left end until (s are enough
- Fill with ) from the right end until )s are enough
- If the result is valid, it is valid
- Otherwise, it is failure
*/
|
D
|
import std.stdio;
void main()
{
printf("25\n");
}
|
D
|
import std.stdio;
import std.uni;
import std.math;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
void main(string[] args)
{
inputFile = stdin;
debug inputFile = File(args[1]);
auto nt = next!int;
foreach(t; 0 .. nt)
{
auto a = next!long;
auto b = next!long;
auto x = next!long;
auto y = next!long;
auto n = next!long;
auto opsa = a - x;
auto opsb = b - y;
if (opsa + opsb <= n)
{
writeln((a - opsa) * (b - opsb));
}
else
{
opsa = min(opsa, n);
opsb = min(opsb, n);
// oa * b + ob * (a - oa)
// ob * a + oa * (b - ob)
auto ifa = opsa * b + min(max(n - opsa, 0), opsb) * (a - opsa);
auto ifb = opsb * a + min(max(n - opsb, 0), opsa) * (b - opsb);
writeln(a * b - max(ifa, ifb));
}
}
}
// INPUT
enum InputStyle
{
byChunk, byLine
};
enum inputStyle = InputStyle.byLine;
File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
{
const chunkSize = 1024 * 1024 * 8;
const wordSize = 4096;
char[chunkSize] chunkBuff;
char[] currChunk;
void renewChunk()
{
if (inputFile.eof)
currChunk = null;
else
currChunk = inputFile.rawRead(chunkBuff);
}
char[wordSize] wordBuff;
char[] word;
string popWord()
{
if (currChunk.length == 0)
renewChunk;
assert(currChunk.length > 0);
while (currChunk[0].isWhite)
{
currChunk = currChunk[1 .. $];
if (currChunk.length == 0)
renewChunk;
}
word = wordBuff[0 .. 0];
while (!currChunk[0].isWhite)
{
word.length++;
word[$ - 1] = currChunk[0];
currChunk = currChunk[1 .. $];
if (currChunk.length == 0)
{
renewChunk;
if (currChunk.length == 0)
return cast(string) word;
}
}
return cast(string) word;
}
}
else
{
DList!string _words;
string popWord()
{
while (_words.empty)
{
foreach(w; inputFile.readln.split)
{
_words.insertBack(w);
}
}
auto word = _words.front;
_words.removeFront;
return word;
}
}
T next(T)()
{
return to!T(popWord);
}
auto next(T, S...)(S s)
{
static if (S.length == 0)
{
return to!T(popWord);
}
else
{
auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
foreach(ref elem; res)
elem = next!T(s[1 .. $]);
return res;
}
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
void main(){
int n; rd(n);
auto s=readln.chomp.to!(char[]);
if(n==1){
writeln("Yes");
return;
}
bool[char] set;
foreach(c; s){
if(c in set){
writeln("Yes");
return;
}
set[c]=true;
}
writeln("No");
}
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, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
int n;
string s;
scan(n);
scan(s);
s = "0" ~ s ~ "0";
auto dp = new int[](n + 1);
foreach (i ; 3 .. n + 1) {
dp[i] = max(dp[i], dp[i - 1]);
if (s[i - 2] == '1' && s[i - 1] == '0' && s[i] == '1') {
foreach_reverse (j ; 0 .. i - 1) {
if (s[j] == '1') {
dp[i] = max(dp[i], dp[j - 1] + i - j - 1);
}
else {
break;
}
}
foreach (j ; i .. n + 2) {
if (s[j] == '1') {
dp[j] = max(dp[j], dp[i - 3] + j - i + 1);
}
else {
break;
}
}
}
}
debug {
writeln(dp);
}
writeln(dp[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.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
long[10^^5][4] CS;
int P = 10^^9+7;
void main()
{
auto S = readln.chomp.to!(wchar[]);
auto len = S.length;
foreach_reverse(i, c; S) {
if (i == len-1) {
switch (c) {
case 'C':
CS[2][len-1] = 1;
CS[3][len-1] = 1;
break;
case '?':
CS[2][len-1] = 1;
CS[3][len-1] = 3;
break;
default:
CS[3][len-1] = 1;
}
} else {
foreach (j; 0..4) {
CS[j][i] = CS[j][i+1];
}
switch (c) {
case 'A':
CS[0][i] = (CS[0][i+1] + CS[1][i+1]) % P;
break;
case 'B':
CS[1][i] = (CS[1][i+1] + CS[2][i+1]) % P;
break;
case 'C':
CS[2][i] = (CS[2][i+1] + CS[3][i+1]) % P;
break;
default:
CS[0][i] = (CS[0][i+1] * 3 + CS[1][i+1]) % P;
CS[1][i] = (CS[1][i+1] * 3 + CS[2][i+1]) % P;
CS[2][i] = (CS[2][i+1] * 3 + CS[3][i+1]) % P;
CS[3][i] = (CS[3][i] * 3) % P;
}
}
}
writeln(CS[0][0]);
}
|
D
|
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
void main() {
try {
for (; ; ) {
const N = readInt();
const D = readInt();
const E = readInt();
int ans = N;
for (int a = 0; D * a <= N; ++a) {
const n = N - D * a;
chmin(ans, n % (E * 5));
}
writeln(ans);
}
} catch (EOFException e) {
}
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;
void main() {
auto N = readln.chomp.to!int;
auto A = readln.split.map!(to!int).array;
auto C = new int[](101010);
foreach (i; 0..N)
C[A[i]] += 1;
int ans = 0;
foreach (i; 0..100100) {
if (i == 0)
ans = max(ans, C[0] + C[1]);
else
ans = max(ans, C[i-1] + C[i] + C[i+1]);
}
ans.writeln;
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
const N = readln.chomp.to!long;
writeln(N < 1000 ? "ABC" : "ABD");
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
void main() {
auto s = readln.stripRight[0 .. 3];
writeln ((s.count ('A') < 3 && s.count('B') < 3) ? "Yes" : "No");
}
|
D
|
module app;
import core.bitop;
import std.algorithm;
import std.array;
import std.bigint;
import std.container.rbtree;
import std.conv;
import std.stdio;
import std.string;
import std.traits;
struct Input
{
long h, w;
}
void parseInput(T)(out Input input, T file)
{
with (file) with (input)
{
auto ar = readln().strip().split().map!(to!long).array();
h = ar[0];
w = ar[1];
}
}
auto main2(Input* input)
{
with (input)
{
if (h == 1 || w == 1)
return 1;
long temp = (h / 2) * w;
if (h % 2 == 1)
temp += (w + 1) / 2;
return temp;
}
}
alias retType = ReturnType!main2;
static if (!is(retType == void))
{
unittest { writeln("begin unittest"); }
auto _placeholder_ = ReturnType!main2.init;
unittest // example1
{
string example =
`4 5`;
if (example.empty) return;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
printResult(result);
assert(result == 10);
}
unittest // example2
{
string example =
`7 3`;
if (example.empty) return;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
printResult(result);
assert(result == 11);
}
unittest // example3
{
string example =
`1000000000 1000000000`;
if (example.empty) return;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
printResult(result);
assert(result == 500000000000000000);
}
unittest { writeln("end unittest"); }
void parseExample(out Input input, string example)
{
struct Adapter
{
string[] _lines;
this(string input) { _lines = input.splitLines(); }
string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; }
}
parseInput(input, Adapter(example));
}
}
void printResult(T)(T result)
{
static if (isFloatingPoint!T) writefln("%f", result);
else writeln(result);
}
void main()
{
Input input = void;
parseInput(input, stdin);
static if (is(retType == void))
main2(&input);
else
{
auto result = main2(&input);
printResult(result);
}
}
|
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 t = readln.chomp.split.to!(int[]);
auto m = readln.chomp.to!int;
auto lsum = t.sum;
foreach (i; 0..m) {
auto x = readln.chomp.split.to!(int[]);
writeln(lsum - t[x[0]-1] + x[1]);
}
}
|
D
|
import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range;
import std.bigint;
class Node{
Edge[] edges;
int id;
int flag;
this(int i){
this.id = i;
}
void add(Edge e){
edges ~= e;
}
}
class Edge{
Node[] nodes;
this(Node n1, Node n2){
this.nodes = [n1, n2];
n1.add(this);
n2.add(this);
}
}
void main(){
int[] ins = readln.chomp.split.map!(to!int).array;
int n = ins[0], k = ins[1];
Edge[] edges;
Node[] nodes;
foreach(i; 0 .. n) nodes ~= new Node(i);
for(int i = 0; i < n - 1; i ++){
int[] ab = readln.chomp.split.map!(to!int).map!(a => a - 1).array;
edges ~= new Edge(nodes[ab[0]], nodes[ab[1]]);
}
int ans;
int getCnt(Node[] ns0, int r){ // count of nodes within radius r
foreach(n2; nodes) n2.flag = 0;
int cnt;
foreach(n0; ns0) n0.flag = 1;
Node[] ns1 = ns0;
for(int d = 0; d <= r; d ++){
cnt += ns1.length;
Node[] ns2;
foreach(n1; ns1){
foreach(e; n1.edges){
foreach(n2; e.nodes){
if(!n2.flag) n2.flag = 1, ns2 ~= n2;
}
}
}
ns1 = ns2;
}
return cnt;
}
if(k % 2 == 0){
int max = 0;
int r = k / 2;
foreach(n0; nodes){ // center node
int cnt = getCnt([n0], r);
if(cnt > max) max = cnt;
}
ans = n - max;
}
else{ // center edge
int max = 0;
int r = k / 2;
foreach(e0; edges){
int cnt = getCnt(e0.nodes, r);
if(cnt > max) max = cnt;
}
ans = n - max;
}
ans.writeln;
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto n = 3;
auto s = new string[](n);
foreach (i; 0..n) s[i] = readln.chomp;
auto turn = 0;
for (;;) {
if (s[turn].empty) {
writeln(cast(char)('A' + turn));
return;
}
auto next = cast(int)(s[turn][0] - 'a');
s[turn] = s[turn][1..$];
turn = next;
}
}
|
D
|
import std.stdio;
import std.string;
import std.algorithm;
import std.conv;
import std.math;
void main()
{
int A, B, C;
auto input = readln.split.to!(int[]);
A = input[0];
B = input[1];
C = input[2];
if(A > B){
writeln(0);
}else if(B/A >C){
writeln(C);
}else{
writeln(B/A);
}
}
|
D
|
import std.stdio,std.string,std.conv;
void main(){
foreach(i;1..int.max){
int a = to!int(readln().chomp());
if( a==0 ){ break; }
writeln("Case ",i,": ",a);
}
}
|
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() {
readint;
auto xs = readints;
int min = xs.reduce!((a, b) => min(a, b));
int max = xs.reduce!((a, b) => max(a, b));
writeln(max - min);
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.