code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
immutable long MOD = 10^^9 + 7;
bool valid(int mask) {
while (mask > 0) {
if ((mask & 0b11) == 0b11) {
return false;
}
mask >>= 1;
}
return true;
}
void main() {
auto s = readln.split.map!(to!int);
auto H = s[0];
auto W = s[1];
auto K = s[2] - 1;
auto dp = new int[][](H+1, W);
dp[0][0] = 1;
auto masks = (1<<(W-1)).iota.filter!(a => valid(a)).array;
foreach (i; 0..H) {
foreach (j; 0..W) {
foreach (mask; masks) {
if (j > 0 && (mask & (1 << (j - 1)))) {
(dp[i+1][j-1] += dp[i][j]) %= MOD;
} else if (j < W - 1 && (mask & (1 << j))) {
(dp[i+1][j+1] += dp[i][j]) %= MOD;
} else {
(dp[i+1][j] += dp[i][j]) %= MOD;
}
}
}
}
dp[H][K].writeln;
}
|
D
|
import std.algorithm, std.array, std.conv, std.range, std.stdio, std.string;
void main()
{
immutable n = readln.chomp.to!size_t;
ubyte[][] b;
foreach (i; 0..n)
b ~= cast(ubyte[])readln.chomp;
size_t ret;
foreach (i; 0..n)
{
ret += b.sym * n;
b = b[1..$] ~ b[0];
}
ret.writeln;
}
bool sym(T)(T[][] b)
{
foreach (i; 0..b.length)
foreach (j; 0..i)
if (b[i][j]!=b[j][i]) return false;
return true;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.algorithm, std.array;
auto solve(string s_) {
immutable N = s_.to!int();
auto a = readln.split.map!(to!int).array();
uint n=1; while(n<N) n*=2;
auto s = new int[n*2]; s[]=-1;
void update(int v, int i) {
for(uint p=v-1+n; p>0; p/=2) s[p]=max(s[p],i);
}
int get(int v) {
if(v<=0 || n<v) return -1;
uint l=0, r=n, p=1;
int w=-1;
while(v<r) {
auto m = (l+r)/2;
if(v>m) w=max(w,s[p*2]), p=p*2+1, l=m;
else p*=2, r=m;
}
return max(w,s[p]);
}
auto dp = new long[N];
foreach(i,v;a) {
immutable k = get(v-1);
if(k<0) dp[i]=v*(i+1);
else dp[i]=dp[k]+v*(i-k);
update(v,i.to!int());
}
return dp.sum();
}
void main(){ for(string s; (s=readln.chomp()).length;) writeln(solve(s)); }
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;
void main() {
auto N = readln.chomp.to!int;
if (N == 0) {
"a".writeln;
return;
}
string ans = "";
char c = 'a';
while (N > 0) {
ans ~= c;
int tmp = 1;
while (N - tmp >= 0) {
N -= tmp;
ans ~= c;
tmp += 1;
}
c += 1;
}
ans.writeln;
}
|
D
|
void main()
{
string s = rdStr;
writeln(s[0..4], " ", s[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.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, std.conv, std.array, std.string;
import std.algorithm;
void main() {
ulong count = 0;
int[string] dict;
auto N = readln.chomp.to!int;
foreach (i; 0..N) {
char[] line = readln.chomp.dup;
sort(line.representation);
auto word = line.to!string;
if (!(word in dict)) {
dict[word] = 1;
continue;
}
count += dict[word]++;
}
write(count);
}
|
D
|
void main() {
auto s = rs;
ulong tmp;
foreach(i, v; s) {
if(v == 'A') {
tmp = i;
break;
}
}
foreach_reverse(i, v; s) {
if(v == 'Z') {
(i - tmp + 1).writeln;
return;
}
}
}
// ===================================
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
int lcs(string x, string y) {
int m = x.length.to!int, n = y.length.to!int;
int[][] c = new int[][](m+1, n+1);
foreach (i; 1 .. m+1) {
foreach (j; 1 .. n+1) {
if (x[i-1] == y[j-1]) {
c[i][j] = c[i-1][j-1] + 1;
} else {
c[i][j] = max(c[i-1][j], c[i][j-1]);
}
}
}
return c[m][n];
}
void main() {
int q = readln.chomp.to!int;
foreach (i; 0 .. q) {
string x = readln.chomp, y = readln.chomp;
lcs(x, y).writeln;
}
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
auto s = readln.chomp.to!(char[]);
auto t = readln.chomp.to!(char[]);
const no = "UNRESTORABLE";
if (t.length > s.length) {
writeln(no);
return;
}
char[][] cand;
for (size_t i = 0; i + t.length <= s.length; i++) {
bool ok = true;
foreach (j; 0 .. t.length) {
if (s[i + j] != '?') {
ok &= s[i + j] == t[j];
}
}
if (ok) {
auto u = s[0 .. i] ~ t ~ s[i + t.length .. $];
foreach (ref c; u)
if (c == '?')
c = 'a';
cand ~= u;
}
}
if (cand.length > 0) {
sort(cand);
writeln(cand[0]);
} else {
writeln(no);
}
}
void rd(T...)(ref T x) {
import std.stdio : readln;
import std.string : split;
import std.conv : to;
auto l = readln.split;
assert(l.length == x.length);
foreach (i, ref e; x)
e = l[i].to!(typeof(e));
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.stdio;
import std.string;
import std.range;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
struct S {
int node;
long cost;
}
long[] bfs(int k, int n, S[][int] adj) {
auto cost = new long[n + 1];
auto used = new bool[n + 1];
S[] q = [S(k, 0)];
while (q.length > 0) {
auto d = q[0];
q = q[1 .. $];
if (used[d.node])
continue;
used[d.node] = true;
cost[d.node] = d.cost;
if (d.node !in adj)
continue;
foreach (s; adj[d.node]) {
q ~= S(s.node, d.cost + s.cost);
}
}
return cost;
}
void main() {
int n = readint();
S[][int] adj;
for (int i = 0; i < n - 1; i++) {
auto abc = readints();
int a = abc[0], b = abc[1], c = abc[2];
adj[a] ~= S(b, c);
adj[b] ~= S(a, c);
}
auto qk = readints();
int q = qk[0], k = qk[1];
auto cost = bfs(k, n, adj);
for (int i = 0; i < q; i++) {
auto xy = readints();
int x = xy[0], y = xy[1];
auto ans = cost[x] + cost[y];
writeln(ans);
}
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;
immutable long MOD = 10^^9+7;
void main() {
auto s = readln.split.map!(to!long);
auto N = s[0];
auto M = s[1];
auto X = readln.split.map!(to!long).array;
auto Y = readln.split.map!(to!long).array;
auto DX = new long[](N-1);
auto DY = new long[](M-1);
foreach (i; 0..N-1) DX[i] = (X[i+1] - X[i]) % MOD;
foreach (i; 0..M-1) DY[i] = (Y[i+1] - Y[i]) % MOD;
long width = 0;
long left = 1;
long right = N - 1;
foreach (i; 0..N-1) {
width += left * right % MOD * DX[i] % MOD;
width %= MOD;
left += 1;
right -= 1;
}
long ans = 0;
long up = 1;
long down = M - 1;
foreach (i; 0..M-1) {
ans += up * down % MOD * width % MOD * DY[i] % MOD;
ans %= MOD;
up += 1;
down -= 1;
}
ans.writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.array;
void main()
{
ulong N = readln.split[0].to!ulong;
char[] S = readln.chomp.to!(char[]);
ulong K = readln.split[0].to!ulong;
foreach (i; 0..N) if (S[i] != S[K - 1]) S[i] = '*';
writeln(S.to!string);
}
|
D
|
import std.stdio, std.array, std.conv, std.string, std.algorithm, std.range;
void main() {
long n = readln.chomp.to!long;
long ans = 1;
for (long i = 1; i <= 20; i++) {
ans *= i;
}
ans.writeln;
}
|
D
|
import std.stdio, std.conv, std.string;
void main(){
int n=to!int(chomp(readln()));
string s, t;
s=chomp(readln());
t=chomp(readln());
if(n==1){
writeln(3);
return;
}
const mod=1_000_000_000+7;
bool y=s[0]==t[0];
long c=y? 3: 6;
int i=y? 1: 2;
while(i<n){
if(i+1<n){
if(s[i]==t[i]){
if(y){
(c*=2)%=mod;
}else{
// c*=1;
}
y=true;
i++;
}else{
if(y){
(c*=2)%=mod;
}else{
(c*=3)%=mod;
}
y=false;
i+=2;
}
}else{
if(y){
(c*=2)%=mod;
}else{
// c*=1;
}
break;
}
}
writeln(c);
}
/*
1 : 2 2 3
2 : 1 3 1
*/
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
void main(){
auto x = readln.chomp.to!int;
auto a = readln.chomp.to!int;
auto b = readln.chomp.to!int;
((x - a) % b).writeln;
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random;
import std.typecons, std.functional, std.traits,std.concurrency;
import std.algorithm, std.container;
import core.bitop, core.time, core.memory;
import std.bitmanip;
import std.regex;
enum INF = long.max/3;
enum MOD = 10L^^9+7;
//辞書順順列はiota(1,N),nextPermituionを使う
void end(T)(T v)
if(isIntegral!T||isSomeString!T||isSomeChar!T)
{
import core.stdc.stdlib;
writeln(v);
exit(0);
}
T[] scanArray(T = long)()
{
static char[] scanBuf;
readln(scanBuf);
return scanBuf.split.to!(T[]);
}
dchar scanChar()
{
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
return cast(dchar)c;
}
T scanElem(T = long)()
{
import core.stdc.stdlib;
static auto scanBuf = appender!(char[])([]);
scanBuf.clear;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
scanBuf ~= cast(char) c;
c = getchar;
}
return scanBuf.data.to!T;
}
dchar[] scanString(){
return scanElem!(dchar[]);
}
void main()
{
auto n=scanElem;
auto k=scanElem;
auto x=scanElem;
auto y=scanElem;
writeln(min(n,k)*x+max(0,n-k)*y);
}
|
D
|
import std.stdio, std.string, std.conv, std.bigint, std.typecons, std.algorithm, std.array, std.math, std.range;
void main() {
auto tmp = readln.split.to!(uint[]);
auto A = tmp[0];
auto B = tmp[1];
auto C = tmp[2];
auto D = tmp[3];
auto E = tmp[4];
auto F = tmp[5];
real rateMax = 0;
int waterMax = 0;
int sugarMax = 0;
foreach (a; 0..cast(int)(F / (100 * A))+1) {
foreach (b; 0..cast(int)((F - 100 * A * a) / (100 * B))+1) {
foreach (c; 0..cast(int)((F - 100 * (A * a + B * b)) / C)+1) {
foreach (d; 0..cast(int)((F - 100 * (A * a + B * b) - C * c) / D)+1) {
auto water = 100 * (A * a + B * b);
auto sugar = C * c + D * d;
if (100 * sugar > water * E) continue;
auto rate = cast(real)sugar / water;
if (rate <= rateMax) continue;
rateMax = rate;
waterMax = water;
sugarMax = sugar;
}
}
}
}
writeln(waterMax+sugarMax, " ", sugarMax);
}
|
D
|
import std.stdio;
import std.algorithm;
import std.array;
import std.conv;
import std.string;
void main() {
int N = readln.chomp.to!int;
auto c = readln.chomp.split.map!(to!int).array;
int Q = readln.chomp.to!int;
bool flag = true;
int[] apple = new int[](N);
for (int i = 0; i < Q; i++) {
auto operation = readln.chomp.split.map!(to!int).array;
int x = operation[1] - 1, d = operation[2];
if (!flag) {
continue;
}
if (operation[0] == 1) {
if (apple[x] + d <= c[x]) {
apple[x] += d;
}
else {
writeln(x + 1);
flag = false;
}
}
else {
if (apple[x] - d >= 0) {
apple[x] -= d;
}
else {
writeln(x + 1);
flag = false;
}
}
}
if (flag) {
0.writeln;
}
return;
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
const tmp = readln.split.to!(long[]);
const A = tmp[0], B = tmp[1], K = tmp[2];
int cnt;
foreach_reverse (x; 1..min(A,B)+1) {
if (A % x == 0 && B % x == 0) {
if (++cnt == K) {
writeln(x);
}
}
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
ulong[50] solve(ulong x)
{
ulong[50] r;
auto y = x;
size_t i;
while (y > 0) {
r[i] = x / 2^^(i+1) * 2^^i;
if (x % 2^^(i+1) >= 2^^i) r[i] += x % 2^^(i+1) + 1 - 2^^i;
++i;
y >>= 1;
}
return r;
}
void main()
{
auto ab = readln.split.to!(ulong[]);
auto A = ab[0];
auto B = ab[1];
if (A == 0) {
ulong r, j = 1;
foreach (i, x; solve(B)) {
if (x % 2 == 1) r |= (j<<i);
}
writeln(r);
return;
}
auto a = solve(A-1);
auto b = solve(B);
ulong r, j = 1;
foreach (i; 0..50) {
if ((b[i] - a[i]) % 2 == 1) {
r |= (j<<i);
}
}
writeln(r);
}
|
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 s = readln.strip;
writeln (s.maxElement);
}
}
|
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 A = scanElem;
long B = scanElem;
writeln((A-1)*(B-1));
}
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
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
string s = read!string;
string t = read!string;
int ans = 0;
for (int i = 0; i < 3; i++) {
if (s[i] == t[i]) ans++;
}
writeln(ans);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto S = RD!string;
long ans;
long cnt;
foreach (i; 0..N)
{
if (S[i] == 'I')
++cnt;
else
--cnt;
ans = max(ans, cnt);
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.conv, std.string, std.math;
void main()
{
int input = to!int(chomp(readln()));
int i = 1;
if(input==1) {
writeln(1);
return;
}
while (pow(i, 2) <= input) {
i++;
}
writeln(pow(i-1, 2));
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto rgbn = readln.split.to!(int[]);
auto R = rgbn[0];
auto G = rgbn[1];
auto B = rgbn[2];
auto N = rgbn[3];
int ret;
foreach (r; 0..3001) {
foreach (g; 0..3001) {
auto rest = N - R*r - G*g;
if (rest == 0 || (rest > 0 && rest%B == 0)) ++ret;
}
}
writeln(ret);
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int n, l, a;
rd(n, l, a);
auto s = new int[](n), t = new int[](n);
foreach (i; 0 .. n)
rd(s[i], t[i]);
long cnt = 0;
foreach (i; 1 .. n) {
cnt += (s[i] - (s[i - 1] + t[i - 1])) / a;
}
if (n > 0) {
cnt += (s[0]) / a;
cnt += (l - (s[$ - 1] + t[$ - 1])) / a;
} else {
cnt += l / a;
}
writeln(cnt);
}
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;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
void main()
{
auto s1 = split(readln());
bool flag;
foreach (ind; 0 .. to!int(s1[0]))
{
auto s2 = split(readln);
if (!s2.find("C").empty || !s2.find("M").empty || !s2.find("Y").empty)
{
flag = true;
break;
}
}
if (flag)
writeln("#Color");
else
writeln("#Black&White");
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }
long mod = 10^^9 + 7;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD!int;
auto s = RD!string;
auto m = N - 11 + 1;
long cnt;
foreach (i; 0..m)
{
if (s[i] == '8')
{
++cnt;
}
}
writeln(cnt > m / 2 ? "YES" : "NO");
stdout.flush();
debug readln();
}
|
D
|
// Code By H~$~C
module main;
import std.stdio, std.array, std.string, std.math, std.uni, std.format, std.bigint;
import std.algorithm, std.conv, std.container, std.range, std.functional;
import std.random, std.typecons;
// FIXME
int[] buildZ(in string s) {
int n = cast(int)(s.length);
int[] z; z.length = n;
for (int i = 1, l = 0, r = 0; i < n; ++i) {
if (i <= r) z[i] = (z[i - l] < r - i + 1 ? z[i - l] : r - i + 1);
while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i];
if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;
}
return z;
}
void solve(in int testcase) {
string s, t;
s.read;
int n = cast(int)(s.length), m;
for (int i = 0; i < n; ++i)
t ~= s[n - i - 1];
if (s == t) {
s.writeln;
return ;
}
int[] z = buildZ(s ~ '$' ~ t);
int x = z[n + 1];
string ans1 = s[0 .. x];
string ans2 = "";
int mx = 0;
string p = s[x .. n - x];
string o;
{
n = cast(int)(p.length), m = n * 2 + 1;
t = "";
for (int i = 0; i < n; ++i)
t ~= p[n - i - 1];
o = p ~ '$' ~ t;
z = buildZ(o);
for (int i = m - 1; i >= 0; --i) {
if (z[i] == m - i && mx < m - i) {
mx = m - i;
ans2 = o[i .. $];
}
}
}
p = t;
{
n = cast(int)(p.length), m = n * 2 + 1;
t = "";
for (int i = 0; i < n; ++i)
t ~= p[n - i - 1];
o = p ~ '$' ~ t;
z = buildZ(o);
for (int i = m - 1; i >= 0; --i) {
if (z[i] == m - i && mx < m - i) {
mx = m - i;
ans2 = o[i .. $];
}
}
}
string ans3;
for (int i = 0; i < x; ++i)
ans3 ~= ans1[x - i - 1];
(ans1 ~ ans2 ~ ans3).writeln;
}
// ************************************************************
T reader(T)() { return readToken().to!T; }
void read(T)(ref T t) { t = reader!T; }
T[] readarray(T)() { return readln.splitter.map!(to!T).array; }
void chmax(T)(ref T a, T b) { a = max(a, b); }
void chmin(T)(ref T a, T b) { a = min(a, b); }
alias less = (a, b) => a < b;
alias greater = (a, b) => a > b;
T min_element(T)(in T[] a) { return a.element!less; }
T max_element(T)(in T[] a) { return a.element!greater; }
alias unique = uniq;
// constant or some variables
immutable int inf = 0x3f3f3f3f;
immutable long lnf = 0x3f3f3f3f3f3f3f3f;
immutable typeof(null) NULL = null;
string[] _tokens;
// functions
T element(alias pred, T)(in T[] a) {
int pos = 0;
for (int i = 1; i < cast(int)(a.length); ++i)
if (binaryFun!pred(a[i], a[pos])) pos = i;
return a[pos];
}
string readToken() {
for (; _tokens.empty; ) {
if (stdin.eof) return "";
_tokens = readln.split;
}
auto token = _tokens.front;
_tokens.popFront;
return token;
}
int binary_bearch(alias pred, T)(in T[] as) {
int l = -1, h = cast(int)(as.length), mid;
for (; l + 1 < h; ) {
mid = (l + h) >> 1;
(unaryFun!pred(as[mid]) ? h : l) = mid;
}
return h;
}
int lower_bound(alias pred = less, T)(in T[] as, T val) {
return as.binary_bearch!(a => a == val || pred(val, a));
}
int upper_bound(alias pred = less, T)(in T[] as, T val) {
return as.binary_bearch!(a => pred(val, a));
}
void main(string[] args) {
int tests = 1;
tests.read();
foreach(test; 1 .. tests + 1) solve(test);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void main()
{
auto s = RD!string;
auto cnt = new long[](26);
auto ans = new long[][](26, 26);
foreach (c; s)
{
auto i = c-'a';
foreach (j; 0..26)
{
ans[i][j] += cnt[j];
}
++cnt[i];
}
long ans0 = cnt[cnt.MIN_POS!"a > b"];
foreach (i; 0..26)
{
foreach (j; 0..26)
{
ans0.chmax(ans[i][j]);
}
}
writeln(ans0);
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.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto T = RD!int;
auto ans = new long[](T);
foreach (i; 0..T)
{
auto n = RD!int;
auto a = RD!int;
auto b = RD!int;
auto s = RD!string;
auto dp = new long[][](n+1, 2);
dp[0][0] = b;
foreach (j; 0..n)
{
if (s[j] == '0')
{
if (dp[j][0] != 0)
{
dp[j+1][0] = dp[j][0] + a + b;
}
if (dp[j][1] != 0)
{
dp[j+1][1] = dp[j][1] + a + b*2;
if (dp[j][0] == 0)
dp[j+1][0] = dp[j][1] + a*2 + b*2;
}
}
else
{
if (dp[j][0] == 0)
{
dp[j+1][1] = dp[j][1] + a + b*2;
}
else
{
auto c1 = dp[j][1] == 0 ? long.max : dp[j][1] + a + b*2;
auto c2 = dp[j][0] + a*2 + b*2;
dp[j+1][1] = min(c1, c2);
}
}
}
ans[i] = dp[n][0];
}
foreach (e; ans)
writeln(e);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new int[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto m = RD!int;
auto a = RDA;
auto b = RDA;
bool[long] set;
foreach (i; 0..n)
{
set[a[i]] = true;
}
foreach (i; 0..m)
{
if (set.get(b[i], false))
{
++ans[ti];
}
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio;
import std.string;
void main ()
{
string s;
while ((s = readln.strip) != "")
{
long res = 0;
foreach (pos; 1..s.length + 1)
{
res += !(s[pos - 1] & 3);
if (pos > 1 && !((s[pos - 2] * 2 + s[pos - 1]) & 3))
{
res += pos - 1;
}
}
writeln (res);
}
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons;
immutable int inf = 10^^9 + 7;
int N, W;
int[] v, w;
void main() {
scan(N, W);
int vi, wi;
auto v = new int[](N);
auto w = new int[](N);
foreach (i ; 0 .. N) {
scan(vi, wi);
v[i] = vi;
w[i] = wi;
}
auto dp = new int[](N * 100 + 1);
dp[] = inf;
dp[0] = 0;
foreach (i ; 0 .. N) {
foreach_reverse (j ; v[i] .. N * 100 + 1) {
dp[j] = min(dp[j], dp[j - v[i]] + w[i]);
}
}
foreach_reverse (j ; 0 .. N * 100 + 1) {
if (dp[j] <= W) {
writeln(j);
return;
}
}
}
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);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.string, std.array, std.math;
void main()
{
const r = readln.chomp.to!int;
(r*r).writeln;
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
string s;
scan(s);
if (s.canFind("AC")) {
writeln("Yes");
}
else {
writeln("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.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
enum PS = [11, 31, 41, 61, 71, 101, 131, 151, 181, 191, 211, 241, 251, 271, 281, 311, 331, 401, 421, 431, 461, 491, 521, 541, 571, 601, 631, 641, 661, 691, 701, 751, 761, 811, 821, 881, 911, 941, 971, 991, 1021, 1031, 1051, 1061, 1091, 1151, 1171, 1181, 1201, 1231, 1291, 1301, 1321, 1361, 1381];
void main()
{
auto N = readln.chomp.to!int;
writeln(PS[0..N].to!(string[]).join(" "));
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
import std.math : abs;
void main() {
int n;
scan(n);
int t, x, y;
bool ok = 1;
foreach (i ; 0 .. n) {
int ti, xi, yi;
scan(ti, xi, yi);
int dt = ti - t;
int dist = abs(xi - x) + abs(yi - y);
if (dt < dist || dt % 2 != dist % 2) {
ok = 0;
}
t = ti, x = xi, y = yi;
}
writeln(ok ? "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, 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 a, b;
scan(a, b);
writeln(max(a + b, a - b, a * b));
}
int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new int[][](n, 0);
foreach (i; 0 .. m) {
int u, v;
scan(u, v);
if (is1indexed) {
u--, v--;
}
adj[u] ~= v;
if (isUndirected) {
adj[v] ~= u;
}
}
return adj;
}
alias Edge = Tuple!(int, "to", int, "cost");
Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new Edge[][](n, 0);
foreach (i; 0 .. m) {
int u, v, c;
scan(u, v, c);
if (is1indexed) {
u--, v--;
}
adj[u] ~= Edge(v, c);
if (isUndirected) {
adj[v] ~= Edge(u, c);
}
}
return adj;
}
void yes(bool b) {
writeln(b ? "Yes" : "No");
}
void YES(bool b) {
writeln(b ? "YES" : "NO");
}
T[] readArr(T)() {
return readln.split.to!(T[]);
}
T[] readArrByLines(T)(int n) {
return iota(n).map!(i => readln.chomp.to!T).array;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
// import 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 a = cin.next!int,
b = cin.next!int,
c = cin.next!int;
int x = cin.next!int;
int res;
foreach (i; 0 .. a + 1) {
foreach (j; 0 .. b + 1) {
foreach (k; 0 .. c + 1) {
res += i * 500 + j * 100 + 50 * k == x;
}
}
}
writeln(res);
}
|
D
|
import std.stdio;
import std.ascii;
import std.conv;
import std.string;
import std.algorithm;
import std.range;
import std.functional;
import std.math;
import core.bitop;
void main()
{
auto nm = readln.chomp.split.map!(to!long);
long n = nm[0];
long m = nm[1];
auto A = readln.chomp.split.map!(to!long).array;
long[long] t;
t[0] = 1;
long b;
foreach (a; A)
{
b += a;
b %= m;
long mod = b % m;
if (mod in t)
t[mod]++;
else
t[mod] = 1;
}
long result;
foreach (key, val; t)
{
result += (val).c(2);
}
writeln(result);
}
long c(long a, long b)
{
if (a < b)
{
return 0;
}
long c = 1;
foreach (i; a - b + 1 .. a + 1)
c *= i;
long d = 1;
foreach (i; 1 .. b + 1)
d *= i;
return c / d;
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void main()
{
string x, y; readV(x, y);
writeln(x > y ? ">" : x < y ? "<" : "=");
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
readln.chomp.count!(x=>x=='1').writeln;
}
|
D
|
import std.stdio, std.algorithm, std.range, std.conv, std.string;
import core.stdc.stdio;
// foreach, foreach_reverse, writeln
void main() {
int n;
scanf("%d", &n);
int[] a = new int[n];
int[] b = new int[n];
foreach (i; 0..n) scanf("%d", &a[i]);
foreach (i; 0..n) scanf("%d", &b[i]);
int ans = 0;
foreach (k; 0..29) {
int mask = (1<<k)-1;
int s = 0;
if (n&1) {
foreach (x; a) s ^= x>>k&1;
foreach (x; b) s ^= x>>k&1;
}
int[] c = new int[n];
c[] = b[]&mask;
sort(c);
auto sorted = assumeSorted(c);
foreach (x; a) {
x &= mask;
int l = n - to!int(sorted.lowerBound((1<<k)-x).length);
s ^= l&1;
}
ans |= s<<k;
}
writeln(ans);
}
|
D
|
// Vicfred
// https://atcoder.jp/contests/abc176/tasks/abc176_b
// implementation
import std.stdio;
import std.string;
void main() {
string n = readln.strip;
int sum = 0;
foreach(ch; n) {
sum += ch - '0';
sum %= 9;
}
if(sum == 0)
"Yes".writeln;
else
"No".writeln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto N = RD!long - 1L;
//foreach (N; 0..10L^^2)
//{
debug writeln("N:", N+1);
long x = 26;
long[] num;
while (N >= 0)
{
num ~= N % 26;
N -= x;
if (N < 0) break;
N /= 26;
}
debug writeln(num);
char[] ans;
foreach (i; 0..num.length)
{
ans ~= cast(char)('a'+num[i]);
}
ans.reverse();
writeln(ans);
stdout.flush;//}
debug readln;
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
void main()
{
auto ip = readln.split.to!(int[]), a = ip[0], b = ip[1];
int s = max(a+b, a-b, a*b);
writeln(s);
}
|
D
|
import std.stdio, std.array, std.conv, std.string, std.range, std.algorithm;
void main() {
long n = readln.strip.to!(long);
string s = readln.strip;
long l = 0, r = s.length - 1;
while (l < s.length && s[l] == '.') {
l++;
}
while (r >= 0 && s[r] == '#') {
r--;
}
r++;
long w = s[l..r].count('.');
long b = s[l..r].count('#');
long right = w;
long left = 0;
long m = left + right;
foreach (i; l..r) {
if (s[i] == '.') {
right--;
} else {
left++;
}
m = min(m, left + right);
}
writeln(m);
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;
immutable long MOD = 10^^9 + 7;
long powmod(long a, long x, long m) {
long ret = 1;
while (x) {
if (x % 2) ret = ret * a % m;
a = a * a % m;
x /= 2;
}
return ret;
}
void main() {
auto N = readln.chomp.to!int;
auto S = readln.chomp;
auto M = S.length.to!int;
auto mem = new long[][](N + 1, N + 1);
foreach (i; 0..N+1) fill(mem[i], -1);
// dp(i, j) : i回キーを叩いてj文字入力されている場合の数
long dp(int i, int j) {
if (i == 0 && j == 0) return 1;
if (i <= 0 || j < 0) return 0;
if (j > i) return 0;
if (mem[i][j] >= 0) return mem[i][j];
long ret = 0;
if (j == 0) {
ret += dp(i - 1, j);
ret %= MOD;
}
ret += dp(i - 1, j + 1); // Back Space
ret %= MOD;
ret += dp(i - 1, j - 1) * 2; // 0 or 1
ret %= MOD;
return mem[i][j] = ret;
}
auto ans = dp(N, M);
ans = ans * powmod(powmod(2, MOD-2, MOD), M, MOD) % MOD;
ans.writeln;
}
|
D
|
import std.stdio, std.typecons, std.algorithm;
alias Tuple!(int, "top", int, "bottom") Bundle;
const int INF = (1 << 24);
void main() {
int times; scanf("%d", ×);
int[][] dp = new int[][](times, times);
Bundle[] cards;
foreach(i; 0..times) {
int top, bottom; scanf("%d %d", &top, &bottom);
cards ~= Bundle(top, bottom);
dp[i][] = INF;
dp[i][i] = 0;
}
// dp[i][j] -> i 番目 から j 番目を計算する時の最適コスト
for(int len = 1; len < times; len++) {
for(int s = 0; s + len < times; s++) {
int f = s + len;
for(int i = s; i < f; i++) {
int cost =
(cards[s].top * cards[i].bottom) * (cards[i + 1].top * cards[f].bottom);
cost += dp[s][i] + dp[i + 1][f];
if (cost < dp[s][f]) dp[s][f] = cost;
}
}
}
writeln(dp[0][times - 1]);
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
string s; readV(s);
writeln(min(s.count('0'), s.count('1'))*2);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
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 (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;
immutable int n = r.next!uint;
immutable int m = r.next!uint;
auto p = uninitializedArray!(int[]) (n);
auto pos = uninitializedArray!(int[]) (n + 1);
foreach (i; 0 .. n) {
p[i] = r.next!uint;
pos[p[i]] = i;
}
auto e = new int[][n+1];
foreach (edgeid; 0 .. m) {
immutable i = r.next!uint;
immutable j = r.next!uint;
e[i] ~= j;
}
debug stderr.writeln (e);
int res;
auto d = new bool[n+1];
int l = 1;
foreach_reverse (i; 0 .. n - 1) {
int cnt;
immutable pi = p[i];
immutable pk = pos[pi];
foreach (j; e[pi]) {
if (!d[j] && pk < pos[j]) ++cnt;
}
debug stderr.writeln (cnt, ' ', l);
if (cnt == l) {
++res;
d[pi] = true;
} else {
++l;
}
}
write (res);
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
void main()
{
int[string] cnt;
string len;
auto str = readln.split;
foreach (s; str) {
if (s.length > len.length) {
len = s;
}
cnt[s]++;
}
string mode;
int mode_v;
foreach (key; cnt.keys) {
if (cnt[key] > mode_v) {
mode = key;
mode_v = cnt[key];
}
}
writeln(mode, " ", len);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string;
void main()
{
auto T = readln.chomp.split(" ")[1].to!long;
auto ts = readln.chomp.split(" ").map!(to!int);
long sum;
int last = 0;
foreach (t; ts[1..$]) {
if (t - last < T)
sum += (t - last);
else
sum += T;
last = t;
}
writeln(sum + T);
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
auto A = ["Christmas Eve Eve Eve", "Christmas Eve Eve", "Christmas Eve", "Christmas"];
writeln(A[lread() - 22]);
}
|
D
|
import std.stdio;
import std.array;
import std.conv;
import std.string;
import std.algorithm;
void main()
{
string s;
while( (s=readln.strip) != "0 0") {
auto n = map!(to!int)(s.strip.split);
int h = n[0], w = n[1];
writeln(replicate("#",w));
for(int i=1;i<h-1;i++) {
write("#");
write(replicate(".",w-2));
writeln("#");
}
if(h>1)
writeln(replicate("#",w));
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 N = RD;
long[2][] nodes;
auto edges = new long[2][][](N);
foreach (i; 0..N-1)
{
auto u = RD-1;
auto v = RD-1;
auto w = RD;
edges[u] ~= [v, w];
edges[v] ~= [u, w];
}
auto cost = new long[](N);
fill(cost, long.max);
cost[0] = 0;
long[2][] open = [[0, 0]];
while (!open.empty)
{
auto n = open.front; open.popFront;
auto from = n[0];
foreach (edge; edges[from])
{
auto to = edge[0];
auto c = n[1] + edge[1];
if (c < cost[to])
{
cost[to] = c;
open ~= [to, c];
}
}
}
auto ans = new long[](N);
foreach (i; 0..N)
{
ans[i] = cost[i] % 2;
}
foreach (e; ans)
writeln(e);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array,
std.functional, std.container, std.typecons;
void main() {
int N = readln.chomp.to!int;
int[] d = readln.split.to!(int[]);
int ans = 0;
foreach(i; 0..N) {
foreach(j; (1+i)..N) {
ans += d[i] * d[j];
}
}
ans.writeln;
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}}
void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}}
void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}}
void main()
{
int n; readV(n);
int[] a; readA(n, a);
writeln(a.map!(ai => calc(ai)).reduce!min);
}
auto calc(int n)
{
auto r = 0;
for (; n%2 == 0; n /= 2) ++r;
return r;
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n; rd(n);
auto d=new int[](n);
foreach(i; 0..n) rd(d[i]);
bool ok=true;
void f(){
int r=0;
foreach(int i, int e; d){
ok&=i*10<=r;
chmax(r, i*10+e);
}
}
f();
reverse(d);
f();
if(ok) writeln("yes");
else writeln("no");
}
void chmax(T)(ref T x, T y){
if(x<y) x=y;
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
import std.concurrency;
void times(alias fun)(int n) {
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
// fold was added in D 2.071.0.
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
return reduce!fun(tuple(seed), r);
}
}
}
void main() {
"aiueo".canFind(readln.chomp.to!char).pipe!(a => a?"vowel":"consonant").writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
import std.concurrency;
long MOD = 10L^^9+7;
long f(long x, long y) {
if (x==0 && y==0) return 1;
long res = 0;
if (x>=0 && y>=0) res = (res + memoize!f((x-0)>>1, (y-0)>>1))%MOD;
if (x>=1 && y>=1) res = (res + memoize!f((x-1)>>1, (y-1)>>1))%MOD;
if (x>=0 && y>=2) res = (res + memoize!f((x-0)>>1, (y-2)>>1))%MOD;
return res;
}
void main() {
readln.chomp.to!long.pipe!(N => f(N, N)).writeln;
}
// ----------------------------------------------
void times(alias fun)(int n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
return reduce!fun(tuple(seed), r);
}
}
}
}
// cumulativeFold was added in D 2.072.0
static if (__VERSION__ < 2072) {
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
}
// minElement/maxElement was added in D 2.072.0
static if (__VERSION__ < 2072) {
auto minElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto minimum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b < minimum) {
element = a;
minimum = b;
}
}
return element;
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto maximum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b > maximum) {
element = a;
maximum = b;
}
}
return element;
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
immutable int letters = 26;
string solve (string s)
{
auto n = s.length.to !(int);
if (n == letters)
{
auto t = s.dup;
if (nextPermutation (t.representation))
{
auto r = t.idup;
while (s < r[0..$ - 1])
{
r = r[0..$ - 1];
}
return r;
}
return "-1";
}
else
{
foreach (c; 'a'..'z' + 1)
{
if (!s.canFind (c))
{
return s ~ c.to !(char);
}
}
assert (false);
}
}
void main ()
{
string s;
while ((s = readln.strip) != "")
{
writeln (solve (s));
}
}
|
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) {
v = (v + n - 2) / n;
va ~= v;
}
auto wa = new int[](0);
wa ~= w;
while (w > 1) {
w = (w + n - 2) / n;
wa ~= w;
}
debug {
writeln(va);
writeln(wa);
}
int ans = 0;
foreach (vi ; va) {
foreach (wi ; wa) {
if (vi == wi) {
ans = max(ans, vi);
}
}
}
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.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!T;r.popFront;}}
void readM(T)(size_t r,size_t c,ref T[][]t){t=new T[][](r);foreach(ref v;t)readA(c,v);}
void main()
{
int h, w; readV(h, w);
int[][] c; readM(10, 10, c);
int[][] a; readM(h, w, a);
auto g = GraphM!()(10).init;
foreach (i; 0..10)
foreach (j; 0..10)
g[i][j] = c[i][j];
auto d = FloydWarshal!(typeof(g)).floydWarshal(g);
auto r = 0;
foreach (i; 0..h)
foreach (j; 0..w)
if (a[i][j] != -1)
r += d[a[i][j]][1];
writeln(r);
}
struct GraphM(W = int, W i = 10^^9)
{
alias Wt = W, inf = i;
int n;
Wt[][] g;
alias g this;
this(int n) { this.n = n; g = new Wt[][](n, n); }
ref auto init() { foreach (i; 0..n) { g[i][] = inf; g[i][i] = 0; } return this; }
}
template FloydWarshal(Graph)
{
import std.algorithm, std.array, std.traits;
alias Wt = TemplateArgsOf!Graph[0];
Wt[][] floydWarshal(ref Graph g)
{
Wt[][] dist;
int[][] inter;
floydWarshal(g, dist, inter);
return dist;
}
void floydWarshal(ref Graph g, out Wt[][] dist, out int[][] inter)
{
auto n = g.n, sent = n;
dist = g.g.map!(i => i.dup).array;
inter = new int[][](n, n);
foreach (i; 0..n) inter[i][] = sent;
foreach (k; 0..n)
foreach (i; 0..n)
foreach (j; 0..n)
if (dist[i][j] > dist[i][k] + dist[k][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
inter[i][j] = k;
}
}
}
|
D
|
import std.stdio,std.conv,std.string;
void main(){
auto n=readln.chomp.to!int;
(n%2==0?n/2:n/2+1).writeln;
}
|
D
|
import std.functional,
std.algorithm,
std.bigint,
std.string,
std.traits,
std.array,
std.range,
std.stdio,
std.conv;
void main() {
ulong[] NM = readln.chomp.split.to!(ulong[]);
ulong N = NM[0],
M = NM[1];
ulong x = 1900*M + 100*(N - M);
ulong p = 1;
foreach (_; 0..M) { p <<= 1; }
writeln(x*p);
}
|
D
|
import std.stdio, std.string, std.array, std.conv;
void insertionSort(int n, int[] x) {
foreach (i; 1 .. n) {
int v = x[i];
int j = i - 1;
while (j >= 0 && x[j] > v) {
x[j+1] = x[j];
--j;
}
x[j+1] = v;
foreach (k, y; x) {
y.write;
if (k != n - 1) " ".write;
else writeln;
}
}
}
void main() {
int n = readln.chomp.to!int;
int[] a = readln.chomp.split.to!(int[]);
foreach (x; a) {
x.write;
if (x != a[n-1]) " ".write;
else writeln;
}
insertionSort(n, a);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex;
void main(){
auto a=readln.split.to!(int[]),n=a[0],k=a[1];
if(n%k>0)writeln(1);
else if(n%k==0)writeln(0);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
auto a = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
void main() {
auto s = readln.strip;
foreach (i; 0 .. a.length) {
if (a[i] == s) {
writeln (7 - i);
break;
}
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nc = readln.split.to!(long[]);
auto N = nc[0];
auto C = nc[1];
long[] xs, vs;
foreach (_; 0..N) {
auto xv = readln.split.to!(long[]);
xs ~= xv[0];
vs ~= xv[1];
}
auto rs = new long[](N);
auto ls = new long[](N);
long m, s;
foreach (i; 0..N) {
s += vs[i];
rs[i] = max(i == 0 ? 0 : rs[i-1], s - xs[i]);
}
m = 0; s = 0;
foreach_reverse (i; 0..N) {
s += vs[i];
ls[i] = max(i == N-1 ? 0 : ls[i+1], s - C + xs[i]);
}
long r;
foreach (i; 0..N) {
r = max(r, rs[i]);
r = max(r, ls[i]);
if (i < N-1) r = max(r, rs[i] - xs[i] + ls[i+1]);
if (i > 0) r = max(r, ls[i] - C + xs[i] + rs[i-1]);
}
writeln(r);
}
|
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 S = readln.chomp;
auto T = readln.chomp;
auto N = S.length.to!int;
auto M = T.length.to!int;
auto next = new int[][](M, 26);
foreach (i; 0..M) fill(next[i], 1<<29);
foreach (i; 0..M) {
foreach_reverse (j; i..M) {
next[i][T[j]-'a'] = j;
}
}
auto dp = new int[][](N+1, M+1);
auto prevc = new dchar[][](N+1, M+1);
auto previ = new Tuple!(int, int)[][](N+1, M+1);
foreach (i; 0..N+1) fill(previ[i], tuple(-1, -1));
foreach (i; 0..N) {
foreach (j; 0..M) {
{
int nx = j;
int nv = dp[i][j];
if (nv > dp[i+1][nx]) {
dp[i+1][nx] = nv;
prevc[i+1][nx] = prevc[i][j];
previ[i+1][nx] = previ[i][j];
}
}
if (next[j][S[i]-'a'] != 1<<29) {
int nx = next[j][S[i]-'a'] + 1;
int nv = dp[i][j] + 1;
if (nv > dp[i+1][nx]) {
dp[i+1][nx] = nv;
prevc[i+1][nx] = S[i];
previ[i+1][nx] = tuple(i, j);
}
}
}
}
int mi = 0;
int mj = 0;
int ms = 0;
foreach (i; 0..N+1) {
foreach (j; 0..M+1) {
if (dp[i][j] > ms) {
ms = dp[i][j];
mi = i;
mj = j;
}
}
}
dchar[] ans;
while (previ[mi][mj][0] != -1) {
ans ~= prevc[mi][mj];
auto t = previ[mi][mj];
mi = t[0];
mj = t[1];
}
ans.reverse;
ans.writeln;
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.algorithm, std.typecons;
void main(){
alias Tuple!(int, int) Data;
int N, M;
auto input = readln.split.map!(to!int);
N = input[0], M = input[1];
int[] A = new int[N]; //?????¨
foreach (i; 0..N) {
A[i] = readln.chomp.to!int;
}
int[] vote = new int[N];
foreach (i; 0..M) {
int B = readln.chomp.to!int;
foreach (j; 0..N) {
if (A[j] <= B) {
vote[j]++;
break;
}
}
}
int max, idx;
foreach (i; 0..N) {
if (vote[i] > max) {
max = vote[i];
idx = i;
}
}
writeln(idx+1);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.numeric;
import std.stdio;
import std.string;
void main()
{
long n;
long sum;
n = readln.chomp.to!(long);
sum = 0;
for (long i = 1; i <= n; i++) {
for (long j = 1; j <= n; j++) {
for (long k = 1; k <= n; k++) {
sum += gcd(i, gcd(j, k));
}
}
}
writeln(sum);
}
|
D
|
import std.stdio;
import std.ascii;
import std.conv;
import std.string;
import std.algorithm;
import std.range;
import std.functional;
import std.math;
import core.bitop;
void main()
{
auto n = readln.chomp.to!long;
foreach (seven; 0 .. (n / 7 + 1))
{
if ((n - (7 * seven)) % 4 == 0)
{
"Yes".writeln;
return;
}
}
"No".writeln;
}
|
D
|
import std;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } bool DEBUG = 0;
void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ std.stdio.write(t, " "), print(a); }
string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
int l = scan!int;
E[] ans;
int i = 0;
while(l > 1){
if(l % 2) ans ~= E(i, -1, l - 1);
ans ~= E(i, i + 1, l / 2);
if(l > 1) ans ~= E(i, i + 1, 0);
l /= 2;
i += 1;
}
print(i + 1, ans.length);
foreach(an; ans){
print(an.u + 1, (an.v < 0)? (i + 1): (an.v + 1), an.w);
}
}
struct E{
int u, v, w;
}
|
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 h, w; readV(h, w);
string[] a; readC(h, a);
auto sh = new bool[](h), sw = new bool[](w);
foreach (i; 0..h)
if (a[i].all!(ai => ai == '.'))
sh[i] = true;
foreach (j; 0..w)
if (a.map!(ai => ai[j]).all!(aj => aj == '.'))
sw[j] = true;
foreach (i; 0..h) {
if (sh[i]) continue;
foreach (j; 0..w) {
if (sw[j]) continue;
write(a[i][j]);
}
writeln;
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
void main() {
int n;
scan(n);
auto s = readln.chomp;
int e, w;
foreach (i ; 0 .. n) {
if (s[i] == 'E') e++;
}
int ans = 1<<30;
foreach (i ; 0 .. n) {
if (ans > e + w) {
ans = e + w;
}
if (s[i] == 'E') e--;
if (s[i] == 'W') w++;
}
ans = min(ans, e + w);
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
void main()
{
long n, m;
rdVals(n, m);
Edge[][] edge = new Edge[][](n);
foreach (i; 0 .. m)
{
long l, r, d;
rdVals(l, r, d);
--l, --r;
edge[l] ~= Edge(r, d), edge[r] ~= Edge(l, -d);
}
bool[] seen = new bool[n];
long[] dist = new long[n];
bool ok = true;
void dfs(long idx)
{
if (seen[idx]) return;
seen[idx] = true;
foreach (e; edge[idx])
{
if (seen[e.to])
{
if (dist[e.to] != dist[idx] + e.dist) ok = false;
}
else
{
dist[e.to] = dist[idx] + e.dist;
dfs(e.to);
}
}
}
foreach (i; 0 .. n)
{
dfs(i);
}
writeln(ok ? "Yes" : "No");
}
struct Edge
{
long to, dist;
}
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, std.algorithm, std.conv, std.array, std.string;
void main()
{
auto str = readln.split(" ");
writeln(
str[0][$-1] == str[1][0] && str[1][$-1] == str[2][0] ? "YES" : "NO"
);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto i = RD;
writeln(N - i + 1);
stdout.flush();
debug readln();
}
|
D
|
void main()
{
long n = rdElem;
long[] a = rdRow;
bool minus;
bool zero;
long result;
long amin = 1L << 60;
foreach (x; a)
{
result += x.abs;
amin = min(amin, x.abs);
if (x < 0) minus ^= true;
if (x == 0) zero = true;
}
if (minus && !zero) result -= 2 * amin;
result.writeln;
}
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;
import std.array;
import std.string;
import std.conv;
import std.algorithm;
import std.typecons;
import std.range;
import std.random;
import std.math;
import std.container;
void main() {
auto input = readln.split.map!(to!int);
int W = input[0];
int H = input[1];
int N = input[2];
int[4] A = [0, W, 0, H];
foreach (i; 0..N) {
input = readln.split.map!(to!int);
if (input[2]== 1)
A[0] = max(A[0], input[0]);
else if (input[2]== 2)
A[1] = min(A[1], input[0]);
else if (input[2]== 3)
A[2] = max(A[2], input[1]);
else if (input[2]== 4)
A[3] = min(A[3], input[1]);
}
if (A[0] >= A[1] || A[2] >= A[3])
writeln(0);
else
writeln((A[1]-A[0])*(A[3]-A[2]));
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void main()
{
int n; readV(n);
int k; readV(k);
auto r = 10^^9;
foreach (i; 0..1<<n) {
auto d = 1;
foreach (j; 0..n)
if (i.bitTest(j)) d *= 2;
else d += k;
r = min(r, d);
}
writeln(r);
}
pragma(inline) {
pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; }
pure T bitSet(T)(T n, size_t i) { return n | (T(1) << i); }
pure T bitReset(T)(T n, size_t i) { return n & ~(T(1) << i); }
pure T bitComp(T)(T n, size_t i) { return n ^ (T(1) << i); }
pure T bitSet(T)(T n, size_t s, size_t e) { return n | ((T(1) << e) - 1) & ~((T(1) << s) - 1); }
pure T bitReset(T)(T n, size_t s, size_t e) { return n & (~((T(1) << e) - 1) | ((T(1) << s) - 1)); }
pure T bitComp(T)(T n, size_t s, size_t e) { return n ^ ((T(1) << e) - 1) & ~((T(1) << s) - 1); }
import core.bitop;
pure int bsf(T)(T n) { return core.bitop.bsf(ulong(n)); }
pure int bsr(T)(T n) { return core.bitop.bsr(ulong(n)); }
pure int popcnt(T)(T n) { return core.bitop.popcnt(ulong(n)); }
}
|
D
|
import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void log(A...)(A arg) {
stderr.writeln(arg);
}
int size(T)(in T s) {
return cast(int)s.length;
}
void main() {
int N; scanf("%d\n", &N);
auto T = readln.chomp.split(" ").map!(to!int).array;
auto A = readln.chomp.split(" ").map!(to!int).array;
auto fixed = new bool[N];
auto limit = new long[N];
int t = 0;
foreach (i; 0 .. N) {
int prev = t;
t = max(t, T[i]);
if (prev == t) {
limit[i] = t;
} else {
fixed[i] = true;
limit[i] = t;
}
}
int a = 0;
foreach_reverse(i; 0 .. N) {
int prev = a;
a = max(a, A[i]);
if (fixed[i]) {
if (limit[i] > a) {
writeln(0);
return;
}
}
if (prev == a) {
limit[i] = min(limit[i], a);
} else {
if (limit[i] < a) {
writeln(0);
return;
}
fixed[i] = true;
}
}
const long mod = cast(long)(1e9 + 7);
long ans = 1;
foreach (i; 0 .. N) {
if (fixed[i]) continue;
ans = (ans * limit[i]) % mod;
}
writeln(ans);
}
|
D
|
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math;
import core.stdc.stdio;
// foreach, foreach_reverse, writeln
class BIT {
int[] data;
this(int n) {
data = new int[n+1];
}
BIT dup() {
BIT ret = new BIT(to!int(data.length));
ret.data = data.dup;
return ret;
}
void add(int i, int x=1) {
i++;
while (i) {
data[i] += x;
i -= i&-i;
}
}
int sum(int i) {
int ret = 0;
i++;
while (i < data.length) {
ret += data[i];
i += i&-i;
}
return ret;
}
}
void main() {
int n;
scanf("%d\n", &n);
int[] b = new int[n];
int[] w = new int[n];
int m = n*2;
foreach (i; 0..m) {
char c;
int a;
scanf("%c %d\n", &c, &a);
if (c == 'B') {
b[a-1] = i;
} else {
w[a-1] = i;
}
}
int[][] dp = new int[][](n+1,n+1);
foreach (i; 0..n+1) dp[i][] = to!int(1e9);
dp[0][0] = 0;
BIT bit = new BIT(m);
foreach (i; 0..n+1) {
BIT _bit = bit.dup();
foreach (j; 0..n+1) {
if (i < n) dp[i+1][j] = min(dp[i+1][j], dp[i][j]+_bit.sum(b[i]));
if (j < n) dp[i][j+1] = min(dp[i][j+1], dp[i][j]+_bit.sum(w[j]));
if (j < n) _bit.add(w[j]);
}
if (i < n) bit.add(b[i]);
}
writeln(dp[n][n]);
}
|
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 M = 2 * N - 1;
auto A = readln.split.map!(to!int).array;
int hi = M + 1;
int lo = 0;
while (hi - lo > 1) {
int mid = (hi + lo) / 2;
auto B = A.map!(a => a >= mid).array;
int a = B[N-2], b = B[N-1], c = B[N];
if ((a & b) || (b & c)) {
lo = mid;
} else if ((!a & !b) || (!b & !c)) {
hi = mid;
} else {
bool ok = false;
for (int l = N - 3, r = N + 1; l >= 0; l--, r++) {
if (B[l] == B[l+1]) {
if (B[l]) lo = mid;
else hi = mid;
ok = true;
break;
} else if (B[r] == B[r-1]) {
if (B[r]) lo = mid;
else hi = mid;
ok = true;
break;
}
}
if (!ok) {
bool hoge = B[N-1];
if (M / 2 % 2) hoge ^= 1;
if (hoge) lo = mid;
else hi = mid;
}
}
}
lo.writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math;
void main()
{
auto it = readln.split.to!(int[]);
auto A = it[0];
auto B = it[1];
auto C = it[2];
auto X = it[3];
auto Y = it[4];
int cost;
if (A + B > C*2) {
if (X > Y) {
cost += 2 * Y * C;
X -= Y;
Y = 0;
} else {
cost += 2 * X * C;
Y -= X;
X = 0;
}
} else {
if (X > Y) {
cost += A * Y + B * Y;
X -= Y;
Y = 0;
} else {
cost += A * X + B * X;
Y -= X;
X = 0;
}
}
if (X > 0) {
if (A > C*2) {
cost += 2 * C * X;
} else {
cost += A * X;
}
}
if (Y > 0) {
if (B > C*2) {
cost += 2 * C * Y;
} else {
cost += B * Y;
}
}
writeln(cost);
}
|
D
|
import std.stdio, std.conv, std.string;
void main()
{
int N = to!int(chomp(readln()));
string str = to!string(N);
int count;
foreach(char chr; str) {
count += to!int(chr)-48;
}
if(N % count == 0) {
writeln("Yes");
} else writeln("No");
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
int n;
foreach (c; readln.chomp) {
if (c == '+') {
++n;
} else {
--n;
}
}
writeln(n);
}
|
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;
long cnt = 1, ans;
while (cnt < B)
{
cnt += A - 1;
++ans;
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio;
import std.array;
import std.string;
import std.conv;
import std.algorithm;
import std.typecons;
import std.range;
import std.random;
import std.math;
import std.container;
void main() {
auto S = readln.chomp;
char c = S[0];
int ans = 0;
foreach (s; S)
if (s != c) {
ans += 1;
c = s;
}
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)); }
double euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
double norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }
double dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new long[][](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto s = RD!string;
ans[ti] = [0, 0];
int i;
while (i < n-1)
{
if (s[i] == ')' && s[i+1] == '(')
{
int pos;
foreach (int j; i+2..n)
{
if (s[j] == ')')
{
pos = j+1;
break;
}
}
if (pos == 0) break;
i = pos;
++ans[ti][0];
}
else
{
i += 2;
++ans[ti][0];
}
}
ans[ti][1] = n - i;
}
foreach (e; ans)
writeln(e[0], " ", e[1]);
stdout.flush;
debug readln;
}
|
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!(string, "x", long, "y");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
long a, b;
scan(a, b);
if(a <= 8 && b <= 8)
writeln("Yay!");
else
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.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new long[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto a = RDA;
auto tot = a.sum;
auto rem = tot % n;
ans[ti] = rem * (n-rem);
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new int[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto m = RD!int;
auto a = new string[](n);
foreach (i; 0..n)
{
a[i] = RD!string;
}
foreach (i; 0..n)
{
if (a[i][$-1] == 'R')
++ans[ti];
}
foreach (i; 0..m)
{
if (a[$-1][i] == 'D')
++ans[ti];
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.array, std.conv, std.algorithm, std.string, std.numeric, std.range;
void main() {
string s;
while( (s=readln.strip).length != 0 ){
auto as = s.to!int;
if(as == 0) break;
int ans = 0;
while(0 < as){
ans += as/5;
as /= 5;
}
writeln(ans);
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
string s = read!string;
int n = 0;
foreach (c; s) {
if (c == 'o') n++;
}
auto m = 15 - s.length;
if (n + m >= 8) writeln("YES");
else writeln("NO");
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
int calc(int[] a) {
double agv = 1.0 * a.sum / a.length;
double best = int.max;
int ans = 0;
for (int i = 0; i < a.length; i++) {
double d = abs(agv - a[i]);
if (d < best) {
best = d;
ans = i;
}
}
return ans;
}
void main() {
readint;
auto a = readints;
writeln(calc(a));
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.