code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import core.bitop;
void main ()
{
int n;
while (scanf (" %d", &n) > 0)
{
bool s = false;
foreach (i; 0..n)
{
foreach (j; 0..n)
{
int t;
scanf (" %d", &t);
if (i == j)
{
if (t)
{
s ^= true;
}
}
}
}
int q;
scanf (" %d", &q);
string res;
foreach (k; 0..q)
{
int t, i;
scanf (" %d", &t);
switch (t)
{
case 1:
case 2:
scanf (" %d", &i);
s ^= true;
break;
case 3:
res ~= s ? '1' : '0';
break;
default:
assert (false);
}
}
writeln (res);
}
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n; rd(n);
auto a=new int[](n);
foreach(i; 0..n) rd(a[i]), a[i]--;
int cur=0, cnt=0;
foreach(_; 0..n+1){
cur=a[cur];
cnt++;
if(cur==1){
writeln(cnt);
return;
}
}
writeln(-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.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
import std.concurrency;
void times(alias fun)(int n) {
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
void main() {
2.rep!(() => readln).back.chomp.map!"a=='x'".array.pipe!(
ary => 4.iota.map!(
i => [i%2==0, i/2==0]
).find!(
seed => ary.fold!(
(a, b) => [a.back, a.front^a.back^b]
)(seed).equal(seed)
).pipe!(
res => res.empty ? "-1" : res.front.pipe!(
seed => ary.cumulativeFold!(
(a, b) => [a.back, a.front^a.back^b]
)(seed).map!front
).map!"a?'W':'S'".array
)
).writeln;
}
// ----------------------------------------------
// 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.stdio;
import std.string;
import std.array; // split
import std.conv; // to
void main()
{
string s1 = chomp(readln());
string s2 = chomp(readln());
string s3 = chomp(readln());
int a = to!int(s1); // 第0要素を整数に変換
int b = to!int(s2); // 第1要素を整数に変換
int c = to!int(s3); // 第1要素を整数に変換
writeln((a+b)*c/2); // 表示
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
enum P = 10L^^9+7;
void main()
{
auto N = readln.chomp.to!int;
auto DP = new long[][][][](4, 4, 4, N);
DP[0][0][0][0] = 1;
DP[0][0][1][0] = 1;
DP[0][0][2][0] = 1;
DP[0][0][3][0] = 1;
foreach (i; 1..N) {
foreach (a; 0..4) foreach (b; 0..4) foreach (c; 0..4) foreach (x; 0..4) {
if (i >= 2 && x == 1 && c == 2 && b == 0) continue;
if (i >= 2 && x == 1 && c == 0 && b == 2) continue;
if (i >= 2 && x == 2 && c == 1 && b == 0) continue;
if (i >= 3 && x == 1 && c == 2 && a == 0) continue;
if (i >= 3 && x == 1 && b == 2 && a == 0) continue;
(DP[b][c][x][i] += DP[a][b][c][i-1]) %= P;
}
}
long r;
foreach (a; 0..4) foreach (b; 0..4) foreach (c; 0..4) {
(r += DP[a][b][c][N-1]) %= P;
}
writeln(r);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math;
void main()
{
auto N = readln.chomp.to!int;
auto AS = readln.split.to!(int[]);
if (N == 1) {
writeln("0");
return;
} else if (N == 2) {
writeln(AS[0] == AS[1] ? "1" : "0");
return;
}
int cnt;
for (size_t i = 1; i < N-1; ++i) {
if (AS[i-1] == AS[i]) {
++cnt;
AS[i] = AS[i+1] + 1;
}
}
if (AS[$-2] == AS[$-1]) ++cnt;
writeln(cnt);
}
|
D
|
void main(){
import std.stdio, std.algorithm;
long n, a, b, k; rd(n, a, b, k);
const long mod=998244353;
const int M=1_000_00*4;
long powmod(long a, long x, long mod){
if(x==0) return 1;
else if(x==1) return a;
else if(x&1) return a*powmod(a, x-1, mod)%mod;
else return powmod(a*a%mod, x/2, mod);
}
long[] genFact(int n, long mod){
auto fact=new long[](n);
fact[0]=fact[1]=1;
foreach(i; 2..n) fact[i]=i*fact[i-1]%mod;
return fact;
}
long[] genInv(long[] arr, long mod){
auto inv=new long[](arr.length);
foreach(i, elem; arr) inv[i]=powmod(arr[i], mod-2, mod);
return inv;
}
auto fact=genFact(M, mod);
auto invFact=genInv(fact, mod);
long comb(long nn, long rr){
if(nn<rr) return 0;
long ret=fact[nn]%mod;
(ret*=invFact[rr])%=mod;
(ret*=invFact[nn-rr])%=mod;
return ret;
}
long tot=0;
for(long x=0; x<=n; x++)if(k-a*x>=0){
if((k-a*x)%b==0){
long y=(k-a*x)/b;
if(y>n) continue;
(tot+=comb(n, x)*comb(n, y)%mod)%=mod;
}
}
writeln(tot);
}
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.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*800-(n/15)*200);
}
|
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 t = read.to!int;
foreach(_; 0 .. t){
long a = read.to!long;
long b = read.to!long;
long n = read.to!long;
if(n % 3 == 0) a.writeln;
if(n % 3 == 1) b.writeln;
if(n % 3 == 2) (a ^b).writeln;
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
readln.split.to!(int[]).reduce!"(a-1)*(b-1)".writeln;
}
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, std.algorithm;
import std.range, std.array, std.container, std.math, std.typecons;
immutable int mod = 10^^9 + 7;
immutable int inf = 20000;
long s;
void main() {
s = readln.chomp.to!long;
solve(s).writeln;
}
string solve(long s) {
long top = s, btm = 0, mid;
while (top - btm > 1) {
mid = btm + (top - btm) / 2;
if (calc(mid) < s) {
btm = mid;
}
else {
top = mid;
}
}
debug {
writeln("btm:", btm);
}
int dif = cast(int)(s - calc(btm) - 1);
string ans;
long i = btm + 1;
while (ans.length < 20 + dif) {
if (i % 15 == 0) ans ~= "FizzBuzz";
else if (i % 3 == 0) ans ~= "Fizz";
else if (i % 5 == 0) ans ~= "Buzz";
else ans ~= i.to!string;
i++;
}
ans = ans.drop(dif).to!string;
ans = ans[0 .. 20];
return ans;
}
unittest {
assert(solve(1) == "12Fizz4BuzzFizz78Fiz");
assert(solve(20) == "zzBuzz11Fizz1314Fizz");
//stderr.writeln(solve(10000000000));
assert(solve(10000000000) == "93FizzBuzz1418650796");
}
long calc(long n) {
if (n == 0) return 0L;
static long[] nonfz = new long[](21);
static bool fc = true;
if (fc) {
foreach (i ; 1 .. 21) {
nonfz[i] = countnonfz(10L^^i - 1) - countnonfz(10L^^(i - 1));
if (i == 1) nonfz[i]++;
}
debug {
writeln("nonfz:", nonfz);
}
fc = false;
}
long res = (n / 3 + n / 5) * 4;
int keta = calcdig(n);
foreach (i ; 1 .. keta) {
res += nonfz[i] * i;
}
res += (countnonfz(n) - countnonfz(10L^^(keta - 1) - 1)) * keta;
return res;
}
unittest {
assert(calc(4) == 7);
assert(calc(10) == 25);
assert(calc(2) == 2);
assert(calc(100) == 313);
assert(calc(1000) == 3673);
assert(calc(983) == 3609);
}
int calcdig(long n) {
return n > 0 ? calcdig(n / 10) + 1 : 0;
}
unittest {
assert(calcdig(8) == 1);
assert(calcdig(132) == 3);
assert(calcdig(101010101010) == 12);
}
long countnonfz(long n) {
return n - n / 3 - n / 5 + n / 15;
}
unittest {
assert(countnonfz(10) == 5);
assert(countnonfz(3) == 2);
assert(countnonfz(100) == 53);
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.array;
void main(){
auto A = readln.chomp.to!(int);
auto B = readln.chomp.to!(int);
auto C = readln.chomp.to!(int);
auto D = readln.chomp.to!(int);
auto E = readln.chomp.to!(int);
int ans;
if(A<0){
ans += -A*C;
A=0;
}
if(A==0){
ans += D;
}
if(A>=0){
ans += (B-A)*E;
}
writeln(ans);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto hw = readln.split.to!(int[]);
auto H = hw[0];
auto W = hw[1];
int[][] AS, BS;
AS.length = H;
BS.length = H;
foreach (i; 0..H) {
AS[i] = readln.split.to!(int[]);
}
foreach (i; 0..H) {
BS[i] = readln.split.to!(int[]);
}
auto DP = new int[][][](H, W, 80*80+1);
foreach_reverse (h; 0..H) {
foreach_reverse (w; 0..W) {
foreach (d; 0..80*80+1) {
auto e = abs(AS[h][w] - BS[h][w]);
auto dpe = d + e;
auto dme = abs(d - e);
if (h+1 == H && w+1 == W) {
DP[h][w][d] = dme;
continue;
}
auto r = int.max;
if (h+1 < H) {
if (dpe <= 80*80) r = min(r, DP[h+1][w][dpe]);
if (dme <= 80*80) r = min(r, DP[h+1][w][dme]);
}
if (w+1 < W) {
if (dpe <= 80*80) r = min(r, DP[h][w+1][dpe]);
if (dme <= 80*80) r = min(r, DP[h][w+1][dme]);
}
DP[h][w][d] = r;
}
}
}
writeln(DP[0][0][0]);
}
|
D
|
import std.stdio;
import std.string;
import std.array;
import std.conv;
bool isRightTriangle(int a, int b, int c) {
int a2 = a * a;
int b2 = b * b;
int c2 = c * c;
return a2 + b2 == c2 || b2 + c2 == a2 || c2 + a2 == b2;
}
void main() {
uint n = to!uint(chomp(readln()));
int[][] data;
for(uint i = 0; i < n; i++) {
auto input = split(readln());
data ~= [to!int(input[0]), to!int(input[1]), to!int(input[2])];
}
foreach(tri; data) {
if(isRightTriangle(tri[0], tri[1], tri[2])) writeln("YES");
else writeln("NO");
}
}
|
D
|
import std.stdio, std.string, std.conv;
void main() {
int[] tmp = readln.split.to!(int[]);
int n = tmp[0], d = tmp[1];
writeln((n + 2 * d) / (2 * d + 1));
}
|
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,
std.typecons;
// }}}
// 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 n = cin.next!int,
a = cin.next!int;
if (n % 500 <= a) {
writeln("Yes");
} else {
writeln("No");
}
}
|
D
|
void main() {
string[] tmp = readln.split('/');
string y = "2018", m = tmp[1], d = tmp[2];
writeln(y, '/', m, '/', d);
}
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 core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
long A, B, C;
scan(A, B, C);
if (A == B)
{
C.writeln();
}
else if (A == C)
{
B.writeln();
}
else
{
A.writeln();
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
int[10^^5] AS;
void main()
{
auto N = readln.chomp.to!int;
long r;
foreach (i; 0..N) {
auto A = readln.chomp.to!long;
if (i != 0 && A > 0 && AS[i-1] > 0) {
--A;
++r;
}
r += A/2;
if (A%2 == 1) AS[i] = 1;
}
writeln(r);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container;
void main()
{
auto nk = readln.split.to!(long[]);
auto N = nk[0];
auto K = nk[1];
writeln(N <= K ? 1 : 0);
}
|
D
|
import std.stdio, std.conv, std.string;
import std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
int DEBUG_LEVEL = 0;
void print()(){ writeln(""); }
void print(T, A ...)(T t, lazy A a){ write(t), print(a); }
void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); }
void main(string[] args){
if(args.length > 1 && args[1] == "-debug"){
if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int;
else DEBUG_LEVEL = 1;
}
int n = read.to!int;
int k = read.to!int - 1;
char[] s = readln.chomp.to!(char[]);
if(s[k] == 'A') s[k] = 'a';
if(s[k] == 'B') s[k] = 'b';
if(s[k] == 'C') s[k] = 'c';
s.writeln;
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dunkelheit" version=">=0.9.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
int main() {
Scanner sc = new Scanner(stdin);
long a, b, c, k;
sc.read(a, b, c, k);
if (k % 2 == 0) {
writeln(a-b);
} else {
writeln(b-a);
}
return 0;
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
void read(bool enforceEOF = false, T, Args...)(ref T x, auto ref Args args) {
import std.exception;
enforce(readSingle(x));
static if (args.length == 0) {
enforce(enforceEOF == false || !succ());
} else {
read!enforceEOF(args);
}
}
void read(bool enforceEOF = false, Args...)(auto ref Args args) {
import std.exception;
static if (args.length == 0) {
enforce(enforceEOF == false || !succ());
} else {
enforce(readSingle(args[0]));
read!enforceEOF(args);
}
}
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/foundation.d */
// module dkh.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.math;
void main()
{
while(true)
{
int n = readln.chomp.to!int;
if(!n)break;
int y = readln.chomp.to!int;
real max = 0;
int maxBank;
foreach(_; 0 .. n)
{
auto inp = readln.split.map!(to!int);
int b = inp[0],
r = inp[1],
t = inp[2];
real sum;
final switch(t)
{
case 1:
sum = (1 + y * r / 100.0);
break;
case 2:
sum = (1 + r / 100.0) ^^ y;
break;
}
if(sum > max)
{
max = sum;
maxBank = b;
}
}
maxBank.writeln;
}
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
ulong MOD = 1_000_000_007;
ulong INF = 1_000_000_000_000;
alias SugarWater = Tuple!(long, "swater", long, "sugar");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
long n, a, b, c;
scan(n, a, b, c);
auto takematsu = only(n, a, b, c);
auto l = new long[](n);
foreach (ref e; l)
e = lread();
long mincost = INF;
foreach (i; 0 .. (1 << n * 2))
{
auto len = new long[](4);
auto cost = new long[](4);
auto fbits = i.fourbitary(n);
foreach (j, e; fbits)
{
len[e] += l[j];
cost[e] += 10;
}
if (!(len[1] * len[2] * len[3]))
continue;
cost[] -= 10;
foreach(j; iota(len.length))
cost[j] += abs(len[j] - takematsu[j]);
mincost = min(cost.sum - cost[0], mincost);
}
mincost.writeln();
}
auto fourbitary(long n, long disit)
{
auto fbits = new long[](disit);
long tmp;
while (n > 0)
{
fbits[tmp++] = n % 4;
n = n >> 2;
}
return fbits;
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}}
void main()
{
int n, m; readV(n, m);
int[] l, r; readC(n, l, r);
auto k = new int[][](m+1);
foreach (i; 0..n) k[r[i]-l[i]+1] ~= l[i];
auto ft = new FenwickTree!int(m+1), t = n;
foreach (d; 1..m+1) {
auto s = t;
for (auto i = d; i <= m; i += d) s += ft[0..i+1];
writeln(s);
foreach (ki; k[d]) {
++ft[ki];
--ft[ki+d];
}
t -= k[d].length;
}
}
class FenwickTree(T)
{
const size_t n;
T[] buf;
this(size_t n)
{
this.n = n;
this.buf = new T[](n+1);
}
void opIndexOpAssign(string op)(T val, size_t i) if (op == "+" || op == "-")
{
++i;
for (; i <= n; i += i & -i) mixin("buf[i] " ~ op ~ "= val;");
}
void opIndexUnary(string op)(size_t i) if (op == "++" || op == "--")
{
++i;
for (; i <= n; i += i & -i) mixin("buf[i]" ~ op ~ ";");
}
pure T opSlice(size_t r, size_t l) { return get(l) - get(r); }
pure T opIndex(size_t i) { return opSlice(i, i+1); }
pure size_t opDollar() { return n; }
private:
pure T get(size_t i)
{
auto s = T(0);
for (; i > 0; i -= i & -i) s += buf[i];
return s;
}
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
auto n = lread();
auto s = sread();
auto t = sread();
// writeln(s);
// writeln(t);
long ans = -1;
foreach (i; 0 .. n + 1)
{
if (func(s, t, i))
{
// writeln(s[($ - i) .. $], t[0 .. i]);
ans = max(ans, i);
}
}
// writeln(ans);
writeln((s.length + t.length) - ans);
}
bool func(string s, string t, long x)
{
if (s[($ - x) .. $] == t[0 .. x])
{
return true;
}
else
{
return false;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
|
D
|
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto n = readln.strip.to !(int);
auto a = readln.splitter.map !(to !(int)).array;
writeln (n * 3);
for (int i = 1; i <= n; i += 2)
{
writeln (1, " ", i, " ", i + 1);
writeln (2, " ", i, " ", i + 1);
writeln (1, " ", i, " ", i + 1);
writeln (1, " ", i, " ", i + 1);
writeln (2, " ", i, " ", i + 1);
writeln (1, " ", i, " ", i + 1);
}
}
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
alias route = Tuple!(long, "From", long, "To");
long bignum = 1_000_000_007;
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
long n, k;
scan(n, k);
if (k > 1)
writeln(n - k);
else
writeln(0);
}
|
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 = RD!string;
auto b = RD!string;
auto used = new bool[](n);
foreach (i; 0..n)
{
if (b[i] == '0') continue;
if (a[i] == '0')
{
++ans[ti];
continue;
}
if (i != 0)
{
if (used[i-1] == false && a[i-1] == '1')
{
used[i-1] = true;
++ans[ti];
continue;
}
}
if (i != n-1)
{
if (used[i+1] == false && a[i+1] == '1')
{
used[i+1] = true;
++ans[ti];
continue;
}
}
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;
import std.numeric;
void main()
{
int n, k;
readln.chomp.split.tie(n, k);
long cnt = 0;
debug {
int ans = 0;
/+
for (int a = 1; a <= n; ++a) {
for (int b = 1; b <= n; ++b) {
if (a % b >= k) {
verbose(a, b);
++ans;
}
}
}
+/
for (int b = 1; b <= n; ++b) {
for (int a = b + 1; a <= n; ++a) {
if (a % b >= k) {
verbose(a, b, "...", a % b);
++ans;
}
}
}
for (int a = 1; a <= n; ++a) {
for (int b = a + 1; b <= n; ++b) {
if (a % b >= k) {
verbose(a, b, "...", a % b);
++ans;
}
}
}
verbose("ans", ans);
}
// 上 (a <= b)
for (int a = max(k, 1); a <= n; ++a) {
cnt += n - a;
}
debug verbose("ue", cnt);
// 下 (a > b)
for (int b = k + 1; b <= n; ++b) {
if (k == 0) {
cnt += n - b + 1;
} else {
debug verbose(b);
for (int d = 0; d < b - k; ++d) {
if (n < k + b + d) {
break;
}
int v = (n - (k + b + d)) / b + 1;
debug verbose("\t", d, v);
int w = v - 1;
w *= b;
int x = n - w - k - b;
debug verbose("\t\tx = ", x, n - (k + b + x));
x = min(x, b - k - 1);
debug verbose("\t\tx' = ", x);
cnt += (x - d + 1) * v;
d = x;
}
}
}
cnt.writeln;
}
void tie(R, Args...)(R arr, ref Args args)
if (isRandomAccessRange!R || isArray!R)
in
{
assert (arr.length == args.length);
}
body
{
foreach (i, ref v; args) {
alias T = typeof(v);
v = arr[i].to!T;
}
}
void verbose(Args...)(in Args args)
{
stderr.write("[");
foreach (i, ref v; args) {
if (i) stderr.write(", ");
stderr.write(v);
}
stderr.writeln("]");
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
bool calc(int a, int b, int c) {
return min(a, b) < c && c < max(a, b);
}
void main() {
auto abc = readints;
int a = abc[0];
int b = abc[1];
int c = abc[2];
writeln(calc(a, b, c) ? "Yes" : "No");
}
|
D
|
string solve(string s){
return s.replace(',',' ');
}
void main(){
string s = readln().chomp();
solve(s).writeln();
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range;
const long mod = 10^^9+7;
// 1要素のみの入力
T inelm(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] inln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split())ln ~= elm.to!T();
return ln;
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto s = readln.split.map!(to!long);
auto N = s[0];
auto Q = s[1];
while (Q--) {
s = readln.split.map!(to!long);
auto r = s[0];
auto c = s[1];
if ((r + c) % 2 == 0) {
long odd = r / 2;
long even = r - 1 - odd;
long n = odd * (N / 2 + N % 2) + even * (N / 2);
writeln(n + (c - 1) / 2 + 1);
} else {
long odd = r / 2;
long even = r - 1 - odd;
long n = odd * (N / 2) + even * (N / 2 + N % 2);
n += N * N / 2 + N % 2;
writeln(n + (c - 1) / 2 + 1);
}
}
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "l ", long, "r");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
long a, b;
scan(a, b);
writeln((a * b) % 2 ? "Odd" : "Even");
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
import std.datetime, std.bigint;
immutable inf = 101101101;
int n, m;
int[][] adj;
void main() {
scan(n, m);
adj = new int[][](n, 0);
int ai, bi;
foreach (i ; 0 .. m) {
scan(ai, bi);
ai--;
bi--;
adj[ai] ~= bi;
adj[bi] ~= ai;
}
auto d = new int[](n);
d[] = inf;
d[0] = 0;
auto q = DList!int();
q.insertBack(0);
while (!q.empty) {
auto u = q.front;
q.removeFront();
if (u == n - 1) break;
foreach (v ; adj[u]) {
if (d[v] == inf) {
d[v] = d[u] + 1;
q.insertBack(v);
}
}
}
writeln(d[n - 1] == 2 ? "POSSIBLE" : "IMPOSSIBLE");
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
const mx=1_000_00+5;
auto isPrime=new bool[](mx);
auto sub=new int[](mx);
isPrime[2]=true;
for(int i=3; i<=1_000_00; i+=2){
for(int j=3; j*j<=i; j+=2){
if(i%j==0) goto hell;
}
isPrime[i]=true;
if(isPrime[(i+1)/2]) sub[i]=1;
hell:;
}
foreach(i; 1..mx) sub[i]+=sub[i-1];
int q; rd(q);
while(q--){
int l, r; rd(l, r);
writeln(sub[r]-sub[l-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));
}
}
void wr(T...)(T x){
import std.stdio;
foreach(e; x) write(e, " ");
writeln();
}
|
D
|
import std.algorithm, std.array, std.container, std.range, std.bitmanip;
import std.numeric, std.math, std.bigint, std.random, core.bitop;
import std.string, std.conv, std.stdio, std.typecons;
void main()
{
auto n = readln.chomp.to!int;
bool include3(int x) {
for (; x > 0; x /= 10)
if (x % 10 == 3) return true;
return false;
}
foreach (i; 1..n + 1)
if (i % 3 == 0 || include3(i)) write(" ", i);
writeln;
}
|
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)); }
bool solve(int[] as) {
const n = cast(int)(as.length);
const cnt1 = as.count(1);
if (cnt1 == 0) {
return false;
}
if (cnt1 == n) {
return true;
}
foreach (i; 0 .. n - 2 + 1) {
if (as[i] >= 1 && as[i + 1] >= 1) {
return true;
}
}
foreach (i; 0 .. n - 3 + 1) {
if (as[i] >= 1 && as[i + 2] >= 1) {
return true;
}
}
return false;
}
void main() {
debug {
enum lim = 8;
foreach (n; 1 .. lim + 1) {
auto graph = new int[][3^^n];
foreach (u; 0 .. 3^^n) {
auto as = new int[n];
foreach (i; 0 .. n) {
as[i] = u / 3^^i % 3;
}
foreach (i; 0 .. n) foreach (j; i + 1 .. n + 1) {
auto bs = as[i .. j].dup;
bs.sort;
auto cs = as.dup;
cs[i .. j] = bs[($ + 1) / 2 - 1];
int v;
foreach_reverse (k; 0 .. n) {
v = v * 3 + cs[k];
}
graph[v] ~= u;
}
}
int start;
foreach_reverse (i; 0 .. n) {
start = start * 3 + 1;
}
DList!int que;
auto vis = new bool[3^^n];
vis[start] = true;
que ~= start;
for (; !que.empty; ) {
const u = que.front;
que.removeFront;
foreach (v; graph[u]) {
if (!vis[v]) {
vis[v] = true;
que ~= v;
}
}
}
foreach (u; 0 .. 3^^n) {
auto as= new int[n];
foreach (i; 0 .. n) {
as[i] = u / 3^^i % 3;
}
const res = solve(as);
if (vis[u] != res) {
foreach (i; 0 .. n) {
write(as[i]);
}
writeln();
writeln(vis[u], " ", res);
assert(false);
}
}
}
}
try {
for (; ; ) {
const numCases = readInt();
foreach (caseId; 0 .. numCases) {
const N = readInt();
const K = readInt();
auto A = new int[N];
foreach (i; 0 .. N) {
A[i] = readInt();
}
auto ss = new int[N];
foreach (i; 0 .. N) {
ss[i] = (A[i] < K) ? 0 : (A[i] == K) ? 1 : 2;
}
const ans = solve(ss);
writeln(ans ? "yes" : "no");
}
}
} catch (EOFException e) {
}
}
|
D
|
import std;
void main() {
readln;
auto S = readln.split[0].to!string;
auto N = S.length;
size_t[][char] rgb;
rgb['R'] = [], rgb['G'] = [], rgb['B'] = [];
foreach (n, c; S) {
rgb[c] ~= n;
}
size_t ans;
ans += rgb['R'].length * rgb['G'].length * rgb['B'].length;
// j - i = k - j
foreach (i; 0 .. N-2) {
foreach (k; iota(i+2, N, 2)) {
auto j = (k+i)/2;
bool[char] set;
set[S[i]] = true; set[S[j]] = true; set[S[k]] = true;
if (set.keys.length == 3) ans--;
}
}
writeln(ans);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
int[10^^5] CS;
void main()
{
auto nq = readln.split.to!(int[]);
auto N = nq[0];
auto Q = nq[1];
auto S = readln.chomp.to!(char[]);
auto pre = 'T';
foreach (i, c; S) {
if (i) CS[i] = CS[i-1];
if (pre == 'A' && c == 'C') ++CS[i];
pre = c;
}
foreach (_; 0..Q) {
auto lr = readln.split.to!(int[]);
auto l = lr[0]-1;
auto r = lr[1]-1;
writeln(CS[r] - CS[l]);
}
}
|
D
|
import std.stdio,
std.string,
std.conv,
std.algorithm;
void main() {
int[] a;
for (int i = 0; i < 4; i++) {
a ~= readln.chomp.to!(int);
}
writeln(min(a[0], a[1]) + min(a[2], a[3]));
}
|
D
|
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container, std.typecons;
import std.numeric, std.math;
import core.bitop;
T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T)(in string str, T fix) { 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 mod = pow(10, 9) + 7;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
struct UnionFind
{
void init(long n) { par = new long[](n); foreach (i; 0..n) par[i] = i; }
long root(long i) { return par[i] == i ? i : (par[i] = root(par[i])); }
bool same(long i, long j) { return root(i) == root(j); }
void unite(long i, long j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; }
long[] par;
}
long[] dijkstra(long from, long to, long[][] edges)
{
long[] path;
auto cost = new long[](edges.length); fill(cost, long.max); cost[from] = 0;
long[][] open = [[from]];
while (!open.empty)
{
auto n = open.front; open.popFront;
auto p = n[0];
foreach (i; 0..edges.length)
{
if (edges[p][i] == -1) continue;
auto c = cost[p] + edges[p][i];
if (c < cost[to] && c < cost[i]) { cost[i] = c; if (i == to) path = i ~ n; else open ~= i ~ n; }
}
}
return cost[to] ~ path;
}
void main()
{
auto N = RD!long;
auto S1 = RD!string;
auto S2 = RD!string;
long ans = 1;
long last;
foreach (i; 0..N)
{
if (S1[i] == S2[i])
{
if (last == 0)
modm(ans, 3);
else if (last == 1)
modm(ans, 2);
last = 1;
}
else
{
if (last == 0)
modm(ans, 6);
else if (last == 1)
modm(ans, 2);
else if (last == 3)
modm(ans, 3);
if (last == 2)
++last;
else
last = 2;
}
}
writeln(ans);
stdout.flush();
}
|
D
|
void main() {
problem();
}
void problem() {
const N = scan!int;
const M = scan!int;
const X = scan!int;
auto C = new int[N];
int[][] A;
foreach(i; 0..N) {
C[i] = scan!int;
A ~= scan!int(M);
}
int[] bitNums = 12.iota.map!(x => 2.pow(x)).array;
void solve() {
int answer = int.max;
foreach(combinationNumber; 0..2.pow(N)) {
auto comb = bitNums.map!(b => (combinationNumber & b) == b);
int price;
auto manabi = new int[M];
foreach(i; 0..N) {
if (comb[i]) continue;
price += C[i];
foreach(a; 0..M) manabi[a] += A[i][a];
}
if (manabi.any!(m => m < X)) continue;
if (answer > price) answer = price;
}
writeln(answer == int.max ? -1 : answer);
}
solve();
}
// ----------------------------------------------
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
// -----------------------------------------------
|
D
|
import std.stdio;
import std.string;
import std.format;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.concurrency;
import std.traits;
import std.uni;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;
enum long INF = long.max/3;
enum long MOD = 10L^^9+7;
void main() {
long N, P;
scanln(N, P);
long _P = P;
if (N == 1) {
P.writeln;
return;
}
if (P == 1) {
1.writeln;
return;
}
long[] primes = getPrimes(10L^^7);
long[] nums = new long[primes.length];
foreach(i, p; primes) {
while(P%p == 0) {
P/=p;
nums[i]++;
}
}
long ans = 1;
foreach(i, n; nums) {
ans *= primes[i] ^^ n.floor(N);
}
ans.writeln;
}
long[] getPrimes(long limit) {
bool[] isPrimes = new bool[limit+1];
isPrimes[2..$] = true;
for (long i=2; i*i<=isPrimes.length; i++) {
if (isPrimes[i]) {
for (long j=i*i; j<isPrimes.length; j+=i) {
isPrimes[j] = false;
}
}
}
long[] primes = [];
foreach (long i, flg; isPrimes) {
if (flg) primes ~= i;
}
return primes;
}
// ----------------------------------------------
void times(alias fun)(long n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(long n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
// `(x+y-1)/y` will only work for positive numbers ...
T t = x / y;
if (t * y < x) t++;
return t;
}
T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
T t = x / y;
if (t * y > x) t--;
return t;
}
ref T ch(alias fun, T, S...)(ref T lhs, S rhs) {
return lhs = fun(lhs, rhs);
}
unittest {
long x = 1000;
x.ch!min(2000);
assert(x == 1000);
x.ch!min(3, 2, 1);
assert(x == 1);
x.ch!max(100).ch!min(1000); // clamp
assert(x == 100);
x.ch!max(0).ch!min(10); // clamp
assert(x == 10);
}
mixin template Constructor() {
import std.traits : FieldNameTuple;
this(Args...)(Args args) {
// static foreach(i, v; args) {
foreach(i, v; args) {
mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v;
}
}
}
void scanln(Args...)(auto ref Args args) {
import std.meta;
template getFormat(T) {
static if (isIntegral!T) {
enum getFormat = "%d";
} else static if (isFloatingPoint!T) {
enum getFormat = "%g";
} else static if (isSomeString!T || isSomeChar!T) {
enum getFormat = "%s";
} else {
static assert(false);
}
}
enum string fmt = [staticMap!(getFormat, Args)].join(" ");
string[] inputs = readln.chomp.split;
foreach(i, ref v; args) {
v = inputs[i].to!(Args[i]);
}
}
// 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) {
private auto extremum(alias map, alias selector = "a < b", Range)(Range r)
if (isInputRange!Range && !isInfinite!Range &&
is(typeof(unaryFun!map(ElementType!(Range).init))))
in
{
assert(!r.empty, "r is an empty range");
}
body
{
alias Element = ElementType!Range;
Unqual!Element seed = r.front;
r.popFront();
return extremum!(map, selector)(r, seed);
}
private auto extremum(alias map, alias selector = "a < b", Range,
RangeElementType = ElementType!Range)
(Range r, RangeElementType seedElement)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void) &&
is(typeof(unaryFun!map(ElementType!(Range).init))))
{
alias mapFun = unaryFun!map;
alias selectorFun = binaryFun!selector;
alias Element = ElementType!Range;
alias CommonElement = CommonType!(Element, RangeElementType);
Unqual!CommonElement extremeElement = seedElement;
alias MapType = Unqual!(typeof(mapFun(CommonElement.init)));
MapType extremeElementMapped = mapFun(extremeElement);
// direct access via a random access range is faster
static if (isRandomAccessRange!Range)
{
foreach (const i; 0 .. r.length)
{
MapType mapElement = mapFun(r[i]);
if (selectorFun(mapElement, extremeElementMapped))
{
extremeElement = r[i];
extremeElementMapped = mapElement;
}
}
}
else
{
while (!r.empty)
{
MapType mapElement = mapFun(r.front);
if (selectorFun(mapElement, extremeElementMapped))
{
extremeElement = r.front;
extremeElementMapped = mapElement;
}
r.popFront();
}
}
return extremeElement;
}
private auto extremum(alias selector = "a < b", Range)(Range r)
if (isInputRange!Range && !isInfinite!Range &&
!is(typeof(unaryFun!selector(ElementType!(Range).init))))
{
alias Element = ElementType!Range;
Unqual!Element seed = r.front;
r.popFront();
return extremum!selector(r, seed);
}
private auto extremum(alias selector = "a < b", Range,
RangeElementType = ElementType!Range)
(Range r, RangeElementType seedElement)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void) &&
!is(typeof(unaryFun!selector(ElementType!(Range).init))))
{
alias Element = ElementType!Range;
alias CommonElement = CommonType!(Element, RangeElementType);
Unqual!CommonElement extremeElement = seedElement;
alias selectorFun = binaryFun!selector;
// direct access via a random access range is faster
static if (isRandomAccessRange!Range)
{
foreach (const i; 0 .. r.length)
{
if (selectorFun(r[i], extremeElement))
{
extremeElement = r[i];
}
}
}
else
{
while (!r.empty)
{
if (selectorFun(r.front, extremeElement))
{
extremeElement = r.front;
}
r.popFront();
}
}
return extremeElement;
}
auto minElement(Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum(r);
}
auto minElement(alias map, Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!map(r, seed);
}
auto minElement(Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum(r, seed);
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum!(map, "a > b")(r);
}
auto maxElement(Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum!`a > b`(r);
}
auto maxElement(alias map, Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!(map, "a > b")(r, seed);
}
auto maxElement(Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!`a > b`(r, seed);
}
}
// popcnt with ulongs was added in D 2.071.0
static if (__VERSION__ < 2071) {
ulong popcnt(ulong x) {
x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L);
x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L);
x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL);
x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL);
x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL);
x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL);
return x;
}
}
|
D
|
// Vicfred
// https://atcoder.jp/contests/abc176/tasks/abc176_c
// greedy
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;
void main() {
long n = readln.chomp.to!long;
long[] a = readln.split.map!(to!long).array;
long last = a[0];
long ans = 0;
for(int i = 1; i < n; i++)
if(a[i] < last)
ans += last - a[i];
else
last = a[i];
ans.writeln;
}
|
D
|
import std.algorithm, std.array, std.conv, std.stdio, std.string;
void main()
{
readln;
auto l = readln.chomp;
l.count("ABC").writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.array;
void main() {
string s;
string op = "+";
int res = 0;
while(1) {
s = readln();
int a = to!int(chomp(s));
if(op[0] == '+')
res += a;
else if(op[0] == '-')
res -= a;
else if(op[0] == '*')
res *= a;
else
res /= a;
op = readln();
if(op[0] == '=')
break;
}
writeln(res);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto hmk = readln.split.to!(int[]);
auto h1 = hmk[0];
auto m1 = hmk[1];
auto h2 = hmk[2];
auto m2 = hmk[3];
auto K = hmk[4];
auto s = h1 * 60 + m1;
auto e = h2 * 60 + m2;
auto r = e - K - s;
writeln(r);
}
|
D
|
import std.stdio, std.string, std.array, std.conv;
void main() {
int[] a = readln.chomp.split.to!(int[]);
if (a[0] < a[1]) {
"a < b".writeln;
} else if (a[0] > a[1]) {
"a > b".writeln;
} else {
"a == b".writeln;
}
}
|
D
|
import std.stdio;
import std.string;
import std.array;
import std.conv;
uint figure(uint num) {
if(num == 0) return 0;
else return figure(num / 10) + 1;
}
void main() {
string[][] inputs;
while(true) {
auto s = readln();
if(stdin.eof()) break;
else inputs ~= split(s);
}
foreach(input; inputs) {
uint a = to!uint(input[0]);
uint b = to!uint(input[1]);
writeln(figure(a + b));
}
}
|
D
|
import std;
void calc(long x) {
foreach (i; -200L..200L) {
foreach (j; -200L..200L) {
long y = i^^5 - j^^5;
if (x == y) {
writeln(i, " ", j);
return;
}
}
}
}
void main() {
long x; scan(x);
calc(x);
}
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
|
import std.stdio, std.algorithm, std.conv, std.array, std.string;
void main()
{
auto n = readln.chomp.to!int;
auto as = readln.split.to!(int[]);
int even_cnt;
int quart_cnt;
foreach (a; as) {
if (!(a&1)) {
++even_cnt;
if (!(a&2))
++quart_cnt;
}
}
if (quart_cnt >= (n - even_cnt) || (even_cnt == quart_cnt && (n - quart_cnt * 2) == 1))
writeln("Yes");
else
writeln("No");
}
|
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 X = lread();
auto a = X == 7 || X == 5 || X == 3;
writeln(a ? "YES" : "NO");
}
|
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, std.typecons;
// }}}
// nep.scanner {{{
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string : chomp;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.chomp.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
}
// }}}
void main()
{
auto cin = new Scanner;
char[] s;
cin.scan(s);
foreach (i; 0 .. 1 << s.length - 1)
{
int sum = s.front - '0';
char[] op;
foreach (j; 0 .. s.length - 1)
{
if ((i >> j) & 1)
{
op ~= '+';
sum += s[j + 1] - '0';
}
else
{
op ~= '-';
sum -= s[j + 1] - '0';
}
}
if (sum == 7)
{
s ~= '7';
op ~= '=';
foreach (j; 0 .. 9)
{
if (j & 1)
{
write(op[j / 2]);
}
else
{
write(s[j / 2]);
}
}
writeln;
break;
}
}
}
|
D
|
import std.stdio, std.algorithm, std.string, std.conv, std.array, std.range, std.math;
int read() { return readln.chomp.to!int; }
int[] reads() { return readln.split.to!(int[]); }
void main() {
readln;
writeln(reads.map!(i => i=i-1).sum);
}
|
D
|
import std.stdio, std.string, std.conv;
void main() {
int n = readln.chomp.to!int;
int[] a = [1, 5, 10, 25];
int coin = 0;
foreach_reverse (x; a) {
coin += n / x;
n %= x;
}
coin.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 a, b;
scan(a, b);
int n = b - a;
int ans = n * (n - 1) / 2 - a;
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main() {
string[] inputs = split(readln());
bool a = inputs[0] == "H";
bool b = inputs[1] == "H";
if(a == b) 'H'.writeln;
else 'D'.writeln;
}
|
D
|
import std;
// 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 arywrite(T=long)(T[] ary){ary.map!(text).join(' ').writeln;}
void scan(TList...)(ref TList Args){auto line = readln.split;
foreach (i,T;TList){T val=line[i].to!(T);Args[i]=val;}}
void dprint(TList...)(TList Args){debug{auto s=new string[](TList.length);
static foreach(i,a;Args) s[i]=text(a);s.map!(text).join(' ').writeln;}}
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 A = aryread();
long ans;
auto diff = new long[](N);
foreach (i; 0 .. N)
diff[i] = (i + 1) - A[i];
auto l = new long[][](2 * 10 ^^ 5 + 1);
foreach (i; 0 .. N)
if (diff[i] < l.length)
l[diff[i]] ~= i;
foreach (i; 0 .. N)
{
if (A[i] + i + 1 < l.length)
ans += assumeSorted(l[A[i] + i + 1]).upperBound(i).length;
}
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;
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() {
for (int i = 0; i < 3; i++) {
write(readln[i]);
}
writeln();
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
string s;
s = readln.chomp;
auto cnt = new int[](26);
foreach (ch ; s) {
cnt[ch - 'a']++;
}
int odd;
foreach (i ; 0 .. 26) {
if (cnt[i] & 1) odd++;
}
if (odd > 1) {
writeln(-1);
return;
}
auto c = new int[](26);
auto d = new int[][](26, 0);
auto b = new int[](s.length);
int mae = 0;
foreach (i, ch ; s) {
int j = ch - 'a';
if ((cnt[j] & 1) && c[j] == cnt[j] / 2) {
b[i] = s.length.to!int /2;
d[j] ~= b[i];
c[j]++;
continue;
}
if (c[j] < cnt[j] / 2) {
b[i] = mae++;
d[j] ~= b[i];
}
else {
b[i] = s.length.to!int - 1 - d[j][cnt[j] - 1 - c[j]];
}
c[j]++;
}
debug {
writeln(b);
}
auto ft = FenwickTree!(int)(s.length.to!int);
long ans;
foreach_reverse (bi ; b) {
ft.add(bi, 1);
ans += ft.sum(bi);
}
writeln(ans);
}
struct FenwickTree(T) {
private {
int _size;
T[] _data;
}
this(int N) {
_size = N;
_data = new T[](_size + 1);
}
void add(int i, T x) {
i++;
while (i <= _size) {
_data[i] += x;
i += i & (-i);
}
}
T sum(int r) {
T res = 0;
while (r > 0) {
res += _data[r];
r -= r & (-r);
}
return res;
}
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.complex;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
auto readInts() {
return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
return readInts()[0];
}
auto readLongs() {
return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
return readLongs()[0];
}
void readlnTo(T...)(ref T t) {
auto s = readln().split();
assert(s.length == t.length);
foreach(ref ti; t) {
ti = s[0].to!(typeof(ti));
s = s[1..$];
}
}
const real eps = 1e-10;
void main(){
auto a = readLongs();
auto ai = a[0];
auto ao = a[1];
auto aj = a[3];
auto al = a[4];
long nOdd;
long ans;
foreach(ax; [ai, aj, al]) {
if(ax % 2 == 1) {
nOdd += 1;
}
}
if(nOdd >= 2 && ai > 0 && aj > 0 && al > 0) {
ans += 3;
--ai;
--aj;
--al;
}
ans += (ai/2)*2 + (aj/2)*2 + (al/2)*2 + ao;
writeln(ans);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto S = readln.chomp;
auto DP = new long[](2019);
DP[0] = 1;
int n, d = 1;
foreach_reverse (c; S) {
n = ((c - '0').to!int * d + n) % 2019;
(d *= 10) %= 2019;
DP[n] += 1;
}
long r;
foreach (x; DP) r += x * (x-1) / 2;
writeln(r);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto M = RD;
auto cnt = new long[](N);
auto red = new bool[](N);
cnt[] = 1;
red[0] = true;
foreach (i; 0..M)
{
auto x = RD-1;
auto y = RD-1;
red[y] |= red[x];
--cnt[x];
++cnt[y];
if (cnt[x] == 0)
red[x] = false;
}
long ans;
foreach (i; 0..N)
{
if (red[i])
++ans;
}
writeln(ans);
stdout.flush();
debug readln();
}
|
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 times(alias pred)(int n) {
foreach(i; 0..n) pred();
}
auto rep(alias pred, T = typeof(pred()))(int n) {
T[] res = new T[n];
foreach(ref e; res) e = pred();
return res;
}
void main() {
int N = readln.chomp.to!int;
int[] ary = readln.split.to!(int[]).map!"a-1".array;
int ans = 0;
foreach(i, a; ary) {
if (i == ary[a]) ans++;
}
writeln(ans/2);
}
|
D
|
void main(){
int r, g, b;
scanf("%d %d %d", &r, &g, &b);
( (100*r+10*g+b)%4?"NO":"YES").writeln();
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range;
const long mod = 10^^9+7;
// 1要素のみの入力
T inelm(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] inln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split())ln ~= elm.to!T();
return ln;
}
|
D
|
// import chie template :) {{{
import std.stdio,
std.algorithm,
std.array,
std.string,
std.math,
std.conv,
std.range,
std.container,
std.bigint,
std.ascii,
std.typecons;
// }}}
// 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 n = cin.next!int;
int[dchar] c;
foreach (i; 0 .. n) {
dchar s = cin.next!dchar;
c[s]++;
}
writeln(c.length == 3 ? "Three" : "Four");
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.math;
void main() {
char[] s = readln.chomp.to!(char[]);
int k; scan(k);
foreach (i ; 0 .. s.length.to!int - 1) {
if (s[i] == 'a') continue;
int t = 26 - (s[i] - 'a');
if (t <= k) {
k -= t;
s[i] = 'a';
}
}
k %= 26;
s[$ - 1] = (s[$ - 1] - 'a' + k) % 26 + 'a';
writeln(s);
}
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.typecons;
import std.algorithm, std.array, std.range, std.container;
import std.math;
void main() {
auto N = readln.split[0].to!int;
auto H = readln.split.to!(int[]);
int ans;
foreach (i, height; H) {
bool flag = true;
foreach (h; H[0..i]) if (h > height) flag = false;
if (flag) ++ans;
}
writeln(ans);
}
|
D
|
/+ dub.sdl:
name "E"
dependency "dunkelheit" version=">=0.9.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
int main() {
Scanner sc = new Scanner(stdin);
scope(exit) assert(!sc.hasNext);
int n;
sc.read(n);
long[] a = new long[n], b = new long[n];
foreach (i; 0..n) {
sc.read(a[i], b[i]);
}
if (equal(a, b)) {
writeln(0);
return 0;
}
long ma = 10L^^15;
foreach (i; 0..n) {
if (a[i] > b[i]) ma = min(ma, b[i]);
}
writeln(a.sum - ma);
return 0;
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */
// module dkh.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
void read(Args...)(auto ref Args args) {
import std.exception;
static if (args.length != 0) {
enforce(readSingle(args[0]));
read(args[1..$]);
}
}
bool hasNext() {
return succ();
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container;
void main()
{
auto nk = readln.split.to!(long[]);
auto N = nk[0];
auto K = nk[1];
writeln(N <= K ? 1 : 0);
}
|
D
|
import std.stdio;
import std.string;
import std.format;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.concurrency;
import std.traits;
import std.uni;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;
enum long INF = long.max/3;
enum long MOD = 10L^^9+7;
void main() {
long N;
scanln(N);
string str = readln.chomp;
long Q;
scanln(Q);
long[] ks = readln.split.to!(long[]);
long[] ds = new long[N+1];
long[] ms = new long[N+1];
long[] cs = new long[N+1];
foreach(i; 0..N) {
ds[i+1] += ds[i];
ms[i+1] += ms[i];
cs[i+1] += cs[i];
if (str[i] == 'D') ds[i+1]++;
if (str[i] == 'M') ms[i+1]++;
if (str[i] == 'C') cs[i+1]++;
}
foreach(k; ks) {
long s = 0;
long[] xs = new long[N+1];
foreach(i; 0..N) {
xs[i+1] += xs[i];
if (str[i] == 'C') {
xs[i+1] += ds[max(0, i-k+1)];
}
}
foreach(i; 0..N) {
if (str[i] == 'M') {
s += (cs[min(N-1, i+k-1) + 1] - cs[i]) * ds[i+1] - (xs[min(N-1, i+k-1) + 1] - xs[i]);
}
}
s.writeln;
}
}
// ----------------------------------------------
void times(alias fun)(long n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(long n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
// `(x+y-1)/y` will only work for positive numbers ...
T t = x / y;
if (t * y < x) t++;
return t;
}
T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
T t = x / y;
if (t * y > x) t--;
return t;
}
ref T ch(alias fun, T, S...)(ref T lhs, S rhs) {
return lhs = fun(lhs, rhs);
}
unittest {
long x = 1000;
x.ch!min(2000);
assert(x == 1000);
x.ch!min(3, 2, 1);
assert(x == 1);
x.ch!max(100).ch!min(1000); // clamp
assert(x == 100);
x.ch!max(0).ch!min(10); // clamp
assert(x == 10);
}
mixin template Constructor() {
import std.traits : FieldNameTuple;
this(Args...)(Args args) {
// static foreach(i, v; args) {
foreach(i, v; args) {
mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v;
}
}
}
void scanln(Args...)(auto ref Args args) {
import std.meta;
template getFormat(T) {
static if (isIntegral!T) {
enum getFormat = "%d";
} else static if (isFloatingPoint!T) {
enum getFormat = "%g";
} else static if (isSomeString!T || isSomeChar!T) {
enum getFormat = "%s";
} else {
static assert(false);
}
}
enum string fmt = [staticMap!(getFormat, Args)].join(" ");
string[] inputs = readln.chomp.split;
foreach(i, ref v; args) {
v = inputs[i].to!(Args[i]);
}
}
// 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) {
private auto extremum(alias map, alias selector = "a < b", Range)(Range r)
if (isInputRange!Range && !isInfinite!Range &&
is(typeof(unaryFun!map(ElementType!(Range).init))))
in
{
assert(!r.empty, "r is an empty range");
}
body
{
alias Element = ElementType!Range;
Unqual!Element seed = r.front;
r.popFront();
return extremum!(map, selector)(r, seed);
}
private auto extremum(alias map, alias selector = "a < b", Range,
RangeElementType = ElementType!Range)
(Range r, RangeElementType seedElement)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void) &&
is(typeof(unaryFun!map(ElementType!(Range).init))))
{
alias mapFun = unaryFun!map;
alias selectorFun = binaryFun!selector;
alias Element = ElementType!Range;
alias CommonElement = CommonType!(Element, RangeElementType);
Unqual!CommonElement extremeElement = seedElement;
alias MapType = Unqual!(typeof(mapFun(CommonElement.init)));
MapType extremeElementMapped = mapFun(extremeElement);
// direct access via a random access range is faster
static if (isRandomAccessRange!Range)
{
foreach (const i; 0 .. r.length)
{
MapType mapElement = mapFun(r[i]);
if (selectorFun(mapElement, extremeElementMapped))
{
extremeElement = r[i];
extremeElementMapped = mapElement;
}
}
}
else
{
while (!r.empty)
{
MapType mapElement = mapFun(r.front);
if (selectorFun(mapElement, extremeElementMapped))
{
extremeElement = r.front;
extremeElementMapped = mapElement;
}
r.popFront();
}
}
return extremeElement;
}
private auto extremum(alias selector = "a < b", Range)(Range r)
if (isInputRange!Range && !isInfinite!Range &&
!is(typeof(unaryFun!selector(ElementType!(Range).init))))
{
alias Element = ElementType!Range;
Unqual!Element seed = r.front;
r.popFront();
return extremum!selector(r, seed);
}
private auto extremum(alias selector = "a < b", Range,
RangeElementType = ElementType!Range)
(Range r, RangeElementType seedElement)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void) &&
!is(typeof(unaryFun!selector(ElementType!(Range).init))))
{
alias Element = ElementType!Range;
alias CommonElement = CommonType!(Element, RangeElementType);
Unqual!CommonElement extremeElement = seedElement;
alias selectorFun = binaryFun!selector;
// direct access via a random access range is faster
static if (isRandomAccessRange!Range)
{
foreach (const i; 0 .. r.length)
{
if (selectorFun(r[i], extremeElement))
{
extremeElement = r[i];
}
}
}
else
{
while (!r.empty)
{
if (selectorFun(r.front, extremeElement))
{
extremeElement = r.front;
}
r.popFront();
}
}
return extremeElement;
}
auto minElement(Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum(r);
}
auto minElement(alias map, Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!map(r, seed);
}
auto minElement(Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum(r, seed);
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum!(map, "a > b")(r);
}
auto maxElement(Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum!`a > b`(r);
}
auto maxElement(alias map, Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!(map, "a > b")(r, seed);
}
auto maxElement(Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!`a > b`(r, seed);
}
}
// popcnt with ulongs was added in D 2.071.0
static if (__VERSION__ < 2071) {
ulong popcnt(ulong x) {
x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L);
x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L);
x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL);
x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL);
x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL);
x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL);
return x;
}
}
|
D
|
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;
int[][][] MEMO;
MEMO.length = N;
foreach (i; 0..N) {
auto A = readln.chomp.to!int;
MEMO[i].length = A;
foreach (j; 0..A) {
auto xy = readln.split.to!(int[]);
xy[0] -= 1;
MEMO[i][j] = xy;
}
}
int r;
foreach (s; 0..(1<<N)) {
int c;
foreach (j; 0..N) if (s & (1<<j)) {
++c;
foreach (xy; MEMO[j]) {
auto x = xy[0];
auto y = xy[1];
if (!!(s & (1<<x)) != !!y) goto ng;
}
}
r = max(r, c);
ng:
}
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.stdio;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto M = s[1];
auto ok = new bool[](N);
auto p = new int[](N);
auto ans = 0;
auto pena = 0;
foreach (_; 0..M) {
auto t = readln.split;
auto a = t[0].to!int - 1;
auto b = t[1];
if (ok[a]) continue;
if (b == "WA") {
p[a] += 1;
} else {
ans += 1;
pena += p[a];
ok[a] = true;
}
}
writeln(ans, " ", pena);
}
|
D
|
/+ dub.sdl:
name "B"
dependency "dcomp" version=">=0.7.4"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
int main() {
Scanner sc = new Scanner(stdin);
int[3] cnt;
string s;
sc.read(s);
int n = s.length.to!int;
foreach (c; s) cnt[c - 'a']++;
bool solve(int a, int b) {
int[] cnt2 = cnt.dup;
foreach (i; 0..n) {
int c = 0;
if (c == a || c == b) c++;
if (c == a || c == b) c++;
if (cnt2[c] == 0) return false;
cnt2[c]--;
a = b;
b = c;
}
cnt = cnt2;
return true;
}
foreach (i; 0..3) foreach (j; 0..3) {
if (solve(i, j)) {
writeln("YES");
return 0;
}
}
writeln("NO");
return 0;
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
// import dcomp.array;
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 {
FastAppender!(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 /Users/yosupo/Program/dcomp/source/dcomp/array.d */
// module dcomp.array;
T[N] fixed(T, size_t N)(T[N] a) {return a;}
struct FastAppender(A, size_t MIN = 4) {
import std.algorithm : max;
import std.conv;
import std.range.primitives : ElementEncodingType;
import core.stdc.string : memcpy;
private alias T = ElementEncodingType!A;
private T* _data;
private uint len, cap;
@property size_t length() const {return len;}
bool empty() const { return len == 0; }
void reserve(size_t nlen) {
import core.memory : GC;
if (nlen <= cap) return;
void* nx = GC.malloc(nlen * T.sizeof);
cap = nlen.to!uint;
if (len) memcpy(nx, _data, len * T.sizeof);
_data = cast(T*)(nx);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void opOpAssign(string op : "~")(T item) {
if (len == cap) {
reserve(max(MIN, cap*2));
}
_data[len++] = item;
}
void insertBack(T item) {
this ~= item;
}
void removeBack() {
len--;
}
void clear() {
len = 0;
}
ref inout(T) back() inout { assert(len); return _data[len-1]; }
ref inout(T) opIndex(size_t i) inout { return _data[i]; }
T[] data() {
return (_data) ? _data[0..len] : null;
}
}
/* IMPORT /Users/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);
}
}
}
}
import core.bitop : popcnt;
static if (!__traits(compiles, popcnt(ulong.max))) {
public import core.bitop : popcnt;
int popcnt(ulong v) {
return popcnt(cast(uint)(v)) + popcnt(cast(uint)(v>>32));
}
}
bool poppar(ulong v) {
v^=v>>1;
v^=v>>2;
v&=0x1111111111111111UL;
v*=0x1111111111111111UL;
return ((v>>60) & 1) != 0;
}
/*
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, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n, k; readV(n, k);
writeln(k*(k-1)^^(n-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(s.count('o') * 100 + 700);
}
|
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 w = readln.chomp;
int[char] cnt;
foreach (e; w) {
cnt[e]++;
}
auto f = false;
foreach (k; cnt.keys) {
if (cnt[k] % 2) {
f = true;
}
}
if (f) writeln("No");
else writeln("Yes");
}
|
D
|
import std.stdio,std.math,std.string,std.conv,std.typecons,std.format;
import std.algorithm,std.range,std.array;
T[] readarr(T=long)(){return readln.chomp.split.to!(T[]);}
void scan(T...)(ref T args){auto input=readln.chomp.split;foreach(i,t;T)args[i]=input[i].to!t;}
//END OF TEMPLATE
void main(){
ulong n,k;
scan(n,k);
ulong x;
if(n<k)
x=min(n,k-n);
else if(k==n)
x=0;
else{
x=min(n%k,abs((n%k).to!long-k.to!long));
}
x.writeln;
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.ascii;
void main()
{
auto n = readln.chomp.map!(a=>a-'0').array;
auto res = n.sum;
int lsum;
foreach (i; 0..n.length) {
if (i == 0) {
if (n[i] != 1) {
lsum += n[i] - 1;
}
} else {
lsum += 9;
}
}
max(res, lsum).writeln;
}
|
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 calc(long[] a) {
long[long] freq;
foreach (x; a) freq[x]++;
long sum = 0;
foreach (v; freq.values) {
sum += v * (v - 1) / 2;
}
foreach (x; a) {
long v = freq[x];
long prev = v * (v - 1) / 2;
long curr = (v - 1) * (v - 2) / 2;
writeln(sum - prev + curr);
}
}
void main() {
readint;
auto a = reads!long;
calc(a);
}
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, std.string, std.array, std.conv;
void main() {
int n = readln.chomp.to!int;
int[] a = readln.chomp.split.to!(int[]);
int q = readln.chomp.to!int;
int[] m = readln.chomp.split.to!(int[]);
bool[] check = new bool[2001];
foreach (i; 0 .. 1<<n) {
int sum = 0;
foreach (j; 0 .. n) {
if (i & 1<<j) sum += a[j];
}
check[sum] = true;
}
foreach (x; m) {
writeln(check[x] ? "yes" : "no");
}
}
|
D
|
import std.stdio, std.string, std.array, std.conv, std.range;
void main() {
int[] tmp = readln.chomp.split.to!(int[]);
int n = tmp[0], k = tmp[1], q = tmp[2];
int[] points = repeat(k-q, n).array;
foreach (i; 0 .. q) {
int a = readln.chomp.to!int - 1;
++points[a];
}
foreach (x; points) {
writeln(x > 0 ? "Yes" : "No");
}
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.conv;
import std.range;
int cnt(int x) {
x = x - ((x >>> 1) & 0x55555555);
x = (x & 0x33333333) + ((x >>> 2) & 0x33333333);
x = (x + (x >>> 4)) & 0x0f0f0f0f;
x = x + (x >>> 8);
x = x + (x >>> 16);
return x & 0x3f;
}
void main() {
int m, n;
scanf("%d %d\n", &n, &m);
auto bs = new int[m];
foreach(i; 0..m) {
auto l = readln.chomp.split.to!(int[]);
l = l[1..$];
auto add = 1;
auto sum = 0;
foreach(x; l) {
sum |= add << (x-1);
}
bs[i] = sum;
}
int r;
auto p = readln.chomp.split.to!(int[]);
foreach(x; 0..2^^n) {
bool acc = true;
foreach(i; 0..bs.length) {
acc &= cnt(x & bs[i]) % 2 == p[i];
}
if (acc) ++r;
}
r.write;
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
void main()
{
auto N = lread();
auto D = new long[](N);
foreach (i; 0 .. N)
{
auto d = lread();
D[i] = d;
}
// writeln(D);
auto D_so = D.sort();
// writeln(D_so);
long cnt;
foreach (i; 0 .. N - 1)
{
if (D[i] == D[i + 1])
{
continue;
}
cnt += 1;
}
writeln(cnt + 1);
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
|
D
|
import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;
import std.numeric;
import std.bigint;
import core.checkedint;
void main()
{
int[] arr;
foreach (i; 0..10) {
arr ~= readln.chomp.to!int;
}
std.algorithm.sort(arr);
foreach (v; arr.retro[0..3]) {
v.writeln;
}
}
void tie(R, Args...)(R arr, ref Args args)
if (isRandomAccessRange!R || isArray!R)
in
{
assert (arr.length == args.length);
}
body
{
foreach (i, ref v; args) {
alias T = typeof(v);
v = arr[i].to!T;
}
}
void verbose(Args...)(in Args args)
{
stderr.write("[");
foreach (i, ref v; args) {
if (i) stderr.write(", ");
stderr.write(v);
}
stderr.writeln("]");
}
|
D
|
void main() {
problem();
}
void problem() {
auto N = scan!int;
auto L = scan!long(N);
long solve() {
long ans;
foreach(x; 0..N-2) {
auto lx = L[x];
foreach(y; x+1..N-1) {
auto ly = L[y];
if (lx == ly) continue;
foreach(z; y+1..N) {
auto lz = L[z];
if (lx == lz || ly == lz) continue;
if (lx+ly > lz && ly+lz > lx && lz+lx > ly) {
[lx,ly,lz].deb;
ans++;
}
}
}
}
return ans;
}
solve().writeln;
}
// ----------------------------------------------
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
// -----------------------------------------------
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.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;
if (n % 2)
{
--n;
auto x = n / 2;
auto d = x % 2 ? 2 : 1;
ans[ti] = [x-d, x+d, 1];
}
else
{
ans[ti] = [n/2, n/2-1, 1];
}
}
foreach (e; ans)
writeln(e[0], " ", e[1], " ", e[2]);
stdout.flush;
debug readln;
}
|
D
|
void main()
{
long n, s;
rdVals(n, s);
long[] a = rdRow;
long[][] dp = new long[][](n+1, s+1);
foreach (i; 0 .. n+1) dp[i][0] = 1;
long result;
foreach (i, x; a)
{
foreach (j; 0 .. s+1)
{
dp[i+1][j] = (dp[i+1][j] + dp[i][j]) % mod;
if (j - x < 0) continue;
dp[i+1][j] = (dp[i+1][j] + dp[i][j-x]) % mod;
}
result = (result + dp[i+1][s]) % mod;
}
result.writeln;
}
enum long mod = 998244353;
//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.conv,std.string,std.stdio;
void main(){
immutable x=readln().chomp().to!int();
writeln(x*x*x);
}
|
D
|
import std.conv, std.functional, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, 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)); }
int N;
string S;
void main() {
try {
for (; ; ) {
N = readInt();
S = readToken();
const cntR = S.count('R');
const cntB = S.count('B');
writeln((cntR > cntB) ? "Yes" : "No");
}
} catch (EOFException e) {
}
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto x = readln.chomp.split.to!(int[]);
auto s = readln.chomp;
auto ab = x[1] + x[2];
auto b = x[2];
foreach (e; s) {
auto f = false;
if (e == 'a' && ab) {
ab--;
f = true;
} else if (e == 'b' && ab && b) {
ab--;
b--;
f = true;
}
writeln(f ? "Yes" : "No");
}
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
alias 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 main()
{
long a,b;
scan(a,b);
if(a <= 5)
writeln(0);
else if(a <= 12)
writeln(b / 2);
else
writeln(b);
}
|
D
|
import std.stdio, std.string, std.conv, std.bigint, std.typecons, std.algorithm, std.array, std.math, std.range, std.functional;
void main() {
auto tmp = readln.split.to!(int[]);
writeln(abs(tmp[0] - tmp[1]) < abs(tmp[0] - tmp[2]) ? "A" : "B");
}
|
D
|
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container, std.typecons;
import std.numeric, std.math;
import core.bitop;
T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T)(in string str) { return str.split.to!(T[]); }
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 mod = pow(10, 9) + 7;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
struct UnionFind
{
void init(long n) { par = new long[](n); foreach (i; 0..n) par[i] = i; }
long root(long i) { return par[i] == i ? i : (par[i] = root(par[i])); }
bool same(long i, long j) { return root(i) == root(j); }
void unite(long i, long j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; }
long[] par;
}
void main()
{
auto H = RD!long;
auto W = RD!long;
string[] s;
foreach (i; 0..H)
{
s ~= RD!string;
}
long[3][] open;
open ~= [0, 0, 1];
auto close = new long[][](H, W);
long[2][4] dir = [[-1, 0], [1, 0], [0, -1], [0, 1]];
long cost = -1;
while (!open.empty)
{
auto node = open.front;
open.popFront;
if (node[0] == H - 1 && node[1] == W - 1)
{
cost = node[2];
break;
}
foreach (d; dir)
{
auto y = node[0] + d[0];
auto x = node[1] + d[1];
if (!INSIDE(y, 0, H) || !INSIDE(x, 0, W)) continue;
if (close[y][x] == 1 || s[y][x] == '#') continue;
open ~= [y, x, node[2] + 1];
close[y][x] = 1;
}
}
if (cost == -1)
{
writeln("-1");
}
else
{
long ans;
foreach (e; s)
{
foreach (ee; e)
{
if (ee == '.') ++ans;
}
}
ans -= cost;
writeln(ans);
}
stdout.flush();
}
|
D
|
// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
int n, q, com, x, y;
void main() {
scan(n, q);
auto sg = SegTree(n);
while (q--) {
scan(com, x, y);
if (com == 0) {
sg.update(x, y);
}
else {
int ans = sg.getmin(x, y + 1);
writeln(ans);
}
}
}
struct SegTree {
private {
int N = 1;
int[] data;
immutable inf = int.max;
}
this(int n) {
while (N < n) N *= 2;
data = new int[](2*N - 1);
data[] = inf;
}
void update(int i, int x) {
i += N - 1;
data[i] = x;
while (i > 0) {
i = (i - 1) / 2;
data[i] = min(data[2*i + 1], data[2*i + 2]);
}
}
int getmin(int a, int b, int k = 0, int l = 0, int r = -1) {
if (r < 0) r = N;
if (b <= l || r <= a) return inf;
if (a <= l && r <= b) return data[k];
int v1 = getmin(a, b, 2*k + 1, l, l + (r - l) / 2);
int v2 = getmin(a, b, 2*k + 2, l + (r - l) / 2, r);
return min(v1, v2);
}
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
void main()
{
long k = rdElem;
long a, b;
rdVals(a, b);
foreach (i; a .. b+1)
{
if (i % k == 0)
{
"OK".writeln;
return;
}
}
"NG".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.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
auto nm = readints;
int n = nm[0], m = nm[1];
auto xs = readints;
auto uf = new UnionFind(n + 1);
for (int i = 0; i < m; i++) {
auto xy = readints;
uf.unite(xs[xy[0] - 1], xs[xy[1] - 1]);
}
int x = 0;
foreach (i; 1 .. n + 1) {
if (!uf.isSame(xs[i - 1], i)) {
x++;
}
}
writeln(n - x);
}
class UnionFind {
private int[] _data;
private int[] _nexts; // 次の要素。なければ -1
private int[] _tails; // 末尾の要素
this(int n) {
_data = new int[](n + 1);
_nexts = new int[](n + 1);
_tails = new int[](n + 1);
_data[] = -1;
_nexts[] = -1;
for (int i = 0; i < _tails.length; i++)
_tails[i] = i;
}
int root(int a) {
if (_data[a] < 0) return a;
return _data[a] = root(_data[a]);
}
bool unite(int a, int b) {
int ra = root(a);
int rb = root(b);
if (ra == rb) return false;
// ra に rb を繋げる
_data[ra] += _data[rb];
_data[rb] = ra;
// ra の末尾の後ろに rb を繋げる
_nexts[_tails[ra]] = rb;
// ra の末尾が rb の末尾になる
_tails[ra] = _tails[rb];
return true;
}
bool isSame(int a, int b) {
return root(a) == root(b);
}
/// a が属する集合のサイズ
int groupSize(int a) {
return -_data[root(a)];
}
/// a が属する集合の要素全て
void doEach(int a, void delegate(int) fn) {
auto node = root(a);
while (node != -1) {
fn(node);
node = _nexts[node];
}
}
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long[] x = new long[](2);
x[0] = lread();
x[1] = lread();
x.sort();
if (x == [1, 2])
writeln(3);
if (x == [1, 3])
writeln(2);
if (x == [2, 3])
writeln(1);
}
|
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;
auto nCoin(int m) {
int ans;
while(m >= 500) {
m -= 500;
++ans;
}
while(m >= 100) {
m -= 100;
++ans;
}
while(m >= 50) {
m -= 50;
++ans;
}
while(m >= 10) {
m -= 10;
++ans;
}
return ans;
}
void main(){
int t;
while(true) {
auto n = readInt();
if(n == 0) return;
if(t > 0) {
writeln();
}
++t;
auto m = readInts();
auto k = new int[](4);
auto l = m[0] * 10 + m[1] * 50 + m[2] * 100 + m[3] * 500 - n;
k[3] = l/500;
l %= 500;
k[2] = l/100;
l %= 100;
k[1] = l/50;
l %= 50;
k[0] = l/10;
auto c = [10, 50, 100, 500];
for(int i; i < 4; ++i) {
if(m[i] > k[i]) {
writeln(c[i], " ", m[i]-k[i]);
}
}
}
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.