code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container;
void main()
{
auto N = readln.chomp.to!int;
auto YS = new long[](N+1);
YS[] = 1;
YS[0] = 0;
foreach (i; 2..N+1) {
long k = 1;
while (i*k <= N) {
YS[i*k] += 1;
k += 1;
}
}
long r;
foreach (i; 1..N+1) r += YS[i] * i;
writeln(r);
}
|
D
|
import core.stdc.stdio;
import std.math;
import std.algorithm;
import std.typecons;
import std.stdio;
void main(){
int n;
scanf("%d",&n);
long[] x=new long[n],y=new long[n];
foreach(i;0..n)
scanf("%lld%lld",&x[i],&y[i]);
alias Tuple!(long,"x",long,"y",int,"k") p;
p[] ps = new p[n-1];
foreach(i;0..n){
int c;
int u1;
int u2;
foreach(j;0..n)
if(j<i){
if(y[j]>=y[i]){
ps[c++]=p(x[j]-x[i],y[j]-y[i],0);
u1++;
}else
ps[c++]=p(x[i]-x[j],y[i]-y[j],1);
}else if(j>i){
if(y[j]>=y[i]){
ps[c++]=p(x[j]-x[i],y[j]-y[i],2);
u2++;
}else
ps[c++]=p(x[i]-x[j],y[i]-y[j],3);
}
sort!((a,b)=>a.x*b.y>a.y*b.x)(ps);
if((u1==0||u1==i)&&(u2==0||u2==(n-i-1)))
continue;
bool ok=false;
foreach(v;ps){
if(v.k==0)
u1--;
else if(v.k==1)
u1++;
else if(v.k==2)
u2--;
else if(v.k==3)
u2++;
else
assert(false);
if((u1==0||u1==i)&&(u2==0||u2==(n-i-1))){
ok=true;
break;
}
}
if(!ok){
printf("Impossible\n");
return;
}
}
printf("Possible\n");
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int q;
rd(q);
bool enough(long k, long n, long a, long b, long m) {
return a * m + b * (n - m) < k;
}
while (q--) {
long k, n, a, b;
rd(k, n, a, b);
if (b * n >= k) {
writeln(-1);
continue;
}
long ok = 0, ng = n + 1;
while (ng - ok > 1) {
auto m = (ok + ng) / 2;
if (enough(k, n, a, b, m)) {
ok = m;
} else {
ng = m;
}
}
writeln(ok);
}
}
void rd(T...)(ref T x) {
import std.stdio : readln;
import std.string : split;
import std.conv : to;
auto l = readln.split;
assert(l.length == x.length);
foreach (i, ref e; x)
e = l[i].to!(typeof(e));
}
|
D
|
import std.stdio, std.string, std.array, std.conv;
void main() {
int[] a = readln.chomp.split.to!(int[]);
writeln(a[0] * a[1], " ", 2 * (a[0] + a[1]));
}
|
D
|
void main() {
int[] tmp = readln.split.to!(int[]);
int a = tmp[0], b = tmp[1], c = tmp[2];
writeln(b - a == c - b ? "YES" : "NO");
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string;
void main()
{
auto S = readln.chomp.to!(dchar[]);
auto N = S.length;
size_t i = 0, j = S.length - 1;
auto a = S[0], b = S[0], c = S[$-1], d = S[$-1];
int x, y;
foreach (s; S) {
++(s == '1' ? x : y);
}
if (!x || !y) {
writeln(N);
return;
}
while (i <= j) {
if (i + 1 < N - j) {
a = S[i];
while (a == b) b = S[++i];
} else {
d = S[j];
while (c == d) c = S[--j];
}
}
writeln(i > N - j - 1 ? i : N - j - 1);
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
auto S = sread();
auto C = new uint[](N);
foreach (i; 0 .. N)
{
C[i] = (1 << (S[i] - 'a'));
}
long ans = -1;
foreach (i; 1 .. N)
{
uint a = (C[0 .. i + 1] ~ 0).reduce!"a|b"();
uint b = (C[i + 1 .. $] ~ 0).reduce!"a|b"();
// writefln("%030b %030b %030b", a, b, a & b);
ans = ans.max(popcnt(a & b));
}
writeln(ans);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
auto as = readln.split.to!(long[]);
long r;
for (;;) {
bool flg;
foreach (i, ref a; as) {
if (a >= N) {
flg = true;
auto n = a / N;
r += n;
a = a % N;
foreach (j, ref aa; as) {
if (i == j) continue;
aa += n;
}
}
}
if (!flg) break;
}
writeln(r);
}
|
D
|
void main()
{
long n = rdElem;
long[] a = 5.rdCol;
long amin = a.reduce!min;
long m = (n + amin - 1) / amin;
writeln(m + 4);
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
void main()
{
long n, c;
rdVals(n, c);
long limit = 2 * 10 ^^ 5 + 1;
long[][] lists = new long[][](c, limit);
foreach (i; 0 .. n)
{
long s, t, d;
rdVals(s, t, d);
s = 2 * s - 1, t = 2 * t, --d;
++lists[d][s], --lists[d][t];
}
long[] record = new long[limit];
long result;
foreach (i; 0 .. c)
{
foreach (j; 1 .. limit)
{
lists[i][j] += lists[i][j-1];
if (lists[i][j])
{
++record[j];
result = max(result, record[j]);
}
}
}
result.writeln;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "damage", long, "cost");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
auto n = lread();
auto h = aryread();
auto dp = new long[](n + 5);
dp[] = INF;
dp[0] = 0;
foreach (i; iota(n))
{
if (i + 1 < n)
dp[i + 1] = min(dp[i] + abs(h[i + 1] - h[i]), dp[i + 1]);
if (i + 2 < n)
dp[i + 2] = min(dp[i] + abs(h[i + 2] - h[i]), dp[i + 2]);
}
dp[n - 1].writeln();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;
void main() {
auto N = readln.chomp.to!int;
auto A = readln.split.map!(to!long).array;
if (A[$-1] != 2) {
writeln(-1);
return;
}
auto B = new Tuple!(long, long)[](N);
B[$-1] = tuple(2L, 3L);
for (int i = N-2; i >= 0; --i) {
long l = B[i+1][0];
long r = B[i+1][1];
long nl = l % A[i] == 0 ? l : (l / A[i] + 1) * A[i];
long nr = r % A[i] == 0 ? r : (r / A[i]) * A[i];
nr += A[i]-1;
if (nl > r || nr < l) {
writeln(-1);
return;
}
B[i] = tuple(nl, nr);
}
writeln(B[0][0], " ", B[0][1]);
}
|
D
|
void main()
{
long n = readln.chomp.to!long;
long[] a = readln.split.to!(long[]);
long[] b = new long[n+2];
foreach (i; 1 .. n+1)
{
b[i] = a[i-1];
}
long total;
foreach (i; 1 .. n+2)
{
total += abs(b[i] - b[i-1]);
}
foreach (i; 1 .. n+1)
{
writeln(total - abs(b[i]-b[i-1]) - abs(b[i+1]-b[i]) + abs(b[i+1]-b[i-1]));
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm, std.array, std.range, std.container;
import std.math;
void main() {
auto input = readln.split.to!(int[]);
auto N = input[0], X = input[1], Y = input[2];
int[] ans; ans.length = N+1;
foreach (k; 1 .. N+1) {
ans[k] = N-k;
}
//writeln(ans);
// i <= X and Y <= j
foreach (k; Y-X .. N+1) {
int diff1;
if (Y-k <= 0)
diff1 = min(X, N-k);
else if (X+k >= N+1)
diff1 = N - max(1, Y) + 1;
else
diff1 = (X+k) - Y + 1;
ans[k] -= diff1;
ans[k - (Y-X-1)] += diff1;
}
//writeln(ans);
// X < i < Y <= j
foreach (i; X+1 .. (X+Y)/2) {
foreach (j; Y .. N+1) {
//writeln(i, " ", j, 1 + i-X + j-Y < j-i);
if (1 + i-X + j-Y < j-i) ans[j-i]--, ans[1 + i-X + j-Y]++;
}
}
// i <= X < j < Y
foreach (j; (X+Y)/2+1 .. Y) {
foreach (i; 1 .. X+1) {
//writeln(i, " ", j, 1 + X-i + Y-j < j-i);
if (1 + X-i + Y-j < j-i) ans[j-i]--, ans[1 + X-i + Y-j]++;
}
}
// X < i < j < Y
foreach (i; X+1 .. Y-1) foreach (j; X+2 .. Y) {
//writeln(i, " ", j, 2*(j-i) >= Y-X+1);
if (2*(j-i) >= Y-X+1) { ans[j-i]--; ans[Y-j + i-X + 1]++; }
}
foreach (k; 1 .. N) {
writeln(ans[k]);
}
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
static import std.ascii;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
auto P = aryread();
auto Q = aryread();
auto I = iota(1, N + 1).array;
long a, b;
long i;
do
{
if (P == I)
a = i;
if (Q == I)
b = i;
i++;
}
while (nextPermutation(I));
writeln(abs(a - b));
}
|
D
|
void main()
{
long x = rdElem;
long limit = 200000;
auto sieve = new Prime(limit);
foreach (i; x .. limit)
{
if (sieve.isPrime(i))
{
i.writeln;
return;
}
}
}
struct Prime
{
bool[] flags;
this(long limit)
{
flags = makeSieve(limit);
}
bool[] makeSieve(long limit)
{
long len = (limit - 1) / 2;
bool[] result = new bool[len];
result[] = true;
foreach (i; 0 .. len)
{
if (!result[i]) continue;
long prime = 3 + 2 * i;
foreach (j; iota(prime+i, len, prime))
{
result[j] = false;
}
}
return result;
}
bool isPrime(long number)
in
{
assert((number - 1) / 2 <= flags.length);
}
body
{
if (number < 2)
return false;
else if (number == 2)
return true;
else if (number % 2 == 0)
return false;
else
return flags[(number-3)/2];
}
}
T rdElem(T = long)()
{
//import std.stdio : readln;
//import std.string : chomp;
//import std.conv : to;
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
dchar[] rdDchar()
{
//import std.conv : to;
return rdStr.to!(dchar[]);
}
T[] rdRow(T = long)()
{
//import std.stdio : readln;
//import std.array : split;
//import std.conv : to;
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
//import std.range : iota;
//import std.algorithm : map;
//import std.array : array;
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
//import std.range : iota;
//import std.algorithm : map;
//import std.array : array;
return iota(col).map!(x => rdRow!T).array;
}
void wrMat(T = long)(T[][] mat)
{
//import std.stdio : write, writeln;
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto ab = readln.split.to!(int[]);
auto A = ab[0];
auto B = ab[1];
if (A >= 13) {
writeln(B);
} else if (A >= 6) {
writeln(B/2);
} else {
writeln(0);
}
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
static import std.ascii;
// 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 H = aryread();
long i, ans;
while (i < N)
{
long cnt;
while (i + cnt + 1 < N && H[i + cnt + 1] <= H[i + cnt])
cnt++;
ans = ans.max(cnt);
i = i + max(1, cnt);
}
writeln(ans);
}
|
D
|
unittest
{
assert( [ "AtCoder Beginner Contest" ].parse.expand.solve == "ABC" );
assert( [ "AtCoder Snuke Contest" ].parse.expand.solve == "ASC" );
assert( [ "AtCoder X Contest" ].parse.expand.solve == "AXC" );
}
import std.conv;
import std.range;
import std.stdio;
import std.typecons;
void main()
{
stdin.byLineCopy.parse.expand.solve.writeln;
}
auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
auto s = input.front;
return tuple( s );
}
auto solve( string s )
{
return "A" ~ s[ 8 ] ~ "C";
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new long[][](t, 2);
foreach (ti; 0..t)
{
auto a = RD;
auto b = RD;
auto c = RD;
if (a < c)
ans[ti][0] = 1;
else
ans[ti][0] = -1;
if (a*b > c)
ans[ti][1] = b;
else
ans[ti][1] = -1;
}
foreach (e; ans)
{
writeln(e[0], " ", e[1]);
}
stdout.flush;
debug readln;
}
|
D
|
module main;
import core.stdc.stdio;
int main(string[] arv) {
int n;
scanf("%d", &n);
char [] s = new char[n];
for(int i = 0; i < n; i++) scanf(" %c", &s[i]);
for(int i = 0; i < n; i++) {
if(s[i] != 'a' && s[i] != 'e' && s[i] != 'i' && s[i] != 'o' && s[i] != 'u' && s[i] != 'y')
printf("%c", s[i]);
else {
int count = 1;
for(int j = i + 1; j < n && s[i] == s[j]; j++) count++;
if((s[i] == 'e' || s[i] == 'o') && count == 2)
printf("%c%c", s[i], s[i]);
else
printf("%c", s[i]);
while(s[i] == s[i+1]) i++;
}
}
printf("\n");
return 0;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
void main(){
int n = readln.chomp.to!int;
int[] a = readln().split().map!(to!int).array;
int[] x = [0,0,0];
foreach(i; 0..n-1){
if(a[i] == a[i+1]){
x ~= i+1;
}
}
x ~= n;
int ans = 0;
foreach(i; 0..x.length-3){
ans = max(ans, x[i+3] - x[i]);
}
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.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto N = RD;
auto A = RDA;
int[long] cnt;
foreach (i; 0..N)
{
++cnt[A[i]];
}
long ans;
foreach (i; 0..N)
{
bool ok = true;
for (long j = 1; j*j <= A[i]; ++j)
{
if (A[i] % j) continue;
auto k = A[i] / j;
if (j == A[i])
{
if (cnt.get(j, 0) >= 2)
{
ok = false;
break;
}
}
else
{
if (cnt.get(j, 0))
{
ok = false;
break;
}
}
if (k == A[i])
{
if (cnt.get(k, 0) >= 2)
{
ok = false;
break;
}
}
else
{
if (cnt.get(k, 0))
{
ok = false;
break;
}
}
}
if (ok)
++ans;
}
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.file;
void main(string[] args)
{
auto s = map!(to!int)(readln.strip.split);
auto d = s[1] * 2;
int n = 1;
while(n <= s[0])
{
auto des = d * n;
if (des >= s[2])
break;
n++;
}
auto p = (n -1) * s[1] * 2;
if (s[2] % 2 == 0)
{
auto m = (s[2] - p) / 2;
writeln(n, " ", m, " R");
}
else
{
auto m = ((s[2] + 1) - p) / 2;
writeln(n, " ", m, " L");
}
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;
import std.random;
void main() {
auto a = readln.chomp.split.map!(to!int);
auto w = a[0];
auto h = a[1];
auto x = a[2];
auto y = a[3];
auto r = a[4];
if (x-r>=0 && x+r<=w && y-r>=0 && y+r<=h) writeln("Yes");
else writeln("No");
}
|
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 x = readln.chomp.split.to!(int[]);
auto y = readln.chomp.split.to!(int[]);
int a, b;
foreach (i; 0..y.length) {
if (y[i] < x[2]) {
a++;
} else {
b++;
}
}
min(a,b).writeln;
}
|
D
|
void main() {
int[] tmp = readln.split.to!(int[]);
int n = tmp[0], m = tmp[1];
int[] a = new int[m+1];
foreach (i; 0 .. n) {
int[] survey = readln.split.to!(int[]);
foreach (x; survey[1..$]) {
++a[x];
}
}
a.count(n).writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.container;
import std.typecons;
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto t = RD!int;
auto ans = new long[](t);
foreach (i; 0..t)
{
auto n = RD;
long x = 10;
while (n >= x)
{
ans[i] += 9;
x *= 10;
}
auto y = n / (x / 10);
ans[i] += y-1;
while (y / 10 != 0)
y /= 10;
auto z = y;
debug writeln("z:", z);
while (z <= n)
{
y *= 10;
z += y;
}
long cnt1;
while (z != 0)
{
z /= 10;
++cnt1;
}
long cnt2;
while (n != 0)
{
n /= 10;
++cnt2;
}
debug writeln(cnt1, ":", cnt2);
if (cnt1 > cnt2)
++ans[i];
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
static import std.ascii;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N, M;
scan(N, M);
writeln((N == M) ? "Yes" : "No");
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;
void main() {
auto N = readln.chomp.to!int;
auto S = readln.chomp;
auto T = S.map!(s => to!int(s - '1')).array;
auto P = 9.iota.array;
auto Q = [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]];
auto mp = new int[][](9, 9);
foreach (i; 0..N-1) mp[T[i]][T[i+1]] += 1;
int ansv = 1 << 29;
int[] ans;
int[] trans(int[] a) {
auto b = new int[](9);
foreach (i; 0..9) b[a[i]] = i;
return b;
}
do {
int tmp = 0;
foreach (i; 0..9) {
foreach (j; 0..9) {
int dist = abs(Q[P[i]][0] - Q[P[j]][0]) + abs(Q[P[i]][1] - Q[P[j]][1]);
tmp += dist * mp[i][j];
}
}
if (tmp < ansv) {
ansv = tmp;
ans = trans(P);
} else if (tmp == ansv) {
auto b = trans(P);
if (b < ans) ans = b.dup;
}
} while (nextPermutation(P));
foreach (i; 0..3) {
foreach (j; 0..3) {
write(ans[i*3+j]+1);
}
writeln;
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons;
import std.math, std.numeric;
void main() {
int n; scan(n);
char[][] ban = new char[][](n, n);
iota(n).each!(i => ban[i] = readln.chomp.to!(char[]));
long ans;
foreach (i ; 0 .. n) {
if (isSym(n, ban, 0, i)) {
ans += n;
}
}
writeln(ans);
}
bool isSym(int n, char[][] ban, int a, int b) {
auto cop = new char[][](n, n);
foreach (i ; 0 .. n) {
foreach (j ; 0 .. n) {
cop[i][j] = ban[(i + a) % n][(j + b) % n];
}
}
debug {
writefln("(a, b) = (%d, %d)", a, b);
writefln("%-(%s\n%)", cop);
}
foreach (i ; 0 .. n) {
foreach (j ; 0 .. n) {
if (cop[i][j] != cop[j][i]) {
return false;
}
}
}
return 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, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n; readV(n);
int[] a; readA(n, a);
auto c = new int[](9);
foreach (ai; a) {
if (ai >= 3200) c[8]++;
else c[ai/400]++;
}
auto b = c[0..8].count!"a>0";
write(b == 0 ? 1 : b, " ", b+c[8]);
}
|
D
|
void main()
{
auto io = new IO();
auto input = io.str();
writeln(input[0..4]," ",input[4..$]);
}
import std.stdio,std.string,std.conv;
class IO
{
string str( size_t lines = 1 )
{
return readln().chomp();
}
T[] line( T = real , string sp = " " )( size_t lines = 1 )
{
T[] ret;
foreach( i ; 0..lines )
ret ~= readln().chomp().split(sp).convert!T();
return ret;
}
T[][] rect( T = real , string sp = " " )( size_t lines = 1 )
{
T[][] ret = new T[][](lines);
foreach( i ; 0..lines )
ret[i] = readln().chomp().split(sp).convert!T();
return ret;
}
}
R[] convert( R , T )( T[] args ) pure
{
R[] ret = new R[](args.length);
foreach( i ; 0..args.length )
ret[i] = args[i].to!R();
return ret;
}
T sum( T )( in T[] args ) pure nothrow
{
T ret = 0;
foreach( elm ; args )
ret += elm;
return ret;
}
|
D
|
import std.stdio, std.string, std.algorithm, std.range;
void main() {
auto S = readln.chomp;
auto s = 0, e = S.length-1;
while (S[s] != 'A') {
s++;
}
while (S[e] != 'Z') {
e--;
}
writeln(e - s + 1);
}
|
D
|
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.complex;
import std.container;
import std.conv;
import std.datetime;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
auto readInts() {
return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
return readInts()[0];
}
auto readLongs() {
return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
return readLongs()[0];
}
const real eps = 1e-10;
auto solve(){
}
string decode(string s) {
if(s[0] < '1' || s[0] > '6' || s[1] < '1' || s[1] > '5') {
return "NA";
}
auto c = (s[0] - '1') * 5 + s[1] - '1';
if(c < 26) {
return "" ~ cast(char)(c + 'a');
}
auto m = ['.', '?', '!', ' '];
return "" ~ m[c-26];
}
void main(){
string s;
loop:
while((s = readln()).length > 0) {
s = s.strip();
if(s.length % 2 != 0) {
writeln("NA");
continue;
}
string t;
foreach(i; 0..s.length/2) {
auto u = decode(s[2*i..2*i+2]);
if(u == "NA") {
writeln("NA");
continue loop;
}
t ~= u;
}
writeln(t);
}
}
|
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 t;
scan(t);
while (t--) {
long ai, bi, ci, di;
scan(ai, bi, ci, di);
writeln(solve(ai, bi, ci, di) ? "Yes" : "No");
}
}
bool solve(long a, long b, long c, long d) {
if (a < b) {
return false;
}
if (b > d) {
return false;
}
if (c >= b) {
return true;
}
// a >= b, d >= b, b > c
auto g = gcd(b, d);
auto f = b - g + (a % g);
if (f > c) {
return false;
}
else {
return 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.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;
import std.bigint;
void main()
{
foreach (line; stdin.byLine) {
auto eqn = line.chomp.split("=");
BigInt nnum, xnum;
int i;
bool zero;
foreach_reverse (e; eqn[1]) {
if (e == 'X') {
xnum += 10.to!BigInt ^^ i;
if (i == eqn[1].length-1 && i >= 1) {
zero = 1;
}
} else {
nnum -= (e - '0') * 10.to!BigInt ^^ i;
}
i++;
}
foreach (e; eqn[0].split("+")) {
i = 0;
foreach_reverse (ee; e) {
if (ee == 'X') {
xnum -= 10.to!BigInt ^^ i;
if (i == e.length-1 && i >= 1) {
zero = 1;
}
} else {
nnum += (ee - '0') * 10.to!BigInt ^^ i;
}
i++;
}
}
bool f;
foreach (j; zero.to!int..10) {
if (nnum == xnum * j) {
j.writeln;
f = 1;
}
}
if (!f) {
writeln("NA");
}
}
}
|
D
|
import std;
void main() {
int n, k; scan(n, k);
auto s = new int[n];
foreach (_; 0..k) {
read;
foreach (e; readints) s[e-1]++;
}
auto ans = s.count!"a == 0";
writeln(ans);
}
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T=string)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readints = reads!int;
|
D
|
/+ dub.sdl:
name "C"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
int main() {
auto sc = new Scanner(stdin);
int H, W, h, w;
sc.read(H, W, h, w);
int[][] cnt = new int[][](h, w);
foreach (y; 0..H) {
foreach (x; 0..W) {
cnt[y%h][x%w]++;
}
}
if (cnt[0][0] == cnt[h-1][w-1]) {
writeln("No");
return 0;
}
writeln("Yes");
long[][] ans = new long[][](H, W);
foreach (y; 0..H) {
foreach (x; 0..W) {
int y2 = y%h, x2 = x%w;
if (y2 == 0 && x2 == 0) {
ans[y][x] = 10L^^9 - 1;
} else if (y2 == h-1 && x2 == w-1) {
ans[y][x] = -10L^^9;
}
}
}
foreach (y; 0..H) {
writeln(ans[y].map!(to!string).join(" "));
}
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
static if (__VERSION__ <= 2070) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
version (X86) static if (__VERSION__ < 2071) {
import core.bitop : bsf, bsr, popcnt;
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
if (f.eof) return false;
line = lineBuf[];
f.readln(line);
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else {
auto buf = line.split.map!(to!E).array;
static if (isStaticArray!T) {
assert(buf.length == T.length);
}
x = buf;
line.length = 0;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
|
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 bool[](t);
foreach (ti; 0..t)
{
auto n = RD;
auto m = RD;
auto k = RD;
long cnt = n-1;
cnt += n * (m-1);
ans[ti] = cnt == k;
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush;
debug readln;
}
|
D
|
// import chie template :) {{{
import std.stdio,
std.algorithm,
std.array,
std.string,
std.math,
std.conv,
std.range,
std.container,
std.bigint,
std.ascii;
// }}}
// tbh.scanner {{{
class Scanner {
import std.stdio;
import std.conv : to;
import std.array : split;
import std.string : chomp;
private File file;
private dchar[][] str;
private uint idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
private dchar[] next() {
if (idx < str.length) {
return str[idx++];
}
dchar[] s;
while (s.length == 0) {
s = file.readln.chomp.to!(dchar[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(uint len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
}
// }}}
void main() {
auto cin = new Scanner;
int x = cin.next!int;
int a = cin.next!int,
b = cin.next!int;
x -= a;
writeln(x % b);
}
|
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 A = RD;
auto B = RD;
writeln(max(0, A-B*2));
stdout.flush();
debug readln();
}
|
D
|
import std.stdio;
import std.string;
import std.array; // split
import std.conv; // to
void main()
{
string s1 = chomp(readln());
string s2 = chomp(readln());
int a = to!int(s1); // 第0要素を整数に変換
int b = to!int(s2); // 第1要素を整数に変換
a *= a;
writeln(a-b); // 表示
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;
void main()
{
int n = readln.chomp.to!int;
auto cash = readln.split.map!(to!int);
auto time = new int[](n);
foreach (i; 0..n) {
auto inp = readln.split.map!(to!int);
foreach (j; 0..cash[i]) {
time[i] += inp[j] * 5;
time[i] += 15;
}
}
time.reduce!(min).writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
auto AS = readln.split.to!(ulong[]);
ulong x;
size_t i, j;
long r;
while (j < N) {
while (i < N && x&AS[j]) {
x ^= AS[i++];
}
r += j-i+1;
x |= AS[j++];
}
writeln(r);
}
|
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 X = to!ulong(tokens[1]);
ulong[2][] memo;
memo ~= [1, 1];
foreach (i; 0..N+1)
{
memo ~= [memo[i][0] * 2 + 3, memo[i][1] * 2 + 1];
}
ulong f(ulong lv, ref ulong remain)
{
if (remain == 0) return 0;
if (lv == 0) return 1;
ulong r;
--remain;
if (remain < memo[lv-1][0])
r += f(lv-1, remain);
else
{
r += memo[lv-1][1];
remain -= memo[lv-1][0];
}
if (remain == 0) return r;
--remain;
++r;
if (remain == 0) return r;
if (remain < memo[lv-1][0])
r += f(lv-1, remain);
else
{
r += memo[lv-1][1];
remain -= memo[lv-1][0];
}
if (remain == 0) return r;
--remain;
return r;
}
auto result = f(N, X);
writeln(result);
stdout.flush();
/*}catch (Throwable e)
{
writeln(e.toString());
}
readln();*/
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.range;
import std.array;
import std.conv;
import std.complex;
import std.math;
import std.ascii;
import std.bigint;
import std.container;
import std.typecons;
auto readInts() {
return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
return readInts()[0];
}
auto readLongs() {
return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
return readLongs()[0];
}
const real eps = 1e-10;
void main(){
auto n = readInt();
long a = 1;
int ans;
while(a < n) {
a *= 3;
++ans;
}
writeln(ans);
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
import std.typecons;
import std.bigint;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.container.deque;
int main() {
auto sc = new Scanner(stdin);
int n, m, k;
sc.read(n, m, k); m++;
int[] a = new int[n];
a.each!((ref x) => sc.read(x));
long[] dp = a.map!(to!long).array;
long[] ndp = new long[n];
auto deq = Deque!(int, false).make();
foreach (int ph; 2..k+1) {
ndp[] = -(10L^^18);
deq.clear();
foreach (int i; 0..n) {
if (deq.length && deq[0] == i-m) {
deq.removeFront();
}
if (deq.length) {
ndp[i] = dp[deq[0]] + 1L * ph * a[i];
}
while (deq.length && dp[deq.back] <= dp[i]) {
deq.removeBack();
}
deq.insertBack(i);
}
swap(dp, ndp);
}
writeln(dp.fold!max);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
T[N] fixed(T, size_t N)(T[N] a) {return a;}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/deque.d */
// module dcomp.container.deque;
struct DequePayload(T) {
import core.exception : RangeError;
private T* _data;
private uint start, len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
if (start + i < cap) return _data[start + i];
else return _data[start + i - cap];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import std.algorithm : max;
import std.conv : to;
if (newCap <= cap) return;
T* newData = cast(T*)GC.malloc(newCap * T.sizeof);
foreach (i; 0..length) {
newData[i] = this[i];
}
_data = newData; start = 0; cap = newCap.to!uint;
}
void clear() {
start = len = 0;
}
import std.algorithm : max;
void insertFront(T item) {
if (len == cap) reserve(max(cap * 2, 4));
if (start == 0) start += cap;
start--; len++;
this[0] = item;
}
void insertBack(T item) {
if (len == cap) reserve(max(cap * 2, 4));
len++;
this[len-1] = item;
}
void removeFront() {
assert(!empty, "Deque.removeFront: Deque is empty");
start++; len--;
if (start == cap) start = 0;
}
void removeBack() {
assert(!empty, "Deque.removeBack: Deque is empty");
len--;
}
}
struct Deque(T, bool mayNull = true) {
import core.exception : RangeError;
import std.range : ElementType, isInputRange;
import std.traits : isImplicitlyConvertible;
alias Payload = DequePayload!T;
Payload* _p;
static if (!mayNull) @disable this();
this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) {
_p = new Payload();
foreach (v; values) {
insertBack(v);
}
}
this(Range)(Range r)
if (isInputRange!Range &&
isImplicitlyConvertible!(ElementType!Range, T) &&
!is(Range == T[])) {
_p = new Payload();
foreach (v; r) {
insertBack(v);
}
}
private this(Payload* p) { _p = p; }
static Deque make() { return Deque(new Payload()); }
private bool havePayload() const { return (!mayNull || _p); }
@property bool empty() const { return (!havePayload || _p.empty); }
@property size_t length() const { return (havePayload ? _p.length : 0); }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {
assert(!empty, "Deque.opIndex: Deque is empty");
return (*_p)[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void clear() { if (_p) _p.clear(); }
void insertFront(T item) {
if (mayNull && !_p) _p = new Payload();
_p.insertFront(item);
}
void insertBack(T item) {
if (mayNull && !_p) _p = new Payload();
_p.insertBack(item);
}
alias opOpAssign(string op : "~") = insertBack;
alias stableInsertBack = insertBack;
void removeFront() {
assert(!mayNull || _p, "Deque.removeFront: Deque is empty");
_p.removeFront();
}
void removeBack() {
assert(!mayNull || _p, "Deque.removeBack: Deque is empty");
_p.removeBack();
}
alias stableRemoveBack = removeBack;
alias Range = RangeT!(DequePayload!T);
alias ConstRange = RangeT!(const DequePayload!T);
alias ImmutableRange = RangeT!(immutable DequePayload!T);
size_t[2] opSlice(size_t dim : 0)(size_t start, size_t end) const {
assert(start <= end && end <= length);
return [start, end];
}
Range opIndex(size_t[2] rng) { return Range(_p, rng[0], rng[1]); }
ConstRange opIndex(size_t[2] rng) const { return ConstRange(_p, rng[0], rng[1]); }
ImmutableRange opIndex(size_t[2] rng) immutable { return ImmutableRange(_p, rng[0], rng[1]); }
auto opIndex() inout { return this[0..$]; }
static struct RangeT(QualifiedPayload) {
alias A = QualifiedPayload;
import std.traits : CopyTypeQualifiers;
alias E = CopyTypeQualifiers!(A, T);
A *p;
size_t l, r;
@property bool empty() const { return r <= l; }
@property size_t length() const { return r - l; }
alias opDollar = length;
@property auto save() { return this; }
ref inout(E) opIndex(size_t i) inout {
version(assert) if (empty) throw new RangeError();
return (*p)[l+i];
}
@property ref inout(E) front() inout { return this[0]; }
@property ref inout(E) back() inout { return this[$-1]; }
void popFront() {
version(assert) if (empty) throw new RangeError();
l++;
}
void popBack() {
version(assert) if (empty) throw new RangeError();
r--;
}
size_t[2] opSlice(size_t dim : 0)(size_t start, size_t end) const {
assert(start <= end && end <= length);
return [start, end];
}
auto opIndex(size_t[2] rng) { return RangeT(p, l+rng[0], l+rng[1]); }
auto opIndex(size_t[2] rng) const { return RangeT!(const A)(p, l+rng[0], l+rng[1]); }
auto opIndex(size_t[2] rng) immutable { return RangeT!(immutable A)(p, l+rng[0], l+rng[1]); }
auto opIndex() inout { return this[0..$]; }
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
// import dcomp.container.stackpayload;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/stackpayload.d */
// module dcomp.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/*
This source code generated by dcomp and include dcomp's source code.
dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp)
dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt)
*/
|
D
|
import std.algorithm;
import std.array;
import std.bigint;
import std.bitmanip;
import std.conv;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; }
T[] readToArray(T)() {
return readln.split.to!(T[]);
}
void readInto(T...)(ref T ts) {
auto ss = readln.split;
foreach(ref t; ts) {
t = ss.front.to!(typeof(t));
ss.popFront;
}
}
// 冪乗をmod取りつつ計算
pure ulong modPow(ulong a, ulong n, ulong m) {
ulong r = 1;
while (n > 0) {
if(n % 2 != 0) r = r * a % m;
a = a * a % m;
n /= 2;
}
return r;
}
// フェルマーの小定理から乗法逆元を計算
// 定理の要請により法は素数
pure ulong modInv(ulong a, ulong m) {
return modPow(a, m-2, m);
}
// mod取りつつ順列を計算
pure ulong modPerm(ulong n, ulong k, ulong m) {
if (n < k) return 0;
ulong r = 1;
for (ulong i = n-k+1; i <= n; i++) {
r *= i;
r %= m;
}
return r;
}
// mod取りつつ順列を計算
pure ulong modFact(ulong n, ulong m) {
return modPerm(n, n, m);
}
// mod取りつつ組み合わせを計算
// modInvを使っているので法は素数
pure ulong modComb(ulong n, ulong r, ulong m) {
return modPerm(n, r, m)*modInv(modFact(r, m), m) % m;
}
immutable ulong MOD = 1000000007;
void main() {
ulong n, k;
readInto(n, k);
for (ulong i = 1; i <= k; i++) {
writeln(modComb(n-k+1, i, MOD)*modComb(k-1, i-1, MOD) % MOD);
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.numeric;
import std.stdio;
import std.string;
void main()
{
long[] s = readln().chomp().split(" ").map!(to!(long)).array;
long a = s[0];
long b = s[1];
if (a + b == 15)
{
writeln("+");
return;
}
if (a * b == 15)
{
writeln("*");
return;
}
writeln("x");
}
|
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;
scan(n);
auto a = readln.split.to!(long[]);
auto asum = a.sum();
auto tn = 1L * n * (n + 1) / 2;
if (asum % tn) {
writeln("NO");
return;
}
auto K = asum / tn;
auto d = new long[](n);
foreach (i ; 0 .. n) {
d[i] = a[(i + 1) % n] - a[i];
}
d[] -= K;
debug {
writeln(d);
}
long cnt;
foreach (i ; 0 .. n) {
if (d[i] > 0 && d[i] % n) {
writeln("NO");
return;
}
cnt += abs(d[i]) / n;
}
writeln(cnt == K ? "YES" : "NO");
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm.searching;
void main(){
auto l = readln.chomp;
auto x = l.to!long;
long u = 0;
while(x >= 500)
{
u += 1000;
x -= 500;
}
while(x >= 5)
{
u+= 5;
x -= 5;
}
u.writeln;
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdlib, std.datetime;
immutable int N = 26;
//immutable int D = 365;
int D;
long[] C;
long[][] S;
void main() {
auto stattime = Clock.currTime();
D = readln.chomp.to!int;
C = readln.split.map!(to!long).array;
S = iota(D).map!(_ => readln.split.map!(to!long).array).array;
auto ans = new int[](D);
auto prev = new int[](N);
prev[] = -1;
foreach (i; 0..D) {
int best_idx = -1;
long best_score = -(1L << 59);
foreach (j; 0..N) {
long minus = (i - prev[j]) * C[j];
long plus = S[i][j];
long score = minus + plus;
if (score >= best_score) {
best_score = score;
best_idx = j;
}
}
ans[i] = best_idx;
}
ans.each!(a => writeln(a + 1));
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto n = readln.chomp.to!size_t;
auto s = readln.chomp;
auto x = 0, m = 0;
foreach (si; s) {
x += si.predSwitch('I', +1, 'D', -1);
m = max(m, x);
}
writeln(m);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.bigint;
import std.typecons;
import std.algorithm;
import std.array;
import std.math;
void main() {
auto N = readln.chomp.to!real;
writeln(cast(ulong)(floor(sqrt(N)) ^^2));
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;
immutable long INF = 1L << 60;
const int N = 10;
const int M = 5;
void main() {
auto B = N.iota.map!(_ => readln.chomp).array;
auto C = ["pppppppppppppppppppp",
"pppppppppppppppppppp",
"pppppppppppppppppppp",
"pppppppppppppppppppp",
"pppppppppppppppppppp"];
foreach (i; 0..N) B[i] = "ppppp" ~ B[i] ~ "ppppp";
B = C ~ B ~ C;
int batu, nasi;
foreach (i; 5..N+5) {
foreach (j; 5..N+5) {
batu = 0;
nasi = 0;
foreach (k; 0..M) {
if (B[i][j+k] == 'X') batu += 1;
if (B[i][j+k] == '.') nasi += 1;
}
if (batu == 4 && nasi == 1) {
writeln("YES");
return;
}
batu = 0;
nasi = 0;
foreach (k; 0..M) {
if (B[i+k][j] == 'X') batu += 1;
if (B[i+k][j] == '.') nasi += 1;
}
if (batu == 4 && nasi == 1) {
writeln("YES");
return;
}
batu = 0;
nasi = 0;
foreach (k; 0..M) {
if (B[i+k][j+k] == 'X') batu += 1;
if (B[i+k][j+k] == '.') nasi += 1;
}
if (batu == 4 && nasi == 1) {
writeln("YES");
return;
}
batu = 0;
nasi = 0;
foreach (k; 0..M) {
if (B[i-k][j+k] == 'X') batu += 1;
if (B[i-k][j+k] == '.') nasi += 1;
}
if (batu == 4 && nasi == 1) {
writeln("YES");
return;
}
}
}
writeln("NO");
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.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 string[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto s = RD!string;
foreach (c; s)
{
if (c == 'U')
ans[ti] ~= "D";
else if (c == 'L')
ans[ti] ~= "L";
else if (c == 'R')
ans[ti] ~= "R";
else
ans[ti] ~= "U";
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
import std.ascii;
void main() {
int n;
scan(n);
auto adj = new int[][](n, 0);
foreach (i ; 1 .. n) {
int pi;
scan(pi);
adj[pi-1] ~= i;
}
bool ans = true;
void dfs(int v, int p) {
if (adj[v].empty) return;
int lc;
foreach (u ; adj[v]) {
if (u == p) continue;
if (adj[u].empty) {
lc++;
}
else {
dfs(u, v);
}
}
if (lc < 3) ans = false;
return;
}
dfs(0,0);
writeln(ans ? "Yes" : "No");
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, 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 p = RD;
auto a = RD;
auto b = RD;
auto c = RD;
auto ap = (a - (p % a)) % a;
auto bp = (b - (p % b)) % b;
auto cp = (c - (p % c)) % c;
ans[ti] = min(ap, bp, cp);
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
void main() {
try {
for (; ; ) {
const numCases = readInt;
foreach (caseId; 0 .. numCases) {
const N = readInt;
auto A = new int[N];
foreach (i; 0 .. N) {
A[i] = readInt;
}
int ans = 1;
foreach (i; 0 .. N) foreach (j; i + 1 .. N) {
int cnt;
foreach (k; 0 .. N) {
if ((j - i) * (A[k] - A[i]) == (k - i) * (A[j] - A[i])) {
++cnt;
}
}
chmax(ans, cnt);
}
writeln(N - ans);
}
}
} catch (EOFException e) {
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto k = RD!int;
auto s = RD!(char[]);
bool ok = true;
foreach (i; 0..n-k)
{
if (s[i] == s[i+k]) continue;
if (s[i] == '?')
s[i] = s[i+k];
else if (s[i+k] == '?')
s[i+k] = s[i];
else
{
ok = false;
break;
}
}
if (!ok) continue;
long cnt0, cnt1;
foreach (i; 0..k)
{
if (s[i] == '0')
++cnt0;
else if (s[i] == '1')
++cnt1;
}
debug writeln("cnt0:", cnt0, " cnt1:", cnt1);
auto k2 = k/2;
foreach (i; 0..n-k)
{
if (cnt0 > k2 || cnt1 > k2)
{
ok = false;
break;
}
if (s[i] == '0')
--cnt0;
else if (s[i] == '1')
--cnt1;
if (s[i+k] == '0')
++cnt0;
else if (s[i+k] == '1')
++cnt1;
debug writeln("i:", i, " cnt0:", cnt0, " cnt1:", cnt1);
}
if (cnt0 > k2 || cnt1 > k2)
ok = false;
ans[ti] = ok;
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush;
debug readln;
}
|
D
|
//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.bitmanip;
import std.typecons;
import std.numeric;
final class PrimeTable {
private:
BitArray a;
size_t n;
public:
pure nothrow @nogc
bool isPrime (size_t i) const {
return (i & 1) ? !a[i>>>1] : (i == 2);
}
pure
int[] primes () const {
int[] p;
if (n > 2) {
p ~= 2;
p ~= (~a).bitsSet.map! (i => (2 * i + 1).to!int).array;
}
return p;
}
pure
this (size_t _n) {
n = _n;
auto m = n >> 1;
a.length = max (1, m);
a[0] = true;
foreach (i; 1 .. ceil((sqrt (n.to!(double)) - 1.0)).to!(size_t)) {
if (!a[i]) {
immutable k = (i << 1) + 1;
for (size_t j = 2 * i * (i + 1); j < m; j += k) {
a[j] = true;
}
}
}
}
}
alias Factorization = Tuple!(ulong, "p", uint, "c")[];
pure
Factorization factorizationTrialDivision (ulong x, in int[] primes) in {
assert (primes.takeExactly (2).equal ([2, 3]));
} body {
Factorization f;
foreach (p; chain (primes, iota (primes.back + 2, int.max, 2))) {
if (p.to!ulong * p > x) {
break;
}
uint c;
while (!(x % p)) {
x /= p;
++c;
}
if (c) {
f ~= tuple!("p", "c")(p.to!ulong, c);
}
}
if (x > 1) {
f ~= tuple!("p", "c")(x, 1U);
}
return f;
}
final class InputReader {
private:
ubyte[] p, buffer;
bool eof;
bool rawRead () {
if (eof) {
return false;
}
p = stdin.rawRead (buffer);
if (p.empty) {
eof = true;
return false;
}
return true;
}
ubyte nextByte(bool check) () {
static if (check) {
if (p.empty) {
if (!rawRead ()) {
return 0;
}
}
}
auto r = p.front;
p.popFront ();
return r;
}
public:
this () {
buffer = uninitializedArray!(ubyte[])(16<<20);
}
bool seekByte (in ubyte lo) {
while (true) {
p = p.find! (c => c >= lo);
if (!p.empty) {
return false;
}
if (!rawRead ()) {
return true;
}
}
}
template next(T) if (isSigned!T) {
T next () {
if (seekByte (45)) {
return 0;
}
T res;
ubyte b = nextByte!false ();
if (b == 45) {
while (true) {
b = nextByte!true ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 - (b - 48);
}
} else {
res = b - 48;
while (true) {
b = nextByte!true ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 + (b - 48);
}
}
}
}
template next(T) if (isUnsigned!T) {
T next () {
if (seekByte (48)) {
return 0;
}
T res = nextByte!false () - 48;
while (true) {
ubyte b = nextByte!true ();
if (b < 48 || b >= 58) {
break;
}
res = res * 10 + (b - 48);
}
return res;
}
}
T[] nextA(T) (in int n) {
auto a = uninitializedArray!(T[]) (n);
foreach (i; 0 .. n) {
a[i] = next!T;
}
return a;
}
}
long test (const ulong a, const ulong m, in int[] p) {
long g = gcd (a, m);
long m1 = m / g;
auto f = factorizationTrialDivision (m1, p);
immutable l = f.length.to!int;
long cnt (const long x) {
immutable t = x / g;
long res;
void go (int k, int sign, long x) {
if (x > t) return;
if (k < 0) {
res += sign * (t / x);
} else {
go (k - 1, sign, x);
go (k - 1, -sign, x * f[k].p);
}
}
go (l - 1, 1, 1L);
return res;
}
return cnt (a + m - 1) - cnt (a - 1);
}
int[] primes () {
auto pt = new PrimeTable (110_000);
return pt.primes ();
}
void main() {
auto p = primes ();
auto r = new InputReader ();
immutable nt = r.next!uint ();
foreach (tid; 0 .. nt) {
immutable a = r.next!ulong;
immutable m = r.next!ulong;
writeln (test (a, m, p));
}
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n; rd(n);
auto x=readln.split.to!(int[]);
int[200005] val,pre,nxt;
for(int i=1;i<=n;i++) {
val[i]=x[i-1];
pre[i]=i-1;
nxt[i]=i+1;
}
nxt[0]=1;
pre[n+1]=n;
int cnt=n;
for(int i=nxt[1];i<=n;i=nxt[i]) {
while(val[i]==val[pre[i]]) {
val[i]++;
cnt--;
nxt[pre[pre[i]]]=nxt[pre[i]];
pre[nxt[pre[i]]]=pre[pre[i]];
}
}
writeln(cnt);
for(int i=nxt[0];i<=n;i=nxt[i])
write(val[i]),write(' ');
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
|
D
|
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto n = readln.strip.to !(int);
auto s = readln.strip.map !(q{a == 'R'}).array;
auto t = s ~ s ~ s;
immutable int [] toFind = [0, 1];
auto pos = t.countUntil (toFind).to !(int);
if (pos == -1)
{
writeln ((n + 2) / 3);
continue;
}
pos += 1;
writeln (t[pos..pos + n].group.map !(q{(a[1] + 0) / 3}).sum);
}
}
|
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 n = RD!int;
auto xy = new int[][](n);
foreach (i; 0..n)
{
xy[i] = [RD!int, RD!int];
}
if (n % 2 == 1)
{
writeln("NO");
}
else
{
int[][] vec;
foreach (i; 1..n/2+1)
{
int[] t = xy[i].dup;
t[] -= xy[i-1][];
vec ~= t;
}
bool ok = true;
foreach (i; n/2..n)
{
int[] t = xy[(i+1)%n].dup;
t[] -= xy[i][];
t = [-t[0], -t[1]];
if ((vec[i-n/2][0] != t[0]) || (vec[i-n/2][1] != t[1]))
{
ok = false;
break;
}
}
writeln(ok ? "YES" : "NO");
}
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new string[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
ans[ti] = "9";
long x = 8;
foreach (i; 1..n)
{
ans[ti] ~= cast(char)('0'+x);
x = (x+1) % 10;
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
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;
}
}
}
void main() {
auto r = new InputReader;
int n = r.next!int, m = r.next!int;
auto f (int k) {
auto c = new int[2];
foreach (i; 0 .. k) {
++c[1 & r.next!int];
}
return c;
}
auto a = f (n), b = f (m);
writeln (min (a[0], b[1]) + min (a[1], b[0]));
}
|
D
|
void main()
{
long n = rdElem;
long total;
foreach (i; 1 .. n+1)
{
if (i % 3 && i % 5) total += i;
}
total.writeln;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
enum double eps = 1.0e-9;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
import std.stdio;
immutable mod = 4294967311L;
void main(){
long x, y;
int n, op;
scanf("%d", &n);
foreach(i; 0..n){
scanf("%d%lld", &op, &y);
if(op == 1) x += y;
else if(op == 2) x -= y;
else if(op == 3) x *= y;
else if(op == 4) x = mul(x, inv(y));
x %= mod;
}
if(x < 0) x += mod;
if(x > int.max) x -= mod;
writeln(x);
}
long inv(long y){
long ret = 1;
for(long e=mod-2; e; e>>=1, y=mul(y,y))if(e&1){
ret = mul(ret, y);
}
return ret;
}
long mul(long x, long y){
if(y < 0){
y = -y; x = -x;
}
return ( ((x*(y>>16)%mod)<<16) + (x*(y&0xffff)) ) % mod;
}
|
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;
}
int calc(int[] xs) {
int[int] d;
for (int i = 0; i < xs.length; i++) {
int x = xs[i];
d[x - 1]++;
d[x]++;
d[x + 1]++;
}
int ans = 0;
foreach (k, v; d) {
ans = max(ans, v);
}
return ans;
}
void main() {
readint;
auto xs = readints;
writeln(calc(xs));
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
writeln(N > 999 ? "ABD" : "ABC");
stdout.flush();
debug readln();
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.ascii;
void main()
{
writeln(readln.chomp.uniq.array.length==1?"No":"Yes");
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
long calc(long h, long w) {
if (h == 1 || w == 1) return 1;
long a = (w + 1) / 2;
long b = w / 2;
long ans = (a + b) * (h / 2);
if (h % 2 == 1) ans += a;
return ans;
}
void main() {
long h, w; scan(h, w);
writeln(calc(h, w));
}
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
|
D
|
import std.stdio;
import std.algorithm;
import std.math;
import std.conv;
import std.string;
T readNum(T)(){
return readStr.to!T;
}
T[] readNums(T)(){
return readStr.split.to!(T[]);
}
string readStr(){
return readln.chomp;
}
void main(){
auto i = readNums!ulong;
auto x = i[0];
auto y = i[1];
auto z = i[2];
auto buy = (x / z) + (y / z);
x %= z;
y %= z;
ulong give;
if(x + y >= z){
give = min(z - x, z - y);
buy += (x + y) / z;
}
writeln(buy, " ", give);
}
|
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 scan(T...)(ref T args) {
auto line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int N;
scan(N);
auto t = readln.split.to!(int[]);
long s = t.sum();
long ans;
int M;
scan(M);
foreach (i ; 0 .. M) {
int pi, xi;
scan(pi, xi);
pi--;
writeln(s - t[pi] + xi);
}
}
|
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;
int N, K;
int[][] edge;
bool[] visited;
void dfs(int n, int prev, int d) {
visited[n] = true;
if (d == 0) return;
foreach (m; edge[n]) {
if (m == prev) continue;
dfs(m, n, d-1);
}
}
void main() {
auto s = readln.split.map!(to!int).array;
N = s[0];
K = s[1];
visited = new bool[](N);
edge = new int[][](N);
foreach (i; 0..N-1) {
s = readln.split.map!(to!int).array;
edge[s[0]-1] ~= s[1]-1;
edge[s[1]-1] ~= s[0]-1;
}
int ans = 1 << 29;
if (K % 2 == 0) {
foreach (i; 0..N) { // 各頂点を木の中心と仮定
fill(visited, false);
dfs(i, -1, K/2);
ans = min(ans, N-visited.sum);
}
}
else {
foreach (i; 0..N) {
foreach (j; edge[i]) {
if (j < i) continue;
fill(visited, false);
dfs(i, -1, K/2);
dfs(j, -1, K/2);
ans = min(ans, N-visited.sum);
}
}
}
writeln(K == 1 ? N-2 : ans);
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;
void main() {
auto K = readln.chomp.to!int;
writeln("3 3");
auto IK = 0b100000000000000000 + K;
auto IO = 0b100000000000000000;
auto OK = K;
auto ans =
[[IK, IO, 0],
[OK, IK, OK],
[0, 0, OK]];
foreach (a; ans) {
foreach (b; a) write(b, " ");
writeln;
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.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;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto A = RD;
auto P = RD;
writeln((A * 3 + P) / 2);
stdout.flush();
debug readln();
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
import std.array;
int n; rd(n);
auto g=readln.split.to!(int[]).map!((e)=>(e-1)).array;
int f(int i, bool[] vis){
if(vis[i]) return i;
vis[i]=true;
return f(g[i], vis);
}
foreach(i; 0..n){
auto vis=new bool[](n);
writeln(f(i, vis)+1);
}
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x) e=l[i].to!(typeof(e));
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main() {
foreach (string line; stdin.lines) {
int ans = 1;
int n = line.chomp.to!int;
for (int i = 1; i <= n; i++) {
ans += i;
}
ans.writeln;
}
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto N = readln.chomp.to!int;
auto S = readln.chomp;
auto A = new int[](N+1);
foreach (i; 0..N) A[i+1] = A[i] + (S[i] == '#');
int ans = 1 << 29;
foreach (i; 0..N+1) {
int left_black = A[i];
int right_white = N - i - (A[N] - A[i]);
ans = min(ans, left_black + right_white);
}
ans.writeln;
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
auto minBy(alias key, T)(T input) {
return input.reduce!((a, b) => unaryFun!key(a) <= unaryFun!key(b) ? a : b);
}
void main() {
int N, L; scan(N, L);
// N 個のリングのおいしさは、L, L + 1, L + 2, ..., L + N - 1
// int t = iota(L, L + N).reduce!((a, b) => abs(a) <= abs(b) ? a : b);
int t = iota(L, L + N).minBy!(abs);
int ans = iota(L, L + N).sum - t;
writeln(ans);
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii;
import std.typecons, std.functional, std.traits;
import std.algorithm, std.container;
import core.stdc.stdlib;
void main()
{
long N = scanElem;
long[] list;
list.length=N;
foreach(i;0..N)
{
list[scanElem-1] = i;
}
long res;
long p;
long k;
foreach(n; list)
{
if(p>n)
{
k=0;
}
k++;
res = max(res, k);
p=n;
}
writeln(N-res);
}
class UnionFind{
UnionFind parent = null;
void merge(UnionFind a)
{
if(same(a)) return;
a.root.parent = this.root;
}
UnionFind root()
{
if(parent is null)return this;
return parent = parent.root;
}
bool same(UnionFind a)
{
return this.root == a.root;
}
}
void scanValues(TList...)(ref TList list)
{
auto lit = readln.splitter;
foreach (ref e; list)
{
e = lit.fornt.to!(typeof(e));
lit.popFront;
}
}
T[] scanArray(T = long)()
{
return readln.split.to!(long[]);
}
void scanStructs(T)(ref T[] t, size_t n)
{
t.length = n;
foreach (ref e; t)
{
auto line = readln.split;
foreach (i, ref v; e.tupleof)
{
v = line[i].to!(typeof(v));
}
}
}
long scanULong(){
long x;
while(true){
const c = getchar;
if(c<'0'||c>'9'){
break;
}
x = x*10+c-'0';
}
return x;
}
T scanElem(T = long)()
{
char[] res;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
res ~= cast(char) c;
c = getchar;
}
return res.strip.to!T;
}
template fold(fun...) if (fun.length >= 1)
{
auto fold(R, S...)(R r, S seed)
{
static if (S.length < 2)
{
return reduce!fun(seed, r);
}
else
{
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
struct Factor
{
long n;
long c;
}
Factor[] factors(long n)
{
Factor[] res;
for (long i = 2; i ^^ 2 <= n; i++)
{
if (n % i != 0)
continue;
int c;
while (n % i == 0)
{
n = n / i;
c++;
}
res ~= Factor(i, c);
}
if (n != 1)
res ~= Factor(n, 1);
return res;
}
long[] primes(long n)
{
if(n<2)return [];
auto table = new long[n+1];
long[] res;
for(int i = 2;i<=n;i++)
{
if(table[i]==-1) continue;
for(int a = i;a<table.length;a+=i)
{
table[a] = -1;
}
res ~= i;
}
return res;
}
bool isPrime(long n)
{
if (n <= 1)
return false;
if (n == 2)
return true;
if (n % 2 == 0)
return false;
for (long i = 3; i ^^ 2 <= n; i += 2)
if (n % i == 0)
return false;
return true;
}
|
D
|
void main(){
string s = readln().chomp();
if(s=="ABC")writeln("ARC");
else writeln("ABC");
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}}
void main()
{
int d, g; readV(d, g); g /= 100;
int[] p, c; readC(d, p, c); c[] /= 100;
auto m = d.iota.map!(i => (i+1)*p[i]).sum + c.sum;
auto dp = new int[][](d+1, m+1), inf = 10^^9;
foreach (dpi; dp) dpi[] = inf;
dp[0][0] = 0;
foreach (i; 0..d) {
auto s = i+1;
foreach (j; 0..m+1) {
foreach (k; 0..p[i])
if (j-k*s >= 0)
dp[i+1][j] = min(dp[i+1][j], dp[i][j-k*s]+k);
if (j-(p[i]*s+c[i]) >= 0)
dp[i+1][j] = min(dp[i+1][j], dp[i][j-(p[i]*s+c[i])]+p[i]);
}
}
writeln(dp[d][g..$].reduce!min);
}
|
D
|
void main()
{
int[] tmp = readln.split.to!(int[]);
int month = tmp[0], day = tmp[1];
int cnt;
foreach (m; 2 .. month+1)
{
foreach (d; 21 .. day+1)
{
int div = d / 10, rem = d % 10;
if (rem >= 2 && m == div * rem)
{
++cnt;
}
}
}
cnt.writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
while (true) {
int n;
scan(n);
if (n == 0) return;
auto map = new int[][](21, 21);
foreach (i ; 0 .. n) {
int xi, yi;
scan(xi, yi);
map[xi][yi]++;
}
int m;
scan(m);
int hsk;
int px = 10, py = 10;
foreach (i ; 0 .. m) {
char di;
int li;
scan(di, li);
foreach (j ; 0 .. li) {
if (di == 'N') {
py++;
}
else if (di == 'S') {
py--;
}
else if (di == 'E') {
px++;
}
else {
px--;
}
hsk += map[px][py];
map[px][py] = 0;
}
}
debug {
writeln(hsk);
}
writeln(hsk == n ? "Yes" : "No");
}
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd = readln.split.to!(int[]), a = rd[0], b = rd[1];
auto s = readln.chomp;
foreach (i, c; s) {
if (i == a) {
if (c != '-') {
writeln("No");
return;
}
} else {
if (c < '0' || c > '9') {
writeln("No");
return;
}
}
}
writeln("Yes");
}
|
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
{
string s;
int q;
Query[] queries;
}
enum QueryType
{
reverse,
prepend,
append,
}
struct Query
{
QueryType type;
char c;
}
void parseInput(T)(out Input input, T file)
{
with (file) with (input)
{
s = readln().strip();
q = readln().strip().to!int;
queries.length = q;
foreach (i; 0..q)
{
Query query;
auto ar = readln().strip().split().array();
auto t = ar[0];
if (t == "1")
{
query.type = QueryType.reverse;
}
else
{
auto f = ar[1];
if (f == "1")
{
query.type = QueryType.prepend;
}
else
{
query.type = QueryType.append;
}
query.c = ar[2][0];
}
queries[i] = query;
}
}
}
auto main2(Input* input)
{
with (input)
{
char[200000] pre;
int preLength = 0;
char[200000] post;
int postLength = 0;
bool reversed = false;
foreach (query; queries)
{
final switch (query.type) with (QueryType)
{
case reverse:
reversed = !reversed;
break;
case prepend:
if (!reversed)
pre[preLength++] = query.c;
else
post[postLength++] = query.c;
break;
case append:
if (reversed)
pre[preLength++] = query.c;
else
post[postLength++] = query.c;
break;
}
}
if (!reversed)
{
for (int i = preLength - 1; i >= 0; i--)
{
write(pre[i]);
}
write(s);
for (int i = 0; i < postLength; i++)
{
write(post[i]);
}
}
else
{
for (int i = postLength - 1; i >= 0; i--)
{
write(post[i]);
}
foreach_reverse (c; s)
write(c);
for (int i = 0; i < preLength; i++)
{
write(pre[i]);
}
}
}
}
alias retType = ReturnType!main2;
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;
import std.ascii;
void main()
{
auto a = ['a', 'i', 'u', 'e', 'o'];
auto c = readln.chomp.to!char;
if (a.count(c)) {
writeln("vowel");
} else {
writeln("consonant");
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void main()
{
auto nm = readln.split.to!(int[]);
auto N = nm[0];
auto M = nm[1];
auto G = new int[][N];
auto RG = new int[][N];
foreach (_; 0..M) {
auto ab = readln.split.to!(int[]);
auto A = ab[0]-1;
auto B = ab[1]-1;
G[A] ~= B;
RG[B] ~= A;
}
foreach (n; 0..N) {
auto memo = new bool[](N);
int[] walk(int i, int p) {
foreach (j; RG[i]) if (j != p && memo[j]) return [];
int[] js;
foreach (j; G[i]) if (memo[j]) js ~= j;
if (js.length == 1) {
return [js[0], i];
} else if (js.length > 1) {
return [];
}
foreach (j; G[i]) {
memo[j] = true;
auto r = walk(j, i);
if (!r.empty) return r ~ i;
memo[j] = false;
}
return [];
}
memo[n] = true;
auto r = walk(n, -1);
if (r.empty) continue;
auto rr = [r[0]];
foreach (p; r[1..$]) {
if (p == rr[0]) break;
rr ~= p;
}
if (!r.empty) {
writeln(rr.length);
foreach (p; rr) writeln(p + 1);
return;
}
}
writeln(-1);
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
int n = read.to!int;
int k = read.to!int;
int[] as;
foreach(i; 0 .. n) as ~= read.to!int;
int[int] ac;
foreach(a; as){
if(a !in ac) ac[a] = 0;
ac[a] += 1;
}
int ans;
int ocnt = n % 2;
int[] ks = ac.keys;
foreach(a; ks){
int c = ac[a];
if(c % 2 == 0) ans += c;
else{
if(ocnt >= 1) ans += c, ocnt = 0;
else ans += c - 1, ocnt = 1;
}
log("a:", a, "cnt:", c, "ocnt:", ocnt, "ans:", ans);
}
ans.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, q;
scan(n, q);
while (q--) {
int vi, wi;
scan(vi, wi);
solve(n, vi, wi);
}
}
void solve(int n, int v, int w) {
if (n == 1) {
writeln(min(v, w));
return;
}
auto va = new int[](0);
va ~= v;
while (v > 1) {
if (v % n <= 1) {
v /= n;
}
else {
v = (v / n) + 1;
}
va ~= v;
}
va.reverse();
auto wa = new int[](0);
wa ~= w;
while (w > 1) {
if (w % n <= 1) {
w /= n;
}
else {
w = (w / n) + 1;
}
wa ~= w;
}
wa.reverse();
debug {
writeln(va);
writeln(wa);
}
int ans = 0;
while (!va.empty) {
while (!wa.empty) {
if (va.front == wa.front) {
ans = max(ans, va.front);
va.popFront;
wa.popFront;
break;
}
else if (va.front < wa.front) {
va.popFront;
break;
}
else {
wa.popFront;
}
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
int[10][10] CS;
void main()
{
int H = readln.split.to!(int[])[0];
foreach (i; 0..10) CS[i][] = readln.split.to!(int[])[];
foreach (_; 0..10)
foreach (i; 0..10)
foreach (j; 0..10)
foreach (k; 0..10)
if (CS[i][j] > CS[i][k] + CS[k][j])
CS[i][j] = CS[i][k] + CS[k][j];
int c;
foreach (_; 0..H) {
auto l = readln.split.to!(int[]);
foreach (e; l) if (e != -1) c += CS[e][1];
}
writeln(c);
}
|
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 n = readln.chomp.to!int;
writeln(n/2+(n%2>0));
}
|
D
|
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
long solve(long X, long Y) {
const c = Y / X, d = Y % X;
long a, b, r;
if (c == 0) {
a = 1;
b = 0;
r = d;
} else {
a = c;
b = 1;
r = d / 2;
}
const n = X * a + r;
debug {
// writeln(X, " ", Y, ": ", n);
}
assert(1 <= n); assert(n <= 2 * max(X, Y)^^2);
assert(n % X == Y % n);
return n;
}
void main() {
debug {{
enum lim = 100;
for (long x = 2; x <= lim; x += 2) for (long y = 2; y <= lim; y += 2) {
solve(x, y);
}
}}
try {
for (; ; ) {
const numCases = readInt();
foreach (_; 0 .. numCases) {
const X = readLong();
const Y = readLong();
const ans = solve(X, Y);
writeln(ans);
}
}
} catch (EOFException e) {
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto n = RD!int;
auto m = RD!int;
auto a = RDA;
auto b = RDA;
int ans = int.max;
foreach (i; 0..2^^9)
{
bool okok = true;
foreach (j; 0..n)
{
bool ok;
foreach (k; 0..m)
{
auto x = a[j] & b[k];
bool ng;
foreach (l; 0..9)
{
auto bit = 1 << l;
if ((i & bit) == 0 && (x & bit))
{
ng = true;
break;
}
}
if (!ng)
{
ok = true;
break;
}
}
if (!ok)
{
okok = false;
break;
}
}
if (okok)
ans.chmin(i);
}
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
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;
auto M = RD;
auto ans = new long[](N);
ans[] = -1;
bool ok = true;
foreach (i; 0..M)
{
auto s = RD-1;
auto c = RD;
if (ans[s] != -1 && ans[s] != c)
ok = false;
ans[s] = c;
}
if (N != 1 && ans[0] == 0)
ok = false;
if (!ok)
writeln(-1);
else
{
if (ans[0] == -1)
write(N == 1 ? 0 : 1);
else
write(ans[0]);
foreach (i; 1..N)
{
if (ans[i] == -1)
write(0);
else
write(ans[i]);
}
}
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
auto N = readln.chomp.to!long;
auto n = N;
auto S = 0;
while (n > 0) {
S += n % 10;
n /= 10;
}
writeln(N % S == 0 ? "Yes" : "No");
}
|
D
|
import std.stdio, std.algorithm, std.string, std.conv, std.array, std.range, std.math;
int readint() { return readln.chomp.to!int; }
int[] readints() { return readln.split.to!(int[]); }
void main() {
int n = readint();
string s = readln.chomp;
int[] a = new int[300010];
int[] b = new int[300010];
foreach (i, c; s) {
a[i] += (c == 'E');
b[i] += (c == 'W');
}
foreach (i; 0..n - 1) {
a[i+1] += a[i];
b[i+1] += b[i];
}
int ans = a[n - 1] - a[0];
foreach (i; 1..n) {
int x = b[i - 1] + (a[n - 1] - a[i]);
ans = min(ans, x);
}
writeln(ans);
string _ = readln;
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.